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 *****************************************************************************/
31 #include <vlc_codec.h>
34 #include <vlc_block_helper.h>
36 /*****************************************************************************
37 * decoder_sys_t : decoder descriptor
38 *****************************************************************************/
42 vlc_bool_t b_packetizer;
49 block_bytestream_t bytestream;
54 audio_date_t end_date;
55 unsigned int i_current_layer;
59 int i_frame_size, i_free_frame_size;
60 unsigned int i_channels_conf, i_channels;
61 unsigned int i_rate, i_max_frame_size, i_frame_length;
62 unsigned int i_layer, i_bit_rate;
75 /* This isn't the place to put mad-specific stuff. However, it makes the
76 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
77 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
78 * bigger buffer. This has no implication on other plug-ins, and we only
79 * lose 8 bytes per frame. --Meuuh */
80 #define MAD_BUFFER_GUARD 8
81 #define MPGA_HEADER_SIZE 4
83 /****************************************************************************
85 ****************************************************************************/
86 static int OpenDecoder ( vlc_object_t * );
87 static int OpenPacketizer( vlc_object_t * );
88 static void CloseDecoder ( vlc_object_t * );
89 static void *DecodeBlock ( decoder_t *, block_t ** );
91 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
92 static aout_buffer_t *GetAoutBuffer( decoder_t * );
93 static block_t *GetSoutBuffer( decoder_t * );
95 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
96 unsigned int * pi_channels_conf,
97 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
98 unsigned int * pi_frame_length,
99 unsigned int * pi_max_frame_size,
100 unsigned int * pi_layer );
102 /*****************************************************************************
104 *****************************************************************************/
106 set_description( _("MPEG audio layer I/II/III decoder") );
107 set_category( CAT_INPUT );
108 set_subcategory( SUBCAT_INPUT_ACODEC );
109 #if defined(UNDER_CE)
110 set_capability( "decoder", 5 );
112 set_capability( "decoder", 100 );
114 set_callbacks( OpenDecoder, CloseDecoder );
117 set_description( _("MPEG audio layer I/II/III packetizer") );
118 set_capability( "packetizer", 10 );
119 set_callbacks( OpenPacketizer, CloseDecoder );
122 /*****************************************************************************
123 * OpenDecoder: probe the decoder and return score
124 *****************************************************************************/
125 static int OpenDecoder( vlc_object_t *p_this )
127 decoder_t *p_dec = (decoder_t*)p_this;
128 decoder_sys_t *p_sys;
130 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
135 /* HACK: Don't use this codec if we don't have an mpga audio filter */
136 if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
137 !config_FindModule( p_this, "mpgatofixed32" ) )
142 /* Allocate the memory needed to store the decoder's structure */
143 if( ( p_dec->p_sys = p_sys =
144 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
146 msg_Err( p_dec, "out of memory" );
151 p_sys->b_packetizer = VLC_FALSE;
152 p_sys->i_state = STATE_NOSYNC;
153 aout_DateSet( &p_sys->end_date, 0 );
154 p_sys->bytestream = block_BytestreamInit( p_dec );
156 /* Set output properties */
157 p_dec->fmt_out.i_cat = AUDIO_ES;
158 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
159 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
162 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
164 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
167 /* Start with the minimum size for a free bitrate frame */
168 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
173 static int OpenPacketizer( vlc_object_t *p_this )
175 decoder_t *p_dec = (decoder_t*)p_this;
177 int i_ret = OpenDecoder( p_this );
179 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
184 /****************************************************************************
185 * DecodeBlock: the whole thing
186 ****************************************************************************
187 * This function is called just after the thread is launched.
188 ****************************************************************************/
189 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
191 decoder_sys_t *p_sys = p_dec->p_sys;
192 uint8_t p_header[MAD_BUFFER_GUARD];
197 if( !pp_block || !*pp_block ) return NULL;
199 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
201 /* We've just started the stream, wait for the first PTS. */
202 msg_Dbg( p_dec, "waiting for PTS" );
203 block_Release( *pp_block );
207 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
209 p_sys->i_state = STATE_NOSYNC;
212 block_BytestreamPush( &p_sys->bytestream, *pp_block );
216 switch( p_sys->i_state )
220 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
223 /* Look for sync word - should be 0xffe */
224 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
226 p_sys->i_state = STATE_SYNC;
229 block_SkipByte( &p_sys->bytestream );
231 if( p_sys->i_state != STATE_SYNC )
233 block_BytestreamFlush( &p_sys->bytestream );
240 /* New frame, set the Presentation Time Stamp */
241 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
242 if( p_sys->i_pts != 0 &&
243 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
245 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
247 p_sys->i_state = STATE_HEADER;
250 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
251 if( block_PeekBytes( &p_sys->bytestream, p_header,
252 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
258 /* Build frame header */
259 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
262 /* Check if frame is valid and get frame info */
263 p_sys->i_frame_size = SyncInfo( i_header,
265 &p_sys->i_channels_conf,
268 &p_sys->i_frame_length,
269 &p_sys->i_max_frame_size,
272 if( p_sys->i_frame_size == -1 )
274 msg_Dbg( p_dec, "emulated startcode" );
275 block_SkipByte( &p_sys->bytestream );
276 p_sys->i_state = STATE_NOSYNC;
280 if( p_sys->i_bit_rate == 0 )
282 /* Free birate, but 99% emulated startcode :( */
283 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
285 msg_Dbg( p_dec, "free bitrate mode");
287 p_sys->i_frame_size = p_sys->i_free_frame_size;
290 p_sys->i_state = STATE_NEXT_SYNC;
292 case STATE_NEXT_SYNC:
293 /* TODO: If p_block == NULL, flush the buffer without checking the
296 /* Check if next expected frame contains the sync word */
297 if( block_PeekOffsetBytes( &p_sys->bytestream,
298 p_sys->i_frame_size, p_header,
299 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
305 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
307 /* Startcode is fine, let's try the header as an extra check */
308 int i_next_frame_size;
309 unsigned int i_next_channels, i_next_channels_conf;
310 unsigned int i_next_rate, i_next_bit_rate;
311 unsigned int i_next_frame_length, i_next_max_frame_size;
312 unsigned int i_next_layer;
314 /* Build frame header */
315 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
318 i_next_frame_size = SyncInfo( i_header,
320 &i_next_channels_conf,
323 &i_next_frame_length,
324 &i_next_max_frame_size,
327 /* Free bitrate only */
328 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
330 if( (unsigned int)p_sys->i_frame_size >
331 p_sys->i_max_frame_size )
333 msg_Dbg( p_dec, "frame too big %d > %d "
334 "(emulated startcode ?)", p_sys->i_frame_size,
335 p_sys->i_max_frame_size );
336 block_SkipByte( &p_sys->bytestream );
337 p_sys->i_state = STATE_NOSYNC;
338 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
342 p_sys->i_frame_size++;
346 if( i_next_frame_size == -1 )
348 msg_Dbg( p_dec, "emulated startcode on next frame" );
349 block_SkipByte( &p_sys->bytestream );
350 p_sys->i_state = STATE_NOSYNC;
354 /* Check info is in sync with previous one */
355 if( i_next_channels_conf != p_sys->i_channels_conf ||
356 i_next_rate != p_sys->i_rate ||
357 i_next_layer != p_sys->i_layer ||
358 i_next_frame_length != p_sys->i_frame_length )
360 /* Free bitrate only */
361 if( p_sys->i_bit_rate == 0 )
363 p_sys->i_frame_size++;
367 msg_Dbg( p_dec, "parameters changed unexpectedly "
368 "(emulated startcode ?)" );
369 block_SkipByte( &p_sys->bytestream );
370 p_sys->i_state = STATE_NOSYNC;
374 /* Free bitrate only */
375 if( p_sys->i_bit_rate == 0 )
377 if( i_next_bit_rate != 0 )
379 p_sys->i_frame_size++;
387 /* Free bitrate only */
388 if( p_sys->i_bit_rate == 0 )
390 if( (unsigned int)p_sys->i_frame_size >
391 p_sys->i_max_frame_size )
393 msg_Dbg( p_dec, "frame too big %d > %d "
394 "(emulated startcode ?)", p_sys->i_frame_size,
395 p_sys->i_max_frame_size );
396 block_SkipByte( &p_sys->bytestream );
397 p_sys->i_state = STATE_NOSYNC;
398 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
402 p_sys->i_frame_size++;
406 msg_Dbg( p_dec, "emulated startcode "
407 "(no startcode on following frame)" );
408 p_sys->i_state = STATE_NOSYNC;
409 block_SkipByte( &p_sys->bytestream );
413 p_sys->i_state = STATE_SEND_DATA;
417 /* Make sure we have enough data.
418 * (Not useful if we went through NEXT_SYNC) */
419 if( block_WaitBytes( &p_sys->bytestream,
420 p_sys->i_frame_size ) != VLC_SUCCESS )
425 p_sys->i_state = STATE_SEND_DATA;
427 case STATE_SEND_DATA:
428 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
430 //p_dec->b_error = VLC_TRUE;
434 /* Free bitrate only */
435 if( p_sys->i_bit_rate == 0 )
437 p_sys->i_free_frame_size = p_sys->i_frame_size;
440 /* Copy the whole frame into the buffer. When we reach this point
441 * we already know we have enough data available. */
442 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
444 /* Get beginning of next frame for libmad */
445 if( !p_sys->b_packetizer )
447 memcpy( p_buf + p_sys->i_frame_size,
448 p_header, MAD_BUFFER_GUARD );
451 p_sys->i_state = STATE_NOSYNC;
453 /* Make sure we don't reuse the same pts twice */
454 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
455 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
457 /* So p_block doesn't get re-added several times */
458 *pp_block = block_BytestreamPop( &p_sys->bytestream );
467 /*****************************************************************************
469 *****************************************************************************/
470 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
472 decoder_sys_t *p_sys = p_dec->p_sys;
475 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
477 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
478 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
480 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
481 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
484 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
485 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
486 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
487 p_dec->fmt_out.audio.i_bytes_per_frame =
488 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
490 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
491 p_dec->fmt_out.audio.i_physical_channels =
492 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
494 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
496 if( p_sys->b_packetizer )
498 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
499 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
500 *pp_out_buffer = p_sout_buffer;
504 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
505 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
506 *pp_out_buffer = p_aout_buffer;
512 /*****************************************************************************
514 *****************************************************************************/
515 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
517 decoder_sys_t *p_sys = p_dec->p_sys;
518 aout_buffer_t *p_buf;
520 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
521 if( p_buf == NULL ) return NULL;
523 p_buf->start_date = aout_DateGet( &p_sys->end_date );
525 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
527 /* Hack for libmad filter */
528 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
533 /*****************************************************************************
535 *****************************************************************************/
536 static block_t *GetSoutBuffer( decoder_t *p_dec )
538 decoder_sys_t *p_sys = p_dec->p_sys;
541 p_block = block_New( p_dec, p_sys->i_frame_size );
542 if( p_block == NULL ) return NULL;
544 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
547 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
553 /*****************************************************************************
554 * CloseDecoder: clean up the decoder
555 *****************************************************************************/
556 static void CloseDecoder( vlc_object_t *p_this )
558 decoder_t *p_dec = (decoder_t *)p_this;
559 decoder_sys_t *p_sys = p_dec->p_sys;
561 block_BytestreamRelease( &p_sys->bytestream );
566 /*****************************************************************************
567 * SyncInfo: parse MPEG audio sync info
568 *****************************************************************************/
569 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
570 unsigned int * pi_channels_conf,
571 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
572 unsigned int * pi_frame_length,
573 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
575 static const int ppi_bitrate[2][3][16] =
579 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
582 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
585 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
591 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
594 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
597 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
602 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
604 { 44100, 48000, 32000, 0 },
605 { 22050, 24000, 16000, 0 }
608 int i_version, i_mode, i_emphasis;
609 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
610 int i_frame_size = 0;
611 int i_bitrate_index, i_samplerate_index;
614 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
615 i_version = 1 - ((i_header & 0x80000) >> 19);
616 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
617 b_crc = !((i_header >> 16) & 0x01);
618 i_bitrate_index = (i_header & 0xf000) >> 12;
619 i_samplerate_index = (i_header & 0xc00) >> 10;
620 b_padding = (i_header & 0x200) >> 9;
622 i_mode = (i_header & 0xc0) >> 6;
623 /* Modeext, copyright & original */
624 i_emphasis = i_header & 0x3;
626 if( *pi_layer != 4 &&
627 i_bitrate_index < 0x0f &&
628 i_samplerate_index != 0x03 &&
634 case 1: /* joint stereo */
636 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
638 case 2: /* dual-mono */
640 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
641 | AOUT_CHAN_DUALMONO;
645 *pi_channels_conf = AOUT_CHAN_CENTER;
648 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
649 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
650 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
654 *pi_sample_rate >>= 1;
660 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
662 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
663 *pi_sample_rate + 1 ) * 4;
664 *pi_frame_length = 384;
668 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
669 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
670 *pi_frame_length = 1152;
674 i_frame_size = ( i_version ? 72000 : 144000 ) *
675 *pi_bit_rate / *pi_sample_rate + b_padding;
676 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
677 i_max_bit_rate / *pi_sample_rate + 1;
678 *pi_frame_length = i_version ? 576 : 1152;