configure.ac: export [_]*{gst,Gst,GST}.* symbols from plugins
[platform/upstream/gst-plugins-good.git] / ext / lame / gstlame.c
index 0c0ab52..0b32b4b 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include "string.h"
 #include "gstlame.h"
 
 /* elementfactory information */
@@ -24,51 +28,39 @@ static GstElementDetails gst_lame_details =
 {
   "L.A.M.E. mp3 encoder",
   "Codec/Audio/Encoder",
-  "GPL",
   "High-quality free MP3 encoder",
-  VERSION,
   "Erik Walthinsen <omega@cse.ogi.edu>",
-  "(C) 2000",
 };
 
-GST_PAD_TEMPLATE_FACTORY (gst_lame_sink_factory,
+static GstStaticPadTemplate gst_lame_sink_template =
+GST_STATIC_PAD_TEMPLATE (
   "sink",
   GST_PAD_SINK,
   GST_PAD_ALWAYS,
-  GST_CAPS_NEW (
-    "gstlame_sink",
-    "audio/raw",
-      "format",     GST_PROPS_STRING ("int"),
-      "law",        GST_PROPS_INT (0),
-      "endianness", GST_PROPS_INT (G_BYTE_ORDER),
-      "signed",     GST_PROPS_BOOLEAN (TRUE),
-      "width",      GST_PROPS_INT (16),
-      "depth",      GST_PROPS_INT (16),
-      "rate",       GST_PROPS_LIST (
-                     GST_PROPS_INT (8000), 
-                     GST_PROPS_INT (11025), 
-                     GST_PROPS_INT (12000), 
-                     GST_PROPS_INT (16000), 
-                     GST_PROPS_INT (22050),
-                     GST_PROPS_INT (24000),
-                     GST_PROPS_INT (32000),
-                     GST_PROPS_INT (44100),
-                     GST_PROPS_INT (48000)
-                   ),
-      "channels",   GST_PROPS_INT_RANGE (1, 2)
+  GST_STATIC_CAPS (
+    "audio/x-raw-int, "
+      "endianness = (int) BYTE_ORDER, "
+      "signed = (boolean) true, "
+      "width = (int) 16, "
+      "depth = (int) 16, "
+      "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
+      "channels = (int) [ 1, 2 ]"
   )
-)
+);
 
-GST_PAD_TEMPLATE_FACTORY (gst_lame_src_factory,
+static GstStaticPadTemplate gst_lame_src_template =
+GST_STATIC_PAD_TEMPLATE (
   "src",
   GST_PAD_SRC,
   GST_PAD_ALWAYS,
-  GST_CAPS_NEW (
-    "gstlame_src",
-    "audio/x-mp3",
-    NULL
+  GST_STATIC_CAPS (
+    "audio/mpeg, "
+      "mpegversion = (int) 1, "
+      "layer = (int) 3, "
+      "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
+      "channels = (int) [ 1, 2 ]"
   )
-)
+);
 
 /********** Define useful types for non-programmatic interfaces **********/
 #define GST_TYPE_LAME_MODE (gst_lame_mode_get_type())
@@ -172,7 +164,7 @@ enum {
   ARG_EMPHASIS,
 };
 
-
+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);
 
@@ -180,7 +172,7 @@ 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 void                    gst_lame_chain          (GstPad *pad, GstBuffer *buf);
+static void                    gst_lame_chain          (GstPad *pad, GstData *_data);
 static gboolean                gst_lame_setup          (GstLame *lame);
 static GstElementStateReturn   gst_lame_change_state   (GstElement *element);
 
@@ -195,7 +187,7 @@ gst_lame_get_type (void)
   if (!gst_lame_type) {
     static const GTypeInfo gst_lame_info = {
       sizeof (GstLameClass),      
-      NULL,
+      gst_lame_base_init,
       NULL,
       (GClassInitFunc) gst_lame_class_init,
       NULL,
@@ -204,12 +196,33 @@ gst_lame_get_type (void)
       0,
       (GInstanceInitFunc) gst_lame_init,
     };
+
+    static const GInterfaceInfo tag_setter_info = {
+      NULL,
+      NULL,
+      NULL
+    };
+
     gst_lame_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLame", &gst_lame_info, 0);
+    g_type_add_interface_static (gst_lame_type, GST_TYPE_TAG_SETTER, &tag_setter_info);
+
   }
   return gst_lame_type;
 }
 
 static void
+gst_lame_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class, 
+      gst_static_pad_template_get (&gst_lame_src_template));
+  gst_element_class_add_pad_template (element_class, 
+      gst_static_pad_template_get (&gst_lame_sink_template));
+  gst_element_class_set_details (element_class, &gst_lame_details);
+}
+
+static void
 gst_lame_class_init (GstLameClass *klass)
 {
   GObjectClass *gobject_class;
@@ -327,46 +340,57 @@ gst_lame_class_init (GstLameClass *klass)
 }
 
 static GstPadLinkReturn
-gst_lame_sinkconnect (GstPad *pad, GstCaps *caps)
+gst_lame_sink_link (GstPad *pad, const GstCaps *caps)
 {
   GstLame *lame;
+  gint out_samplerate;
+  GstStructure *structure;
+  GstCaps *othercaps;
 
   lame = GST_LAME (gst_pad_get_parent (pad));
+  structure = gst_caps_get_structure (caps, 0);
 
-  if (!GST_CAPS_IS_FIXED (caps)) {
-    GST_DEBUG (GST_CAT_CAPS, "caps on lame pad %s:%s not fixed, delayed",
-              GST_DEBUG_PAD_NAME (pad));
-    return GST_PAD_LINK_DELAYED;
-  }
-
-  gst_caps_get_int (caps, "rate", &lame->samplerate);
-  gst_caps_get_int (caps, "channels", &lame->num_channels);
+  gst_structure_get_int (structure, "rate", &lame->samplerate);
+  gst_structure_get_int (structure, "channels", &lame->num_channels);
 
   if (!gst_lame_setup (lame)) {
-    gst_element_error (GST_ELEMENT (lame), 
-                      "could not initialize encoder (wrong parameters?)");
+    GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
+                      ("could not initialize encoder (wrong parameters?)"));
     return GST_PAD_LINK_REFUSED;
   }
 
-  return GST_PAD_LINK_OK;
+  out_samplerate = lame_get_out_samplerate (lame->lgf);
+  othercaps = 
+         gst_caps_new_simple (
+                               "audio/mpeg",
+                                "mpegversion", G_TYPE_INT, 1,
+                               "layer", G_TYPE_INT, 3,
+                               "channels", G_TYPE_INT, lame->num_channels,
+                               "rate", G_TYPE_INT, out_samplerate,
+                               NULL
+                             );
+
+  return gst_pad_try_set_caps (lame->srcpad, othercaps);
 }
 
 static void
 gst_lame_init (GstLame *lame)
 {
-  GST_DEBUG_ENTER ("(\"%s\")", gst_element_get_name (GST_ELEMENT (lame)));
+  GST_DEBUG_OBJECT (lame, "starting initialization");
 
-  lame->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (gst_lame_sink_factory), "sink");
+  lame->sinkpad = gst_pad_new_from_template (
+                   gst_static_pad_template_get (&gst_lame_sink_template), "sink");
   gst_element_add_pad (GST_ELEMENT (lame), lame->sinkpad);
   gst_pad_set_chain_function (lame->sinkpad, gst_lame_chain);
-  gst_pad_set_link_function (lame->sinkpad, gst_lame_sinkconnect);
+  gst_pad_set_link_function (lame->sinkpad, gst_lame_sink_link);
 
-  lame->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (gst_lame_src_factory), "src");
+  lame->srcpad = gst_pad_new_from_template (
+                   gst_static_pad_template_get (&gst_lame_src_template), "src");
   gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
 
   GST_FLAG_SET (lame, GST_ELEMENT_EVENT_AWARE);
 
-  GST_DEBUG (GST_CAT_PLUGIN_INFO, "setting up lame encoder");
+  GST_DEBUG ("setting up lame encoder");
   lame->lgf = lame_init ();
 
   lame->samplerate = 44100;
@@ -404,10 +428,104 @@ gst_lame_init (GstLame *lame)
   lame->allow_diff_short = lame_get_allow_diff_short (lame->lgf);
   lame->no_short_blocks = lame_get_no_short_blocks (lame->lgf);
   lame->emphasis = lame_get_emphasis (lame->lgf);
+  lame->tags = gst_tag_list_new ();
 
-  GST_DEBUG (GST_CAT_PLUGIN_INFO, "done initializing lame element");
+  id3tag_init (lame->lgf);
+  
+  GST_DEBUG_OBJECT (lame, "done initializing");
 }
 
+typedef struct _GstLameTagMatch GstLameTagMatch;
+typedef void (*GstLameTagFunc)(lame_global_flags* gfp, const char *value);
+
+struct _GstLameTagMatch
+{
+  gchar *gstreamer_tag;
+  GstLameTagFunc tag_func;
+};
+
+static GstLameTagMatch tag_matches[] = 
+  {
+    {GST_TAG_TITLE,        id3tag_set_title},
+    {GST_TAG_DATE,         id3tag_set_year},
+    {GST_TAG_TRACK_NUMBER, id3tag_set_track},
+    {GST_TAG_COMMENT,      id3tag_set_comment},
+    {GST_TAG_ARTIST,       id3tag_set_artist},
+    {GST_TAG_ALBUM,        id3tag_set_album},
+    {GST_TAG_GENRE,        (GstLameTagFunc)id3tag_set_genre},
+    {NULL,                 NULL}
+  };
+
+static void 
+add_one_tag (const GstTagList *list, const gchar *tag, 
+            gpointer user_data)
+{
+  GstLame *lame;
+  gchar *value;
+  int i = 0;
+
+  lame = GST_LAME (user_data);
+  g_return_if_fail (lame != NULL);
+
+  while (tag_matches[i].gstreamer_tag != NULL) {
+    if (strcmp (tag, tag_matches[i].gstreamer_tag) == 0) {
+      break;
+    }
+    i++;
+  }
+  
+  if (tag_matches[i].tag_func == NULL) {
+    g_print ("Couldn't find matching gstreamer tag for %s\n", tag);
+    return;
+  }
+
+  switch (gst_tag_get_type (tag)) {
+  case G_TYPE_UINT: {
+    guint ivalue;
+    if (!gst_tag_list_get_uint (list, tag, &ivalue)) {      
+      GST_DEBUG ("Error reading \"%s\" tag value\n", tag);
+      return;
+    }
+    value = g_strdup_printf ("%u", ivalue);
+    break;
+  }
+  case G_TYPE_STRING: 
+    if (!gst_tag_list_get_string (list, tag, &value)) {
+      GST_DEBUG ("Error reading \"%s\" tag value\n", tag);
+      return;
+    };
+    break;
+  default:
+    GST_DEBUG ("Couldn't write tag %s", tag);
+    break;
+  }
+
+  tag_matches[i].tag_func (lame->lgf, value);
+
+  if (gst_tag_get_type (tag) == G_TYPE_UINT) {
+    g_free (value);
+  }
+}
+
+static void
+gst_lame_set_metadata (GstLame *lame)
+{
+  const GstTagList *user_tags;
+  GstTagList *copy;
+
+  g_return_if_fail (lame != NULL);
+  user_tags = gst_tag_setter_get_list (GST_TAG_SETTER (lame));
+  if ((lame->tags == NULL) && (user_tags == NULL)) {
+    return;
+  }
+  copy = gst_tag_list_merge (user_tags, lame->tags, 
+                            gst_tag_setter_get_merge_mode (GST_TAG_SETTER (lame)));
+  gst_tag_list_foreach ((GstTagList*)copy, add_one_tag, lame);
+
+  gst_tag_list_free (copy);
+}
+
+
 
 static void
 gst_lame_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
@@ -624,8 +742,9 @@ gst_lame_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec
 }
 
 static void
-gst_lame_chain (GstPad *pad, GstBuffer *buf)
+gst_lame_chain (GstPad *pad, GstData *_data)
 {
+  GstBuffer *buf = GST_BUFFER (_data);
   GstLame *lame;
   GstBuffer *outbuf;
   gchar *mp3_data = NULL;
@@ -634,7 +753,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
 
   lame = GST_LAME (gst_pad_get_parent (pad));
 
-  GST_DEBUG (GST_CAT_PLUGIN_INFO, "entered chain");
+  GST_DEBUG ("entered chain");
 
   if (GST_IS_EVENT (buf)) {
     switch (GST_EVENT_TYPE (buf)) {
@@ -643,10 +762,19 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
       case GST_EVENT_FLUSH:
         mp3_buffer_size = 7200;
         mp3_data = g_malloc (mp3_buffer_size);
-       
-        mp3_size = lame_encode_flush_nogap (lame->lgf, mp3_data, mp3_buffer_size);
+
+        mp3_size = lame_encode_flush (lame->lgf, mp3_data, mp3_buffer_size);
        gst_event_unref (GST_EVENT (buf));
         break; 
+      case GST_EVENT_TAG:
+       if (lame->tags) {
+         gst_tag_list_insert (lame->tags, gst_event_tag_get_list (GST_EVENT (buf)), 
+                 gst_tag_setter_get_merge_mode (GST_TAG_SETTER (lame)));
+       } else {
+         g_assert_not_reached ();
+       }       
+       //      gst_pad_event_default (pad, GST_EVENT (buf));
+       break;
       default:
        gst_pad_event_default (pad, GST_EVENT (buf));
        break;
@@ -657,7 +785,8 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
 
     if (!lame->initialized) {
       gst_buffer_unref (buf);
-      gst_element_error (GST_ELEMENT (lame), "encoder not initialized (input is not audio?)");
+      GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
+                         ("encoder not initialized (input is not audio?)"));
       return;
     }
 
@@ -679,7 +808,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
                    mp3_data, mp3_buffer_size);
     }
 
-    GST_DEBUG (GST_CAT_PLUGIN_INFO, 
+    GST_DEBUG (
               "encoded %d bytes of audio to %d bytes of mp3", 
               GST_BUFFER_SIZE (buf), mp3_size);
 
@@ -688,7 +817,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
 
     if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE &&
        GST_BUFFER_DURATION (buf) != duration)
-      GST_DEBUG (GST_CAT_PLUGIN_INFO,
+      GST_DEBUG (
                 "mad: incoming buffer had incorrect duration %lld, "
                 "outgoing buffer will have correct duration %lld",
                 GST_BUFFER_DURATION (buf), duration);
@@ -712,7 +841,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
     GST_BUFFER_OFFSET (outbuf)    = lame->last_offs;
     GST_BUFFER_DURATION (outbuf)  = lame->last_duration;
 
-    gst_pad_push (lame->srcpad,outbuf);
+    gst_pad_push (lame->srcpad,GST_DATA (outbuf));
 
     lame->last_ts = GST_CLOCK_TIME_NONE;
   }
@@ -721,7 +850,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
   }
 
   if (eos) {
-    gst_pad_push (lame->srcpad, GST_BUFFER (gst_event_new (GST_EVENT_EOS)));
+    gst_pad_push (lame->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
     gst_element_set_eos (GST_ELEMENT (lame));
   }
 }
@@ -730,7 +859,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
 static gboolean
 gst_lame_setup (GstLame *lame)
 {
-  GST_DEBUG_ENTER ("(\"%s\")", gst_element_get_name (GST_ELEMENT (lame)));
+  GST_DEBUG_OBJECT (lame, "starting setup");
 
   /* check if we're already initialized; if we are, we might want to check
    * if this initialization is compatible with the previous one */
@@ -776,6 +905,8 @@ gst_lame_setup (GstLame *lame)
   lame_set_no_short_blocks (lame->lgf, lame->no_short_blocks);
   lame_set_emphasis (lame->lgf, lame->emphasis);
 
+  gst_lame_set_metadata (lame);
+
   /* initialize the lame encoder */
   if (lame_init_params (lame->lgf) < 0) {
     lame->initialized = FALSE;
@@ -783,12 +914,12 @@ gst_lame_setup (GstLame *lame)
   else {
     lame->initialized = TRUE;
     /* FIXME: it would be nice to print out the mode here */
-    GST_INFO (GST_CAT_PLUGIN_INFO, 
+    GST_INFO ( 
              "lame encoder initialized (%d kbit/s, %d Hz, %d channels)", 
              lame->bitrate, lame->samplerate, lame->num_channels);
   }
 
-  GST_DEBUG_LEAVE ("");
+  GST_DEBUG_OBJECT (lame, "done with setup");
 
   return lame->initialized;
 }
@@ -802,7 +933,7 @@ gst_lame_change_state (GstElement *element)
 
   lame = GST_LAME (element);
 
-  GST_DEBUG (0,"state pending %d", GST_STATE_PENDING (element));
+  GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
 
   switch (GST_STATE_TRANSITION (element)) {
     case GST_STATE_READY_TO_PAUSED:
@@ -826,32 +957,21 @@ gst_lame_change_state (GstElement *element)
 }
 
 static gboolean
-plugin_init (GModule *module, GstPlugin *plugin)
+plugin_init (GstPlugin *plugin)
 {
-  GstElementFactory *factory;
-
-  /* create an elementfactory for the gst_lame element */
-  factory = gst_element_factory_new ("lame", GST_TYPE_LAME,
-                                    &gst_lame_details);
-  g_return_val_if_fail (factory != NULL, FALSE);
-
-  /* register the source's padtemplate */
-  gst_element_factory_add_pad_template (factory, 
-               GST_PAD_TEMPLATE_GET (gst_lame_src_factory));
-
-  /* register the sink's padtemplate */
-  gst_element_factory_add_pad_template (factory, 
-               GST_PAD_TEMPLATE_GET (gst_lame_sink_factory));
-
-  /* and add the gst_lame element factory to the plugin */
-  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
-
+  if (!gst_element_register (plugin, "lame", GST_RANK_NONE, GST_TYPE_LAME))
+    return FALSE;
+  
   return TRUE;
 }
 
-GstPluginDesc plugin_desc = {
+GST_PLUGIN_DEFINE (
   GST_VERSION_MAJOR,
   GST_VERSION_MINOR,
   "lame",
-  plugin_init
-};
+  "Encode MP3's with LAME",
+  plugin_init,
+  VERSION,
+  "LGPL",
+  GST_PACKAGE,
+  GST_ORIGIN)