tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.git] / ext / vorbis / gstvorbisdec.c
index 645c27d..f476d45 100644 (file)
@@ -64,38 +64,26 @@ GST_STATIC_PAD_TEMPLATE ("sink",
     GST_STATIC_CAPS ("audio/x-vorbis")
     );
 
-GST_BOILERPLATE (GST_VORBIS_DEC_GLIB_TYPE_NAME, gst_vorbis_dec, GstElement,
-    GST_TYPE_ELEMENT);
+GST_BOILERPLATE (GstVorbisDec, gst_vorbis_dec, GstAudioDecoder,
+    GST_TYPE_AUDIO_DECODER);
 
 static void vorbis_dec_finalize (GObject * object);
-static gboolean vorbis_dec_sink_event (GstPad * pad, GstEvent * event);
-static GstFlowReturn vorbis_dec_chain (GstPad * pad, GstBuffer * buffer);
-static GstFlowReturn vorbis_dec_chain_forward (GstVorbisDec * vd,
-    gboolean discont, GstBuffer * buffer);
-static GstFlowReturn vorbis_dec_chain_reverse (GstVorbisDec * vd,
-    gboolean discont, GstBuffer * buf);
-static GstStateChangeReturn vorbis_dec_change_state (GstElement * element,
-    GstStateChange transition);
-
-static gboolean vorbis_dec_src_event (GstPad * pad, GstEvent * event);
-static gboolean vorbis_dec_src_query (GstPad * pad, GstQuery * query);
-static gboolean vorbis_dec_convert (GstPad * pad,
-    GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value);
-
-static gboolean vorbis_dec_sink_query (GstPad * pad, GstQuery * query);
+
+static gboolean vorbis_dec_start (GstAudioDecoder * dec);
+static gboolean vorbis_dec_stop (GstAudioDecoder * dec);
+static GstFlowReturn vorbis_dec_handle_frame (GstAudioDecoder * dec,
+    GstBuffer * buffer);
+static void vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard);
 
 static void
 gst_vorbis_dec_base_init (gpointer g_class)
 {
   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
-  GstPadTemplate *src_template, *sink_template;
-
-  src_template = gst_static_pad_template_get (&vorbis_dec_src_factory);
-  gst_element_class_add_pad_template (element_class, src_template);
 
-  sink_template = gst_static_pad_template_get (&vorbis_dec_sink_factory);
-  gst_element_class_add_pad_template (element_class, sink_template);
+  gst_element_class_add_static_pad_template (element_class,
+      &vorbis_dec_src_factory);
+  gst_element_class_add_static_pad_template (element_class,
+      &vorbis_dec_sink_factory);
 
   gst_element_class_set_details_simple (element_class,
       "Vorbis audio decoder", "Codec/Decoder/Audio",
@@ -107,55 +95,19 @@ static void
 gst_vorbis_dec_class_init (GstVorbisDecClass * klass)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
-  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
+  GstAudioDecoderClass *base_class = GST_AUDIO_DECODER_CLASS (klass);
 
   gobject_class->finalize = vorbis_dec_finalize;
 
-  gstelement_class->change_state = GST_DEBUG_FUNCPTR (vorbis_dec_change_state);
-}
-
-static const GstQueryType *
-vorbis_get_query_types (GstPad * pad)
-{
-  static const GstQueryType vorbis_dec_src_query_types[] = {
-    GST_QUERY_POSITION,
-    GST_QUERY_DURATION,
-    GST_QUERY_CONVERT,
-    0
-  };
-
-  return vorbis_dec_src_query_types;
+  base_class->start = GST_DEBUG_FUNCPTR (vorbis_dec_start);
+  base_class->stop = GST_DEBUG_FUNCPTR (vorbis_dec_stop);
+  base_class->handle_frame = GST_DEBUG_FUNCPTR (vorbis_dec_handle_frame);
+  base_class->flush = GST_DEBUG_FUNCPTR (vorbis_dec_flush);
 }
 
 static void
 gst_vorbis_dec_init (GstVorbisDec * dec, GstVorbisDecClass * g_class)
 {
-  dec->sinkpad = gst_pad_new_from_static_template (&vorbis_dec_sink_factory,
-      "sink");
-
-  gst_pad_set_event_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (vorbis_dec_sink_event));
-  gst_pad_set_chain_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (vorbis_dec_chain));
-  gst_pad_set_query_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (vorbis_dec_sink_query));
-  gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
-
-  dec->srcpad = gst_pad_new_from_static_template (&vorbis_dec_src_factory,
-      "src");
-
-  gst_pad_set_event_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (vorbis_dec_src_event));
-  gst_pad_set_query_type_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (vorbis_get_query_types));
-  gst_pad_set_query_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (vorbis_dec_src_query));
-  gst_pad_use_fixed_caps (dec->srcpad);
-  gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
-
-  dec->queued = NULL;
-  dec->pendingevents = NULL;
-  dec->taglist = NULL;
 }
 
 static void
@@ -169,7 +121,6 @@ vorbis_dec_finalize (GObject * object)
 #ifndef USE_TREMOLO
   vorbis_block_clear (&vd->vb);
 #endif
-
   vorbis_dsp_clear (&vd->vd);
   vorbis_comment_clear (&vd->vc);
   vorbis_info_clear (&vd->vi);
@@ -180,232 +131,44 @@ vorbis_dec_finalize (GObject * object)
 static void
 gst_vorbis_dec_reset (GstVorbisDec * dec)
 {
-  dec->last_timestamp = GST_CLOCK_TIME_NONE;
-  dec->discont = TRUE;
-  dec->seqnum = gst_util_seqnum_next ();
-  gst_segment_init (&dec->segment, GST_FORMAT_TIME);
-
-  g_list_foreach (dec->queued, (GFunc) gst_mini_object_unref, NULL);
-  g_list_free (dec->queued);
-  dec->queued = NULL;
-  g_list_foreach (dec->gather, (GFunc) gst_mini_object_unref, NULL);
-  g_list_free (dec->gather);
-  dec->gather = NULL;
-  g_list_foreach (dec->decode, (GFunc) gst_mini_object_unref, NULL);
-  g_list_free (dec->decode);
-  dec->decode = NULL;
-  g_list_foreach (dec->pendingevents, (GFunc) gst_mini_object_unref, NULL);
-  g_list_free (dec->pendingevents);
-  dec->pendingevents = NULL;
-
   if (dec->taglist)
     gst_tag_list_free (dec->taglist);
   dec->taglist = NULL;
 }
 
-
 static gboolean
-vorbis_dec_convert (GstPad * pad,
-    GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value)
+vorbis_dec_start (GstAudioDecoder * dec)
 {
-  gboolean res = TRUE;
-  GstVorbisDec *dec;
-  guint64 scale = 1;
-
-  if (src_format == *dest_format) {
-    *dest_value = src_value;
-    return TRUE;
-  }
+  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
 
-  dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
+  GST_DEBUG_OBJECT (dec, "start");
+  vorbis_info_init (&vd->vi);
+  vorbis_comment_init (&vd->vc);
+  vd->initialized = FALSE;
+  gst_vorbis_dec_reset (vd);
 
-  if (!dec->initialized)
-    goto no_header;
-
-  if (dec->sinkpad == pad &&
-      (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES))
-    goto no_format;
-
-  switch (src_format) {
-    case GST_FORMAT_TIME:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          scale = dec->width * dec->vi.channels;
-        case GST_FORMAT_DEFAULT:
-          *dest_value =
-              scale * gst_util_uint64_scale_int (src_value, dec->vi.rate,
-              GST_SECOND);
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    case GST_FORMAT_DEFAULT:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          *dest_value = src_value * dec->width * dec->vi.channels;
-          break;
-        case GST_FORMAT_TIME:
-          *dest_value =
-              gst_util_uint64_scale_int (src_value, GST_SECOND, dec->vi.rate);
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    case GST_FORMAT_BYTES:
-      switch (*dest_format) {
-        case GST_FORMAT_DEFAULT:
-          *dest_value = src_value / (dec->width * dec->vi.channels);
-          break;
-        case GST_FORMAT_TIME:
-          *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
-              dec->vi.rate * dec->width * dec->vi.channels);
-          break;
-        default:
-          res = FALSE;
-      }
-      break;
-    default:
-      res = FALSE;
-  }
-done:
-  gst_object_unref (dec);
-
-  return res;
-
-  /* ERRORS */
-no_header:
-  {
-    GST_DEBUG_OBJECT (dec, "no header packets received");
-    res = FALSE;
-    goto done;
-  }
-no_format:
-  {
-    GST_DEBUG_OBJECT (dec, "formats unsupported");
-    res = FALSE;
-    goto done;
-  }
+  return TRUE;
 }
 
 static gboolean
-vorbis_dec_src_query (GstPad * pad, GstQuery * query)
+vorbis_dec_stop (GstAudioDecoder * dec)
 {
-  GstVorbisDec *dec;
-  gboolean res = FALSE;
-
-  dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (dec == NULL))
-    return FALSE;
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_POSITION:
-    {
-      gint64 value;
-      GstFormat format;
-      gint64 time;
-
-      gst_query_parse_position (query, &format, NULL);
-
-      /* we start from the last seen time */
-      time = dec->last_timestamp;
-      /* correct for the segment values */
-      time = gst_segment_to_stream_time (&dec->segment, GST_FORMAT_TIME, time);
-
-      GST_LOG_OBJECT (dec,
-          "query %p: our time: %" GST_TIME_FORMAT, query, GST_TIME_ARGS (time));
-
-      /* and convert to the final format */
-      if (!(res =
-              vorbis_dec_convert (pad, GST_FORMAT_TIME, time, &format, &value)))
-        goto error;
-
-      gst_query_set_position (query, format, value);
-
-      GST_LOG_OBJECT (dec,
-          "query %p: we return %" G_GINT64_FORMAT " (format %u)", query, value,
-          format);
-
-      break;
-    }
-    case GST_QUERY_DURATION:
-    {
-      res = gst_pad_peer_query (dec->sinkpad, query);
-      if (!res)
-        goto error;
-
-      break;
-    }
-    case GST_QUERY_CONVERT:
-    {
-      GstFormat src_fmt, dest_fmt;
-      gint64 src_val, dest_val;
-
-      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      if (!(res =
-              vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
-        goto error;
-      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
-      break;
-    }
-    default:
-      res = gst_pad_query_default (pad, query);
-      break;
-  }
-done:
-  gst_object_unref (dec);
-
-  return res;
-
-  /* ERRORS */
-error:
-  {
-    GST_WARNING_OBJECT (dec, "error handling query");
-    goto done;
-  }
-}
+  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
 
-static gboolean
-vorbis_dec_sink_query (GstPad * pad, GstQuery * query)
-{
-  GstVorbisDec *dec;
-  gboolean res;
-
-  dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_CONVERT:
-    {
-      GstFormat src_fmt, dest_fmt;
-      gint64 src_val, dest_val;
-
-      gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      if (!(res =
-              vorbis_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val)))
-        goto error;
-      gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
-      break;
-    }
-    default:
-      res = gst_pad_query_default (pad, query);
-      break;
-  }
-
-done:
-  gst_object_unref (dec);
-
-  return res;
+  GST_DEBUG_OBJECT (dec, "stop");
+  vd->initialized = FALSE;
+#ifndef USE_TREMOLO
+  vorbis_block_clear (&vd->vb);
+#endif
+  vorbis_dsp_clear (&vd->vd);
+  vorbis_comment_clear (&vd->vc);
+  vorbis_info_clear (&vd->vi);
+  gst_vorbis_dec_reset (vd);
 
-  /* ERRORS */
-error:
-  {
-    GST_DEBUG_OBJECT (dec, "error converting value");
-    goto done;
-  }
+  return TRUE;
 }
 
+#if 0
 static gboolean
 vorbis_dec_src_event (GstPad * pad, GstEvent * event)
 {
@@ -413,10 +176,6 @@ vorbis_dec_src_event (GstPad * pad, GstEvent * event)
   GstVorbisDec *dec;
 
   dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
-  if (G_UNLIKELY (dec == NULL)) {
-    gst_event_unref (event);
-    return FALSE;
-  }
 
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_SEEK:
@@ -466,96 +225,7 @@ convert_error:
     goto done;
   }
 }
-
-static gboolean
-vorbis_dec_sink_event (GstPad * pad, GstEvent * event)
-{
-  gboolean ret = FALSE;
-  GstVorbisDec *dec;
-
-  dec = GST_VORBIS_DEC (gst_pad_get_parent (pad));
-
-  GST_LOG_OBJECT (dec, "handling event");
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_EOS:
-      if (dec->segment.rate < 0.0)
-        vorbis_dec_chain_reverse (dec, TRUE, NULL);
-      ret = gst_pad_push_event (dec->srcpad, event);
-      break;
-    case GST_EVENT_FLUSH_START:
-      ret = gst_pad_push_event (dec->srcpad, event);
-      break;
-    case GST_EVENT_FLUSH_STOP:
-      /* here we must clean any state in the decoder */
-#ifdef HAVE_VORBIS_SYNTHESIS_RESTART
-      vorbis_synthesis_restart (&dec->vd);
 #endif
-      gst_vorbis_dec_reset (dec);
-      ret = gst_pad_push_event (dec->srcpad, event);
-      break;
-    case GST_EVENT_NEWSEGMENT:
-    {
-      GstFormat format;
-      gdouble rate, arate;
-      gint64 start, stop, time;
-      gboolean update;
-
-      gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
-          &start, &stop, &time);
-
-      /* we need time for now */
-      if (format != GST_FORMAT_TIME)
-        goto newseg_wrong_format;
-
-      GST_DEBUG_OBJECT (dec,
-          "newsegment: update %d, rate %g, arate %g, start %" GST_TIME_FORMAT
-          ", stop %" GST_TIME_FORMAT ", time %" GST_TIME_FORMAT,
-          update, rate, arate, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
-          GST_TIME_ARGS (time));
-
-      /* now configure the values */
-      gst_segment_set_newsegment_full (&dec->segment, update,
-          rate, arate, format, start, stop, time);
-      dec->seqnum = gst_event_get_seqnum (event);
-
-      if (dec->initialized)
-        /* and forward */
-        ret = gst_pad_push_event (dec->srcpad, event);
-      else {
-        /* store it to send once we're initialized */
-        dec->pendingevents = g_list_append (dec->pendingevents, event);
-        ret = TRUE;
-      }
-      break;
-    }
-    case GST_EVENT_TAG:
-    {
-      if (dec->initialized)
-        /* and forward */
-        ret = gst_pad_push_event (dec->srcpad, event);
-      else {
-        /* store it to send once we're initialized */
-        dec->pendingevents = g_list_append (dec->pendingevents, event);
-        ret = TRUE;
-      }
-      break;
-    }
-    default:
-      ret = gst_pad_push_event (dec->srcpad, event);
-      break;
-  }
-done:
-  gst_object_unref (dec);
-
-  return ret;
-
-  /* ERRORS */
-newseg_wrong_format:
-  {
-    GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
-    goto done;
-  }
-}
 
 static GstFlowReturn
 vorbis_handle_identification_packet (GstVorbisDec * vd)
@@ -593,7 +263,7 @@ vorbis_handle_identification_packet (GstVorbisDec * vd)
   }
 
   /* negotiate width with downstream */
-  caps = gst_pad_get_allowed_caps (vd->srcpad);
+  caps = gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (vd));
   if (caps) {
     if (!gst_caps_is_empty (caps)) {
       GstStructure *s;
@@ -613,10 +283,11 @@ vorbis_handle_identification_packet (GstVorbisDec * vd)
    * for mono/stereo and avoid the depth switch in tremor case */
   vd->copy_samples = get_copy_sample_func (vd->vi.channels, vd->width);
 
-  caps = gst_caps_copy (gst_pad_get_pad_template_caps (vd->srcpad));
-  gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate,
-      "channels", G_TYPE_INT, vd->vi.channels,
-      "width", G_TYPE_INT, width, NULL);
+  caps =
+      gst_caps_copy (gst_pad_get_pad_template_caps
+      (GST_AUDIO_DECODER_SRC_PAD (vd)));
+  gst_caps_set_simple (caps, "rate", G_TYPE_INT, vd->vi.rate, "channels",
+      G_TYPE_INT, vd->vi.channels, "width", G_TYPE_INT, width, NULL);
 
   if (pos) {
     gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
@@ -626,7 +297,7 @@ vorbis_handle_identification_packet (GstVorbisDec * vd)
     g_free ((GstAudioChannelPosition *) pos);
   }
 
-  gst_pad_set_caps (vd->srcpad, caps);
+  gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (vd), caps);
   gst_caps_unref (caps);
 
   return GST_FLOW_OK;
@@ -694,8 +365,8 @@ vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
   }
 
   if (vd->initialized) {
-    gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd), vd->srcpad,
-        vd->taglist);
+    gst_element_found_tags_for_pad (GST_ELEMENT_CAST (vd),
+        GST_AUDIO_DECODER_SRC_PAD (vd), vd->taglist);
     vd->taglist = NULL;
   } else {
     /* Only post them as messages for the time being. *
@@ -710,7 +381,6 @@ vorbis_handle_comment_packet (GstVorbisDec * vd, ogg_packet * packet)
 static GstFlowReturn
 vorbis_handle_type_packet (GstVorbisDec * vd)
 {
-  GList *walk;
   gint res;
 
   g_assert (vd->initialized == FALSE);
@@ -728,16 +398,10 @@ vorbis_handle_type_packet (GstVorbisDec * vd)
 
   vd->initialized = TRUE;
 
-  if (vd->pendingevents) {
-    for (walk = vd->pendingevents; walk; walk = g_list_next (walk))
-      gst_pad_push_event (vd->srcpad, GST_EVENT_CAST (walk->data));
-    g_list_free (vd->pendingevents);
-    vd->pendingevents = NULL;
-  }
-
   if (vd->taglist) {
     /* The tags have already been sent on the bus as messages. */
-    gst_pad_push_event (vd->srcpad, gst_event_new_tag (vd->taglist));
+    gst_pad_push_event (GST_AUDIO_DECODER_SRC_PAD (vd),
+        gst_event_new_tag (vd->taglist));
     vd->taglist = NULL;
   }
   return GST_FLOW_OK;
@@ -791,6 +455,7 @@ vorbis_handle_header_packet (GstVorbisDec * vd, ogg_packet * packet)
       res = GST_FLOW_OK;
       break;
   }
+
   return res;
 
   /* ERRORS */
@@ -803,72 +468,67 @@ header_read_error:
 }
 
 static GstFlowReturn
-vorbis_dec_push_forward (GstVorbisDec * dec, GstBuffer * buf)
+vorbis_dec_handle_header_buffer (GstVorbisDec * vd, GstBuffer * buffer)
 {
-  GstFlowReturn result;
-
-  /* clip */
-  if (!(buf = gst_audio_buffer_clip (buf, &dec->segment, dec->vi.rate,
-              dec->vi.channels * dec->width))) {
-    GST_LOG_OBJECT (dec, "clipped buffer");
-    return GST_FLOW_OK;
-  }
-
-  if (dec->discont) {
-    GST_LOG_OBJECT (dec, "setting DISCONT");
-    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
-    dec->discont = FALSE;
-  }
-
-  GST_DEBUG_OBJECT (dec,
-      "pushing time %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
-      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
-      GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
+  ogg_packet *packet;
+  ogg_packet_wrapper packet_wrapper;
 
-  result = gst_pad_push (dec->srcpad, buf);
+  gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
+  packet = gst_ogg_packet_from_wrapper (&packet_wrapper);
 
-  return result;
+  return vorbis_handle_header_packet (vd, packet);
 }
 
+#define MIN_NUM_HEADERS 3
 static GstFlowReturn
-vorbis_dec_push_reverse (GstVorbisDec * dec, GstBuffer * buf)
+vorbis_dec_handle_header_caps (GstVorbisDec * vd)
 {
   GstFlowReturn result = GST_FLOW_OK;
+  GstCaps *caps;
+  GstStructure *s = NULL;
+  const GValue *array = NULL;
+
+  caps = GST_PAD_CAPS (GST_AUDIO_DECODER_SINK_PAD (vd));
+  if (caps)
+    s = gst_caps_get_structure (caps, 0);
+  if (s)
+    array = gst_structure_get_value (s, "streamheader");
+
+  if (array && (gst_value_array_get_size (array) >= MIN_NUM_HEADERS)) {
+    const GValue *value = NULL;
+    GstBuffer *buf = NULL;
+    gint i = 0;
+
+    while (result == GST_FLOW_OK && i < gst_value_array_get_size (array)) {
+      value = gst_value_array_get_value (array, i);
+      buf = gst_value_get_buffer (value);
+      if (!buf)
+        goto null_buffer;
+      result = vorbis_dec_handle_header_buffer (vd, buf);
+      i++;
+    }
+  } else
+    goto array_error;
 
-  dec->queued = g_list_prepend (dec->queued, buf);
-
-  return result;
-}
+done:
+  return (result != GST_FLOW_OK ? GST_FLOW_NOT_NEGOTIATED : GST_FLOW_OK);
 
-static void
-vorbis_do_timestamps (GstVorbisDec * vd, GstBuffer * buf, gboolean reverse,
-    GstClockTime timestamp, GstClockTime duration)
-{
-  /* interpolate reverse */
-  if (vd->last_timestamp != -1 && duration != -1 && reverse)
-    vd->last_timestamp -= duration;
-
-  /* take buffer timestamp, use interpolated timestamp otherwise */
-  if (timestamp != -1)
-    vd->last_timestamp = timestamp;
-  else
-    timestamp = vd->last_timestamp;
-
-  /* interpolate forwards */
-  if (vd->last_timestamp != -1 && duration != -1 && !reverse)
-    vd->last_timestamp += duration;
-
-  GST_LOG_OBJECT (vd,
-      "keeping timestamp %" GST_TIME_FORMAT " ts %" GST_TIME_FORMAT " dur %"
-      GST_TIME_FORMAT, GST_TIME_ARGS (vd->last_timestamp),
-      GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
-
-  if (buf) {
-    GST_BUFFER_TIMESTAMP (buf) = timestamp;
-    GST_BUFFER_DURATION (buf) = duration;
+  /* ERRORS */
+array_error:
+  {
+    GST_WARNING_OBJECT (vd, "streamheader array not found");
+    result = GST_FLOW_ERROR;
+    goto done;
+  }
+null_buffer:
+  {
+    GST_WARNING_OBJECT (vd, "streamheader with null buffer received");
+    result = GST_FLOW_ERROR;
+    goto done;
   }
 }
 
+
 static GstFlowReturn
 vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
     GstClockTime timestamp, GstClockTime duration)
@@ -883,13 +543,16 @@ vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
   GstFlowReturn result;
   gint size;
 
-  if (G_UNLIKELY (!vd->initialized))
-    goto not_initialized;
+  if (G_UNLIKELY (!vd->initialized)) {
+    result = vorbis_dec_handle_header_caps (vd);
+    if (result != GST_FLOW_OK)
+      goto not_initialized;
+  }
 
   /* normal data packet */
   /* FIXME, we can skip decoding if the packet is outside of the
    * segment, this is however not very trivial as we need a previous
-   * packet to decode the current one so we must be carefull not to
+   * packet to decode the current one so we must be careful not to
    * throw away too much. For now we decode everything and clip right
    * before pushing data. */
 
@@ -912,8 +575,8 @@ vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
   if ((sample_count = vorbis_dsp_pcmout (&vd->vd, NULL, 0)) == 0)
 #else
   if ((sample_count = vorbis_synthesis_pcmout (&vd->vd, NULL)) == 0)
-#endif
     goto done;
+#endif
 
   size = sample_count * vd->vi.channels * vd->width;
   GST_LOG_OBJECT (vd, "%d samples ready for reading, size %d", sample_count,
@@ -921,17 +584,19 @@ vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
 
   /* alloc buffer for it */
   result =
-      gst_pad_alloc_buffer_and_set_caps (vd->srcpad, GST_BUFFER_OFFSET_NONE,
-      size, GST_PAD_CAPS (vd->srcpad), &out);
+      gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (vd),
+      GST_BUFFER_OFFSET_NONE, size,
+      GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (vd)), &out);
   if (G_UNLIKELY (result != GST_FLOW_OK))
     goto done;
 
   /* get samples ready for reading now, should be sample_count */
 #ifdef USE_TREMOLO
   pcm = GST_BUFFER_DATA (out);
-  if (G_UNLIKELY ((vorbis_dsp_pcmout (&vd->vd, pcm, sample_count)) != sample_count))
+  if (G_UNLIKELY (vorbis_dsp_pcmout (&vd->vd, pcm, sample_count) !=
+          sample_count))
 #else
-  if (G_UNLIKELY ((vorbis_synthesis_pcmout (&vd->vd, &pcm)) != sample_count))
+  if (G_UNLIKELY (vorbis_synthesis_pcmout (&vd->vd, &pcm) != sample_count))
 #endif
     goto wrong_samples;
 
@@ -944,22 +609,9 @@ vorbis_handle_data_packet (GstVorbisDec * vd, ogg_packet * packet,
   GST_LOG_OBJECT (vd, "setting output size to %d", size);
   GST_BUFFER_SIZE (out) = size;
 
-  /* this should not overflow */
-  if (duration == -1)
-    duration = sample_count * GST_SECOND / vd->vi.rate;
-
-  vorbis_do_timestamps (vd, out, FALSE, timestamp, duration);
-
-  if (vd->segment.rate >= 0.0)
-    result = vorbis_dec_push_forward (vd, out);
-  else
-    result = vorbis_dec_push_reverse (vd, out);
-
 done:
-  if (out == NULL) {
-    /* no output, still keep track of timestamps */
-    vorbis_do_timestamps (vd, NULL, FALSE, timestamp, duration);
-  }
+  /* whether or not data produced, consume one frame and advance time */
+  result = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), out, 1);
 
 #ifdef USE_TREMOLO
   vorbis_dsp_read (&vd->vd, sample_count);
@@ -974,7 +626,7 @@ not_initialized:
   {
     GST_ELEMENT_ERROR (GST_ELEMENT (vd), STREAM, DECODE,
         (NULL), ("no header sent yet"));
-    return GST_FLOW_ERROR;
+    return GST_FLOW_NOT_NEGOTIATED;
   }
 could_not_read:
   {
@@ -998,11 +650,16 @@ wrong_samples:
 }
 
 static GstFlowReturn
-vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
+vorbis_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
 {
   ogg_packet *packet;
   ogg_packet_wrapper packet_wrapper;
   GstFlowReturn result = GST_FLOW_OK;
+  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
+
+  /* no draining etc */
+  if (G_UNLIKELY (!buffer))
+    return GST_FLOW_OK;
 
   /* make ogg_packet out of the buffer */
   gst_ogg_packet_wrapper_from_buffer (&packet_wrapper, buffer);
@@ -1031,6 +688,8 @@ vorbis_dec_decode_buffer (GstVorbisDec * vd, GstBuffer * buffer)
       goto done;
     }
     result = vorbis_handle_header_packet (vd, packet);
+    /* consumer header packet/frame */
+    gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (vd), NULL, 1);
   } else {
     GstClockTime timestamp, duration;
 
@@ -1057,245 +716,19 @@ empty_header:
   {
     GST_ELEMENT_ERROR (vd, STREAM, DECODE, (NULL), ("empty header received"));
     result = GST_FLOW_ERROR;
-    vd->discont = TRUE;
     goto done;
   }
 }
 
-/*
- * Input:
- *  Buffer decoding order:  7  8  9  4  5  6  3  1  2  EOS
- *  Discont flag:           D        D        D  D
- *
- * - Each Discont marks a discont in the decoding order.
- *
- * for vorbis, each buffer is a keyframe when we have the previous
- * buffer. This means that to decode buffer 7, we need buffer 6, which
- * arrives out of order.
- *
- * we first gather buffers in the gather queue until we get a DISCONT. We
- * prepend each incomming buffer so that they are in reversed order.
- *
- *    gather queue:    9  8  7
- *    decode queue:
- *    output queue:
- *
- * When a DISCONT is received (buffer 4), we move the gather queue to the
- * decode queue. This is simply done be taking the head of the gather queue
- * and prepending it to the decode queue. This yields:
- *
- *    gather queue:
- *    decode queue:    7  8  9
- *    output queue:
- *
- * Then we decode each buffer in the decode queue in order and put the output
- * buffer in the output queue. The first buffer (7) will not produce any output
- * because it needs the previous buffer (6) which did not arrive yet. This
- * yields:
- *
- *    gather queue:
- *    decode queue:    7  8  9
- *    output queue:    9  8
- *
- * Then we remove the consumed buffers from the decode queue. Buffer 7 is not
- * completely consumed, we need to keep it around for when we receive buffer
- * 6. This yields:
- *
- *    gather queue:
- *    decode queue:    7
- *    output queue:    9  8
- *
- * Then we accumulate more buffers:
- *
- *    gather queue:    6  5  4
- *    decode queue:    7
- *    output queue:
- *
- * prepending to the decode queue on DISCONT yields:
- *
- *    gather queue:
- *    decode queue:    4  5  6  7
- *    output queue:
- *
- * after decoding and keeping buffer 4:
- *
- *    gather queue:
- *    decode queue:    4
- *    output queue:    7  6  5
- *
- * Etc..
- */
-static GstFlowReturn
-vorbis_dec_flush_decode (GstVorbisDec * dec)
-{
-  GstFlowReturn res = GST_FLOW_OK;
-  GList *walk;
-
-  walk = dec->decode;
-
-  GST_DEBUG_OBJECT (dec, "flushing buffers to decoder");
-
-  while (walk) {
-    GList *next;
-    GstBuffer *buf = GST_BUFFER_CAST (walk->data);
-
-    GST_DEBUG_OBJECT (dec, "decoding buffer %p, ts %" GST_TIME_FORMAT,
-        buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
-
-    next = g_list_next (walk);
-
-    /* decode buffer, prepend to output queue */
-    res = vorbis_dec_decode_buffer (dec, buf);
-
-    /* if we generated output, we can discard the buffer, else we
-     * keep it in the queue */
-    if (dec->queued) {
-      GST_DEBUG_OBJECT (dec, "decoded buffer to %p", dec->queued->data);
-      dec->decode = g_list_delete_link (dec->decode, walk);
-      gst_buffer_unref (buf);
-    } else {
-      GST_DEBUG_OBJECT (dec, "buffer did not decode, keeping");
-    }
-    walk = next;
-  }
-  while (dec->queued) {
-    GstBuffer *buf = GST_BUFFER_CAST (dec->queued->data);
-    GstClockTime timestamp, duration;
-
-    timestamp = GST_BUFFER_TIMESTAMP (buf);
-    duration = GST_BUFFER_DURATION (buf);
-
-    vorbis_do_timestamps (dec, buf, TRUE, timestamp, duration);
-    res = vorbis_dec_push_forward (dec, buf);
-
-    dec->queued = g_list_delete_link (dec->queued, dec->queued);
-  }
-  return res;
-}
-
-static GstFlowReturn
-vorbis_dec_chain_reverse (GstVorbisDec * vd, gboolean discont, GstBuffer * buf)
-{
-  GstFlowReturn result = GST_FLOW_OK;
-
-  /* if we have a discont, move buffers to the decode list */
-  if (G_UNLIKELY (discont)) {
-    GST_DEBUG_OBJECT (vd, "received discont");
-    while (vd->gather) {
-      GstBuffer *gbuf;
-
-      gbuf = GST_BUFFER_CAST (vd->gather->data);
-      /* remove from the gather list */
-      vd->gather = g_list_delete_link (vd->gather, vd->gather);
-      /* copy to decode queue */
-      vd->decode = g_list_prepend (vd->decode, gbuf);
-    }
-    /* flush and decode the decode queue */
-    result = vorbis_dec_flush_decode (vd);
-  }
-
-  if (G_LIKELY (buf)) {
-    GST_DEBUG_OBJECT (vd,
-        "gathering buffer %p of size %u, time %" GST_TIME_FORMAT
-        ", dur %" GST_TIME_FORMAT, buf, GST_BUFFER_SIZE (buf),
-        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
-        GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
-
-    /* add buffer to gather queue */
-    vd->gather = g_list_prepend (vd->gather, buf);
-  }
-
-  return result;
-}
-
-static GstFlowReturn
-vorbis_dec_chain_forward (GstVorbisDec * vd, gboolean discont,
-    GstBuffer * buffer)
-{
-  GstFlowReturn result;
-
-  result = vorbis_dec_decode_buffer (vd, buffer);
-
-  gst_buffer_unref (buffer);
-
-  return result;
-}
-
-static GstFlowReturn
-vorbis_dec_chain (GstPad * pad, GstBuffer * buffer)
+static void
+vorbis_dec_flush (GstAudioDecoder * dec, gboolean hard)
 {
-  GstVorbisDec *vd;
-  GstFlowReturn result = GST_FLOW_OK;
-  gboolean discont;
-
-  vd = GST_VORBIS_DEC (gst_pad_get_parent (pad));
-
-  discont = GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT);
+  GstVorbisDec *vd = GST_VORBIS_DEC (dec);
 
-  /* resync on DISCONT */
-  if (G_UNLIKELY (discont)) {
-    GST_DEBUG_OBJECT (vd, "received DISCONT buffer");
-    vd->last_timestamp = GST_CLOCK_TIME_NONE;
 #ifdef HAVE_VORBIS_SYNTHESIS_RESTART
-    vorbis_synthesis_restart (&vd->vd);
-#endif
-    vd->discont = TRUE;
-  }
-
-  if (vd->segment.rate >= 0.0)
-    result = vorbis_dec_chain_forward (vd, discont, buffer);
-  else
-    result = vorbis_dec_chain_reverse (vd, discont, buffer);
-
-  gst_object_unref (vd);
-
-  return result;
-}
-
-static GstStateChangeReturn
-vorbis_dec_change_state (GstElement * element, GstStateChange transition)
-{
-  GstVorbisDec *vd = GST_VORBIS_DEC (element);
-  GstStateChangeReturn res;
-
-  switch (transition) {
-    case GST_STATE_CHANGE_NULL_TO_READY:
-      break;
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-      vorbis_info_init (&vd->vi);
-      vorbis_comment_init (&vd->vc);
-      vd->initialized = FALSE;
-      gst_vorbis_dec_reset (vd);
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-      break;
-    default:
-      break;
-  }
-
-  res = parent_class->change_state (element, transition);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_READY:
-      GST_DEBUG_OBJECT (vd, "PAUSED -> READY, clearing vorbis structures");
-      vd->initialized = FALSE;
-
-#ifndef USE_TREMOLO
-      vorbis_block_clear (&vd->vb);
+  vorbis_synthesis_restart (&vd->vd);
 #endif
 
-      vorbis_dsp_clear (&vd->vd);
-      vorbis_comment_clear (&vd->vc);
-      vorbis_info_clear (&vd->vi);
-      gst_vorbis_dec_reset (vd);
-      break;
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      break;
-    default:
-      break;
-  }
-
-  return res;
+  if (hard)
+    gst_vorbis_dec_reset (vd);
 }