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.11 2003/02/18 00:51:40 fenrir 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 10000
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;
116 byte_t p_sync[MAD_BUFFER_GUARD];
118 int i_free_frame_size = 0;
120 /* Allocate the memory needed to store the thread's structure */
121 p_dec = malloc( sizeof(dec_thread_t) );
124 msg_Err( p_fifo, "out of memory" );
125 DecoderError( p_fifo );
129 /* Initialize the thread properties */
130 p_dec->p_aout = NULL;
131 p_dec->p_aout_input = NULL;
132 p_dec->p_fifo = p_fifo;
134 aout_DateSet( &end_date, 0 );
136 /* Init the bitstream */
137 if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
138 NULL, NULL ) != VLC_SUCCESS )
140 msg_Err( p_fifo, "cannot initialize bitstream" );
141 DecoderError( p_fifo );
146 /* Init sync buffer. */
147 NextPTS( &p_dec->bit_stream, &pts, NULL );
148 GetChunk( &p_dec->bit_stream, p_sync, MAD_BUFFER_GUARD );
150 /* Decoder thread's main loop */
151 while ( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
153 int i_current_frame_size;
154 unsigned int i_rate, i_original_channels, i_frame_size, i_frame_length;
155 unsigned int i_new_layer, i_bit_rate;
157 aout_buffer_t * p_buffer;
160 /* Look for sync word - should be 0xffe */
161 if ( (p_sync[0] != 0xff) || ((p_sync[1] & 0xe0) != 0xe0) )
163 msg_Warn( p_dec->p_fifo, "no sync - skipping" );
164 /* Look inside the sync buffer. */
165 for ( i = 1; i < MAD_BUFFER_GUARD - 1; i++ )
167 if ( (p_sync[i] == 0xff) && ((p_sync[i + 1] & 0xe0) != 0xe0) )
170 if ( i < MAD_BUFFER_GUARD - 1 )
173 memmove( p_sync, &p_sync[i], MAD_BUFFER_GUARD - i );
174 GetChunk( &p_dec->bit_stream, &p_sync[MAD_BUFFER_GUARD - i],
179 if ( p_sync[MAD_BUFFER_GUARD - 1] == 0xff
180 && ShowBits( &p_dec->bit_stream, 3 ) == 0x3 )
183 p_sync[0] = p_sync[MAD_BUFFER_GUARD - 1];
184 GetChunk( &p_dec->bit_stream,
185 &p_sync[1], MAD_BUFFER_GUARD - 1 );
189 /* Scan the stream. */
190 while ( ShowBits( &p_dec->bit_stream, 11 ) != 0x07ff &&
191 (!p_dec->p_fifo->b_die) &&
192 (!p_dec->p_fifo->b_error) )
194 RemoveBits( &p_dec->bit_stream, 8 );
196 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
198 NextPTS( &p_dec->bit_stream, &pts, NULL );
199 GetChunk( &p_dec->bit_stream,p_sync, MAD_BUFFER_GUARD );
203 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) break;
205 /* Set the Presentation Time Stamp */
206 fprintf( stderr, "mpgadec pts:%lld\n", pts );
207 if ( pts != 0 && pts != aout_DateGet( &end_date ) )
209 aout_DateSet( &end_date, pts );
212 /* Get frame header */
213 i_header = (p_sync[0] << 24) | (p_sync[1] << 16) | (p_sync[2] << 8)
216 /* Check if frame is valid and get frame info */
217 i_current_frame_size = SyncInfo( i_header,
218 &i_original_channels, &i_rate,
219 &i_bit_rate, &i_frame_length,
220 &i_frame_size, &i_new_layer );
221 if ( !i_current_frame_size )
222 i_current_frame_size = i_free_frame_size;
224 if ( i_current_frame_size == -1 )
226 msg_Warn( p_dec->p_fifo, "syncinfo failed" );
227 /* This is probably an emulated startcode, drop the first byte
228 * to force looking for the next startcode. */
229 memmove( p_sync, &p_sync[1], MAD_BUFFER_GUARD - 1 );
230 p_sync[MAD_BUFFER_GUARD - 1] = GetBits( &p_dec->bit_stream, 8 );
233 if ( (unsigned int)i_current_frame_size > i_frame_size )
235 msg_Warn( p_dec->p_fifo, "frame too big %d > %d",
236 i_current_frame_size, i_frame_size );
237 memmove( p_sync, &p_sync[1], MAD_BUFFER_GUARD - 1 );
238 p_sync[MAD_BUFFER_GUARD - 1] = GetBits( &p_dec->bit_stream, 8 );
242 if( (p_dec->p_aout_input != NULL) &&
243 ( (p_dec->output_format.i_rate != i_rate)
244 || (p_dec->output_format.i_original_channels
245 != i_original_channels)
246 || (p_dec->output_format.i_bytes_per_frame
247 != i_frame_size + MAD_BUFFER_GUARD)
248 || (p_dec->output_format.i_frame_length != i_frame_length)
249 || (i_layer != i_new_layer) ) )
251 /* Parameters changed - this should not happen. */
252 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
253 p_dec->p_aout_input = NULL;
256 /* Creating the audio input if not created yet. */
257 if( p_dec->p_aout_input == NULL )
259 i_layer = i_new_layer;
262 p_dec->output_format.i_format = VLC_FOURCC('m','p','g','3');
266 p_dec->output_format.i_format = VLC_FOURCC('m','p','g','a');
268 p_dec->output_format.i_rate = i_rate;
269 p_dec->output_format.i_original_channels = i_original_channels;
270 p_dec->output_format.i_physical_channels
271 = i_original_channels & AOUT_CHAN_PHYSMASK;
272 p_dec->output_format.i_bytes_per_frame = i_frame_size
274 p_dec->output_format.i_frame_length = i_frame_length;
275 aout_DateInit( &end_date, i_rate );
276 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
278 &p_dec->output_format );
280 if ( p_dec->p_aout_input == NULL )
282 p_dec->p_fifo->b_error = 1;
287 if ( !aout_DateGet( &end_date ) )
289 byte_t p_junk[MAX_FRAME_SIZE];
290 int i_skip = i_current_frame_size - MAD_BUFFER_GUARD;
292 fprintf( stderr, "waiting pts\n" );
293 /* We've just started the stream, wait for the first PTS. */
298 i_read = __MIN( i_current_frame_size - MAD_BUFFER_GUARD, MAX_FRAME_SIZE );
299 GetChunk( &p_dec->bit_stream, p_junk, i_read );
303 NextPTS( &p_dec->bit_stream, &pts, NULL );
304 GetChunk( &p_dec->bit_stream, p_sync, MAD_BUFFER_GUARD );
308 p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
310 if ( p_buffer == NULL )
312 p_dec->p_fifo->b_error = 1;
315 p_buffer->start_date = aout_DateGet( &end_date );
316 p_buffer->end_date = aout_DateIncrement( &end_date,
319 /* Get the whole frame. */
320 memcpy( p_buffer->p_buffer, p_sync, MAD_BUFFER_GUARD );
321 if ( i_current_frame_size )
323 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + MAD_BUFFER_GUARD,
324 i_current_frame_size - MAD_BUFFER_GUARD );
328 /* Free bit-rate stream. Peek at next frame header. */
329 i = MAD_BUFFER_GUARD;
332 int i_next_real_frame_size;
333 unsigned int i_next_channels, i_next_rate, i_next_bit_rate;
334 unsigned int i_next_frame_length, i_next_frame_size;
335 unsigned int i_next_layer;
336 while ( ShowBits( &p_dec->bit_stream, 11 ) != 0x07ff &&
337 (!p_dec->p_fifo->b_die) &&
338 (!p_dec->p_fifo->b_error) &&
339 i < (int)i_frame_size + MAD_BUFFER_GUARD )
341 ((uint8_t *)p_buffer->p_buffer)[i++] =
342 GetBits( &p_dec->bit_stream, 8 );
344 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error
345 || i == (int)i_frame_size + MAD_BUFFER_GUARD )
347 i_header = ShowBits( &p_dec->bit_stream, 8 );
348 i_next_real_frame_size = SyncInfo( i_header,
349 &i_next_channels, &i_next_rate,
350 &i_next_bit_rate, &i_next_frame_length,
351 &i_next_frame_size, &i_next_layer );
352 if ( i_next_real_frame_size != 0 ||
353 i_next_channels != i_original_channels ||
354 i_next_rate != i_rate ||
355 i_next_bit_rate != i_bit_rate ||
356 i_next_frame_length != i_frame_length ||
357 i_next_frame_size != i_frame_size ||
358 i_next_layer != i_new_layer )
360 /* This is an emulated start code, try again. */
363 i_free_frame_size = i;
366 if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
368 if ( i == (int)i_frame_size + MAD_BUFFER_GUARD )
370 /* Couldn't find the next start-code. This is sooo
372 aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
374 NextPTS( &p_dec->bit_stream, &pts, NULL );
375 GetChunk( &p_dec->bit_stream, p_sync, MAD_BUFFER_GUARD );
379 if( p_dec->p_fifo->b_die )
381 aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
385 /* Get beginning of next frame. */
386 NextPTS( &p_dec->bit_stream, &pts, NULL );
387 GetChunk( &p_dec->bit_stream, p_buffer->p_buffer + i_current_frame_size,
389 memcpy( p_sync, p_buffer->p_buffer + i_current_frame_size,
392 p_buffer->i_nb_bytes = i_current_frame_size + MAD_BUFFER_GUARD;
394 /* Send the buffer to the aout core. */
395 fprintf( stderr, "sending.." );
396 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
397 fprintf( stderr, "..done\n" );
400 if( p_dec->p_fifo->b_error )
402 DecoderError( p_dec->p_fifo );
410 /*****************************************************************************
411 * EndThread : thread destruction
412 *****************************************************************************/
413 static void EndThread( dec_thread_t * p_dec )
415 if ( p_dec->p_aout_input != NULL )
417 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
420 CloseBitstream( &p_dec->bit_stream );
424 /*****************************************************************************
425 * SyncInfo: parse MPEG audio sync info
426 *****************************************************************************/
427 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
428 unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
429 unsigned int * pi_frame_length,
430 unsigned int * pi_frame_size, unsigned int * pi_layer )
432 static const int pppi_mpegaudio_bitrate[2][3][16] =
436 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
439 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
442 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
448 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192,
451 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
454 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128,
459 static const int ppi_mpegaudio_samplerate[2][4] = /* version 1 then 2 */
461 { 44100, 48000, 32000, 0 },
462 { 22050, 24000, 16000, 0 }
465 int i_version, i_mode, i_emphasis;
466 vlc_bool_t b_padding, b_mpeg_2_5;
467 int i_current_frame_size = 0;
468 int i_bitrate_index, i_samplerate_index;
471 b_mpeg_2_5 = 1 - ((i_header & 0x100000) >> 20);
472 i_version = 1 - ((i_header & 0x80000) >> 19);
473 *pi_layer = 4 - ((i_header & 0x60000) >> 17);
475 i_bitrate_index = (i_header & 0xf000) >> 12;
476 i_samplerate_index = (i_header & 0xc00) >> 10;
477 b_padding = (i_header & 0x200) >> 9;
479 i_mode = (i_header & 0xc0) >> 6;
480 /* Modeext, copyright & original */
481 i_emphasis = i_header & 0x3;
483 if( *pi_layer != 4 &&
484 i_bitrate_index < 0x0f &&
485 i_samplerate_index != 0x03 &&
491 case 1: /* joint stereo */
492 *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
494 case 2: /* dual-mono */
495 *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
496 | AOUT_CHAN_DUALMONO;
499 *pi_channels = AOUT_CHAN_CENTER;
502 *pi_bit_rate = pppi_mpegaudio_bitrate[i_version][*pi_layer-1][i_bitrate_index];
503 i_max_bit_rate = pppi_mpegaudio_bitrate[i_version][*pi_layer-1][14];
504 *pi_sample_rate = ppi_mpegaudio_samplerate[i_version][i_samplerate_index];
508 *pi_sample_rate >>= 1;
514 i_current_frame_size = ( 12000 *
515 *pi_bit_rate / *pi_sample_rate
517 *pi_frame_size = ( 12000 *
518 i_max_bit_rate / *pi_sample_rate + 1 ) * 4;
519 *pi_frame_length = 384;
523 i_current_frame_size = 144000 *
524 *pi_bit_rate / *pi_sample_rate
526 *pi_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
527 *pi_frame_length = 1152;
531 i_current_frame_size = ( i_version ? 72000 : 144000 ) *
532 *pi_bit_rate / *pi_sample_rate + b_padding;
533 *pi_frame_size = ( i_version ? 72000 : 144000 ) *
534 i_max_bit_rate / *pi_sample_rate + 1;
535 *pi_frame_length = i_version ? 576 : 1152;
547 return i_current_frame_size;