a52.c 11.3 KB
Newer Older
1
/*****************************************************************************
2
 * a52.c: parse A/52 audio sync info and packetize the stream
3
 *****************************************************************************
Thomas Guillem's avatar
Thomas Guillem committed
4
 * Copyright (C) 2001-2016 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Stéphane Borel <stef@via.ecp.fr>
8
 *          Christophe Massiot <massiot@via.ecp.fr>
9
 *          Gildas Bazin <gbazin@videolan.org>
10
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * 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
14
 * (at your option) any later version.
15
 *
16 17
 * 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
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * 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.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33 34
#include <assert.h>

35
#include <vlc_common.h>
36
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
37 38
#include <vlc_codec.h>
#include <vlc_block_helper.h>
39
#include <vlc_modules.h>
40 41

#include "a52.h"
42

Thomas Guillem's avatar
Thomas Guillem committed
43
#include "packetizer_helper.h"
44

Thomas Guillem's avatar
Thomas Guillem committed
45 46
static int  Open( vlc_object_t * );
static void Close( vlc_object_t * );
47

48
vlc_module_begin ()
Thomas Guillem's avatar
Thomas Guillem committed
49 50
    set_category(CAT_SOUT)
    set_subcategory(SUBCAT_SOUT_PACKETIZER)
51 52
    set_description( N_("A/52 audio packetizer") )
    set_capability( "packetizer", 10 )
Thomas Guillem's avatar
Thomas Guillem committed
53
    set_callbacks( Open, Close )
54
vlc_module_end ()
55 56

struct decoder_sys_t
Christophe Massiot's avatar
Christophe Massiot committed
57 58
{
    /*
59
     * Input properties
Christophe Massiot's avatar
Christophe Massiot committed
60
     */
61
    int i_state;
62

63
    block_bytestream_t bytestream;
Christophe Massiot's avatar
Christophe Massiot committed
64 65

    /*
66
     * Common properties
Christophe Massiot's avatar
Christophe Massiot committed
67
     */
68
    date_t  end_date;
69
    mtime_t i_prev_bytestream_pts;
70
    bool    b_discontuinity;
71 72

    vlc_a52_header_t frame;
73 74
};

Thomas Guillem's avatar
Thomas Guillem committed
75 76 77
static void PacketizeFlush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
78

79
    p_sys->b_discontuinity = true;
80
    date_Set( &p_sys->end_date, VLC_TS_INVALID );
Thomas Guillem's avatar
Thomas Guillem committed
81
    p_sys->i_state = STATE_NOSYNC;
82
    p_sys->i_prev_bytestream_pts = VLC_TS_INVALID;
Thomas Guillem's avatar
Thomas Guillem committed
83 84
    block_BytestreamEmpty( &p_sys->bytestream );
}
85

Thomas Guillem's avatar
Thomas Guillem committed
86
static block_t *GetOutBuffer( decoder_t *p_dec )
87
{
Thomas Guillem's avatar
Thomas Guillem committed
88
    decoder_sys_t *p_sys = p_dec->p_sys;
89

90 91 92 93 94
    assert( p_sys->frame.i_rate > 0 );

    block_t *p_block = block_Alloc( p_sys->frame.i_size );
    if( p_block == NULL )
        return NULL;
95

96 97
    if( p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
    {
Thomas Guillem's avatar
Thomas Guillem committed
98 99
        msg_Dbg( p_dec, "A/52 channels:%d samplerate:%d bitrate:%d",
                 p_sys->frame.i_channels, p_sys->frame.i_rate, p_sys->frame.i_bitrate );
100 101 102 103 104
        if( p_sys->end_date.i_divider_num )
            date_Change( &p_sys->end_date, p_sys->frame.i_rate, 1 );
        else
            date_Init( &p_sys->end_date, p_sys->frame.i_rate, 1 );
    }
105

106 107 108 109 110
    if( p_sys->bytestream.p_block->i_pts != date_Get( &p_sys->end_date ) &&
        p_sys->bytestream.p_block->i_pts != VLC_TS_INVALID )
    {
        date_Set( &p_sys->end_date, p_sys->bytestream.p_block->i_pts );
        p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;
Thomas Guillem's avatar
Thomas Guillem committed
111
    }
112

Thomas Guillem's avatar
Thomas Guillem committed
113 114 115 116 117
    p_dec->fmt_out.audio.i_rate     = p_sys->frame.i_rate;
    p_dec->fmt_out.audio.i_channels = p_sys->frame.i_channels;
    if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->frame.i_size )
        p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->frame.i_size;
    p_dec->fmt_out.audio.i_frame_length = p_sys->frame.i_samples;
118

119 120
    p_dec->fmt_out.audio.i_chan_mode = p_sys->frame.i_chan_mode;
    p_dec->fmt_out.audio.i_physical_channels = p_sys->frame.i_channels_conf;
121

Thomas Guillem's avatar
Thomas Guillem committed
122
    p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
123

Thomas Guillem's avatar
Thomas Guillem committed
124 125
    p_block->i_nb_samples = p_sys->frame.i_samples;
    p_block->i_pts = p_block->i_dts = date_Get( &p_sys->end_date );
126 127 128 129
    if( p_block->i_pts != VLC_TS_INVALID )
        p_block->i_length = date_Increment( &p_sys->end_date,
                                            p_block->i_nb_samples ) - p_block->i_pts;

Thomas Guillem's avatar
Thomas Guillem committed
130
    return p_block;
131 132
}

Thomas Guillem's avatar
Thomas Guillem committed
133
static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
134
{
135
    decoder_sys_t *p_sys = p_dec->p_sys;
136
    uint8_t p_header[VLC_A52_HEADER_SIZE];
137
    block_t *p_out_buffer;
138

139
    block_t *p_block = pp_block ? *pp_block : NULL;
140

141
    if( p_block )
142
    {
143
        if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
144
        {
145 146 147 148 149
            /* First always drain complete blocks before discontinuity */
            block_t *p_drain = PacketizeBlock( p_dec, NULL );
            if(p_drain)
                return p_drain;

Thomas Guillem's avatar
Thomas Guillem committed
150
            PacketizeFlush( p_dec );
151 152 153 154 155 156

            if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
            {
                block_Release( p_block );
                return NULL;
            }
157
        }
158

159 160
        /* Make sure we don't reuse the same pts twice */
        if( p_block->i_pts > VLC_TS_INVALID )
161
        {
162 163 164 165
            if( p_block->i_pts == p_sys->i_prev_bytestream_pts )
                p_block->i_pts = VLC_TS_INVALID;
            else
                p_sys->i_prev_bytestream_pts = p_block->i_pts;
166
        }
167

168
        block_BytestreamPush( &p_sys->bytestream, p_block );
169 170
    }

171
    while( 1 )
172
    {
173 174 175
        switch( p_sys->i_state )
        {
        case STATE_NOSYNC:
176 177
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
                   == VLC_SUCCESS )
178
            {
179 180 181 182 183 184
                if( p_header[0] == 0x0b && p_header[1] == 0x77 )
                {
                    p_sys->i_state = STATE_SYNC;
                    break;
                }
                block_SkipByte( &p_sys->bytestream );
185
            }
186
            if( p_sys->i_state != STATE_SYNC )
187
            {
188
                block_BytestreamFlush( &p_sys->bytestream );
189

190
                /* Need more data */
191
                return NULL;
192
            }
193
            /* fallthrough */
194

195 196
        case STATE_SYNC:
            p_sys->i_state = STATE_HEADER;
197
            /* fallthrough */
198 199

        case STATE_HEADER:
200
            /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
201
            if( block_PeekBytes( &p_sys->bytestream, p_header,
202
                                 VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
203
            {
204
                /* Need more data */
205
                return NULL;
206 207
            }

208
            /* Check if frame is valid and get frame info */
209 210
            vlc_a52_header_t a52;
            if( vlc_a52_header_Parse( &a52, p_header, VLC_A52_HEADER_SIZE ) )
211
            {
212 213
                msg_Dbg( p_dec, "emulated sync word" );
                block_SkipByte( &p_sys->bytestream );
214 215 216
                p_sys->i_state = STATE_NOSYNC;
                break;
            }
217

218 219 220
            if( a52.b_eac3 && a52.eac3.strmtyp != EAC3_STRMTYP_INDEPENDENT )
            {
                /* Use the channel configuration of the independent stream */
221 222 223 224 225 226 227
                if( !p_sys->frame.i_blocks_per_sync_frame )
                {
                    /* Not synced on main stream yet */
                    block_SkipByte( &p_sys->bytestream );
                    p_sys->i_state = STATE_NOSYNC;
                    break;
                }
228 229 230 231 232 233
                p_sys->frame.i_samples = a52.i_samples;
                p_sys->frame.i_size = a52.i_size;
            }
            else
                p_sys->frame = a52;

234
            p_sys->i_state = STATE_NEXT_SYNC;
235
            /* fallthrough */
236

237 238 239
        case STATE_NEXT_SYNC:
            /* Check if next expected frame contains the sync word */
            if( block_PeekOffsetBytes( &p_sys->bytestream,
240
                                       p_sys->frame.i_size, p_header, 2 )
241 242
                != VLC_SUCCESS )
            {
243 244
                if( p_block == NULL ) /* drain */
                {
245
                    p_sys->i_state = STATE_GET_DATA;
246 247
                    break;
                }
248 249
                /* Need more data */
                return NULL;
250
            }
251

Thomas Guillem's avatar
Thomas Guillem committed
252
            if( p_header[0] == 0 && p_header[1] == 0 )
253 254 255 256 257 258
            {
                /* A52 wav files and audio CD's use stuffing */
                p_sys->i_state = STATE_GET_DATA;
                break;
            }

259
            if( p_header[0] != 0x0b || p_header[1] != 0x77 )
260
            {
261 262 263 264 265
                msg_Dbg( p_dec, "emulated sync word "
                         "(no sync on following frame)" );
                p_sys->i_state = STATE_NOSYNC;
                block_SkipByte( &p_sys->bytestream );
                break;
266
            }
267
            p_sys->i_state = STATE_GET_DATA;
268
            break;
269

270
        case STATE_GET_DATA:
271
            /* Make sure we have enough data. */
272
            if( block_WaitBytes( &p_sys->bytestream,
273
                                 p_sys->frame.i_size ) != VLC_SUCCESS )
274 275
            {
                /* Need more data */
276
                return NULL;
277
            }
278
            p_sys->i_state = STATE_SEND_DATA;
279
            /* fallthrough */
280

281
        case STATE_SEND_DATA:
Thomas Guillem's avatar
Thomas Guillem committed
282
            if( !(p_out_buffer = GetOutBuffer( p_dec )) )
283 284 285
            {
                return NULL;
            }
286

287 288
            /* Copy the whole frame into the buffer. When we reach this point
             * we already know we have enough data available. */
Thomas Guillem's avatar
Thomas Guillem committed
289 290
            block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
                            __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) );
291

292 293 294 295 296 297 298
            p_sys->i_state = STATE_NOSYNC;

            if( p_out_buffer->i_dts == VLC_TS_INVALID )
            {
                block_Release( p_out_buffer );
                return NULL;
            }
299

300 301 302 303 304 305
            if( p_sys->b_discontuinity )
            {
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
                p_sys->b_discontuinity = false;
            }

306
            /* So p_block doesn't get re-added several times */
307 308
            if( pp_block )
                *pp_block = block_BytestreamPop( &p_sys->bytestream );
309

310
            return p_out_buffer;
311
        }
312 313
    }
}
314

Thomas Guillem's avatar
Thomas Guillem committed
315
static void Close( vlc_object_t *p_this )
316
{
317
    decoder_t *p_dec = (decoder_t*)p_this;
318
    decoder_sys_t *p_sys = p_dec->p_sys;
319

320
    block_BytestreamRelease( &p_sys->bytestream );
321

322
    free( p_sys );
323
}
324

Thomas Guillem's avatar
Thomas Guillem committed
325
static int Open( vlc_object_t *p_this )
326
{
Thomas Guillem's avatar
Thomas Guillem committed
327 328
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
329

Thomas Guillem's avatar
Thomas Guillem committed
330
    switch( p_dec->fmt_in.i_codec )
331
    {
332
    case VLC_CODEC_EAC3:
Thomas Guillem's avatar
Thomas Guillem committed
333 334 335 336
    case VLC_CODEC_A52:
        break;
    default:
        return VLC_EGENERIC;
337 338
    }

Thomas Guillem's avatar
Thomas Guillem committed
339 340 341 342
    /* 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;
343

Thomas Guillem's avatar
Thomas Guillem committed
344 345
    /* Misc init */
    p_sys->i_state = STATE_NOSYNC;
346
    date_Set( &p_sys->end_date, VLC_TS_INVALID );
347
    p_sys->b_discontuinity = false;
348
    p_sys->i_prev_bytestream_pts = VLC_TS_INVALID;
349
    memset(&p_sys->frame, 0, sizeof(vlc_a52_header_t));
350

Thomas Guillem's avatar
Thomas Guillem committed
351
    block_BytestreamInit( &p_sys->bytestream );
352

353
    /* Set output properties (Passthrough ONLY) */
Thomas Guillem's avatar
Thomas Guillem committed
354 355
    p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
    p_dec->fmt_out.audio = p_dec->fmt_in.audio;
356
    p_dec->fmt_out.audio.i_rate = 0;
357

Thomas Guillem's avatar
Thomas Guillem committed
358 359 360 361
    /* Set callback */
    p_dec->pf_packetize = PacketizeBlock;
    p_dec->pf_flush     = PacketizeFlush;
    return VLC_SUCCESS;
362
}