1 /* MP3 decoding plugin for GStreamer using the mpg123 library
2 * Copyright (C) 2012 Carlos Rafael Giani
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "gstmpg123audiodec.h"
28 GST_DEBUG_CATEGORY_STATIC (mpg123_debug);
29 #define GST_CAT_DEFAULT mpg123_debug
32 * Omitted sample formats that mpg123 supports (or at least can support):
33 * - 8bit integer signed
34 * - 8bit integer unsigned
39 * The first four formats are not supported by the GstAudioDecoder base class.
40 * (The internal gst_audio_format_from_caps_structure() call fails.)
42 * The 64bit float issue is tricky. mpg123 actually decodes to "real",
43 * not necessarily to "float".
45 * "real" can be fixed point, 32bit float, 64bit float. There seems to be
46 * no way how to find out which one of them is actually used.
48 * However, in all known installations, "real" equals 32bit float, so that's
52 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("audio/mpeg, "
56 "mpegversion = (int) { 1 }, "
57 "layer = (int) [ 1, 3 ], "
58 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
59 "channels = (int) [ 1, 2 ], " "parsed = (boolean) true ")
62 static void gst_mpg123_audio_dec_finalize (GObject * object);
63 static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec);
64 static gboolean gst_mpg123_audio_dec_stop (GstAudioDecoder * dec);
65 static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec
66 * mpg123_decoder, unsigned char const *decoded_bytes,
67 size_t const num_decoded_bytes);
68 static GstFlowReturn gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec,
70 static gboolean gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec,
71 GstCaps * incoming_caps);
72 static void gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard);
74 G_DEFINE_TYPE (GstMpg123AudioDec, gst_mpg123_audio_dec, GST_TYPE_AUDIO_DECODER);
77 gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass)
79 GObjectClass *object_class;
80 GstAudioDecoderClass *base_class;
81 GstElementClass *element_class;
82 GstPadTemplate *src_template;
85 GST_DEBUG_CATEGORY_INIT (mpg123_debug, "mpg123", 0, "mpg123 mp3 decoder");
87 object_class = G_OBJECT_CLASS (klass);
88 base_class = GST_AUDIO_DECODER_CLASS (klass);
89 element_class = GST_ELEMENT_CLASS (klass);
91 object_class->finalize = gst_mpg123_audio_dec_finalize;
93 gst_element_class_set_static_metadata (element_class,
95 "Codec/Decoder/Audio",
96 "Decodes mp3 streams using the mpg123 library",
97 "Carlos Rafael Giani <dv@pseudoterminal.org>");
100 Not using static pad template for srccaps, since the comma-separated list of formats needs to be
101 created depending on whatever mpg123 supports
104 const int *format_list;
105 const long *rates_list;
109 s = g_string_new ("audio/x-raw, ");
111 mpg123_encodings (&format_list, &num);
112 g_string_append (s, "format = { ");
113 for (i = 0; i < num; ++i) {
114 switch (format_list[i]) {
115 case MPG123_ENC_SIGNED_16:
116 g_string_append (s, (i > 0) ? ", " : "");
117 g_string_append (s, GST_AUDIO_NE (S16));
119 case MPG123_ENC_UNSIGNED_16:
120 g_string_append (s, (i > 0) ? ", " : "");
121 g_string_append (s, GST_AUDIO_NE (U16));
123 case MPG123_ENC_SIGNED_24:
124 g_string_append (s, (i > 0) ? ", " : "");
125 g_string_append (s, GST_AUDIO_NE (S24));
127 case MPG123_ENC_UNSIGNED_24:
128 g_string_append (s, (i > 0) ? ", " : "");
129 g_string_append (s, GST_AUDIO_NE (U24));
131 case MPG123_ENC_SIGNED_32:
132 g_string_append (s, (i > 0) ? ", " : "");
133 g_string_append (s, GST_AUDIO_NE (S32));
135 case MPG123_ENC_UNSIGNED_32:
136 g_string_append (s, (i > 0) ? ", " : "");
137 g_string_append (s, GST_AUDIO_NE (U32));
139 case MPG123_ENC_FLOAT_32:
140 g_string_append (s, (i > 0) ? ", " : "");
141 g_string_append (s, GST_AUDIO_NE (F32));
144 GST_DEBUG ("Ignoring mpg123 format %d", format_list[i]);
148 g_string_append (s, " }, ");
150 mpg123_rates (&rates_list, &num);
151 g_string_append (s, "rate = (int) { ");
152 for (i = 0; i < num; ++i) {
153 g_string_append_printf (s, "%s%lu", (i > 0) ? ", " : "", rates_list[i]);
155 g_string_append (s, "}, ");
157 g_string_append (s, "channels = (int) [ 1, 2 ], ");
158 g_string_append (s, "layout = (string) interleaved");
160 src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
161 gst_caps_from_string (s->str));
163 g_string_free (s, TRUE);
166 gst_element_class_add_pad_template (element_class,
167 gst_static_pad_template_get (&sink_template));
168 gst_element_class_add_pad_template (element_class, src_template);
170 base_class->start = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_start);
171 base_class->stop = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_stop);
172 base_class->handle_frame =
173 GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_handle_frame);
174 base_class->set_format = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_set_format);
175 base_class->flush = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_flush);
177 error = mpg123_init ();
178 if (G_UNLIKELY (error != MPG123_OK))
179 GST_ERROR ("Could not initialize mpg123 library: %s",
180 mpg123_plain_strerror (error));
182 GST_TRACE ("mpg123 library initialized");
187 gst_mpg123_audio_dec_init (GstMpg123AudioDec * mpg123_decoder)
189 mpg123_decoder->handle = NULL;
194 gst_mpg123_audio_dec_finalize (GObject * object)
196 GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (object);
197 if (G_LIKELY (mpg123_decoder->handle != NULL)) {
198 mpg123_delete (mpg123_decoder->handle);
199 mpg123_decoder->handle = NULL;
205 gst_mpg123_audio_dec_start (GstAudioDecoder * dec)
207 GstMpg123AudioDec *mpg123_decoder;
210 mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
213 mpg123_decoder->handle = mpg123_new (NULL, &error);
214 mpg123_decoder->has_next_audioinfo = FALSE;
215 mpg123_decoder->frame_offset = 0;
218 Initially, the mpg123 handle comes with a set of default formats supported. This clears this set.
219 This is necessary, since only one format shall be supported (see set_format for more).
221 mpg123_format_none (mpg123_decoder->handle);
223 mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0); /* Built-in mpg123 support for gapless decoding is disabled for now, since it does not work well with seeking */
224 mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0); /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync; essential for MP3 radio streams */
225 mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0); /* Sets the resync limit to the end of the stream (e.g. don't give up prematurely) */
227 /* Open in feed mode (= encoded data is fed manually into the handle). */
228 error = mpg123_open_feed (mpg123_decoder->handle);
230 if (G_UNLIKELY (error != MPG123_OK)) {
231 GstElement *element = GST_ELEMENT (dec);
232 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL),
233 ("Error opening mpg123 feed: %s", mpg123_plain_strerror (error)));
234 mpg123_close (mpg123_decoder->handle);
235 mpg123_delete (mpg123_decoder->handle);
236 mpg123_decoder->handle = NULL;
240 GST_DEBUG_OBJECT (dec, "mpg123 decoder started");
247 gst_mpg123_audio_dec_stop (GstAudioDecoder * dec)
249 GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
251 if (G_LIKELY (mpg123_decoder->handle != NULL)) {
252 mpg123_close (mpg123_decoder->handle);
253 mpg123_delete (mpg123_decoder->handle);
254 mpg123_decoder->handle = NULL;
257 GST_DEBUG_OBJECT (dec, "mpg123 decoder stopped");
264 gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder,
265 unsigned char const *decoded_bytes, size_t const num_decoded_bytes)
267 GstBuffer *output_buffer;
268 GstFlowReturn alloc_error;
269 GstAudioDecoder *dec;
271 output_buffer = NULL;
272 dec = GST_AUDIO_DECODER (mpg123_decoder);
274 if ((num_decoded_bytes == 0) || (decoded_bytes == NULL)) {
275 /* This occurs in the first few frames, which do not carry data; once MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */
276 GST_TRACE_OBJECT (mpg123_decoder,
277 "Nothing was decoded -> no output buffer to push");
281 output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL);
282 alloc_error = (output_buffer == NULL) ? GST_FLOW_ERROR : GST_FLOW_OK;
284 if (alloc_error != GST_FLOW_OK) {
285 /* This is necessary to advance playback in time, even when nothing was decoded. */
286 return gst_audio_decoder_finish_frame (dec, NULL, 1);
290 if (gst_buffer_map (output_buffer, &info, GST_MAP_WRITE)) {
291 if (info.size != num_decoded_bytes)
292 GST_ERROR_OBJECT (mpg123_decoder,
293 "Mapped memory region has size %u instead of expected size %u",
294 info.size, num_decoded_bytes);
296 memcpy (info.data, decoded_bytes, num_decoded_bytes);
298 gst_buffer_unmap (output_buffer, &info);
300 GST_ERROR_OBJECT (mpg123_decoder, "Could not map buffer");
302 return gst_audio_decoder_finish_frame (dec, output_buffer, 1);
308 gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
310 GstMpg123AudioDec *mpg123_decoder;
312 unsigned char *decoded_bytes;
313 size_t num_decoded_bytes;
315 if (G_UNLIKELY (!buffer))
318 mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
320 if (G_UNLIKELY (mpg123_decoder->handle == NULL)) {
321 GstElement *element = GST_ELEMENT (dec);
322 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL),
323 ("mpg123 handle is NULL"));
324 return GST_FLOW_ERROR;
327 /* The actual decoding */
329 unsigned char const *inmemory;
334 memory = gst_buffer_get_all_memory (buffer);
336 return GST_FLOW_ERROR;
338 if (!gst_memory_map (memory, &info, GST_MAP_READ)) {
339 gst_memory_unref (memory);
340 return GST_FLOW_ERROR;
343 inmemory = info.data;
344 inmemsize = info.size;
346 mpg123_feed (mpg123_decoder->handle, inmemory, inmemsize);
347 decoded_bytes = NULL;
348 num_decoded_bytes = 0;
349 decode_error = mpg123_decode_frame (mpg123_decoder->handle,
350 &mpg123_decoder->frame_offset, &decoded_bytes, &num_decoded_bytes);
352 gst_memory_unmap (memory, &info);
353 gst_memory_unref (memory);
356 switch (decode_error) {
357 case MPG123_NEW_FORMAT:
359 As mentioned in gst_mpg123_audio_dec_set_format(), the next audioinfo is not set immediately;
360 instead, the code waits for mpg123 to take note of the new format, and then sets the audioinfo
361 This fixes glitches with mp3s containing several format headers (for example, first half using 44.1kHz, second half 32 kHz)
364 GST_DEBUG_OBJECT (dec,
365 "mpg123 reported a new format -> setting next srccaps");
367 gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes,
371 If there is a next audioinfo, use it, then set has_next_audioinfo to FALSE, to make sure
372 gst_audio_decoder_set_output_format() isn't called again until set_format is called by the base class
374 if (mpg123_decoder->has_next_audioinfo) {
375 if (!gst_audio_decoder_set_output_format (dec,
376 &(mpg123_decoder->next_audioinfo))) {
377 GstElement *element = GST_ELEMENT (dec);
378 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL),
379 ("Unable to set output format"));
381 mpg123_decoder->has_next_audioinfo = FALSE;
386 case MPG123_NEED_MORE:
388 return gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder,
389 decoded_bytes, num_decoded_bytes);
391 /* If this happens, then the upstream parser somehow missed the ending of the bitstream */
393 GST_DEBUG_OBJECT (dec, "mpg123 is done decoding");
394 gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes,
398 /* Anything else is considered an error */
401 GstElement *element = GST_ELEMENT (dec);
402 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Decoding error: %s",
403 mpg123_plain_strerror (decode_error)));
405 return GST_FLOW_ERROR;
414 gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps)
417 Using the parsed information upstream, and the list of allowed caps downstream, this code
418 tries to find a suitable audio info. It is important to keep in mind that the rate and number of channels
419 should never deviate from the one the bitstream has, otherwise mpg123 has to mix channels and/or
420 resample (and as its docs say, its internal resampler is very crude). The sample format, however,
421 can be chosen freely, because the MPEG specs do not mandate any special format.
422 Therefore, rate and number of channels are taken from upstream (which parsed the MPEG frames, so
423 the incoming_caps contain exactly the rate and number of channels the bitstream actually has), while
424 the sample format is chosen by trying out all caps that are allowed by downstream. This way, the output
425 is adjusted to what the downstream prefers.
427 Also, the new output audio info is not set immediately. Instead, it is considered the "next audioinfo".
428 The code waits for mpg123 to notice the new format (= when mpg123_decode_frame() returns MPG123_AUDIO_DEC_NEW_FORMAT),
429 and then sets the next audioinfo. Otherwise, the next audioinfo is set too soon, which may cause problems with
430 mp3s containing several format headers. One example would be an mp3 with the first 30 seconds using 44.1 kHz,
431 then the next 30 seconds using 32 kHz. Rare, but possible.
435 1. get rate and channels from incoming_caps
436 2. get allowed caps from src pad
437 3. for each structure in allowed caps:
439 3.2. if the combination of format with rate and channels is unsupported by mpg123, go to (3),
440 or exit with error if there are no more structures to try
441 3.3. create next audioinfo out of rate,channels,format, and exit
446 GstMpg123AudioDec *mpg123_decoder;
447 GstCaps *allowed_srccaps;
449 gboolean match_found = FALSE;
451 mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
453 if (G_UNLIKELY (mpg123_decoder->handle == NULL)) {
454 GstElement *element = GST_ELEMENT (dec);
455 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL),
456 ("mpg123 handle is NULL"));
460 mpg123_decoder->has_next_audioinfo = FALSE;
462 /* Get rate and channels from incoming_caps */
464 GstStructure *structure;
465 gboolean err = FALSE;
467 /* Only the first structure is used (multiple incoming structures don't make sense */
468 structure = gst_caps_get_structure (incoming_caps, 0);
470 if (!gst_structure_get_int (structure, "rate", &rate)) {
472 GST_ERROR_OBJECT (dec, "Incoming caps do not have a rate value");
474 if (!gst_structure_get_int (structure, "channels", &channels)) {
476 GST_ERROR_OBJECT (dec, "Incoming caps do not have a channel value");
483 /* Get the caps that are allowed by downstream */
485 GstCaps *allowed_srccaps_unnorm =
486 gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec));
487 allowed_srccaps = gst_caps_normalize (allowed_srccaps_unnorm);
488 /* TODO: this causes errors with 1.0 - perhaps a bug? */
489 /*gst_caps_unref(allowed_srccaps_unnorm); */
492 /* Go through all allowed caps, pick the first one that matches */
493 for (structure_nr = 0; structure_nr < gst_caps_get_size (allowed_srccaps);
495 GstStructure *structure;
496 gchar const *format_str;
497 GstAudioFormat format;
500 structure = gst_caps_get_structure (allowed_srccaps, structure_nr);
502 format_str = gst_structure_get_string (structure, "format");
503 if (format_str == NULL) {
504 GST_DEBUG_OBJECT (dec, "Could not get format from src caps");
508 format = gst_audio_format_from_string (format_str);
509 if (format == GST_AUDIO_FORMAT_UNKNOWN) {
510 GST_DEBUG_OBJECT (dec, "Unknown format %s", format_str);
515 case GST_AUDIO_FORMAT_S16:
516 encoding = MPG123_ENC_SIGNED_16;
518 case GST_AUDIO_FORMAT_S24:
519 encoding = MPG123_ENC_SIGNED_24;
521 case GST_AUDIO_FORMAT_S32:
522 encoding = MPG123_ENC_SIGNED_32;
524 case GST_AUDIO_FORMAT_U16:
525 encoding = MPG123_ENC_UNSIGNED_16;
527 case GST_AUDIO_FORMAT_U24:
528 encoding = MPG123_ENC_UNSIGNED_24;
530 case GST_AUDIO_FORMAT_U32:
531 encoding = MPG123_ENC_UNSIGNED_32;
533 case GST_AUDIO_FORMAT_F32:
534 encoding = MPG123_ENC_FLOAT_32;
537 GST_DEBUG_OBJECT (dec,
538 "Format %s in srccaps is not supported", format_str);
545 /* Cleanup old formats & set new one */
546 mpg123_format_none (mpg123_decoder->handle);
547 err = mpg123_format (mpg123_decoder->handle, rate, channels, encoding);
548 if (err != MPG123_OK) {
549 GST_DEBUG_OBJECT (dec,
550 "mpg123 cannot use caps %" GST_PTR_FORMAT
551 " because mpg123_format() failed: %s", structure,
552 mpg123_plain_strerror (err));
557 gst_audio_info_init (&(mpg123_decoder->next_audioinfo));
558 gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format, rate,
560 GST_DEBUG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels",
561 format_str, rate, channels);
562 mpg123_decoder->has_next_audioinfo = TRUE;
569 gst_caps_unref (allowed_srccaps);
576 gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard)
579 GstMpg123AudioDec *mpg123_decoder;
583 GST_DEBUG_OBJECT (dec, "Flushing decoder");
585 mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
587 if (G_UNLIKELY (mpg123_decoder->handle == NULL)) {
588 GstElement *element = GST_ELEMENT (dec);
589 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL),
590 ("mpg123 handle is NULL"));
594 /* Flush by reopening the feed */
595 mpg123_close (mpg123_decoder->handle);
596 error = mpg123_open_feed (mpg123_decoder->handle);
598 if (G_UNLIKELY (error != MPG123_OK)) {
599 GstElement *element = GST_ELEMENT (dec);
600 GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL),
601 ("Error reopening mpg123 feed: %s", mpg123_plain_strerror (error)));
602 mpg123_close (mpg123_decoder->handle);
603 mpg123_delete (mpg123_decoder->handle);
604 mpg123_decoder->handle = NULL;
607 mpg123_decoder->has_next_audioinfo = FALSE;
610 opening/closing feeds do not affect the format defined by the mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(),
611 and since the up/downstream caps are not expected to change here, no mpg123_format() calls are done
616 plugin_init (GstPlugin * plugin)
618 return gst_element_register (plugin, "mpg123audiodec",
619 GST_RANK_NONE, gst_mpg123_audio_dec_get_type ());
622 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
624 mpg123, "mp3 decoding based on the mpg123 library",
625 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)