From 6c44696b3d504eb87d60915919074da530cd379f Mon Sep 17 00:00:00 2001 From: foo86 Date: Sun, 1 May 2016 18:43:00 +0300 Subject: [PATCH] avcodec/dca: add DTS Express (LBR) decoder Signed-off-by: James Almer --- Changelog | 1 + libavcodec/Makefile | 2 +- libavcodec/dca_lbr.c | 1825 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/dca_lbr.h | 130 +++ libavcodec/dcadata.c | 466 +++++++++++ libavcodec/dcadata.h | 47 ++ libavcodec/dcadec.c | 22 +- libavcodec/dcadec.h | 5 +- libavcodec/dcadsp.c | 74 ++ libavcodec/dcadsp.h | 7 + libavcodec/dcahuff.c | 244 +++++- libavcodec/dcahuff.h | 13 + 12 files changed, 2830 insertions(+), 6 deletions(-) create mode 100644 libavcodec/dca_lbr.c create mode 100644 libavcodec/dca_lbr.h diff --git a/Changelog b/Changelog index ad2f6baaec..6dd0a14885 100644 --- a/Changelog +++ b/Changelog @@ -31,6 +31,7 @@ version : - Duck TrueMotion 2.0 Real Time decoder - Wideband Single-bit Data (WSD) demuxer - VAAPI-accelerated H.264/HEVC/MJPEG encoding +- DTS Express (LBR) decoder version 3.0: - Common Encryption (CENC) MP4 encoding and decoding support diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 347e15a052..1ee3977c7e 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -232,7 +232,7 @@ OBJS-$(CONFIG_CPIA_DECODER) += cpia.o OBJS-$(CONFIG_CSCD_DECODER) += cscd.o OBJS-$(CONFIG_CYUV_DECODER) += cyuv.o OBJS-$(CONFIG_DCA_DECODER) += dcadec.o dca.o dcadata.o dcahuff.o \ - dca_core.o dca_exss.o dca_xll.o \ + dca_core.o dca_exss.o dca_xll.o dca_lbr.o \ dcadsp.o dcadct.o synth_filter.o OBJS-$(CONFIG_DCA_ENCODER) += dcaenc.o dca.o dcadata.o OBJS-$(CONFIG_DDS_DECODER) += dds.o diff --git a/libavcodec/dca_lbr.c b/libavcodec/dca_lbr.c new file mode 100644 index 0000000000..595187c258 --- /dev/null +++ b/libavcodec/dca_lbr.c @@ -0,0 +1,1825 @@ +/* + * 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 + */ + +#define UNCHECKED_BITSTREAM_READER 1 +#define BITSTREAM_READER_LE + +#include "libavutil/channel_layout.h" + +#include "dcadec.h" +#include "dcadata.h" +#include "dcahuff.h" +#include "dca_syncwords.h" +#include "bytestream.h" + +#define AMP_MAX 56 + +enum LBRHeader { + LBR_HEADER_SYNC_ONLY = 1, + LBR_HEADER_DECODER_INIT = 2 +}; + +enum LBRFlags { + LBR_FLAG_24_BIT = 0x01, + LBR_FLAG_LFE_PRESENT = 0x02, + LBR_FLAG_BAND_LIMIT_2_3 = 0x04, + LBR_FLAG_BAND_LIMIT_1_2 = 0x08, + LBR_FLAG_BAND_LIMIT_1_3 = 0x0c, + LBR_FLAG_BAND_LIMIT_1_4 = 0x10, + LBR_FLAG_BAND_LIMIT_1_8 = 0x18, + LBR_FLAG_BAND_LIMIT_NONE = 0x14, + LBR_FLAG_BAND_LIMIT_MASK = 0x1c, + LBR_FLAG_DMIX_STEREO = 0x20, + LBR_FLAG_DMIX_MULTI_CH = 0x40 +}; + +enum LBRChunkTypes { + LBR_CHUNK_NULL = 0x00, + LBR_CHUNK_PAD = 0x01, + LBR_CHUNK_FRAME = 0x04, + LBR_CHUNK_FRAME_NO_CSUM = 0x06, + LBR_CHUNK_LFE = 0x0a, + LBR_CHUNK_ECS = 0x0b, + LBR_CHUNK_RESERVED_1 = 0x0c, + LBR_CHUNK_RESERVED_2 = 0x0d, + LBR_CHUNK_SCF = 0x0e, + LBR_CHUNK_TONAL = 0x10, + LBR_CHUNK_TONAL_GRP_1 = 0x11, + LBR_CHUNK_TONAL_GRP_2 = 0x12, + LBR_CHUNK_TONAL_GRP_3 = 0x13, + LBR_CHUNK_TONAL_GRP_4 = 0x14, + LBR_CHUNK_TONAL_GRP_5 = 0x15, + LBR_CHUNK_TONAL_SCF = 0x16, + LBR_CHUNK_TONAL_SCF_GRP_1 = 0x17, + LBR_CHUNK_TONAL_SCF_GRP_2 = 0x18, + LBR_CHUNK_TONAL_SCF_GRP_3 = 0x19, + LBR_CHUNK_TONAL_SCF_GRP_4 = 0x1a, + LBR_CHUNK_TONAL_SCF_GRP_5 = 0x1b, + LBR_CHUNK_RES_GRID_LR = 0x30, + LBR_CHUNK_RES_GRID_LR_LAST = 0x3f, + LBR_CHUNK_RES_GRID_HR = 0x40, + LBR_CHUNK_RES_GRID_HR_LAST = 0x4f, + LBR_CHUNK_RES_TS_1 = 0x50, + LBR_CHUNK_RES_TS_1_LAST = 0x5f, + LBR_CHUNK_RES_TS_2 = 0x60, + LBR_CHUNK_RES_TS_2_LAST = 0x6f, + LBR_CHUNK_EXTENSION = 0x7f +}; + +typedef struct LBRChunk { + int id, len; + const uint8_t *data; +} LBRChunk; + +static const int8_t channel_reorder_nolfe[7][5] = { + { 0, -1, -1, -1, -1 }, // C + { 0, 1, -1, -1, -1 }, // LR + { 0, 1, 2, -1, -1 }, // LR C + { 0, 1, -1, -1, -1 }, // LsRs + { 1, 2, 0, -1, -1 }, // LsRs C + { 0, 1, 2, 3, -1 }, // LR LsRs + { 0, 1, 3, 4, 2 }, // LR LsRs C +}; + +static const int8_t channel_reorder_lfe[7][5] = { + { 0, -1, -1, -1, -1 }, // C + { 0, 1, -1, -1, -1 }, // LR + { 0, 1, 2, -1, -1 }, // LR C + { 1, 2, -1, -1, -1 }, // LsRs + { 2, 3, 0, -1, -1 }, // LsRs C + { 0, 1, 3, 4, -1 }, // LR LsRs + { 0, 1, 4, 5, 2 }, // LR LsRs C +}; + +static const uint8_t lfe_index[7] = { + 1, 2, 3, 0, 1, 2, 3 +}; + +static const uint8_t channel_counts[7] = { + 1, 2, 3, 2, 3, 4, 5 +}; + +static const uint16_t channel_layouts[7] = { + AV_CH_LAYOUT_MONO, + AV_CH_LAYOUT_STEREO, + AV_CH_LAYOUT_SURROUND, + AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, + AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, + AV_CH_LAYOUT_2_2, + AV_CH_LAYOUT_5POINT0 +}; + +static float cos_tab[256]; +static float lpc_tab[16]; + +static av_cold void init_tables(void) +{ + static int initialized; + int i; + + if (initialized) + return; + + for (i = 0; i < 256; i++) + cos_tab[i] = cos(M_PI * i / 128); + + for (i = 0; i < 16; i++) + lpc_tab[i] = sin((i - 8) * (M_PI / ((i < 8) ? 17 : 15))); + + initialized = 1; +} + +static int parse_lfe_24(DCALbrDecoder *s) +{ + int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_24) - 1; + int i, ps, si, code, step_i; + float step, value, delta; + + ps = get_bits(&s->gb, 24); + si = ps >> 23; + + value = (((ps & 0x7fffff) ^ -si) + si) * (1.0f / 0x7fffff); + + step_i = get_bits(&s->gb, 8); + if (step_i > step_max) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n"); + return -1; + } + + step = ff_dca_lfe_step_size_24[step_i]; + + for (i = 0; i < 64; i++) { + code = get_bits(&s->gb, 6); + + delta = step * 0.03125f; + if (code & 16) + delta += step; + if (code & 8) + delta += step * 0.5f; + if (code & 4) + delta += step * 0.25f; + if (code & 2) + delta += step * 0.125f; + if (code & 1) + delta += step * 0.0625f; + + if (code & 32) { + value -= delta; + if (value < -3.0f) + value = -3.0f; + } else { + value += delta; + if (value > 3.0f) + value = 3.0f; + } + + step_i += ff_dca_lfe_delta_index_24[code & 31]; + step_i = av_clip(step_i, 0, step_max); + + step = ff_dca_lfe_step_size_24[step_i]; + s->lfe_data[i] = value * s->lfe_scale; + } + + return 0; +} + +static int parse_lfe_16(DCALbrDecoder *s) +{ + int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_16) - 1; + int i, ps, si, code, step_i; + float step, value, delta; + + ps = get_bits(&s->gb, 16); + si = ps >> 15; + + value = (((ps & 0x7fff) ^ -si) + si) * (1.0f / 0x7fff); + + step_i = get_bits(&s->gb, 8); + if (step_i > step_max) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n"); + return -1; + } + + step = ff_dca_lfe_step_size_16[step_i]; + + for (i = 0; i < 64; i++) { + code = get_bits(&s->gb, 4); + + delta = step * 0.125f; + if (code & 4) + delta += step; + if (code & 2) + delta += step * 0.5f; + if (code & 1) + delta += step * 0.25f; + + if (code & 8) { + value -= delta; + if (value < -3.0f) + value = -3.0f; + } else { + value += delta; + if (value > 3.0f) + value = 3.0f; + } + + step_i += ff_dca_lfe_delta_index_16[code & 7]; + step_i = av_clip(step_i, 0, step_max); + + step = ff_dca_lfe_step_size_16[step_i]; + s->lfe_data[i] = value * s->lfe_scale; + } + + return 0; +} + +static int parse_lfe_chunk(DCALbrDecoder *s, LBRChunk *chunk) +{ + if (!(s->flags & LBR_FLAG_LFE_PRESENT)) + return 0; + + if (!chunk->len) + return 0; + + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + + // Determine bit depth from chunk size + if (chunk->len >= 52) + return parse_lfe_24(s); + if (chunk->len >= 35) + return parse_lfe_16(s); + + av_log(s->avctx, AV_LOG_ERROR, "LFE chunk too short\n"); + return -1; +} + +static inline int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth) +{ + int v = get_vlc2(s, vlc->table, vlc->bits, max_depth); + if (v > 0) + return v - 1; + // Rare value + return get_bits(s, get_bits(s, 3) + 1); +} + +static int parse_tonal(DCALbrDecoder *s, int group) +{ + unsigned int amp[DCA_LBR_CHANNELS_TOTAL]; + unsigned int phs[DCA_LBR_CHANNELS_TOTAL]; + unsigned int diff, main_amp, shift; + int sf, sf_idx, ch, main_ch, freq; + int ch_nbits = av_ceil_log2(s->nchannels_total); + + // Parse subframes for this group + for (sf = 0; sf < 1 << group; sf += diff ? 8 : 1) { + sf_idx = ((s->framenum << group) + sf) & 31; + s->tonal_bounds[group][sf_idx][0] = s->ntones; + + // Parse tones for this subframe + for (freq = 1;; freq++) { + if (get_bits_left(&s->gb) < 1) { + av_log(s->avctx, AV_LOG_ERROR, "Tonal group chunk too short\n"); + return -1; + } + + diff = parse_vlc(&s->gb, &ff_dca_vlc_tnl_grp[group], 2); + if (diff >= FF_ARRAY_ELEMS(ff_dca_fst_amp)) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid tonal frequency diff\n"); + return -1; + } + + diff = get_bitsz(&s->gb, diff >> 2) + ff_dca_fst_amp[diff]; + if (diff <= 1) + break; // End of subframe + + freq += diff - 2; + if (freq >> (5 - group) > s->nsubbands * 4 - 5) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid spectral line offset\n"); + return -1; + } + + // Main channel + main_ch = get_bitsz(&s->gb, ch_nbits); + main_amp = parse_vlc(&s->gb, &ff_dca_vlc_tnl_scf, 2) + + s->tonal_scf[ff_dca_freq_to_sb[freq >> (7 - group)]] + + s->limited_range - 2; + amp[main_ch] = main_amp < AMP_MAX ? main_amp : 0; + phs[main_ch] = get_bits(&s->gb, 3); + + // Secondary channels + for (ch = 0; ch < s->nchannels_total; ch++) { + if (ch == main_ch) + continue; + if (get_bits1(&s->gb)) { + amp[ch] = amp[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_damp, 1); + phs[ch] = phs[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_dph, 1); + } else { + amp[ch] = 0; + phs[ch] = 0; + } + } + + if (amp[main_ch]) { + // Allocate new tone + DCALbrTone *t = &s->tones[s->ntones]; + s->ntones = (s->ntones + 1) & (DCA_LBR_TONES - 1); + + t->x_freq = freq >> (5 - group); + t->f_delt = (freq & ((1 << (5 - group)) - 1)) << group; + t->ph_rot = 256 - (t->x_freq & 1) * 128 - t->f_delt * 4; + + shift = ff_dca_ph0_shift[(t->x_freq & 3) * 2 + (freq & 1)] + - ((t->ph_rot << (5 - group)) - t->ph_rot); + + for (ch = 0; ch < s->nchannels; ch++) { + t->amp[ch] = amp[ch] < AMP_MAX ? amp[ch] : 0; + t->phs[ch] = 128 - phs[ch] * 32 + shift; + } + } + } + + s->tonal_bounds[group][sf_idx][1] = s->ntones; + } + + return 0; +} + +static int parse_tonal_chunk(DCALbrDecoder *s, LBRChunk *chunk) +{ + int sb, group; + + if (!chunk->len) + return 0; + + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + + // Scale factors + if (chunk->id == LBR_CHUNK_SCF || chunk->id == LBR_CHUNK_TONAL_SCF) { + if (get_bits_left(&s->gb) < 36) { + av_log(s->avctx, AV_LOG_ERROR, "Tonal scale factor chunk too short\n"); + return -1; + } + for (sb = 0; sb < 6; sb++) + s->tonal_scf[sb] = get_bits(&s->gb, 6); + } + + // Tonal groups + if (chunk->id == LBR_CHUNK_TONAL || chunk->id == LBR_CHUNK_TONAL_SCF) + for (group = 0; group < 5; group++) + if (parse_tonal(s, group) < 0) + return -1; + + return 0; +} + +static int parse_tonal_group(DCALbrDecoder *s, LBRChunk *chunk) +{ + if (!chunk->len) + return 0; + + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + + return parse_tonal(s, chunk->id); +} + +/** + * Check point to ensure that enough bits are left. Aborts decoding + * by skipping to the end of chunk otherwise. + */ +static int ensure_bits(GetBitContext *s, int n) +{ + int left = get_bits_left(s); + if (left < 0) + return -1; + if (left < n) { + skip_bits_long(s, left); + return 1; + } + return 0; +} + +static int parse_scale_factors(DCALbrDecoder *s, uint8_t *scf) +{ + int i, sf, prev, next, dist; + + // Truncated scale factors remain zero + if (ensure_bits(&s->gb, 20)) + return 0; + + // Initial scale factor + prev = parse_vlc(&s->gb, &ff_dca_vlc_fst_rsd_amp, 2); + + for (sf = 0; sf < 7; sf += dist) { + scf[sf] = prev; // Store previous value + + if (ensure_bits(&s->gb, 20)) + return 0; + + // Interpolation distance + dist = parse_vlc(&s->gb, &ff_dca_vlc_rsd_apprx, 1) + 1; + if (dist > 7 - sf) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor distance\n"); + return -1; + } + + if (ensure_bits(&s->gb, 20)) + return 0; + + // Final interpolation point + next = parse_vlc(&s->gb, &ff_dca_vlc_rsd_amp, 2); + + if (next & 1) + next = prev + ((next + 1) >> 1); + else + next = prev - ( next >> 1); + + // Interpolate + switch (dist) { + case 2: + if (next > prev) + scf[sf + 1] = prev + ((next - prev) >> 1); + else + scf[sf + 1] = prev - ((prev - next) >> 1); + break; + + case 4: + if (next > prev) { + scf[sf + 1] = prev + ( (next - prev) >> 2); + scf[sf + 2] = prev + ( (next - prev) >> 1); + scf[sf + 3] = prev + (((next - prev) * 3) >> 2); + } else { + scf[sf + 1] = prev - ( (prev - next) >> 2); + scf[sf + 2] = prev - ( (prev - next) >> 1); + scf[sf + 3] = prev - (((prev - next) * 3) >> 2); + } + break; + + default: + for (i = 1; i < dist; i++) + scf[sf + i] = prev + (next - prev) * i / dist; + break; + } + + prev = next; + } + + scf[sf] = next; // Store final value + + return 0; +} + +static int parse_st_code(GetBitContext *s, int min_v) +{ + unsigned int v = parse_vlc(s, &ff_dca_vlc_st_grid, 2) + min_v; + + if (v & 1) + v = 16 + (v >> 1); + else + v = 16 - (v >> 1); + + if (v >= FF_ARRAY_ELEMS(ff_dca_st_coeff)) + v = 16; + return v; +} + +static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2) +{ + int ch, sb, sf, nsubbands; + + if (!chunk->len) + return 0; + + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + + // Scale factors + nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1; + for (sb = 2; sb < nsubbands; sb++) { + if (parse_scale_factors(s, s->grid_1_scf[ch1][sb]) < 0) + return -1; + if (ch1 != ch2 && ff_dca_grid_1_to_scf[sb] < s->min_mono_subband + && parse_scale_factors(s, s->grid_1_scf[ch2][sb]) < 0) + return -1; + } + + if (get_bits_left(&s->gb) < 1) + return 0; // Should not happen, but a sample exists that proves otherwise + + // Average values for third grid + for (sb = 0; sb < s->nsubbands - 4; sb++) { + s->grid_3_avg[ch1][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16; + if (ch1 != ch2) { + if (sb + 4 < s->min_mono_subband) + s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16; + else + s->grid_3_avg[ch2][sb] = s->grid_3_avg[ch1][sb]; + } + } + + if (get_bits_left(&s->gb) < 0) { + av_log(s->avctx, AV_LOG_ERROR, "First grid chunk too short\n"); + return -1; + } + + // Stereo image for partial mono mode + if (ch1 != ch2) { + int min_v[2]; + + if (ensure_bits(&s->gb, 8)) + return 0; + + min_v[0] = get_bits(&s->gb, 4); + min_v[1] = get_bits(&s->gb, 4); + + nsubbands = (s->nsubbands - s->min_mono_subband + 3) / 4; + for (sb = 0; sb < nsubbands; sb++) + for (ch = ch1; ch <= ch2; ch++) + for (sf = 1; sf <= 4; sf++) + s->part_stereo[ch][sb][sf] = parse_st_code(&s->gb, min_v[ch - ch1]); + + if (get_bits_left(&s->gb) >= 0) + s->part_stereo_pres |= 1 << ch1; + } + + // Low resolution spatial information is not decoded + + return 0; +} + +static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2) +{ + int sb, nsubbands; + + // Scale factors + nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1; + for (sb = 2; sb < nsubbands; sb++) { + if (ff_dca_grid_1_to_scf[sb] >= s->min_mono_subband + && parse_scale_factors(s, s->grid_1_scf[ch2][sb]) < 0) + return -1; + } + + // Average values for third grid + for (sb = 0; sb < s->nsubbands - 4; sb++) { + if (sb + 4 >= s->min_mono_subband) { + if (ensure_bits(&s->gb, 20)) + return 0; + s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16; + } + } + + return 0; +} + +static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag) +{ + int i, ch; + + for (ch = ch1; ch <= ch2; ch++) { + if ((ch != ch1 && sb + 4 >= s->min_mono_subband) != flag) + continue; + + if (s->grid_3_pres[ch] & (1U << sb)) + continue; // Already parsed + + for (i = 0; i < 8; i++) { + if (ensure_bits(&s->gb, 20)) + return; + s->grid_3_scf[ch][sb][i] = parse_vlc(&s->gb, &ff_dca_vlc_grid_3, 2) - 16; + } + + // Flag scale factors for this subband parsed + s->grid_3_pres[ch] |= 1U << sb; + } +} + +static float lbr_rand(DCALbrDecoder *s, int sb) +{ + s->lbr_rand = 1103515245U * s->lbr_rand + 12345U; + return s->lbr_rand * s->sb_scf[sb]; +} + +/** + * Parse time samples for one subband, filling truncated samples with randomness + */ +static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag) +{ + float *samples = s->time_samples[ch][sb]; + int i, j, code, nblocks, coding_method; + + if (ensure_bits(&s->gb, 20)) + return; // Too few bits left + + coding_method = get_bits1(&s->gb); + + switch (quant_level) { + case 1: + nblocks = FFMIN(get_bits_left(&s->gb) / 8, DCA_LBR_TIME_SAMPLES / 8); + for (i = 0; i < nblocks; i++, samples += 8) { + code = get_bits(&s->gb, 8); + for (j = 0; j < 8; j++) + samples[j] = ff_dca_rsd_level_2a[(code >> j) & 1]; + } + i = nblocks * 8; + break; + + case 2: + if (coding_method) { + for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 2; i++) { + if (get_bits1(&s->gb)) + samples[i] = ff_dca_rsd_level_2b[get_bits1(&s->gb)]; + else + samples[i] = 0; + } + } else { + nblocks = FFMIN(get_bits_left(&s->gb) / 8, (DCA_LBR_TIME_SAMPLES + 4) / 5); + for (i = 0; i < nblocks; i++, samples += 5) { + code = ff_dca_rsd_pack_5_in_8[get_bits(&s->gb, 8)]; + for (j = 0; j < 5; j++) + samples[j] = ff_dca_rsd_level_3[(code >> j * 2) & 3]; + } + i = nblocks * 5; + } + break; + + case 3: + nblocks = FFMIN(get_bits_left(&s->gb) / 7, (DCA_LBR_TIME_SAMPLES + 2) / 3); + for (i = 0; i < nblocks; i++, samples += 3) { + code = get_bits(&s->gb, 7); + for (j = 0; j < 3; j++) + samples[j] = ff_dca_rsd_level_5[ff_dca_rsd_pack_3_in_7[code][j]]; + } + i = nblocks * 3; + break; + + case 4: + for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 6; i++) + samples[i] = ff_dca_rsd_level_8[get_vlc2(&s->gb, ff_dca_vlc_rsd.table, 6, 1)]; + break; + + case 5: + nblocks = FFMIN(get_bits_left(&s->gb) / 4, DCA_LBR_TIME_SAMPLES); + for (i = 0; i < nblocks; i++) + samples[i] = ff_dca_rsd_level_16[get_bits(&s->gb, 4)]; + break; + + default: + av_assert0(0); + } + + if (flag && get_bits_left(&s->gb) < 20) + return; // Skip incomplete mono subband + + for (; i < DCA_LBR_TIME_SAMPLES; i++) + s->time_samples[ch][sb][i] = lbr_rand(s, sb); + + s->ch_pres[ch] |= 1U << sb; +} + +static int parse_ts(DCALbrDecoder *s, int ch1, int ch2, + int start_sb, int end_sb, int flag) +{ + int sb, sb_g3, sb_reorder, quant_level; + + for (sb = start_sb; sb < end_sb; sb++) { + // Subband number before reordering + if (sb < 6) { + sb_reorder = sb; + } else if (flag && sb < s->max_mono_subband) { + sb_reorder = s->sb_indices[sb]; + } else { + if (ensure_bits(&s->gb, 28)) + break; + sb_reorder = get_bits(&s->gb, s->limited_range + 3); + if (sb_reorder < 6) + sb_reorder = 6; + s->sb_indices[sb] = sb_reorder; + } + if (sb_reorder >= s->nsubbands) + return -1; + + // Third grid scale factors + if (sb == 12) { + for (sb_g3 = 0; sb_g3 < s->g3_avg_only_start_sb - 4; sb_g3++) + parse_grid_3(s, ch1, ch2, sb_g3, flag); + } else if (sb < 12 && sb_reorder >= 4) { + parse_grid_3(s, ch1, ch2, sb_reorder - 4, flag); + } + + // Secondary channel flags + if (ch1 != ch2) { + if (ensure_bits(&s->gb, 20)) + break; + if (!flag || sb_reorder >= s->max_mono_subband) + s->sec_ch_sbms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8); + if (flag && sb_reorder >= s->min_mono_subband) + s->sec_ch_lrms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8); + } + + quant_level = s->quant_levels[ch1 / 2][sb]; + if (!quant_level) + return -1; + + // Time samples for one or both channels + if (sb < s->max_mono_subband && sb_reorder >= s->min_mono_subband) { + if (!flag) + parse_ch(s, ch1, sb_reorder, quant_level, 0); + else if (ch1 != ch2) + parse_ch(s, ch2, sb_reorder, quant_level, 1); + } else { + parse_ch(s, ch1, sb_reorder, quant_level, 0); + if (ch1 != ch2) + parse_ch(s, ch2, sb_reorder, quant_level, 0); + } + } + + return 0; +} + +/** + * Convert from reflection coefficients to direct form coefficients + */ +static void convert_lpc(float *coeff, const int *codes) +{ + int i, j; + + for (i = 0; i < 8; i++) { + float rc = lpc_tab[codes[i]]; + for (j = 0; j < (i + 1) / 2; j++) { + float tmp1 = coeff[ j ]; + float tmp2 = coeff[i - j - 1]; + coeff[ j ] = tmp1 + rc * tmp2; + coeff[i - j - 1] = tmp2 + rc * tmp1; + } + coeff[i] = rc; + } +} + +static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb) +{ + int f = s->framenum & 1; + int i, sb, ch, codes[16]; + + // First two subbands have two sets of coefficients, third subband has one + for (sb = start_sb; sb < end_sb; sb++) { + int ncodes = 8 * (1 + (sb < 2)); + for (ch = ch1; ch <= ch2; ch++) { + if (ensure_bits(&s->gb, 4 * ncodes)) + return 0; + for (i = 0; i < ncodes; i++) + codes[i] = get_bits(&s->gb, 4); + for (i = 0; i < ncodes / 8; i++) + convert_lpc(s->lpc_coeff[f][ch][sb][i], &codes[i * 8]); + } + } + + return 0; +} + +static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2) +{ + int quant_levels[DCA_LBR_SUBBANDS]; + int sb, ch, ol, st, max_sb, profile; + + if (!chunk->len) + return 0; + + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + + // Quantizer profile + profile = get_bits(&s->gb, 8); + // Overall level + ol = (profile >> 3) & 7; + // Steepness + st = profile >> 6; + // Max energy subband + max_sb = profile & 7; + + // Calculate quantization levels + for (sb = 0; sb < s->nsubbands; sb++) { + int f = sb * s->limited_rate / s->nsubbands; + int a = 18000 / (12 * f / 1000 + 100 + 40 * st) + 20 * ol; + if (a <= 95) + quant_levels[sb] = 1; + else if (a <= 140) + quant_levels[sb] = 2; + else if (a <= 180) + quant_levels[sb] = 3; + else if (a <= 230) + quant_levels[sb] = 4; + else + quant_levels[sb] = 5; + } + + // Reorder quantization levels for lower subbands + for (sb = 0; sb < 8; sb++) + s->quant_levels[ch1 / 2][sb] = quant_levels[ff_dca_sb_reorder[max_sb][sb]]; + for (; sb < s->nsubbands; sb++) + s->quant_levels[ch1 / 2][sb] = quant_levels[sb]; + + // LPC for the first two subbands + if (parse_lpc(s, ch1, ch2, 0, 2) < 0) + return -1; + + // Time-samples for the first two subbands of main channel + if (parse_ts(s, ch1, ch2, 0, 2, 0) < 0) + return -1; + + // First two bands of the first grid + for (sb = 0; sb < 2; sb++) + for (ch = ch1; ch <= ch2; ch++) + if (parse_scale_factors(s, s->grid_1_scf[ch][sb]) < 0) + return -1; + + return 0; +} + +static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2, + int start_sb, int end_sb, int flag) +{ + int i, j, sb, ch, nsubbands; + + nsubbands = ff_dca_scf_to_grid_2[s->nsubbands - 1] + 1; + if (end_sb > nsubbands) + end_sb = nsubbands; + + for (sb = start_sb; sb < end_sb; sb++) { + for (ch = ch1; ch <= ch2; ch++) { + uint8_t *g2_scf = s->grid_2_scf[ch][sb]; + + if ((ch != ch1 && ff_dca_grid_2_to_scf[sb] >= s->min_mono_subband) != flag) { + if (!flag) + memcpy(g2_scf, s->grid_2_scf[ch1][sb], 64); + continue; + } + + // Scale factors in groups of 8 + for (i = 0; i < 8; i++, g2_scf += 8) { + if (get_bits_left(&s->gb) < 1) { + memset(g2_scf, 0, 64 - i * 8); + break; + } + // Bit indicating if whole group has zero values + if (get_bits1(&s->gb)) { + for (j = 0; j < 8; j++) { + if (ensure_bits(&s->gb, 20)) + break; + g2_scf[j] = parse_vlc(&s->gb, &ff_dca_vlc_grid_2, 2); + } + } else { + memset(g2_scf, 0, 8); + } + } + } + } + + return 0; +} + +static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2) +{ + if (!chunk->len) + return 0; + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + if (parse_lpc(s, ch1, ch2, 2, 3) < 0) + return -1; + if (parse_ts(s, ch1, ch2, 2, 4, 0) < 0) + return -1; + if (parse_grid_2(s, ch1, ch2, 0, 1, 0) < 0) + return -1; + if (parse_ts(s, ch1, ch2, 4, 6, 0) < 0) + return -1; + return 0; +} + +static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2) +{ + if (!chunk->len) + return 0; + if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0) + return -1; + if (parse_grid_2(s, ch1, ch2, 1, 3, 0) < 0) + return -1; + if (parse_ts(s, ch1, ch2, 6, s->max_mono_subband, 0) < 0) + return -1; + if (ch1 != ch2) { + if (parse_grid_1_sec_ch(s, ch2) < 0) + return -1; + if (parse_grid_2(s, ch1, ch2, 0, 3, 1) < 0) + return -1; + } + if (parse_ts(s, ch1, ch2, s->min_mono_subband, s->nsubbands, 1) < 0) + return -1; + return 0; +} + +static int init_sample_rate(DCALbrDecoder *s) +{ + double scale = (-1.0 / (1 << 17)) * sqrt(1 << (2 - s->limited_range)); + int i, br_per_ch = s->bit_rate_scaled / s->nchannels_total; + + ff_mdct_end(&s->imdct); + + if (ff_mdct_init(&s->imdct, s->freq_range + 6, 1, scale) < 0) + return -1; + + for (i = 0; i < 32 << s->freq_range; i++) + s->window[i] = ff_dca_long_window[i << (2 - s->freq_range)]; + + if (br_per_ch < 14000) + scale = 0.85; + else if (br_per_ch < 32000) + scale = (br_per_ch - 14000) * (1.0 / 120000) + 0.85; + else + scale = 1.0; + + scale *= 1.0 / INT_MAX; + + for (i = 0; i < s->nsubbands; i++) { + if (i < 2) + s->sb_scf[i] = 0; // The first two subbands are always zero + else if (i < 5) + s->sb_scf[i] = (i - 1) * 0.25 * 0.785 * scale; + else + s->sb_scf[i] = 0.785 * scale; + } + + s->lfe_scale = (16 << s->freq_range) * 0.0000078265894; + + return 0; +} + +static int alloc_sample_buffer(DCALbrDecoder *s) +{ + // Reserve space for history and padding + int nchsamples = DCA_LBR_TIME_SAMPLES + DCA_LBR_TIME_HISTORY * 2; + int nsamples = nchsamples * s->nchannels * s->nsubbands; + int ch, sb; + float *ptr; + + // Reallocate time sample buffer + av_fast_mallocz(&s->ts_buffer, &s->ts_size, nsamples * sizeof(float)); + if (!s->ts_buffer) + return -1; + + ptr = s->ts_buffer + DCA_LBR_TIME_HISTORY; + for (ch = 0; ch < s->nchannels; ch++) { + for (sb = 0; sb < s->nsubbands; sb++) { + s->time_samples[ch][sb] = ptr; + ptr += nchsamples; + } + } + + return 0; +} + +static int parse_decoder_init(DCALbrDecoder *s, GetByteContext *gb) +{ + int old_rate = s->sample_rate; + int old_band_limit = s->band_limit; + int old_nchannels = s->nchannels; + int version, bit_rate_hi; + unsigned int code; + + // Sample rate of LBR audio + code = bytestream2_get_byte(gb); + if (code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sample rate\n"); + return AVERROR_INVALIDDATA; + } + s->sample_rate = ff_dca_sampling_freqs[code]; + if (s->sample_rate > 48000) { + avpriv_report_missing_feature(s->avctx, "%d Hz LBR sample rate", s->sample_rate); + return AVERROR_PATCHWELCOME; + } + + // LBR speaker mask + s->ch_mask = bytestream2_get_le16(gb); + if (!(s->ch_mask & 0x7)) { + avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask); + return AVERROR_PATCHWELCOME; + } + if ((s->ch_mask & 0xfff0) && !(s->warned & 1)) { + avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask); + s->warned |= 1; + } + + // LBR bitstream version + version = bytestream2_get_le16(gb); + if ((version & 0xff00) != 0x0800) { + avpriv_report_missing_feature(s->avctx, "LBR stream version %#x", version); + return AVERROR_PATCHWELCOME; + } + + // Flags for LBR decoder initialization + s->flags = bytestream2_get_byte(gb); + if (s->flags & LBR_FLAG_DMIX_MULTI_CH) { + avpriv_report_missing_feature(s->avctx, "LBR multi-channel downmix"); + return AVERROR_PATCHWELCOME; + } + if ((s->flags & LBR_FLAG_LFE_PRESENT) && s->sample_rate != 48000) { + if (!(s->warned & 2)) { + avpriv_report_missing_feature(s->avctx, "%d Hz LFE interpolation", s->sample_rate); + s->warned |= 2; + } + s->flags &= ~LBR_FLAG_LFE_PRESENT; + } + + // Most significant bit rate nibbles + bit_rate_hi = bytestream2_get_byte(gb); + + // Least significant original bit rate word + s->bit_rate_orig = bytestream2_get_le16(gb) | ((bit_rate_hi & 0x0F) << 16); + + // Least significant scaled bit rate word + s->bit_rate_scaled = bytestream2_get_le16(gb) | ((bit_rate_hi & 0xF0) << 12); + + // Setup number of fullband channels + s->nchannels_total = ff_dca_count_chs_for_mask(s->ch_mask & ~DCA_SPEAKER_PAIR_LFE1); + s->nchannels = FFMIN(s->nchannels_total, DCA_LBR_CHANNELS); + + // Setup band limit + switch (s->flags & LBR_FLAG_BAND_LIMIT_MASK) { + case LBR_FLAG_BAND_LIMIT_NONE: + s->band_limit = 0; + break; + case LBR_FLAG_BAND_LIMIT_1_2: + s->band_limit = 1; + break; + case LBR_FLAG_BAND_LIMIT_1_4: + s->band_limit = 2; + break; + default: + avpriv_report_missing_feature(s->avctx, "LBR band limit %#x", s->flags & LBR_FLAG_BAND_LIMIT_MASK); + return AVERROR_PATCHWELCOME; + } + + // Setup frequency range + if (s->sample_rate < 14000) + s->freq_range = 0; + else if (s->sample_rate < 28000) + s->freq_range = 1; + else + s->freq_range = 2; + + // Setup resolution profile + if (s->bit_rate_orig >= 44000 * (s->nchannels_total + 2)) + s->res_profile = 2; + else if (s->bit_rate_orig >= 25000 * (s->nchannels_total + 2)) + s->res_profile = 1; + else + s->res_profile = 0; + + // Setup limited sample rate, number of subbands, etc + s->limited_rate = s->sample_rate >> s->band_limit; + s->limited_range = s->freq_range - s->band_limit; + if (s->limited_range < 0) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR band limit for frequency range\n"); + return AVERROR_INVALIDDATA; + } + + s->nsubbands = 8 << s->limited_range; + + s->g3_avg_only_start_sb = s->nsubbands * ff_dca_avg_g3_freqs[s->res_profile] / (s->limited_rate / 2); + if (s->g3_avg_only_start_sb > s->nsubbands) + s->g3_avg_only_start_sb = s->nsubbands; + + s->min_mono_subband = s->nsubbands * 2000 / (s->limited_rate / 2); + if (s->min_mono_subband > s->nsubbands) + s->min_mono_subband = s->nsubbands; + + s->max_mono_subband = s->nsubbands * 14000 / (s->limited_rate / 2); + if (s->max_mono_subband > s->nsubbands) + s->max_mono_subband = s->nsubbands; + + // Handle change of sample rate + if ((old_rate != s->sample_rate || old_band_limit != s->band_limit) && init_sample_rate(s) < 0) + return AVERROR(ENOMEM); + + // Setup stereo downmix + if (s->flags & LBR_FLAG_DMIX_STEREO) { + DCAContext *dca = s->avctx->priv_data; + + if (s->nchannels_total < 3 || s->nchannels_total > DCA_LBR_CHANNELS_TOTAL - 2) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels for LBR stereo downmix\n"); + return AVERROR_INVALIDDATA; + } + + // This decoder doesn't support ECS chunk + if (dca->request_channel_layout != DCA_SPEAKER_LAYOUT_STEREO && !(s->warned & 4)) { + avpriv_report_missing_feature(s->avctx, "Embedded LBR stereo downmix"); + s->warned |= 4; + } + + // Account for extra downmixed channel pair + s->nchannels_total += 2; + s->nchannels = 2; + s->ch_mask = DCA_SPEAKER_PAIR_LR; + s->flags &= ~LBR_FLAG_LFE_PRESENT; + } + + // Handle change of sample rate or number of channels + if (old_rate != s->sample_rate + || old_band_limit != s->band_limit + || old_nchannels != s->nchannels) { + if (alloc_sample_buffer(s) < 0) + return AVERROR(ENOMEM); + ff_dca_lbr_flush(s); + } + + return 0; +} + +int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset) +{ + struct { + LBRChunk lfe; + LBRChunk tonal; + LBRChunk tonal_grp[5]; + LBRChunk grid1[DCA_LBR_CHANNELS / 2]; + LBRChunk hr_grid[DCA_LBR_CHANNELS / 2]; + LBRChunk ts1[DCA_LBR_CHANNELS / 2]; + LBRChunk ts2[DCA_LBR_CHANNELS / 2]; + } chunk = { }; + + GetByteContext gb; + + int i, ch, sb, sf, ret, group, chunk_id, chunk_len; + + bytestream2_init(&gb, data + asset->lbr_offset, asset->lbr_size); + + // LBR sync word + if (bytestream2_get_be32(&gb) != DCA_SYNCWORD_LBR) { + av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sync word\n"); + return AVERROR_INVALIDDATA; + } + + // LBR header type + switch (bytestream2_get_byte(&gb)) { + case LBR_HEADER_SYNC_ONLY: + if (!s->sample_rate) { + av_log(s->avctx, AV_LOG_ERROR, "LBR decoder not initialized\n"); + return AVERROR_INVALIDDATA; + } + break; + case LBR_HEADER_DECODER_INIT: + if ((ret = parse_decoder_init(s, &gb)) < 0) { + s->sample_rate = 0; + return ret; + } + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR header type\n"); + return AVERROR_INVALIDDATA; + } + + // LBR frame chunk header + chunk_id = bytestream2_get_byte(&gb); + chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb); + + if (chunk_len > bytestream2_get_bytes_left(&gb)) { + chunk_len = bytestream2_get_bytes_left(&gb); + av_log(s->avctx, AV_LOG_WARNING, "LBR frame chunk was truncated\n"); + if (s->avctx->err_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + } + + bytestream2_init(&gb, gb.buffer, chunk_len); + + switch (chunk_id & 0x7f) { + case LBR_CHUNK_FRAME: + if (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) { + int checksum = bytestream2_get_be16(&gb); + uint16_t res = chunk_id; + res += (chunk_len >> 8) & 0xff; + res += chunk_len & 0xff; + for (i = 0; i < chunk_len - 2; i++) + res += gb.buffer[i]; + if (checksum != res) { + av_log(s->avctx, AV_LOG_WARNING, "Invalid LBR checksum\n"); + if (s->avctx->err_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + } + } else { + bytestream2_skip(&gb, 2); + } + break; + case LBR_CHUNK_FRAME_NO_CSUM: + break; + default: + av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR frame chunk ID\n"); + return AVERROR_INVALIDDATA; + } + + // Clear current frame + memset(s->quant_levels, 0, sizeof(s->quant_levels)); + memset(s->sb_indices, 0xff, sizeof(s->sb_indices)); + memset(s->sec_ch_sbms, 0, sizeof(s->sec_ch_sbms)); + memset(s->sec_ch_lrms, 0, sizeof(s->sec_ch_lrms)); + memset(s->ch_pres, 0, sizeof(s->ch_pres)); + memset(s->grid_1_scf, 0, sizeof(s->grid_1_scf)); + memset(s->grid_2_scf, 0, sizeof(s->grid_2_scf)); + memset(s->grid_3_avg, 0, sizeof(s->grid_3_avg)); + memset(s->grid_3_scf, 0, sizeof(s->grid_3_scf)); + memset(s->grid_3_pres, 0, sizeof(s->grid_3_pres)); + memset(s->tonal_scf, 0, sizeof(s->tonal_scf)); + memset(s->lfe_data, 0, sizeof(s->lfe_data)); + s->part_stereo_pres = 0; + s->framenum = (s->framenum + 1) & 31; + + for (ch = 0; ch < s->nchannels; ch++) { + for (sb = 0; sb < s->nsubbands / 4; sb++) { + s->part_stereo[ch][sb][0] = s->part_stereo[ch][sb][4]; + s->part_stereo[ch][sb][4] = 16; + } + } + + memset(s->lpc_coeff[s->framenum & 1], 0, sizeof(s->lpc_coeff[0])); + + for (group = 0; group < 5; group++) { + for (sf = 0; sf < 1 << group; sf++) { + int sf_idx = ((s->framenum << group) + sf) & 31; + s->tonal_bounds[group][sf_idx][0] = + s->tonal_bounds[group][sf_idx][1] = s->ntones; + } + } + + // Parse chunk headers + while (bytestream2_get_bytes_left(&gb) > 0) { + chunk_id = bytestream2_get_byte(&gb); + chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb); + chunk_id &= 0x7f; + + if (chunk_len > bytestream2_get_bytes_left(&gb)) { + chunk_len = bytestream2_get_bytes_left(&gb); + av_log(s->avctx, AV_LOG_WARNING, "LBR chunk %#x was truncated\n", chunk_id); + if (s->avctx->err_recognition & AV_EF_EXPLODE) + return AVERROR_INVALIDDATA; + } + + switch (chunk_id) { + case LBR_CHUNK_LFE: + chunk.lfe.len = chunk_len; + chunk.lfe.data = gb.buffer; + break; + + case LBR_CHUNK_SCF: + case LBR_CHUNK_TONAL: + case LBR_CHUNK_TONAL_SCF: + chunk.tonal.id = chunk_id; + chunk.tonal.len = chunk_len; + chunk.tonal.data = gb.buffer; + break; + + case LBR_CHUNK_TONAL_GRP_1: + case LBR_CHUNK_TONAL_GRP_2: + case LBR_CHUNK_TONAL_GRP_3: + case LBR_CHUNK_TONAL_GRP_4: + case LBR_CHUNK_TONAL_GRP_5: + i = LBR_CHUNK_TONAL_GRP_5 - chunk_id; + chunk.tonal_grp[i].id = i; + chunk.tonal_grp[i].len = chunk_len; + chunk.tonal_grp[i].data = gb.buffer; + break; + + case LBR_CHUNK_TONAL_SCF_GRP_1: + case LBR_CHUNK_TONAL_SCF_GRP_2: + case LBR_CHUNK_TONAL_SCF_GRP_3: + case LBR_CHUNK_TONAL_SCF_GRP_4: + case LBR_CHUNK_TONAL_SCF_GRP_5: + i = LBR_CHUNK_TONAL_SCF_GRP_5 - chunk_id; + chunk.tonal_grp[i].id = i; + chunk.tonal_grp[i].len = chunk_len; + chunk.tonal_grp[i].data = gb.buffer; + break; + + case LBR_CHUNK_RES_GRID_LR: + case LBR_CHUNK_RES_GRID_LR + 1: + case LBR_CHUNK_RES_GRID_LR + 2: + i = chunk_id - LBR_CHUNK_RES_GRID_LR; + chunk.grid1[i].len = chunk_len; + chunk.grid1[i].data = gb.buffer; + break; + + case LBR_CHUNK_RES_GRID_HR: + case LBR_CHUNK_RES_GRID_HR + 1: + case LBR_CHUNK_RES_GRID_HR + 2: + i = chunk_id - LBR_CHUNK_RES_GRID_HR; + chunk.hr_grid[i].len = chunk_len; + chunk.hr_grid[i].data = gb.buffer; + break; + + case LBR_CHUNK_RES_TS_1: + case LBR_CHUNK_RES_TS_1 + 1: + case LBR_CHUNK_RES_TS_1 + 2: + i = chunk_id - LBR_CHUNK_RES_TS_1; + chunk.ts1[i].len = chunk_len; + chunk.ts1[i].data = gb.buffer; + break; + + case LBR_CHUNK_RES_TS_2: + case LBR_CHUNK_RES_TS_2 + 1: + case LBR_CHUNK_RES_TS_2 + 2: + i = chunk_id - LBR_CHUNK_RES_TS_2; + chunk.ts2[i].len = chunk_len; + chunk.ts2[i].data = gb.buffer; + break; + } + + bytestream2_skip(&gb, chunk_len); + } + + // Parse the chunks + ret = parse_lfe_chunk(s, &chunk.lfe); + + ret |= parse_tonal_chunk(s, &chunk.tonal); + + for (i = 0; i < 5; i++) + ret |= parse_tonal_group(s, &chunk.tonal_grp[i]); + + for (i = 0; i < (s->nchannels + 1) / 2; i++) { + int ch1 = i * 2; + int ch2 = FFMIN(ch1 + 1, s->nchannels - 1); + + if (parse_grid_1_chunk (s, &chunk.grid1 [i], ch1, ch2) < 0 || + parse_high_res_grid(s, &chunk.hr_grid[i], ch1, ch2) < 0) { + ret = -1; + continue; + } + + // TS chunks depend on both grids. TS_2 depends on TS_1. + if (!chunk.grid1[i].len || !chunk.hr_grid[i].len || !chunk.ts1[i].len) + continue; + + if (parse_ts1_chunk(s, &chunk.ts1[i], ch1, ch2) < 0 || + parse_ts2_chunk(s, &chunk.ts2[i], ch1, ch2) < 0) { + ret = -1; + continue; + } + } + + if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE)) + return AVERROR_INVALIDDATA; + + return 0; +} + +/** + * Reconstruct high-frequency resolution grid from first and third grids + */ +static void decode_grid(DCALbrDecoder *s, int ch1, int ch2) +{ + int i, ch, sb; + + for (ch = ch1; ch <= ch2; ch++) { + for (sb = 0; sb < s->nsubbands; sb++) { + int g1_sb = ff_dca_scf_to_grid_1[sb]; + + uint8_t *g1_scf_a = s->grid_1_scf[ch][g1_sb ]; + uint8_t *g1_scf_b = s->grid_1_scf[ch][g1_sb + 1]; + + int w1 = ff_dca_grid_1_weights[g1_sb ][sb]; + int w2 = ff_dca_grid_1_weights[g1_sb + 1][sb]; + + uint8_t *hr_scf = s->high_res_scf[ch][sb]; + + if (sb < 4) { + for (i = 0; i < 8; i++) { + int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i]; + hr_scf[i] = scf >> 7; + } + } else { + int8_t *g3_scf = s->grid_3_scf[ch][sb - 4]; + int g3_avg = s->grid_3_avg[ch][sb - 4]; + + for (i = 0; i < 8; i++) { + int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i]; + hr_scf[i] = (scf >> 7) - g3_avg - g3_scf[i]; + } + } + } + } +} + +/** + * Fill unallocated subbands with randomness + */ +static void random_ts(DCALbrDecoder *s, int ch1, int ch2) +{ + int i, j, k, ch, sb; + + for (ch = ch1; ch <= ch2; ch++) { + for (sb = 0; sb < s->nsubbands; sb++) { + float *samples = s->time_samples[ch][sb]; + + if (s->ch_pres[ch] & (1U << sb)) + continue; // Skip allocated subband + + if (sb < 2) { + // The first two subbands are always zero + memset(samples, 0, DCA_LBR_TIME_SAMPLES * sizeof(float)); + } else if (sb < 10) { + for (i = 0; i < DCA_LBR_TIME_SAMPLES; i++) + samples[i] = lbr_rand(s, sb); + } else { + for (i = 0; i < DCA_LBR_TIME_SAMPLES / 8; i++, samples += 8) { + float accum[8] = { 0 }; + + // Modulate by subbands 2-5 in blocks of 8 + for (k = 2; k < 6; k++) { + float *other = &s->time_samples[ch][k][i * 8]; + for (j = 0; j < 8; j++) + accum[j] += fabs(other[j]); + } + + for (j = 0; j < 8; j++) + samples[j] = (accum[j] * 0.25f + 0.5f) * lbr_rand(s, sb); + } + } + } + } +} + +static void predict(float *samples, const float *coeff, int nsamples) +{ + int i, j; + + for (i = 0; i < nsamples; i++) { + float res = 0; + for (j = 0; j < 8; j++) + res += coeff[j] * samples[i - j - 1]; + samples[i] -= res; + } +} + +static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb) +{ + int f = s->framenum & 1; + int ch; + + for (ch = ch1; ch <= ch2; ch++) { + float *samples = s->time_samples[ch][sb]; + + if (!(s->ch_pres[ch] & (1U << sb))) + continue; + + if (sb < 2) { + predict(samples, s->lpc_coeff[f^1][ch][sb][1], 16); + predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 64); + predict(samples + 80, s->lpc_coeff[f ][ch][sb][1], 48); + } else { + predict(samples, s->lpc_coeff[f^1][ch][sb][0], 16); + predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 112); + } + } +} + +static void filter_ts(DCALbrDecoder *s, int ch1, int ch2) +{ + int i, j, sb, ch; + + for (sb = 0; sb < s->nsubbands; sb++) { + // Scale factors + for (ch = ch1; ch <= ch2; ch++) { + float *samples = s->time_samples[ch][sb]; + uint8_t *hr_scf = s->high_res_scf[ch][sb]; + if (sb < 4) { + for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++, samples += 16) { + unsigned int scf = hr_scf[i]; + if (scf > AMP_MAX) + scf = AMP_MAX; + for (j = 0; j < 16; j++) + samples[j] *= ff_dca_quant_amp[scf]; + } + } else { + uint8_t *g2_scf = s->grid_2_scf[ch][ff_dca_scf_to_grid_2[sb]]; + for (i = 0; i < DCA_LBR_TIME_SAMPLES / 2; i++, samples += 2) { + unsigned int scf = hr_scf[i / 8] - g2_scf[i]; + if (scf > AMP_MAX) + scf = AMP_MAX; + samples[0] *= ff_dca_quant_amp[scf]; + samples[1] *= ff_dca_quant_amp[scf]; + } + } + } + + // Mid-side stereo + if (ch1 != ch2) { + float *samples_l = s->time_samples[ch1][sb]; + float *samples_r = s->time_samples[ch2][sb]; + int ch2_pres = s->ch_pres[ch2] & (1U << sb); + + for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++) { + int sbms = (s->sec_ch_sbms[ch1 / 2][sb] >> i) & 1; + int lrms = (s->sec_ch_lrms[ch1 / 2][sb] >> i) & 1; + + if (sb >= s->min_mono_subband) { + if (lrms && ch2_pres) { + if (sbms) { + for (j = 0; j < 16; j++) { + float tmp = samples_l[j]; + samples_l[j] = samples_r[j]; + samples_r[j] = -tmp; + } + } else { + for (j = 0; j < 16; j++) { + float tmp = samples_l[j]; + samples_l[j] = samples_r[j]; + samples_r[j] = tmp; + } + } + } else if (!ch2_pres) { + if (sbms && (s->part_stereo_pres & (1 << ch1))) { + for (j = 0; j < 16; j++) + samples_r[j] = -samples_l[j]; + } else { + for (j = 0; j < 16; j++) + samples_r[j] = samples_l[j]; + } + } + } else if (sbms && ch2_pres) { + for (j = 0; j < 16; j++) { + float tmp = samples_l[j]; + samples_l[j] = (tmp + samples_r[j]) * 0.5f; + samples_r[j] = (tmp - samples_r[j]) * 0.5f; + } + } + + samples_l += 16; + samples_r += 16; + } + } + + // Inverse prediction + if (sb < 3) + synth_lpc(s, ch1, ch2, sb); + } +} + +/** + * Modulate by interpolated partial stereo coefficients + */ +static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2) +{ + int i, ch, sb, sf; + + for (ch = ch1; ch <= ch2; ch++) { + for (sb = s->min_mono_subband; sb < s->nsubbands; sb++) { + uint8_t *pt_st = s->part_stereo[ch][(sb - s->min_mono_subband) / 4]; + float *samples = s->time_samples[ch][sb]; + + if (s->ch_pres[ch2] & (1U << sb)) + continue; + + for (sf = 1; sf <= 4; sf++, samples += 32) { + float prev = ff_dca_st_coeff[pt_st[sf - 1]]; + float next = ff_dca_st_coeff[pt_st[sf ]]; + + for (i = 0; i < 32; i++) + samples[i] *= (32 - i) * prev + i * next; + } + } + } +} + +/** + * Synthesise tones in the given group for the given tonal subframe + */ +static void synth_tones(DCALbrDecoder *s, int ch, float *values, + int group, int group_sf, int synth_idx) +{ + int i, start, count; + + if (synth_idx < 0) + return; + + start = s->tonal_bounds[group][group_sf][0]; + count = (s->tonal_bounds[group][group_sf][1] - start) & (DCA_LBR_TONES - 1); + + for (i = 0; i < count; i++) { + DCALbrTone *t = &s->tones[(start + i) & (DCA_LBR_TONES - 1)]; + + if (t->amp[ch]) { + float amp = ff_dca_synth_env[synth_idx] * ff_dca_quant_amp[t->amp[ch]]; + float c = amp * cos_tab[(t->phs[ch] ) & 255]; + float s = amp * cos_tab[(t->phs[ch] + 64) & 255]; + const float *cf = ff_dca_corr_cf[t->f_delt]; + int x_freq = t->x_freq; + + switch (x_freq) { + case 0: + goto p0; + case 1: + values[3] += cf[0] * -s; + values[2] += cf[1] * c; + values[1] += cf[2] * s; + values[0] += cf[3] * -c; + goto p1; + case 2: + values[2] += cf[0] * -s; + values[1] += cf[1] * c; + values[0] += cf[2] * s; + goto p2; + case 3: + values[1] += cf[0] * -s; + values[0] += cf[1] * c; + goto p3; + case 4: + values[0] += cf[0] * -s; + goto p4; + } + + values[x_freq - 5] += cf[ 0] * -s; + p4: values[x_freq - 4] += cf[ 1] * c; + p3: values[x_freq - 3] += cf[ 2] * s; + p2: values[x_freq - 2] += cf[ 3] * -c; + p1: values[x_freq - 1] += cf[ 4] * -s; + p0: values[x_freq ] += cf[ 5] * c; + values[x_freq + 1] += cf[ 6] * s; + values[x_freq + 2] += cf[ 7] * -c; + values[x_freq + 3] += cf[ 8] * -s; + values[x_freq + 4] += cf[ 9] * c; + values[x_freq + 5] += cf[10] * s; + } + + t->phs[ch] += t->ph_rot; + } +} + +/** + * Synthesise all tones in all groups for the given residual subframe + */ +static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf) +{ + int group; + + // Tonal vs residual shift is 22 subframes + for (group = 0; group < 5; group++) { + int group_sf = (s->framenum << group) + ((sf - 22) >> (5 - group)); + int synth_idx = ((((sf - 22) & 31) << group) & 31) + (1 << group) - 1; + + synth_tones(s, ch, values, group, (group_sf - 1) & 31, 30 - synth_idx); + synth_tones(s, ch, values, group, (group_sf ) & 31, synth_idx); + } +} + +static void transform_channel(DCALbrDecoder *s, int ch, float *output) +{ + LOCAL_ALIGNED_32(float, values, [DCA_LBR_SUBBANDS ], [4]); + LOCAL_ALIGNED_32(float, result, [DCA_LBR_SUBBANDS * 2], [4]); + int sf, sb, nsubbands = s->nsubbands, noutsubbands = 8 << s->freq_range; + + // Clear inactive subbands + if (nsubbands < noutsubbands) + memset(values[nsubbands], 0, (noutsubbands - nsubbands) * sizeof(values[0])); + + for (sf = 0; sf < DCA_LBR_TIME_SAMPLES / 4; sf++) { + // Hybrid filterbank + s->dcadsp->lbr_bank(values, s->time_samples[ch], + ff_dca_bank_coeff, sf * 4, nsubbands); + + base_func_synth(s, ch, values[0], sf); + + s->imdct.imdct_calc(&s->imdct, result[0], values[0]); + + // Long window and overlap-add + s->fdsp->vector_fmul_add(output, result[0], s->window, + s->history[ch], noutsubbands * 4); + s->fdsp->vector_fmul_reverse(s->history[ch], result[noutsubbands], + s->window, noutsubbands * 4); + output += noutsubbands * 4; + } + + // Update history for LPC and forward MDCT + for (sb = 0; sb < nsubbands; sb++) { + float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY; + memcpy(samples, samples + DCA_LBR_TIME_SAMPLES, DCA_LBR_TIME_HISTORY * sizeof(float)); + } +} + +int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame) +{ + AVCodecContext *avctx = s->avctx; + int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1; + const int8_t *reorder; + + avctx->channel_layout = channel_layouts[ch_conf]; + avctx->channels = nchannels = channel_counts[ch_conf]; + avctx->sample_rate = s->sample_rate; + avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; + avctx->bits_per_raw_sample = 0; + avctx->profile = FF_PROFILE_DTS_EXPRESS; + avctx->bit_rate = s->bit_rate_scaled; + + if (s->flags & LBR_FLAG_LFE_PRESENT) { + avctx->channel_layout |= AV_CH_LOW_FREQUENCY; + avctx->channels++; + reorder = channel_reorder_lfe[ch_conf]; + } else { + reorder = channel_reorder_nolfe[ch_conf]; + } + + frame->nb_samples = 1024 << s->freq_range; + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) + return ret; + + // Filter fullband channels + for (i = 0; i < (s->nchannels + 1) / 2; i++) { + int ch1 = i * 2; + int ch2 = FFMIN(ch1 + 1, s->nchannels - 1); + + decode_grid(s, ch1, ch2); + + random_ts(s, ch1, ch2); + + filter_ts(s, ch1, ch2); + + if (ch1 != ch2 && (s->part_stereo_pres & (1 << ch1))) + decode_part_stereo(s, ch1, ch2); + + if (ch1 < nchannels) + transform_channel(s, ch1, (float *)frame->extended_data[reorder[ch1]]); + + if (ch1 != ch2 && ch2 < nchannels) + transform_channel(s, ch2, (float *)frame->extended_data[reorder[ch2]]); + } + + // Interpolate LFE channel + if (s->flags & LBR_FLAG_LFE_PRESENT) { + s->dcadsp->lfe_iir((float *)frame->extended_data[lfe_index[ch_conf]], + s->lfe_data, ff_dca_lfe_iir, + s->lfe_history, 16 << s->freq_range); + } + + if ((ret = ff_side_data_update_matrix_encoding(frame, AV_MATRIX_ENCODING_NONE)) < 0) + return ret; + + return 0; +} + +av_cold void ff_dca_lbr_flush(DCALbrDecoder *s) +{ + int ch, sb; + + if (!s->sample_rate) + return; + + // Clear history + memset(s->part_stereo, 16, sizeof(s->part_stereo)); + memset(s->lpc_coeff, 0, sizeof(s->lpc_coeff)); + memset(s->history, 0, sizeof(s->history)); + memset(s->tonal_bounds, 0, sizeof(s->tonal_bounds)); + memset(s->lfe_history, 0, sizeof(s->lfe_history)); + s->framenum = 0; + s->ntones = 0; + + for (ch = 0; ch < s->nchannels; ch++) { + for (sb = 0; sb < s->nsubbands; sb++) { + float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY; + memset(samples, 0, DCA_LBR_TIME_HISTORY * sizeof(float)); + } + } +} + +av_cold int ff_dca_lbr_init(DCALbrDecoder *s) +{ + init_tables(); + + if (!(s->fdsp = avpriv_float_dsp_alloc(0))) + return -1; + + s->lbr_rand = 1; + return 0; +} + +av_cold void ff_dca_lbr_close(DCALbrDecoder *s) +{ + s->sample_rate = 0; + + av_freep(&s->ts_buffer); + s->ts_size = 0; + + av_freep(&s->fdsp); + ff_mdct_end(&s->imdct); +} diff --git a/libavcodec/dca_lbr.h b/libavcodec/dca_lbr.h new file mode 100644 index 0000000000..e6ca8057e9 --- /dev/null +++ b/libavcodec/dca_lbr.h @@ -0,0 +1,130 @@ +/* + * 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 + */ + +#ifndef AVCODEC_DCA_LBR_H +#define AVCODEC_DCA_LBR_H + +#include "libavutil/common.h" +#include "libavutil/float_dsp.h" +#include "libavutil/mem.h" + +#include "avcodec.h" +#include "internal.h" +#include "get_bits.h" +#include "dca.h" +#include "dca_exss.h" +#include "dcadsp.h" +#include "fft.h" + +#define DCA_LBR_CHANNELS 6 +#define DCA_LBR_CHANNELS_TOTAL 32 +#define DCA_LBR_SUBBANDS 32 +#define DCA_LBR_TONES 512 + +#define DCA_LBR_TIME_SAMPLES 128 +#define DCA_LBR_TIME_HISTORY 8 + +typedef struct DCALbrTone { + uint8_t x_freq; ///< Spectral line offset + uint8_t f_delt; ///< Difference between original and center frequency + uint8_t ph_rot; ///< Phase rotation + uint8_t pad; ///< Padding field + uint8_t amp[DCA_LBR_CHANNELS]; ///< Per-channel amplitude + uint8_t phs[DCA_LBR_CHANNELS]; ///< Per-channel phase +} DCALbrTone; + +typedef struct DCALbrDecoder { + AVCodecContext *avctx; + GetBitContext gb; + + int sample_rate; ///< Sample rate of LBR audio + int ch_mask; ///< LBR speaker mask + int flags; ///< Flags for LBR decoder initialization + int bit_rate_orig; ///< Original bit rate + int bit_rate_scaled; ///< Scaled bit rate + + int nchannels; ///< Number of fullband channels to decode + int nchannels_total; ///< Total number of fullband channels + int freq_range; ///< Frequency range of LBR audio + int band_limit; ///< Band limit factor + int limited_rate; ///< Band limited sample rate + int limited_range; ///< Band limited frequency range + int res_profile; ///< Resolution profile + int nsubbands; ///< Number of encoded subbands + int g3_avg_only_start_sb; ///< Subband index where grid 3 scale factors end + int min_mono_subband; ///< Subband index where mono encoding starts + int max_mono_subband; ///< Subband index where mono encoding ends + + int framenum; ///< Lower 5 bits of current frame number + int lbr_rand; ///< Seed for subband randomization + int warned; ///< Flags for warning suppression + + uint8_t quant_levels[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS]; ///< Quantization levels + uint8_t sb_indices[DCA_LBR_SUBBANDS]; ///< Subband reordering indices + + uint8_t sec_ch_sbms[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS]; ///< Right channel inversion or mid/side decoding flags + uint8_t sec_ch_lrms[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS]; ///< Flags indicating if left/right channel are swapped + uint32_t ch_pres[DCA_LBR_CHANNELS]; ///< Subband allocation flags + + uint8_t grid_1_scf[DCA_LBR_CHANNELS][12][8]; ///< Grid 1 scale factors + uint8_t grid_2_scf[DCA_LBR_CHANNELS][3][64]; ///< Grid 2 scale factors + + int8_t grid_3_avg[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS - 4]; ///< Grid 3 average values + int8_t grid_3_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS - 4][8]; ///< Grid 3 scale factors + uint32_t grid_3_pres[DCA_LBR_CHANNELS]; ///< Grid 3 scale factors presence flags + + uint8_t high_res_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS][8]; ///< High-frequency resolution scale factors + + uint8_t part_stereo[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS / 4][5]; ///< Partial stereo coefficients + uint8_t part_stereo_pres; ///< Partial stereo coefficients presence flags + + float lpc_coeff[2][DCA_LBR_CHANNELS][3][2][8]; ///< Predictor coefficients + + float sb_scf[DCA_LBR_SUBBANDS]; ///< Subband randomization scale factors + + float *time_samples[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS]; ///< Time samples + + float *ts_buffer; ///< Time sample buffer base + unsigned int ts_size; ///< Time sample buffer size + + DECLARE_ALIGNED(32, float, history)[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS * 4]; ///< IMDCT history + DECLARE_ALIGNED(32, float, window)[DCA_LBR_SUBBANDS * 4]; ///< Long window for IMDCT + + DECLARE_ALIGNED(32, float, lfe_data)[64]; ///< Decimated LFE samples + DECLARE_ALIGNED(32, float, lfe_history)[5][2]; ///< LFE IIR filter history + float lfe_scale; ///< Scale factor of LFE samples before IIR filter + + uint8_t tonal_scf[6]; ///< Tonal scale factors + uint16_t tonal_bounds[5][32][2]; ///< Per-group per-subframe start/end positions of tones + DCALbrTone tones[DCA_LBR_TONES]; ///< Circular buffer of tones + int ntones; ///< Circular buffer head position + + FFTContext imdct; + AVFloatDSPContext *fdsp; + DCADSPContext *dcadsp; +} DCALbrDecoder; + +int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset); +int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame); +av_cold void ff_dca_lbr_flush(DCALbrDecoder *s); +av_cold int ff_dca_lbr_init(DCALbrDecoder *s); +av_cold void ff_dca_lbr_close(DCALbrDecoder *s); + +#endif diff --git a/libavcodec/dcadata.c b/libavcodec/dcadata.c index 122db185c7..99e5311991 100644 --- a/libavcodec/dcadata.c +++ b/libavcodec/dcadata.c @@ -8729,3 +8729,469 @@ const int32_t ff_dca_sampling_freqs[16] = { 8000, 16000, 32000, 64000, 128000, 22050, 44100, 88200, 176400, 352800, 12000, 24000, 48000, 96000, 192000, 384000, }; + +const uint16_t ff_dca_avg_g3_freqs[3] = { 16000, 18000, 24000 }; + +const uint16_t ff_dca_fst_amp[44] = { + 0, 1, 2, 3, + 4, 6, 8, 10, + 12, 16, 20, 24, + 28, 36, 44, 52, + 60, 76, 92, 108, + 124, 156, 188, 220, + 252, 316, 380, 444, + 508, 636, 764, 892, + 1020, 1276, 1532, 1788, + 2044, 2556, 3068, 3580, + 4092, 5116, 6140, 7164 +}; + +const uint8_t ff_dca_freq_to_sb[32] = { + 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 +}; + +const int8_t ff_dca_ph0_shift[8] = { + -32, +96, -96, +32, +96, -32, +32, -96 +}; + +const uint8_t ff_dca_grid_1_to_scf[11] = { + 0, 1, 2, 3, 4, 6, 7, 10, 14, 19, 26 +}; + +const uint8_t ff_dca_grid_2_to_scf[3] = { + 4, 10, 18 +}; + +const uint8_t ff_dca_scf_to_grid_1[32] = { + 0, 1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, + 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10 +}; + +const uint8_t ff_dca_scf_to_grid_2[32] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, + 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 +}; + +const uint8_t ff_dca_grid_1_weights[12][32] = { + { + 128, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 128, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 128, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 128, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 128, 128, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 128, 85, + 43, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 43, + 85, 128, 96, 64, 32, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 32, 64, 96, 128, 102, 77, + 51, 26, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 26, 51, + 77, 102, 128, 107, 85, 64, 43, 21, + 0, 0, 0, 0, 0, 0, 0, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 21, 43, 64, 85, 107, + 128, 110, 91, 73, 55, 37, 18, 0, + }, { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 18, 37, 55, 73, 91, 110, 128, + }, { + /* empty */ + } +}; + +const uint8_t ff_dca_sb_reorder[8][8] = { + { 0, 1, 2, 3, 4, 5, 6, 7 }, + { 1, 0, 2, 3, 4, 5, 6, 7 }, + { 3, 1, 0, 2, 4, 5, 6, 7 }, + { 1, 2, 3, 0, 4, 5, 6, 7 }, + { 1, 2, 5, 3, 0, 4, 6, 7 }, + { 1, 2, 2, 5, 3, 0, 4, 6 }, + { 1, 2, 2, 6, 5, 3, 0, 4 }, + { 1, 2, 2, 6, 5, 4, 0, 3 } +}; + +const int8_t ff_dca_lfe_delta_index_16[8] = { + -4, -3, -2, -1, 2, 4, 6, 8 +}; + +const int8_t ff_dca_lfe_delta_index_24[32] = { + -8, -8, -7, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, + 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8 +}; + +const uint16_t ff_dca_rsd_pack_5_in_8[256] = { + 0x0000, 0x0100, 0x0200, 0x0040, 0x0140, 0x0240, 0x0080, 0x0180, + 0x0280, 0x0010, 0x0110, 0x0210, 0x0050, 0x0150, 0x0250, 0x0090, + 0x0190, 0x0290, 0x0020, 0x0120, 0x0220, 0x0060, 0x0160, 0x0260, + 0x00a0, 0x01a0, 0x02a0, 0x0004, 0x0104, 0x0204, 0x0044, 0x0144, + 0x0244, 0x0084, 0x0184, 0x0284, 0x0014, 0x0114, 0x0214, 0x0054, + 0x0154, 0x0254, 0x0094, 0x0194, 0x0294, 0x0024, 0x0124, 0x0224, + 0x0064, 0x0164, 0x0264, 0x00a4, 0x01a4, 0x02a4, 0x0008, 0x0108, + 0x0208, 0x0048, 0x0148, 0x0248, 0x0088, 0x0188, 0x0288, 0x0018, + 0x0118, 0x0218, 0x0058, 0x0158, 0x0258, 0x0098, 0x0198, 0x0298, + 0x0028, 0x0128, 0x0228, 0x0068, 0x0168, 0x0268, 0x00a8, 0x01a8, + 0x02a8, 0x0001, 0x0101, 0x0201, 0x0041, 0x0141, 0x0241, 0x0081, + 0x0181, 0x0281, 0x0011, 0x0111, 0x0211, 0x0051, 0x0151, 0x0251, + 0x0091, 0x0191, 0x0291, 0x0021, 0x0121, 0x0221, 0x0061, 0x0161, + 0x0261, 0x00a1, 0x01a1, 0x02a1, 0x0005, 0x0105, 0x0205, 0x0045, + 0x0145, 0x0245, 0x0085, 0x0185, 0x0285, 0x0015, 0x0115, 0x0215, + 0x0055, 0x0155, 0x0255, 0x0095, 0x0195, 0x0295, 0x0025, 0x0125, + 0x0225, 0x0065, 0x0165, 0x0265, 0x00a5, 0x01a5, 0x02a5, 0x0009, + 0x0109, 0x0209, 0x0049, 0x0149, 0x0249, 0x0089, 0x0189, 0x0289, + 0x0019, 0x0119, 0x0219, 0x0059, 0x0159, 0x0259, 0x0099, 0x0199, + 0x0299, 0x0029, 0x0129, 0x0229, 0x0069, 0x0169, 0x0269, 0x00a9, + 0x01a9, 0x02a9, 0x0002, 0x0102, 0x0202, 0x0042, 0x0142, 0x0242, + 0x0082, 0x0182, 0x0282, 0x0012, 0x0112, 0x0212, 0x0052, 0x0152, + 0x0252, 0x0092, 0x0192, 0x0292, 0x0022, 0x0122, 0x0222, 0x0062, + 0x0162, 0x0262, 0x00a2, 0x01a2, 0x02a2, 0x0006, 0x0106, 0x0206, + 0x0046, 0x0146, 0x0246, 0x0086, 0x0186, 0x0286, 0x0016, 0x0116, + 0x0216, 0x0056, 0x0156, 0x0256, 0x0096, 0x0196, 0x0296, 0x0026, + 0x0126, 0x0226, 0x0066, 0x0166, 0x0266, 0x00a6, 0x01a6, 0x02a6, + 0x000a, 0x010a, 0x020a, 0x004a, 0x014a, 0x024a, 0x008a, 0x018a, + 0x028a, 0x001a, 0x011a, 0x021a, 0x005a, 0x015a, 0x025a, 0x009a, + 0x019a, 0x029a, 0x002a, 0x012a, 0x022a, 0x006a, 0x016a, 0x026a, + 0x00aa, 0x01aa, 0x02aa, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155, + 0x0155, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155 +}; + +const uint8_t ff_dca_rsd_pack_3_in_7[128][3] = { + { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, { 0, 0, 3 }, + { 0, 0, 4 }, { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, + { 0, 1, 3 }, { 0, 1, 4 }, { 0, 2, 0 }, { 0, 2, 1 }, + { 0, 2, 2 }, { 0, 2, 3 }, { 0, 2, 4 }, { 0, 3, 0 }, + { 0, 3, 1 }, { 0, 3, 2 }, { 0, 3, 3 }, { 0, 3, 4 }, + { 0, 4, 0 }, { 0, 4, 1 }, { 0, 4, 2 }, { 0, 4, 3 }, + { 0, 4, 4 }, { 1, 0, 0 }, { 1, 0, 1 }, { 1, 0, 2 }, + { 1, 0, 3 }, { 1, 0, 4 }, { 1, 1, 0 }, { 1, 1, 1 }, + { 1, 1, 2 }, { 1, 1, 3 }, { 1, 1, 4 }, { 1, 2, 0 }, + { 1, 2, 1 }, { 1, 2, 2 }, { 1, 2, 3 }, { 1, 2, 4 }, + { 1, 3, 0 }, { 1, 3, 1 }, { 1, 3, 2 }, { 1, 3, 3 }, + { 1, 3, 4 }, { 1, 4, 0 }, { 1, 4, 1 }, { 1, 4, 2 }, + { 1, 4, 3 }, { 1, 4, 4 }, { 2, 0, 0 }, { 2, 0, 1 }, + { 2, 0, 2 }, { 2, 0, 3 }, { 2, 0, 4 }, { 2, 1, 0 }, + { 2, 1, 1 }, { 2, 1, 2 }, { 2, 1, 3 }, { 2, 1, 4 }, + { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, { 2, 2, 3 }, + { 2, 2, 4 }, { 2, 3, 0 }, { 2, 3, 1 }, { 2, 3, 2 }, + { 2, 3, 3 }, { 2, 3, 4 }, { 2, 4, 0 }, { 2, 4, 1 }, + { 2, 4, 2 }, { 2, 4, 3 }, { 2, 4, 4 }, { 3, 0, 0 }, + { 3, 0, 1 }, { 3, 0, 2 }, { 3, 0, 3 }, { 3, 0, 4 }, + { 3, 1, 0 }, { 3, 1, 1 }, { 3, 1, 2 }, { 3, 1, 3 }, + { 3, 1, 4 }, { 3, 2, 0 }, { 3, 2, 1 }, { 3, 2, 2 }, + { 3, 2, 3 }, { 3, 2, 4 }, { 3, 3, 0 }, { 3, 3, 1 }, + { 3, 3, 2 }, { 3, 3, 3 }, { 3, 3, 4 }, { 3, 4, 0 }, + { 3, 4, 1 }, { 3, 4, 2 }, { 3, 4, 3 }, { 3, 4, 4 }, + { 4, 0, 0 }, { 4, 0, 1 }, { 4, 0, 2 }, { 4, 0, 3 }, + { 4, 0, 4 }, { 4, 1, 0 }, { 4, 1, 1 }, { 4, 1, 2 }, + { 4, 1, 3 }, { 4, 1, 4 }, { 4, 2, 0 }, { 4, 2, 1 }, + { 4, 2, 2 }, { 4, 2, 3 }, { 4, 2, 4 }, { 4, 3, 0 }, + { 4, 3, 1 }, { 4, 3, 2 }, { 4, 3, 3 }, { 4, 3, 4 }, + { 4, 4, 0 }, { 4, 4, 1 }, { 4, 4, 2 }, { 4, 4, 3 }, + { 4, 4, 4 }, { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 } +}; + +const float ff_dca_rsd_level_2a[2] = { + -0.47, 0.47 +}; + +const float ff_dca_rsd_level_2b[2] = { + -0.645, 0.645 +}; + +const float ff_dca_rsd_level_3[3] = { + -0.645, 0.0, 0.645 +}; + +const float ff_dca_rsd_level_5[5] = { + -0.875, -0.375, 0.0, 0.375, 0.875 +}; + +const float ff_dca_rsd_level_8[8] = { + -1.0, -0.625, -0.291666667, 0.0, 0.25, 0.5, 0.75, 1.0 +}; + +const float ff_dca_rsd_level_16[16] = { + -1.3125, -1.1375, -0.9625, -0.7875, + -0.6125, -0.4375, -0.2625, -0.0875, + 0.0875, 0.2625, 0.4375, 0.6125, + 0.7875, 0.9625, 1.1375, 1.3125 +}; + +const float ff_dca_synth_env[32] = { + 0.00240763666390, 0.00960735979838, 0.02152983213390, 0.03806023374436, + 0.05903936782582, 0.08426519384873, 0.11349477331863, 0.14644660940673, + 0.18280335791818, 0.22221488349020, 0.26430163158700, 0.30865828381746, + 0.35485766137277, 0.40245483899194, 0.45099142983522, 0.5, + 0.54900857016478, 0.59754516100806, 0.64514233862723, 0.69134171618254, + 0.73569836841300, 0.77778511650980, 0.81719664208182, 0.85355339059327, + 0.88650522668137, 0.91573480615127, 0.94096063217418, 0.96193976625564, + 0.97847016786610, 0.99039264020162, 0.99759236333610, 1.0 +}; + +const float ff_dca_corr_cf[32][11] = { + {-0.01179, 0.04281, 0.46712, 0.46345,-3.94525, 3.94525, + -0.46345,-0.46712,-0.04281, 0.01179,-0.00299 }, + {-0.00929, 0.04882, 0.45252, 0.37972,-3.85446, 4.03189, + -0.55069,-0.48040,-0.03599, 0.01445,-0.00229 }, + {-0.00696, 0.05403, 0.43674, 0.29961,-3.75975, 4.11413, + -0.64135,-0.49221,-0.02834, 0.01726,-0.00156 }, + {-0.00481, 0.05847, 0.41993, 0.22319,-3.66138, 4.19175, + -0.73529,-0.50241,-0.01983, 0.02021,-0.00080 }, + {-0.00284, 0.06216, 0.40224, 0.15053,-3.55963, 4.26452, + -0.83239,-0.51085,-0.01047, 0.02328,-0.00003 }, + {-0.00105, 0.06515, 0.38378, 0.08168,-3.45475, 4.33225, + -0.93249,-0.51738,-0.00024, 0.02646, 0.00074 }, + { 0.00054, 0.06745, 0.36471, 0.01668,-3.34703, 4.39475, + -1.03543,-0.52184, 0.01085, 0.02973, 0.00152 }, + { 0.00195, 0.06912, 0.34515,-0.04445,-3.23676, 4.45185, + -1.14105,-0.52410, 0.02280, 0.03306, 0.00228 }, + { 0.00318, 0.07017, 0.32521,-0.10168,-3.12422, 4.50339, + -1.24914,-0.52400, 0.03561, 0.03643, 0.00302 }, + { 0.00422, 0.07065, 0.30503,-0.15503,-3.00969, 4.54921, + -1.35952,-0.52141, 0.04925, 0.03981, 0.00373 }, + { 0.00508, 0.07061, 0.28471,-0.20450,-2.89348, 4.58919, + -1.47197,-0.51618, 0.06370, 0.04319, 0.00440 }, + { 0.00577, 0.07007, 0.26436,-0.25013,-2.77587, 4.62321, + -1.58627,-0.50818, 0.07895, 0.04652, 0.00501 }, + { 0.00629, 0.06909, 0.24410,-0.29194,-2.65716, 4.65118, + -1.70219,-0.49727, 0.09494, 0.04979, 0.00556 }, + { 0.00666, 0.06769, 0.22400,-0.33000,-2.53764, 4.67302, + -1.81949,-0.48335, 0.11166, 0.05295, 0.00604 }, + { 0.00687, 0.06592, 0.20416,-0.36435,-2.41760, 4.68866, + -1.93791,-0.46627, 0.12904, 0.05597, 0.00642 }, + { 0.00694, 0.06383, 0.18468,-0.39506,-2.29732, 4.69806, + -2.05720,-0.44593, 0.14705, 0.05881, 0.00671 }, + { 0.00689, 0.06144, 0.16561,-0.42223,-2.17710, 4.70120, + -2.17710,-0.42223, 0.16561, 0.06144, 0.00689 }, + { 0.00671, 0.05881, 0.14705,-0.44593,-2.05720, 4.69806, + -2.29732,-0.39506, 0.18468, 0.06383, 0.00694 }, + { 0.00642, 0.05597, 0.12904,-0.46627,-1.93791, 4.68865, + -2.41759,-0.36435, 0.20416, 0.06592, 0.00687 }, + { 0.00604, 0.05295, 0.11166,-0.48334,-1.81949, 4.67301, + -2.53763,-0.33000, 0.22400, 0.06769, 0.00666 }, + { 0.00556, 0.04979, 0.09494,-0.49727,-1.70219, 4.65117, + -2.65715,-0.29194, 0.24409, 0.06909, 0.00629 }, + { 0.00501, 0.04652, 0.07894,-0.50818,-1.58627, 4.62321, + -2.77587,-0.25013, 0.26436, 0.07007, 0.00577 }, + { 0.00440, 0.04319, 0.06370,-0.51618,-1.47197, 4.58919, + -2.89348,-0.20450, 0.28471, 0.07061, 0.00508 }, + { 0.00373, 0.03981, 0.04925,-0.52141,-1.35952, 4.54921, + -3.00970,-0.15503, 0.30503, 0.07065, 0.00422 }, + { 0.00302, 0.03643, 0.03561,-0.52400,-1.24915, 4.50339, + -3.12422,-0.10168, 0.32521, 0.07017, 0.00318 }, + { 0.00228, 0.03306, 0.02280,-0.52410,-1.14105, 4.45186, + -3.23677,-0.04445, 0.34515, 0.06912, 0.00195 }, + { 0.00152, 0.02973, 0.01085,-0.52184,-1.03544, 4.39477, + -3.34704, 0.01668, 0.36471, 0.06745, 0.00054 }, + { 0.00074, 0.02646,-0.00024,-0.51738,-0.93249, 4.33226, + -3.45476, 0.08168, 0.38378, 0.06515,-0.00105 }, + {-0.00003, 0.02328,-0.01047,-0.51085,-0.83239, 4.26452, + -3.55963, 0.15053, 0.40224, 0.06216,-0.00284 }, + {-0.00080, 0.02021,-0.01983,-0.50241,-0.73529, 4.19174, + -3.66138, 0.22319, 0.41993, 0.05847,-0.00481 }, + {-0.00156, 0.01726,-0.02834,-0.49221,-0.64135, 4.11413, + -3.75974, 0.29961, 0.43674, 0.05403,-0.00696 }, + {-0.00229, 0.01445,-0.03599,-0.48040,-0.55069, 4.03188, + -3.85445, 0.37972, 0.45251, 0.04882,-0.00929 }, +}; + +const float ff_dca_quant_amp[57] = { + 4.88281250E-04, 1.46484375E-03, 2.32267031E-03, 3.28475167E-03, + 4.64534014E-03, 6.56950334E-03, 9.29068029E-03, 1.31390067E-02, + 1.85813606E-02, 2.62780134E-02, 3.71627212E-02, 5.25560267E-02, + 7.43254423E-02, 1.05112053E-01, 1.48650885E-01, 2.10224107E-01, + 2.97301769E-01, 4.20448214E-01, 5.94603539E-01, 8.40896428E-01, + 1.18920708E+00, 1.68179286E+00, 2.37841415E+00, 3.36358571E+00, + 4.75682831E+00, 6.72717142E+00, 9.51365662E+00, 1.34543428E+01, + 1.90273132E+01, 2.69086857E+01, 3.80546265E+01, 5.38173714E+01, + 7.61092529E+01, 1.07634743E+02, 1.52218506E+02, 2.15269485E+02, + 3.04437012E+02, 4.30538971E+02, 6.08874023E+02, 8.61077942E+02, + 1.21774805E+03, 1.72215588E+03, 2.43549609E+03, 3.44431177E+03, + 4.87099219E+03, 6.88862354E+03, 9.74198438E+03, 1.37772471E+04, + 1.94839688E+04, 2.75544941E+04, 3.89679375E+04, 5.51089883E+04, + 7.79358750E+04, 1.10217977E+05, 1.55871750E+05, 2.20435953E+05, + 0.00000000E+00, +}; + +const float ff_dca_st_coeff[34] = { + 2.69086857E+01, 2.69086857E+01, 1.34543419E+01, 6.72717142E+00, + 3.36358571E+00, 1.68179286E+00, 8.40896428E-01, 5.94603479E-01, + 4.20448214E-01, 2.97301799E-01, 2.10224107E-01, 1.48650900E-01, + 1.05112098E-01, 7.43253976E-02, 5.25560006E-02, 3.71626988E-02, + 3.12500000E-02, 2.62780003E-02, 1.85813997E-02, 1.31390002E-02, + 9.29069985E-03, 6.56950008E-03, 4.64530010E-03, 3.28480010E-03, + 2.32270011E-03, 1.64240005E-03, 1.16130000E-03, 5.80699998E-04, + 2.90299999E-04, 1.45200000E-04, 7.25999998E-05, 3.62999999E-05, + 1.82000003E-05, 0.00000000E+00, +}; + +const float ff_dca_long_window[128] = { + 0.00000000E+00, 7.42882412E-06, 5.28020973E-05, 1.71007006E-04, + 3.96653224E-04, 7.63946096E-04, 1.30655791E-03, 2.05750111E-03, + 3.04900459E-03, 4.31239139E-03, 5.87796280E-03, 7.77488295E-03, + 1.00310687E-02, 1.26730874E-02, 1.57260559E-02, 1.92135461E-02, + 2.31574941E-02, 2.75781266E-02, 3.24938744E-02, 3.79213169E-02, + 4.38751020E-02, 5.03679104E-02, 5.74104004E-02, 6.50111660E-02, + 7.31767192E-02, 8.19114447E-02, 9.12176073E-02, 1.01095326E-01, + 1.11542597E-01, 1.22555278E-01, 1.34127125E-01, 1.46249816E-01, + 1.58912972E-01, 1.72104210E-01, 1.85809180E-01, 2.00011641E-01, + 2.14693516E-01, 2.29834959E-01, 2.45414421E-01, 2.61408776E-01, + 2.77793378E-01, 2.94542134E-01, 3.11627686E-01, 3.29021394E-01, + 3.46693635E-01, 3.64613682E-01, 3.82750064E-01, 4.01070446E-01, + 4.19541985E-01, 4.38131332E-01, 4.56804723E-01, 4.75528270E-01, + 4.94267941E-01, 5.12989700E-01, 5.31659782E-01, 5.50244689E-01, + 5.68711281E-01, 5.87027133E-01, 6.05160415E-01, 6.23080134E-01, + 6.40756190E-01, 6.58159554E-01, 6.75262392E-01, 6.92038059E-01, + 7.08461344E-01, 7.24508464E-01, 7.40157187E-01, 7.55386829E-01, + 7.70178556E-01, 7.84515142E-01, 7.98381269E-01, 8.11763465E-01, + 8.24650168E-01, 8.37031603E-01, 8.48900259E-01, 8.60250235E-01, + 8.71077836E-01, 8.81381273E-01, 8.91160548E-01, 9.00417745E-01, + 9.09156621E-01, 9.17382956E-01, 9.25104082E-01, 9.32328999E-01, + 9.39068437E-01, 9.45334494E-01, 9.51140642E-01, 9.56501782E-01, + 9.61433768E-01, 9.65953648E-01, 9.70079303E-01, 9.73829389E-01, + 9.77223217E-01, 9.80280578E-01, 9.83021557E-01, 9.85466540E-01, + 9.87635851E-01, 9.89549816E-01, 9.91228402E-01, 9.92691338E-01, + 9.93957877E-01, 9.95046616E-01, 9.95975435E-01, 9.96761382E-01, + 9.97420728E-01, 9.97968733E-01, 9.98419642E-01, 9.98786569E-01, + 9.99081731E-01, 9.99315977E-01, 9.99499321E-01, 9.99640644E-01, + 9.99747574E-01, 9.99826968E-01, 9.99884665E-01, 9.99925494E-01, + 9.99953628E-01, 9.99972343E-01, 9.99984324E-01, 9.99991655E-01, + 9.99995887E-01, 9.99998152E-01, 9.99999285E-01, 9.99999762E-01, + 9.99999940E-01, 1.00000000E+00, 1.00000000E+00, 1.00000000E+00, +}; + +const float ff_dca_lfe_step_size_16[101] = { + 2.1362956633198035E-004, 2.4414807580797754E-004, 2.7466658528397473E-004, + 2.7466658528397473E-004, 3.0518509475997192E-004, 3.3570360423596911E-004, + 3.9674062318796350E-004, 4.2725913266396069E-004, 4.5777764213995788E-004, + 5.1881466109195227E-004, 5.7985168004394665E-004, 6.1037018951994385E-004, + 6.7140720847193823E-004, 7.6296273689992981E-004, 8.2399975585192419E-004, + 9.1555528427991577E-004, 1.0071108127079073E-003, 1.0986663411358989E-003, + 1.2207403790398877E-003, 1.3428144169438765E-003, 1.4648884548478652E-003, + 1.6174810022278512E-003, 1.7700735496078372E-003, 1.9531846064638203E-003, + 2.1362956633198035E-003, 2.3499252296517838E-003, 2.5940733054597613E-003, + 2.8687398907437361E-003, 3.1434064760277108E-003, 3.4485915707876827E-003, + 3.7842951750236518E-003, 4.1810357982116153E-003, 4.6082949308755760E-003, + 5.0660725730155339E-003, 5.5543687246314890E-003, 6.1037018951994385E-003, + 6.7445905941953795E-003, 7.4159978026673177E-003, 8.1484420300912512E-003, + 8.9419232764671782E-003, 9.8574785607470940E-003, 1.0834070863979004E-002, + 1.1932737205114903E-002, 1.3122959074678793E-002, 1.4435254982146673E-002, + 1.5869624927518540E-002, 1.7456587420270394E-002, 1.9196142460402233E-002, + 2.1118808557390057E-002, 2.3224585711233862E-002, 2.5543992431409649E-002, + 2.8107547227393413E-002, 3.0915250099185155E-002, 3.4028138065736867E-002, + 3.7415692617572556E-002, 4.1169469283120215E-002, 4.5258949552903834E-002, + 4.9806207464827418E-002, 5.4780724509414958E-002, 6.0274056215094456E-002, + 6.6286202581865905E-002, 7.2908719138157288E-002, 8.0202642902920618E-002, + 8.8229010895107887E-002, 9.7048860133671075E-002, 1.0675374614703818E-001, + 1.1743522446363720E-001, 1.2918485061189611E-001, 1.4209418012024294E-001, + 1.5628528702658162E-001, 1.7191076387829218E-001, 1.8912320322275461E-001, + 2.0804467909787286E-001, 2.2882778405102694E-001, 2.5171666615802485E-001, + 2.7689443647572254E-001, 3.0457472457045198E-001, 3.3503219702749720E-001, + 3.6854152043214211E-001, 4.0537736136967073E-001, 4.4593646046327096E-001, + 4.9052400280770286E-001, 5.3956724753563035E-001, 5.9352397228919340E-001, + 6.5288247322000792E-001, 7.1816156498916595E-001, 7.9000213629566329E-001, + 8.6898403881954400E-001, 9.5590075380718409E-001, 1.0514847254860074E+000, + 1.1566209906308176E+000, 1.2722861415448470E+000, 1.3995178075502792E+000, + 1.5394756920072024E+000, 1.6934110538041323E+000, 1.8627582628864405E+000, + 2.0490432447279274E+000, 2.2539445173497725E+000, 2.4793237098300120E+000, + 2.7272865993224893E+000, 3.0000000000000000E+000 +}; + +const float ff_dca_lfe_step_size_24[144] = { + 3.5762791128491298E-006, 3.9339070241340428E-006, 4.4107442391805934E-006, + 4.7683721504655064E-006, 5.2452093655120570E-006, 5.8412558843202453E-006, + 6.4373024031284336E-006, 7.0333489219366219E-006, 7.7486047445064479E-006, + 8.4638605670762738E-006, 9.4175349971693751E-006, 1.0252000123500839E-005, + 1.1324883857355578E-005, 1.2516976894971954E-005, 1.3709069932588331E-005, + 1.5139581577727983E-005, 1.6570093222867636E-005, 1.8239023475530564E-005, + 2.0146372335716766E-005, 2.2053721195902969E-005, 2.4318697967374082E-005, + 2.6702884042606836E-005, 2.9444698029124504E-005, 3.2305721319403807E-005, + 3.5643581824729662E-005, 3.9100651633817152E-005, 4.3034558657951193E-005, + 4.7326093593370149E-005, 5.2094465743835655E-005, 5.7339675109347712E-005, + 6.3061721689906320E-005, 6.9379814789273121E-005, 7.6293954407448102E-005, + 8.3923349848192912E-005, 9.2268001111507552E-005, 1.0156632680491529E-004, + 1.1169911762465449E-004, 1.2290479217824841E-004, 1.3518335046569711E-004, + 1.4865400179076216E-004, 1.6355516476096688E-004, 1.7988683937631122E-004, + 1.9788744424431852E-004, 2.1767618866875036E-004, 2.3949149125713007E-004, + 2.6345256131321922E-004, 2.8979781744454115E-004, 3.1876567825861912E-004, + 3.5059456236297636E-004, 3.8564209766889782E-004, 4.2426591208766842E-004, + 4.6670442422681142E-004, 5.1331526199761173E-004, 5.6469447191887759E-004, + 6.2108047259813216E-004, 6.8318851985794547E-004, 7.5149545091336386E-004, + 8.2671652158695713E-004, 9.0932856909377204E-004, 1.0002852678639017E-003, + 1.1003018737199156E-003, 1.2103320610919071E-003, 1.3314487137137310E-003, + 1.4646055060154803E-003, 1.6109945310347714E-003, 1.7721655097205054E-003, + 1.9493105351102991E-003, 2.1442177467605765E-003, 2.3586752842277626E-003, + 2.5945904963720436E-003, 2.8539899413573674E-003, 3.1393770145627278E-003, + 3.4533743206708813E-003, 3.7987236736683454E-003, 4.1785245154529228E-003, + 4.5963531251374630E-003, 5.0560242004423382E-003, 5.5617100669992049E-003, + 6.1178214690472445E-003, 6.7296036159519689E-003, 7.4025401356864135E-003, + 8.1428299120461841E-003, 8.9571486660419298E-003, 9.8527681652031147E-003, + 1.0838033060793050E-002, 1.1921884050593860E-002, 1.3114096297513997E-002, + 1.4425517848195773E-002, 1.5868069633015350E-002, 1.7454864675386508E-002, + 1.9200327301064409E-002, 2.1120431556753107E-002, 2.3232462791498040E-002, + 2.5555613703204836E-002, 2.8111222757246822E-002, 3.0922297349250002E-002, + 3.4014586688826884E-002, 3.7415985753057691E-002, 4.1157608170224208E-002, + 4.5273428591898514E-002, 4.9800759530157987E-002, 5.4780847404104160E-002, + 6.0258872539862694E-002, 6.6284783635709721E-002, 7.2913297762071824E-002, + 8.0204615617348624E-002, 8.8225017574431602E-002, 9.7047578936526643E-002, + 1.0675228914645780E-001, 1.1742748229831246E-001, 1.2917031397465634E-001, + 1.4208735729305236E-001, 1.5629603341770570E-001, 1.7192568444319778E-001, + 1.8911816944100493E-001, 2.0803001022696618E-001, 2.2883310661710579E-001, + 2.5171640535788598E-001, 2.7688804589367461E-001, 3.0457679087839018E-001, + 3.3503452957088109E-001, 3.6853794676517804E-001, 4.0539174144169587E-001, + 4.4593089174400469E-001, 4.9052399283933557E-001, 5.3957635636047796E-001, + 5.9353406352210802E-001, 6.5288742219059737E-001, 7.1817609288407480E-001, + 7.8999373793527339E-001, 8.6899314749159184E-001, 9.5589243839889027E-001, + 1.0514817299225008E+000, 1.1566298194682383E+000, 1.2722928848615747E+000, + 1.3995221137430804E+000, 1.5394743131964581E+000, 1.6934218041207556E+000, + 1.8627639845328312E+000, 2.0490403233814627E+000, 2.2539444272451910E+000, + 2.4793389414952922E+000, 2.7272728356448215E+000, 2.9999998807906962E+000 +}; + +const float ff_dca_bank_coeff[10] = { + 0.022810893, 0.41799772, 0.90844810, 0.99973983, + 0.068974845, 0.34675997, 0.29396889, 0.19642374, + 0.308658270, 0.038060233 +}; + +const float ff_dca_lfe_iir[5][4] = { + { -0.98618466, 1.9861259, 1.0, -1.9840510 }, + { -0.98883152, 1.9887193, 1.0, -1.9979848 }, + { -0.99252087, 1.9923381, 1.0, -1.9990897 }, + { -0.99591690, 1.9956781, 1.0, -1.9993745 }, + { -0.99872285, 1.9984550, 1.0, -1.9994639 } +}; diff --git a/libavcodec/dcadata.h b/libavcodec/dcadata.h index 263f7b1acc..0c54225f97 100644 --- a/libavcodec/dcadata.h +++ b/libavcodec/dcadata.h @@ -73,4 +73,51 @@ extern const int32_t ff_dca_xll_band_coeff[20]; extern const int32_t ff_dca_sampling_freqs[16]; +extern const uint16_t ff_dca_avg_g3_freqs[3]; + +extern const uint16_t ff_dca_fst_amp[44]; + +extern const uint8_t ff_dca_freq_to_sb[32]; + +extern const int8_t ff_dca_ph0_shift[8]; + +extern const uint8_t ff_dca_grid_1_to_scf[11]; +extern const uint8_t ff_dca_grid_2_to_scf[3]; + +extern const uint8_t ff_dca_scf_to_grid_1[32]; +extern const uint8_t ff_dca_scf_to_grid_2[32]; + +extern const uint8_t ff_dca_grid_1_weights[12][32]; + +extern const uint8_t ff_dca_sb_reorder[8][8]; + +extern const int8_t ff_dca_lfe_delta_index_16[8]; +extern const int8_t ff_dca_lfe_delta_index_24[32]; + +extern const uint16_t ff_dca_rsd_pack_5_in_8[256]; +extern const uint8_t ff_dca_rsd_pack_3_in_7[128][3]; + +extern const float ff_dca_rsd_level_2a[2]; +extern const float ff_dca_rsd_level_2b[2]; +extern const float ff_dca_rsd_level_3[3]; +extern const float ff_dca_rsd_level_5[5]; +extern const float ff_dca_rsd_level_8[8]; +extern const float ff_dca_rsd_level_16[16]; + +extern const float ff_dca_synth_env[32]; + +extern const float ff_dca_corr_cf[32][11]; + +extern const float ff_dca_quant_amp[57]; + +extern const float ff_dca_st_coeff[34]; + +extern const float ff_dca_long_window[128]; + +extern const float ff_dca_lfe_step_size_16[101]; +extern const float ff_dca_lfe_step_size_24[144]; + +extern const float ff_dca_bank_coeff[10]; +extern const float ff_dca_lfe_iir[5][4]; + #endif /* AVCODEC_DCADATA_H */ diff --git a/libavcodec/dcadec.c b/libavcodec/dcadec.c index f5ddc21c86..303b0c2bc8 100644 --- a/libavcodec/dcadec.c +++ b/libavcodec/dcadec.c @@ -235,6 +235,16 @@ static int dcadec_decode_frame(AVCodecContext *avctx, void *data, } } + // Parse LBR component in EXSS + if (asset && (asset->extension_mask & DCA_EXSS_LBR)) { + if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) { + if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE)) + return ret; + } else { + s->packet |= DCA_PACKET_LBR; + } + } + // Parse core extensions in EXSS or backward compatible core sub-stream if ((s->packet & DCA_PACKET_CORE) && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0) @@ -242,7 +252,10 @@ static int dcadec_decode_frame(AVCodecContext *avctx, void *data, } // Filter the frame - if (s->packet & DCA_PACKET_XLL) { + if (s->packet & DCA_PACKET_LBR) { + if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0) + return ret; + } else if (s->packet & DCA_PACKET_XLL) { if (s->packet & DCA_PACKET_CORE) { int x96_synth = -1; @@ -297,6 +310,7 @@ static av_cold void dcadec_flush(AVCodecContext *avctx) ff_dca_core_flush(&s->core); ff_dca_xll_flush(&s->xll); + ff_dca_lbr_flush(&s->lbr); s->core_residual_valid = 0; } @@ -307,6 +321,7 @@ static av_cold int dcadec_close(AVCodecContext *avctx) ff_dca_core_close(&s->core); ff_dca_xll_close(&s->xll); + ff_dca_lbr_close(&s->lbr); av_freep(&s->buffer); s->buffer_size = 0; @@ -322,15 +337,20 @@ static av_cold int dcadec_init(AVCodecContext *avctx) s->core.avctx = avctx; s->exss.avctx = avctx; s->xll.avctx = avctx; + s->lbr.avctx = avctx; ff_dca_init_vlcs(); if (ff_dca_core_init(&s->core) < 0) return AVERROR(ENOMEM); + if (ff_dca_lbr_init(&s->lbr) < 0) + return AVERROR(ENOMEM); + ff_dcadsp_init(&s->dcadsp); s->core.dcadsp = &s->dcadsp; s->xll.dcadsp = &s->dcadsp; + s->lbr.dcadsp = &s->dcadsp; s->crctab = av_crc_get_table(AV_CRC_16_CCITT); diff --git a/libavcodec/dcadec.h b/libavcodec/dcadec.h index 0d8a145edf..5e4707758b 100644 --- a/libavcodec/dcadec.h +++ b/libavcodec/dcadec.h @@ -32,13 +32,15 @@ #include "dca_core.h" #include "dca_exss.h" #include "dca_xll.h" +#include "dca_lbr.h" #define DCA_BUFFER_PADDING_SIZE 1024 #define DCA_PACKET_CORE 0x01 #define DCA_PACKET_EXSS 0x02 #define DCA_PACKET_XLL 0x04 -#define DCA_PACKET_RECOVERY 0x08 +#define DCA_PACKET_LBR 0x08 +#define DCA_PACKET_RECOVERY 0x10 typedef struct DCAContext { const AVClass *class; ///< class for AVOptions @@ -47,6 +49,7 @@ typedef struct DCAContext { DCACoreDecoder core; ///< Core decoder context DCAExssParser exss; ///< EXSS parser context DCAXllDecoder xll; ///< XLL decoder context + DCALbrDecoder lbr; ///< LBR decoder context DCADSPContext dcadsp; diff --git a/libavcodec/dcadsp.c b/libavcodec/dcadsp.c index 25a2039908..1cd2e4eddf 100644 --- a/libavcodec/dcadsp.c +++ b/libavcodec/dcadsp.c @@ -385,6 +385,77 @@ static void assemble_freq_bands_c(int32_t *dst, int32_t *src0, int32_t *src1, } } +static void lbr_bank_c(float output[32][4], float **input, + const float *coeff, ptrdiff_t ofs, ptrdiff_t len) +{ + float SW0 = coeff[0]; + float SW1 = coeff[1]; + float SW2 = coeff[2]; + float SW3 = coeff[3]; + + float C1 = coeff[4]; + float C2 = coeff[5]; + float C3 = coeff[6]; + float C4 = coeff[7]; + + float AL1 = coeff[8]; + float AL2 = coeff[9]; + + int i; + + // Short window and 8 point forward MDCT + for (i = 0; i < len; i++) { + float *src = input[i] + ofs; + + float a = src[-4] * SW0 - src[-1] * SW3; + float b = src[-3] * SW1 - src[-2] * SW2; + float c = src[ 2] * SW1 + src[ 1] * SW2; + float d = src[ 3] * SW0 + src[ 0] * SW3; + + output[i][0] = C1 * b - C2 * c + C4 * a - C3 * d; + output[i][1] = C1 * d - C2 * a - C4 * b - C3 * c; + output[i][2] = C3 * b + C2 * d - C4 * c + C1 * a; + output[i][3] = C3 * a - C2 * b + C4 * d - C1 * c; + } + + // Aliasing cancellation for high frequencies + for (i = 12; i < len - 1; i++) { + float a = output[i ][3] * AL1; + float b = output[i+1][0] * AL1; + output[i ][3] += b - a; + output[i+1][0] -= b + a; + a = output[i ][2] * AL2; + b = output[i+1][1] * AL2; + output[i ][2] += b - a; + output[i+1][1] -= b + a; + } +} + +static void lfe_iir_c(float *output, const float *input, + const float iir[5][4], float hist[5][2], + ptrdiff_t factor) +{ + float res, tmp; + int i, j, k; + + for (i = 0; i < 64; i++) { + res = *input++; + + for (j = 0; j < factor; j++) { + for (k = 0; k < 5; k++) { + tmp = hist[k][0] * iir[k][0] + hist[k][1] * iir[k][1] + res; + res = hist[k][0] * iir[k][2] + hist[k][1] * iir[k][3] + tmp; + + hist[k][0] = hist[k][1]; + hist[k][1] = tmp; + } + + *output++ = res; + res = 0; + } + } +} + av_cold void ff_dcadsp_init(DCADSPContext *s) { s->decode_hf = decode_hf_c; @@ -411,6 +482,9 @@ av_cold void ff_dcadsp_init(DCADSPContext *s) s->assemble_freq_bands = assemble_freq_bands_c; + s->lbr_bank = lbr_bank_c; + s->lfe_iir = lfe_iir_c; + if (ARCH_X86) ff_dcadsp_init_x86(s); } diff --git a/libavcodec/dcadsp.h b/libavcodec/dcadsp.h index c82b7b1095..8f2f467070 100644 --- a/libavcodec/dcadsp.h +++ b/libavcodec/dcadsp.h @@ -84,6 +84,13 @@ typedef struct DCADSPContext { void (*assemble_freq_bands)(int32_t *dst, int32_t *src0, int32_t *src1, const int32_t *coeff, ptrdiff_t len); + + void (*lbr_bank)(float output[32][4], float **input, + const float *coeff, ptrdiff_t ofs, ptrdiff_t len); + + void (*lfe_iir)(float *output, const float *input, + const float iir[5][4], float hist[5][2], + ptrdiff_t factor); } DCADSPContext; av_cold void ff_dcadsp_init(DCADSPContext *s); diff --git a/libavcodec/dcahuff.c b/libavcodec/dcahuff.c index 4b62322f13..bea3530214 100644 --- a/libavcodec/dcahuff.c +++ b/libavcodec/dcahuff.c @@ -1038,13 +1038,209 @@ static const uint8_t *const bitalloc_bits[DCA_CODE_BOOKS][8] = { bitalloc_129_bits_e, bitalloc_129_bits_f, bitalloc_129_bits_g, NULL } }; -static const uint16_t vlc_offs[63] = { +static const uint16_t tnl_grp_0_codes[37] = { + 0x0000, 0x0003, 0x0004, 0x0007, 0x0001, 0x0009, 0x000a, 0x000d, + 0x000e, 0x0006, 0x0012, 0x0005, 0x0015, 0x0016, 0x0022, 0x0025, + 0x0035, 0x0076, 0x0002, 0x0042, 0x00b6, 0x0036, 0x00c2, 0x0136, + 0x0182, 0x01c2, 0x03c2, 0x0482, 0x0682, 0x0082, 0x0882, 0x0a82, + 0x0282, 0x2282, 0x3282, 0x1282, 0x5282, +}; + +static const uint16_t tnl_grp_1_codes[34] = { + 0x0001, 0x0003, 0x0006, 0x0000, 0x0002, 0x0004, 0x0005, 0x0007, + 0x0008, 0x000f, 0x001a, 0x001c, 0x001d, 0x000a, 0x002c, 0x002d, + 0x000d, 0x002a, 0x004c, 0x004d, 0x006a, 0x008c, 0x00cd, 0x00ea, + 0x000c, 0x010c, 0x01ea, 0x020c, 0x030c, 0x07ea, 0x0bea, 0x03ea, + 0x13ea, 0x33ea, +}; + +static const uint16_t tnl_grp_2_codes[31] = { + 0x0001, 0x0003, 0x0006, 0x0007, 0x0004, 0x0008, 0x000c, 0x0010, + 0x0012, 0x001a, 0x0022, 0x0000, 0x000a, 0x0020, 0x0040, 0x004a, + 0x006a, 0x0002, 0x002a, 0x0042, 0x0082, 0x00aa, 0x00e0, 0x0060, + 0x00c2, 0x01c2, 0x0160, 0x0360, 0x0f60, 0x0760, 0x1760, +}; + +static const uint16_t tnl_grp_3_codes[28] = { + 0x0001, 0x0006, 0x0008, 0x0014, 0x001c, 0x0000, 0x0002, 0x0004, + 0x000a, 0x000c, 0x0010, 0x0012, 0x001a, 0x0020, 0x002a, 0x002c, + 0x0032, 0x003a, 0x0022, 0x0030, 0x0062, 0x0064, 0x0070, 0x0024, + 0x00a4, 0x01a4, 0x03a4, 0x07a4, +}; + +static const uint16_t tnl_grp_4_codes[23] = { + 0x0001, 0x0000, 0x000a, 0x0006, 0x0012, 0x001e, 0x0022, 0x002e, + 0x0036, 0x003e, 0x0002, 0x0016, 0x0032, 0x004e, 0x0056, 0x000e, + 0x0042, 0x0072, 0x00c2, 0x00f2, 0x008e, 0x018e, 0x038e, +}; + +static const uint16_t tnl_scf_codes[20] = { + 0x0000, 0x0001, 0x0002, 0x0005, 0x0006, 0x0007, 0x000b, 0x000c, + 0x0013, 0x0014, 0x0003, 0x0004, 0x0023, 0x0064, 0x00a4, 0x0024, + 0x0124, 0x0324, 0x0724, 0x0f24, +}; + +static const uint16_t damp_codes[7] = { + 0x0001, 0x0000, 0x0002, 0x0006, 0x000e, 0x001e, 0x003e, +}; + +static const uint16_t dph_codes[9] = { + 0x0000, 0x0002, 0x0003, 0x0001, 0x0009, 0x000d, 0x0005, 0x0015, + 0x0035, +}; + +static const uint16_t fst_rsd_amp_codes[24] = { + 0x0003, 0x0005, 0x0006, 0x0007, 0x0000, 0x0001, 0x0002, 0x0008, + 0x0009, 0x000a, 0x0014, 0x0004, 0x001a, 0x001c, 0x0024, 0x002c, + 0x003a, 0x000c, 0x003c, 0x004c, 0x00fc, 0x007c, 0x017c, 0x037c, +}; + +static const uint16_t rsd_apprx_codes[6] = { + 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, +}; + +static const uint16_t rsd_amp_codes[33] = { + 0x0001, 0x0000, 0x0002, 0x0003, 0x0004, 0x000e, 0x000f, 0x0016, + 0x0007, 0x0027, 0x0037, 0x0026, 0x0066, 0x0006, 0x0017, 0x0046, + 0x0097, 0x00d7, 0x0086, 0x00c6, 0x01c6, 0x0157, 0x0186, 0x0257, + 0x0357, 0x0057, 0x0786, 0x0386, 0x0b86, 0x0457, 0x0c57, 0x1457, + 0x1c57, +}; + +static const uint16_t avg_g3_codes[18] = { + 0x0001, 0x0002, 0x0003, 0x0000, 0x000c, 0x0014, 0x0018, 0x0004, + 0x0008, 0x0028, 0x0068, 0x0024, 0x00a4, 0x00e4, 0x0164, 0x0064, + 0x0264, 0x0664, +}; + +static const uint16_t st_grid_codes[22] = { + 0x0001, 0x0002, 0x0000, 0x0004, 0x0008, 0x001c, 0x004c, 0x006c, + 0x000c, 0x002c, 0x008c, 0x00ac, 0x012c, 0x018c, 0x01ac, 0x038c, + 0x03ac, 0x032c, 0x072c, 0x0f2c, 0x172c, 0x1f2c, +}; + +static const uint16_t grid_2_codes[20] = { + 0x0000, 0x0002, 0x0003, 0x0001, 0x0005, 0x000d, 0x003d, 0x005d, + 0x009d, 0x011d, 0x001d, 0x061d, 0x041d, 0x0c1d, 0x0a1d, 0x121d, + 0x021d, 0x1a1d, 0x221d, 0x3a1d, +}; + +static const uint16_t grid_3_codes[13] = { + 0x0001, 0x0002, 0x0000, 0x0004, 0x000c, 0x001c, 0x007c, 0x003c, + 0x01bc, 0x00bc, 0x06bc, 0x02bc, 0x0abc, +}; + +static const uint16_t rsd_codes[9] = { + 0x0001, 0x0003, 0x0000, 0x0002, 0x0006, 0x0004, 0x000c, 0x001c, + 0x003c, +}; + +static const uint8_t tnl_grp_0_bitvals[74] = { + 3, 5, 3, 9, 3, 4, 3, 6, 4, 10, 4, 13, 4, 7, 4, 11, + 4, 8, 5, 12, 5, 14, 6, 15, 6, 18, 6, 1, 6, 17, 6, 16, + 6, 21, 7, 20, 8, 19, 8, 22, 8, 25, 9, 26, 9, 23, 9, 3, + 9, 24, 10, 29, 10, 27, 11, 28, 11, 30, 12, 33, 12, 31, 12, 32, + 14, 34, 14, 37, 14, 36, 15, 35, 15, 0, +}; + +static const uint8_t tnl_grp_1_bitvals[68] = { + 3, 9, 3, 6, 3, 5, 4, 4, 4, 8, 4, 10, 4, 1, 4, 11, + 4, 7, 4, 13, 5, 12, 5, 14, 5, 17, 6, 16, 6, 15, 6, 18, + 7, 20, 7, 19, 7, 21, 8, 25, 8, 23, 8, 22, 8, 24, 9, 26, + 10, 3, 10, 29, 10, 30, 10, 27, 10, 28, 11, 31, 12, 32, 13, 33, + 14, 34, 14, 0, +}; + +static const uint8_t tnl_grp_2_bitvals[62] = { + 2, 1, 3, 6, 3, 5, 3, 7, 4, 9, 4, 8, 4, 4, 5, 10, + 5, 11, 5, 13, 6, 12, 7, 14, 7, 16, 7, 15, 7, 17, 7, 18, + 7, 19, 8, 22, 8, 20, 8, 21, 8, 3, 8, 24, 8, 25, 9, 23, + 9, 26, 9, 27, 10, 28, 11, 29, 12, 31, 13, 30, 13, 0, +}; + +static const uint8_t tnl_grp_3_bitvals[56] = { + 1, 1, 3, 6, 4, 5, 5, 9, 5, 4, 6, 8, 6, 14, 6, 10, + 6, 21, 6, 13, 6, 7, 6, 3, 6, 16, 6, 2, 6, 18, 6, 17, + 6, 11, 6, 15, 7, 19, 7, 23, 7, 24, 7, 22, 7, 12, 8, 20, + 9, 25, 10, 26, 11, 27, 11, 0, +}; + +static const uint8_t tnl_grp_4_bitvals[46] = { + 1, 1, 2, 2, 4, 4, 5, 5, 6, 6, 6, 8, 6, 3, 6, 19, + 6, 20, 6, 9, 7, 7, 7, 11, 7, 13, 7, 17, 7, 10, 8, 12, + 8, 15, 8, 14, 8, 21, 8, 18, 9, 16, 10, 22, 10, 0, +}; + +static const uint8_t tnl_scf_bitvals[40] = { + 3, 3, 3, 1, 3, 2, 3, 5, 3, 4, 3, 6, 4, 8, 4, 7, + 5, 10, 5, 9, 6, 12, 6, 11, 6, 13, 7, 14, 8, 15, 9, 16, + 10, 17, 11, 18, 12, 19, 12, 0, +}; + +static const uint8_t damp_bitvals[14] = { + 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 0, +}; + +static const uint8_t dph_bitvals[18] = { + 2, 2, 2, 1, 2, 8, 4, 3, 4, 7, 4, 4, 5, 6, 6, 5, + 6, 0, +}; + +static const uint8_t fst_rsd_amp_bitvals[48] = { + 3, 13, 3, 15, 3, 16, 3, 14, 4, 12, 4, 10, 4, 11, 4, 17, + 4, 18, 5, 19, 5, 9, 6, 1, 6, 7, 6, 6, 6, 8, 6, 5, + 6, 4, 7, 20, 7, 2, 7, 3, 8, 21, 9, 22, 10, 23, 10, 0, +}; + +static const uint8_t rsd_apprx_bitvals[12] = { + 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 0, +}; + +static const uint8_t rsd_amp_bitvals[66] = { + 2, 3, 3, 2, 3, 5, 3, 4, 3, 1, 4, 7, 4, 6, 5, 9, + 6, 8, 6, 11, 6, 10, 7, 12, 7, 13, 8, 14, 8, 18, 8, 16, + 8, 15, 8, 22, 9, 20, 9, 24, 9, 17, 10, 28, 10, 26, 10, 21, + 10, 23, 11, 30, 11, 19, 12, 25, 12, 32, 13, 36, 13, 29, 13, 34, + 13, 0, +}; + +static const uint8_t avg_g3_bitvals[36] = { + 2, 15, 2, 16, 2, 17, 4, 14, 4, 18, 5, 12, 5, 13, 6, 10, + 6, 11, 7, 19, 7, 9, 8, 20, 8, 8, 8, 7, 9, 21, 10, 6, + 11, 23, 11, 0, +}; + +static const uint8_t st_grid_bitvals[44] = { + 1, 6, 2, 1, 4, 4, 4, 8, 4, 3, 5, 10, 7, 12, 7, 5, + 8, 14, 9, 16, 9, 7, 9, 18, 10, 11, 10, 9, 10, 20, 10, 22, + 10, 2, 11, 13, 13, 17, 13, 24, 13, 15, 13, 0, +}; + +static const uint8_t grid_2_bitvals[40] = { + 2, 3, 2, 2, 2, 1, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, + 8, 9, 9, 10, 11, 11, 11, 12, 12, 13, 12, 17, 13, 15, 13, 18, + 14, 19, 14, 16, 14, 14, 14, 0, +}; + +static const uint8_t grid_3_bitvals[26] = { + 1, 17, 2, 16, 3, 18, 4, 15, 5, 19, 6, 14, 7, 20, 8, 13, + 9, 21, 10, 12, 11, 22, 12, 11, 12, 0, +}; + +static const uint8_t rsd_bitvals[18] = { + 2, 2, 2, 3, 3, 1, 3, 4, 3, 0, 4, 5, 5, 6, 6, 7, + 6, 4, +}; + +static const uint16_t vlc_offs[80] = { 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364, 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508, 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564, 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240, 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264, - 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622, + 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622, 24200, 24748, 25276, + 25792, 26306, 26826, 26890, 26954, 27468, 27500, 28038, 28554, 29086, 29630, + 30150, 30214 }; DCAVLC ff_dca_vlc_bit_allocation; @@ -1052,9 +1248,22 @@ DCAVLC ff_dca_vlc_transition_mode; DCAVLC ff_dca_vlc_scale_factor; DCAVLC ff_dca_vlc_quant_index[DCA_CODE_BOOKS]; +VLC ff_dca_vlc_tnl_grp[5]; +VLC ff_dca_vlc_tnl_scf; +VLC ff_dca_vlc_damp; +VLC ff_dca_vlc_dph; +VLC ff_dca_vlc_fst_rsd_amp; +VLC ff_dca_vlc_rsd_apprx; +VLC ff_dca_vlc_rsd_amp; +VLC ff_dca_vlc_avg_g3; +VLC ff_dca_vlc_st_grid; +VLC ff_dca_vlc_grid_2; +VLC ff_dca_vlc_grid_3; +VLC ff_dca_vlc_rsd; + av_cold void ff_dca_init_vlcs(void) { - static VLC_TYPE dca_table[23622][2]; + static VLC_TYPE dca_table[30214][2]; static int vlcs_initialized = 0; int i, j, k = 0; @@ -1095,5 +1304,34 @@ av_cold void ff_dca_init_vlcs(void) bitalloc_sizes[i], bitalloc_bits[i][j], bitalloc_codes[i][j]); } +#define LBR_INIT_VLC(vlc, tab, nb_bits) \ + do { \ + vlc.table = &dca_table[vlc_offs[k]]; \ + vlc.table_allocated = vlc_offs[k + 1] - vlc_offs[k]; \ + ff_init_vlc_sparse(&vlc, nb_bits, FF_ARRAY_ELEMS(tab##_codes), \ + &tab##_bitvals[0], 2, 1, \ + tab##_codes, 2, 2, \ + &tab##_bitvals[1], 2, 1, \ + INIT_VLC_LE | INIT_VLC_USE_NEW_STATIC); \ + k++; \ + } while (0) + + LBR_INIT_VLC(ff_dca_vlc_tnl_grp[0], tnl_grp_0, 9); + LBR_INIT_VLC(ff_dca_vlc_tnl_grp[1], tnl_grp_1, 9); + LBR_INIT_VLC(ff_dca_vlc_tnl_grp[2], tnl_grp_2, 9); + LBR_INIT_VLC(ff_dca_vlc_tnl_grp[3], tnl_grp_3, 9); + LBR_INIT_VLC(ff_dca_vlc_tnl_grp[4], tnl_grp_4, 9); + LBR_INIT_VLC(ff_dca_vlc_tnl_scf, tnl_scf, 9); + LBR_INIT_VLC(ff_dca_vlc_damp, damp, 6); + LBR_INIT_VLC(ff_dca_vlc_dph, dph, 6); + LBR_INIT_VLC(ff_dca_vlc_fst_rsd_amp, fst_rsd_amp, 9); + LBR_INIT_VLC(ff_dca_vlc_rsd_apprx, rsd_apprx, 5); + LBR_INIT_VLC(ff_dca_vlc_rsd_amp, rsd_amp, 9); + LBR_INIT_VLC(ff_dca_vlc_avg_g3, avg_g3, 9); + LBR_INIT_VLC(ff_dca_vlc_st_grid, st_grid, 9); + LBR_INIT_VLC(ff_dca_vlc_grid_2, grid_2, 9); + LBR_INIT_VLC(ff_dca_vlc_grid_3, grid_3, 9); + LBR_INIT_VLC(ff_dca_vlc_rsd, rsd, 6); + vlcs_initialized = 1; } diff --git a/libavcodec/dcahuff.h b/libavcodec/dcahuff.h index d0ddfc4e8a..b1d5735d57 100644 --- a/libavcodec/dcahuff.h +++ b/libavcodec/dcahuff.h @@ -41,6 +41,19 @@ extern DCAVLC ff_dca_vlc_transition_mode; extern DCAVLC ff_dca_vlc_scale_factor; extern DCAVLC ff_dca_vlc_quant_index[DCA_CODE_BOOKS]; +extern VLC ff_dca_vlc_tnl_grp[5]; +extern VLC ff_dca_vlc_tnl_scf; +extern VLC ff_dca_vlc_damp; +extern VLC ff_dca_vlc_dph; +extern VLC ff_dca_vlc_fst_rsd_amp; +extern VLC ff_dca_vlc_rsd_apprx; +extern VLC ff_dca_vlc_rsd_amp; +extern VLC ff_dca_vlc_avg_g3; +extern VLC ff_dca_vlc_st_grid; +extern VLC ff_dca_vlc_grid_2; +extern VLC ff_dca_vlc_grid_3; +extern VLC ff_dca_vlc_rsd; + av_cold void ff_dca_init_vlcs(void); #endif /* AVCODEC_DCAHUFF_H */