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