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 #if defined(SYS_DARWIN)
108 set_capability( "decoder", 5 );
110 set_capability( "decoder", 100 );
112 set_callbacks( OpenDecoder, CloseDecoder );
115 set_description( _("MPEG audio layer I/II/III packetizer") );
116 set_capability( "packetizer", 10 );
117 set_callbacks( OpenPacketizer, CloseDecoder );
120 /*****************************************************************************
121 * OpenDecoder: probe the decoder and return score
122 *****************************************************************************/
123 static int OpenDecoder( vlc_object_t *p_this )
125 decoder_t *p_dec = (decoder_t*)p_this;
126 decoder_sys_t *p_sys;
128 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
133 /* Allocate the memory needed to store the decoder's structure */
134 if( ( p_dec->p_sys = p_sys =
135 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
137 msg_Err( p_dec, "out of memory" );
142 p_sys->b_packetizer = VLC_FALSE;
143 p_sys->i_state = STATE_NOSYNC;
144 aout_DateSet( &p_sys->end_date, 0 );
145 p_sys->bytestream = block_BytestreamInit( p_dec );
147 /* Set output properties */
148 p_dec->fmt_out.i_cat = AUDIO_ES;
149 p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
150 p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
153 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
155 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
158 /* Start with the minimum size for a free bitrate frame */
159 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
164 static int OpenPacketizer( vlc_object_t *p_this )
166 decoder_t *p_dec = (decoder_t*)p_this;
168 int i_ret = OpenDecoder( p_this );
170 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
175 /****************************************************************************
176 * DecodeBlock: the whole thing
177 ****************************************************************************
178 * This function is called just after the thread is launched.
179 ****************************************************************************/
180 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
182 decoder_sys_t *p_sys = p_dec->p_sys;
183 uint8_t p_header[MAD_BUFFER_GUARD];
188 if( !pp_block || !*pp_block ) return NULL;
190 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
192 /* We've just started the stream, wait for the first PTS. */
193 block_Release( *pp_block );
197 if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
199 p_sys->i_state = STATE_NOSYNC;
202 block_BytestreamPush( &p_sys->bytestream, *pp_block );
206 switch( p_sys->i_state )
210 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
213 /* Look for sync word - should be 0xffe */
214 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
216 p_sys->i_state = STATE_SYNC;
219 block_SkipByte( &p_sys->bytestream );
221 if( p_sys->i_state != STATE_SYNC )
223 block_BytestreamFlush( &p_sys->bytestream );
230 /* New frame, set the Presentation Time Stamp */
231 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
232 if( p_sys->i_pts != 0 &&
233 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
235 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
237 p_sys->i_state = STATE_HEADER;
240 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
241 if( block_PeekBytes( &p_sys->bytestream, p_header,
242 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
248 /* Build frame header */
249 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
252 /* Check if frame is valid and get frame info */
253 p_sys->i_frame_size = SyncInfo( i_header,
255 &p_sys->i_channels_conf,
258 &p_sys->i_frame_length,
259 &p_sys->i_max_frame_size,
262 if( p_sys->i_frame_size == -1 )
264 msg_Dbg( p_dec, "emulated startcode" );
265 block_SkipByte( &p_sys->bytestream );
266 p_sys->i_state = STATE_NOSYNC;
270 if( p_sys->i_bit_rate == 0 )
272 /* Free birate, but 99% emulated startcode :( */
273 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
275 msg_Dbg( p_dec, "free bitrate mode");
277 p_sys->i_frame_size = p_sys->i_free_frame_size;
280 p_sys->i_state = STATE_NEXT_SYNC;
282 case STATE_NEXT_SYNC:
283 /* TODO: If p_block == NULL, flush the buffer without checking the
286 /* Check if next expected frame contains the sync word */
287 if( block_PeekOffsetBytes( &p_sys->bytestream,
288 p_sys->i_frame_size, p_header,
289 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
295 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
297 /* Startcode is fine, let's try the header as an extra check */
298 int i_next_frame_size;
299 unsigned int i_next_channels, i_next_channels_conf;
300 unsigned int i_next_rate, i_next_bit_rate;
301 unsigned int i_next_frame_length, i_next_max_frame_size;
302 unsigned int i_next_layer;
304 /* Build frame header */
305 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
308 i_next_frame_size = SyncInfo( i_header,
310 &i_next_channels_conf,
313 &i_next_frame_length,
314 &i_next_max_frame_size,
317 /* Free bitrate only */
318 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
320 if( (unsigned int)p_sys->i_frame_size >
321 p_sys->i_max_frame_size )
323 msg_Dbg( p_dec, "frame too big %d > %d "
324 "(emulated startcode ?)", p_sys->i_frame_size,
325 p_sys->i_max_frame_size );
326 block_SkipByte( &p_sys->bytestream );
327 p_sys->i_state = STATE_NOSYNC;
328 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
332 p_sys->i_frame_size++;
336 if( i_next_frame_size == -1 )
338 msg_Dbg( p_dec, "emulated startcode on next frame" );
339 block_SkipByte( &p_sys->bytestream );
340 p_sys->i_state = STATE_NOSYNC;
344 /* Check info is in sync with previous one */
345 if( i_next_channels_conf != p_sys->i_channels_conf ||
346 i_next_rate != p_sys->i_rate ||
347 i_next_layer != p_sys->i_layer ||
348 i_next_frame_length != p_sys->i_frame_length )
350 /* Free bitrate only */
351 if( p_sys->i_bit_rate == 0 )
353 p_sys->i_frame_size++;
357 msg_Dbg( p_dec, "parameters changed unexpectedly "
358 "(emulated startcode ?)" );
359 block_SkipByte( &p_sys->bytestream );
360 p_sys->i_state = STATE_NOSYNC;
364 /* Free bitrate only */
365 if( p_sys->i_bit_rate == 0 )
367 if( i_next_bit_rate != 0 )
369 p_sys->i_frame_size++;
377 /* Free bitrate only */
378 if( p_sys->i_bit_rate == 0 )
380 if( (unsigned int)p_sys->i_frame_size >
381 p_sys->i_max_frame_size )
383 msg_Dbg( p_dec, "frame too big %d > %d "
384 "(emulated startcode ?)", p_sys->i_frame_size,
385 p_sys->i_max_frame_size );
386 block_SkipByte( &p_sys->bytestream );
387 p_sys->i_state = STATE_NOSYNC;
388 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
392 p_sys->i_frame_size++;
396 msg_Dbg( p_dec, "emulated startcode "
397 "(no startcode on following frame)" );
398 p_sys->i_state = STATE_NOSYNC;
399 block_SkipByte( &p_sys->bytestream );
403 p_sys->i_state = STATE_SEND_DATA;
407 /* Make sure we have enough data.
408 * (Not useful if we went through NEXT_SYNC) */
409 if( block_WaitBytes( &p_sys->bytestream,
410 p_sys->i_frame_size ) != VLC_SUCCESS )
415 p_sys->i_state = STATE_SEND_DATA;
417 case STATE_SEND_DATA:
418 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
420 //p_dec->b_error = VLC_TRUE;
424 /* Free bitrate only */
425 if( p_sys->i_bit_rate == 0 )
427 p_sys->i_free_frame_size = p_sys->i_frame_size;
430 /* Copy the whole frame into the buffer. When we reach this point
431 * we already know we have enough data available. */
432 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
434 /* Get beginning of next frame for libmad */
435 if( !p_sys->b_packetizer )
437 memcpy( p_buf + p_sys->i_frame_size,
438 p_header, MAD_BUFFER_GUARD );
441 p_sys->i_state = STATE_NOSYNC;
443 /* Make sure we don't reuse the same pts twice */
444 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
445 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
447 /* So p_block doesn't get re-added several times */
448 *pp_block = block_BytestreamPop( &p_sys->bytestream );
457 /*****************************************************************************
459 *****************************************************************************/
460 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
462 decoder_sys_t *p_sys = p_dec->p_sys;
465 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
467 msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
468 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
470 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
471 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
474 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
475 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
476 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
477 p_dec->fmt_out.audio.i_bytes_per_frame =
478 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
480 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
481 p_dec->fmt_out.audio.i_physical_channels =
482 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
484 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
486 if( p_sys->b_packetizer )
488 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
489 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
490 *pp_out_buffer = p_sout_buffer;
494 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
495 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
496 *pp_out_buffer = p_aout_buffer;
502 /*****************************************************************************
504 *****************************************************************************/
505 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
507 decoder_sys_t *p_sys = p_dec->p_sys;
508 aout_buffer_t *p_buf;
510 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
511 if( p_buf == NULL ) return NULL;
513 p_buf->start_date = aout_DateGet( &p_sys->end_date );
515 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
517 /* Hack for libmad filter */
518 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
523 /*****************************************************************************
525 *****************************************************************************/
526 static block_t *GetSoutBuffer( decoder_t *p_dec )
528 decoder_sys_t *p_sys = p_dec->p_sys;
531 p_block = block_New( p_dec, p_sys->i_frame_size );
532 if( p_block == NULL ) return NULL;
534 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
537 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
543 /*****************************************************************************
544 * CloseDecoder: clean up the decoder
545 *****************************************************************************/
546 static void CloseDecoder( vlc_object_t *p_this )
548 decoder_t *p_dec = (decoder_t *)p_this;
549 decoder_sys_t *p_sys = p_dec->p_sys;
551 block_BytestreamRelease( &p_sys->bytestream );
556 /*****************************************************************************
557 * SyncInfo: parse MPEG audio sync info
558 *****************************************************************************/
559 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
560 unsigned int * pi_channels_conf,
561 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
562 unsigned int * pi_frame_length,
563 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
565 static const int ppi_bitrate[2][3][16] =
569 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
572 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
575 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
581 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
584 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
587 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
592 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
594 { 44100, 48000, 32000, 0 },
595 { 22050, 24000, 16000, 0 }
598 int i_version, i_mode, i_emphasis;
599 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
600 int i_frame_size = 0;
601 int i_bitrate_index, i_samplerate_index;
604 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
605 i_version = 1 - ((i_header & 0x80000) >> 19);
606 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
607 b_crc = !((i_header >> 16) & 0x01);
608 i_bitrate_index = (i_header & 0xf000) >> 12;
609 i_samplerate_index = (i_header & 0xc00) >> 10;
610 b_padding = (i_header & 0x200) >> 9;
612 i_mode = (i_header & 0xc0) >> 6;
613 /* Modeext, copyright & original */
614 i_emphasis = i_header & 0x3;
616 if( *pi_layer != 4 &&
617 i_bitrate_index < 0x0f &&
618 i_samplerate_index != 0x03 &&
624 case 1: /* joint stereo */
626 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
628 case 2: /* dual-mono */
630 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
631 | AOUT_CHAN_DUALMONO;
635 *pi_channels_conf = AOUT_CHAN_CENTER;
638 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
639 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
640 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
644 *pi_sample_rate >>= 1;
650 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
652 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
653 *pi_sample_rate + 1 ) * 4;
654 *pi_frame_length = 384;
658 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
659 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
660 *pi_frame_length = 1152;
664 i_frame_size = ( i_version ? 72000 : 144000 ) *
665 *pi_bit_rate / *pi_sample_rate + b_padding;
666 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
667 i_max_bit_rate / *pi_sample_rate + 1;
668 *pi_frame_length = i_version ? 576 : 1152;