]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blob - ext/lame/gstlamemp3enc.c
lamemp3enc: do not leak merged tags
[glsdk/gst-plugins-ugly0-10.git] / ext / lame / gstlamemp3enc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Wim Taymans <wim@fluendo.com>
4  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
5  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
23 /**
24  * SECTION:element-lamemp3enc
25  * @see_also: lame, mad, vorbisenc
26  *
27  * This element encodes raw integer audio into an MPEG-1 layer 3 (MP3) stream.
28  * Note that <ulink url="http://en.wikipedia.org/wiki/MP3">MP3</ulink> is not
29  * a free format, there are licensing and patent issues to take into
30  * consideration. See <ulink url="http://www.vorbis.com/">Ogg/Vorbis</ulink>
31  * for a royalty free (and often higher quality) alternative.
32  *
33  * <refsect2>
34  * <title>Output sample rate</title>
35  * If no fixed output sample rate is negotiated on the element's src pad,
36  * the element will choose an optimal sample rate to resample to internally.
37  * For example, a 16-bit 44.1 KHz mono audio stream encoded at 48 kbit will
38  * get resampled to 32 KHz.  Use filter caps on the src pad to force a
39  * particular sample rate.
40  * </refsect2>
41  * <refsect2>
42  * <title>Example pipelines</title>
43  * |[
44  * gst-launch -v audiotestsrc wave=sine num-buffers=100 ! audioconvert ! lamemp3enc ! filesink location=sine.mp3
45  * ]| Encode a test sine signal to MP3.
46  * |[
47  * gst-launch -v alsasrc ! audioconvert ! lamemp3enc target=bitrate bitrate=192 ! filesink location=alsasrc.mp3
48  * ]| Record from a sound card using ALSA and encode to MP3 with an average bitrate of 192kbps
49  * |[
50  * gst-launch -v filesrc location=music.wav ! decodebin ! audioconvert ! audioresample ! lamemp3enc target=quality quality=0 ! id3v2mux ! filesink location=music.mp3
51  * ]| Transcode from a .wav file to MP3 (the id3v2mux element is optional) with best VBR quality
52  * |[
53  * gst-launch -v cdda://5 ! audioconvert ! lamemp3enc target=bitrate cbr=true bitrate=192 ! filesink location=track5.mp3
54  * ]| Encode Audio CD track 5 to MP3 with a constant bitrate of 192kbps
55  * |[
56  * gst-launch -v audiotestsrc num-buffers=10 ! audio/x-raw-int,rate=44100,channels=1 ! lamemp3enc target=bitrate cbr=true bitrate=48 ! filesink location=test.mp3
57  * ]| Encode to a fixed sample rate
58  * </refsect2>
59  *
60  * Since: 0.10.12
61  */
63 #ifdef HAVE_CONFIG_H
64 #include "config.h"
65 #endif
67 #include <string.h>
68 #include "gstlamemp3enc.h"
69 #include <gst/gst-i18n-plugin.h>
71 /* lame < 3.98 */
72 #ifndef HAVE_LAME_SET_VBR_QUALITY
73 #define lame_set_VBR_quality(flags,q) lame_set_VBR_q((flags),(int)(q))
74 #endif
76 GST_DEBUG_CATEGORY_STATIC (debug);
77 #define GST_CAT_DEFAULT debug
79 /* elementfactory information */
81 /* LAMEMP3ENC can do MPEG-1, MPEG-2, and MPEG-2.5, so it has 9 possible
82  * sample rates it supports */
83 static GstStaticPadTemplate gst_lamemp3enc_sink_template =
84 GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS ("audio/x-raw-int, "
88         "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
89         "signed = (boolean) true, "
90         "width = (int) 16, "
91         "depth = (int) 16, "
92         "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
93         "channels = (int) [ 1, 2 ]")
94     );
96 static GstStaticPadTemplate gst_lamemp3enc_src_template =
97 GST_STATIC_PAD_TEMPLATE ("src",
98     GST_PAD_SRC,
99     GST_PAD_ALWAYS,
100     GST_STATIC_CAPS ("audio/mpeg, "
101         "mpegversion = (int) 1, "
102         "layer = (int) 3, "
103         "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
104         "channels = (int) [ 1, 2 ]")
105     );
107 /********** Define useful types for non-programmatic interfaces **********/
108 enum
110   LAMEMP3ENC_TARGET_QUALITY = 0,
111   LAMEMP3ENC_TARGET_BITRATE
112 };
114 #define GST_TYPE_LAMEMP3ENC_TARGET (gst_lamemp3enc_target_get_type())
115 static GType
116 gst_lamemp3enc_target_get_type (void)
118   static GType lame_target_type = 0;
119   static GEnumValue lame_targets[] = {
120     {LAMEMP3ENC_TARGET_QUALITY, "Quality", "quality"},
121     {LAMEMP3ENC_TARGET_BITRATE, "Bitrate", "bitrate"},
122     {0, NULL, NULL}
123   };
125   if (!lame_target_type) {
126     lame_target_type =
127         g_enum_register_static ("GstLameMP3EncTarget", lame_targets);
128   }
129   return lame_target_type;
132 enum
134   LAMEMP3ENC_ENCODING_ENGINE_QUALITY_FAST = 0,
135   LAMEMP3ENC_ENCODING_ENGINE_QUALITY_STANDARD,
136   LAMEMP3ENC_ENCODING_ENGINE_QUALITY_HIGH
137 };
139 #define GST_TYPE_LAMEMP3ENC_ENCODING_ENGINE_QUALITY (gst_lamemp3enc_encoding_engine_quality_get_type())
140 static GType
141 gst_lamemp3enc_encoding_engine_quality_get_type (void)
143   static GType lame_encoding_engine_quality_type = 0;
144   static GEnumValue lame_encoding_engine_quality[] = {
145     {0, "Fast", "fast"},
146     {1, "Standard", "standard"},
147     {2, "High", "high"},
148     {0, NULL, NULL}
149   };
151   if (!lame_encoding_engine_quality_type) {
152     lame_encoding_engine_quality_type =
153         g_enum_register_static ("GstLameMP3EncEncodingEngineQuality",
154         lame_encoding_engine_quality);
155   }
156   return lame_encoding_engine_quality_type;
159 /********** Standard stuff for signals and arguments **********/
161 enum
163   ARG_0,
164   ARG_TARGET,
165   ARG_BITRATE,
166   ARG_CBR,
167   ARG_QUALITY,
168   ARG_ENCODING_ENGINE_QUALITY,
169   ARG_MONO
170 };
172 #define DEFAULT_TARGET LAMEMP3ENC_TARGET_QUALITY
173 #define DEFAULT_BITRATE 128
174 #define DEFAULT_CBR FALSE
175 #define DEFAULT_QUALITY 4
176 #define DEFAULT_ENCODING_ENGINE_QUALITY LAMEMP3ENC_ENCODING_ENGINE_QUALITY_STANDARD
177 #define DEFAULT_MONO FALSE
179 static gboolean gst_lamemp3enc_start (GstAudioEncoder * enc);
180 static gboolean gst_lamemp3enc_stop (GstAudioEncoder * enc);
181 static gboolean gst_lamemp3enc_set_format (GstAudioEncoder * enc,
182     GstAudioInfo * info);
183 static GstFlowReturn gst_lamemp3enc_handle_frame (GstAudioEncoder * enc,
184     GstBuffer * in_buf);
185 static void gst_lamemp3enc_flush (GstAudioEncoder * enc);
187 static void gst_lamemp3enc_set_property (GObject * object, guint prop_id,
188     const GValue * value, GParamSpec * pspec);
189 static void gst_lamemp3enc_get_property (GObject * object, guint prop_id,
190     GValue * value, GParamSpec * pspec);
191 static gboolean gst_lamemp3enc_setup (GstLameMP3Enc * lame, GstTagList ** tags);
193 GST_BOILERPLATE (GstLameMP3Enc, gst_lamemp3enc, GstAudioEncoder,
194     GST_TYPE_AUDIO_ENCODER);
196 static void
197 gst_lamemp3enc_release_memory (GstLameMP3Enc * lame)
199   if (lame->lgf) {
200     lame_close (lame->lgf);
201     lame->lgf = NULL;
202   }
205 static void
206 gst_lamemp3enc_finalize (GObject * obj)
208   gst_lamemp3enc_release_memory (GST_LAMEMP3ENC (obj));
210   G_OBJECT_CLASS (parent_class)->finalize (obj);
213 static void
214 gst_lamemp3enc_base_init (gpointer g_class)
216   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
218   gst_element_class_add_static_pad_template (element_class,
219       &gst_lamemp3enc_src_template);
220   gst_element_class_add_static_pad_template (element_class,
221       &gst_lamemp3enc_sink_template);
222   gst_element_class_set_details_simple (element_class, "L.A.M.E. mp3 encoder",
223       "Codec/Encoder/Audio",
224       "High-quality free MP3 encoder",
225       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
228 static void
229 gst_lamemp3enc_class_init (GstLameMP3EncClass * klass)
231   GObjectClass *gobject_class;
232   GstAudioEncoderClass *base_class;
234   gobject_class = (GObjectClass *) klass;
235   base_class = (GstAudioEncoderClass *) klass;
237   gobject_class->set_property = gst_lamemp3enc_set_property;
238   gobject_class->get_property = gst_lamemp3enc_get_property;
239   gobject_class->finalize = gst_lamemp3enc_finalize;
241   base_class->start = GST_DEBUG_FUNCPTR (gst_lamemp3enc_start);
242   base_class->stop = GST_DEBUG_FUNCPTR (gst_lamemp3enc_stop);
243   base_class->set_format = GST_DEBUG_FUNCPTR (gst_lamemp3enc_set_format);
244   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_lamemp3enc_handle_frame);
245   base_class->flush = GST_DEBUG_FUNCPTR (gst_lamemp3enc_flush);
247   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TARGET,
248       g_param_spec_enum ("target", "Target",
249           "Optimize for quality or bitrate", GST_TYPE_LAMEMP3ENC_TARGET,
250           DEFAULT_TARGET,
251           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
253       g_param_spec_int ("bitrate", "Bitrate (kb/s)",
254           "Bitrate in kbit/sec (Only valid if target is bitrate, for CBR one "
255           "of 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, "
256           "256 or 320)", 8, 320, DEFAULT_BITRATE,
257           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CBR,
259       g_param_spec_boolean ("cbr", "CBR", "Enforce constant bitrate encoding "
260           "(Only valid if target is bitrate)", DEFAULT_CBR,
261           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
262   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
263       g_param_spec_float ("quality", "Quality",
264           "VBR Quality from 0 to 10, 0 being the best "
265           "(Only valid if target is quality)", 0.0, 9.999,
266           DEFAULT_QUALITY,
267           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
268   g_object_class_install_property (G_OBJECT_CLASS (klass),
269       ARG_ENCODING_ENGINE_QUALITY, g_param_spec_enum ("encoding-engine-quality",
270           "Encoding Engine Quality", "Quality/speed of the encoding engine, "
271           "this does not affect the bitrate!",
272           GST_TYPE_LAMEMP3ENC_ENCODING_ENGINE_QUALITY,
273           DEFAULT_ENCODING_ENGINE_QUALITY,
274           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MONO,
276       g_param_spec_boolean ("mono", "Mono", "Enforce mono encoding",
277           DEFAULT_MONO,
278           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 static void
282 gst_lamemp3enc_init (GstLameMP3Enc * lame, GstLameMP3EncClass * klass)
286 static gboolean
287 gst_lamemp3enc_start (GstAudioEncoder * enc)
289   GstLameMP3Enc *lame = GST_LAMEMP3ENC (enc);
291   GST_DEBUG_OBJECT (lame, "start");
292   return TRUE;
295 static gboolean
296 gst_lamemp3enc_stop (GstAudioEncoder * enc)
298   GstLameMP3Enc *lame = GST_LAMEMP3ENC (enc);
300   GST_DEBUG_OBJECT (lame, "stop");
302   gst_lamemp3enc_release_memory (lame);
303   return TRUE;
306 static gboolean
307 gst_lamemp3enc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
309   GstLameMP3Enc *lame;
310   gint out_samplerate;
311   gint version;
312   GstCaps *othercaps;
313   GstClockTime latency;
314   GstTagList *tags = NULL;
316   lame = GST_LAMEMP3ENC (enc);
318   /* parameters already parsed for us */
319   lame->samplerate = GST_AUDIO_INFO_RATE (info);
320   lame->num_channels = GST_AUDIO_INFO_CHANNELS (info);
322   /* but we might be asked to reconfigure, so reset */
323   gst_lamemp3enc_release_memory (lame);
325   GST_DEBUG_OBJECT (lame, "setting up lame");
326   if (!gst_lamemp3enc_setup (lame, &tags))
327     goto setup_failed;
329   out_samplerate = lame_get_out_samplerate (lame->lgf);
330   if (out_samplerate == 0)
331     goto zero_output_rate;
332   if (out_samplerate != lame->samplerate) {
333     GST_WARNING_OBJECT (lame,
334         "output samplerate %d is different from incoming samplerate %d",
335         out_samplerate, lame->samplerate);
336   }
338   version = lame_get_version (lame->lgf);
339   if (version == 0)
340     version = 2;
341   else if (version == 1)
342     version = 1;
343   else if (version == 2)
344     version = 3;
346   othercaps =
347       gst_caps_new_simple ("audio/mpeg",
348       "mpegversion", G_TYPE_INT, 1,
349       "mpegaudioversion", G_TYPE_INT, version,
350       "layer", G_TYPE_INT, 3,
351       "channels", G_TYPE_INT, lame->mono ? 1 : lame->num_channels,
352       "rate", G_TYPE_INT, out_samplerate, NULL);
354   /* and use these caps */
355   gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), othercaps);
356   gst_caps_unref (othercaps);
358   /* base class feedback:
359    * - we will handle buffers, just hand us all available
360    * - report latency */
361   latency = gst_util_uint64_scale_int (lame_get_framesize (lame->lgf),
362       GST_SECOND, lame->samplerate);
363   gst_audio_encoder_set_latency (enc, latency, latency);
365   if (tags) {
366     gst_audio_encoder_merge_tags (enc, tags, GST_TAG_MERGE_REPLACE);
367     gst_tag_list_free (tags);
368   }
370   return TRUE;
372 zero_output_rate:
373   {
374     if (tags)
375       gst_tag_list_free (tags);
376     GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS, (NULL),
377         ("LAMEMP3ENC decided on a zero sample rate"));
378     return FALSE;
379   }
380 setup_failed:
381   {
382     GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS,
383         (_("Failed to configure LAMEMP3ENC encoder. Check your encoding parameters.")), (NULL));
384     return FALSE;
385   }
388 /* <php-emulation-mode>three underscores for ___rate is really really really
389  * private as opposed to one underscore<php-emulation-mode> */
390 /* call this MACRO outside of the NULL state so that we have a higher chance
391  * of actually having a pipeline and bus to get the message through */
393 #define CHECK_AND_FIXUP_BITRATE(obj,param,rate)                           \
394 G_STMT_START {                                                            \
395   gint ___rate = rate;                                                    \
396   gint maxrate = 320;                                                     \
397   gint multiplier = 64;                                                   \
398   if (rate == 0) {                                                        \
399     ___rate = rate;                                                       \
400   } else if (rate <= 64) {                                                \
401     maxrate = 64; multiplier = 8;                                         \
402     if ((rate % 8) != 0) ___rate = GST_ROUND_UP_8 (rate);                 \
403   } else if (rate <= 128) {                                               \
404     maxrate = 128; multiplier = 16;                                       \
405     if ((rate % 16) != 0) ___rate = GST_ROUND_UP_16 (rate);               \
406   } else if (rate <= 256) {                                               \
407     maxrate = 256; multiplier = 32;                                       \
408     if ((rate % 32) != 0) ___rate = GST_ROUND_UP_32 (rate);               \
409   } else if (rate <= 320) {                                               \
410     maxrate = 320; multiplier = 64;                                       \
411     if ((rate % 64) != 0) ___rate = GST_ROUND_UP_64 (rate);               \
412   }                                                                       \
413   if (___rate != rate) {                                                  \
414     GST_ELEMENT_WARNING (obj, LIBRARY, SETTINGS,                          \
415       (_("The requested bitrate %d kbit/s for property '%s' "             \
416        "is not allowed. "                                                 \
417        "The bitrate was changed to %d kbit/s."), rate,                    \
418          param,  ___rate),                                                \
419        ("A bitrate below %d should be a multiple of %d.",                 \
420           maxrate, multiplier));                                          \
421     rate = ___rate;                                                       \
422   }                                                                       \
423 } G_STMT_END
425 static void
426 gst_lamemp3enc_set_property (GObject * object, guint prop_id,
427     const GValue * value, GParamSpec * pspec)
429   GstLameMP3Enc *lame;
431   lame = GST_LAMEMP3ENC (object);
433   switch (prop_id) {
434     case ARG_TARGET:
435       lame->target = g_value_get_enum (value);
436       break;
437     case ARG_BITRATE:
438       lame->bitrate = g_value_get_int (value);
439       break;
440     case ARG_CBR:
441       lame->cbr = g_value_get_boolean (value);
442       break;
443     case ARG_QUALITY:
444       lame->quality = g_value_get_float (value);
445       break;
446     case ARG_ENCODING_ENGINE_QUALITY:
447       lame->encoding_engine_quality = g_value_get_enum (value);
448       break;
449     case ARG_MONO:
450       lame->mono = g_value_get_boolean (value);
451       break;
452     default:
453       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
454       break;
455   }
458 static void
459 gst_lamemp3enc_get_property (GObject * object, guint prop_id, GValue * value,
460     GParamSpec * pspec)
462   GstLameMP3Enc *lame;
464   lame = GST_LAMEMP3ENC (object);
466   switch (prop_id) {
467     case ARG_TARGET:
468       g_value_set_enum (value, lame->target);
469       break;
470     case ARG_BITRATE:
471       g_value_set_int (value, lame->bitrate);
472       break;
473     case ARG_CBR:
474       g_value_set_boolean (value, lame->cbr);
475       break;
476     case ARG_QUALITY:
477       g_value_set_float (value, lame->quality);
478       break;
479     case ARG_ENCODING_ENGINE_QUALITY:
480       g_value_set_enum (value, lame->encoding_engine_quality);
481       break;
482     case ARG_MONO:
483       g_value_set_boolean (value, lame->mono);
484       break;
485     default:
486       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
487       break;
488   }
491 static GstFlowReturn
492 gst_lamemp3enc_flush_full (GstLameMP3Enc * lame, gboolean push)
494   GstBuffer *buf;
495   gint size;
496   GstFlowReturn result = GST_FLOW_OK;
498   if (!lame->lgf)
499     return GST_FLOW_OK;
501   buf = gst_buffer_new_and_alloc (7200);
502   size = lame_encode_flush (lame->lgf, GST_BUFFER_DATA (buf), 7200);
504   if (size > 0 && push) {
505     GST_BUFFER_SIZE (buf) = size;
506     GST_DEBUG_OBJECT (lame, "pushing final packet of %u bytes", size);
507     result = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (lame), buf, -1);
508   } else {
509     GST_DEBUG_OBJECT (lame, "no final packet (size=%d, push=%d)", size, push);
510     gst_buffer_unref (buf);
511     result = GST_FLOW_OK;
512   }
513   return result;
516 static void
517 gst_lamemp3enc_flush (GstAudioEncoder * enc)
519   gst_lamemp3enc_flush_full (GST_LAMEMP3ENC (enc), FALSE);
522 static GstFlowReturn
523 gst_lamemp3enc_handle_frame (GstAudioEncoder * enc, GstBuffer * in_buf)
525   GstLameMP3Enc *lame;
526   guchar *mp3_data;
527   gint mp3_buffer_size, mp3_size;
528   GstBuffer *mp3_buf;
529   GstFlowReturn result;
530   gint num_samples;
531   guint8 *data;
532   guint size;
534   lame = GST_LAMEMP3ENC (enc);
536   /* squeeze remaining and push */
537   if (G_UNLIKELY (in_buf == NULL))
538     return gst_lamemp3enc_flush_full (lame, TRUE);
540   data = GST_BUFFER_DATA (in_buf);
541   size = GST_BUFFER_SIZE (in_buf);
543   num_samples = size / 2;
545   /* allocate space for output */
546   mp3_buffer_size = 1.25 * num_samples + 7200;
547   mp3_buf = gst_buffer_new_and_alloc (mp3_buffer_size);
548   mp3_data = GST_BUFFER_DATA (mp3_buf);
550   /* lame seems to be too stupid to get mono interleaved going */
551   if (lame->num_channels == 1) {
552     mp3_size = lame_encode_buffer (lame->lgf,
553         (short int *) data,
554         (short int *) data, num_samples, mp3_data, mp3_buffer_size);
555   } else {
556     mp3_size = lame_encode_buffer_interleaved (lame->lgf,
557         (short int *) data,
558         num_samples / lame->num_channels, mp3_data, mp3_buffer_size);
559   }
561   GST_LOG_OBJECT (lame, "encoded %d bytes of audio to %d bytes of mp3",
562       size, mp3_size);
564   if (G_LIKELY (mp3_size > 0)) {
565     GST_BUFFER_SIZE (mp3_buf) = mp3_size;
566     result = gst_audio_encoder_finish_frame (enc, mp3_buf, -1);
567   } else {
568     if (mp3_size < 0) {
569       /* eat error ? */
570       g_warning ("error %d", mp3_size);
571     }
572     result = GST_FLOW_OK;
573     gst_buffer_unref (mp3_buf);
574   }
576   return result;
579 /* set up the encoder state */
580 static gboolean
581 gst_lamemp3enc_setup (GstLameMP3Enc * lame, GstTagList ** tags)
583   gboolean res;
585 #define CHECK_ERROR(command) G_STMT_START {\
586   if ((command) < 0) { \
587     GST_ERROR_OBJECT (lame, "setup failed: " G_STRINGIFY (command)); \
588     if (*tags) { \
589       gst_tag_list_free (*tags); \
590       *tags = NULL; \
591     } \
592     return FALSE; \
593   } \
594 }G_STMT_END
596   int retval;
597   GstCaps *allowed_caps;
599   GST_DEBUG_OBJECT (lame, "starting setup");
601   lame->lgf = lame_init ();
603   if (lame->lgf == NULL)
604     return FALSE;
606   *tags = gst_tag_list_new ();
608   /* copy the parameters over */
609   lame_set_in_samplerate (lame->lgf, lame->samplerate);
611   /* let lame choose default samplerate unless outgoing sample rate is fixed */
612   allowed_caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (lame));
614   if (allowed_caps != NULL) {
615     GstStructure *structure;
616     gint samplerate;
618     structure = gst_caps_get_structure (allowed_caps, 0);
620     if (gst_structure_get_int (structure, "rate", &samplerate)) {
621       GST_DEBUG_OBJECT (lame, "Setting sample rate to %d as fixed in src caps",
622           samplerate);
623       lame_set_out_samplerate (lame->lgf, samplerate);
624     } else {
625       GST_DEBUG_OBJECT (lame, "Letting lame choose sample rate");
626       lame_set_out_samplerate (lame->lgf, 0);
627     }
628     gst_caps_unref (allowed_caps);
629     allowed_caps = NULL;
630   } else {
631     GST_DEBUG_OBJECT (lame, "No peer yet, letting lame choose sample rate");
632     lame_set_out_samplerate (lame->lgf, 0);
633   }
635   CHECK_ERROR (lame_set_num_channels (lame->lgf, lame->num_channels));
636   CHECK_ERROR (lame_set_bWriteVbrTag (lame->lgf, 0));
638   if (lame->target == LAMEMP3ENC_TARGET_QUALITY) {
639     CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_default));
640     CHECK_ERROR (lame_set_VBR_quality (lame->lgf, lame->quality));
641   } else {
642     if (lame->cbr) {
643       CHECK_AND_FIXUP_BITRATE (lame, "bitrate", lame->bitrate);
644       CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_off));
645       CHECK_ERROR (lame_set_brate (lame->lgf, lame->bitrate));
646     } else {
647       CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_abr));
648       CHECK_ERROR (lame_set_VBR_mean_bitrate_kbps (lame->lgf, lame->bitrate));
649     }
650     gst_tag_list_add (*tags, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
651         lame->bitrate * 1000, NULL);
652   }
654   if (lame->encoding_engine_quality == LAMEMP3ENC_ENCODING_ENGINE_QUALITY_FAST)
655     CHECK_ERROR (lame_set_quality (lame->lgf, 7));
656   else if (lame->encoding_engine_quality ==
657       LAMEMP3ENC_ENCODING_ENGINE_QUALITY_HIGH)
658     CHECK_ERROR (lame_set_quality (lame->lgf, 2));
659   /* else default */
661   if (lame->mono)
662     CHECK_ERROR (lame_set_mode (lame->lgf, MONO));
664   /* initialize the lame encoder */
665   if ((retval = lame_init_params (lame->lgf)) >= 0) {
666     /* FIXME: it would be nice to print out the mode here */
667     GST_INFO
668         ("lame encoder setup (target %s, quality %f, bitrate %d, %d Hz, %d channels)",
669         (lame->target == LAMEMP3ENC_TARGET_QUALITY) ? "quality" : "bitrate",
670         lame->quality, lame->bitrate, lame->samplerate, lame->num_channels);
671     res = TRUE;
672   } else {
673     GST_ERROR_OBJECT (lame, "lame_init_params returned %d", retval);
674     res = FALSE;
675   }
677   GST_DEBUG_OBJECT (lame, "done with setup");
678   return res;
679 #undef CHECK_ERROR
682 gboolean
683 gst_lamemp3enc_register (GstPlugin * plugin)
685   GST_DEBUG_CATEGORY_INIT (debug, "lamemp3enc", 0, "lame mp3 encoder");
687   if (!gst_element_register (plugin, "lamemp3enc", GST_RANK_PRIMARY,
688           GST_TYPE_LAMEMP3ENC))
689     return FALSE;
691   return TRUE;