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>
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( N_("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( N_("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 )
158 p_sys->b_packetizer = false;
159 p_sys->i_state = STATE_NOSYNC;
160 aout_DateSet( &p_sys->end_date, 0 );
161 p_sys->bytestream = block_BytestreamInit();
162 p_sys->b_discontinuity = false;
163 p_sys->i_input_rate = INPUT_RATE_DEFAULT;
165 /* Set output properties */
166 p_dec->fmt_out.i_cat = AUDIO_ES;
167 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
168 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
171 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
173 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
176 /* Start with the minimum size for a free bitrate frame */
177 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
182 static int OpenPacketizer( vlc_object_t *p_this )
184 decoder_t *p_dec = (decoder_t*)p_this;
186 int i_ret = OpenDecoder( 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];
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_BytestreamFlush( &p_sys->bytestream );
215 // aout_DateSet( &p_sys->end_date, 0 );
216 block_Release( *pp_block );
217 p_sys->b_discontinuity = true;
221 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
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 if( (*pp_block)->i_rate > 0 )
230 p_sys->i_input_rate = (*pp_block)->i_rate;
232 block_BytestreamPush( &p_sys->bytestream, *pp_block );
236 switch( p_sys->i_state )
240 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
243 /* Look for sync word - should be 0xffe */
244 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
246 p_sys->i_state = STATE_SYNC;
249 block_SkipByte( &p_sys->bytestream );
251 if( p_sys->i_state != STATE_SYNC )
253 block_BytestreamFlush( &p_sys->bytestream );
260 /* New frame, set the Presentation Time Stamp */
261 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
262 if( p_sys->i_pts != 0 &&
263 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
265 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
267 p_sys->i_state = STATE_HEADER;
270 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
271 if( block_PeekBytes( &p_sys->bytestream, p_header,
272 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
278 /* Build frame header */
279 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
282 /* Check if frame is valid and get frame info */
283 p_sys->i_frame_size = SyncInfo( i_header,
285 &p_sys->i_channels_conf,
288 &p_sys->i_frame_length,
289 &p_sys->i_max_frame_size,
292 if( p_sys->i_frame_size == -1 )
294 msg_Dbg( p_dec, "emulated startcode" );
295 block_SkipByte( &p_sys->bytestream );
296 p_sys->i_state = STATE_NOSYNC;
297 p_sys->b_discontinuity = true;
301 if( p_sys->i_bit_rate == 0 )
303 /* Free bitrate, but 99% emulated startcode :( */
304 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
306 msg_Dbg( p_dec, "free bitrate mode");
308 /* The -1 below is to account for the frame padding */
309 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
312 p_sys->i_state = STATE_NEXT_SYNC;
314 case STATE_NEXT_SYNC:
315 /* TODO: If p_block == NULL, flush the buffer without checking the
318 /* Check if next expected frame contains the sync word */
319 if( block_PeekOffsetBytes( &p_sys->bytestream,
320 p_sys->i_frame_size, p_header,
321 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
327 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
329 /* Startcode is fine, let's try the header as an extra check */
330 int i_next_frame_size;
331 unsigned int i_next_channels, i_next_channels_conf;
332 unsigned int i_next_rate, i_next_bit_rate;
333 unsigned int i_next_frame_length, i_next_max_frame_size;
334 unsigned int i_next_layer;
336 /* Build frame header */
337 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
340 i_next_frame_size = SyncInfo( i_header,
342 &i_next_channels_conf,
345 &i_next_frame_length,
346 &i_next_max_frame_size,
349 /* Free bitrate only */
350 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
352 if( (unsigned int)p_sys->i_frame_size >
353 p_sys->i_max_frame_size )
355 msg_Dbg( p_dec, "frame too big %d > %d "
356 "(emulated startcode ?)", p_sys->i_frame_size,
357 p_sys->i_max_frame_size );
358 block_SkipByte( &p_sys->bytestream );
359 p_sys->i_state = STATE_NOSYNC;
360 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
364 p_sys->i_frame_size++;
368 if( i_next_frame_size == -1 )
370 msg_Dbg( p_dec, "emulated startcode on next frame" );
371 block_SkipByte( &p_sys->bytestream );
372 p_sys->i_state = STATE_NOSYNC;
373 p_sys->b_discontinuity = true;
377 /* Check info is in sync with previous one */
378 if( i_next_channels_conf != p_sys->i_channels_conf ||
379 i_next_rate != p_sys->i_rate ||
380 i_next_layer != p_sys->i_layer ||
381 i_next_frame_length != p_sys->i_frame_length )
383 /* Free bitrate only */
384 if( p_sys->i_bit_rate == 0 )
386 p_sys->i_frame_size++;
390 msg_Dbg( p_dec, "parameters changed unexpectedly "
391 "(emulated startcode ?)" );
392 block_SkipByte( &p_sys->bytestream );
393 p_sys->i_state = STATE_NOSYNC;
397 /* Free bitrate only */
398 if( p_sys->i_bit_rate == 0 )
400 if( i_next_bit_rate != 0 )
402 p_sys->i_frame_size++;
410 /* Free bitrate only */
411 if( p_sys->i_bit_rate == 0 )
413 if( (unsigned int)p_sys->i_frame_size >
414 p_sys->i_max_frame_size )
416 msg_Dbg( p_dec, "frame too big %d > %d "
417 "(emulated startcode ?)", p_sys->i_frame_size,
418 p_sys->i_max_frame_size );
419 block_SkipByte( &p_sys->bytestream );
420 p_sys->i_state = STATE_NOSYNC;
421 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
425 p_sys->i_frame_size++;
429 msg_Dbg( p_dec, "emulated startcode "
430 "(no startcode on following frame)" );
431 p_sys->i_state = STATE_NOSYNC;
432 block_SkipByte( &p_sys->bytestream );
436 p_sys->i_state = STATE_SEND_DATA;
440 /* Make sure we have enough data.
441 * (Not useful if we went through NEXT_SYNC) */
442 if( block_WaitBytes( &p_sys->bytestream,
443 p_sys->i_frame_size ) != VLC_SUCCESS )
448 p_sys->i_state = STATE_SEND_DATA;
450 case STATE_SEND_DATA:
451 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
453 //p_dec->b_error = true;
457 /* Free bitrate only */
458 if( p_sys->i_bit_rate == 0 )
460 p_sys->i_free_frame_size = p_sys->i_frame_size;
463 /* Copy the whole frame into the buffer. When we reach this point
464 * we already know we have enough data available. */
465 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
467 /* Get beginning of next frame for libmad */
468 if( !p_sys->b_packetizer )
470 memcpy( p_buf + p_sys->i_frame_size,
471 p_header, MAD_BUFFER_GUARD );
474 p_sys->i_state = STATE_NOSYNC;
476 /* Make sure we don't reuse the same pts twice */
477 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
478 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
480 /* So p_block doesn't get re-added several times */
481 *pp_block = block_BytestreamPop( &p_sys->bytestream );
490 /*****************************************************************************
492 *****************************************************************************/
493 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
495 decoder_sys_t *p_sys = p_dec->p_sys;
498 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
500 msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
501 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
503 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
504 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
507 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
508 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
509 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
510 p_dec->fmt_out.audio.i_bytes_per_frame =
511 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
513 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
514 p_dec->fmt_out.audio.i_physical_channels =
515 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
517 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
519 if( p_sys->b_packetizer )
521 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
522 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
523 *pp_out_buffer = p_sout_buffer;
527 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
528 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
529 *pp_out_buffer = p_aout_buffer;
535 /*****************************************************************************
537 *****************************************************************************/
538 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
540 decoder_sys_t *p_sys = p_dec->p_sys;
541 aout_buffer_t *p_buf;
543 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
544 if( p_buf == NULL ) return NULL;
546 p_buf->start_date = aout_DateGet( &p_sys->end_date );
548 aout_DateIncrement( &p_sys->end_date,
549 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT );
550 p_buf->b_discontinuity = p_sys->b_discontinuity;
551 p_sys->b_discontinuity = false;
553 /* Hack for libmad filter */
554 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
559 /*****************************************************************************
561 *****************************************************************************/
562 static block_t *GetSoutBuffer( decoder_t *p_dec )
564 decoder_sys_t *p_sys = p_dec->p_sys;
567 p_block = block_New( p_dec, p_sys->i_frame_size );
568 if( p_block == NULL ) return NULL;
570 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
573 aout_DateIncrement( &p_sys->end_date,
574 p_sys->i_frame_length * p_sys->i_input_rate / INPUT_RATE_DEFAULT ) -
580 /*****************************************************************************
581 * CloseDecoder: clean up the decoder
582 *****************************************************************************/
583 static void CloseDecoder( vlc_object_t *p_this )
585 decoder_t *p_dec = (decoder_t *)p_this;
586 decoder_sys_t *p_sys = p_dec->p_sys;
588 block_BytestreamRelease( &p_sys->bytestream );
593 /*****************************************************************************
594 * SyncInfo: parse MPEG audio sync info
595 *****************************************************************************/
596 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
597 unsigned int * pi_channels_conf,
598 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
599 unsigned int * pi_frame_length,
600 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
602 static const int ppi_bitrate[2][3][16] =
606 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
609 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
612 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
618 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
621 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
624 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
629 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
631 { 44100, 48000, 32000, 0 },
632 { 22050, 24000, 16000, 0 }
635 int i_version, i_mode, i_emphasis;
636 bool b_padding, b_mpeg_2_5, b_crc;
637 int i_frame_size = 0;
638 int i_bitrate_index, i_samplerate_index;
641 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
642 i_version = 1 - ((i_header & 0x80000) >> 19);
643 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
644 b_crc = !((i_header >> 16) & 0x01);
645 i_bitrate_index = (i_header & 0xf000) >> 12;
646 i_samplerate_index = (i_header & 0xc00) >> 10;
647 b_padding = (i_header & 0x200) >> 9;
649 i_mode = (i_header & 0xc0) >> 6;
650 /* Modeext, copyright & original */
651 i_emphasis = i_header & 0x3;
653 if( *pi_layer != 4 &&
654 i_bitrate_index < 0x0f &&
655 i_samplerate_index != 0x03 &&
661 case 1: /* joint stereo */
663 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
665 case 2: /* dual-mono */
667 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
668 | AOUT_CHAN_DUALMONO;
672 *pi_channels_conf = AOUT_CHAN_CENTER;
675 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
676 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
677 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
681 *pi_sample_rate >>= 1;
687 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
689 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
690 *pi_sample_rate + 1 ) * 4;
691 *pi_frame_length = 384;
695 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
696 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
697 *pi_frame_length = 1152;
701 i_frame_size = ( i_version ? 72000 : 144000 ) *
702 *pi_bit_rate / *pi_sample_rate + b_padding;
703 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
704 i_max_bit_rate / *pi_sample_rate + 1;
705 *pi_frame_length = i_version ? 576 : 1152;
712 /* Free bitrate mode can support higher bitrates */
713 if( !*pi_bit_rate ) *pi_max_frame_size *= 2;