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