]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
* modules/codec/mpeg_audio.c: fixes and improvements.
[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.19 2003/10/05 00:50:05 gbazin Exp $
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <string.h>                                              /* strdup() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/decoder.h>
35 #include <vlc/input.h>
36 #include <vlc/aout.h>
37 #include <vlc/sout.h>
38
39 #include "vlc_block_helper.h"
40
41 /*****************************************************************************
42  * decoder_sys_t : decoder descriptor
43  *****************************************************************************/
44 struct decoder_sys_t
45 {
46     /* Module mode */
47     vlc_bool_t b_packetizer;
48
49     /*
50      * Input properties
51      */
52     int        i_state;
53     vlc_bool_t b_synchro;
54
55     block_t *p_chain;
56     block_bytestream_t bytestream;
57
58     /*
59      * Decoder output properties
60      */
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 */
65
66     /*
67      * Packetizer output properties
68      */
69     sout_packetizer_input_t *p_sout_input;
70     sout_format_t           sout_format;
71     sout_buffer_t *         p_sout_buffer;            /* current sout buffer */
72
73     /*
74      * Common properties
75      */
76     uint8_t               *p_out_buffer;                    /* output buffer */
77     audio_date_t          end_date;
78     unsigned int          i_current_layer;
79
80     mtime_t pts;
81
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;
86 };
87
88 enum {
89
90     STATE_NOSYNC,
91     STATE_SYNC,
92     STATE_HEADER,
93     STATE_NEXT_SYNC,
94     STATE_DATA
95 };
96
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
104
105 /****************************************************************************
106  * Local prototypes
107  ****************************************************************************/
108 static int OpenDecoder   ( vlc_object_t * );
109 static int OpenPacketizer( vlc_object_t * );
110
111 static int InitDecoder   ( decoder_t * );
112 static int RunDecoder    ( decoder_t *, block_t * );
113 static int EndDecoder    ( decoder_t * );
114
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 * );
119
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 );
126
127 /*****************************************************************************
128  * Module descriptor
129  *****************************************************************************/
130 vlc_module_begin();
131     set_description( _("MPEG audio layer I/II/III parser") );
132     set_capability( "decoder", 100 );
133     set_callbacks( OpenDecoder, NULL );
134
135     add_submodule();
136     set_description( _("MPEG audio layer I/II/III packetizer") );
137     set_capability( "packetizer", 10 );
138     set_callbacks( OpenPacketizer, NULL );
139 vlc_module_end();
140
141 /*****************************************************************************
142  * OpenDecoder: probe the decoder and return score
143  *****************************************************************************/
144 static int OpenDecoder( vlc_object_t *p_this )
145 {
146     decoder_t *p_dec = (decoder_t*)p_this;
147
148     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
149     {
150         return VLC_EGENERIC;
151     }
152
153     p_dec->pf_init = InitDecoder;
154     p_dec->pf_decode = RunDecoder;
155     p_dec->pf_end = EndDecoder;
156
157     /* Allocate the memory needed to store the decoder's structure */
158     if( ( p_dec->p_sys =
159           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
160     {
161         msg_Err( p_dec, "out of memory" );
162         return VLC_EGENERIC;
163     }
164     p_dec->p_sys->b_packetizer = VLC_FALSE;
165
166     return VLC_SUCCESS;
167 }
168
169 static int OpenPacketizer( vlc_object_t *p_this )
170 {
171     decoder_t *p_dec = (decoder_t*)p_this;
172
173     int i_ret = OpenDecoder( p_this );
174
175     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
176
177     return i_ret;
178 }
179
180 /*****************************************************************************
181  * InitDecoder: Initalize the decoder
182  *****************************************************************************/
183 static int InitDecoder( decoder_t *p_dec )
184 {
185     p_dec->p_sys->i_state = STATE_NOSYNC;
186     p_dec->p_sys->b_synchro = VLC_FALSE;
187
188     p_dec->p_sys->p_out_buffer = NULL;
189     aout_DateSet( &p_dec->p_sys->end_date, 0 );
190
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');
195
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');
200
201     /* Start with the minimum size for a free bitrate frame */
202     p_dec->p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
203
204     p_dec->p_sys->p_chain = NULL;
205
206     return VLC_SUCCESS;
207 }
208
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 )
215 {
216     decoder_sys_t *p_sys = p_dec->p_sys;
217     uint8_t p_header[MAD_BUFFER_GUARD];
218     uint32_t i_header;
219
220     if( (!aout_DateGet( &p_sys->end_date ) && !p_block->i_pts)
221         || p_block->b_discontinuity )
222     {
223         /* We've just started the stream, wait for the first PTS. */
224         block_Release( p_block );
225         p_sys->b_synchro = VLC_FALSE;
226         return VLC_SUCCESS;
227     }
228
229     if( p_sys->p_chain )
230     {
231         block_ChainAppend( &p_sys->p_chain, p_block );
232     }
233     else
234     {
235         block_ChainAppend( &p_sys->p_chain, p_block );
236         p_sys->bytestream = block_BytestreamInit( p_dec, p_sys->p_chain, 0 );
237     }
238
239     while( 1 )
240     {
241         switch( p_sys->i_state )
242         {
243
244         case STATE_NOSYNC:
245             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
246                    == VLC_SUCCESS )
247             {
248                 /* Look for sync word - should be 0xffe */
249                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
250                 {
251                     p_sys->i_state = STATE_SYNC;
252                     break;
253                 }
254                 block_SkipByte( &p_sys->bytestream );
255                 p_sys->b_synchro = VLC_FALSE;
256             }
257             if( p_sys->i_state != STATE_SYNC )
258             {
259                 block_ChainRelease( p_sys->p_chain );
260                 p_sys->p_chain = NULL;
261
262                 /* Need more data */
263                 return VLC_SUCCESS;
264             }
265
266         case STATE_SYNC:
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 ) )
271             {
272                 aout_DateSet( &p_sys->end_date, p_sys->pts );
273             }
274             p_sys->i_state = STATE_HEADER;
275             break;
276
277         case 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 )
281             {
282                 /* Need more data */
283                 return VLC_SUCCESS;
284             }
285
286             /* Build frame header */
287             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
288                        |p_header[3];
289
290             /* Check if frame is valid and get frame info */
291             p_sys->i_frame_size = SyncInfo( i_header,
292                                             &p_sys->i_channels,
293                                             &p_sys->i_channels_conf,
294                                             &p_sys->i_rate,
295                                             &p_sys->i_bit_rate,
296                                             &p_sys->i_frame_length,
297                                             &p_sys->i_max_frame_size,
298                                             &p_sys->i_layer );
299
300             if( p_sys->i_frame_size == -1 )
301             {
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;
306                 break;
307             }
308
309             if( p_sys->i_bit_rate == 0 )
310             {
311                 /* Free birate, but 99% emulated startcode :( */
312                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
313                 {
314                     msg_Dbg( p_dec, "free bitrate mode");
315                 }
316                 p_sys->i_frame_size = p_sys->i_free_frame_size;
317             }
318
319             p_sys->i_state = STATE_NEXT_SYNC;
320
321         case STATE_NEXT_SYNC:
322             /* TODO: If p_block == NULL, flush the buffer without checking the
323              * next sync word */
324
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 )
329             {
330                 /* Need more data */
331                 return VLC_SUCCESS;
332             }
333
334             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
335             {
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;
342
343                 /* Build frame header */
344                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
345                            |p_header[3];
346
347                 i_next_frame_size = SyncInfo( i_header,
348                                               &i_next_channels,
349                                               &i_next_channels_conf,
350                                               &i_next_rate,
351                                               &i_next_bit_rate,
352                                               &i_next_frame_length,
353                                               &i_next_max_frame_size,
354                                               &i_next_layer );
355
356                 if( i_next_frame_size == -1 )
357                 {
358                     msg_Dbg( p_dec, "emulated start code on next frame" );
359                     block_SkipByte( &p_sys->bytestream );
360                     p_sys->i_state = STATE_NOSYNC;
361                     break;
362                 }
363
364                 /* Free bitrate only */
365                 if( p_sys->i_bit_rate == 0 )
366                 {
367                     if( i_next_bit_rate != 0 )
368                     {
369                         p_sys->i_frame_size++;
370                         break;
371                     }
372
373                     if( (unsigned int)p_sys->i_frame_size >
374                         p_sys->i_max_frame_size )
375                     {
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;
382                         break;
383                     }
384                 }
385
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 )
391                 {
392                     /* Free bitrate only */
393                     if( p_sys->i_bit_rate == 0 )
394                     {
395                         p_sys->i_frame_size++;
396                         break;
397                     }
398
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;
404                     break;
405                 }
406             }
407             else
408             {
409                 /* Free bitrate only */
410                 if( p_sys->i_bit_rate == 0 )
411                 {
412                     p_sys->i_frame_size++;
413                     break;
414                 }
415
416                 if( !p_sys->b_synchro )
417                 {
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 );
422                     break;
423                 }
424             }
425
426             if( GetOutBuffer( p_dec, &p_sys->p_out_buffer ) != VLC_SUCCESS )
427             {
428                 return VLC_EGENERIC;
429             }
430
431             /* Free bitrate only */
432             if( p_sys->i_bit_rate == 0 )
433             {
434                 p_sys->i_free_frame_size = p_sys->i_frame_size;
435             }
436
437             p_sys->i_state = STATE_DATA;
438
439         case 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 )
443             {
444                 /* Need more data */
445                 return VLC_SUCCESS;
446             }
447
448             p_sys->p_chain = block_BytestreamFlush( &p_sys->bytestream );
449
450             /* Get beginning of next frame for libmad */
451             if( !p_sys->b_packetizer )
452             {
453                 memcpy( p_sys->p_out_buffer + p_sys->i_frame_size,
454                         p_header, MAD_BUFFER_GUARD );
455             }
456
457             SendOutBuffer( p_dec );
458             p_sys->i_state = STATE_NOSYNC;
459             p_sys->b_synchro = VLC_TRUE;
460
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;
464         }
465     }
466
467     return VLC_SUCCESS;
468 }
469
470 /*****************************************************************************
471  * GetOutBuffer:
472  *****************************************************************************/
473 static int GetOutBuffer( decoder_t *p_dec, uint8_t **pp_out_buffer )
474 {
475     decoder_sys_t *p_sys = p_dec->p_sys;
476     int i_ret;
477
478     if( p_sys->b_packetizer )
479     {
480         i_ret = GetSoutBuffer( p_dec, &p_sys->p_sout_buffer );
481         *pp_out_buffer =
482             p_sys->p_sout_buffer ? p_sys->p_sout_buffer->p_buffer : NULL;
483     }
484     else
485     {
486         i_ret = GetAoutBuffer( p_dec, &p_sys->p_aout_buffer );
487         *pp_out_buffer =
488             p_sys->p_aout_buffer ? p_sys->p_aout_buffer->p_buffer : NULL;
489     }
490
491     return i_ret;
492 }
493
494 /*****************************************************************************
495  * GetAoutBuffer:
496  *****************************************************************************/
497 static int GetAoutBuffer( decoder_t *p_dec, aout_buffer_t **pp_buffer )
498 {
499     decoder_sys_t *p_sys = p_dec->p_sys;
500
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 ) )
508     {
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;
512     }
513
514     /* Creating the audio input if not created yet. */
515     if( p_sys->p_aout_input == NULL )
516     {
517         p_sys->i_current_layer = p_sys->i_layer;
518         if( p_sys->i_layer == 3 )
519         {
520             p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','3');
521         }
522         else
523         {
524             p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
525         }
526
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
532             + MAD_BUFFER_GUARD;
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,
537                                            &p_sys->p_aout,
538                                            &p_sys->aout_format );
539         if( p_sys->p_aout_input == NULL )
540         {
541             *pp_buffer = NULL;
542             return VLC_EGENERIC;
543         }
544     }
545
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 )
549     {
550         return VLC_EGENERIC;
551     }
552
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 );
556
557     return VLC_SUCCESS;
558 }
559
560 /*****************************************************************************
561  * GetSoutBuffer:
562  *****************************************************************************/
563 static int GetSoutBuffer( decoder_t *p_dec, sout_buffer_t **pp_buffer )
564 {
565     decoder_sys_t *p_sys = p_dec->p_sys;
566
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 ) )
570     {
571         /* Parameters changed - this should not happen. */
572     }
573
574     /* Creating the sout input if not created yet. */
575     if( p_sys->p_sout_input == NULL )
576     {
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;
583
584         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
585         aout_DateSet( &p_sys->end_date, p_sys->pts );
586
587         p_sys->p_sout_input = sout_InputNew( p_dec, &p_sys->sout_format );
588         if( p_sys->p_sout_input == NULL )
589         {
590             msg_Err( p_dec, "cannot add a new stream" );
591             *pp_buffer = NULL;
592             return VLC_EGENERIC;
593         }
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 );
596     }
597
598     *pp_buffer = sout_BufferNew( p_sys->p_sout_input->p_sout,
599                                  p_sys->i_frame_size );
600     if( *pp_buffer == NULL )
601     {
602         return VLC_EGENERIC;
603     }
604
605     (*pp_buffer)->i_pts =
606         (*pp_buffer)->i_dts = aout_DateGet( &p_sys->end_date );
607
608     (*pp_buffer)->i_length =
609         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length )
610         - (*pp_buffer)->i_pts;
611
612     return VLC_SUCCESS;
613 }
614
615 /*****************************************************************************
616  * SendOutBuffer:
617  *****************************************************************************/
618 static int SendOutBuffer( decoder_t *p_dec )
619 {
620     decoder_sys_t *p_sys = p_dec->p_sys;
621
622     if( p_sys->b_packetizer )
623     {
624         sout_InputSendBuffer( p_sys->p_sout_input, p_sys->p_sout_buffer );
625         p_sys->p_sout_buffer = NULL;
626     }
627     else
628     {
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;
635     }
636
637     p_sys->p_out_buffer = NULL;
638
639     return VLC_SUCCESS;
640 }
641
642 /*****************************************************************************
643  * EndDecoder: clean up the decoder
644  *****************************************************************************/
645 static int EndDecoder( decoder_t *p_dec )
646 {
647     if( p_dec->p_sys->p_aout_input != NULL )
648     {
649         if( p_dec->p_sys->p_aout_buffer )
650         {
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 );
654         }
655
656         aout_DecDelete( p_dec->p_sys->p_aout, p_dec->p_sys->p_aout_input );
657     }
658
659     if( p_dec->p_sys->p_sout_input != NULL )
660     {
661         if( p_dec->p_sys->p_sout_buffer )
662         {
663             sout_BufferDelete( p_dec->p_sys->p_sout_input->p_sout,
664                                p_dec->p_sys->p_sout_buffer );
665         }
666
667         sout_InputDelete( p_dec->p_sys->p_sout_input );
668     }
669
670     if( p_dec->p_sys->p_chain ) block_ChainRelease( p_dec->p_sys->p_chain );
671
672     free( p_dec->p_sys );
673
674     return VLC_SUCCESS;
675 }
676
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)
685 {
686     static const int ppi_bitrate[2][3][16] =
687     {
688         {
689             /* v1 l1 */
690             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
691               416, 448, 0},
692             /* v1 l2 */
693             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
694               320, 384, 0},
695             /* v1 l3 */
696             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
697               256, 320, 0}
698         },
699
700         {
701             /* v2 l1 */
702             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
703               224, 256, 0},
704             /* v2 l2 */
705             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
706               144, 160, 0},
707             /* v2 l3 */
708             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
709               144, 160, 0}
710         }
711     };
712
713     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
714     {
715         { 44100, 48000, 32000, 0 },
716         { 22050, 24000, 16000, 0 }
717     };
718
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;
723     int i_max_bit_rate;
724
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);
728     /* CRC */
729     i_bitrate_index = (i_header & 0xf000) >> 12;
730     i_samplerate_index = (i_header & 0xc00) >> 10;
731     b_padding   = (i_header & 0x200) >> 9;
732     /* Extension */
733     i_mode      = (i_header & 0xc0) >> 6;
734     /* Modeext, copyright & original */
735     i_emphasis  = i_header & 0x3;
736
737     if( *pi_layer != 4 &&
738         i_bitrate_index < 0x0f &&
739         i_samplerate_index != 0x03 &&
740         i_emphasis != 0x02 )
741     {
742         switch ( i_mode )
743         {
744         case 0: /* stereo */
745         case 1: /* joint stereo */
746             *pi_channels = 2;
747             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
748             break;
749         case 2: /* dual-mono */
750             *pi_channels = 2;
751             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
752                                 | AOUT_CHAN_DUALMONO;
753             break;
754         case 3: /* mono */
755             *pi_channels = 1;
756             *pi_channels_conf = AOUT_CHAN_CENTER;
757             break;
758         }
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];
762
763         if ( b_mpeg_2_5 )
764         {
765             *pi_sample_rate >>= 1;
766         }
767
768         switch( *pi_layer )
769         {
770         case 1:
771             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
772                            b_padding ) * 4;
773             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
774                                  *pi_sample_rate + 1 ) * 4;
775             *pi_frame_length = 384;
776             break;
777
778         case 2:
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;
782             break;
783
784         case 3:
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;
790             break;
791
792         default:
793             break;
794         }
795     }
796     else
797     {
798         return -1;
799     }
800
801     return i_frame_size;
802 }