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