]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
Copyright fixes
[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 VideoLAN (Centrale Réseaux) and its contributors
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31 #include <vlc/decoder.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 parser") );
107     set_category( CAT_INPUT );
108     set_subcategory( SUBCAT_INPUT_ACODEC );
109 #if defined(SYS_DARWIN) || 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         block_Release( *pp_block );
203         return NULL;
204     }
205
206     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
207     {
208         p_sys->i_state = STATE_NOSYNC;
209     }
210
211     block_BytestreamPush( &p_sys->bytestream, *pp_block );
212
213     while( 1 )
214     {
215         switch( p_sys->i_state )
216         {
217
218         case STATE_NOSYNC:
219             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
220                    == VLC_SUCCESS )
221             {
222                 /* Look for sync word - should be 0xffe */
223                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
224                 {
225                     p_sys->i_state = STATE_SYNC;
226                     break;
227                 }
228                 block_SkipByte( &p_sys->bytestream );
229             }
230             if( p_sys->i_state != STATE_SYNC )
231             {
232                 block_BytestreamFlush( &p_sys->bytestream );
233
234                 /* Need more data */
235                 return NULL;
236             }
237
238         case STATE_SYNC:
239             /* New frame, set the Presentation Time Stamp */
240             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
241             if( p_sys->i_pts != 0 &&
242                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
243             {
244                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
245             }
246             p_sys->i_state = STATE_HEADER;
247
248         case STATE_HEADER:
249             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
250             if( block_PeekBytes( &p_sys->bytestream, p_header,
251                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
252             {
253                 /* Need more data */
254                 return NULL;
255             }
256
257             /* Build frame header */
258             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
259                        |p_header[3];
260
261             /* Check if frame is valid and get frame info */
262             p_sys->i_frame_size = SyncInfo( i_header,
263                                             &p_sys->i_channels,
264                                             &p_sys->i_channels_conf,
265                                             &p_sys->i_rate,
266                                             &p_sys->i_bit_rate,
267                                             &p_sys->i_frame_length,
268                                             &p_sys->i_max_frame_size,
269                                             &p_sys->i_layer );
270
271             if( p_sys->i_frame_size == -1 )
272             {
273                 msg_Dbg( p_dec, "emulated startcode" );
274                 block_SkipByte( &p_sys->bytestream );
275                 p_sys->i_state = STATE_NOSYNC;
276                 break;
277             }
278
279             if( p_sys->i_bit_rate == 0 )
280             {
281                 /* Free birate, but 99% emulated startcode :( */
282                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
283                 {
284                     msg_Dbg( p_dec, "free bitrate mode");
285                 }
286                 p_sys->i_frame_size = p_sys->i_free_frame_size;
287             }
288
289             p_sys->i_state = STATE_NEXT_SYNC;
290
291         case STATE_NEXT_SYNC:
292             /* TODO: If p_block == NULL, flush the buffer without checking the
293              * next sync word */
294
295             /* Check if next expected frame contains the sync word */
296             if( block_PeekOffsetBytes( &p_sys->bytestream,
297                                        p_sys->i_frame_size, p_header,
298                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
299             {
300                 /* Need more data */
301                 return NULL;
302             }
303
304             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
305             {
306                 /* Startcode is fine, let's try the header as an extra check */
307                 int i_next_frame_size;
308                 unsigned int i_next_channels, i_next_channels_conf;
309                 unsigned int i_next_rate, i_next_bit_rate;
310                 unsigned int i_next_frame_length, i_next_max_frame_size;
311                 unsigned int i_next_layer;
312
313                 /* Build frame header */
314                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
315                            |p_header[3];
316
317                 i_next_frame_size = SyncInfo( i_header,
318                                               &i_next_channels,
319                                               &i_next_channels_conf,
320                                               &i_next_rate,
321                                               &i_next_bit_rate,
322                                               &i_next_frame_length,
323                                               &i_next_max_frame_size,
324                                               &i_next_layer );
325
326                 /* Free bitrate only */
327                 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
328                 {
329                     if( (unsigned int)p_sys->i_frame_size >
330                         p_sys->i_max_frame_size )
331                     {
332                         msg_Dbg( p_dec, "frame too big %d > %d "
333                                  "(emulated startcode ?)", p_sys->i_frame_size,
334                                  p_sys->i_max_frame_size );
335                         block_SkipByte( &p_sys->bytestream );
336                         p_sys->i_state = STATE_NOSYNC;
337                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
338                         break;
339                     }
340
341                     p_sys->i_frame_size++;
342                     break;
343                 }
344
345                 if( i_next_frame_size == -1 )
346                 {
347                     msg_Dbg( p_dec, "emulated startcode on next frame" );
348                     block_SkipByte( &p_sys->bytestream );
349                     p_sys->i_state = STATE_NOSYNC;
350                     break;
351                 }
352
353                 /* Check info is in sync with previous one */
354                 if( i_next_channels_conf != p_sys->i_channels_conf ||
355                     i_next_rate != p_sys->i_rate ||
356                     i_next_layer != p_sys->i_layer ||
357                     i_next_frame_length != p_sys->i_frame_length )
358                 {
359                     /* Free bitrate only */
360                     if( p_sys->i_bit_rate == 0 )
361                     {
362                         p_sys->i_frame_size++;
363                         break;
364                     }
365
366                     msg_Dbg( p_dec, "parameters changed unexpectedly "
367                              "(emulated startcode ?)" );
368                     block_SkipByte( &p_sys->bytestream );
369                     p_sys->i_state = STATE_NOSYNC;
370                     break;
371                 }
372
373                 /* Free bitrate only */
374                 if( p_sys->i_bit_rate == 0 )
375                 {
376                     if( i_next_bit_rate != 0 )
377                     {
378                         p_sys->i_frame_size++;
379                         break;
380                     }
381                 }
382
383             }
384             else
385             {
386                 /* Free bitrate only */
387                 if( p_sys->i_bit_rate == 0 )
388                 {
389                     if( (unsigned int)p_sys->i_frame_size >
390                         p_sys->i_max_frame_size )
391                     {
392                         msg_Dbg( p_dec, "frame too big %d > %d "
393                                  "(emulated startcode ?)", p_sys->i_frame_size,
394                                  p_sys->i_max_frame_size );
395                         block_SkipByte( &p_sys->bytestream );
396                         p_sys->i_state = STATE_NOSYNC;
397                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
398                         break;
399                     }
400
401                     p_sys->i_frame_size++;
402                     break;
403                 }
404
405                 msg_Dbg( p_dec, "emulated startcode "
406                          "(no startcode on following frame)" );
407                 p_sys->i_state = STATE_NOSYNC;
408                 block_SkipByte( &p_sys->bytestream );
409                 break;
410             }
411
412             p_sys->i_state = STATE_SEND_DATA;
413             break;
414
415         case STATE_GET_DATA:
416             /* Make sure we have enough data.
417              * (Not useful if we went through NEXT_SYNC) */
418             if( block_WaitBytes( &p_sys->bytestream,
419                                  p_sys->i_frame_size ) != VLC_SUCCESS )
420             {
421                 /* Need more data */
422                 return NULL;
423             }
424             p_sys->i_state = STATE_SEND_DATA;
425
426         case STATE_SEND_DATA:
427             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
428             {
429                 //p_dec->b_error = VLC_TRUE;
430                 return NULL;
431             }
432
433             /* Free bitrate only */
434             if( p_sys->i_bit_rate == 0 )
435             {
436                 p_sys->i_free_frame_size = p_sys->i_frame_size;
437             }
438
439             /* Copy the whole frame into the buffer. When we reach this point
440              * we already know we have enough data available. */
441             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
442
443             /* Get beginning of next frame for libmad */
444             if( !p_sys->b_packetizer )
445             {
446                 memcpy( p_buf + p_sys->i_frame_size,
447                         p_header, MAD_BUFFER_GUARD );
448             }
449
450             p_sys->i_state = STATE_NOSYNC;
451
452             /* Make sure we don't reuse the same pts twice */
453             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
454                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
455
456             /* So p_block doesn't get re-added several times */
457             *pp_block = block_BytestreamPop( &p_sys->bytestream );
458
459             return p_out_buffer;
460         }
461     }
462
463     return NULL;
464 }
465
466 /*****************************************************************************
467  * GetOutBuffer:
468  *****************************************************************************/
469 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
470 {
471     decoder_sys_t *p_sys = p_dec->p_sys;
472     uint8_t *p_buf;
473
474     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
475     {
476         msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
477                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
478
479         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
480         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
481     }
482
483     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
484     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
485     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
486     p_dec->fmt_out.audio.i_bytes_per_frame =
487         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
488
489     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
490     p_dec->fmt_out.audio.i_physical_channels =
491         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
492
493     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
494
495     if( p_sys->b_packetizer )
496     {
497         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
498         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
499         *pp_out_buffer = p_sout_buffer;
500     }
501     else
502     {
503         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
504         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
505         *pp_out_buffer = p_aout_buffer;
506     }
507
508     return p_buf;
509 }
510
511 /*****************************************************************************
512  * GetAoutBuffer:
513  *****************************************************************************/
514 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
515 {
516     decoder_sys_t *p_sys = p_dec->p_sys;
517     aout_buffer_t *p_buf;
518
519     p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
520     if( p_buf == NULL ) return NULL;
521
522     p_buf->start_date = aout_DateGet( &p_sys->end_date );
523     p_buf->end_date =
524         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
525
526     /* Hack for libmad filter */
527     p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
528
529     return p_buf;
530 }
531
532 /*****************************************************************************
533  * GetSoutBuffer:
534  *****************************************************************************/
535 static block_t *GetSoutBuffer( decoder_t *p_dec )
536 {
537     decoder_sys_t *p_sys = p_dec->p_sys;
538     block_t *p_block;
539
540     p_block = block_New( p_dec, p_sys->i_frame_size );
541     if( p_block == NULL ) return NULL;
542
543     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
544
545     p_block->i_length =
546         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
547             p_block->i_pts;
548
549     return p_block;
550 }
551
552 /*****************************************************************************
553  * CloseDecoder: clean up the decoder
554  *****************************************************************************/
555 static void CloseDecoder( vlc_object_t *p_this )
556 {
557     decoder_t *p_dec = (decoder_t *)p_this;
558     decoder_sys_t *p_sys = p_dec->p_sys;
559
560     block_BytestreamRelease( &p_sys->bytestream );
561
562     free( p_sys );
563 }
564
565 /*****************************************************************************
566  * SyncInfo: parse MPEG audio sync info
567  *****************************************************************************/
568 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
569                      unsigned int * pi_channels_conf,
570                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
571                      unsigned int * pi_frame_length,
572                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
573 {
574     static const int ppi_bitrate[2][3][16] =
575     {
576         {
577             /* v1 l1 */
578             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
579               416, 448, 0},
580             /* v1 l2 */
581             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
582               320, 384, 0},
583             /* v1 l3 */
584             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
585               256, 320, 0}
586         },
587
588         {
589             /* v2 l1 */
590             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
591               224, 256, 0},
592             /* v2 l2 */
593             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
594               144, 160, 0},
595             /* v2 l3 */
596             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
597               144, 160, 0}
598         }
599     };
600
601     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
602     {
603         { 44100, 48000, 32000, 0 },
604         { 22050, 24000, 16000, 0 }
605     };
606
607     int i_version, i_mode, i_emphasis;
608     vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
609     int i_frame_size = 0;
610     int i_bitrate_index, i_samplerate_index;
611     int i_max_bit_rate;
612
613     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
614     i_version   = 1 - ((i_header & 0x80000) >> 19);
615     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
616     b_crc = !((i_header >> 16) & 0x01);
617     i_bitrate_index = (i_header & 0xf000) >> 12;
618     i_samplerate_index = (i_header & 0xc00) >> 10;
619     b_padding   = (i_header & 0x200) >> 9;
620     /* Extension */
621     i_mode      = (i_header & 0xc0) >> 6;
622     /* Modeext, copyright & original */
623     i_emphasis  = i_header & 0x3;
624
625     if( *pi_layer != 4 &&
626         i_bitrate_index < 0x0f &&
627         i_samplerate_index != 0x03 &&
628         i_emphasis != 0x02 )
629     {
630         switch ( i_mode )
631         {
632         case 0: /* stereo */
633         case 1: /* joint stereo */
634             *pi_channels = 2;
635             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
636             break;
637         case 2: /* dual-mono */
638             *pi_channels = 2;
639             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
640                                 | AOUT_CHAN_DUALMONO;
641             break;
642         case 3: /* mono */
643             *pi_channels = 1;
644             *pi_channels_conf = AOUT_CHAN_CENTER;
645             break;
646         }
647         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
648         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
649         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
650
651         if ( b_mpeg_2_5 )
652         {
653             *pi_sample_rate >>= 1;
654         }
655
656         switch( *pi_layer )
657         {
658         case 1:
659             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
660                            b_padding ) * 4;
661             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
662                                  *pi_sample_rate + 1 ) * 4;
663             *pi_frame_length = 384;
664             break;
665
666         case 2:
667             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
668             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
669             *pi_frame_length = 1152;
670             break;
671
672         case 3:
673             i_frame_size = ( i_version ? 72000 : 144000 ) *
674                            *pi_bit_rate / *pi_sample_rate + b_padding;
675             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
676                                  i_max_bit_rate / *pi_sample_rate + 1;
677             *pi_frame_length = i_version ? 576 : 1152;
678             break;
679
680         default:
681             break;
682         }
683     }
684     else
685     {
686         return -1;
687     }
688
689     return i_frame_size;
690 }