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>
40 #include <vlc_block_helper.h>
42 /*****************************************************************************
43 * decoder_sys_t : decoder descriptor
44 *****************************************************************************/
55 block_bytestream_t bytestream;
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;
83 /* This isn't the place to put mad-specific stuff. However, it makes the
84 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
85 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
86 * bigger buffer. This has no implication on other plug-ins, and we only
87 * lose 8 bytes per frame. --Meuuh */
88 #define MAD_BUFFER_GUARD 8
89 #define MPGA_HEADER_SIZE 4
91 /****************************************************************************
93 ****************************************************************************/
94 static int OpenDecoder ( vlc_object_t * );
95 static int OpenPacketizer( vlc_object_t * );
96 static void CloseDecoder ( vlc_object_t * );
97 static void *DecodeBlock ( decoder_t *, block_t ** );
99 static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
100 static aout_buffer_t *GetAoutBuffer( decoder_t * );
101 static block_t *GetSoutBuffer( decoder_t * );
103 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
104 unsigned int * pi_channels_conf,
105 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
106 unsigned int * pi_frame_length,
107 unsigned int * pi_max_frame_size,
108 unsigned int * pi_layer );
110 /*****************************************************************************
112 *****************************************************************************/
114 set_description( N_("MPEG audio layer I/II/III decoder") )
115 set_category( CAT_INPUT )
116 set_subcategory( SUBCAT_INPUT_ACODEC )
117 #if defined(UNDER_CE)
118 set_capability( "decoder", 5 )
120 set_capability( "decoder", 100 )
122 set_callbacks( OpenDecoder, CloseDecoder )
125 set_description( N_("MPEG audio layer I/II/III packetizer") )
126 set_capability( "packetizer", 10 )
127 set_callbacks( OpenPacketizer, CloseDecoder )
130 /*****************************************************************************
131 * Open: probe the decoder and return score
132 *****************************************************************************/
133 static int Open( vlc_object_t *p_this )
135 decoder_t *p_dec = (decoder_t*)p_this;
136 decoder_sys_t *p_sys;
138 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA )
143 /* Allocate the memory needed to store the decoder's structure */
144 if( ( p_dec->p_sys = p_sys =
145 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
149 p_sys->b_packetizer = false;
150 p_sys->i_state = STATE_NOSYNC;
151 date_Set( &p_sys->end_date, 0 );
152 p_sys->bytestream = block_BytestreamInit();
153 p_sys->i_pts = VLC_TS_INVALID;
154 p_sys->b_discontinuity = false;
156 /* Set output properties */
157 p_dec->fmt_out.i_cat = AUDIO_ES;
158 p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
159 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
162 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
164 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
167 /* Start with the minimum size for a free bitrate frame */
168 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
173 static int OpenDecoder( vlc_object_t *p_this )
175 /* HACK: Don't use this codec if we don't have an mpga audio filter */
176 if( !module_exists( "mpgatofixed32" ) )
179 return Open( p_this );
182 static int OpenPacketizer( vlc_object_t *p_this )
184 decoder_t *p_dec = (decoder_t*)p_this;
186 int i_ret = Open( p_this );
188 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
193 /****************************************************************************
194 * DecodeBlock: the whole thing
195 ****************************************************************************
196 * This function is called just after the thread is launched.
197 ****************************************************************************/
198 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
200 decoder_sys_t *p_sys = p_dec->p_sys;
201 uint8_t p_header[MAD_BUFFER_GUARD];
204 block_t *p_out_buffer;
206 if( !pp_block || !*pp_block ) return NULL;
208 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
210 if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
212 p_sys->i_state = STATE_NOSYNC;
213 block_BytestreamEmpty( &p_sys->bytestream );
215 date_Set( &p_sys->end_date, 0 );
216 block_Release( *pp_block );
217 p_sys->b_discontinuity = true;
221 if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
223 /* We've just started the stream, wait for the first PTS. */
224 msg_Dbg( p_dec, "waiting for PTS" );
225 block_Release( *pp_block );
229 block_BytestreamPush( &p_sys->bytestream, *pp_block );
233 switch( p_sys->i_state )
237 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
240 /* Look for sync word - should be 0xffe */
241 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
243 p_sys->i_state = STATE_SYNC;
246 block_SkipByte( &p_sys->bytestream );
248 if( p_sys->i_state != STATE_SYNC )
250 block_BytestreamFlush( &p_sys->bytestream );
257 /* New frame, set the Presentation Time Stamp */
258 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
259 if( p_sys->i_pts > VLC_TS_INVALID &&
260 p_sys->i_pts != date_Get( &p_sys->end_date ) )
262 date_Set( &p_sys->end_date, p_sys->i_pts );
264 p_sys->i_state = STATE_HEADER;
267 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
268 if( block_PeekBytes( &p_sys->bytestream, p_header,
269 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
275 /* Build frame header */
276 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
279 /* Check if frame is valid and get frame info */
280 p_sys->i_frame_size = SyncInfo( i_header,
282 &p_sys->i_channels_conf,
285 &p_sys->i_frame_length,
286 &p_sys->i_max_frame_size,
289 p_dec->fmt_in.i_profile = p_sys->i_layer;
291 if( p_sys->i_frame_size == -1 )
293 msg_Dbg( p_dec, "emulated startcode" );
294 block_SkipByte( &p_sys->bytestream );
295 p_sys->i_state = STATE_NOSYNC;
296 p_sys->b_discontinuity = true;
300 if( p_sys->i_bit_rate == 0 )
302 /* Free bitrate, but 99% emulated startcode :( */
303 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
305 msg_Dbg( p_dec, "free bitrate mode");
307 /* The -1 below is to account for the frame padding */
308 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
311 p_sys->i_state = STATE_NEXT_SYNC;
313 case STATE_NEXT_SYNC:
314 /* TODO: If p_block == NULL, flush the buffer without checking the
317 /* Check if next expected frame contains the sync word */
318 if( block_PeekOffsetBytes( &p_sys->bytestream,
319 p_sys->i_frame_size, p_header,
320 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
326 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
328 /* Startcode is fine, let's try the header as an extra check */
329 int i_next_frame_size;
330 unsigned int i_next_channels, i_next_channels_conf;
331 unsigned int i_next_rate, i_next_bit_rate;
332 unsigned int i_next_frame_length, i_next_max_frame_size;
333 unsigned int i_next_layer;
335 /* Build frame header */
336 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
339 i_next_frame_size = SyncInfo( i_header,
341 &i_next_channels_conf,
344 &i_next_frame_length,
345 &i_next_max_frame_size,
348 /* Free bitrate only */
349 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
351 if( (unsigned int)p_sys->i_frame_size >
352 p_sys->i_max_frame_size )
354 msg_Dbg( p_dec, "frame too big %d > %d "
355 "(emulated startcode ?)", p_sys->i_frame_size,
356 p_sys->i_max_frame_size );
357 block_SkipByte( &p_sys->bytestream );
358 p_sys->i_state = STATE_NOSYNC;
359 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
363 p_sys->i_frame_size++;
367 if( i_next_frame_size == -1 )
369 msg_Dbg( p_dec, "emulated startcode on next frame" );
370 block_SkipByte( &p_sys->bytestream );
371 p_sys->i_state = STATE_NOSYNC;
372 p_sys->b_discontinuity = true;
376 /* Check info is in sync with previous one */
377 if( i_next_channels_conf != p_sys->i_channels_conf ||
378 i_next_rate != p_sys->i_rate ||
379 i_next_layer != p_sys->i_layer ||
380 i_next_frame_length != p_sys->i_frame_length )
382 /* Free bitrate only */
383 if( p_sys->i_bit_rate == 0 )
385 p_sys->i_frame_size++;
389 msg_Dbg( p_dec, "parameters changed unexpectedly "
390 "(emulated startcode ?)" );
391 block_SkipByte( &p_sys->bytestream );
392 p_sys->i_state = STATE_NOSYNC;
396 /* Free bitrate only */
397 if( p_sys->i_bit_rate == 0 )
399 if( i_next_bit_rate != 0 )
401 p_sys->i_frame_size++;
409 /* Free bitrate only */
410 if( p_sys->i_bit_rate == 0 )
412 if( (unsigned int)p_sys->i_frame_size >
413 p_sys->i_max_frame_size )
415 msg_Dbg( p_dec, "frame too big %d > %d "
416 "(emulated startcode ?)", p_sys->i_frame_size,
417 p_sys->i_max_frame_size );
418 block_SkipByte( &p_sys->bytestream );
419 p_sys->i_state = STATE_NOSYNC;
420 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
424 p_sys->i_frame_size++;
428 msg_Dbg( p_dec, "emulated startcode "
429 "(no startcode on following frame)" );
430 p_sys->i_state = STATE_NOSYNC;
431 block_SkipByte( &p_sys->bytestream );
435 p_sys->i_state = STATE_SEND_DATA;
439 /* Make sure we have enough data.
440 * (Not useful if we went through NEXT_SYNC) */
441 if( block_WaitBytes( &p_sys->bytestream,
442 p_sys->i_frame_size ) != VLC_SUCCESS )
447 p_sys->i_state = STATE_SEND_DATA;
449 case STATE_SEND_DATA:
450 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
452 //p_dec->b_error = true;
456 /* Free bitrate only */
457 if( p_sys->i_bit_rate == 0 )
459 p_sys->i_free_frame_size = p_sys->i_frame_size;
462 /* Copy the whole frame into the buffer. When we reach this point
463 * we already know we have enough data available. */
464 block_GetBytes( &p_sys->bytestream,
465 p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) );
467 /* Get beginning of next frame for libmad */
468 if( !p_sys->b_packetizer )
470 assert( p_out_buffer->i_buffer >= (unsigned)p_sys->i_frame_size + MAD_BUFFER_GUARD );
471 memcpy( p_buf + p_sys->i_frame_size,
472 p_header, MAD_BUFFER_GUARD );
475 p_sys->i_state = STATE_NOSYNC;
477 /* Make sure we don't reuse the same pts twice */
478 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
479 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
481 /* So p_block doesn't get re-added several times */
482 *pp_block = block_BytestreamPop( &p_sys->bytestream );
491 /*****************************************************************************
493 *****************************************************************************/
494 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
496 decoder_sys_t *p_sys = p_dec->p_sys;
499 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
501 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
502 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
504 date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
505 date_Set( &p_sys->end_date, p_sys->i_pts );
508 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
509 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
510 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
511 p_dec->fmt_out.audio.i_bytes_per_frame =
512 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
514 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
515 p_dec->fmt_out.audio.i_physical_channels =
516 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
518 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
520 if( p_sys->b_packetizer )
522 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
523 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
524 *pp_out_buffer = p_sout_buffer;
528 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
529 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
530 *pp_out_buffer = p_aout_buffer;
536 /*****************************************************************************
538 *****************************************************************************/
539 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
541 decoder_sys_t *p_sys = p_dec->p_sys;
542 aout_buffer_t *p_buf;
544 p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
545 if( p_buf == NULL ) return NULL;
547 p_buf->i_pts = date_Get( &p_sys->end_date );
548 p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
550 if( p_sys->b_discontinuity )
551 p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
552 p_sys->b_discontinuity = false;
554 /* Hack for libmad filter */
555 p_buf = block_Realloc( p_buf, 0, p_sys->i_frame_size + MAD_BUFFER_GUARD );
560 /*****************************************************************************
562 *****************************************************************************/
563 static block_t *GetSoutBuffer( decoder_t *p_dec )
565 decoder_sys_t *p_sys = p_dec->p_sys;
568 p_block = block_New( p_dec, p_sys->i_frame_size );
569 if( p_block == NULL ) return NULL;
571 p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
574 date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
579 /*****************************************************************************
580 * CloseDecoder: clean up the decoder
581 *****************************************************************************/
582 static void CloseDecoder( vlc_object_t *p_this )
584 decoder_t *p_dec = (decoder_t *)p_this;
585 decoder_sys_t *p_sys = p_dec->p_sys;
587 block_BytestreamRelease( &p_sys->bytestream );
592 /*****************************************************************************
593 * SyncInfo: parse MPEG audio sync info
594 *****************************************************************************/
595 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
596 unsigned int * pi_channels_conf,
597 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
598 unsigned int * pi_frame_length,
599 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
601 static const int ppi_bitrate[2][3][16] =
605 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
608 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
611 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
617 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
620 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
623 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
628 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
630 { 44100, 48000, 32000, 0 },
631 { 22050, 24000, 16000, 0 }
634 int i_version, i_mode, i_emphasis;
635 bool b_padding, b_mpeg_2_5, b_crc;
636 int i_frame_size = 0;
637 int i_bitrate_index, i_samplerate_index;
640 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
641 i_version = 1 - ((i_header & 0x80000) >> 19);
642 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
643 b_crc = !((i_header >> 16) & 0x01);
644 i_bitrate_index = (i_header & 0xf000) >> 12;
645 i_samplerate_index = (i_header & 0xc00) >> 10;
646 b_padding = (i_header & 0x200) >> 9;
648 i_mode = (i_header & 0xc0) >> 6;
649 /* Modeext, copyright & original */
650 i_emphasis = i_header & 0x3;
652 if( *pi_layer != 4 &&
653 i_bitrate_index < 0x0f &&
654 i_samplerate_index != 0x03 &&
660 case 1: /* joint stereo */
662 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
664 case 2: /* dual-mono */
666 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
667 | AOUT_CHAN_DUALMONO;
671 *pi_channels_conf = AOUT_CHAN_CENTER;
674 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
675 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
676 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
680 *pi_sample_rate >>= 1;
686 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
688 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
689 *pi_sample_rate + 1 ) * 4;
690 *pi_frame_length = 384;
694 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
695 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
696 *pi_frame_length = 1152;
700 i_frame_size = ( i_version ? 72000 : 144000 ) *
701 *pi_bit_rate / *pi_sample_rate + b_padding;
702 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
703 i_max_bit_rate / *pi_sample_rate + 1;
704 *pi_frame_length = i_version ? 576 : 1152;
711 /* Free bitrate mode can support higher bitrates */
712 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;