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