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');
152 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
154 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
157 /* Start with the minimum size for a free bitrate frame */
158 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
163 static int OpenPacketizer( vlc_object_t *p_this )
165 decoder_t *p_dec = (decoder_t*)p_this;
167 int i_ret = OpenDecoder( p_this );
169 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
174 /****************************************************************************
175 * DecodeBlock: the whole thing
176 ****************************************************************************
177 * This function is called just after the thread is launched.
178 ****************************************************************************/
179 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
181 decoder_sys_t *p_sys = p_dec->p_sys;
182 uint8_t p_header[MAD_BUFFER_GUARD];
187 if( !pp_block || !*pp_block ) return NULL;
189 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
191 /* We've just started the stream, wait for the first PTS. */
192 block_Release( *pp_block );
196 if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
198 p_sys->i_state = STATE_NOSYNC;
201 block_BytestreamPush( &p_sys->bytestream, *pp_block );
205 switch( p_sys->i_state )
209 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
212 /* Look for sync word - should be 0xffe */
213 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
215 p_sys->i_state = STATE_SYNC;
218 block_SkipByte( &p_sys->bytestream );
220 if( p_sys->i_state != STATE_SYNC )
222 block_BytestreamFlush( &p_sys->bytestream );
229 /* New frame, set the Presentation Time Stamp */
230 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
231 if( p_sys->i_pts != 0 &&
232 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
234 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
236 p_sys->i_state = STATE_HEADER;
239 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
240 if( block_PeekBytes( &p_sys->bytestream, p_header,
241 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
247 /* Build frame header */
248 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
251 /* Check if frame is valid and get frame info */
252 p_sys->i_frame_size = SyncInfo( i_header,
254 &p_sys->i_channels_conf,
257 &p_sys->i_frame_length,
258 &p_sys->i_max_frame_size,
261 if( p_sys->i_frame_size == -1 )
263 msg_Dbg( p_dec, "emulated startcode" );
264 block_SkipByte( &p_sys->bytestream );
265 p_sys->i_state = STATE_NOSYNC;
269 if( p_sys->i_bit_rate == 0 )
271 /* Free birate, but 99% emulated startcode :( */
272 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
274 msg_Dbg( p_dec, "free bitrate mode");
276 p_sys->i_frame_size = p_sys->i_free_frame_size;
279 p_sys->i_state = STATE_NEXT_SYNC;
281 case STATE_NEXT_SYNC:
282 /* TODO: If p_block == NULL, flush the buffer without checking the
285 /* Check if next expected frame contains the sync word */
286 if( block_PeekOffsetBytes( &p_sys->bytestream,
287 p_sys->i_frame_size, p_header,
288 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
294 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
296 /* Startcode is fine, let's try the header as an extra check */
297 int i_next_frame_size;
298 unsigned int i_next_channels, i_next_channels_conf;
299 unsigned int i_next_rate, i_next_bit_rate;
300 unsigned int i_next_frame_length, i_next_max_frame_size;
301 unsigned int i_next_layer;
303 /* Build frame header */
304 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
307 i_next_frame_size = SyncInfo( i_header,
309 &i_next_channels_conf,
312 &i_next_frame_length,
313 &i_next_max_frame_size,
316 /* Free bitrate only */
317 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
319 if( (unsigned int)p_sys->i_frame_size >
320 p_sys->i_max_frame_size )
322 msg_Dbg( p_dec, "frame too big %d > %d "
323 "(emulated startcode ?)", p_sys->i_frame_size,
324 p_sys->i_max_frame_size );
325 block_SkipByte( &p_sys->bytestream );
326 p_sys->i_state = STATE_NOSYNC;
327 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
331 p_sys->i_frame_size++;
335 if( i_next_frame_size == -1 )
337 msg_Dbg( p_dec, "emulated startcode on next frame" );
338 block_SkipByte( &p_sys->bytestream );
339 p_sys->i_state = STATE_NOSYNC;
343 /* Check info is in sync with previous one */
344 if( i_next_channels_conf != p_sys->i_channels_conf ||
345 i_next_rate != p_sys->i_rate ||
346 i_next_layer != p_sys->i_layer ||
347 i_next_frame_length != p_sys->i_frame_length )
349 /* Free bitrate only */
350 if( p_sys->i_bit_rate == 0 )
352 p_sys->i_frame_size++;
356 msg_Dbg( p_dec, "parameters changed unexpectedly "
357 "(emulated startcode ?)" );
358 block_SkipByte( &p_sys->bytestream );
359 p_sys->i_state = STATE_NOSYNC;
363 /* Free bitrate only */
364 if( p_sys->i_bit_rate == 0 )
366 if( i_next_bit_rate != 0 )
368 p_sys->i_frame_size++;
376 /* Free bitrate only */
377 if( p_sys->i_bit_rate == 0 )
379 if( (unsigned int)p_sys->i_frame_size >
380 p_sys->i_max_frame_size )
382 msg_Dbg( p_dec, "frame too big %d > %d "
383 "(emulated startcode ?)", p_sys->i_frame_size,
384 p_sys->i_max_frame_size );
385 block_SkipByte( &p_sys->bytestream );
386 p_sys->i_state = STATE_NOSYNC;
387 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
391 p_sys->i_frame_size++;
395 msg_Dbg( p_dec, "emulated startcode "
396 "(no startcode on following frame)" );
397 p_sys->i_state = STATE_NOSYNC;
398 block_SkipByte( &p_sys->bytestream );
402 p_sys->i_state = STATE_SEND_DATA;
406 /* Make sure we have enough data.
407 * (Not useful if we went through NEXT_SYNC) */
408 if( block_WaitBytes( &p_sys->bytestream,
409 p_sys->i_frame_size ) != VLC_SUCCESS )
414 p_sys->i_state = STATE_SEND_DATA;
416 case STATE_SEND_DATA:
417 if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
419 //p_dec->b_error = VLC_TRUE;
423 /* Free bitrate only */
424 if( p_sys->i_bit_rate == 0 )
426 p_sys->i_free_frame_size = p_sys->i_frame_size;
429 /* Copy the whole frame into the buffer. When we reach this point
430 * we already know we have enough data available. */
431 block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
433 /* Get beginning of next frame for libmad */
434 if( !p_sys->b_packetizer )
436 memcpy( p_buf + p_sys->i_frame_size,
437 p_header, MAD_BUFFER_GUARD );
440 p_sys->i_state = STATE_NOSYNC;
442 /* Make sure we don't reuse the same pts twice */
443 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
444 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
446 /* So p_block doesn't get re-added several times */
447 *pp_block = block_BytestreamPop( &p_sys->bytestream );
456 /*****************************************************************************
458 *****************************************************************************/
459 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
461 decoder_sys_t *p_sys = p_dec->p_sys;
464 if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
466 msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
467 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
469 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
470 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
473 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
474 p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
475 p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
476 p_dec->fmt_out.audio.i_bytes_per_frame =
477 p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
479 p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
480 p_dec->fmt_out.audio.i_physical_channels =
481 p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
483 p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;
485 if( p_sys->b_packetizer )
487 block_t *p_sout_buffer = GetSoutBuffer( p_dec );
488 p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
489 *pp_out_buffer = p_sout_buffer;
493 aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
494 p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
495 *pp_out_buffer = p_aout_buffer;
501 /*****************************************************************************
503 *****************************************************************************/
504 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
506 decoder_sys_t *p_sys = p_dec->p_sys;
507 aout_buffer_t *p_buf;
509 p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
510 if( p_buf == NULL ) return NULL;
512 p_buf->start_date = aout_DateGet( &p_sys->end_date );
514 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
516 /* Hack for libmad filter */
517 p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
522 /*****************************************************************************
524 *****************************************************************************/
525 static block_t *GetSoutBuffer( decoder_t *p_dec )
527 decoder_sys_t *p_sys = p_dec->p_sys;
530 p_block = block_New( p_dec, p_sys->i_frame_size );
531 if( p_block == NULL ) return NULL;
533 p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
536 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
542 /*****************************************************************************
543 * CloseDecoder: clean up the decoder
544 *****************************************************************************/
545 static void CloseDecoder( vlc_object_t *p_this )
547 decoder_t *p_dec = (decoder_t *)p_this;
548 decoder_sys_t *p_sys = p_dec->p_sys;
550 block_BytestreamRelease( &p_sys->bytestream );
555 /*****************************************************************************
556 * SyncInfo: parse MPEG audio sync info
557 *****************************************************************************/
558 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
559 unsigned int * pi_channels_conf,
560 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
561 unsigned int * pi_frame_length,
562 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
564 static const int ppi_bitrate[2][3][16] =
568 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
571 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
574 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
580 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
583 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
586 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
591 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
593 { 44100, 48000, 32000, 0 },
594 { 22050, 24000, 16000, 0 }
597 int i_version, i_mode, i_emphasis;
598 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
599 int i_frame_size = 0;
600 int i_bitrate_index, i_samplerate_index;
603 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
604 i_version = 1 - ((i_header & 0x80000) >> 19);
605 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
606 b_crc = !((i_header >> 16) & 0x01);
607 i_bitrate_index = (i_header & 0xf000) >> 12;
608 i_samplerate_index = (i_header & 0xc00) >> 10;
609 b_padding = (i_header & 0x200) >> 9;
611 i_mode = (i_header & 0xc0) >> 6;
612 /* Modeext, copyright & original */
613 i_emphasis = i_header & 0x3;
615 if( *pi_layer != 4 &&
616 i_bitrate_index < 0x0f &&
617 i_samplerate_index != 0x03 &&
623 case 1: /* joint stereo */
625 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
627 case 2: /* dual-mono */
629 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
630 | AOUT_CHAN_DUALMONO;
634 *pi_channels_conf = AOUT_CHAN_CENTER;
637 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
638 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
639 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
643 *pi_sample_rate >>= 1;
649 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
651 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
652 *pi_sample_rate + 1 ) * 4;
653 *pi_frame_length = 384;
657 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
658 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
659 *pi_frame_length = 1152;
663 i_frame_size = ( i_version ? 72000 : 144000 ) *
664 *pi_bit_rate / *pi_sample_rate + b_padding;
665 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
666 i_max_bit_rate / *pi_sample_rate + 1;
667 *pi_frame_length = i_version ? 576 : 1152;