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_codec.h>
37 #include <vlc_input.h>
39 #include <vlc_block_helper.h>
41 /*****************************************************************************
42 * decoder_sys_t : decoder descriptor
43 *****************************************************************************/
47 vlc_bool_t b_packetizer;
54 block_bytestream_t bytestream;
59 audio_date_t end_date;
60 unsigned int i_current_layer;
64 int i_frame_size, i_free_frame_size;
65 unsigned int i_channels_conf, i_channels;
66 unsigned int i_rate, i_max_frame_size, i_frame_length;
67 unsigned int i_layer, i_bit_rate;
69 vlc_bool_t b_discontinuity;
84 /* This isn't the place to put mad-specific stuff. However, it makes the
85 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
86 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
87 * bigger buffer. This has no implication on other plug-ins, and we only
88 * lose 8 bytes per frame. --Meuuh */
89 #define MAD_BUFFER_GUARD 8
90 #define MPGA_HEADER_SIZE 4
92 /****************************************************************************
94 ****************************************************************************/
95 static int OpenDecoder ( vlc_object_t * );
96 static int OpenPacketizer( vlc_object_t * );
97 static void CloseDecoder ( vlc_object_t * );
98 static void *DecodeBlock ( decoder_t *, block_t ** );
100 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
101 static aout_buffer_t *GetAoutBuffer( decoder_t * );
102 static block_t *GetSoutBuffer( decoder_t * );
104 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
105 unsigned int * pi_channels_conf,
106 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
107 unsigned int * pi_frame_length,
108 unsigned int * pi_max_frame_size,
109 unsigned int * pi_layer );
111 /*****************************************************************************
113 *****************************************************************************/
115 set_description( _("MPEG audio layer I/II/III decoder") );
116 set_category( CAT_INPUT );
117 set_subcategory( SUBCAT_INPUT_ACODEC );
118 #if defined(UNDER_CE)
119 set_capability( "decoder", 5 );
121 set_capability( "decoder", 100 );
123 set_callbacks( OpenDecoder, CloseDecoder );
126 set_description( _("MPEG audio layer I/II/III packetizer") );
127 set_capability( "packetizer", 10 );
128 set_callbacks( OpenPacketizer, CloseDecoder );
131 /*****************************************************************************
132 * OpenDecoder: probe the decoder and return score
133 *****************************************************************************/
134 static int OpenDecoder( vlc_object_t *p_this )
136 decoder_t *p_dec = (decoder_t*)p_this;
137 decoder_sys_t *p_sys;
139 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
144 /* HACK: Don't use this codec if we don't have an mpga audio filter */
145 if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
146 !module_Exists( p_this, "mpgatofixed32" ) )
151 /* Allocate the memory needed to store the decoder's structure */
152 if( ( p_dec->p_sys = p_sys =
153 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
155 msg_Err( p_dec, "out of memory" );
160 p_sys->b_packetizer = VLC_FALSE;
161 p_sys->i_state = STATE_NOSYNC;
162 aout_DateSet( &p_sys->end_date, 0 );
163 p_sys->bytestream = block_BytestreamInit();
164 p_sys->b_discontinuity = VLC_FALSE;
165 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
167 /* Set output properties */
168 p_dec->fmt_out.i_cat = AUDIO_ES;
169 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
170 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
173 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
175 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
178 /* Start with the minimum size for a free bitrate frame */
179 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
184 static int OpenPacketizer( vlc_object_t *p_this )
186 decoder_t *p_dec = (decoder_t*)p_this;
188 int i_ret = OpenDecoder( p_this );
190 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
195 /****************************************************************************
196 * DecodeBlock: the whole thing
197 ****************************************************************************
198 * This function is called just after the thread is launched.
199 ****************************************************************************/
200 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
202 decoder_sys_t *p_sys = p_dec->p_sys;
203 uint8_t p_header[MAD_BUFFER_GUARD];
208 if( !pp_block || !*pp_block ) return NULL;
210 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
212 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
214 p_sys->i_state = STATE_NOSYNC;
215 block_BytestreamFlush( &p_sys->bytestream );
217 // aout_DateSet( &p_sys->end_date, 0 );
218 block_Release( *pp_block );
219 p_sys->b_discontinuity = VLC_TRUE;
223 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
225 /* We've just started the stream, wait for the first PTS. */
226 msg_Dbg( p_dec, "waiting for PTS" );
227 block_Release( *pp_block );
231 if( (*pp_block)->i_rate > 0 )
232 p_sys->i_input_rate = (*pp_block)->i_rate;
234 block_BytestreamPush( &p_sys->bytestream, *pp_block );
238 switch( p_sys->i_state )
242 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
245 /* Look for sync word - should be 0xffe */
246 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
248 p_sys->i_state = STATE_SYNC;
251 block_SkipByte( &p_sys->bytestream );
253 if( p_sys->i_state != STATE_SYNC )
255 block_BytestreamFlush( &p_sys->bytestream );
262 /* New frame, set the Presentation Time Stamp */
263 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
264 if( p_sys->i_pts != 0 &&
265 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
267 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
269 p_sys->i_state = STATE_HEADER;
272 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
273 if( block_PeekBytes( &p_sys->bytestream, p_header,
274 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
280 /* Build frame header */
281 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
284 /* Check if frame is valid and get frame info */
285 p_sys->i_frame_size = SyncInfo( i_header,
287 &p_sys->i_channels_conf,
290 &p_sys->i_frame_length,
291 &p_sys->i_max_frame_size,
294 if( p_sys->i_frame_size == -1 )
296 msg_Dbg( p_dec, "emulated startcode" );
297 block_SkipByte( &p_sys->bytestream );
298 p_sys->i_state = STATE_NOSYNC;
299 p_sys->b_discontinuity = VLC_TRUE;
303 if( p_sys->i_bit_rate == 0 )
305 /* Free bitrate, but 99% emulated startcode :( */
306 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
308 msg_Dbg( p_dec, "free bitrate mode");
310 /* The -1 below is to account for the frame padding */
311 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
314 p_sys->i_state = STATE_NEXT_SYNC;
316 case STATE_NEXT_SYNC:
317 /* TODO: If p_block == NULL, flush the buffer without checking the
320 /* Check if next expected frame contains the sync word */
321 if( block_PeekOffsetBytes( &p_sys->bytestream,
322 p_sys->i_frame_size, p_header,
323 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
329 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
331 /* Startcode is fine, let's try the header as an extra check */
332 int i_next_frame_size;
333 unsigned int i_next_channels, i_next_channels_conf;
334 unsigned int i_next_rate, i_next_bit_rate;
335 unsigned int i_next_frame_length, i_next_max_frame_size;
336 unsigned int i_next_layer;
338 /* Build frame header */
339 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
342 i_next_frame_size = SyncInfo( i_header,
344 &i_next_channels_conf,
347 &i_next_frame_length,
348 &i_next_max_frame_size,
351 /* Free bitrate only */
352 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
354 if( (unsigned int)p_sys->i_frame_size >
355 p_sys->i_max_frame_size )
357 msg_Dbg( p_dec, "frame too big %d > %d "
358 "(emulated startcode ?)", p_sys->i_frame_size,
359 p_sys->i_max_frame_size );
360 block_SkipByte( &p_sys->bytestream );
361 p_sys->i_state = STATE_NOSYNC;
362 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
366 p_sys->i_frame_size++;
370 if( i_next_frame_size == -1 )
372 msg_Dbg( p_dec, "emulated startcode on next frame" );
373 block_SkipByte( &p_sys->bytestream );
374 p_sys->i_state = STATE_NOSYNC;
375 p_sys->b_discontinuity = VLC_TRUE;
379 /* Check info is in sync with previous one */
380 if( i_next_channels_conf != p_sys->i_channels_conf ||
381 i_next_rate != p_sys->i_rate ||
382 i_next_layer != p_sys->i_layer ||
383 i_next_frame_length != p_sys->i_frame_length )
385 /* Free bitrate only */
386 if( p_sys->i_bit_rate == 0 )
388 p_sys->i_frame_size++;
392 msg_Dbg( p_dec, "parameters changed unexpectedly "
393 "(emulated startcode ?)" );
394 block_SkipByte( &p_sys->bytestream );
395 p_sys->i_state = STATE_NOSYNC;
399 /* Free bitrate only */
400 if( p_sys->i_bit_rate == 0 )
402 if( i_next_bit_rate != 0 )
404 p_sys->i_frame_size++;
412 /* Free bitrate only */
413 if( p_sys->i_bit_rate == 0 )
415 if( (unsigned int)p_sys->i_frame_size >
416 p_sys->i_max_frame_size )
418 msg_Dbg( p_dec, "frame too big %d > %d "
419 "(emulated startcode ?)", p_sys->i_frame_size,
420 p_sys->i_max_frame_size );
421 block_SkipByte( &p_sys->bytestream );
422 p_sys->i_state = STATE_NOSYNC;
423 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
427 p_sys->i_frame_size++;
431 msg_Dbg( p_dec, "emulated startcode "
432 "(no startcode on following frame)" );
433 p_sys->i_state = STATE_NOSYNC;
434 block_SkipByte( &p_sys->bytestream );
438 p_sys->i_state = STATE_SEND_DATA;
442 /* Make sure we have enough data.
443 * (Not useful if we went through NEXT_SYNC) */
444 if( block_WaitBytes( &p_sys->bytestream,
445 p_sys->i_frame_size ) != VLC_SUCCESS )
450 p_sys->i_state = STATE_SEND_DATA;
452 case STATE_SEND_DATA:
453 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
455 //p_dec->b_error = VLC_TRUE;
459 /* Free bitrate only */
460 if( p_sys->i_bit_rate == 0 )
462 p_sys->i_free_frame_size = p_sys->i_frame_size;
465 /* Copy the whole frame into the buffer. When we reach this point
466 * we already know we have enough data available. */
467 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
469 /* Get beginning of next frame for libmad */
470 if( !p_sys->b_packetizer )
472 memcpy( p_buf + p_sys->i_frame_size,
473 p_header, MAD_BUFFER_GUARD );
476 p_sys->i_state = STATE_NOSYNC;
478 /* Make sure we don't reuse the same pts twice */
479 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
480 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
482 /* So p_block doesn't get re-added several times */
483 *pp_block = block_BytestreamPop( &p_sys->bytestream );
492 /*****************************************************************************
494 *****************************************************************************/
495 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
497 decoder_sys_t *p_sys = p_dec->p_sys;
500 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
502 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
503 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
505 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
506 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
509 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
510 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
511 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
512 p_dec->fmt_out.audio.i_bytes_per_frame =
513 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
515 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
516 p_dec->fmt_out.audio.i_physical_channels =
517 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
519 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
521 if( p_sys->b_packetizer )
523 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
524 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
525 *pp_out_buffer = p_sout_buffer;
529 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
530 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
531 *pp_out_buffer = p_aout_buffer;
537 /*****************************************************************************
539 *****************************************************************************/
540 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
542 decoder_sys_t *p_sys = p_dec->p_sys;
543 aout_buffer_t *p_buf;
545 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
546 if( p_buf == NULL ) return NULL;
548 p_buf->start_date = aout_DateGet( &p_sys->end_date );
550 aout_DateIncrement( &p_sys->end_date,
551 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
552 p_buf->b_discontinuity = p_sys->b_discontinuity;
553 p_sys->b_discontinuity = VLC_FALSE;
555 /* Hack for libmad filter */
556 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
561 /*****************************************************************************
563 *****************************************************************************/
564 static block_t *GetSoutBuffer( decoder_t *p_dec )
566 decoder_sys_t *p_sys = p_dec->p_sys;
569 p_block = block_New( p_dec, p_sys->i_frame_size );
570 if( p_block == NULL ) return NULL;
572 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
575 aout_DateIncrement( &p_sys->end_date,
576 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
582 /*****************************************************************************
583 * CloseDecoder: clean up the decoder
584 *****************************************************************************/
585 static void CloseDecoder( vlc_object_t *p_this )
587 decoder_t *p_dec = (decoder_t *)p_this;
588 decoder_sys_t *p_sys = p_dec->p_sys;
590 block_BytestreamRelease( &p_sys->bytestream );
595 /*****************************************************************************
596 * SyncInfo: parse MPEG audio sync info
597 *****************************************************************************/
598 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
599 unsigned int * pi_channels_conf,
600 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
601 unsigned int * pi_frame_length,
602 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
604 static const int ppi_bitrate[2][3][16] =
608 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
611 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
614 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
620 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
623 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
626 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
631 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
633 { 44100, 48000, 32000, 0 },
634 { 22050, 24000, 16000, 0 }
637 int i_version, i_mode, i_emphasis;
638 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
639 int i_frame_size = 0;
640 int i_bitrate_index, i_samplerate_index;
643 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
644 i_version = 1 - ((i_header & 0x80000) >> 19);
645 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
646 b_crc = !((i_header >> 16) & 0x01);
647 i_bitrate_index = (i_header & 0xf000) >> 12;
648 i_samplerate_index = (i_header & 0xc00) >> 10;
649 b_padding = (i_header & 0x200) >> 9;
651 i_mode = (i_header & 0xc0) >> 6;
652 /* Modeext, copyright & original */
653 i_emphasis = i_header & 0x3;
655 if( *pi_layer != 4 &&
656 i_bitrate_index < 0x0f &&
657 i_samplerate_index != 0x03 &&
663 case 1: /* joint stereo */
665 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
667 case 2: /* dual-mono */
669 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
670 | AOUT_CHAN_DUALMONO;
674 *pi_channels_conf = AOUT_CHAN_CENTER;
677 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
678 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
679 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
683 *pi_sample_rate >>= 1;
689 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
691 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
692 *pi_sample_rate + 1 ) * 4;
693 *pi_frame_length = 384;
697 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
698 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
699 *pi_frame_length = 1152;
703 i_frame_size = ( i_version ? 72000 : 144000 ) *
704 *pi_bit_rate / *pi_sample_rate + b_padding;
705 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
706 i_max_bit_rate / *pi_sample_rate + 1;
707 *pi_frame_length = i_version ? 576 : 1152;
714 /* Free bitrate mode can support higher bitrates */
715 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;