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