]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
fd3c8ed944860f4f9b7be255ded2373b21b4fab6
[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     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
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)->i_flags&BLOCK_FLAG_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 startcode" );
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 startcode 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_frame_length = p_sys->i_frame_length;
477     p_dec->fmt_out.audio.i_bytes_per_frame =
478         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
479
480     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
481     p_dec->fmt_out.audio.i_physical_channels =
482         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
483
484     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
485
486     if( p_sys->b_packetizer )
487     {
488         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
489         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
490         *pp_out_buffer = p_sout_buffer;
491     }
492     else
493     {
494         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
495         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
496         *pp_out_buffer = p_aout_buffer;
497     }
498
499     return p_buf;
500 }
501
502 /*****************************************************************************
503  * GetAoutBuffer:
504  *****************************************************************************/
505 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
506 {
507     decoder_sys_t *p_sys = p_dec->p_sys;
508     aout_buffer_t *p_buf;
509
510     p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
511     if( p_buf == NULL ) return NULL;
512
513     p_buf->start_date = aout_DateGet( &p_sys->end_date );
514     p_buf->end_date =
515         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
516
517     /* Hack for libmad filter */
518     p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
519
520     return p_buf;
521 }
522
523 /*****************************************************************************
524  * GetSoutBuffer:
525  *****************************************************************************/
526 static block_t *GetSoutBuffer( decoder_t *p_dec )
527 {
528     decoder_sys_t *p_sys = p_dec->p_sys;
529     block_t *p_block;
530
531     p_block = block_New( p_dec, p_sys->i_frame_size );
532     if( p_block == NULL ) return NULL;
533
534     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
535
536     p_block->i_length =
537         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
538             p_block->i_pts;
539
540     return p_block;
541 }
542
543 /*****************************************************************************
544  * CloseDecoder: clean up the decoder
545  *****************************************************************************/
546 static void CloseDecoder( vlc_object_t *p_this )
547 {
548     decoder_t *p_dec = (decoder_t *)p_this;
549     decoder_sys_t *p_sys = p_dec->p_sys;
550
551     block_BytestreamRelease( &p_sys->bytestream );
552
553     free( p_sys );
554 }
555
556 /*****************************************************************************
557  * SyncInfo: parse MPEG audio sync info
558  *****************************************************************************/
559 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
560                      unsigned int * pi_channels_conf,
561                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
562                      unsigned int * pi_frame_length,
563                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
564 {
565     static const int ppi_bitrate[2][3][16] =
566     {
567         {
568             /* v1 l1 */
569             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
570               416, 448, 0},
571             /* v1 l2 */
572             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
573               320, 384, 0},
574             /* v1 l3 */
575             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
576               256, 320, 0}
577         },
578
579         {
580             /* v2 l1 */
581             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
582               224, 256, 0},
583             /* v2 l2 */
584             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
585               144, 160, 0},
586             /* v2 l3 */
587             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
588               144, 160, 0}
589         }
590     };
591
592     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
593     {
594         { 44100, 48000, 32000, 0 },
595         { 22050, 24000, 16000, 0 }
596     };
597
598     int i_version, i_mode, i_emphasis;
599     vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
600     int i_frame_size = 0;
601     int i_bitrate_index, i_samplerate_index;
602     int i_max_bit_rate;
603
604     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
605     i_version   = 1 - ((i_header & 0x80000) >> 19);
606     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
607     b_crc = !((i_header >> 16) & 0x01);
608     i_bitrate_index = (i_header & 0xf000) >> 12;
609     i_samplerate_index = (i_header & 0xc00) >> 10;
610     b_padding   = (i_header & 0x200) >> 9;
611     /* Extension */
612     i_mode      = (i_header & 0xc0) >> 6;
613     /* Modeext, copyright & original */
614     i_emphasis  = i_header & 0x3;
615
616     if( *pi_layer != 4 &&
617         i_bitrate_index < 0x0f &&
618         i_samplerate_index != 0x03 &&
619         i_emphasis != 0x02 )
620     {
621         switch ( i_mode )
622         {
623         case 0: /* stereo */
624         case 1: /* joint stereo */
625             *pi_channels = 2;
626             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
627             break;
628         case 2: /* dual-mono */
629             *pi_channels = 2;
630             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
631                                 | AOUT_CHAN_DUALMONO;
632             break;
633         case 3: /* mono */
634             *pi_channels = 1;
635             *pi_channels_conf = AOUT_CHAN_CENTER;
636             break;
637         }
638         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
639         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
640         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
641
642         if ( b_mpeg_2_5 )
643         {
644             *pi_sample_rate >>= 1;
645         }
646
647         switch( *pi_layer )
648         {
649         case 1:
650             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
651                            b_padding ) * 4;
652             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
653                                  *pi_sample_rate + 1 ) * 4;
654             *pi_frame_length = 384;
655             break;
656
657         case 2:
658             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
659             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
660             *pi_frame_length = 1152;
661             break;
662
663         case 3:
664             i_frame_size = ( i_version ? 72000 : 144000 ) *
665                            *pi_bit_rate / *pi_sample_rate + b_padding;
666             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
667                                  i_max_bit_rate / *pi_sample_rate + 1;
668             *pi_frame_length = i_version ? 576 : 1152;
669             break;
670
671         default:
672             break;
673         }
674     }
675     else
676     {
677         return -1;
678     }
679
680     return i_frame_size;
681 }