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