lame: port to audioencoder
authorMark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Fri, 23 Sep 2011 13:26:48 +0000 (15:26 +0200)
committerMark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
Mon, 26 Sep 2011 13:17:04 +0000 (15:17 +0200)
ext/lame/gstlame.c
ext/lame/gstlame.h

index b4aecf9..36e4087 100644 (file)
@@ -21,7 +21,7 @@
 
 /**
  * SECTION:element-lame
- * @see_also: lamemp3enc, mad, vorbisenc
+ * @see_also: lame, mad, vorbisenc
  *
  * This element encodes raw integer audio into an MPEG-1 layer 3 (MP3) stream.
  * Note that <ulink url="http://en.wikipedia.org/wiki/MP3">MP3</ulink> is not
@@ -31,7 +31,7 @@
  *
  * <refsect2>
  * <title>Note</title>
- * This element is deprecated, use the lamemp3enc element instead
+ * This element is deprecated, use the lame element instead
  * which provides a much simpler interface and results in better MP3 files.
  * </refsect2>
  *
@@ -309,15 +309,19 @@ static void gst_lame_base_init (gpointer g_class);
 static void gst_lame_class_init (GstLameClass * klass);
 static void gst_lame_init (GstLame * gst_lame);
 
+static gboolean gst_lame_start (GstAudioEncoder * enc);
+static gboolean gst_lame_stop (GstAudioEncoder * enc);
+static gboolean gst_lame_set_format (GstAudioEncoder * enc,
+    GstAudioInfo * info);
+static GstFlowReturn gst_lame_handle_frame (GstAudioEncoder * enc,
+    GstBuffer * in_buf);
+static void gst_lame_flush (GstAudioEncoder * enc);
+
 static void gst_lame_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec);
 static void gst_lame_get_property (GObject * object, guint prop_id,
     GValue * value, GParamSpec * pspec);
-static gboolean gst_lame_sink_event (GstPad * pad, GstEvent * event);
-static GstFlowReturn gst_lame_chain (GstPad * pad, GstBuffer * buf);
 static gboolean gst_lame_setup (GstLame * lame);
-static GstStateChangeReturn gst_lame_change_state (GstElement * element,
-    GstStateChange transition);
 
 static GstElementClass *parent_class = NULL;
 
@@ -352,7 +356,8 @@ gst_lame_get_type (void)
     };
 
     gst_lame_type =
-        g_type_register_static (GST_TYPE_ELEMENT, "GstLame", &gst_lame_info, 0);
+        g_type_register_static (GST_TYPE_AUDIO_ENCODER, "GstLame",
+        &gst_lame_info, 0);
     g_type_add_interface_static (gst_lame_type, GST_TYPE_TAG_SETTER,
         &tag_setter_info);
     g_type_add_interface_static (gst_lame_type, GST_TYPE_PRESET, &preset_info);
@@ -397,9 +402,11 @@ gst_lame_class_init (GstLameClass * klass)
 {
   GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
+  GstAudioEncoderClass *base_class;
 
   gobject_class = (GObjectClass *) klass;
   gstelement_class = (GstElementClass *) klass;
+  base_class = (GstAudioEncoderClass *) klass;
 
   parent_class = g_type_class_peek_parent (klass);
 
@@ -407,6 +414,12 @@ gst_lame_class_init (GstLameClass * klass)
   gobject_class->get_property = gst_lame_get_property;
   gobject_class->finalize = gst_lame_finalize;
 
+  base_class->start = GST_DEBUG_FUNCPTR (gst_lame_start);
+  base_class->stop = GST_DEBUG_FUNCPTR (gst_lame_stop);
+  base_class->set_format = GST_DEBUG_FUNCPTR (gst_lame_set_format);
+  base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_lame_handle_frame);
+  base_class->flush = GST_DEBUG_FUNCPTR (gst_lame_flush);
+
   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
       g_param_spec_int ("bitrate", "Bitrate (kb/s)",
           "Bitrate in kbit/sec (8, 16, 24, 32, 40, 48, 56, 64, 80, 96, "
@@ -565,39 +578,30 @@ gst_lame_class_init (GstLameClass * klass)
           GST_TYPE_LAME_PRESET, gst_lame_default_settings.preset,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 #endif
-
-  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_lame_change_state);
-}
-
-static gboolean
-gst_lame_src_setcaps (GstPad * pad, GstCaps * caps)
-{
-  GST_DEBUG_OBJECT (pad, "caps: %" GST_PTR_FORMAT, caps);
-  return TRUE;
 }
 
 static gboolean
-gst_lame_sink_setcaps (GstPad * pad, GstCaps * caps)
+gst_lame_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
 {
   GstLame *lame;
   gint out_samplerate;
   gint version;
-  GstStructure *structure;
   GstCaps *othercaps;
+  GstClockTime latency;
+
+  lame = GST_LAME (enc);
 
-  lame = GST_LAME (GST_PAD_PARENT (pad));
-  structure = gst_caps_get_structure (caps, 0);
+  /* parameters already parsed for us */
+  lame->samplerate = GST_AUDIO_INFO_RATE (info);
+  lame->num_channels = GST_AUDIO_INFO_CHANNELS (info);
 
-  if (!gst_structure_get_int (structure, "rate", &lame->samplerate))
-    goto no_rate;
-  if (!gst_structure_get_int (structure, "channels", &lame->num_channels))
-    goto no_channels;
+  /* but we might be asked to reconfigure, so reset */
+  gst_lame_release_memory (lame);
 
   GST_DEBUG_OBJECT (lame, "setting up lame");
   if (!gst_lame_setup (lame))
     goto setup_failed;
 
-
   out_samplerate = lame_get_out_samplerate (lame->lgf);
   if (out_samplerate == 0)
     goto zero_output_rate;
@@ -624,21 +628,18 @@ gst_lame_sink_setcaps (GstPad * pad, GstCaps * caps)
       "rate", G_TYPE_INT, out_samplerate, NULL);
 
   /* and use these caps */
-  gst_pad_set_caps (lame->srcpad, othercaps);
+  gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (lame), othercaps);
   gst_caps_unref (othercaps);
 
+  /* base class feedback:
+   * - we will handle buffers, just hand us all available
+   * - report latency */
+  latency = gst_util_uint64_scale_int (lame_get_framesize (lame->lgf),
+      GST_SECOND, lame->samplerate);
+  gst_audio_encoder_set_latency (enc, latency, latency);
+
   return TRUE;
 
-no_rate:
-  {
-    GST_ERROR_OBJECT (lame, "input caps have no sample rate field");
-    return FALSE;
-  }
-no_channels:
-  {
-    GST_ERROR_OBJECT (lame, "input caps have no channels field");
-    return FALSE;
-  }
 zero_output_rate:
   {
     GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS, (NULL),
@@ -658,26 +659,6 @@ gst_lame_init (GstLame * lame)
 {
   GST_DEBUG_OBJECT (lame, "starting initialization");
 
-  lame->sinkpad =
-      gst_pad_new_from_static_template (&gst_lame_sink_template, "sink");
-  gst_pad_set_event_function (lame->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_lame_sink_event));
-  gst_pad_set_chain_function (lame->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_lame_chain));
-  gst_pad_set_setcaps_function (lame->sinkpad,
-      GST_DEBUG_FUNCPTR (gst_lame_sink_setcaps));
-  gst_element_add_pad (GST_ELEMENT (lame), lame->sinkpad);
-
-  lame->srcpad =
-      gst_pad_new_from_static_template (&gst_lame_src_template, "src");
-  gst_pad_set_setcaps_function (lame->srcpad,
-      GST_DEBUG_FUNCPTR (gst_lame_src_setcaps));
-  gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
-
-  lame->samplerate = 44100;
-  lame->num_channels = 2;
-  lame->setup = FALSE;
-
   /* Set default settings */
   lame->bitrate = gst_lame_default_settings.bitrate;
   lame->compression_ratio = gst_lame_default_settings.compression_ratio;
@@ -714,6 +695,27 @@ gst_lame_init (GstLame * lame)
   GST_DEBUG_OBJECT (lame, "done initializing");
 }
 
+static gboolean
+gst_lame_start (GstAudioEncoder * enc)
+{
+  GstLame *lame = GST_LAME (enc);
+
+  GST_DEBUG_OBJECT (lame, "start");
+  return TRUE;
+}
+
+static gboolean
+gst_lame_stop (GstAudioEncoder * enc)
+{
+  GstLame *lame = GST_LAME (enc);
+
+  GST_DEBUG_OBJECT (lame, "stop");
+
+  gst_lame_release_memory (lame);
+  return TRUE;
+}
+
+
 /* <php-emulation-mode>three underscores for ___rate is really really really
  * private as opposed to one underscore<php-emulation-mode> */
 /* call this MACRO outside of the NULL state so that we have a higher chance
@@ -979,108 +981,54 @@ gst_lame_get_property (GObject * object, guint prop_id, GValue * value,
   }
 }
 
-static gboolean
-gst_lame_sink_event (GstPad * pad, GstEvent * event)
+static GstFlowReturn
+gst_lame_flush_full (GstLame * lame, gboolean push)
 {
-  gboolean ret;
-  GstLame *lame;
-
-  lame = GST_LAME (gst_pad_get_parent (pad));
-
-  switch (GST_EVENT_TYPE (event)) {
-    case GST_EVENT_EOS:{
-      GST_DEBUG_OBJECT (lame, "handling EOS event");
-
-      if (lame->lgf != NULL) {
-        GstBuffer *buf;
-        gint size;
-
-        buf = gst_buffer_new_and_alloc (7200);
-        size = lame_encode_flush (lame->lgf, GST_BUFFER_DATA (buf), 7200);
-
-        if (size > 0 && lame->last_flow == GST_FLOW_OK) {
-          gint64 duration;
-
-          duration = gst_util_uint64_scale (size, 8 * GST_SECOND,
-              1000 * lame->bitrate);
-
-          if (lame->last_ts == GST_CLOCK_TIME_NONE) {
-            lame->last_ts = lame->eos_ts;
-            lame->last_duration = duration;
-          } else {
-            lame->last_duration += duration;
-          }
-
-          GST_BUFFER_TIMESTAMP (buf) = lame->last_ts;
-          GST_BUFFER_DURATION (buf) = lame->last_duration;
-          lame->last_ts = GST_CLOCK_TIME_NONE;
-          GST_BUFFER_SIZE (buf) = size;
-          GST_DEBUG_OBJECT (lame, "pushing final packet of %u bytes", size);
-          gst_buffer_set_caps (buf, GST_PAD_CAPS (lame->srcpad));
-          gst_pad_push (lame->srcpad, buf);
-        } else {
-          GST_DEBUG_OBJECT (lame, "no final packet (size=%d, last_flow=%s)",
-              size, gst_flow_get_name (lame->last_flow));
-          gst_buffer_unref (buf);
-        }
-      }
-
-      ret = gst_pad_event_default (pad, event);
-      break;
-    }
-    case GST_EVENT_FLUSH_START:
-      GST_DEBUG_OBJECT (lame, "handling FLUSH start event");
-      /* forward event */
-      ret = gst_pad_push_event (lame->srcpad, event);
-      break;
-    case GST_EVENT_FLUSH_STOP:
-    {
-      guchar *mp3_data = NULL;
-      gint mp3_buffer_size;
+  GstBuffer *buf;
+  gint size;
+  GstFlowReturn result = GST_FLOW_OK;
 
-      GST_DEBUG_OBJECT (lame, "handling FLUSH stop event");
+  if (!lame->lgf)
+    return GST_FLOW_OK;
 
-      if (lame->lgf) {
-        /* clear buffers if we already have lame set up */
-        mp3_buffer_size = 7200;
-        mp3_data = g_malloc (mp3_buffer_size);
-        lame_encode_flush (lame->lgf, mp3_data, mp3_buffer_size);
-        g_free (mp3_data);
-      }
+  buf = gst_buffer_new_and_alloc (7200);
+  size = lame_encode_flush (lame->lgf, GST_BUFFER_DATA (buf), 7200);
 
-      ret = gst_pad_push_event (lame->srcpad, event);
-      break;
-    }
-    case GST_EVENT_TAG:
-      GST_DEBUG_OBJECT (lame, "ignoring TAG event, passing it on");
-      ret = gst_pad_push_event (lame->srcpad, event);
-      break;
-    default:
-      ret = gst_pad_event_default (pad, event);
-      break;
+  if (size > 0 && push) {
+    GST_BUFFER_SIZE (buf) = size;
+    GST_DEBUG_OBJECT (lame, "pushing final packet of %u bytes", size);
+    result = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (lame), buf, -1);
+  } else {
+    GST_DEBUG_OBJECT (lame, "no final packet (size=%d, push=%d)", size, push);
+    gst_buffer_unref (buf);
+    result = GST_FLOW_OK;
   }
-  gst_object_unref (lame);
-  return ret;
+  return result;
+}
+
+static void
+gst_lame_flush (GstAudioEncoder * enc)
+{
+  gst_lame_flush_full (GST_LAME (enc), FALSE);
 }
 
 static GstFlowReturn
-gst_lame_chain (GstPad * pad, GstBuffer * buf)
+gst_lame_handle_frame (GstAudioEncoder * enc, GstBuffer * buf)
 {
   GstLame *lame;
   guchar *mp3_data;
+  GstBuffer *mp3_buf;
   gint mp3_buffer_size, mp3_size;
-  gint64 duration;
   GstFlowReturn result;
   gint num_samples;
   guint8 *data;
   guint size;
 
-  lame = GST_LAME (GST_PAD_PARENT (pad));
-
-  GST_LOG_OBJECT (lame, "entered chain");
+  lame = GST_LAME (enc);
 
-  if (!lame->setup)
-    goto not_setup;
+  /* squeeze remaining and push */
+  if (G_UNLIKELY (buf == NULL))
+    return gst_lame_flush_full (lame, TRUE);
 
   data = GST_BUFFER_DATA (buf);
   size = GST_BUFFER_SIZE (buf);
@@ -1089,7 +1037,8 @@ gst_lame_chain (GstPad * pad, GstBuffer * buf)
 
   /* allocate space for output */
   mp3_buffer_size = 1.25 * num_samples + 7200;
-  mp3_data = g_malloc (mp3_buffer_size);
+  mp3_buf = gst_buffer_new_and_alloc (mp3_buffer_size);
+  mp3_data = GST_BUFFER_DATA (mp3_buf);
 
   /* lame seems to be too stupid to get mono interleaved going */
   if (lame->num_channels == 1) {
@@ -1105,69 +1054,23 @@ gst_lame_chain (GstPad * pad, GstBuffer * buf)
   GST_LOG_OBJECT (lame, "encoded %d bytes of audio to %d bytes of mp3",
       size, mp3_size);
 
-  duration = gst_util_uint64_scale_int (size, GST_SECOND,
-      2 * lame->samplerate * lame->num_channels);
-
-  if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE &&
-      GST_BUFFER_DURATION (buf) != duration) {
-    GST_DEBUG_OBJECT (lame, "incoming buffer had incorrect duration %"
-        GST_TIME_FORMAT ", outgoing buffer will have correct duration %"
-        GST_TIME_FORMAT,
-        GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_TIME_ARGS (duration));
-  }
-
-  if (lame->last_ts == GST_CLOCK_TIME_NONE) {
-    lame->last_ts = GST_BUFFER_TIMESTAMP (buf);
-    lame->last_offs = GST_BUFFER_OFFSET (buf);
-    lame->last_duration = duration;
-  } else {
-    lame->last_duration += duration;
-  }
-
-  gst_buffer_unref (buf);
-
   if (mp3_size < 0) {
     g_warning ("error %d", mp3_size);
   }
 
-  if (mp3_size > 0) {
-    GstBuffer *outbuf;
-
-    outbuf = gst_buffer_new ();
-    GST_BUFFER_DATA (outbuf) = mp3_data;
-    GST_BUFFER_MALLOCDATA (outbuf) = mp3_data;
-    GST_BUFFER_SIZE (outbuf) = mp3_size;
-    GST_BUFFER_TIMESTAMP (outbuf) = lame->last_ts;
-    GST_BUFFER_OFFSET (outbuf) = lame->last_offs;
-    GST_BUFFER_DURATION (outbuf) = lame->last_duration;
-    gst_buffer_set_caps (outbuf, GST_PAD_CAPS (lame->srcpad));
-
-    result = gst_pad_push (lame->srcpad, outbuf);
-    lame->last_flow = result;
-    if (result != GST_FLOW_OK) {
-      GST_DEBUG_OBJECT (lame, "flow return: %s", gst_flow_get_name (result));
-    }
-
-    if (GST_CLOCK_TIME_IS_VALID (lame->last_ts))
-      lame->eos_ts = lame->last_ts + lame->last_duration;
-    else
-      lame->eos_ts = GST_CLOCK_TIME_NONE;
-    lame->last_ts = GST_CLOCK_TIME_NONE;
+  if (G_LIKELY (mp3_size > 0)) {
+    GST_BUFFER_SIZE (mp3_buf) = mp3_size;
+    result = gst_audio_encoder_finish_frame (enc, mp3_buf, -1);
   } else {
-    g_free (mp3_data);
+    if (mp3_size < 0) {
+      /* eat error ? */
+      g_warning ("error %d", mp3_size);
+    }
     result = GST_FLOW_OK;
+    gst_buffer_unref (mp3_buf);
   }
 
   return result;
-
-  /* ERRORS */
-not_setup:
-  {
-    gst_buffer_unref (buf);
-    GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
-        ("encoder not initialized (input is not audio?)"));
-    return GST_FLOW_ERROR;
-  }
 }
 
 /* set up the encoder state */
@@ -1204,7 +1107,7 @@ gst_lame_setup (GstLame * lame)
   lame_set_in_samplerate (lame->lgf, lame->samplerate);
 
   /* let lame choose default samplerate unless outgoing sample rate is fixed */
-  allowed_caps = gst_pad_get_allowed_caps (lame->srcpad);
+  allowed_caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (lame));
 
   if (allowed_caps != NULL) {
     GstStructure *structure;
@@ -1294,37 +1197,6 @@ gst_lame_setup (GstLame * lame)
 #undef CHECK_ERROR
 }
 
-static GstStateChangeReturn
-gst_lame_change_state (GstElement * element, GstStateChange transition)
-{
-  GstLame *lame;
-  GstStateChangeReturn result;
-
-  lame = GST_LAME (element);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_READY_TO_PAUSED:
-      lame->last_flow = GST_FLOW_OK;
-      lame->last_ts = GST_CLOCK_TIME_NONE;
-      lame->eos_ts = GST_CLOCK_TIME_NONE;
-      break;
-    default:
-      break;
-  }
-
-  result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
-
-  switch (transition) {
-    case GST_STATE_CHANGE_READY_TO_NULL:
-      gst_lame_release_memory (lame);
-      break;
-    default:
-      break;
-  }
-
-  return result;
-}
-
 static gboolean
 gst_lame_get_default_settings (void)
 {
index f9a1370..b84cca3 100644 (file)
@@ -27,6 +27,7 @@
 G_BEGIN_DECLS
 
 #include <lame/lame.h>
+#include <gst/audio/gstaudioencoder.h>
 
 #define GST_TYPE_LAME \
   (gst_lame_get_type())
@@ -48,10 +49,9 @@ typedef struct _GstLameClass GstLameClass;
  * Opaque data structure.
  */
 struct _GstLame {
-  GstElement element;
+  GstAudioEncoder element;
 
   /*< private >*/
-  GstPad *srcpad, *sinkpad;
 
   gint samplerate;
   gint num_channels;
@@ -100,7 +100,7 @@ struct _GstLame {
 };
 
 struct _GstLameClass {
-  GstElementClass parent_class;
+  GstAudioEncoderClass parent_class;
 };
 
 GType gst_lame_get_type(void);