]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
* ALL: final improvements to the decoders/packetizers api.
[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.22 2003/11/16 21:07:30 gbazin 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 <stdlib.h>                                      /* malloc(), free() */
31 #include <string.h>                                              /* strdup() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/decoder.h>
35 #include <vlc/input.h>
36 #include <vlc/aout.h>
37 #include <vlc/sout.h>
38
39 #include "vlc_block_helper.h"
40
41 /*****************************************************************************
42  * decoder_sys_t : decoder descriptor
43  *****************************************************************************/
44 struct decoder_sys_t
45 {
46     /* Module mode */
47     vlc_bool_t b_packetizer;
48
49     /*
50      * Input properties
51      */
52     int        i_state;
53
54     block_bytestream_t bytestream;
55
56     /*
57      * Common properties
58      */
59     audio_date_t          end_date;
60     unsigned int          i_current_layer;
61
62     mtime_t i_pts;
63
64     int i_frame_size, i_free_frame_size;
65     unsigned int i_channels_conf, i_channels;
66     unsigned int i_rate, i_max_frame_size, i_frame_length;
67     unsigned int i_layer, i_bit_rate;
68 };
69
70 enum {
71
72     STATE_NOSYNC,
73     STATE_SYNC,
74     STATE_HEADER,
75     STATE_NEXT_SYNC,
76     STATE_GET_DATA,
77     STATE_SEND_DATA
78 };
79
80 /* This isn't the place to put mad-specific stuff. However, it makes the
81  * mad plug-in's life much easier if we put 8 extra bytes at the end of the
82  * buffer, because that way it doesn't have to copy the aout_buffer_t to a
83  * bigger buffer. This has no implication on other plug-ins, and we only
84  * lose 8 bytes per frame. --Meuuh */
85 #define MAD_BUFFER_GUARD 8
86 #define MPGA_HEADER_SIZE 4
87
88 /****************************************************************************
89  * Local prototypes
90  ****************************************************************************/
91 static int  OpenDecoder   ( vlc_object_t * );
92 static int  OpenPacketizer( vlc_object_t * );
93 static void CloseDecoder  ( vlc_object_t * );
94 static void *DecodeBlock  ( decoder_t *, block_t ** );
95
96 static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
97 static aout_buffer_t *GetAoutBuffer( decoder_t * );
98 static block_t       *GetSoutBuffer( decoder_t * );
99
100 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
101                      unsigned int * pi_channels_conf,
102                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
103                      unsigned int * pi_frame_length,
104                      unsigned int * pi_max_frame_size,
105                      unsigned int * pi_layer );
106
107 /*****************************************************************************
108  * Module descriptor
109  *****************************************************************************/
110 vlc_module_begin();
111     set_description( _("MPEG audio layer I/II/III parser") );
112     set_capability( "decoder", 100 );
113     set_callbacks( OpenDecoder, CloseDecoder );
114
115     add_submodule();
116     set_description( _("MPEG audio layer I/II/III packetizer") );
117     set_capability( "packetizer", 10 );
118     set_callbacks( OpenPacketizer, CloseDecoder );
119 vlc_module_end();
120
121 /*****************************************************************************
122  * OpenDecoder: probe the decoder and return score
123  *****************************************************************************/
124 static int OpenDecoder( vlc_object_t *p_this )
125 {
126     decoder_t *p_dec = (decoder_t*)p_this;
127     decoder_sys_t *p_sys;
128
129     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
130     {
131         return VLC_EGENERIC;
132     }
133
134     /* Allocate the memory needed to store the decoder's structure */
135     if( ( p_dec->p_sys = p_sys =
136           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
137     {
138         msg_Err( p_dec, "out of memory" );
139         return VLC_EGENERIC;
140     }
141
142     /* Misc init */
143     p_sys->b_packetizer = VLC_FALSE;
144     p_sys->i_state = STATE_NOSYNC;
145     aout_DateSet( &p_sys->end_date, 0 );
146     p_sys->bytestream = block_BytestreamInit( p_dec );
147
148     /* Set output properties */
149     p_dec->fmt_out.i_cat = AUDIO_ES;
150     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
151
152     /* Set callback */
153     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
154         DecodeBlock;
155     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
156         DecodeBlock;
157
158     /* Start with the minimum size for a free bitrate frame */
159     p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
160
161     return VLC_SUCCESS;
162 }
163
164 static int OpenPacketizer( vlc_object_t *p_this )
165 {
166     decoder_t *p_dec = (decoder_t*)p_this;
167
168     int i_ret = OpenDecoder( p_this );
169
170     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
171
172     return i_ret;
173 }
174
175 /****************************************************************************
176  * DecodeBlock: the whole thing
177  ****************************************************************************
178  * This function is called just after the thread is launched.
179  ****************************************************************************/
180 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
181 {
182     decoder_sys_t *p_sys = p_dec->p_sys;
183     uint8_t p_header[MAD_BUFFER_GUARD];
184     uint32_t i_header;
185     uint8_t *p_buf;
186     void *p_out_buffer;
187
188     if( !pp_block || !*pp_block ) return NULL;
189
190     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
191     {
192         /* We've just started the stream, wait for the first PTS. */
193         block_Release( *pp_block );
194         return NULL;
195     }
196
197     if( (*pp_block)->b_discontinuity )
198     {
199         p_sys->i_state = STATE_NOSYNC;
200     }
201
202     block_BytestreamPush( &p_sys->bytestream, *pp_block );
203
204     while( 1 )
205     {
206         switch( p_sys->i_state )
207         {
208
209         case STATE_NOSYNC:
210             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
211                    == VLC_SUCCESS )
212             {
213                 /* Look for sync word - should be 0xffe */
214                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
215                 {
216                     p_sys->i_state = STATE_SYNC;
217                     break;
218                 }
219                 block_SkipByte( &p_sys->bytestream );
220             }
221             if( p_sys->i_state != STATE_SYNC )
222             {
223                 block_BytestreamFlush( &p_sys->bytestream );
224
225                 /* Need more data */
226                 return NULL;
227             }
228
229         case STATE_SYNC:
230             /* New frame, set the Presentation Time Stamp */
231             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
232             if( p_sys->i_pts != 0 &&
233                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
234             {
235                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
236             }
237             p_sys->i_state = STATE_HEADER;
238
239         case STATE_HEADER:
240             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
241             if( block_PeekBytes( &p_sys->bytestream, p_header,
242                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
243             {
244                 /* Need more data */
245                 return NULL;
246             }
247
248             /* Build frame header */
249             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
250                        |p_header[3];
251
252             /* Check if frame is valid and get frame info */
253             p_sys->i_frame_size = SyncInfo( i_header,
254                                             &p_sys->i_channels,
255                                             &p_sys->i_channels_conf,
256                                             &p_sys->i_rate,
257                                             &p_sys->i_bit_rate,
258                                             &p_sys->i_frame_length,
259                                             &p_sys->i_max_frame_size,
260                                             &p_sys->i_layer );
261
262             if( p_sys->i_frame_size == -1 )
263             {
264                 msg_Dbg( p_dec, "emulated start code" );
265                 block_SkipByte( &p_sys->bytestream );
266                 p_sys->i_state = STATE_NOSYNC;
267                 break;
268             }
269
270             if( p_sys->i_bit_rate == 0 )
271             {
272                 /* Free birate, but 99% emulated startcode :( */
273                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
274                 {
275                     msg_Dbg( p_dec, "free bitrate mode");
276                 }
277                 p_sys->i_frame_size = p_sys->i_free_frame_size;
278             }
279
280             p_sys->i_state = STATE_NEXT_SYNC;
281
282         case STATE_NEXT_SYNC:
283             /* TODO: If p_block == NULL, flush the buffer without checking the
284              * next sync word */
285
286             /* Check if next expected frame contains the sync word */
287             if( block_PeekOffsetBytes( &p_sys->bytestream,
288                                        p_sys->i_frame_size, p_header,
289                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
290             {
291                 /* Need more data */
292                 return NULL;
293             }
294
295             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
296             {
297                 /* Startcode is fine, let's try the header as an extra check */
298                 int i_next_frame_size;
299                 unsigned int i_next_channels, i_next_channels_conf;
300                 unsigned int i_next_rate, i_next_bit_rate;
301                 unsigned int i_next_frame_length, i_next_max_frame_size;
302                 unsigned int i_next_layer;
303
304                 /* Build frame header */
305                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
306                            |p_header[3];
307
308                 i_next_frame_size = SyncInfo( i_header,
309                                               &i_next_channels,
310                                               &i_next_channels_conf,
311                                               &i_next_rate,
312                                               &i_next_bit_rate,
313                                               &i_next_frame_length,
314                                               &i_next_max_frame_size,
315                                               &i_next_layer );
316
317                 /* Free bitrate only */
318                 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
319                 {
320                     if( (unsigned int)p_sys->i_frame_size >
321                         p_sys->i_max_frame_size )
322                     {
323                         msg_Dbg( p_dec, "frame too big %d > %d "
324                                  "(emulated startcode ?)", p_sys->i_frame_size,
325                                  p_sys->i_max_frame_size );
326                         block_SkipByte( &p_sys->bytestream );
327                         p_sys->i_state = STATE_NOSYNC;
328                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
329                         break;
330                     }
331
332                     p_sys->i_frame_size++;
333                     break;
334                 }
335
336                 if( i_next_frame_size == -1 )
337                 {
338                     msg_Dbg( p_dec, "emulated start code on next frame" );
339                     block_SkipByte( &p_sys->bytestream );
340                     p_sys->i_state = STATE_NOSYNC;
341                     break;
342                 }
343
344                 /* Check info is in sync with previous one */
345                 if( i_next_channels_conf != p_sys->i_channels_conf ||
346                     i_next_rate != p_sys->i_rate ||
347                     i_next_layer != p_sys->i_layer ||
348                     i_next_frame_length != p_sys->i_frame_length )
349                 {
350                     /* Free bitrate only */
351                     if( p_sys->i_bit_rate == 0 )
352                     {
353                         p_sys->i_frame_size++;
354                         break;
355                     }
356
357                     msg_Dbg( p_dec, "parameters changed unexpectedly "
358                              "(emulated startcode ?)" );
359                     block_SkipByte( &p_sys->bytestream );
360                     p_sys->i_state = STATE_NOSYNC;
361                     break;
362                 }
363
364                 /* Free bitrate only */
365                 if( p_sys->i_bit_rate == 0 )
366                 {
367                     if( i_next_bit_rate != 0 )
368                     {
369                         p_sys->i_frame_size++;
370                         break;
371                     }
372                 }
373
374             }
375             else
376             {
377                 /* Free bitrate only */
378                 if( p_sys->i_bit_rate == 0 )
379                 {
380                     if( (unsigned int)p_sys->i_frame_size >
381                         p_sys->i_max_frame_size )
382                     {
383                         msg_Dbg( p_dec, "frame too big %d > %d "
384                                  "(emulated startcode ?)", p_sys->i_frame_size,
385                                  p_sys->i_max_frame_size );
386                         block_SkipByte( &p_sys->bytestream );
387                         p_sys->i_state = STATE_NOSYNC;
388                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
389                         break;
390                     }
391
392                     p_sys->i_frame_size++;
393                     break;
394                 }
395
396                 msg_Dbg( p_dec, "emulated startcode "
397                          "(no startcode on following frame)" );
398                 p_sys->i_state = STATE_NOSYNC;
399                 block_SkipByte( &p_sys->bytestream );
400                 break;
401             }
402
403             p_sys->i_state = STATE_SEND_DATA;
404             break;
405
406         case STATE_GET_DATA:
407             /* Make sure we have enough data.
408              * (Not useful if we went through NEXT_SYNC) */
409             if( block_WaitBytes( &p_sys->bytestream,
410                                  p_sys->i_frame_size ) != VLC_SUCCESS )
411             {
412                 /* Need more data */
413                 return NULL;
414             }
415             p_sys->i_state = STATE_SEND_DATA;
416
417         case STATE_SEND_DATA:
418             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
419             {
420                 //p_dec->b_error = VLC_TRUE;
421                 return NULL;
422             }
423
424             /* Free bitrate only */
425             if( p_sys->i_bit_rate == 0 )
426             {
427                 p_sys->i_free_frame_size = p_sys->i_frame_size;
428             }
429
430             /* Copy the whole frame into the buffer. When we reach this point
431              * we already know we have enough data available. */
432             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
433
434             /* Get beginning of next frame for libmad */
435             if( !p_sys->b_packetizer )
436             {
437                 memcpy( p_buf + p_sys->i_frame_size,
438                         p_header, MAD_BUFFER_GUARD );
439             }
440
441             p_sys->i_state = STATE_NOSYNC;
442
443             /* Make sure we don't reuse the same pts twice */
444             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
445                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
446
447             /* So p_block doesn't get re-added several times */
448             *pp_block = block_BytestreamPop( &p_sys->bytestream );
449
450             return p_out_buffer;
451         }
452     }
453
454     return NULL;
455 }
456
457 /*****************************************************************************
458  * GetOutBuffer:
459  *****************************************************************************/
460 static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
461 {
462     decoder_sys_t *p_sys = p_dec->p_sys;
463     uint8_t *p_buf;
464
465     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
466     {
467         msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
468                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
469
470         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
471         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
472     }
473
474     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
475     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
476     p_dec->fmt_out.audio.i_bitrate  = p_sys->i_bit_rate;
477     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
478     p_dec->fmt_out.audio.i_bytes_per_frame =
479         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
480
481     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
482     p_dec->fmt_out.audio.i_physical_channels =
483         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
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 }