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