mpeg_audio.c 23.2 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
 *****************************************************************************
 * Copyright (C) 2001-2003 VideoLAN
Benjamin Pracht's avatar
Benjamin Pracht committed
5
 * $Id: mpeg_audio.c,v 1.25 2004/01/25 18:20:12 bigben Exp $
6 7 8 9
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *          Christophe Massiot <massiot@via.ecp.fr>
Gildas Bazin's avatar
 
Gildas Bazin committed
10
 *          Gildas Bazin <gbazin@netcourrier.com>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/decoder.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
32 33 34
#include <vlc/aout.h>

#include "vlc_block_helper.h"
35 36

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
37
 * decoder_sys_t : decoder descriptor
38
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
39
struct decoder_sys_t
40
{
Gildas Bazin's avatar
 
Gildas Bazin committed
41 42 43
    /* Module mode */
    vlc_bool_t b_packetizer;

44
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
45
     * Input properties
46
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
47 48 49
    int        i_state;

    block_bytestream_t bytestream;
50

Gildas Bazin's avatar
 
Gildas Bazin committed
51 52 53 54 55 56
    /*
     * Common properties
     */
    audio_date_t          end_date;
    unsigned int          i_current_layer;

Gildas Bazin's avatar
 
Gildas Bazin committed
57
    mtime_t i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
58 59 60 61 62 63 64 65 66 67 68 69 70

    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;
};

enum {

    STATE_NOSYNC,
    STATE_SYNC,
    STATE_HEADER,
    STATE_NEXT_SYNC,
Gildas Bazin's avatar
 
Gildas Bazin committed
71 72
    STATE_GET_DATA,
    STATE_SEND_DATA
Gildas Bazin's avatar
 
Gildas Bazin committed
73
};
74 75 76 77 78 79 80

/* 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
 * buffer, because that way it doesn't have to copy the aout_buffer_t to a
 * bigger buffer. This has no implication on other plug-ins, and we only
 * lose 8 bytes per frame. --Meuuh */
#define MAD_BUFFER_GUARD 8
Gildas Bazin's avatar
 
Gildas Bazin committed
81
#define MPGA_HEADER_SIZE 4
82 83 84 85

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
86 87 88 89
static int  OpenDecoder   ( vlc_object_t * );
static int  OpenPacketizer( vlc_object_t * );
static void CloseDecoder  ( vlc_object_t * );
static void *DecodeBlock  ( decoder_t *, block_t ** );
90

Gildas Bazin's avatar
 
Gildas Bazin committed
91 92 93
static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
static aout_buffer_t *GetAoutBuffer( decoder_t * );
static block_t       *GetSoutBuffer( decoder_t * );
94 95

static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
96
                     unsigned int * pi_channels_conf,
97 98
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
Gildas Bazin's avatar
 
Gildas Bazin committed
99
                     unsigned int * pi_max_frame_size,
100 101 102 103 104 105 106 107
                     unsigned int * pi_layer );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("MPEG audio layer I/II/III parser") );
    set_capability( "decoder", 100 );
Gildas Bazin's avatar
 
Gildas Bazin committed
108
    set_callbacks( OpenDecoder, CloseDecoder );
Gildas Bazin's avatar
 
Gildas Bazin committed
109 110 111 112

    add_submodule();
    set_description( _("MPEG audio layer I/II/III packetizer") );
    set_capability( "packetizer", 10 );
Gildas Bazin's avatar
 
Gildas Bazin committed
113
    set_callbacks( OpenPacketizer, CloseDecoder );
114 115 116 117 118
vlc_module_end();

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
119
static int OpenDecoder( vlc_object_t *p_this )
120
{
Gildas Bazin's avatar
 
Gildas Bazin committed
121
    decoder_t *p_dec = (decoder_t*)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
122
    decoder_sys_t *p_sys;
123

Gildas Bazin's avatar
 
Gildas Bazin committed
124
    if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
125 126 127 128
    {
        return VLC_EGENERIC;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
129
    /* Allocate the memory needed to store the decoder's structure */
Gildas Bazin's avatar
 
Gildas Bazin committed
130
    if( ( p_dec->p_sys = p_sys =
Gildas Bazin's avatar
 
Gildas Bazin committed
131 132 133 134 135
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
    {
        msg_Err( p_dec, "out of memory" );
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

    /* Misc init */
    p_sys->b_packetizer = VLC_FALSE;
    p_sys->i_state = STATE_NOSYNC;
    aout_DateSet( &p_sys->end_date, 0 );
    p_sys->bytestream = block_BytestreamInit( p_dec );

    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
    p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');

    /* Set callback */
    p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
        DecodeBlock;
    p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
        DecodeBlock;

    /* Start with the minimum size for a free bitrate frame */
    p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
Gildas Bazin's avatar
 
Gildas Bazin committed
155

156 157 158
    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
159 160 161 162 163 164 165 166 167 168 169 170
static int OpenPacketizer( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;

    int i_ret = OpenDecoder( p_this );

    if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;

    return i_ret;
}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
171
 * DecodeBlock: the whole thing
Gildas Bazin's avatar
 
Gildas Bazin committed
172 173 174
 ****************************************************************************
 * This function is called just after the thread is launched.
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
175
static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
Gildas Bazin's avatar
 
Gildas Bazin committed
176 177 178 179
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t p_header[MAD_BUFFER_GUARD];
    uint32_t i_header;
Gildas Bazin's avatar
 
Gildas Bazin committed
180 181 182 183
    uint8_t *p_buf;
    void *p_out_buffer;

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

Gildas Bazin's avatar
 
Gildas Bazin committed
185
    if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
186
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
187
        /* We've just started the stream, wait for the first PTS. */
Gildas Bazin's avatar
 
Gildas Bazin committed
188 189
        block_Release( *pp_block );
        return NULL;
190 191
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
192
    if( (*pp_block)->b_discontinuity )
Gildas Bazin's avatar
 
Gildas Bazin committed
193
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
194
        p_sys->i_state = STATE_NOSYNC;
Gildas Bazin's avatar
 
Gildas Bazin committed
195 196
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
197
    block_BytestreamPush( &p_sys->bytestream, *pp_block );
198

Gildas Bazin's avatar
 
Gildas Bazin committed
199
    while( 1 )
200
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
201
        switch( p_sys->i_state )
202
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
203 204 205 206

        case STATE_NOSYNC:
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
                   == VLC_SUCCESS )
207
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
208 209 210 211
                /* Look for sync word - should be 0xffe */
                if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
                {
                    p_sys->i_state = STATE_SYNC;
212
                    break;
Gildas Bazin's avatar
 
Gildas Bazin committed
213 214 215 216 217
                }
                block_SkipByte( &p_sys->bytestream );
            }
            if( p_sys->i_state != STATE_SYNC )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
218
                block_BytestreamFlush( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
219 220

                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
221
                return NULL;
222
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
223 224 225

        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
Gildas Bazin's avatar
 
Gildas Bazin committed
226 227 228
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
            if( p_sys->i_pts != 0 &&
                p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
229
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
230
                aout_DateSet( &p_sys->end_date, p_sys->i_pts );
231
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
232 233 234 235 236 237
            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 )
238
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
239
                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
240
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
241 242 243 244 245 246 247 248
            }

            /* 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,
Gildas Bazin's avatar
 
Gildas Bazin committed
249
                                            &p_sys->i_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
250 251 252 253
                                            &p_sys->i_channels_conf,
                                            &p_sys->i_rate,
                                            &p_sys->i_bit_rate,
                                            &p_sys->i_frame_length,
Gildas Bazin's avatar
 
Gildas Bazin committed
254
                                            &p_sys->i_max_frame_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
255 256 257 258
                                            &p_sys->i_layer );

            if( p_sys->i_frame_size == -1 )
            {
Benjamin Pracht's avatar
Benjamin Pracht committed
259
                msg_Dbg( p_dec, "emulated startcode" );
Gildas Bazin's avatar
 
Gildas Bazin committed
260 261 262 263 264 265 266 267 268
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
                break;
            }

            if( p_sys->i_bit_rate == 0 )
            {
                /* Free birate, but 99% emulated startcode :( */
                if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
269
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
270
                    msg_Dbg( p_dec, "free bitrate mode");
271
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
272
                p_sys->i_frame_size = p_sys->i_free_frame_size;
273
            }
274

Gildas Bazin's avatar
 
Gildas Bazin committed
275
            p_sys->i_state = STATE_NEXT_SYNC;
276

Gildas Bazin's avatar
 
Gildas Bazin committed
277 278 279
        case STATE_NEXT_SYNC:
            /* TODO: If p_block == NULL, flush the buffer without checking the
             * next sync word */
280

Gildas Bazin's avatar
 
Gildas Bazin committed
281 282 283 284 285 286
            /* 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 */
Gildas Bazin's avatar
 
Gildas Bazin committed
287
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
288
            }
289

Gildas Bazin's avatar
 
Gildas Bazin committed
290 291 292 293
            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;
Gildas Bazin's avatar
 
Gildas Bazin committed
294 295
                unsigned int i_next_channels, i_next_channels_conf;
                unsigned int i_next_rate, i_next_bit_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
296 297
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
298

Gildas Bazin's avatar
 
Gildas Bazin committed
299 300 301
                /* Build frame header */
                i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
                           |p_header[3];
302

Gildas Bazin's avatar
 
Gildas Bazin committed
303 304
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
305
                                              &i_next_channels_conf,
Gildas Bazin's avatar
 
Gildas Bazin committed
306 307 308 309 310
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
311

Gildas Bazin's avatar
 
Gildas Bazin committed
312
                /* Free bitrate only */
Gildas Bazin's avatar
 
Gildas Bazin committed
313
                if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
314 315 316 317 318 319 320 321 322 323 324 325
                {
                    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;
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
326 327 328 329 330 331 332

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
Benjamin Pracht's avatar
Benjamin Pracht committed
333
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
Gildas Bazin's avatar
 
Gildas Bazin committed
334 335 336
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
                    break;
Gildas Bazin's avatar
 
Gildas Bazin committed
337 338 339
                }

                /* Check info is in sync with previous one */
Gildas Bazin's avatar
 
Gildas Bazin committed
340
                if( i_next_channels_conf != p_sys->i_channels_conf ||
Gildas Bazin's avatar
 
Gildas Bazin committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
                    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;
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
358 359 360 361 362 363 364 365 366 367 368

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

369 370 371
            }
            else
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
372 373 374
                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
375 376 377 378 379 380 381 382 383 384 385 386
                    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;
                    }

Gildas Bazin's avatar
 
Gildas Bazin committed
387 388 389 390
                    p_sys->i_frame_size++;
                    break;
                }

Gildas Bazin's avatar
 
Gildas Bazin committed
391 392 393 394 395
                msg_Dbg( p_dec, "emulated startcode "
                         "(no startcode on following frame)" );
                p_sys->i_state = STATE_NOSYNC;
                block_SkipByte( &p_sys->bytestream );
                break;
Gildas Bazin's avatar
 
Gildas Bazin committed
396 397
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
398 399 400 401 402 403 404 405
            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 )
Gildas Bazin's avatar
 
Gildas Bazin committed
406
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
407 408
                /* Need more data */
                return NULL;
409
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
410
            p_sys->i_state = STATE_SEND_DATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
411

Gildas Bazin's avatar
 
Gildas Bazin committed
412 413
        case STATE_SEND_DATA:
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
414
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
415 416
                //p_dec->b_error = VLC_TRUE;
                return NULL;
417 418
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
419 420
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
421
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
422
                p_sys->i_free_frame_size = p_sys->i_frame_size;
Gildas Bazin's avatar
 
Gildas Bazin committed
423
            }
424

Gildas Bazin's avatar
 
Gildas Bazin committed
425 426 427
            /* Copy the whole frame into the buffer. When we reach this point
             * we already know we have enough data available. */
            block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
428

Gildas Bazin's avatar
 
Gildas Bazin committed
429 430 431
            /* Get beginning of next frame for libmad */
            if( !p_sys->b_packetizer )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
432
                memcpy( p_buf + p_sys->i_frame_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
433
                        p_header, MAD_BUFFER_GUARD );
434
            }
435

Gildas Bazin's avatar
 
Gildas Bazin committed
436 437 438
            p_sys->i_state = STATE_NOSYNC;

            /* Make sure we don't reuse the same pts twice */
Gildas Bazin's avatar
 
Gildas Bazin committed
439 440 441 442 443 444 445
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;

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

            return p_out_buffer;
446
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
447
    }
448

Gildas Bazin's avatar
 
Gildas Bazin committed
449
    return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
450 451 452 453 454
}

/*****************************************************************************
 * GetOutBuffer:
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
455
static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
Gildas Bazin's avatar
 
Gildas Bazin committed
456 457
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    uint8_t *p_buf;

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

        aout_DateInit( &p_sys->end_date, p_sys->i_rate );
        aout_DateSet( &p_sys->end_date, p_sys->i_pts );
    }

    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;
Gildas Bazin's avatar
 
Gildas Bazin committed
478

Gildas Bazin's avatar
 
Gildas Bazin committed
479 480
    p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate;

Gildas Bazin's avatar
 
Gildas Bazin committed
481 482
    if( p_sys->b_packetizer )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
483 484 485
        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;
Gildas Bazin's avatar
 
Gildas Bazin committed
486 487 488
    }
    else
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
489 490 491
        aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
        p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
        *pp_out_buffer = p_aout_buffer;
Gildas Bazin's avatar
 
Gildas Bazin committed
492 493
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
494
    return p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
495 496 497 498 499
}

/*****************************************************************************
 * GetAoutBuffer:
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
500
static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
501 502
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
503
    aout_buffer_t *p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
504

Gildas Bazin's avatar
 
Gildas Bazin committed
505 506
    p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
    if( p_buf == NULL ) return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
507

Gildas Bazin's avatar
 
Gildas Bazin committed
508 509 510
    p_buf->start_date = aout_DateGet( &p_sys->end_date );
    p_buf->end_date =
        aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
Gildas Bazin's avatar
 
Gildas Bazin committed
511

Gildas Bazin's avatar
 
Gildas Bazin committed
512 513
    /* Hack for libmad filter */
    p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
514

Gildas Bazin's avatar
 
Gildas Bazin committed
515
    return p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
516 517 518 519 520
}

/*****************************************************************************
 * GetSoutBuffer:
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
521
static block_t *GetSoutBuffer( decoder_t *p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
522 523
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
524
    block_t *p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
525

Gildas Bazin's avatar
 
Gildas Bazin committed
526 527
    p_block = block_New( p_dec, p_sys->i_frame_size );
    if( p_block == NULL ) return NULL;
528

Gildas Bazin's avatar
 
Gildas Bazin committed
529
    p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
Gildas Bazin's avatar
 
Gildas Bazin committed
530

Gildas Bazin's avatar
 
Gildas Bazin committed
531 532 533
    p_block->i_length =
        aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
            p_block->i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
534

Gildas Bazin's avatar
 
Gildas Bazin committed
535
    return p_block;
536 537 538
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
539
 * CloseDecoder: clean up the decoder
540
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
541
static void CloseDecoder( vlc_object_t *p_this )
542
{
Gildas Bazin's avatar
 
Gildas Bazin committed
543
    decoder_t *p_dec = (decoder_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
544 545
    decoder_sys_t *p_sys = p_dec->p_sys;

Gildas Bazin's avatar
 
Gildas Bazin committed
546
    block_BytestreamRelease( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
547

Gildas Bazin's avatar
 
Gildas Bazin committed
548
    free( p_sys );
549 550 551 552 553 554
}

/*****************************************************************************
 * SyncInfo: parse MPEG audio sync info
 *****************************************************************************/
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
555
                     unsigned int * pi_channels_conf,
556 557
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
Gildas Bazin's avatar
 
Gildas Bazin committed
558
                     unsigned int * pi_max_frame_size, unsigned int * pi_layer)
559
{
Gildas Bazin's avatar
 
Gildas Bazin committed
560
    static const int ppi_bitrate[2][3][16] =
561 562 563 564 565 566 567 568 569 570
    {
        {
            /* 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,
571
              256, 320, 0}
572 573 574 575 576 577 578 579 580 581 582
        },

        {
            /* 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,
583
              144, 160, 0}
584 585 586
        }
    };

Gildas Bazin's avatar
 
Gildas Bazin committed
587
    static const int ppi_samplerate[2][4] = /* version 1 then 2 */
588 589 590 591 592 593
    {
        { 44100, 48000, 32000, 0 },
        { 22050, 24000, 16000, 0 }
    };

    int i_version, i_mode, i_emphasis;
Gildas Bazin's avatar
 
Gildas Bazin committed
594
    vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
Gildas Bazin's avatar
 
Gildas Bazin committed
595
    int i_frame_size = 0;
596
    int i_bitrate_index, i_samplerate_index;
597
    int i_max_bit_rate;
598 599 600 601

    b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
    i_version   = 1 - ((i_header & 0x80000) >> 19);
    *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
Gildas Bazin's avatar
 
Gildas Bazin committed
602
    b_crc = !((i_header >> 16) & 0x01);
603 604 605 606 607 608 609 610 611
    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 &&
612
        i_bitrate_index < 0x0f &&
613 614 615 616 617 618 619
        i_samplerate_index != 0x03 &&
        i_emphasis != 0x02 )
    {
        switch ( i_mode )
        {
        case 0: /* stereo */
        case 1: /* joint stereo */
Gildas Bazin's avatar
 
Gildas Bazin committed
620 621
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
622 623
            break;
        case 2: /* dual-mono */
Gildas Bazin's avatar
 
Gildas Bazin committed
624 625 626
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                                | AOUT_CHAN_DUALMONO;
627 628
            break;
        case 3: /* mono */
Gildas Bazin's avatar
 
Gildas Bazin committed
629 630
            *pi_channels = 1;
            *pi_channels_conf = AOUT_CHAN_CENTER;
631 632
            break;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
633 634 635
        *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];
636 637 638 639 640 641

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

642
        switch( *pi_layer )
643
        {
644
        case 1:
Gildas Bazin's avatar
 
Gildas Bazin committed
645 646 647 648
            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;
649 650 651
            *pi_frame_length = 384;
            break;

652
        case 2:
Gildas Bazin's avatar
 
Gildas Bazin committed
653 654
            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;
655 656 657
            *pi_frame_length = 1152;
            break;

658
        case 3:
Gildas Bazin's avatar
 
Gildas Bazin committed
659 660 661
            i_frame_size = ( i_version ? 72000 : 144000 ) *
                           *pi_bit_rate / *pi_sample_rate + b_padding;
            *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
662
                                 i_max_bit_rate / *pi_sample_rate + 1;
663 664 665 666
            *pi_frame_length = i_version ? 576 : 1152;
            break;

        default:
667
            break;
668 669
        }
    }
670 671 672 673
    else
    {
        return -1;
    }
674

Gildas Bazin's avatar
 
Gildas Bazin committed
675
    return i_frame_size;
676
}