a52.c 11.2 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
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
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
35 36
#include <vlc_codec.h>
#include <vlc_block_helper.h>
37
#include <vlc_modules.h>
38 39

#include "a52.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
40

Thomas Guillem's avatar
Thomas Guillem committed
41
#include "packetizer_helper.h"
42

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

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

struct decoder_sys_t
Christophe Massiot's avatar
Christophe Massiot committed
55 56
{
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
57
     * Input properties
Christophe Massiot's avatar
Christophe Massiot committed
58
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
59
    int i_state;
Gildas Bazin's avatar
 
Gildas Bazin committed
60

Gildas Bazin's avatar
 
Gildas Bazin committed
61
    block_bytestream_t bytestream;
Christophe Massiot's avatar
Christophe Massiot committed
62 63

    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
64
     * Common properties
Christophe Massiot's avatar
Christophe Massiot committed
65
     */
66
    date_t  end_date;
Thomas Guillem's avatar
Thomas Guillem committed
67
    bool    b_date_set;
Gildas Bazin's avatar
 
Gildas Bazin committed
68

Gildas Bazin's avatar
 
Gildas Bazin committed
69
    mtime_t i_pts;
70
    bool    b_discontuinity;
71 72

    vlc_a52_header_t frame;
Gildas Bazin's avatar
 
Gildas Bazin committed
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;
Thomas Guillem's avatar
Thomas Guillem committed
80 81 82 83
    date_Set( &p_sys->end_date, 0 );
    p_sys->i_state = STATE_NOSYNC;
    block_BytestreamEmpty( &p_sys->bytestream );
}
84

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

Thomas Guillem's avatar
Thomas Guillem committed
89 90
    if( !p_sys->b_date_set
     || p_dec->fmt_out.audio.i_rate != p_sys->frame.i_rate )
Gildas Bazin's avatar
 
Gildas Bazin committed
91
    {
Thomas Guillem's avatar
Thomas Guillem committed
92 93
        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 );
Gildas Bazin's avatar
 
Gildas Bazin committed
94

Thomas Guillem's avatar
Thomas Guillem committed
95 96 97 98
        date_Init( &p_sys->end_date, p_sys->frame.i_rate, 1 );
        date_Set( &p_sys->end_date, p_sys->i_pts );
        p_sys->b_date_set = true;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
99

Thomas Guillem's avatar
Thomas Guillem committed
100 101 102 103 104
    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;
Gildas Bazin's avatar
 
Gildas Bazin committed
105

Thomas Guillem's avatar
Thomas Guillem committed
106 107 108
    p_dec->fmt_out.audio.i_original_channels = p_sys->frame.i_channels_conf;
    p_dec->fmt_out.audio.i_physical_channels =
        p_sys->frame.i_channels_conf & AOUT_CHAN_PHYSMASK;
Gildas Bazin's avatar
 
Gildas Bazin committed
109

Thomas Guillem's avatar
Thomas Guillem committed
110
    p_dec->fmt_out.i_bitrate = p_sys->frame.i_bitrate;
Gildas Bazin's avatar
 
Gildas Bazin committed
111

Thomas Guillem's avatar
Thomas Guillem committed
112 113 114
    block_t *p_block = block_Alloc( p_sys->frame.i_size );
    if( p_block == NULL )
        return NULL;
115

Thomas Guillem's avatar
Thomas Guillem committed
116 117 118 119 120
    p_block->i_nb_samples = p_sys->frame.i_samples;
    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_block->i_nb_samples ) - p_block->i_pts;
    return p_block;
121 122
}

Thomas Guillem's avatar
Thomas Guillem committed
123
static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
124
{
Gildas Bazin's avatar
 
Gildas Bazin committed
125
    decoder_sys_t *p_sys = p_dec->p_sys;
126
    uint8_t p_header[VLC_A52_HEADER_SIZE];
127
    block_t *p_out_buffer;
Gildas Bazin's avatar
 
Gildas Bazin committed
128

129
    block_t *p_block = pp_block ? *pp_block : NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
130

131
    if( p_block )
Gildas Bazin's avatar
 
Gildas Bazin committed
132
    {
133
        if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
134
        {
135 136 137 138 139
            /* 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
140
            PacketizeFlush( p_dec );
141 142 143 144 145 146

            if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
            {
                block_Release( p_block );
                return NULL;
            }
147
        }
148

149 150 151 152 153 154
        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. */
            block_Release( p_block );
            return NULL;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
155

156
        block_BytestreamPush( &p_sys->bytestream, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
157 158
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
159
    while( 1 )
160
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
161 162 163
        switch( p_sys->i_state )
        {
        case STATE_NOSYNC:
Gildas Bazin's avatar
 
Gildas Bazin committed
164 165
            while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
                   == VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
166
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
167 168 169 170 171 172
                if( p_header[0] == 0x0b && p_header[1] == 0x77 )
                {
                    p_sys->i_state = STATE_SYNC;
                    break;
                }
                block_SkipByte( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
173
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
174
            if( p_sys->i_state != STATE_SYNC )
Gildas Bazin's avatar
 
Gildas Bazin committed
175
            {
176
                block_BytestreamFlush( &p_sys->bytestream );
177

Gildas Bazin's avatar
 
Gildas Bazin committed
178
                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
179
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
180
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
181

Gildas Bazin's avatar
 
Gildas Bazin committed
182 183
        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
Gildas Bazin's avatar
 
Gildas Bazin committed
184
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
185
            if( p_sys->i_pts > VLC_TS_INVALID &&
186
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
187
            {
188
                date_Set( &p_sys->end_date, p_sys->i_pts );
Gildas Bazin's avatar
 
Gildas Bazin committed
189 190 191 192
            }
            p_sys->i_state = STATE_HEADER;

        case STATE_HEADER:
193
            /* Get A/52 frame header (VLC_A52_HEADER_SIZE bytes) */
Gildas Bazin's avatar
 
Gildas Bazin committed
194
            if( block_PeekBytes( &p_sys->bytestream, p_header,
195
                                 VLC_A52_HEADER_SIZE ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
196
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
197
                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
198
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
199 200
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
201
            /* Check if frame is valid and get frame info */
202 203
            vlc_a52_header_t a52;
            if( vlc_a52_header_Parse( &a52, p_header, VLC_A52_HEADER_SIZE ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
204
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
205 206
                msg_Dbg( p_dec, "emulated sync word" );
                block_SkipByte( &p_sys->bytestream );
Gildas Bazin's avatar
 
Gildas Bazin committed
207 208 209
                p_sys->i_state = STATE_NOSYNC;
                break;
            }
210

211 212 213 214 215 216 217 218 219
            if( a52.b_eac3 && a52.eac3.strmtyp != EAC3_STRMTYP_INDEPENDENT )
            {
                /* Use the channel configuration of the independent stream */
                p_sys->frame.i_samples = a52.i_samples;
                p_sys->frame.i_size = a52.i_size;
            }
            else
                p_sys->frame = a52;

Gildas Bazin's avatar
 
Gildas Bazin committed
220
            p_sys->i_state = STATE_NEXT_SYNC;
Gildas Bazin's avatar
 
Gildas Bazin committed
221

Gildas Bazin's avatar
 
Gildas Bazin committed
222 223 224
        case STATE_NEXT_SYNC:
            /* Check if next expected frame contains the sync word */
            if( block_PeekOffsetBytes( &p_sys->bytestream,
225
                                       p_sys->frame.i_size, p_header, 2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
226 227
                != VLC_SUCCESS )
            {
228 229 230 231 232
                if( p_block == NULL ) /* drain */
                {
                    p_sys->i_state = STATE_SEND_DATA;
                    break;
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
233 234
                /* Need more data */
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
235
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
236

Thomas Guillem's avatar
Thomas Guillem committed
237
            if( p_header[0] == 0 && p_header[1] == 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
238 239 240 241 242 243
            {
                /* A52 wav files and audio CD's use stuffing */
                p_sys->i_state = STATE_GET_DATA;
                break;
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
244
            if( p_header[0] != 0x0b || p_header[1] != 0x77 )
Gildas Bazin's avatar
 
Gildas Bazin committed
245
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
246 247 248 249 250
                msg_Dbg( p_dec, "emulated sync word "
                         "(no sync on following frame)" );
                p_sys->i_state = STATE_NOSYNC;
                block_SkipByte( &p_sys->bytestream );
                break;
Gildas Bazin's avatar
 
Gildas Bazin committed
251
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
252 253
            p_sys->i_state = STATE_SEND_DATA;
            break;
Gildas Bazin's avatar
 
Gildas Bazin committed
254

Gildas Bazin's avatar
 
Gildas Bazin committed
255 256 257 258
        case STATE_GET_DATA:
            /* Make sure we have enough data.
             * (Not useful if we went through NEXT_SYNC) */
            if( block_WaitBytes( &p_sys->bytestream,
259
                                 p_sys->frame.i_size ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
260 261
            {
                /* Need more data */
Gildas Bazin's avatar
 
Gildas Bazin committed
262
                return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
263
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
264
            p_sys->i_state = STATE_SEND_DATA;
Gildas Bazin's avatar
 
Gildas Bazin committed
265

Gildas Bazin's avatar
 
Gildas Bazin committed
266
        case STATE_SEND_DATA:
Thomas Guillem's avatar
Thomas Guillem committed
267
            if( !(p_out_buffer = GetOutBuffer( p_dec )) )
Gildas Bazin's avatar
 
Gildas Bazin committed
268
            {
269
                //p_dec->b_error = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
270 271
                return NULL;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
272

Gildas Bazin's avatar
 
Gildas Bazin committed
273 274
            /* 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
275 276
            block_GetBytes( &p_sys->bytestream, p_out_buffer->p_buffer,
                            __MIN( p_sys->frame.i_size, p_out_buffer->i_buffer ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
277 278

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

282 283 284 285 286 287
            if( p_sys->b_discontuinity )
            {
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
                p_sys->b_discontuinity = false;
            }

Gildas Bazin's avatar
 
Gildas Bazin committed
288
            /* So p_block doesn't get re-added several times */
289 290
            if( pp_block )
                *pp_block = block_BytestreamPop( &p_sys->bytestream );
291

Gildas Bazin's avatar
 
Gildas Bazin committed
292
            p_sys->i_state = STATE_NOSYNC;
Gildas Bazin's avatar
 
Gildas Bazin committed
293

Gildas Bazin's avatar
 
Gildas Bazin committed
294
            return p_out_buffer;
295
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
296 297
    }
}
298

Thomas Guillem's avatar
Thomas Guillem committed
299
static void Close( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
300
{
Gildas Bazin's avatar
 
Gildas Bazin committed
301
    decoder_t *p_dec = (decoder_t*)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
302
    decoder_sys_t *p_sys = p_dec->p_sys;
303

Gildas Bazin's avatar
 
Gildas Bazin committed
304
    block_BytestreamRelease( &p_sys->bytestream );
305

Gildas Bazin's avatar
 
Gildas Bazin committed
306
    free( p_sys );
Gildas Bazin's avatar
 
Gildas Bazin committed
307
}
308

Thomas Guillem's avatar
Thomas Guillem committed
309
static int Open( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
310
{
Thomas Guillem's avatar
Thomas Guillem committed
311 312
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
Gildas Bazin's avatar
 
Gildas Bazin committed
313

Thomas Guillem's avatar
Thomas Guillem committed
314
    switch( p_dec->fmt_in.i_codec )
Gildas Bazin's avatar
 
Gildas Bazin committed
315
    {
316
    case VLC_CODEC_EAC3:
Thomas Guillem's avatar
Thomas Guillem committed
317 318 319 320
    case VLC_CODEC_A52:
        break;
    default:
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
321 322
    }

Thomas Guillem's avatar
Thomas Guillem committed
323 324 325 326
    /* 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;
Gildas Bazin's avatar
 
Gildas Bazin committed
327

Thomas Guillem's avatar
Thomas Guillem committed
328 329 330 331 332
    /* Misc init */
    p_sys->i_state = STATE_NOSYNC;
    date_Set( &p_sys->end_date, 0 );
    p_sys->i_pts = VLC_TS_INVALID;
    p_sys->b_date_set = false;
333
    p_sys->b_discontuinity = false;
Gildas Bazin's avatar
 
Gildas Bazin committed
334

Thomas Guillem's avatar
Thomas Guillem committed
335
    block_BytestreamInit( &p_sys->bytestream );
336

Thomas Guillem's avatar
Thomas Guillem committed
337 338 339 340
    /* Set output properties */
    p_dec->fmt_out.i_cat = AUDIO_ES;
    p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
    p_dec->fmt_out.audio = p_dec->fmt_in.audio;
Gildas Bazin's avatar
 
Gildas Bazin committed
341

342
    p_sys->frame.b_eac3 = false;
Thomas Guillem's avatar
Thomas Guillem committed
343 344 345 346 347 348
    p_sys->frame.i_rate = p_dec->fmt_out.audio.i_rate;
    p_sys->frame.i_channels = p_dec->fmt_out.audio.i_channels;
    p_sys->frame.i_size = p_dec->fmt_out.audio.i_bytes_per_frame;
    p_sys->frame.i_samples = p_dec->fmt_out.audio.i_frame_length;
    p_sys->frame.i_channels_conf = p_dec->fmt_out.audio.i_original_channels;
    p_sys->frame.i_bitrate = p_dec->fmt_out.i_bitrate;
Gildas Bazin's avatar
 
Gildas Bazin committed
349

Thomas Guillem's avatar
Thomas Guillem committed
350 351 352 353
    /* Set callback */
    p_dec->pf_packetize = PacketizeBlock;
    p_dec->pf_flush     = PacketizeFlush;
    return VLC_SUCCESS;
354
}