]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
Better recovery on emulated startcode. Still doesn't work with VBR mp3's,
[vlc] / modules / codec / mpeg_audio.c
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.2 2003/01/15 13:14:50 massiot Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>                                              /* strdup() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34 #include <vlc/decoder.h>
35
36 /*****************************************************************************
37  * dec_thread_t : decoder thread descriptor
38  *****************************************************************************/
39 typedef struct dec_thread_t
40 {
41     /*
42      * Thread properties
43      */
44     vlc_thread_t        thread_id;                /* id for thread functions */
45
46     /*
47      * Input properties
48      */
49     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
50     bit_stream_t        bit_stream;
51
52     /*
53      * Output properties
54      */
55     aout_instance_t *   p_aout; /* opaque */
56     aout_input_t *      p_aout_input; /* opaque */
57     audio_sample_format_t output_format;
58 } dec_thread_t;
59
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
67
68
69 /****************************************************************************
70  * Local prototypes
71  ****************************************************************************/
72 static int  Open           ( vlc_object_t * );
73 static int  RunDecoder     ( decoder_fifo_t * );
74
75 static void EndThread      ( dec_thread_t * );
76
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 );
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86 vlc_module_begin();
87     set_description( _("MPEG audio layer I/II/III parser") );
88     set_capability( "decoder", 100 );
89     set_callbacks( Open, NULL );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * OpenDecoder: probe the decoder and return score
94  *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
96 {
97     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
98
99     if( p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
100     {
101         return VLC_EGENERIC;
102     }
103
104     p_fifo->pf_run = RunDecoder;
105     return VLC_SUCCESS;
106 }
107
108 /*****************************************************************************
109  * RunDecoder: this function is called just after the thread is created
110  *****************************************************************************/
111 static int RunDecoder( decoder_fifo_t *p_fifo )
112 {
113     dec_thread_t * p_dec;
114     audio_date_t end_date;
115     unsigned int i_layer = 0;
116
117     /* Allocate the memory needed to store the thread's structure */
118     p_dec = malloc( sizeof(dec_thread_t) );
119     if( p_dec == NULL )
120     {
121         msg_Err( p_fifo, "out of memory" );
122         DecoderError( p_fifo );
123         return -1;
124     }
125
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;
130
131     aout_DateSet( &end_date, 0 );
132
133     /* Init the bitstream */
134     if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
135                        NULL, NULL ) != VLC_SUCCESS )
136     {
137         msg_Err( p_fifo, "cannot initialize bitstream" );
138         DecoderError( p_fifo );
139         free( p_dec );
140         return -1;
141     }
142
143     /* Decoder thread's main loop */
144     while ( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
145     {
146         int i_bit_rate;
147         unsigned int i_rate, i_original_channels, i_frame_size, i_frame_length;
148         unsigned int i_new_layer, i_current_frame_size;
149         mtime_t pts;
150         uint32_t i_header;
151         aout_buffer_t * p_buffer;
152
153         /* Look for sync word - should be 0xfff */
154         RealignBits( &p_dec->bit_stream );
155         if ( ShowBits( &p_dec->bit_stream, 11 ) != 0x07ff )
156         {
157             msg_Warn( p_dec->p_fifo, "no sync - skipping" );
158         }
159         while ( ShowBits( &p_dec->bit_stream, 11 ) != 0x07ff &&
160                 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
161         {
162             RemoveBits( &p_dec->bit_stream, 8 );
163         }
164         if ( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error ) break;
165
166         /* Set the Presentation Time Stamp */
167         NextPTS( &p_dec->bit_stream, &pts, NULL );
168         if ( pts != 0 && pts != aout_DateGet( &end_date ) )
169         {
170             aout_DateSet( &end_date, pts );
171         }
172
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;
176
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 );
182
183         if( !i_current_frame_size )
184         {
185             msg_Warn( p_dec->p_fifo, "syncinfo failed" );
186             /* This is probably an emulated startcode, drop the first byte
187              * to force looking for the next startcode. */
188             RemoveBits( &p_dec->bit_stream, 8 );
189             continue;
190         }
191
192         if( (p_dec->p_aout_input != NULL) &&
193             ( (p_dec->output_format.i_rate != i_rate)
194                 || (p_dec->output_format.i_original_channels
195                       != i_original_channels)
196                 || (p_dec->output_format.i_bytes_per_frame
197                       != i_frame_size + MAD_BUFFER_GUARD)
198                 || (p_dec->output_format.i_frame_length != i_frame_length)
199                 || (i_layer != i_new_layer) ) )
200         {
201             /* Parameters changed - this should not happen. */
202             aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
203             p_dec->p_aout_input = NULL;
204         }
205
206         /* Creating the audio input if not created yet. */
207         if( p_dec->p_aout_input == NULL )
208         {
209             i_layer = i_new_layer;
210             if ( i_layer == 3 )
211             {
212                 p_dec->output_format.i_format = VLC_FOURCC('m','p','g','3');
213             }
214             else
215             {
216                 p_dec->output_format.i_format = VLC_FOURCC('m','p','g','a');
217             }
218             p_dec->output_format.i_rate = i_rate;
219             p_dec->output_format.i_original_channels = i_original_channels;
220             p_dec->output_format.i_physical_channels
221                        = i_original_channels & AOUT_CHAN_PHYSMASK;
222             p_dec->output_format.i_bytes_per_frame = i_frame_size
223                                                         + MAD_BUFFER_GUARD;
224             p_dec->output_format.i_frame_length = i_frame_length;
225             aout_DateInit( &end_date, i_rate );
226             p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
227                                                &p_dec->p_aout,
228                                                &p_dec->output_format );
229
230             if ( p_dec->p_aout_input == NULL )
231             {
232                 p_dec->p_fifo->b_error = 1;
233                 break;
234             }
235         }
236
237         if ( !aout_DateGet( &end_date ) )
238         {
239             byte_t p_junk[MAX_FRAME_SIZE];
240
241             /* We've just started the stream, wait for the first PTS. */
242             GetChunk( &p_dec->bit_stream, p_junk, i_frame_size );
243             continue;
244         }
245
246         p_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
247                                       i_frame_length );
248         if ( p_buffer == NULL )
249         {
250             p_dec->p_fifo->b_error = 1;
251             break;
252         }
253         p_buffer->start_date = aout_DateGet( &end_date );
254         p_buffer->end_date = aout_DateIncrement( &end_date,
255                                                  i_frame_length );
256
257         /* Get the whole frame. */
258         GetChunk( &p_dec->bit_stream, p_buffer->p_buffer,
259                   i_current_frame_size );
260         if( p_dec->p_fifo->b_die )
261         {
262             aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input,
263                                   p_buffer );
264             break;
265         }
266
267         /* Send the buffer to the aout core. */
268         aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
269     }
270
271     if( p_dec->p_fifo->b_error )
272     {
273         DecoderError( p_dec->p_fifo );
274     }
275
276     EndThread( p_dec );
277
278     return 0;
279 }
280
281 /*****************************************************************************
282  * EndThread : thread destruction
283  *****************************************************************************/
284 static void EndThread( dec_thread_t * p_dec )
285 {
286     if ( p_dec->p_aout_input != NULL )
287     {
288         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
289     }
290
291     CloseBitstream( &p_dec->bit_stream );
292     free( p_dec );
293 }
294
295 /*****************************************************************************
296  * SyncInfo: parse MPEG audio sync info
297  *****************************************************************************/
298 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
299                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
300                      unsigned int * pi_frame_length,
301                      unsigned int * pi_frame_size, unsigned int * pi_layer )
302 {
303     static const int pppi_mpegaudio_bitrate[2][3][16] =
304     {
305         {
306             /* v1 l1 */
307             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
308               416, 448, 0},
309             /* v1 l2 */
310             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
311               320, 384, 0},
312             /* v1 l3 */
313             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
314               256, 320, 0} 
315         },
316
317         {
318             /* v2 l1 */
319             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
320               224, 256, 0},
321             /* v2 l2 */
322             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
323               144, 160, 0},
324             /* v2 l3 */
325             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
326               144, 160, 0} 
327         }
328     };
329
330     static const int ppi_mpegaudio_samplerate[2][4] = /* version 1 then 2 */
331     {
332         { 44100, 48000, 32000, 0 },
333         { 22050, 24000, 16000, 0 }
334     };
335
336     int i_version, i_mode, i_emphasis;
337     vlc_bool_t b_padding, b_mpeg_2_5;
338     int i_current_frame_size = 0;
339     int i_bitrate_index, i_samplerate_index;
340
341     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
342     i_version   = 1 - ((i_header & 0x80000) >> 19);
343     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
344     /* CRC */
345     i_bitrate_index = (i_header & 0xf000) >> 12;
346     i_samplerate_index = (i_header & 0xc00) >> 10;
347     b_padding   = (i_header & 0x200) >> 9;
348     /* Extension */
349     i_mode      = (i_header & 0xc0) >> 6;
350     /* Modeext, copyright & original */
351     i_emphasis  = i_header & 0x3;
352
353     if( *pi_layer != 4 &&
354         i_bitrate_index < 0x0f &&
355         i_samplerate_index != 0x03 &&
356         i_emphasis != 0x02 )
357     {
358         switch ( i_mode )
359         {
360         case 0: /* stereo */
361         case 1: /* joint stereo */
362             *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
363             break;
364         case 2: /* dual-mono */
365             *pi_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
366                             | AOUT_CHAN_DUALMONO;
367             break;
368         case 3: /* mono */
369             *pi_channels = AOUT_CHAN_CENTER;
370             break;
371         }
372         *pi_bit_rate = pppi_mpegaudio_bitrate[i_version][*pi_layer-1][i_bitrate_index];
373         *pi_sample_rate = ppi_mpegaudio_samplerate[i_version][i_samplerate_index];
374
375         if ( b_mpeg_2_5 )
376         {
377             *pi_sample_rate >>= 1;
378         }
379
380         switch( *pi_layer )
381         {
382         case 1:
383             i_current_frame_size = ( ( i_version ? 6000 : 12000 ) *
384                                         *pi_bit_rate / *pi_sample_rate
385                                         + b_padding ) * 4;
386             *pi_frame_size = ( ( i_version ? 6000 : 12000 ) *
387                                   *pi_bit_rate / *pi_sample_rate + 1 ) * 4;
388             *pi_frame_length = 384;
389             break;
390
391         case 2:
392             i_current_frame_size = ( i_version ? 72000 : 144000 ) *
393                                       *pi_bit_rate / *pi_sample_rate
394                                       + b_padding;
395             *pi_frame_size = ( i_version ? 72000 : 144000 ) *
396                                       *pi_bit_rate / *pi_sample_rate + 1;
397             *pi_frame_length = 1152;
398             break;
399
400         case 3:
401             i_current_frame_size = ( i_version ? 72000 : 144000 ) *
402                                       *pi_bit_rate / *pi_sample_rate
403                                       + b_padding;
404             *pi_frame_size = ( i_version ? 72000 : 144000 ) *
405                                       *pi_bit_rate / *pi_sample_rate + 1;
406             *pi_frame_length = i_version ? 576 : 1152;
407             break;
408
409         default:
410             i_current_frame_size = *pi_frame_size = 0;
411             *pi_frame_length = 0;
412         }
413     }
414     
415     return i_current_frame_size;
416 }