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