1 /*****************************************************************************
2 * mpegaudio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2016 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
10 * Gildas Bazin <gbazin@videolan.org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
38 #include <vlc_modules.h>
41 #include <vlc_block_helper.h>
43 #include "packetizer_helper.h"
45 /*****************************************************************************
46 * decoder_sys_t : decoder descriptor
47 *****************************************************************************/
55 block_bytestream_t bytestream
;
64 int i_frame_size
, i_free_frame_size
;
65 unsigned int i_channels_conf
, i_chan_mode
, i_channels
;
66 unsigned int i_rate
, i_max_frame_size
, i_frame_length
;
67 unsigned int i_layer
, i_bit_rate
;
72 #define MAD_BUFFER_GUARD 8
73 #define MPGA_HEADER_SIZE 4
75 /****************************************************************************
77 ****************************************************************************/
79 static int Open( vlc_object_t
* );
80 static void Close( vlc_object_t
* );
82 /*****************************************************************************
84 *****************************************************************************/
86 set_category( CAT_INPUT
)
87 set_subcategory( SUBCAT_INPUT_ACODEC
)
88 set_description( N_("MPEG audio layer I/II/III packetizer") )
89 set_capability( "packetizer", 10 )
90 set_callbacks( Open
, Close
)
93 /*****************************************************************************
95 *****************************************************************************/
96 static void Flush( decoder_t
*p_dec
)
98 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
100 date_Set( &p_sys
->end_date
, VLC_TS_INVALID
);
101 p_sys
->i_state
= STATE_NOSYNC
;
102 block_BytestreamEmpty( &p_sys
->bytestream
);
103 p_sys
->b_discontinuity
= true;
106 /*****************************************************************************
108 *****************************************************************************/
109 static uint8_t *GetOutBuffer( decoder_t
*p_dec
, block_t
**pp_out_buffer
)
111 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
113 if( p_dec
->fmt_out
.audio
.i_rate
!= p_sys
->i_rate
||
114 date_Get( &p_sys
->end_date
) == VLC_TS_INVALID
)
116 msg_Dbg( p_dec
, "MPGA channels:%d samplerate:%d bitrate:%d",
117 p_sys
->i_channels
, p_sys
->i_rate
, p_sys
->i_bit_rate
);
119 if( p_sys
->end_date
.i_divider_num
== 0 )
120 date_Init( &p_sys
->end_date
, p_sys
->i_rate
, 1 );
122 date_Change( &p_sys
->end_date
, p_sys
->i_rate
, 1 );
123 date_Set( &p_sys
->end_date
, p_sys
->i_pts
);
126 p_dec
->fmt_out
.i_profile
= p_sys
->i_layer
;
127 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->i_rate
;
128 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->i_channels
;
129 p_dec
->fmt_out
.audio
.i_frame_length
= p_sys
->i_frame_length
;
130 p_dec
->fmt_out
.audio
.i_bytes_per_frame
= p_sys
->i_max_frame_size
;
132 p_dec
->fmt_out
.audio
.i_physical_channels
= p_sys
->i_channels_conf
;
133 p_dec
->fmt_out
.audio
.i_chan_mode
= p_sys
->i_chan_mode
;
135 p_dec
->fmt_out
.i_bitrate
= p_sys
->i_bit_rate
* 1000;
137 block_t
*p_block
= block_Alloc( p_sys
->i_frame_size
);
138 if( p_block
== NULL
)
141 p_block
->i_pts
= p_block
->i_dts
= date_Get( &p_sys
->end_date
);
143 date_Increment( &p_sys
->end_date
, p_sys
->i_frame_length
) - p_block
->i_pts
;
145 *pp_out_buffer
= p_block
;
146 return p_block
->p_buffer
;
149 /*****************************************************************************
150 * SyncInfo: parse MPEG audio sync info
151 *****************************************************************************/
152 static int SyncInfo( uint32_t i_header
, unsigned int * pi_channels
,
153 unsigned int * pi_channels_conf
,
154 unsigned int * pi_chan_mode
,
155 unsigned int * pi_sample_rate
, unsigned int * pi_bit_rate
,
156 unsigned int * pi_frame_length
,
157 unsigned int * pi_max_frame_size
, unsigned int * pi_layer
)
159 static const int ppi_bitrate
[2][3][16] =
163 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
166 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
169 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
175 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
178 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
181 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
186 static const int ppi_samplerate
[2][4] = /* version 1 then 2 */
188 { 44100, 48000, 32000, 0 },
189 { 22050, 24000, 16000, 0 }
192 int i_version
, i_mode
, i_emphasis
;
193 bool b_padding
, b_mpeg_2_5
;
194 int i_frame_size
= 0;
195 int i_bitrate_index
, i_samplerate_index
;
198 b_mpeg_2_5
= 1 - ((i_header
& 0x100000) >> 20);
199 i_version
= 1 - ((i_header
& 0x80000) >> 19);
200 *pi_layer
= 4 - ((i_header
& 0x60000) >> 17);
201 //bool b_crc = !((i_header >> 16) & 0x01);
202 i_bitrate_index
= (i_header
& 0xf000) >> 12;
203 i_samplerate_index
= (i_header
& 0xc00) >> 10;
204 b_padding
= (i_header
& 0x200) >> 9;
206 i_mode
= (i_header
& 0xc0) >> 6;
207 /* Modeext, copyright & original */
208 i_emphasis
= i_header
& 0x3;
211 if( *pi_layer
!= 4 &&
212 i_bitrate_index
< 0x0f &&
213 i_samplerate_index
!= 0x03 &&
218 case 2: /* dual-mono */
219 *pi_chan_mode
= AOUT_CHANMODE_DUALMONO
;
222 case 1: /* joint stereo */
224 *pi_channels_conf
= AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
228 *pi_channels_conf
= AOUT_CHAN_CENTER
;
231 *pi_bit_rate
= ppi_bitrate
[i_version
][*pi_layer
-1][i_bitrate_index
];
232 i_max_bit_rate
= ppi_bitrate
[i_version
][*pi_layer
-1][14];
233 *pi_sample_rate
= ppi_samplerate
[i_version
][i_samplerate_index
];
237 *pi_sample_rate
>>= 1;
243 i_frame_size
= ( 12000 * *pi_bit_rate
/ *pi_sample_rate
+
245 *pi_max_frame_size
= ( 12000 * i_max_bit_rate
/
246 *pi_sample_rate
+ 1 ) * 4;
247 *pi_frame_length
= 384;
251 i_frame_size
= 144000 * *pi_bit_rate
/ *pi_sample_rate
+ b_padding
;
252 *pi_max_frame_size
= 144000 * i_max_bit_rate
/ *pi_sample_rate
+ 1;
253 *pi_frame_length
= 1152;
257 i_frame_size
= ( i_version
? 72000 : 144000 ) *
258 *pi_bit_rate
/ *pi_sample_rate
+ b_padding
;
259 *pi_max_frame_size
= ( i_version
? 72000 : 144000 ) *
260 i_max_bit_rate
/ *pi_sample_rate
+ 1;
261 *pi_frame_length
= i_version
? 576 : 1152;
268 /* Free bitrate mode can support higher bitrates */
269 if( !*pi_bit_rate
) *pi_max_frame_size
*= 2;
279 /****************************************************************************
280 * DecodeBlock: the whole thing
281 ****************************************************************************
282 * This function is called just after the thread is launched.
283 ****************************************************************************/
284 static block_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
286 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
287 uint8_t p_header
[MAD_BUFFER_GUARD
];
290 block_t
*p_out_buffer
;
292 block_t
*p_block
= pp_block
? *pp_block
: NULL
;
296 if( p_block
->i_flags
& (BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
298 /* First always drain complete blocks before discontinuity */
299 block_t
*p_drain
= DecodeBlock( p_dec
, NULL
);
305 if( p_block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
307 block_Release( p_block
);
312 if( p_block
->i_pts
== VLC_TS_INVALID
&&
313 date_Get( &p_sys
->end_date
) == VLC_TS_INVALID
)
315 /* We've just started the stream, wait for the first PTS. */
316 msg_Dbg( p_dec
, "waiting for PTS" );
317 block_Release( p_block
);
321 block_BytestreamPush( &p_sys
->bytestream
, p_block
);
326 switch( p_sys
->i_state
)
330 while( block_PeekBytes( &p_sys
->bytestream
, p_header
, 2 )
333 /* Look for sync word - should be 0xffe */
334 if( p_header
[0] == 0xff && (p_header
[1] & 0xe0) == 0xe0 )
336 p_sys
->i_state
= STATE_SYNC
;
339 block_SkipByte( &p_sys
->bytestream
);
341 if( p_sys
->i_state
!= STATE_SYNC
)
343 block_BytestreamFlush( &p_sys
->bytestream
);
351 /* New frame, set the Presentation Time Stamp */
352 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
;
353 if( p_sys
->i_pts
!= VLC_TS_INVALID
&&
354 p_sys
->i_pts
!= date_Get( &p_sys
->end_date
) )
356 if( p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC( 'D','V','R',' ') )
358 if( date_Get( &p_sys
->end_date
) == VLC_TS_INVALID
)
359 date_Set( &p_sys
->end_date
, p_sys
->i_pts
);
361 else if ( p_sys
->i_pts
!= date_Get( &p_sys
->end_date
) )
363 date_Set( &p_sys
->end_date
, p_sys
->i_pts
);
366 p_sys
->i_state
= STATE_HEADER
;
370 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
371 if( block_PeekBytes( &p_sys
->bytestream
, p_header
,
372 MPGA_HEADER_SIZE
) != VLC_SUCCESS
)
378 /* Build frame header */
379 i_header
= GetDWBE(p_header
);
381 /* Check if frame is valid and get frame info */
382 p_sys
->i_frame_size
= SyncInfo( i_header
,
384 &p_sys
->i_channels_conf
,
388 &p_sys
->i_frame_length
,
389 &p_sys
->i_max_frame_size
,
392 if( p_sys
->i_frame_size
== -1 )
394 msg_Dbg( p_dec
, "emulated startcode" );
395 block_SkipByte( &p_sys
->bytestream
);
396 p_sys
->i_state
= STATE_NOSYNC
;
400 if( p_sys
->i_bit_rate
== 0 )
402 /* Free bitrate, but 99% emulated startcode :( */
403 if( p_sys
->i_free_frame_size
== MPGA_HEADER_SIZE
)
405 msg_Dbg( p_dec
, "free bitrate mode");
407 /* The -1 below is to account for the frame padding */
408 p_sys
->i_frame_size
= p_sys
->i_free_frame_size
- 1;
411 p_sys
->i_state
= STATE_NEXT_SYNC
;
414 case STATE_NEXT_SYNC
:
415 /* Check if next expected frame contains the sync word */
416 if( block_PeekOffsetBytes( &p_sys
->bytestream
,
417 p_sys
->i_frame_size
, p_header
,
418 MAD_BUFFER_GUARD
) != VLC_SUCCESS
)
420 if( p_block
== NULL
) /* drain */
422 p_sys
->i_state
= STATE_SEND_DATA
;
429 if( p_header
[0] == 0xff && (p_header
[1] & 0xe0) == 0xe0 )
431 /* Startcode is fine, let's try the header as an extra check */
432 int i_next_frame_size
;
433 unsigned int i_next_channels
, i_next_stereo_mode
, i_next_channels_conf
;
434 unsigned int i_next_rate
, i_next_bit_rate
;
435 unsigned int i_next_frame_length
, i_next_max_frame_size
;
436 unsigned int i_next_layer
;
438 /* Build frame header */
439 i_header
= GetDWBE(p_header
);
441 i_next_frame_size
= SyncInfo( i_header
,
443 &i_next_channels_conf
,
447 &i_next_frame_length
,
448 &i_next_max_frame_size
,
451 /* Free bitrate only */
452 if( p_sys
->i_bit_rate
== 0 && i_next_frame_size
== -1 )
454 if( (unsigned int)p_sys
->i_frame_size
>
455 p_sys
->i_max_frame_size
)
457 msg_Dbg( p_dec
, "frame too big %d > %d "
458 "(emulated startcode ?)", p_sys
->i_frame_size
,
459 p_sys
->i_max_frame_size
);
460 block_SkipByte( &p_sys
->bytestream
);
461 p_sys
->i_state
= STATE_NOSYNC
;
462 p_sys
->i_free_frame_size
= MPGA_HEADER_SIZE
;
466 p_sys
->i_frame_size
++;
470 if( i_next_frame_size
== -1 )
472 msg_Dbg( p_dec
, "emulated startcode on next frame" );
473 block_SkipByte( &p_sys
->bytestream
);
474 p_sys
->i_state
= STATE_NOSYNC
;
478 /* Check info is in sync with previous one */
479 if( i_next_channels_conf
!= p_sys
->i_channels_conf
||
480 i_next_stereo_mode
!= p_sys
->i_chan_mode
||
481 i_next_rate
!= p_sys
->i_rate
||
482 i_next_layer
!= p_sys
->i_layer
||
483 i_next_frame_length
!= p_sys
->i_frame_length
)
485 /* Free bitrate only */
486 if( p_sys
->i_bit_rate
== 0 )
488 p_sys
->i_frame_size
++;
492 msg_Dbg( p_dec
, "parameters changed unexpectedly "
493 "(emulated startcode ?)" );
494 block_SkipByte( &p_sys
->bytestream
);
495 p_sys
->i_state
= STATE_NOSYNC
;
499 /* Free bitrate only */
500 if( p_sys
->i_bit_rate
== 0 )
502 if( i_next_bit_rate
!= 0 )
504 p_sys
->i_frame_size
++;
512 /* Free bitrate only */
513 if( p_sys
->i_bit_rate
== 0 )
515 if( (unsigned int)p_sys
->i_frame_size
>
516 p_sys
->i_max_frame_size
)
518 msg_Dbg( p_dec
, "frame too big %d > %d "
519 "(emulated startcode ?)", p_sys
->i_frame_size
,
520 p_sys
->i_max_frame_size
);
521 block_SkipByte( &p_sys
->bytestream
);
522 p_sys
->i_state
= STATE_NOSYNC
;
523 p_sys
->i_free_frame_size
= MPGA_HEADER_SIZE
;
527 p_sys
->i_frame_size
++;
531 msg_Dbg( p_dec
, "emulated startcode "
532 "(no startcode on following frame)" );
533 p_sys
->i_state
= STATE_NOSYNC
;
534 block_SkipByte( &p_sys
->bytestream
);
538 p_sys
->i_state
= STATE_GET_DATA
;
542 /* Make sure we have enough data.
543 * (Not useful if we went through NEXT_SYNC) */
544 if( block_WaitBytes( &p_sys
->bytestream
,
545 p_sys
->i_frame_size
) != VLC_SUCCESS
)
550 p_sys
->i_state
= STATE_SEND_DATA
;
553 case STATE_SEND_DATA
:
554 if( !(p_buf
= GetOutBuffer( p_dec
, &p_out_buffer
)) )
559 /* Free bitrate only */
560 if( p_sys
->i_bit_rate
== 0 )
562 p_sys
->i_free_frame_size
= p_sys
->i_frame_size
;
565 /* Copy the whole frame into the buffer. */
566 if (block_GetBytes( &p_sys
->bytestream
,
567 p_buf
, __MIN( (unsigned)p_sys
->i_frame_size
, p_out_buffer
->i_buffer
) )) {
568 block_Release(p_out_buffer
);
572 p_sys
->i_state
= STATE_NOSYNC
;
574 /* Make sure we don't reuse the same pts twice */
575 if( p_sys
->i_pts
== p_sys
->bytestream
.p_block
->i_pts
)
576 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
= VLC_TS_INVALID
;
578 if( p_sys
->b_discontinuity
)
580 p_out_buffer
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
581 p_sys
->b_discontinuity
= false;
584 /* So p_block doesn't get re-added several times */
585 p_block
= block_BytestreamPop( &p_sys
->bytestream
);
589 block_Release(p_block
);
598 /*****************************************************************************
599 * Close: clean up the decoder
600 *****************************************************************************/
601 static void Close( vlc_object_t
*p_this
)
603 decoder_t
*p_dec
= (decoder_t
*)p_this
;
604 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
606 block_BytestreamRelease( &p_sys
->bytestream
);
611 /*****************************************************************************
612 * Open: probe the decoder and return score
613 *****************************************************************************/
614 static int Open( vlc_object_t
*p_this
)
616 decoder_t
*p_dec
= (decoder_t
*)p_this
;
617 decoder_sys_t
*p_sys
;
619 if(( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_MPGA
) &&
620 ( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_MP3
) )
625 /* Allocate the memory needed to store the decoder's structure */
626 if( ( p_dec
->p_sys
= p_sys
=
627 (decoder_sys_t
*)malloc(sizeof(decoder_sys_t
)) ) == NULL
)
631 p_sys
->i_state
= STATE_NOSYNC
;
632 date_Init( &p_sys
->end_date
, 1, 1 );
633 date_Set( &p_sys
->end_date
, VLC_TS_INVALID
);
634 block_BytestreamInit( &p_sys
->bytestream
);
635 p_sys
->i_pts
= VLC_TS_INVALID
;
636 p_sys
->b_discontinuity
= false;
637 p_sys
->i_frame_size
= 0;
639 p_sys
->i_channels_conf
= p_sys
->i_chan_mode
= p_sys
->i_channels
=
640 p_sys
->i_rate
= p_sys
->i_max_frame_size
= p_sys
->i_frame_length
=
641 p_sys
->i_layer
= p_sys
->i_bit_rate
= 0;
643 /* Set output properties */
644 p_dec
->fmt_out
.i_codec
= VLC_CODEC_MPGA
;
645 p_dec
->fmt_out
.audio
.i_rate
= 0; /* So end_date gets initialized */
648 p_dec
->pf_packetize
= DecodeBlock
;
649 p_dec
->pf_flush
= Flush
;
651 /* Start with the minimum size for a free bitrate frame */
652 p_sys
->i_free_frame_size
= MPGA_HEADER_SIZE
;