2 * Copyright (C) 2016 foo86
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define BITSTREAM_READER_LE
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/mem_internal.h"
29 #include "dca_syncwords.h"
30 #include "bytestream.h"
35 LBR_FLAG_24_BIT = 0x01,
36 LBR_FLAG_LFE_PRESENT = 0x02,
37 LBR_FLAG_BAND_LIMIT_2_3 = 0x04,
38 LBR_FLAG_BAND_LIMIT_1_2 = 0x08,
39 LBR_FLAG_BAND_LIMIT_1_3 = 0x0c,
40 LBR_FLAG_BAND_LIMIT_1_4 = 0x10,
41 LBR_FLAG_BAND_LIMIT_1_8 = 0x18,
42 LBR_FLAG_BAND_LIMIT_NONE = 0x14,
43 LBR_FLAG_BAND_LIMIT_MASK = 0x1c,
44 LBR_FLAG_DMIX_STEREO = 0x20,
45 LBR_FLAG_DMIX_MULTI_CH = 0x40
49 LBR_CHUNK_NULL = 0x00,
51 LBR_CHUNK_FRAME = 0x04,
52 LBR_CHUNK_FRAME_NO_CSUM = 0x06,
55 LBR_CHUNK_RESERVED_1 = 0x0c,
56 LBR_CHUNK_RESERVED_2 = 0x0d,
58 LBR_CHUNK_TONAL = 0x10,
59 LBR_CHUNK_TONAL_GRP_1 = 0x11,
60 LBR_CHUNK_TONAL_GRP_2 = 0x12,
61 LBR_CHUNK_TONAL_GRP_3 = 0x13,
62 LBR_CHUNK_TONAL_GRP_4 = 0x14,
63 LBR_CHUNK_TONAL_GRP_5 = 0x15,
64 LBR_CHUNK_TONAL_SCF = 0x16,
65 LBR_CHUNK_TONAL_SCF_GRP_1 = 0x17,
66 LBR_CHUNK_TONAL_SCF_GRP_2 = 0x18,
67 LBR_CHUNK_TONAL_SCF_GRP_3 = 0x19,
68 LBR_CHUNK_TONAL_SCF_GRP_4 = 0x1a,
69 LBR_CHUNK_TONAL_SCF_GRP_5 = 0x1b,
70 LBR_CHUNK_RES_GRID_LR = 0x30,
71 LBR_CHUNK_RES_GRID_LR_LAST = 0x3f,
72 LBR_CHUNK_RES_GRID_HR = 0x40,
73 LBR_CHUNK_RES_GRID_HR_LAST = 0x4f,
74 LBR_CHUNK_RES_TS_1 = 0x50,
75 LBR_CHUNK_RES_TS_1_LAST = 0x5f,
76 LBR_CHUNK_RES_TS_2 = 0x60,
77 LBR_CHUNK_RES_TS_2_LAST = 0x6f,
78 LBR_CHUNK_EXTENSION = 0x7f
81 typedef struct LBRChunk {
86 static const int8_t channel_reorder_nolfe[7][5] = {
87 { 0, -1, -1, -1, -1 }, // C
88 { 0, 1, -1, -1, -1 }, // LR
89 { 0, 1, 2, -1, -1 }, // LR C
90 { 0, 1, -1, -1, -1 }, // LsRs
91 { 1, 2, 0, -1, -1 }, // LsRs C
92 { 0, 1, 2, 3, -1 }, // LR LsRs
93 { 0, 1, 3, 4, 2 }, // LR LsRs C
96 static const int8_t channel_reorder_lfe[7][5] = {
97 { 0, -1, -1, -1, -1 }, // C
98 { 0, 1, -1, -1, -1 }, // LR
99 { 0, 1, 2, -1, -1 }, // LR C
100 { 1, 2, -1, -1, -1 }, // LsRs
101 { 2, 3, 0, -1, -1 }, // LsRs C
102 { 0, 1, 3, 4, -1 }, // LR LsRs
103 { 0, 1, 4, 5, 2 }, // LR LsRs C
106 static const uint8_t lfe_index[7] = {
110 static const uint8_t channel_counts[7] = {
114 static const uint16_t channel_layouts[7] = {
117 AV_CH_LAYOUT_SURROUND,
118 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
119 AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
124 static float cos_tab[256];
125 static float lpc_tab[16];
127 static av_cold void init_tables(void)
129 static int initialized;
135 for (i = 0; i < 256; i++)
136 cos_tab[i] = cos(M_PI * i / 128);
138 for (i = 0; i < 16; i++)
139 lpc_tab[i] = sin((i - 8) * (M_PI / ((i < 8) ? 17 : 15)));
144 static int parse_lfe_24(DCALbrDecoder *s)
146 int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_24) - 1;
147 int i, ps, si, code, step_i;
148 float step, value, delta;
150 ps = get_bits(&s->gb, 24);
153 value = (((ps & 0x7fffff) ^ -si) + si) * (1.0f / 0x7fffff);
155 step_i = get_bits(&s->gb, 8);
156 if (step_i > step_max) {
157 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
158 return AVERROR_INVALIDDATA;
161 step = ff_dca_lfe_step_size_24[step_i];
163 for (i = 0; i < 64; i++) {
164 code = get_bits(&s->gb, 6);
166 delta = step * 0.03125f;
170 delta += step * 0.5f;
172 delta += step * 0.25f;
174 delta += step * 0.125f;
176 delta += step * 0.0625f;
188 step_i += ff_dca_lfe_delta_index_24[code & 31];
189 step_i = av_clip(step_i, 0, step_max);
191 step = ff_dca_lfe_step_size_24[step_i];
192 s->lfe_data[i] = value * s->lfe_scale;
198 static int parse_lfe_16(DCALbrDecoder *s)
200 int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_16) - 1;
201 int i, ps, si, code, step_i;
202 float step, value, delta;
204 ps = get_bits(&s->gb, 16);
207 value = (((ps & 0x7fff) ^ -si) + si) * (1.0f / 0x7fff);
209 step_i = get_bits(&s->gb, 8);
210 if (step_i > step_max) {
211 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
212 return AVERROR_INVALIDDATA;
215 step = ff_dca_lfe_step_size_16[step_i];
217 for (i = 0; i < 64; i++) {
218 code = get_bits(&s->gb, 4);
220 delta = step * 0.125f;
224 delta += step * 0.5f;
226 delta += step * 0.25f;
238 step_i += ff_dca_lfe_delta_index_16[code & 7];
239 step_i = av_clip(step_i, 0, step_max);
241 step = ff_dca_lfe_step_size_16[step_i];
242 s->lfe_data[i] = value * s->lfe_scale;
248 static int parse_lfe_chunk(DCALbrDecoder *s, LBRChunk *chunk)
252 if (!(s->flags & LBR_FLAG_LFE_PRESENT))
258 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
262 // Determine bit depth from chunk size
263 if (chunk->len >= 52)
264 return parse_lfe_24(s);
265 if (chunk->len >= 35)
266 return parse_lfe_16(s);
268 av_log(s->avctx, AV_LOG_ERROR, "LFE chunk too short\n");
269 return AVERROR_INVALIDDATA;
272 static inline int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth)
274 int v = get_vlc2(s, vlc->table, vlc->bits, max_depth);
278 return get_bits(s, get_bits(s, 3) + 1);
281 static int parse_tonal(DCALbrDecoder *s, int group)
283 unsigned int amp[DCA_LBR_CHANNELS_TOTAL];
284 unsigned int phs[DCA_LBR_CHANNELS_TOTAL];
285 unsigned int diff, main_amp, shift;
286 int sf, sf_idx, ch, main_ch, freq;
287 int ch_nbits = av_ceil_log2(s->nchannels_total);
289 // Parse subframes for this group
290 for (sf = 0; sf < 1 << group; sf += diff ? 8 : 1) {
291 sf_idx = ((s->framenum << group) + sf) & 31;
292 s->tonal_bounds[group][sf_idx][0] = s->ntones;
294 // Parse tones for this subframe
295 for (freq = 1;; freq++) {
296 if (get_bits_left(&s->gb) < 1) {
297 av_log(s->avctx, AV_LOG_ERROR, "Tonal group chunk too short\n");
298 return AVERROR_INVALIDDATA;
301 diff = parse_vlc(&s->gb, &ff_dca_vlc_tnl_grp[group], 2);
302 if (diff >= FF_ARRAY_ELEMS(ff_dca_fst_amp)) {
303 av_log(s->avctx, AV_LOG_ERROR, "Invalid tonal frequency diff\n");
304 return AVERROR_INVALIDDATA;
307 diff = get_bitsz(&s->gb, diff >> 2) + ff_dca_fst_amp[diff];
309 break; // End of subframe
312 if (freq >> (5 - group) > s->nsubbands * 4 - 6) {
313 av_log(s->avctx, AV_LOG_ERROR, "Invalid spectral line offset\n");
314 return AVERROR_INVALIDDATA;
318 main_ch = get_bitsz(&s->gb, ch_nbits);
319 main_amp = parse_vlc(&s->gb, &ff_dca_vlc_tnl_scf, 2)
320 + s->tonal_scf[ff_dca_freq_to_sb[freq >> (7 - group)]]
321 + s->limited_range - 2;
322 amp[main_ch] = main_amp < AMP_MAX ? main_amp : 0;
323 phs[main_ch] = get_bits(&s->gb, 3);
325 // Secondary channels
326 for (ch = 0; ch < s->nchannels_total; ch++) {
329 if (get_bits1(&s->gb)) {
330 amp[ch] = amp[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_damp, 1);
331 phs[ch] = phs[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_dph, 1);
340 DCALbrTone *t = &s->tones[s->ntones];
341 s->ntones = (s->ntones + 1) & (DCA_LBR_TONES - 1);
343 t->x_freq = freq >> (5 - group);
344 t->f_delt = (freq & ((1 << (5 - group)) - 1)) << group;
345 t->ph_rot = 256 - (t->x_freq & 1) * 128 - t->f_delt * 4;
347 shift = ff_dca_ph0_shift[(t->x_freq & 3) * 2 + (freq & 1)]
348 - ((t->ph_rot << (5 - group)) - t->ph_rot);
350 for (ch = 0; ch < s->nchannels; ch++) {
351 t->amp[ch] = amp[ch] < AMP_MAX ? amp[ch] : 0;
352 t->phs[ch] = 128 - phs[ch] * 32 + shift;
357 s->tonal_bounds[group][sf_idx][1] = s->ntones;
363 static int parse_tonal_chunk(DCALbrDecoder *s, LBRChunk *chunk)
370 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
376 if (chunk->id == LBR_CHUNK_SCF || chunk->id == LBR_CHUNK_TONAL_SCF) {
377 if (get_bits_left(&s->gb) < 36) {
378 av_log(s->avctx, AV_LOG_ERROR, "Tonal scale factor chunk too short\n");
379 return AVERROR_INVALIDDATA;
381 for (sb = 0; sb < 6; sb++)
382 s->tonal_scf[sb] = get_bits(&s->gb, 6);
386 if (chunk->id == LBR_CHUNK_TONAL || chunk->id == LBR_CHUNK_TONAL_SCF)
387 for (group = 0; group < 5; group++) {
388 ret = parse_tonal(s, group);
396 static int parse_tonal_group(DCALbrDecoder *s, LBRChunk *chunk)
403 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
407 return parse_tonal(s, chunk->id);
411 * Check point to ensure that enough bits are left. Aborts decoding
412 * by skipping to the end of chunk otherwise.
414 static int ensure_bits(GetBitContext *s, int n)
416 int left = get_bits_left(s);
418 return AVERROR_INVALIDDATA;
420 skip_bits_long(s, left);
426 static int parse_scale_factors(DCALbrDecoder *s, uint8_t *scf)
428 int i, sf, prev, next, dist;
430 // Truncated scale factors remain zero
431 if (ensure_bits(&s->gb, 20))
434 // Initial scale factor
435 prev = parse_vlc(&s->gb, &ff_dca_vlc_fst_rsd_amp, 2);
437 for (sf = 0; sf < 7; sf += dist) {
438 scf[sf] = prev; // Store previous value
440 if (ensure_bits(&s->gb, 20))
443 // Interpolation distance
444 dist = parse_vlc(&s->gb, &ff_dca_vlc_rsd_apprx, 1) + 1;
446 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor distance\n");
447 return AVERROR_INVALIDDATA;
450 if (ensure_bits(&s->gb, 20))
453 // Final interpolation point
454 next = parse_vlc(&s->gb, &ff_dca_vlc_rsd_amp, 2);
457 next = prev + ((next + 1) >> 1);
459 next = prev - ( next >> 1);
465 scf[sf + 1] = prev + ((next - prev) >> 1);
467 scf[sf + 1] = prev - ((prev - next) >> 1);
472 scf[sf + 1] = prev + ( (next - prev) >> 2);
473 scf[sf + 2] = prev + ( (next - prev) >> 1);
474 scf[sf + 3] = prev + (((next - prev) * 3) >> 2);
476 scf[sf + 1] = prev - ( (prev - next) >> 2);
477 scf[sf + 2] = prev - ( (prev - next) >> 1);
478 scf[sf + 3] = prev - (((prev - next) * 3) >> 2);
483 for (i = 1; i < dist; i++)
484 scf[sf + i] = prev + (next - prev) * i / dist;
491 scf[sf] = next; // Store final value
496 static int parse_st_code(GetBitContext *s, int min_v)
498 unsigned int v = parse_vlc(s, &ff_dca_vlc_st_grid, 2) + min_v;
505 if (v >= FF_ARRAY_ELEMS(ff_dca_st_coeff))
510 static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
512 int ch, sb, sf, nsubbands, ret;
517 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
522 nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
523 for (sb = 2; sb < nsubbands; sb++) {
524 ret = parse_scale_factors(s, s->grid_1_scf[ch1][sb]);
527 if (ch1 != ch2 && ff_dca_grid_1_to_scf[sb] < s->min_mono_subband) {
528 ret = parse_scale_factors(s, s->grid_1_scf[ch2][sb]);
534 if (get_bits_left(&s->gb) < 1)
535 return 0; // Should not happen, but a sample exists that proves otherwise
537 // Average values for third grid
538 for (sb = 0; sb < s->nsubbands - 4; sb++) {
539 s->grid_3_avg[ch1][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
541 if (sb + 4 < s->min_mono_subband)
542 s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
544 s->grid_3_avg[ch2][sb] = s->grid_3_avg[ch1][sb];
548 if (get_bits_left(&s->gb) < 0) {
549 av_log(s->avctx, AV_LOG_ERROR, "First grid chunk too short\n");
550 return AVERROR_INVALIDDATA;
553 // Stereo image for partial mono mode
557 if (ensure_bits(&s->gb, 8))
560 min_v[0] = get_bits(&s->gb, 4);
561 min_v[1] = get_bits(&s->gb, 4);
563 nsubbands = (s->nsubbands - s->min_mono_subband + 3) / 4;
564 for (sb = 0; sb < nsubbands; sb++)
565 for (ch = ch1; ch <= ch2; ch++)
566 for (sf = 1; sf <= 4; sf++)
567 s->part_stereo[ch][sb][sf] = parse_st_code(&s->gb, min_v[ch - ch1]);
569 if (get_bits_left(&s->gb) >= 0)
570 s->part_stereo_pres |= 1 << ch1;
573 // Low resolution spatial information is not decoded
578 static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2)
580 int sb, nsubbands, ret;
583 nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
584 for (sb = 2; sb < nsubbands; sb++) {
585 if (ff_dca_grid_1_to_scf[sb] >= s->min_mono_subband) {
586 ret = parse_scale_factors(s, s->grid_1_scf[ch2][sb]);
592 // Average values for third grid
593 for (sb = 0; sb < s->nsubbands - 4; sb++) {
594 if (sb + 4 >= s->min_mono_subband) {
595 if (ensure_bits(&s->gb, 20))
597 s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
604 static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag)
608 for (ch = ch1; ch <= ch2; ch++) {
609 if ((ch != ch1 && sb + 4 >= s->min_mono_subband) != flag)
612 if (s->grid_3_pres[ch] & (1U << sb))
613 continue; // Already parsed
615 for (i = 0; i < 8; i++) {
616 if (ensure_bits(&s->gb, 20))
618 s->grid_3_scf[ch][sb][i] = parse_vlc(&s->gb, &ff_dca_vlc_grid_3, 2) - 16;
621 // Flag scale factors for this subband parsed
622 s->grid_3_pres[ch] |= 1U << sb;
626 static float lbr_rand(DCALbrDecoder *s, int sb)
628 s->lbr_rand = 1103515245U * s->lbr_rand + 12345U;
629 return s->lbr_rand * s->sb_scf[sb];
633 * Parse time samples for one subband, filling truncated samples with randomness
635 static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag)
637 float *samples = s->time_samples[ch][sb];
638 int i, j, code, nblocks, coding_method;
640 if (ensure_bits(&s->gb, 20))
641 return; // Too few bits left
643 coding_method = get_bits1(&s->gb);
645 switch (quant_level) {
647 nblocks = FFMIN(get_bits_left(&s->gb) / 8, DCA_LBR_TIME_SAMPLES / 8);
648 for (i = 0; i < nblocks; i++, samples += 8) {
649 code = get_bits(&s->gb, 8);
650 for (j = 0; j < 8; j++)
651 samples[j] = ff_dca_rsd_level_2a[(code >> j) & 1];
658 for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 2; i++) {
659 if (get_bits1(&s->gb))
660 samples[i] = ff_dca_rsd_level_2b[get_bits1(&s->gb)];
665 nblocks = FFMIN(get_bits_left(&s->gb) / 8, (DCA_LBR_TIME_SAMPLES + 4) / 5);
666 for (i = 0; i < nblocks; i++, samples += 5) {
667 code = ff_dca_rsd_pack_5_in_8[get_bits(&s->gb, 8)];
668 for (j = 0; j < 5; j++)
669 samples[j] = ff_dca_rsd_level_3[(code >> j * 2) & 3];
676 nblocks = FFMIN(get_bits_left(&s->gb) / 7, (DCA_LBR_TIME_SAMPLES + 2) / 3);
677 for (i = 0; i < nblocks; i++, samples += 3) {
678 code = get_bits(&s->gb, 7);
679 for (j = 0; j < 3; j++)
680 samples[j] = ff_dca_rsd_level_5[ff_dca_rsd_pack_3_in_7[code][j]];
686 for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 6; i++)
687 samples[i] = ff_dca_rsd_level_8[get_vlc2(&s->gb, ff_dca_vlc_rsd.table, 6, 1)];
691 nblocks = FFMIN(get_bits_left(&s->gb) / 4, DCA_LBR_TIME_SAMPLES);
692 for (i = 0; i < nblocks; i++)
693 samples[i] = ff_dca_rsd_level_16[get_bits(&s->gb, 4)];
700 if (flag && get_bits_left(&s->gb) < 20)
701 return; // Skip incomplete mono subband
703 for (; i < DCA_LBR_TIME_SAMPLES; i++)
704 s->time_samples[ch][sb][i] = lbr_rand(s, sb);
706 s->ch_pres[ch] |= 1U << sb;
709 static int parse_ts(DCALbrDecoder *s, int ch1, int ch2,
710 int start_sb, int end_sb, int flag)
712 int sb, sb_g3, sb_reorder, quant_level;
714 for (sb = start_sb; sb < end_sb; sb++) {
715 // Subband number before reordering
718 } else if (flag && sb < s->max_mono_subband) {
719 sb_reorder = s->sb_indices[sb];
721 if (ensure_bits(&s->gb, 28))
723 sb_reorder = get_bits(&s->gb, s->limited_range + 3);
726 s->sb_indices[sb] = sb_reorder;
728 if (sb_reorder >= s->nsubbands)
729 return AVERROR_INVALIDDATA;
731 // Third grid scale factors
733 for (sb_g3 = 0; sb_g3 < s->g3_avg_only_start_sb - 4; sb_g3++)
734 parse_grid_3(s, ch1, ch2, sb_g3, flag);
735 } else if (sb < 12 && sb_reorder >= 4) {
736 parse_grid_3(s, ch1, ch2, sb_reorder - 4, flag);
739 // Secondary channel flags
741 if (ensure_bits(&s->gb, 20))
743 if (!flag || sb_reorder >= s->max_mono_subband)
744 s->sec_ch_sbms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
745 if (flag && sb_reorder >= s->min_mono_subband)
746 s->sec_ch_lrms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
749 quant_level = s->quant_levels[ch1 / 2][sb];
751 return AVERROR_INVALIDDATA;
753 // Time samples for one or both channels
754 if (sb < s->max_mono_subband && sb_reorder >= s->min_mono_subband) {
756 parse_ch(s, ch1, sb_reorder, quant_level, 0);
758 parse_ch(s, ch2, sb_reorder, quant_level, 1);
760 parse_ch(s, ch1, sb_reorder, quant_level, 0);
762 parse_ch(s, ch2, sb_reorder, quant_level, 0);
770 * Convert from reflection coefficients to direct form coefficients
772 static void convert_lpc(float *coeff, const int *codes)
776 for (i = 0; i < 8; i++) {
777 float rc = lpc_tab[codes[i]];
778 for (j = 0; j < (i + 1) / 2; j++) {
779 float tmp1 = coeff[ j ];
780 float tmp2 = coeff[i - j - 1];
781 coeff[ j ] = tmp1 + rc * tmp2;
782 coeff[i - j - 1] = tmp2 + rc * tmp1;
788 static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb)
790 int f = s->framenum & 1;
791 int i, sb, ch, codes[16];
793 // First two subbands have two sets of coefficients, third subband has one
794 for (sb = start_sb; sb < end_sb; sb++) {
795 int ncodes = 8 * (1 + (sb < 2));
796 for (ch = ch1; ch <= ch2; ch++) {
797 if (ensure_bits(&s->gb, 4 * ncodes))
799 for (i = 0; i < ncodes; i++)
800 codes[i] = get_bits(&s->gb, 4);
801 for (i = 0; i < ncodes / 8; i++)
802 convert_lpc(s->lpc_coeff[f][ch][sb][i], &codes[i * 8]);
809 static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
811 int quant_levels[DCA_LBR_SUBBANDS];
812 int sb, ch, ol, st, max_sb, profile, ret;
817 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
822 profile = get_bits(&s->gb, 8);
824 ol = (profile >> 3) & 7;
827 // Max energy subband
828 max_sb = profile & 7;
830 // Calculate quantization levels
831 for (sb = 0; sb < s->nsubbands; sb++) {
832 int f = sb * s->limited_rate / s->nsubbands;
833 int a = 18000 / (12 * f / 1000 + 100 + 40 * st) + 20 * ol;
835 quant_levels[sb] = 1;
837 quant_levels[sb] = 2;
839 quant_levels[sb] = 3;
841 quant_levels[sb] = 4;
843 quant_levels[sb] = 5;
846 // Reorder quantization levels for lower subbands
847 for (sb = 0; sb < 8; sb++)
848 s->quant_levels[ch1 / 2][sb] = quant_levels[ff_dca_sb_reorder[max_sb][sb]];
849 for (; sb < s->nsubbands; sb++)
850 s->quant_levels[ch1 / 2][sb] = quant_levels[sb];
852 // LPC for the first two subbands
853 ret = parse_lpc(s, ch1, ch2, 0, 2);
857 // Time-samples for the first two subbands of main channel
858 ret = parse_ts(s, ch1, ch2, 0, 2, 0);
862 // First two bands of the first grid
863 for (sb = 0; sb < 2; sb++)
864 for (ch = ch1; ch <= ch2; ch++)
865 if ((ret = parse_scale_factors(s, s->grid_1_scf[ch][sb])) < 0)
871 static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2,
872 int start_sb, int end_sb, int flag)
874 int i, j, sb, ch, nsubbands;
876 nsubbands = ff_dca_scf_to_grid_2[s->nsubbands - 1] + 1;
877 if (end_sb > nsubbands)
880 for (sb = start_sb; sb < end_sb; sb++) {
881 for (ch = ch1; ch <= ch2; ch++) {
882 uint8_t *g2_scf = s->grid_2_scf[ch][sb];
884 if ((ch != ch1 && ff_dca_grid_2_to_scf[sb] >= s->min_mono_subband) != flag) {
886 memcpy(g2_scf, s->grid_2_scf[ch1][sb], 64);
890 // Scale factors in groups of 8
891 for (i = 0; i < 8; i++, g2_scf += 8) {
892 if (get_bits_left(&s->gb) < 1) {
893 memset(g2_scf, 0, 64 - i * 8);
896 // Bit indicating if whole group has zero values
897 if (get_bits1(&s->gb)) {
898 for (j = 0; j < 8; j++) {
899 if (ensure_bits(&s->gb, 20))
901 g2_scf[j] = parse_vlc(&s->gb, &ff_dca_vlc_grid_2, 2);
904 memset(g2_scf, 0, 8);
913 static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
918 if ((ret = init_get_bits8(&s->gb, chunk->data, chunk->len)) < 0)
920 if ((ret = parse_lpc(s, ch1, ch2, 2, 3)) < 0)
922 if ((ret = parse_ts(s, ch1, ch2, 2, 4, 0)) < 0)
924 if ((ret = parse_grid_2(s, ch1, ch2, 0, 1, 0)) < 0)
926 if ((ret = parse_ts(s, ch1, ch2, 4, 6, 0)) < 0)
931 static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
937 if ((ret = init_get_bits8(&s->gb, chunk->data, chunk->len)) < 0)
939 if ((ret = parse_grid_2(s, ch1, ch2, 1, 3, 0)) < 0)
941 if ((ret = parse_ts(s, ch1, ch2, 6, s->max_mono_subband, 0)) < 0)
944 if ((ret = parse_grid_1_sec_ch(s, ch2)) < 0)
946 if ((ret = parse_grid_2(s, ch1, ch2, 0, 3, 1)) < 0)
949 if ((ret = parse_ts(s, ch1, ch2, s->min_mono_subband, s->nsubbands, 1)) < 0)
954 static int init_sample_rate(DCALbrDecoder *s)
956 double scale = (-1.0 / (1 << 17)) * sqrt(1 << (2 - s->limited_range));
957 int i, br_per_ch = s->bit_rate_scaled / s->nchannels_total;
960 ff_mdct_end(&s->imdct);
962 ret = ff_mdct_init(&s->imdct, s->freq_range + 6, 1, scale);
966 for (i = 0; i < 32 << s->freq_range; i++)
967 s->window[i] = ff_dca_long_window[i << (2 - s->freq_range)];
969 if (br_per_ch < 14000)
971 else if (br_per_ch < 32000)
972 scale = (br_per_ch - 14000) * (1.0 / 120000) + 0.85;
976 scale *= 1.0 / INT_MAX;
978 for (i = 0; i < s->nsubbands; i++) {
980 s->sb_scf[i] = 0; // The first two subbands are always zero
982 s->sb_scf[i] = (i - 1) * 0.25 * 0.785 * scale;
984 s->sb_scf[i] = 0.785 * scale;
987 s->lfe_scale = (16 << s->freq_range) * 0.0000078265894;
992 static int alloc_sample_buffer(DCALbrDecoder *s)
994 // Reserve space for history and padding
995 int nchsamples = DCA_LBR_TIME_SAMPLES + DCA_LBR_TIME_HISTORY * 2;
996 int nsamples = nchsamples * s->nchannels * s->nsubbands;
1000 // Reallocate time sample buffer
1001 av_fast_mallocz(&s->ts_buffer, &s->ts_size, nsamples * sizeof(float));
1003 return AVERROR(ENOMEM);
1005 ptr = s->ts_buffer + DCA_LBR_TIME_HISTORY;
1006 for (ch = 0; ch < s->nchannels; ch++) {
1007 for (sb = 0; sb < s->nsubbands; sb++) {
1008 s->time_samples[ch][sb] = ptr;
1016 static int parse_decoder_init(DCALbrDecoder *s, GetByteContext *gb)
1018 int old_rate = s->sample_rate;
1019 int old_band_limit = s->band_limit;
1020 int old_nchannels = s->nchannels;
1021 int version, bit_rate_hi;
1022 unsigned int sr_code;
1024 // Sample rate of LBR audio
1025 sr_code = bytestream2_get_byte(gb);
1026 if (sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) {
1027 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sample rate\n");
1028 return AVERROR_INVALIDDATA;
1030 s->sample_rate = ff_dca_sampling_freqs[sr_code];
1031 if (s->sample_rate > 48000) {
1032 avpriv_report_missing_feature(s->avctx, "%d Hz LBR sample rate", s->sample_rate);
1033 return AVERROR_PATCHWELCOME;
1037 s->ch_mask = bytestream2_get_le16(gb);
1038 if (!(s->ch_mask & 0x7)) {
1039 avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1040 return AVERROR_PATCHWELCOME;
1042 if ((s->ch_mask & 0xfff0) && !(s->warned & 1)) {
1043 avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1047 // LBR bitstream version
1048 version = bytestream2_get_le16(gb);
1049 if ((version & 0xff00) != 0x0800) {
1050 avpriv_report_missing_feature(s->avctx, "LBR stream version %#x", version);
1051 return AVERROR_PATCHWELCOME;
1054 // Flags for LBR decoder initialization
1055 s->flags = bytestream2_get_byte(gb);
1056 if (s->flags & LBR_FLAG_DMIX_MULTI_CH) {
1057 avpriv_report_missing_feature(s->avctx, "LBR multi-channel downmix");
1058 return AVERROR_PATCHWELCOME;
1060 if ((s->flags & LBR_FLAG_LFE_PRESENT) && s->sample_rate != 48000) {
1061 if (!(s->warned & 2)) {
1062 avpriv_report_missing_feature(s->avctx, "%d Hz LFE interpolation", s->sample_rate);
1065 s->flags &= ~LBR_FLAG_LFE_PRESENT;
1068 // Most significant bit rate nibbles
1069 bit_rate_hi = bytestream2_get_byte(gb);
1071 // Least significant original bit rate word
1072 s->bit_rate_orig = bytestream2_get_le16(gb) | ((bit_rate_hi & 0x0F) << 16);
1074 // Least significant scaled bit rate word
1075 s->bit_rate_scaled = bytestream2_get_le16(gb) | ((bit_rate_hi & 0xF0) << 12);
1077 // Setup number of fullband channels
1078 s->nchannels_total = ff_dca_count_chs_for_mask(s->ch_mask & ~DCA_SPEAKER_PAIR_LFE1);
1079 s->nchannels = FFMIN(s->nchannels_total, DCA_LBR_CHANNELS);
1082 switch (s->flags & LBR_FLAG_BAND_LIMIT_MASK) {
1083 case LBR_FLAG_BAND_LIMIT_NONE:
1086 case LBR_FLAG_BAND_LIMIT_1_2:
1089 case LBR_FLAG_BAND_LIMIT_1_4:
1093 avpriv_report_missing_feature(s->avctx, "LBR band limit %#x", s->flags & LBR_FLAG_BAND_LIMIT_MASK);
1094 return AVERROR_PATCHWELCOME;
1097 // Setup frequency range
1098 s->freq_range = ff_dca_freq_ranges[sr_code];
1100 // Setup resolution profile
1101 if (s->bit_rate_orig >= 44000 * (s->nchannels_total + 2))
1103 else if (s->bit_rate_orig >= 25000 * (s->nchannels_total + 2))
1108 // Setup limited sample rate, number of subbands, etc
1109 s->limited_rate = s->sample_rate >> s->band_limit;
1110 s->limited_range = s->freq_range - s->band_limit;
1111 if (s->limited_range < 0) {
1112 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR band limit for frequency range\n");
1113 return AVERROR_INVALIDDATA;
1116 s->nsubbands = 8 << s->limited_range;
1118 s->g3_avg_only_start_sb = s->nsubbands * ff_dca_avg_g3_freqs[s->res_profile] / (s->limited_rate / 2);
1119 if (s->g3_avg_only_start_sb > s->nsubbands)
1120 s->g3_avg_only_start_sb = s->nsubbands;
1122 s->min_mono_subband = s->nsubbands * 2000 / (s->limited_rate / 2);
1123 if (s->min_mono_subband > s->nsubbands)
1124 s->min_mono_subband = s->nsubbands;
1126 s->max_mono_subband = s->nsubbands * 14000 / (s->limited_rate / 2);
1127 if (s->max_mono_subband > s->nsubbands)
1128 s->max_mono_subband = s->nsubbands;
1130 // Handle change of sample rate
1131 if ((old_rate != s->sample_rate || old_band_limit != s->band_limit) && init_sample_rate(s) < 0)
1132 return AVERROR(ENOMEM);
1134 // Setup stereo downmix
1135 if (s->flags & LBR_FLAG_DMIX_STEREO) {
1136 DCAContext *dca = s->avctx->priv_data;
1138 if (s->nchannels_total < 3 || s->nchannels_total > DCA_LBR_CHANNELS_TOTAL - 2) {
1139 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels for LBR stereo downmix\n");
1140 return AVERROR_INVALIDDATA;
1143 // This decoder doesn't support ECS chunk
1144 if (dca->request_channel_layout != DCA_SPEAKER_LAYOUT_STEREO && !(s->warned & 4)) {
1145 avpriv_report_missing_feature(s->avctx, "Embedded LBR stereo downmix");
1149 // Account for extra downmixed channel pair
1150 s->nchannels_total += 2;
1152 s->ch_mask = DCA_SPEAKER_PAIR_LR;
1153 s->flags &= ~LBR_FLAG_LFE_PRESENT;
1156 // Handle change of sample rate or number of channels
1157 if (old_rate != s->sample_rate
1158 || old_band_limit != s->band_limit
1159 || old_nchannels != s->nchannels) {
1160 if (alloc_sample_buffer(s) < 0)
1161 return AVERROR(ENOMEM);
1162 ff_dca_lbr_flush(s);
1168 int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset)
1173 LBRChunk tonal_grp[5];
1174 LBRChunk grid1[DCA_LBR_CHANNELS / 2];
1175 LBRChunk hr_grid[DCA_LBR_CHANNELS / 2];
1176 LBRChunk ts1[DCA_LBR_CHANNELS / 2];
1177 LBRChunk ts2[DCA_LBR_CHANNELS / 2];
1182 int i, ch, sb, sf, ret, group, chunk_id, chunk_len;
1184 bytestream2_init(&gb, data + asset->lbr_offset, asset->lbr_size);
1187 if (bytestream2_get_be32(&gb) != DCA_SYNCWORD_LBR) {
1188 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sync word\n");
1189 return AVERROR_INVALIDDATA;
1193 switch (bytestream2_get_byte(&gb)) {
1194 case DCA_LBR_HEADER_SYNC_ONLY:
1195 if (!s->sample_rate) {
1196 av_log(s->avctx, AV_LOG_ERROR, "LBR decoder not initialized\n");
1197 return AVERROR_INVALIDDATA;
1200 case DCA_LBR_HEADER_DECODER_INIT:
1201 if ((ret = parse_decoder_init(s, &gb)) < 0) {
1207 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR header type\n");
1208 return AVERROR_INVALIDDATA;
1211 // LBR frame chunk header
1212 chunk_id = bytestream2_get_byte(&gb);
1213 chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1215 if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1216 chunk_len = bytestream2_get_bytes_left(&gb);
1217 av_log(s->avctx, AV_LOG_WARNING, "LBR frame chunk was truncated\n");
1218 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1219 return AVERROR_INVALIDDATA;
1222 bytestream2_init(&gb, gb.buffer, chunk_len);
1224 switch (chunk_id & 0x7f) {
1225 case LBR_CHUNK_FRAME:
1226 if (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) {
1227 int checksum = bytestream2_get_be16(&gb);
1228 uint16_t res = chunk_id;
1229 res += (chunk_len >> 8) & 0xff;
1230 res += chunk_len & 0xff;
1231 for (i = 0; i < chunk_len - 2; i++)
1232 res += gb.buffer[i];
1233 if (checksum != res) {
1234 av_log(s->avctx, AV_LOG_WARNING, "Invalid LBR checksum\n");
1235 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1236 return AVERROR_INVALIDDATA;
1239 bytestream2_skip(&gb, 2);
1242 case LBR_CHUNK_FRAME_NO_CSUM:
1245 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR frame chunk ID\n");
1246 return AVERROR_INVALIDDATA;
1249 // Clear current frame
1250 memset(s->quant_levels, 0, sizeof(s->quant_levels));
1251 memset(s->sb_indices, 0xff, sizeof(s->sb_indices));
1252 memset(s->sec_ch_sbms, 0, sizeof(s->sec_ch_sbms));
1253 memset(s->sec_ch_lrms, 0, sizeof(s->sec_ch_lrms));
1254 memset(s->ch_pres, 0, sizeof(s->ch_pres));
1255 memset(s->grid_1_scf, 0, sizeof(s->grid_1_scf));
1256 memset(s->grid_2_scf, 0, sizeof(s->grid_2_scf));
1257 memset(s->grid_3_avg, 0, sizeof(s->grid_3_avg));
1258 memset(s->grid_3_scf, 0, sizeof(s->grid_3_scf));
1259 memset(s->grid_3_pres, 0, sizeof(s->grid_3_pres));
1260 memset(s->tonal_scf, 0, sizeof(s->tonal_scf));
1261 memset(s->lfe_data, 0, sizeof(s->lfe_data));
1262 s->part_stereo_pres = 0;
1263 s->framenum = (s->framenum + 1) & 31;
1265 for (ch = 0; ch < s->nchannels; ch++) {
1266 for (sb = 0; sb < s->nsubbands / 4; sb++) {
1267 s->part_stereo[ch][sb][0] = s->part_stereo[ch][sb][4];
1268 s->part_stereo[ch][sb][4] = 16;
1272 memset(s->lpc_coeff[s->framenum & 1], 0, sizeof(s->lpc_coeff[0]));
1274 for (group = 0; group < 5; group++) {
1275 for (sf = 0; sf < 1 << group; sf++) {
1276 int sf_idx = ((s->framenum << group) + sf) & 31;
1277 s->tonal_bounds[group][sf_idx][0] =
1278 s->tonal_bounds[group][sf_idx][1] = s->ntones;
1282 // Parse chunk headers
1283 while (bytestream2_get_bytes_left(&gb) > 0) {
1284 chunk_id = bytestream2_get_byte(&gb);
1285 chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1288 if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1289 chunk_len = bytestream2_get_bytes_left(&gb);
1290 av_log(s->avctx, AV_LOG_WARNING, "LBR chunk %#x was truncated\n", chunk_id);
1291 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1292 return AVERROR_INVALIDDATA;
1297 chunk.lfe.len = chunk_len;
1298 chunk.lfe.data = gb.buffer;
1302 case LBR_CHUNK_TONAL:
1303 case LBR_CHUNK_TONAL_SCF:
1304 chunk.tonal.id = chunk_id;
1305 chunk.tonal.len = chunk_len;
1306 chunk.tonal.data = gb.buffer;
1309 case LBR_CHUNK_TONAL_GRP_1:
1310 case LBR_CHUNK_TONAL_GRP_2:
1311 case LBR_CHUNK_TONAL_GRP_3:
1312 case LBR_CHUNK_TONAL_GRP_4:
1313 case LBR_CHUNK_TONAL_GRP_5:
1314 i = LBR_CHUNK_TONAL_GRP_5 - chunk_id;
1315 chunk.tonal_grp[i].id = i;
1316 chunk.tonal_grp[i].len = chunk_len;
1317 chunk.tonal_grp[i].data = gb.buffer;
1320 case LBR_CHUNK_TONAL_SCF_GRP_1:
1321 case LBR_CHUNK_TONAL_SCF_GRP_2:
1322 case LBR_CHUNK_TONAL_SCF_GRP_3:
1323 case LBR_CHUNK_TONAL_SCF_GRP_4:
1324 case LBR_CHUNK_TONAL_SCF_GRP_5:
1325 i = LBR_CHUNK_TONAL_SCF_GRP_5 - chunk_id;
1326 chunk.tonal_grp[i].id = i;
1327 chunk.tonal_grp[i].len = chunk_len;
1328 chunk.tonal_grp[i].data = gb.buffer;
1331 case LBR_CHUNK_RES_GRID_LR:
1332 case LBR_CHUNK_RES_GRID_LR + 1:
1333 case LBR_CHUNK_RES_GRID_LR + 2:
1334 i = chunk_id - LBR_CHUNK_RES_GRID_LR;
1335 chunk.grid1[i].len = chunk_len;
1336 chunk.grid1[i].data = gb.buffer;
1339 case LBR_CHUNK_RES_GRID_HR:
1340 case LBR_CHUNK_RES_GRID_HR + 1:
1341 case LBR_CHUNK_RES_GRID_HR + 2:
1342 i = chunk_id - LBR_CHUNK_RES_GRID_HR;
1343 chunk.hr_grid[i].len = chunk_len;
1344 chunk.hr_grid[i].data = gb.buffer;
1347 case LBR_CHUNK_RES_TS_1:
1348 case LBR_CHUNK_RES_TS_1 + 1:
1349 case LBR_CHUNK_RES_TS_1 + 2:
1350 i = chunk_id - LBR_CHUNK_RES_TS_1;
1351 chunk.ts1[i].len = chunk_len;
1352 chunk.ts1[i].data = gb.buffer;
1355 case LBR_CHUNK_RES_TS_2:
1356 case LBR_CHUNK_RES_TS_2 + 1:
1357 case LBR_CHUNK_RES_TS_2 + 2:
1358 i = chunk_id - LBR_CHUNK_RES_TS_2;
1359 chunk.ts2[i].len = chunk_len;
1360 chunk.ts2[i].data = gb.buffer;
1364 bytestream2_skip(&gb, chunk_len);
1368 ret = parse_lfe_chunk(s, &chunk.lfe);
1370 ret |= parse_tonal_chunk(s, &chunk.tonal);
1372 for (i = 0; i < 5; i++)
1373 ret |= parse_tonal_group(s, &chunk.tonal_grp[i]);
1375 for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1377 int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1379 if (parse_grid_1_chunk (s, &chunk.grid1 [i], ch1, ch2) < 0 ||
1380 parse_high_res_grid(s, &chunk.hr_grid[i], ch1, ch2) < 0) {
1385 // TS chunks depend on both grids. TS_2 depends on TS_1.
1386 if (!chunk.grid1[i].len || !chunk.hr_grid[i].len || !chunk.ts1[i].len)
1389 if (parse_ts1_chunk(s, &chunk.ts1[i], ch1, ch2) < 0 ||
1390 parse_ts2_chunk(s, &chunk.ts2[i], ch1, ch2) < 0) {
1396 if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1397 return AVERROR_INVALIDDATA;
1403 * Reconstruct high-frequency resolution grid from first and third grids
1405 static void decode_grid(DCALbrDecoder *s, int ch1, int ch2)
1409 for (ch = ch1; ch <= ch2; ch++) {
1410 for (sb = 0; sb < s->nsubbands; sb++) {
1411 int g1_sb = ff_dca_scf_to_grid_1[sb];
1413 uint8_t *g1_scf_a = s->grid_1_scf[ch][g1_sb ];
1414 uint8_t *g1_scf_b = s->grid_1_scf[ch][g1_sb + 1];
1416 int w1 = ff_dca_grid_1_weights[g1_sb ][sb];
1417 int w2 = ff_dca_grid_1_weights[g1_sb + 1][sb];
1419 uint8_t *hr_scf = s->high_res_scf[ch][sb];
1422 for (i = 0; i < 8; i++) {
1423 int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1424 hr_scf[i] = scf >> 7;
1427 int8_t *g3_scf = s->grid_3_scf[ch][sb - 4];
1428 int g3_avg = s->grid_3_avg[ch][sb - 4];
1430 for (i = 0; i < 8; i++) {
1431 int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1432 hr_scf[i] = (scf >> 7) - g3_avg - g3_scf[i];
1440 * Fill unallocated subbands with randomness
1442 static void random_ts(DCALbrDecoder *s, int ch1, int ch2)
1444 int i, j, k, ch, sb;
1446 for (ch = ch1; ch <= ch2; ch++) {
1447 for (sb = 0; sb < s->nsubbands; sb++) {
1448 float *samples = s->time_samples[ch][sb];
1450 if (s->ch_pres[ch] & (1U << sb))
1451 continue; // Skip allocated subband
1454 // The first two subbands are always zero
1455 memset(samples, 0, DCA_LBR_TIME_SAMPLES * sizeof(float));
1456 } else if (sb < 10) {
1457 for (i = 0; i < DCA_LBR_TIME_SAMPLES; i++)
1458 samples[i] = lbr_rand(s, sb);
1460 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 8; i++, samples += 8) {
1461 float accum[8] = { 0 };
1463 // Modulate by subbands 2-5 in blocks of 8
1464 for (k = 2; k < 6; k++) {
1465 float *other = &s->time_samples[ch][k][i * 8];
1466 for (j = 0; j < 8; j++)
1467 accum[j] += fabs(other[j]);
1470 for (j = 0; j < 8; j++)
1471 samples[j] = (accum[j] * 0.25f + 0.5f) * lbr_rand(s, sb);
1478 static void predict(float *samples, const float *coeff, int nsamples)
1482 for (i = 0; i < nsamples; i++) {
1484 for (j = 0; j < 8; j++)
1485 res += coeff[j] * samples[i - j - 1];
1490 static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb)
1492 int f = s->framenum & 1;
1495 for (ch = ch1; ch <= ch2; ch++) {
1496 float *samples = s->time_samples[ch][sb];
1498 if (!(s->ch_pres[ch] & (1U << sb)))
1502 predict(samples, s->lpc_coeff[f^1][ch][sb][1], 16);
1503 predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 64);
1504 predict(samples + 80, s->lpc_coeff[f ][ch][sb][1], 48);
1506 predict(samples, s->lpc_coeff[f^1][ch][sb][0], 16);
1507 predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 112);
1512 static void filter_ts(DCALbrDecoder *s, int ch1, int ch2)
1516 for (sb = 0; sb < s->nsubbands; sb++) {
1518 for (ch = ch1; ch <= ch2; ch++) {
1519 float *samples = s->time_samples[ch][sb];
1520 uint8_t *hr_scf = s->high_res_scf[ch][sb];
1522 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++, samples += 16) {
1523 unsigned int scf = hr_scf[i];
1526 for (j = 0; j < 16; j++)
1527 samples[j] *= ff_dca_quant_amp[scf];
1530 uint8_t *g2_scf = s->grid_2_scf[ch][ff_dca_scf_to_grid_2[sb]];
1531 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 2; i++, samples += 2) {
1532 unsigned int scf = hr_scf[i / 8] - g2_scf[i];
1535 samples[0] *= ff_dca_quant_amp[scf];
1536 samples[1] *= ff_dca_quant_amp[scf];
1543 float *samples_l = s->time_samples[ch1][sb];
1544 float *samples_r = s->time_samples[ch2][sb];
1545 int ch2_pres = s->ch_pres[ch2] & (1U << sb);
1547 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++) {
1548 int sbms = (s->sec_ch_sbms[ch1 / 2][sb] >> i) & 1;
1549 int lrms = (s->sec_ch_lrms[ch1 / 2][sb] >> i) & 1;
1551 if (sb >= s->min_mono_subband) {
1552 if (lrms && ch2_pres) {
1554 for (j = 0; j < 16; j++) {
1555 float tmp = samples_l[j];
1556 samples_l[j] = samples_r[j];
1557 samples_r[j] = -tmp;
1560 for (j = 0; j < 16; j++) {
1561 float tmp = samples_l[j];
1562 samples_l[j] = samples_r[j];
1566 } else if (!ch2_pres) {
1567 if (sbms && (s->part_stereo_pres & (1 << ch1))) {
1568 for (j = 0; j < 16; j++)
1569 samples_r[j] = -samples_l[j];
1571 for (j = 0; j < 16; j++)
1572 samples_r[j] = samples_l[j];
1575 } else if (sbms && ch2_pres) {
1576 for (j = 0; j < 16; j++) {
1577 float tmp = samples_l[j];
1578 samples_l[j] = (tmp + samples_r[j]) * 0.5f;
1579 samples_r[j] = (tmp - samples_r[j]) * 0.5f;
1588 // Inverse prediction
1590 synth_lpc(s, ch1, ch2, sb);
1595 * Modulate by interpolated partial stereo coefficients
1597 static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2)
1601 for (ch = ch1; ch <= ch2; ch++) {
1602 for (sb = s->min_mono_subband; sb < s->nsubbands; sb++) {
1603 uint8_t *pt_st = s->part_stereo[ch][(sb - s->min_mono_subband) / 4];
1604 float *samples = s->time_samples[ch][sb];
1606 if (s->ch_pres[ch2] & (1U << sb))
1609 for (sf = 1; sf <= 4; sf++, samples += 32) {
1610 float prev = ff_dca_st_coeff[pt_st[sf - 1]];
1611 float next = ff_dca_st_coeff[pt_st[sf ]];
1613 for (i = 0; i < 32; i++)
1614 samples[i] *= (32 - i) * prev + i * next;
1621 * Synthesise tones in the given group for the given tonal subframe
1623 static void synth_tones(DCALbrDecoder *s, int ch, float *values,
1624 int group, int group_sf, int synth_idx)
1626 int i, start, count;
1631 start = s->tonal_bounds[group][group_sf][0];
1632 count = (s->tonal_bounds[group][group_sf][1] - start) & (DCA_LBR_TONES - 1);
1634 for (i = 0; i < count; i++) {
1635 DCALbrTone *t = &s->tones[(start + i) & (DCA_LBR_TONES - 1)];
1638 float amp = ff_dca_synth_env[synth_idx] * ff_dca_quant_amp[t->amp[ch]];
1639 float c = amp * cos_tab[(t->phs[ch] ) & 255];
1640 float s = amp * cos_tab[(t->phs[ch] + 64) & 255];
1641 const float *cf = ff_dca_corr_cf[t->f_delt];
1642 int x_freq = t->x_freq;
1648 values[3] += cf[0] * -s;
1649 values[2] += cf[1] * c;
1650 values[1] += cf[2] * s;
1651 values[0] += cf[3] * -c;
1654 values[2] += cf[0] * -s;
1655 values[1] += cf[1] * c;
1656 values[0] += cf[2] * s;
1659 values[1] += cf[0] * -s;
1660 values[0] += cf[1] * c;
1663 values[0] += cf[0] * -s;
1667 values[x_freq - 5] += cf[ 0] * -s;
1668 p4: values[x_freq - 4] += cf[ 1] * c;
1669 p3: values[x_freq - 3] += cf[ 2] * s;
1670 p2: values[x_freq - 2] += cf[ 3] * -c;
1671 p1: values[x_freq - 1] += cf[ 4] * -s;
1672 p0: values[x_freq ] += cf[ 5] * c;
1673 values[x_freq + 1] += cf[ 6] * s;
1674 values[x_freq + 2] += cf[ 7] * -c;
1675 values[x_freq + 3] += cf[ 8] * -s;
1676 values[x_freq + 4] += cf[ 9] * c;
1677 values[x_freq + 5] += cf[10] * s;
1680 t->phs[ch] += t->ph_rot;
1685 * Synthesise all tones in all groups for the given residual subframe
1687 static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf)
1691 // Tonal vs residual shift is 22 subframes
1692 for (group = 0; group < 5; group++) {
1693 int group_sf = (s->framenum << group) + ((sf - 22) >> (5 - group));
1694 int synth_idx = ((((sf - 22) & 31) << group) & 31) + (1 << group) - 1;
1696 synth_tones(s, ch, values, group, (group_sf - 1) & 31, 30 - synth_idx);
1697 synth_tones(s, ch, values, group, (group_sf ) & 31, synth_idx);
1701 static void transform_channel(DCALbrDecoder *s, int ch, float *output)
1703 LOCAL_ALIGNED_32(float, values, [DCA_LBR_SUBBANDS ], [4]);
1704 LOCAL_ALIGNED_32(float, result, [DCA_LBR_SUBBANDS * 2], [4]);
1705 int sf, sb, nsubbands = s->nsubbands, noutsubbands = 8 << s->freq_range;
1707 // Clear inactive subbands
1708 if (nsubbands < noutsubbands)
1709 memset(values[nsubbands], 0, (noutsubbands - nsubbands) * sizeof(values[0]));
1711 for (sf = 0; sf < DCA_LBR_TIME_SAMPLES / 4; sf++) {
1712 // Hybrid filterbank
1713 s->dcadsp->lbr_bank(values, s->time_samples[ch],
1714 ff_dca_bank_coeff, sf * 4, nsubbands);
1716 base_func_synth(s, ch, values[0], sf);
1718 s->imdct.imdct_calc(&s->imdct, result[0], values[0]);
1720 // Long window and overlap-add
1721 s->fdsp->vector_fmul_add(output, result[0], s->window,
1722 s->history[ch], noutsubbands * 4);
1723 s->fdsp->vector_fmul_reverse(s->history[ch], result[noutsubbands],
1724 s->window, noutsubbands * 4);
1725 output += noutsubbands * 4;
1728 // Update history for LPC and forward MDCT
1729 for (sb = 0; sb < nsubbands; sb++) {
1730 float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1731 memcpy(samples, samples + DCA_LBR_TIME_SAMPLES, DCA_LBR_TIME_HISTORY * sizeof(float));
1735 int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
1737 AVCodecContext *avctx = s->avctx;
1738 int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1;
1739 const int8_t *reorder;
1741 avctx->channel_layout = channel_layouts[ch_conf];
1742 avctx->channels = nchannels = channel_counts[ch_conf];
1743 avctx->sample_rate = s->sample_rate;
1744 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1745 avctx->bits_per_raw_sample = 0;
1746 avctx->profile = FF_PROFILE_DTS_EXPRESS;
1747 avctx->bit_rate = s->bit_rate_scaled;
1749 if (s->flags & LBR_FLAG_LFE_PRESENT) {
1750 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1752 reorder = channel_reorder_lfe[ch_conf];
1754 reorder = channel_reorder_nolfe[ch_conf];
1757 frame->nb_samples = 1024 << s->freq_range;
1758 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1761 // Filter fullband channels
1762 for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1764 int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1766 decode_grid(s, ch1, ch2);
1768 random_ts(s, ch1, ch2);
1770 filter_ts(s, ch1, ch2);
1772 if (ch1 != ch2 && (s->part_stereo_pres & (1 << ch1)))
1773 decode_part_stereo(s, ch1, ch2);
1775 if (ch1 < nchannels)
1776 transform_channel(s, ch1, (float *)frame->extended_data[reorder[ch1]]);
1778 if (ch1 != ch2 && ch2 < nchannels)
1779 transform_channel(s, ch2, (float *)frame->extended_data[reorder[ch2]]);
1782 // Interpolate LFE channel
1783 if (s->flags & LBR_FLAG_LFE_PRESENT) {
1784 s->dcadsp->lfe_iir((float *)frame->extended_data[lfe_index[ch_conf]],
1785 s->lfe_data, ff_dca_lfe_iir,
1786 s->lfe_history, 16 << s->freq_range);
1789 if ((ret = ff_side_data_update_matrix_encoding(frame, AV_MATRIX_ENCODING_NONE)) < 0)
1795 av_cold void ff_dca_lbr_flush(DCALbrDecoder *s)
1799 if (!s->sample_rate)
1803 memset(s->part_stereo, 16, sizeof(s->part_stereo));
1804 memset(s->lpc_coeff, 0, sizeof(s->lpc_coeff));
1805 memset(s->history, 0, sizeof(s->history));
1806 memset(s->tonal_bounds, 0, sizeof(s->tonal_bounds));
1807 memset(s->lfe_history, 0, sizeof(s->lfe_history));
1811 for (ch = 0; ch < s->nchannels; ch++) {
1812 for (sb = 0; sb < s->nsubbands; sb++) {
1813 float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1814 memset(samples, 0, DCA_LBR_TIME_HISTORY * sizeof(float));
1819 av_cold int ff_dca_lbr_init(DCALbrDecoder *s)
1823 if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
1824 return AVERROR(ENOMEM);
1830 av_cold void ff_dca_lbr_close(DCALbrDecoder *s)
1834 av_freep(&s->ts_buffer);
1838 ff_mdct_end(&s->imdct);