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.19 2003/10/05 00:50:05 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;
56 block_bytestream_t bytestream;
59 * Decoder output properties
61 aout_instance_t * p_aout; /* opaque */
62 aout_input_t * p_aout_input; /* opaque */
63 audio_sample_format_t aout_format;
64 aout_buffer_t * p_aout_buffer; /* current aout buffer being filled */
67 * Packetizer output properties
69 sout_packetizer_input_t *p_sout_input;
70 sout_format_t sout_format;
71 sout_buffer_t * p_sout_buffer; /* current sout buffer */
76 uint8_t *p_out_buffer; /* output buffer */
77 audio_date_t end_date;
78 unsigned int i_current_layer;
82 int i_frame_size, i_free_frame_size;
83 unsigned int i_channels_conf, i_channels;
84 unsigned int i_rate, i_max_frame_size, i_frame_length;
85 unsigned int i_layer, i_bit_rate;
97 /* This isn't the place to put mad-specific stuff. However, it makes the
98 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
99 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
100 * bigger buffer. This has no implication on other plug-ins, and we only
101 * lose 8 bytes per frame. --Meuuh */
102 #define MAD_BUFFER_GUARD 8
103 #define MPGA_HEADER_SIZE 4
105 /****************************************************************************
107 ****************************************************************************/
108 static int OpenDecoder ( vlc_object_t * );
109 static int OpenPacketizer( vlc_object_t * );
111 static int InitDecoder ( decoder_t * );
112 static int RunDecoder ( decoder_t *, block_t * );
113 static int EndDecoder ( decoder_t * );
115 static int GetOutBuffer ( decoder_t *, uint8_t ** );
116 static int GetAoutBuffer( decoder_t *, aout_buffer_t ** );
117 static int GetSoutBuffer( decoder_t *, sout_buffer_t ** );
118 static int SendOutBuffer( decoder_t * );
120 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
121 unsigned int * pi_channels_conf,
122 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
123 unsigned int * pi_frame_length,
124 unsigned int * pi_max_frame_size,
125 unsigned int * pi_layer );
127 /*****************************************************************************
129 *****************************************************************************/
131 set_description( _("MPEG audio layer I/II/III parser") );
132 set_capability( "decoder", 100 );
133 set_callbacks( OpenDecoder, NULL );
136 set_description( _("MPEG audio layer I/II/III packetizer") );
137 set_capability( "packetizer", 10 );
138 set_callbacks( OpenPacketizer, NULL );
141 /*****************************************************************************
142 * OpenDecoder: probe the decoder and return score
143 *****************************************************************************/
144 static int OpenDecoder( vlc_object_t *p_this )
146 decoder_t *p_dec = (decoder_t*)p_this;
148 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
153 p_dec->pf_init = InitDecoder;
154 p_dec->pf_decode = RunDecoder;
155 p_dec->pf_end = EndDecoder;
157 /* Allocate the memory needed to store the decoder's structure */
159 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
161 msg_Err( p_dec, "out of memory" );
164 p_dec->p_sys->b_packetizer = VLC_FALSE;
169 static int OpenPacketizer( vlc_object_t *p_this )
171 decoder_t *p_dec = (decoder_t*)p_this;
173 int i_ret = OpenDecoder( p_this );
175 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
180 /*****************************************************************************
181 * InitDecoder: Initalize the decoder
182 *****************************************************************************/
183 static int InitDecoder( decoder_t *p_dec )
185 p_dec->p_sys->i_state = STATE_NOSYNC;
186 p_dec->p_sys->b_synchro = VLC_FALSE;
188 p_dec->p_sys->p_out_buffer = NULL;
189 aout_DateSet( &p_dec->p_sys->end_date, 0 );
191 p_dec->p_sys->p_aout = NULL;
192 p_dec->p_sys->p_aout_input = NULL;
193 p_dec->p_sys->p_aout_buffer = NULL;
194 p_dec->p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
196 p_dec->p_sys->p_sout_input = NULL;
197 p_dec->p_sys->p_sout_buffer = NULL;
198 p_dec->p_sys->sout_format.i_cat = AUDIO_ES;
199 p_dec->p_sys->sout_format.i_fourcc = VLC_FOURCC('m','p','g','a');
201 /* Start with the minimum size for a free bitrate frame */
202 p_dec->p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
204 p_dec->p_sys->p_chain = NULL;
209 /****************************************************************************
210 * RunDecoder: the whole thing
211 ****************************************************************************
212 * This function is called just after the thread is launched.
213 ****************************************************************************/
214 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
216 decoder_sys_t *p_sys = p_dec->p_sys;
217 uint8_t p_header[MAD_BUFFER_GUARD];
220 if( (!aout_DateGet( &p_sys->end_date ) && !p_block->i_pts)
221 || p_block->b_discontinuity )
223 /* We've just started the stream, wait for the first PTS. */
224 block_Release( p_block );
225 p_sys->b_synchro = VLC_FALSE;
231 block_ChainAppend( &p_sys->p_chain, p_block );
235 block_ChainAppend( &p_sys->p_chain, p_block );
236 p_sys->bytestream = block_BytestreamInit( p_dec, p_sys->p_chain, 0 );
241 switch( p_sys->i_state )
245 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
248 /* Look for sync word - should be 0xffe */
249 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
251 p_sys->i_state = STATE_SYNC;
254 block_SkipByte( &p_sys->bytestream );
255 p_sys->b_synchro = VLC_FALSE;
257 if( p_sys->i_state != STATE_SYNC )
259 block_ChainRelease( p_sys->p_chain );
260 p_sys->p_chain = NULL;
267 /* New frame, set the Presentation Time Stamp */
268 p_sys->pts = p_sys->bytestream.p_block->i_pts;
269 if( p_sys->pts != 0 &&
270 p_sys->pts != aout_DateGet( &p_sys->end_date ) )
272 aout_DateSet( &p_sys->end_date, p_sys->pts );
274 p_sys->i_state = STATE_HEADER;
278 /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
279 if( block_PeekBytes( &p_sys->bytestream, p_header,
280 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
286 /* Build frame header */
287 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
290 /* Check if frame is valid and get frame info */
291 p_sys->i_frame_size = SyncInfo( i_header,
293 &p_sys->i_channels_conf,
296 &p_sys->i_frame_length,
297 &p_sys->i_max_frame_size,
300 if( p_sys->i_frame_size == -1 )
302 msg_Dbg( p_dec, "emulated start code" );
303 block_SkipByte( &p_sys->bytestream );
304 p_sys->i_state = STATE_NOSYNC;
305 p_sys->b_synchro = VLC_FALSE;
309 if( p_sys->i_bit_rate == 0 )
311 /* Free birate, but 99% emulated startcode :( */
312 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
314 msg_Dbg( p_dec, "free bitrate mode");
316 p_sys->i_frame_size = p_sys->i_free_frame_size;
319 p_sys->i_state = STATE_NEXT_SYNC;
321 case STATE_NEXT_SYNC:
322 /* TODO: If p_block == NULL, flush the buffer without checking the
325 /* Check if next expected frame contains the sync word */
326 if( block_PeekOffsetBytes( &p_sys->bytestream,
327 p_sys->i_frame_size, p_header,
328 MAD_BUFFER_GUARD ) != VLC_SUCCESS )
334 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
336 /* Startcode is fine, let's try the header as an extra check */
337 int i_next_frame_size;
338 unsigned int i_next_channels, i_next_channels_conf;
339 unsigned int i_next_rate, i_next_bit_rate;
340 unsigned int i_next_frame_length, i_next_max_frame_size;
341 unsigned int i_next_layer;
343 /* Build frame header */
344 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
347 i_next_frame_size = SyncInfo( i_header,
349 &i_next_channels_conf,
352 &i_next_frame_length,
353 &i_next_max_frame_size,
356 if( i_next_frame_size == -1 )
358 msg_Dbg( p_dec, "emulated start code on next frame" );
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++;
373 if( (unsigned int)p_sys->i_frame_size >
374 p_sys->i_max_frame_size )
376 msg_Dbg( p_dec, "frame too big %d > %d "
377 "(emulated startcode ?)", p_sys->i_frame_size,
378 p_sys->i_max_frame_size );
379 block_SkipByte( &p_sys->bytestream );
380 p_sys->i_state = STATE_NOSYNC;
381 p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
386 /* Check info is in sync with previous one */
387 if( i_next_channels_conf != p_sys->i_channels_conf ||
388 i_next_rate != p_sys->i_rate ||
389 i_next_layer != p_sys->i_layer ||
390 i_next_frame_length != p_sys->i_frame_length )
392 /* Free bitrate only */
393 if( p_sys->i_bit_rate == 0 )
395 p_sys->i_frame_size++;
399 msg_Dbg( p_dec, "parameters changed unexpectedly "
400 "(emulated startcode ?)" );
401 block_SkipByte( &p_sys->bytestream );
402 p_sys->i_state = STATE_NOSYNC;
403 p_sys->b_synchro = VLC_FALSE;
409 /* Free bitrate only */
410 if( p_sys->i_bit_rate == 0 )
412 p_sys->i_frame_size++;
416 if( !p_sys->b_synchro )
418 msg_Dbg( p_dec, "emulated startcode "
419 "(no startcode on following frame)" );
420 p_sys->i_state = STATE_NOSYNC;
421 block_SkipByte( &p_sys->bytestream );
426 if( GetOutBuffer( p_dec, &p_sys->p_out_buffer ) != VLC_SUCCESS )
431 /* Free bitrate only */
432 if( p_sys->i_bit_rate == 0 )
434 p_sys->i_free_frame_size = p_sys->i_frame_size;
437 p_sys->i_state = STATE_DATA;
440 /* Copy the whole frame into the buffer */
441 if( block_GetBytes( &p_sys->bytestream, p_sys->p_out_buffer,
442 p_sys->i_frame_size ) != VLC_SUCCESS )
448 p_sys->p_chain = block_BytestreamFlush( &p_sys->bytestream );
450 /* Get beginning of next frame for libmad */
451 if( !p_sys->b_packetizer )
453 memcpy( p_sys->p_out_buffer + p_sys->i_frame_size,
454 p_header, MAD_BUFFER_GUARD );
457 SendOutBuffer( p_dec );
458 p_sys->i_state = STATE_NOSYNC;
459 p_sys->b_synchro = VLC_TRUE;
461 /* Make sure we don't reuse the same pts twice */
462 if( p_sys->pts == p_sys->bytestream.p_block->i_pts )
463 p_sys->pts = p_sys->bytestream.p_block->i_pts = 0;
470 /*****************************************************************************
472 *****************************************************************************/
473 static int GetOutBuffer( decoder_t *p_dec, uint8_t **pp_out_buffer )
475 decoder_sys_t *p_sys = p_dec->p_sys;
478 if( p_sys->b_packetizer )
480 i_ret = GetSoutBuffer( p_dec, &p_sys->p_sout_buffer );
482 p_sys->p_sout_buffer ? p_sys->p_sout_buffer->p_buffer : NULL;
486 i_ret = GetAoutBuffer( p_dec, &p_sys->p_aout_buffer );
488 p_sys->p_aout_buffer ? p_sys->p_aout_buffer->p_buffer : NULL;
494 /*****************************************************************************
496 *****************************************************************************/
497 static int GetAoutBuffer( decoder_t *p_dec, aout_buffer_t **pp_buffer )
499 decoder_sys_t *p_sys = p_dec->p_sys;
501 if( p_sys->p_aout_input != NULL &&
502 ( p_sys->aout_format.i_rate != p_sys->i_rate
503 || p_sys->aout_format.i_original_channels != p_sys->i_channels_conf
504 || p_sys->aout_format.i_bytes_per_frame !=
505 p_sys->i_max_frame_size + MAD_BUFFER_GUARD
506 || p_sys->aout_format.i_frame_length != p_sys->i_frame_length
507 || p_sys->i_current_layer != p_sys->i_layer ) )
509 /* Parameters changed - this should not happen. */
510 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
511 p_sys->p_aout_input = NULL;
514 /* Creating the audio input if not created yet. */
515 if( p_sys->p_aout_input == NULL )
517 p_sys->i_current_layer = p_sys->i_layer;
518 if( p_sys->i_layer == 3 )
520 p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','3');
524 p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
527 p_sys->aout_format.i_rate = p_sys->i_rate;
528 p_sys->aout_format.i_original_channels = p_sys->i_channels_conf;
529 p_sys->aout_format.i_physical_channels
530 = p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
531 p_sys->aout_format.i_bytes_per_frame = p_sys->i_max_frame_size
533 p_sys->aout_format.i_frame_length = p_sys->i_frame_length;
534 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
535 aout_DateSet( &p_sys->end_date, p_sys->pts );
536 p_sys->p_aout_input = aout_DecNew( p_dec,
538 &p_sys->aout_format );
539 if( p_sys->p_aout_input == NULL )
546 *pp_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
547 p_sys->i_frame_length );
548 if( *pp_buffer == NULL )
553 (*pp_buffer)->start_date = aout_DateGet( &p_sys->end_date );
554 (*pp_buffer)->end_date =
555 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
560 /*****************************************************************************
562 *****************************************************************************/
563 static int GetSoutBuffer( decoder_t *p_dec, sout_buffer_t **pp_buffer )
565 decoder_sys_t *p_sys = p_dec->p_sys;
567 if( p_sys->p_sout_input != NULL &&
568 ( p_sys->sout_format.i_sample_rate != (int)p_sys->i_rate
569 || p_sys->sout_format.i_channels != (int)p_sys->i_channels ) )
571 /* Parameters changed - this should not happen. */
574 /* Creating the sout input if not created yet. */
575 if( p_sys->p_sout_input == NULL )
577 p_sys->sout_format.i_sample_rate = p_sys->i_rate;
578 p_sys->sout_format.i_channels = p_sys->i_channels;
579 p_sys->sout_format.i_block_align = 0;
580 p_sys->sout_format.i_bitrate = p_sys->i_bit_rate;
581 p_sys->sout_format.i_extra_data = 0;
582 p_sys->sout_format.p_extra_data = NULL;
584 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
585 aout_DateSet( &p_sys->end_date, p_sys->pts );
587 p_sys->p_sout_input = sout_InputNew( p_dec, &p_sys->sout_format );
588 if( p_sys->p_sout_input == NULL )
590 msg_Err( p_dec, "cannot add a new stream" );
594 msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
595 p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
598 *pp_buffer = sout_BufferNew( p_sys->p_sout_input->p_sout,
599 p_sys->i_frame_size );
600 if( *pp_buffer == NULL )
605 (*pp_buffer)->i_pts =
606 (*pp_buffer)->i_dts = aout_DateGet( &p_sys->end_date );
608 (*pp_buffer)->i_length =
609 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length )
610 - (*pp_buffer)->i_pts;
615 /*****************************************************************************
617 *****************************************************************************/
618 static int SendOutBuffer( decoder_t *p_dec )
620 decoder_sys_t *p_sys = p_dec->p_sys;
622 if( p_sys->b_packetizer )
624 sout_InputSendBuffer( p_sys->p_sout_input, p_sys->p_sout_buffer );
625 p_sys->p_sout_buffer = NULL;
629 /* We have all we need, send the buffer to the aout core. */
630 p_sys->p_aout_buffer->i_nb_bytes =
631 p_sys->i_frame_size + MAD_BUFFER_GUARD;
632 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input,
633 p_sys->p_aout_buffer );
634 p_sys->p_aout_buffer = NULL;
637 p_sys->p_out_buffer = NULL;
642 /*****************************************************************************
643 * EndDecoder: clean up the decoder
644 *****************************************************************************/
645 static int EndDecoder( decoder_t *p_dec )
647 if( p_dec->p_sys->p_aout_input != NULL )
649 if( p_dec->p_sys->p_aout_buffer )
651 aout_DecDeleteBuffer( p_dec->p_sys->p_aout,
652 p_dec->p_sys->p_aout_input,
653 p_dec->p_sys->p_aout_buffer );
656 aout_DecDelete( p_dec->p_sys->p_aout, p_dec->p_sys->p_aout_input );
659 if( p_dec->p_sys->p_sout_input != NULL )
661 if( p_dec->p_sys->p_sout_buffer )
663 sout_BufferDelete( p_dec->p_sys->p_sout_input->p_sout,
664 p_dec->p_sys->p_sout_buffer );
667 sout_InputDelete( p_dec->p_sys->p_sout_input );
670 if( p_dec->p_sys->p_chain ) block_ChainRelease( p_dec->p_sys->p_chain );
672 free( p_dec->p_sys );
677 /*****************************************************************************
678 * SyncInfo: parse MPEG audio sync info
679 *****************************************************************************/
680 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
681 unsigned int * pi_channels_conf,
682 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
683 unsigned int * pi_frame_length,
684 unsigned int * pi_max_frame_size, unsigned int * pi_layer)
686 static const int ppi_bitrate[2][3][16] =
690 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
693 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
696 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
702 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
705 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
708 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
713 static const int ppi_samplerate[2][4] = /* version 1 then 2 */
715 { 44100, 48000, 32000, 0 },
716 { 22050, 24000, 16000, 0 }
719 int i_version, i_mode, i_emphasis;
720 vlc_bool_t b_padding, b_mpeg_2_5;
721 int i_frame_size = 0;
722 int i_bitrate_index, i_samplerate_index;
725 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
726 i_version = 1 - ((i_header & 0x80000) >> 19);
727 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
729 i_bitrate_index = (i_header & 0xf000) >> 12;
730 i_samplerate_index = (i_header & 0xc00) >> 10;
731 b_padding = (i_header & 0x200) >> 9;
733 i_mode = (i_header & 0xc0) >> 6;
734 /* Modeext, copyright & original */
735 i_emphasis = i_header & 0x3;
737 if( *pi_layer != 4 &&
738 i_bitrate_index < 0x0f &&
739 i_samplerate_index != 0x03 &&
745 case 1: /* joint stereo */
747 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
749 case 2: /* dual-mono */
751 *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
752 | AOUT_CHAN_DUALMONO;
756 *pi_channels_conf = AOUT_CHAN_CENTER;
759 *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
760 i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
761 *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
765 *pi_sample_rate >>= 1;
771 i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
773 *pi_max_frame_size = ( 12000 * i_max_bit_rate /
774 *pi_sample_rate + 1 ) * 4;
775 *pi_frame_length = 384;
779 i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
780 *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
781 *pi_frame_length = 1152;
785 i_frame_size = ( i_version ? 72000 : 144000 ) *
786 *pi_bit_rate / *pi_sample_rate + b_padding;
787 *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
788 i_max_bit_rate / *pi_sample_rate + 1;
789 *pi_frame_length = i_version ? 576 : 1152;