ec6a2fc166bbcca7589c3fed4bc444b109599ec1
[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 /*
32  * Omitted sample formats that mpg123 supports (or at least can support):
33  *  - 8bit integer signed
34  *  - 8bit integer unsigned
35  *  - a-law
36  *  - mu-law
37  *  - 64bit float
38  *
39  * The first four formats are not supported by the GstAudioDecoder base class.
40  * (The internal gst_audio_format_from_caps_structure() call fails.)
41  *
42  * The 64bit float issue is tricky. mpg123 actually decodes to "real",
43  * not necessarily to "float".
44  *
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.
47  *
48  * However, in all known installations, "real" equals 32bit float, so that's
49  * what is used.
50  */
51
52 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
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 ")
60     );
61
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,
69     GstBuffer * buffer);
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);
73
74 G_DEFINE_TYPE (GstMpg123AudioDec, gst_mpg123_audio_dec, GST_TYPE_AUDIO_DECODER);
75
76 static void
77 gst_mpg123_audio_dec_class_init (GstMpg123AudioDecClass * klass)
78 {
79   GObjectClass *object_class;
80   GstAudioDecoderClass *base_class;
81   GstElementClass *element_class;
82   GstPadTemplate *src_template;
83   int error;
84
85   GST_DEBUG_CATEGORY_INIT (mpg123_debug, "mpg123", 0, "mpg123 mp3 decoder");
86
87   object_class = G_OBJECT_CLASS (klass);
88   base_class = GST_AUDIO_DECODER_CLASS (klass);
89   element_class = GST_ELEMENT_CLASS (klass);
90
91   object_class->finalize = gst_mpg123_audio_dec_finalize;
92
93   gst_element_class_set_static_metadata (element_class,
94       "mpg123 mp3 decoder",
95       "Codec/Decoder/Audio",
96       "Decodes mp3 streams using the mpg123 library",
97       "Carlos Rafael Giani <dv@pseudoterminal.org>");
98
99   /*
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
102    */
103   {
104     const int *format_list;
105     const long *rates_list;
106     size_t num, i;
107     GString *s;
108
109     s = g_string_new ("audio/x-raw, ");
110
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));
118           break;
119         case MPG123_ENC_UNSIGNED_16:
120           g_string_append (s, (i > 0) ? ", " : "");
121           g_string_append (s, GST_AUDIO_NE (U16));
122           break;
123         case MPG123_ENC_SIGNED_24:
124           g_string_append (s, (i > 0) ? ", " : "");
125           g_string_append (s, GST_AUDIO_NE (S24));
126           break;
127         case MPG123_ENC_UNSIGNED_24:
128           g_string_append (s, (i > 0) ? ", " : "");
129           g_string_append (s, GST_AUDIO_NE (U24));
130           break;
131         case MPG123_ENC_SIGNED_32:
132           g_string_append (s, (i > 0) ? ", " : "");
133           g_string_append (s, GST_AUDIO_NE (S32));
134           break;
135         case MPG123_ENC_UNSIGNED_32:
136           g_string_append (s, (i > 0) ? ", " : "");
137           g_string_append (s, GST_AUDIO_NE (U32));
138           break;
139         case MPG123_ENC_FLOAT_32:
140           g_string_append (s, (i > 0) ? ", " : "");
141           g_string_append (s, GST_AUDIO_NE (F32));
142           break;
143         default:
144           GST_DEBUG ("Ignoring mpg123 format %d", format_list[i]);
145           break;
146       }
147     }
148     g_string_append (s, " }, ");
149
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]);
154     }
155     g_string_append (s, "}, ");
156
157     g_string_append (s, "channels = (int) [ 1, 2 ], ");
158     g_string_append (s, "layout = (string) interleaved");
159
160     src_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
161         gst_caps_from_string (s->str));
162
163     g_string_free (s, TRUE);
164   }
165
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);
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_TRACE ("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   /*
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).
220    */
221   mpg123_format_none (mpg123_decoder->handle);
222
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) */
226
227   /* Open in feed mode (= encoded data is fed manually into the handle). */
228   error = mpg123_open_feed (mpg123_decoder->handle);
229
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;
237     return FALSE;
238   }
239
240   GST_DEBUG_OBJECT (dec, "mpg123 decoder started");
241
242   return TRUE;
243 }
244
245
246 static gboolean
247 gst_mpg123_audio_dec_stop (GstAudioDecoder * dec)
248 {
249   GstMpg123AudioDec *mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
250
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;
255   }
256
257   GST_DEBUG_OBJECT (dec, "mpg123 decoder stopped");
258
259   return TRUE;
260 }
261
262
263 static GstFlowReturn
264 gst_mpg123_audio_dec_push_decoded_bytes (GstMpg123AudioDec * mpg123_decoder,
265     unsigned char const *decoded_bytes, size_t const num_decoded_bytes)
266 {
267   GstBuffer *output_buffer;
268   GstFlowReturn alloc_error;
269   GstAudioDecoder *dec;
270
271   output_buffer = NULL;
272   dec = GST_AUDIO_DECODER (mpg123_decoder);
273
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");
278     return GST_FLOW_OK;
279   }
280
281   output_buffer = gst_buffer_new_allocate (NULL, num_decoded_bytes, NULL);
282   alloc_error = (output_buffer == NULL) ? GST_FLOW_ERROR : GST_FLOW_OK;
283
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);
287   } else {
288     GstMapInfo info;
289
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);
295       else
296         memcpy (info.data, decoded_bytes, num_decoded_bytes);
297
298       gst_buffer_unmap (output_buffer, &info);
299     } else
300       GST_ERROR_OBJECT (mpg123_decoder, "Could not map buffer");
301
302     return gst_audio_decoder_finish_frame (dec, output_buffer, 1);
303   }
304 }
305
306
307 static GstFlowReturn
308 gst_mpg123_audio_dec_handle_frame (GstAudioDecoder * dec, GstBuffer * buffer)
309 {
310   GstMpg123AudioDec *mpg123_decoder;
311   int decode_error;
312   unsigned char *decoded_bytes;
313   size_t num_decoded_bytes;
314
315   if (G_UNLIKELY (!buffer))
316     return GST_FLOW_OK;
317
318   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
319
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;
325   }
326
327   /* The actual decoding */
328   {
329     unsigned char const *inmemory;
330     size_t inmemsize;
331     GstMemory *memory;
332     GstMapInfo info;
333
334     memory = gst_buffer_get_all_memory (buffer);
335     if (memory == NULL)
336       return GST_FLOW_ERROR;
337
338     if (!gst_memory_map (memory, &info, GST_MAP_READ)) {
339       gst_memory_unref (memory);
340       return GST_FLOW_ERROR;
341     }
342
343     inmemory = info.data;
344     inmemsize = info.size;
345
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);
351
352     gst_memory_unmap (memory, &info);
353     gst_memory_unref (memory);
354   }
355
356   switch (decode_error) {
357     case MPG123_NEW_FORMAT:
358       /*
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)
362        */
363
364       GST_DEBUG_OBJECT (dec,
365           "mpg123 reported a new format -> setting next srccaps");
366
367       gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes,
368           num_decoded_bytes);
369
370       /*
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
373        */
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"));
380         }
381         mpg123_decoder->has_next_audioinfo = FALSE;
382       }
383
384       break;
385
386     case MPG123_NEED_MORE:
387     case MPG123_OK:
388       return gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder,
389           decoded_bytes, num_decoded_bytes);
390
391       /* If this happens, then the upstream parser somehow missed the ending of the bitstream */
392     case MPG123_DONE:
393       GST_DEBUG_OBJECT (dec, "mpg123 is done decoding");
394       gst_mpg123_audio_dec_push_decoded_bytes (mpg123_decoder, decoded_bytes,
395           num_decoded_bytes);
396       return GST_FLOW_EOS;
397
398       /* Anything else is considered an error */
399     default:
400     {
401       GstElement *element = GST_ELEMENT (dec);
402       GST_ELEMENT_ERROR (element, STREAM, DECODE, (NULL), ("Decoding error: %s",
403               mpg123_plain_strerror (decode_error)));
404
405       return GST_FLOW_ERROR;
406     }
407   }
408
409   return GST_FLOW_OK;
410 }
411
412
413 static gboolean
414 gst_mpg123_audio_dec_set_format (GstAudioDecoder * dec, GstCaps * incoming_caps)
415 {
416 /*
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.
426
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.
432
433         STEPS:
434
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:
438         3.1. take format
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
442 */
443
444
445   int rate, channels;
446   GstMpg123AudioDec *mpg123_decoder;
447   GstCaps *allowed_srccaps;
448   guint structure_nr;
449   gboolean match_found = FALSE;
450
451   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
452
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"));
457     return FALSE;
458   }
459
460   mpg123_decoder->has_next_audioinfo = FALSE;
461
462   /* Get rate and channels from incoming_caps */
463   {
464     GstStructure *structure;
465     gboolean err = FALSE;
466
467     /* Only the first structure is used (multiple incoming structures don't make sense */
468     structure = gst_caps_get_structure (incoming_caps, 0);
469
470     if (!gst_structure_get_int (structure, "rate", &rate)) {
471       err = TRUE;
472       GST_ERROR_OBJECT (dec, "Incoming caps do not have a rate value");
473     }
474     if (!gst_structure_get_int (structure, "channels", &channels)) {
475       err = TRUE;
476       GST_ERROR_OBJECT (dec, "Incoming caps do not have a channel value");
477     }
478
479     if (err)
480       return FALSE;
481   }
482
483   /* Get the caps that are allowed by downstream */
484   {
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); */
490   }
491
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);
494       ++structure_nr) {
495     GstStructure *structure;
496     gchar const *format_str;
497     GstAudioFormat format;
498     int encoding;
499
500     structure = gst_caps_get_structure (allowed_srccaps, structure_nr);
501
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");
505       continue;
506     }
507
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);
511       continue;
512     }
513
514     switch (format) {
515       case GST_AUDIO_FORMAT_S16:
516         encoding = MPG123_ENC_SIGNED_16;
517         break;
518       case GST_AUDIO_FORMAT_S24:
519         encoding = MPG123_ENC_SIGNED_24;
520         break;
521       case GST_AUDIO_FORMAT_S32:
522         encoding = MPG123_ENC_SIGNED_32;
523         break;
524       case GST_AUDIO_FORMAT_U16:
525         encoding = MPG123_ENC_UNSIGNED_16;
526         break;
527       case GST_AUDIO_FORMAT_U24:
528         encoding = MPG123_ENC_UNSIGNED_24;
529         break;
530       case GST_AUDIO_FORMAT_U32:
531         encoding = MPG123_ENC_UNSIGNED_32;
532         break;
533       case GST_AUDIO_FORMAT_F32:
534         encoding = MPG123_ENC_FLOAT_32;
535         break;
536       default:
537         GST_DEBUG_OBJECT (dec,
538             "Format %s in srccaps is not supported", format_str);
539         continue;
540     }
541
542     {
543       int err;
544
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));
553         continue;
554       }
555     }
556
557     gst_audio_info_init (&(mpg123_decoder->next_audioinfo));
558     gst_audio_info_set_format (&(mpg123_decoder->next_audioinfo), format, rate,
559         channels, NULL);
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;
563
564     match_found = TRUE;
565
566     break;
567   }
568
569   gst_caps_unref (allowed_srccaps);
570
571   return match_found;
572 }
573
574
575 static void
576 gst_mpg123_audio_dec_flush (GstAudioDecoder * dec, gboolean hard)
577 {
578   int error;
579   GstMpg123AudioDec *mpg123_decoder;
580
581   hard = hard;
582
583   GST_DEBUG_OBJECT (dec, "Flushing decoder");
584
585   mpg123_decoder = GST_MPG123_AUDIO_DEC (dec);
586
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"));
591     return;
592   }
593
594   /* Flush by reopening the feed */
595   mpg123_close (mpg123_decoder->handle);
596   error = mpg123_open_feed (mpg123_decoder->handle);
597
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;
605   }
606
607   mpg123_decoder->has_next_audioinfo = FALSE;
608
609   /*
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
612    */
613 }
614
615 static gboolean
616 plugin_init (GstPlugin * plugin)
617 {
618   return gst_element_register (plugin, "mpg123audiodec",
619       GST_RANK_NONE, gst_mpg123_audio_dec_get_type ());
620 }
621
622 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
623     GST_VERSION_MINOR,
624     mpg123, "mp3 decoding based on the mpg123 library",
625     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)