Fixed use of released pointer (race condition).
[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 the VideoLAN team
5  * $Id$
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@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31 #include <vlc_codec.h>
32 #include <vlc_aout.h>
33
34 #include <vlc_block_helper.h>
35
36 /*****************************************************************************
37  * decoder_sys_t : decoder descriptor
38  *****************************************************************************/
39 struct decoder_sys_t
40 {
41     /* Module mode */
42     vlc_bool_t b_packetizer;
43
44     /*
45      * Input properties
46      */
47     int        i_state;
48
49     block_bytestream_t bytestream;
50
51     /*
52      * Common properties
53      */
54     audio_date_t          end_date;
55     unsigned int          i_current_layer;
56
57     mtime_t i_pts;
58
59     int i_frame_size, i_free_frame_size;
60     unsigned int i_channels_conf, i_channels;
61     unsigned int i_rate, i_max_frame_size, i_frame_length;
62     unsigned int i_layer, i_bit_rate;
63
64     vlc_bool_t   b_discontinuity;
65 };
66
67 enum {
68
69     STATE_NOSYNC,
70     STATE_SYNC,
71     STATE_HEADER,
72     STATE_NEXT_SYNC,
73     STATE_GET_DATA,
74     STATE_SEND_DATA
75 };
76
77 /* This isn't the place to put mad-specific stuff. However, it makes the
78  * mad plug-in's life much easier if we put 8 extra bytes at the end of the
79  * buffer, because that way it doesn't have to copy the aout_buffer_t to a
80  * bigger buffer. This has no implication on other plug-ins, and we only
81  * lose 8 bytes per frame. --Meuuh */
82 #define MAD_BUFFER_GUARD 8
83 #define MPGA_HEADER_SIZE 4
84
85 /****************************************************************************
86  * Local prototypes
87  ****************************************************************************/
88 static int  OpenDecoder   ( vlc_object_t * );
89 static int  OpenPacketizer( vlc_object_t * );
90 static void CloseDecoder  ( vlc_object_t * );
91 static void *DecodeBlock  ( decoder_t *, block_t ** );
92
93 static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
94 static aout_buffer_t *GetAoutBuffer( decoder_t * );
95 static block_t       *GetSoutBuffer( decoder_t * );
96
97 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
98                      unsigned int * pi_channels_conf,
99                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
100                      unsigned int * pi_frame_length,
101                      unsigned int * pi_max_frame_size,
102                      unsigned int * pi_layer );
103
104 /*****************************************************************************
105  * Module descriptor
106  *****************************************************************************/
107 vlc_module_begin();
108     set_description( _("MPEG audio layer I/II/III decoder") );
109     set_category( CAT_INPUT );
110     set_subcategory( SUBCAT_INPUT_ACODEC );
111 #if defined(UNDER_CE)
112    set_capability( "decoder", 5 );
113 #else
114     set_capability( "decoder", 100 );
115 #endif
116     set_callbacks( OpenDecoder, CloseDecoder );
117
118     add_submodule();
119     set_description( _("MPEG audio layer I/II/III packetizer") );
120     set_capability( "packetizer", 10 );
121     set_callbacks( OpenPacketizer, CloseDecoder );
122 vlc_module_end();
123
124 /*****************************************************************************
125  * OpenDecoder: probe the decoder and return score
126  *****************************************************************************/
127 static int OpenDecoder( vlc_object_t *p_this )
128 {
129     decoder_t *p_dec = (decoder_t*)p_this;
130     decoder_sys_t *p_sys;
131
132     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
133     {
134         return VLC_EGENERIC;
135     }
136
137     /* HACK: Don't use this codec if we don't have an mpga audio filter */
138     if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
139         !config_FindModule( p_this, "mpgatofixed32" ) )
140     {
141         return VLC_EGENERIC;
142     }
143
144     /* Allocate the memory needed to store the decoder's structure */
145     if( ( p_dec->p_sys = p_sys =
146           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
147     {
148         msg_Err( p_dec, "out of memory" );
149         return VLC_EGENERIC;
150     }
151
152     /* Misc init */
153     p_sys->b_packetizer = VLC_FALSE;
154     p_sys->i_state = STATE_NOSYNC;
155     aout_DateSet( &p_sys->end_date, 0 );
156     p_sys->bytestream = block_BytestreamInit( p_dec );
157     p_sys->b_discontinuity = VLC_FALSE;
158
159     /* Set output properties */
160     p_dec->fmt_out.i_cat = AUDIO_ES;
161     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
162     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
163
164     /* Set callback */
165     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
166         DecodeBlock;
167     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
168         DecodeBlock;
169
170     /* Start with the minimum size for a free bitrate frame */
171     p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
172
173     return VLC_SUCCESS;
174 }
175
176 static int OpenPacketizer( vlc_object_t *p_this )
177 {
178     decoder_t *p_dec = (decoder_t*)p_this;
179
180     int i_ret = OpenDecoder( p_this );
181
182     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
183
184     return i_ret;
185 }
186
187 /****************************************************************************
188  * DecodeBlock: the whole thing
189  ****************************************************************************
190  * This function is called just after the thread is launched.
191  ****************************************************************************/
192 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
193 {
194     decoder_sys_t *p_sys = p_dec->p_sys;
195     uint8_t p_header[MAD_BUFFER_GUARD];
196     uint32_t i_header;
197     uint8_t *p_buf;
198     void *p_out_buffer;
199
200     if( !pp_block || !*pp_block ) return NULL;
201
202     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
203     {
204         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
205         {
206             p_sys->i_state = STATE_NOSYNC;
207             block_BytestreamFlush( &p_sys->bytestream );
208         }
209 //        aout_DateSet( &p_sys->end_date, 0 );
210         block_Release( *pp_block );
211         p_sys->b_discontinuity = VLC_TRUE;
212         return NULL;
213     }
214
215     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
216     {
217         /* We've just started the stream, wait for the first PTS. */
218         msg_Dbg( p_dec, "waiting for PTS" );
219         block_Release( *pp_block );
220         return NULL;
221     }
222
223     block_BytestreamPush( &p_sys->bytestream, *pp_block );
224
225     while( 1 )
226     {
227         switch( p_sys->i_state )
228         {
229
230         case STATE_NOSYNC:
231             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
232                    == VLC_SUCCESS )
233             {
234                 /* Look for sync word - should be 0xffe */
235                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
236                 {
237                     p_sys->i_state = STATE_SYNC;
238                     break;
239                 }
240                 block_SkipByte( &p_sys->bytestream );
241             }
242             if( p_sys->i_state != STATE_SYNC )
243             {
244                 block_BytestreamFlush( &p_sys->bytestream );
245
246                 /* Need more data */
247                 return NULL;
248             }
249
250         case STATE_SYNC:
251             /* New frame, set the Presentation Time Stamp */
252             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
253             if( p_sys->i_pts != 0 &&
254                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
255             {
256                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
257             }
258             p_sys->i_state = STATE_HEADER;
259
260         case STATE_HEADER:
261             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
262             if( block_PeekBytes( &p_sys->bytestream, p_header,
263                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
264             {
265                 /* Need more data */
266                 return NULL;
267             }
268
269             /* Build frame header */
270             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
271                        |p_header[3];
272
273             /* Check if frame is valid and get frame info */
274             p_sys->i_frame_size = SyncInfo( i_header,
275                                             &p_sys->i_channels,
276                                             &p_sys->i_channels_conf,
277                                             &p_sys->i_rate,
278                                             &p_sys->i_bit_rate,
279                                             &p_sys->i_frame_length,
280                                             &p_sys->i_max_frame_size,
281                                             &p_sys->i_layer );
282
283             if( p_sys->i_frame_size == -1 )
284             {
285                 msg_Dbg( p_dec, "emulated startcode" );
286                 block_SkipByte( &p_sys->bytestream );
287                 p_sys->i_state = STATE_NOSYNC;
288                 p_sys->b_discontinuity = VLC_TRUE;
289                 break;
290             }
291
292             if( p_sys->i_bit_rate == 0 )
293             {
294                 /* Free birate, but 99% emulated startcode :( */
295                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
296                 {
297                     msg_Dbg( p_dec, "free bitrate mode");
298                 }
299                 p_sys->i_frame_size = p_sys->i_free_frame_size;
300             }
301
302             p_sys->i_state = STATE_NEXT_SYNC;
303
304         case STATE_NEXT_SYNC:
305             /* TODO: If p_block == NULL, flush the buffer without checking the
306              * next sync word */
307
308             /* Check if next expected frame contains the sync word */
309             if( block_PeekOffsetBytes( &p_sys->bytestream,
310                                        p_sys->i_frame_size, p_header,
311                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
312             {
313                 /* Need more data */
314                 return NULL;
315             }
316
317             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
318             {
319                 /* Startcode is fine, let's try the header as an extra check */
320                 int i_next_frame_size;
321                 unsigned int i_next_channels, i_next_channels_conf;
322                 unsigned int i_next_rate, i_next_bit_rate;
323                 unsigned int i_next_frame_length, i_next_max_frame_size;
324                 unsigned int i_next_layer;
325
326                 /* Build frame header */
327                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
328                            |p_header[3];
329
330                 i_next_frame_size = SyncInfo( i_header,
331                                               &i_next_channels,
332                                               &i_next_channels_conf,
333                                               &i_next_rate,
334                                               &i_next_bit_rate,
335                                               &i_next_frame_length,
336                                               &i_next_max_frame_size,
337                                               &i_next_layer );
338
339                 /* Free bitrate only */
340                 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
341                 {
342                     if( (unsigned int)p_sys->i_frame_size >
343                         p_sys->i_max_frame_size )
344                     {
345                         msg_Dbg( p_dec, "frame too big %d > %d "
346                                  "(emulated startcode ?)", p_sys->i_frame_size,
347                                  p_sys->i_max_frame_size );
348                         block_SkipByte( &p_sys->bytestream );
349                         p_sys->i_state = STATE_NOSYNC;
350                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
351                         break;
352                     }
353
354                     p_sys->i_frame_size++;
355                     break;
356                 }
357
358                 if( i_next_frame_size == -1 )
359                 {
360                     msg_Dbg( p_dec, "emulated startcode on next frame" );
361                     block_SkipByte( &p_sys->bytestream );
362                     p_sys->i_state = STATE_NOSYNC;
363                     p_sys->b_discontinuity = VLC_TRUE;
364                     break;
365                 }
366
367                 /* Check info is in sync with previous one */
368                 if( i_next_channels_conf != p_sys->i_channels_conf ||
369                     i_next_rate != p_sys->i_rate ||
370                     i_next_layer != p_sys->i_layer ||
371                     i_next_frame_length != p_sys->i_frame_length )
372                 {
373                     /* Free bitrate only */
374                     if( p_sys->i_bit_rate == 0 )
375                     {
376                         p_sys->i_frame_size++;
377                         break;
378                     }
379
380                     msg_Dbg( p_dec, "parameters changed unexpectedly "
381                              "(emulated startcode ?)" );
382                     block_SkipByte( &p_sys->bytestream );
383                     p_sys->i_state = STATE_NOSYNC;
384                     break;
385                 }
386
387                 /* Free bitrate only */
388                 if( p_sys->i_bit_rate == 0 )
389                 {
390                     if( i_next_bit_rate != 0 )
391                     {
392                         p_sys->i_frame_size++;
393                         break;
394                     }
395                 }
396
397             }
398             else
399             {
400                 /* Free bitrate only */
401                 if( p_sys->i_bit_rate == 0 )
402                 {
403                     if( (unsigned int)p_sys->i_frame_size >
404                         p_sys->i_max_frame_size )
405                     {
406                         msg_Dbg( p_dec, "frame too big %d > %d "
407                                  "(emulated startcode ?)", p_sys->i_frame_size,
408                                  p_sys->i_max_frame_size );
409                         block_SkipByte( &p_sys->bytestream );
410                         p_sys->i_state = STATE_NOSYNC;
411                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
412                         break;
413                     }
414
415                     p_sys->i_frame_size++;
416                     break;
417                 }
418
419                 msg_Dbg( p_dec, "emulated startcode "
420                          "(no startcode on following frame)" );
421                 p_sys->i_state = STATE_NOSYNC;
422                 block_SkipByte( &p_sys->bytestream );
423                 break;
424             }
425
426             p_sys->i_state = STATE_SEND_DATA;
427             break;
428
429         case STATE_GET_DATA:
430             /* Make sure we have enough data.
431              * (Not useful if we went through NEXT_SYNC) */
432             if( block_WaitBytes( &p_sys->bytestream,
433                                  p_sys->i_frame_size ) != VLC_SUCCESS )
434             {
435                 /* Need more data */
436                 return NULL;
437             }
438             p_sys->i_state = STATE_SEND_DATA;
439
440         case STATE_SEND_DATA:
441             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
442             {
443                 //p_dec->b_error = VLC_TRUE;
444                 return NULL;
445             }
446
447             /* Free bitrate only */
448             if( p_sys->i_bit_rate == 0 )
449             {
450                 p_sys->i_free_frame_size = p_sys->i_frame_size;
451             }
452
453             /* Copy the whole frame into the buffer. When we reach this point
454              * we already know we have enough data available. */
455             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
456
457             /* Get beginning of next frame for libmad */
458             if( !p_sys->b_packetizer )
459             {
460                 memcpy( p_buf + p_sys->i_frame_size,
461                         p_header, MAD_BUFFER_GUARD );
462             }
463
464             p_sys->i_state = STATE_NOSYNC;
465
466             /* Make sure we don't reuse the same pts twice */
467             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
468                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
469
470             /* So p_block doesn't get re-added several times */
471             *pp_block = block_BytestreamPop( &p_sys->bytestream );
472
473             return p_out_buffer;
474         }
475     }
476
477     return NULL;
478 }
479
480 /*****************************************************************************
481  * GetOutBuffer:
482  *****************************************************************************/
483 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
484 {
485     decoder_sys_t *p_sys = p_dec->p_sys;
486     uint8_t *p_buf;
487
488     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
489     {
490         msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
491                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
492
493         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
494         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
495     }
496
497     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
498     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
499     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
500     p_dec->fmt_out.audio.i_bytes_per_frame =
501         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
502
503     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
504     p_dec->fmt_out.audio.i_physical_channels =
505         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
506
507     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
508
509     if( p_sys->b_packetizer )
510     {
511         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
512         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
513         *pp_out_buffer = p_sout_buffer;
514     }
515     else
516     {
517         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
518         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
519         *pp_out_buffer = p_aout_buffer;
520     }
521
522     return p_buf;
523 }
524
525 /*****************************************************************************
526  * GetAoutBuffer:
527  *****************************************************************************/
528 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
529 {
530     decoder_sys_t *p_sys = p_dec->p_sys;
531     aout_buffer_t *p_buf;
532
533     p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
534     if( p_buf == NULL ) return NULL;
535
536     p_buf->start_date = aout_DateGet( &p_sys->end_date );
537     p_buf->end_date =
538         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
539     p_buf->b_discontinuity = p_sys->b_discontinuity;
540     p_sys->b_discontinuity = VLC_FALSE;
541
542     /* Hack for libmad filter */
543     p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
544
545     return p_buf;
546 }
547
548 /*****************************************************************************
549  * GetSoutBuffer:
550  *****************************************************************************/
551 static block_t *GetSoutBuffer( decoder_t *p_dec )
552 {
553     decoder_sys_t *p_sys = p_dec->p_sys;
554     block_t *p_block;
555
556     p_block = block_New( p_dec, p_sys->i_frame_size );
557     if( p_block == NULL ) return NULL;
558
559     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
560
561     p_block->i_length =
562         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
563             p_block->i_pts;
564
565     return p_block;
566 }
567
568 /*****************************************************************************
569  * CloseDecoder: clean up the decoder
570  *****************************************************************************/
571 static void CloseDecoder( vlc_object_t *p_this )
572 {
573     decoder_t *p_dec = (decoder_t *)p_this;
574     decoder_sys_t *p_sys = p_dec->p_sys;
575
576     block_BytestreamRelease( &p_sys->bytestream );
577
578     free( p_sys );
579 }
580
581 /*****************************************************************************
582  * SyncInfo: parse MPEG audio sync info
583  *****************************************************************************/
584 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
585                      unsigned int * pi_channels_conf,
586                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
587                      unsigned int * pi_frame_length,
588                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
589 {
590     static const int ppi_bitrate[2][3][16] =
591     {
592         {
593             /* v1 l1 */
594             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
595               416, 448, 0},
596             /* v1 l2 */
597             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
598               320, 384, 0},
599             /* v1 l3 */
600             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
601               256, 320, 0}
602         },
603
604         {
605             /* v2 l1 */
606             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
607               224, 256, 0},
608             /* v2 l2 */
609             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
610               144, 160, 0},
611             /* v2 l3 */
612             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
613               144, 160, 0}
614         }
615     };
616
617     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
618     {
619         { 44100, 48000, 32000, 0 },
620         { 22050, 24000, 16000, 0 }
621     };
622
623     int i_version, i_mode, i_emphasis;
624     vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
625     int i_frame_size = 0;
626     int i_bitrate_index, i_samplerate_index;
627     int i_max_bit_rate;
628
629     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
630     i_version   = 1 - ((i_header & 0x80000) >> 19);
631     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
632     b_crc = !((i_header >> 16) & 0x01);
633     i_bitrate_index = (i_header & 0xf000) >> 12;
634     i_samplerate_index = (i_header & 0xc00) >> 10;
635     b_padding   = (i_header & 0x200) >> 9;
636     /* Extension */
637     i_mode      = (i_header & 0xc0) >> 6;
638     /* Modeext, copyright & original */
639     i_emphasis  = i_header & 0x3;
640
641     if( *pi_layer != 4 &&
642         i_bitrate_index < 0x0f &&
643         i_samplerate_index != 0x03 &&
644         i_emphasis != 0x02 )
645     {
646         switch ( i_mode )
647         {
648         case 0: /* stereo */
649         case 1: /* joint stereo */
650             *pi_channels = 2;
651             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
652             break;
653         case 2: /* dual-mono */
654             *pi_channels = 2;
655             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
656                                 | AOUT_CHAN_DUALMONO;
657             break;
658         case 3: /* mono */
659             *pi_channels = 1;
660             *pi_channels_conf = AOUT_CHAN_CENTER;
661             break;
662         }
663         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
664         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
665         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
666
667         if ( b_mpeg_2_5 )
668         {
669             *pi_sample_rate >>= 1;
670         }
671
672         switch( *pi_layer )
673         {
674         case 1:
675             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
676                            b_padding ) * 4;
677             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
678                                  *pi_sample_rate + 1 ) * 4;
679             *pi_frame_length = 384;
680             break;
681
682         case 2:
683             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
684             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
685             *pi_frame_length = 1152;
686             break;
687
688         case 3:
689             i_frame_size = ( i_version ? 72000 : 144000 ) *
690                            *pi_bit_rate / *pi_sample_rate + b_padding;
691             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
692                                  i_max_bit_rate / *pi_sample_rate + 1;
693             *pi_frame_length = i_version ? 576 : 1152;
694             break;
695
696         default:
697             break;
698         }
699     }
700     else
701     {
702         return -1;
703     }
704
705     return i_frame_size;
706 }