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