* include/vlc_block_helper.h: fixed an idiotically stupid bug in block_PeekOffsetBytes().
[vlc.git] / 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.20 2003/10/23 20:51:20 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
54     block_t *p_chain;
55     block_bytestream_t bytestream;
56
57     /*
58      * Decoder output properties
59      */
60     aout_instance_t *     p_aout;                                  /* opaque */
61     aout_input_t *        p_aout_input;                            /* opaque */
62     audio_sample_format_t aout_format;
63     aout_buffer_t *       p_aout_buffer; /* current aout buffer being filled */
64
65     /*
66      * Packetizer output properties
67      */
68     sout_packetizer_input_t *p_sout_input;
69     sout_format_t           sout_format;
70     sout_buffer_t *         p_sout_buffer;            /* current sout buffer */
71
72     /*
73      * Common properties
74      */
75     uint8_t               *p_out_buffer;                    /* output buffer */
76     audio_date_t          end_date;
77     unsigned int          i_current_layer;
78
79     mtime_t pts;
80
81     int i_frame_size, i_free_frame_size;
82     unsigned int i_channels_conf, i_channels;
83     unsigned int i_rate, i_max_frame_size, i_frame_length;
84     unsigned int i_layer, i_bit_rate;
85 };
86
87 enum {
88
89     STATE_NOSYNC,
90     STATE_SYNC,
91     STATE_HEADER,
92     STATE_NEXT_SYNC,
93     STATE_DATA
94 };
95
96 /* This isn't the place to put mad-specific stuff. However, it makes the
97  * mad plug-in's life much easier if we put 8 extra bytes at the end of the
98  * buffer, because that way it doesn't have to copy the aout_buffer_t to a
99  * bigger buffer. This has no implication on other plug-ins, and we only
100  * lose 8 bytes per frame. --Meuuh */
101 #define MAD_BUFFER_GUARD 8
102 #define MPGA_HEADER_SIZE 4
103
104 /****************************************************************************
105  * Local prototypes
106  ****************************************************************************/
107 static int OpenDecoder   ( vlc_object_t * );
108 static int OpenPacketizer( vlc_object_t * );
109
110 static int InitDecoder   ( decoder_t * );
111 static int RunDecoder    ( decoder_t *, block_t * );
112 static int EndDecoder    ( decoder_t * );
113
114 static int GetOutBuffer ( decoder_t *, uint8_t ** );
115 static int GetAoutBuffer( decoder_t *, aout_buffer_t ** );
116 static int GetSoutBuffer( decoder_t *, sout_buffer_t ** );
117 static int SendOutBuffer( decoder_t * );
118
119 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
120                      unsigned int * pi_channels_conf,
121                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
122                      unsigned int * pi_frame_length,
123                      unsigned int * pi_max_frame_size,
124                      unsigned int * pi_layer );
125
126 /*****************************************************************************
127  * Module descriptor
128  *****************************************************************************/
129 vlc_module_begin();
130     set_description( _("MPEG audio layer I/II/III parser") );
131     set_capability( "decoder", 100 );
132     set_callbacks( OpenDecoder, NULL );
133
134     add_submodule();
135     set_description( _("MPEG audio layer I/II/III packetizer") );
136     set_capability( "packetizer", 10 );
137     set_callbacks( OpenPacketizer, NULL );
138 vlc_module_end();
139
140 /*****************************************************************************
141  * OpenDecoder: probe the decoder and return score
142  *****************************************************************************/
143 static int OpenDecoder( vlc_object_t *p_this )
144 {
145     decoder_t *p_dec = (decoder_t*)p_this;
146
147     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC( 'm', 'p', 'g', 'a') )
148     {
149         return VLC_EGENERIC;
150     }
151
152     p_dec->pf_init = InitDecoder;
153     p_dec->pf_decode = RunDecoder;
154     p_dec->pf_end = EndDecoder;
155
156     /* Allocate the memory needed to store the decoder's structure */
157     if( ( p_dec->p_sys =
158           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
159     {
160         msg_Err( p_dec, "out of memory" );
161         return VLC_EGENERIC;
162     }
163     p_dec->p_sys->b_packetizer = VLC_FALSE;
164
165     return VLC_SUCCESS;
166 }
167
168 static int OpenPacketizer( vlc_object_t *p_this )
169 {
170     decoder_t *p_dec = (decoder_t*)p_this;
171
172     int i_ret = OpenDecoder( p_this );
173
174     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
175
176     return i_ret;
177 }
178
179 /*****************************************************************************
180  * InitDecoder: Initalize the decoder
181  *****************************************************************************/
182 static int InitDecoder( decoder_t *p_dec )
183 {
184     p_dec->p_sys->i_state = STATE_NOSYNC;
185
186     p_dec->p_sys->p_out_buffer = NULL;
187     aout_DateSet( &p_dec->p_sys->end_date, 0 );
188
189     p_dec->p_sys->p_aout = NULL;
190     p_dec->p_sys->p_aout_input = NULL;
191     p_dec->p_sys->p_aout_buffer = NULL;
192     p_dec->p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
193
194     p_dec->p_sys->p_sout_input = NULL;
195     p_dec->p_sys->p_sout_buffer = NULL;
196     p_dec->p_sys->sout_format.i_cat = AUDIO_ES;
197     p_dec->p_sys->sout_format.i_fourcc = VLC_FOURCC('m','p','g','a');
198
199     /* Start with the minimum size for a free bitrate frame */
200     p_dec->p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
201
202     p_dec->p_sys->p_chain = NULL;
203
204     return VLC_SUCCESS;
205 }
206
207 /****************************************************************************
208  * RunDecoder: the whole thing
209  ****************************************************************************
210  * This function is called just after the thread is launched.
211  ****************************************************************************/
212 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
213 {
214     decoder_sys_t *p_sys = p_dec->p_sys;
215     uint8_t p_header[MAD_BUFFER_GUARD];
216     uint32_t i_header;
217
218     if( !aout_DateGet( &p_sys->end_date ) && !p_block->i_pts )
219     {
220         /* We've just started the stream, wait for the first PTS. */
221         block_Release( p_block );
222         return VLC_SUCCESS;
223     }
224
225     if( p_block->b_discontinuity )
226     {
227         p_sys->i_state = STATE_SYNC;
228     }
229
230     if( p_sys->p_chain )
231     {
232         block_ChainAppend( &p_sys->p_chain, p_block );
233     }
234     else
235     {
236         block_ChainAppend( &p_sys->p_chain, p_block );
237         p_sys->bytestream = block_BytestreamInit( p_dec, p_sys->p_chain, 0 );
238     }
239
240     while( 1 )
241     {
242         switch( p_sys->i_state )
243         {
244
245         case STATE_NOSYNC:
246             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
247                    == VLC_SUCCESS )
248             {
249                 /* Look for sync word - should be 0xffe */
250                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
251                 {
252                     p_sys->i_state = STATE_SYNC;
253                     break;
254                 }
255                 block_SkipByte( &p_sys->bytestream );
256             }
257             if( p_sys->i_state != STATE_SYNC )
258             {
259                 if( block_PeekByte( &p_sys->bytestream, p_header )
260                     == VLC_SUCCESS && p_header[0] == 0xff )
261                 {
262                     /* Start of a sync word, need more data */
263                     return VLC_SUCCESS;
264                 }
265
266                 block_ChainRelease( p_sys->p_chain );
267                 p_sys->p_chain = NULL;
268
269                 /* Need more data */
270                 return VLC_SUCCESS;
271             }
272
273         case STATE_SYNC:
274             /* New frame, set the Presentation Time Stamp */
275             p_sys->pts = p_sys->bytestream.p_block->i_pts;
276             if( p_sys->pts != 0 &&
277                 p_sys->pts != aout_DateGet( &p_sys->end_date ) )
278             {
279               msg_Err( p_dec, "set PTS: %lli old: %lli", p_sys->pts,
280                        aout_DateGet( &p_sys->end_date ) );
281                 aout_DateSet( &p_sys->end_date, p_sys->pts );
282             }
283             p_sys->i_state = STATE_HEADER;
284             break;
285
286         case STATE_HEADER:
287             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
288             if( block_PeekBytes( &p_sys->bytestream, p_header,
289                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
290             {
291                 /* Need more data */
292                 return VLC_SUCCESS;
293             }
294
295             /* Build frame header */
296             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
297                        |p_header[3];
298
299             /* Check if frame is valid and get frame info */
300             p_sys->i_frame_size = SyncInfo( i_header,
301                                             &p_sys->i_channels,
302                                             &p_sys->i_channels_conf,
303                                             &p_sys->i_rate,
304                                             &p_sys->i_bit_rate,
305                                             &p_sys->i_frame_length,
306                                             &p_sys->i_max_frame_size,
307                                             &p_sys->i_layer );
308
309             if( p_sys->i_frame_size == -1 )
310             {
311                 msg_Dbg( p_dec, "emulated start code" );
312                 block_SkipByte( &p_sys->bytestream );
313                 p_sys->i_state = STATE_NOSYNC;
314                 break;
315             }
316
317             if( p_sys->i_bit_rate == 0 )
318             {
319                 /* Free birate, but 99% emulated startcode :( */
320                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
321                 {
322                     msg_Dbg( p_dec, "free bitrate mode");
323                 }
324                 p_sys->i_frame_size = p_sys->i_free_frame_size;
325             }
326
327             p_sys->i_state = STATE_NEXT_SYNC;
328
329         case STATE_NEXT_SYNC:
330             /* TODO: If p_block == NULL, flush the buffer without checking the
331              * next sync word */
332
333             /* Check if next expected frame contains the sync word */
334             if( block_PeekOffsetBytes( &p_sys->bytestream,
335                                        p_sys->i_frame_size, p_header,
336                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
337             {
338                 /* Need more data */
339                 return VLC_SUCCESS;
340             }
341
342             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
343             {
344                 /* Startcode is fine, let's try the header as an extra check */
345                 int i_next_frame_size;
346                 unsigned int i_next_channels, i_next_channels_conf;
347                 unsigned int i_next_rate, i_next_bit_rate;
348                 unsigned int i_next_frame_length, i_next_max_frame_size;
349                 unsigned int i_next_layer;
350
351                 /* Build frame header */
352                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
353                            |p_header[3];
354
355                 i_next_frame_size = SyncInfo( i_header,
356                                               &i_next_channels,
357                                               &i_next_channels_conf,
358                                               &i_next_rate,
359                                               &i_next_bit_rate,
360                                               &i_next_frame_length,
361                                               &i_next_max_frame_size,
362                                               &i_next_layer );
363
364                 if( i_next_frame_size == -1 )
365                 {
366                     msg_Dbg( p_dec, "emulated start code on next frame" );
367                     block_SkipByte( &p_sys->bytestream );
368                     p_sys->i_state = STATE_NOSYNC;
369                     break;
370                 }
371
372                 /* Free bitrate only */
373                 if( p_sys->i_bit_rate == 0 )
374                 {
375                     if( i_next_bit_rate != 0 )
376                     {
377                         p_sys->i_frame_size++;
378                         break;
379                     }
380
381                     if( (unsigned int)p_sys->i_frame_size >
382                         p_sys->i_max_frame_size )
383                     {
384                         msg_Dbg( p_dec, "frame too big %d > %d "
385                                  "(emulated startcode ?)", p_sys->i_frame_size,
386                                  p_sys->i_max_frame_size );
387                         block_SkipByte( &p_sys->bytestream );
388                         p_sys->i_state = STATE_NOSYNC;
389                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
390                         break;
391                     }
392                 }
393
394                 /* Check info is in sync with previous one */
395                 if( i_next_channels_conf != p_sys->i_channels_conf ||
396                     i_next_rate != p_sys->i_rate ||
397                     i_next_layer != p_sys->i_layer ||
398                     i_next_frame_length != p_sys->i_frame_length )
399                 {
400                     /* Free bitrate only */
401                     if( p_sys->i_bit_rate == 0 )
402                     {
403                         p_sys->i_frame_size++;
404                         break;
405                     }
406
407                     msg_Dbg( p_dec, "parameters changed unexpectedly "
408                              "(emulated startcode ?)" );
409                     block_SkipByte( &p_sys->bytestream );
410                     p_sys->i_state = STATE_NOSYNC;
411                     break;
412                 }
413             }
414             else
415             {
416                 /* Free bitrate only */
417                 if( p_sys->i_bit_rate == 0 )
418                 {
419                     p_sys->i_frame_size++;
420                     break;
421                 }
422
423                 msg_Dbg( p_dec, "emulated startcode "
424                          "(no startcode on following frame)" );
425                 p_sys->i_state = STATE_NOSYNC;
426                 block_SkipByte( &p_sys->bytestream );
427                 break;
428             }
429
430             if( GetOutBuffer( p_dec, &p_sys->p_out_buffer ) != VLC_SUCCESS )
431             {
432                 return VLC_EGENERIC;
433             }
434
435             /* Free bitrate only */
436             if( p_sys->i_bit_rate == 0 )
437             {
438                 p_sys->i_free_frame_size = p_sys->i_frame_size;
439             }
440
441             p_sys->i_state = STATE_DATA;
442
443         case STATE_DATA:
444             /* Copy the whole frame into the buffer */
445             if( block_GetBytes( &p_sys->bytestream, p_sys->p_out_buffer,
446                                 p_sys->i_frame_size ) != VLC_SUCCESS )
447             {
448                 /* Need more data */
449                 return VLC_SUCCESS;
450             }
451
452             p_sys->p_chain = block_BytestreamFlush( &p_sys->bytestream );
453
454             /* Get beginning of next frame for libmad */
455             if( !p_sys->b_packetizer )
456             {
457                 memcpy( p_sys->p_out_buffer + p_sys->i_frame_size,
458                         p_header, MAD_BUFFER_GUARD );
459             }
460
461             SendOutBuffer( p_dec );
462             p_sys->i_state = STATE_NOSYNC;
463
464             /* Make sure we don't reuse the same pts twice */
465             if( p_sys->pts == p_sys->bytestream.p_block->i_pts )
466                 p_sys->pts = p_sys->bytestream.p_block->i_pts = 0;
467         }
468     }
469
470     return VLC_SUCCESS;
471 }
472
473 /*****************************************************************************
474  * GetOutBuffer:
475  *****************************************************************************/
476 static int GetOutBuffer( decoder_t *p_dec, uint8_t **pp_out_buffer )
477 {
478     decoder_sys_t *p_sys = p_dec->p_sys;
479     int i_ret;
480
481     if( p_sys->b_packetizer )
482     {
483         i_ret = GetSoutBuffer( p_dec, &p_sys->p_sout_buffer );
484         *pp_out_buffer =
485             p_sys->p_sout_buffer ? p_sys->p_sout_buffer->p_buffer : NULL;
486     }
487     else
488     {
489         i_ret = GetAoutBuffer( p_dec, &p_sys->p_aout_buffer );
490         *pp_out_buffer =
491             p_sys->p_aout_buffer ? p_sys->p_aout_buffer->p_buffer : NULL;
492     }
493
494     return i_ret;
495 }
496
497 /*****************************************************************************
498  * GetAoutBuffer:
499  *****************************************************************************/
500 static int GetAoutBuffer( decoder_t *p_dec, aout_buffer_t **pp_buffer )
501 {
502     decoder_sys_t *p_sys = p_dec->p_sys;
503
504     if( p_sys->p_aout_input != NULL &&
505         ( p_sys->aout_format.i_rate != p_sys->i_rate
506         || p_sys->aout_format.i_original_channels != p_sys->i_channels_conf
507         || p_sys->aout_format.i_bytes_per_frame !=
508            p_sys->i_max_frame_size + MAD_BUFFER_GUARD
509         || p_sys->aout_format.i_frame_length != p_sys->i_frame_length
510         || p_sys->i_current_layer != p_sys->i_layer ) )
511     {
512         /* Parameters changed - this should not happen. */
513         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
514         p_sys->p_aout_input = NULL;
515     }
516
517     /* Creating the audio input if not created yet. */
518     if( p_sys->p_aout_input == NULL )
519     {
520         p_sys->i_current_layer = p_sys->i_layer;
521         if( p_sys->i_layer == 3 )
522         {
523             p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','3');
524         }
525         else
526         {
527             p_sys->aout_format.i_format = VLC_FOURCC('m','p','g','a');
528         }
529
530         p_sys->aout_format.i_rate = p_sys->i_rate;
531         p_sys->aout_format.i_original_channels = p_sys->i_channels_conf;
532         p_sys->aout_format.i_physical_channels
533             = p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
534         p_sys->aout_format.i_bytes_per_frame = p_sys->i_max_frame_size
535             + MAD_BUFFER_GUARD;
536         p_sys->aout_format.i_frame_length = p_sys->i_frame_length;
537         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
538         aout_DateSet( &p_sys->end_date, p_sys->pts );
539         p_sys->p_aout_input = aout_DecNew( p_dec,
540                                            &p_sys->p_aout,
541                                            &p_sys->aout_format );
542         if( p_sys->p_aout_input == NULL )
543         {
544             *pp_buffer = NULL;
545             return VLC_EGENERIC;
546         }
547     }
548
549     *pp_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
550                                     p_sys->i_frame_length );
551     if( *pp_buffer == NULL )
552     {
553         return VLC_EGENERIC;
554     }
555
556     (*pp_buffer)->start_date = aout_DateGet( &p_sys->end_date );
557     (*pp_buffer)->end_date =
558          aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
559
560     return VLC_SUCCESS;
561 }
562
563 /*****************************************************************************
564  * GetSoutBuffer:
565  *****************************************************************************/
566 static int GetSoutBuffer( decoder_t *p_dec, sout_buffer_t **pp_buffer )
567 {
568     decoder_sys_t *p_sys = p_dec->p_sys;
569
570     if( p_sys->p_sout_input != NULL &&
571         ( p_sys->sout_format.i_sample_rate != (int)p_sys->i_rate
572           || p_sys->sout_format.i_channels != (int)p_sys->i_channels ) )
573     {
574         /* Parameters changed - this should not happen. */
575     }
576
577     /* Creating the sout input if not created yet. */
578     if( p_sys->p_sout_input == NULL )
579     {
580         p_sys->sout_format.i_sample_rate = p_sys->i_rate;
581         p_sys->sout_format.i_channels    = p_sys->i_channels;
582         p_sys->sout_format.i_block_align = 0;
583         p_sys->sout_format.i_bitrate     = p_sys->i_bit_rate;
584         p_sys->sout_format.i_extra_data  = 0;
585         p_sys->sout_format.p_extra_data  = NULL;
586
587         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
588         aout_DateSet( &p_sys->end_date, p_sys->pts );
589
590         p_sys->p_sout_input = sout_InputNew( p_dec, &p_sys->sout_format );
591         if( p_sys->p_sout_input == NULL )
592         {
593             msg_Err( p_dec, "cannot add a new stream" );
594             *pp_buffer = NULL;
595             return VLC_EGENERIC;
596         }
597         msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
598                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
599     }
600
601     *pp_buffer = sout_BufferNew( p_sys->p_sout_input->p_sout,
602                                  p_sys->i_frame_size );
603     if( *pp_buffer == NULL )
604     {
605         return VLC_EGENERIC;
606     }
607
608     (*pp_buffer)->i_pts =
609         (*pp_buffer)->i_dts = aout_DateGet( &p_sys->end_date );
610
611     (*pp_buffer)->i_length =
612         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length )
613         - (*pp_buffer)->i_pts;
614
615     return VLC_SUCCESS;
616 }
617
618 /*****************************************************************************
619  * SendOutBuffer:
620  *****************************************************************************/
621 static int SendOutBuffer( decoder_t *p_dec )
622 {
623     decoder_sys_t *p_sys = p_dec->p_sys;
624
625     if( p_sys->b_packetizer )
626     {
627         sout_InputSendBuffer( p_sys->p_sout_input, p_sys->p_sout_buffer );
628         p_sys->p_sout_buffer = NULL;
629     }
630     else
631     {
632         /* We have all we need, send the buffer to the aout core. */
633         p_sys->p_aout_buffer->i_nb_bytes =
634             p_sys->i_frame_size + MAD_BUFFER_GUARD;
635         aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input,
636                       p_sys->p_aout_buffer );
637         p_sys->p_aout_buffer = NULL;
638     }
639
640     p_sys->p_out_buffer = NULL;
641
642     return VLC_SUCCESS;
643 }
644
645 /*****************************************************************************
646  * EndDecoder: clean up the decoder
647  *****************************************************************************/
648 static int EndDecoder( decoder_t *p_dec )
649 {
650     if( p_dec->p_sys->p_aout_input != NULL )
651     {
652         if( p_dec->p_sys->p_aout_buffer )
653         {
654             aout_DecDeleteBuffer( p_dec->p_sys->p_aout,
655                                   p_dec->p_sys->p_aout_input,
656                                   p_dec->p_sys->p_aout_buffer );
657         }
658
659         aout_DecDelete( p_dec->p_sys->p_aout, p_dec->p_sys->p_aout_input );
660     }
661
662     if( p_dec->p_sys->p_sout_input != NULL )
663     {
664         if( p_dec->p_sys->p_sout_buffer )
665         {
666             sout_BufferDelete( p_dec->p_sys->p_sout_input->p_sout,
667                                p_dec->p_sys->p_sout_buffer );
668         }
669
670         sout_InputDelete( p_dec->p_sys->p_sout_input );
671     }
672
673     if( p_dec->p_sys->p_chain ) block_ChainRelease( p_dec->p_sys->p_chain );
674
675     free( p_dec->p_sys );
676
677     return VLC_SUCCESS;
678 }
679
680 /*****************************************************************************
681  * SyncInfo: parse MPEG audio sync info
682  *****************************************************************************/
683 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
684                      unsigned int * pi_channels_conf,
685                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
686                      unsigned int * pi_frame_length,
687                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
688 {
689     static const int ppi_bitrate[2][3][16] =
690     {
691         {
692             /* v1 l1 */
693             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
694               416, 448, 0},
695             /* v1 l2 */
696             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
697               320, 384, 0},
698             /* v1 l3 */
699             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
700               256, 320, 0}
701         },
702
703         {
704             /* v2 l1 */
705             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
706               224, 256, 0},
707             /* v2 l2 */
708             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
709               144, 160, 0},
710             /* v2 l3 */
711             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
712               144, 160, 0}
713         }
714     };
715
716     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
717     {
718         { 44100, 48000, 32000, 0 },
719         { 22050, 24000, 16000, 0 }
720     };
721
722     int i_version, i_mode, i_emphasis;
723     vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
724     int i_frame_size = 0;
725     int i_bitrate_index, i_samplerate_index;
726     int i_max_bit_rate;
727
728     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
729     i_version   = 1 - ((i_header & 0x80000) >> 19);
730     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
731     b_crc = !((i_header >> 16) & 0x01);
732     i_bitrate_index = (i_header & 0xf000) >> 12;
733     i_samplerate_index = (i_header & 0xc00) >> 10;
734     b_padding   = (i_header & 0x200) >> 9;
735     /* Extension */
736     i_mode      = (i_header & 0xc0) >> 6;
737     /* Modeext, copyright & original */
738     i_emphasis  = i_header & 0x3;
739
740     if( *pi_layer != 4 &&
741         i_bitrate_index < 0x0f &&
742         i_samplerate_index != 0x03 &&
743         i_emphasis != 0x02 )
744     {
745         switch ( i_mode )
746         {
747         case 0: /* stereo */
748         case 1: /* joint stereo */
749             *pi_channels = 2;
750             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
751             break;
752         case 2: /* dual-mono */
753             *pi_channels = 2;
754             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
755                                 | AOUT_CHAN_DUALMONO;
756             break;
757         case 3: /* mono */
758             *pi_channels = 1;
759             *pi_channels_conf = AOUT_CHAN_CENTER;
760             break;
761         }
762         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
763         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
764         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
765
766         if ( b_mpeg_2_5 )
767         {
768             *pi_sample_rate >>= 1;
769         }
770
771         switch( *pi_layer )
772         {
773         case 1:
774             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
775                            b_padding ) * 4;
776             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
777                                  *pi_sample_rate + 1 ) * 4;
778             *pi_frame_length = 384;
779             break;
780
781         case 2:
782             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
783             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
784             *pi_frame_length = 1152;
785             break;
786
787         case 3:
788             i_frame_size = ( i_version ? 72000 : 144000 ) *
789                            *pi_bit_rate / *pi_sample_rate + b_padding;
790             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
791                                  i_max_bit_rate / *pi_sample_rate + 1;
792             *pi_frame_length = i_version ? 576 : 1152;
793             break;
794
795         default:
796             break;
797         }
798     }
799     else
800     {
801         return -1;
802     }
803
804     return i_frame_size;
805 }