]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_audio.c
Use var_InheritString for --decklink-video-connection.
[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 the VideoLAN team
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@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_codec.h>
37 #include <vlc_aout.h>
38 #include <vlc_modules.h>
39 #include <assert.h>
40
41 #include <vlc_block_helper.h>
42
43 /*****************************************************************************
44  * decoder_sys_t : decoder descriptor
45  *****************************************************************************/
46 struct decoder_sys_t
47 {
48     /* Module mode */
49     bool b_packetizer;
50
51     /*
52      * Input properties
53      */
54     int        i_state;
55
56     block_bytestream_t bytestream;
57
58     /*
59      * Common properties
60      */
61     date_t          end_date;
62     unsigned int    i_current_layer;
63
64     mtime_t i_pts;
65
66     int i_frame_size, i_free_frame_size;
67     unsigned int i_channels_conf, i_channels;
68     unsigned int i_rate, i_max_frame_size, i_frame_length;
69     unsigned int i_layer, i_bit_rate;
70
71     bool   b_discontinuity;
72 };
73
74 enum {
75
76     STATE_NOSYNC,
77     STATE_SYNC,
78     STATE_HEADER,
79     STATE_NEXT_SYNC,
80     STATE_GET_DATA,
81     STATE_SEND_DATA
82 };
83
84 /* This isn't the place to put mad-specific stuff. However, it makes the
85  * mad plug-in's life much easier if we put 8 extra bytes at the end of the
86  * buffer, because that way it doesn't have to copy the aout_buffer_t to a
87  * bigger buffer. This has no implication on other plug-ins, and we only
88  * lose 8 bytes per frame. --Meuuh */
89 #define MAD_BUFFER_GUARD 8
90 #define MPGA_HEADER_SIZE 4
91
92 /****************************************************************************
93  * Local prototypes
94  ****************************************************************************/
95 static int  OpenDecoder   ( vlc_object_t * );
96 static int  OpenPacketizer( vlc_object_t * );
97 static void CloseDecoder  ( vlc_object_t * );
98 static void *DecodeBlock  ( decoder_t *, block_t ** );
99
100 static uint8_t       *GetOutBuffer ( decoder_t *, block_t ** );
101 static aout_buffer_t *GetAoutBuffer( decoder_t * );
102 static block_t       *GetSoutBuffer( decoder_t * );
103
104 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
105                      unsigned int * pi_channels_conf,
106                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
107                      unsigned int * pi_frame_length,
108                      unsigned int * pi_max_frame_size,
109                      unsigned int * pi_layer );
110
111 /*****************************************************************************
112  * Module descriptor
113  *****************************************************************************/
114 vlc_module_begin ()
115     set_description( N_("MPEG audio layer I/II/III decoder") )
116     set_category( CAT_INPUT )
117     set_subcategory( SUBCAT_INPUT_ACODEC )
118 #if defined(UNDER_CE)
119    set_capability( "decoder", 5 )
120 #else
121     set_capability( "decoder", 100 )
122 #endif
123     set_callbacks( OpenDecoder, CloseDecoder )
124
125     add_submodule ()
126     set_description( N_("MPEG audio layer I/II/III packetizer") )
127     set_capability( "packetizer", 10 )
128     set_callbacks( OpenPacketizer, CloseDecoder )
129 vlc_module_end ()
130
131 /*****************************************************************************
132  * Open: probe the decoder and return score
133  *****************************************************************************/
134 static int Open( vlc_object_t *p_this )
135 {
136     decoder_t *p_dec = (decoder_t*)p_this;
137     decoder_sys_t *p_sys;
138
139     if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA )
140     {
141         return VLC_EGENERIC;
142     }
143
144     /* Allocate the memory needed to store the decoder's structure */
145     if( ( p_dec->p_sys = p_sys =
146           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
147         return VLC_ENOMEM;
148
149     /* Misc init */
150     p_sys->b_packetizer = false;
151     p_sys->i_state = STATE_NOSYNC;
152     date_Set( &p_sys->end_date, 0 );
153     p_sys->bytestream = block_BytestreamInit();
154     p_sys->i_pts = VLC_TS_INVALID;
155     p_sys->b_discontinuity = false;
156
157     /* Set output properties */
158     p_dec->fmt_out.i_cat = AUDIO_ES;
159     p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
160     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
161
162     /* Set callback */
163     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
164         DecodeBlock;
165     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
166         DecodeBlock;
167
168     /* Start with the minimum size for a free bitrate frame */
169     p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
170
171     return VLC_SUCCESS;
172 }
173
174 static int OpenDecoder( vlc_object_t *p_this )
175 {
176     /* HACK: Don't use this codec if we don't have an mpga audio filter */
177     if( !module_exists( "mpgatofixed32" ) )
178         return VLC_EGENERIC;
179
180     return Open( p_this );
181 }
182
183 static int OpenPacketizer( vlc_object_t *p_this )
184 {
185     decoder_t *p_dec = (decoder_t*)p_this;
186
187     int i_ret = Open( p_this );
188
189     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
190
191     return i_ret;
192 }
193
194 /****************************************************************************
195  * DecodeBlock: the whole thing
196  ****************************************************************************
197  * This function is called just after the thread is launched.
198  ****************************************************************************/
199 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
200 {
201     decoder_sys_t *p_sys = p_dec->p_sys;
202     uint8_t p_header[MAD_BUFFER_GUARD];
203     uint32_t i_header;
204     uint8_t *p_buf;
205     block_t *p_out_buffer;
206
207     if( !pp_block || !*pp_block ) return NULL;
208
209     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
210     {
211         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
212         {
213             p_sys->i_state = STATE_NOSYNC;
214             block_BytestreamEmpty( &p_sys->bytestream );
215         }
216         date_Set( &p_sys->end_date, 0 );
217         block_Release( *pp_block );
218         p_sys->b_discontinuity = true;
219         return NULL;
220     }
221
222     if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
223     {
224         /* We've just started the stream, wait for the first PTS. */
225         msg_Dbg( p_dec, "waiting for PTS" );
226         block_Release( *pp_block );
227         return NULL;
228     }
229
230     block_BytestreamPush( &p_sys->bytestream, *pp_block );
231
232     while( 1 )
233     {
234         switch( p_sys->i_state )
235         {
236
237         case STATE_NOSYNC:
238             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
239                    == VLC_SUCCESS )
240             {
241                 /* Look for sync word - should be 0xffe */
242                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
243                 {
244                     p_sys->i_state = STATE_SYNC;
245                     break;
246                 }
247                 block_SkipByte( &p_sys->bytestream );
248             }
249             if( p_sys->i_state != STATE_SYNC )
250             {
251                 block_BytestreamFlush( &p_sys->bytestream );
252
253                 /* Need more data */
254                 return NULL;
255             }
256
257         case STATE_SYNC:
258             /* New frame, set the Presentation Time Stamp */
259             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
260             if( p_sys->i_pts > VLC_TS_INVALID &&
261                 p_sys->i_pts != date_Get( &p_sys->end_date ) )
262             {
263                 date_Set( &p_sys->end_date, p_sys->i_pts );
264             }
265             p_sys->i_state = STATE_HEADER;
266
267         case STATE_HEADER:
268             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
269             if( block_PeekBytes( &p_sys->bytestream, p_header,
270                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
271             {
272                 /* Need more data */
273                 return NULL;
274             }
275
276             /* Build frame header */
277             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
278                        |p_header[3];
279
280             /* Check if frame is valid and get frame info */
281             p_sys->i_frame_size = SyncInfo( i_header,
282                                             &p_sys->i_channels,
283                                             &p_sys->i_channels_conf,
284                                             &p_sys->i_rate,
285                                             &p_sys->i_bit_rate,
286                                             &p_sys->i_frame_length,
287                                             &p_sys->i_max_frame_size,
288                                             &p_sys->i_layer );
289
290             p_dec->fmt_in.i_profile = p_sys->i_layer;
291
292             if( p_sys->i_frame_size == -1 )
293             {
294                 msg_Dbg( p_dec, "emulated startcode" );
295                 block_SkipByte( &p_sys->bytestream );
296                 p_sys->i_state = STATE_NOSYNC;
297                 p_sys->b_discontinuity = true;
298                 break;
299             }
300
301             if( p_sys->i_bit_rate == 0 )
302             {
303                 /* Free bitrate, but 99% emulated startcode :( */
304                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
305                 {
306                     msg_Dbg( p_dec, "free bitrate mode");
307                 }
308                 /* The -1 below is to account for the frame padding */
309                 p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
310             }
311
312             p_sys->i_state = STATE_NEXT_SYNC;
313
314         case STATE_NEXT_SYNC:
315             /* TODO: If p_block == NULL, flush the buffer without checking the
316              * next sync word */
317
318             /* Check if next expected frame contains the sync word */
319             if( block_PeekOffsetBytes( &p_sys->bytestream,
320                                        p_sys->i_frame_size, p_header,
321                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
322             {
323                 /* Need more data */
324                 return NULL;
325             }
326
327             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
328             {
329                 /* Startcode is fine, let's try the header as an extra check */
330                 int i_next_frame_size;
331                 unsigned int i_next_channels, i_next_channels_conf;
332                 unsigned int i_next_rate, i_next_bit_rate;
333                 unsigned int i_next_frame_length, i_next_max_frame_size;
334                 unsigned int i_next_layer;
335
336                 /* Build frame header */
337                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
338                            |p_header[3];
339
340                 i_next_frame_size = SyncInfo( i_header,
341                                               &i_next_channels,
342                                               &i_next_channels_conf,
343                                               &i_next_rate,
344                                               &i_next_bit_rate,
345                                               &i_next_frame_length,
346                                               &i_next_max_frame_size,
347                                               &i_next_layer );
348
349                 /* Free bitrate only */
350                 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
351                 {
352                     if( (unsigned int)p_sys->i_frame_size >
353                         p_sys->i_max_frame_size )
354                     {
355                         msg_Dbg( p_dec, "frame too big %d > %d "
356                                  "(emulated startcode ?)", p_sys->i_frame_size,
357                                  p_sys->i_max_frame_size );
358                         block_SkipByte( &p_sys->bytestream );
359                         p_sys->i_state = STATE_NOSYNC;
360                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
361                         break;
362                     }
363
364                     p_sys->i_frame_size++;
365                     break;
366                 }
367
368                 if( i_next_frame_size == -1 )
369                 {
370                     msg_Dbg( p_dec, "emulated startcode on next frame" );
371                     block_SkipByte( &p_sys->bytestream );
372                     p_sys->i_state = STATE_NOSYNC;
373                     p_sys->b_discontinuity = true;
374                     break;
375                 }
376
377                 /* Check info is in sync with previous one */
378                 if( i_next_channels_conf != p_sys->i_channels_conf ||
379                     i_next_rate != p_sys->i_rate ||
380                     i_next_layer != p_sys->i_layer ||
381                     i_next_frame_length != p_sys->i_frame_length )
382                 {
383                     /* Free bitrate only */
384                     if( p_sys->i_bit_rate == 0 )
385                     {
386                         p_sys->i_frame_size++;
387                         break;
388                     }
389
390                     msg_Dbg( p_dec, "parameters changed unexpectedly "
391                              "(emulated startcode ?)" );
392                     block_SkipByte( &p_sys->bytestream );
393                     p_sys->i_state = STATE_NOSYNC;
394                     break;
395                 }
396
397                 /* Free bitrate only */
398                 if( p_sys->i_bit_rate == 0 )
399                 {
400                     if( i_next_bit_rate != 0 )
401                     {
402                         p_sys->i_frame_size++;
403                         break;
404                     }
405                 }
406
407             }
408             else
409             {
410                 /* Free bitrate only */
411                 if( p_sys->i_bit_rate == 0 )
412                 {
413                     if( (unsigned int)p_sys->i_frame_size >
414                         p_sys->i_max_frame_size )
415                     {
416                         msg_Dbg( p_dec, "frame too big %d > %d "
417                                  "(emulated startcode ?)", p_sys->i_frame_size,
418                                  p_sys->i_max_frame_size );
419                         block_SkipByte( &p_sys->bytestream );
420                         p_sys->i_state = STATE_NOSYNC;
421                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
422                         break;
423                     }
424
425                     p_sys->i_frame_size++;
426                     break;
427                 }
428
429                 msg_Dbg( p_dec, "emulated startcode "
430                          "(no startcode on following frame)" );
431                 p_sys->i_state = STATE_NOSYNC;
432                 block_SkipByte( &p_sys->bytestream );
433                 break;
434             }
435
436             p_sys->i_state = STATE_SEND_DATA;
437             break;
438
439         case STATE_GET_DATA:
440             /* Make sure we have enough data.
441              * (Not useful if we went through NEXT_SYNC) */
442             if( block_WaitBytes( &p_sys->bytestream,
443                                  p_sys->i_frame_size ) != VLC_SUCCESS )
444             {
445                 /* Need more data */
446                 return NULL;
447             }
448             p_sys->i_state = STATE_SEND_DATA;
449
450         case STATE_SEND_DATA:
451             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
452             {
453                 //p_dec->b_error = true;
454                 return NULL;
455             }
456
457             /* Free bitrate only */
458             if( p_sys->i_bit_rate == 0 )
459             {
460                 p_sys->i_free_frame_size = p_sys->i_frame_size;
461             }
462
463             /* Copy the whole frame into the buffer. When we reach this point
464              * we already know we have enough data available. */
465             block_GetBytes( &p_sys->bytestream,
466                             p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) );
467
468             /* Get beginning of next frame for libmad */
469             if( !p_sys->b_packetizer )
470             {
471                 assert( p_out_buffer->i_buffer >= (unsigned)p_sys->i_frame_size + MAD_BUFFER_GUARD );
472                 memcpy( p_buf + p_sys->i_frame_size,
473                         p_header, MAD_BUFFER_GUARD );
474             }
475
476             p_sys->i_state = STATE_NOSYNC;
477
478             /* Make sure we don't reuse the same pts twice */
479             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
480                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
481
482             /* So p_block doesn't get re-added several times */
483             *pp_block = block_BytestreamPop( &p_sys->bytestream );
484
485             return p_out_buffer;
486         }
487     }
488
489     return NULL;
490 }
491
492 /*****************************************************************************
493  * GetOutBuffer:
494  *****************************************************************************/
495 static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
496 {
497     decoder_sys_t *p_sys = p_dec->p_sys;
498     uint8_t *p_buf;
499
500     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
501     {
502         msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
503                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
504
505         date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
506         date_Set( &p_sys->end_date, p_sys->i_pts );
507     }
508
509     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
510     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
511     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
512     p_dec->fmt_out.audio.i_bytes_per_frame =
513         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
514
515     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
516     p_dec->fmt_out.audio.i_physical_channels =
517         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
518
519     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
520
521     if( p_sys->b_packetizer )
522     {
523         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
524         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
525         *pp_out_buffer = p_sout_buffer;
526     }
527     else
528     {
529         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
530         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
531         *pp_out_buffer = p_aout_buffer;
532     }
533
534     return p_buf;
535 }
536
537 /*****************************************************************************
538  * GetAoutBuffer:
539  *****************************************************************************/
540 static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
541 {
542     decoder_sys_t *p_sys = p_dec->p_sys;
543     aout_buffer_t *p_buf;
544
545     p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
546     if( p_buf == NULL ) return NULL;
547
548     p_buf->i_pts = date_Get( &p_sys->end_date );
549     p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
550                       - p_buf->i_pts;
551     if( p_sys->b_discontinuity )
552         p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
553     p_sys->b_discontinuity = false;
554
555     /* Hack for libmad filter */
556     p_buf = block_Realloc( p_buf, 0, p_sys->i_frame_size + MAD_BUFFER_GUARD );
557
558     return p_buf;
559 }
560
561 /*****************************************************************************
562  * GetSoutBuffer:
563  *****************************************************************************/
564 static block_t *GetSoutBuffer( decoder_t *p_dec )
565 {
566     decoder_sys_t *p_sys = p_dec->p_sys;
567     block_t *p_block;
568
569     p_block = block_New( p_dec, p_sys->i_frame_size );
570     if( p_block == NULL ) return NULL;
571
572     p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
573
574     p_block->i_length =
575         date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
576
577     return p_block;
578 }
579
580 /*****************************************************************************
581  * CloseDecoder: clean up the decoder
582  *****************************************************************************/
583 static void CloseDecoder( vlc_object_t *p_this )
584 {
585     decoder_t *p_dec = (decoder_t *)p_this;
586     decoder_sys_t *p_sys = p_dec->p_sys;
587
588     block_BytestreamRelease( &p_sys->bytestream );
589
590     free( p_sys );
591 }
592
593 /*****************************************************************************
594  * SyncInfo: parse MPEG audio sync info
595  *****************************************************************************/
596 static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
597                      unsigned int * pi_channels_conf,
598                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
599                      unsigned int * pi_frame_length,
600                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
601 {
602     static const int ppi_bitrate[2][3][16] =
603     {
604         {
605             /* v1 l1 */
606             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
607               416, 448, 0},
608             /* v1 l2 */
609             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
610               320, 384, 0},
611             /* v1 l3 */
612             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
613               256, 320, 0}
614         },
615
616         {
617             /* v2 l1 */
618             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
619               224, 256, 0},
620             /* v2 l2 */
621             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
622               144, 160, 0},
623             /* v2 l3 */
624             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
625               144, 160, 0}
626         }
627     };
628
629     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
630     {
631         { 44100, 48000, 32000, 0 },
632         { 22050, 24000, 16000, 0 }
633     };
634
635     int i_version, i_mode, i_emphasis;
636     bool b_padding, b_mpeg_2_5, b_crc;
637     int i_frame_size = 0;
638     int i_bitrate_index, i_samplerate_index;
639     int i_max_bit_rate;
640
641     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
642     i_version   = 1 - ((i_header & 0x80000) >> 19);
643     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
644     b_crc = !((i_header >> 16) & 0x01);
645     i_bitrate_index = (i_header & 0xf000) >> 12;
646     i_samplerate_index = (i_header & 0xc00) >> 10;
647     b_padding   = (i_header & 0x200) >> 9;
648     /* Extension */
649     i_mode      = (i_header & 0xc0) >> 6;
650     /* Modeext, copyright & original */
651     i_emphasis  = i_header & 0x3;
652
653     if( *pi_layer != 4 &&
654         i_bitrate_index < 0x0f &&
655         i_samplerate_index != 0x03 &&
656         i_emphasis != 0x02 )
657     {
658         switch ( i_mode )
659         {
660         case 0: /* stereo */
661         case 1: /* joint stereo */
662             *pi_channels = 2;
663             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
664             break;
665         case 2: /* dual-mono */
666             *pi_channels = 2;
667             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
668                                 | AOUT_CHAN_DUALMONO;
669             break;
670         case 3: /* mono */
671             *pi_channels = 1;
672             *pi_channels_conf = AOUT_CHAN_CENTER;
673             break;
674         }
675         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
676         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
677         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
678
679         if ( b_mpeg_2_5 )
680         {
681             *pi_sample_rate >>= 1;
682         }
683
684         switch( *pi_layer )
685         {
686         case 1:
687             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
688                            b_padding ) * 4;
689             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
690                                  *pi_sample_rate + 1 ) * 4;
691             *pi_frame_length = 384;
692             break;
693
694         case 2:
695             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
696             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
697             *pi_frame_length = 1152;
698             break;
699
700         case 3:
701             i_frame_size = ( i_version ? 72000 : 144000 ) *
702                            *pi_bit_rate / *pi_sample_rate + b_padding;
703             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
704                                  i_max_bit_rate / *pi_sample_rate + 1;
705             *pi_frame_length = i_version ? 576 : 1152;
706             break;
707
708         default:
709             break;
710         }
711
712         /* Free bitrate mode can support higher bitrates */
713         if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
714     }
715     else
716     {
717         return -1;
718     }
719
720     return i_frame_size;
721 }