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