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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
39 #include <vlc_block_helper.h>
41 /*****************************************************************************
42 * decoder_sys_t : decoder descriptor
43 *****************************************************************************/
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;
82 /* This isn't the place to put mad-specific stuff. However, it makes the
83 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
84 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
85 * bigger buffer. This has no implication on other plug-ins, and we only
86 * lose 8 bytes per frame. --Meuuh */
87 #define MAD_BUFFER_GUARD 8
88 #define MPGA_HEADER_SIZE 4
90 /****************************************************************************
92 ****************************************************************************/
93 static int OpenDecoder ( vlc_object_t * );
94 static int OpenPacketizer( vlc_object_t * );
95 static void CloseDecoder ( vlc_object_t * );
96 static void *DecodeBlock ( decoder_t *, block_t ** );
98 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
99 static aout_buffer_t *GetAoutBuffer( decoder_t * );
100 static block_t *GetSoutBuffer( decoder_t * );
102 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
103 unsigned int * pi_channels_conf,
104 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
105 unsigned int * pi_frame_length,
106 unsigned int * pi_max_frame_size,
107 unsigned int * pi_layer );
109 /*****************************************************************************
111 *****************************************************************************/
113 set_description( N_("MPEG audio layer I/II/III decoder") )
114 set_category( CAT_INPUT )
115 set_subcategory( SUBCAT_INPUT_ACODEC )
116 #if defined(UNDER_CE)
117 set_capability( "decoder", 5 )
119 set_capability( "decoder", 100 )
121 set_callbacks( OpenDecoder, CloseDecoder )
124 set_description( N_("MPEG audio layer I/II/III packetizer") )
125 set_capability( "packetizer", 10 )
126 set_callbacks( OpenPacketizer, CloseDecoder )
129 /*****************************************************************************
130 * OpenDecoder: probe the decoder and return score
131 *****************************************************************************/
132 static int OpenDecoder( vlc_object_t *p_this )
134 decoder_t *p_dec = (decoder_t*)p_this;
135 decoder_sys_t *p_sys;
137 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
142 /* HACK: Don't use this codec if we don't have an mpga audio filter */
143 if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
144 !module_exists( "mpgatofixed32" ) )
149 /* Allocate the memory needed to store the decoder's structure */
150 if( ( p_dec->p_sys = p_sys =
151 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
155 p_sys->b_packetizer = false;
156 p_sys->i_state = STATE_NOSYNC;
157 aout_DateSet( &p_sys->end_date, 0 );
158 p_sys->bytestream = block_BytestreamInit();
159 p_sys->b_discontinuity = false;
161 /* Set output properties */
162 p_dec->fmt_out.i_cat = AUDIO_ES;
163 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
164 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
167 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
169 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
172 /* Start with the minimum size for a free bitrate frame */
173 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
178 static int OpenPacketizer( vlc_object_t *p_this )
180 decoder_t *p_dec = (decoder_t*)p_this;
182 int i_ret = OpenDecoder( p_this );
184 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
189 /****************************************************************************
190 * DecodeBlock: the whole thing
191 ****************************************************************************
192 * This function is called just after the thread is launched.
193 ****************************************************************************/
194 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
196 decoder_sys_t *p_sys = p_dec->p_sys;
197 uint8_t p_header[MAD_BUFFER_GUARD];
202 if( !pp_block || !*pp_block ) return NULL;
204 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
206 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
208 p_sys->i_state = STATE_NOSYNC;
209 block_BytestreamFlush( &p_sys->bytestream );
211 // aout_DateSet( &p_sys->end_date, 0 );
212 block_Release( *pp_block );
213 p_sys->b_discontinuity = true;
217 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
219 /* We've just started the stream, wait for the first PTS. */
220 msg_Dbg( p_dec, "waiting for PTS" );
221 block_Release( *pp_block );
225 block_BytestreamPush( &p_sys->bytestream, *pp_block );
229 switch( p_sys->i_state )
233 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
236 /* Look for sync word - should be 0xffe */
237 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
239 p_sys->i_state = STATE_SYNC;
242 block_SkipByte( &p_sys->bytestream );
244 if( p_sys->i_state != STATE_SYNC )
246 block_BytestreamFlush( &p_sys->bytestream );
253 /* New frame, set the Presentation Time Stamp */
254 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
255 if( p_sys->i_pts != 0 &&
256 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
258 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
260 p_sys->i_state = STATE_HEADER;
263 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
264 if( block_PeekBytes( &p_sys->bytestream, p_header,
265 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
271 /* Build frame header */
272 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
275 /* Check if frame is valid and get frame info */
276 p_sys->i_frame_size = SyncInfo( i_header,
278 &p_sys->i_channels_conf,
281 &p_sys->i_frame_length,
282 &p_sys->i_max_frame_size,
285 if( p_sys->i_frame_size == -1 )
287 msg_Dbg( p_dec, "emulated startcode" );
288 block_SkipByte( &p_sys->bytestream );
289 p_sys->i_state = STATE_NOSYNC;
290 p_sys->b_discontinuity = true;
294 if( p_sys->i_bit_rate == 0 )
296 /* Free bitrate, but 99% emulated startcode :( */
297 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
299 msg_Dbg( p_dec, "free bitrate mode");
301 /* The -1 below is to account for the frame padding */
302 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
305 p_sys->i_state = STATE_NEXT_SYNC;
307 case STATE_NEXT_SYNC:
308 /* TODO: If p_block == NULL, flush the buffer without checking the
311 /* Check if next expected frame contains the sync word */
312 if( block_PeekOffsetBytes( &p_sys->bytestream,
313 p_sys->i_frame_size, p_header,
314 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
320 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
322 /* Startcode is fine, let's try the header as an extra check */
323 int i_next_frame_size;
324 unsigned int i_next_channels, i_next_channels_conf;
325 unsigned int i_next_rate, i_next_bit_rate;
326 unsigned int i_next_frame_length, i_next_max_frame_size;
327 unsigned int i_next_layer;
329 /* Build frame header */
330 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
333 i_next_frame_size = SyncInfo( i_header,
335 &i_next_channels_conf,
338 &i_next_frame_length,
339 &i_next_max_frame_size,
342 /* Free bitrate only */
343 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
345 if( (unsigned int)p_sys->i_frame_size >
346 p_sys->i_max_frame_size )
348 msg_Dbg( p_dec, "frame too big %d > %d "
349 "(emulated startcode ?)", p_sys->i_frame_size,
350 p_sys->i_max_frame_size );
351 block_SkipByte( &p_sys->bytestream );
352 p_sys->i_state = STATE_NOSYNC;
353 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
357 p_sys->i_frame_size++;
361 if( i_next_frame_size == -1 )
363 msg_Dbg( p_dec, "emulated startcode on next frame" );
364 block_SkipByte( &p_sys->bytestream );
365 p_sys->i_state = STATE_NOSYNC;
366 p_sys->b_discontinuity = true;
370 /* Check info is in sync with previous one */
371 if( i_next_channels_conf != p_sys->i_channels_conf ||
372 i_next_rate != p_sys->i_rate ||
373 i_next_layer != p_sys->i_layer ||
374 i_next_frame_length != p_sys->i_frame_length )
376 /* Free bitrate only */
377 if( p_sys->i_bit_rate == 0 )
379 p_sys->i_frame_size++;
383 msg_Dbg( p_dec, "parameters changed unexpectedly "
384 "(emulated startcode ?)" );
385 block_SkipByte( &p_sys->bytestream );
386 p_sys->i_state = STATE_NOSYNC;
390 /* Free bitrate only */
391 if( p_sys->i_bit_rate == 0 )
393 if( i_next_bit_rate != 0 )
395 p_sys->i_frame_size++;
403 /* Free bitrate only */
404 if( p_sys->i_bit_rate == 0 )
406 if( (unsigned int)p_sys->i_frame_size >
407 p_sys->i_max_frame_size )
409 msg_Dbg( p_dec, "frame too big %d > %d "
410 "(emulated startcode ?)", p_sys->i_frame_size,
411 p_sys->i_max_frame_size );
412 block_SkipByte( &p_sys->bytestream );
413 p_sys->i_state = STATE_NOSYNC;
414 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
418 p_sys->i_frame_size++;
422 msg_Dbg( p_dec, "emulated startcode "
423 "(no startcode on following frame)" );
424 p_sys->i_state = STATE_NOSYNC;
425 block_SkipByte( &p_sys->bytestream );
429 p_sys->i_state = STATE_SEND_DATA;
433 /* Make sure we have enough data.
434 * (Not useful if we went through NEXT_SYNC) */
435 if( block_WaitBytes( &p_sys->bytestream,
436 p_sys->i_frame_size ) != VLC_SUCCESS )
441 p_sys->i_state = STATE_SEND_DATA;
443 case STATE_SEND_DATA:
444 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
446 //p_dec->b_error = true;
450 /* Free bitrate only */
451 if( p_sys->i_bit_rate == 0 )
453 p_sys->i_free_frame_size = p_sys->i_frame_size;
456 /* Copy the whole frame into the buffer. When we reach this point
457 * we already know we have enough data available. */
458 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
460 /* Get beginning of next frame for libmad */
461 if( !p_sys->b_packetizer )
463 memcpy( p_buf + p_sys->i_frame_size,
464 p_header, MAD_BUFFER_GUARD );
467 p_sys->i_state = STATE_NOSYNC;
469 /* Make sure we don't reuse the same pts twice */
470 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
471 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
473 /* So p_block doesn't get re-added several times */
474 *pp_block = block_BytestreamPop( &p_sys->bytestream );
483 /*****************************************************************************
485 *****************************************************************************/
486 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
488 decoder_sys_t *p_sys = p_dec->p_sys;
491 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
493 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
494 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
496 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
497 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
500 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
501 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
502 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
503 p_dec->fmt_out.audio.i_bytes_per_frame =
504 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
506 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
507 p_dec->fmt_out.audio.i_physical_channels =
508 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
510 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
512 if( p_sys->b_packetizer )
514 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
515 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
516 *pp_out_buffer = p_sout_buffer;
520 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
521 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
522 *pp_out_buffer = p_aout_buffer;
528 /*****************************************************************************
530 *****************************************************************************/
531 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
533 decoder_sys_t *p_sys = p_dec->p_sys;
534 aout_buffer_t *p_buf;
536 p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
537 if( p_buf == NULL ) return NULL;
539 p_buf->start_date = aout_DateGet( &p_sys->end_date );
541 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
542 p_buf->b_discontinuity = p_sys->b_discontinuity;
543 p_sys->b_discontinuity = false;
545 /* Hack for libmad filter */
546 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
551 /*****************************************************************************
553 *****************************************************************************/
554 static block_t *GetSoutBuffer( decoder_t *p_dec )
556 decoder_sys_t *p_sys = p_dec->p_sys;
559 p_block = block_New( p_dec, p_sys->i_frame_size );
560 if( p_block == NULL ) return NULL;
562 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
565 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
570 /*****************************************************************************
571 * CloseDecoder: clean up the decoder
572 *****************************************************************************/
573 static void CloseDecoder( vlc_object_t *p_this )
575 decoder_t *p_dec = (decoder_t *)p_this;
576 decoder_sys_t *p_sys = p_dec->p_sys;
578 block_BytestreamRelease( &p_sys->bytestream );
583 /*****************************************************************************
584 * SyncInfo: parse MPEG audio sync info
585 *****************************************************************************/
586 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
587 unsigned int * pi_channels_conf,
588 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
589 unsigned int * pi_frame_length,
590 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
592 static const int ppi_bitrate[2][3][16] =
596 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
599 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
602 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
608 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
611 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
614 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
619 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
621 { 44100, 48000, 32000, 0 },
622 { 22050, 24000, 16000, 0 }
625 int i_version, i_mode, i_emphasis;
626 bool b_padding, b_mpeg_2_5, b_crc;
627 int i_frame_size = 0;
628 int i_bitrate_index, i_samplerate_index;
631 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
632 i_version = 1 - ((i_header & 0x80000) >> 19);
633 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
634 b_crc = !((i_header >> 16) & 0x01);
635 i_bitrate_index = (i_header & 0xf000) >> 12;
636 i_samplerate_index = (i_header & 0xc00) >> 10;
637 b_padding = (i_header & 0x200) >> 9;
639 i_mode = (i_header & 0xc0) >> 6;
640 /* Modeext, copyright & original */
641 i_emphasis = i_header & 0x3;
643 if( *pi_layer != 4 &&
644 i_bitrate_index < 0x0f &&
645 i_samplerate_index != 0x03 &&
651 case 1: /* joint stereo */
653 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
655 case 2: /* dual-mono */
657 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
658 | AOUT_CHAN_DUALMONO;
662 *pi_channels_conf = AOUT_CHAN_CENTER;
665 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
666 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
667 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
671 *pi_sample_rate >>= 1;
677 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
679 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
680 *pi_sample_rate + 1 ) * 4;
681 *pi_frame_length = 384;
685 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
686 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
687 *pi_frame_length = 1152;
691 i_frame_size = ( i_version ? 72000 : 144000 ) *
692 *pi_bit_rate / *pi_sample_rate + b_padding;
693 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
694 i_max_bit_rate / *pi_sample_rate + 1;
695 *pi_frame_length = i_version ? 576 : 1152;
702 /* Free bitrate mode can support higher bitrates */
703 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;