mpegaudio.c 22.2 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>
Gildas Bazin's avatar
 
Gildas Bazin committed
40

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

43
#include "packetizer_helper.h"
44

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

    block_bytestream_t bytestream;
56

Gildas Bazin's avatar
 
Gildas Bazin committed
57 58 59
    /*
     * Common properties
     */
60
    date_t          end_date;
Gildas Bazin's avatar
 
Gildas Bazin committed
61

Gildas Bazin's avatar
 
Gildas Bazin committed
62
    mtime_t i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
63 64 65 66 67

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

69
    bool   b_discontinuity;
Gildas Bazin's avatar
 
Gildas Bazin committed
70 71
};

72
#define MAD_BUFFER_GUARD 8
Gildas Bazin's avatar
 
Gildas Bazin committed
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;
Gildas Bazin's avatar
 
Gildas Bazin committed
99

100
    date_Set( &p_sys->end_date, 0 );
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 )
Gildas Bazin's avatar
 
Gildas Bazin committed
110 111
{
    decoder_sys_t *p_sys = p_dec->p_sys;
112

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

Thomas Guillem's avatar
Thomas Guillem committed
118 119 120
        date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
        date_Set( &p_sys->end_date, p_sys->i_pts );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
121

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

    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;

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

133 134 135 136 137 138 139 140 141 142
    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
143
}
144

Thomas Guillem's avatar
Thomas Guillem committed
145 146 147 148 149 150 151 152 153 154
/*****************************************************************************
 * SyncInfo: parse MPEG audio sync info
 *****************************************************************************/
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
                     unsigned int * pi_channels_conf,
                     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] =
155 156
    {
        {
Thomas Guillem's avatar
Thomas Guillem committed
157 158 159 160 161 162 163 164 165 166
            /* 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}
        },
Gildas Bazin's avatar
 
Gildas Bazin committed
167

Thomas Guillem's avatar
Thomas Guillem committed
168 169 170 171 172 173 174 175 176 177 178 179
        {
            /* 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}
        }
    };
Gildas Bazin's avatar
 
Gildas Bazin committed
180

Thomas Guillem's avatar
Thomas Guillem committed
181 182 183 184 185
    static const int ppi_samplerate[2][4] = /* version 1 then 2 */
    {
        { 44100, 48000, 32000, 0 },
        { 22050, 24000, 16000, 0 }
    };
Gildas Bazin's avatar
 
Gildas Bazin committed
186

Thomas Guillem's avatar
Thomas Guillem committed
187 188 189 190 191
    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;
Gildas Bazin's avatar
 
Gildas Bazin committed
192

Thomas Guillem's avatar
Thomas Guillem committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 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 347
    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;

    if( *pi_layer != 4 &&
        i_bitrate_index < 0x0f &&
        i_samplerate_index != 0x03 &&
        i_emphasis != 0x02 )
    {
        switch ( i_mode )
        {
        case 0: /* stereo */
        case 1: /* joint stereo */
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
            break;
        case 2: /* dual-mono */
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                                | AOUT_CHAN_DUALMONO;
            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) )
        {
            if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
            {
                Flush( p_dec );
                block_Release( p_block );
                *pp_block = NULL;
                return NULL;
            }
            else /* BLOCK_FLAG_DISCONTINUITY */
                date_Set( &p_sys->end_date, 0 );
            p_sys->b_discontinuity = true;
        }

        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 );
    } else if (p_sys->i_frame_size)
        p_sys->i_state = STATE_SEND_DATA; /* return all the data we have left */
    else
        return NULL;

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

        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
348
            if( p_sys->i_pts > VLC_TS_INVALID )
Thomas Guillem's avatar
Thomas Guillem committed
349
            {
350 351 352 353 354 355 356 357 358 359
                /* start or discontinuity */
                if( date_Get( &p_sys->end_date ) == 0 )
                {
                    date_Set( &p_sys->end_date, p_sys->i_pts );
                }
                else if( date_Get( &p_sys->end_date ) != p_sys->i_pts )
                {
                    date_Set( &p_sys->end_date, p_sys->i_pts );
                    p_sys->b_discontinuity = true;
                }
Thomas Guillem's avatar
Thomas Guillem committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373
            }
            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 )
            {
                /* Need more data */
                return NULL;
            }

            /* Build frame header */
            i_header = GetDWBE(p_header);
Gildas Bazin's avatar
 
Gildas Bazin committed
374 375 376

            /* Check if frame is valid and get frame info */
            p_sys->i_frame_size = SyncInfo( i_header,
Gildas Bazin's avatar
 
Gildas Bazin committed
377
                                            &p_sys->i_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
378 379 380 381
                                            &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
382
                                            &p_sys->i_max_frame_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
383 384
                                            &p_sys->i_layer );

385 386
            p_dec->fmt_in.i_profile = p_sys->i_layer;

Gildas Bazin's avatar
 
Gildas Bazin committed
387 388
            if( p_sys->i_frame_size == -1 )
            {
Benjamin Pracht's avatar
Benjamin Pracht committed
389
                msg_Dbg( p_dec, "emulated startcode" );
Gildas Bazin's avatar
 
Gildas Bazin committed
390 391 392 393 394 395 396
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
                break;
            }

            if( p_sys->i_bit_rate == 0 )
            {
397
                /* Free bitrate, but 99% emulated startcode :( */
Gildas Bazin's avatar
 
Gildas Bazin committed
398
                if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
399
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
400
                    msg_Dbg( p_dec, "free bitrate mode");
401
                }
402 403
                /* The -1 below is to account for the frame padding */
                p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
404
            }
405

Gildas Bazin's avatar
 
Gildas Bazin committed
406
            p_sys->i_state = STATE_NEXT_SYNC;
407

Gildas Bazin's avatar
 
Gildas Bazin committed
408 409 410
        case STATE_NEXT_SYNC:
            /* TODO: If p_block == NULL, flush the buffer without checking the
             * next sync word */
411

Gildas Bazin's avatar
 
Gildas Bazin committed
412 413 414 415 416 417
            /* 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
418
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
419
            }
420

Gildas Bazin's avatar
 
Gildas Bazin committed
421 422 423 424
            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
425 426
                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
427 428
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
429

Gildas Bazin's avatar
 
Gildas Bazin committed
430
                /* Build frame header */
431
                i_header = GetDWBE(p_header);
432

Gildas Bazin's avatar
 
Gildas Bazin committed
433 434
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
435
                                              &i_next_channels_conf,
Gildas Bazin's avatar
 
Gildas Bazin committed
436 437 438 439 440
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
441

Gildas Bazin's avatar
 
Gildas Bazin committed
442
                /* Free bitrate only */
Gildas Bazin's avatar
 
Gildas Bazin committed
443
                if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
444 445 446 447 448 449 450 451 452 453 454 455
                {
                    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
456 457 458 459 460 461 462

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
Benjamin Pracht's avatar
Benjamin Pracht committed
463
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
Gildas Bazin's avatar
 
Gildas Bazin committed
464 465 466
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
                    break;
Gildas Bazin's avatar
 
Gildas Bazin committed
467 468 469
                }

                /* Check info is in sync with previous one */
Gildas Bazin's avatar
 
Gildas Bazin committed
470
                if( i_next_channels_conf != p_sys->i_channels_conf ||
Gildas Bazin's avatar
 
Gildas Bazin committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                    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
488 489 490 491 492 493 494 495 496 497 498

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

499 500 501
            }
            else
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
502 503 504
                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
505 506 507 508 509 510 511 512 513 514 515 516
                    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
517 518 519 520
                    p_sys->i_frame_size++;
                    break;
                }

Gildas Bazin's avatar
 
Gildas Bazin committed
521 522 523 524 525
                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
526 527
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
528 529 530 531 532 533 534 535
            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
536
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
537 538
                /* Need more data */
                return NULL;
539
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
540
            p_sys->i_state = STATE_SEND_DATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
541

Gildas Bazin's avatar
 
Gildas Bazin committed
542 543
        case STATE_SEND_DATA:
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
544
            {
545
                //p_dec->b_error = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
546
                return NULL;
547 548
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
549 550
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
551
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
552
                p_sys->i_free_frame_size = p_sys->i_frame_size;
Gildas Bazin's avatar
 
Gildas Bazin committed
553
            }
554

555 556 557 558 559 560
            /* 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;
            }
561

Gildas Bazin's avatar
 
Gildas Bazin committed
562 563 564
            p_sys->i_state = STATE_NOSYNC;

            /* Make sure we don't reuse the same pts twice */
Gildas Bazin's avatar
 
Gildas Bazin committed
565
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
566
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
Gildas Bazin's avatar
 
Gildas Bazin committed
567

568 569 570 571 572 573
            if( p_sys->b_discontinuity )
            {
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
                p_sys->b_discontinuity = false;
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
574
            /* So p_block doesn't get re-added several times */
575 576 577 578 579
            p_block = block_BytestreamPop( &p_sys->bytestream );
            if (pp_block)
                *pp_block = p_block;
            else if (p_block)
                block_Release(p_block);
Gildas Bazin's avatar
 
Gildas Bazin committed
580 581

            return p_out_buffer;
582
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
583
    }
584

Gildas Bazin's avatar
 
Gildas Bazin committed
585
    return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
586 587
}

588
/*****************************************************************************
589
 * Close: clean up the decoder
590
 *****************************************************************************/
591
static void Close( vlc_object_t *p_this )
592
{
Gildas Bazin's avatar
 
Gildas Bazin committed
593
    decoder_t *p_dec = (decoder_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
594 595
    decoder_sys_t *p_sys = p_dec->p_sys;

Gildas Bazin's avatar
 
Gildas Bazin committed
596
    block_BytestreamRelease( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
597

Gildas Bazin's avatar
 
Gildas Bazin committed
598
    free( p_sys );
599 600 601
}

/*****************************************************************************
Thomas Guillem's avatar
Thomas Guillem committed
602
 * Open: probe the decoder and return score
603
 *****************************************************************************/
Thomas Guillem's avatar
Thomas Guillem committed
604
static int Open( vlc_object_t *p_this )
605
{
Thomas Guillem's avatar
Thomas Guillem committed
606 607
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
608

Thomas Guillem's avatar
Thomas Guillem committed
609 610
    if(( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA ) &&
       ( p_dec->fmt_in.i_codec != VLC_CODEC_MP3 ) )
611
    {
Thomas Guillem's avatar
Thomas Guillem committed
612 613
        return VLC_EGENERIC;
    }
614

Thomas Guillem's avatar
Thomas Guillem committed
615 616 617 618
    /* 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;
619

Thomas Guillem's avatar
Thomas Guillem committed
620 621 622 623 624 625 626
    /* Misc init */
    p_sys->i_state = STATE_NOSYNC;
    date_Set( &p_sys->end_date, 0 );
    block_BytestreamInit( &p_sys->bytestream );
    p_sys->i_pts = VLC_TS_INVALID;
    p_sys->b_discontinuity = false;
    p_sys->i_frame_size = 0;
627

Thomas Guillem's avatar
Thomas Guillem committed
628 629 630
    p_sys->i_channels_conf = 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;
631

Thomas Guillem's avatar
Thomas Guillem committed
632 633 634 635
    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
    p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
    p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
636

Thomas Guillem's avatar
Thomas Guillem committed
637 638 639
    /* Set callback */
    p_dec->pf_packetize    = DecodeBlock;
    p_dec->pf_flush        = Flush;
640

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

Thomas Guillem's avatar
Thomas Guillem committed
644 645
    return VLC_SUCCESS;
}