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
109 {
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)
117 {
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;
130 }
132 enum
133 {
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)
142 {
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;
157 }
159 /********** Standard stuff for signals and arguments **********/
161 enum
162 {
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 void gst_lamemp3enc_base_init (gpointer g_class);
180 static void gst_lamemp3enc_class_init (GstLameMP3EncClass * klass);
181 static void gst_lamemp3enc_init (GstLameMP3Enc * gst_lame);
183 static gboolean gst_lamemp3enc_start (GstAudioEncoder * enc);
184 static gboolean gst_lamemp3enc_stop (GstAudioEncoder * enc);
185 static gboolean gst_lamemp3enc_set_format (GstAudioEncoder * enc,
186 GstAudioInfo * info);
187 static GstFlowReturn gst_lamemp3enc_handle_frame (GstAudioEncoder * enc,
188 GstBuffer * in_buf);
189 static void gst_lamemp3enc_flush (GstAudioEncoder * enc);
191 static void gst_lamemp3enc_set_property (GObject * object, guint prop_id,
192 const GValue * value, GParamSpec * pspec);
193 static void gst_lamemp3enc_get_property (GObject * object, guint prop_id,
194 GValue * value, GParamSpec * pspec);
195 static gboolean gst_lamemp3enc_setup (GstLameMP3Enc * lame, GstTagList ** tags);
197 static GstElementClass *parent_class = NULL;
199 GType
200 gst_lamemp3enc_get_type (void)
201 {
202 static GType gst_lamemp3enc_type = 0;
204 if (!gst_lamemp3enc_type) {
205 static const GTypeInfo gst_lamemp3enc_info = {
206 sizeof (GstLameMP3EncClass),
207 gst_lamemp3enc_base_init,
208 NULL,
209 (GClassInitFunc) gst_lamemp3enc_class_init,
210 NULL,
211 NULL,
212 sizeof (GstLameMP3Enc),
213 0,
214 (GInstanceInitFunc) gst_lamemp3enc_init,
215 };
217 gst_lamemp3enc_type =
218 g_type_register_static (GST_TYPE_AUDIO_ENCODER, "GstLameMP3Enc",
219 &gst_lamemp3enc_info, 0);
220 }
221 return gst_lamemp3enc_type;
222 }
224 static void
225 gst_lamemp3enc_release_memory (GstLameMP3Enc * lame)
226 {
227 if (lame->lgf) {
228 lame_close (lame->lgf);
229 lame->lgf = NULL;
230 }
231 }
233 static void
234 gst_lamemp3enc_finalize (GObject * obj)
235 {
236 gst_lamemp3enc_release_memory (GST_LAMEMP3ENC (obj));
238 G_OBJECT_CLASS (parent_class)->finalize (obj);
239 }
241 static void
242 gst_lamemp3enc_base_init (gpointer g_class)
243 {
244 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
246 gst_element_class_add_pad_template (element_class,
247 gst_static_pad_template_get (&gst_lamemp3enc_src_template));
248 gst_element_class_add_pad_template (element_class,
249 gst_static_pad_template_get (&gst_lamemp3enc_sink_template));
250 gst_element_class_set_details_simple (element_class, "L.A.M.E. mp3 encoder",
251 "Codec/Encoder/Audio",
252 "High-quality free MP3 encoder",
253 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
254 }
256 static void
257 gst_lamemp3enc_class_init (GstLameMP3EncClass * klass)
258 {
259 GObjectClass *gobject_class;
260 GstElementClass *gstelement_class;
261 GstAudioEncoderClass *base_class;
263 gobject_class = (GObjectClass *) klass;
264 gstelement_class = (GstElementClass *) klass;
265 base_class = (GstAudioEncoderClass *) klass;
267 parent_class = g_type_class_peek_parent (klass);
269 gobject_class->set_property = gst_lamemp3enc_set_property;
270 gobject_class->get_property = gst_lamemp3enc_get_property;
271 gobject_class->finalize = gst_lamemp3enc_finalize;
273 base_class->start = GST_DEBUG_FUNCPTR (gst_lamemp3enc_start);
274 base_class->stop = GST_DEBUG_FUNCPTR (gst_lamemp3enc_stop);
275 base_class->set_format = GST_DEBUG_FUNCPTR (gst_lamemp3enc_set_format);
276 base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_lamemp3enc_handle_frame);
277 base_class->flush = GST_DEBUG_FUNCPTR (gst_lamemp3enc_flush);
279 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TARGET,
280 g_param_spec_enum ("target", "Target",
281 "Optimize for quality or bitrate", GST_TYPE_LAMEMP3ENC_TARGET,
282 DEFAULT_TARGET,
283 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
285 g_param_spec_int ("bitrate", "Bitrate (kb/s)",
286 "Bitrate in kbit/sec (Only valid if target is bitrate, for CBR one "
287 "of 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, "
288 "256 or 320)", 8, 320, DEFAULT_BITRATE,
289 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CBR,
291 g_param_spec_boolean ("cbr", "CBR", "Enforce constant bitrate encoding "
292 "(Only valid if target is bitrate)", DEFAULT_CBR,
293 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
295 g_param_spec_float ("quality", "Quality",
296 "VBR Quality from 0 to 10, 0 being the best "
297 "(Only valid if target is quality)", 0.0, 9.999,
298 DEFAULT_QUALITY,
299 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300 g_object_class_install_property (G_OBJECT_CLASS (klass),
301 ARG_ENCODING_ENGINE_QUALITY, g_param_spec_enum ("encoding-engine-quality",
302 "Encoding Engine Quality", "Quality/speed of the encoding engine, "
303 "this does not affect the bitrate!",
304 GST_TYPE_LAMEMP3ENC_ENCODING_ENGINE_QUALITY,
305 DEFAULT_ENCODING_ENGINE_QUALITY,
306 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MONO,
308 g_param_spec_boolean ("mono", "Mono", "Enforce mono encoding",
309 DEFAULT_MONO,
310 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 }
313 static void
314 gst_lamemp3enc_init (GstLameMP3Enc * lame)
315 {
316 }
318 static gboolean
319 gst_lamemp3enc_start (GstAudioEncoder * enc)
320 {
321 GstLameMP3Enc *lame = GST_LAMEMP3ENC (enc);
323 GST_DEBUG_OBJECT (lame, "start");
324 return TRUE;
325 }
327 static gboolean
328 gst_lamemp3enc_stop (GstAudioEncoder * enc)
329 {
330 GstLameMP3Enc *lame = GST_LAMEMP3ENC (enc);
332 GST_DEBUG_OBJECT (lame, "stop");
334 gst_lamemp3enc_release_memory (lame);
335 return TRUE;
336 }
338 static gboolean
339 gst_lamemp3enc_set_format (GstAudioEncoder * enc, GstAudioInfo * info)
340 {
341 GstLameMP3Enc *lame;
342 gint out_samplerate;
343 gint version;
344 GstCaps *othercaps;
345 GstClockTime latency;
346 GstTagList *tags = NULL;
348 lame = GST_LAMEMP3ENC (enc);
350 /* parameters already parsed for us */
351 lame->samplerate = GST_AUDIO_INFO_RATE (info);
352 lame->num_channels = GST_AUDIO_INFO_CHANNELS (info);
354 /* but we might be asked to reconfigure, so reset */
355 gst_lamemp3enc_release_memory (lame);
357 GST_DEBUG_OBJECT (lame, "setting up lame");
358 if (!gst_lamemp3enc_setup (lame, &tags))
359 goto setup_failed;
361 out_samplerate = lame_get_out_samplerate (lame->lgf);
362 if (out_samplerate == 0)
363 goto zero_output_rate;
364 if (out_samplerate != lame->samplerate) {
365 GST_WARNING_OBJECT (lame,
366 "output samplerate %d is different from incoming samplerate %d",
367 out_samplerate, lame->samplerate);
368 }
370 version = lame_get_version (lame->lgf);
371 if (version == 0)
372 version = 2;
373 else if (version == 1)
374 version = 1;
375 else if (version == 2)
376 version = 3;
378 othercaps =
379 gst_caps_new_simple ("audio/mpeg",
380 "mpegversion", G_TYPE_INT, 1,
381 "mpegaudioversion", G_TYPE_INT, version,
382 "layer", G_TYPE_INT, 3,
383 "channels", G_TYPE_INT, lame->mono ? 1 : lame->num_channels,
384 "rate", G_TYPE_INT, out_samplerate, NULL);
386 /* and use these caps */
387 gst_pad_set_caps (GST_AUDIO_ENCODER_SRC_PAD (enc), othercaps);
388 gst_caps_unref (othercaps);
390 /* base class feedback:
391 * - we will handle buffers, just hand us all available
392 * - report latency */
393 latency = gst_util_uint64_scale_int (lame_get_framesize (lame->lgf),
394 GST_SECOND, lame->samplerate);
395 gst_audio_encoder_set_latency (enc, latency, latency);
397 if (tags)
398 gst_audio_encoder_merge_tags (enc, tags, GST_TAG_MERGE_REPLACE);
400 return TRUE;
402 zero_output_rate:
403 {
404 if (tags)
405 gst_tag_list_free (tags);
406 GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS, (NULL),
407 ("LAMEMP3ENC decided on a zero sample rate"));
408 return FALSE;
409 }
410 setup_failed:
411 {
412 GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS,
413 (_("Failed to configure LAMEMP3ENC encoder. Check your encoding parameters.")), (NULL));
414 return FALSE;
415 }
416 }
418 /* <php-emulation-mode>three underscores for ___rate is really really really
419 * private as opposed to one underscore<php-emulation-mode> */
420 /* call this MACRO outside of the NULL state so that we have a higher chance
421 * of actually having a pipeline and bus to get the message through */
423 #define CHECK_AND_FIXUP_BITRATE(obj,param,rate) \
424 G_STMT_START { \
425 gint ___rate = rate; \
426 gint maxrate = 320; \
427 gint multiplier = 64; \
428 if (rate == 0) { \
429 ___rate = rate; \
430 } else if (rate <= 64) { \
431 maxrate = 64; multiplier = 8; \
432 if ((rate % 8) != 0) ___rate = GST_ROUND_UP_8 (rate); \
433 } else if (rate <= 128) { \
434 maxrate = 128; multiplier = 16; \
435 if ((rate % 16) != 0) ___rate = GST_ROUND_UP_16 (rate); \
436 } else if (rate <= 256) { \
437 maxrate = 256; multiplier = 32; \
438 if ((rate % 32) != 0) ___rate = GST_ROUND_UP_32 (rate); \
439 } else if (rate <= 320) { \
440 maxrate = 320; multiplier = 64; \
441 if ((rate % 64) != 0) ___rate = GST_ROUND_UP_64 (rate); \
442 } \
443 if (___rate != rate) { \
444 GST_ELEMENT_WARNING (obj, LIBRARY, SETTINGS, \
445 (_("The requested bitrate %d kbit/s for property '%s' " \
446 "is not allowed. " \
447 "The bitrate was changed to %d kbit/s."), rate, \
448 param, ___rate), \
449 ("A bitrate below %d should be a multiple of %d.", \
450 maxrate, multiplier)); \
451 rate = ___rate; \
452 } \
453 } G_STMT_END
455 static void
456 gst_lamemp3enc_set_property (GObject * object, guint prop_id,
457 const GValue * value, GParamSpec * pspec)
458 {
459 GstLameMP3Enc *lame;
461 lame = GST_LAMEMP3ENC (object);
463 switch (prop_id) {
464 case ARG_TARGET:
465 lame->target = g_value_get_enum (value);
466 break;
467 case ARG_BITRATE:
468 lame->bitrate = g_value_get_int (value);
469 break;
470 case ARG_CBR:
471 lame->cbr = g_value_get_boolean (value);
472 break;
473 case ARG_QUALITY:
474 lame->quality = g_value_get_float (value);
475 break;
476 case ARG_ENCODING_ENGINE_QUALITY:
477 lame->encoding_engine_quality = g_value_get_enum (value);
478 break;
479 case ARG_MONO:
480 lame->mono = g_value_get_boolean (value);
481 break;
482 default:
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484 break;
485 }
486 }
488 static void
489 gst_lamemp3enc_get_property (GObject * object, guint prop_id, GValue * value,
490 GParamSpec * pspec)
491 {
492 GstLameMP3Enc *lame;
494 lame = GST_LAMEMP3ENC (object);
496 switch (prop_id) {
497 case ARG_TARGET:
498 g_value_set_enum (value, lame->target);
499 break;
500 case ARG_BITRATE:
501 g_value_set_int (value, lame->bitrate);
502 break;
503 case ARG_CBR:
504 g_value_set_boolean (value, lame->cbr);
505 break;
506 case ARG_QUALITY:
507 g_value_set_float (value, lame->quality);
508 break;
509 case ARG_ENCODING_ENGINE_QUALITY:
510 g_value_set_enum (value, lame->encoding_engine_quality);
511 break;
512 case ARG_MONO:
513 g_value_set_boolean (value, lame->mono);
514 break;
515 default:
516 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 break;
518 }
519 }
521 static GstFlowReturn
522 gst_lamemp3enc_flush_full (GstLameMP3Enc * lame, gboolean push)
523 {
524 GstBuffer *buf;
525 gint size;
526 GstFlowReturn result = GST_FLOW_OK;
528 if (!lame->lgf)
529 return GST_FLOW_OK;
531 buf = gst_buffer_new_and_alloc (7200);
532 size = lame_encode_flush (lame->lgf, GST_BUFFER_DATA (buf), 7200);
534 if (size > 0 && push) {
535 GST_BUFFER_SIZE (buf) = size;
536 GST_DEBUG_OBJECT (lame, "pushing final packet of %u bytes", size);
537 result = gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (lame), buf, -1);
538 } else {
539 GST_DEBUG_OBJECT (lame, "no final packet (size=%d, push=%d)", size, push);
540 gst_buffer_unref (buf);
541 result = GST_FLOW_OK;
542 }
543 return result;
544 }
546 static void
547 gst_lamemp3enc_flush (GstAudioEncoder * enc)
548 {
549 gst_lamemp3enc_flush_full (GST_LAMEMP3ENC (enc), FALSE);
550 }
552 static GstFlowReturn
553 gst_lamemp3enc_handle_frame (GstAudioEncoder * enc, GstBuffer * in_buf)
554 {
555 GstLameMP3Enc *lame;
556 guchar *mp3_data;
557 gint mp3_buffer_size, mp3_size;
558 GstBuffer *mp3_buf;
559 GstFlowReturn result;
560 gint num_samples;
561 guint8 *data;
562 guint size;
564 lame = GST_LAMEMP3ENC (enc);
566 /* squeeze remaining and push */
567 if (G_UNLIKELY (in_buf == NULL))
568 return gst_lamemp3enc_flush_full (lame, TRUE);
570 data = GST_BUFFER_DATA (in_buf);
571 size = GST_BUFFER_SIZE (in_buf);
573 num_samples = size / 2;
575 /* allocate space for output */
576 mp3_buffer_size = 1.25 * num_samples + 7200;
577 mp3_buf = gst_buffer_new_and_alloc (mp3_buffer_size);
578 mp3_data = GST_BUFFER_DATA (mp3_buf);
580 /* lame seems to be too stupid to get mono interleaved going */
581 if (lame->num_channels == 1) {
582 mp3_size = lame_encode_buffer (lame->lgf,
583 (short int *) data,
584 (short int *) data, num_samples, mp3_data, mp3_buffer_size);
585 } else {
586 mp3_size = lame_encode_buffer_interleaved (lame->lgf,
587 (short int *) data,
588 num_samples / lame->num_channels, mp3_data, mp3_buffer_size);
589 }
591 GST_LOG_OBJECT (lame, "encoded %d bytes of audio to %d bytes of mp3",
592 size, mp3_size);
594 if (G_LIKELY (mp3_size > 0)) {
595 GST_BUFFER_SIZE (mp3_buf) = mp3_size;
596 result = gst_audio_encoder_finish_frame (enc, mp3_buf, -1);
597 } else {
598 if (mp3_size < 0) {
599 /* eat error ? */
600 g_warning ("error %d", mp3_size);
601 }
602 result = GST_FLOW_OK;
603 gst_buffer_unref (mp3_buf);
604 }
606 return result;
607 }
609 /* set up the encoder state */
610 static gboolean
611 gst_lamemp3enc_setup (GstLameMP3Enc * lame, GstTagList ** tags)
612 {
613 gboolean res;
615 #define CHECK_ERROR(command) G_STMT_START {\
616 if ((command) < 0) { \
617 GST_ERROR_OBJECT (lame, "setup failed: " G_STRINGIFY (command)); \
618 if (*tags) { \
619 gst_tag_list_free (*tags); \
620 *tags = NULL; \
621 } \
622 return FALSE; \
623 } \
624 }G_STMT_END
626 int retval;
627 GstCaps *allowed_caps;
629 GST_DEBUG_OBJECT (lame, "starting setup");
631 lame->lgf = lame_init ();
633 if (lame->lgf == NULL)
634 return FALSE;
636 *tags = gst_tag_list_new ();
638 /* copy the parameters over */
639 lame_set_in_samplerate (lame->lgf, lame->samplerate);
641 /* let lame choose default samplerate unless outgoing sample rate is fixed */
642 allowed_caps = gst_pad_get_allowed_caps (GST_AUDIO_ENCODER_SRC_PAD (lame));
644 if (allowed_caps != NULL) {
645 GstStructure *structure;
646 gint samplerate;
648 structure = gst_caps_get_structure (allowed_caps, 0);
650 if (gst_structure_get_int (structure, "rate", &samplerate)) {
651 GST_DEBUG_OBJECT (lame, "Setting sample rate to %d as fixed in src caps",
652 samplerate);
653 lame_set_out_samplerate (lame->lgf, samplerate);
654 } else {
655 GST_DEBUG_OBJECT (lame, "Letting lame choose sample rate");
656 lame_set_out_samplerate (lame->lgf, 0);
657 }
658 gst_caps_unref (allowed_caps);
659 allowed_caps = NULL;
660 } else {
661 GST_DEBUG_OBJECT (lame, "No peer yet, letting lame choose sample rate");
662 lame_set_out_samplerate (lame->lgf, 0);
663 }
665 CHECK_ERROR (lame_set_num_channels (lame->lgf, lame->num_channels));
666 CHECK_ERROR (lame_set_bWriteVbrTag (lame->lgf, 0));
668 if (lame->target == LAMEMP3ENC_TARGET_QUALITY) {
669 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_default));
670 CHECK_ERROR (lame_set_VBR_quality (lame->lgf, lame->quality));
671 } else {
672 if (lame->cbr) {
673 CHECK_AND_FIXUP_BITRATE (lame, "bitrate", lame->bitrate);
674 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_off));
675 CHECK_ERROR (lame_set_brate (lame->lgf, lame->bitrate));
676 } else {
677 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_abr));
678 CHECK_ERROR (lame_set_VBR_mean_bitrate_kbps (lame->lgf, lame->bitrate));
679 }
680 gst_tag_list_add (*tags, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
681 lame->bitrate * 1000, NULL);
682 }
684 if (lame->encoding_engine_quality == LAMEMP3ENC_ENCODING_ENGINE_QUALITY_FAST)
685 CHECK_ERROR (lame_set_quality (lame->lgf, 7));
686 else if (lame->encoding_engine_quality ==
687 LAMEMP3ENC_ENCODING_ENGINE_QUALITY_HIGH)
688 CHECK_ERROR (lame_set_quality (lame->lgf, 2));
689 /* else default */
691 if (lame->mono)
692 CHECK_ERROR (lame_set_mode (lame->lgf, MONO));
694 /* initialize the lame encoder */
695 if ((retval = lame_init_params (lame->lgf)) >= 0) {
696 /* FIXME: it would be nice to print out the mode here */
697 GST_INFO
698 ("lame encoder setup (target %s, quality %f, bitrate %d, %d Hz, %d channels)",
699 (lame->target == LAMEMP3ENC_TARGET_QUALITY) ? "quality" : "bitrate",
700 lame->quality, lame->bitrate, lame->samplerate, lame->num_channels);
701 res = TRUE;
702 } else {
703 GST_ERROR_OBJECT (lame, "lame_init_params returned %d", retval);
704 res = FALSE;
705 }
707 GST_DEBUG_OBJECT (lame, "done with setup");
708 return res;
709 #undef CHECK_ERROR
710 }
712 gboolean
713 gst_lamemp3enc_register (GstPlugin * plugin)
714 {
715 GST_DEBUG_CATEGORY_INIT (debug, "lamemp3enc", 0, "lame mp3 encoder");
717 if (!gst_element_register (plugin, "lamemp3enc", GST_RANK_PRIMARY,
718 GST_TYPE_LAMEMP3ENC))
719 return FALSE;
721 return TRUE;
722 }