mpeg_audio.c 24.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2003 VLC authors and VideoLAN
5
 * $Id$
6 7 8 9
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *          Christophe Massiot <massiot@via.ecp.fr>
10
 *          Gildas Bazin <gbazin@videolan.org>
11
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
36 37
#include <vlc_codec.h>
#include <vlc_aout.h>
38
#include <vlc_modules.h>
39
#include <assert.h>
40

41
#include <vlc_block_helper.h>
42

43 44
#include "../packetizer/packetizer_helper.h"

45
/*****************************************************************************
46
 * decoder_sys_t : decoder descriptor
47
 *****************************************************************************/
48
struct decoder_sys_t
49
{
50
    /* Module mode */
51
    bool b_packetizer;
52

53
    /*
54
     * Input properties
55
     */
56 57 58
    int        i_state;

    block_bytestream_t bytestream;
59

60 61 62
    /*
     * Common properties
     */
63 64
    date_t          end_date;
    unsigned int    i_current_layer;
65

66
    mtime_t i_pts;
67 68 69 70 71

    int i_frame_size, i_free_frame_size;
    unsigned int i_channels_conf, i_channels;
    unsigned int i_rate, i_max_frame_size, i_frame_length;
    unsigned int i_layer, i_bit_rate;
72

73
    bool   b_discontinuity;
74 75
};

76 77
/* This isn't the place to put mad-specific stuff. However, it makes the
 * mad plug-in's life much easier if we put 8 extra bytes at the end of the
78 79 80
 * buffer, because that way it doesn't have to copy the block_t to a bigger
 * buffer. This has no implication on other plug-ins, and we only lose 8 bytes
 * per frame. --Meuuh */
81
#define MAD_BUFFER_GUARD 8
82
#define MPGA_HEADER_SIZE 4
83 84 85 86

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
87 88 89
static int  OpenDecoder   ( vlc_object_t * );
static int  OpenPacketizer( vlc_object_t * );
static void CloseDecoder  ( vlc_object_t * );
90
static block_t *DecodeBlock  ( decoder_t *, block_t ** );
91

92 93 94
static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
static block_t *GetAoutBuffer( decoder_t * );
static block_t *GetSoutBuffer( decoder_t * );
95 96

static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
97
                     unsigned int * pi_channels_conf,
98 99
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
100
                     unsigned int * pi_max_frame_size,
101 102 103 104 105
                     unsigned int * pi_layer );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
106 107 108 109 110 111
vlc_module_begin ()
    set_description( N_("MPEG audio layer I/II/III decoder") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
    set_capability( "decoder", 100 )
    set_callbacks( OpenDecoder, CloseDecoder )
112

113 114 115 116 117
    add_submodule ()
    set_description( N_("MPEG audio layer I/II/III packetizer") )
    set_capability( "packetizer", 10 )
    set_callbacks( OpenPacketizer, CloseDecoder )
vlc_module_end ()
118 119

/*****************************************************************************
120
 * Open: probe the decoder and return score
121
 *****************************************************************************/
122
static int Open( vlc_object_t *p_this )
123
{
124
    decoder_t *p_dec = (decoder_t*)p_this;
125
    decoder_sys_t *p_sys;
126

127
    if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA )
128 129 130 131
    {
        return VLC_EGENERIC;
    }

132
    /* Allocate the memory needed to store the decoder's structure */
133
    if( ( p_dec->p_sys = p_sys =
134
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
135
        return VLC_ENOMEM;
136 137

    /* Misc init */
138
    p_sys->b_packetizer = false;
139
    p_sys->i_state = STATE_NOSYNC;
140
    date_Set( &p_sys->end_date, 0 );
141
    block_BytestreamInit( &p_sys->bytestream );
142
    p_sys->i_pts = VLC_TS_INVALID;
143
    p_sys->b_discontinuity = false;
144 145 146

    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
147
    p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
148
    p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
149 150

    /* Set callback */
151 152
    p_dec->pf_decode_audio = DecodeBlock;
    p_dec->pf_packetize    = DecodeBlock;
153 154 155

    /* Start with the minimum size for a free bitrate frame */
    p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
156

157 158 159
    return VLC_SUCCESS;
}

160 161 162 163 164 165 166 167 168
static int OpenDecoder( vlc_object_t *p_this )
{
    /* HACK: Don't use this codec if we don't have an mpga audio filter */
    if( !module_exists( "mpgatofixed32" ) )
        return VLC_EGENERIC;

    return Open( p_this );
}

169 170 171 172
static int OpenPacketizer( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;

173
    int i_ret = Open( p_this );
174

175
    if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
176 177 178 179 180

    return i_ret;
}

/****************************************************************************
181
 * DecodeBlock: the whole thing
182 183 184
 ****************************************************************************
 * This function is called just after the thread is launched.
 ****************************************************************************/
185
static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
186 187 188 189
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t p_header[MAD_BUFFER_GUARD];
    uint32_t i_header;
190
    uint8_t *p_buf;
191
    block_t *p_out_buffer;
192 193

    if( !pp_block || !*pp_block ) return NULL;
194

195
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
196
    {
197 198 199
        if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
        {
            p_sys->i_state = STATE_NOSYNC;
200
            block_BytestreamEmpty( &p_sys->bytestream );
201
        }
202
        date_Set( &p_sys->end_date, 0 );
203
        block_Release( *pp_block );
204
        p_sys->b_discontinuity = true;
205
        return NULL;
206 207
    }

208
    if( !date_Get( &p_sys->end_date ) && (*pp_block)->i_pts <= VLC_TS_INVALID )
209
    {
210 211 212 213
        /* We've just started the stream, wait for the first PTS. */
        msg_Dbg( p_dec, "waiting for PTS" );
        block_Release( *pp_block );
        return NULL;
214 215
    }

216
    block_BytestreamPush( &p_sys->bytestream, *pp_block );
217

218
    while( 1 )
219
    {
220
        switch( p_sys->i_state )
221
        {
222 223 224 225

        case STATE_NOSYNC:
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
                   == VLC_SUCCESS )
226
            {
227 228 229 230
                /* Look for sync word - should be 0xffe */
                if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
                {
                    p_sys->i_state = STATE_SYNC;
231
                    break;
232 233 234 235 236
                }
                block_SkipByte( &p_sys->bytestream );
            }
            if( p_sys->i_state != STATE_SYNC )
            {
237
                block_BytestreamFlush( &p_sys->bytestream );
238 239

                /* Need more data */
240
                return NULL;
241
            }
242 243 244

        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
245
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
246
            if( p_sys->i_pts > VLC_TS_INVALID &&
247
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
248
            {
249
                date_Set( &p_sys->end_date, p_sys->i_pts );
250
            }
251 252 253 254 255 256
            p_sys->i_state = STATE_HEADER;

        case STATE_HEADER:
            /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
            if( block_PeekBytes( &p_sys->bytestream, p_header,
                                 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
257
            {
258
                /* Need more data */
259
                return NULL;
260 261 262 263 264 265 266 267
            }

            /* Build frame header */
            i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
                       |p_header[3];

            /* Check if frame is valid and get frame info */
            p_sys->i_frame_size = SyncInfo( i_header,
268
                                            &p_sys->i_channels,
269 270 271 272
                                            &p_sys->i_channels_conf,
                                            &p_sys->i_rate,
                                            &p_sys->i_bit_rate,
                                            &p_sys->i_frame_length,
273
                                            &p_sys->i_max_frame_size,
274 275
                                            &p_sys->i_layer );

276 277
            p_dec->fmt_in.i_profile = p_sys->i_layer;

278 279
            if( p_sys->i_frame_size == -1 )
            {
Benjamin Pracht's avatar
Benjamin Pracht committed
280
                msg_Dbg( p_dec, "emulated startcode" );
281 282
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
283
                p_sys->b_discontinuity = true;
284 285 286 287 288
                break;
            }

            if( p_sys->i_bit_rate == 0 )
            {
289
                /* Free bitrate, but 99% emulated startcode :( */
290
                if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
291
                {
292
                    msg_Dbg( p_dec, "free bitrate mode");
293
                }
294 295
                /* The -1 below is to account for the frame padding */
                p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
296
            }
297

298
            p_sys->i_state = STATE_NEXT_SYNC;
299

300 301 302
        case STATE_NEXT_SYNC:
            /* TODO: If p_block == NULL, flush the buffer without checking the
             * next sync word */
303

304 305 306 307 308 309
            /* Check if next expected frame contains the sync word */
            if( block_PeekOffsetBytes( &p_sys->bytestream,
                                       p_sys->i_frame_size, p_header,
                                       MAD_BUFFER_GUARD ) != VLC_SUCCESS )
            {
                /* Need more data */
310
                return NULL;
311
            }
312

313 314 315 316
            if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
            {
                /* Startcode is fine, let's try the header as an extra check */
                int i_next_frame_size;
317 318
                unsigned int i_next_channels, i_next_channels_conf;
                unsigned int i_next_rate, i_next_bit_rate;
319 320
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
321

322 323 324
                /* Build frame header */
                i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
                           |p_header[3];
325

326 327
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
328
                                              &i_next_channels_conf,
329 330 331 332 333
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
334

335
                /* Free bitrate only */
336
                if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
337 338 339 340 341 342 343 344 345 346 347 348
                {
                    if( (unsigned int)p_sys->i_frame_size >
                        p_sys->i_max_frame_size )
                    {
                        msg_Dbg( p_dec, "frame too big %d > %d "
                                 "(emulated startcode ?)", p_sys->i_frame_size,
                                 p_sys->i_max_frame_size );
                        block_SkipByte( &p_sys->bytestream );
                        p_sys->i_state = STATE_NOSYNC;
                        p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
                        break;
                    }
349 350 351 352 353 354 355

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
Benjamin Pracht's avatar
Benjamin Pracht committed
356
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
357 358
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
359
                    p_sys->b_discontinuity = true;
360
                    break;
361 362 363
                }

                /* Check info is in sync with previous one */
364
                if( i_next_channels_conf != p_sys->i_channels_conf ||
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
                    i_next_rate != p_sys->i_rate ||
                    i_next_layer != p_sys->i_layer ||
                    i_next_frame_length != p_sys->i_frame_length )
                {
                    /* Free bitrate only */
                    if( p_sys->i_bit_rate == 0 )
                    {
                        p_sys->i_frame_size++;
                        break;
                    }

                    msg_Dbg( p_dec, "parameters changed unexpectedly "
                             "(emulated startcode ?)" );
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
                    break;
                }
382 383 384 385 386 387 388 389 390 391 392

                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
                    if( i_next_bit_rate != 0 )
                    {
                        p_sys->i_frame_size++;
                        break;
                    }
                }

393 394 395
            }
            else
            {
396 397 398
                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
399 400 401 402 403 404 405 406 407 408 409 410
                    if( (unsigned int)p_sys->i_frame_size >
                        p_sys->i_max_frame_size )
                    {
                        msg_Dbg( p_dec, "frame too big %d > %d "
                                 "(emulated startcode ?)", p_sys->i_frame_size,
                                 p_sys->i_max_frame_size );
                        block_SkipByte( &p_sys->bytestream );
                        p_sys->i_state = STATE_NOSYNC;
                        p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
                        break;
                    }

411 412 413 414
                    p_sys->i_frame_size++;
                    break;
                }

415 416 417 418 419
                msg_Dbg( p_dec, "emulated startcode "
                         "(no startcode on following frame)" );
                p_sys->i_state = STATE_NOSYNC;
                block_SkipByte( &p_sys->bytestream );
                break;
420 421
            }

422 423 424 425 426 427 428 429
            p_sys->i_state = STATE_SEND_DATA;
            break;

        case STATE_GET_DATA:
            /* Make sure we have enough data.
             * (Not useful if we went through NEXT_SYNC) */
            if( block_WaitBytes( &p_sys->bytestream,
                                 p_sys->i_frame_size ) != VLC_SUCCESS )
430
            {
431 432
                /* Need more data */
                return NULL;
433
            }
434
            p_sys->i_state = STATE_SEND_DATA;
435

436 437
        case STATE_SEND_DATA:
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
438
            {
439
                //p_dec->b_error = true;
440
                return NULL;
441 442
            }

443 444
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
445
            {
446
                p_sys->i_free_frame_size = p_sys->i_frame_size;
447
            }
448

449 450
            /* Copy the whole frame into the buffer. When we reach this point
             * we already know we have enough data available. */
451 452
            block_GetBytes( &p_sys->bytestream,
                            p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) );
453

454 455 456
            /* Get beginning of next frame for libmad */
            if( !p_sys->b_packetizer )
            {
457
                assert( p_out_buffer->i_buffer >= (unsigned)p_sys->i_frame_size + MAD_BUFFER_GUARD );
458
                memcpy( p_buf + p_sys->i_frame_size,
459
                        p_header, MAD_BUFFER_GUARD );
460
            }
461

462 463 464
            p_sys->i_state = STATE_NOSYNC;

            /* Make sure we don't reuse the same pts twice */
465
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
466
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
467 468 469 470 471

            /* So p_block doesn't get re-added several times */
            *pp_block = block_BytestreamPop( &p_sys->bytestream );

            return p_out_buffer;
472
        }
473
    }
474

475
    return NULL;
476 477 478 479 480
}

/*****************************************************************************
 * GetOutBuffer:
 *****************************************************************************/
481
static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
482 483
{
    decoder_sys_t *p_sys = p_dec->p_sys;
484 485 486 487
    uint8_t *p_buf;

    if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
    {
488
        msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
489
                  p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
490

491 492
        date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
        date_Set( &p_sys->end_date, p_sys->i_pts );
493 494 495 496 497 498 499 500 501 502 503
    }

    p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
    p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
    p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
    p_dec->fmt_out.audio.i_bytes_per_frame =
        p_sys->i_max_frame_size + MAD_BUFFER_GUARD;

    p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
    p_dec->fmt_out.audio.i_physical_channels =
        p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
504

505
    p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
506

507 508
    if( p_sys->b_packetizer )
    {
509 510 511
        block_t *p_sout_buffer = GetSoutBuffer( p_dec );
        p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
        *pp_out_buffer = p_sout_buffer;
512 513 514
    }
    else
    {
515
        block_t *p_aout_buffer = GetAoutBuffer( p_dec );
516 517
        p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
        *pp_out_buffer = p_aout_buffer;
518 519
    }

520
    return p_buf;
521 522 523 524 525
}

/*****************************************************************************
 * GetAoutBuffer:
 *****************************************************************************/
526
static block_t *GetAoutBuffer( decoder_t *p_dec )
527 528
{
    decoder_sys_t *p_sys = p_dec->p_sys;
529
    block_t *p_buf;
530

531
    p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
532
    if( p_buf == NULL ) return NULL;
533

534
    p_buf->i_pts = date_Get( &p_sys->end_date );
535 536
    p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
                      - p_buf->i_pts;
537 538
    if( p_sys->b_discontinuity )
        p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
539
    p_sys->b_discontinuity = false;
540

541
    /* Hack for libmad filter */
542
    p_buf = block_Realloc( p_buf, 0, p_sys->i_frame_size + MAD_BUFFER_GUARD );
543

544
    return p_buf;
545 546 547 548 549
}

/*****************************************************************************
 * GetSoutBuffer:
 *****************************************************************************/
550
static block_t *GetSoutBuffer( decoder_t *p_dec )
551 552
{
    decoder_sys_t *p_sys = p_dec->p_sys;
553
    block_t *p_block;
554

555
    p_block = block_Alloc( p_sys->i_frame_size );
556
    if( p_block == NULL ) return NULL;
557

558
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
559

560
    p_block->i_length =
561
        date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
562

563
    return p_block;
564 565 566
}

/*****************************************************************************
567
 * CloseDecoder: clean up the decoder
568
 *****************************************************************************/
569
static void CloseDecoder( vlc_object_t *p_this )
570
{
571
    decoder_t *p_dec = (decoder_t *)p_this;
572 573
    decoder_sys_t *p_sys = p_dec->p_sys;

574
    block_BytestreamRelease( &p_sys->bytestream );
575

576
    free( p_sys );
577 578 579 580 581 582
}

/*****************************************************************************
 * SyncInfo: parse MPEG audio sync info
 *****************************************************************************/
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
583
                     unsigned int * pi_channels_conf,
584 585
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
586
                     unsigned int * pi_max_frame_size, unsigned int * pi_layer)
587
{
588
    static const int ppi_bitrate[2][3][16] =
589 590 591 592 593 594 595 596 597 598
    {
        {
            /* v1 l1 */
            { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
              416, 448, 0},
            /* v1 l2 */
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
              320, 384, 0},
            /* v1 l3 */
            { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
599
              256, 320, 0}
600 601 602 603 604 605 606 607 608 609 610
        },

        {
            /* v2 l1 */
            { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
              224, 256, 0},
            /* v2 l2 */
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
              144, 160, 0},
            /* v2 l3 */
            { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
611
              144, 160, 0}
612 613 614
        }
    };

615
    static const int ppi_samplerate[2][4] = /* version 1 then 2 */
616 617 618 619 620 621
    {
        { 44100, 48000, 32000, 0 },
        { 22050, 24000, 16000, 0 }
    };

    int i_version, i_mode, i_emphasis;
622
    bool b_padding, b_mpeg_2_5;
623
    int i_frame_size = 0;
624
    int i_bitrate_index, i_samplerate_index;
625
    int i_max_bit_rate;
626 627 628 629

    b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
    i_version   = 1 - ((i_header & 0x80000) >> 19);
    *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
630
    //bool b_crc = !((i_header >> 16) & 0x01);
631 632 633 634 635 636 637 638 639
    i_bitrate_index = (i_header & 0xf000) >> 12;
    i_samplerate_index = (i_header & 0xc00) >> 10;
    b_padding   = (i_header & 0x200) >> 9;
    /* Extension */
    i_mode      = (i_header & 0xc0) >> 6;
    /* Modeext, copyright & original */
    i_emphasis  = i_header & 0x3;

    if( *pi_layer != 4 &&
640
        i_bitrate_index < 0x0f &&
641 642 643 644 645 646 647
        i_samplerate_index != 0x03 &&
        i_emphasis != 0x02 )
    {
        switch ( i_mode )
        {
        case 0: /* stereo */
        case 1: /* joint stereo */
648 649
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
650 651
            break;
        case 2: /* dual-mono */
652 653 654
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                                | AOUT_CHAN_DUALMONO;
655 656
            break;
        case 3: /* mono */
657 658
            *pi_channels = 1;
            *pi_channels_conf = AOUT_CHAN_CENTER;
659 660
            break;
        }
661 662 663
        *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
        i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
        *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
664 665 666 667 668 669

        if ( b_mpeg_2_5 )
        {
            *pi_sample_rate >>= 1;
        }

670
        switch( *pi_layer )
671
        {
672
        case 1:
673 674 675 676
            i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
                           b_padding ) * 4;
            *pi_max_frame_size = ( 12000 * i_max_bit_rate /
                                 *pi_sample_rate + 1 ) * 4;
677 678 679
            *pi_frame_length = 384;
            break;

680
        case 2:
681 682
            i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
            *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
683 684 685
            *pi_frame_length = 1152;
            break;

686
        case 3:
687 688 689
            i_frame_size = ( i_version ? 72000 : 144000 ) *
                           *pi_bit_rate / *pi_sample_rate + b_padding;
            *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
690
                                 i_max_bit_rate / *pi_sample_rate + 1;
691 692 693 694
            *pi_frame_length = i_version ? 576 : 1152;
            break;

        default:
695
            break;
696
        }
697 698 699

        /* Free bitrate mode can support higher bitrates */
        if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
700
    }
701 702 703 704
    else
    {
        return -1;
    }
705

706
    return i_frame_size;
707
}