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