mpg123: fix sign comparison about rate
[vlc.git] / modules / codec / mpg123.c
blob171254f707a66f937a1c5e8799a115e9d9461dfc
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_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 );
193 date_Set( &p_sys->end_date, 0 );
196 return decoder_UpdateAudioFormat( p_dec );
199 /****************************************************************************
200 * DecodeBlock: the whole thing
201 ****************************************************************************/
202 static int DecodeBlock( decoder_t *p_dec, block_t *p_block )
204 int i_err;
205 decoder_sys_t *p_sys = p_dec->p_sys;
206 block_t *p_out = NULL;
208 if( !p_sys->b_opened )
210 if( p_block )
211 block_Release( p_block );
212 return VLCDEC_ECRITICAL;
215 /* Feed input block */
216 if( p_block != NULL )
218 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
220 /* We've just started the stream, wait for the first PTS. */
221 msg_Dbg( p_dec, "waiting for PTS" );
222 goto end;
225 if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
227 Flush( p_dec );
228 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
229 goto end;
232 /* Feed mpg123 with raw data */
233 i_err = mpg123_feed( p_sys->p_handle, p_block->p_buffer,
234 p_block->i_buffer );
236 if( i_err != MPG123_OK )
238 msg_Err( p_dec, "mpg123_feed failed: %s",
239 mpg123_plain_strerror( i_err ) );
240 goto end;
244 /* Fetch a new output block (if possible) */
245 if( !p_sys->p_out
246 || p_sys->p_out->i_buffer != mpg123_outblock( p_sys->p_handle ) )
248 if( p_sys->p_out )
249 block_Release( p_sys->p_out );
251 /* Keep the output buffer for next calls in case it's not used (in case
252 * of MPG123_NEED_MORE status) */
253 p_sys->p_out = block_Alloc( mpg123_outblock( p_sys->p_handle ) );
255 if( unlikely( !p_sys->p_out ) )
256 return VLCDEC_SUCCESS;
259 /* Do the actual decoding now */
260 size_t i_bytes = 0;
261 while( true )
263 /* Make mpg123 write directly into the VLC output buffer */
264 i_err = mpg123_replace_buffer( p_sys->p_handle, p_sys->p_out->p_buffer,
265 p_sys->p_out->i_buffer );
266 if( i_err != MPG123_OK )
268 msg_Err( p_dec, "could not replace buffer: %s",
269 mpg123_plain_strerror( i_err ) );
270 block_Release( p_sys->p_out );
271 p_sys->p_out = NULL;
272 return VLCDEC_SUCCESS;
275 i_err = mpg123_decode_frame( p_sys->p_handle, NULL, NULL, &i_bytes );
276 if( i_err != MPG123_OK )
278 if( i_err == MPG123_NEW_FORMAT )
280 if( UpdateAudioFormat( p_dec ) != VLC_SUCCESS )
281 goto end;
282 else
283 continue;
285 else if( i_err != MPG123_NEED_MORE )
286 msg_Err( p_dec, "mpg123_decode_frame error: %s",
287 mpg123_plain_strerror( i_err ) );
289 else if( p_dec->fmt_out.audio.i_rate == 0 )
291 msg_Warn( p_dec, "mpg123_decode_frame returned valid frame without "
292 "updating the format" );
293 if( UpdateAudioFormat( p_dec ) != VLC_SUCCESS )
294 goto end;
296 break;
299 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
300 p_block->i_pts != date_Get( &p_sys->end_date ) )
301 date_Set( &p_sys->end_date, p_block->i_pts );
303 if( i_bytes == 0 )
304 goto end;
306 assert( p_dec->fmt_out.audio.i_rate != 0 );
308 p_out = p_sys->p_out;
309 p_sys->p_out = NULL;
311 assert( p_out->i_buffer >= i_bytes );
312 p_out->i_buffer = i_bytes;
313 p_out->i_nb_samples = p_out->i_buffer * p_dec->fmt_out.audio.i_frame_length
314 / p_dec->fmt_out.audio.i_bytes_per_frame;
316 /* Configure the buffer */
317 p_out->i_dts = p_out->i_pts = date_Get( &p_sys->end_date );
318 p_out->i_length = date_Increment( &p_sys->end_date, p_out->i_nb_samples )
319 - p_out->i_pts;
320 end:
321 if( p_block )
322 block_Release( p_block );
323 if( p_out != NULL )
324 decoder_QueueAudio( p_dec, p_out );
325 return VLCDEC_SUCCESS;
329 /*****************************************************************************
330 * InitMPG123 : initialize the mpg123 library (reentrant)
331 *****************************************************************************/
332 static int InitMPG123( void )
334 int i_ret;
335 vlc_mutex_lock( &mpg123_mutex );
336 if( mpg123_refcount > 0 )
338 mpg123_refcount++;
339 vlc_mutex_unlock( &mpg123_mutex );
340 return MPG123_OK;
342 if( ( i_ret = mpg123_init() ) == MPG123_OK )
343 mpg123_refcount++;
344 vlc_mutex_unlock( &mpg123_mutex );
345 return i_ret;
348 /*****************************************************************************
349 * ExitMPG123 : close down the mpg123 library (reentrant)
350 *****************************************************************************/
351 static void ExitMPG123( void )
353 vlc_mutex_lock( &mpg123_mutex );
354 mpg123_refcount--;
355 if( mpg123_refcount == 0 )
356 mpg123_exit();
357 vlc_mutex_unlock( &mpg123_mutex );
360 /*****************************************************************************
361 * OpenDecoder :
362 *****************************************************************************/
363 static int OpenDecoder( vlc_object_t *p_this )
365 decoder_t *p_dec = (decoder_t *)p_this;
366 decoder_sys_t *p_sys;
368 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA &&
369 p_dec->fmt_in.i_codec != VLC_CODEC_MP3 )
370 return VLC_EGENERIC;
372 /* Initialize libmpg123 */
373 if( InitMPG123() != MPG123_OK )
374 return VLC_EGENERIC;
376 /* Allocate the memory needed to store the module's structure */
377 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
378 if( p_sys == NULL )
379 return VLC_ENOMEM;
381 p_sys->p_out = NULL;
382 date_Set( &p_sys->end_date, VLC_TS_INVALID );
384 if( MPG123Open( p_dec ) )
385 goto error;
387 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
388 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
389 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
390 p_dec->pf_decode = DecodeBlock;
391 p_dec->pf_flush = Flush;
393 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
394 (char *)&p_dec->fmt_in.i_codec,
395 (char *)&p_dec->fmt_out.i_codec,
396 aout_BitsPerSample( p_dec->fmt_out.i_codec ) );
398 return VLC_SUCCESS;
399 error:
400 ExitMPG123();
401 free( p_sys );
402 return VLC_EGENERIC;
405 /*****************************************************************************
406 * CloseDecoder : deallocate data structures
407 *****************************************************************************/
408 static void CloseDecoder( vlc_object_t *p_this )
410 decoder_t *p_dec = (decoder_t *)p_this;
411 decoder_sys_t *p_sys = p_dec->p_sys;
413 mpg123_close( p_sys->p_handle );
414 mpg123_delete( p_sys->p_handle );
415 ExitMPG123();
416 if( p_sys->p_out )
417 block_Release( p_sys->p_out );
418 free( p_sys );