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