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