• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*  MP3 decoding plugin for GStreamer using the mpg123 library
2  *  Copyright (C) 2012 Carlos Rafael Giani
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 /**
20  * SECTION: element-mpg123audiodec
21  * @see_also: lamemp3enc, mad
22  *
23  * Audio decoder for MPEG-1 layer 1/2/3 audio data.
24  *
25  * ## Example pipelines
26  *
27  * |[
28  * gst-launch-1.0 filesrc location=music.mp3 ! mpegaudioparse ! mpg123audiodec ! audioconvert ! audioresample ! autoaudiosink
29  * ]| Decode and play the mp3 file
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35 
36 #include "gstmpg123audiodec.h"
37 
38 #include <stdlib.h>
39 #include <string.h>
40 
41 GST_DEBUG_CATEGORY_STATIC (mpg123_debug);
42 #define GST_CAT_DEFAULT mpg123_debug
43 
44 /* Omitted sample formats that mpg123 supports (or at least can support):
45  *  - 8bit integer signed
46  *  - 8bit integer unsigned
47  *  - a-law
48  *  - mu-law
49  *  - 64bit float
50  *
51  * The first four formats are not supported by the GstAudioDecoder base class.
52  * (The internal gst_audio_format_from_caps_structure() call fails.)
53  *
54  * The 64bit float issue is tricky. mpg123 actually decodes to "real",
55  * not necessarily to "float".
56  *
57  * "real" can be fixed point, 32bit float, 64bit float. There seems to be
58  * no way how to find out which one of them is actually used.
59  *
60  * However, in all known installations, "real" equals 32bit float, so that's
61  * what is used. */
62 
63 static GstStaticPadTemplate static_sink_template =
64 GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("audio/mpeg, "
68         "mpegversion = (int) 1, "
69         "layer = (int) [ 1, 3 ], "
70         "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
71         "channels = (int) [ 1, 2 ], " "parsed = (boolean) true ")
72     );
73 
74 static gboolean gst_mpg123_audio_dec_start (GstAudioDecoder * dec);
75 static gboolean gst_mpg123_audio_dec_stop (GstAudioDecoder * dec);
76 static GstFlowReturn gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec
77     * mpg123_decoder, unsigned char const *decoded_bytes,
78     size_t const num_decoded_bytes);
79 static GstFlowReturn gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec,
80     GstBuffer * input_buffer);
81 static gboolean gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec,
82     GstCaps * input_caps);
83 static void gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard);
84 
85 G_DEFINE_TYPE (GstMpg123AudioDec, gst_mpg123_audio_dec, GST_TYPE_AUDIO_DECODER);
86 GST_ELEMENT_REGISTER_DEFINE (mpg123audiodec, "mpg123audiodec",
87     GST_RANK_MARGINAL, GST_TYPE_MPG123_AUDIO_DEC);
88 
89 static void
gst_mpg123_audio_dec_class_init(GstMpg123AudioDecClass * klass)90 gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass)
91 {
92   GstAudioDecoderClass *base_class;
93   GstElementClass *element_class;
94   GstPadTemplate *src_template, *sink_template;
95   int error;
96 
97   GST_DEBUG_CATEGORY_INIT (mpg123_debug, "mpg123", 0, "mpg123 mp3 decoder");
98 
99   base_class = GST_AUDIO_DECODER_CLASS (klass);
100   element_class = GST_ELEMENT_CLASS (klass);
101 
102   gst_element_class_set_static_metadata (element_class,
103       "mpg123 mp3 decoder",
104       "Codec/Decoder/Audio",
105       "Decodes mp3 streams using the mpg123 library",
106       "Carlos Rafael Giani <dv@pseudoterminal.org>");
107 
108   /* Not using static pad template for srccaps, since the comma-separated list
109    * of formats needs to be created depending on whatever mpg123 supports */
110   {
111     const int *format_list;
112     const long *rates_list;
113     size_t num, i;
114     GString *s;
115     GstCaps *src_template_caps;
116 
117     s = g_string_new ("audio/x-raw, ");
118 
119     mpg123_encodings (&format_list, &num);
120     g_string_append (s, "format = { ");
121     for (i = 0; i < num; ++i) {
122       switch (format_list[i]) {
123         case MPG123_ENC_SIGNED_16:
124           g_string_append (s, (i > 0) ? ", " : "");
125           g_string_append (s, GST_AUDIO_NE (S16));
126           break;
127         case MPG123_ENC_UNSIGNED_16:
128           g_string_append (s, (i > 0) ? ", " : "");
129           g_string_append (s, GST_AUDIO_NE (U16));
130           break;
131         case MPG123_ENC_SIGNED_24:
132           g_string_append (s, (i > 0) ? ", " : "");
133           g_string_append (s, GST_AUDIO_NE (S24));
134           break;
135         case MPG123_ENC_UNSIGNED_24:
136           g_string_append (s, (i > 0) ? ", " : "");
137           g_string_append (s, GST_AUDIO_NE (U24));
138           break;
139         case MPG123_ENC_SIGNED_32:
140           g_string_append (s, (i > 0) ? ", " : "");
141           g_string_append (s, GST_AUDIO_NE (S32));
142           break;
143         case MPG123_ENC_UNSIGNED_32:
144           g_string_append (s, (i > 0) ? ", " : "");
145           g_string_append (s, GST_AUDIO_NE (U32));
146           break;
147         case MPG123_ENC_FLOAT_32:
148           g_string_append (s, (i > 0) ? ", " : "");
149           g_string_append (s, GST_AUDIO_NE (F32));
150           break;
151         default:
152           GST_DEBUG ("Ignoring mpg123 format %d", format_list[i]);
153           break;
154       }
155     }
156     g_string_append (s, " }, ");
157 
158     mpg123_rates (&rates_list, &num);
159     g_string_append (s, "rate = (int) { ");
160     for (i = 0; i < num; ++i) {
161       g_string_append_printf (s, "%s%lu", (i > 0) ? ", " : "", rates_list[i]);
162     }
163     g_string_append (s, "}, ");
164 
165     g_string_append (s, "channels = (int) [ 1, 2 ], ");
166     g_string_append (s, "layout = (string) interleaved");
167 
168     src_template_caps = gst_caps_from_string (s->str);
169     src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
170         src_template_caps);
171     gst_caps_unref (src_template_caps);
172 
173     g_string_free (s, TRUE);
174   }
175 
176   sink_template = gst_static_pad_template_get (&static_sink_template);
177 
178   gst_element_class_add_pad_template (element_class, sink_template);
179   gst_element_class_add_pad_template (element_class, src_template);
180 
181   base_class->start = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_start);
182   base_class->stop = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_stop);
183   base_class->handle_frame =
184       GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_handle_frame);
185   base_class->set_format = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_set_format);
186   base_class->flush = GST_DEBUG_FUNCPTR (gst_mpg123_audio_dec_flush);
187 
188   error = mpg123_init ();
189   if (G_UNLIKELY (error != MPG123_OK))
190     GST_ERROR ("Could not initialize mpg123 library: %s",
191         mpg123_plain_strerror (error));
192   else
193     GST_INFO ("mpg123 library initialized");
194 }
195 
196 
197 void
gst_mpg123_audio_dec_init(GstMpg123AudioDec * mpg123_decoder)198 gst_mpg123_audio_dec_init (GstMpg123AudioDec * mpg123_decoder)
199 {
200   mpg123_decoder->handle = NULL;
201   gst_audio_decoder_set_needs_format (GST_AUDIO_DECODER (mpg123_decoder), TRUE);
202   gst_audio_decoder_set_use_default_pad_acceptcaps (GST_AUDIO_DECODER_CAST
203       (mpg123_decoder), TRUE);
204   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_DECODER_SINK_PAD (mpg123_decoder));
205 }
206 
207 
208 static gboolean
gst_mpg123_audio_dec_start(GstAudioDecoder * dec)209 gst_mpg123_audio_dec_start (GstAudioDecoder * dec)
210 {
211   GstMpg123AudioDec *mpg123_decoder;
212   int error;
213 
214   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
215   error = 0;
216 
217   mpg123_decoder->handle = mpg123_new (NULL, &error);
218   mpg123_decoder->has_next_audioinfo = FALSE;
219   mpg123_decoder->frame_offset = 0;
220 
221   /* Initially, the mpg123 handle comes with a set of default formats
222    * supported. This clears this set.  This is necessary, since only one
223    * format shall be supported (see set_format for more). */
224   mpg123_format_none (mpg123_decoder->handle);
225 
226   /* Built-in mpg123 support for gapless decoding is disabled for now,
227    * since it does not work well with seeking */
228   mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0);
229   /* Tells mpg123 to use a small read-ahead buffer for better MPEG sync;
230    * essential for MP3 radio streams */
231   mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_SEEKBUFFER, 0);
232   /* Sets the resync limit to the end of the stream (otherwise mpg123 may give
233    * up on decoding prematurely, especially with mp3 web radios) */
234   mpg123_param (mpg123_decoder->handle, MPG123_RESYNC_LIMIT, -1, 0);
235 #if MPG123_API_VERSION >= 36
236   /* The precise API version where MPG123_AUTO_RESAMPLE appeared is
237    * somewhere between 29 and 36 */
238   /* Don't let mpg123 resample output */
239   mpg123_param (mpg123_decoder->handle, MPG123_REMOVE_FLAGS,
240       MPG123_AUTO_RESAMPLE, 0);
241 #endif
242   /* Don't let mpg123 print messages to stdout/stderr */
243   mpg123_param (mpg123_decoder->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0);
244 
245   /* Open in feed mode (= encoded data is fed manually into the handle). */
246   error = mpg123_open_feed (mpg123_decoder->handle);
247 
248   if (G_UNLIKELY (error != MPG123_OK)) {
249     GST_ELEMENT_ERROR (dec, LIBRARY, INIT, (NULL),
250         ("%s", mpg123_strerror (mpg123_decoder->handle)));
251     mpg123_close (mpg123_decoder->handle);
252     mpg123_delete (mpg123_decoder->handle);
253     mpg123_decoder->handle = NULL;
254     return FALSE;
255   }
256 
257   GST_INFO_OBJECT (dec, "mpg123 decoder started");
258 
259   return TRUE;
260 }
261 
262 
263 static gboolean
gst_mpg123_audio_dec_stop(GstAudioDecoder * dec)264 gst_mpg123_audio_dec_stop (GstAudioDecoder * dec)
265 {
266   GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
267 
268   if (G_LIKELY (mpg123_decoder->handle != NULL)) {
269     mpg123_close (mpg123_decoder->handle);
270     mpg123_delete (mpg123_decoder->handle);
271     mpg123_decoder->handle = NULL;
272   }
273 
274   GST_INFO_OBJECT (dec, "mpg123 decoder stopped");
275 
276   return TRUE;
277 }
278 
279 
280 static GstFlowReturn
gst_mpg123_audio_dec_push_decoded_bytes(GstMpg123AudioDec * mpg123_decoder,unsigned char const * decoded_bytes,size_t const num_decoded_bytes)281 gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder,
282     unsigned char const *decoded_bytes, size_t const num_decoded_bytes)
283 {
284   GstBuffer *output_buffer;
285   GstAudioDecoder *dec;
286 
287   output_buffer = NULL;
288   dec = GST_AUDIO_DECODER (mpg123_decoder);
289 
290   if ((num_decoded_bytes == 0) || (decoded_bytes == NULL)) {
291     /* This occurs in the first few frames, which do not carry data; once
292      * MPG123_AUDIO_DEC_NEW_FORMAT is received, the empty frames stop occurring */
293     GST_DEBUG_OBJECT (mpg123_decoder,
294         "cannot decode yet, need more data -> no output buffer to push");
295     return GST_FLOW_OK;
296   }
297 
298   output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL);
299 
300   if (output_buffer == NULL) {
301     /* This is necessary to advance playback in time,
302      * even when nothing was decoded. */
303     return gst_audio_decoder_finish_frame (dec, NULL, 1);
304   } else {
305     GstMapInfo info;
306 
307     if (gst_buffer_map (output_buffer, &info, GST_MAP_WRITE)) {
308       memcpy (info.data, decoded_bytes, num_decoded_bytes);
309       gst_buffer_unmap (output_buffer, &info);
310     } else {
311       GST_ERROR_OBJECT (mpg123_decoder, "gst_buffer_map() returned NULL");
312       gst_buffer_unref (output_buffer);
313       output_buffer = NULL;
314     }
315 
316     return gst_audio_decoder_finish_frame (dec, output_buffer, 1);
317   }
318 }
319 
320 
321 static GstFlowReturn
gst_mpg123_audio_dec_handle_frame(GstAudioDecoder * dec,GstBuffer * input_buffer)322 gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec,
323     GstBuffer * input_buffer)
324 {
325   GstMpg123AudioDec *mpg123_decoder;
326   int decode_error;
327   unsigned char *decoded_bytes;
328   size_t num_decoded_bytes;
329   GstFlowReturn retval;
330 
331   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
332 
333   g_assert (mpg123_decoder->handle != NULL);
334 
335   /* The actual decoding */
336   {
337     /* feed input data (if there is any) */
338     if (G_LIKELY (input_buffer != NULL)) {
339       GstMapInfo info;
340 
341       if (gst_buffer_map (input_buffer, &info, GST_MAP_READ)) {
342         mpg123_feed (mpg123_decoder->handle, info.data, info.size);
343         gst_buffer_unmap (input_buffer, &info);
344       } else {
345         GST_AUDIO_DECODER_ERROR (mpg123_decoder, 1, RESOURCE, READ, (NULL),
346             ("gst_memory_map() failed"), retval);
347         return retval;
348       }
349     }
350 
351     /* Try to decode a frame */
352     decoded_bytes = NULL;
353     num_decoded_bytes = 0;
354     decode_error = mpg123_decode_frame (mpg123_decoder->handle,
355         &mpg123_decoder->frame_offset, &decoded_bytes, &num_decoded_bytes);
356   }
357 
358   retval = GST_FLOW_OK;
359 
360   switch (decode_error) {
361     case MPG123_NEW_FORMAT:
362       /* As mentioned in gst_mpg123_audio_dec_set_format(), the next audioinfo
363        * is not set immediately; instead, the code waits for mpg123 to take
364        * note of the new format, and then sets the audioinfo. This fixes glitches
365        * with mp3s containing several format headers (for example, first half
366        * using 44.1kHz, second half 32 kHz) */
367 
368       GST_LOG_OBJECT (dec,
369           "mpg123 reported a new format -> setting next srccaps");
370 
371       gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes,
372           num_decoded_bytes);
373 
374       /* If there is a next audioinfo, use it, then set has_next_audioinfo to
375        * FALSE, to make sure gst_audio_decoder_set_output_format() isn't called
376        * again until set_format is called by the base class */
377       if (mpg123_decoder->has_next_audioinfo) {
378         if (!gst_audio_decoder_set_output_format (dec,
379                 &(mpg123_decoder->next_audioinfo))) {
380           GST_WARNING_OBJECT (dec, "Unable to set output format");
381           retval = GST_FLOW_NOT_NEGOTIATED;
382         }
383         mpg123_decoder->has_next_audioinfo = FALSE;
384       }
385 
386       break;
387 
388     case MPG123_NEED_MORE:
389     case MPG123_OK:
390       retval = gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder,
391           decoded_bytes, num_decoded_bytes);
392       break;
393 
394     case MPG123_DONE:
395       /* If this happens, then the upstream parser somehow missed the ending
396        * of the bitstream */
397       GST_LOG_OBJECT (dec, "mpg123 is done decoding");
398       gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes,
399           num_decoded_bytes);
400       retval = GST_FLOW_EOS;
401       break;
402 
403     default:
404     {
405       /* Anything else is considered an error */
406       int errcode;
407       retval = GST_FLOW_ERROR;  /* use error by default */
408       switch (decode_error) {
409         case MPG123_ERR:
410           errcode = mpg123_errcode (mpg123_decoder->handle);
411           break;
412         default:
413           errcode = decode_error;
414       }
415       switch (errcode) {
416         case MPG123_BAD_OUTFORMAT:{
417           GstCaps *input_caps =
418               gst_pad_get_current_caps (GST_AUDIO_DECODER_SINK_PAD (dec));
419           GST_ELEMENT_ERROR (dec, STREAM, FORMAT, (NULL),
420               ("Output sample format could not be used when trying to decode frame. "
421                   "This is typically caused when the input caps (often the sample "
422                   "rate) do not match the actual format of the audio data. "
423                   "Input caps: %" GST_PTR_FORMAT, input_caps)
424               );
425           gst_caps_unref (input_caps);
426           break;
427         }
428         default:{
429           char const *errmsg = mpg123_plain_strerror (errcode);
430           /* GST_AUDIO_DECODER_ERROR sets a new return value according to
431            * its estimations */
432           GST_AUDIO_DECODER_ERROR (mpg123_decoder, 1, STREAM, DECODE, (NULL),
433               ("mpg123 decoding error: %s", errmsg), retval);
434         }
435       }
436     }
437   }
438 
439   return retval;
440 }
441 
442 
443 static gboolean
gst_mpg123_audio_dec_set_format(GstAudioDecoder * dec,GstCaps * input_caps)444 gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * input_caps)
445 {
446   /* "encoding" is the sample format specifier for mpg123 */
447   int encoding;
448   int sample_rate, num_channels;
449   GstAudioFormat format;
450   GstMpg123AudioDec *mpg123_decoder;
451   gboolean retval = FALSE;
452 
453   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
454 
455   g_assert (mpg123_decoder->handle != NULL);
456 
457   mpg123_decoder->has_next_audioinfo = FALSE;
458 
459   /* Get sample rate and number of channels from input_caps */
460   {
461     GstStructure *structure;
462     gboolean err = FALSE;
463 
464     /* Only the first structure is used (multiple
465      * input caps structures don't make sense */
466     structure = gst_caps_get_structure (input_caps, 0);
467 
468     if (!gst_structure_get_int (structure, "rate", &sample_rate)) {
469       err = TRUE;
470       GST_ERROR_OBJECT (dec, "Input caps do not have a rate value");
471     }
472     if (!gst_structure_get_int (structure, "channels", &num_channels)) {
473       err = TRUE;
474       GST_ERROR_OBJECT (dec, "Input caps do not have a channel value");
475     }
476 
477     if (G_UNLIKELY (err))
478       goto done;
479   }
480 
481   /* Get sample format from the allowed src caps */
482   {
483     GstCaps *allowed_srccaps =
484         gst_pad_get_allowed_caps (GST_AUDIO_DECODER_SRC_PAD (dec));
485 
486     if (allowed_srccaps == NULL) {
487       /* srcpad is not linked (yet), so no peer information is available;
488        * just use the default sample format (16 bit signed integer) */
489       GST_DEBUG_OBJECT (mpg123_decoder,
490           "srcpad is not linked (yet) -> using S16 sample format");
491       format = GST_AUDIO_FORMAT_S16;
492       encoding = MPG123_ENC_SIGNED_16;
493     } else if (gst_caps_is_empty (allowed_srccaps)) {
494       gst_caps_unref (allowed_srccaps);
495       goto done;
496     } else {
497       gchar const *format_str;
498       GValue const *format_value;
499 
500       /* Look at the sample format values from the first structure */
501       GstStructure *structure = gst_caps_get_structure (allowed_srccaps, 0);
502       format_value = gst_structure_get_value (structure, "format");
503 
504       if (format_value == NULL) {
505         gst_caps_unref (allowed_srccaps);
506         goto done;
507       } else if (GST_VALUE_HOLDS_LIST (format_value)) {
508         /* if value is a format list, pick the first entry */
509         GValue const *fmt_list_value =
510             gst_value_list_get_value (format_value, 0);
511         format_str = g_value_get_string (fmt_list_value);
512       } else if (G_VALUE_HOLDS_STRING (format_value)) {
513         /* if value is a string, use it directly */
514         format_str = g_value_get_string (format_value);
515       } else {
516         GST_ERROR_OBJECT (mpg123_decoder, "unexpected type for 'format' field "
517             "in caps structure %" GST_PTR_FORMAT, structure);
518         gst_caps_unref (allowed_srccaps);
519         goto done;
520       }
521 
522       /* get the format value from the string */
523       format = gst_audio_format_from_string (format_str);
524       gst_caps_unref (allowed_srccaps);
525 
526       g_assert (format != GST_AUDIO_FORMAT_UNKNOWN);
527 
528       /* convert format to mpg123 encoding */
529       switch (format) {
530         case GST_AUDIO_FORMAT_S16:
531           encoding = MPG123_ENC_SIGNED_16;
532           break;
533         case GST_AUDIO_FORMAT_S24:
534           encoding = MPG123_ENC_SIGNED_24;
535           break;
536         case GST_AUDIO_FORMAT_S32:
537           encoding = MPG123_ENC_SIGNED_32;
538           break;
539         case GST_AUDIO_FORMAT_U16:
540           encoding = MPG123_ENC_UNSIGNED_16;
541           break;
542         case GST_AUDIO_FORMAT_U24:
543           encoding = MPG123_ENC_UNSIGNED_24;
544           break;
545         case GST_AUDIO_FORMAT_U32:
546           encoding = MPG123_ENC_UNSIGNED_32;
547           break;
548         case GST_AUDIO_FORMAT_F32:
549           encoding = MPG123_ENC_FLOAT_32;
550           break;
551         default:
552           g_assert_not_reached ();
553           goto done;
554       }
555     }
556   }
557 
558   /* Sample rate, number of channels, and sample format are known at this point.
559    * Set the audioinfo structure's values and the mpg123 format. */
560   {
561     int err;
562 
563     /* clear all existing format settings from the mpg123 instance */
564     mpg123_format_none (mpg123_decoder->handle);
565     /* set the chosen format */
566     err =
567         mpg123_format (mpg123_decoder->handle, sample_rate, num_channels,
568         encoding);
569 
570     if (err != MPG123_OK) {
571       GST_WARNING_OBJECT (dec,
572           "mpg123_format() failed: %s",
573           mpg123_strerror (mpg123_decoder->handle));
574     } else {
575       gst_audio_info_init (&(mpg123_decoder->next_audioinfo));
576       gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format,
577           sample_rate, num_channels, NULL);
578       GST_LOG_OBJECT (dec, "The next audio format is: %s, %u Hz, %u channels",
579           gst_audio_format_to_string (format), sample_rate, num_channels);
580       mpg123_decoder->has_next_audioinfo = TRUE;
581 
582       retval = TRUE;
583     }
584   }
585 
586 done:
587   return retval;
588 }
589 
590 
591 static void
gst_mpg123_audio_dec_flush(GstAudioDecoder * dec,gboolean hard)592 gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard)
593 {
594   int error;
595   GstMpg123AudioDec *mpg123_decoder;
596 
597   GST_LOG_OBJECT (dec, "Flushing decoder");
598 
599   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
600 
601   g_assert (mpg123_decoder->handle != NULL);
602 
603   /* Flush by reopening the feed */
604   mpg123_close (mpg123_decoder->handle);
605   error = mpg123_open_feed (mpg123_decoder->handle);
606 
607   if (G_UNLIKELY (error != MPG123_OK)) {
608     GST_ELEMENT_ERROR (dec, LIBRARY, INIT, (NULL),
609         ("Error while reopening mpg123 feed: %s",
610             mpg123_plain_strerror (error)));
611     mpg123_close (mpg123_decoder->handle);
612     mpg123_delete (mpg123_decoder->handle);
613     mpg123_decoder->handle = NULL;
614   }
615 
616   if (hard)
617     mpg123_decoder->has_next_audioinfo = FALSE;
618 
619   /* opening/closing feeds do not affect the format defined by the
620    * mpg123_format() call that was made in gst_mpg123_audio_dec_set_format(),
621    * and since the up/downstream caps are not expected to change here, no
622    * mpg123_format() calls are done */
623 }
624 
625 static gboolean
plugin_init(GstPlugin * plugin)626 plugin_init (GstPlugin * plugin)
627 {
628   return GST_ELEMENT_REGISTER (mpg123audiodec, plugin);
629 }
630 
631 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
632     GST_VERSION_MINOR,
633     mpg123, "mp3 decoding based on the mpg123 library",
634     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
635