1 /*****************************************************************************
2 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: mpeg_audio.c,v 1.25 2004/01/25 18:20:12 bigben Exp $
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_capability( "decoder", 100 );
108 set_callbacks( OpenDecoder, CloseDecoder );
111 set_description( _("MPEG audio layer I/II/III packetizer") );
112 set_capability( "packetizer", 10 );
113 set_callbacks( OpenPacketizer, CloseDecoder );
116 /*****************************************************************************
117 * OpenDecoder: probe the decoder and return score
118 *****************************************************************************/
119 static int OpenDecoder( vlc_object_t *p_this )
121 decoder_t *p_dec = (decoder_t*)p_this;
122 decoder_sys_t *p_sys;
124 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
129 /* Allocate the memory needed to store the decoder's structure */
130 if( ( p_dec->p_sys = p_sys =
131 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
133 msg_Err( p_dec, "out of memory" );
138 p_sys->b_packetizer = VLC_FALSE;
139 p_sys->i_state = STATE_NOSYNC;
140 aout_DateSet( &p_sys->end_date, 0 );
141 p_sys->bytestream = block_BytestreamInit( p_dec );
143 /* Set output properties */
144 p_dec->fmt_out.i_cat = AUDIO_ES;
145 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
148 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
150 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
153 /* Start with the minimum size for a free bitrate frame */
154 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
159 static int OpenPacketizer( vlc_object_t *p_this )
161 decoder_t *p_dec = (decoder_t*)p_this;
163 int i_ret = OpenDecoder( p_this );
165 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
170 /****************************************************************************
171 * DecodeBlock: the whole thing
172 ****************************************************************************
173 * This function is called just after the thread is launched.
174 ****************************************************************************/
175 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
177 decoder_sys_t *p_sys = p_dec->p_sys;
178 uint8_t p_header[MAD_BUFFER_GUARD];
183 if( !pp_block || !*pp_block ) return NULL;
185 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
187 /* We've just started the stream, wait for the first PTS. */
188 block_Release( *pp_block );
192 if( (*pp_block)->b_discontinuity )
194 p_sys->i_state = STATE_NOSYNC;
197 block_BytestreamPush( &p_sys->bytestream, *pp_block );
201 switch( p_sys->i_state )
205 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
208 /* Look for sync word - should be 0xffe */
209 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
211 p_sys->i_state = STATE_SYNC;
214 block_SkipByte( &p_sys->bytestream );
216 if( p_sys->i_state != STATE_SYNC )
218 block_BytestreamFlush( &p_sys->bytestream );
225 /* New frame, set the Presentation Time Stamp */
226 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
227 if( p_sys->i_pts != 0 &&
228 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
230 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
232 p_sys->i_state = STATE_HEADER;
235 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
236 if( block_PeekBytes( &p_sys->bytestream, p_header,
237 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
243 /* Build frame header */
244 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
247 /* Check if frame is valid and get frame info */
248 p_sys->i_frame_size = SyncInfo( i_header,
250 &p_sys->i_channels_conf,
253 &p_sys->i_frame_length,
254 &p_sys->i_max_frame_size,
257 if( p_sys->i_frame_size == -1 )
259 msg_Dbg( p_dec, "emulated startcode" );
260 block_SkipByte( &p_sys->bytestream );
261 p_sys->i_state = STATE_NOSYNC;
265 if( p_sys->i_bit_rate == 0 )
267 /* Free birate, but 99% emulated startcode :( */
268 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
270 msg_Dbg( p_dec, "free bitrate mode");
272 p_sys->i_frame_size = p_sys->i_free_frame_size;
275 p_sys->i_state = STATE_NEXT_SYNC;
277 case STATE_NEXT_SYNC:
278 /* TODO: If p_block == NULL, flush the buffer without checking the
281 /* Check if next expected frame contains the sync word */
282 if( block_PeekOffsetBytes( &p_sys->bytestream,
283 p_sys->i_frame_size, p_header,
284 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
290 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
292 /* Startcode is fine, let's try the header as an extra check */
293 int i_next_frame_size;
294 unsigned int i_next_channels, i_next_channels_conf;
295 unsigned int i_next_rate, i_next_bit_rate;
296 unsigned int i_next_frame_length, i_next_max_frame_size;
297 unsigned int i_next_layer;
299 /* Build frame header */
300 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
303 i_next_frame_size = SyncInfo( i_header,
305 &i_next_channels_conf,
308 &i_next_frame_length,
309 &i_next_max_frame_size,
312 /* Free bitrate only */
313 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
315 if( (unsigned int)p_sys->i_frame_size >
316 p_sys->i_max_frame_size )
318 msg_Dbg( p_dec, "frame too big %d > %d "
319 "(emulated startcode ?)", p_sys->i_frame_size,
320 p_sys->i_max_frame_size );
321 block_SkipByte( &p_sys->bytestream );
322 p_sys->i_state = STATE_NOSYNC;
323 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
327 p_sys->i_frame_size++;
331 if( i_next_frame_size == -1 )
333 msg_Dbg( p_dec, "emulated startcode on next frame" );
334 block_SkipByte( &p_sys->bytestream );
335 p_sys->i_state = STATE_NOSYNC;
339 /* Check info is in sync with previous one */
340 if( i_next_channels_conf != p_sys->i_channels_conf ||
341 i_next_rate != p_sys->i_rate ||
342 i_next_layer != p_sys->i_layer ||
343 i_next_frame_length != p_sys->i_frame_length )
345 /* Free bitrate only */
346 if( p_sys->i_bit_rate == 0 )
348 p_sys->i_frame_size++;
352 msg_Dbg( p_dec, "parameters changed unexpectedly "
353 "(emulated startcode ?)" );
354 block_SkipByte( &p_sys->bytestream );
355 p_sys->i_state = STATE_NOSYNC;
359 /* Free bitrate only */
360 if( p_sys->i_bit_rate == 0 )
362 if( i_next_bit_rate != 0 )
364 p_sys->i_frame_size++;
372 /* Free bitrate only */
373 if( p_sys->i_bit_rate == 0 )
375 if( (unsigned int)p_sys->i_frame_size >
376 p_sys->i_max_frame_size )
378 msg_Dbg( p_dec, "frame too big %d > %d "
379 "(emulated startcode ?)", p_sys->i_frame_size,
380 p_sys->i_max_frame_size );
381 block_SkipByte( &p_sys->bytestream );
382 p_sys->i_state = STATE_NOSYNC;
383 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
387 p_sys->i_frame_size++;
391 msg_Dbg( p_dec, "emulated startcode "
392 "(no startcode on following frame)" );
393 p_sys->i_state = STATE_NOSYNC;
394 block_SkipByte( &p_sys->bytestream );
398 p_sys->i_state = STATE_SEND_DATA;
402 /* Make sure we have enough data.
403 * (Not useful if we went through NEXT_SYNC) */
404 if( block_WaitBytes( &p_sys->bytestream,
405 p_sys->i_frame_size ) != VLC_SUCCESS )
410 p_sys->i_state = STATE_SEND_DATA;
412 case STATE_SEND_DATA:
413 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
415 //p_dec->b_error = VLC_TRUE;
419 /* Free bitrate only */
420 if( p_sys->i_bit_rate == 0 )
422 p_sys->i_free_frame_size = p_sys->i_frame_size;
425 /* Copy the whole frame into the buffer. When we reach this point
426 * we already know we have enough data available. */
427 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
429 /* Get beginning of next frame for libmad */
430 if( !p_sys->b_packetizer )
432 memcpy( p_buf + p_sys->i_frame_size,
433 p_header, MAD_BUFFER_GUARD );
436 p_sys->i_state = STATE_NOSYNC;
438 /* Make sure we don't reuse the same pts twice */
439 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
440 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
442 /* So p_block doesn't get re-added several times */
443 *pp_block = block_BytestreamPop( &p_sys->bytestream );
452 /*****************************************************************************
454 *****************************************************************************/
455 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
457 decoder_sys_t *p_sys = p_dec->p_sys;
460 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
462 msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
463 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
465 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
466 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
469 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
470 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
471 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
472 p_dec->fmt_out.audio.i_bytes_per_frame =
473 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
475 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
476 p_dec->fmt_out.audio.i_physical_channels =
477 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
479 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
481 if( p_sys->b_packetizer )
483 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
484 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
485 *pp_out_buffer = p_sout_buffer;
489 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
490 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
491 *pp_out_buffer = p_aout_buffer;
497 /*****************************************************************************
499 *****************************************************************************/
500 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
502 decoder_sys_t *p_sys = p_dec->p_sys;
503 aout_buffer_t *p_buf;
505 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
506 if( p_buf == NULL ) return NULL;
508 p_buf->start_date = aout_DateGet( &p_sys->end_date );
510 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
512 /* Hack for libmad filter */
513 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
518 /*****************************************************************************
520 *****************************************************************************/
521 static block_t *GetSoutBuffer( decoder_t *p_dec )
523 decoder_sys_t *p_sys = p_dec->p_sys;
526 p_block = block_New( p_dec, p_sys->i_frame_size );
527 if( p_block == NULL ) return NULL;
529 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
532 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
538 /*****************************************************************************
539 * CloseDecoder: clean up the decoder
540 *****************************************************************************/
541 static void CloseDecoder( vlc_object_t *p_this )
543 decoder_t *p_dec = (decoder_t *)p_this;
544 decoder_sys_t *p_sys = p_dec->p_sys;
546 block_BytestreamRelease( &p_sys->bytestream );
551 /*****************************************************************************
552 * SyncInfo: parse MPEG audio sync info
553 *****************************************************************************/
554 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
555 unsigned int * pi_channels_conf,
556 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
557 unsigned int * pi_frame_length,
558 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
560 static const int ppi_bitrate[2][3][16] =
564 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
567 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
570 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
576 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
579 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
582 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
587 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
589 { 44100, 48000, 32000, 0 },
590 { 22050, 24000, 16000, 0 }
593 int i_version, i_mode, i_emphasis;
594 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
595 int i_frame_size = 0;
596 int i_bitrate_index, i_samplerate_index;
599 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
600 i_version = 1 - ((i_header & 0x80000) >> 19);
601 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
602 b_crc = !((i_header >> 16) & 0x01);
603 i_bitrate_index = (i_header & 0xf000) >> 12;
604 i_samplerate_index = (i_header & 0xc00) >> 10;
605 b_padding = (i_header & 0x200) >> 9;
607 i_mode = (i_header & 0xc0) >> 6;
608 /* Modeext, copyright & original */
609 i_emphasis = i_header & 0x3;
611 if( *pi_layer != 4 &&
612 i_bitrate_index < 0x0f &&
613 i_samplerate_index != 0x03 &&
619 case 1: /* joint stereo */
621 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
623 case 2: /* dual-mono */
625 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
626 | AOUT_CHAN_DUALMONO;
630 *pi_channels_conf = AOUT_CHAN_CENTER;
633 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
634 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
635 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
639 *pi_sample_rate >>= 1;
645 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
647 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
648 *pi_sample_rate + 1 ) * 4;
649 *pi_frame_length = 384;
653 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
654 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
655 *pi_frame_length = 1152;
659 i_frame_size = ( i_version ? 72000 : 144000 ) *
660 *pi_bit_rate / *pi_sample_rate + b_padding;
661 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
662 i_max_bit_rate / *pi_sample_rate + 1;
663 *pi_frame_length = i_version ? 576 : 1152;