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.21 2003/10/23 21:28:11 gbazin 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 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
31 #include <string.h> /* strdup() */
34 #include <vlc/decoder.h>
35 #include <vlc/input.h>
39 #include "vlc_block_helper.h"
41 /*****************************************************************************
42 * decoder_sys_t : decoder descriptor
43 *****************************************************************************/
47 vlc_bool_t b_packetizer;
55 block_bytestream_t bytestream;
58 * Decoder output properties
60 aout_instance_t * p_aout; /* opaque */
61 aout_input_t * p_aout_input; /* opaque */
62 audio_sample_format_t aout_format;
63 aout_buffer_t * p_aout_buffer; /* current aout buffer being filled */
66 * Packetizer output properties
68 sout_packetizer_input_t *p_sout_input;
69 sout_format_t sout_format;
70 sout_buffer_t * p_sout_buffer; /* current sout buffer */
75 uint8_t *p_out_buffer; /* output buffer */
76 audio_date_t end_date;
77 unsigned int i_current_layer;
81 int i_frame_size, i_free_frame_size;
82 unsigned int i_channels_conf, i_channels;
83 unsigned int i_rate, i_max_frame_size, i_frame_length;
84 unsigned int i_layer, i_bit_rate;
96 /* This isn't the place to put mad-specific stuff. However, it makes the
97 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
98 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
99 * bigger buffer. This has no implication on other plug-ins, and we only
100 * lose 8 bytes per frame. --Meuuh */
101 #define MAD_BUFFER_GUARD 8
102 #define MPGA_HEADER_SIZE 4
104 /****************************************************************************
106 ****************************************************************************/
107 static int OpenDecoder ( vlc_object_t * );
108 static int OpenPacketizer( vlc_object_t * );
110 static int InitDecoder ( decoder_t * );
111 static int RunDecoder ( decoder_t *, block_t * );
112 static int EndDecoder ( decoder_t * );
114 static int GetOutBuffer ( decoder_t *, uint8_t ** );
115 static int GetAoutBuffer( decoder_t *, aout_buffer_t ** );
116 static int GetSoutBuffer( decoder_t *, sout_buffer_t ** );
117 static int SendOutBuffer( decoder_t * );
119 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
120 unsigned int * pi_channels_conf,
121 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
122 unsigned int * pi_frame_length,
123 unsigned int * pi_max_frame_size,
124 unsigned int * pi_layer );
126 /*****************************************************************************
128 *****************************************************************************/
130 set_description( _("MPEG audio layer I/II/III parser") );
131 set_capability( "decoder", 100 );
132 set_callbacks( OpenDecoder, NULL );
135 set_description( _("MPEG audio layer I/II/III packetizer") );
136 set_capability( "packetizer", 10 );
137 set_callbacks( OpenPacketizer, NULL );
140 /*****************************************************************************
141 * OpenDecoder: probe the decoder and return score
142 *****************************************************************************/
143 static int OpenDecoder( vlc_object_t *p_this )
145 decoder_t *p_dec = (decoder_t*)p_this;
147 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
152 p_dec->pf_init = InitDecoder;
153 p_dec->pf_decode = RunDecoder;
154 p_dec->pf_end = EndDecoder;
156 /* Allocate the memory needed to store the decoder's structure */
158 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
160 msg_Err( p_dec, "out of memory" );
163 p_dec->p_sys->b_packetizer = VLC_FALSE;
168 static int OpenPacketizer( vlc_object_t *p_this )
170 decoder_t *p_dec = (decoder_t*)p_this;
172 int i_ret = OpenDecoder( p_this );
174 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
179 /*****************************************************************************
180 * InitDecoder: Initalize the decoder
181 *****************************************************************************/
182 static int InitDecoder( decoder_t *p_dec )
184 p_dec->p_sys->i_state = STATE_NOSYNC;
186 p_dec->p_sys->p_out_buffer = NULL;
187 aout_DateSet( &p_dec->p_sys->end_date, 0 );
189 p_dec->p_sys->p_aout = NULL;
190 p_dec->p_sys->p_aout_input = NULL;
191 p_dec->p_sys->p_aout_buffer = NULL;
192 p_dec->p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
194 p_dec->p_sys->p_sout_input = NULL;
195 p_dec->p_sys->p_sout_buffer = NULL;
196 p_dec->p_sys->sout_format.i_cat = AUDIO_ES;
197 p_dec->p_sys->sout_format.i_fourcc = VLC_FOURCC('m','p','g','a');
199 /* Start with the minimum size for a free bitrate frame */
200 p_dec->p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
202 p_dec->p_sys->p_chain = NULL;
207 /****************************************************************************
208 * RunDecoder: the whole thing
209 ****************************************************************************
210 * This function is called just after the thread is launched.
211 ****************************************************************************/
212 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
214 decoder_sys_t *p_sys = p_dec->p_sys;
215 uint8_t p_header[MAD_BUFFER_GUARD];
218 if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
220 /* We've just started the stream, wait for the first PTS. */
221 block_Release( p_block );
225 if( p_block->b_discontinuity )
227 p_sys->i_state = STATE_SYNC;
232 block_ChainAppend( &p_sys->p_chain, p_block );
236 block_ChainAppend( &p_sys->p_chain, p_block );
237 p_sys->bytestream = block_BytestreamInit( p_dec, p_sys->p_chain, 0 );
242 switch( p_sys->i_state )
246 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
249 /* Look for sync word - should be 0xffe */
250 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
252 p_sys->i_state = STATE_SYNC;
255 block_SkipByte( &p_sys->bytestream );
257 if( p_sys->i_state != STATE_SYNC )
259 if( block_PeekByte( &p_sys->bytestream, p_header )
260 == VLC_SUCCESS && p_header[0] == 0xff )
262 /* Start of a sync word, need more data */
266 block_ChainRelease( p_sys->p_chain );
267 p_sys->p_chain = NULL;
274 /* New frame, set the Presentation Time Stamp */
275 p_sys->pts = p_sys->bytestream.p_block->i_pts;
276 if( p_sys->pts != 0 &&
277 p_sys->pts != aout_DateGet( &p_sys->end_date ) )
279 aout_DateSet( &p_sys->end_date, p_sys->pts );
281 p_sys->i_state = STATE_HEADER;
285 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
286 if( block_PeekBytes( &p_sys->bytestream, p_header,
287 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
293 /* Build frame header */
294 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
297 /* Check if frame is valid and get frame info */
298 p_sys->i_frame_size = SyncInfo( i_header,
300 &p_sys->i_channels_conf,
303 &p_sys->i_frame_length,
304 &p_sys->i_max_frame_size,
307 if( p_sys->i_frame_size == -1 )
309 msg_Dbg( p_dec, "emulated start code" );
310 block_SkipByte( &p_sys->bytestream );
311 p_sys->i_state = STATE_NOSYNC;
315 if( p_sys->i_bit_rate == 0 )
317 /* Free birate, but 99% emulated startcode :( */
318 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
320 msg_Dbg( p_dec, "free bitrate mode");
322 p_sys->i_frame_size = p_sys->i_free_frame_size;
325 p_sys->i_state = STATE_NEXT_SYNC;
327 case STATE_NEXT_SYNC:
328 /* TODO: If p_block == NULL, flush the buffer without checking the
331 /* Check if next expected frame contains the sync word */
332 if( block_PeekOffsetBytes( &p_sys->bytestream,
333 p_sys->i_frame_size, p_header,
334 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
340 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
342 /* Startcode is fine, let's try the header as an extra check */
343 int i_next_frame_size;
344 unsigned int i_next_channels, i_next_channels_conf;
345 unsigned int i_next_rate, i_next_bit_rate;
346 unsigned int i_next_frame_length, i_next_max_frame_size;
347 unsigned int i_next_layer;
349 /* Build frame header */
350 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
353 i_next_frame_size = SyncInfo( i_header,
355 &i_next_channels_conf,
358 &i_next_frame_length,
359 &i_next_max_frame_size,
362 if( i_next_frame_size == -1 )
364 msg_Dbg( p_dec, "emulated start code on next frame" );
365 block_SkipByte( &p_sys->bytestream );
366 p_sys->i_state = STATE_NOSYNC;
370 /* Free bitrate only */
371 if( p_sys->i_bit_rate == 0 )
373 if( i_next_bit_rate != 0 )
375 p_sys->i_frame_size++;
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;
392 /* Check info is in sync with previous one */
393 if( i_next_channels_conf != p_sys->i_channels_conf ||
394 i_next_rate != p_sys->i_rate ||
395 i_next_layer != p_sys->i_layer ||
396 i_next_frame_length != p_sys->i_frame_length )
398 /* Free bitrate only */
399 if( p_sys->i_bit_rate == 0 )
401 p_sys->i_frame_size++;
405 msg_Dbg( p_dec, "parameters changed unexpectedly "
406 "(emulated startcode ?)" );
407 block_SkipByte( &p_sys->bytestream );
408 p_sys->i_state = STATE_NOSYNC;
414 /* Free bitrate only */
415 if( p_sys->i_bit_rate == 0 )
417 p_sys->i_frame_size++;
421 msg_Dbg( p_dec, "emulated startcode "
422 "(no startcode on following frame)" );
423 p_sys->i_state = STATE_NOSYNC;
424 block_SkipByte( &p_sys->bytestream );
428 if( GetOutBuffer( p_dec, &p_sys->p_out_buffer ) != VLC_SUCCESS )
433 /* Free bitrate only */
434 if( p_sys->i_bit_rate == 0 )
436 p_sys->i_free_frame_size = p_sys->i_frame_size;
439 p_sys->i_state = STATE_DATA;
442 /* Copy the whole frame into the buffer */
443 if( block_GetBytes( &p_sys->bytestream, p_sys->p_out_buffer,
444 p_sys->i_frame_size ) != VLC_SUCCESS )
450 p_sys->p_chain = block_BytestreamFlush( &p_sys->bytestream );
452 /* Get beginning of next frame for libmad */
453 if( !p_sys->b_packetizer )
455 memcpy( p_sys->p_out_buffer + p_sys->i_frame_size,
456 p_header, MAD_BUFFER_GUARD );
459 SendOutBuffer( p_dec );
460 p_sys->i_state = STATE_NOSYNC;
462 /* Make sure we don't reuse the same pts twice */
463 if( p_sys->pts == p_sys->bytestream.p_block->i_pts )
464 p_sys->pts = p_sys->bytestream.p_block->i_pts = 0;
471 /*****************************************************************************
473 *****************************************************************************/
474 static int GetOutBuffer( decoder_t *p_dec, uint8_t **pp_out_buffer )
476 decoder_sys_t *p_sys = p_dec->p_sys;
479 if( p_sys->b_packetizer )
481 i_ret = GetSoutBuffer( p_dec, &p_sys->p_sout_buffer );
483 p_sys->p_sout_buffer ? p_sys->p_sout_buffer->p_buffer : NULL;
487 i_ret = GetAoutBuffer( p_dec, &p_sys->p_aout_buffer );
489 p_sys->p_aout_buffer ? p_sys->p_aout_buffer->p_buffer : NULL;
495 /*****************************************************************************
497 *****************************************************************************/
498 static int GetAoutBuffer( decoder_t *p_dec, aout_buffer_t **pp_buffer )
500 decoder_sys_t *p_sys = p_dec->p_sys;
502 if( p_sys->p_aout_input != NULL &&
503 ( p_sys->aout_format.i_rate != p_sys->i_rate
504 || p_sys->aout_format.i_original_channels != p_sys->i_channels_conf
505 || p_sys->aout_format.i_bytes_per_frame !=
506 p_sys->i_max_frame_size + MAD_BUFFER_GUARD
507 || p_sys->aout_format.i_frame_length != p_sys->i_frame_length
508 || p_sys->i_current_layer != p_sys->i_layer ) )
510 /* Parameters changed - this should not happen. */
511 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
512 p_sys->p_aout_input = NULL;
515 /* Creating the audio input if not created yet. */
516 if( p_sys->p_aout_input == NULL )
518 p_sys->i_current_layer = p_sys->i_layer;
519 if( p_sys->i_layer == 3 )
521 p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','3');
525 p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
528 p_sys->aout_format.i_rate = p_sys->i_rate;
529 p_sys->aout_format.i_original_channels = p_sys->i_channels_conf;
530 p_sys->aout_format.i_physical_channels
531 = p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
532 p_sys->aout_format.i_bytes_per_frame = p_sys->i_max_frame_size
534 p_sys->aout_format.i_frame_length = p_sys->i_frame_length;
535 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
536 aout_DateSet( &p_sys->end_date, p_sys->pts );
537 p_sys->p_aout_input = aout_DecNew( p_dec,
539 &p_sys->aout_format );
540 if( p_sys->p_aout_input == NULL )
547 *pp_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
548 p_sys->i_frame_length );
549 if( *pp_buffer == NULL )
554 (*pp_buffer)->start_date = aout_DateGet( &p_sys->end_date );
555 (*pp_buffer)->end_date =
556 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
561 /*****************************************************************************
563 *****************************************************************************/
564 static int GetSoutBuffer( decoder_t *p_dec, sout_buffer_t **pp_buffer )
566 decoder_sys_t *p_sys = p_dec->p_sys;
568 if( p_sys->p_sout_input != NULL &&
569 ( p_sys->sout_format.i_sample_rate != (int)p_sys->i_rate
570 || p_sys->sout_format.i_channels != (int)p_sys->i_channels ) )
572 /* Parameters changed - this should not happen. */
575 /* Creating the sout input if not created yet. */
576 if( p_sys->p_sout_input == NULL )
578 p_sys->sout_format.i_sample_rate = p_sys->i_rate;
579 p_sys->sout_format.i_channels = p_sys->i_channels;
580 p_sys->sout_format.i_block_align = 0;
581 p_sys->sout_format.i_bitrate = p_sys->i_bit_rate;
582 p_sys->sout_format.i_extra_data = 0;
583 p_sys->sout_format.p_extra_data = NULL;
585 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
586 aout_DateSet( &p_sys->end_date, p_sys->pts );
588 p_sys->p_sout_input = sout_InputNew( p_dec, &p_sys->sout_format );
589 if( p_sys->p_sout_input == NULL )
591 msg_Err( p_dec, "cannot add a new stream" );
595 msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
596 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
599 *pp_buffer = sout_BufferNew( p_sys->p_sout_input->p_sout,
600 p_sys->i_frame_size );
601 if( *pp_buffer == NULL )
606 (*pp_buffer)->i_pts =
607 (*pp_buffer)->i_dts = aout_DateGet( &p_sys->end_date );
609 (*pp_buffer)->i_length =
610 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length )
611 - (*pp_buffer)->i_pts;
616 /*****************************************************************************
618 *****************************************************************************/
619 static int SendOutBuffer( decoder_t *p_dec )
621 decoder_sys_t *p_sys = p_dec->p_sys;
623 if( p_sys->b_packetizer )
625 sout_InputSendBuffer( p_sys->p_sout_input, p_sys->p_sout_buffer );
626 p_sys->p_sout_buffer = NULL;
630 /* We have all we need, send the buffer to the aout core. */
631 p_sys->p_aout_buffer->i_nb_bytes =
632 p_sys->i_frame_size + MAD_BUFFER_GUARD;
633 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input,
634 p_sys->p_aout_buffer );
635 p_sys->p_aout_buffer = NULL;
638 p_sys->p_out_buffer = NULL;
643 /*****************************************************************************
644 * EndDecoder: clean up the decoder
645 *****************************************************************************/
646 static int EndDecoder( decoder_t *p_dec )
648 if( p_dec->p_sys->p_aout_input != NULL )
650 if( p_dec->p_sys->p_aout_buffer )
652 aout_DecDeleteBuffer( p_dec->p_sys->p_aout,
653 p_dec->p_sys->p_aout_input,
654 p_dec->p_sys->p_aout_buffer );
657 aout_DecDelete( p_dec->p_sys->p_aout, p_dec->p_sys->p_aout_input );
660 if( p_dec->p_sys->p_sout_input != NULL )
662 if( p_dec->p_sys->p_sout_buffer )
664 sout_BufferDelete( p_dec->p_sys->p_sout_input->p_sout,
665 p_dec->p_sys->p_sout_buffer );
668 sout_InputDelete( p_dec->p_sys->p_sout_input );
671 if( p_dec->p_sys->p_chain ) block_ChainRelease( p_dec->p_sys->p_chain );
673 free( p_dec->p_sys );
678 /*****************************************************************************
679 * SyncInfo: parse MPEG audio sync info
680 *****************************************************************************/
681 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
682 unsigned int * pi_channels_conf,
683 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
684 unsigned int * pi_frame_length,
685 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
687 static const int ppi_bitrate[2][3][16] =
691 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
694 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
697 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
703 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
706 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
709 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
714 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
716 { 44100, 48000, 32000, 0 },
717 { 22050, 24000, 16000, 0 }
720 int i_version, i_mode, i_emphasis;
721 vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
722 int i_frame_size = 0;
723 int i_bitrate_index, i_samplerate_index;
726 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
727 i_version = 1 - ((i_header & 0x80000) >> 19);
728 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
729 b_crc = !((i_header >> 16) & 0x01);
730 i_bitrate_index = (i_header & 0xf000) >> 12;
731 i_samplerate_index = (i_header & 0xc00) >> 10;
732 b_padding = (i_header & 0x200) >> 9;
734 i_mode = (i_header & 0xc0) >> 6;
735 /* Modeext, copyright & original */
736 i_emphasis = i_header & 0x3;
738 if( *pi_layer != 4 &&
739 i_bitrate_index < 0x0f &&
740 i_samplerate_index != 0x03 &&
746 case 1: /* joint stereo */
748 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
750 case 2: /* dual-mono */
752 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
753 | AOUT_CHAN_DUALMONO;
757 *pi_channels_conf = AOUT_CHAN_CENTER;
760 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
761 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
762 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
766 *pi_sample_rate >>= 1;
772 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
774 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
775 *pi_sample_rate + 1 ) * 4;
776 *pi_frame_length = 384;
780 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
781 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
782 *pi_frame_length = 1152;
786 i_frame_size = ( i_version ? 72000 : 144000 ) *
787 *pi_bit_rate / *pi_sample_rate + b_padding;
788 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
789 i_max_bit_rate / *pi_sample_rate + 1;
790 *pi_frame_length = i_version ? 576 : 1152;