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>
33 #include <vlc_input.h>
35 #include <vlc_block_helper.h>
37 /*****************************************************************************
38 * decoder_sys_t : decoder descriptor
39 *****************************************************************************/
43 vlc_bool_t b_packetizer;
50 block_bytestream_t bytestream;
55 audio_date_t end_date;
56 unsigned int i_current_layer;
60 int i_frame_size, i_free_frame_size;
61 unsigned int i_channels_conf, i_channels;
62 unsigned int i_rate, i_max_frame_size, i_frame_length;
63 unsigned int i_layer, i_bit_rate;
65 vlc_bool_t b_discontinuity;
80 /* This isn't the place to put mad-specific stuff. However, it makes the
81 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
82 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
83 * bigger buffer. This has no implication on other plug-ins, and we only
84 * lose 8 bytes per frame. --Meuuh */
85 #define MAD_BUFFER_GUARD 8
86 #define MPGA_HEADER_SIZE 4
88 /****************************************************************************
90 ****************************************************************************/
91 static int OpenDecoder ( vlc_object_t * );
92 static int OpenPacketizer( vlc_object_t * );
93 static void CloseDecoder ( vlc_object_t * );
94 static void *DecodeBlock ( decoder_t *, block_t ** );
96 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
97 static aout_buffer_t *GetAoutBuffer( decoder_t * );
98 static block_t *GetSoutBuffer( decoder_t * );
100 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
101 unsigned int * pi_channels_conf,
102 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
103 unsigned int * pi_frame_length,
104 unsigned int * pi_max_frame_size,
105 unsigned int * pi_layer );
107 /*****************************************************************************
109 *****************************************************************************/
111 set_description( _("MPEG audio layer I/II/III decoder") );
112 set_category( CAT_INPUT );
113 set_subcategory( SUBCAT_INPUT_ACODEC );
114 #if defined(UNDER_CE)
115 set_capability( "decoder", 5 );
117 set_capability( "decoder", 100 );
119 set_callbacks( OpenDecoder, CloseDecoder );
122 set_description( _("MPEG audio layer I/II/III packetizer") );
123 set_capability( "packetizer", 10 );
124 set_callbacks( OpenPacketizer, CloseDecoder );
127 /*****************************************************************************
128 * OpenDecoder: probe the decoder and return score
129 *****************************************************************************/
130 static int OpenDecoder( vlc_object_t *p_this )
132 decoder_t *p_dec = (decoder_t*)p_this;
133 decoder_sys_t *p_sys;
135 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
140 /* HACK: Don't use this codec if we don't have an mpga audio filter */
141 if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
142 !config_FindModule( p_this, "mpgatofixed32" ) )
147 /* Allocate the memory needed to store the decoder's structure */
148 if( ( p_dec->p_sys = p_sys =
149 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
151 msg_Err( p_dec, "out of memory" );
156 p_sys->b_packetizer = VLC_FALSE;
157 p_sys->i_state = STATE_NOSYNC;
158 aout_DateSet( &p_sys->end_date, 0 );
159 p_sys->bytestream = block_BytestreamInit( p_dec );
160 p_sys->b_discontinuity = VLC_FALSE;
161 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
163 /* Set output properties */
164 p_dec->fmt_out.i_cat = AUDIO_ES;
165 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
166 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
169 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
171 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
174 /* Start with the minimum size for a free bitrate frame */
175 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
180 static int OpenPacketizer( vlc_object_t *p_this )
182 decoder_t *p_dec = (decoder_t*)p_this;
184 int i_ret = OpenDecoder( p_this );
186 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
191 /****************************************************************************
192 * DecodeBlock: the whole thing
193 ****************************************************************************
194 * This function is called just after the thread is launched.
195 ****************************************************************************/
196 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
198 decoder_sys_t *p_sys = p_dec->p_sys;
199 uint8_t p_header[MAD_BUFFER_GUARD];
204 if( !pp_block || !*pp_block ) return NULL;
206 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
208 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
210 p_sys->i_state = STATE_NOSYNC;
211 block_BytestreamFlush( &p_sys->bytestream );
213 // aout_DateSet( &p_sys->end_date, 0 );
214 block_Release( *pp_block );
215 p_sys->b_discontinuity = VLC_TRUE;
219 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
221 /* We've just started the stream, wait for the first PTS. */
222 msg_Dbg( p_dec, "waiting for PTS" );
223 block_Release( *pp_block );
227 if( (*pp_block)->i_rate > 0 )
228 p_sys->i_input_rate = (*pp_block)->i_rate;
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 != 0 &&
261 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
263 aout_DateSet( &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 if( p_sys->i_frame_size == -1 )
292 msg_Dbg( p_dec, "emulated startcode" );
293 block_SkipByte( &p_sys->bytestream );
294 p_sys->i_state = STATE_NOSYNC;
295 p_sys->b_discontinuity = VLC_TRUE;
299 if( p_sys->i_bit_rate == 0 )
301 /* Free bitrate, but 99% emulated startcode :( */
302 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
304 msg_Dbg( p_dec, "free bitrate mode");
306 /* The -1 below is to account for the frame padding */
307 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
310 p_sys->i_state = STATE_NEXT_SYNC;
312 case STATE_NEXT_SYNC:
313 /* TODO: If p_block == NULL, flush the buffer without checking the
316 /* Check if next expected frame contains the sync word */
317 if( block_PeekOffsetBytes( &p_sys->bytestream,
318 p_sys->i_frame_size, p_header,
319 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
325 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
327 /* Startcode is fine, let's try the header as an extra check */
328 int i_next_frame_size;
329 unsigned int i_next_channels, i_next_channels_conf;
330 unsigned int i_next_rate, i_next_bit_rate;
331 unsigned int i_next_frame_length, i_next_max_frame_size;
332 unsigned int i_next_layer;
334 /* Build frame header */
335 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
338 i_next_frame_size = SyncInfo( i_header,
340 &i_next_channels_conf,
343 &i_next_frame_length,
344 &i_next_max_frame_size,
347 /* Free bitrate only */
348 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
350 if( (unsigned int)p_sys->i_frame_size >
351 p_sys->i_max_frame_size )
353 msg_Dbg( p_dec, "frame too big %d > %d "
354 "(emulated startcode ?)", p_sys->i_frame_size,
355 p_sys->i_max_frame_size );
356 block_SkipByte( &p_sys->bytestream );
357 p_sys->i_state = STATE_NOSYNC;
358 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
362 p_sys->i_frame_size++;
366 if( i_next_frame_size == -1 )
368 msg_Dbg( p_dec, "emulated startcode on next frame" );
369 block_SkipByte( &p_sys->bytestream );
370 p_sys->i_state = STATE_NOSYNC;
371 p_sys->b_discontinuity = VLC_TRUE;
375 /* Check info is in sync with previous one */
376 if( i_next_channels_conf != p_sys->i_channels_conf ||
377 i_next_rate != p_sys->i_rate ||
378 i_next_layer != p_sys->i_layer ||
379 i_next_frame_length != p_sys->i_frame_length )
381 /* Free bitrate only */
382 if( p_sys->i_bit_rate == 0 )
384 p_sys->i_frame_size++;
388 msg_Dbg( p_dec, "parameters changed unexpectedly "
389 "(emulated startcode ?)" );
390 block_SkipByte( &p_sys->bytestream );
391 p_sys->i_state = STATE_NOSYNC;
395 /* Free bitrate only */
396 if( p_sys->i_bit_rate == 0 )
398 if( i_next_bit_rate != 0 )
400 p_sys->i_frame_size++;
408 /* Free bitrate only */
409 if( p_sys->i_bit_rate == 0 )
411 if( (unsigned int)p_sys->i_frame_size >
412 p_sys->i_max_frame_size )
414 msg_Dbg( p_dec, "frame too big %d > %d "
415 "(emulated startcode ?)", p_sys->i_frame_size,
416 p_sys->i_max_frame_size );
417 block_SkipByte( &p_sys->bytestream );
418 p_sys->i_state = STATE_NOSYNC;
419 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
423 p_sys->i_frame_size++;
427 msg_Dbg( p_dec, "emulated startcode "
428 "(no startcode on following frame)" );
429 p_sys->i_state = STATE_NOSYNC;
430 block_SkipByte( &p_sys->bytestream );
434 p_sys->i_state = STATE_SEND_DATA;
438 /* Make sure we have enough data.
439 * (Not useful if we went through NEXT_SYNC) */
440 if( block_WaitBytes( &p_sys->bytestream,
441 p_sys->i_frame_size ) != VLC_SUCCESS )
446 p_sys->i_state = STATE_SEND_DATA;
448 case STATE_SEND_DATA:
449 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
451 //p_dec->b_error = VLC_TRUE;
455 /* Free bitrate only */
456 if( p_sys->i_bit_rate == 0 )
458 p_sys->i_free_frame_size = p_sys->i_frame_size;
461 /* Copy the whole frame into the buffer. When we reach this point
462 * we already know we have enough data available. */
463 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
465 /* Get beginning of next frame for libmad */
466 if( !p_sys->b_packetizer )
468 memcpy( p_buf + p_sys->i_frame_size,
469 p_header, MAD_BUFFER_GUARD );
472 p_sys->i_state = STATE_NOSYNC;
474 /* Make sure we don't reuse the same pts twice */
475 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
476 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
478 /* So p_block doesn't get re-added several times */
479 *pp_block = block_BytestreamPop( &p_sys->bytestream );
488 /*****************************************************************************
490 *****************************************************************************/
491 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
493 decoder_sys_t *p_sys = p_dec->p_sys;
496 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
498 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
499 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
501 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
502 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
505 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
506 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
507 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
508 p_dec->fmt_out.audio.i_bytes_per_frame =
509 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
511 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
512 p_dec->fmt_out.audio.i_physical_channels =
513 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
515 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
517 if( p_sys->b_packetizer )
519 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
520 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
521 *pp_out_buffer = p_sout_buffer;
525 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
526 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
527 *pp_out_buffer = p_aout_buffer;
533 /*****************************************************************************
535 *****************************************************************************/
536 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
538 decoder_sys_t *p_sys = p_dec->p_sys;
539 aout_buffer_t *p_buf;
541 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
542 if( p_buf == NULL ) return NULL;
544 p_buf->start_date = aout_DateGet( &p_sys->end_date );
546 aout_DateIncrement( &p_sys->end_date,
547 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
548 p_buf->b_discontinuity = p_sys->b_discontinuity;
549 p_sys->b_discontinuity = VLC_FALSE;
551 /* Hack for libmad filter */
552 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
557 /*****************************************************************************
559 *****************************************************************************/
560 static block_t *GetSoutBuffer( decoder_t *p_dec )
562 decoder_sys_t *p_sys = p_dec->p_sys;
565 p_block = block_New( p_dec, p_sys->i_frame_size );
566 if( p_block == NULL ) return NULL;
568 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
571 aout_DateIncrement( &p_sys->end_date,
572 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
578 /*****************************************************************************
579 * CloseDecoder: clean up the decoder
580 *****************************************************************************/
581 static void CloseDecoder( vlc_object_t *p_this )
583 decoder_t *p_dec = (decoder_t *)p_this;
584 decoder_sys_t *p_sys = p_dec->p_sys;
586 block_BytestreamRelease( &p_sys->bytestream );
591 /*****************************************************************************
592 * SyncInfo: parse MPEG audio sync info
593 *****************************************************************************/
594 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
595 unsigned int * pi_channels_conf,
596 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
597 unsigned int * pi_frame_length,
598 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
600 static const int ppi_bitrate[2][3][16] =
604 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
607 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
610 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
616 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
619 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
622 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
627 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
629 { 44100, 48000, 32000, 0 },
630 { 22050, 24000, 16000, 0 }
633 int i_version, i_mode, i_emphasis;
634 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
635 int i_frame_size = 0;
636 int i_bitrate_index, i_samplerate_index;
639 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
640 i_version = 1 - ((i_header & 0x80000) >> 19);
641 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
642 b_crc = !((i_header >> 16) & 0x01);
643 i_bitrate_index = (i_header & 0xf000) >> 12;
644 i_samplerate_index = (i_header & 0xc00) >> 10;
645 b_padding = (i_header & 0x200) >> 9;
647 i_mode = (i_header & 0xc0) >> 6;
648 /* Modeext, copyright & original */
649 i_emphasis = i_header & 0x3;
651 if( *pi_layer != 4 &&
652 i_bitrate_index < 0x0f &&
653 i_samplerate_index != 0x03 &&
659 case 1: /* joint stereo */
661 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
663 case 2: /* dual-mono */
665 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
666 | AOUT_CHAN_DUALMONO;
670 *pi_channels_conf = AOUT_CHAN_CENTER;
673 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
674 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
675 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
679 *pi_sample_rate >>= 1;
685 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
687 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
688 *pi_sample_rate + 1 ) * 4;
689 *pi_frame_length = 384;
693 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
694 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
695 *pi_frame_length = 1152;
699 i_frame_size = ( i_version ? 72000 : 144000 ) *
700 *pi_bit_rate / *pi_sample_rate + b_padding;
701 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
702 i_max_bit_rate / *pi_sample_rate + 1;
703 *pi_frame_length = i_version ? 576 : 1152;
710 /* Free bitrate mode can support higher bitrates */
711 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;