1 /*****************************************************************************
2 * mpg123.c: MPEG-1 & 2 audio layer I, II, III + MPEG 2.5 decoder
3 *****************************************************************************
4 * Copyright (C) 2001-2014 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 /*****************************************************************************
25 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_codec.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int OpenDecoder( vlc_object_t * );
43 static void CloseDecoder( vlc_object_t * );
44 static block_t *DecodeBlock( decoder_t *, block_t ** );
45 static int InitMPG123( void );
46 static void ExitMPG123( void );
48 static unsigned int mpg123_refcount = 0;
49 static vlc_mutex_t mpg123_mutex = VLC_STATIC_MUTEX;
51 /*****************************************************************************
53 *****************************************************************************/
56 mpg123_handle * p_handle;
59 struct mpg123_frameinfo frame_info;
62 /*****************************************************************************
64 *****************************************************************************/
66 set_category( CAT_INPUT )
67 set_subcategory( SUBCAT_INPUT_ACODEC )
68 set_description( N_("MPEG audio decoder using mpg123") )
69 set_capability( "decoder", 99 )
70 set_shortname( "mpg123" )
71 set_callbacks( OpenDecoder, CloseDecoder )
74 /****************************************************************************
75 * DecodeBlock: the whole thing
76 ****************************************************************************/
77 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
80 block_t *p_block = pp_block ? *pp_block : NULL;
81 decoder_sys_t *p_sys = p_dec->p_sys;
83 if( !pp_block || !p_block )
86 if( p_block->i_buffer == 0 )
89 if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
91 /* We've just started the stream, wait for the first PTS. */
92 msg_Dbg( p_dec, "waiting for PTS" );
96 if( p_block->i_flags & ( BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED ) )
98 date_Set( &p_sys->end_date, 0 );
102 /* Feed mpg123 with raw data */
103 i_err = mpg123_feed( p_sys->p_handle, p_block->p_buffer,
106 if( i_err != MPG123_OK )
108 msg_Err( p_dec, "mpg123_feed failed: %s", mpg123_plain_strerror( i_err ) );
112 /* Get details about the stream */
113 i_err = mpg123_info( p_sys->p_handle, &p_sys->frame_info );
115 if( i_err == MPG123_NEED_MORE )
120 else if( i_err != MPG123_OK )
122 msg_Err( p_dec, "mpg123_info failed: %s", mpg123_plain_strerror( i_err ) );
126 /* Configure the output */
127 p_block->i_nb_samples = mpg123_spf( p_sys->p_handle );
128 p_dec->fmt_out.i_bitrate = p_sys->frame_info.bitrate * 1000;
130 switch( p_sys->frame_info.mode )
132 case MPG123_M_STEREO:
134 p_dec->fmt_out.audio.i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
137 p_dec->fmt_out.audio.i_original_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
138 | AOUT_CHAN_DUALMONO;
141 p_dec->fmt_out.audio.i_original_channels = AOUT_CHAN_CENTER;
144 msg_Err( p_dec, "Unknown mode");
148 p_dec->fmt_out.audio.i_physical_channels =
149 p_dec->fmt_out.audio.i_original_channels & AOUT_CHAN_PHYSMASK;
151 /* Date management */
152 if( p_dec->fmt_out.audio.i_rate != p_sys->frame_info.rate )
154 p_dec->fmt_out.audio.i_rate = p_sys->frame_info.rate;
155 date_Init( &p_sys->end_date, p_dec->fmt_out.audio.i_rate, 1 );
156 date_Set( &p_sys->end_date, 0 );
159 if( p_block->i_pts > VLC_TS_INVALID &&
160 p_block->i_pts != date_Get( &p_sys->end_date ) )
162 date_Set( &p_sys->end_date, p_block->i_pts );
165 /* Request a new audio buffer */
166 block_t *p_out = decoder_NewAudioBuffer( p_dec, p_block->i_nb_samples );
167 if( unlikely( !p_out ) )
170 /* Configure the buffer */
171 p_out->i_nb_samples = p_block->i_nb_samples;
172 p_out->i_dts = p_out->i_pts = date_Get( &p_sys->end_date );
173 p_out->i_length = date_Increment( &p_sys->end_date, p_block->i_nb_samples )
176 /* Make mpg123 write directly into the VLC output buffer */
177 i_err = mpg123_replace_buffer( p_sys->p_handle, p_out->p_buffer, p_out->i_buffer );
178 if( i_err != MPG123_OK )
180 msg_Err( p_dec, "could not replace buffer: %s", mpg123_plain_strerror( i_err ) );
181 block_Release( p_out );
185 *pp_block = NULL; /* avoid being fed the same packet again */
187 /* Do the actual decoding now */
188 i_err = mpg123_decode_frame( p_sys->p_handle, NULL, NULL, NULL );
189 if( i_err != MPG123_OK )
191 if( i_err != MPG123_NEW_FORMAT )
192 msg_Err( p_dec, "mpg123_decode_frame error: %s", mpg123_plain_strerror( i_err ) );
193 block_Release( p_out );
197 block_Release( p_block );
201 block_Release( p_block );
205 /*****************************************************************************
207 *****************************************************************************/
208 static int OpenDecoder( vlc_object_t *p_this )
210 decoder_t *p_dec = (decoder_t *)p_this;
211 decoder_sys_t *p_sys;
213 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA &&
214 p_dec->fmt_in.i_codec != VLC_CODEC_MP3 )
217 p_dec->fmt_out.i_cat = AUDIO_ES;
218 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
220 /* Initialize libmpg123 */
221 if( InitMPG123() != MPG123_OK )
224 /* Allocate the memory needed to store the module's structure */
225 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
229 /* Create our mpg123 handle */
230 if( ( p_sys->p_handle = mpg123_new( NULL, NULL ) ) == NULL )
233 /* Open a new bitstream */
234 if( mpg123_open_feed( p_sys->p_handle ) != MPG123_OK )
236 msg_Err( p_this, "mpg123 error: can't open feed" );
240 /* Disable resync stream after error */
241 mpg123_param( p_sys->p_handle, MPG123_ADD_FLAGS, MPG123_NO_RESYNC, 0 );
243 /* Setup output format */
244 mpg123_format_none( p_sys->p_handle );
246 if( MPG123_OK != mpg123_format( p_sys->p_handle,
247 p_dec->fmt_in.audio.i_rate,
248 MPG123_MONO | MPG123_STEREO,
249 MPG123_ENC_FLOAT_32 ) )
251 msg_Err( p_this, "mpg123 error: %s",
252 mpg123_strerror( p_sys->p_handle ) );
253 mpg123_close( p_sys->p_handle );
257 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
258 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
259 p_dec->pf_decode_audio = DecodeBlock;
261 msg_Dbg( p_this, "%4.4s->%4.4s, bits per sample: %i",
262 (char *)&p_dec->fmt_in.i_codec,
263 (char *)&p_dec->fmt_out.i_codec,
264 aout_BitsPerSample( p_dec->fmt_out.i_codec ) );
268 mpg123_delete( p_sys->p_handle );
274 /*****************************************************************************
275 * CloseDecoder : deallocate data structures
276 *****************************************************************************/
277 static void CloseDecoder( vlc_object_t *p_this )
279 decoder_t *p_dec = (decoder_t *)p_this;
280 decoder_sys_t *p_sys = p_dec->p_sys;
282 mpg123_close( p_sys->p_handle );
283 mpg123_delete( p_sys->p_handle );
288 /*****************************************************************************
289 * InitMPG123 : initialize the mpg123 library (reentrant)
290 *****************************************************************************/
291 static int InitMPG123( void )
294 vlc_mutex_lock( &mpg123_mutex );
295 if( mpg123_refcount > 0 )
298 vlc_mutex_unlock( &mpg123_mutex );
301 if( ( i_ret = mpg123_init() ) == MPG123_OK )
303 vlc_mutex_unlock( &mpg123_mutex );
307 /*****************************************************************************
308 * ExitMPG123 : close down the mpg123 library (reentrant)
309 *****************************************************************************/
310 static void ExitMPG123( void )
312 vlc_mutex_lock( &mpg123_mutex );
314 if( mpg123_refcount == 0 )
316 vlc_mutex_unlock( &mpg123_mutex );