mpeg_audio.c 24.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2003 VLC authors and VideoLAN
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
 *
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>
zorglub's avatar
zorglub committed
36 37
#include <vlc_codec.h>
#include <vlc_aout.h>
38
#include <vlc_modules.h>
39
#include <assert.h>
gbazin's avatar
 
gbazin committed
40

zorglub's avatar
zorglub committed
41
#include <vlc_block_helper.h>
42

43 44
#include "../packetizer/packetizer_helper.h"

45
/*****************************************************************************
gbazin's avatar
 
gbazin committed
46
 * decoder_sys_t : decoder descriptor
47
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
48
struct decoder_sys_t
49
{
50
#ifdef HAVE_MPGA_FILTER
gbazin's avatar
 
gbazin committed
51
    /* Module mode */
52
    bool b_packetizer;
53
#endif
54
    /*
gbazin's avatar
 
gbazin committed
55
     * Input properties
56
     */
gbazin's avatar
 
gbazin committed
57 58 59
    int        i_state;

    block_bytestream_t bytestream;
60

gbazin's avatar
 
gbazin committed
61 62 63
    /*
     * Common properties
     */
64
    date_t          end_date;
gbazin's avatar
 
gbazin committed
65

gbazin's avatar
 
gbazin committed
66
    mtime_t i_pts;
gbazin's avatar
 
gbazin committed
67 68 69 70 71

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

73
    bool   b_discontinuity;
gbazin's avatar
 
gbazin committed
74 75
};

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

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
87 88

#ifdef HAVE_MPGA_FILTER
gbazin's avatar
 
gbazin committed
89
static int  OpenDecoder   ( vlc_object_t * );
90 91 92
static block_t *GetAoutBuffer( decoder_t * );
#endif
static int  Open( vlc_object_t * );
93
static block_t *DecodeBlock  ( decoder_t *, block_t ** );
94 95
static uint8_t *GetOutBuffer ( decoder_t *, block_t ** );
static block_t *GetSoutBuffer( decoder_t * );
96
static void Close(  vlc_object_t * );
97
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
gbazin's avatar
 
gbazin committed
98
                     unsigned int * pi_channels_conf,
99 100
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
gbazin's avatar
 
gbazin committed
101
                     unsigned int * pi_max_frame_size,
102 103 104 105 106
                     unsigned int * pi_layer );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
107 108 109 110 111
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 )
112 113 114 115 116 117 118
    set_callbacks( Open, Close )
#ifdef HAVE_MPGA_FILTER
    add_submodule ()
    set_description( N_("MPEG audio layer I/II/III decoder") )
    set_capability( "decoder", 100 )
    set_callbacks( OpenDecoder, Close )
#endif
119
vlc_module_end ()
120 121

/*****************************************************************************
122
 * Open: probe the decoder and return score
123
 *****************************************************************************/
124
static int Open( vlc_object_t *p_this )
125
{
gbazin's avatar
 
gbazin committed
126
    decoder_t *p_dec = (decoder_t*)p_this;
gbazin's avatar
 
gbazin committed
127
    decoder_sys_t *p_sys;
128

129 130
    if(( p_dec->fmt_in.i_codec != VLC_CODEC_MPGA ) &&
       ( p_dec->fmt_in.i_codec != VLC_CODEC_MP3 ) )
131 132 133 134
    {
        return VLC_EGENERIC;
    }

gbazin's avatar
 
gbazin committed
135
    /* Allocate the memory needed to store the decoder's structure */
gbazin's avatar
 
gbazin committed
136
    if( ( p_dec->p_sys = p_sys =
gbazin's avatar
 
gbazin committed
137
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
138
        return VLC_ENOMEM;
gbazin's avatar
 
gbazin committed
139 140

    /* Misc init */
141 142 143
#ifdef HAVE_MPGA_FILTER
    p_sys->b_packetizer = true;
#endif
gbazin's avatar
 
gbazin committed
144
    p_sys->i_state = STATE_NOSYNC;
145
    date_Set( &p_sys->end_date, 0 );
146
    block_BytestreamInit( &p_sys->bytestream );
147
    p_sys->i_pts = VLC_TS_INVALID;
148
    p_sys->b_discontinuity = false;
149 150 151 152 153
    p_sys->i_frame_size = 0;

    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;
gbazin's avatar
 
gbazin committed
154 155 156

    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
157
    p_dec->fmt_out.i_codec = VLC_CODEC_MPGA;
158
    p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
gbazin's avatar
 
gbazin committed
159 160

    /* Set callback */
161 162
    p_dec->pf_decode_audio = DecodeBlock;
    p_dec->pf_packetize    = DecodeBlock;
gbazin's avatar
 
gbazin committed
163 164 165

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

167 168 169
    return VLC_SUCCESS;
}

170
#ifdef HAVE_MPGA_FILTER
171 172
static int OpenDecoder( vlc_object_t *p_this )
{
173 174
    decoder_t *p_dec = (decoder_t *)p_this;

175
    /* HACK: Don't use this codec if we don't have an mpga audio filter */
176
    if( !module_exists( "mad" ) )
177 178 179
        return VLC_EGENERIC;

    int i_ret = Open( p_this );
180 181
    if( i_ret != VLC_SUCCESS )
        return i_ret;
gbazin's avatar
 
gbazin committed
182

183 184
    p_dec->p_sys->b_packetizer = false;
    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
185
}
186
#endif
gbazin's avatar
 
gbazin committed
187 188

/****************************************************************************
gbazin's avatar
 
gbazin committed
189
 * DecodeBlock: the whole thing
gbazin's avatar
 
gbazin committed
190 191 192
 ****************************************************************************
 * This function is called just after the thread is launched.
 ****************************************************************************/
193
static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
gbazin's avatar
 
gbazin committed
194 195 196 197
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t p_header[MAD_BUFFER_GUARD];
    uint32_t i_header;
gbazin's avatar
 
gbazin committed
198
    uint8_t *p_buf;
199
    block_t *p_out_buffer;
gbazin's avatar
 
gbazin committed
200

201
    block_t *p_block = pp_block ? *pp_block : NULL;
202

203
    if (p_block) {
204
        if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
205
        {
206 207 208 209 210
            if( p_block->i_flags&BLOCK_FLAG_CORRUPTED )
            {
                p_sys->i_state = STATE_NOSYNC;
                block_BytestreamEmpty( &p_sys->bytestream );
            }
211 212
            date_Set( &p_sys->end_date, 0 );
            block_Release( p_block );
213
            p_sys->b_discontinuity = true;
214
            return NULL;
215
        }
216

217 218 219 220 221 222 223
        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;
        }
gbazin's avatar
 
gbazin committed
224

225
        block_BytestreamPush( &p_sys->bytestream, p_block );
226
    } else if (p_sys->i_frame_size)
227
        p_sys->i_state = STATE_SEND_DATA; /* return all the data we have left */
228 229
    else
        return NULL;
230

gbazin's avatar
 
gbazin committed
231
    while( 1 )
232
    {
gbazin's avatar
 
gbazin committed
233
        switch( p_sys->i_state )
234
        {
gbazin's avatar
 
gbazin committed
235 236 237 238

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

                /* Need more data */
gbazin's avatar
 
gbazin committed
253
                return NULL;
254
            }
gbazin's avatar
 
gbazin committed
255 256 257

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

            /* Build frame header */
276
            i_header = GetDWBE(p_header);
gbazin's avatar
 
gbazin committed
277 278 279

            /* Check if frame is valid and get frame info */
            p_sys->i_frame_size = SyncInfo( i_header,
gbazin's avatar
 
gbazin committed
280
                                            &p_sys->i_channels,
gbazin's avatar
 
gbazin committed
281 282 283 284
                                            &p_sys->i_channels_conf,
                                            &p_sys->i_rate,
                                            &p_sys->i_bit_rate,
                                            &p_sys->i_frame_length,
gbazin's avatar
 
gbazin committed
285
                                            &p_sys->i_max_frame_size,
gbazin's avatar
 
gbazin committed
286 287
                                            &p_sys->i_layer );

288 289
            p_dec->fmt_in.i_profile = p_sys->i_layer;

gbazin's avatar
 
gbazin committed
290 291
            if( p_sys->i_frame_size == -1 )
            {
bigben's avatar
bigben committed
292
                msg_Dbg( p_dec, "emulated startcode" );
gbazin's avatar
 
gbazin committed
293 294
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
295
                p_sys->b_discontinuity = true;
gbazin's avatar
 
gbazin committed
296 297 298 299 300
                break;
            }

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

gbazin's avatar
 
gbazin committed
310
            p_sys->i_state = STATE_NEXT_SYNC;
311

gbazin's avatar
 
gbazin committed
312 313 314
        case STATE_NEXT_SYNC:
            /* TODO: If p_block == NULL, flush the buffer without checking the
             * next sync word */
315

gbazin's avatar
 
gbazin committed
316 317 318 319 320 321
            /* 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 */
gbazin's avatar
 
gbazin committed
322
                return NULL;
gbazin's avatar
 
gbazin committed
323
            }
324

gbazin's avatar
 
gbazin committed
325 326 327 328
            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;
gbazin's avatar
 
gbazin committed
329 330
                unsigned int i_next_channels, i_next_channels_conf;
                unsigned int i_next_rate, i_next_bit_rate;
gbazin's avatar
 
gbazin committed
331 332
                unsigned int i_next_frame_length, i_next_max_frame_size;
                unsigned int i_next_layer;
333

gbazin's avatar
 
gbazin committed
334
                /* Build frame header */
335
                i_header = GetDWBE(p_header);
336

gbazin's avatar
 
gbazin committed
337 338
                i_next_frame_size = SyncInfo( i_header,
                                              &i_next_channels,
gbazin's avatar
 
gbazin committed
339
                                              &i_next_channels_conf,
gbazin's avatar
 
gbazin committed
340 341 342 343 344
                                              &i_next_rate,
                                              &i_next_bit_rate,
                                              &i_next_frame_length,
                                              &i_next_max_frame_size,
                                              &i_next_layer );
345

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

                    p_sys->i_frame_size++;
                    break;
                }

                if( i_next_frame_size == -1 )
                {
bigben's avatar
bigben committed
367
                    msg_Dbg( p_dec, "emulated startcode on next frame" );
gbazin's avatar
 
gbazin committed
368 369
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
370
                    p_sys->b_discontinuity = true;
gbazin's avatar
 
gbazin committed
371
                    break;
gbazin's avatar
 
gbazin committed
372 373 374
                }

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

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

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

gbazin's avatar
 
gbazin committed
422 423 424 425
                    p_sys->i_frame_size++;
                    break;
                }

gbazin's avatar
 
gbazin committed
426 427 428 429 430
                msg_Dbg( p_dec, "emulated startcode "
                         "(no startcode on following frame)" );
                p_sys->i_state = STATE_NOSYNC;
                block_SkipByte( &p_sys->bytestream );
                break;
gbazin's avatar
 
gbazin committed
431 432
            }

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

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

gbazin's avatar
 
gbazin committed
454 455
            /* Free bitrate only */
            if( p_sys->i_bit_rate == 0 )
456
            {
gbazin's avatar
 
gbazin committed
457
                p_sys->i_free_frame_size = p_sys->i_frame_size;
gbazin's avatar
 
gbazin committed
458
            }
459

460 461 462 463 464 465
            /* 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;
            }
466

467
#ifdef HAVE_MPGA_FILTER
gbazin's avatar
 
gbazin committed
468 469 470
            /* Get beginning of next frame for libmad */
            if( !p_sys->b_packetizer )
            {
471
                assert( p_out_buffer->i_buffer >= (unsigned)p_sys->i_frame_size + MAD_BUFFER_GUARD );
gbazin's avatar
 
gbazin committed
472
                memcpy( p_buf + p_sys->i_frame_size,
gbazin's avatar
 
gbazin committed
473
                        p_header, MAD_BUFFER_GUARD );
474
            }
475
#endif
gbazin's avatar
 
gbazin committed
476 477 478
            p_sys->i_state = STATE_NOSYNC;

            /* Make sure we don't reuse the same pts twice */
gbazin's avatar
 
gbazin committed
479
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
480
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
gbazin's avatar
 
gbazin committed
481 482

            /* So p_block doesn't get re-added several times */
483 484 485 486 487
            p_block = block_BytestreamPop( &p_sys->bytestream );
            if (pp_block)
                *pp_block = p_block;
            else if (p_block)
                block_Release(p_block);
gbazin's avatar
 
gbazin committed
488 489

            return p_out_buffer;
490
        }
gbazin's avatar
 
gbazin committed
491
    }
492

gbazin's avatar
 
gbazin committed
493
    return NULL;
gbazin's avatar
 
gbazin committed
494 495 496 497 498
}

/*****************************************************************************
 * GetOutBuffer:
 *****************************************************************************/
499
static uint8_t *GetOutBuffer( decoder_t *p_dec, block_t **pp_out_buffer )
gbazin's avatar
 
gbazin committed
500 501
{
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
 
gbazin committed
502 503 504 505
    uint8_t *p_buf;

    if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
    {
zorglub's avatar
zorglub committed
506
        msg_Dbg( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
507
                  p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
Rocky Bernstein's avatar
Rocky Bernstein committed
508

509 510
        date_Init( &p_sys->end_date, p_sys->i_rate, 1 );
        date_Set( &p_sys->end_date, p_sys->i_pts );
gbazin's avatar
 
gbazin committed
511 512 513 514 515 516 517 518 519 520 521
    }

    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;
gbazin's avatar
 
gbazin committed
522

523
    p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
gbazin's avatar
 
gbazin committed
524

525 526
#ifdef HAVE_MPGA_FILTER
    if( !p_sys->b_packetizer )
gbazin's avatar
 
gbazin committed
527
    {
528
        block_t *p_aout_buffer = GetAoutBuffer( p_dec );
gbazin's avatar
 
gbazin committed
529 530
        p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
        *pp_out_buffer = p_aout_buffer;
gbazin's avatar
 
gbazin committed
531
    }
532 533 534 535 536 537 538
    else
#endif
    {
        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;
    }
gbazin's avatar
 
gbazin committed
539
    return p_buf;
gbazin's avatar
 
gbazin committed
540 541
}

542
#ifdef HAVE_MPGA_FILTER
gbazin's avatar
 
gbazin committed
543 544 545
/*****************************************************************************
 * GetAoutBuffer:
 *****************************************************************************/
546
static block_t *GetAoutBuffer( decoder_t *p_dec )
gbazin's avatar
 
gbazin committed
547 548
{
    decoder_sys_t *p_sys = p_dec->p_sys;
549
    block_t *p_buf;
gbazin's avatar
 
gbazin committed
550

551
    p_buf = decoder_NewAudioBuffer( p_dec, p_sys->i_frame_length );
gbazin's avatar
 
gbazin committed
552
    if( p_buf == NULL ) return NULL;
gbazin's avatar
 
gbazin committed
553

554
    p_buf->i_pts = date_Get( &p_sys->end_date );
555 556
    p_buf->i_length = date_Increment( &p_sys->end_date, p_sys->i_frame_length )
                      - p_buf->i_pts;
557 558
    if( p_sys->b_discontinuity )
        p_buf->i_flags |= BLOCK_FLAG_DISCONTINUITY;
559
    p_sys->b_discontinuity = false;
gbazin's avatar
 
gbazin committed
560

gbazin's avatar
 
gbazin committed
561
    /* Hack for libmad filter */
562
    p_buf = block_Realloc( p_buf, 0, p_sys->i_frame_size + MAD_BUFFER_GUARD );
563

gbazin's avatar
 
gbazin committed
564
    return p_buf;
gbazin's avatar
 
gbazin committed
565
}
566
#endif
gbazin's avatar
 
gbazin committed
567 568 569 570

/*****************************************************************************
 * GetSoutBuffer:
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
571
static block_t *GetSoutBuffer( decoder_t *p_dec )
gbazin's avatar
 
gbazin committed
572 573
{
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
 
gbazin committed
574
    block_t *p_block;
gbazin's avatar
 
gbazin committed
575

576
    p_block = block_Alloc( p_sys->i_frame_size );
gbazin's avatar
 
gbazin committed
577
    if( p_block == NULL ) return NULL;
578

579
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
gbazin's avatar
 
gbazin committed
580

gbazin's avatar
 
gbazin committed
581
    p_block->i_length =
582
        date_Increment( &p_sys->end_date, p_sys->i_frame_length ) - p_block->i_pts;
gbazin's avatar
 
gbazin committed
583

gbazin's avatar
 
gbazin committed
584
    return p_block;
585 586 587
}

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

gbazin's avatar
 
gbazin committed
595
    block_BytestreamRelease( &p_sys->bytestream );
gbazin's avatar
 
gbazin committed
596

gbazin's avatar
 
gbazin committed
597
    free( p_sys );
598 599 600 601 602 603
}

/*****************************************************************************
 * SyncInfo: parse MPEG audio sync info
 *****************************************************************************/
static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
gbazin's avatar
 
gbazin committed
604
                     unsigned int * pi_channels_conf,
605 606
                     unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
                     unsigned int * pi_frame_length,
gbazin's avatar
 
gbazin committed
607
                     unsigned int * pi_max_frame_size, unsigned int * pi_layer)
608
{
gbazin's avatar
 
gbazin committed
609
    static const int ppi_bitrate[2][3][16] =
610 611 612 613 614 615 616 617 618 619
    {
        {
            /* 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
620
              256, 320, 0}
621 622 623 624 625 626 627 628 629 630 631
        },

        {
            /* 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
632
              144, 160, 0}
633 634 635
        }
    };

gbazin's avatar
 
gbazin committed
636
    static const int ppi_samplerate[2][4] = /* version 1 then 2 */
637 638 639 640 641 642
    {
        { 44100, 48000, 32000, 0 },
        { 22050, 24000, 16000, 0 }
    };

    int i_version, i_mode, i_emphasis;
643
    bool b_padding, b_mpeg_2_5;
gbazin's avatar
 
gbazin committed
644
    int i_frame_size = 0;
645
    int i_bitrate_index, i_samplerate_index;
646
    int i_max_bit_rate;
647 648 649 650

    b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
    i_version   = 1 - ((i_header & 0x80000) >> 19);
    *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
651
    //bool b_crc = !((i_header >> 16) & 0x01);
652 653 654 655 656 657 658 659 660
    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 &&
661
        i_bitrate_index < 0x0f &&
662 663 664 665 666 667 668
        i_samplerate_index != 0x03 &&
        i_emphasis != 0x02 )
    {
        switch ( i_mode )
        {
        case 0: /* stereo */
        case 1: /* joint stereo */
gbazin's avatar
 
gbazin committed
669 670
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
671 672
            break;
        case 2: /* dual-mono */
gbazin's avatar
 
gbazin committed
673 674 675
            *pi_channels = 2;
            *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                                | AOUT_CHAN_DUALMONO;
676 677
            break;
        case 3: /* mono */
gbazin's avatar
 
gbazin committed
678 679
            *pi_channels = 1;
            *pi_channels_conf = AOUT_CHAN_CENTER;
680 681
            break;
        }
gbazin's avatar
 
gbazin committed
682 683 684
        *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];
685 686 687 688 689 690

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

691
        switch( *pi_layer )
692
        {
693
        case 1:
gbazin's avatar
 
gbazin committed
694 695 696 697
            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;
698 699 700
            *pi_frame_length = 384;
            break;

701
        case 2:
gbazin's avatar
 
gbazin committed
702 703
            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;
704 705 706
            *pi_frame_length = 1152;
            break;

707
        case 3:
gbazin's avatar
 
gbazin committed
708 709 710
            i_frame_size = ( i_version ? 72000 : 144000 ) *
                           *pi_bit_rate / *pi_sample_rate + b_padding;
            *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
711
                                 i_max_bit_rate / *pi_sample_rate + 1;
712 713 714 715
            *pi_frame_length = i_version ? 576 : 1152;
            break;

        default:
Sam Hocevar's avatar
Sam Hocevar committed
716
            break;
717
        }
718 719 720

        /* Free bitrate mode can support higher bitrates */
        if( !*pi_bit_rate ) *pi_max_frame_size *= 2;
721
    }
722 723 724 725
    else
    {
        return -1;
    }
Laurent Aimar's avatar
Laurent Aimar committed
726

gbazin's avatar
 
gbazin committed
727
    return i_frame_size;
728
}