mpegaudio.c 21.8 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
    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) )
        {
294 295 296 297 298 299 300
            /* 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
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
            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 );
317
    }
Thomas Guillem's avatar
Thomas Guillem committed
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

    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;
347 348
            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
349
            {
350
                date_Set( &p_sys->end_date, p_sys->i_pts );
Thomas Guillem's avatar
Thomas Guillem committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364
            }
            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
365 366 367

            /* Check if frame is valid and get frame info */
            p_sys->i_frame_size = SyncInfo( i_header,
Gildas Bazin's avatar
 
Gildas Bazin committed
368
                                            &p_sys->i_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
369 370 371 372
                                            &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
373
                                            &p_sys->i_max_frame_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
374 375
                                            &p_sys->i_layer );

376 377
            p_dec->fmt_in.i_profile = p_sys->i_layer;

Gildas Bazin's avatar
 
Gildas Bazin committed
378 379
            if( p_sys->i_frame_size == -1 )
            {
Benjamin Pracht's avatar
Benjamin Pracht committed
380
                msg_Dbg( p_dec, "emulated startcode" );
Gildas Bazin's avatar
 
Gildas Bazin committed
381 382 383 384 385 386 387
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
                break;
            }

            if( p_sys->i_bit_rate == 0 )
            {
388
                /* Free bitrate, but 99% emulated startcode :( */
Gildas Bazin's avatar
 
Gildas Bazin committed
389
                if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
390
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
391
                    msg_Dbg( p_dec, "free bitrate mode");
392
                }
393 394
                /* The -1 below is to account for the frame padding */
                p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
395
            }
396

Gildas Bazin's avatar
 
Gildas Bazin committed
397
            p_sys->i_state = STATE_NEXT_SYNC;
398

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

Gildas Bazin's avatar
 
Gildas Bazin committed
414 415 416 417
            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
418 419
                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
420 421
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
422

Gildas Bazin's avatar
 
Gildas Bazin committed
423
                /* Build frame header */
424
                i_header = GetDWBE(p_header);
425

Gildas Bazin's avatar
 
Gildas Bazin committed
426 427
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
428
                                              &i_next_channels_conf,
Gildas Bazin's avatar
 
Gildas Bazin committed
429 430 431 432 433
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
434

Gildas Bazin's avatar
 
Gildas Bazin committed
435
                /* Free bitrate only */
Gildas Bazin's avatar
 
Gildas Bazin committed
436
                if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
437 438 439 440 441 442 443 444 445 446 447 448
                {
                    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
449 450 451 452 453 454 455

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
Benjamin Pracht's avatar
Benjamin Pracht committed
456
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
Gildas Bazin's avatar
 
Gildas Bazin committed
457 458 459
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
                    break;
Gildas Bazin's avatar
 
Gildas Bazin committed
460 461 462
                }

                /* Check info is in sync with previous one */
Gildas Bazin's avatar
 
Gildas Bazin committed
463
                if( i_next_channels_conf != p_sys->i_channels_conf ||
Gildas Bazin's avatar
 
Gildas Bazin committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
                    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
481 482 483 484 485 486 487 488 489 490 491

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

492 493 494
            }
            else
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
495 496 497
                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
498 499 500 501 502 503 504 505 506 507 508 509
                    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
510 511 512 513
                    p_sys->i_frame_size++;
                    break;
                }

Gildas Bazin's avatar
 
Gildas Bazin committed
514 515 516 517 518
                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
519 520
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
521 522 523 524 525 526 527 528
            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
529
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
530 531
                /* Need more data */
                return NULL;
532
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
533
            p_sys->i_state = STATE_SEND_DATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
534

Gildas Bazin's avatar
 
Gildas Bazin committed
535 536
        case STATE_SEND_DATA:
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
537
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
538
                return NULL;
539 540
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
541 542
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
543
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
544
                p_sys->i_free_frame_size = p_sys->i_frame_size;
Gildas Bazin's avatar
 
Gildas Bazin committed
545
            }
546

547 548 549 550 551 552
            /* 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;
            }
553

Gildas Bazin's avatar
 
Gildas Bazin committed
554 555 556
            p_sys->i_state = STATE_NOSYNC;

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

560 561 562 563 564 565
            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
566
            /* So p_block doesn't get re-added several times */
567 568 569 570 571
            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
572 573

            return p_out_buffer;
574
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
575
    }
576

Gildas Bazin's avatar
 
Gildas Bazin committed
577
    return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
578 579
}

580
/*****************************************************************************
581
 * Close: clean up the decoder
582
 *****************************************************************************/
583
static void Close( vlc_object_t *p_this )
584
{
Gildas Bazin's avatar
 
Gildas Bazin committed
585
    decoder_t *p_dec = (decoder_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
586 587
    decoder_sys_t *p_sys = p_dec->p_sys;

Gildas Bazin's avatar
 
Gildas Bazin committed
588
    block_BytestreamRelease( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
589

Gildas Bazin's avatar
 
Gildas Bazin committed
590
    free( p_sys );
591 592 593
}

/*****************************************************************************
Thomas Guillem's avatar
Thomas Guillem committed
594
 * Open: probe the decoder and return score
595
 *****************************************************************************/
Thomas Guillem's avatar
Thomas Guillem committed
596
static int Open( vlc_object_t *p_this )
597
{
Thomas Guillem's avatar
Thomas Guillem committed
598 599
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
600

Thomas Guillem's avatar
Thomas Guillem committed
601 602
    if(( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA ) &&
       ( p_dec->fmt_in.i_codec != VLC_CODEC_MP3 ) )
603
    {
Thomas Guillem's avatar
Thomas Guillem committed
604 605
        return VLC_EGENERIC;
    }
606

Thomas Guillem's avatar
Thomas Guillem committed
607 608 609 610
    /* 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;
611

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

Thomas Guillem's avatar
Thomas Guillem committed
620 621 622
    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;
623

Thomas Guillem's avatar
Thomas Guillem committed
624 625 626 627
    /* 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 */
628

Thomas Guillem's avatar
Thomas Guillem committed
629 630 631
    /* Set callback */
    p_dec->pf_packetize    = DecodeBlock;
    p_dec->pf_flush        = Flush;
632

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

Thomas Guillem's avatar
Thomas Guillem committed
636 637
    return VLC_SUCCESS;
}