]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
Correctly reset date after a discontinuity in audio decoder.
[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     audio_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  * OpenDecoder: probe the decoder and return score
131  *****************************************************************************/
132 static int OpenDecoder( 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_FOURCC('m','p','g','a') )
138     {
139         return VLC_EGENERIC;
140     }
141
142     /* HACK: Don't use this codec if we don't have an mpga audio filter */
143     if( p_dec->i_object_type == VLC_OBJECT_DECODER &&
144         !module_exists( "mpgatofixed32" ) )
145     {
146         return VLC_EGENERIC;
147     }
148
149     /* Allocate the memory needed to store the decoder's structure */
150     if( ( p_dec->p_sys = p_sys =
151           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
152         return VLC_ENOMEM;
153
154     /* Misc init */
155     p_sys->b_packetizer = false;
156     p_sys->i_state = STATE_NOSYNC;
157     aout_DateSet( &p_sys->end_date, 0 );
158     p_sys->bytestream = block_BytestreamInit();
159     p_sys->b_discontinuity = false;
160
161     /* Set output properties */
162     p_dec->fmt_out.i_cat = AUDIO_ES;
163     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
164     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
165
166     /* Set callback */
167     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
168         DecodeBlock;
169     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
170         DecodeBlock;
171
172     /* Start with the minimum size for a free bitrate frame */
173     p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
174
175     return VLC_SUCCESS;
176 }
177
178 static int OpenPacketizer( vlc_object_t *p_this )
179 {
180     decoder_t *p_dec = (decoder_t*)p_this;
181
182     int i_ret = OpenDecoder( p_this );
183
184     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
185
186     return i_ret;
187 }
188
189 /****************************************************************************
190  * DecodeBlock: the whole thing
191  ****************************************************************************
192  * This function is called just after the thread is launched.
193  ****************************************************************************/
194 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
195 {
196     decoder_sys_t *p_sys = p_dec->p_sys;
197     uint8_t p_header[MAD_BUFFER_GUARD];
198     uint32_t i_header;
199     uint8_t *p_buf;
200     void *p_out_buffer;
201
202     if( !pp_block || !*pp_block ) return NULL;
203
204     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
205     {
206         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
207         {
208             p_sys->i_state = STATE_NOSYNC;
209             block_BytestreamFlush( &p_sys->bytestream );
210         }
211         aout_DateSet( &p_sys->end_date, 0 );
212         block_Release( *pp_block );
213         p_sys->b_discontinuity = true;
214         return NULL;
215     }
216
217     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
218     {
219         /* We've just started the stream, wait for the first PTS. */
220         msg_Dbg( p_dec, "waiting for PTS" );
221         block_Release( *pp_block );
222         return NULL;
223     }
224
225     block_BytestreamPush( &p_sys->bytestream, *pp_block );
226
227     while( 1 )
228     {
229         switch( p_sys->i_state )
230         {
231
232         case STATE_NOSYNC:
233             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
234                    == VLC_SUCCESS )
235             {
236                 /* Look for sync word - should be 0xffe */
237                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
238                 {
239                     p_sys->i_state = STATE_SYNC;
240                     break;
241                 }
242                 block_SkipByte( &p_sys->bytestream );
243             }
244             if( p_sys->i_state != STATE_SYNC )
245             {
246                 block_BytestreamFlush( &p_sys->bytestream );
247
248                 /* Need more data */
249                 return NULL;
250             }
251
252         case STATE_SYNC:
253             /* New frame, set the Presentation Time Stamp */
254             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
255             if( p_sys->i_pts != 0 &&
256                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
257             {
258                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
259             }
260             p_sys->i_state = STATE_HEADER;
261
262         case STATE_HEADER:
263             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
264             if( block_PeekBytes( &p_sys->bytestream, p_header,
265                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
266             {
267                 /* Need more data */
268                 return NULL;
269             }
270
271             /* Build frame header */
272             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
273                        |p_header[3];
274
275             /* Check if frame is valid and get frame info */
276             p_sys->i_frame_size = SyncInfo( i_header,
277                                             &p_sys->i_channels,
278                                             &p_sys->i_channels_conf,
279                                             &p_sys->i_rate,
280                                             &p_sys->i_bit_rate,
281                                             &p_sys->i_frame_length,
282                                             &p_sys->i_max_frame_size,
283                                             &p_sys->i_layer );
284
285             if( p_sys->i_frame_size == -1 )
286             {
287                 msg_Dbg( p_dec, "emulated startcode" );
288                 block_SkipByte( &p_sys->bytestream );
289                 p_sys->i_state = STATE_NOSYNC;
290                 p_sys->b_discontinuity = true;
291                 break;
292             }
293
294             if( p_sys->i_bit_rate == 0 )
295             {
296                 /* Free bitrate, but 99% emulated startcode :( */
297                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
298                 {
299                     msg_Dbg( p_dec, "free bitrate mode");
300                 }
301                 /* The -1 below is to account for the frame padding */
302                 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
303             }
304
305             p_sys->i_state = STATE_NEXT_SYNC;
306
307         case STATE_NEXT_SYNC:
308             /* TODO: If p_block == NULL, flush the buffer without checking the
309              * next sync word */
310
311             /* Check if next expected frame contains the sync word */
312             if( block_PeekOffsetBytes( &p_sys->bytestream,
313                                        p_sys->i_frame_size, p_header,
314                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
315             {
316                 /* Need more data */
317                 return NULL;
318             }
319
320             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
321             {
322                 /* Startcode is fine, let's try the header as an extra check */
323                 int i_next_frame_size;
324                 unsigned int i_next_channels, i_next_channels_conf;
325                 unsigned int i_next_rate, i_next_bit_rate;
326                 unsigned int i_next_frame_length, i_next_max_frame_size;
327                 unsigned int i_next_layer;
328
329                 /* Build frame header */
330                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
331                            |p_header[3];
332
333                 i_next_frame_size = SyncInfo( i_header,
334                                               &i_next_channels,
335                                               &i_next_channels_conf,
336                                               &i_next_rate,
337                                               &i_next_bit_rate,
338                                               &i_next_frame_length,
339                                               &i_next_max_frame_size,
340                                               &i_next_layer );
341
342                 /* Free bitrate only */
343                 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
344                 {
345                     if( (unsigned int)p_sys->i_frame_size >
346                         p_sys->i_max_frame_size )
347                     {
348                         msg_Dbg( p_dec, "frame too big %d > %d "
349                                  "(emulated startcode ?)", p_sys->i_frame_size,
350                                  p_sys->i_max_frame_size );
351                         block_SkipByte( &p_sys->bytestream );
352                         p_sys->i_state = STATE_NOSYNC;
353                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
354                         break;
355                     }
356
357                     p_sys->i_frame_size++;
358                     break;
359                 }
360
361                 if( i_next_frame_size == -1 )
362                 {
363                     msg_Dbg( p_dec, "emulated startcode on next frame" );
364                     block_SkipByte( &p_sys->bytestream );
365                     p_sys->i_state = STATE_NOSYNC;
366                     p_sys->b_discontinuity = true;
367                     break;
368                 }
369
370                 /* Check info is in sync with previous one */
371                 if( i_next_channels_conf != p_sys->i_channels_conf ||
372                     i_next_rate != p_sys->i_rate ||
373                     i_next_layer != p_sys->i_layer ||
374                     i_next_frame_length != p_sys->i_frame_length )
375                 {
376                     /* Free bitrate only */
377                     if( p_sys->i_bit_rate == 0 )
378                     {
379                         p_sys->i_frame_size++;
380                         break;
381                     }
382
383                     msg_Dbg( p_dec, "parameters changed unexpectedly "
384                              "(emulated startcode ?)" );
385                     block_SkipByte( &p_sys->bytestream );
386                     p_sys->i_state = STATE_NOSYNC;
387                     break;
388                 }
389
390                 /* Free bitrate only */
391                 if( p_sys->i_bit_rate == 0 )
392                 {
393                     if( i_next_bit_rate != 0 )
394                     {
395                         p_sys->i_frame_size++;
396                         break;
397                     }
398                 }
399
400             }
401             else
402             {
403                 /* Free bitrate only */
404                 if( p_sys->i_bit_rate == 0 )
405                 {
406                     if( (unsigned int)p_sys->i_frame_size >
407                         p_sys->i_max_frame_size )
408                     {
409                         msg_Dbg( p_dec, "frame too big %d > %d "
410                                  "(emulated startcode ?)", p_sys->i_frame_size,
411                                  p_sys->i_max_frame_size );
412                         block_SkipByte( &p_sys->bytestream );
413                         p_sys->i_state = STATE_NOSYNC;
414                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
415                         break;
416                     }
417
418                     p_sys->i_frame_size++;
419                     break;
420                 }
421
422                 msg_Dbg( p_dec, "emulated startcode "
423                          "(no startcode on following frame)" );
424                 p_sys->i_state = STATE_NOSYNC;
425                 block_SkipByte( &p_sys->bytestream );
426                 break;
427             }
428
429             p_sys->i_state = STATE_SEND_DATA;
430             break;
431
432         case STATE_GET_DATA:
433             /* Make sure we have enough data.
434              * (Not useful if we went through NEXT_SYNC) */
435             if( block_WaitBytes( &p_sys->bytestream,
436                                  p_sys->i_frame_size ) != VLC_SUCCESS )
437             {
438                 /* Need more data */
439                 return NULL;
440             }
441             p_sys->i_state = STATE_SEND_DATA;
442
443         case STATE_SEND_DATA:
444             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
445             {
446                 //p_dec->b_error = true;
447                 return NULL;
448             }
449
450             /* Free bitrate only */
451             if( p_sys->i_bit_rate == 0 )
452             {
453                 p_sys->i_free_frame_size = p_sys->i_frame_size;
454             }
455
456             /* Copy the whole frame into the buffer. When we reach this point
457              * we already know we have enough data available. */
458             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
459
460             /* Get beginning of next frame for libmad */
461             if( !p_sys->b_packetizer )
462             {
463                 memcpy( p_buf + p_sys->i_frame_size,
464                         p_header, MAD_BUFFER_GUARD );
465             }
466
467             p_sys->i_state = STATE_NOSYNC;
468
469             /* Make sure we don't reuse the same pts twice */
470             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
471                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
472
473             /* So p_block doesn't get re-added several times */
474             *pp_block = block_BytestreamPop( &p_sys->bytestream );
475
476             return p_out_buffer;
477         }
478     }
479
480     return NULL;
481 }
482
483 /*****************************************************************************
484  * GetOutBuffer:
485  *****************************************************************************/
486 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
487 {
488     decoder_sys_t *p_sys = p_dec->p_sys;
489     uint8_t *p_buf;
490
491     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
492     {
493         msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
494                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
495
496         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
497         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
498     }
499
500     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
501     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
502     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
503     p_dec->fmt_out.audio.i_bytes_per_frame =
504         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
505
506     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
507     p_dec->fmt_out.audio.i_physical_channels =
508         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
509
510     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
511
512     if( p_sys->b_packetizer )
513     {
514         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
515         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
516         *pp_out_buffer = p_sout_buffer;
517     }
518     else
519     {
520         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
521         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
522         *pp_out_buffer = p_aout_buffer;
523     }
524
525     return p_buf;
526 }
527
528 /*****************************************************************************
529  * GetAoutBuffer:
530  *****************************************************************************/
531 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
532 {
533     decoder_sys_t *p_sys = p_dec->p_sys;
534     aout_buffer_t *p_buf;
535
536     p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
537     if( p_buf == NULL ) return NULL;
538
539     p_buf->start_date = aout_DateGet( &p_sys->end_date );
540     p_buf->end_date =
541         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
542     p_buf->b_discontinuity = p_sys->b_discontinuity;
543     p_sys->b_discontinuity = false;
544
545     /* Hack for libmad filter */
546     p_buf->i_nb_bytes = 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 = aout_DateGet( &p_sys->end_date );
563
564     p_block->i_length =
565         aout_DateIncrement( &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, b_crc;
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     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 }