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 *****************************************************************************/
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
38 #include <vlc_input.h>
40 #include <vlc_block_helper.h>
42 /*****************************************************************************
43 * decoder_sys_t : decoder descriptor
44 *****************************************************************************/
55 block_bytestream_t bytestream;
60 audio_date_t end_date;
61 unsigned int i_current_layer;
65 int i_frame_size, i_free_frame_size;
66 unsigned int i_channels_conf, i_channels;
67 unsigned int i_rate, i_max_frame_size, i_frame_length;
68 unsigned int i_layer, i_bit_rate;
85 /* This isn't the place to put mad-specific stuff. However, it makes the
86 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
87 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
88 * bigger buffer. This has no implication on other plug-ins, and we only
89 * lose 8 bytes per frame. --Meuuh */
90 #define MAD_BUFFER_GUARD 8
91 #define MPGA_HEADER_SIZE 4
93 /****************************************************************************
95 ****************************************************************************/
96 static int OpenDecoder ( vlc_object_t * );
97 static int OpenPacketizer( vlc_object_t * );
98 static void CloseDecoder ( vlc_object_t * );
99 static void *DecodeBlock ( decoder_t *, block_t ** );
101 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
102 static aout_buffer_t *GetAoutBuffer( decoder_t * );
103 static block_t *GetSoutBuffer( decoder_t * );
105 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
106 unsigned int * pi_channels_conf,
107 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
108 unsigned int * pi_frame_length,
109 unsigned int * pi_max_frame_size,
110 unsigned int * pi_layer );
112 /*****************************************************************************
114 *****************************************************************************/
116 set_description( _("MPEG audio layer I/II/III decoder") );
117 set_category( CAT_INPUT );
118 set_subcategory( SUBCAT_INPUT_ACODEC );
119 #if defined(UNDER_CE)
120 set_capability( "decoder", 5 );
122 set_capability( "decoder", 100 );
124 set_callbacks( OpenDecoder, CloseDecoder );
127 set_description( _("MPEG audio layer I/II/III packetizer") );
128 set_capability( "packetizer", 10 );
129 set_callbacks( OpenPacketizer, CloseDecoder );
132 /*****************************************************************************
133 * OpenDecoder: probe the decoder and return score
134 *****************************************************************************/
135 static int OpenDecoder( vlc_object_t *p_this )
137 decoder_t *p_dec = (decoder_t*)p_this;
138 decoder_sys_t *p_sys;
140 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
145 /* HACK: Don't use this codec if we don't have an mpga audio filter */
146 if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
147 !module_Exists( p_this, "mpgatofixed32" ) )
152 /* Allocate the memory needed to store the decoder's structure */
153 if( ( p_dec->p_sys = p_sys =
154 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
156 msg_Err( p_dec, "out of memory" );
161 p_sys->b_packetizer = false;
162 p_sys->i_state = STATE_NOSYNC;
163 aout_DateSet( &p_sys->end_date, 0 );
164 p_sys->bytestream = block_BytestreamInit();
165 p_sys->b_discontinuity = false;
166 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
168 /* Set output properties */
169 p_dec->fmt_out.i_cat = AUDIO_ES;
170 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
171 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
174 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
176 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
179 /* Start with the minimum size for a free bitrate frame */
180 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
185 static int OpenPacketizer( vlc_object_t *p_this )
187 decoder_t *p_dec = (decoder_t*)p_this;
189 int i_ret = OpenDecoder( p_this );
191 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
196 /****************************************************************************
197 * DecodeBlock: the whole thing
198 ****************************************************************************
199 * This function is called just after the thread is launched.
200 ****************************************************************************/
201 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
203 decoder_sys_t *p_sys = p_dec->p_sys;
204 uint8_t p_header[MAD_BUFFER_GUARD];
209 if( !pp_block || !*pp_block ) return NULL;
211 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
213 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
215 p_sys->i_state = STATE_NOSYNC;
216 block_BytestreamFlush( &p_sys->bytestream );
218 // aout_DateSet( &p_sys->end_date, 0 );
219 block_Release( *pp_block );
220 p_sys->b_discontinuity = true;
224 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
226 /* We've just started the stream, wait for the first PTS. */
227 msg_Dbg( p_dec, "waiting for PTS" );
228 block_Release( *pp_block );
232 if( (*pp_block)->i_rate > 0 )
233 p_sys->i_input_rate = (*pp_block)->i_rate;
235 block_BytestreamPush( &p_sys->bytestream, *pp_block );
239 switch( p_sys->i_state )
243 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
246 /* Look for sync word - should be 0xffe */
247 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
249 p_sys->i_state = STATE_SYNC;
252 block_SkipByte( &p_sys->bytestream );
254 if( p_sys->i_state != STATE_SYNC )
256 block_BytestreamFlush( &p_sys->bytestream );
263 /* New frame, set the Presentation Time Stamp */
264 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
265 if( p_sys->i_pts != 0 &&
266 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
268 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
270 p_sys->i_state = STATE_HEADER;
273 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
274 if( block_PeekBytes( &p_sys->bytestream, p_header,
275 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
281 /* Build frame header */
282 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
285 /* Check if frame is valid and get frame info */
286 p_sys->i_frame_size = SyncInfo( i_header,
288 &p_sys->i_channels_conf,
291 &p_sys->i_frame_length,
292 &p_sys->i_max_frame_size,
295 if( p_sys->i_frame_size == -1 )
297 msg_Dbg( p_dec, "emulated startcode" );
298 block_SkipByte( &p_sys->bytestream );
299 p_sys->i_state = STATE_NOSYNC;
300 p_sys->b_discontinuity = true;
304 if( p_sys->i_bit_rate == 0 )
306 /* Free bitrate, but 99% emulated startcode :( */
307 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
309 msg_Dbg( p_dec, "free bitrate mode");
311 /* The -1 below is to account for the frame padding */
312 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
315 p_sys->i_state = STATE_NEXT_SYNC;
317 case STATE_NEXT_SYNC:
318 /* TODO: If p_block == NULL, flush the buffer without checking the
321 /* Check if next expected frame contains the sync word */
322 if( block_PeekOffsetBytes( &p_sys->bytestream,
323 p_sys->i_frame_size, p_header,
324 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
330 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
332 /* Startcode is fine, let's try the header as an extra check */
333 int i_next_frame_size;
334 unsigned int i_next_channels, i_next_channels_conf;
335 unsigned int i_next_rate, i_next_bit_rate;
336 unsigned int i_next_frame_length, i_next_max_frame_size;
337 unsigned int i_next_layer;
339 /* Build frame header */
340 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
343 i_next_frame_size = SyncInfo( i_header,
345 &i_next_channels_conf,
348 &i_next_frame_length,
349 &i_next_max_frame_size,
352 /* Free bitrate only */
353 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
355 if( (unsigned int)p_sys->i_frame_size >
356 p_sys->i_max_frame_size )
358 msg_Dbg( p_dec, "frame too big %d > %d "
359 "(emulated startcode ?)", p_sys->i_frame_size,
360 p_sys->i_max_frame_size );
361 block_SkipByte( &p_sys->bytestream );
362 p_sys->i_state = STATE_NOSYNC;
363 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
367 p_sys->i_frame_size++;
371 if( i_next_frame_size == -1 )
373 msg_Dbg( p_dec, "emulated startcode on next frame" );
374 block_SkipByte( &p_sys->bytestream );
375 p_sys->i_state = STATE_NOSYNC;
376 p_sys->b_discontinuity = true;
380 /* Check info is in sync with previous one */
381 if( i_next_channels_conf != p_sys->i_channels_conf ||
382 i_next_rate != p_sys->i_rate ||
383 i_next_layer != p_sys->i_layer ||
384 i_next_frame_length != p_sys->i_frame_length )
386 /* Free bitrate only */
387 if( p_sys->i_bit_rate == 0 )
389 p_sys->i_frame_size++;
393 msg_Dbg( p_dec, "parameters changed unexpectedly "
394 "(emulated startcode ?)" );
395 block_SkipByte( &p_sys->bytestream );
396 p_sys->i_state = STATE_NOSYNC;
400 /* Free bitrate only */
401 if( p_sys->i_bit_rate == 0 )
403 if( i_next_bit_rate != 0 )
405 p_sys->i_frame_size++;
413 /* Free bitrate only */
414 if( p_sys->i_bit_rate == 0 )
416 if( (unsigned int)p_sys->i_frame_size >
417 p_sys->i_max_frame_size )
419 msg_Dbg( p_dec, "frame too big %d > %d "
420 "(emulated startcode ?)", p_sys->i_frame_size,
421 p_sys->i_max_frame_size );
422 block_SkipByte( &p_sys->bytestream );
423 p_sys->i_state = STATE_NOSYNC;
424 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
428 p_sys->i_frame_size++;
432 msg_Dbg( p_dec, "emulated startcode "
433 "(no startcode on following frame)" );
434 p_sys->i_state = STATE_NOSYNC;
435 block_SkipByte( &p_sys->bytestream );
439 p_sys->i_state = STATE_SEND_DATA;
443 /* Make sure we have enough data.
444 * (Not useful if we went through NEXT_SYNC) */
445 if( block_WaitBytes( &p_sys->bytestream,
446 p_sys->i_frame_size ) != VLC_SUCCESS )
451 p_sys->i_state = STATE_SEND_DATA;
453 case STATE_SEND_DATA:
454 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
456 //p_dec->b_error = true;
460 /* Free bitrate only */
461 if( p_sys->i_bit_rate == 0 )
463 p_sys->i_free_frame_size = p_sys->i_frame_size;
466 /* Copy the whole frame into the buffer. When we reach this point
467 * we already know we have enough data available. */
468 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
470 /* Get beginning of next frame for libmad */
471 if( !p_sys->b_packetizer )
473 memcpy( p_buf + p_sys->i_frame_size,
474 p_header, MAD_BUFFER_GUARD );
477 p_sys->i_state = STATE_NOSYNC;
479 /* Make sure we don't reuse the same pts twice */
480 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
481 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
483 /* So p_block doesn't get re-added several times */
484 *pp_block = block_BytestreamPop( &p_sys->bytestream );
493 /*****************************************************************************
495 *****************************************************************************/
496 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
498 decoder_sys_t *p_sys = p_dec->p_sys;
501 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
503 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
504 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
506 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
507 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
510 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
511 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
512 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
513 p_dec->fmt_out.audio.i_bytes_per_frame =
514 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
516 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
517 p_dec->fmt_out.audio.i_physical_channels =
518 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
520 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
522 if( p_sys->b_packetizer )
524 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
525 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
526 *pp_out_buffer = p_sout_buffer;
530 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
531 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
532 *pp_out_buffer = p_aout_buffer;
538 /*****************************************************************************
540 *****************************************************************************/
541 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
543 decoder_sys_t *p_sys = p_dec->p_sys;
544 aout_buffer_t *p_buf;
546 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
547 if( p_buf == NULL ) return NULL;
549 p_buf->start_date = aout_DateGet( &p_sys->end_date );
551 aout_DateIncrement( &p_sys->end_date,
552 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
553 p_buf->b_discontinuity = p_sys->b_discontinuity;
554 p_sys->b_discontinuity = false;
556 /* Hack for libmad filter */
557 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
562 /*****************************************************************************
564 *****************************************************************************/
565 static block_t *GetSoutBuffer( decoder_t *p_dec )
567 decoder_sys_t *p_sys = p_dec->p_sys;
570 p_block = block_New( p_dec, p_sys->i_frame_size );
571 if( p_block == NULL ) return NULL;
573 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
576 aout_DateIncrement( &p_sys->end_date,
577 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
583 /*****************************************************************************
584 * CloseDecoder: clean up the decoder
585 *****************************************************************************/
586 static void CloseDecoder( vlc_object_t *p_this )
588 decoder_t *p_dec = (decoder_t *)p_this;
589 decoder_sys_t *p_sys = p_dec->p_sys;
591 block_BytestreamRelease( &p_sys->bytestream );
596 /*****************************************************************************
597 * SyncInfo: parse MPEG audio sync info
598 *****************************************************************************/
599 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
600 unsigned int * pi_channels_conf,
601 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
602 unsigned int * pi_frame_length,
603 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
605 static const int ppi_bitrate[2][3][16] =
609 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
612 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
615 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
621 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
624 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
627 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
632 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
634 { 44100, 48000, 32000, 0 },
635 { 22050, 24000, 16000, 0 }
638 int i_version, i_mode, i_emphasis;
639 bool b_padding, b_mpeg_2_5, b_crc;
640 int i_frame_size = 0;
641 int i_bitrate_index, i_samplerate_index;
644 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
645 i_version = 1 - ((i_header & 0x80000) >> 19);
646 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
647 b_crc = !((i_header >> 16) & 0x01);
648 i_bitrate_index = (i_header & 0xf000) >> 12;
649 i_samplerate_index = (i_header & 0xc00) >> 10;
650 b_padding = (i_header & 0x200) >> 9;
652 i_mode = (i_header & 0xc0) >> 6;
653 /* Modeext, copyright & original */
654 i_emphasis = i_header & 0x3;
656 if( *pi_layer != 4 &&
657 i_bitrate_index < 0x0f &&
658 i_samplerate_index != 0x03 &&
664 case 1: /* joint stereo */
666 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
668 case 2: /* dual-mono */
670 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
671 | AOUT_CHAN_DUALMONO;
675 *pi_channels_conf = AOUT_CHAN_CENTER;
678 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
679 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
680 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
684 *pi_sample_rate >>= 1;
690 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
692 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
693 *pi_sample_rate + 1 ) * 4;
694 *pi_frame_length = 384;
698 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
699 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
700 *pi_frame_length = 1152;
704 i_frame_size = ( i_version ? 72000 : 144000 ) *
705 *pi_bit_rate / *pi_sample_rate + b_padding;
706 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
707 i_max_bit_rate / *pi_sample_rate + 1;
708 *pi_frame_length = i_version ? 576 : 1152;
715 /* Free bitrate mode can support higher bitrates */
716 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;