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;
64 vlc_bool_t b_discontinuity;
77 /* This isn't the place to put mad-specific stuff. However, it makes the
78 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
79 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
80 * bigger buffer. This has no implication on other plug-ins, and we only
81 * lose 8 bytes per frame. --Meuuh */
82 #define MAD_BUFFER_GUARD 8
83 #define MPGA_HEADER_SIZE 4
85 /****************************************************************************
87 ****************************************************************************/
88 static int OpenDecoder ( vlc_object_t * );
89 static int OpenPacketizer( vlc_object_t * );
90 static void CloseDecoder ( vlc_object_t * );
91 static void *DecodeBlock ( decoder_t *, block_t ** );
93 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
94 static aout_buffer_t *GetAoutBuffer( decoder_t * );
95 static block_t *GetSoutBuffer( decoder_t * );
97 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
98 unsigned int * pi_channels_conf,
99 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
100 unsigned int * pi_frame_length,
101 unsigned int * pi_max_frame_size,
102 unsigned int * pi_layer );
104 /*****************************************************************************
106 *****************************************************************************/
108 set_description( _("MPEG audio layer I/II/III decoder") );
109 set_category( CAT_INPUT );
110 set_subcategory( SUBCAT_INPUT_ACODEC );
111 #if defined(UNDER_CE)
112 set_capability( "decoder", 5 );
114 set_capability( "decoder", 100 );
116 set_callbacks( OpenDecoder, CloseDecoder );
119 set_description( _("MPEG audio layer I/II/III packetizer") );
120 set_capability( "packetizer", 10 );
121 set_callbacks( OpenPacketizer, CloseDecoder );
124 /*****************************************************************************
125 * OpenDecoder: probe the decoder and return score
126 *****************************************************************************/
127 static int OpenDecoder( vlc_object_t *p_this )
129 decoder_t *p_dec = (decoder_t*)p_this;
130 decoder_sys_t *p_sys;
132 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
137 /* HACK: Don't use this codec if we don't have an mpga audio filter */
138 if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
139 !config_FindModule( p_this, "mpgatofixed32" ) )
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 )
148 msg_Err( p_dec, "out of memory" );
153 p_sys->b_packetizer = VLC_FALSE;
154 p_sys->i_state = STATE_NOSYNC;
155 aout_DateSet( &p_sys->end_date, 0 );
156 p_sys->bytestream = block_BytestreamInit( p_dec );
157 p_sys->b_discontinuity = VLC_FALSE;
159 /* Set output properties */
160 p_dec->fmt_out.i_cat = AUDIO_ES;
161 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
162 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
165 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
167 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
170 /* Start with the minimum size for a free bitrate frame */
171 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
176 static int OpenPacketizer( vlc_object_t *p_this )
178 decoder_t *p_dec = (decoder_t*)p_this;
180 int i_ret = OpenDecoder( p_this );
182 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
187 /****************************************************************************
188 * DecodeBlock: the whole thing
189 ****************************************************************************
190 * This function is called just after the thread is launched.
191 ****************************************************************************/
192 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
194 decoder_sys_t *p_sys = p_dec->p_sys;
195 uint8_t p_header[MAD_BUFFER_GUARD];
200 if( !pp_block || !*pp_block ) return NULL;
202 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
204 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
206 p_sys->i_state = STATE_NOSYNC;
207 block_BytestreamFlush( &p_sys->bytestream );
209 // aout_DateSet( &p_sys->end_date, 0 );
210 block_Release( *pp_block );
211 p_sys->b_discontinuity = VLC_TRUE;
215 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
217 /* We've just started the stream, wait for the first PTS. */
218 msg_Dbg( p_dec, "waiting for PTS" );
219 block_Release( *pp_block );
223 block_BytestreamPush( &p_sys->bytestream, *pp_block );
227 switch( p_sys->i_state )
231 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
234 /* Look for sync word - should be 0xffe */
235 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
237 p_sys->i_state = STATE_SYNC;
240 block_SkipByte( &p_sys->bytestream );
242 if( p_sys->i_state != STATE_SYNC )
244 block_BytestreamFlush( &p_sys->bytestream );
251 /* New frame, set the Presentation Time Stamp */
252 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
253 if( p_sys->i_pts != 0 &&
254 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
256 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
258 p_sys->i_state = STATE_HEADER;
261 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
262 if( block_PeekBytes( &p_sys->bytestream, p_header,
263 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
269 /* Build frame header */
270 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
273 /* Check if frame is valid and get frame info */
274 p_sys->i_frame_size = SyncInfo( i_header,
276 &p_sys->i_channels_conf,
279 &p_sys->i_frame_length,
280 &p_sys->i_max_frame_size,
283 if( p_sys->i_frame_size == -1 )
285 msg_Dbg( p_dec, "emulated startcode" );
286 block_SkipByte( &p_sys->bytestream );
287 p_sys->i_state = STATE_NOSYNC;
288 p_sys->b_discontinuity = VLC_TRUE;
292 if( p_sys->i_bit_rate == 0 )
294 /* Free birate, but 99% emulated startcode :( */
295 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
297 msg_Dbg( p_dec, "free bitrate mode");
299 p_sys->i_frame_size = p_sys->i_free_frame_size;
302 p_sys->i_state = STATE_NEXT_SYNC;
304 case STATE_NEXT_SYNC:
305 /* TODO: If p_block == NULL, flush the buffer without checking the
308 /* Check if next expected frame contains the sync word */
309 if( block_PeekOffsetBytes( &p_sys->bytestream,
310 p_sys->i_frame_size, p_header,
311 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
317 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
319 /* Startcode is fine, let's try the header as an extra check */
320 int i_next_frame_size;
321 unsigned int i_next_channels, i_next_channels_conf;
322 unsigned int i_next_rate, i_next_bit_rate;
323 unsigned int i_next_frame_length, i_next_max_frame_size;
324 unsigned int i_next_layer;
326 /* Build frame header */
327 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
330 i_next_frame_size = SyncInfo( i_header,
332 &i_next_channels_conf,
335 &i_next_frame_length,
336 &i_next_max_frame_size,
339 /* Free bitrate only */
340 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
342 if( (unsigned int)p_sys->i_frame_size >
343 p_sys->i_max_frame_size )
345 msg_Dbg( p_dec, "frame too big %d > %d "
346 "(emulated startcode ?)", p_sys->i_frame_size,
347 p_sys->i_max_frame_size );
348 block_SkipByte( &p_sys->bytestream );
349 p_sys->i_state = STATE_NOSYNC;
350 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
354 p_sys->i_frame_size++;
358 if( i_next_frame_size == -1 )
360 msg_Dbg( p_dec, "emulated startcode on next frame" );
361 block_SkipByte( &p_sys->bytestream );
362 p_sys->i_state = STATE_NOSYNC;
363 p_sys->b_discontinuity = VLC_TRUE;
367 /* Check info is in sync with previous one */
368 if( i_next_channels_conf != p_sys->i_channels_conf ||
369 i_next_rate != p_sys->i_rate ||
370 i_next_layer != p_sys->i_layer ||
371 i_next_frame_length != p_sys->i_frame_length )
373 /* Free bitrate only */
374 if( p_sys->i_bit_rate == 0 )
376 p_sys->i_frame_size++;
380 msg_Dbg( p_dec, "parameters changed unexpectedly "
381 "(emulated startcode ?)" );
382 block_SkipByte( &p_sys->bytestream );
383 p_sys->i_state = STATE_NOSYNC;
387 /* Free bitrate only */
388 if( p_sys->i_bit_rate == 0 )
390 if( i_next_bit_rate != 0 )
392 p_sys->i_frame_size++;
400 /* Free bitrate only */
401 if( p_sys->i_bit_rate == 0 )
403 if( (unsigned int)p_sys->i_frame_size >
404 p_sys->i_max_frame_size )
406 msg_Dbg( p_dec, "frame too big %d > %d "
407 "(emulated startcode ?)", p_sys->i_frame_size,
408 p_sys->i_max_frame_size );
409 block_SkipByte( &p_sys->bytestream );
410 p_sys->i_state = STATE_NOSYNC;
411 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
415 p_sys->i_frame_size++;
419 msg_Dbg( p_dec, "emulated startcode "
420 "(no startcode on following frame)" );
421 p_sys->i_state = STATE_NOSYNC;
422 block_SkipByte( &p_sys->bytestream );
426 p_sys->i_state = STATE_SEND_DATA;
430 /* Make sure we have enough data.
431 * (Not useful if we went through NEXT_SYNC) */
432 if( block_WaitBytes( &p_sys->bytestream,
433 p_sys->i_frame_size ) != VLC_SUCCESS )
438 p_sys->i_state = STATE_SEND_DATA;
440 case STATE_SEND_DATA:
441 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
443 //p_dec->b_error = VLC_TRUE;
447 /* Free bitrate only */
448 if( p_sys->i_bit_rate == 0 )
450 p_sys->i_free_frame_size = p_sys->i_frame_size;
453 /* Copy the whole frame into the buffer. When we reach this point
454 * we already know we have enough data available. */
455 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
457 /* Get beginning of next frame for libmad */
458 if( !p_sys->b_packetizer )
460 memcpy( p_buf + p_sys->i_frame_size,
461 p_header, MAD_BUFFER_GUARD );
464 p_sys->i_state = STATE_NOSYNC;
466 /* Make sure we don't reuse the same pts twice */
467 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
468 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
470 /* So p_block doesn't get re-added several times */
471 *pp_block = block_BytestreamPop( &p_sys->bytestream );
480 /*****************************************************************************
482 *****************************************************************************/
483 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
485 decoder_sys_t *p_sys = p_dec->p_sys;
488 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
490 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
491 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
493 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
494 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
497 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
498 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
499 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
500 p_dec->fmt_out.audio.i_bytes_per_frame =
501 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
503 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
504 p_dec->fmt_out.audio.i_physical_channels =
505 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
507 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
509 if( p_sys->b_packetizer )
511 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
512 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
513 *pp_out_buffer = p_sout_buffer;
517 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
518 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
519 *pp_out_buffer = p_aout_buffer;
525 /*****************************************************************************
527 *****************************************************************************/
528 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
530 decoder_sys_t *p_sys = p_dec->p_sys;
531 aout_buffer_t *p_buf;
533 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
534 if( p_buf == NULL ) return NULL;
536 p_buf->start_date = aout_DateGet( &p_sys->end_date );
538 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
539 p_buf->b_discontinuity = p_sys->b_discontinuity;
540 p_sys->b_discontinuity = VLC_FALSE;
542 /* Hack for libmad filter */
543 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
548 /*****************************************************************************
550 *****************************************************************************/
551 static block_t *GetSoutBuffer( decoder_t *p_dec )
553 decoder_sys_t *p_sys = p_dec->p_sys;
556 p_block = block_New( p_dec, p_sys->i_frame_size );
557 if( p_block == NULL ) return NULL;
559 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
562 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
568 /*****************************************************************************
569 * CloseDecoder: clean up the decoder
570 *****************************************************************************/
571 static void CloseDecoder( vlc_object_t *p_this )
573 decoder_t *p_dec = (decoder_t *)p_this;
574 decoder_sys_t *p_sys = p_dec->p_sys;
576 block_BytestreamRelease( &p_sys->bytestream );
581 /*****************************************************************************
582 * SyncInfo: parse MPEG audio sync info
583 *****************************************************************************/
584 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
585 unsigned int * pi_channels_conf,
586 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
587 unsigned int * pi_frame_length,
588 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
590 static const int ppi_bitrate[2][3][16] =
594 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
597 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
600 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
606 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
609 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
612 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
617 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
619 { 44100, 48000, 32000, 0 },
620 { 22050, 24000, 16000, 0 }
623 int i_version, i_mode, i_emphasis;
624 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
625 int i_frame_size = 0;
626 int i_bitrate_index, i_samplerate_index;
629 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
630 i_version = 1 - ((i_header & 0x80000) >> 19);
631 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
632 b_crc = !((i_header >> 16) & 0x01);
633 i_bitrate_index = (i_header & 0xf000) >> 12;
634 i_samplerate_index = (i_header & 0xc00) >> 10;
635 b_padding = (i_header & 0x200) >> 9;
637 i_mode = (i_header & 0xc0) >> 6;
638 /* Modeext, copyright & original */
639 i_emphasis = i_header & 0x3;
641 if( *pi_layer != 4 &&
642 i_bitrate_index < 0x0f &&
643 i_samplerate_index != 0x03 &&
649 case 1: /* joint stereo */
651 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
653 case 2: /* dual-mono */
655 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
656 | AOUT_CHAN_DUALMONO;
660 *pi_channels_conf = AOUT_CHAN_CENTER;
663 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
664 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
665 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
669 *pi_sample_rate >>= 1;
675 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
677 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
678 *pi_sample_rate + 1 ) * 4;
679 *pi_frame_length = 384;
683 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
684 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
685 *pi_frame_length = 1152;
689 i_frame_size = ( i_version ? 72000 : 144000 ) *
690 *pi_bit_rate / *pi_sample_rate + b_padding;
691 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
692 i_max_bit_rate / *pi_sample_rate + 1;
693 *pi_frame_length = i_version ? 576 : 1152;