input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / mpg123.c
blob054482832a9f62388b27b42e77f109883be78426
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 typedef struct
55 mpg123_handle * p_handle;
56 date_t end_date;
57 block_t * p_out;
58 bool b_opened;
59 } decoder_sys_t;
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, VLC_TICK_INVALID );
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_DUAL:
172 p_dec->fmt_out.audio.i_chan_mode = AOUT_CHANMODE_DUALMONO;
173 /* fall through */
174 case MPG123_M_STEREO:
175 case MPG123_M_JOINT:
176 p_dec->fmt_out.audio.i_physical_channels =
177 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
178 break;
179 case MPG123_M_MONO:
180 p_dec->fmt_out.audio.i_physical_channels = AOUT_CHAN_CENTER;
181 break;
182 default:
183 return VLC_EGENERIC;
186 aout_FormatPrepare( &p_dec->fmt_out.audio );
188 /* Date management */
189 if( p_dec->fmt_out.audio.i_rate != (unsigned int)frame_info.rate )
191 p_dec->fmt_out.audio.i_rate = (unsigned int)frame_info.rate;
192 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
195 return decoder_UpdateAudioFormat( p_dec );
198 /****************************************************************************
199 * DecodeBlock: the whole thing
200 ****************************************************************************/
201 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
203 int i_err;
204 decoder_sys_t *p_sys = p_dec->p_sys;
205 vlc_tick_t i_pts = VLC_TICK_INVALID;
207 if( !p_sys->b_opened )
209 if( p_block )
210 block_Release( p_block );
211 return VLCDEC_ECRITICAL;
214 /* Feed input block */
215 if( p_block != NULL )
217 i_pts = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
219 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
221 Flush( p_dec );
222 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
224 block_Release( p_block );
225 return VLCDEC_SUCCESS;
229 if( i_pts == VLC_TICK_INVALID &&
230 date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
232 /* We've just started the stream, wait for the first PTS. */
233 msg_Dbg( p_dec, "waiting for PTS" );
234 block_Release( p_block );
235 return VLCDEC_SUCCESS;
238 /* Feed mpg123 with raw data */
239 i_err = mpg123_feed( p_sys->p_handle, p_block->p_buffer, p_block->i_buffer );
240 block_Release( p_block );
242 if( i_err != MPG123_OK )
244 msg_Err( p_dec, "mpg123_feed failed: %s",
245 mpg123_plain_strerror( i_err ) );
246 return VLCDEC_SUCCESS;
250 while( true )
252 /* Fetch a new output block (if possible) */
253 if( !p_sys->p_out
254 || p_sys->p_out->i_buffer != mpg123_outblock( p_sys->p_handle ) )
256 if( p_sys->p_out )
257 block_Release( p_sys->p_out );
259 /* Keep the output buffer for next calls in case it's not used (in case
260 * of MPG123_NEED_MORE status) */
261 p_sys->p_out = block_Alloc( mpg123_outblock( p_sys->p_handle ) );
263 if( unlikely( !p_sys->p_out ) )
264 return VLCDEC_SUCCESS;
267 /* Do the actual decoding now */
268 size_t i_bytes = 0;
270 /* Make mpg123 write directly into the VLC output buffer */
271 i_err = mpg123_replace_buffer( p_sys->p_handle, p_sys->p_out->p_buffer,
272 p_sys->p_out->i_buffer );
273 if( i_err != MPG123_OK )
275 msg_Err( p_dec, "could not replace buffer: %s",
276 mpg123_plain_strerror( i_err ) );
277 block_Release( p_sys->p_out );
278 p_sys->p_out = NULL;
279 break;
282 i_err = mpg123_decode_frame( p_sys->p_handle, NULL, NULL, &i_bytes );
283 if( i_err != MPG123_OK && i_err != MPG123_NEED_MORE )
285 if( i_err == MPG123_NEW_FORMAT )
287 p_dec->fmt_out.audio.i_rate = 0;
289 else
291 msg_Err( p_dec, "mpg123_decode_frame error: %s",
292 mpg123_plain_strerror( i_err ) );
293 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
294 break;
298 if( i_bytes == 0 )
299 break;
301 if( p_dec->fmt_out.audio.i_rate == 0 )
303 if( UpdateAudioFormat( p_dec ) != VLC_SUCCESS )
305 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
306 break;
310 block_t *p_out = p_sys->p_out;
311 p_sys->p_out = NULL;
313 if( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
315 if( i_pts != VLC_TICK_INVALID )
317 date_Set( &p_sys->end_date, i_pts );
319 else if( p_out ) /* we need a valid date and that's not guaranteed on flush/error */
321 block_Release( p_out );
322 break;
326 if( p_out )
328 assert( p_dec->fmt_out.audio.i_rate != 0 );
329 assert( p_out->i_buffer >= i_bytes );
330 p_out->i_buffer = i_bytes;
331 p_out->i_nb_samples = p_out->i_buffer * p_dec->fmt_out.audio.i_frame_length
332 / p_dec->fmt_out.audio.i_bytes_per_frame;
334 /* Configure the buffer */
335 p_out->i_dts = p_out->i_pts = date_Get( &p_sys->end_date );
336 p_out->i_length = date_Increment( &p_sys->end_date, p_out->i_nb_samples )
337 - p_out->i_pts;
338 decoder_QueueAudio( p_dec, p_out );
342 return VLCDEC_SUCCESS;
346 /*****************************************************************************
347 * InitMPG123 : initialize the mpg123 library (reentrant)
348 *****************************************************************************/
349 static int InitMPG123( void )
351 int i_ret;
352 vlc_mutex_lock( &mpg123_mutex );
353 if( mpg123_refcount > 0 )
355 mpg123_refcount++;
356 vlc_mutex_unlock( &mpg123_mutex );
357 return MPG123_OK;
359 if( ( i_ret = mpg123_init() ) == MPG123_OK )
360 mpg123_refcount++;
361 vlc_mutex_unlock( &mpg123_mutex );
362 return i_ret;
365 /*****************************************************************************
366 * ExitMPG123 : close down the mpg123 library (reentrant)
367 *****************************************************************************/
368 static void ExitMPG123( void )
370 vlc_mutex_lock( &mpg123_mutex );
371 mpg123_refcount--;
372 if( mpg123_refcount == 0 )
373 mpg123_exit();
374 vlc_mutex_unlock( &mpg123_mutex );
377 /*****************************************************************************
378 * OpenDecoder :
379 *****************************************************************************/
380 static int OpenDecoder( vlc_object_t *p_this )
382 decoder_t *p_dec = (decoder_t *)p_this;
383 decoder_sys_t *p_sys;
385 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA &&
386 p_dec->fmt_in.i_codec != VLC_CODEC_MP3 )
387 return VLC_EGENERIC;
389 /* Initialize libmpg123 */
390 if( InitMPG123() != MPG123_OK )
391 return VLC_EGENERIC;
393 /* Allocate the memory needed to store the module's structure */
394 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
395 if( p_sys == NULL )
396 return VLC_ENOMEM;
398 p_sys->p_out = NULL;
399 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
401 if( MPG123Open( p_dec ) )
402 goto error;
404 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
405 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
406 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
407 p_dec->pf_decode = DecodeBlock;
408 p_dec->pf_flush = Flush;
410 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
411 (char *)&p_dec->fmt_in.i_codec,
412 (char *)&p_dec->fmt_out.i_codec,
413 aout_BitsPerSample( p_dec->fmt_out.i_codec ) );
415 return VLC_SUCCESS;
416 error:
417 ExitMPG123();
418 free( p_sys );
419 return VLC_EGENERIC;
422 /*****************************************************************************
423 * CloseDecoder : deallocate data structures
424 *****************************************************************************/
425 static void CloseDecoder( vlc_object_t *p_this )
427 decoder_t *p_dec = (decoder_t *)p_this;
428 decoder_sys_t *p_sys = p_dec->p_sys;
430 mpg123_close( p_sys->p_handle );
431 mpg123_delete( p_sys->p_handle );
432 ExitMPG123();
433 if( p_sys->p_out )
434 block_Release( p_sys->p_out );
435 free( p_sys );