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>
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.
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.
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-lamemp3enc
25 * @see_also: lame, mad, vorbisenc
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.
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.
42 * <title>Example pipelines</title>
44 * gst-launch-1.0 -v audiotestsrc wave=sine num-buffers=100 ! audioconvert ! lamemp3enc ! filesink location=sine.mp3
45 * ]| Encode a test sine signal to MP3.
47 * gst-launch-1.0 -v autoaudiosrc ! 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
50 * gst-launch-1.0 -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
53 * gst-launch-1.0 -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
56 * gst-launch-1.0 -v audiotestsrc num-buffers=10 ! audio/x-raw,rate=44100,channels=1 ! lamemp3enc target=bitrate cbr=true bitrate=48 ! filesink location=test.mp3
57 * ]| Encode to a fixed sample rate
66 #include "gstlamemp3enc.h"
67 #include <gst/gst-i18n-plugin.h>
70 #ifndef HAVE_LAME_SET_VBR_QUALITY
71 #define lame_set_VBR_quality(flags,q) lame_set_VBR_q((flags),(int)(q))
74 GST_DEBUG_CATEGORY_STATIC (debug);
75 #define GST_CAT_DEFAULT debug
77 /* elementfactory information */
79 /* LAMEMP3ENC can do MPEG-1, MPEG-2, and MPEG-2.5, so it has 9 possible
80 * sample rates it supports */
81 static GstStaticPadTemplate gst_lamemp3enc_sink_template =
82 GST_STATIC_PAD_TEMPLATE ("sink",
85 GST_STATIC_CAPS ("audio/x-raw, "
86 "format = (string) " GST_AUDIO_NE (S16) ", "
87 "layout = (string) interleaved, "
88 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
89 "channels = (int) 1; "
91 "format = (string) " GST_AUDIO_NE (S16) ", "
92 "layout = (string) interleaved, "
93 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
94 "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
97 static GstStaticPadTemplate gst_lamemp3enc_src_template =
98 GST_STATIC_PAD_TEMPLATE ("src",
101 GST_STATIC_CAPS ("audio/mpeg, "
102 "mpegversion = (int) 1, "
104 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
105 "channels = (int) [ 1, 2 ]")
108 /********** Define useful types for non-programmatic interfaces **********/
111 LAMEMP3ENC_TARGET_QUALITY = 0,
112 LAMEMP3ENC_TARGET_BITRATE
115 #define GST_TYPE_LAMEMP3ENC_TARGET (gst_lamemp3enc_target_get_type())
117 gst_lamemp3enc_target_get_type (void)
119 static GType lame_target_type = 0;
120 static const GEnumValue lame_targets[] = {
121 {LAMEMP3ENC_TARGET_QUALITY, "Quality", "quality"},
122 {LAMEMP3ENC_TARGET_BITRATE, "Bitrate", "bitrate"},
126 if (!lame_target_type) {
128 g_enum_register_static ("GstLameMP3EncTarget", lame_targets);
130 return lame_target_type;
135 LAMEMP3ENC_ENCODING_ENGINE_QUALITY_FAST = 0,
136 LAMEMP3ENC_ENCODING_ENGINE_QUALITY_STANDARD,
137 LAMEMP3ENC_ENCODING_ENGINE_QUALITY_HIGH
140 #define GST_TYPE_LAMEMP3ENC_ENCODING_ENGINE_QUALITY (gst_lamemp3enc_encoding_engine_quality_get_type())
142 gst_lamemp3enc_encoding_engine_quality_get_type (void)
144 static GType lame_encoding_engine_quality_type = 0;
145 static const GEnumValue lame_encoding_engine_quality[] = {
147 {1, "Standard", "standard"},
152 if (!lame_encoding_engine_quality_type) {
153 lame_encoding_engine_quality_type =
154 g_enum_register_static ("GstLameMP3EncEncodingEngineQuality",
155 lame_encoding_engine_quality);
157 return lame_encoding_engine_quality_type;
160 /********** Standard stuff for signals and arguments **********/
169 ARG_ENCODING_ENGINE_QUALITY,
173 #define DEFAULT_TARGET LAMEMP3ENC_TARGET_QUALITY
174 #define DEFAULT_BITRATE 128
175 #define DEFAULT_CBR FALSE
176 #define DEFAULT_QUALITY 4
177 #define DEFAULT_ENCODING_ENGINE_QUALITY LAMEMP3ENC_ENCODING_ENGINE_QUALITY_STANDARD
178 #define DEFAULT_MONO FALSE
180 static gboolean gst_lamemp3enc_start (GstAudioEncoder * enc);
181 static gboolean gst_lamemp3enc_stop (GstAudioEncoder * enc);
182 static gboolean gst_lamemp3enc_set_format (GstAudioEncoder * enc,
183 GstAudioInfo * info);
184 static GstFlowReturn gst_lamemp3enc_handle_frame (GstAudioEncoder * enc,
186 static void gst_lamemp3enc_flush (GstAudioEncoder * enc);
188 static void gst_lamemp3enc_set_property (GObject * object, guint prop_id,
189 const GValue * value, GParamSpec * pspec);
190 static void gst_lamemp3enc_get_property (GObject * object, guint prop_id,
191 GValue * value, GParamSpec * pspec);
192 static gboolean gst_lamemp3enc_setup (GstLameMP3Enc * lame, GstTagList ** tags);
194 #define gst_lamemp3enc_parent_class parent_class
195 G_DEFINE_TYPE (GstLameMP3Enc, gst_lamemp3enc, GST_TYPE_AUDIO_ENCODER);
198 gst_lamemp3enc_release_memory (GstLameMP3Enc * lame)
201 lame_close (lame->lgf);
207 gst_lamemp3enc_finalize (GObject * obj)
209 gst_lamemp3enc_release_memory (GST_LAMEMP3ENC (obj));
211 G_OBJECT_CLASS (parent_class)->finalize (obj);
215 gst_lamemp3enc_class_init (GstLameMP3EncClass * klass)
217 GObjectClass *gobject_class;
218 GstElementClass *gstelement_class;
219 GstAudioEncoderClass *base_class;
221 gobject_class = (GObjectClass *) klass;
222 gstelement_class = (GstElementClass *) klass;
223 base_class = (GstAudioEncoderClass *) klass;
225 gobject_class->set_property = gst_lamemp3enc_set_property;
226 gobject_class->get_property = gst_lamemp3enc_get_property;
227 gobject_class->finalize = gst_lamemp3enc_finalize;
229 gst_element_class_add_static_pad_template (gstelement_class,
230 &gst_lamemp3enc_src_template);
231 gst_element_class_add_static_pad_template (gstelement_class,
232 &gst_lamemp3enc_sink_template);
234 gst_element_class_set_static_metadata (gstelement_class,
235 "L.A.M.E. mp3 encoder", "Codec/Encoder/Audio",
236 "High-quality free MP3 encoder",
237 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
239 base_class->start = GST_DEBUG_FUNCPTR (gst_lamemp3enc_start);
240 base_class->stop = GST_DEBUG_FUNCPTR (gst_lamemp3enc_stop);
241 base_class->set_format = GST_DEBUG_FUNCPTR (gst_lamemp3enc_set_format);
242 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_lamemp3enc_handle_frame);
243 base_class->flush = GST_DEBUG_FUNCPTR (gst_lamemp3enc_flush);
245 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TARGET,
246 g_param_spec_enum ("target", "Target",
247 "Optimize for quality or bitrate", GST_TYPE_LAMEMP3ENC_TARGET,
249 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
251 g_param_spec_int ("bitrate", "Bitrate (kb/s)",
252 "Bitrate in kbit/sec (Only valid if target is bitrate, for CBR one "
253 "of 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, "
254 "256 or 320)", 8, 320, DEFAULT_BITRATE,
255 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
256 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CBR,
257 g_param_spec_boolean ("cbr", "CBR", "Enforce constant bitrate encoding "
258 "(Only valid if target is bitrate)", DEFAULT_CBR,
259 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
260 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
261 g_param_spec_float ("quality", "Quality",
262 "VBR Quality from 0 to 10, 0 being the best "
263 "(Only valid if target is quality)", 0.0, 9.999,
265 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 g_object_class_install_property (G_OBJECT_CLASS (klass),
267 ARG_ENCODING_ENGINE_QUALITY, g_param_spec_enum ("encoding-engine-quality",
268 "Encoding Engine Quality", "Quality/speed of the encoding engine, "
269 "this does not affect the bitrate!",
270 GST_TYPE_LAMEMP3ENC_ENCODING_ENGINE_QUALITY,
271 DEFAULT_ENCODING_ENGINE_QUALITY,
272 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
273 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MONO,
274 g_param_spec_boolean ("mono", "Mono", "Enforce mono encoding",
276 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 gst_lamemp3enc_init (GstLameMP3Enc * lame)
282 GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (lame));
286 gst_lamemp3enc_start (GstAudioEncoder * enc)
288 GstLameMP3Enc *lame = GST_LAMEMP3ENC (enc);
290 GST_DEBUG_OBJECT (lame, "start");
293 lame->adapter = gst_adapter_new ();
294 gst_adapter_clear (lame->adapter);
300 gst_lamemp3enc_stop (GstAudioEncoder * enc)
302 GstLameMP3Enc *lame = GST_LAMEMP3ENC (enc);
304 GST_DEBUG_OBJECT (lame, "stop");
307 g_object_unref (lame->adapter);
308 lame->adapter = NULL;
311 gst_lamemp3enc_release_memory (lame);
316 gst_lamemp3enc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
322 GstClockTime latency;
323 GstTagList *tags = NULL;
325 lame = GST_LAMEMP3ENC (enc);
327 /* parameters already parsed for us */
328 lame->samplerate = GST_AUDIO_INFO_RATE (info);
329 lame->num_channels = GST_AUDIO_INFO_CHANNELS (info);
331 /* but we might be asked to reconfigure, so reset */
332 gst_lamemp3enc_release_memory (lame);
334 GST_DEBUG_OBJECT (lame, "setting up lame");
335 if (!gst_lamemp3enc_setup (lame, &tags))
338 out_samplerate = lame_get_out_samplerate (lame->lgf);
339 if (out_samplerate == 0)
340 goto zero_output_rate;
341 if (out_samplerate != lame->samplerate) {
342 GST_WARNING_OBJECT (lame,
343 "output samplerate %d is different from incoming samplerate %d",
344 out_samplerate, lame->samplerate);
346 lame->out_samplerate = out_samplerate;
348 version = lame_get_version (lame->lgf);
351 else if (version == 1)
353 else if (version == 2)
357 gst_caps_new_simple ("audio/mpeg",
358 "mpegversion", G_TYPE_INT, 1,
359 "mpegaudioversion", G_TYPE_INT, version,
360 "layer", G_TYPE_INT, 3,
361 "channels", G_TYPE_INT, lame->mono ? 1 : lame->num_channels,
362 "rate", G_TYPE_INT, out_samplerate, NULL);
364 /* and use these caps */
365 gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (enc), othercaps);
366 gst_caps_unref (othercaps);
368 /* base class feedback:
369 * - we will handle buffers, just hand us all available
370 * - report latency */
371 latency = gst_util_uint64_scale_int (lame_get_framesize (lame->lgf),
372 GST_SECOND, lame->samplerate);
373 gst_audio_encoder_set_latency (enc, latency, latency);
376 gst_audio_encoder_merge_tags (enc, tags, GST_TAG_MERGE_REPLACE);
377 gst_tag_list_unref (tags);
385 gst_tag_list_unref (tags);
386 GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS, (NULL),
387 ("LAME mp3 audio decided on a zero sample rate"));
392 GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS,
393 (_("Failed to configure LAME mp3 audio encoder. Check your encoding parameters.")), (NULL));
398 /* <php-emulation-mode>three underscores for ___rate is really really really
399 * private as opposed to one underscore<php-emulation-mode> */
400 /* call this MACRO outside of the NULL state so that we have a higher chance
401 * of actually having a pipeline and bus to get the message through */
403 #define CHECK_AND_FIXUP_BITRATE(obj,param,rate) \
405 gint ___rate = rate; \
406 gint maxrate = 320; \
407 gint multiplier = 64; \
410 } else if (rate <= 64) { \
411 maxrate = 64; multiplier = 8; \
412 if ((rate % 8) != 0) ___rate = GST_ROUND_UP_8 (rate); \
413 } else if (rate <= 128) { \
414 maxrate = 128; multiplier = 16; \
415 if ((rate % 16) != 0) ___rate = GST_ROUND_UP_16 (rate); \
416 } else if (rate <= 256) { \
417 maxrate = 256; multiplier = 32; \
418 if ((rate % 32) != 0) ___rate = GST_ROUND_UP_32 (rate); \
419 } else if (rate <= 320) { \
420 maxrate = 320; multiplier = 64; \
421 if ((rate % 64) != 0) ___rate = GST_ROUND_UP_64 (rate); \
423 if (___rate != rate) { \
424 GST_ELEMENT_WARNING (obj, LIBRARY, SETTINGS, \
425 (_("The requested bitrate %d kbit/s for property '%s' " \
427 "The bitrate was changed to %d kbit/s."), rate, \
429 ("A bitrate below %d should be a multiple of %d.", \
430 maxrate, multiplier)); \
436 gst_lamemp3enc_set_property (GObject * object, guint prop_id,
437 const GValue * value, GParamSpec * pspec)
441 lame = GST_LAMEMP3ENC (object);
445 lame->target = g_value_get_enum (value);
448 lame->bitrate = g_value_get_int (value);
451 lame->cbr = g_value_get_boolean (value);
454 lame->quality = g_value_get_float (value);
456 case ARG_ENCODING_ENGINE_QUALITY:
457 lame->encoding_engine_quality = g_value_get_enum (value);
460 lame->mono = g_value_get_boolean (value);
463 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
469 gst_lamemp3enc_get_property (GObject * object, guint prop_id, GValue * value,
474 lame = GST_LAMEMP3ENC (object);
478 g_value_set_enum (value, lame->target);
481 g_value_set_int (value, lame->bitrate);
484 g_value_set_boolean (value, lame->cbr);
487 g_value_set_float (value, lame->quality);
489 case ARG_ENCODING_ENGINE_QUALITY:
490 g_value_set_enum (value, lame->encoding_engine_quality);
493 g_value_set_boolean (value, lame->mono);
496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
501 /* **** credits go to mpegaudioparse **** */
503 static const guint mp3types_bitrates[2][3][16] = {
505 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
506 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
507 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
510 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
511 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
512 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
516 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
517 {22050, 24000, 16000},
522 mp3_type_frame_length_from_header (GstLameMP3Enc * lame, guint32 header,
523 guint * put_version, guint * put_layer, guint * put_channels,
524 guint * put_bitrate, guint * put_samplerate, guint * put_mode,
528 gulong mode, samplerate, bitrate, layer, channels, padding, crc;
532 if (header & (1 << 20)) {
533 lsf = (header & (1 << 19)) ? 0 : 1;
540 version = 1 + lsf + mpg25;
542 layer = 4 - ((header >> 17) & 0x3);
544 crc = (header >> 16) & 0x1;
546 bitrate = (header >> 12) & 0xF;
547 bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
548 /* The caller has ensured we have a valid header, so bitrate can't be
550 g_assert (bitrate != 0);
552 samplerate = (header >> 10) & 0x3;
553 samplerate = mp3types_freqs[lsf + mpg25][samplerate];
555 padding = (header >> 9) & 0x1;
557 mode = (header >> 6) & 0x3;
558 channels = (mode == 3) ? 1 : 2;
562 length = 4 * ((bitrate * 12) / samplerate + padding);
565 length = (bitrate * 144) / samplerate + padding;
569 length = (bitrate * 144) / (samplerate << lsf) + padding;
573 GST_DEBUG_OBJECT (lame, "Calculated mp3 frame length of %u bytes", length);
574 GST_DEBUG_OBJECT (lame, "samplerate = %lu, bitrate = %lu, version = %lu, "
575 "layer = %lu, channels = %lu", samplerate, bitrate, version,
579 *put_version = version;
583 *put_channels = channels;
585 *put_bitrate = bitrate;
587 *put_samplerate = samplerate;
597 mp3_sync_check (GstLameMP3Enc * lame, unsigned long head)
599 GST_DEBUG_OBJECT (lame, "checking mp3 header 0x%08lx", head);
600 /* if it's not a valid sync */
601 if ((head & 0xffe00000) != 0xffe00000) {
602 GST_WARNING_OBJECT (lame, "invalid sync");
605 /* if it's an invalid MPEG version */
606 if (((head >> 19) & 3) == 0x1) {
607 GST_WARNING_OBJECT (lame, "invalid MPEG version: 0x%lx", (head >> 19) & 3);
610 /* if it's an invalid layer */
611 if (!((head >> 17) & 3)) {
612 GST_WARNING_OBJECT (lame, "invalid layer: 0x%lx", (head >> 17) & 3);
615 /* if it's an invalid bitrate */
616 if (((head >> 12) & 0xf) == 0x0) {
617 GST_WARNING_OBJECT (lame, "invalid bitrate: 0x%lx."
618 "Free format files are not supported yet", (head >> 12) & 0xf);
621 if (((head >> 12) & 0xf) == 0xf) {
622 GST_WARNING_OBJECT (lame, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
625 /* if it's an invalid samplerate */
626 if (((head >> 10) & 0x3) == 0x3) {
627 GST_WARNING_OBJECT (lame, "invalid samplerate: 0x%lx", (head >> 10) & 0x3);
631 if ((head & 0x3) == 0x2) {
632 /* Ignore this as there are some files with emphasis 0x2 that can
633 * be played fine. See BGO #537235 */
634 GST_WARNING_OBJECT (lame, "invalid emphasis: 0x%lx", head & 0x3);
640 /* **** end mpegaudioparse **** */
643 gst_lamemp3enc_finish_frames (GstLameMP3Enc * lame)
647 GstFlowReturn result = GST_FLOW_OK;
649 /* limited parsing, we don't expect to lose sync here */
650 while ((result == GST_FLOW_OK) &&
651 ((av = gst_adapter_available (lame->adapter)) > 4)) {
652 guint rate, version, layer, size;
655 guint samples_per_frame;
657 data = gst_adapter_map (lame->adapter, 4);
658 header = GST_READ_UINT32_BE (data);
659 gst_adapter_unmap (lame->adapter);
661 if (!mp3_sync_check (lame, header))
664 size = mp3_type_frame_length_from_header (lame, header, &version, &layer,
665 NULL, NULL, &rate, NULL, NULL);
667 if (G_UNLIKELY (layer != 3 || rate != lame->out_samplerate)) {
668 GST_DEBUG_OBJECT (lame,
669 "unexpected mp3 header with rate %u, version %u, layer %u",
670 rate, version, layer);
675 /* pretty likely to occur when lame is holding back on us */
676 GST_LOG_OBJECT (lame, "frame size %u (> %d)", size, av);
680 /* Account for the internal resampling, finish frame really wants to
681 * know about the number of incoming samples
683 samples_per_frame = (version == 1) ? 1152 : 576;
684 samples_per_frame *= lame->samplerate;
685 samples_per_frame /= lame->out_samplerate;
687 /* should be ok now */
688 mp3_buf = gst_adapter_take_buffer (lame->adapter, size);
689 /* number of samples for MPEG-1, layer 3 */
690 result = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (lame),
691 mp3_buf, samples_per_frame);
700 GST_ELEMENT_ERROR (lame, STREAM, ENCODE,
701 ("invalid lame mp3 sync header %08X", header), (NULL));
702 result = GST_FLOW_ERROR;
708 gst_lamemp3enc_flush_full (GstLameMP3Enc * lame, gboolean push)
713 GstFlowReturn result = GST_FLOW_OK;
719 buf = gst_buffer_new_and_alloc (7200);
720 gst_buffer_map (buf, &map, GST_MAP_WRITE);
721 size = lame_encode_flush (lame->lgf, map.data, 7200);
724 gst_buffer_unmap (buf, &map);
725 gst_buffer_resize (buf, 0, size);
726 GST_DEBUG_OBJECT (lame, "collecting final %d bytes", size);
727 gst_adapter_push (lame->adapter, buf);
729 gst_buffer_unmap (buf, &map);
730 GST_DEBUG_OBJECT (lame, "no final packet (size=%d, push=%d)", size, push);
731 gst_buffer_unref (buf);
732 result = GST_FLOW_OK;
736 result = gst_lamemp3enc_finish_frames (lame);
739 gst_adapter_clear (lame->adapter);
742 /* either way, we expect nothing left */
743 if ((av = gst_adapter_available (lame->adapter))) {
744 /* should this be more fatal ?? */
745 GST_WARNING_OBJECT (lame, "unparsed %d bytes left after flushing", av);
746 /* clean up anyway */
747 gst_adapter_clear (lame->adapter);
754 gst_lamemp3enc_flush (GstAudioEncoder * enc)
756 gst_lamemp3enc_flush_full (GST_LAMEMP3ENC (enc), FALSE);
760 gst_lamemp3enc_handle_frame (GstAudioEncoder * enc, GstBuffer * in_buf)
763 gint mp3_buffer_size, mp3_size;
765 GstFlowReturn result;
767 GstMapInfo in_map, mp3_map;
769 lame = GST_LAMEMP3ENC (enc);
771 /* squeeze remaining and push */
772 if (G_UNLIKELY (in_buf == NULL))
773 return gst_lamemp3enc_flush_full (lame, TRUE);
775 gst_buffer_map (in_buf, &in_map, GST_MAP_READ);
777 num_samples = in_map.size / 2;
779 /* allocate space for output */
780 mp3_buffer_size = 1.25 * num_samples + 7200;
781 mp3_buf = gst_buffer_new_allocate (NULL, mp3_buffer_size, NULL);
782 gst_buffer_map (mp3_buf, &mp3_map, GST_MAP_WRITE);
784 /* lame seems to be too stupid to get mono interleaved going */
785 if (lame->num_channels == 1) {
786 mp3_size = lame_encode_buffer (lame->lgf,
787 (short int *) in_map.data,
788 (short int *) in_map.data, num_samples, mp3_map.data, mp3_buffer_size);
790 mp3_size = lame_encode_buffer_interleaved (lame->lgf,
791 (short int *) in_map.data,
792 num_samples / lame->num_channels, mp3_map.data, mp3_buffer_size);
794 gst_buffer_unmap (in_buf, &in_map);
796 GST_LOG_OBJECT (lame, "encoded %" G_GSIZE_FORMAT " bytes of audio "
797 "to %d bytes of mp3", in_map.size, mp3_size);
799 if (G_LIKELY (mp3_size > 0)) {
800 /* unfortunately lame does not provide frame delineated output,
801 * so collect output and parse into frames ... */
802 gst_buffer_unmap (mp3_buf, &mp3_map);
803 gst_buffer_resize (mp3_buf, 0, mp3_size);
804 gst_adapter_push (lame->adapter, mp3_buf);
805 result = gst_lamemp3enc_finish_frames (lame);
807 gst_buffer_unmap (mp3_buf, &mp3_map);
810 g_warning ("error %d", mp3_size);
812 gst_buffer_unref (mp3_buf);
813 result = GST_FLOW_OK;
819 /* set up the encoder state */
821 gst_lamemp3enc_setup (GstLameMP3Enc * lame, GstTagList ** tags)
825 #define CHECK_ERROR(command) G_STMT_START {\
826 if ((command) < 0) { \
827 GST_ERROR_OBJECT (lame, "setup failed: " G_STRINGIFY (command)); \
829 gst_tag_list_unref (*tags); \
837 GstCaps *allowed_caps;
839 GST_DEBUG_OBJECT (lame, "starting setup");
841 lame->lgf = lame_init ();
843 if (lame->lgf == NULL)
846 *tags = gst_tag_list_new_empty ();
848 /* copy the parameters over */
849 lame_set_in_samplerate (lame->lgf, lame->samplerate);
851 /* let lame choose default samplerate unless outgoing sample rate is fixed */
852 allowed_caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (lame));
854 if (allowed_caps != NULL) {
855 GstStructure *structure;
858 structure = gst_caps_get_structure (allowed_caps, 0);
860 if (gst_structure_get_int (structure, "rate", &samplerate)) {
861 GST_DEBUG_OBJECT (lame, "Setting sample rate to %d as fixed in src caps",
863 lame_set_out_samplerate (lame->lgf, samplerate);
865 GST_DEBUG_OBJECT (lame, "Letting lame choose sample rate");
866 lame_set_out_samplerate (lame->lgf, 0);
868 gst_caps_unref (allowed_caps);
871 GST_DEBUG_OBJECT (lame, "No peer yet, letting lame choose sample rate");
872 lame_set_out_samplerate (lame->lgf, 0);
875 CHECK_ERROR (lame_set_num_channels (lame->lgf, lame->num_channels));
876 CHECK_ERROR (lame_set_bWriteVbrTag (lame->lgf, 0));
878 if (lame->target == LAMEMP3ENC_TARGET_QUALITY) {
879 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_default));
880 CHECK_ERROR (lame_set_VBR_quality (lame->lgf, lame->quality));
883 CHECK_AND_FIXUP_BITRATE (lame, "bitrate", lame->bitrate);
884 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_off));
885 CHECK_ERROR (lame_set_brate (lame->lgf, lame->bitrate));
887 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_abr));
888 CHECK_ERROR (lame_set_VBR_mean_bitrate_kbps (lame->lgf, lame->bitrate));
890 gst_tag_list_add (*tags, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
891 lame->bitrate * 1000, NULL);
894 if (lame->encoding_engine_quality == LAMEMP3ENC_ENCODING_ENGINE_QUALITY_FAST)
895 CHECK_ERROR (lame_set_quality (lame->lgf, 7));
896 else if (lame->encoding_engine_quality ==
897 LAMEMP3ENC_ENCODING_ENGINE_QUALITY_HIGH)
898 CHECK_ERROR (lame_set_quality (lame->lgf, 2));
902 CHECK_ERROR (lame_set_mode (lame->lgf, MONO));
904 /* initialize the lame encoder */
905 if ((retval = lame_init_params (lame->lgf)) >= 0) {
906 /* FIXME: it would be nice to print out the mode here */
908 ("lame encoder setup (target %s, quality %f, bitrate %d, %d Hz, %d channels)",
909 (lame->target == LAMEMP3ENC_TARGET_QUALITY) ? "quality" : "bitrate",
910 lame->quality, lame->bitrate, lame->samplerate, lame->num_channels);
913 GST_ERROR_OBJECT (lame, "lame_init_params returned %d", retval);
917 GST_DEBUG_OBJECT (lame, "done with setup");
923 gst_lamemp3enc_register (GstPlugin * plugin)
925 GST_DEBUG_CATEGORY_INIT (debug, "lamemp3enc", 0, "lame mp3 encoder");
927 if (!gst_element_register (plugin, "lamemp3enc", GST_RANK_PRIMARY,
928 GST_TYPE_LAMEMP3ENC))