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.1 2003/01/15 10:58:47 massiot Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <string.h> /* strdup() */
34 #include <vlc/decoder.h>
36 /*****************************************************************************
37 * dec_thread_t : decoder thread descriptor
38 *****************************************************************************/
39 typedef struct dec_thread_t
44 vlc_thread_t thread_id; /* id for thread functions */
49 decoder_fifo_t * p_fifo; /* stores the PES stream data */
50 bit_stream_t bit_stream;
55 aout_instance_t * p_aout; /* opaque */
56 aout_input_t * p_aout_input; /* opaque */
57 audio_sample_format_t output_format;
60 #define MAX_FRAME_SIZE (511 + 2048)
61 /* This isn't the place to put mad-specific stuff. However, it makes the
62 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
63 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
64 * bigger buffer. This has no implication on other plug-ins, and we only
65 * lose 8 bytes per frame. --Meuuh */
66 #define MAD_BUFFER_GUARD 8
69 /****************************************************************************
71 ****************************************************************************/
72 static int Open ( vlc_object_t * );
73 static int RunDecoder ( decoder_fifo_t * );
75 static void EndThread ( dec_thread_t * );
77 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
78 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
79 unsigned int * pi_frame_length,
80 unsigned int * pi_current_frame_length,
81 unsigned int * pi_layer );
83 /*****************************************************************************
85 *****************************************************************************/
87 set_description( _("MPEG audio layer I/II/III parser") );
88 set_capability( "decoder", 100 );
89 set_callbacks( Open, NULL );
92 /*****************************************************************************
93 * OpenDecoder: probe the decoder and return score
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
99 if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
104 p_fifo->pf_run = RunDecoder;
108 /*****************************************************************************
109 * RunDecoder: this function is called just after the thread is created
110 *****************************************************************************/
111 static int RunDecoder( decoder_fifo_t *p_fifo )
113 dec_thread_t * p_dec;
114 audio_date_t end_date;
115 unsigned int i_layer = 0;
117 /* Allocate the memory needed to store the thread's structure */
118 p_dec = malloc( sizeof(dec_thread_t) );
121 msg_Err( p_fifo, "out of memory" );
122 DecoderError( p_fifo );
126 /* Initialize the thread properties */
127 p_dec->p_aout = NULL;
128 p_dec->p_aout_input = NULL;
129 p_dec->p_fifo = p_fifo;
131 aout_DateSet( &end_date, 0 );
133 /* Init the bitstream */
134 if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
135 NULL, NULL ) != VLC_SUCCESS )
137 msg_Err( p_fifo, "cannot initialize bitstream" );
138 DecoderError( p_fifo );
143 /* Decoder thread's main loop */
144 while ( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
147 unsigned int i_rate, i_original_channels, i_frame_size, i_frame_length;
148 unsigned int i_new_layer, i_current_frame_size;
151 aout_buffer_t * p_buffer;
153 /* Look for sync word - should be 0xfff */
154 RealignBits( &p_dec->bit_stream );
155 if ( ShowBits( &p_dec->bit_stream, 11 ) != 0x07ff )
157 msg_Warn( p_dec->p_fifo, "no sync - skipping" );
159 while ( ShowBits( &p_dec->bit_stream, 11 ) != 0x07ff &&
160 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
162 RemoveBits( &p_dec->bit_stream, 8 );
164 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) break;
166 /* Set the Presentation Time Stamp */
167 NextPTS( &p_dec->bit_stream, &pts, NULL );
168 if ( pts != 0 && pts != aout_DateGet( &end_date ) )
170 aout_DateSet( &end_date, pts );
173 /* Get frame header */
174 i_header = ShowBits( &p_dec->bit_stream, 32 );
175 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) break;
177 /* Check if frame is valid and get frame info */
178 i_current_frame_size = SyncInfo( i_header,
179 &i_original_channels, &i_rate,
180 &i_bit_rate, &i_frame_length,
181 &i_frame_size, &i_new_layer );
183 if( !i_current_frame_size )
185 msg_Warn( p_dec->p_fifo, "syncinfo failed" );
189 if( (p_dec->p_aout_input != NULL) &&
190 ( (p_dec->output_format.i_rate != i_rate)
191 || (p_dec->output_format.i_original_channels
192 != i_original_channels)
193 || (p_dec->output_format.i_bytes_per_frame
194 != i_frame_size + MAD_BUFFER_GUARD)
195 || (p_dec->output_format.i_frame_length != i_frame_length)
196 || (i_layer != i_new_layer) ) )
198 /* Parameters changed - this should not happen. */
199 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
200 p_dec->p_aout_input = NULL;
203 /* Creating the audio input if not created yet. */
204 if( p_dec->p_aout_input == NULL )
206 i_layer = i_new_layer;
209 p_dec->output_format.i_format = VLC_FOURCC('m','p','g','3');
213 p_dec->output_format.i_format = VLC_FOURCC('m','p','g','a');
215 p_dec->output_format.i_rate = i_rate;
216 p_dec->output_format.i_original_channels = i_original_channels;
217 p_dec->output_format.i_physical_channels
218 = i_original_channels & AOUT_CHAN_PHYSMASK;
219 p_dec->output_format.i_bytes_per_frame = i_frame_size
221 p_dec->output_format.i_frame_length = i_frame_length;
222 aout_DateInit( &end_date, i_rate );
223 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
225 &p_dec->output_format );
227 if ( p_dec->p_aout_input == NULL )
229 p_dec->p_fifo->b_error = 1;
234 if ( !aout_DateGet( &end_date ) )
236 byte_t p_junk[MAX_FRAME_SIZE];
238 /* We've just started the stream, wait for the first PTS. */
239 GetChunk( &p_dec->bit_stream, p_junk, i_frame_size );
243 p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
245 if ( p_buffer == NULL )
247 p_dec->p_fifo->b_error = 1;
250 p_buffer->start_date = aout_DateGet( &end_date );
251 p_buffer->end_date = aout_DateIncrement( &end_date,
254 /* Get the whole frame. */
255 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer,
256 i_current_frame_size );
257 if( p_dec->p_fifo->b_die )
259 aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
264 /* Send the buffer to the aout core. */
265 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
268 if( p_dec->p_fifo->b_error )
270 DecoderError( p_dec->p_fifo );
278 /*****************************************************************************
279 * EndThread : thread destruction
280 *****************************************************************************/
281 static void EndThread( dec_thread_t * p_dec )
283 if ( p_dec->p_aout_input != NULL )
285 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
288 CloseBitstream( &p_dec->bit_stream );
292 /*****************************************************************************
293 * SyncInfo: parse MPEG audio sync info
294 *****************************************************************************/
295 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
296 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
297 unsigned int * pi_frame_length,
298 unsigned int * pi_frame_size, unsigned int * pi_layer )
300 static const int pppi_mpegaudio_bitrate[2][3][16] =
304 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
307 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
310 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
316 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
319 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
322 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
327 static const int ppi_mpegaudio_samplerate[2][4] = /* version 1 then 2 */
329 { 44100, 48000, 32000, 0 },
330 { 22050, 24000, 16000, 0 }
333 int i_version, i_mode, i_emphasis;
334 vlc_bool_t b_padding, b_mpeg_2_5;
335 int i_current_frame_size = 0;
336 int i_bitrate_index, i_samplerate_index;
338 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
339 i_version = 1 - ((i_header & 0x80000) >> 19);
340 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
342 i_bitrate_index = (i_header & 0xf000) >> 12;
343 i_samplerate_index = (i_header & 0xc00) >> 10;
344 b_padding = (i_header & 0x200) >> 9;
346 i_mode = (i_header & 0xc0) >> 6;
347 /* Modeext, copyright & original */
348 i_emphasis = i_header & 0x3;
350 if( *pi_layer != 4 &&
351 i_bitrate_index > 0x00 && i_bitrate_index < 0x0f &&
352 i_samplerate_index != 0x03 &&
358 case 1: /* joint stereo */
359 *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
361 case 2: /* dual-mono */
362 *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
363 | AOUT_CHAN_DUALMONO;
366 *pi_channels = AOUT_CHAN_CENTER;
369 *pi_bit_rate = pppi_mpegaudio_bitrate[i_version][*pi_layer-1][i_bitrate_index];
370 *pi_sample_rate = ppi_mpegaudio_samplerate[i_version][i_samplerate_index];
374 *pi_sample_rate >>= 1;
377 switch( *pi_layer -1 )
380 i_current_frame_size = ( ( i_version ? 6000 : 12000 ) *
381 *pi_bit_rate / *pi_sample_rate
383 *pi_frame_size = ( ( i_version ? 6000 : 12000 ) *
384 *pi_bit_rate / *pi_sample_rate + 1 ) * 4;
385 *pi_frame_length = 384;
389 i_current_frame_size = ( i_version ? 72000 : 144000 ) *
390 *pi_bit_rate / *pi_sample_rate
392 *pi_frame_size = ( i_version ? 72000 : 144000 ) *
393 *pi_bit_rate / *pi_sample_rate + 1;
394 *pi_frame_length = 1152;
398 i_current_frame_size = ( i_version ? 72000 : 144000 ) *
399 *pi_bit_rate / *pi_sample_rate
401 *pi_frame_size = ( i_version ? 72000 : 144000 ) *
402 *pi_bit_rate / *pi_sample_rate + 1;
403 *pi_frame_length = i_version ? 576 : 1152;
407 i_current_frame_size = *pi_frame_size = 0;
408 *pi_frame_length = 0;
412 return i_current_frame_size;