dts.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*****************************************************************************
 * dts.c: parse DTS audio sync info and packetize the stream
 *****************************************************************************
 * Copyright (C) 2001-2016 VLC authors and VideoLAN
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *          Thomas Guillem <thomas@gllm.fr>
 *
 * 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
 * (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 Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>
#include <vlc_block_helper.h>
#include <vlc_modules.h>

#include "dts_header.h"

#include "packetizer_helper.h"

static int  Open( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin ()
    set_category( CAT_SOUT )
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
    set_description( N_("DTS audio packetizer") )
    set_capability( "packetizer", 10 )
    set_callbacks( Open, Close )
vlc_module_end ()

struct decoder_sys_t
{
    /*
     * Input properties
     */
    int i_state;

    block_bytestream_t bytestream;
60
    size_t i_next_offset;
61 62 63 64 65 66 67 68

    /*
     * Common properties
     */
    date_t  end_date;
    bool    b_date_set;

    mtime_t i_pts;
69
    bool    b_discontinuity;
70 71

    vlc_dts_header_t dts;
72
    size_t  i_input_size;
73 74 75 76 77 78
};

static void PacketizeFlush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

79
    p_sys->b_discontinuity = true;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    date_Set( &p_sys->end_date, 0 );
    p_sys->i_state = STATE_NOSYNC;
    block_BytestreamEmpty( &p_sys->bytestream );
}

static block_t *GetOutBuffer( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( !p_sys->b_date_set
     || p_dec->fmt_out.audio.i_rate != p_sys->dts.i_rate )
    {
        msg_Dbg( p_dec, "DTS samplerate:%d bitrate:%d",
                 p_sys->dts.i_rate, p_sys->dts.i_bitrate );

        date_Init( &p_sys->end_date, p_sys->dts.i_rate, 1 );
        date_Set( &p_sys->end_date, p_sys->i_pts );
        p_sys->b_date_set = true;
    }

    p_dec->fmt_out.audio.i_rate     = p_sys->dts.i_rate;
    if( p_dec->fmt_out.audio.i_bytes_per_frame < p_sys->dts.i_frame_size )
        p_dec->fmt_out.audio.i_bytes_per_frame = p_sys->dts.i_frame_size;
    p_dec->fmt_out.audio.i_frame_length = p_sys->dts.i_frame_length;

105 106
    p_dec->fmt_out.audio.i_chan_mode = p_sys->dts.i_chan_mode;
    p_dec->fmt_out.audio.i_physical_channels = p_sys->dts.i_physical_channels;
107 108 109 110 111
    p_dec->fmt_out.audio.i_channels =
        popcount( p_dec->fmt_out.audio.i_physical_channels );

    p_dec->fmt_out.i_bitrate = p_sys->dts.i_bitrate;

112
    block_t *p_block = block_Alloc( p_sys->i_input_size );
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    if( p_block == NULL )
        return NULL;

    p_block->i_nb_samples = p_sys->dts.i_frame_length;
    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;
}

static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    uint8_t p_header[VLC_DTS_HEADER_SIZE];
    block_t *p_out_buffer;

129
    block_t *p_block = pp_block ? *pp_block : NULL;
130

131
    if( p_block )
132
    {
133 134 135 136 137 138
        if ( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) {
            /* First always drain complete blocks before discontinuity */
            block_t *p_drain = PacketizeBlock( p_dec, NULL );
            if(p_drain)
                return p_drain;

139
            PacketizeFlush( p_dec );
140 141 142 143 144

            if ( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) {
                block_Release( p_block );
                return NULL;
            }
145
        }
146

147 148 149 150 151
        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;
        }
152

153
        block_BytestreamPush( &p_sys->bytestream, p_block );
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    }

    while( 1 )
    {
        switch( p_sys->i_state )
        {
        case STATE_NOSYNC:
            while( block_PeekBytes( &p_sys->bytestream, p_header, 6 )
                   == VLC_SUCCESS )
            {
                if( vlc_dts_header_IsSync( p_header, 6 ) )
                {
                    p_sys->i_state = STATE_SYNC;
                    break;
                }
                block_SkipByte( &p_sys->bytestream );
            }
            if( p_sys->i_state != STATE_SYNC )
            {
                block_BytestreamFlush( &p_sys->bytestream );

                /* Need more data */
                return NULL;
            }
178
            /* fallthrough */
179 180 181 182 183 184 185 186 187 188

        case STATE_SYNC:
            /* New frame, set the Presentation Time Stamp */
            p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
            if( p_sys->i_pts > VLC_TS_INVALID &&
                p_sys->i_pts != date_Get( &p_sys->end_date ) )
            {
                date_Set( &p_sys->end_date, p_sys->i_pts );
            }
            p_sys->i_state = STATE_HEADER;
189
            /* fallthrough */
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

        case STATE_HEADER:
            /* Get DTS frame header (VLC_DTS_HEADER_SIZE bytes) */
            if( block_PeekBytes( &p_sys->bytestream, p_header,
                                 VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
            {
                /* Need more data */
                return NULL;
            }

            /* Check if frame is valid and get frame info */
            if( vlc_dts_header_Parse( &p_sys->dts, p_header,
                                      VLC_DTS_HEADER_SIZE ) != VLC_SUCCESS )
            {
                msg_Dbg( p_dec, "emulated sync word" );
                block_SkipByte( &p_sys->bytestream );
                p_sys->i_state = STATE_NOSYNC;
                break;
            }

210 211 212 213 214 215 216 217 218 219
            if( p_sys->dts.b_substream  )
            {
                msg_Warn( p_dec, "substream without the paired core stream, "
                          "skip it" );
                p_sys->i_state = STATE_NOSYNC;
                if( block_SkipBytes( &p_sys->bytestream,
                                     p_sys->dts.i_frame_size ) != VLC_SUCCESS )
                    return NULL;
                break;
            }
220 221

            p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size;
222
            p_sys->i_state = STATE_NEXT_SYNC;
223
            /* fallthrough */
224 225 226

        case STATE_NEXT_SYNC:
            /* Check if next expected frame contains the sync word */
227
            while( p_sys->i_state == STATE_NEXT_SYNC )
228
            {
229
                if( block_PeekOffsetBytes( &p_sys->bytestream,
230 231
                                           p_sys->i_next_offset, p_header,
                                           VLC_DTS_HEADER_SIZE )
232
                                           != VLC_SUCCESS )
233
                {
234 235 236 237 238 239 240
                    if( p_block == NULL ) /* drain */
                    {
                        p_sys->i_state = STATE_GET_DATA;
                        break;
                    }
                    /* Need more data */
                    return NULL;
241
                }
242

243 244 245 246 247 248
                if( p_header[0] == 0 )
                {
                    /* DTS wav files, audio CD's and some mkvs use stuffing */
                    p_sys->i_next_offset++;
                    continue;
                }
249

250
                if( !vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
251
                {
252 253 254 255 256 257 258 259 260 261 262
                    /* Even frame size is likely incorrect FSIZE #18166 */
                    if( (p_sys->dts.i_frame_size % 2) && p_sys->i_next_offset > 0 &&
                        block_PeekOffsetBytes( &p_sys->bytestream,
                                               p_sys->i_next_offset - 1, p_header,
                                               VLC_DTS_HEADER_SIZE ) == 0 &&
                         vlc_dts_header_IsSync( p_header, VLC_DTS_HEADER_SIZE ) )
                    {
                        p_sys->i_input_size = p_sys->i_next_offset = p_sys->dts.i_frame_size - 1;
                        /* reenter */
                        break;
                    }
263 264 265 266 267 268
                    msg_Dbg( p_dec, "emulated sync word "
                             "(no sync on following frame)" );
                    p_sys->i_state = STATE_NOSYNC;
                    block_SkipByte( &p_sys->bytestream );
                    break;
                }
269 270 271 272 273 274 275 276 277 278

                /* Check if a DTS substream packet is located just after
                 * the core packet */
                if( p_sys->i_next_offset == p_sys->dts.i_frame_size )
                {
                    vlc_dts_header_t next_header;
                    if( vlc_dts_header_Parse( &next_header, p_header,
                                              VLC_DTS_HEADER_SIZE )
                        == VLC_SUCCESS && next_header.b_substream )
                    {
279
                        p_dec->fmt_out.i_profile = PROFILE_DTS_HD;
280 281 282
                        p_sys->i_input_size += next_header.i_frame_size;
                    }
                }
283
                p_sys->i_state = STATE_GET_DATA;
284 285 286 287
            }
            break;

        case STATE_GET_DATA:
288
            /* Make sure we have enough data. */
289
            if( block_WaitBytes( &p_sys->bytestream,
290
                                 p_sys->i_input_size ) != VLC_SUCCESS )
291 292 293 294 295
            {
                /* Need more data */
                return NULL;
            }
            p_sys->i_state = STATE_SEND_DATA;
296
            /* fallthrough */
297 298 299 300 301 302 303 304 305 306

        case STATE_SEND_DATA:
            if( !(p_out_buffer = GetOutBuffer( p_dec )) )
            {
                return NULL;
            }

            /* 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_out_buffer->p_buffer,
307
                            p_out_buffer->i_buffer );
308 309 310 311 312

            /* Make sure we don't reuse the same pts twice */
            if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
                p_sys->i_pts = p_sys->bytestream.p_block->i_pts = VLC_TS_INVALID;

313 314 315 316 317 318
            if( p_sys->b_discontinuity )
            {
                p_sys->b_discontinuity = false;
                p_out_buffer->i_flags |= BLOCK_FLAG_DISCONTINUITY;
            }

319
            /* So p_block doesn't get re-added several times */
320 321
            if( pp_block )
                *pp_block = block_BytestreamPop( &p_sys->bytestream );
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

            p_sys->i_state = STATE_NOSYNC;

            return p_out_buffer;
        }
    }
}

static void Close( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;

    block_BytestreamRelease( &p_sys->bytestream );

    free( p_sys );
}

static int Open( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;

Steve Lhomme's avatar
Steve Lhomme committed
345 346
    if( p_dec->fmt_in.i_codec != VLC_CODEC_DTS )
        return VLC_EGENERIC;
347 348 349 350 351 352 353 354 355 356

    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
        return VLC_ENOMEM;

    /* 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;
357
    p_sys->b_discontinuity = false;
358
    memset(&p_sys->dts, 0, sizeof(vlc_dts_header_t));
359 360
    block_BytestreamInit( &p_sys->bytestream );

361
    /* Set output properties (passthrough only) */
362 363 364 365 366 367 368 369
    p_dec->fmt_out.i_codec = p_dec->fmt_in.i_codec;
    p_dec->fmt_out.audio = p_dec->fmt_in.audio;

    /* Set callback */
    p_dec->pf_packetize = PacketizeBlock;
    p_dec->pf_flush     = PacketizeFlush;
    return VLC_SUCCESS;
}