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