1520 lines · 50.4 KB · C
RawDownload
/* * 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 "avcodec.h"#include "libavutil/channel_layout.h"#include "libavutil/mem.h"#include "dcadec.h"#include "dcadata.h"#include "dcamath.h"#include "dca_syncwords.h"#include "decode.h"#include "unary.h" static int get_linear(GetBitContext *gb, int n){    unsigned int v = get_bits_long(gb, n);    return (v >> 1) ^ -(v & 1);} static int get_rice_un(GetBitContext *gb, int k){    unsigned int v = get_unary(gb, 1, get_bits_left(gb));    return (v << k) | get_bits_long(gb, k);} static int get_rice(GetBitContext *gb, int k){    unsigned int v = get_rice_un(gb, k);    return (v >> 1) ^ -(v & 1);} static void get_array(GetBitContext *gb, int32_t *array, int size, int n){    int i;     for (i = 0; i < size; i++)        array[i] = get_bits(gb, n);} static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n){    int i;     if (n == 0)        memset(array, 0, sizeof(*array) * size);    else for (i = 0; i < size; i++)        array[i] = get_linear(gb, n);} static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k){    int i;     for (i = 0; i < size; i++)        array[i] = get_rice(gb, k);} static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c){    // Size of downmix coefficient matrix    int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;    int i, j, *coeff_ptr = c->dmix_coeff;     for (i = 0; i < m; i++) {        int code, sign, coeff, scale, scale_inv = 0;        unsigned int index;         // Downmix scale (only for non-primary channel sets)        if (!c->primary_chset) {            code = get_bits(&s->gb, 9);            sign = (code >> 8) - 1;            index = (code & 0xff) - FF_DCA_DMIXTABLE_OFFSET;            if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {                av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");                return AVERROR_INVALIDDATA;            }            scale = ff_dca_dmixtable[index + FF_DCA_DMIXTABLE_OFFSET];            scale_inv = ff_dca_inv_dmixtable[index];            c->dmix_scale[i] = (scale ^ sign) - sign;            c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;        }         // Downmix coefficients        for (j = 0; j < c->nchannels; j++) {            code = get_bits(&s->gb, 9);            sign = (code >> 8) - 1;            index = code & 0xff;            if (index >= FF_DCA_DMIXTABLE_SIZE) {                av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");                return AVERROR_INVALIDDATA;            }            coeff = ff_dca_dmixtable[index];            if (!c->primary_chset)                // Multiply by |InvDmixScale| to get |UndoDmixScale|                coeff = mul16(scale_inv, coeff);            *coeff_ptr++ = (coeff ^ sign) - sign;        }    }     return 0;} static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset){    int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);    DCAXllChSet *p = &s->chset[0];    DCAXllBand *b;     // Size of channel set sub-header    header_size = get_bits(&s->gb, 10) + 1;     // Check CRC    if (ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {        av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");        return AVERROR_INVALIDDATA;    }     // Number of channels in the channel set    c->nchannels = get_bits(&s->gb, 4) + 1;    if (c->nchannels > DCA_XLL_CHANNELS_MAX) {        avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);        return AVERROR_PATCHWELCOME;    }     // Residual type    c->residual_encode = get_bits(&s->gb, c->nchannels);     // PCM bit resolution    c->pcm_bit_res = get_bits(&s->gb, 5) + 1;     // Storage unit width    c->storage_bit_res = get_bits(&s->gb, 5) + 1;    if (c->storage_bit_res != 16 && c->storage_bit_res != 20 && c->storage_bit_res != 24) {        avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);        return AVERROR_PATCHWELCOME;    }     if (c->pcm_bit_res > c->storage_bit_res) {        av_log(s->avctx, AV_LOG_ERROR, "Invalid PCM bit resolution for XLL channel set (%d > %d)\n", c->pcm_bit_res, c->storage_bit_res);        return AVERROR_INVALIDDATA;    }     // Original sampling frequency    c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];    if (c->freq > 192000) {        avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);        return AVERROR_PATCHWELCOME;    }     // Sampling frequency modifier    if (get_bits(&s->gb, 2)) {        avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");        return AVERROR_PATCHWELCOME;    }     // Which replacement set this channel set is member of    if (get_bits(&s->gb, 2)) {        avpriv_request_sample(s->avctx, "XLL replacement set");        return AVERROR_PATCHWELCOME;    }     if (asset->one_to_one_map_ch_to_spkr) {        // Primary channel set flag        c->primary_chset = get_bits1(&s->gb);        if (c->primary_chset != (c == p)) {            av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");            return AVERROR_INVALIDDATA;        }         // Downmix coefficients present in stream        c->dmix_coeffs_present = get_bits1(&s->gb);         // Downmix already performed by encoder        c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);         // Downmix type        if (c->dmix_coeffs_present && c->primary_chset) {            c->dmix_type = get_bits(&s->gb, 3);            if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {                av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");                return AVERROR_INVALIDDATA;            }        }         // Whether the channel set is part of a hierarchy        c->hier_chset = get_bits1(&s->gb);        if (!c->hier_chset && s->nchsets != 1) {            avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");            return AVERROR_PATCHWELCOME;        }         // Downmix coefficients        if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)            return ret;         // Channel mask enabled        if (!get_bits1(&s->gb)) {            avpriv_request_sample(s->avctx, "Disabled XLL channel mask");            return AVERROR_PATCHWELCOME;        }         // Channel mask for set        c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);        if (av_popcount(c->ch_mask) != c->nchannels) {            av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");            return AVERROR_INVALIDDATA;        }         // Build the channel to speaker map        for (i = 0, j = 0; i < s->ch_mask_nbits; i++)            if (c->ch_mask & (1U << i))                c->ch_remap[j++] = i;    } else {        // Mapping coeffs present flag        if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {            avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");            return AVERROR_PATCHWELCOME;        }         // Setup for LtRt decoding        c->primary_chset = 1;        c->dmix_coeffs_present = 0;        c->dmix_embedded = 0;        c->hier_chset = 0;        c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;        c->ch_remap[0] = DCA_SPEAKER_L;        c->ch_remap[1] = DCA_SPEAKER_R;    }     if (c->freq > 96000) {        // Extra frequency bands flag        if (get_bits1(&s->gb)) {            avpriv_request_sample(s->avctx, "Extra XLL frequency bands");            return AVERROR_PATCHWELCOME;        }        c->nfreqbands = 2;    } else {        c->nfreqbands = 1;    }     // Set the sampling frequency to that of the first frequency band.    // Frequency will be doubled again after bands assembly.    c->freq >>= c->nfreqbands - 1;     // Verify that all channel sets have the same audio characteristics    if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq                   || c->pcm_bit_res != p->pcm_bit_res                   || c->storage_bit_res != p->storage_bit_res)) {        avpriv_request_sample(s->avctx, "Different XLL audio characteristics");        return AVERROR_PATCHWELCOME;    }     // Determine number of bits to read bit allocation coding parameter    if (c->storage_bit_res > 16)        c->nabits = 5;    else if (c->storage_bit_res > 8)        c->nabits = 4;    else        c->nabits = 3;     // Account for embedded downmix and decimator saturation    if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)        c->nabits++;     for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {        // Pairwise channel decorrelation        if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {            int ch_nbits = av_ceil_log2(c->nchannels);             // Original channel order            for (i = 0; i < c->nchannels; i++) {                b->orig_order[i] = get_bits(&s->gb, ch_nbits);                if (b->orig_order[i] >= c->nchannels) {                    av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");                    return AVERROR_INVALIDDATA;                }            }             // Pairwise channel coefficients            for (i = 0; i < c->nchannels / 2; i++)                b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;        } else {            for (i = 0; i < c->nchannels; i++)                b->orig_order[i] = i;            for (i = 0; i < c->nchannels / 2; i++)                b->decor_coeff[i] = 0;        }         // Adaptive predictor order        b->highest_pred_order = 0;        for (i = 0; i < c->nchannels; i++) {            b->adapt_pred_order[i] = get_bits(&s->gb, 4);            if (b->adapt_pred_order[i] > b->highest_pred_order)                b->highest_pred_order = b->adapt_pred_order[i];        }        if (b->highest_pred_order > s->nsegsamples) {            av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive predicition order\n");            return AVERROR_INVALIDDATA;        }         // Fixed predictor order        for (i = 0; i < c->nchannels; i++)            b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);         // Adaptive predictor quantized reflection coefficients        for (i = 0; i < c->nchannels; i++) {            for (j = 0; j < b->adapt_pred_order[i]; j++) {                k = get_linear(&s->gb, 8);                if (k == -128) {                    av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");                    return AVERROR_INVALIDDATA;                }                if (k < 0)                    b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];                else                    b->adapt_refl_coeff[i][j] =  (int)ff_dca_xll_refl_coeff[ k];            }        }         // Downmix performed by encoder in extension frequency band        b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));         // MSB/LSB split flag in extension frequency band        if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {            // Size of LSB section in any segment            b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);            if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {                av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");                return AVERROR_INVALIDDATA;            }             // Account for optional CRC bytes after LSB section            if (b->lsb_section_size && (s->band_crc_present > 2 ||                                        (band == 0 && s->band_crc_present > 1)))                b->lsb_section_size += 2;             // Number of bits to represent the samples in LSB part            for (i = 0; i < c->nchannels; i++) {                b->nscalablelsbs[i] = get_bits(&s->gb, 4);                if (b->nscalablelsbs[i] && !b->lsb_section_size) {                    av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");                    return AVERROR_INVALIDDATA;                }            }        } else {            b->lsb_section_size = 0;            for (i = 0; i < c->nchannels; i++)                b->nscalablelsbs[i] = 0;        }         // Scalable resolution flag in extension frequency band        if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {            // Number of bits discarded by authoring            for (i = 0; i < c->nchannels; i++)                b->bit_width_adjust[i] = get_bits(&s->gb, 4);        } else {            for (i = 0; i < c->nchannels; i++)                b->bit_width_adjust[i] = 0;        }    }     // Reserved    // Byte align    // CRC16 of channel set sub-header    if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {        av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");        return AVERROR_INVALIDDATA;    }     return 0;} static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c){    int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;    int nchsamples = s->nframesamples + ndecisamples;    int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;    int32_t *ptr;     // Reallocate MSB sample buffer    av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));    if (!c->sample_buffer[0])        return AVERROR(ENOMEM);     ptr = c->sample_buffer[0] + ndecisamples;    for (i = 0; i < c->nfreqbands; i++) {        for (j = 0; j < c->nchannels; j++) {            c->bands[i].msb_sample_buffer[j] = ptr;            ptr += nchsamples;        }    }     return 0;} static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c){    int i, j, nsamples = 0;    int32_t *ptr;     // Determine number of frequency bands that have MSB/LSB split    for (i = 0; i < c->nfreqbands; i++)        if (c->bands[i].lsb_section_size)            nsamples += s->nframesamples * c->nchannels;    if (!nsamples)        return 0;     // Reallocate LSB sample buffer    av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));    if (!c->sample_buffer[1])        return AVERROR(ENOMEM);     ptr = c->sample_buffer[1];    for (i = 0; i < c->nfreqbands; i++) {        if (c->bands[i].lsb_section_size) {            for (j = 0; j < c->nchannels; j++) {                c->bands[i].lsb_sample_buffer[j] = ptr;                ptr += s->nframesamples;            }        } else {            for (j = 0; j < c->nchannels; j++)                c->bands[i].lsb_sample_buffer[j] = NULL;        }    }     return 0;} static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end){    DCAXllBand *b = &c->bands[band];    int i, j, k;     // Start unpacking MSB portion of the segment    if (!(seg && get_bits1(&s->gb))) {        // Unpack segment type        // 0 - distinct coding parameters for each channel        // 1 - common coding parameters for all channels        c->seg_common = get_bits1(&s->gb);         // Determine number of coding parameters encoded in segment        k = c->seg_common ? 1 : c->nchannels;         // Unpack Rice coding parameters        for (i = 0; i < k; i++) {            // Unpack Rice coding flag            // 0 - linear code, 1 - Rice code            c->rice_code_flag[i] = get_bits1(&s->gb);            // Unpack Hybrid Rice coding flag            // 0 - Rice code, 1 - Hybrid Rice code            if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))                // Unpack binary code length for isolated samples                c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;            else                // 0 indicates no Hybrid Rice coding                c->bitalloc_hybrid_linear[i] = 0;        }         // Unpack coding parameters        for (i = 0; i < k; i++) {            if (seg == 0) {                // Unpack coding parameter for part A of segment 0                c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);                 // Adjust for the linear code                if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])                    c->bitalloc_part_a[i]++;                 if (!c->seg_common)                    c->nsamples_part_a[i] = b->adapt_pred_order[i];                else                    c->nsamples_part_a[i] = b->highest_pred_order;            } else {                c->bitalloc_part_a[i] = 0;                c->nsamples_part_a[i] = 0;            }             // Unpack coding parameter for part B of segment            c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);             // Adjust for the linear code            if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])                c->bitalloc_part_b[i]++;

Lines 1–500 of 1520.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t