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 void gst_lamemp3enc_set_property (GObject * object, guint prop_id,
184 const GValue * value, GParamSpec * pspec);
185 static void gst_lamemp3enc_get_property (GObject * object, guint prop_id,
186 GValue * value, GParamSpec * pspec);
187 static gboolean gst_lamemp3enc_sink_event (GstPad * pad, GstEvent * event);
188 static GstFlowReturn gst_lamemp3enc_chain (GstPad * pad, GstBuffer * buf);
189 static gboolean gst_lamemp3enc_setup (GstLameMP3Enc * lame);
190 static GstStateChangeReturn gst_lamemp3enc_change_state (GstElement * element,
191 GstStateChange transition);
193 static GstElementClass *parent_class = NULL;
195 GType
196 gst_lamemp3enc_get_type (void)
197 {
198 static GType gst_lamemp3enc_type = 0;
200 if (!gst_lamemp3enc_type) {
201 static const GTypeInfo gst_lamemp3enc_info = {
202 sizeof (GstLameMP3EncClass),
203 gst_lamemp3enc_base_init,
204 NULL,
205 (GClassInitFunc) gst_lamemp3enc_class_init,
206 NULL,
207 NULL,
208 sizeof (GstLameMP3Enc),
209 0,
210 (GInstanceInitFunc) gst_lamemp3enc_init,
211 };
212 static const GInterfaceInfo preset_info = {
213 NULL,
214 NULL,
215 NULL
216 };
218 gst_lamemp3enc_type =
219 g_type_register_static (GST_TYPE_ELEMENT, "GstLameMP3Enc",
220 &gst_lamemp3enc_info, 0);
221 g_type_add_interface_static (gst_lamemp3enc_type, GST_TYPE_PRESET,
222 &preset_info);
223 }
224 return gst_lamemp3enc_type;
225 }
227 static void
228 gst_lamemp3enc_release_memory (GstLameMP3Enc * lame)
229 {
230 if (lame->lgf) {
231 lame_close (lame->lgf);
232 lame->lgf = NULL;
233 }
234 }
236 static void
237 gst_lamemp3enc_finalize (GObject * obj)
238 {
239 gst_lamemp3enc_release_memory (GST_LAMEMP3ENC (obj));
241 G_OBJECT_CLASS (parent_class)->finalize (obj);
242 }
244 static void
245 gst_lamemp3enc_base_init (gpointer g_class)
246 {
247 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
249 gst_element_class_add_pad_template (element_class,
250 gst_static_pad_template_get (&gst_lamemp3enc_src_template));
251 gst_element_class_add_pad_template (element_class,
252 gst_static_pad_template_get (&gst_lamemp3enc_sink_template));
253 gst_element_class_set_details_simple (element_class, "L.A.M.E. mp3 encoder",
254 "Codec/Encoder/Audio",
255 "High-quality free MP3 encoder",
256 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
257 }
259 static void
260 gst_lamemp3enc_class_init (GstLameMP3EncClass * klass)
261 {
262 GObjectClass *gobject_class;
263 GstElementClass *gstelement_class;
265 gobject_class = (GObjectClass *) klass;
266 gstelement_class = (GstElementClass *) klass;
268 parent_class = g_type_class_peek_parent (klass);
270 gobject_class->set_property = gst_lamemp3enc_set_property;
271 gobject_class->get_property = gst_lamemp3enc_get_property;
272 gobject_class->finalize = gst_lamemp3enc_finalize;
274 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TARGET,
275 g_param_spec_enum ("target", "Target",
276 "Optimize for quality or bitrate", GST_TYPE_LAMEMP3ENC_TARGET,
277 DEFAULT_TARGET, G_PARAM_READWRITE));
278 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
279 g_param_spec_int ("bitrate", "Bitrate (kb/s)",
280 "Bitrate in kbit/sec (Only valid if target is bitrate, for CBR one "
281 "of 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, "
282 "256 or 320)", 8, 320, DEFAULT_BITRATE, G_PARAM_READWRITE));
283 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CBR,
284 g_param_spec_boolean ("cbr", "CBR", "Enforce constant bitrate encoding "
285 "(Only valid if target is bitrate)", DEFAULT_CBR, G_PARAM_READWRITE));
286 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_QUALITY,
287 g_param_spec_float ("quality", "Quality",
288 "VBR Quality from 0 to 10, 0 being the best "
289 "(Only valid if target is quality)", 0.0, 9.999,
290 DEFAULT_QUALITY, G_PARAM_READWRITE));
291 g_object_class_install_property (G_OBJECT_CLASS (klass),
292 ARG_ENCODING_ENGINE_QUALITY, g_param_spec_enum ("encoding-engine-quality",
293 "Encoding Engine Quality", "Quality/speed of the encoding engine, "
294 "this does not affect the bitrate!",
295 GST_TYPE_LAMEMP3ENC_ENCODING_ENGINE_QUALITY,
296 DEFAULT_ENCODING_ENGINE_QUALITY, G_PARAM_READWRITE));
297 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MONO,
298 g_param_spec_boolean ("mono", "Mono", "Enforce mono encoding",
299 DEFAULT_MONO, G_PARAM_READWRITE));
301 gstelement_class->change_state =
302 GST_DEBUG_FUNCPTR (gst_lamemp3enc_change_state);
303 }
305 static gboolean
306 gst_lamemp3enc_src_setcaps (GstPad * pad, GstCaps * caps)
307 {
308 GST_DEBUG_OBJECT (pad, "caps: %" GST_PTR_FORMAT, caps);
309 return TRUE;
310 }
312 static gboolean
313 gst_lamemp3enc_sink_setcaps (GstPad * pad, GstCaps * caps)
314 {
315 GstLameMP3Enc *lame;
316 gint out_samplerate;
317 gint version;
318 GstStructure *structure;
319 GstCaps *othercaps;
321 lame = GST_LAMEMP3ENC (GST_PAD_PARENT (pad));
322 structure = gst_caps_get_structure (caps, 0);
324 if (!gst_structure_get_int (structure, "rate", &lame->samplerate))
325 goto no_rate;
326 if (!gst_structure_get_int (structure, "channels", &lame->num_channels))
327 goto no_channels;
329 GST_DEBUG_OBJECT (lame, "setting up lame");
330 if (!gst_lamemp3enc_setup (lame))
331 goto setup_failed;
334 out_samplerate = lame_get_out_samplerate (lame->lgf);
335 if (out_samplerate == 0)
336 goto zero_output_rate;
337 if (out_samplerate != lame->samplerate) {
338 GST_WARNING_OBJECT (lame,
339 "output samplerate %d is different from incoming samplerate %d",
340 out_samplerate, lame->samplerate);
341 }
343 version = lame_get_version (lame->lgf);
344 if (version == 0)
345 version = 2;
346 else if (version == 1)
347 version = 1;
348 else if (version == 2)
349 version = 3;
351 othercaps =
352 gst_caps_new_simple ("audio/mpeg",
353 "mpegversion", G_TYPE_INT, 1,
354 "mpegaudioversion", G_TYPE_INT, version,
355 "layer", G_TYPE_INT, 3,
356 "channels", G_TYPE_INT, lame->mono ? 1 : lame->num_channels,
357 "rate", G_TYPE_INT, out_samplerate, NULL);
359 /* and use these caps */
360 gst_pad_set_caps (lame->srcpad, othercaps);
361 gst_caps_unref (othercaps);
363 return TRUE;
365 no_rate:
366 {
367 GST_ERROR_OBJECT (lame, "input caps have no sample rate field");
368 return FALSE;
369 }
370 no_channels:
371 {
372 GST_ERROR_OBJECT (lame, "input caps have no channels field");
373 return FALSE;
374 }
375 zero_output_rate:
376 {
377 GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS, (NULL),
378 ("LAMEMP3ENC decided on a zero sample rate"));
379 return FALSE;
380 }
381 setup_failed:
382 {
383 GST_ELEMENT_ERROR (lame, LIBRARY, SETTINGS,
384 (_("Failed to configure LAMEMP3ENC encoder. Check your encoding parameters.")), (NULL));
385 return FALSE;
386 }
387 }
389 static gint64
390 gst_lamemp3enc_get_latency (GstLameMP3Enc * lame)
391 {
392 return gst_util_uint64_scale_int (lame_get_framesize (lame->lgf),
393 GST_SECOND, lame->samplerate);
394 }
396 static gboolean
397 gst_lamemp3enc_src_query (GstPad * pad, GstQuery * query)
398 {
399 gboolean res = TRUE;
400 GstLameMP3Enc *lame;
401 GstPad *peerpad;
403 lame = GST_LAMEMP3ENC (gst_pad_get_parent (pad));
404 peerpad = gst_pad_get_peer (GST_PAD (lame->sinkpad));
406 switch (GST_QUERY_TYPE (query)) {
407 case GST_QUERY_LATENCY:
408 {
409 if ((res = gst_pad_query (peerpad, query))) {
410 gboolean live;
411 GstClockTime min_latency, max_latency;
412 gint64 latency;
414 if (lame->lgf == NULL)
415 break;
417 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
419 latency = gst_lamemp3enc_get_latency (lame);
421 /* add our latency */
422 min_latency += latency;
423 if (max_latency != -1)
424 max_latency += latency;
426 gst_query_set_latency (query, live, min_latency, max_latency);
427 }
428 break;
429 }
430 default:
431 res = gst_pad_query (peerpad, query);
432 break;
433 }
435 gst_object_unref (peerpad);
436 gst_object_unref (lame);
437 return res;
438 }
440 static void
441 gst_lamemp3enc_init (GstLameMP3Enc * lame)
442 {
443 GST_DEBUG_OBJECT (lame, "starting initialization");
445 lame->sinkpad =
446 gst_pad_new_from_static_template (&gst_lamemp3enc_sink_template, "sink");
447 gst_pad_set_event_function (lame->sinkpad,
448 GST_DEBUG_FUNCPTR (gst_lamemp3enc_sink_event));
449 gst_pad_set_chain_function (lame->sinkpad,
450 GST_DEBUG_FUNCPTR (gst_lamemp3enc_chain));
451 gst_pad_set_setcaps_function (lame->sinkpad,
452 GST_DEBUG_FUNCPTR (gst_lamemp3enc_sink_setcaps));
453 gst_element_add_pad (GST_ELEMENT (lame), lame->sinkpad);
455 lame->srcpad =
456 gst_pad_new_from_static_template (&gst_lamemp3enc_src_template, "src");
457 gst_pad_set_query_function (lame->srcpad,
458 GST_DEBUG_FUNCPTR (gst_lamemp3enc_src_query));
459 gst_pad_set_setcaps_function (lame->srcpad,
460 GST_DEBUG_FUNCPTR (gst_lamemp3enc_src_setcaps));
461 gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
463 lame->samplerate = 44100;
464 lame->num_channels = 2;
465 lame->setup = FALSE;
467 /* Set default settings */
468 lame->target = DEFAULT_TARGET;
469 lame->bitrate = DEFAULT_BITRATE;
470 lame->cbr = DEFAULT_CBR;
471 lame->quality = DEFAULT_QUALITY;
472 lame->encoding_engine_quality = DEFAULT_ENCODING_ENGINE_QUALITY;
473 lame->mono = DEFAULT_MONO;
475 GST_DEBUG_OBJECT (lame, "done initializing");
476 }
478 /* <php-emulation-mode>three underscores for ___rate is really really really
479 * private as opposed to one underscore<php-emulation-mode> */
480 /* call this MACRO outside of the NULL state so that we have a higher chance
481 * of actually having a pipeline and bus to get the message through */
483 #define CHECK_AND_FIXUP_BITRATE(obj,param,rate) \
484 G_STMT_START { \
485 gint ___rate = rate; \
486 gint maxrate = 320; \
487 gint multiplier = 64; \
488 if (rate == 0) { \
489 ___rate = rate; \
490 } else if (rate <= 64) { \
491 maxrate = 64; multiplier = 8; \
492 if ((rate % 8) != 0) ___rate = GST_ROUND_UP_8 (rate); \
493 } else if (rate <= 128) { \
494 maxrate = 128; multiplier = 16; \
495 if ((rate % 16) != 0) ___rate = GST_ROUND_UP_16 (rate); \
496 } else if (rate <= 256) { \
497 maxrate = 256; multiplier = 32; \
498 if ((rate % 32) != 0) ___rate = GST_ROUND_UP_32 (rate); \
499 } else if (rate <= 320) { \
500 maxrate = 320; multiplier = 64; \
501 if ((rate % 64) != 0) ___rate = GST_ROUND_UP_64 (rate); \
502 } \
503 if (___rate != rate) { \
504 GST_ELEMENT_WARNING (obj, LIBRARY, SETTINGS, \
505 (_("The requested bitrate %d kbit/s for property '%s' " \
506 "is not allowed. " \
507 "The bitrate was changed to %d kbit/s."), rate, \
508 param, ___rate), \
509 ("A bitrate below %d should be a multiple of %d.", \
510 maxrate, multiplier)); \
511 rate = ___rate; \
512 } \
513 } G_STMT_END
515 static void
516 gst_lamemp3enc_set_property (GObject * object, guint prop_id,
517 const GValue * value, GParamSpec * pspec)
518 {
519 GstLameMP3Enc *lame;
521 lame = GST_LAMEMP3ENC (object);
523 switch (prop_id) {
524 case ARG_TARGET:
525 lame->target = g_value_get_enum (value);
526 break;
527 case ARG_BITRATE:
528 lame->bitrate = g_value_get_int (value);
529 break;
530 case ARG_CBR:
531 lame->cbr = g_value_get_boolean (value);
532 break;
533 case ARG_QUALITY:
534 lame->quality = g_value_get_float (value);
535 break;
536 case ARG_ENCODING_ENGINE_QUALITY:
537 lame->encoding_engine_quality = g_value_get_enum (value);
538 break;
539 case ARG_MONO:
540 lame->mono = g_value_get_boolean (value);
541 break;
542 default:
543 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
544 break;
545 }
546 }
548 static void
549 gst_lamemp3enc_get_property (GObject * object, guint prop_id, GValue * value,
550 GParamSpec * pspec)
551 {
552 GstLameMP3Enc *lame;
554 lame = GST_LAMEMP3ENC (object);
556 switch (prop_id) {
557 case ARG_TARGET:
558 g_value_set_enum (value, lame->target);
559 break;
560 case ARG_BITRATE:
561 g_value_set_int (value, lame->bitrate);
562 break;
563 case ARG_CBR:
564 g_value_set_boolean (value, lame->cbr);
565 break;
566 case ARG_QUALITY:
567 g_value_set_float (value, lame->quality);
568 break;
569 case ARG_ENCODING_ENGINE_QUALITY:
570 g_value_set_enum (value, lame->encoding_engine_quality);
571 break;
572 case ARG_MONO:
573 g_value_set_boolean (value, lame->mono);
574 break;
575 default:
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
577 break;
578 }
579 }
581 static gboolean
582 gst_lamemp3enc_sink_event (GstPad * pad, GstEvent * event)
583 {
584 gboolean ret;
585 GstLameMP3Enc *lame;
587 lame = GST_LAMEMP3ENC (gst_pad_get_parent (pad));
589 switch (GST_EVENT_TYPE (event)) {
590 case GST_EVENT_EOS:{
591 GST_DEBUG_OBJECT (lame, "handling EOS event");
593 if (lame->lgf != NULL) {
594 GstBuffer *buf;
595 gint size;
597 buf = gst_buffer_new_and_alloc (7200);
598 size = lame_encode_flush (lame->lgf, GST_BUFFER_DATA (buf), 7200);
600 if (size > 0 && lame->last_flow == GST_FLOW_OK) {
601 gint64 duration;
603 duration = gst_util_uint64_scale (size, 8 * GST_SECOND,
604 1000 * lame->bitrate);
606 if (lame->last_ts == GST_CLOCK_TIME_NONE) {
607 lame->last_ts = lame->eos_ts;
608 lame->last_duration = duration;
609 } else {
610 lame->last_duration += duration;
611 }
613 GST_BUFFER_TIMESTAMP (buf) = lame->last_ts;
614 GST_BUFFER_DURATION (buf) = lame->last_duration;
615 lame->last_ts = GST_CLOCK_TIME_NONE;
616 GST_BUFFER_SIZE (buf) = size;
617 GST_DEBUG_OBJECT (lame, "pushing final packet of %u bytes", size);
618 gst_buffer_set_caps (buf, GST_PAD_CAPS (lame->srcpad));
619 gst_pad_push (lame->srcpad, buf);
620 } else {
621 GST_DEBUG_OBJECT (lame, "no final packet (size=%d, last_flow=%s)",
622 size, gst_flow_get_name (lame->last_flow));
623 gst_buffer_unref (buf);
624 }
625 }
627 ret = gst_pad_event_default (pad, event);
628 break;
629 }
630 case GST_EVENT_FLUSH_START:
631 GST_DEBUG_OBJECT (lame, "handling FLUSH start event");
632 /* forward event */
633 ret = gst_pad_push_event (lame->srcpad, event);
634 break;
635 case GST_EVENT_FLUSH_STOP:
636 {
637 guchar *mp3_data = NULL;
638 gint mp3_buffer_size;
640 GST_DEBUG_OBJECT (lame, "handling FLUSH stop event");
642 if (lame->lgf) {
643 /* clear buffers if we already have lame set up */
644 mp3_buffer_size = 7200;
645 mp3_data = g_malloc (mp3_buffer_size);
646 lame_encode_flush (lame->lgf, mp3_data, mp3_buffer_size);
647 g_free (mp3_data);
648 }
650 ret = gst_pad_push_event (lame->srcpad, event);
651 break;
652 }
653 case GST_EVENT_TAG:
654 GST_DEBUG_OBJECT (lame, "ignoring TAG event, passing it on");
655 ret = gst_pad_push_event (lame->srcpad, event);
656 break;
657 default:
658 ret = gst_pad_event_default (pad, event);
659 break;
660 }
661 gst_object_unref (lame);
662 return ret;
663 }
665 static GstFlowReturn
666 gst_lamemp3enc_chain (GstPad * pad, GstBuffer * buf)
667 {
668 GstLameMP3Enc *lame;
669 guchar *mp3_data;
670 gint mp3_buffer_size, mp3_size;
671 gint64 duration;
672 GstFlowReturn result;
673 gint num_samples;
674 guint8 *data;
675 guint size;
677 lame = GST_LAMEMP3ENC (GST_PAD_PARENT (pad));
679 GST_LOG_OBJECT (lame, "entered chain");
681 if (!lame->setup)
682 goto not_setup;
684 data = GST_BUFFER_DATA (buf);
685 size = GST_BUFFER_SIZE (buf);
687 num_samples = size / 2;
689 /* allocate space for output */
690 mp3_buffer_size = 1.25 * num_samples + 7200;
691 mp3_data = g_malloc (mp3_buffer_size);
693 /* lame seems to be too stupid to get mono interleaved going */
694 if (lame->num_channels == 1) {
695 mp3_size = lame_encode_buffer (lame->lgf,
696 (short int *) data,
697 (short int *) data, num_samples, mp3_data, mp3_buffer_size);
698 } else {
699 mp3_size = lame_encode_buffer_interleaved (lame->lgf,
700 (short int *) data,
701 num_samples / lame->num_channels, mp3_data, mp3_buffer_size);
702 }
704 GST_LOG_OBJECT (lame, "encoded %d bytes of audio to %d bytes of mp3",
705 size, mp3_size);
707 duration = gst_util_uint64_scale_int (size, GST_SECOND,
708 2 * lame->samplerate * lame->num_channels);
710 if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE &&
711 GST_BUFFER_DURATION (buf) != duration) {
712 GST_DEBUG_OBJECT (lame, "incoming buffer had incorrect duration %"
713 GST_TIME_FORMAT ", outgoing buffer will have correct duration %"
714 GST_TIME_FORMAT,
715 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_TIME_ARGS (duration));
716 }
718 if (lame->last_ts == GST_CLOCK_TIME_NONE) {
719 lame->last_ts = GST_BUFFER_TIMESTAMP (buf);
720 lame->last_offs = GST_BUFFER_OFFSET (buf);
721 lame->last_duration = duration;
722 } else {
723 lame->last_duration += duration;
724 }
726 gst_buffer_unref (buf);
728 if (mp3_size < 0) {
729 g_warning ("error %d", mp3_size);
730 }
732 if (mp3_size > 0) {
733 GstBuffer *outbuf;
735 outbuf = gst_buffer_new ();
736 GST_BUFFER_DATA (outbuf) = mp3_data;
737 GST_BUFFER_MALLOCDATA (outbuf) = mp3_data;
738 GST_BUFFER_SIZE (outbuf) = mp3_size;
739 GST_BUFFER_TIMESTAMP (outbuf) = lame->last_ts;
740 GST_BUFFER_OFFSET (outbuf) = lame->last_offs;
741 GST_BUFFER_DURATION (outbuf) = lame->last_duration;
742 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (lame->srcpad));
744 result = gst_pad_push (lame->srcpad, outbuf);
745 lame->last_flow = result;
746 if (result != GST_FLOW_OK) {
747 GST_DEBUG_OBJECT (lame, "flow return: %s", gst_flow_get_name (result));
748 }
750 if (GST_CLOCK_TIME_IS_VALID (lame->last_ts))
751 lame->eos_ts = lame->last_ts + lame->last_duration;
752 else
753 lame->eos_ts = GST_CLOCK_TIME_NONE;
754 lame->last_ts = GST_CLOCK_TIME_NONE;
755 } else {
756 g_free (mp3_data);
757 result = GST_FLOW_OK;
758 }
760 return result;
762 /* ERRORS */
763 not_setup:
764 {
765 gst_buffer_unref (buf);
766 GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
767 ("encoder not initialized (input is not audio?)"));
768 return GST_FLOW_ERROR;
769 }
770 }
772 /* set up the encoder state */
773 static gboolean
774 gst_lamemp3enc_setup (GstLameMP3Enc * lame)
775 {
777 #define CHECK_ERROR(command) G_STMT_START {\
778 if ((command) < 0) { \
779 GST_ERROR_OBJECT (lame, "setup failed: " G_STRINGIFY (command)); \
780 return FALSE; \
781 } \
782 }G_STMT_END
784 int retval;
785 GstCaps *allowed_caps;
787 GST_DEBUG_OBJECT (lame, "starting setup");
789 /* check if we're already setup; if we are, we might want to check
790 * if this initialization is compatible with the previous one */
791 /* FIXME: do this */
792 if (lame->setup) {
793 GST_WARNING_OBJECT (lame, "already setup");
794 lame->setup = FALSE;
795 }
797 lame->lgf = lame_init ();
799 if (lame->lgf == NULL)
800 return FALSE;
802 /* post latency message on the bus */
803 gst_element_post_message (GST_ELEMENT (lame),
804 gst_message_new_latency (GST_OBJECT (lame)));
806 /* copy the parameters over */
807 lame_set_in_samplerate (lame->lgf, lame->samplerate);
809 /* let lame choose default samplerate unless outgoing sample rate is fixed */
810 allowed_caps = gst_pad_get_allowed_caps (lame->srcpad);
812 if (allowed_caps != NULL) {
813 GstStructure *structure;
814 gint samplerate;
816 structure = gst_caps_get_structure (allowed_caps, 0);
818 if (gst_structure_get_int (structure, "rate", &samplerate)) {
819 GST_DEBUG_OBJECT (lame, "Setting sample rate to %d as fixed in src caps",
820 samplerate);
821 lame_set_out_samplerate (lame->lgf, samplerate);
822 } else {
823 GST_DEBUG_OBJECT (lame, "Letting lame choose sample rate");
824 lame_set_out_samplerate (lame->lgf, 0);
825 }
826 gst_caps_unref (allowed_caps);
827 allowed_caps = NULL;
828 } else {
829 GST_DEBUG_OBJECT (lame, "No peer yet, letting lame choose sample rate");
830 lame_set_out_samplerate (lame->lgf, 0);
831 }
833 CHECK_ERROR (lame_set_num_channels (lame->lgf, lame->num_channels));
834 CHECK_ERROR (lame_set_bWriteVbrTag (lame->lgf, 0));
836 if (lame->target == LAMEMP3ENC_TARGET_QUALITY) {
837 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_default));
838 CHECK_ERROR (lame_set_VBR_quality (lame->lgf, lame->quality));
839 } else {
840 if (lame->cbr) {
841 CHECK_AND_FIXUP_BITRATE (lame, "bitrate", lame->bitrate);
842 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_off));
843 CHECK_ERROR (lame_set_brate (lame->lgf, lame->bitrate));
844 } else {
845 CHECK_ERROR (lame_set_VBR (lame->lgf, vbr_abr));
846 CHECK_ERROR (lame_set_VBR_mean_bitrate_kbps (lame->lgf, lame->bitrate));
847 }
848 }
850 if (lame->encoding_engine_quality == LAMEMP3ENC_ENCODING_ENGINE_QUALITY_FAST)
851 CHECK_ERROR (lame_set_quality (lame->lgf, 7));
852 else if (lame->encoding_engine_quality ==
853 LAMEMP3ENC_ENCODING_ENGINE_QUALITY_HIGH)
854 CHECK_ERROR (lame_set_quality (lame->lgf, 2));
855 /* else default */
857 if (lame->mono)
858 CHECK_ERROR (lame_set_mode (lame->lgf, MONO));
860 /* initialize the lame encoder */
861 if ((retval = lame_init_params (lame->lgf)) >= 0) {
862 lame->setup = TRUE;
863 /* FIXME: it would be nice to print out the mode here */
864 GST_INFO
865 ("lame encoder setup (target %s, quality %f, bitrate %d, %d Hz, %d channels)",
866 (lame->target == LAMEMP3ENC_TARGET_QUALITY) ? "quality" : "bitrate",
867 lame->quality, lame->bitrate, lame->samplerate, lame->num_channels);
868 } else {
869 GST_ERROR_OBJECT (lame, "lame_init_params returned %d", retval);
870 }
872 GST_DEBUG_OBJECT (lame, "done with setup");
874 return lame->setup;
875 #undef CHECK_ERROR
876 }
878 static GstStateChangeReturn
879 gst_lamemp3enc_change_state (GstElement * element, GstStateChange transition)
880 {
881 GstLameMP3Enc *lame;
882 GstStateChangeReturn result;
884 lame = GST_LAMEMP3ENC (element);
886 switch (transition) {
887 case GST_STATE_CHANGE_READY_TO_PAUSED:
888 lame->last_flow = GST_FLOW_OK;
889 lame->last_ts = GST_CLOCK_TIME_NONE;
890 lame->eos_ts = GST_CLOCK_TIME_NONE;
891 break;
892 default:
893 break;
894 }
896 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
898 switch (transition) {
899 case GST_STATE_CHANGE_READY_TO_NULL:
900 gst_lamemp3enc_release_memory (lame);
901 break;
902 default:
903 break;
904 }
906 return result;
907 }
909 gboolean
910 gst_lamemp3enc_register (GstPlugin * plugin)
911 {
912 GST_DEBUG_CATEGORY_INIT (debug, "lamemp3enc", 0, "lame mp3 encoder");
914 if (!gst_element_register (plugin, "lamemp3enc", GST_RANK_PRIMARY,
915 GST_TYPE_LAMEMP3ENC))
916 return FALSE;
918 return TRUE;
919 }