1 /*****************************************************************************
2 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
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@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
31 #include <vlc/decoder.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;
75 /* This isn't the place to put mad-specific stuff. However, it makes the
76 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
77 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
78 * bigger buffer. This has no implication on other plug-ins, and we only
79 * lose 8 bytes per frame. --Meuuh */
80 #define MAD_BUFFER_GUARD 8
81 #define MPGA_HEADER_SIZE 4
83 /****************************************************************************
85 ****************************************************************************/
86 static int OpenDecoder ( vlc_object_t * );
87 static int OpenPacketizer( vlc_object_t * );
88 static void CloseDecoder ( vlc_object_t * );
89 static void *DecodeBlock ( decoder_t *, block_t ** );
91 static uint8_t *GetOutBuffer ( decoder_t *, void ** );
92 static aout_buffer_t *GetAoutBuffer( decoder_t * );
93 static block_t *GetSoutBuffer( decoder_t * );
95 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
96 unsigned int * pi_channels_conf,
97 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
98 unsigned int * pi_frame_length,
99 unsigned int * pi_max_frame_size,
100 unsigned int * pi_layer );
102 /*****************************************************************************
104 *****************************************************************************/
106 set_description( _("MPEG audio layer I/II/III parser") );
107 set_category( CAT_INPUT );
108 set_subcategory( SUBCAT_INPUT_ACODEC );
109 #if defined(SYS_DARWIN) || defined(UNDER_CE)
110 set_capability( "decoder", 5 );
112 set_capability( "decoder", 100 );
114 set_callbacks( OpenDecoder, CloseDecoder );
117 set_description( _("MPEG audio layer I/II/III packetizer") );
118 set_capability( "packetizer", 10 );
119 set_callbacks( OpenPacketizer, CloseDecoder );
122 /*****************************************************************************
123 * OpenDecoder: probe the decoder and return score
124 *****************************************************************************/
125 static int OpenDecoder( vlc_object_t *p_this )
127 decoder_t *p_dec = (decoder_t*)p_this;
128 decoder_sys_t *p_sys;
130 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
135 /* Allocate the memory needed to store the decoder's structure */
136 if( ( p_dec->p_sys = p_sys =
137 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
139 msg_Err( p_dec, "out of memory" );
144 p_sys->b_packetizer = VLC_FALSE;
145 p_sys->i_state = STATE_NOSYNC;
146 aout_DateSet( &p_sys->end_date, 0 );
147 p_sys->bytestream = block_BytestreamInit( p_dec );
149 /* Set output properties */
150 p_dec->fmt_out.i_cat = AUDIO_ES;
151 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
152 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
155 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
157 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
160 /* Start with the minimum size for a free bitrate frame */
161 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
166 static int OpenPacketizer( vlc_object_t *p_this )
168 decoder_t *p_dec = (decoder_t*)p_this;
170 int i_ret = OpenDecoder( p_this );
172 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
177 /****************************************************************************
178 * DecodeBlock: the whole thing
179 ****************************************************************************
180 * This function is called just after the thread is launched.
181 ****************************************************************************/
182 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
184 decoder_sys_t *p_sys = p_dec->p_sys;
185 uint8_t p_header[MAD_BUFFER_GUARD];
190 if( !pp_block || !*pp_block ) return NULL;
192 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
194 /* We've just started the stream, wait for the first PTS. */
195 block_Release( *pp_block );
199 if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
201 p_sys->i_state = STATE_NOSYNC;
204 block_BytestreamPush( &p_sys->bytestream, *pp_block );
208 switch( p_sys->i_state )
212 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
215 /* Look for sync word - should be 0xffe */
216 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
218 p_sys->i_state = STATE_SYNC;
221 block_SkipByte( &p_sys->bytestream );
223 if( p_sys->i_state != STATE_SYNC )
225 block_BytestreamFlush( &p_sys->bytestream );
232 /* New frame, set the Presentation Time Stamp */
233 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
234 if( p_sys->i_pts != 0 &&
235 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
237 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
239 p_sys->i_state = STATE_HEADER;
242 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
243 if( block_PeekBytes( &p_sys->bytestream, p_header,
244 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
250 /* Build frame header */
251 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
254 /* Check if frame is valid and get frame info */
255 p_sys->i_frame_size = SyncInfo( i_header,
257 &p_sys->i_channels_conf,
260 &p_sys->i_frame_length,
261 &p_sys->i_max_frame_size,
264 if( p_sys->i_frame_size == -1 )
266 msg_Dbg( p_dec, "emulated startcode" );
267 block_SkipByte( &p_sys->bytestream );
268 p_sys->i_state = STATE_NOSYNC;
272 if( p_sys->i_bit_rate == 0 )
274 /* Free birate, but 99% emulated startcode :( */
275 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
277 msg_Dbg( p_dec, "free bitrate mode");
279 p_sys->i_frame_size = p_sys->i_free_frame_size;
282 p_sys->i_state = STATE_NEXT_SYNC;
284 case STATE_NEXT_SYNC:
285 /* TODO: If p_block == NULL, flush the buffer without checking the
288 /* Check if next expected frame contains the sync word */
289 if( block_PeekOffsetBytes( &p_sys->bytestream,
290 p_sys->i_frame_size, p_header,
291 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
297 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
299 /* Startcode is fine, let's try the header as an extra check */
300 int i_next_frame_size;
301 unsigned int i_next_channels, i_next_channels_conf;
302 unsigned int i_next_rate, i_next_bit_rate;
303 unsigned int i_next_frame_length, i_next_max_frame_size;
304 unsigned int i_next_layer;
306 /* Build frame header */
307 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
310 i_next_frame_size = SyncInfo( i_header,
312 &i_next_channels_conf,
315 &i_next_frame_length,
316 &i_next_max_frame_size,
319 /* Free bitrate only */
320 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
322 if( (unsigned int)p_sys->i_frame_size >
323 p_sys->i_max_frame_size )
325 msg_Dbg( p_dec, "frame too big %d > %d "
326 "(emulated startcode ?)", p_sys->i_frame_size,
327 p_sys->i_max_frame_size );
328 block_SkipByte( &p_sys->bytestream );
329 p_sys->i_state = STATE_NOSYNC;
330 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
334 p_sys->i_frame_size++;
338 if( i_next_frame_size == -1 )
340 msg_Dbg( p_dec, "emulated startcode on next frame" );
341 block_SkipByte( &p_sys->bytestream );
342 p_sys->i_state = STATE_NOSYNC;
346 /* Check info is in sync with previous one */
347 if( i_next_channels_conf != p_sys->i_channels_conf ||
348 i_next_rate != p_sys->i_rate ||
349 i_next_layer != p_sys->i_layer ||
350 i_next_frame_length != p_sys->i_frame_length )
352 /* Free bitrate only */
353 if( p_sys->i_bit_rate == 0 )
355 p_sys->i_frame_size++;
359 msg_Dbg( p_dec, "parameters changed unexpectedly "
360 "(emulated startcode ?)" );
361 block_SkipByte( &p_sys->bytestream );
362 p_sys->i_state = STATE_NOSYNC;
366 /* Free bitrate only */
367 if( p_sys->i_bit_rate == 0 )
369 if( i_next_bit_rate != 0 )
371 p_sys->i_frame_size++;
379 /* Free bitrate only */
380 if( p_sys->i_bit_rate == 0 )
382 if( (unsigned int)p_sys->i_frame_size >
383 p_sys->i_max_frame_size )
385 msg_Dbg( p_dec, "frame too big %d > %d "
386 "(emulated startcode ?)", p_sys->i_frame_size,
387 p_sys->i_max_frame_size );
388 block_SkipByte( &p_sys->bytestream );
389 p_sys->i_state = STATE_NOSYNC;
390 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
394 p_sys->i_frame_size++;
398 msg_Dbg( p_dec, "emulated startcode "
399 "(no startcode on following frame)" );
400 p_sys->i_state = STATE_NOSYNC;
401 block_SkipByte( &p_sys->bytestream );
405 p_sys->i_state = STATE_SEND_DATA;
409 /* Make sure we have enough data.
410 * (Not useful if we went through NEXT_SYNC) */
411 if( block_WaitBytes( &p_sys->bytestream,
412 p_sys->i_frame_size ) != VLC_SUCCESS )
417 p_sys->i_state = STATE_SEND_DATA;
419 case STATE_SEND_DATA:
420 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
422 //p_dec->b_error = VLC_TRUE;
426 /* Free bitrate only */
427 if( p_sys->i_bit_rate == 0 )
429 p_sys->i_free_frame_size = p_sys->i_frame_size;
432 /* Copy the whole frame into the buffer. When we reach this point
433 * we already know we have enough data available. */
434 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
436 /* Get beginning of next frame for libmad */
437 if( !p_sys->b_packetizer )
439 memcpy( p_buf + p_sys->i_frame_size,
440 p_header, MAD_BUFFER_GUARD );
443 p_sys->i_state = STATE_NOSYNC;
445 /* Make sure we don't reuse the same pts twice */
446 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
447 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
449 /* So p_block doesn't get re-added several times */
450 *pp_block = block_BytestreamPop( &p_sys->bytestream );
459 /*****************************************************************************
461 *****************************************************************************/
462 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
464 decoder_sys_t *p_sys = p_dec->p_sys;
467 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
469 msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
470 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
472 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
473 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
476 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
477 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
478 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
479 p_dec->fmt_out.audio.i_bytes_per_frame =
480 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
482 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
483 p_dec->fmt_out.audio.i_physical_channels =
484 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
486 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
488 if( p_sys->b_packetizer )
490 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
491 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
492 *pp_out_buffer = p_sout_buffer;
496 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
497 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
498 *pp_out_buffer = p_aout_buffer;
504 /*****************************************************************************
506 *****************************************************************************/
507 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
509 decoder_sys_t *p_sys = p_dec->p_sys;
510 aout_buffer_t *p_buf;
512 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
513 if( p_buf == NULL ) return NULL;
515 p_buf->start_date = aout_DateGet( &p_sys->end_date );
517 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
519 /* Hack for libmad filter */
520 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
525 /*****************************************************************************
527 *****************************************************************************/
528 static block_t *GetSoutBuffer( decoder_t *p_dec )
530 decoder_sys_t *p_sys = p_dec->p_sys;
533 p_block = block_New( p_dec, p_sys->i_frame_size );
534 if( p_block == NULL ) return NULL;
536 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
539 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
545 /*****************************************************************************
546 * CloseDecoder: clean up the decoder
547 *****************************************************************************/
548 static void CloseDecoder( vlc_object_t *p_this )
550 decoder_t *p_dec = (decoder_t *)p_this;
551 decoder_sys_t *p_sys = p_dec->p_sys;
553 block_BytestreamRelease( &p_sys->bytestream );
558 /*****************************************************************************
559 * SyncInfo: parse MPEG audio sync info
560 *****************************************************************************/
561 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
562 unsigned int * pi_channels_conf,
563 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
564 unsigned int * pi_frame_length,
565 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
567 static const int ppi_bitrate[2][3][16] =
571 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
574 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
577 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
583 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
586 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
589 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
594 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
596 { 44100, 48000, 32000, 0 },
597 { 22050, 24000, 16000, 0 }
600 int i_version, i_mode, i_emphasis;
601 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
602 int i_frame_size = 0;
603 int i_bitrate_index, i_samplerate_index;
606 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
607 i_version = 1 - ((i_header & 0x80000) >> 19);
608 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
609 b_crc = !((i_header >> 16) & 0x01);
610 i_bitrate_index = (i_header & 0xf000) >> 12;
611 i_samplerate_index = (i_header & 0xc00) >> 10;
612 b_padding = (i_header & 0x200) >> 9;
614 i_mode = (i_header & 0xc0) >> 6;
615 /* Modeext, copyright & original */
616 i_emphasis = i_header & 0x3;
618 if( *pi_layer != 4 &&
619 i_bitrate_index < 0x0f &&
620 i_samplerate_index != 0x03 &&
626 case 1: /* joint stereo */
628 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
630 case 2: /* dual-mono */
632 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
633 | AOUT_CHAN_DUALMONO;
637 *pi_channels_conf = AOUT_CHAN_CENTER;
640 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
641 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
642 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
646 *pi_sample_rate >>= 1;
652 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
654 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
655 *pi_sample_rate + 1 ) * 4;
656 *pi_frame_length = 384;
660 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
661 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
662 *pi_frame_length = 1152;
666 i_frame_size = ( i_version ? 72000 : 144000 ) *
667 *pi_bit_rate / *pi_sample_rate + b_padding;
668 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
669 i_max_bit_rate / *pi_sample_rate + 1;
670 *pi_frame_length = i_version ? 576 : 1152;