demux: adaptive: inflate streamUrl streams
[vlc.git] / modules / codec / mpg123.c
blob4ab9d6cd5f819727a2fd0630cbd8d869a348e236
1 /*****************************************************************************
2 * mpg123.c: MPEG-1 & 2 audio layer I, II, III + MPEG 2.5 decoder
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
6 * Authors: Ludovic Fauvet <etix@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <assert.h>
33 #include <mpg123.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_aout.h>
38 #include <vlc_block.h>
39 #include <vlc_codec.h>
41 /*****************************************************************************
42 * Local prototypes
43 *****************************************************************************/
44 static int OpenDecoder( vlc_object_t * );
45 static void CloseDecoder( vlc_object_t * );
47 static unsigned int mpg123_refcount = 0;
48 static vlc_mutex_t mpg123_mutex = VLC_STATIC_MUTEX;
50 /*****************************************************************************
51 * Local structures
52 *****************************************************************************/
53 struct decoder_sys_t
55 mpg123_handle * p_handle;
56 date_t end_date;
57 block_t * p_out;
58 bool b_opened;
61 /*****************************************************************************
62 * Module descriptor
63 *****************************************************************************/
64 vlc_module_begin ()
65 set_category( CAT_INPUT )
66 set_subcategory( SUBCAT_INPUT_ACODEC )
67 set_description( N_("MPEG audio decoder using mpg123") )
68 set_capability( "audio decoder", 100 )
69 set_shortname( "mpg123" )
70 set_callbacks( OpenDecoder, CloseDecoder )
71 vlc_module_end ()
73 /*****************************************************************************
74 * MPG123Open
75 *****************************************************************************/
76 static int MPG123Open( decoder_t *p_dec )
78 decoder_sys_t *p_sys = p_dec->p_sys;
80 /* Create our mpg123 handle */
81 if( ( p_sys->p_handle = mpg123_new( NULL, NULL ) ) == NULL )
83 msg_Err( p_dec, "mpg123 error: can't create handle" );
84 return VLC_EGENERIC;
87 /* Open a new bitstream */
88 if( mpg123_open_feed( p_sys->p_handle ) != MPG123_OK )
90 msg_Err( p_dec, "mpg123 error: can't open feed" );
91 mpg123_delete( p_sys->p_handle );
92 return VLC_EGENERIC;
95 /* Disable resync stream after error */
96 mpg123_param( p_sys->p_handle, MPG123_ADD_FLAGS, MPG123_NO_RESYNC, 0 );
98 /* Setup output format */
99 mpg123_format_none( p_sys->p_handle );
101 int i_ret = MPG123_OK;
102 if( p_dec->fmt_in.audio.i_rate != 0 )
104 i_ret = mpg123_format( p_sys->p_handle, p_dec->fmt_in.audio.i_rate,
105 MPG123_MONO | MPG123_STEREO,
106 MPG123_ENC_FLOAT_32 );
108 else
110 /* The rate from the input is unknown. Tell mpg123 to accept all rates
111 * to avoid conversion on their side */
112 static const long mp3_rates[] = {
113 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
115 for( size_t i = 0;
116 i < sizeof(mp3_rates) / sizeof(*mp3_rates) && i_ret == MPG123_OK;
117 ++i )
119 i_ret = mpg123_format( p_sys->p_handle, mp3_rates[i],
120 MPG123_MONO | MPG123_STEREO,
121 MPG123_ENC_FLOAT_32 );
124 if( i_ret != MPG123_OK )
126 msg_Err( p_dec, "mpg123 error: %s",
127 mpg123_strerror( p_sys->p_handle ) );
128 mpg123_close( p_sys->p_handle );
129 mpg123_delete( p_sys->p_handle );
130 return VLC_EGENERIC;
133 p_sys->b_opened = true;
134 return VLC_SUCCESS;
137 /*****************************************************************************
138 * Flush:
139 *****************************************************************************/
140 static void Flush( decoder_t *p_dec )
142 decoder_sys_t *p_sys = p_dec->p_sys;
144 date_Set( &p_sys->end_date, 0 );
146 mpg123_close( p_sys->p_handle );
147 mpg123_delete( p_sys->p_handle );
148 p_sys->b_opened = false;
149 MPG123Open( p_dec );
152 static int UpdateAudioFormat( decoder_t *p_dec )
154 int i_err;
155 decoder_sys_t *p_sys = p_dec->p_sys;
156 struct mpg123_frameinfo frame_info;
158 /* Get details about the stream */
159 i_err = mpg123_info( p_sys->p_handle, &frame_info );
160 if( i_err != MPG123_OK )
162 msg_Err( p_dec, "mpg123_info failed: %s",
163 mpg123_plain_strerror( i_err ) );
164 return VLC_EGENERIC;
167 p_dec->fmt_out.i_bitrate = frame_info.bitrate * 1000;
169 switch( frame_info.mode )
171 case MPG123_M_STEREO:
172 case MPG123_M_JOINT:
173 p_dec->fmt_out.audio.i_original_channels =
174 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
175 break;
176 case MPG123_M_DUAL:
177 p_dec->fmt_out.audio.i_original_channels =
178 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DUALMONO;
179 break;
180 case MPG123_M_MONO:
181 p_dec->fmt_out.audio.i_original_channels = AOUT_CHAN_CENTER;
182 break;
183 default:
184 return VLC_EGENERIC;
187 p_dec->fmt_out.audio.i_physical_channels =
188 p_dec->fmt_out.audio.i_original_channels & AOUT_CHAN_PHYSMASK;
189 aout_FormatPrepare( &p_dec->fmt_out.audio );
191 /* Date management */
192 if( p_dec->fmt_out.audio.i_rate != frame_info.rate )
194 p_dec->fmt_out.audio.i_rate = frame_info.rate;
195 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
196 date_Set( &p_sys->end_date, 0 );
199 return decoder_UpdateAudioFormat( p_dec );
202 /****************************************************************************
203 * DecodeBlock: the whole thing
204 ****************************************************************************/
205 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
207 int i_err;
208 decoder_sys_t *p_sys = p_dec->p_sys;
209 block_t *p_out = NULL;
211 if( !p_sys->b_opened )
213 if( p_block )
214 block_Release( p_block );
215 return VLCDEC_ECRITICAL;
218 /* Feed input block */
219 if( p_block != NULL )
221 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
223 /* We've just started the stream, wait for the first PTS. */
224 msg_Dbg( p_dec, "waiting for PTS" );
225 goto end;
228 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
230 Flush( p_dec );
231 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
232 goto end;
235 /* Feed mpg123 with raw data */
236 i_err = mpg123_feed( p_sys->p_handle, p_block->p_buffer,
237 p_block->i_buffer );
239 if( i_err != MPG123_OK )
241 msg_Err( p_dec, "mpg123_feed failed: %s",
242 mpg123_plain_strerror( i_err ) );
243 goto end;
247 /* Fetch a new output block (if possible) */
248 if( !p_sys->p_out
249 || p_sys->p_out->i_buffer != mpg123_outblock( p_sys->p_handle ) )
251 if( p_sys->p_out )
252 block_Release( p_sys->p_out );
254 /* Keep the output buffer for next calls in case it's not used (in case
255 * of MPG123_NEED_MORE status) */
256 p_sys->p_out = block_Alloc( mpg123_outblock( p_sys->p_handle ) );
258 if( unlikely( !p_sys->p_out ) )
259 return VLCDEC_SUCCESS;
262 /* Do the actual decoding now */
263 size_t i_bytes = 0;
264 while( true )
266 /* Make mpg123 write directly into the VLC output buffer */
267 i_err = mpg123_replace_buffer( p_sys->p_handle, p_sys->p_out->p_buffer,
268 p_sys->p_out->i_buffer );
269 if( i_err != MPG123_OK )
271 msg_Err( p_dec, "could not replace buffer: %s",
272 mpg123_plain_strerror( i_err ) );
273 block_Release( p_sys->p_out );
274 p_sys->p_out = NULL;
275 return VLCDEC_SUCCESS;
278 i_err = mpg123_decode_frame( p_sys->p_handle, NULL, NULL, &i_bytes );
279 if( i_err != MPG123_OK )
281 if( i_err == MPG123_NEW_FORMAT )
283 if( UpdateAudioFormat( p_dec ) != VLC_SUCCESS )
284 goto end;
285 else
286 continue;
288 else if( i_err != MPG123_NEED_MORE )
289 msg_Err( p_dec, "mpg123_decode_frame error: %s",
290 mpg123_plain_strerror( i_err ) );
292 else if( p_dec->fmt_out.audio.i_rate == 0 )
294 msg_Warn( p_dec, "mpg123_decode_frame returned valid frame without "
295 "updating the format" );
296 if( UpdateAudioFormat( p_dec ) != VLC_SUCCESS )
297 goto end;
299 break;
302 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
303 p_block->i_pts != date_Get( &p_sys->end_date ) )
304 date_Set( &p_sys->end_date, p_block->i_pts );
306 if( i_bytes == 0 )
307 goto end;
309 assert( p_dec->fmt_out.audio.i_rate != 0 );
311 p_out = p_sys->p_out;
312 p_sys->p_out = NULL;
314 assert( p_out->i_buffer >= i_bytes );
315 p_out->i_buffer = i_bytes;
316 p_out->i_nb_samples = p_out->i_buffer * p_dec->fmt_out.audio.i_frame_length
317 / p_dec->fmt_out.audio.i_bytes_per_frame;
319 /* Configure the buffer */
320 p_out->i_dts = p_out->i_pts = date_Get( &p_sys->end_date );
321 p_out->i_length = date_Increment( &p_sys->end_date, p_out->i_nb_samples )
322 - p_out->i_pts;
323 end:
324 if( p_block )
325 block_Release( p_block );
326 if( p_out != NULL )
327 decoder_QueueAudio( p_dec, p_out );
328 return VLCDEC_SUCCESS;
332 /*****************************************************************************
333 * InitMPG123 : initialize the mpg123 library (reentrant)
334 *****************************************************************************/
335 static int InitMPG123( void )
337 int i_ret;
338 vlc_mutex_lock( &mpg123_mutex );
339 if( mpg123_refcount > 0 )
341 mpg123_refcount++;
342 vlc_mutex_unlock( &mpg123_mutex );
343 return MPG123_OK;
345 if( ( i_ret = mpg123_init() ) == MPG123_OK )
346 mpg123_refcount++;
347 vlc_mutex_unlock( &mpg123_mutex );
348 return i_ret;
351 /*****************************************************************************
352 * ExitMPG123 : close down the mpg123 library (reentrant)
353 *****************************************************************************/
354 static void ExitMPG123( void )
356 vlc_mutex_lock( &mpg123_mutex );
357 mpg123_refcount--;
358 if( mpg123_refcount == 0 )
359 mpg123_exit();
360 vlc_mutex_unlock( &mpg123_mutex );
363 /*****************************************************************************
364 * OpenDecoder :
365 *****************************************************************************/
366 static int OpenDecoder( vlc_object_t *p_this )
368 decoder_t *p_dec = (decoder_t *)p_this;
369 decoder_sys_t *p_sys;
371 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA &&
372 p_dec->fmt_in.i_codec != VLC_CODEC_MP3 )
373 return VLC_EGENERIC;
375 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
377 /* Initialize libmpg123 */
378 if( InitMPG123() != MPG123_OK )
379 return VLC_EGENERIC;
381 /* Allocate the memory needed to store the module's structure */
382 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
383 if( p_sys == NULL )
384 return VLC_ENOMEM;
386 p_sys->p_out = NULL;
387 date_Set( &p_sys->end_date, VLC_TS_INVALID );
389 if( MPG123Open( p_dec ) )
390 goto error;
392 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
393 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
394 p_dec->pf_decode = DecodeBlock;
395 p_dec->pf_flush = Flush;
397 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
398 (char *)&p_dec->fmt_in.i_codec,
399 (char *)&p_dec->fmt_out.i_codec,
400 aout_BitsPerSample( p_dec->fmt_out.i_codec ) );
402 return VLC_SUCCESS;
403 error:
404 ExitMPG123();
405 free( p_sys );
406 return VLC_EGENERIC;
409 /*****************************************************************************
410 * CloseDecoder : deallocate data structures
411 *****************************************************************************/
412 static void CloseDecoder( vlc_object_t *p_this )
414 decoder_t *p_dec = (decoder_t *)p_this;
415 decoder_sys_t *p_sys = p_dec->p_sys;
417 mpg123_close( p_sys->p_handle );
418 mpg123_delete( p_sys->p_handle );
419 ExitMPG123();
420 if( p_sys->p_out )
421 block_Release( p_sys->p_out );
422 free( p_sys );