1 /*****************************************************************************
2 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2003 the VideoLAN team
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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 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 General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, 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 /*****************************************************************************
44 * decoder_sys_t : decoder descriptor
45 *****************************************************************************/
56 block_bytestream_t bytestream
;
62 unsigned int i_current_layer
;
66 int i_frame_size
, i_free_frame_size
;
67 unsigned int i_channels_conf
, i_channels
;
68 unsigned int i_rate
, i_max_frame_size
, i_frame_length
;
69 unsigned int i_layer
, i_bit_rate
;
84 /* This isn't the place to put mad-specific stuff. However, it makes the
85 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
86 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
87 * bigger buffer. This has no implication on other plug-ins, and we only
88 * lose 8 bytes per frame. --Meuuh */
89 #define MAD_BUFFER_GUARD 8
90 #define MPGA_HEADER_SIZE 4
92 /****************************************************************************
94 ****************************************************************************/
95 static int OpenDecoder ( vlc_object_t
* );
96 static int OpenPacketizer( vlc_object_t
* );
97 static void CloseDecoder ( vlc_object_t
* );
98 static void *DecodeBlock ( decoder_t
*, block_t
** );
100 static uint8_t *GetOutBuffer ( decoder_t
*, block_t
** );
101 static aout_buffer_t
*GetAoutBuffer( decoder_t
* );
102 static block_t
*GetSoutBuffer( decoder_t
* );
104 static int SyncInfo( uint32_t i_header
, unsigned int * pi_channels
,
105 unsigned int * pi_channels_conf
,
106 unsigned int * pi_sample_rate
, unsigned int * pi_bit_rate
,
107 unsigned int * pi_frame_length
,
108 unsigned int * pi_max_frame_size
,
109 unsigned int * pi_layer
);
111 /*****************************************************************************
113 *****************************************************************************/
115 set_description( N_("MPEG audio layer I/II/III decoder") )
116 set_category( CAT_INPUT
)
117 set_subcategory( SUBCAT_INPUT_ACODEC
)
118 #if defined(UNDER_CE)
119 set_capability( "decoder", 5 )
121 set_capability( "decoder", 100 )
123 set_callbacks( OpenDecoder
, CloseDecoder
)
126 set_description( N_("MPEG audio layer I/II/III packetizer") )
127 set_capability( "packetizer", 10 )
128 set_callbacks( OpenPacketizer
, CloseDecoder
)
131 /*****************************************************************************
132 * Open: probe the decoder and return score
133 *****************************************************************************/
134 static int Open( vlc_object_t
*p_this
)
136 decoder_t
*p_dec
= (decoder_t
*)p_this
;
137 decoder_sys_t
*p_sys
;
139 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_MPGA
)
144 /* Allocate the memory needed to store the decoder's structure */
145 if( ( p_dec
->p_sys
= p_sys
=
146 (decoder_sys_t
*)malloc(sizeof(decoder_sys_t
)) ) == NULL
)
150 p_sys
->b_packetizer
= false;
151 p_sys
->i_state
= STATE_NOSYNC
;
152 date_Set( &p_sys
->end_date
, 0 );
153 p_sys
->bytestream
= block_BytestreamInit();
154 p_sys
->i_pts
= VLC_TS_INVALID
;
155 p_sys
->b_discontinuity
= false;
157 /* Set output properties */
158 p_dec
->fmt_out
.i_cat
= AUDIO_ES
;
159 p_dec
->fmt_out
.i_codec
= VLC_CODEC_MPGA
;
160 p_dec
->fmt_out
.audio
.i_rate
= 0; /* So end_date gets initialized */
163 p_dec
->pf_decode_audio
= (aout_buffer_t
*(*)(decoder_t
*, block_t
**))
165 p_dec
->pf_packetize
= (block_t
*(*)(decoder_t
*, block_t
**))
168 /* Start with the minimum size for a free bitrate frame */
169 p_sys
->i_free_frame_size
= MPGA_HEADER_SIZE
;
174 static int OpenDecoder( vlc_object_t
*p_this
)
176 /* HACK: Don't use this codec if we don't have an mpga audio filter */
177 if( !module_exists( "mpgatofixed32" ) )
180 return Open( p_this
);
183 static int OpenPacketizer( vlc_object_t
*p_this
)
185 decoder_t
*p_dec
= (decoder_t
*)p_this
;
187 int i_ret
= Open( p_this
);
189 if( i_ret
== VLC_SUCCESS
) p_dec
->p_sys
->b_packetizer
= true;
194 /****************************************************************************
195 * DecodeBlock: the whole thing
196 ****************************************************************************
197 * This function is called just after the thread is launched.
198 ****************************************************************************/
199 static void *DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
201 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
202 uint8_t p_header
[MAD_BUFFER_GUARD
];
205 block_t
*p_out_buffer
;
207 if( !pp_block
|| !*pp_block
) return NULL
;
209 if( (*pp_block
)->i_flags
&(BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
211 if( (*pp_block
)->i_flags
&BLOCK_FLAG_CORRUPTED
)
213 p_sys
->i_state
= STATE_NOSYNC
;
214 block_BytestreamEmpty( &p_sys
->bytestream
);
216 date_Set( &p_sys
->end_date
, 0 );
217 block_Release( *pp_block
);
218 p_sys
->b_discontinuity
= true;
222 if( !date_Get( &p_sys
->end_date
) && (*pp_block
)->i_pts
<= VLC_TS_INVALID
)
224 /* We've just started the stream, wait for the first PTS. */
225 msg_Dbg( p_dec
, "waiting for PTS" );
226 block_Release( *pp_block
);
230 block_BytestreamPush( &p_sys
->bytestream
, *pp_block
);
234 switch( p_sys
->i_state
)
238 while( block_PeekBytes( &p_sys
->bytestream
, p_header
, 2 )
241 /* Look for sync word - should be 0xffe */
242 if( p_header
[0] == 0xff && (p_header
[1] & 0xe0) == 0xe0 )
244 p_sys
->i_state
= STATE_SYNC
;
247 block_SkipByte( &p_sys
->bytestream
);
249 if( p_sys
->i_state
!= STATE_SYNC
)
251 block_BytestreamFlush( &p_sys
->bytestream
);
258 /* New frame, set the Presentation Time Stamp */
259 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
;
260 if( p_sys
->i_pts
> VLC_TS_INVALID
&&
261 p_sys
->i_pts
!= date_Get( &p_sys
->end_date
) )
263 date_Set( &p_sys
->end_date
, p_sys
->i_pts
);
265 p_sys
->i_state
= STATE_HEADER
;
268 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
269 if( block_PeekBytes( &p_sys
->bytestream
, p_header
,
270 MPGA_HEADER_SIZE
) != VLC_SUCCESS
)
276 /* Build frame header */
277 i_header
= (p_header
[0]<<24)|(p_header
[1]<<16)|(p_header
[2]<<8)
280 /* Check if frame is valid and get frame info */
281 p_sys
->i_frame_size
= SyncInfo( i_header
,
283 &p_sys
->i_channels_conf
,
286 &p_sys
->i_frame_length
,
287 &p_sys
->i_max_frame_size
,
290 p_dec
->fmt_in
.i_profile
= p_sys
->i_layer
;
292 if( p_sys
->i_frame_size
== -1 )
294 msg_Dbg( p_dec
, "emulated startcode" );
295 block_SkipByte( &p_sys
->bytestream
);
296 p_sys
->i_state
= STATE_NOSYNC
;
297 p_sys
->b_discontinuity
= true;
301 if( p_sys
->i_bit_rate
== 0 )
303 /* Free bitrate, but 99% emulated startcode :( */
304 if( p_dec
->p_sys
->i_free_frame_size
== MPGA_HEADER_SIZE
)
306 msg_Dbg( p_dec
, "free bitrate mode");
308 /* The -1 below is to account for the frame padding */
309 p_sys
->i_frame_size
= p_sys
->i_free_frame_size
- 1;
312 p_sys
->i_state
= STATE_NEXT_SYNC
;
314 case STATE_NEXT_SYNC
:
315 /* TODO: If p_block == NULL, flush the buffer without checking the
318 /* Check if next expected frame contains the sync word */
319 if( block_PeekOffsetBytes( &p_sys
->bytestream
,
320 p_sys
->i_frame_size
, p_header
,
321 MAD_BUFFER_GUARD
) != VLC_SUCCESS
)
327 if( p_header
[0] == 0xff && (p_header
[1] & 0xe0) == 0xe0 )
329 /* Startcode is fine, let's try the header as an extra check */
330 int i_next_frame_size
;
331 unsigned int i_next_channels
, i_next_channels_conf
;
332 unsigned int i_next_rate
, i_next_bit_rate
;
333 unsigned int i_next_frame_length
, i_next_max_frame_size
;
334 unsigned int i_next_layer
;
336 /* Build frame header */
337 i_header
= (p_header
[0]<<24)|(p_header
[1]<<16)|(p_header
[2]<<8)
340 i_next_frame_size
= SyncInfo( i_header
,
342 &i_next_channels_conf
,
345 &i_next_frame_length
,
346 &i_next_max_frame_size
,
349 /* Free bitrate only */
350 if( p_sys
->i_bit_rate
== 0 && i_next_frame_size
== -1 )
352 if( (unsigned int)p_sys
->i_frame_size
>
353 p_sys
->i_max_frame_size
)
355 msg_Dbg( p_dec
, "frame too big %d > %d "
356 "(emulated startcode ?)", p_sys
->i_frame_size
,
357 p_sys
->i_max_frame_size
);
358 block_SkipByte( &p_sys
->bytestream
);
359 p_sys
->i_state
= STATE_NOSYNC
;
360 p_sys
->i_free_frame_size
= MPGA_HEADER_SIZE
;
364 p_sys
->i_frame_size
++;
368 if( i_next_frame_size
== -1 )
370 msg_Dbg( p_dec
, "emulated startcode on next frame" );
371 block_SkipByte( &p_sys
->bytestream
);
372 p_sys
->i_state
= STATE_NOSYNC
;
373 p_sys
->b_discontinuity
= true;
377 /* Check info is in sync with previous one */
378 if( i_next_channels_conf
!= p_sys
->i_channels_conf
||
379 i_next_rate
!= p_sys
->i_rate
||
380 i_next_layer
!= p_sys
->i_layer
||
381 i_next_frame_length
!= p_sys
->i_frame_length
)
383 /* Free bitrate only */
384 if( p_sys
->i_bit_rate
== 0 )
386 p_sys
->i_frame_size
++;
390 msg_Dbg( p_dec
, "parameters changed unexpectedly "
391 "(emulated startcode ?)" );
392 block_SkipByte( &p_sys
->bytestream
);
393 p_sys
->i_state
= STATE_NOSYNC
;
397 /* Free bitrate only */
398 if( p_sys
->i_bit_rate
== 0 )
400 if( i_next_bit_rate
!= 0 )
402 p_sys
->i_frame_size
++;
410 /* Free bitrate only */
411 if( p_sys
->i_bit_rate
== 0 )
413 if( (unsigned int)p_sys
->i_frame_size
>
414 p_sys
->i_max_frame_size
)
416 msg_Dbg( p_dec
, "frame too big %d > %d "
417 "(emulated startcode ?)", p_sys
->i_frame_size
,
418 p_sys
->i_max_frame_size
);
419 block_SkipByte( &p_sys
->bytestream
);
420 p_sys
->i_state
= STATE_NOSYNC
;
421 p_sys
->i_free_frame_size
= MPGA_HEADER_SIZE
;
425 p_sys
->i_frame_size
++;
429 msg_Dbg( p_dec
, "emulated startcode "
430 "(no startcode on following frame)" );
431 p_sys
->i_state
= STATE_NOSYNC
;
432 block_SkipByte( &p_sys
->bytestream
);
436 p_sys
->i_state
= STATE_SEND_DATA
;
440 /* Make sure we have enough data.
441 * (Not useful if we went through NEXT_SYNC) */
442 if( block_WaitBytes( &p_sys
->bytestream
,
443 p_sys
->i_frame_size
) != VLC_SUCCESS
)
448 p_sys
->i_state
= STATE_SEND_DATA
;
450 case STATE_SEND_DATA
:
451 if( !(p_buf
= GetOutBuffer( p_dec
, &p_out_buffer
)) )
453 //p_dec->b_error = true;
457 /* Free bitrate only */
458 if( p_sys
->i_bit_rate
== 0 )
460 p_sys
->i_free_frame_size
= p_sys
->i_frame_size
;
463 /* Copy the whole frame into the buffer. When we reach this point
464 * we already know we have enough data available. */
465 block_GetBytes( &p_sys
->bytestream
,
466 p_buf
, __MIN( (unsigned)p_sys
->i_frame_size
, p_out_buffer
->i_buffer
) );
468 /* Get beginning of next frame for libmad */
469 if( !p_sys
->b_packetizer
)
471 assert( p_out_buffer
->i_buffer
>= (unsigned)p_sys
->i_frame_size
+ MAD_BUFFER_GUARD
);
472 memcpy( p_buf
+ p_sys
->i_frame_size
,
473 p_header
, MAD_BUFFER_GUARD
);
476 p_sys
->i_state
= STATE_NOSYNC
;
478 /* Make sure we don't reuse the same pts twice */
479 if( p_sys
->i_pts
== p_sys
->bytestream
.p_block
->i_pts
)
480 p_sys
->i_pts
= p_sys
->bytestream
.p_block
->i_pts
= VLC_TS_INVALID
;
482 /* So p_block doesn't get re-added several times */
483 *pp_block
= block_BytestreamPop( &p_sys
->bytestream
);
492 /*****************************************************************************
494 *****************************************************************************/
495 static uint8_t *GetOutBuffer( decoder_t
*p_dec
, block_t
**pp_out_buffer
)
497 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
500 if( p_dec
->fmt_out
.audio
.i_rate
!= p_sys
->i_rate
)
502 msg_Dbg( p_dec
, "MPGA channels:%d samplerate:%d bitrate:%d",
503 p_sys
->i_channels
, p_sys
->i_rate
, p_sys
->i_bit_rate
);
505 date_Init( &p_sys
->end_date
, p_sys
->i_rate
, 1 );
506 date_Set( &p_sys
->end_date
, p_sys
->i_pts
);
509 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->i_rate
;
510 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->i_channels
;
511 p_dec
->fmt_out
.audio
.i_frame_length
= p_sys
->i_frame_length
;
512 p_dec
->fmt_out
.audio
.i_bytes_per_frame
=
513 p_sys
->i_max_frame_size
+ MAD_BUFFER_GUARD
;
515 p_dec
->fmt_out
.audio
.i_original_channels
= p_sys
->i_channels_conf
;
516 p_dec
->fmt_out
.audio
.i_physical_channels
=
517 p_sys
->i_channels_conf
& AOUT_CHAN_PHYSMASK
;
519 p_dec
->fmt_out
.i_bitrate
= p_sys
->i_bit_rate
* 1000;
521 if( p_sys
->b_packetizer
)
523 block_t
*p_sout_buffer
= GetSoutBuffer( p_dec
);
524 p_buf
= p_sout_buffer
? p_sout_buffer
->p_buffer
: NULL
;
525 *pp_out_buffer
= p_sout_buffer
;
529 aout_buffer_t
*p_aout_buffer
= GetAoutBuffer( p_dec
);
530 p_buf
= p_aout_buffer
? p_aout_buffer
->p_buffer
: NULL
;
531 *pp_out_buffer
= p_aout_buffer
;
537 /*****************************************************************************
539 *****************************************************************************/
540 static aout_buffer_t
*GetAoutBuffer( decoder_t
*p_dec
)
542 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
543 aout_buffer_t
*p_buf
;
545 p_buf
= decoder_NewAudioBuffer( p_dec
, p_sys
->i_frame_length
);
546 if( p_buf
== NULL
) return NULL
;
548 p_buf
->i_pts
= date_Get( &p_sys
->end_date
);
549 p_buf
->i_length
= date_Increment( &p_sys
->end_date
, p_sys
->i_frame_length
)
551 if( p_sys
->b_discontinuity
)
552 p_buf
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
553 p_sys
->b_discontinuity
= false;
555 /* Hack for libmad filter */
556 p_buf
= block_Realloc( p_buf
, 0, p_sys
->i_frame_size
+ MAD_BUFFER_GUARD
);
561 /*****************************************************************************
563 *****************************************************************************/
564 static block_t
*GetSoutBuffer( decoder_t
*p_dec
)
566 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
569 p_block
= block_New( p_dec
, p_sys
->i_frame_size
);
570 if( p_block
== NULL
) return NULL
;
572 p_block
->i_pts
= p_block
->i_dts
= date_Get( &p_sys
->end_date
);
575 date_Increment( &p_sys
->end_date
, p_sys
->i_frame_length
) - p_block
->i_pts
;
580 /*****************************************************************************
581 * CloseDecoder: clean up the decoder
582 *****************************************************************************/
583 static void CloseDecoder( vlc_object_t
*p_this
)
585 decoder_t
*p_dec
= (decoder_t
*)p_this
;
586 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
588 block_BytestreamRelease( &p_sys
->bytestream
);
593 /*****************************************************************************
594 * SyncInfo: parse MPEG audio sync info
595 *****************************************************************************/
596 static int SyncInfo( uint32_t i_header
, unsigned int * pi_channels
,
597 unsigned int * pi_channels_conf
,
598 unsigned int * pi_sample_rate
, unsigned int * pi_bit_rate
,
599 unsigned int * pi_frame_length
,
600 unsigned int * pi_max_frame_size
, unsigned int * pi_layer
)
602 static const int ppi_bitrate
[2][3][16] =
606 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
609 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
612 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
618 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
621 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
624 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
629 static const int ppi_samplerate
[2][4] = /* version 1 then 2 */
631 { 44100, 48000, 32000, 0 },
632 { 22050, 24000, 16000, 0 }
635 int i_version
, i_mode
, i_emphasis
;
636 bool b_padding
, b_mpeg_2_5
, b_crc
;
637 int i_frame_size
= 0;
638 int i_bitrate_index
, i_samplerate_index
;
641 b_mpeg_2_5
= 1 - ((i_header
& 0x100000) >> 20);
642 i_version
= 1 - ((i_header
& 0x80000) >> 19);
643 *pi_layer
= 4 - ((i_header
& 0x60000) >> 17);
644 b_crc
= !((i_header
>> 16) & 0x01);
645 i_bitrate_index
= (i_header
& 0xf000) >> 12;
646 i_samplerate_index
= (i_header
& 0xc00) >> 10;
647 b_padding
= (i_header
& 0x200) >> 9;
649 i_mode
= (i_header
& 0xc0) >> 6;
650 /* Modeext, copyright & original */
651 i_emphasis
= i_header
& 0x3;
653 if( *pi_layer
!= 4 &&
654 i_bitrate_index
< 0x0f &&
655 i_samplerate_index
!= 0x03 &&
661 case 1: /* joint stereo */
663 *pi_channels_conf
= AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
665 case 2: /* dual-mono */
667 *pi_channels_conf
= AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
668 | AOUT_CHAN_DUALMONO
;
672 *pi_channels_conf
= AOUT_CHAN_CENTER
;
675 *pi_bit_rate
= ppi_bitrate
[i_version
][*pi_layer
-1][i_bitrate_index
];
676 i_max_bit_rate
= ppi_bitrate
[i_version
][*pi_layer
-1][14];
677 *pi_sample_rate
= ppi_samplerate
[i_version
][i_samplerate_index
];
681 *pi_sample_rate
>>= 1;
687 i_frame_size
= ( 12000 * *pi_bit_rate
/ *pi_sample_rate
+
689 *pi_max_frame_size
= ( 12000 * i_max_bit_rate
/
690 *pi_sample_rate
+ 1 ) * 4;
691 *pi_frame_length
= 384;
695 i_frame_size
= 144000 * *pi_bit_rate
/ *pi_sample_rate
+ b_padding
;
696 *pi_max_frame_size
= 144000 * i_max_bit_rate
/ *pi_sample_rate
+ 1;
697 *pi_frame_length
= 1152;
701 i_frame_size
= ( i_version
? 72000 : 144000 ) *
702 *pi_bit_rate
/ *pi_sample_rate
+ b_padding
;
703 *pi_max_frame_size
= ( i_version
? 72000 : 144000 ) *
704 i_max_bit_rate
/ *pi_sample_rate
+ 1;
705 *pi_frame_length
= i_version
? 576 : 1152;
712 /* Free bitrate mode can support higher bitrates */
713 if( !*pi_bit_rate
) *pi_max_frame_size
*= 2;