]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
Spatializer: kill warnings and improve locality
[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 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 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_aout.h>
38 #include <vlc_modules.h>
39 #include <assert.h>
40
41 #include <vlc_block_helper.h>
42
43 #include "../packetizer/packetizer_helper.h"
44
45 /*****************************************************************************
46  * decoder_sys_t : decoder descriptor
47  *****************************************************************************/
48 struct decoder_sys_t
49 {
50     /* Module mode */
51     bool b_packetizer;
52
53     /*
54      * Input properties
55      */
56     int        i_state;
57
58     block_bytestream_t bytestream;
59
60     /*
61      * Common properties
62      */
63     date_t          end_date;
64     unsigned int    i_current_layer;
65
66     mtime_t i_pts;
67
68     int i_frame_size, i_free_frame_size;
69     unsigned int i_channels_conf, i_channels;
70     unsigned int i_rate, i_max_frame_size, i_frame_length;
71     unsigned int i_layer, i_bit_rate;
72
73     bool   b_discontinuity;
74 };
75
76 /* This isn't the place to put mad-specific stuff. However, it makes the
77  * mad plug-in's life much easier if we put 8 extra bytes at the end of the
78  * buffer, because that way it doesn't have to copy the aout_buffer_t to a
79  * bigger buffer. This has no implication on other plug-ins, and we only
80  * lose 8 bytes per frame. --Meuuh */
81 #define MAD_BUFFER_GUARD 8
82 #define MPGA_HEADER_SIZE 4
83
84 /****************************************************************************
85  * Local prototypes
86  ****************************************************************************/
87 static int  OpenDecoder   ( vlc_object_t * );
88 static int  OpenPacketizer( vlc_object_t * );
89 static void CloseDecoder  ( vlc_object_t * );
90 static block_t *DecodeBlock  ( decoder_t *, block_t ** );
91
92 static uint8_t       *GetOutBuffer ( decoder_t *, block_t ** );
93 static aout_buffer_t *GetAoutBuffer( decoder_t * );
94 static block_t       *GetSoutBuffer( decoder_t * );
95
96 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
97                      unsigned int * pi_channels_conf,
98                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
99                      unsigned int * pi_frame_length,
100                      unsigned int * pi_max_frame_size,
101                      unsigned int * pi_layer );
102
103 /*****************************************************************************
104  * Module descriptor
105  *****************************************************************************/
106 vlc_module_begin ()
107     set_description( N_("MPEG audio layer I/II/III decoder") )
108     set_category( CAT_INPUT )
109     set_subcategory( SUBCAT_INPUT_ACODEC )
110 #if defined(UNDER_CE)
111    set_capability( "decoder", 5 )
112 #else
113     set_capability( "decoder", 100 )
114 #endif
115     set_callbacks( OpenDecoder, CloseDecoder )
116
117     add_submodule ()
118     set_description( N_("MPEG audio layer I/II/III packetizer") )
119     set_capability( "packetizer", 10 )
120     set_callbacks( OpenPacketizer, CloseDecoder )
121 vlc_module_end ()
122
123 /*****************************************************************************
124  * Open: probe the decoder and return score
125  *****************************************************************************/
126 static int Open( vlc_object_t *p_this )
127 {
128     decoder_t *p_dec = (decoder_t*)p_this;
129     decoder_sys_t *p_sys;
130
131     if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA )
132     {
133         return VLC_EGENERIC;
134     }
135
136     /* Allocate the memory needed to store the decoder's structure */
137     if( ( p_dec->p_sys = p_sys =
138           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
139         return VLC_ENOMEM;
140
141     /* Misc init */
142     p_sys->b_packetizer = false;
143     p_sys->i_state = STATE_NOSYNC;
144     date_Set( &p_sys->end_date, 0 );
145     block_BytestreamInit( &p_sys->bytestream );
146     p_sys->i_pts = VLC_TS_INVALID;
147     p_sys->b_discontinuity = false;
148
149     /* Set output properties */
150     p_dec->fmt_out.i_cat = AUDIO_ES;
151     p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
152     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
153
154     /* Set callback */
155     p_dec->pf_decode_audio = DecodeBlock;
156     p_dec->pf_packetize    = DecodeBlock;
157
158     /* Start with the minimum size for a free bitrate frame */
159     p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
160
161     return VLC_SUCCESS;
162 }
163
164 static int OpenDecoder( vlc_object_t *p_this )
165 {
166     /* HACK: Don't use this codec if we don't have an mpga audio filter */
167     if( !module_exists( "mpgatofixed32" ) )
168         return VLC_EGENERIC;
169
170     return Open( p_this );
171 }
172
173 static int OpenPacketizer( vlc_object_t *p_this )
174 {
175     decoder_t *p_dec = (decoder_t*)p_this;
176
177     int i_ret = Open( p_this );
178
179     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
180
181     return i_ret;
182 }
183
184 /****************************************************************************
185  * DecodeBlock: the whole thing
186  ****************************************************************************
187  * This function is called just after the thread is launched.
188  ****************************************************************************/
189 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
190 {
191     decoder_sys_t *p_sys = p_dec->p_sys;
192     uint8_t p_header[MAD_BUFFER_GUARD];
193     uint32_t i_header;
194     uint8_t *p_buf;
195     block_t *p_out_buffer;
196
197     if( !pp_block || !*pp_block ) return NULL;
198
199     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
200     {
201         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
202         {
203             p_sys->i_state = STATE_NOSYNC;
204             block_BytestreamEmpty( &p_sys->bytestream );
205         }
206         date_Set( &p_sys->end_date, 0 );
207         block_Release( *pp_block );
208         p_sys->b_discontinuity = true;
209         return NULL;
210     }
211
212     if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
213     {
214         /* We've just started the stream, wait for the first PTS. */
215         msg_Dbg( p_dec, "waiting for PTS" );
216         block_Release( *pp_block );
217         return NULL;
218     }
219
220     block_BytestreamPush( &p_sys->bytestream, *pp_block );
221
222     while( 1 )
223     {
224         switch( p_sys->i_state )
225         {
226
227         case STATE_NOSYNC:
228             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
229                    == VLC_SUCCESS )
230             {
231                 /* Look for sync word - should be 0xffe */
232                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
233                 {
234                     p_sys->i_state = STATE_SYNC;
235                     break;
236                 }
237                 block_SkipByte( &p_sys->bytestream );
238             }
239             if( p_sys->i_state != STATE_SYNC )
240             {
241                 block_BytestreamFlush( &p_sys->bytestream );
242
243                 /* Need more data */
244                 return NULL;
245             }
246
247         case STATE_SYNC:
248             /* New frame, set the Presentation Time Stamp */
249             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
250             if( p_sys->i_pts > VLC_TS_INVALID &&
251                 p_sys->i_pts != date_Get( &p_sys->end_date ) )
252             {
253                 date_Set( &p_sys->end_date, p_sys->i_pts );
254             }
255             p_sys->i_state = STATE_HEADER;
256
257         case STATE_HEADER:
258             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
259             if( block_PeekBytes( &p_sys->bytestream, p_header,
260                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
261             {
262                 /* Need more data */
263                 return NULL;
264             }
265
266             /* Build frame header */
267             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
268                        |p_header[3];
269
270             /* Check if frame is valid and get frame info */
271             p_sys->i_frame_size = SyncInfo( i_header,
272                                             &p_sys->i_channels,
273                                             &p_sys->i_channels_conf,
274                                             &p_sys->i_rate,
275                                             &p_sys->i_bit_rate,
276                                             &p_sys->i_frame_length,
277                                             &p_sys->i_max_frame_size,
278                                             &p_sys->i_layer );
279
280             p_dec->fmt_in.i_profile = p_sys->i_layer;
281
282             if( p_sys->i_frame_size == -1 )
283             {
284                 msg_Dbg( p_dec, "emulated startcode" );
285                 block_SkipByte( &p_sys->bytestream );
286                 p_sys->i_state = STATE_NOSYNC;
287                 p_sys->b_discontinuity = true;
288                 break;
289             }
290
291             if( p_sys->i_bit_rate == 0 )
292             {
293                 /* Free bitrate, but 99% emulated startcode :( */
294                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
295                 {
296                     msg_Dbg( p_dec, "free bitrate mode");
297                 }
298                 /* The -1 below is to account for the frame padding */
299                 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
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 = 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 = 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,
456                             p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) );
457
458             /* Get beginning of next frame for libmad */
459             if( !p_sys->b_packetizer )
460             {
461                 assert( p_out_buffer->i_buffer >= (unsigned)p_sys->i_frame_size + MAD_BUFFER_GUARD );
462                 memcpy( p_buf + p_sys->i_frame_size,
463                         p_header, MAD_BUFFER_GUARD );
464             }
465
466             p_sys->i_state = STATE_NOSYNC;
467
468             /* Make sure we don't reuse the same pts twice */
469             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
470                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
471
472             /* So p_block doesn't get re-added several times */
473             *pp_block = block_BytestreamPop( &p_sys->bytestream );
474
475             return p_out_buffer;
476         }
477     }
478
479     return NULL;
480 }
481
482 /*****************************************************************************
483  * GetOutBuffer:
484  *****************************************************************************/
485 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
486 {
487     decoder_sys_t *p_sys = p_dec->p_sys;
488     uint8_t *p_buf;
489
490     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
491     {
492         msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
493                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
494
495         date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
496         date_Set( &p_sys->end_date, p_sys->i_pts );
497     }
498
499     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
500     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
501     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
502     p_dec->fmt_out.audio.i_bytes_per_frame =
503         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
504
505     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
506     p_dec->fmt_out.audio.i_physical_channels =
507         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
508
509     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
510
511     if( p_sys->b_packetizer )
512     {
513         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
514         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
515         *pp_out_buffer = p_sout_buffer;
516     }
517     else
518     {
519         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
520         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
521         *pp_out_buffer = p_aout_buffer;
522     }
523
524     return p_buf;
525 }
526
527 /*****************************************************************************
528  * GetAoutBuffer:
529  *****************************************************************************/
530 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
531 {
532     decoder_sys_t *p_sys = p_dec->p_sys;
533     aout_buffer_t *p_buf;
534
535     p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
536     if( p_buf == NULL ) return NULL;
537
538     p_buf->i_pts = date_Get( &p_sys->end_date );
539     p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
540                       - p_buf->i_pts;
541     if( p_sys->b_discontinuity )
542         p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
543     p_sys->b_discontinuity = false;
544
545     /* Hack for libmad filter */
546     p_buf = block_Realloc( p_buf, 0, p_sys->i_frame_size + MAD_BUFFER_GUARD );
547
548     return p_buf;
549 }
550
551 /*****************************************************************************
552  * GetSoutBuffer:
553  *****************************************************************************/
554 static block_t *GetSoutBuffer( decoder_t *p_dec )
555 {
556     decoder_sys_t *p_sys = p_dec->p_sys;
557     block_t *p_block;
558
559     p_block = block_New( p_dec, p_sys->i_frame_size );
560     if( p_block == NULL ) return NULL;
561
562     p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
563
564     p_block->i_length =
565         date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
566
567     return p_block;
568 }
569
570 /*****************************************************************************
571  * CloseDecoder: clean up the decoder
572  *****************************************************************************/
573 static void CloseDecoder( vlc_object_t *p_this )
574 {
575     decoder_t *p_dec = (decoder_t *)p_this;
576     decoder_sys_t *p_sys = p_dec->p_sys;
577
578     block_BytestreamRelease( &p_sys->bytestream );
579
580     free( p_sys );
581 }
582
583 /*****************************************************************************
584  * SyncInfo: parse MPEG audio sync info
585  *****************************************************************************/
586 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
587                      unsigned int * pi_channels_conf,
588                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
589                      unsigned int * pi_frame_length,
590                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
591 {
592     static const int ppi_bitrate[2][3][16] =
593     {
594         {
595             /* v1 l1 */
596             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
597               416, 448, 0},
598             /* v1 l2 */
599             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
600               320, 384, 0},
601             /* v1 l3 */
602             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
603               256, 320, 0}
604         },
605
606         {
607             /* v2 l1 */
608             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
609               224, 256, 0},
610             /* v2 l2 */
611             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
612               144, 160, 0},
613             /* v2 l3 */
614             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
615               144, 160, 0}
616         }
617     };
618
619     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
620     {
621         { 44100, 48000, 32000, 0 },
622         { 22050, 24000, 16000, 0 }
623     };
624
625     int i_version, i_mode, i_emphasis;
626     bool b_padding, b_mpeg_2_5;
627     int i_frame_size = 0;
628     int i_bitrate_index, i_samplerate_index;
629     int i_max_bit_rate;
630
631     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
632     i_version   = 1 - ((i_header & 0x80000) >> 19);
633     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
634     //bool b_crc = !((i_header >> 16) & 0x01);
635     i_bitrate_index = (i_header & 0xf000) >> 12;
636     i_samplerate_index = (i_header & 0xc00) >> 10;
637     b_padding   = (i_header & 0x200) >> 9;
638     /* Extension */
639     i_mode      = (i_header & 0xc0) >> 6;
640     /* Modeext, copyright & original */
641     i_emphasis  = i_header & 0x3;
642
643     if( *pi_layer != 4 &&
644         i_bitrate_index < 0x0f &&
645         i_samplerate_index != 0x03 &&
646         i_emphasis != 0x02 )
647     {
648         switch ( i_mode )
649         {
650         case 0: /* stereo */
651         case 1: /* joint stereo */
652             *pi_channels = 2;
653             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
654             break;
655         case 2: /* dual-mono */
656             *pi_channels = 2;
657             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
658                                 | AOUT_CHAN_DUALMONO;
659             break;
660         case 3: /* mono */
661             *pi_channels = 1;
662             *pi_channels_conf = AOUT_CHAN_CENTER;
663             break;
664         }
665         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
666         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
667         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
668
669         if ( b_mpeg_2_5 )
670         {
671             *pi_sample_rate >>= 1;
672         }
673
674         switch( *pi_layer )
675         {
676         case 1:
677             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
678                            b_padding ) * 4;
679             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
680                                  *pi_sample_rate + 1 ) * 4;
681             *pi_frame_length = 384;
682             break;
683
684         case 2:
685             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
686             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
687             *pi_frame_length = 1152;
688             break;
689
690         case 3:
691             i_frame_size = ( i_version ? 72000 : 144000 ) *
692                            *pi_bit_rate / *pi_sample_rate + b_padding;
693             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
694                                  i_max_bit_rate / *pi_sample_rate + 1;
695             *pi_frame_length = i_version ? 576 : 1152;
696             break;
697
698         default:
699             break;
700         }
701
702         /* Free bitrate mode can support higher bitrates */
703         if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
704     }
705     else
706     {
707         return -1;
708     }
709
710     return i_frame_size;
711 }