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