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 /*****************************************************************************
25 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_block.h>
39 #include <vlc_codec.h>
41 /*****************************************************************************
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 /*****************************************************************************
52 *****************************************************************************/
55 mpg123_handle
* p_handle
;
61 /*****************************************************************************
63 *****************************************************************************/
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
)
73 /*****************************************************************************
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" );
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
);
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
);
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,
116 i
< sizeof(mp3_rates
) / sizeof(*mp3_rates
) && i_ret
== MPG123_OK
;
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
);
133 p_sys
->b_opened
= true;
137 /*****************************************************************************
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;
152 static int UpdateAudioFormat( decoder_t
*p_dec
)
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
) );
167 p_dec
->fmt_out
.i_bitrate
= frame_info
.bitrate
* 1000;
169 switch( frame_info
.mode
)
171 case MPG123_M_STEREO
:
173 p_dec
->fmt_out
.audio
.i_original_channels
=
174 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
177 p_dec
->fmt_out
.audio
.i_original_channels
=
178 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_DUALMONO
;
181 p_dec
->fmt_out
.audio
.i_original_channels
= AOUT_CHAN_CENTER
;
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
)
208 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
209 block_t
*p_out
= NULL
;
211 if( !p_sys
->b_opened
)
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" );
228 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
231 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
235 /* Feed mpg123 with raw data */
236 i_err
= mpg123_feed( p_sys
->p_handle
, p_block
->p_buffer
,
239 if( i_err
!= MPG123_OK
)
241 msg_Err( p_dec
, "mpg123_feed failed: %s",
242 mpg123_plain_strerror( i_err
) );
247 /* Fetch a new output block (if possible) */
249 || p_sys
->p_out
->i_buffer
!= mpg123_outblock( p_sys
->p_handle
) )
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 */
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
);
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
)
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
)
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
);
309 assert( p_dec
->fmt_out
.audio
.i_rate
!= 0 );
311 p_out
= p_sys
->p_out
;
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
)
325 block_Release( p_block
);
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 )
338 vlc_mutex_lock( &mpg123_mutex
);
339 if( mpg123_refcount
> 0 )
342 vlc_mutex_unlock( &mpg123_mutex
);
345 if( ( i_ret
= mpg123_init() ) == MPG123_OK
)
347 vlc_mutex_unlock( &mpg123_mutex
);
351 /*****************************************************************************
352 * ExitMPG123 : close down the mpg123 library (reentrant)
353 *****************************************************************************/
354 static void ExitMPG123( void )
356 vlc_mutex_lock( &mpg123_mutex
);
358 if( mpg123_refcount
== 0 )
360 vlc_mutex_unlock( &mpg123_mutex
);
363 /*****************************************************************************
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
)
375 p_dec
->fmt_out
.i_codec
= VLC_CODEC_FL32
;
377 /* Initialize libmpg123 */
378 if( InitMPG123() != MPG123_OK
)
381 /* Allocate the memory needed to store the module's structure */
382 p_sys
= p_dec
->p_sys
= malloc( sizeof(decoder_sys_t
) );
387 date_Set( &p_sys
->end_date
, VLC_TS_INVALID
);
389 if( MPG123Open( p_dec
) )
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
) );
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
);
421 block_Release( p_sys
->p_out
);