mpegaudio.c 22.6 KB
Newer Older
1
/*****************************************************************************
2
 * mpegaudio.c: parse MPEG audio sync info and packetize the stream
3
 *****************************************************************************
4
 * Copyright (C) 2001-2016 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
LGPL  
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
LGPL  
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
LGPL  
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>
Clément Stenac's avatar
Clément Stenac committed
36 37
#include <vlc_codec.h>
#include <vlc_aout.h>
38
#include <vlc_modules.h>
39
#include <assert.h>
40

Clément Stenac's avatar
Clément Stenac committed
41
#include <vlc_block_helper.h>
42

43
#include "packetizer_helper.h"
44

45
/*****************************************************************************
46
 * decoder_sys_t : decoder descriptor
47
 *****************************************************************************/
48
struct decoder_sys_t
49 50
{
    /*
51
     * Input properties
52
     */
53 54 55
    int        i_state;

    block_bytestream_t bytestream;
56

57 58 59
    /*
     * Common properties
     */
60
    date_t          end_date;
61

62
    mtime_t i_pts;
63 64

    int i_frame_size, i_free_frame_size;
65
    unsigned int i_channels_conf, i_chan_mode, i_channels;
66 67
    unsigned int i_rate, i_max_frame_size, i_frame_length;
    unsigned int i_layer, i_bit_rate;
68

69
    bool   b_discontinuity;
70 71
};

72
#define MAD_BUFFER_GUARD 8
73
#define MPGA_HEADER_SIZE 4
74 75 76 77

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
78 79 80

static int  Open( vlc_object_t * );
static void Close(  vlc_object_t * );
81 82 83 84

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
85 86 87 88 89
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
    set_description( N_("MPEG audio layer I/II/III packetizer") )
    set_capability( "packetizer", 10 )
90
    set_callbacks( Open, Close )
91
vlc_module_end ()
92 93

/*****************************************************************************
Thomas Guillem's avatar
Thomas Guillem committed
94
 * Flush:
95
 *****************************************************************************/
Thomas Guillem's avatar
Thomas Guillem committed
96
static void Flush( decoder_t *p_dec )
97
{
Thomas Guillem's avatar
Thomas Guillem committed
98
    decoder_sys_t *p_sys = p_dec->p_sys;
99

100
    date_Set( &p_sys->end_date, VLC_TS_INVALID );
Thomas Guillem's avatar
Thomas Guillem committed
101 102 103
    p_sys->i_state = STATE_NOSYNC;
    block_BytestreamEmpty( &p_sys->bytestream );
    p_sys->b_discontinuity = true;
104 105
}

Thomas Guillem's avatar
Thomas Guillem committed
106 107 108 109
/*****************************************************************************
 * GetOutBuffer:
 *****************************************************************************/
static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
110 111
{
    decoder_sys_t *p_sys = p_dec->p_sys;
112

113 114
    if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate ||
        date_Get( &p_sys->end_date ) == VLC_TS_INVALID )
115
    {
Thomas Guillem's avatar
Thomas Guillem committed
116 117
        msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
                  p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
118

119 120 121 122
        if( p_sys->end_date.i_divider_num == 0 )
            date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
        else
            date_Change( &p_sys->end_date, p_sys->i_rate, 1 );
Thomas Guillem's avatar
Thomas Guillem committed
123 124
        date_Set( &p_sys->end_date, p_sys->i_pts );
    }
125

126
    p_dec->fmt_out.i_profile        = p_sys->i_layer;
Thomas Guillem's avatar
Thomas Guillem committed
127 128 129
    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;
130
    p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->i_max_frame_size;
Thomas Guillem's avatar
Thomas Guillem committed
131

132 133
    p_dec->fmt_out.audio.i_physical_channels = p_sys->i_channels_conf;
    p_dec->fmt_out.audio.i_chan_mode = p_sys->i_chan_mode;
Thomas Guillem's avatar
Thomas Guillem committed
134 135 136

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

137 138 139 140 141 142 143 144 145 146
    block_t *p_block = block_Alloc( p_sys->i_frame_size );
    if( p_block == NULL )
        return NULL;

    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
    p_block->i_length =
        date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;

    *pp_out_buffer = p_block;
    return p_block->p_buffer;
Thomas Guillem's avatar
Thomas Guillem committed
147
}
148

Thomas Guillem's avatar
Thomas Guillem committed
149 150 151 152 153
/*****************************************************************************
 * SyncInfo: parse MPEG audio sync info
 *****************************************************************************/
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
                     unsigned int * pi_channels_conf,
154
                     unsigned int * pi_chan_mode,
Thomas Guillem's avatar
Thomas Guillem committed
155 156 157 158 159
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
                     unsigned int * pi_max_frame_size, unsigned int * pi_layer)
{
    static const int ppi_bitrate[2][3][16] =
160 161
    {
        {
Thomas Guillem's avatar
Thomas Guillem committed
162 163 164 165 166 167 168 169 170 171
            /* 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,
              256, 320, 0}
        },
172

Thomas Guillem's avatar
Thomas Guillem committed
173 174 175 176 177 178 179 180 181 182 183 184
        {
            /* 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,
              144, 160, 0}
        }
    };
185

Thomas Guillem's avatar
Thomas Guillem committed
186 187 188 189 190
    static const int ppi_samplerate[2][4] = /* version 1 then 2 */
    {
        { 44100, 48000, 32000, 0 },
        { 22050, 24000, 16000, 0 }
    };
191

Thomas Guillem's avatar
Thomas Guillem committed
192 193 194 195 196
    int i_version, i_mode, i_emphasis;
    bool b_padding, b_mpeg_2_5;
    int i_frame_size = 0;
    int i_bitrate_index, i_samplerate_index;
    int i_max_bit_rate;
197

Thomas Guillem's avatar
Thomas Guillem committed
198 199 200 201 202 203 204 205 206 207 208
    b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
    i_version   = 1 - ((i_header & 0x80000) >> 19);
    *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
    //bool b_crc = !((i_header >> 16) & 0x01);
    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;
209
    *pi_chan_mode = 0;
Thomas Guillem's avatar
Thomas Guillem committed
210 211 212 213 214 215 216 217

    if( *pi_layer != 4 &&
        i_bitrate_index < 0x0f &&
        i_samplerate_index != 0x03 &&
        i_emphasis != 0x02 )
    {
        switch ( i_mode )
        {
218 219 220
        case 2: /* dual-mono */
            *pi_chan_mode = AOUT_CHANMODE_DUALMONO;
            /* fall through */
Thomas Guillem's avatar
Thomas Guillem committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        case 0: /* stereo */
        case 1: /* joint stereo */
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
            break;
        case 3: /* mono */
            *pi_channels = 1;
            *pi_channels_conf = AOUT_CHAN_CENTER;
            break;
        }
        *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];

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

        switch( *pi_layer )
        {
        case 1:
            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;
            *pi_frame_length = 384;
            break;

        case 2:
            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;
            *pi_frame_length = 1152;
            break;

        case 3:
            i_frame_size = ( i_version ? 72000 : 144000 ) *
                           *pi_bit_rate / *pi_sample_rate + b_padding;
            *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
                                 i_max_bit_rate / *pi_sample_rate + 1;
            *pi_frame_length = i_version ? 576 : 1152;
            break;

        default:
            break;
        }

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

    return i_frame_size;
}

/****************************************************************************
 * DecodeBlock: the whole thing
 ****************************************************************************
 * This function is called just after the thread is launched.
 ****************************************************************************/
static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t p_header[MAD_BUFFER_GUARD];
    uint32_t i_header;
    uint8_t *p_buf;
    block_t *p_out_buffer;

    block_t *p_block = pp_block ? *pp_block : NULL;

    if (p_block)
    {
        if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
        {
298 299 300 301 302 303 304
            /* First always drain complete blocks before discontinuity */
            block_t *p_drain = DecodeBlock( p_dec, NULL );
            if( p_drain )
                return p_drain;

            Flush( p_dec );

Thomas Guillem's avatar
Thomas Guillem committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
            {
                block_Release( p_block );
                return NULL;
            }
        }

        if( !date_Get( &p_sys->end_date ) && p_block->i_pts <= VLC_TS_INVALID )
        {
            /* We've just started the stream, wait for the first PTS. */
            msg_Dbg( p_dec, "waiting for PTS" );
            block_Release( p_block );
            return NULL;
        }

        block_BytestreamPush( &p_sys->bytestream, p_block );
321
    }
Thomas Guillem's avatar
Thomas Guillem committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

    while( 1 )
    {
        switch( p_sys->i_state )
        {

        case STATE_NOSYNC:
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
                   == VLC_SUCCESS )
            {
                /* Look for sync word - should be 0xffe */
                if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
                {
                    p_sys->i_state = STATE_SYNC;
                    break;
                }
                block_SkipByte( &p_sys->bytestream );
            }
            if( p_sys->i_state != STATE_SYNC )
            {
                block_BytestreamFlush( &p_sys->bytestream );

                /* Need more data */
                return NULL;
            }
347
            /* fallthrough */
Thomas Guillem's avatar
Thomas Guillem committed
348 349 350 351

        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
352 353
            if( p_sys->i_pts > VLC_TS_INVALID &&
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
Thomas Guillem's avatar
Thomas Guillem committed
354
            {
355 356 357 358 359 360 361 362 363
                if( p_dec->fmt_in.i_original_fourcc == VLC_FOURCC( 'D','V','R',' ') )
                {
                    if( date_Get( &p_sys->end_date ) == VLC_TS_INVALID )
                        date_Set( &p_sys->end_date, p_sys->i_pts );
                }
                else if ( p_sys->i_pts != date_Get( &p_sys->end_date ) )
                {
                    date_Set( &p_sys->end_date, p_sys->i_pts );
                }
Thomas Guillem's avatar
Thomas Guillem committed
364 365
            }
            p_sys->i_state = STATE_HEADER;
366
            /* fallthrough */
Thomas Guillem's avatar
Thomas Guillem committed
367 368 369 370 371 372 373 374 375 376 377 378

        case STATE_HEADER:
            /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
            if( block_PeekBytes( &p_sys->bytestream, p_header,
                                 MPGA_HEADER_SIZE ) != VLC_SUCCESS )
            {
                /* Need more data */
                return NULL;
            }

            /* Build frame header */
            i_header = GetDWBE(p_header);
379 380 381

            /* Check if frame is valid and get frame info */
            p_sys->i_frame_size = SyncInfo( i_header,
382
                                            &p_sys->i_channels,
383
                                            &p_sys->i_channels_conf,
384
                                            &p_sys->i_chan_mode,
385 386 387
                                            &p_sys->i_rate,
                                            &p_sys->i_bit_rate,
                                            &p_sys->i_frame_length,
388
                                            &p_sys->i_max_frame_size,
389 390 391 392
                                            &p_sys->i_layer );

            if( p_sys->i_frame_size == -1 )
            {
Benjamin Pracht's avatar
Benjamin Pracht committed
393
                msg_Dbg( p_dec, "emulated startcode" );
394 395 396 397 398 399 400
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
                break;
            }

            if( p_sys->i_bit_rate == 0 )
            {
401
                /* Free bitrate, but 99% emulated startcode :( */
402
                if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
403
                {
404
                    msg_Dbg( p_dec, "free bitrate mode");
405
                }
406 407
                /* The -1 below is to account for the frame padding */
                p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
408
            }
409

410
            p_sys->i_state = STATE_NEXT_SYNC;
411
            /* fallthrough */
412

413 414 415 416 417 418
        case STATE_NEXT_SYNC:
            /* 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 )
            {
419 420 421 422 423
                if( p_block == NULL ) /* drain */
                {
                    p_sys->i_state = STATE_SEND_DATA;
                    break;
                }
424
                /* Need more data */
425
                return NULL;
426
            }
427

428 429 430 431
            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;
432
                unsigned int i_next_channels, i_next_stereo_mode, i_next_channels_conf;
433
                unsigned int i_next_rate, i_next_bit_rate;
434 435
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
436

437
                /* Build frame header */
438
                i_header = GetDWBE(p_header);
439

440 441
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
442
                                              &i_next_channels_conf,
443
                                              &i_next_stereo_mode,
444 445 446 447 448
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
449

450
                /* Free bitrate only */
451
                if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
452 453 454 455 456 457 458 459 460 461 462 463
                {
                    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;
                    }
464 465 466 467 468 469 470

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
Benjamin Pracht's avatar
Benjamin Pracht committed
471
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
472 473 474
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
                    break;
475 476 477
                }

                /* Check info is in sync with previous one */
478
                if( i_next_channels_conf != p_sys->i_channels_conf ||
479
                    i_next_stereo_mode != p_sys->i_chan_mode ||
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
                    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;
                }
497 498 499 500 501 502 503 504 505 506 507

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

508 509 510
            }
            else
            {
511 512 513
                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
514 515 516 517 518 519 520 521 522 523 524 525
                    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;
                    }

526 527 528 529
                    p_sys->i_frame_size++;
                    break;
                }

530 531 532 533 534
                msg_Dbg( p_dec, "emulated startcode "
                         "(no startcode on following frame)" );
                p_sys->i_state = STATE_NOSYNC;
                block_SkipByte( &p_sys->bytestream );
                break;
535 536
            }

537
            p_sys->i_state = STATE_GET_DATA;
538 539 540 541 542 543 544
            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 )
545
            {
546 547
                /* Need more data */
                return NULL;
548
            }
549
            p_sys->i_state = STATE_SEND_DATA;
550
            /* fallthrough */
551

552 553
        case STATE_SEND_DATA:
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
554
            {
555
                return NULL;
556 557
            }

558 559
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
560
            {
561
                p_sys->i_free_frame_size = p_sys->i_frame_size;
562
            }
563

564 565 566 567 568 569
            /* Copy the whole frame into the buffer. */
            if (block_GetBytes( &p_sys->bytestream,
                            p_buf, __MIN( (unsigned)p_sys->i_frame_size, p_out_buffer->i_buffer ) )) {
                block_Release(p_out_buffer);
                return NULL;
            }
570

571 572 573
            p_sys->i_state = STATE_NOSYNC;

            /* Make sure we don't reuse the same pts twice */
574
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
575
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
576

577 578 579 580 581 582
            if( p_sys->b_discontinuity )
            {
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
                p_sys->b_discontinuity = false;
            }

583
            /* So p_block doesn't get re-added several times */
584 585 586 587 588
            p_block = block_BytestreamPop( &p_sys->bytestream );
            if (pp_block)
                *pp_block = p_block;
            else if (p_block)
                block_Release(p_block);
589 590

            return p_out_buffer;
591
        }
592
    }
593

594
    return NULL;
595 596
}

597
/*****************************************************************************
598
 * Close: clean up the decoder
599
 *****************************************************************************/
600
static void Close( vlc_object_t *p_this )
601
{
602
    decoder_t *p_dec = (decoder_t *)p_this;
603 604
    decoder_sys_t *p_sys = p_dec->p_sys;

605
    block_BytestreamRelease( &p_sys->bytestream );
606

607
    free( p_sys );
608 609 610
}

/*****************************************************************************
Thomas Guillem's avatar
Thomas Guillem committed
611
 * Open: probe the decoder and return score
612
 *****************************************************************************/
Thomas Guillem's avatar
Thomas Guillem committed
613
static int Open( vlc_object_t *p_this )
614
{
Thomas Guillem's avatar
Thomas Guillem committed
615 616
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
617

Thomas Guillem's avatar
Thomas Guillem committed
618 619
    if(( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA ) &&
       ( p_dec->fmt_in.i_codec != VLC_CODEC_MP3 ) )
620
    {
Thomas Guillem's avatar
Thomas Guillem committed
621 622
        return VLC_EGENERIC;
    }
623

Thomas Guillem's avatar
Thomas Guillem committed
624 625 626 627
    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_dec->p_sys = p_sys =
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
        return VLC_ENOMEM;
628

Thomas Guillem's avatar
Thomas Guillem committed
629 630
    /* Misc init */
    p_sys->i_state = STATE_NOSYNC;
631 632
    date_Init( &p_sys->end_date, 1, 1 );
    date_Set( &p_sys->end_date, VLC_TS_INVALID );
Thomas Guillem's avatar
Thomas Guillem committed
633 634 635 636
    block_BytestreamInit( &p_sys->bytestream );
    p_sys->i_pts = VLC_TS_INVALID;
    p_sys->b_discontinuity = false;
    p_sys->i_frame_size = 0;
637

638 639 640
    p_sys->i_channels_conf = p_sys->i_chan_mode = p_sys->i_channels =
    p_sys->i_rate = p_sys->i_max_frame_size = p_sys->i_frame_length =
    p_sys->i_layer = p_sys->i_bit_rate = 0;
641

Thomas Guillem's avatar
Thomas Guillem committed
642 643 644
    /* Set output properties */
    p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
    p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
645

Thomas Guillem's avatar
Thomas Guillem committed
646 647 648
    /* Set callback */
    p_dec->pf_packetize    = DecodeBlock;
    p_dec->pf_flush        = Flush;
649

Thomas Guillem's avatar
Thomas Guillem committed
650 651
    /* Start with the minimum size for a free bitrate frame */
    p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
652

Thomas Guillem's avatar
Thomas Guillem committed
653 654
    return VLC_SUCCESS;
}