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