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 block_Release( *pp_block );
206 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
208 p_sys->i_state = STATE_NOSYNC;
211 block_BytestreamPush( &p_sys->bytestream, *pp_block );
215 switch( p_sys->i_state )
219 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
222 /* Look for sync word - should be 0xffe */
223 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
225 p_sys->i_state = STATE_SYNC;
228 block_SkipByte( &p_sys->bytestream );
230 if( p_sys->i_state != STATE_SYNC )
232 block_BytestreamFlush( &p_sys->bytestream );
239 /* New frame, set the Presentation Time Stamp */
240 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
241 if( p_sys->i_pts != 0 &&
242 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
244 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
246 p_sys->i_state = STATE_HEADER;
249 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
250 if( block_PeekBytes( &p_sys->bytestream, p_header,
251 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
257 /* Build frame header */
258 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
261 /* Check if frame is valid and get frame info */
262 p_sys->i_frame_size = SyncInfo( i_header,
264 &p_sys->i_channels_conf,
267 &p_sys->i_frame_length,
268 &p_sys->i_max_frame_size,
271 if( p_sys->i_frame_size == -1 )
273 msg_Dbg( p_dec, "emulated startcode" );
274 block_SkipByte( &p_sys->bytestream );
275 p_sys->i_state = STATE_NOSYNC;
279 if( p_sys->i_bit_rate == 0 )
281 /* Free birate, but 99% emulated startcode :( */
282 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
284 msg_Dbg( p_dec, "free bitrate mode");
286 p_sys->i_frame_size = p_sys->i_free_frame_size;
289 p_sys->i_state = STATE_NEXT_SYNC;
291 case STATE_NEXT_SYNC:
292 /* TODO: If p_block == NULL, flush the buffer without checking the
295 /* Check if next expected frame contains the sync word */
296 if( block_PeekOffsetBytes( &p_sys->bytestream,
297 p_sys->i_frame_size, p_header,
298 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
304 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
306 /* Startcode is fine, let's try the header as an extra check */
307 int i_next_frame_size;
308 unsigned int i_next_channels, i_next_channels_conf;
309 unsigned int i_next_rate, i_next_bit_rate;
310 unsigned int i_next_frame_length, i_next_max_frame_size;
311 unsigned int i_next_layer;
313 /* Build frame header */
314 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
317 i_next_frame_size = SyncInfo( i_header,
319 &i_next_channels_conf,
322 &i_next_frame_length,
323 &i_next_max_frame_size,
326 /* Free bitrate only */
327 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
329 if( (unsigned int)p_sys->i_frame_size >
330 p_sys->i_max_frame_size )
332 msg_Dbg( p_dec, "frame too big %d > %d "
333 "(emulated startcode ?)", p_sys->i_frame_size,
334 p_sys->i_max_frame_size );
335 block_SkipByte( &p_sys->bytestream );
336 p_sys->i_state = STATE_NOSYNC;
337 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
341 p_sys->i_frame_size++;
345 if( i_next_frame_size == -1 )
347 msg_Dbg( p_dec, "emulated startcode on next frame" );
348 block_SkipByte( &p_sys->bytestream );
349 p_sys->i_state = STATE_NOSYNC;
353 /* Check info is in sync with previous one */
354 if( i_next_channels_conf != p_sys->i_channels_conf ||
355 i_next_rate != p_sys->i_rate ||
356 i_next_layer != p_sys->i_layer ||
357 i_next_frame_length != p_sys->i_frame_length )
359 /* Free bitrate only */
360 if( p_sys->i_bit_rate == 0 )
362 p_sys->i_frame_size++;
366 msg_Dbg( p_dec, "parameters changed unexpectedly "
367 "(emulated startcode ?)" );
368 block_SkipByte( &p_sys->bytestream );
369 p_sys->i_state = STATE_NOSYNC;
373 /* Free bitrate only */
374 if( p_sys->i_bit_rate == 0 )
376 if( i_next_bit_rate != 0 )
378 p_sys->i_frame_size++;
386 /* Free bitrate only */
387 if( p_sys->i_bit_rate == 0 )
389 if( (unsigned int)p_sys->i_frame_size >
390 p_sys->i_max_frame_size )
392 msg_Dbg( p_dec, "frame too big %d > %d "
393 "(emulated startcode ?)", p_sys->i_frame_size,
394 p_sys->i_max_frame_size );
395 block_SkipByte( &p_sys->bytestream );
396 p_sys->i_state = STATE_NOSYNC;
397 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
401 p_sys->i_frame_size++;
405 msg_Dbg( p_dec, "emulated startcode "
406 "(no startcode on following frame)" );
407 p_sys->i_state = STATE_NOSYNC;
408 block_SkipByte( &p_sys->bytestream );
412 p_sys->i_state = STATE_SEND_DATA;
416 /* Make sure we have enough data.
417 * (Not useful if we went through NEXT_SYNC) */
418 if( block_WaitBytes( &p_sys->bytestream,
419 p_sys->i_frame_size ) != VLC_SUCCESS )
424 p_sys->i_state = STATE_SEND_DATA;
426 case STATE_SEND_DATA:
427 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
429 //p_dec->b_error = VLC_TRUE;
433 /* Free bitrate only */
434 if( p_sys->i_bit_rate == 0 )
436 p_sys->i_free_frame_size = p_sys->i_frame_size;
439 /* Copy the whole frame into the buffer. When we reach this point
440 * we already know we have enough data available. */
441 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
443 /* Get beginning of next frame for libmad */
444 if( !p_sys->b_packetizer )
446 memcpy( p_buf + p_sys->i_frame_size,
447 p_header, MAD_BUFFER_GUARD );
450 p_sys->i_state = STATE_NOSYNC;
452 /* Make sure we don't reuse the same pts twice */
453 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
454 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
456 /* So p_block doesn't get re-added several times */
457 *pp_block = block_BytestreamPop( &p_sys->bytestream );
466 /*****************************************************************************
468 *****************************************************************************/
469 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
471 decoder_sys_t *p_sys = p_dec->p_sys;
474 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
476 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
477 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
479 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
480 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
483 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
484 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
485 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
486 p_dec->fmt_out.audio.i_bytes_per_frame =
487 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
489 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
490 p_dec->fmt_out.audio.i_physical_channels =
491 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
493 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
495 if( p_sys->b_packetizer )
497 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
498 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
499 *pp_out_buffer = p_sout_buffer;
503 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
504 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
505 *pp_out_buffer = p_aout_buffer;
511 /*****************************************************************************
513 *****************************************************************************/
514 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
516 decoder_sys_t *p_sys = p_dec->p_sys;
517 aout_buffer_t *p_buf;
519 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
520 if( p_buf == NULL ) return NULL;
522 p_buf->start_date = aout_DateGet( &p_sys->end_date );
524 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
526 /* Hack for libmad filter */
527 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
532 /*****************************************************************************
534 *****************************************************************************/
535 static block_t *GetSoutBuffer( decoder_t *p_dec )
537 decoder_sys_t *p_sys = p_dec->p_sys;
540 p_block = block_New( p_dec, p_sys->i_frame_size );
541 if( p_block == NULL ) return NULL;
543 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
546 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
552 /*****************************************************************************
553 * CloseDecoder: clean up the decoder
554 *****************************************************************************/
555 static void CloseDecoder( vlc_object_t *p_this )
557 decoder_t *p_dec = (decoder_t *)p_this;
558 decoder_sys_t *p_sys = p_dec->p_sys;
560 block_BytestreamRelease( &p_sys->bytestream );
565 /*****************************************************************************
566 * SyncInfo: parse MPEG audio sync info
567 *****************************************************************************/
568 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
569 unsigned int * pi_channels_conf,
570 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
571 unsigned int * pi_frame_length,
572 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
574 static const int ppi_bitrate[2][3][16] =
578 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
581 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
584 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
590 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
593 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
596 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
601 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
603 { 44100, 48000, 32000, 0 },
604 { 22050, 24000, 16000, 0 }
607 int i_version, i_mode, i_emphasis;
608 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
609 int i_frame_size = 0;
610 int i_bitrate_index, i_samplerate_index;
613 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
614 i_version = 1 - ((i_header & 0x80000) >> 19);
615 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
616 b_crc = !((i_header >> 16) & 0x01);
617 i_bitrate_index = (i_header & 0xf000) >> 12;
618 i_samplerate_index = (i_header & 0xc00) >> 10;
619 b_padding = (i_header & 0x200) >> 9;
621 i_mode = (i_header & 0xc0) >> 6;
622 /* Modeext, copyright & original */
623 i_emphasis = i_header & 0x3;
625 if( *pi_layer != 4 &&
626 i_bitrate_index < 0x0f &&
627 i_samplerate_index != 0x03 &&
633 case 1: /* joint stereo */
635 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
637 case 2: /* dual-mono */
639 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
640 | AOUT_CHAN_DUALMONO;
644 *pi_channels_conf = AOUT_CHAN_CENTER;
647 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
648 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
649 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
653 *pi_sample_rate >>= 1;
659 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
661 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
662 *pi_sample_rate + 1 ) * 4;
663 *pi_frame_length = 384;
667 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
668 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
669 *pi_frame_length = 1152;
673 i_frame_size = ( i_version ? 72000 : 144000 ) *
674 *pi_bit_rate / *pi_sample_rate + b_padding;
675 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
676 i_max_bit_rate / *pi_sample_rate + 1;
677 *pi_frame_length = i_version ? 576 : 1152;