celtdec: port to audiodecoder
authorMark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Wed, 16 Nov 2011 18:18:50 +0000 (19:18 +0100)
committerMark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Wed, 16 Nov 2011 18:19:15 +0000 (19:19 +0100)
ext/celt/gstceltdec.c
ext/celt/gstceltdec.h

index 6d3d943..0813a26 100644 (file)
@@ -68,29 +68,15 @@ GST_STATIC_PAD_TEMPLATE ("sink",
     GST_STATIC_CAPS ("audio/x-celt")
     );
 
-GST_BOILERPLATE (GstCeltDec, gst_celt_dec, GstElement, GST_TYPE_ELEMENT);
-
-static gboolean celt_dec_sink_event (GstPad * pad, GstEvent * event);
-static GstFlowReturn celt_dec_chain (GstPad * pad, GstBuffer * buf);
-static gboolean celt_dec_sink_setcaps (GstPad * pad, GstCaps * caps);
-static GstStateChangeReturn celt_dec_change_state (GstElement * element,
-    GstStateChange transition);
-
-static gboolean celt_dec_src_event (GstPad * pad, GstEvent * event);
-static gboolean celt_dec_src_query (GstPad * pad, GstQuery * query);
-static gboolean celt_dec_sink_query (GstPad * pad, GstQuery * query);
-static const GstQueryType *celt_get_src_query_types (GstPad * pad);
-static const GstQueryType *celt_get_sink_query_types (GstPad * pad);
-static gboolean celt_dec_convert (GstPad * pad,
-    GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value);
-
-static GstFlowReturn celt_dec_chain_parse_data (GstCeltDec * dec,
-    GstBuffer * buf, GstClockTime timestamp, GstClockTime duration);
-static GstFlowReturn celt_dec_chain_parse_header (GstCeltDec * dec,
-    GstBuffer * buf);
-static GstFlowReturn celt_dec_chain_parse_comments (GstCeltDec * dec,
-    GstBuffer * buf);
+GST_BOILERPLATE (GstCeltDec, gst_celt_dec, GstAudioDecoder,
+    GST_TYPE_AUDIO_DECODER);
+
+static gboolean gst_celt_dec_start (GstAudioDecoder * dec);
+static gboolean gst_celt_dec_stop (GstAudioDecoder * dec);
+static gboolean gst_celt_dec_set_format (GstAudioDecoder * bdec,
+    GstCaps * caps);
+static GstFlowReturn gst_celt_dec_handle_frame (GstAudioDecoder * dec,
+    GstBuffer * buffer);
 
 static void
 gst_celt_dec_base_init (gpointer g_class)
@@ -110,11 +96,14 @@ gst_celt_dec_base_init (gpointer g_class)
 static void
 gst_celt_dec_class_init (GstCeltDecClass * klass)
 {
-  GstElementClass *gstelement_class;
+  GstAudioDecoderClass *gstbase_class;
 
-  gstelement_class = (GstElementClass *) klass;
+  gstbase_class = (GstAudioDecoderClass *) klass;
 
-  gstelement_class->change_state = GST_DEBUG_FUNCPTR (celt_dec_change_state);
+  gstbase_class->start = GST_DEBUG_FUNCPTR (gst_celt_dec_start);
+  gstbase_class->stop = GST_DEBUG_FUNCPTR (gst_celt_dec_stop);
+  gstbase_class->set_format = GST_DEBUG_FUNCPTR (gst_celt_dec_set_format);
+  gstbase_class->handle_frame = GST_DEBUG_FUNCPTR (gst_celt_dec_handle_frame);
 
   GST_DEBUG_CATEGORY_INIT (celtdec_debug, "celtdec", 0,
       "celt decoding element");
@@ -123,11 +112,8 @@ gst_celt_dec_class_init (GstCeltDecClass * klass)
 static void
 gst_celt_dec_reset (GstCeltDec * dec)
 {
-  gst_segment_init (&dec->segment, GST_FORMAT_UNDEFINED);
-  dec->granulepos = -1;
   dec->packetno = 0;
   dec->frame_size = 0;
-  dec->frame_duration = 0;
   if (dec->state) {
     celt_decoder_destroy (dec->state);
     dec->state = NULL;
@@ -150,411 +136,36 @@ gst_celt_dec_reset (GstCeltDec * dec)
 static void
 gst_celt_dec_init (GstCeltDec * dec, GstCeltDecClass * g_class)
 {
-  dec->sinkpad =
-      gst_pad_new_from_static_template (&celt_dec_sink_factory, "sink");
-  gst_pad_set_chain_function (dec->sinkpad, GST_DEBUG_FUNCPTR (celt_dec_chain));
-  gst_pad_set_event_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (celt_dec_sink_event));
-  gst_pad_set_query_type_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (celt_get_sink_query_types));
-  gst_pad_set_query_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (celt_dec_sink_query));
-  gst_pad_set_setcaps_function (dec->sinkpad,
-      GST_DEBUG_FUNCPTR (celt_dec_sink_setcaps));
-  gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
-
-  dec->srcpad = gst_pad_new_from_static_template (&celt_dec_src_factory, "src");
-  gst_pad_use_fixed_caps (dec->srcpad);
-  gst_pad_set_event_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (celt_dec_src_event));
-  gst_pad_set_query_type_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (celt_get_src_query_types));
-  gst_pad_set_query_function (dec->srcpad,
-      GST_DEBUG_FUNCPTR (celt_dec_src_query));
-  gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
-
   gst_celt_dec_reset (dec);
 }
 
 static gboolean
-celt_dec_sink_setcaps (GstPad * pad, GstCaps * caps)
-{
-  GstCeltDec *dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-  gboolean ret = TRUE;
-  GstStructure *s;
-  const GValue *streamheader;
-
-  s = gst_caps_get_structure (caps, 0);
-  if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
-      G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
-      gst_value_array_get_size (streamheader) >= 2) {
-    const GValue *header, *vorbiscomment;
-    GstBuffer *buf;
-    GstFlowReturn res = GST_FLOW_OK;
-
-    header = gst_value_array_get_value (streamheader, 0);
-    if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
-      buf = gst_value_get_buffer (header);
-      res = celt_dec_chain_parse_header (dec, buf);
-      if (res != GST_FLOW_OK)
-        goto done;
-      gst_buffer_replace (&dec->streamheader, buf);
-    }
-
-    vorbiscomment = gst_value_array_get_value (streamheader, 1);
-    if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
-      buf = gst_value_get_buffer (vorbiscomment);
-      res = celt_dec_chain_parse_comments (dec, buf);
-      if (res != GST_FLOW_OK)
-        goto done;
-      gst_buffer_replace (&dec->vorbiscomment, buf);
-    }
-
-    g_list_foreach (dec->extra_headers, (GFunc) gst_mini_object_unref, NULL);
-    g_list_free (dec->extra_headers);
-    dec->extra_headers = NULL;
-
-    if (gst_value_array_get_size (streamheader) > 2) {
-      gint i, n;
-
-      n = gst_value_array_get_size (streamheader);
-      for (i = 2; i < n; i++) {
-        header = gst_value_array_get_value (streamheader, i);
-        buf = gst_value_get_buffer (header);
-        dec->extra_headers =
-            g_list_prepend (dec->extra_headers, gst_buffer_ref (buf));
-      }
-    }
-  }
-
-done:
-  gst_object_unref (dec);
-  return ret;
-}
-
-static gboolean
-celt_dec_convert (GstPad * pad,
-    GstFormat src_format, gint64 src_value,
-    GstFormat * dest_format, gint64 * dest_value)
+gst_celt_dec_start (GstAudioDecoder * dec)
 {
-  gboolean res = TRUE;
-  GstCeltDec *dec;
-  guint64 scale = 1;
-
-  dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
-  if (dec->packetno < 1) {
-    res = FALSE;
-    goto cleanup;
-  }
-
-  if (src_format == *dest_format) {
-    *dest_value = src_value;
-    res = TRUE;
-    goto cleanup;
-  }
-
-  if (pad == dec->sinkpad &&
-      (src_format == GST_FORMAT_BYTES || *dest_format == GST_FORMAT_BYTES)) {
-    res = FALSE;
-    goto cleanup;
-  }
-
-  switch (src_format) {
-    case GST_FORMAT_TIME:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          scale = sizeof (gint16) * dec->header.nb_channels;
-        case GST_FORMAT_DEFAULT:
-          *dest_value =
-              gst_util_uint64_scale_int (scale * src_value,
-              dec->header.sample_rate, GST_SECOND);
-          break;
-        default:
-          res = FALSE;
-          break;
-      }
-      break;
-    case GST_FORMAT_DEFAULT:
-      switch (*dest_format) {
-        case GST_FORMAT_BYTES:
-          *dest_value = src_value * sizeof (gint16) * dec->header.nb_channels;
-          break;
-        case GST_FORMAT_TIME:
-          *dest_value =
-              gst_util_uint64_scale_int (src_value, GST_SECOND,
-              dec->header.sample_rate);
-          break;
-        default:
-          res = FALSE;
-          break;
-      }
-      break;
-    case GST_FORMAT_BYTES:
-      switch (*dest_format) {
-        case GST_FORMAT_DEFAULT:
-          *dest_value = src_value / (sizeof (gint16) * dec->header.nb_channels);
-          break;
-        case GST_FORMAT_TIME:
-          *dest_value = gst_util_uint64_scale_int (src_value, GST_SECOND,
-              dec->header.sample_rate * sizeof (gint16) *
-              dec->header.nb_channels);
-          break;
-        default:
-          res = FALSE;
-          break;
-      }
-      break;
-    default:
-      res = FALSE;
-      break;
-  }
-
-cleanup:
-  gst_object_unref (dec);
-  return res;
-}
-
-static const GstQueryType *
-celt_get_sink_query_types (GstPad * pad)
-{
-  static const GstQueryType celt_dec_sink_query_types[] = {
-    GST_QUERY_CONVERT,
-    0
-  };
-
-  return celt_dec_sink_query_types;
-}
-
-static gboolean
-celt_dec_sink_query (GstPad * pad, GstQuery * query)
-{
-  GstCeltDec *dec;
-  gboolean res;
-
-  dec = GST_CELT_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);
-      res = celt_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
-      if (res) {
-        gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
-      }
-      break;
-    }
-    default:
-      res = gst_pad_query_default (pad, query);
-      break;
-  }
-
-  gst_object_unref (dec);
-  return res;
-}
-
-static const GstQueryType *
-celt_get_src_query_types (GstPad * pad)
-{
-  static const GstQueryType celt_dec_src_query_types[] = {
-    GST_QUERY_POSITION,
-    GST_QUERY_DURATION,
-    0
-  };
-
-  return celt_dec_src_query_types;
-}
-
-static gboolean
-celt_dec_src_query (GstPad * pad, GstQuery * query)
-{
-  GstCeltDec *dec;
-  gboolean res = FALSE;
-
-  dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
-  switch (GST_QUERY_TYPE (query)) {
-    case GST_QUERY_POSITION:{
-      GstSegment segment;
-      GstFormat format;
-      gint64 cur;
-
-      gst_query_parse_position (query, &format, NULL);
-
-      GST_PAD_STREAM_LOCK (dec->sinkpad);
-      segment = dec->segment;
-      GST_PAD_STREAM_UNLOCK (dec->sinkpad);
-
-      if (segment.format != GST_FORMAT_TIME) {
-        GST_DEBUG_OBJECT (dec, "segment not initialised yet");
-        break;
-      }
-
-      if ((res = celt_dec_convert (dec->srcpad, GST_FORMAT_TIME,
-                  segment.last_stop, &format, &cur))) {
-        gst_query_set_position (query, format, cur);
-      }
-      break;
-    }
-    case GST_QUERY_DURATION:{
-      GstFormat format = GST_FORMAT_TIME;
-      gint64 dur;
-
-      /* get duration from demuxer */
-      if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &dur))
-        break;
-
-      gst_query_parse_duration (query, &format, NULL);
-
-      /* and convert it into the requested format */
-      if ((res = celt_dec_convert (dec->srcpad, GST_FORMAT_TIME,
-                  dur, &format, &dur))) {
-        gst_query_set_duration (query, format, dur);
-      }
-      break;
-    }
-    default:
-      res = gst_pad_query_default (pad, query);
-      break;
-  }
+  GstCeltDec *cd = GST_CELT_DEC (dec);
 
-  gst_object_unref (dec);
-  return res;
-}
+  GST_DEBUG_OBJECT (dec, "start");
+  gst_celt_dec_reset (cd);
 
-static gboolean
-celt_dec_src_event (GstPad * pad, GstEvent * event)
-{
-  gboolean res = FALSE;
-  GstCeltDec *dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
-  GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
-
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_SEEK:{
-      GstFormat format, tformat;
-      gdouble rate;
-      GstEvent *real_seek;
-      GstSeekFlags flags;
-      GstSeekType cur_type, stop_type;
-      gint64 cur, stop;
-      gint64 tcur, tstop;
-
-      gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
-          &stop_type, &stop);
-
-      /* we have to ask our peer to seek to time here as we know
-       * nothing about how to generate a granulepos from the src
-       * formats or anything.
-       *
-       * First bring the requested format to time
-       */
-      tformat = GST_FORMAT_TIME;
-      if (!(res = celt_dec_convert (pad, format, cur, &tformat, &tcur)))
-        break;
-      if (!(res = celt_dec_convert (pad, format, stop, &tformat, &tstop)))
-        break;
-
-      /* then seek with time on the peer */
-      real_seek = gst_event_new_seek (rate, GST_FORMAT_TIME,
-          flags, cur_type, tcur, stop_type, tstop);
-
-      GST_LOG_OBJECT (dec, "seek to %" GST_TIME_FORMAT, GST_TIME_ARGS (tcur));
-
-      res = gst_pad_push_event (dec->sinkpad, real_seek);
-      gst_event_unref (event);
-      break;
-    }
-    default:
-      res = gst_pad_event_default (pad, event);
-      break;
-  }
+  /* we know about concealment */
+  gst_audio_decoder_set_plc_aware (dec, TRUE);
 
-  gst_object_unref (dec);
-  return res;
+  return TRUE;
 }
 
 static gboolean
-celt_dec_sink_event (GstPad * pad, GstEvent * event)
+gst_celt_dec_stop (GstAudioDecoder * dec)
 {
-  GstCeltDec *dec;
-  gboolean ret = FALSE;
-
-  dec = GST_CELT_DEC (gst_pad_get_parent (pad));
-
-  GST_LOG_OBJECT (dec, "handling %s event", GST_EVENT_TYPE_NAME (event));
-
-  switch (GST_EVENT_TYPE (event)) {
-    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);
+  GstCeltDec *cd = GST_CELT_DEC (dec);
 
-      if (format != GST_FORMAT_TIME)
-        goto newseg_wrong_format;
-
-      if (rate <= 0.0)
-        goto newseg_wrong_rate;
-
-      if (update) {
-        /* time progressed without data, see if we can fill the gap with
-         * some concealment data */
-        if (dec->segment.last_stop < start) {
-          GstClockTime duration;
-
-          duration = start - dec->segment.last_stop;
-          celt_dec_chain_parse_data (dec, NULL, dec->segment.last_stop,
-              duration);
-        }
-      }
-
-      /* now configure the values */
-      gst_segment_set_newsegment_full (&dec->segment, update,
-          rate, arate, GST_FORMAT_TIME, start, stop, time);
-
-      dec->granulepos = -1;
-
-      GST_DEBUG_OBJECT (dec, "segment now: cur = %" GST_TIME_FORMAT " [%"
-          GST_TIME_FORMAT " - %" GST_TIME_FORMAT "]",
-          GST_TIME_ARGS (dec->segment.last_stop),
-          GST_TIME_ARGS (dec->segment.start),
-          GST_TIME_ARGS (dec->segment.stop));
-
-      ret = gst_pad_push_event (dec->srcpad, event);
-      break;
-    }
-    default:
-      ret = gst_pad_event_default (pad, event);
-      break;
-  }
-
-  gst_object_unref (dec);
-  return ret;
+  GST_DEBUG_OBJECT (dec, "stop");
+  gst_celt_dec_reset (cd);
 
-  /* ERRORS */
-newseg_wrong_format:
-  {
-    GST_DEBUG_OBJECT (dec, "received non TIME newsegment");
-    gst_object_unref (dec);
-    return FALSE;
-  }
-newseg_wrong_rate:
-  {
-    GST_DEBUG_OBJECT (dec, "negative rates not supported yet");
-    gst_object_unref (dec);
-    return FALSE;
-  }
+  return TRUE;
 }
 
 static GstFlowReturn
-celt_dec_chain_parse_header (GstCeltDec * dec, GstBuffer * buf)
+gst_celt_dec_parse_header (GstCeltDec * dec, GstBuffer * buf)
 {
   GstCaps *caps;
   gint error = CELT_OK;
@@ -601,9 +212,6 @@ celt_dec_chain_parse_header (GstCeltDec * dec, GstBuffer * buf)
   celt_mode_info (dec->mode, CELT_GET_FRAME_SIZE, &dec->frame_size);
 #endif
 
-  dec->frame_duration = gst_util_uint64_scale_int (dec->frame_size,
-      GST_SECOND, dec->header.sample_rate);
-
   /* set caps */
   caps = gst_caps_new_simple ("audio/x-raw-int",
       "rate", G_TYPE_INT, dec->header.sample_rate,
@@ -615,7 +223,7 @@ celt_dec_chain_parse_header (GstCeltDec * dec, GstBuffer * buf)
   GST_DEBUG_OBJECT (dec, "rate=%d channels=%d frame-size=%d",
       dec->header.sample_rate, dec->header.nb_channels, dec->frame_size);
 
-  if (!gst_pad_set_caps (dec->srcpad, caps))
+  if (!gst_pad_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec), caps))
     goto nego_failed;
 
   gst_caps_unref (caps);
@@ -655,7 +263,7 @@ nego_failed:
 }
 
 static GstFlowReturn
-celt_dec_chain_parse_comments (GstCeltDec * dec, GstBuffer * buf)
+gst_celt_dec_parse_comments (GstCeltDec * dec, GstBuffer * buf)
 {
   GstTagList *list;
   gchar *ver, *encoder = NULL;
@@ -690,7 +298,8 @@ celt_dec_chain_parse_comments (GstCeltDec * dec, GstBuffer * buf)
 
   GST_INFO_OBJECT (dec, "tags: %" GST_PTR_FORMAT, list);
 
-  gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, list);
+  gst_element_found_tags_for_pad (GST_ELEMENT (dec),
+      GST_AUDIO_DECODER_SRC_PAD (dec), list);
 
   g_free (encoder);
   g_free (ver);
@@ -699,8 +308,7 @@ celt_dec_chain_parse_comments (GstCeltDec * dec, GstBuffer * buf)
 }
 
 static GstFlowReturn
-celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
-    GstClockTime timestamp, GstClockTime duration)
+gst_celt_dec_parse_data (GstCeltDec * dec, GstBuffer * buf)
 {
   GstFlowReturn res = GST_FLOW_OK;
   gint size;
@@ -710,33 +318,23 @@ celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
   gint error = CELT_OK;
   int skip = 0;
 
-  if (timestamp != -1) {
-    dec->segment.last_stop = timestamp;
-    dec->granulepos = -1;
-  }
+  if (!dec->frame_size)
+    goto not_negotiated;
 
-  if (buf) {
+  if (G_LIKELY (GST_BUFFER_SIZE (buf))) {
     data = GST_BUFFER_DATA (buf);
     size = GST_BUFFER_SIZE (buf);
-
-    GST_DEBUG_OBJECT (dec, "received buffer of size %u", size);
-    if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)
-        && GST_BUFFER_OFFSET_END_IS_VALID (buf)) {
-      dec->granulepos = GST_BUFFER_OFFSET_END (buf);
-      GST_DEBUG_OBJECT (dec,
-          "Taking granulepos from upstream: %" G_GUINT64_FORMAT,
-          dec->granulepos);
-    }
-
-    /* copy timestamp */
   } else {
+    /* FIXME ? actually consider how much concealment is needed */
     /* concealment data, pass NULL as the bits parameters */
     GST_DEBUG_OBJECT (dec, "creating concealment data");
     data = NULL;
     size = 0;
   }
 
-  if (dec->discont) {
+  /* FIXME really needed ?; this might lead to skipping samples below
+   * which kind of messes with subsequent timestamping */
+  if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))) {
 #ifdef CELT_GET_LOOKAHEAD_REQUEST
     /* what will be 0.11.5, I guess, but no versioning yet in git */
     celt_decoder_ctl (dec->state, CELT_GET_LOOKAHEAD_REQUEST, &skip);
@@ -745,9 +343,9 @@ celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
 #endif
   }
 
-  res = gst_pad_alloc_buffer_and_set_caps (dec->srcpad,
+  res = gst_pad_alloc_buffer_and_set_caps (GST_AUDIO_DECODER_SRC_PAD (dec),
       GST_BUFFER_OFFSET_NONE, dec->frame_size * dec->header.nb_channels * 2,
-      GST_PAD_CAPS (dec->srcpad), &outbuf);
+      GST_PAD_CAPS (GST_AUDIO_DECODER_SRC_PAD (dec)), &outbuf);
 
   if (res != GST_FLOW_OK) {
     GST_DEBUG_OBJECT (dec, "buf alloc flow: %s", gst_flow_get_name (res));
@@ -780,59 +378,88 @@ celt_dec_chain_parse_data (GstCeltDec * dec, GstBuffer * buf,
         skip * dec->header.nb_channels * 2;
   }
 
-  if (dec->granulepos == -1) {
-    if (dec->segment.format != GST_FORMAT_TIME) {
-      GST_WARNING_OBJECT (dec, "segment not initialized or not TIME format");
-      dec->granulepos = dec->frame_size;
-    } else {
-      dec->granulepos = gst_util_uint64_scale_int (dec->segment.last_stop,
-          dec->header.sample_rate, GST_SECOND) + dec->frame_size;
-    }
-    GST_DEBUG_OBJECT (dec, "granulepos=%" G_GINT64_FORMAT, dec->granulepos);
-  }
+  res = gst_audio_decoder_finish_frame (GST_AUDIO_DECODER (dec), outbuf, 1);
 
-  if (!GST_CLOCK_TIME_IS_VALID (timestamp))
-    timestamp = gst_util_uint64_scale_int (dec->granulepos - dec->frame_size,
-        GST_SECOND, dec->header.sample_rate);
+  if (res != GST_FLOW_OK)
+    GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
 
-  GST_DEBUG_OBJECT (dec, "timestamp=%" GST_TIME_FORMAT,
-      GST_TIME_ARGS (timestamp));
+  return res;
 
-  GST_BUFFER_OFFSET (outbuf) = dec->granulepos - dec->frame_size;
-  GST_BUFFER_OFFSET_END (outbuf) = dec->granulepos;
-  GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
-  GST_BUFFER_DURATION (outbuf) = dec->frame_duration;
-  if (dec->discont) {
-    GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
-    dec->discont = 0;
+  /* ERRORS */
+not_negotiated:
+  {
+    GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL),
+        ("decoder not initialized"));
+    return GST_FLOW_NOT_NEGOTIATED;
   }
+}
 
-  dec->granulepos += dec->frame_size;
-  dec->segment.last_stop += dec->frame_duration;
+static gboolean
+gst_celt_dec_set_format (GstAudioDecoder * bdec, GstCaps * caps)
+{
+  GstCeltDec *dec = GST_CELT_DEC (bdec);
+  gboolean ret = TRUE;
+  GstStructure *s;
+  const GValue *streamheader;
 
-  GST_LOG_OBJECT (dec, "pushing buffer with ts=%" GST_TIME_FORMAT ", dur=%"
-      GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
-      GST_TIME_ARGS (dec->frame_duration));
+  s = gst_caps_get_structure (caps, 0);
+  if ((streamheader = gst_structure_get_value (s, "streamheader")) &&
+      G_VALUE_HOLDS (streamheader, GST_TYPE_ARRAY) &&
+      gst_value_array_get_size (streamheader) >= 2) {
+    const GValue *header, *vorbiscomment;
+    GstBuffer *buf;
+    GstFlowReturn res = GST_FLOW_OK;
 
-  res = gst_pad_push (dec->srcpad, outbuf);
+    header = gst_value_array_get_value (streamheader, 0);
+    if (header && G_VALUE_HOLDS (header, GST_TYPE_BUFFER)) {
+      buf = gst_value_get_buffer (header);
+      res = gst_celt_dec_parse_header (dec, buf);
+      if (res != GST_FLOW_OK)
+        goto done;
+      gst_buffer_replace (&dec->streamheader, buf);
+    }
 
-  if (res != GST_FLOW_OK)
-    GST_DEBUG_OBJECT (dec, "flow: %s", gst_flow_get_name (res));
+    vorbiscomment = gst_value_array_get_value (streamheader, 1);
+    if (vorbiscomment && G_VALUE_HOLDS (vorbiscomment, GST_TYPE_BUFFER)) {
+      buf = gst_value_get_buffer (vorbiscomment);
+      res = gst_celt_dec_parse_comments (dec, buf);
+      if (res != GST_FLOW_OK)
+        goto done;
+      gst_buffer_replace (&dec->vorbiscomment, buf);
+    }
 
-  return res;
+    g_list_foreach (dec->extra_headers, (GFunc) gst_mini_object_unref, NULL);
+    g_list_free (dec->extra_headers);
+    dec->extra_headers = NULL;
+
+    if (gst_value_array_get_size (streamheader) > 2) {
+      gint i, n;
+
+      n = gst_value_array_get_size (streamheader);
+      for (i = 2; i < n; i++) {
+        header = gst_value_array_get_value (streamheader, i);
+        buf = gst_value_get_buffer (header);
+        dec->extra_headers =
+            g_list_prepend (dec->extra_headers, gst_buffer_ref (buf));
+      }
+    }
+  }
+
+done:
+  return ret;
 }
 
 static GstFlowReturn
-celt_dec_chain (GstPad * pad, GstBuffer * buf)
+gst_celt_dec_handle_frame (GstAudioDecoder * bdec, GstBuffer * buf)
 {
   GstFlowReturn res;
   GstCeltDec *dec;
 
-  dec = GST_CELT_DEC (gst_pad_get_parent (pad));
+  dec = GST_CELT_DEC (bdec);
 
-  if (GST_BUFFER_IS_DISCONT (buf)) {
-    dec->discont = TRUE;
-  }
+  /* no fancy draining */
+  if (G_UNLIKELY (!buf))
+    return GST_FLOW_OK;
 
   /* If we have the streamheader and vorbiscomment from the caps already
    * ignore them here */
@@ -840,10 +467,14 @@ celt_dec_chain (GstPad * pad, GstBuffer * buf)
     if (GST_BUFFER_SIZE (dec->streamheader) == GST_BUFFER_SIZE (buf)
         && memcmp (GST_BUFFER_DATA (dec->streamheader), GST_BUFFER_DATA (buf),
             GST_BUFFER_SIZE (buf)) == 0) {
+      GST_DEBUG_OBJECT (dec, "found streamheader");
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
       res = GST_FLOW_OK;
     } else if (GST_BUFFER_SIZE (dec->vorbiscomment) == GST_BUFFER_SIZE (buf)
         && memcmp (GST_BUFFER_DATA (dec->vorbiscomment), GST_BUFFER_DATA (buf),
             GST_BUFFER_SIZE (buf)) == 0) {
+      GST_DEBUG_OBJECT (dec, "found vorbiscomments");
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
       res = GST_FLOW_OK;
     } else {
       GList *l;
@@ -853,66 +484,36 @@ celt_dec_chain (GstPad * pad, GstBuffer * buf)
         if (GST_BUFFER_SIZE (header) == GST_BUFFER_SIZE (buf) &&
             memcmp (GST_BUFFER_DATA (header), GST_BUFFER_DATA (buf),
                 GST_BUFFER_SIZE (buf)) == 0) {
+          GST_DEBUG_OBJECT (dec, "found extra header buffer");
+          gst_audio_decoder_finish_frame (bdec, NULL, 1);
           res = GST_FLOW_OK;
           goto done;
         }
       }
-      res =
-          celt_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
-          GST_BUFFER_DURATION (buf));
+      res = gst_celt_dec_parse_data (dec, buf);
     }
   } else {
     /* Otherwise fall back to packet counting and assume that the
      * first two packets are the headers. */
-    if (dec->packetno == 0)
-      res = celt_dec_chain_parse_header (dec, buf);
-    else if (dec->packetno == 1)
-      res = celt_dec_chain_parse_comments (dec, buf);
-    else if (dec->packetno <= 1 + dec->header.extra_headers)
+    if (dec->packetno == 0) {
+      GST_DEBUG_OBJECT (dec, "counted streamheader");
+      res = gst_celt_dec_parse_header (dec, buf);
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
+    } else if (dec->packetno == 1) {
+      GST_DEBUG_OBJECT (dec, "counted vorbiscomments");
+      res = gst_celt_dec_parse_comments (dec, buf);
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
+    } else if (dec->packetno <= 1 + dec->header.extra_headers) {
+      GST_DEBUG_OBJECT (dec, "counted extra header");
+      gst_audio_decoder_finish_frame (bdec, NULL, 1);
       res = GST_FLOW_OK;
-    else
-      res = celt_dec_chain_parse_data (dec, buf, GST_BUFFER_TIMESTAMP (buf),
-          GST_BUFFER_DURATION (buf));
+    } else {
+      res = gst_celt_dec_parse_data (dec, buf);
+    }
   }
 
 done:
   dec->packetno++;
 
-  gst_buffer_unref (buf);
-  gst_object_unref (dec);
-
   return res;
 }
-
-static GstStateChangeReturn
-celt_dec_change_state (GstElement * element, GstStateChange transition)
-{
-  GstStateChangeReturn ret;
-  GstCeltDec *dec = GST_CELT_DEC (element);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_NULL_TO_READY:
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
-    default:
-      break;
-  }
-
-  ret = parent_class->change_state (element, transition);
-  if (ret != GST_STATE_CHANGE_SUCCESS)
-    return ret;
-
-  switch (transition) {
-    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
-      break;
-    case GST_STATE_CHANGE_PAUSED_TO_READY:
-      gst_celt_dec_reset (dec);
-      break;
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      break;
-    default:
-      break;
-  }
-
-  return ret;
-}
index b6b4960..9baf719 100644 (file)
@@ -22,6 +22,7 @@
 #define __GST_CELT_DEC_H__
 
 #include <gst/gst.h>
+#include <gst/audio/gstaudiodecoder.h>
 #include <celt/celt.h>
 #include <celt/celt_header.h>
 
@@ -42,22 +43,15 @@ typedef struct _GstCeltDec GstCeltDec;
 typedef struct _GstCeltDecClass GstCeltDecClass;
 
 struct _GstCeltDec {
-  GstElement            element;
-
-  /* pads */
-  GstPad                *sinkpad;
-  GstPad                *srcpad;
+  GstAudioDecoder       element;
 
   CELTDecoder          *state;
   CELTMode             *mode;
   CELTHeader            header;
 
   gint                  frame_size;
-  GstClockTime          frame_duration;
   guint64               packetno;
 
-  GstSegment            segment;    /* STREAM LOCK */
-  gint64                granulepos; /* -1 = needs to be set from current time */
   gboolean              discont;
 
   GstBuffer            *streamheader;
@@ -66,7 +60,7 @@ struct _GstCeltDec {
 };
 
 struct _GstCeltDecClass {
-  GstElementClass parent_class;
+  GstAudioDecoderClass parent_class;
 };
 
 GType gst_celt_dec_get_type (void);