dca_exss.c 16 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 105 106 107 108 109 110 111 112 113
/*
 * Copyright (C) 2016 foo86
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg 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.
 *
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "dcadec.h"

static void parse_xll_parameters(DCAExssParser *s, DCAExssAsset *asset)
{
    // Size of XLL data in extension substream
    asset->xll_size = get_bits(&s->gb, s->exss_size_nbits) + 1;

    // XLL sync word present flag
    if (asset->xll_sync_present = get_bits1(&s->gb)) {
        int xll_delay_nbits;

        // Peak bit rate smoothing buffer size
        skip_bits(&s->gb, 4);

        // Number of bits for XLL decoding delay
        xll_delay_nbits = get_bits(&s->gb, 5) + 1;

        // Initial XLL decoding delay in frames
        asset->xll_delay_nframes = get_bits_long(&s->gb, xll_delay_nbits);

        // Number of bytes offset to XLL sync
        asset->xll_sync_offset = get_bits(&s->gb, s->exss_size_nbits);
    } else {
        asset->xll_delay_nframes = 0;
        asset->xll_sync_offset = 0;
    }
}

static void parse_lbr_parameters(DCAExssParser *s, DCAExssAsset *asset)
{
    // Size of LBR component in extension substream
    asset->lbr_size = get_bits(&s->gb, 14) + 1;

    // LBR sync word present flag
    if (get_bits1(&s->gb))
        // LBR sync distance
        skip_bits(&s->gb, 2);
}

static int parse_descriptor(DCAExssParser *s, DCAExssAsset *asset)
{
    int i, j, drc_present, descr_size, descr_pos = get_bits_count(&s->gb);

    // Size of audio asset descriptor in bytes
    descr_size = get_bits(&s->gb, 9) + 1;

    // Audio asset identifier
    asset->asset_index = get_bits(&s->gb, 3);

    //
    // Per stream static metadata
    //

    if (s->static_fields_present) {
        // Asset type descriptor presence
        if (get_bits1(&s->gb))
            // Asset type descriptor
            skip_bits(&s->gb, 4);

        // Language descriptor presence
        if (get_bits1(&s->gb))
            // Language descriptor
            skip_bits(&s->gb, 24);

        // Additional textual information presence
        if (get_bits1(&s->gb)) {
            // Byte size of additional text info
            int text_size = get_bits(&s->gb, 10) + 1;

            // Sanity check available size
            if (get_bits_left(&s->gb) < text_size * 8)
                return AVERROR_INVALIDDATA;

            // Additional textual information string
            skip_bits_long(&s->gb, text_size * 8);
        }

        // PCM bit resolution
        asset->pcm_bit_res = get_bits(&s->gb, 5) + 1;

        // Maximum sample rate
        asset->max_sample_rate = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];

        // Total number of channels
        asset->nchannels_total = get_bits(&s->gb, 8) + 1;

        // One to one map channel to speakers
        if (asset->one_to_one_map_ch_to_spkr = get_bits1(&s->gb)) {
            int spkr_mask_nbits = 0;
            int spkr_remap_nsets;
            int nspeakers[8];

            // Embedded stereo flag
114
            asset->embedded_stereo = asset->nchannels_total > 2 && get_bits1(&s->gb);
115 116

            // Embedded 6 channels flag
117
            asset->embedded_6ch = asset->nchannels_total > 6 && get_bits1(&s->gb);
118 119 120 121 122 123 124 125 126 127 128 129

            // Speaker mask enabled flag
            if (asset->spkr_mask_enabled = get_bits1(&s->gb)) {
                // Number of bits for speaker activity mask
                spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;

                // Loudspeaker activity mask
                asset->spkr_mask = get_bits(&s->gb, spkr_mask_nbits);
            }

            // Number of speaker remapping sets
            if ((spkr_remap_nsets = get_bits(&s->gb, 3)) && !spkr_mask_nbits) {
130 131
                if (s->avctx)
                    av_log(s->avctx, AV_LOG_ERROR, "Speaker mask disabled yet there are remapping sets\n");
132 133 134 135 136
                return AVERROR_INVALIDDATA;
            }

            // Standard loudspeaker layout mask
            for (i = 0; i < spkr_remap_nsets; i++)
137
                nspeakers[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

            for (i = 0; i < spkr_remap_nsets; i++) {
                // Number of channels to be decoded for speaker remapping
                int nch_for_remaps = get_bits(&s->gb, 5) + 1;

                for (j = 0; j < nspeakers[i]; j++) {
                    // Decoded channels to output speaker mapping mask
                    int remap_ch_mask = get_bits_long(&s->gb, nch_for_remaps);

                    // Loudspeaker remapping codes
                    skip_bits_long(&s->gb, av_popcount(remap_ch_mask) * 5);
                }
            }
        } else {
            asset->embedded_stereo = 0;
            asset->embedded_6ch = 0;
            asset->spkr_mask_enabled = 0;
            asset->spkr_mask = 0;

            // Representation type
            asset->representation_type = get_bits(&s->gb, 3);
        }
    }

    //
    // DRC, DNC and mixing metadata
    //

    // Dynamic range coefficient presence flag
    drc_present = get_bits1(&s->gb);

    // Code for dynamic range coefficient
    if (drc_present)
        skip_bits(&s->gb, 8);

    // Dialog normalization presence flag
    if (get_bits1(&s->gb))
        // Dialog normalization code
        skip_bits(&s->gb, 5);

    // DRC for stereo downmix
    if (drc_present && asset->embedded_stereo)
        skip_bits(&s->gb, 8);

    // Mixing metadata presence flag
    if (s->mix_metadata_enabled && get_bits1(&s->gb)) {
        int nchannels_dmix;

        // External mixing flag
        skip_bits1(&s->gb);

        // Post mixing / replacement gain adjustment
        skip_bits(&s->gb, 6);

        // DRC prior to mixing
        if (get_bits(&s->gb, 2) == 3)
            // Custom code for mixing DRC
            skip_bits(&s->gb, 8);
        else
            // Limit for mixing DRC
            skip_bits(&s->gb, 3);

        // Scaling type for channels of main audio
        // Scaling parameters of main audio
        if (get_bits1(&s->gb))
            for (i = 0; i < s->nmixoutconfigs; i++)
                skip_bits_long(&s->gb, 6 * s->nmixoutchs[i]);
        else
            skip_bits_long(&s->gb, 6 * s->nmixoutconfigs);

        nchannels_dmix = asset->nchannels_total;
        if (asset->embedded_6ch)
            nchannels_dmix += 6;
        if (asset->embedded_stereo)
            nchannels_dmix += 2;

        for (i = 0; i < s->nmixoutconfigs; i++) {
            if (!s->nmixoutchs[i]) {
216 217
                if (s->avctx)
                    av_log(s->avctx, AV_LOG_ERROR, "Invalid speaker layout mask for mixing configuration\n");
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
                return AVERROR_INVALIDDATA;
            }
            for (j = 0; j < nchannels_dmix; j++) {
                // Mix output mask
                int mix_map_mask = get_bits(&s->gb, s->nmixoutchs[i]);

                // Mixing coefficients
                skip_bits_long(&s->gb, av_popcount(mix_map_mask) * 6);
            }
        }
    }

    //
    // Decoder navigation data
    //

    // Coding mode for the asset
    asset->coding_mode = get_bits(&s->gb, 2);

    // Coding components used in asset
    switch (asset->coding_mode) {
    case 0: // Coding mode that may contain multiple coding components
        asset->extension_mask = get_bits(&s->gb, 12);

        if (asset->extension_mask & DCA_EXSS_CORE) {
            // Size of core component in extension substream
            asset->core_size = get_bits(&s->gb, 14) + 1;
            // Core sync word present flag
            if (get_bits1(&s->gb))
                // Core sync distance
                skip_bits(&s->gb, 2);
        }

        if (asset->extension_mask & DCA_EXSS_XBR)
            // Size of XBR extension in extension substream
            asset->xbr_size = get_bits(&s->gb, 14) + 1;

        if (asset->extension_mask & DCA_EXSS_XXCH)
            // Size of XXCH extension in extension substream
            asset->xxch_size = get_bits(&s->gb, 14) + 1;

        if (asset->extension_mask & DCA_EXSS_X96)
            // Size of X96 extension in extension substream
            asset->x96_size = get_bits(&s->gb, 12) + 1;

        if (asset->extension_mask & DCA_EXSS_LBR)
            parse_lbr_parameters(s, asset);

        if (asset->extension_mask & DCA_EXSS_XLL)
            parse_xll_parameters(s, asset);

        if (asset->extension_mask & DCA_EXSS_RSV1)
            skip_bits(&s->gb, 16);

        if (asset->extension_mask & DCA_EXSS_RSV2)
            skip_bits(&s->gb, 16);
        break;

    case 1: // Loss-less coding mode without CBR component
        asset->extension_mask = DCA_EXSS_XLL;
        parse_xll_parameters(s, asset);
        break;

    case 2: // Low bit rate mode
        asset->extension_mask = DCA_EXSS_LBR;
        parse_lbr_parameters(s, asset);
        break;

    case 3: // Auxiliary coding mode
        asset->extension_mask = 0;

        // Size of auxiliary coded data
        skip_bits(&s->gb, 14);

        // Auxiliary codec identification
        skip_bits(&s->gb, 8);

        // Aux sync word present flag
        if (get_bits1(&s->gb))
            // Aux sync distance
            skip_bits(&s->gb, 3);
        break;
    }

    if (asset->extension_mask & DCA_EXSS_XLL)
        // DTS-HD stream ID
        asset->hd_stream_id = get_bits(&s->gb, 3);

    // One to one mixing flag
    // Per channel main audio scaling flag
    // Main audio scaling codes
    // Decode asset in secondary decoder flag
    // Revision 2 DRC metadata
    // Reserved
    // Zero pad
    if (ff_dca_seek_bits(&s->gb, descr_pos + descr_size * 8)) {
314 315
        if (s->avctx)
            av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS asset descriptor\n");
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
        return AVERROR_INVALIDDATA;
    }

    return 0;
}

static int set_exss_offsets(DCAExssAsset *asset)
{
    int offs = asset->asset_offset;
    int size = asset->asset_size;

    if (asset->extension_mask & DCA_EXSS_CORE) {
        asset->core_offset = offs;
        if (asset->core_size > size)
            return AVERROR_INVALIDDATA;
        offs += asset->core_size;
        size -= asset->core_size;
    }

    if (asset->extension_mask & DCA_EXSS_XBR) {
        asset->xbr_offset = offs;
        if (asset->xbr_size > size)
            return AVERROR_INVALIDDATA;
        offs += asset->xbr_size;
        size -= asset->xbr_size;
    }

    if (asset->extension_mask & DCA_EXSS_XXCH) {
        asset->xxch_offset = offs;
        if (asset->xxch_size > size)
            return AVERROR_INVALIDDATA;
        offs += asset->xxch_size;
        size -= asset->xxch_size;
    }

    if (asset->extension_mask & DCA_EXSS_X96) {
        asset->x96_offset = offs;
        if (asset->x96_size > size)
            return AVERROR_INVALIDDATA;
        offs += asset->x96_size;
        size -= asset->x96_size;
    }

    if (asset->extension_mask & DCA_EXSS_LBR) {
        asset->lbr_offset = offs;
        if (asset->lbr_size > size)
            return AVERROR_INVALIDDATA;
        offs += asset->lbr_size;
        size -= asset->lbr_size;
    }

    if (asset->extension_mask & DCA_EXSS_XLL) {
        asset->xll_offset = offs;
        if (asset->xll_size > size)
            return AVERROR_INVALIDDATA;
        offs += asset->xll_size;
        size -= asset->xll_size;
    }

    return 0;
}

378
int ff_dca_exss_parse(DCAExssParser *s, const uint8_t *data, int size)
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
{
    int i, ret, offset, wide_hdr, header_size;

    if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
        return ret;

    // Extension substream sync word
    skip_bits_long(&s->gb, 32);

    // User defined bits
    skip_bits(&s->gb, 8);

    // Extension substream index
    s->exss_index = get_bits(&s->gb, 2);

    // Flag indicating short or long header size
    wide_hdr = get_bits1(&s->gb);

    // Extension substream header length
    header_size = get_bits(&s->gb, 8 + 4 * wide_hdr) + 1;

    // Check CRC
401
    if (s->avctx && ff_dca_check_crc(s->avctx, &s->gb, 32 + 8, header_size * 8)) {
402 403 404 405 406 407 408 409 410
        av_log(s->avctx, AV_LOG_ERROR, "Invalid EXSS header checksum\n");
        return AVERROR_INVALIDDATA;
    }

    s->exss_size_nbits = 16 + 4 * wide_hdr;

    // Number of bytes of extension substream
    s->exss_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
    if (s->exss_size > size) {
411 412
        if (s->avctx)
            av_log(s->avctx, AV_LOG_ERROR, "Packet too short for EXSS frame\n");
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
        return AVERROR_INVALIDDATA;
    }

    // Per stream static fields presence flag
    if (s->static_fields_present = get_bits1(&s->gb)) {
        int active_exss_mask[8];

        // Reference clock code
        skip_bits(&s->gb, 2);

        // Extension substream frame duration
        skip_bits(&s->gb, 3);

        // Timecode presence flag
        if (get_bits1(&s->gb))
            // Timecode data
            skip_bits_long(&s->gb, 36);

        // Number of defined audio presentations
        s->npresents = get_bits(&s->gb, 3) + 1;
        if (s->npresents > 1) {
434 435
            if (s->avctx)
                avpriv_request_sample(s->avctx, "%d audio presentations", s->npresents);
436 437 438 439 440 441
            return AVERROR_PATCHWELCOME;
        }

        // Number of audio assets in extension substream
        s->nassets = get_bits(&s->gb, 3) + 1;
        if (s->nassets > 1) {
442 443
            if (s->avctx)
                avpriv_request_sample(s->avctx, "%d audio assets", s->nassets);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
            return AVERROR_PATCHWELCOME;
        }

        // Active extension substream mask for audio presentation
        for (i = 0; i < s->npresents; i++)
            active_exss_mask[i] = get_bits(&s->gb, s->exss_index + 1);

        // Active audio asset mask
        for (i = 0; i < s->npresents; i++)
            skip_bits_long(&s->gb, av_popcount(active_exss_mask[i]) * 8);

        // Mixing metadata enable flag
        if (s->mix_metadata_enabled = get_bits1(&s->gb)) {
            int spkr_mask_nbits;

            // Mixing metadata adjustment level
            skip_bits(&s->gb, 2);

            // Number of bits for mixer output speaker activity mask
            spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;

            // Number of mixing configurations
            s->nmixoutconfigs = get_bits(&s->gb, 2) + 1;

            // Speaker layout mask for mixer output channels
            for (i = 0; i < s->nmixoutconfigs; i++)
470
                s->nmixoutchs[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
471 472 473 474 475 476 477 478 479 480 481 482 483
        }
    } else {
        s->npresents = 1;
        s->nassets = 1;
    }

    // Size of encoded asset data in bytes
    offset = header_size;
    for (i = 0; i < s->nassets; i++) {
        s->assets[i].asset_offset = offset;
        s->assets[i].asset_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
        offset += s->assets[i].asset_size;
        if (offset > s->exss_size) {
484 485
            if (s->avctx)
                av_log(s->avctx, AV_LOG_ERROR, "EXSS asset out of bounds\n");
486 487 488 489 490 491 492 493 494
            return AVERROR_INVALIDDATA;
        }
    }

    // Audio asset descriptor
    for (i = 0; i < s->nassets; i++) {
        if ((ret = parse_descriptor(s, &s->assets[i])) < 0)
            return ret;
        if ((ret = set_exss_offsets(&s->assets[i])) < 0) {
495 496
            if (s->avctx)
                av_log(s->avctx, AV_LOG_ERROR, "Invalid extension size in EXSS asset descriptor\n");
497 498 499 500 501 502 503 504 505 506 507
            return ret;
        }
    }

    // Backward compatible core present
    // Backward compatible core substream index
    // Backward compatible core asset index
    // Reserved
    // Byte align
    // CRC16 of extension substream header
    if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
508 509
        if (s->avctx)
            av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS header\n");
510 511 512 513 514
        return AVERROR_INVALIDDATA;
    }

    return 0;
}