mpeg_audio.c 24.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
 *****************************************************************************
4
 * Copyright (C) 2001-2003 the VideoLAN team
Rocky Bernstein's avatar
Rocky Bernstein committed
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 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
24
 * 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>
Gildas Bazin's avatar
 
Gildas Bazin committed
38

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

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
42
 * decoder_sys_t : decoder descriptor
43
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
44
struct decoder_sys_t
45
{
Gildas Bazin's avatar
 
Gildas Bazin committed
46
    /* Module mode */
47
    bool b_packetizer;
Gildas Bazin's avatar
 
Gildas Bazin committed
48

49
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
50
     * Input properties
51
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
52 53 54
    int        i_state;

    block_bytestream_t bytestream;
55

Gildas Bazin's avatar
 
Gildas Bazin committed
56 57 58
    /*
     * Common properties
     */
59 60
    date_t          end_date;
    unsigned int    i_current_layer;
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 73 74 75 76 77
};

enum {

    STATE_NOSYNC,
    STATE_SYNC,
    STATE_HEADER,
    STATE_NEXT_SYNC,
Gildas Bazin's avatar
 
Gildas Bazin committed
78 79
    STATE_GET_DATA,
    STATE_SEND_DATA
Gildas Bazin's avatar
 
Gildas Bazin committed
80
};
81 82 83 84 85 86 87

/* 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
88
#define MPGA_HEADER_SIZE 4
89 90 91 92

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
93 94 95 96
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 ** );
97

Gildas Bazin's avatar
 
Gildas Bazin committed
98 99 100
static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
static aout_buffer_t *GetAoutBuffer( decoder_t * );
static block_t       *GetSoutBuffer( decoder_t * );
101 102

static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
103
                     unsigned int * pi_channels_conf,
104 105
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
Gildas Bazin's avatar
 
Gildas Bazin committed
106
                     unsigned int * pi_max_frame_size,
107 108 109 110 111
                     unsigned int * pi_layer );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
112 113 114 115
vlc_module_begin ()
    set_description( N_("MPEG audio layer I/II/III decoder") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACODEC )
116
#if defined(UNDER_CE)
117
   set_capability( "decoder", 5 )
118
#else
119
    set_capability( "decoder", 100 )
120
#endif
121
    set_callbacks( OpenDecoder, CloseDecoder )
Gildas Bazin's avatar
 
Gildas Bazin committed
122

123 124 125 126 127
    add_submodule ()
    set_description( N_("MPEG audio layer I/II/III packetizer") )
    set_capability( "packetizer", 10 )
    set_callbacks( OpenPacketizer, CloseDecoder )
vlc_module_end ()
128 129

/*****************************************************************************
130
 * Open: probe the decoder and return score
131
 *****************************************************************************/
132
static int Open( vlc_object_t *p_this )
133
{
Gildas Bazin's avatar
 
Gildas Bazin committed
134
    decoder_t *p_dec = (decoder_t*)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
135
    decoder_sys_t *p_sys;
136

137
    if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA )
138 139 140 141
    {
        return VLC_EGENERIC;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
142
    /* Allocate the memory needed to store the decoder's structure */
Gildas Bazin's avatar
 
Gildas Bazin committed
143
    if( ( p_dec->p_sys = p_sys =
Gildas Bazin's avatar
 
Gildas Bazin committed
144
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
145
        return VLC_ENOMEM;
Gildas Bazin's avatar
 
Gildas Bazin committed
146 147

    /* Misc init */
148
    p_sys->b_packetizer = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
149
    p_sys->i_state = STATE_NOSYNC;
150
    date_Set( &p_sys->end_date, 0 );
151
    p_sys->bytestream = block_BytestreamInit();
152
    p_sys->b_discontinuity = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
153 154 155

    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
156
    p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
157
    p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
Gildas Bazin's avatar
 
Gildas Bazin committed
158 159 160 161 162 163 164 165 166

    /* 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
167

168 169 170
    return VLC_SUCCESS;
}

171 172 173 174 175 176 177 178 179
static int OpenDecoder( vlc_object_t *p_this )
{
    /* HACK: Don't use this codec if we don't have an mpga audio filter */
    if( !module_exists( "mpgatofixed32" ) )
        return VLC_EGENERIC;

    return Open( p_this );
}

Gildas Bazin's avatar
 
Gildas Bazin committed
180 181 182 183
static int OpenPacketizer( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;

184
    int i_ret = Open( p_this );
Gildas Bazin's avatar
 
Gildas Bazin committed
185

186
    if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
187 188 189 190 191

    return i_ret;
}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
192
 * DecodeBlock: the whole thing
Gildas Bazin's avatar
 
Gildas Bazin committed
193 194 195
 ****************************************************************************
 * This function is called just after the thread is launched.
 ****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
196
static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
Gildas Bazin's avatar
 
Gildas Bazin committed
197 198 199 200
{
    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
201 202 203 204
    uint8_t *p_buf;
    void *p_out_buffer;

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

206
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
207
    {
208 209 210
        if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
        {
            p_sys->i_state = STATE_NOSYNC;
211
            block_BytestreamEmpty( &p_sys->bytestream );
212
        }
213
        date_Set( &p_sys->end_date, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
214
        block_Release( *pp_block );
215
        p_sys->b_discontinuity = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
216
        return NULL;
217 218
    }

219
    if( !date_Get( &p_sys->end_date ) && !(*pp_block)->i_pts )
Gildas Bazin's avatar
 
Gildas Bazin committed
220
    {
221 222 223 224
        /* We've just started the stream, wait for the first PTS. */
        msg_Dbg( p_dec, "waiting for PTS" );
        block_Release( *pp_block );
        return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
225 226
    }

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

Gildas Bazin's avatar
 
Gildas Bazin committed
229
    while( 1 )
230
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
231
        switch( p_sys->i_state )
232
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
233 234 235 236

        case STATE_NOSYNC:
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
                   == VLC_SUCCESS )
237
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
238 239 240 241
                /* Look for sync word - should be 0xffe */
                if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
                {
                    p_sys->i_state = STATE_SYNC;
242
                    break;
Gildas Bazin's avatar
 
Gildas Bazin committed
243 244 245 246 247
                }
                block_SkipByte( &p_sys->bytestream );
            }
            if( p_sys->i_state != STATE_SYNC )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
248
                block_BytestreamFlush( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
249 250

                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
251
                return NULL;
252
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
253 254 255

        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
Gildas Bazin's avatar
 
Gildas Bazin committed
256 257
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
            if( p_sys->i_pts != 0 &&
258
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
259
            {
260
                date_Set( &p_sys->end_date, p_sys->i_pts );
261
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
262 263 264 265 266 267
            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 )
268
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
269
                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
270
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
271 272 273 274 275 276 277 278
            }

            /* 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
279
                                            &p_sys->i_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
280 281 282 283
                                            &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
284
                                            &p_sys->i_max_frame_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
285 286 287 288
                                            &p_sys->i_layer );

            if( p_sys->i_frame_size == -1 )
            {
Benjamin Pracht's avatar
Benjamin Pracht committed
289
                msg_Dbg( p_dec, "emulated startcode" );
Gildas Bazin's avatar
 
Gildas Bazin committed
290 291
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
292
                p_sys->b_discontinuity = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
293 294 295 296 297
                break;
            }

            if( p_sys->i_bit_rate == 0 )
            {
298
                /* Free bitrate, but 99% emulated startcode :( */
Gildas Bazin's avatar
 
Gildas Bazin committed
299
                if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
300
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
301
                    msg_Dbg( p_dec, "free bitrate mode");
302
                }
303 304
                /* The -1 below is to account for the frame padding */
                p_sys->i_frame_size = p_sys->i_free_frame_size - 1;
305
            }
306

Gildas Bazin's avatar
 
Gildas Bazin committed
307
            p_sys->i_state = STATE_NEXT_SYNC;
308

Gildas Bazin's avatar
 
Gildas Bazin committed
309 310 311
        case STATE_NEXT_SYNC:
            /* TODO: If p_block == NULL, flush the buffer without checking the
             * next sync word */
312

Gildas Bazin's avatar
 
Gildas Bazin committed
313 314 315 316 317 318
            /* 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
319
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
320
            }
321

Gildas Bazin's avatar
 
Gildas Bazin committed
322 323 324 325
            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
326 327
                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
328 329
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
330

Gildas Bazin's avatar
 
Gildas Bazin committed
331 332 333
                /* Build frame header */
                i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
                           |p_header[3];
334

Gildas Bazin's avatar
 
Gildas Bazin committed
335 336
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
337
                                              &i_next_channels_conf,
Gildas Bazin's avatar
 
Gildas Bazin committed
338 339 340 341 342
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
343

Gildas Bazin's avatar
 
Gildas Bazin committed
344
                /* Free bitrate only */
Gildas Bazin's avatar
 
Gildas Bazin committed
345
                if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
Gildas Bazin's avatar
 
Gildas Bazin committed
346 347 348 349 350 351 352 353 354 355 356 357
                {
                    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
358 359 360 361 362 363 364

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
Benjamin Pracht's avatar
Benjamin Pracht committed
365
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
Gildas Bazin's avatar
 
Gildas Bazin committed
366 367
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
368
                    p_sys->b_discontinuity = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
369
                    break;
Gildas Bazin's avatar
 
Gildas Bazin committed
370 371 372
                }

                /* Check info is in sync with previous one */
Gildas Bazin's avatar
 
Gildas Bazin committed
373
                if( i_next_channels_conf != p_sys->i_channels_conf ||
Gildas Bazin's avatar
 
Gildas Bazin committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
                    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
391 392 393 394 395 396 397 398 399 400 401

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

402 403 404
            }
            else
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
405 406 407
                /* Free bitrate only */
                if( p_sys->i_bit_rate == 0 )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
408 409 410 411 412 413 414 415 416 417 418 419
                    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
420 421 422 423
                    p_sys->i_frame_size++;
                    break;
                }

Gildas Bazin's avatar
 
Gildas Bazin committed
424 425 426 427 428
                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
429 430
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
431 432 433 434 435 436 437 438
            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
439
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
440 441
                /* Need more data */
                return NULL;
442
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
443
            p_sys->i_state = STATE_SEND_DATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
444

Gildas Bazin's avatar
 
Gildas Bazin committed
445 446
        case STATE_SEND_DATA:
            if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
447
            {
448
                //p_dec->b_error = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
449
                return NULL;
450 451
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
452 453
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
454
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
455
                p_sys->i_free_frame_size = p_sys->i_frame_size;
Gildas Bazin's avatar
 
Gildas Bazin committed
456
            }
457

Gildas Bazin's avatar
 
Gildas Bazin committed
458 459 460
            /* 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 );
461

Gildas Bazin's avatar
 
Gildas Bazin committed
462 463 464
            /* Get beginning of next frame for libmad */
            if( !p_sys->b_packetizer )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
465
                memcpy( p_buf + p_sys->i_frame_size,
Gildas Bazin's avatar
 
Gildas Bazin committed
466
                        p_header, MAD_BUFFER_GUARD );
467
            }
468

Gildas Bazin's avatar
 
Gildas Bazin committed
469 470 471
            p_sys->i_state = STATE_NOSYNC;

            /* Make sure we don't reuse the same pts twice */
Gildas Bazin's avatar
 
Gildas Bazin committed
472 473 474 475 476 477 478
            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;
479
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
480
    }
481

Gildas Bazin's avatar
 
Gildas Bazin committed
482
    return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
483 484 485 486 487
}

/*****************************************************************************
 * GetOutBuffer:
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
488
static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
Gildas Bazin's avatar
 
Gildas Bazin committed
489 490
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
491 492 493 494
    uint8_t *p_buf;

    if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
    {
Clément Stenac's avatar
Clément Stenac committed
495
        msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
496
                  p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
Rocky Bernstein's avatar
Rocky Bernstein committed
497

498 499
        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
500 501 502 503 504 505 506 507 508 509 510
    }

    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
511

512
    p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
Gildas Bazin's avatar
 
Gildas Bazin committed
513

Gildas Bazin's avatar
 
Gildas Bazin committed
514 515
    if( p_sys->b_packetizer )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
516 517 518
        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
519 520 521
    }
    else
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
522 523 524
        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
525 526
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
527
    return p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
528 529 530 531 532
}

/*****************************************************************************
 * GetAoutBuffer:
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
533
static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
534 535
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
536
    aout_buffer_t *p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
537

538
    p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
Gildas Bazin's avatar
 
Gildas Bazin committed
539
    if( p_buf == NULL ) return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
540

541
    p_buf->i_pts = date_Get( &p_sys->end_date );
542 543
    p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
                      - p_buf->i_pts;
544 545
    if( p_sys->b_discontinuity )
        p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
546
    p_sys->b_discontinuity = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
547

Gildas Bazin's avatar
 
Gildas Bazin committed
548 549
    /* Hack for libmad filter */
    p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
550

Gildas Bazin's avatar
 
Gildas Bazin committed
551
    return p_buf;
Gildas Bazin's avatar
 
Gildas Bazin committed
552 553 554 555 556
}

/*****************************************************************************
 * GetSoutBuffer:
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
557
static block_t *GetSoutBuffer( decoder_t *p_dec )
Gildas Bazin's avatar
 
Gildas Bazin committed
558 559
{
    decoder_sys_t *p_sys = p_dec->p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
560
    block_t *p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
561

Gildas Bazin's avatar
 
Gildas Bazin committed
562 563
    p_block = block_New( p_dec, p_sys->i_frame_size );
    if( p_block == NULL ) return NULL;
564

565
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
Gildas Bazin's avatar
 
Gildas Bazin committed
566

Gildas Bazin's avatar
 
Gildas Bazin committed
567
    p_block->i_length =
568
        date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
569

Gildas Bazin's avatar
 
Gildas Bazin committed
570
    return p_block;
571 572 573
}

/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
574
 * CloseDecoder: clean up the decoder
575
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
576
static void CloseDecoder( vlc_object_t *p_this )
577
{
Gildas Bazin's avatar
 
Gildas Bazin committed
578
    decoder_t *p_dec = (decoder_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
579 580
    decoder_sys_t *p_sys = p_dec->p_sys;

Gildas Bazin's avatar
 
Gildas Bazin committed
581
    block_BytestreamRelease( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
582

Gildas Bazin's avatar
 
Gildas Bazin committed
583
    free( p_sys );
584 585 586 587 588 589
}

/*****************************************************************************
 * SyncInfo: parse MPEG audio sync info
 *****************************************************************************/
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
Gildas Bazin's avatar
 
Gildas Bazin committed
590
                     unsigned int * pi_channels_conf,
591 592
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
Gildas Bazin's avatar
 
Gildas Bazin committed
593
                     unsigned int * pi_max_frame_size, unsigned int * pi_layer)
594
{
Gildas Bazin's avatar
 
Gildas Bazin committed
595
    static const int ppi_bitrate[2][3][16] =
596 597 598 599 600 601 602 603 604 605
    {
        {
            /* 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,
Laurent Aimar's avatar
Laurent Aimar committed
606
              256, 320, 0}
607 608 609 610 611 612 613 614 615 616 617
        },

        {
            /* 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,
Laurent Aimar's avatar
Laurent Aimar committed
618
              144, 160, 0}
619 620 621
        }
    };

Gildas Bazin's avatar
 
Gildas Bazin committed
622
    static const int ppi_samplerate[2][4] = /* version 1 then 2 */
623 624 625 626 627 628
    {
        { 44100, 48000, 32000, 0 },
        { 22050, 24000, 16000, 0 }
    };

    int i_version, i_mode, i_emphasis;
629
    bool b_padding, b_mpeg_2_5, b_crc;
Gildas Bazin's avatar
 
Gildas Bazin committed
630
    int i_frame_size = 0;
631
    int i_bitrate_index, i_samplerate_index;
632
    int i_max_bit_rate;
633 634 635 636

    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
637
    b_crc = !((i_header >> 16) & 0x01);
638 639 640 641 642 643 644 645 646
    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 &&
647
        i_bitrate_index < 0x0f &&
648 649 650 651 652 653 654
        i_samplerate_index != 0x03 &&
        i_emphasis != 0x02 )
    {
        switch ( i_mode )
        {
        case 0: /* stereo */
        case 1: /* joint stereo */
Gildas Bazin's avatar
 
Gildas Bazin committed
655 656
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
657 658
            break;
        case 2: /* dual-mono */
Gildas Bazin's avatar
 
Gildas Bazin committed
659 660 661
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                                | AOUT_CHAN_DUALMONO;
662 663
            break;
        case 3: /* mono */
Gildas Bazin's avatar
 
Gildas Bazin committed
664 665
            *pi_channels = 1;
            *pi_channels_conf = AOUT_CHAN_CENTER;
666 667
            break;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
668 669 670
        *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];
671 672 673 674 675 676

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

677
        switch( *pi_layer )
678
        {
679
        case 1:
Gildas Bazin's avatar
 
Gildas Bazin committed
680 681 682 683
            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;
684 685 686
            *pi_frame_length = 384;
            break;

687
        case 2:
Gildas Bazin's avatar
 
Gildas Bazin committed
688 689
            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;
690 691 692
            *pi_frame_length = 1152;
            break;

693
        case 3:
Gildas Bazin's avatar
 
Gildas Bazin committed
694 695 696
            i_frame_size = ( i_version ? 72000 : 144000 ) *
                           *pi_bit_rate / *pi_sample_rate + b_padding;
            *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
697
                                 i_max_bit_rate / *pi_sample_rate + 1;
698 699 700 701
            *pi_frame_length = i_version ? 576 : 1152;
            break;

        default:
Sam Hocevar's avatar
Sam Hocevar committed
702
            break;
703
        }
704 705 706

        /* Free bitrate mode can support higher bitrates */
        if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
707
    }
708 709 710 711
    else
    {
        return -1;
    }
Laurent Aimar's avatar
Laurent Aimar committed
712

Gildas Bazin's avatar
 
Gildas Bazin committed
713
    return i_frame_size;
714
}