Browse code

avcodec/dca: add DTS Express (LBR) decoder

Signed-off-by: James Almer <jamrial@gmail.com>

foo86 authored on 2016/05/02 00:43:00
Showing 12 changed files
... ...
@@ -31,6 +31,7 @@ version <next>:
31 31
 - Duck TrueMotion 2.0 Real Time decoder
32 32
 - Wideband Single-bit Data (WSD) demuxer
33 33
 - VAAPI-accelerated H.264/HEVC/MJPEG encoding
34
+- DTS Express (LBR) decoder
34 35
 
35 36
 version 3.0:
36 37
 - Common Encryption (CENC) MP4 encoding and decoding support
... ...
@@ -232,7 +232,7 @@ OBJS-$(CONFIG_CPIA_DECODER)            += cpia.o
232 232
 OBJS-$(CONFIG_CSCD_DECODER)            += cscd.o
233 233
 OBJS-$(CONFIG_CYUV_DECODER)            += cyuv.o
234 234
 OBJS-$(CONFIG_DCA_DECODER)             += dcadec.o dca.o dcadata.o dcahuff.o \
235
-                                          dca_core.o dca_exss.o dca_xll.o \
235
+                                          dca_core.o dca_exss.o dca_xll.o dca_lbr.o \
236 236
                                           dcadsp.o dcadct.o synth_filter.o
237 237
 OBJS-$(CONFIG_DCA_ENCODER)             += dcaenc.o dca.o dcadata.o
238 238
 OBJS-$(CONFIG_DDS_DECODER)             += dds.o
239 239
new file mode 100644
... ...
@@ -0,0 +1,1825 @@
0
+/*
1
+ * Copyright (C) 2016 foo86
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#define UNCHECKED_BITSTREAM_READER  1
21
+#define BITSTREAM_READER_LE
22
+
23
+#include "libavutil/channel_layout.h"
24
+
25
+#include "dcadec.h"
26
+#include "dcadata.h"
27
+#include "dcahuff.h"
28
+#include "dca_syncwords.h"
29
+#include "bytestream.h"
30
+
31
+#define AMP_MAX     56
32
+
33
+enum LBRHeader {
34
+    LBR_HEADER_SYNC_ONLY    = 1,
35
+    LBR_HEADER_DECODER_INIT = 2
36
+};
37
+
38
+enum LBRFlags {
39
+    LBR_FLAG_24_BIT             = 0x01,
40
+    LBR_FLAG_LFE_PRESENT        = 0x02,
41
+    LBR_FLAG_BAND_LIMIT_2_3     = 0x04,
42
+    LBR_FLAG_BAND_LIMIT_1_2     = 0x08,
43
+    LBR_FLAG_BAND_LIMIT_1_3     = 0x0c,
44
+    LBR_FLAG_BAND_LIMIT_1_4     = 0x10,
45
+    LBR_FLAG_BAND_LIMIT_1_8     = 0x18,
46
+    LBR_FLAG_BAND_LIMIT_NONE    = 0x14,
47
+    LBR_FLAG_BAND_LIMIT_MASK    = 0x1c,
48
+    LBR_FLAG_DMIX_STEREO        = 0x20,
49
+    LBR_FLAG_DMIX_MULTI_CH      = 0x40
50
+};
51
+
52
+enum LBRChunkTypes {
53
+    LBR_CHUNK_NULL              = 0x00,
54
+    LBR_CHUNK_PAD               = 0x01,
55
+    LBR_CHUNK_FRAME             = 0x04,
56
+    LBR_CHUNK_FRAME_NO_CSUM     = 0x06,
57
+    LBR_CHUNK_LFE               = 0x0a,
58
+    LBR_CHUNK_ECS               = 0x0b,
59
+    LBR_CHUNK_RESERVED_1        = 0x0c,
60
+    LBR_CHUNK_RESERVED_2        = 0x0d,
61
+    LBR_CHUNK_SCF               = 0x0e,
62
+    LBR_CHUNK_TONAL             = 0x10,
63
+    LBR_CHUNK_TONAL_GRP_1       = 0x11,
64
+    LBR_CHUNK_TONAL_GRP_2       = 0x12,
65
+    LBR_CHUNK_TONAL_GRP_3       = 0x13,
66
+    LBR_CHUNK_TONAL_GRP_4       = 0x14,
67
+    LBR_CHUNK_TONAL_GRP_5       = 0x15,
68
+    LBR_CHUNK_TONAL_SCF         = 0x16,
69
+    LBR_CHUNK_TONAL_SCF_GRP_1   = 0x17,
70
+    LBR_CHUNK_TONAL_SCF_GRP_2   = 0x18,
71
+    LBR_CHUNK_TONAL_SCF_GRP_3   = 0x19,
72
+    LBR_CHUNK_TONAL_SCF_GRP_4   = 0x1a,
73
+    LBR_CHUNK_TONAL_SCF_GRP_5   = 0x1b,
74
+    LBR_CHUNK_RES_GRID_LR       = 0x30,
75
+    LBR_CHUNK_RES_GRID_LR_LAST  = 0x3f,
76
+    LBR_CHUNK_RES_GRID_HR       = 0x40,
77
+    LBR_CHUNK_RES_GRID_HR_LAST  = 0x4f,
78
+    LBR_CHUNK_RES_TS_1          = 0x50,
79
+    LBR_CHUNK_RES_TS_1_LAST     = 0x5f,
80
+    LBR_CHUNK_RES_TS_2          = 0x60,
81
+    LBR_CHUNK_RES_TS_2_LAST     = 0x6f,
82
+    LBR_CHUNK_EXTENSION         = 0x7f
83
+};
84
+
85
+typedef struct LBRChunk {
86
+    int id, len;
87
+    const uint8_t *data;
88
+} LBRChunk;
89
+
90
+static const int8_t channel_reorder_nolfe[7][5] = {
91
+    { 0, -1, -1, -1, -1 },  // C
92
+    { 0,  1, -1, -1, -1 },  // LR
93
+    { 0,  1,  2, -1, -1 },  // LR C
94
+    { 0,  1, -1, -1, -1 },  // LsRs
95
+    { 1,  2,  0, -1, -1 },  // LsRs C
96
+    { 0,  1,  2,  3, -1 },  // LR LsRs
97
+    { 0,  1,  3,  4,  2 },  // LR LsRs C
98
+};
99
+
100
+static const int8_t channel_reorder_lfe[7][5] = {
101
+    { 0, -1, -1, -1, -1 },  // C
102
+    { 0,  1, -1, -1, -1 },  // LR
103
+    { 0,  1,  2, -1, -1 },  // LR C
104
+    { 1,  2, -1, -1, -1 },  // LsRs
105
+    { 2,  3,  0, -1, -1 },  // LsRs C
106
+    { 0,  1,  3,  4, -1 },  // LR LsRs
107
+    { 0,  1,  4,  5,  2 },  // LR LsRs C
108
+};
109
+
110
+static const uint8_t lfe_index[7] = {
111
+    1, 2, 3, 0, 1, 2, 3
112
+};
113
+
114
+static const uint8_t channel_counts[7] = {
115
+    1, 2, 3, 2, 3, 4, 5
116
+};
117
+
118
+static const uint16_t channel_layouts[7] = {
119
+    AV_CH_LAYOUT_MONO,
120
+    AV_CH_LAYOUT_STEREO,
121
+    AV_CH_LAYOUT_SURROUND,
122
+    AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
123
+    AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
124
+    AV_CH_LAYOUT_2_2,
125
+    AV_CH_LAYOUT_5POINT0
126
+};
127
+
128
+static float    cos_tab[256];
129
+static float    lpc_tab[16];
130
+
131
+static av_cold void init_tables(void)
132
+{
133
+    static int initialized;
134
+    int i;
135
+
136
+    if (initialized)
137
+        return;
138
+
139
+    for (i = 0; i < 256; i++)
140
+        cos_tab[i] = cos(M_PI * i / 128);
141
+
142
+    for (i = 0; i < 16; i++)
143
+        lpc_tab[i] = sin((i - 8) * (M_PI / ((i < 8) ? 17 : 15)));
144
+
145
+    initialized = 1;
146
+}
147
+
148
+static int parse_lfe_24(DCALbrDecoder *s)
149
+{
150
+    int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_24) - 1;
151
+    int i, ps, si, code, step_i;
152
+    float step, value, delta;
153
+
154
+    ps = get_bits(&s->gb, 24);
155
+    si = ps >> 23;
156
+
157
+    value = (((ps & 0x7fffff) ^ -si) + si) * (1.0f / 0x7fffff);
158
+
159
+    step_i = get_bits(&s->gb, 8);
160
+    if (step_i > step_max) {
161
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
162
+        return -1;
163
+    }
164
+
165
+    step = ff_dca_lfe_step_size_24[step_i];
166
+
167
+    for (i = 0; i < 64; i++) {
168
+        code = get_bits(&s->gb, 6);
169
+
170
+        delta = step * 0.03125f;
171
+        if (code & 16)
172
+            delta += step;
173
+        if (code & 8)
174
+            delta += step * 0.5f;
175
+        if (code & 4)
176
+            delta += step * 0.25f;
177
+        if (code & 2)
178
+            delta += step * 0.125f;
179
+        if (code & 1)
180
+            delta += step * 0.0625f;
181
+
182
+        if (code & 32) {
183
+            value -= delta;
184
+            if (value < -3.0f)
185
+                value = -3.0f;
186
+        } else {
187
+            value += delta;
188
+            if (value > 3.0f)
189
+                value = 3.0f;
190
+        }
191
+
192
+        step_i += ff_dca_lfe_delta_index_24[code & 31];
193
+        step_i = av_clip(step_i, 0, step_max);
194
+
195
+        step = ff_dca_lfe_step_size_24[step_i];
196
+        s->lfe_data[i] = value * s->lfe_scale;
197
+    }
198
+
199
+    return 0;
200
+}
201
+
202
+static int parse_lfe_16(DCALbrDecoder *s)
203
+{
204
+    int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_16) - 1;
205
+    int i, ps, si, code, step_i;
206
+    float step, value, delta;
207
+
208
+    ps = get_bits(&s->gb, 16);
209
+    si = ps >> 15;
210
+
211
+    value = (((ps & 0x7fff) ^ -si) + si) * (1.0f / 0x7fff);
212
+
213
+    step_i = get_bits(&s->gb, 8);
214
+    if (step_i > step_max) {
215
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
216
+        return -1;
217
+    }
218
+
219
+    step = ff_dca_lfe_step_size_16[step_i];
220
+
221
+    for (i = 0; i < 64; i++) {
222
+        code = get_bits(&s->gb, 4);
223
+
224
+        delta = step * 0.125f;
225
+        if (code & 4)
226
+            delta += step;
227
+        if (code & 2)
228
+            delta += step * 0.5f;
229
+        if (code & 1)
230
+            delta += step * 0.25f;
231
+
232
+        if (code & 8) {
233
+            value -= delta;
234
+            if (value < -3.0f)
235
+                value = -3.0f;
236
+        } else {
237
+            value += delta;
238
+            if (value > 3.0f)
239
+                value = 3.0f;
240
+        }
241
+
242
+        step_i += ff_dca_lfe_delta_index_16[code & 7];
243
+        step_i = av_clip(step_i, 0, step_max);
244
+
245
+        step = ff_dca_lfe_step_size_16[step_i];
246
+        s->lfe_data[i] = value * s->lfe_scale;
247
+    }
248
+
249
+    return 0;
250
+}
251
+
252
+static int parse_lfe_chunk(DCALbrDecoder *s, LBRChunk *chunk)
253
+{
254
+    if (!(s->flags & LBR_FLAG_LFE_PRESENT))
255
+        return 0;
256
+
257
+    if (!chunk->len)
258
+        return 0;
259
+
260
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
261
+        return -1;
262
+
263
+    // Determine bit depth from chunk size
264
+    if (chunk->len >= 52)
265
+        return parse_lfe_24(s);
266
+    if (chunk->len >= 35)
267
+        return parse_lfe_16(s);
268
+
269
+    av_log(s->avctx, AV_LOG_ERROR, "LFE chunk too short\n");
270
+    return -1;
271
+}
272
+
273
+static inline int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth)
274
+{
275
+    int v = get_vlc2(s, vlc->table, vlc->bits, max_depth);
276
+    if (v > 0)
277
+        return v - 1;
278
+    // Rare value
279
+    return get_bits(s, get_bits(s, 3) + 1);
280
+}
281
+
282
+static int parse_tonal(DCALbrDecoder *s, int group)
283
+{
284
+    unsigned int amp[DCA_LBR_CHANNELS_TOTAL];
285
+    unsigned int phs[DCA_LBR_CHANNELS_TOTAL];
286
+    unsigned int diff, main_amp, shift;
287
+    int sf, sf_idx, ch, main_ch, freq;
288
+    int ch_nbits = av_ceil_log2(s->nchannels_total);
289
+
290
+    // Parse subframes for this group
291
+    for (sf = 0; sf < 1 << group; sf += diff ? 8 : 1) {
292
+        sf_idx = ((s->framenum << group) + sf) & 31;
293
+        s->tonal_bounds[group][sf_idx][0] = s->ntones;
294
+
295
+        // Parse tones for this subframe
296
+        for (freq = 1;; freq++) {
297
+            if (get_bits_left(&s->gb) < 1) {
298
+                av_log(s->avctx, AV_LOG_ERROR, "Tonal group chunk too short\n");
299
+                return -1;
300
+            }
301
+
302
+            diff = parse_vlc(&s->gb, &ff_dca_vlc_tnl_grp[group], 2);
303
+            if (diff >= FF_ARRAY_ELEMS(ff_dca_fst_amp)) {
304
+                av_log(s->avctx, AV_LOG_ERROR, "Invalid tonal frequency diff\n");
305
+                return -1;
306
+            }
307
+
308
+            diff = get_bitsz(&s->gb, diff >> 2) + ff_dca_fst_amp[diff];
309
+            if (diff <= 1)
310
+                break;  // End of subframe
311
+
312
+            freq += diff - 2;
313
+            if (freq >> (5 - group) > s->nsubbands * 4 - 5) {
314
+                av_log(s->avctx, AV_LOG_ERROR, "Invalid spectral line offset\n");
315
+                return -1;
316
+            }
317
+
318
+            // Main channel
319
+            main_ch = get_bitsz(&s->gb, ch_nbits);
320
+            main_amp = parse_vlc(&s->gb, &ff_dca_vlc_tnl_scf, 2)
321
+                + s->tonal_scf[ff_dca_freq_to_sb[freq >> (7 - group)]]
322
+                + s->limited_range - 2;
323
+            amp[main_ch] = main_amp < AMP_MAX ? main_amp : 0;
324
+            phs[main_ch] = get_bits(&s->gb, 3);
325
+
326
+            // Secondary channels
327
+            for (ch = 0; ch < s->nchannels_total; ch++) {
328
+                if (ch == main_ch)
329
+                    continue;
330
+                if (get_bits1(&s->gb)) {
331
+                    amp[ch] = amp[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_damp, 1);
332
+                    phs[ch] = phs[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_dph,  1);
333
+                } else {
334
+                    amp[ch] = 0;
335
+                    phs[ch] = 0;
336
+                }
337
+            }
338
+
339
+            if (amp[main_ch]) {
340
+                // Allocate new tone
341
+                DCALbrTone *t = &s->tones[s->ntones];
342
+                s->ntones = (s->ntones + 1) & (DCA_LBR_TONES - 1);
343
+
344
+                t->x_freq = freq >> (5 - group);
345
+                t->f_delt = (freq & ((1 << (5 - group)) - 1)) << group;
346
+                t->ph_rot = 256 - (t->x_freq & 1) * 128 - t->f_delt * 4;
347
+
348
+                shift = ff_dca_ph0_shift[(t->x_freq & 3) * 2 + (freq & 1)]
349
+                    - ((t->ph_rot << (5 - group)) - t->ph_rot);
350
+
351
+                for (ch = 0; ch < s->nchannels; ch++) {
352
+                    t->amp[ch] = amp[ch] < AMP_MAX ? amp[ch] : 0;
353
+                    t->phs[ch] = 128 - phs[ch] * 32 + shift;
354
+                }
355
+            }
356
+        }
357
+
358
+        s->tonal_bounds[group][sf_idx][1] = s->ntones;
359
+    }
360
+
361
+    return 0;
362
+}
363
+
364
+static int parse_tonal_chunk(DCALbrDecoder *s, LBRChunk *chunk)
365
+{
366
+    int sb, group;
367
+
368
+    if (!chunk->len)
369
+        return 0;
370
+
371
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
372
+        return -1;
373
+
374
+    // Scale factors
375
+    if (chunk->id == LBR_CHUNK_SCF || chunk->id == LBR_CHUNK_TONAL_SCF) {
376
+        if (get_bits_left(&s->gb) < 36) {
377
+            av_log(s->avctx, AV_LOG_ERROR, "Tonal scale factor chunk too short\n");
378
+            return -1;
379
+        }
380
+        for (sb = 0; sb < 6; sb++)
381
+            s->tonal_scf[sb] = get_bits(&s->gb, 6);
382
+    }
383
+
384
+    // Tonal groups
385
+    if (chunk->id == LBR_CHUNK_TONAL || chunk->id == LBR_CHUNK_TONAL_SCF)
386
+        for (group = 0; group < 5; group++)
387
+            if (parse_tonal(s, group) < 0)
388
+                return -1;
389
+
390
+    return 0;
391
+}
392
+
393
+static int parse_tonal_group(DCALbrDecoder *s, LBRChunk *chunk)
394
+{
395
+    if (!chunk->len)
396
+        return 0;
397
+
398
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
399
+        return -1;
400
+
401
+    return parse_tonal(s, chunk->id);
402
+}
403
+
404
+/**
405
+ * Check point to ensure that enough bits are left. Aborts decoding
406
+ * by skipping to the end of chunk otherwise.
407
+ */
408
+static int ensure_bits(GetBitContext *s, int n)
409
+{
410
+    int left = get_bits_left(s);
411
+    if (left < 0)
412
+        return -1;
413
+    if (left < n) {
414
+        skip_bits_long(s, left);
415
+        return 1;
416
+    }
417
+    return 0;
418
+}
419
+
420
+static int parse_scale_factors(DCALbrDecoder *s, uint8_t *scf)
421
+{
422
+    int i, sf, prev, next, dist;
423
+
424
+    // Truncated scale factors remain zero
425
+    if (ensure_bits(&s->gb, 20))
426
+        return 0;
427
+
428
+    // Initial scale factor
429
+    prev = parse_vlc(&s->gb, &ff_dca_vlc_fst_rsd_amp, 2);
430
+
431
+    for (sf = 0; sf < 7; sf += dist) {
432
+        scf[sf] = prev; // Store previous value
433
+
434
+        if (ensure_bits(&s->gb, 20))
435
+            return 0;
436
+
437
+        // Interpolation distance
438
+        dist = parse_vlc(&s->gb, &ff_dca_vlc_rsd_apprx, 1) + 1;
439
+        if (dist > 7 - sf) {
440
+            av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor distance\n");
441
+            return -1;
442
+        }
443
+
444
+        if (ensure_bits(&s->gb, 20))
445
+            return 0;
446
+
447
+        // Final interpolation point
448
+        next = parse_vlc(&s->gb, &ff_dca_vlc_rsd_amp, 2);
449
+
450
+        if (next & 1)
451
+            next = prev + ((next + 1) >> 1);
452
+        else
453
+            next = prev - ( next      >> 1);
454
+
455
+        // Interpolate
456
+        switch (dist) {
457
+        case 2:
458
+            if (next > prev)
459
+                scf[sf + 1] = prev + ((next - prev) >> 1);
460
+            else
461
+                scf[sf + 1] = prev - ((prev - next) >> 1);
462
+            break;
463
+
464
+        case 4:
465
+            if (next > prev) {
466
+                scf[sf + 1] = prev + ( (next - prev)      >> 2);
467
+                scf[sf + 2] = prev + ( (next - prev)      >> 1);
468
+                scf[sf + 3] = prev + (((next - prev) * 3) >> 2);
469
+            } else {
470
+                scf[sf + 1] = prev - ( (prev - next)      >> 2);
471
+                scf[sf + 2] = prev - ( (prev - next)      >> 1);
472
+                scf[sf + 3] = prev - (((prev - next) * 3) >> 2);
473
+            }
474
+            break;
475
+
476
+        default:
477
+            for (i = 1; i < dist; i++)
478
+                scf[sf + i] = prev + (next - prev) * i / dist;
479
+            break;
480
+        }
481
+
482
+        prev = next;
483
+    }
484
+
485
+    scf[sf] = next; // Store final value
486
+
487
+    return 0;
488
+}
489
+
490
+static int parse_st_code(GetBitContext *s, int min_v)
491
+{
492
+    unsigned int v = parse_vlc(s, &ff_dca_vlc_st_grid, 2) + min_v;
493
+
494
+    if (v & 1)
495
+        v = 16 + (v >> 1);
496
+    else
497
+        v = 16 - (v >> 1);
498
+
499
+    if (v >= FF_ARRAY_ELEMS(ff_dca_st_coeff))
500
+        v = 16;
501
+    return v;
502
+}
503
+
504
+static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
505
+{
506
+    int ch, sb, sf, nsubbands;
507
+
508
+    if (!chunk->len)
509
+        return 0;
510
+
511
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
512
+        return -1;
513
+
514
+    // Scale factors
515
+    nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
516
+    for (sb = 2; sb < nsubbands; sb++) {
517
+        if (parse_scale_factors(s, s->grid_1_scf[ch1][sb]) < 0)
518
+            return -1;
519
+        if (ch1 != ch2 && ff_dca_grid_1_to_scf[sb] < s->min_mono_subband
520
+            && parse_scale_factors(s, s->grid_1_scf[ch2][sb]) < 0)
521
+            return -1;
522
+    }
523
+
524
+    if (get_bits_left(&s->gb) < 1)
525
+        return 0;   // Should not happen, but a sample exists that proves otherwise
526
+
527
+    // Average values for third grid
528
+    for (sb = 0; sb < s->nsubbands - 4; sb++) {
529
+        s->grid_3_avg[ch1][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
530
+        if (ch1 != ch2) {
531
+            if (sb + 4 < s->min_mono_subband)
532
+                s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
533
+            else
534
+                s->grid_3_avg[ch2][sb] = s->grid_3_avg[ch1][sb];
535
+        }
536
+    }
537
+
538
+    if (get_bits_left(&s->gb) < 0) {
539
+        av_log(s->avctx, AV_LOG_ERROR, "First grid chunk too short\n");
540
+        return -1;
541
+    }
542
+
543
+    // Stereo image for partial mono mode
544
+    if (ch1 != ch2) {
545
+        int min_v[2];
546
+
547
+        if (ensure_bits(&s->gb, 8))
548
+            return 0;
549
+
550
+        min_v[0] = get_bits(&s->gb, 4);
551
+        min_v[1] = get_bits(&s->gb, 4);
552
+
553
+        nsubbands = (s->nsubbands - s->min_mono_subband + 3) / 4;
554
+        for (sb = 0; sb < nsubbands; sb++)
555
+            for (ch = ch1; ch <= ch2; ch++)
556
+                for (sf = 1; sf <= 4; sf++)
557
+                    s->part_stereo[ch][sb][sf] = parse_st_code(&s->gb, min_v[ch - ch1]);
558
+
559
+        if (get_bits_left(&s->gb) >= 0)
560
+            s->part_stereo_pres |= 1 << ch1;
561
+    }
562
+
563
+    // Low resolution spatial information is not decoded
564
+
565
+    return 0;
566
+}
567
+
568
+static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2)
569
+{
570
+    int sb, nsubbands;
571
+
572
+    // Scale factors
573
+    nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
574
+    for (sb = 2; sb < nsubbands; sb++) {
575
+        if (ff_dca_grid_1_to_scf[sb] >= s->min_mono_subband
576
+            && parse_scale_factors(s, s->grid_1_scf[ch2][sb]) < 0)
577
+            return -1;
578
+    }
579
+
580
+    // Average values for third grid
581
+    for (sb = 0; sb < s->nsubbands - 4; sb++) {
582
+        if (sb + 4 >= s->min_mono_subband) {
583
+            if (ensure_bits(&s->gb, 20))
584
+                return 0;
585
+            s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
586
+        }
587
+    }
588
+
589
+    return 0;
590
+}
591
+
592
+static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag)
593
+{
594
+    int i, ch;
595
+
596
+    for (ch = ch1; ch <= ch2; ch++) {
597
+        if ((ch != ch1 && sb + 4 >= s->min_mono_subband) != flag)
598
+            continue;
599
+
600
+        if (s->grid_3_pres[ch] & (1U << sb))
601
+            continue;   // Already parsed
602
+
603
+        for (i = 0; i < 8; i++) {
604
+            if (ensure_bits(&s->gb, 20))
605
+                return;
606
+            s->grid_3_scf[ch][sb][i] = parse_vlc(&s->gb, &ff_dca_vlc_grid_3, 2) - 16;
607
+        }
608
+
609
+        // Flag scale factors for this subband parsed
610
+        s->grid_3_pres[ch] |= 1U << sb;
611
+    }
612
+}
613
+
614
+static float lbr_rand(DCALbrDecoder *s, int sb)
615
+{
616
+    s->lbr_rand = 1103515245U * s->lbr_rand + 12345U;
617
+    return s->lbr_rand * s->sb_scf[sb];
618
+}
619
+
620
+/**
621
+ * Parse time samples for one subband, filling truncated samples with randomness
622
+ */
623
+static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag)
624
+{
625
+    float *samples = s->time_samples[ch][sb];
626
+    int i, j, code, nblocks, coding_method;
627
+
628
+    if (ensure_bits(&s->gb, 20))
629
+        return; // Too few bits left
630
+
631
+    coding_method = get_bits1(&s->gb);
632
+
633
+    switch (quant_level) {
634
+    case 1:
635
+        nblocks = FFMIN(get_bits_left(&s->gb) / 8, DCA_LBR_TIME_SAMPLES / 8);
636
+        for (i = 0; i < nblocks; i++, samples += 8) {
637
+            code = get_bits(&s->gb, 8);
638
+            for (j = 0; j < 8; j++)
639
+                samples[j] = ff_dca_rsd_level_2a[(code >> j) & 1];
640
+        }
641
+        i = nblocks * 8;
642
+        break;
643
+
644
+    case 2:
645
+        if (coding_method) {
646
+            for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 2; i++) {
647
+                if (get_bits1(&s->gb))
648
+                    samples[i] = ff_dca_rsd_level_2b[get_bits1(&s->gb)];
649
+                else
650
+                    samples[i] = 0;
651
+            }
652
+        } else {
653
+            nblocks = FFMIN(get_bits_left(&s->gb) / 8, (DCA_LBR_TIME_SAMPLES + 4) / 5);
654
+            for (i = 0; i < nblocks; i++, samples += 5) {
655
+                code = ff_dca_rsd_pack_5_in_8[get_bits(&s->gb, 8)];
656
+                for (j = 0; j < 5; j++)
657
+                    samples[j] = ff_dca_rsd_level_3[(code >> j * 2) & 3];
658
+            }
659
+            i = nblocks * 5;
660
+        }
661
+        break;
662
+
663
+    case 3:
664
+        nblocks = FFMIN(get_bits_left(&s->gb) / 7, (DCA_LBR_TIME_SAMPLES + 2) / 3);
665
+        for (i = 0; i < nblocks; i++, samples += 3) {
666
+            code = get_bits(&s->gb, 7);
667
+            for (j = 0; j < 3; j++)
668
+                samples[j] = ff_dca_rsd_level_5[ff_dca_rsd_pack_3_in_7[code][j]];
669
+        }
670
+        i = nblocks * 3;
671
+        break;
672
+
673
+    case 4:
674
+        for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 6; i++)
675
+            samples[i] = ff_dca_rsd_level_8[get_vlc2(&s->gb, ff_dca_vlc_rsd.table, 6, 1)];
676
+        break;
677
+
678
+    case 5:
679
+        nblocks = FFMIN(get_bits_left(&s->gb) / 4, DCA_LBR_TIME_SAMPLES);
680
+        for (i = 0; i < nblocks; i++)
681
+            samples[i] = ff_dca_rsd_level_16[get_bits(&s->gb, 4)];
682
+        break;
683
+
684
+    default:
685
+        av_assert0(0);
686
+    }
687
+
688
+    if (flag && get_bits_left(&s->gb) < 20)
689
+        return; // Skip incomplete mono subband
690
+
691
+    for (; i < DCA_LBR_TIME_SAMPLES; i++)
692
+        s->time_samples[ch][sb][i] = lbr_rand(s, sb);
693
+
694
+    s->ch_pres[ch] |= 1U << sb;
695
+}
696
+
697
+static int parse_ts(DCALbrDecoder *s, int ch1, int ch2,
698
+                    int start_sb, int end_sb, int flag)
699
+{
700
+    int sb, sb_g3, sb_reorder, quant_level;
701
+
702
+    for (sb = start_sb; sb < end_sb; sb++) {
703
+        // Subband number before reordering
704
+        if (sb < 6) {
705
+            sb_reorder = sb;
706
+        } else if (flag && sb < s->max_mono_subband) {
707
+            sb_reorder = s->sb_indices[sb];
708
+        } else {
709
+            if (ensure_bits(&s->gb, 28))
710
+                break;
711
+            sb_reorder = get_bits(&s->gb, s->limited_range + 3);
712
+            if (sb_reorder < 6)
713
+                sb_reorder = 6;
714
+            s->sb_indices[sb] = sb_reorder;
715
+        }
716
+        if (sb_reorder >= s->nsubbands)
717
+            return -1;
718
+
719
+        // Third grid scale factors
720
+        if (sb == 12) {
721
+            for (sb_g3 = 0; sb_g3 < s->g3_avg_only_start_sb - 4; sb_g3++)
722
+                parse_grid_3(s, ch1, ch2, sb_g3, flag);
723
+        } else if (sb < 12 && sb_reorder >= 4) {
724
+            parse_grid_3(s, ch1, ch2, sb_reorder - 4, flag);
725
+        }
726
+
727
+        // Secondary channel flags
728
+        if (ch1 != ch2) {
729
+            if (ensure_bits(&s->gb, 20))
730
+                break;
731
+            if (!flag || sb_reorder >= s->max_mono_subband)
732
+                s->sec_ch_sbms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
733
+            if (flag && sb_reorder >= s->min_mono_subband)
734
+                s->sec_ch_lrms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
735
+        }
736
+
737
+        quant_level = s->quant_levels[ch1 / 2][sb];
738
+        if (!quant_level)
739
+            return -1;
740
+
741
+        // Time samples for one or both channels
742
+        if (sb < s->max_mono_subband && sb_reorder >= s->min_mono_subband) {
743
+            if (!flag)
744
+                parse_ch(s, ch1, sb_reorder, quant_level, 0);
745
+            else if (ch1 != ch2)
746
+                parse_ch(s, ch2, sb_reorder, quant_level, 1);
747
+        } else {
748
+            parse_ch(s, ch1, sb_reorder, quant_level, 0);
749
+            if (ch1 != ch2)
750
+                parse_ch(s, ch2, sb_reorder, quant_level, 0);
751
+        }
752
+    }
753
+
754
+    return 0;
755
+}
756
+
757
+/**
758
+ * Convert from reflection coefficients to direct form coefficients
759
+ */
760
+static void convert_lpc(float *coeff, const int *codes)
761
+{
762
+    int i, j;
763
+
764
+    for (i = 0; i < 8; i++) {
765
+        float rc = lpc_tab[codes[i]];
766
+        for (j = 0; j < (i + 1) / 2; j++) {
767
+            float tmp1 = coeff[    j    ];
768
+            float tmp2 = coeff[i - j - 1];
769
+            coeff[    j    ] = tmp1 + rc * tmp2;
770
+            coeff[i - j - 1] = tmp2 + rc * tmp1;
771
+        }
772
+        coeff[i] = rc;
773
+    }
774
+}
775
+
776
+static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb)
777
+{
778
+    int f = s->framenum & 1;
779
+    int i, sb, ch, codes[16];
780
+
781
+    // First two subbands have two sets of coefficients, third subband has one
782
+    for (sb = start_sb; sb < end_sb; sb++) {
783
+        int ncodes = 8 * (1 + (sb < 2));
784
+        for (ch = ch1; ch <= ch2; ch++) {
785
+            if (ensure_bits(&s->gb, 4 * ncodes))
786
+                return 0;
787
+            for (i = 0; i < ncodes; i++)
788
+                codes[i] = get_bits(&s->gb, 4);
789
+            for (i = 0; i < ncodes / 8; i++)
790
+                convert_lpc(s->lpc_coeff[f][ch][sb][i], &codes[i * 8]);
791
+        }
792
+    }
793
+
794
+    return 0;
795
+}
796
+
797
+static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
798
+{
799
+    int quant_levels[DCA_LBR_SUBBANDS];
800
+    int sb, ch, ol, st, max_sb, profile;
801
+
802
+    if (!chunk->len)
803
+        return 0;
804
+
805
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
806
+        return -1;
807
+
808
+    // Quantizer profile
809
+    profile = get_bits(&s->gb, 8);
810
+    // Overall level
811
+    ol = (profile >> 3) & 7;
812
+    // Steepness
813
+    st = profile >> 6;
814
+    // Max energy subband
815
+    max_sb = profile & 7;
816
+
817
+    // Calculate quantization levels
818
+    for (sb = 0; sb < s->nsubbands; sb++) {
819
+        int f = sb * s->limited_rate / s->nsubbands;
820
+        int a = 18000 / (12 * f / 1000 + 100 + 40 * st) + 20 * ol;
821
+        if (a <= 95)
822
+            quant_levels[sb] = 1;
823
+        else if (a <= 140)
824
+            quant_levels[sb] = 2;
825
+        else if (a <= 180)
826
+            quant_levels[sb] = 3;
827
+        else if (a <= 230)
828
+            quant_levels[sb] = 4;
829
+        else
830
+            quant_levels[sb] = 5;
831
+    }
832
+
833
+    // Reorder quantization levels for lower subbands
834
+    for (sb = 0; sb < 8; sb++)
835
+        s->quant_levels[ch1 / 2][sb] = quant_levels[ff_dca_sb_reorder[max_sb][sb]];
836
+    for (; sb < s->nsubbands; sb++)
837
+        s->quant_levels[ch1 / 2][sb] = quant_levels[sb];
838
+
839
+    // LPC for the first two subbands
840
+    if (parse_lpc(s, ch1, ch2, 0, 2) < 0)
841
+        return -1;
842
+
843
+    // Time-samples for the first two subbands of main channel
844
+    if (parse_ts(s, ch1, ch2, 0, 2, 0) < 0)
845
+        return -1;
846
+
847
+    // First two bands of the first grid
848
+    for (sb = 0; sb < 2; sb++)
849
+        for (ch = ch1; ch <= ch2; ch++)
850
+            if (parse_scale_factors(s, s->grid_1_scf[ch][sb]) < 0)
851
+                return -1;
852
+
853
+    return 0;
854
+}
855
+
856
+static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2,
857
+                        int start_sb, int end_sb, int flag)
858
+{
859
+    int i, j, sb, ch, nsubbands;
860
+
861
+    nsubbands = ff_dca_scf_to_grid_2[s->nsubbands - 1] + 1;
862
+    if (end_sb > nsubbands)
863
+        end_sb = nsubbands;
864
+
865
+    for (sb = start_sb; sb < end_sb; sb++) {
866
+        for (ch = ch1; ch <= ch2; ch++) {
867
+            uint8_t *g2_scf = s->grid_2_scf[ch][sb];
868
+
869
+            if ((ch != ch1 && ff_dca_grid_2_to_scf[sb] >= s->min_mono_subband) != flag) {
870
+                if (!flag)
871
+                    memcpy(g2_scf, s->grid_2_scf[ch1][sb], 64);
872
+                continue;
873
+            }
874
+
875
+            // Scale factors in groups of 8
876
+            for (i = 0; i < 8; i++, g2_scf += 8) {
877
+                if (get_bits_left(&s->gb) < 1) {
878
+                    memset(g2_scf, 0, 64 - i * 8);
879
+                    break;
880
+                }
881
+                // Bit indicating if whole group has zero values
882
+                if (get_bits1(&s->gb)) {
883
+                    for (j = 0; j < 8; j++) {
884
+                        if (ensure_bits(&s->gb, 20))
885
+                            break;
886
+                        g2_scf[j] = parse_vlc(&s->gb, &ff_dca_vlc_grid_2, 2);
887
+                    }
888
+                } else {
889
+                    memset(g2_scf, 0, 8);
890
+                }
891
+            }
892
+        }
893
+    }
894
+
895
+    return 0;
896
+}
897
+
898
+static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
899
+{
900
+    if (!chunk->len)
901
+        return 0;
902
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
903
+        return -1;
904
+    if (parse_lpc(s, ch1, ch2, 2, 3) < 0)
905
+        return -1;
906
+    if (parse_ts(s, ch1, ch2, 2, 4, 0) < 0)
907
+        return -1;
908
+    if (parse_grid_2(s, ch1, ch2, 0, 1, 0) < 0)
909
+        return -1;
910
+    if (parse_ts(s, ch1, ch2, 4, 6, 0) < 0)
911
+        return -1;
912
+    return 0;
913
+}
914
+
915
+static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
916
+{
917
+    if (!chunk->len)
918
+        return 0;
919
+    if (init_get_bits8(&s->gb, chunk->data, chunk->len) < 0)
920
+        return -1;
921
+    if (parse_grid_2(s, ch1, ch2, 1, 3, 0) < 0)
922
+        return -1;
923
+    if (parse_ts(s, ch1, ch2, 6, s->max_mono_subband, 0) < 0)
924
+        return -1;
925
+    if (ch1 != ch2) {
926
+        if (parse_grid_1_sec_ch(s, ch2) < 0)
927
+            return -1;
928
+        if (parse_grid_2(s, ch1, ch2, 0, 3, 1) < 0)
929
+            return -1;
930
+    }
931
+    if (parse_ts(s, ch1, ch2, s->min_mono_subband, s->nsubbands, 1) < 0)
932
+        return -1;
933
+    return 0;
934
+}
935
+
936
+static int init_sample_rate(DCALbrDecoder *s)
937
+{
938
+    double scale = (-1.0 / (1 << 17)) * sqrt(1 << (2 - s->limited_range));
939
+    int i, br_per_ch = s->bit_rate_scaled / s->nchannels_total;
940
+
941
+    ff_mdct_end(&s->imdct);
942
+
943
+    if (ff_mdct_init(&s->imdct, s->freq_range + 6, 1, scale) < 0)
944
+        return -1;
945
+
946
+    for (i = 0; i < 32 << s->freq_range; i++)
947
+        s->window[i] = ff_dca_long_window[i << (2 - s->freq_range)];
948
+
949
+    if (br_per_ch < 14000)
950
+        scale = 0.85;
951
+    else if (br_per_ch < 32000)
952
+        scale = (br_per_ch - 14000) * (1.0 / 120000) + 0.85;
953
+    else
954
+        scale = 1.0;
955
+
956
+    scale *= 1.0 / INT_MAX;
957
+
958
+    for (i = 0; i < s->nsubbands; i++) {
959
+        if (i < 2)
960
+            s->sb_scf[i] = 0;   // The first two subbands are always zero
961
+        else if (i < 5)
962
+            s->sb_scf[i] = (i - 1) * 0.25 * 0.785 * scale;
963
+        else
964
+            s->sb_scf[i] = 0.785 * scale;
965
+    }
966
+
967
+    s->lfe_scale = (16 << s->freq_range) * 0.0000078265894;
968
+
969
+    return 0;
970
+}
971
+
972
+static int alloc_sample_buffer(DCALbrDecoder *s)
973
+{
974
+    // Reserve space for history and padding
975
+    int nchsamples = DCA_LBR_TIME_SAMPLES + DCA_LBR_TIME_HISTORY * 2;
976
+    int nsamples = nchsamples * s->nchannels * s->nsubbands;
977
+    int ch, sb;
978
+    float *ptr;
979
+
980
+    // Reallocate time sample buffer
981
+    av_fast_mallocz(&s->ts_buffer, &s->ts_size, nsamples * sizeof(float));
982
+    if (!s->ts_buffer)
983
+        return -1;
984
+
985
+    ptr = s->ts_buffer + DCA_LBR_TIME_HISTORY;
986
+    for (ch = 0; ch < s->nchannels; ch++) {
987
+        for (sb = 0; sb < s->nsubbands; sb++) {
988
+            s->time_samples[ch][sb] = ptr;
989
+            ptr += nchsamples;
990
+        }
991
+    }
992
+
993
+    return 0;
994
+}
995
+
996
+static int parse_decoder_init(DCALbrDecoder *s, GetByteContext *gb)
997
+{
998
+    int old_rate = s->sample_rate;
999
+    int old_band_limit = s->band_limit;
1000
+    int old_nchannels = s->nchannels;
1001
+    int version, bit_rate_hi;
1002
+    unsigned int code;
1003
+
1004
+    // Sample rate of LBR audio
1005
+    code = bytestream2_get_byte(gb);
1006
+    if (code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) {
1007
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sample rate\n");
1008
+        return AVERROR_INVALIDDATA;
1009
+    }
1010
+    s->sample_rate = ff_dca_sampling_freqs[code];
1011
+    if (s->sample_rate > 48000) {
1012
+        avpriv_report_missing_feature(s->avctx, "%d Hz LBR sample rate", s->sample_rate);
1013
+        return AVERROR_PATCHWELCOME;
1014
+    }
1015
+
1016
+    // LBR speaker mask
1017
+    s->ch_mask = bytestream2_get_le16(gb);
1018
+    if (!(s->ch_mask & 0x7)) {
1019
+        avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1020
+        return AVERROR_PATCHWELCOME;
1021
+    }
1022
+    if ((s->ch_mask & 0xfff0) && !(s->warned & 1)) {
1023
+        avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1024
+        s->warned |= 1;
1025
+    }
1026
+
1027
+    // LBR bitstream version
1028
+    version = bytestream2_get_le16(gb);
1029
+    if ((version & 0xff00) != 0x0800) {
1030
+        avpriv_report_missing_feature(s->avctx, "LBR stream version %#x", version);
1031
+        return AVERROR_PATCHWELCOME;
1032
+    }
1033
+
1034
+    // Flags for LBR decoder initialization
1035
+    s->flags = bytestream2_get_byte(gb);
1036
+    if (s->flags & LBR_FLAG_DMIX_MULTI_CH) {
1037
+        avpriv_report_missing_feature(s->avctx, "LBR multi-channel downmix");
1038
+        return AVERROR_PATCHWELCOME;
1039
+    }
1040
+    if ((s->flags & LBR_FLAG_LFE_PRESENT) && s->sample_rate != 48000) {
1041
+        if (!(s->warned & 2)) {
1042
+            avpriv_report_missing_feature(s->avctx, "%d Hz LFE interpolation", s->sample_rate);
1043
+            s->warned |= 2;
1044
+        }
1045
+        s->flags &= ~LBR_FLAG_LFE_PRESENT;
1046
+    }
1047
+
1048
+    // Most significant bit rate nibbles
1049
+    bit_rate_hi = bytestream2_get_byte(gb);
1050
+
1051
+    // Least significant original bit rate word
1052
+    s->bit_rate_orig = bytestream2_get_le16(gb) | ((bit_rate_hi & 0x0F) << 16);
1053
+
1054
+    // Least significant scaled bit rate word
1055
+    s->bit_rate_scaled = bytestream2_get_le16(gb) | ((bit_rate_hi & 0xF0) << 12);
1056
+
1057
+    // Setup number of fullband channels
1058
+    s->nchannels_total = ff_dca_count_chs_for_mask(s->ch_mask & ~DCA_SPEAKER_PAIR_LFE1);
1059
+    s->nchannels = FFMIN(s->nchannels_total, DCA_LBR_CHANNELS);
1060
+
1061
+    // Setup band limit
1062
+    switch (s->flags & LBR_FLAG_BAND_LIMIT_MASK) {
1063
+    case LBR_FLAG_BAND_LIMIT_NONE:
1064
+        s->band_limit = 0;
1065
+        break;
1066
+    case LBR_FLAG_BAND_LIMIT_1_2:
1067
+        s->band_limit = 1;
1068
+        break;
1069
+    case LBR_FLAG_BAND_LIMIT_1_4:
1070
+        s->band_limit = 2;
1071
+        break;
1072
+    default:
1073
+        avpriv_report_missing_feature(s->avctx, "LBR band limit %#x", s->flags & LBR_FLAG_BAND_LIMIT_MASK);
1074
+        return AVERROR_PATCHWELCOME;
1075
+    }
1076
+
1077
+    // Setup frequency range
1078
+    if (s->sample_rate < 14000)
1079
+        s->freq_range = 0;
1080
+    else if (s->sample_rate < 28000)
1081
+        s->freq_range = 1;
1082
+    else
1083
+        s->freq_range = 2;
1084
+
1085
+    // Setup resolution profile
1086
+    if (s->bit_rate_orig >= 44000 * (s->nchannels_total + 2))
1087
+        s->res_profile = 2;
1088
+    else if (s->bit_rate_orig >= 25000 * (s->nchannels_total + 2))
1089
+        s->res_profile = 1;
1090
+    else
1091
+        s->res_profile = 0;
1092
+
1093
+    // Setup limited sample rate, number of subbands, etc
1094
+    s->limited_rate = s->sample_rate >> s->band_limit;
1095
+    s->limited_range = s->freq_range - s->band_limit;
1096
+    if (s->limited_range < 0) {
1097
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR band limit for frequency range\n");
1098
+        return AVERROR_INVALIDDATA;
1099
+    }
1100
+
1101
+    s->nsubbands = 8 << s->limited_range;
1102
+
1103
+    s->g3_avg_only_start_sb = s->nsubbands * ff_dca_avg_g3_freqs[s->res_profile] / (s->limited_rate / 2);
1104
+    if (s->g3_avg_only_start_sb > s->nsubbands)
1105
+        s->g3_avg_only_start_sb = s->nsubbands;
1106
+
1107
+    s->min_mono_subband = s->nsubbands *  2000 / (s->limited_rate / 2);
1108
+    if (s->min_mono_subband > s->nsubbands)
1109
+        s->min_mono_subband = s->nsubbands;
1110
+
1111
+    s->max_mono_subband = s->nsubbands * 14000 / (s->limited_rate / 2);
1112
+    if (s->max_mono_subband > s->nsubbands)
1113
+        s->max_mono_subband = s->nsubbands;
1114
+
1115
+    // Handle change of sample rate
1116
+    if ((old_rate != s->sample_rate || old_band_limit != s->band_limit) && init_sample_rate(s) < 0)
1117
+        return AVERROR(ENOMEM);
1118
+
1119
+    // Setup stereo downmix
1120
+    if (s->flags & LBR_FLAG_DMIX_STEREO) {
1121
+        DCAContext *dca = s->avctx->priv_data;
1122
+
1123
+        if (s->nchannels_total < 3 || s->nchannels_total > DCA_LBR_CHANNELS_TOTAL - 2) {
1124
+            av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels for LBR stereo downmix\n");
1125
+            return AVERROR_INVALIDDATA;
1126
+        }
1127
+
1128
+        // This decoder doesn't support ECS chunk
1129
+        if (dca->request_channel_layout != DCA_SPEAKER_LAYOUT_STEREO && !(s->warned & 4)) {
1130
+            avpriv_report_missing_feature(s->avctx, "Embedded LBR stereo downmix");
1131
+            s->warned |= 4;
1132
+        }
1133
+
1134
+        // Account for extra downmixed channel pair
1135
+        s->nchannels_total += 2;
1136
+        s->nchannels = 2;
1137
+        s->ch_mask = DCA_SPEAKER_PAIR_LR;
1138
+        s->flags &= ~LBR_FLAG_LFE_PRESENT;
1139
+    }
1140
+
1141
+    // Handle change of sample rate or number of channels
1142
+    if (old_rate != s->sample_rate
1143
+        || old_band_limit != s->band_limit
1144
+        || old_nchannels != s->nchannels) {
1145
+        if (alloc_sample_buffer(s) < 0)
1146
+            return AVERROR(ENOMEM);
1147
+        ff_dca_lbr_flush(s);
1148
+    }
1149
+
1150
+    return 0;
1151
+}
1152
+
1153
+int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset)
1154
+{
1155
+    struct {
1156
+        LBRChunk    lfe;
1157
+        LBRChunk    tonal;
1158
+        LBRChunk    tonal_grp[5];
1159
+        LBRChunk    grid1[DCA_LBR_CHANNELS / 2];
1160
+        LBRChunk    hr_grid[DCA_LBR_CHANNELS / 2];
1161
+        LBRChunk    ts1[DCA_LBR_CHANNELS / 2];
1162
+        LBRChunk    ts2[DCA_LBR_CHANNELS / 2];
1163
+    } chunk = { };
1164
+
1165
+    GetByteContext gb;
1166
+
1167
+    int i, ch, sb, sf, ret, group, chunk_id, chunk_len;
1168
+
1169
+    bytestream2_init(&gb, data + asset->lbr_offset, asset->lbr_size);
1170
+
1171
+    // LBR sync word
1172
+    if (bytestream2_get_be32(&gb) != DCA_SYNCWORD_LBR) {
1173
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sync word\n");
1174
+        return AVERROR_INVALIDDATA;
1175
+    }
1176
+
1177
+    // LBR header type
1178
+    switch (bytestream2_get_byte(&gb)) {
1179
+    case LBR_HEADER_SYNC_ONLY:
1180
+        if (!s->sample_rate) {
1181
+            av_log(s->avctx, AV_LOG_ERROR, "LBR decoder not initialized\n");
1182
+            return AVERROR_INVALIDDATA;
1183
+        }
1184
+        break;
1185
+    case LBR_HEADER_DECODER_INIT:
1186
+        if ((ret = parse_decoder_init(s, &gb)) < 0) {
1187
+            s->sample_rate = 0;
1188
+            return ret;
1189
+        }
1190
+        break;
1191
+    default:
1192
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR header type\n");
1193
+        return AVERROR_INVALIDDATA;
1194
+    }
1195
+
1196
+    // LBR frame chunk header
1197
+    chunk_id = bytestream2_get_byte(&gb);
1198
+    chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1199
+
1200
+    if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1201
+        chunk_len = bytestream2_get_bytes_left(&gb);
1202
+        av_log(s->avctx, AV_LOG_WARNING, "LBR frame chunk was truncated\n");
1203
+        if (s->avctx->err_recognition & AV_EF_EXPLODE)
1204
+            return AVERROR_INVALIDDATA;
1205
+    }
1206
+
1207
+    bytestream2_init(&gb, gb.buffer, chunk_len);
1208
+
1209
+    switch (chunk_id & 0x7f) {
1210
+    case LBR_CHUNK_FRAME:
1211
+        if (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) {
1212
+            int checksum = bytestream2_get_be16(&gb);
1213
+            uint16_t res = chunk_id;
1214
+            res += (chunk_len >> 8) & 0xff;
1215
+            res += chunk_len & 0xff;
1216
+            for (i = 0; i < chunk_len - 2; i++)
1217
+                res += gb.buffer[i];
1218
+            if (checksum != res) {
1219
+                av_log(s->avctx, AV_LOG_WARNING, "Invalid LBR checksum\n");
1220
+                if (s->avctx->err_recognition & AV_EF_EXPLODE)
1221
+                    return AVERROR_INVALIDDATA;
1222
+            }
1223
+        } else {
1224
+            bytestream2_skip(&gb, 2);
1225
+        }
1226
+        break;
1227
+    case LBR_CHUNK_FRAME_NO_CSUM:
1228
+        break;
1229
+    default:
1230
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR frame chunk ID\n");
1231
+        return AVERROR_INVALIDDATA;
1232
+    }
1233
+
1234
+    // Clear current frame
1235
+    memset(s->quant_levels, 0, sizeof(s->quant_levels));
1236
+    memset(s->sb_indices, 0xff, sizeof(s->sb_indices));
1237
+    memset(s->sec_ch_sbms, 0, sizeof(s->sec_ch_sbms));
1238
+    memset(s->sec_ch_lrms, 0, sizeof(s->sec_ch_lrms));
1239
+    memset(s->ch_pres, 0, sizeof(s->ch_pres));
1240
+    memset(s->grid_1_scf, 0, sizeof(s->grid_1_scf));
1241
+    memset(s->grid_2_scf, 0, sizeof(s->grid_2_scf));
1242
+    memset(s->grid_3_avg, 0, sizeof(s->grid_3_avg));
1243
+    memset(s->grid_3_scf, 0, sizeof(s->grid_3_scf));
1244
+    memset(s->grid_3_pres, 0, sizeof(s->grid_3_pres));
1245
+    memset(s->tonal_scf, 0, sizeof(s->tonal_scf));
1246
+    memset(s->lfe_data, 0, sizeof(s->lfe_data));
1247
+    s->part_stereo_pres = 0;
1248
+    s->framenum = (s->framenum + 1) & 31;
1249
+
1250
+    for (ch = 0; ch < s->nchannels; ch++) {
1251
+        for (sb = 0; sb < s->nsubbands / 4; sb++) {
1252
+            s->part_stereo[ch][sb][0] = s->part_stereo[ch][sb][4];
1253
+            s->part_stereo[ch][sb][4] = 16;
1254
+        }
1255
+    }
1256
+
1257
+    memset(s->lpc_coeff[s->framenum & 1], 0, sizeof(s->lpc_coeff[0]));
1258
+
1259
+    for (group = 0; group < 5; group++) {
1260
+        for (sf = 0; sf < 1 << group; sf++) {
1261
+            int sf_idx = ((s->framenum << group) + sf) & 31;
1262
+            s->tonal_bounds[group][sf_idx][0] =
1263
+            s->tonal_bounds[group][sf_idx][1] = s->ntones;
1264
+        }
1265
+    }
1266
+
1267
+    // Parse chunk headers
1268
+    while (bytestream2_get_bytes_left(&gb) > 0) {
1269
+        chunk_id = bytestream2_get_byte(&gb);
1270
+        chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1271
+        chunk_id &= 0x7f;
1272
+
1273
+        if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1274
+            chunk_len = bytestream2_get_bytes_left(&gb);
1275
+            av_log(s->avctx, AV_LOG_WARNING, "LBR chunk %#x was truncated\n", chunk_id);
1276
+            if (s->avctx->err_recognition & AV_EF_EXPLODE)
1277
+                return AVERROR_INVALIDDATA;
1278
+        }
1279
+
1280
+        switch (chunk_id) {
1281
+        case LBR_CHUNK_LFE:
1282
+            chunk.lfe.len  = chunk_len;
1283
+            chunk.lfe.data = gb.buffer;
1284
+            break;
1285
+
1286
+        case LBR_CHUNK_SCF:
1287
+        case LBR_CHUNK_TONAL:
1288
+        case LBR_CHUNK_TONAL_SCF:
1289
+            chunk.tonal.id   = chunk_id;
1290
+            chunk.tonal.len  = chunk_len;
1291
+            chunk.tonal.data = gb.buffer;
1292
+            break;
1293
+
1294
+        case LBR_CHUNK_TONAL_GRP_1:
1295
+        case LBR_CHUNK_TONAL_GRP_2:
1296
+        case LBR_CHUNK_TONAL_GRP_3:
1297
+        case LBR_CHUNK_TONAL_GRP_4:
1298
+        case LBR_CHUNK_TONAL_GRP_5:
1299
+            i = LBR_CHUNK_TONAL_GRP_5 - chunk_id;
1300
+            chunk.tonal_grp[i].id   = i;
1301
+            chunk.tonal_grp[i].len  = chunk_len;
1302
+            chunk.tonal_grp[i].data = gb.buffer;
1303
+            break;
1304
+
1305
+        case LBR_CHUNK_TONAL_SCF_GRP_1:
1306
+        case LBR_CHUNK_TONAL_SCF_GRP_2:
1307
+        case LBR_CHUNK_TONAL_SCF_GRP_3:
1308
+        case LBR_CHUNK_TONAL_SCF_GRP_4:
1309
+        case LBR_CHUNK_TONAL_SCF_GRP_5:
1310
+            i = LBR_CHUNK_TONAL_SCF_GRP_5 - chunk_id;
1311
+            chunk.tonal_grp[i].id   = i;
1312
+            chunk.tonal_grp[i].len  = chunk_len;
1313
+            chunk.tonal_grp[i].data = gb.buffer;
1314
+            break;
1315
+
1316
+        case LBR_CHUNK_RES_GRID_LR:
1317
+        case LBR_CHUNK_RES_GRID_LR + 1:
1318
+        case LBR_CHUNK_RES_GRID_LR + 2:
1319
+            i = chunk_id - LBR_CHUNK_RES_GRID_LR;
1320
+            chunk.grid1[i].len  = chunk_len;
1321
+            chunk.grid1[i].data = gb.buffer;
1322
+            break;
1323
+
1324
+        case LBR_CHUNK_RES_GRID_HR:
1325
+        case LBR_CHUNK_RES_GRID_HR + 1:
1326
+        case LBR_CHUNK_RES_GRID_HR + 2:
1327
+            i = chunk_id - LBR_CHUNK_RES_GRID_HR;
1328
+            chunk.hr_grid[i].len  = chunk_len;
1329
+            chunk.hr_grid[i].data = gb.buffer;
1330
+            break;
1331
+
1332
+        case LBR_CHUNK_RES_TS_1:
1333
+        case LBR_CHUNK_RES_TS_1 + 1:
1334
+        case LBR_CHUNK_RES_TS_1 + 2:
1335
+            i = chunk_id - LBR_CHUNK_RES_TS_1;
1336
+            chunk.ts1[i].len  = chunk_len;
1337
+            chunk.ts1[i].data = gb.buffer;
1338
+            break;
1339
+
1340
+        case LBR_CHUNK_RES_TS_2:
1341
+        case LBR_CHUNK_RES_TS_2 + 1:
1342
+        case LBR_CHUNK_RES_TS_2 + 2:
1343
+            i = chunk_id - LBR_CHUNK_RES_TS_2;
1344
+            chunk.ts2[i].len  = chunk_len;
1345
+            chunk.ts2[i].data = gb.buffer;
1346
+            break;
1347
+        }
1348
+
1349
+        bytestream2_skip(&gb, chunk_len);
1350
+    }
1351
+
1352
+    // Parse the chunks
1353
+    ret = parse_lfe_chunk(s, &chunk.lfe);
1354
+
1355
+    ret |= parse_tonal_chunk(s, &chunk.tonal);
1356
+
1357
+    for (i = 0; i < 5; i++)
1358
+        ret |= parse_tonal_group(s, &chunk.tonal_grp[i]);
1359
+
1360
+    for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1361
+        int ch1 = i * 2;
1362
+        int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1363
+
1364
+        if (parse_grid_1_chunk (s, &chunk.grid1  [i], ch1, ch2) < 0 ||
1365
+            parse_high_res_grid(s, &chunk.hr_grid[i], ch1, ch2) < 0) {
1366
+            ret = -1;
1367
+            continue;
1368
+        }
1369
+
1370
+        // TS chunks depend on both grids. TS_2 depends on TS_1.
1371
+        if (!chunk.grid1[i].len || !chunk.hr_grid[i].len || !chunk.ts1[i].len)
1372
+            continue;
1373
+
1374
+        if (parse_ts1_chunk(s, &chunk.ts1[i], ch1, ch2) < 0 ||
1375
+            parse_ts2_chunk(s, &chunk.ts2[i], ch1, ch2) < 0) {
1376
+            ret = -1;
1377
+            continue;
1378
+        }
1379
+    }
1380
+
1381
+    if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1382
+        return AVERROR_INVALIDDATA;
1383
+
1384
+    return 0;
1385
+}
1386
+
1387
+/**
1388
+ * Reconstruct high-frequency resolution grid from first and third grids
1389
+ */
1390
+static void decode_grid(DCALbrDecoder *s, int ch1, int ch2)
1391
+{
1392
+    int i, ch, sb;
1393
+
1394
+    for (ch = ch1; ch <= ch2; ch++) {
1395
+        for (sb = 0; sb < s->nsubbands; sb++) {
1396
+            int g1_sb = ff_dca_scf_to_grid_1[sb];
1397
+
1398
+            uint8_t *g1_scf_a = s->grid_1_scf[ch][g1_sb    ];
1399
+            uint8_t *g1_scf_b = s->grid_1_scf[ch][g1_sb + 1];
1400
+
1401
+            int w1 = ff_dca_grid_1_weights[g1_sb    ][sb];
1402
+            int w2 = ff_dca_grid_1_weights[g1_sb + 1][sb];
1403
+
1404
+            uint8_t *hr_scf = s->high_res_scf[ch][sb];
1405
+
1406
+            if (sb < 4) {
1407
+                for (i = 0; i < 8; i++) {
1408
+                    int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1409
+                    hr_scf[i] = scf >> 7;
1410
+                }
1411
+            } else {
1412
+                int8_t *g3_scf = s->grid_3_scf[ch][sb - 4];
1413
+                int g3_avg = s->grid_3_avg[ch][sb - 4];
1414
+
1415
+                for (i = 0; i < 8; i++) {
1416
+                    int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1417
+                    hr_scf[i] = (scf >> 7) - g3_avg - g3_scf[i];
1418
+                }
1419
+            }
1420
+        }
1421
+    }
1422
+}
1423
+
1424
+/**
1425
+ * Fill unallocated subbands with randomness
1426
+ */
1427
+static void random_ts(DCALbrDecoder *s, int ch1, int ch2)
1428
+{
1429
+    int i, j, k, ch, sb;
1430
+
1431
+    for (ch = ch1; ch <= ch2; ch++) {
1432
+        for (sb = 0; sb < s->nsubbands; sb++) {
1433
+            float *samples = s->time_samples[ch][sb];
1434
+
1435
+            if (s->ch_pres[ch] & (1U << sb))
1436
+                continue;   // Skip allocated subband
1437
+
1438
+            if (sb < 2) {
1439
+                // The first two subbands are always zero
1440
+                memset(samples, 0, DCA_LBR_TIME_SAMPLES * sizeof(float));
1441
+            } else if (sb < 10) {
1442
+                for (i = 0; i < DCA_LBR_TIME_SAMPLES; i++)
1443
+                    samples[i] = lbr_rand(s, sb);
1444
+            } else {
1445
+                for (i = 0; i < DCA_LBR_TIME_SAMPLES / 8; i++, samples += 8) {
1446
+                    float accum[8] = { 0 };
1447
+
1448
+                    // Modulate by subbands 2-5 in blocks of 8
1449
+                    for (k = 2; k < 6; k++) {
1450
+                        float *other = &s->time_samples[ch][k][i * 8];
1451
+                        for (j = 0; j < 8; j++)
1452
+                            accum[j] += fabs(other[j]);
1453
+                    }
1454
+
1455
+                    for (j = 0; j < 8; j++)
1456
+                        samples[j] = (accum[j] * 0.25f + 0.5f) * lbr_rand(s, sb);
1457
+                }
1458
+            }
1459
+        }
1460
+    }
1461
+}
1462
+
1463
+static void predict(float *samples, const float *coeff, int nsamples)
1464
+{
1465
+    int i, j;
1466
+
1467
+    for (i = 0; i < nsamples; i++) {
1468
+        float res = 0;
1469
+        for (j = 0; j < 8; j++)
1470
+            res += coeff[j] * samples[i - j - 1];
1471
+        samples[i] -= res;
1472
+    }
1473
+}
1474
+
1475
+static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb)
1476
+{
1477
+    int f = s->framenum & 1;
1478
+    int ch;
1479
+
1480
+    for (ch = ch1; ch <= ch2; ch++) {
1481
+        float *samples = s->time_samples[ch][sb];
1482
+
1483
+        if (!(s->ch_pres[ch] & (1U << sb)))
1484
+            continue;
1485
+
1486
+        if (sb < 2) {
1487
+            predict(samples,      s->lpc_coeff[f^1][ch][sb][1],  16);
1488
+            predict(samples + 16, s->lpc_coeff[f  ][ch][sb][0],  64);
1489
+            predict(samples + 80, s->lpc_coeff[f  ][ch][sb][1],  48);
1490
+        } else {
1491
+            predict(samples,      s->lpc_coeff[f^1][ch][sb][0],  16);
1492
+            predict(samples + 16, s->lpc_coeff[f  ][ch][sb][0], 112);
1493
+        }
1494
+    }
1495
+}
1496
+
1497
+static void filter_ts(DCALbrDecoder *s, int ch1, int ch2)
1498
+{
1499
+    int i, j, sb, ch;
1500
+
1501
+    for (sb = 0; sb < s->nsubbands; sb++) {
1502
+        // Scale factors
1503
+        for (ch = ch1; ch <= ch2; ch++) {
1504
+            float *samples = s->time_samples[ch][sb];
1505
+            uint8_t *hr_scf = s->high_res_scf[ch][sb];
1506
+            if (sb < 4) {
1507
+                for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++, samples += 16) {
1508
+                    unsigned int scf = hr_scf[i];
1509
+                    if (scf > AMP_MAX)
1510
+                        scf = AMP_MAX;
1511
+                    for (j = 0; j < 16; j++)
1512
+                        samples[j] *= ff_dca_quant_amp[scf];
1513
+                }
1514
+            } else {
1515
+                uint8_t *g2_scf = s->grid_2_scf[ch][ff_dca_scf_to_grid_2[sb]];
1516
+                for (i = 0; i < DCA_LBR_TIME_SAMPLES / 2; i++, samples += 2) {
1517
+                    unsigned int scf = hr_scf[i / 8] - g2_scf[i];
1518
+                    if (scf > AMP_MAX)
1519
+                        scf = AMP_MAX;
1520
+                    samples[0] *= ff_dca_quant_amp[scf];
1521
+                    samples[1] *= ff_dca_quant_amp[scf];
1522
+                }
1523
+            }
1524
+        }
1525
+
1526
+        // Mid-side stereo
1527
+        if (ch1 != ch2) {
1528
+            float *samples_l = s->time_samples[ch1][sb];
1529
+            float *samples_r = s->time_samples[ch2][sb];
1530
+            int ch2_pres = s->ch_pres[ch2] & (1U << sb);
1531
+
1532
+            for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++) {
1533
+                int sbms = (s->sec_ch_sbms[ch1 / 2][sb] >> i) & 1;
1534
+                int lrms = (s->sec_ch_lrms[ch1 / 2][sb] >> i) & 1;
1535
+
1536
+                if (sb >= s->min_mono_subband) {
1537
+                    if (lrms && ch2_pres) {
1538
+                        if (sbms) {
1539
+                            for (j = 0; j < 16; j++) {
1540
+                                float tmp = samples_l[j];
1541
+                                samples_l[j] =  samples_r[j];
1542
+                                samples_r[j] = -tmp;
1543
+                            }
1544
+                        } else {
1545
+                            for (j = 0; j < 16; j++) {
1546
+                                float tmp = samples_l[j];
1547
+                                samples_l[j] =  samples_r[j];
1548
+                                samples_r[j] =  tmp;
1549
+                            }
1550
+                        }
1551
+                    } else if (!ch2_pres) {
1552
+                        if (sbms && (s->part_stereo_pres & (1 << ch1))) {
1553
+                            for (j = 0; j < 16; j++)
1554
+                                samples_r[j] = -samples_l[j];
1555
+                        } else {
1556
+                            for (j = 0; j < 16; j++)
1557
+                                samples_r[j] =  samples_l[j];
1558
+                        }
1559
+                    }
1560
+                } else if (sbms && ch2_pres) {
1561
+                    for (j = 0; j < 16; j++) {
1562
+                        float tmp = samples_l[j];
1563
+                        samples_l[j] = (tmp + samples_r[j]) * 0.5f;
1564
+                        samples_r[j] = (tmp - samples_r[j]) * 0.5f;
1565
+                    }
1566
+                }
1567
+
1568
+                samples_l += 16;
1569
+                samples_r += 16;
1570
+            }
1571
+        }
1572
+
1573
+        // Inverse prediction
1574
+        if (sb < 3)
1575
+            synth_lpc(s, ch1, ch2, sb);
1576
+    }
1577
+}
1578
+
1579
+/**
1580
+ * Modulate by interpolated partial stereo coefficients
1581
+ */
1582
+static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2)
1583
+{
1584
+    int i, ch, sb, sf;
1585
+
1586
+    for (ch = ch1; ch <= ch2; ch++) {
1587
+        for (sb = s->min_mono_subband; sb < s->nsubbands; sb++) {
1588
+            uint8_t *pt_st = s->part_stereo[ch][(sb - s->min_mono_subband) / 4];
1589
+            float *samples = s->time_samples[ch][sb];
1590
+
1591
+            if (s->ch_pres[ch2] & (1U << sb))
1592
+                continue;
1593
+
1594
+            for (sf = 1; sf <= 4; sf++, samples += 32) {
1595
+                float prev = ff_dca_st_coeff[pt_st[sf - 1]];
1596
+                float next = ff_dca_st_coeff[pt_st[sf    ]];
1597
+
1598
+                for (i = 0; i < 32; i++)
1599
+                    samples[i] *= (32 - i) * prev + i * next;
1600
+            }
1601
+        }
1602
+    }
1603
+}
1604
+
1605
+/**
1606
+ * Synthesise tones in the given group for the given tonal subframe
1607
+ */
1608
+static void synth_tones(DCALbrDecoder *s, int ch, float *values,
1609
+                        int group, int group_sf, int synth_idx)
1610
+{
1611
+    int i, start, count;
1612
+
1613
+    if (synth_idx < 0)
1614
+        return;
1615
+
1616
+    start =  s->tonal_bounds[group][group_sf][0];
1617
+    count = (s->tonal_bounds[group][group_sf][1] - start) & (DCA_LBR_TONES - 1);
1618
+
1619
+    for (i = 0; i < count; i++) {
1620
+        DCALbrTone *t = &s->tones[(start + i) & (DCA_LBR_TONES - 1)];
1621
+
1622
+        if (t->amp[ch]) {
1623
+            float amp = ff_dca_synth_env[synth_idx] * ff_dca_quant_amp[t->amp[ch]];
1624
+            float c = amp * cos_tab[(t->phs[ch]     ) & 255];
1625
+            float s = amp * cos_tab[(t->phs[ch] + 64) & 255];
1626
+            const float *cf = ff_dca_corr_cf[t->f_delt];
1627
+            int x_freq = t->x_freq;
1628
+
1629
+            switch (x_freq) {
1630
+            case 0:
1631
+                goto p0;
1632
+            case 1:
1633
+                values[3] += cf[0] * -s;
1634
+                values[2] += cf[1] *  c;
1635
+                values[1] += cf[2] *  s;
1636
+                values[0] += cf[3] * -c;
1637
+                goto p1;
1638
+            case 2:
1639
+                values[2] += cf[0] * -s;
1640
+                values[1] += cf[1] *  c;
1641
+                values[0] += cf[2] *  s;
1642
+                goto p2;
1643
+            case 3:
1644
+                values[1] += cf[0] * -s;
1645
+                values[0] += cf[1] *  c;
1646
+                goto p3;
1647
+            case 4:
1648
+                values[0] += cf[0] * -s;
1649
+                goto p4;
1650
+            }
1651
+
1652
+            values[x_freq - 5] += cf[ 0] * -s;
1653
+        p4: values[x_freq - 4] += cf[ 1] *  c;
1654
+        p3: values[x_freq - 3] += cf[ 2] *  s;
1655
+        p2: values[x_freq - 2] += cf[ 3] * -c;
1656
+        p1: values[x_freq - 1] += cf[ 4] * -s;
1657
+        p0: values[x_freq    ] += cf[ 5] *  c;
1658
+            values[x_freq + 1] += cf[ 6] *  s;
1659
+            values[x_freq + 2] += cf[ 7] * -c;
1660
+            values[x_freq + 3] += cf[ 8] * -s;
1661
+            values[x_freq + 4] += cf[ 9] *  c;
1662
+            values[x_freq + 5] += cf[10] *  s;
1663
+        }
1664
+
1665
+        t->phs[ch] += t->ph_rot;
1666
+    }
1667
+}
1668
+
1669
+/**
1670
+ * Synthesise all tones in all groups for the given residual subframe
1671
+ */
1672
+static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf)
1673
+{
1674
+    int group;
1675
+
1676
+    // Tonal vs residual shift is 22 subframes
1677
+    for (group = 0; group < 5; group++) {
1678
+        int group_sf = (s->framenum << group) + ((sf - 22) >> (5 - group));
1679
+        int synth_idx = ((((sf - 22) & 31) << group) & 31) + (1 << group) - 1;
1680
+
1681
+        synth_tones(s, ch, values, group, (group_sf - 1) & 31, 30 - synth_idx);
1682
+        synth_tones(s, ch, values, group, (group_sf    ) & 31,      synth_idx);
1683
+    }
1684
+}
1685
+
1686
+static void transform_channel(DCALbrDecoder *s, int ch, float *output)
1687
+{
1688
+    LOCAL_ALIGNED_32(float, values, [DCA_LBR_SUBBANDS    ], [4]);
1689
+    LOCAL_ALIGNED_32(float, result, [DCA_LBR_SUBBANDS * 2], [4]);
1690
+    int sf, sb, nsubbands = s->nsubbands, noutsubbands = 8 << s->freq_range;
1691
+
1692
+    // Clear inactive subbands
1693
+    if (nsubbands < noutsubbands)
1694
+        memset(values[nsubbands], 0, (noutsubbands - nsubbands) * sizeof(values[0]));
1695
+
1696
+    for (sf = 0; sf < DCA_LBR_TIME_SAMPLES / 4; sf++) {
1697
+        // Hybrid filterbank
1698
+        s->dcadsp->lbr_bank(values, s->time_samples[ch],
1699
+                            ff_dca_bank_coeff, sf * 4, nsubbands);
1700
+
1701
+        base_func_synth(s, ch, values[0], sf);
1702
+
1703
+        s->imdct.imdct_calc(&s->imdct, result[0], values[0]);
1704
+
1705
+        // Long window and overlap-add
1706
+        s->fdsp->vector_fmul_add(output, result[0], s->window,
1707
+                                 s->history[ch], noutsubbands * 4);
1708
+        s->fdsp->vector_fmul_reverse(s->history[ch], result[noutsubbands],
1709
+                                     s->window, noutsubbands * 4);
1710
+        output += noutsubbands * 4;
1711
+    }
1712
+
1713
+    // Update history for LPC and forward MDCT
1714
+    for (sb = 0; sb < nsubbands; sb++) {
1715
+        float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1716
+        memcpy(samples, samples + DCA_LBR_TIME_SAMPLES, DCA_LBR_TIME_HISTORY * sizeof(float));
1717
+    }
1718
+}
1719
+
1720
+int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
1721
+{
1722
+    AVCodecContext *avctx = s->avctx;
1723
+    int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1;
1724
+    const int8_t *reorder;
1725
+
1726
+    avctx->channel_layout = channel_layouts[ch_conf];
1727
+    avctx->channels = nchannels = channel_counts[ch_conf];
1728
+    avctx->sample_rate = s->sample_rate;
1729
+    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1730
+    avctx->bits_per_raw_sample = 0;
1731
+    avctx->profile = FF_PROFILE_DTS_EXPRESS;
1732
+    avctx->bit_rate = s->bit_rate_scaled;
1733
+
1734
+    if (s->flags & LBR_FLAG_LFE_PRESENT) {
1735
+        avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1736
+        avctx->channels++;
1737
+        reorder = channel_reorder_lfe[ch_conf];
1738
+    } else {
1739
+        reorder = channel_reorder_nolfe[ch_conf];
1740
+    }
1741
+
1742
+    frame->nb_samples = 1024 << s->freq_range;
1743
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1744
+        return ret;
1745
+
1746
+    // Filter fullband channels
1747
+    for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1748
+        int ch1 = i * 2;
1749
+        int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1750
+
1751
+        decode_grid(s, ch1, ch2);
1752
+
1753
+        random_ts(s, ch1, ch2);
1754
+
1755
+        filter_ts(s, ch1, ch2);
1756
+
1757
+        if (ch1 != ch2 && (s->part_stereo_pres & (1 << ch1)))
1758
+            decode_part_stereo(s, ch1, ch2);
1759
+
1760
+        if (ch1 < nchannels)
1761
+            transform_channel(s, ch1, (float *)frame->extended_data[reorder[ch1]]);
1762
+
1763
+        if (ch1 != ch2 && ch2 < nchannels)
1764
+            transform_channel(s, ch2, (float *)frame->extended_data[reorder[ch2]]);
1765
+    }
1766
+
1767
+    // Interpolate LFE channel
1768
+    if (s->flags & LBR_FLAG_LFE_PRESENT) {
1769
+        s->dcadsp->lfe_iir((float *)frame->extended_data[lfe_index[ch_conf]],
1770
+                           s->lfe_data, ff_dca_lfe_iir,
1771
+                           s->lfe_history, 16 << s->freq_range);
1772
+    }
1773
+
1774
+    if ((ret = ff_side_data_update_matrix_encoding(frame, AV_MATRIX_ENCODING_NONE)) < 0)
1775
+        return ret;
1776
+
1777
+    return 0;
1778
+}
1779
+
1780
+av_cold void ff_dca_lbr_flush(DCALbrDecoder *s)
1781
+{
1782
+    int ch, sb;
1783
+
1784
+    if (!s->sample_rate)
1785
+        return;
1786
+
1787
+    // Clear history
1788
+    memset(s->part_stereo, 16, sizeof(s->part_stereo));
1789
+    memset(s->lpc_coeff, 0, sizeof(s->lpc_coeff));
1790
+    memset(s->history, 0, sizeof(s->history));
1791
+    memset(s->tonal_bounds, 0, sizeof(s->tonal_bounds));
1792
+    memset(s->lfe_history, 0, sizeof(s->lfe_history));
1793
+    s->framenum = 0;
1794
+    s->ntones = 0;
1795
+
1796
+    for (ch = 0; ch < s->nchannels; ch++) {
1797
+        for (sb = 0; sb < s->nsubbands; sb++) {
1798
+            float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1799
+            memset(samples, 0, DCA_LBR_TIME_HISTORY * sizeof(float));
1800
+        }
1801
+    }
1802
+}
1803
+
1804
+av_cold int ff_dca_lbr_init(DCALbrDecoder *s)
1805
+{
1806
+    init_tables();
1807
+
1808
+    if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
1809
+        return -1;
1810
+
1811
+    s->lbr_rand = 1;
1812
+    return 0;
1813
+}
1814
+
1815
+av_cold void ff_dca_lbr_close(DCALbrDecoder *s)
1816
+{
1817
+    s->sample_rate = 0;
1818
+
1819
+    av_freep(&s->ts_buffer);
1820
+    s->ts_size = 0;
1821
+
1822
+    av_freep(&s->fdsp);
1823
+    ff_mdct_end(&s->imdct);
1824
+}
0 1825
new file mode 100644
... ...
@@ -0,0 +1,130 @@
0
+/*
1
+ * Copyright (C) 2016 foo86
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#ifndef AVCODEC_DCA_LBR_H
21
+#define AVCODEC_DCA_LBR_H
22
+
23
+#include "libavutil/common.h"
24
+#include "libavutil/float_dsp.h"
25
+#include "libavutil/mem.h"
26
+
27
+#include "avcodec.h"
28
+#include "internal.h"
29
+#include "get_bits.h"
30
+#include "dca.h"
31
+#include "dca_exss.h"
32
+#include "dcadsp.h"
33
+#include "fft.h"
34
+
35
+#define DCA_LBR_CHANNELS        6
36
+#define DCA_LBR_CHANNELS_TOTAL  32
37
+#define DCA_LBR_SUBBANDS        32
38
+#define DCA_LBR_TONES           512
39
+
40
+#define DCA_LBR_TIME_SAMPLES    128
41
+#define DCA_LBR_TIME_HISTORY    8
42
+
43
+typedef struct DCALbrTone {
44
+    uint8_t     x_freq;     ///< Spectral line offset
45
+    uint8_t     f_delt;     ///< Difference between original and center frequency
46
+    uint8_t     ph_rot;     ///< Phase rotation
47
+    uint8_t     pad;        ///< Padding field
48
+    uint8_t     amp[DCA_LBR_CHANNELS];  ///< Per-channel amplitude
49
+    uint8_t     phs[DCA_LBR_CHANNELS];  ///< Per-channel phase
50
+} DCALbrTone;
51
+
52
+typedef struct DCALbrDecoder {
53
+    AVCodecContext  *avctx;
54
+    GetBitContext   gb;
55
+
56
+    int     sample_rate;        ///< Sample rate of LBR audio
57
+    int     ch_mask;            ///< LBR speaker mask
58
+    int     flags;              ///< Flags for LBR decoder initialization
59
+    int     bit_rate_orig;      ///< Original bit rate
60
+    int     bit_rate_scaled;    ///< Scaled bit rate
61
+
62
+    int     nchannels;          ///< Number of fullband channels to decode
63
+    int     nchannels_total;    ///< Total number of fullband channels
64
+    int     freq_range;         ///< Frequency range of LBR audio
65
+    int     band_limit;         ///< Band limit factor
66
+    int     limited_rate;       ///< Band limited sample rate
67
+    int     limited_range;      ///< Band limited frequency range
68
+    int     res_profile;        ///< Resolution profile
69
+    int     nsubbands;          ///< Number of encoded subbands
70
+    int     g3_avg_only_start_sb;   ///< Subband index where grid 3 scale factors end
71
+    int     min_mono_subband;   ///< Subband index where mono encoding starts
72
+    int     max_mono_subband;   ///< Subband index where mono encoding ends
73
+
74
+    int     framenum;   ///< Lower 5 bits of current frame number
75
+    int     lbr_rand;   ///< Seed for subband randomization
76
+    int     warned;     ///< Flags for warning suppression
77
+
78
+    uint8_t     quant_levels[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS];   ///< Quantization levels
79
+    uint8_t     sb_indices[DCA_LBR_SUBBANDS];   ///< Subband reordering indices
80
+
81
+    uint8_t     sec_ch_sbms[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS];    ///< Right channel inversion or mid/side decoding flags
82
+    uint8_t     sec_ch_lrms[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS];    ///< Flags indicating if left/right channel are swapped
83
+    uint32_t    ch_pres[DCA_LBR_CHANNELS];  ///< Subband allocation flags
84
+
85
+    uint8_t     grid_1_scf[DCA_LBR_CHANNELS][12][8];    ///< Grid 1 scale factors
86
+    uint8_t     grid_2_scf[DCA_LBR_CHANNELS][3][64];    ///< Grid 2 scale factors
87
+
88
+    int8_t      grid_3_avg[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS - 4];     ///< Grid 3 average values
89
+    int8_t      grid_3_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS - 4][8];  ///< Grid 3 scale factors
90
+    uint32_t    grid_3_pres[DCA_LBR_CHANNELS];  ///< Grid 3 scale factors presence flags
91
+
92
+    uint8_t     high_res_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS][8];    ///< High-frequency resolution scale factors
93
+
94
+    uint8_t     part_stereo[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS / 4][5]; ///< Partial stereo coefficients
95
+    uint8_t     part_stereo_pres;   ///< Partial stereo coefficients presence flags
96
+
97
+    float       lpc_coeff[2][DCA_LBR_CHANNELS][3][2][8];    ///< Predictor coefficients
98
+
99
+    float       sb_scf[DCA_LBR_SUBBANDS];   ///< Subband randomization scale factors
100
+
101
+    float       *time_samples[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS]; ///< Time samples
102
+
103
+    float           *ts_buffer; ///< Time sample buffer base
104
+    unsigned int    ts_size;    ///< Time sample buffer size
105
+
106
+    DECLARE_ALIGNED(32, float, history)[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS * 4];    ///< IMDCT history
107
+    DECLARE_ALIGNED(32, float, window)[DCA_LBR_SUBBANDS * 4];   ///< Long window for IMDCT
108
+
109
+    DECLARE_ALIGNED(32, float, lfe_data)[64];       ///< Decimated LFE samples
110
+    DECLARE_ALIGNED(32, float, lfe_history)[5][2];  ///< LFE IIR filter history
111
+    float lfe_scale;    ///< Scale factor of LFE samples before IIR filter
112
+
113
+    uint8_t     tonal_scf[6];           ///< Tonal scale factors
114
+    uint16_t    tonal_bounds[5][32][2]; ///< Per-group per-subframe start/end positions of tones
115
+    DCALbrTone  tones[DCA_LBR_TONES];   ///< Circular buffer of tones
116
+    int         ntones;                 ///< Circular buffer head position
117
+
118
+    FFTContext          imdct;
119
+    AVFloatDSPContext   *fdsp;
120
+    DCADSPContext       *dcadsp;
121
+} DCALbrDecoder;
122
+
123
+int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset);
124
+int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame);
125
+av_cold void ff_dca_lbr_flush(DCALbrDecoder *s);
126
+av_cold int ff_dca_lbr_init(DCALbrDecoder *s);
127
+av_cold void ff_dca_lbr_close(DCALbrDecoder *s);
128
+
129
+#endif
... ...
@@ -8729,3 +8729,469 @@ const int32_t ff_dca_sampling_freqs[16] = {
8729 8729
       8000,  16000, 32000, 64000, 128000, 22050,  44100,  88200,
8730 8730
     176400, 352800, 12000, 24000,  48000, 96000, 192000, 384000,
8731 8731
 };
8732
+
8733
+const uint16_t ff_dca_avg_g3_freqs[3] = { 16000, 18000, 24000 };
8734
+
8735
+const uint16_t ff_dca_fst_amp[44] = {
8736
+       0,    1,    2,    3,
8737
+       4,    6,    8,   10,
8738
+      12,   16,   20,   24,
8739
+      28,   36,   44,   52,
8740
+      60,   76,   92,  108,
8741
+     124,  156,  188,  220,
8742
+     252,  316,  380,  444,
8743
+     508,  636,  764,  892,
8744
+    1020, 1276, 1532, 1788,
8745
+    2044, 2556, 3068, 3580,
8746
+    4092, 5116, 6140, 7164
8747
+};
8748
+
8749
+const uint8_t ff_dca_freq_to_sb[32] = {
8750
+    0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
8751
+    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
8752
+};
8753
+
8754
+const int8_t ff_dca_ph0_shift[8] = {
8755
+    -32, +96, -96, +32, +96, -32, +32, -96
8756
+};
8757
+
8758
+const uint8_t ff_dca_grid_1_to_scf[11] = {
8759
+    0, 1, 2, 3, 4, 6, 7, 10, 14, 19, 26
8760
+};
8761
+
8762
+const uint8_t ff_dca_grid_2_to_scf[3] = {
8763
+    4, 10, 18
8764
+};
8765
+
8766
+const uint8_t ff_dca_scf_to_grid_1[32] = {
8767
+    0, 1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7,
8768
+    7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10
8769
+};
8770
+
8771
+const uint8_t ff_dca_scf_to_grid_2[32] = {
8772
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
8773
+    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
8774
+};
8775
+
8776
+const uint8_t ff_dca_grid_1_weights[12][32] = {
8777
+    {
8778
+        128,   0,   0,   0,   0,   0,   0,   0,
8779
+          0,   0,   0,   0,   0,   0,   0,   0,
8780
+          0,   0,   0,   0,   0,   0,   0,   0,
8781
+          0,   0,   0,   0,   0,   0,   0,   0,
8782
+    }, {
8783
+          0, 128,   0,   0,   0,   0,   0,   0,
8784
+          0,   0,   0,   0,   0,   0,   0,   0,
8785
+          0,   0,   0,   0,   0,   0,   0,   0,
8786
+          0,   0,   0,   0,   0,   0,   0,   0,
8787
+    }, {
8788
+          0,   0, 128,   0,   0,   0,   0,   0,
8789
+          0,   0,   0,   0,   0,   0,   0,   0,
8790
+          0,   0,   0,   0,   0,   0,   0,   0,
8791
+          0,   0,   0,   0,   0,   0,   0,   0,
8792
+    }, {
8793
+          0,   0,   0, 128,   0,   0,   0,   0,
8794
+          0,   0,   0,   0,   0,   0,   0,   0,
8795
+          0,   0,   0,   0,   0,   0,   0,   0,
8796
+          0,   0,   0,   0,   0,   0,   0,   0,
8797
+    }, {
8798
+          0,   0,   0,   0, 128, 128,   0,   0,
8799
+          0,   0,   0,   0,   0,   0,   0,   0,
8800
+          0,   0,   0,   0,   0,   0,   0,   0,
8801
+          0,   0,   0,   0,   0,   0,   0,   0,
8802
+    }, {
8803
+          0,   0,   0,   0,   0,   0, 128,  85,
8804
+         43,   0,   0,   0,   0,   0,   0,   0,
8805
+          0,   0,   0,   0,   0,   0,   0,   0,
8806
+          0,   0,   0,   0,   0,   0,   0,   0,
8807
+    }, {
8808
+          0,   0,   0,   0,   0,   0,   0,  43,
8809
+         85, 128,  96,  64,  32,   0,   0,   0,
8810
+          0,   0,   0,   0,   0,   0,   0,   0,
8811
+          0,   0,   0,   0,   0,   0,   0,   0,
8812
+    }, {
8813
+          0,   0,   0,   0,   0,   0,   0,   0,
8814
+          0,   0,  32,  64,  96, 128, 102,  77,
8815
+         51,  26,   0,   0,   0,   0,   0,   0,
8816
+          0,   0,   0,   0,   0,   0,   0,   0,
8817
+    }, {
8818
+          0,   0,   0,   0,   0,   0,   0,   0,
8819
+          0,   0,   0,   0,   0,   0,  26,  51,
8820
+         77, 102, 128, 107,  85,  64,  43,  21,
8821
+          0,   0,   0,   0,   0,   0,   0,   0,
8822
+    }, {
8823
+          0,   0,   0,   0,   0,   0,   0,   0,
8824
+          0,   0,   0,   0,   0,   0,   0,   0,
8825
+          0,   0,   0,  21,  43,  64,  85, 107,
8826
+        128, 110,  91,  73,  55,  37,  18,   0,
8827
+    }, {
8828
+          0,   0,   0,   0,   0,   0,   0,   0,
8829
+          0,   0,   0,   0,   0,   0,   0,   0,
8830
+          0,   0,   0,   0,   0,   0,   0,   0,
8831
+          0,  18,  37,  55,  73,  91, 110, 128,
8832
+    }, {
8833
+        /* empty */
8834
+    }
8835
+};
8836
+
8837
+const uint8_t ff_dca_sb_reorder[8][8] = {
8838
+    { 0, 1, 2, 3, 4, 5, 6, 7 },
8839
+    { 1, 0, 2, 3, 4, 5, 6, 7 },
8840
+    { 3, 1, 0, 2, 4, 5, 6, 7 },
8841
+    { 1, 2, 3, 0, 4, 5, 6, 7 },
8842
+    { 1, 2, 5, 3, 0, 4, 6, 7 },
8843
+    { 1, 2, 2, 5, 3, 0, 4, 6 },
8844
+    { 1, 2, 2, 6, 5, 3, 0, 4 },
8845
+    { 1, 2, 2, 6, 5, 4, 0, 3 }
8846
+};
8847
+
8848
+const int8_t ff_dca_lfe_delta_index_16[8] = {
8849
+    -4, -3, -2, -1, 2, 4, 6, 8
8850
+};
8851
+
8852
+const int8_t ff_dca_lfe_delta_index_24[32] = {
8853
+    -8, -8, -7, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1,
8854
+     1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,  8
8855
+};
8856
+
8857
+const uint16_t ff_dca_rsd_pack_5_in_8[256] = {
8858
+    0x0000, 0x0100, 0x0200, 0x0040, 0x0140, 0x0240, 0x0080, 0x0180,
8859
+    0x0280, 0x0010, 0x0110, 0x0210, 0x0050, 0x0150, 0x0250, 0x0090,
8860
+    0x0190, 0x0290, 0x0020, 0x0120, 0x0220, 0x0060, 0x0160, 0x0260,
8861
+    0x00a0, 0x01a0, 0x02a0, 0x0004, 0x0104, 0x0204, 0x0044, 0x0144,
8862
+    0x0244, 0x0084, 0x0184, 0x0284, 0x0014, 0x0114, 0x0214, 0x0054,
8863
+    0x0154, 0x0254, 0x0094, 0x0194, 0x0294, 0x0024, 0x0124, 0x0224,
8864
+    0x0064, 0x0164, 0x0264, 0x00a4, 0x01a4, 0x02a4, 0x0008, 0x0108,
8865
+    0x0208, 0x0048, 0x0148, 0x0248, 0x0088, 0x0188, 0x0288, 0x0018,
8866
+    0x0118, 0x0218, 0x0058, 0x0158, 0x0258, 0x0098, 0x0198, 0x0298,
8867
+    0x0028, 0x0128, 0x0228, 0x0068, 0x0168, 0x0268, 0x00a8, 0x01a8,
8868
+    0x02a8, 0x0001, 0x0101, 0x0201, 0x0041, 0x0141, 0x0241, 0x0081,
8869
+    0x0181, 0x0281, 0x0011, 0x0111, 0x0211, 0x0051, 0x0151, 0x0251,
8870
+    0x0091, 0x0191, 0x0291, 0x0021, 0x0121, 0x0221, 0x0061, 0x0161,
8871
+    0x0261, 0x00a1, 0x01a1, 0x02a1, 0x0005, 0x0105, 0x0205, 0x0045,
8872
+    0x0145, 0x0245, 0x0085, 0x0185, 0x0285, 0x0015, 0x0115, 0x0215,
8873
+    0x0055, 0x0155, 0x0255, 0x0095, 0x0195, 0x0295, 0x0025, 0x0125,
8874
+    0x0225, 0x0065, 0x0165, 0x0265, 0x00a5, 0x01a5, 0x02a5, 0x0009,
8875
+    0x0109, 0x0209, 0x0049, 0x0149, 0x0249, 0x0089, 0x0189, 0x0289,
8876
+    0x0019, 0x0119, 0x0219, 0x0059, 0x0159, 0x0259, 0x0099, 0x0199,
8877
+    0x0299, 0x0029, 0x0129, 0x0229, 0x0069, 0x0169, 0x0269, 0x00a9,
8878
+    0x01a9, 0x02a9, 0x0002, 0x0102, 0x0202, 0x0042, 0x0142, 0x0242,
8879
+    0x0082, 0x0182, 0x0282, 0x0012, 0x0112, 0x0212, 0x0052, 0x0152,
8880
+    0x0252, 0x0092, 0x0192, 0x0292, 0x0022, 0x0122, 0x0222, 0x0062,
8881
+    0x0162, 0x0262, 0x00a2, 0x01a2, 0x02a2, 0x0006, 0x0106, 0x0206,
8882
+    0x0046, 0x0146, 0x0246, 0x0086, 0x0186, 0x0286, 0x0016, 0x0116,
8883
+    0x0216, 0x0056, 0x0156, 0x0256, 0x0096, 0x0196, 0x0296, 0x0026,
8884
+    0x0126, 0x0226, 0x0066, 0x0166, 0x0266, 0x00a6, 0x01a6, 0x02a6,
8885
+    0x000a, 0x010a, 0x020a, 0x004a, 0x014a, 0x024a, 0x008a, 0x018a,
8886
+    0x028a, 0x001a, 0x011a, 0x021a, 0x005a, 0x015a, 0x025a, 0x009a,
8887
+    0x019a, 0x029a, 0x002a, 0x012a, 0x022a, 0x006a, 0x016a, 0x026a,
8888
+    0x00aa, 0x01aa, 0x02aa, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155,
8889
+    0x0155, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155, 0x0155
8890
+};
8891
+
8892
+const uint8_t ff_dca_rsd_pack_3_in_7[128][3] = {
8893
+    { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, { 0, 0, 3 },
8894
+    { 0, 0, 4 }, { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 },
8895
+    { 0, 1, 3 }, { 0, 1, 4 }, { 0, 2, 0 }, { 0, 2, 1 },
8896
+    { 0, 2, 2 }, { 0, 2, 3 }, { 0, 2, 4 }, { 0, 3, 0 },
8897
+    { 0, 3, 1 }, { 0, 3, 2 }, { 0, 3, 3 }, { 0, 3, 4 },
8898
+    { 0, 4, 0 }, { 0, 4, 1 }, { 0, 4, 2 }, { 0, 4, 3 },
8899
+    { 0, 4, 4 }, { 1, 0, 0 }, { 1, 0, 1 }, { 1, 0, 2 },
8900
+    { 1, 0, 3 }, { 1, 0, 4 }, { 1, 1, 0 }, { 1, 1, 1 },
8901
+    { 1, 1, 2 }, { 1, 1, 3 }, { 1, 1, 4 }, { 1, 2, 0 },
8902
+    { 1, 2, 1 }, { 1, 2, 2 }, { 1, 2, 3 }, { 1, 2, 4 },
8903
+    { 1, 3, 0 }, { 1, 3, 1 }, { 1, 3, 2 }, { 1, 3, 3 },
8904
+    { 1, 3, 4 }, { 1, 4, 0 }, { 1, 4, 1 }, { 1, 4, 2 },
8905
+    { 1, 4, 3 }, { 1, 4, 4 }, { 2, 0, 0 }, { 2, 0, 1 },
8906
+    { 2, 0, 2 }, { 2, 0, 3 }, { 2, 0, 4 }, { 2, 1, 0 },
8907
+    { 2, 1, 1 }, { 2, 1, 2 }, { 2, 1, 3 }, { 2, 1, 4 },
8908
+    { 2, 2, 0 }, { 2, 2, 1 }, { 2, 2, 2 }, { 2, 2, 3 },
8909
+    { 2, 2, 4 }, { 2, 3, 0 }, { 2, 3, 1 }, { 2, 3, 2 },
8910
+    { 2, 3, 3 }, { 2, 3, 4 }, { 2, 4, 0 }, { 2, 4, 1 },
8911
+    { 2, 4, 2 }, { 2, 4, 3 }, { 2, 4, 4 }, { 3, 0, 0 },
8912
+    { 3, 0, 1 }, { 3, 0, 2 }, { 3, 0, 3 }, { 3, 0, 4 },
8913
+    { 3, 1, 0 }, { 3, 1, 1 }, { 3, 1, 2 }, { 3, 1, 3 },
8914
+    { 3, 1, 4 }, { 3, 2, 0 }, { 3, 2, 1 }, { 3, 2, 2 },
8915
+    { 3, 2, 3 }, { 3, 2, 4 }, { 3, 3, 0 }, { 3, 3, 1 },
8916
+    { 3, 3, 2 }, { 3, 3, 3 }, { 3, 3, 4 }, { 3, 4, 0 },
8917
+    { 3, 4, 1 }, { 3, 4, 2 }, { 3, 4, 3 }, { 3, 4, 4 },
8918
+    { 4, 0, 0 }, { 4, 0, 1 }, { 4, 0, 2 }, { 4, 0, 3 },
8919
+    { 4, 0, 4 }, { 4, 1, 0 }, { 4, 1, 1 }, { 4, 1, 2 },
8920
+    { 4, 1, 3 }, { 4, 1, 4 }, { 4, 2, 0 }, { 4, 2, 1 },
8921
+    { 4, 2, 2 }, { 4, 2, 3 }, { 4, 2, 4 }, { 4, 3, 0 },
8922
+    { 4, 3, 1 }, { 4, 3, 2 }, { 4, 3, 3 }, { 4, 3, 4 },
8923
+    { 4, 4, 0 }, { 4, 4, 1 }, { 4, 4, 2 }, { 4, 4, 3 },
8924
+    { 4, 4, 4 }, { 2, 2, 2 }, { 2, 2, 2 }, { 2, 2, 2 }
8925
+};
8926
+
8927
+const float ff_dca_rsd_level_2a[2] = {
8928
+    -0.47, 0.47
8929
+};
8930
+
8931
+const float ff_dca_rsd_level_2b[2] = {
8932
+    -0.645, 0.645
8933
+};
8934
+
8935
+const float ff_dca_rsd_level_3[3] = {
8936
+    -0.645, 0.0, 0.645
8937
+};
8938
+
8939
+const float ff_dca_rsd_level_5[5] = {
8940
+    -0.875, -0.375, 0.0, 0.375, 0.875
8941
+};
8942
+
8943
+const float ff_dca_rsd_level_8[8] = {
8944
+    -1.0, -0.625, -0.291666667, 0.0, 0.25, 0.5, 0.75, 1.0
8945
+};
8946
+
8947
+const float ff_dca_rsd_level_16[16] = {
8948
+    -1.3125, -1.1375, -0.9625, -0.7875,
8949
+    -0.6125, -0.4375, -0.2625, -0.0875,
8950
+     0.0875,  0.2625,  0.4375,  0.6125,
8951
+     0.7875,  0.9625,  1.1375,  1.3125
8952
+};
8953
+
8954
+const float ff_dca_synth_env[32] = {
8955
+    0.00240763666390, 0.00960735979838, 0.02152983213390, 0.03806023374436,
8956
+    0.05903936782582, 0.08426519384873, 0.11349477331863, 0.14644660940673,
8957
+    0.18280335791818, 0.22221488349020, 0.26430163158700, 0.30865828381746,
8958
+    0.35485766137277, 0.40245483899194, 0.45099142983522, 0.5,
8959
+    0.54900857016478, 0.59754516100806, 0.64514233862723, 0.69134171618254,
8960
+    0.73569836841300, 0.77778511650980, 0.81719664208182, 0.85355339059327,
8961
+    0.88650522668137, 0.91573480615127, 0.94096063217418, 0.96193976625564,
8962
+    0.97847016786610, 0.99039264020162, 0.99759236333610, 1.0
8963
+};
8964
+
8965
+const float ff_dca_corr_cf[32][11] = {
8966
+    {-0.01179, 0.04281, 0.46712, 0.46345,-3.94525, 3.94525,
8967
+     -0.46345,-0.46712,-0.04281, 0.01179,-0.00299 },
8968
+    {-0.00929, 0.04882, 0.45252, 0.37972,-3.85446, 4.03189,
8969
+     -0.55069,-0.48040,-0.03599, 0.01445,-0.00229 },
8970
+    {-0.00696, 0.05403, 0.43674, 0.29961,-3.75975, 4.11413,
8971
+     -0.64135,-0.49221,-0.02834, 0.01726,-0.00156 },
8972
+    {-0.00481, 0.05847, 0.41993, 0.22319,-3.66138, 4.19175,
8973
+     -0.73529,-0.50241,-0.01983, 0.02021,-0.00080 },
8974
+    {-0.00284, 0.06216, 0.40224, 0.15053,-3.55963, 4.26452,
8975
+     -0.83239,-0.51085,-0.01047, 0.02328,-0.00003 },
8976
+    {-0.00105, 0.06515, 0.38378, 0.08168,-3.45475, 4.33225,
8977
+     -0.93249,-0.51738,-0.00024, 0.02646, 0.00074 },
8978
+    { 0.00054, 0.06745, 0.36471, 0.01668,-3.34703, 4.39475,
8979
+     -1.03543,-0.52184, 0.01085, 0.02973, 0.00152 },
8980
+    { 0.00195, 0.06912, 0.34515,-0.04445,-3.23676, 4.45185,
8981
+     -1.14105,-0.52410, 0.02280, 0.03306, 0.00228 },
8982
+    { 0.00318, 0.07017, 0.32521,-0.10168,-3.12422, 4.50339,
8983
+     -1.24914,-0.52400, 0.03561, 0.03643, 0.00302 },
8984
+    { 0.00422, 0.07065, 0.30503,-0.15503,-3.00969, 4.54921,
8985
+     -1.35952,-0.52141, 0.04925, 0.03981, 0.00373 },
8986
+    { 0.00508, 0.07061, 0.28471,-0.20450,-2.89348, 4.58919,
8987
+     -1.47197,-0.51618, 0.06370, 0.04319, 0.00440 },
8988
+    { 0.00577, 0.07007, 0.26436,-0.25013,-2.77587, 4.62321,
8989
+     -1.58627,-0.50818, 0.07895, 0.04652, 0.00501 },
8990
+    { 0.00629, 0.06909, 0.24410,-0.29194,-2.65716, 4.65118,
8991
+     -1.70219,-0.49727, 0.09494, 0.04979, 0.00556 },
8992
+    { 0.00666, 0.06769, 0.22400,-0.33000,-2.53764, 4.67302,
8993
+     -1.81949,-0.48335, 0.11166, 0.05295, 0.00604 },
8994
+    { 0.00687, 0.06592, 0.20416,-0.36435,-2.41760, 4.68866,
8995
+     -1.93791,-0.46627, 0.12904, 0.05597, 0.00642 },
8996
+    { 0.00694, 0.06383, 0.18468,-0.39506,-2.29732, 4.69806,
8997
+     -2.05720,-0.44593, 0.14705, 0.05881, 0.00671 },
8998
+    { 0.00689, 0.06144, 0.16561,-0.42223,-2.17710, 4.70120,
8999
+     -2.17710,-0.42223, 0.16561, 0.06144, 0.00689 },
9000
+    { 0.00671, 0.05881, 0.14705,-0.44593,-2.05720, 4.69806,
9001
+     -2.29732,-0.39506, 0.18468, 0.06383, 0.00694 },
9002
+    { 0.00642, 0.05597, 0.12904,-0.46627,-1.93791, 4.68865,
9003
+     -2.41759,-0.36435, 0.20416, 0.06592, 0.00687 },
9004
+    { 0.00604, 0.05295, 0.11166,-0.48334,-1.81949, 4.67301,
9005
+     -2.53763,-0.33000, 0.22400, 0.06769, 0.00666 },
9006
+    { 0.00556, 0.04979, 0.09494,-0.49727,-1.70219, 4.65117,
9007
+     -2.65715,-0.29194, 0.24409, 0.06909, 0.00629 },
9008
+    { 0.00501, 0.04652, 0.07894,-0.50818,-1.58627, 4.62321,
9009
+     -2.77587,-0.25013, 0.26436, 0.07007, 0.00577 },
9010
+    { 0.00440, 0.04319, 0.06370,-0.51618,-1.47197, 4.58919,
9011
+     -2.89348,-0.20450, 0.28471, 0.07061, 0.00508 },
9012
+    { 0.00373, 0.03981, 0.04925,-0.52141,-1.35952, 4.54921,
9013
+     -3.00970,-0.15503, 0.30503, 0.07065, 0.00422 },
9014
+    { 0.00302, 0.03643, 0.03561,-0.52400,-1.24915, 4.50339,
9015
+     -3.12422,-0.10168, 0.32521, 0.07017, 0.00318 },
9016
+    { 0.00228, 0.03306, 0.02280,-0.52410,-1.14105, 4.45186,
9017
+     -3.23677,-0.04445, 0.34515, 0.06912, 0.00195 },
9018
+    { 0.00152, 0.02973, 0.01085,-0.52184,-1.03544, 4.39477,
9019
+     -3.34704, 0.01668, 0.36471, 0.06745, 0.00054 },
9020
+    { 0.00074, 0.02646,-0.00024,-0.51738,-0.93249, 4.33226,
9021
+     -3.45476, 0.08168, 0.38378, 0.06515,-0.00105 },
9022
+    {-0.00003, 0.02328,-0.01047,-0.51085,-0.83239, 4.26452,
9023
+     -3.55963, 0.15053, 0.40224, 0.06216,-0.00284 },
9024
+    {-0.00080, 0.02021,-0.01983,-0.50241,-0.73529, 4.19174,
9025
+     -3.66138, 0.22319, 0.41993, 0.05847,-0.00481 },
9026
+    {-0.00156, 0.01726,-0.02834,-0.49221,-0.64135, 4.11413,
9027
+     -3.75974, 0.29961, 0.43674, 0.05403,-0.00696 },
9028
+    {-0.00229, 0.01445,-0.03599,-0.48040,-0.55069, 4.03188,
9029
+     -3.85445, 0.37972, 0.45251, 0.04882,-0.00929 },
9030
+};
9031
+
9032
+const float ff_dca_quant_amp[57] = {
9033
+    4.88281250E-04, 1.46484375E-03, 2.32267031E-03, 3.28475167E-03,
9034
+    4.64534014E-03, 6.56950334E-03, 9.29068029E-03, 1.31390067E-02,
9035
+    1.85813606E-02, 2.62780134E-02, 3.71627212E-02, 5.25560267E-02,
9036
+    7.43254423E-02, 1.05112053E-01, 1.48650885E-01, 2.10224107E-01,
9037
+    2.97301769E-01, 4.20448214E-01, 5.94603539E-01, 8.40896428E-01,
9038
+    1.18920708E+00, 1.68179286E+00, 2.37841415E+00, 3.36358571E+00,
9039
+    4.75682831E+00, 6.72717142E+00, 9.51365662E+00, 1.34543428E+01,
9040
+    1.90273132E+01, 2.69086857E+01, 3.80546265E+01, 5.38173714E+01,
9041
+    7.61092529E+01, 1.07634743E+02, 1.52218506E+02, 2.15269485E+02,
9042
+    3.04437012E+02, 4.30538971E+02, 6.08874023E+02, 8.61077942E+02,
9043
+    1.21774805E+03, 1.72215588E+03, 2.43549609E+03, 3.44431177E+03,
9044
+    4.87099219E+03, 6.88862354E+03, 9.74198438E+03, 1.37772471E+04,
9045
+    1.94839688E+04, 2.75544941E+04, 3.89679375E+04, 5.51089883E+04,
9046
+    7.79358750E+04, 1.10217977E+05, 1.55871750E+05, 2.20435953E+05,
9047
+    0.00000000E+00,
9048
+};
9049
+
9050
+const float ff_dca_st_coeff[34] = {
9051
+    2.69086857E+01, 2.69086857E+01, 1.34543419E+01, 6.72717142E+00,
9052
+    3.36358571E+00, 1.68179286E+00, 8.40896428E-01, 5.94603479E-01,
9053
+    4.20448214E-01, 2.97301799E-01, 2.10224107E-01, 1.48650900E-01,
9054
+    1.05112098E-01, 7.43253976E-02, 5.25560006E-02, 3.71626988E-02,
9055
+    3.12500000E-02, 2.62780003E-02, 1.85813997E-02, 1.31390002E-02,
9056
+    9.29069985E-03, 6.56950008E-03, 4.64530010E-03, 3.28480010E-03,
9057
+    2.32270011E-03, 1.64240005E-03, 1.16130000E-03, 5.80699998E-04,
9058
+    2.90299999E-04, 1.45200000E-04, 7.25999998E-05, 3.62999999E-05,
9059
+    1.82000003E-05, 0.00000000E+00,
9060
+};
9061
+
9062
+const float ff_dca_long_window[128] = {
9063
+    0.00000000E+00, 7.42882412E-06, 5.28020973E-05, 1.71007006E-04,
9064
+    3.96653224E-04, 7.63946096E-04, 1.30655791E-03, 2.05750111E-03,
9065
+    3.04900459E-03, 4.31239139E-03, 5.87796280E-03, 7.77488295E-03,
9066
+    1.00310687E-02, 1.26730874E-02, 1.57260559E-02, 1.92135461E-02,
9067
+    2.31574941E-02, 2.75781266E-02, 3.24938744E-02, 3.79213169E-02,
9068
+    4.38751020E-02, 5.03679104E-02, 5.74104004E-02, 6.50111660E-02,
9069
+    7.31767192E-02, 8.19114447E-02, 9.12176073E-02, 1.01095326E-01,
9070
+    1.11542597E-01, 1.22555278E-01, 1.34127125E-01, 1.46249816E-01,
9071
+    1.58912972E-01, 1.72104210E-01, 1.85809180E-01, 2.00011641E-01,
9072
+    2.14693516E-01, 2.29834959E-01, 2.45414421E-01, 2.61408776E-01,
9073
+    2.77793378E-01, 2.94542134E-01, 3.11627686E-01, 3.29021394E-01,
9074
+    3.46693635E-01, 3.64613682E-01, 3.82750064E-01, 4.01070446E-01,
9075
+    4.19541985E-01, 4.38131332E-01, 4.56804723E-01, 4.75528270E-01,
9076
+    4.94267941E-01, 5.12989700E-01, 5.31659782E-01, 5.50244689E-01,
9077
+    5.68711281E-01, 5.87027133E-01, 6.05160415E-01, 6.23080134E-01,
9078
+    6.40756190E-01, 6.58159554E-01, 6.75262392E-01, 6.92038059E-01,
9079
+    7.08461344E-01, 7.24508464E-01, 7.40157187E-01, 7.55386829E-01,
9080
+    7.70178556E-01, 7.84515142E-01, 7.98381269E-01, 8.11763465E-01,
9081
+    8.24650168E-01, 8.37031603E-01, 8.48900259E-01, 8.60250235E-01,
9082
+    8.71077836E-01, 8.81381273E-01, 8.91160548E-01, 9.00417745E-01,
9083
+    9.09156621E-01, 9.17382956E-01, 9.25104082E-01, 9.32328999E-01,
9084
+    9.39068437E-01, 9.45334494E-01, 9.51140642E-01, 9.56501782E-01,
9085
+    9.61433768E-01, 9.65953648E-01, 9.70079303E-01, 9.73829389E-01,
9086
+    9.77223217E-01, 9.80280578E-01, 9.83021557E-01, 9.85466540E-01,
9087
+    9.87635851E-01, 9.89549816E-01, 9.91228402E-01, 9.92691338E-01,
9088
+    9.93957877E-01, 9.95046616E-01, 9.95975435E-01, 9.96761382E-01,
9089
+    9.97420728E-01, 9.97968733E-01, 9.98419642E-01, 9.98786569E-01,
9090
+    9.99081731E-01, 9.99315977E-01, 9.99499321E-01, 9.99640644E-01,
9091
+    9.99747574E-01, 9.99826968E-01, 9.99884665E-01, 9.99925494E-01,
9092
+    9.99953628E-01, 9.99972343E-01, 9.99984324E-01, 9.99991655E-01,
9093
+    9.99995887E-01, 9.99998152E-01, 9.99999285E-01, 9.99999762E-01,
9094
+    9.99999940E-01, 1.00000000E+00, 1.00000000E+00, 1.00000000E+00,
9095
+};
9096
+
9097
+const float ff_dca_lfe_step_size_16[101] = {
9098
+    2.1362956633198035E-004, 2.4414807580797754E-004, 2.7466658528397473E-004,
9099
+    2.7466658528397473E-004, 3.0518509475997192E-004, 3.3570360423596911E-004,
9100
+    3.9674062318796350E-004, 4.2725913266396069E-004, 4.5777764213995788E-004,
9101
+    5.1881466109195227E-004, 5.7985168004394665E-004, 6.1037018951994385E-004,
9102
+    6.7140720847193823E-004, 7.6296273689992981E-004, 8.2399975585192419E-004,
9103
+    9.1555528427991577E-004, 1.0071108127079073E-003, 1.0986663411358989E-003,
9104
+    1.2207403790398877E-003, 1.3428144169438765E-003, 1.4648884548478652E-003,
9105
+    1.6174810022278512E-003, 1.7700735496078372E-003, 1.9531846064638203E-003,
9106
+    2.1362956633198035E-003, 2.3499252296517838E-003, 2.5940733054597613E-003,
9107
+    2.8687398907437361E-003, 3.1434064760277108E-003, 3.4485915707876827E-003,
9108
+    3.7842951750236518E-003, 4.1810357982116153E-003, 4.6082949308755760E-003,
9109
+    5.0660725730155339E-003, 5.5543687246314890E-003, 6.1037018951994385E-003,
9110
+    6.7445905941953795E-003, 7.4159978026673177E-003, 8.1484420300912512E-003,
9111
+    8.9419232764671782E-003, 9.8574785607470940E-003, 1.0834070863979004E-002,
9112
+    1.1932737205114903E-002, 1.3122959074678793E-002, 1.4435254982146673E-002,
9113
+    1.5869624927518540E-002, 1.7456587420270394E-002, 1.9196142460402233E-002,
9114
+    2.1118808557390057E-002, 2.3224585711233862E-002, 2.5543992431409649E-002,
9115
+    2.8107547227393413E-002, 3.0915250099185155E-002, 3.4028138065736867E-002,
9116
+    3.7415692617572556E-002, 4.1169469283120215E-002, 4.5258949552903834E-002,
9117
+    4.9806207464827418E-002, 5.4780724509414958E-002, 6.0274056215094456E-002,
9118
+    6.6286202581865905E-002, 7.2908719138157288E-002, 8.0202642902920618E-002,
9119
+    8.8229010895107887E-002, 9.7048860133671075E-002, 1.0675374614703818E-001,
9120
+    1.1743522446363720E-001, 1.2918485061189611E-001, 1.4209418012024294E-001,
9121
+    1.5628528702658162E-001, 1.7191076387829218E-001, 1.8912320322275461E-001,
9122
+    2.0804467909787286E-001, 2.2882778405102694E-001, 2.5171666615802485E-001,
9123
+    2.7689443647572254E-001, 3.0457472457045198E-001, 3.3503219702749720E-001,
9124
+    3.6854152043214211E-001, 4.0537736136967073E-001, 4.4593646046327096E-001,
9125
+    4.9052400280770286E-001, 5.3956724753563035E-001, 5.9352397228919340E-001,
9126
+    6.5288247322000792E-001, 7.1816156498916595E-001, 7.9000213629566329E-001,
9127
+    8.6898403881954400E-001, 9.5590075380718409E-001, 1.0514847254860074E+000,
9128
+    1.1566209906308176E+000, 1.2722861415448470E+000, 1.3995178075502792E+000,
9129
+    1.5394756920072024E+000, 1.6934110538041323E+000, 1.8627582628864405E+000,
9130
+    2.0490432447279274E+000, 2.2539445173497725E+000, 2.4793237098300120E+000,
9131
+    2.7272865993224893E+000, 3.0000000000000000E+000
9132
+};
9133
+
9134
+const float ff_dca_lfe_step_size_24[144] = {
9135
+    3.5762791128491298E-006, 3.9339070241340428E-006, 4.4107442391805934E-006,
9136
+    4.7683721504655064E-006, 5.2452093655120570E-006, 5.8412558843202453E-006,
9137
+    6.4373024031284336E-006, 7.0333489219366219E-006, 7.7486047445064479E-006,
9138
+    8.4638605670762738E-006, 9.4175349971693751E-006, 1.0252000123500839E-005,
9139
+    1.1324883857355578E-005, 1.2516976894971954E-005, 1.3709069932588331E-005,
9140
+    1.5139581577727983E-005, 1.6570093222867636E-005, 1.8239023475530564E-005,
9141
+    2.0146372335716766E-005, 2.2053721195902969E-005, 2.4318697967374082E-005,
9142
+    2.6702884042606836E-005, 2.9444698029124504E-005, 3.2305721319403807E-005,
9143
+    3.5643581824729662E-005, 3.9100651633817152E-005, 4.3034558657951193E-005,
9144
+    4.7326093593370149E-005, 5.2094465743835655E-005, 5.7339675109347712E-005,
9145
+    6.3061721689906320E-005, 6.9379814789273121E-005, 7.6293954407448102E-005,
9146
+    8.3923349848192912E-005, 9.2268001111507552E-005, 1.0156632680491529E-004,
9147
+    1.1169911762465449E-004, 1.2290479217824841E-004, 1.3518335046569711E-004,
9148
+    1.4865400179076216E-004, 1.6355516476096688E-004, 1.7988683937631122E-004,
9149
+    1.9788744424431852E-004, 2.1767618866875036E-004, 2.3949149125713007E-004,
9150
+    2.6345256131321922E-004, 2.8979781744454115E-004, 3.1876567825861912E-004,
9151
+    3.5059456236297636E-004, 3.8564209766889782E-004, 4.2426591208766842E-004,
9152
+    4.6670442422681142E-004, 5.1331526199761173E-004, 5.6469447191887759E-004,
9153
+    6.2108047259813216E-004, 6.8318851985794547E-004, 7.5149545091336386E-004,
9154
+    8.2671652158695713E-004, 9.0932856909377204E-004, 1.0002852678639017E-003,
9155
+    1.1003018737199156E-003, 1.2103320610919071E-003, 1.3314487137137310E-003,
9156
+    1.4646055060154803E-003, 1.6109945310347714E-003, 1.7721655097205054E-003,
9157
+    1.9493105351102991E-003, 2.1442177467605765E-003, 2.3586752842277626E-003,
9158
+    2.5945904963720436E-003, 2.8539899413573674E-003, 3.1393770145627278E-003,
9159
+    3.4533743206708813E-003, 3.7987236736683454E-003, 4.1785245154529228E-003,
9160
+    4.5963531251374630E-003, 5.0560242004423382E-003, 5.5617100669992049E-003,
9161
+    6.1178214690472445E-003, 6.7296036159519689E-003, 7.4025401356864135E-003,
9162
+    8.1428299120461841E-003, 8.9571486660419298E-003, 9.8527681652031147E-003,
9163
+    1.0838033060793050E-002, 1.1921884050593860E-002, 1.3114096297513997E-002,
9164
+    1.4425517848195773E-002, 1.5868069633015350E-002, 1.7454864675386508E-002,
9165
+    1.9200327301064409E-002, 2.1120431556753107E-002, 2.3232462791498040E-002,
9166
+    2.5555613703204836E-002, 2.8111222757246822E-002, 3.0922297349250002E-002,
9167
+    3.4014586688826884E-002, 3.7415985753057691E-002, 4.1157608170224208E-002,
9168
+    4.5273428591898514E-002, 4.9800759530157987E-002, 5.4780847404104160E-002,
9169
+    6.0258872539862694E-002, 6.6284783635709721E-002, 7.2913297762071824E-002,
9170
+    8.0204615617348624E-002, 8.8225017574431602E-002, 9.7047578936526643E-002,
9171
+    1.0675228914645780E-001, 1.1742748229831246E-001, 1.2917031397465634E-001,
9172
+    1.4208735729305236E-001, 1.5629603341770570E-001, 1.7192568444319778E-001,
9173
+    1.8911816944100493E-001, 2.0803001022696618E-001, 2.2883310661710579E-001,
9174
+    2.5171640535788598E-001, 2.7688804589367461E-001, 3.0457679087839018E-001,
9175
+    3.3503452957088109E-001, 3.6853794676517804E-001, 4.0539174144169587E-001,
9176
+    4.4593089174400469E-001, 4.9052399283933557E-001, 5.3957635636047796E-001,
9177
+    5.9353406352210802E-001, 6.5288742219059737E-001, 7.1817609288407480E-001,
9178
+    7.8999373793527339E-001, 8.6899314749159184E-001, 9.5589243839889027E-001,
9179
+    1.0514817299225008E+000, 1.1566298194682383E+000, 1.2722928848615747E+000,
9180
+    1.3995221137430804E+000, 1.5394743131964581E+000, 1.6934218041207556E+000,
9181
+    1.8627639845328312E+000, 2.0490403233814627E+000, 2.2539444272451910E+000,
9182
+    2.4793389414952922E+000, 2.7272728356448215E+000, 2.9999998807906962E+000
9183
+};
9184
+
9185
+const float ff_dca_bank_coeff[10] = {
9186
+    0.022810893, 0.41799772, 0.90844810, 0.99973983,
9187
+    0.068974845, 0.34675997, 0.29396889, 0.19642374,
9188
+    0.308658270, 0.038060233
9189
+};
9190
+
9191
+const float ff_dca_lfe_iir[5][4] = {
9192
+    { -0.98618466, 1.9861259, 1.0, -1.9840510 },
9193
+    { -0.98883152, 1.9887193, 1.0, -1.9979848 },
9194
+    { -0.99252087, 1.9923381, 1.0, -1.9990897 },
9195
+    { -0.99591690, 1.9956781, 1.0, -1.9993745 },
9196
+    { -0.99872285, 1.9984550, 1.0, -1.9994639 }
9197
+};
... ...
@@ -73,4 +73,51 @@ extern const int32_t ff_dca_xll_band_coeff[20];
73 73
 
74 74
 extern const int32_t ff_dca_sampling_freqs[16];
75 75
 
76
+extern const uint16_t ff_dca_avg_g3_freqs[3];
77
+
78
+extern const uint16_t ff_dca_fst_amp[44];
79
+
80
+extern const uint8_t ff_dca_freq_to_sb[32];
81
+
82
+extern const int8_t ff_dca_ph0_shift[8];
83
+
84
+extern const uint8_t ff_dca_grid_1_to_scf[11];
85
+extern const uint8_t ff_dca_grid_2_to_scf[3];
86
+
87
+extern const uint8_t ff_dca_scf_to_grid_1[32];
88
+extern const uint8_t ff_dca_scf_to_grid_2[32];
89
+
90
+extern const uint8_t ff_dca_grid_1_weights[12][32];
91
+
92
+extern const uint8_t ff_dca_sb_reorder[8][8];
93
+
94
+extern const int8_t ff_dca_lfe_delta_index_16[8];
95
+extern const int8_t ff_dca_lfe_delta_index_24[32];
96
+
97
+extern const uint16_t ff_dca_rsd_pack_5_in_8[256];
98
+extern const uint8_t ff_dca_rsd_pack_3_in_7[128][3];
99
+
100
+extern const float ff_dca_rsd_level_2a[2];
101
+extern const float ff_dca_rsd_level_2b[2];
102
+extern const float ff_dca_rsd_level_3[3];
103
+extern const float ff_dca_rsd_level_5[5];
104
+extern const float ff_dca_rsd_level_8[8];
105
+extern const float ff_dca_rsd_level_16[16];
106
+
107
+extern const float ff_dca_synth_env[32];
108
+
109
+extern const float ff_dca_corr_cf[32][11];
110
+
111
+extern const float ff_dca_quant_amp[57];
112
+
113
+extern const float ff_dca_st_coeff[34];
114
+
115
+extern const float ff_dca_long_window[128];
116
+
117
+extern const float ff_dca_lfe_step_size_16[101];
118
+extern const float ff_dca_lfe_step_size_24[144];
119
+
120
+extern const float ff_dca_bank_coeff[10];
121
+extern const float ff_dca_lfe_iir[5][4];
122
+
76 123
 #endif /* AVCODEC_DCADATA_H */
... ...
@@ -235,6 +235,16 @@ static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
235 235
             }
236 236
         }
237 237
 
238
+        // Parse LBR component in EXSS
239
+        if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {
240
+            if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {
241
+                if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
242
+                    return ret;
243
+            } else {
244
+                s->packet |= DCA_PACKET_LBR;
245
+            }
246
+        }
247
+
238 248
         // Parse core extensions in EXSS or backward compatible core sub-stream
239 249
         if ((s->packet & DCA_PACKET_CORE)
240 250
             && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
... ...
@@ -242,7 +252,10 @@ static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
242 242
     }
243 243
 
244 244
     // Filter the frame
245
-    if (s->packet & DCA_PACKET_XLL) {
245
+    if (s->packet & DCA_PACKET_LBR) {
246
+        if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)
247
+            return ret;
248
+    } else if (s->packet & DCA_PACKET_XLL) {
246 249
         if (s->packet & DCA_PACKET_CORE) {
247 250
             int x96_synth = -1;
248 251
 
... ...
@@ -297,6 +310,7 @@ static av_cold void dcadec_flush(AVCodecContext *avctx)
297 297
 
298 298
     ff_dca_core_flush(&s->core);
299 299
     ff_dca_xll_flush(&s->xll);
300
+    ff_dca_lbr_flush(&s->lbr);
300 301
 
301 302
     s->core_residual_valid = 0;
302 303
 }
... ...
@@ -307,6 +321,7 @@ static av_cold int dcadec_close(AVCodecContext *avctx)
307 307
 
308 308
     ff_dca_core_close(&s->core);
309 309
     ff_dca_xll_close(&s->xll);
310
+    ff_dca_lbr_close(&s->lbr);
310 311
 
311 312
     av_freep(&s->buffer);
312 313
     s->buffer_size = 0;
... ...
@@ -322,15 +337,20 @@ static av_cold int dcadec_init(AVCodecContext *avctx)
322 322
     s->core.avctx = avctx;
323 323
     s->exss.avctx = avctx;
324 324
     s->xll.avctx = avctx;
325
+    s->lbr.avctx = avctx;
325 326
 
326 327
     ff_dca_init_vlcs();
327 328
 
328 329
     if (ff_dca_core_init(&s->core) < 0)
329 330
         return AVERROR(ENOMEM);
330 331
 
332
+    if (ff_dca_lbr_init(&s->lbr) < 0)
333
+        return AVERROR(ENOMEM);
334
+
331 335
     ff_dcadsp_init(&s->dcadsp);
332 336
     s->core.dcadsp = &s->dcadsp;
333 337
     s->xll.dcadsp = &s->dcadsp;
338
+    s->lbr.dcadsp = &s->dcadsp;
334 339
 
335 340
     s->crctab = av_crc_get_table(AV_CRC_16_CCITT);
336 341
 
... ...
@@ -32,13 +32,15 @@
32 32
 #include "dca_core.h"
33 33
 #include "dca_exss.h"
34 34
 #include "dca_xll.h"
35
+#include "dca_lbr.h"
35 36
 
36 37
 #define DCA_BUFFER_PADDING_SIZE     1024
37 38
 
38 39
 #define DCA_PACKET_CORE         0x01
39 40
 #define DCA_PACKET_EXSS         0x02
40 41
 #define DCA_PACKET_XLL          0x04
41
-#define DCA_PACKET_RECOVERY     0x08
42
+#define DCA_PACKET_LBR          0x08
43
+#define DCA_PACKET_RECOVERY     0x10
42 44
 
43 45
 typedef struct DCAContext {
44 46
     const AVClass   *class;       ///< class for AVOptions
... ...
@@ -47,6 +49,7 @@ typedef struct DCAContext {
47 47
     DCACoreDecoder core;  ///< Core decoder context
48 48
     DCAExssParser  exss;  ///< EXSS parser context
49 49
     DCAXllDecoder  xll;   ///< XLL decoder context
50
+    DCALbrDecoder  lbr;   ///< LBR decoder context
50 51
 
51 52
     DCADSPContext   dcadsp;
52 53
 
... ...
@@ -385,6 +385,77 @@ static void assemble_freq_bands_c(int32_t *dst, int32_t *src0, int32_t *src1,
385 385
     }
386 386
 }
387 387
 
388
+static void lbr_bank_c(float output[32][4], float **input,
389
+                       const float *coeff, ptrdiff_t ofs, ptrdiff_t len)
390
+{
391
+    float SW0 = coeff[0];
392
+    float SW1 = coeff[1];
393
+    float SW2 = coeff[2];
394
+    float SW3 = coeff[3];
395
+
396
+    float C1  = coeff[4];
397
+    float C2  = coeff[5];
398
+    float C3  = coeff[6];
399
+    float C4  = coeff[7];
400
+
401
+    float AL1 = coeff[8];
402
+    float AL2 = coeff[9];
403
+
404
+    int i;
405
+
406
+    // Short window and 8 point forward MDCT
407
+    for (i = 0; i < len; i++) {
408
+        float *src = input[i] + ofs;
409
+
410
+        float a = src[-4] * SW0 - src[-1] * SW3;
411
+        float b = src[-3] * SW1 - src[-2] * SW2;
412
+        float c = src[ 2] * SW1 + src[ 1] * SW2;
413
+        float d = src[ 3] * SW0 + src[ 0] * SW3;
414
+
415
+        output[i][0] = C1 * b - C2 * c + C4 * a - C3 * d;
416
+        output[i][1] = C1 * d - C2 * a - C4 * b - C3 * c;
417
+        output[i][2] = C3 * b + C2 * d - C4 * c + C1 * a;
418
+        output[i][3] = C3 * a - C2 * b + C4 * d - C1 * c;
419
+    }
420
+
421
+    // Aliasing cancellation for high frequencies
422
+    for (i = 12; i < len - 1; i++) {
423
+        float a = output[i  ][3] * AL1;
424
+        float b = output[i+1][0] * AL1;
425
+        output[i  ][3] += b - a;
426
+        output[i+1][0] -= b + a;
427
+        a = output[i  ][2] * AL2;
428
+        b = output[i+1][1] * AL2;
429
+        output[i  ][2] += b - a;
430
+        output[i+1][1] -= b + a;
431
+    }
432
+}
433
+
434
+static void lfe_iir_c(float *output, const float *input,
435
+                      const float iir[5][4], float hist[5][2],
436
+                      ptrdiff_t factor)
437
+{
438
+    float res, tmp;
439
+    int i, j, k;
440
+
441
+    for (i = 0; i < 64; i++) {
442
+        res = *input++;
443
+
444
+        for (j = 0; j < factor; j++) {
445
+            for (k = 0; k < 5; k++) {
446
+                tmp = hist[k][0] * iir[k][0] + hist[k][1] * iir[k][1] + res;
447
+                res = hist[k][0] * iir[k][2] + hist[k][1] * iir[k][3] + tmp;
448
+
449
+                hist[k][0] = hist[k][1];
450
+                hist[k][1] = tmp;
451
+            }
452
+
453
+            *output++ = res;
454
+            res = 0;
455
+        }
456
+    }
457
+}
458
+
388 459
 av_cold void ff_dcadsp_init(DCADSPContext *s)
389 460
 {
390 461
     s->decode_hf     = decode_hf_c;
... ...
@@ -411,6 +482,9 @@ av_cold void ff_dcadsp_init(DCADSPContext *s)
411 411
 
412 412
     s->assemble_freq_bands = assemble_freq_bands_c;
413 413
 
414
+    s->lbr_bank = lbr_bank_c;
415
+    s->lfe_iir = lfe_iir_c;
416
+
414 417
     if (ARCH_X86)
415 418
         ff_dcadsp_init_x86(s);
416 419
 }
... ...
@@ -84,6 +84,13 @@ typedef struct DCADSPContext {
84 84
 
85 85
     void (*assemble_freq_bands)(int32_t *dst, int32_t *src0, int32_t *src1,
86 86
                                 const int32_t *coeff, ptrdiff_t len);
87
+
88
+    void (*lbr_bank)(float output[32][4], float **input,
89
+                     const float *coeff, ptrdiff_t ofs, ptrdiff_t len);
90
+
91
+    void (*lfe_iir)(float *output, const float *input,
92
+                    const float iir[5][4], float hist[5][2],
93
+                    ptrdiff_t factor);
87 94
 } DCADSPContext;
88 95
 
89 96
 av_cold void ff_dcadsp_init(DCADSPContext *s);
... ...
@@ -1038,13 +1038,209 @@ static const uint8_t *const bitalloc_bits[DCA_CODE_BOOKS][8] = {
1038 1038
       bitalloc_129_bits_e, bitalloc_129_bits_f, bitalloc_129_bits_g, NULL }
1039 1039
 };
1040 1040
 
1041
-static const uint16_t vlc_offs[63] = {
1041
+static const uint16_t tnl_grp_0_codes[37] = {
1042
+    0x0000, 0x0003, 0x0004, 0x0007, 0x0001, 0x0009, 0x000a, 0x000d,
1043
+    0x000e, 0x0006, 0x0012, 0x0005, 0x0015, 0x0016, 0x0022, 0x0025,
1044
+    0x0035, 0x0076, 0x0002, 0x0042, 0x00b6, 0x0036, 0x00c2, 0x0136,
1045
+    0x0182, 0x01c2, 0x03c2, 0x0482, 0x0682, 0x0082, 0x0882, 0x0a82,
1046
+    0x0282, 0x2282, 0x3282, 0x1282, 0x5282,
1047
+};
1048
+
1049
+static const uint16_t tnl_grp_1_codes[34] = {
1050
+    0x0001, 0x0003, 0x0006, 0x0000, 0x0002, 0x0004, 0x0005, 0x0007,
1051
+    0x0008, 0x000f, 0x001a, 0x001c, 0x001d, 0x000a, 0x002c, 0x002d,
1052
+    0x000d, 0x002a, 0x004c, 0x004d, 0x006a, 0x008c, 0x00cd, 0x00ea,
1053
+    0x000c, 0x010c, 0x01ea, 0x020c, 0x030c, 0x07ea, 0x0bea, 0x03ea,
1054
+    0x13ea, 0x33ea,
1055
+};
1056
+
1057
+static const uint16_t tnl_grp_2_codes[31] = {
1058
+    0x0001, 0x0003, 0x0006, 0x0007, 0x0004, 0x0008, 0x000c, 0x0010,
1059
+    0x0012, 0x001a, 0x0022, 0x0000, 0x000a, 0x0020, 0x0040, 0x004a,
1060
+    0x006a, 0x0002, 0x002a, 0x0042, 0x0082, 0x00aa, 0x00e0, 0x0060,
1061
+    0x00c2, 0x01c2, 0x0160, 0x0360, 0x0f60, 0x0760, 0x1760,
1062
+};
1063
+
1064
+static const uint16_t tnl_grp_3_codes[28] = {
1065
+    0x0001, 0x0006, 0x0008, 0x0014, 0x001c, 0x0000, 0x0002, 0x0004,
1066
+    0x000a, 0x000c, 0x0010, 0x0012, 0x001a, 0x0020, 0x002a, 0x002c,
1067
+    0x0032, 0x003a, 0x0022, 0x0030, 0x0062, 0x0064, 0x0070, 0x0024,
1068
+    0x00a4, 0x01a4, 0x03a4, 0x07a4,
1069
+};
1070
+
1071
+static const uint16_t tnl_grp_4_codes[23] = {
1072
+    0x0001, 0x0000, 0x000a, 0x0006, 0x0012, 0x001e, 0x0022, 0x002e,
1073
+    0x0036, 0x003e, 0x0002, 0x0016, 0x0032, 0x004e, 0x0056, 0x000e,
1074
+    0x0042, 0x0072, 0x00c2, 0x00f2, 0x008e, 0x018e, 0x038e,
1075
+};
1076
+
1077
+static const uint16_t tnl_scf_codes[20] = {
1078
+    0x0000, 0x0001, 0x0002, 0x0005, 0x0006, 0x0007, 0x000b, 0x000c,
1079
+    0x0013, 0x0014, 0x0003, 0x0004, 0x0023, 0x0064, 0x00a4, 0x0024,
1080
+    0x0124, 0x0324, 0x0724, 0x0f24,
1081
+};
1082
+
1083
+static const uint16_t damp_codes[7] = {
1084
+    0x0001, 0x0000, 0x0002, 0x0006, 0x000e, 0x001e, 0x003e,
1085
+};
1086
+
1087
+static const uint16_t dph_codes[9] = {
1088
+    0x0000, 0x0002, 0x0003, 0x0001, 0x0009, 0x000d, 0x0005, 0x0015,
1089
+    0x0035,
1090
+};
1091
+
1092
+static const uint16_t fst_rsd_amp_codes[24] = {
1093
+    0x0003, 0x0005, 0x0006, 0x0007, 0x0000, 0x0001, 0x0002, 0x0008,
1094
+    0x0009, 0x000a, 0x0014, 0x0004, 0x001a, 0x001c, 0x0024, 0x002c,
1095
+    0x003a, 0x000c, 0x003c, 0x004c, 0x00fc, 0x007c, 0x017c, 0x037c,
1096
+};
1097
+
1098
+static const uint16_t rsd_apprx_codes[6] = {
1099
+    0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f,
1100
+};
1101
+
1102
+static const uint16_t rsd_amp_codes[33] = {
1103
+    0x0001, 0x0000, 0x0002, 0x0003, 0x0004, 0x000e, 0x000f, 0x0016,
1104
+    0x0007, 0x0027, 0x0037, 0x0026, 0x0066, 0x0006, 0x0017, 0x0046,
1105
+    0x0097, 0x00d7, 0x0086, 0x00c6, 0x01c6, 0x0157, 0x0186, 0x0257,
1106
+    0x0357, 0x0057, 0x0786, 0x0386, 0x0b86, 0x0457, 0x0c57, 0x1457,
1107
+    0x1c57,
1108
+};
1109
+
1110
+static const uint16_t avg_g3_codes[18] = {
1111
+    0x0001, 0x0002, 0x0003, 0x0000, 0x000c, 0x0014, 0x0018, 0x0004,
1112
+    0x0008, 0x0028, 0x0068, 0x0024, 0x00a4, 0x00e4, 0x0164, 0x0064,
1113
+    0x0264, 0x0664,
1114
+};
1115
+
1116
+static const uint16_t st_grid_codes[22] = {
1117
+    0x0001, 0x0002, 0x0000, 0x0004, 0x0008, 0x001c, 0x004c, 0x006c,
1118
+    0x000c, 0x002c, 0x008c, 0x00ac, 0x012c, 0x018c, 0x01ac, 0x038c,
1119
+    0x03ac, 0x032c, 0x072c, 0x0f2c, 0x172c, 0x1f2c,
1120
+};
1121
+
1122
+static const uint16_t grid_2_codes[20] = {
1123
+    0x0000, 0x0002, 0x0003, 0x0001, 0x0005, 0x000d, 0x003d, 0x005d,
1124
+    0x009d, 0x011d, 0x001d, 0x061d, 0x041d, 0x0c1d, 0x0a1d, 0x121d,
1125
+    0x021d, 0x1a1d, 0x221d, 0x3a1d,
1126
+};
1127
+
1128
+static const uint16_t grid_3_codes[13] = {
1129
+    0x0001, 0x0002, 0x0000, 0x0004, 0x000c, 0x001c, 0x007c, 0x003c,
1130
+    0x01bc, 0x00bc, 0x06bc, 0x02bc, 0x0abc,
1131
+};
1132
+
1133
+static const uint16_t rsd_codes[9] = {
1134
+    0x0001, 0x0003, 0x0000, 0x0002, 0x0006, 0x0004, 0x000c, 0x001c,
1135
+    0x003c,
1136
+};
1137
+
1138
+static const uint8_t tnl_grp_0_bitvals[74] = {
1139
+     3,  5,  3,  9,  3,  4,  3,  6,  4, 10,  4, 13,  4,  7,  4, 11,
1140
+     4,  8,  5, 12,  5, 14,  6, 15,  6, 18,  6,  1,  6, 17,  6, 16,
1141
+     6, 21,  7, 20,  8, 19,  8, 22,  8, 25,  9, 26,  9, 23,  9,  3,
1142
+     9, 24, 10, 29, 10, 27, 11, 28, 11, 30, 12, 33, 12, 31, 12, 32,
1143
+    14, 34, 14, 37, 14, 36, 15, 35, 15,  0,
1144
+};
1145
+
1146
+static const uint8_t tnl_grp_1_bitvals[68] = {
1147
+     3,  9,  3,  6,  3,  5,  4,  4,  4,  8,  4, 10,  4,  1,  4, 11,
1148
+     4,  7,  4, 13,  5, 12,  5, 14,  5, 17,  6, 16,  6, 15,  6, 18,
1149
+     7, 20,  7, 19,  7, 21,  8, 25,  8, 23,  8, 22,  8, 24,  9, 26,
1150
+    10,  3, 10, 29, 10, 30, 10, 27, 10, 28, 11, 31, 12, 32, 13, 33,
1151
+    14, 34, 14,  0,
1152
+};
1153
+
1154
+static const uint8_t tnl_grp_2_bitvals[62] = {
1155
+     2,  1,  3,  6,  3,  5,  3,  7,  4,  9,  4,  8,  4,  4,  5, 10,
1156
+     5, 11,  5, 13,  6, 12,  7, 14,  7, 16,  7, 15,  7, 17,  7, 18,
1157
+     7, 19,  8, 22,  8, 20,  8, 21,  8,  3,  8, 24,  8, 25,  9, 23,
1158
+     9, 26,  9, 27, 10, 28, 11, 29, 12, 31, 13, 30, 13,  0,
1159
+};
1160
+
1161
+static const uint8_t tnl_grp_3_bitvals[56] = {
1162
+     1,  1,  3,  6,  4,  5,  5,  9,  5,  4,  6,  8,  6, 14,  6, 10,
1163
+     6, 21,  6, 13,  6,  7,  6,  3,  6, 16,  6,  2,  6, 18,  6, 17,
1164
+     6, 11,  6, 15,  7, 19,  7, 23,  7, 24,  7, 22,  7, 12,  8, 20,
1165
+     9, 25, 10, 26, 11, 27, 11,  0,
1166
+};
1167
+
1168
+static const uint8_t tnl_grp_4_bitvals[46] = {
1169
+     1,  1,  2,  2,  4,  4,  5,  5,  6,  6,  6,  8,  6,  3,  6, 19,
1170
+     6, 20,  6,  9,  7,  7,  7, 11,  7, 13,  7, 17,  7, 10,  8, 12,
1171
+     8, 15,  8, 14,  8, 21,  8, 18,  9, 16, 10, 22, 10,  0,
1172
+};
1173
+
1174
+static const uint8_t tnl_scf_bitvals[40] = {
1175
+     3,  3,  3,  1,  3,  2,  3,  5,  3,  4,  3,  6,  4,  8,  4,  7,
1176
+     5, 10,  5,  9,  6, 12,  6, 11,  6, 13,  7, 14,  8, 15,  9, 16,
1177
+    10, 17, 11, 18, 12, 19, 12,  0,
1178
+};
1179
+
1180
+static const uint8_t damp_bitvals[14] = {
1181
+     1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  6,  0,
1182
+};
1183
+
1184
+static const uint8_t dph_bitvals[18] = {
1185
+     2,  2,  2,  1,  2,  8,  4,  3,  4,  7,  4,  4,  5,  6,  6,  5,
1186
+     6,  0,
1187
+};
1188
+
1189
+static const uint8_t fst_rsd_amp_bitvals[48] = {
1190
+     3, 13,  3, 15,  3, 16,  3, 14,  4, 12,  4, 10,  4, 11,  4, 17,
1191
+     4, 18,  5, 19,  5,  9,  6,  1,  6,  7,  6,  6,  6,  8,  6,  5,
1192
+     6,  4,  7, 20,  7,  2,  7,  3,  8, 21,  9, 22, 10, 23, 10,  0,
1193
+};
1194
+
1195
+static const uint8_t rsd_apprx_bitvals[12] = {
1196
+     1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  5,  0,
1197
+};
1198
+
1199
+static const uint8_t rsd_amp_bitvals[66] = {
1200
+     2,  3,  3,  2,  3,  5,  3,  4,  3,  1,  4,  7,  4,  6,  5,  9,
1201
+     6,  8,  6, 11,  6, 10,  7, 12,  7, 13,  8, 14,  8, 18,  8, 16,
1202
+     8, 15,  8, 22,  9, 20,  9, 24,  9, 17, 10, 28, 10, 26, 10, 21,
1203
+    10, 23, 11, 30, 11, 19, 12, 25, 12, 32, 13, 36, 13, 29, 13, 34,
1204
+    13,  0,
1205
+};
1206
+
1207
+static const uint8_t avg_g3_bitvals[36] = {
1208
+     2, 15,  2, 16,  2, 17,  4, 14,  4, 18,  5, 12,  5, 13,  6, 10,
1209
+     6, 11,  7, 19,  7,  9,  8, 20,  8,  8,  8,  7,  9, 21, 10,  6,
1210
+    11, 23, 11,  0,
1211
+};
1212
+
1213
+static const uint8_t st_grid_bitvals[44] = {
1214
+     1,  6,  2,  1,  4,  4,  4,  8,  4,  3,  5, 10,  7, 12,  7,  5,
1215
+     8, 14,  9, 16,  9,  7,  9, 18, 10, 11, 10,  9, 10, 20, 10, 22,
1216
+    10,  2, 11, 13, 13, 17, 13, 24, 13, 15, 13,  0,
1217
+};
1218
+
1219
+static const uint8_t grid_2_bitvals[40] = {
1220
+     2,  3,  2,  2,  2,  1,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,
1221
+     8,  9,  9, 10, 11, 11, 11, 12, 12, 13, 12, 17, 13, 15, 13, 18,
1222
+    14, 19, 14, 16, 14, 14, 14,  0,
1223
+};
1224
+
1225
+static const uint8_t grid_3_bitvals[26] = {
1226
+     1, 17,  2, 16,  3, 18,  4, 15,  5, 19,  6, 14,  7, 20,  8, 13,
1227
+     9, 21, 10, 12, 11, 22, 12, 11, 12,  0,
1228
+};
1229
+
1230
+static const uint8_t rsd_bitvals[18] = {
1231
+     2,  2,  2,  3,  3,  1,  3,  4,  3,  0,  4,  5,  5,  6,  6,  7,
1232
+     6,  4,
1233
+};
1234
+
1235
+static const uint16_t vlc_offs[80] = {
1042 1236
         0,   512,   640,   768,  1282,  1794,  2436,  3080,  3770,  4454,  5364,
1043 1237
      5372,  5380,  5388,  5392,  5396,  5412,  5420,  5428,  5460,  5492,  5508,
1044 1238
      5572,  5604,  5668,  5796,  5860,  5892,  6412,  6668,  6796,  7308,  7564,
1045 1239
      7820,  8076,  8620,  9132,  9388,  9910, 10166, 10680, 11196, 11726, 12240,
1046 1240
     12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
1047
-    18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
1241
+    18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622, 24200, 24748, 25276,
1242
+    25792, 26306, 26826, 26890, 26954, 27468, 27500, 28038, 28554, 29086, 29630,
1243
+    30150, 30214
1048 1244
 };
1049 1245
 
1050 1246
 DCAVLC  ff_dca_vlc_bit_allocation;
... ...
@@ -1052,9 +1248,22 @@ DCAVLC  ff_dca_vlc_transition_mode;
1052 1052
 DCAVLC  ff_dca_vlc_scale_factor;
1053 1053
 DCAVLC  ff_dca_vlc_quant_index[DCA_CODE_BOOKS];
1054 1054
 
1055
+VLC     ff_dca_vlc_tnl_grp[5];
1056
+VLC     ff_dca_vlc_tnl_scf;
1057
+VLC     ff_dca_vlc_damp;
1058
+VLC     ff_dca_vlc_dph;
1059
+VLC     ff_dca_vlc_fst_rsd_amp;
1060
+VLC     ff_dca_vlc_rsd_apprx;
1061
+VLC     ff_dca_vlc_rsd_amp;
1062
+VLC     ff_dca_vlc_avg_g3;
1063
+VLC     ff_dca_vlc_st_grid;
1064
+VLC     ff_dca_vlc_grid_2;
1065
+VLC     ff_dca_vlc_grid_3;
1066
+VLC     ff_dca_vlc_rsd;
1067
+
1055 1068
 av_cold void ff_dca_init_vlcs(void)
1056 1069
 {
1057
-    static VLC_TYPE dca_table[23622][2];
1070
+    static VLC_TYPE dca_table[30214][2];
1058 1071
     static int vlcs_initialized = 0;
1059 1072
     int i, j, k = 0;
1060 1073
 
... ...
@@ -1095,5 +1304,34 @@ av_cold void ff_dca_init_vlcs(void)
1095 1095
                          bitalloc_sizes[i], bitalloc_bits[i][j], bitalloc_codes[i][j]);
1096 1096
     }
1097 1097
 
1098
+#define LBR_INIT_VLC(vlc, tab, nb_bits)                                 \
1099
+    do {                                                                \
1100
+        vlc.table           = &dca_table[vlc_offs[k]];                  \
1101
+        vlc.table_allocated = vlc_offs[k + 1] - vlc_offs[k];            \
1102
+        ff_init_vlc_sparse(&vlc, nb_bits, FF_ARRAY_ELEMS(tab##_codes),  \
1103
+                           &tab##_bitvals[0], 2, 1,                     \
1104
+                           tab##_codes, 2, 2,                           \
1105
+                           &tab##_bitvals[1], 2, 1,                     \
1106
+                           INIT_VLC_LE | INIT_VLC_USE_NEW_STATIC);      \
1107
+        k++;                                                            \
1108
+    } while (0)
1109
+
1110
+    LBR_INIT_VLC(ff_dca_vlc_tnl_grp[0],  tnl_grp_0,   9);
1111
+    LBR_INIT_VLC(ff_dca_vlc_tnl_grp[1],  tnl_grp_1,   9);
1112
+    LBR_INIT_VLC(ff_dca_vlc_tnl_grp[2],  tnl_grp_2,   9);
1113
+    LBR_INIT_VLC(ff_dca_vlc_tnl_grp[3],  tnl_grp_3,   9);
1114
+    LBR_INIT_VLC(ff_dca_vlc_tnl_grp[4],  tnl_grp_4,   9);
1115
+    LBR_INIT_VLC(ff_dca_vlc_tnl_scf,     tnl_scf,     9);
1116
+    LBR_INIT_VLC(ff_dca_vlc_damp,        damp,        6);
1117
+    LBR_INIT_VLC(ff_dca_vlc_dph,         dph,         6);
1118
+    LBR_INIT_VLC(ff_dca_vlc_fst_rsd_amp, fst_rsd_amp, 9);
1119
+    LBR_INIT_VLC(ff_dca_vlc_rsd_apprx,   rsd_apprx,   5);
1120
+    LBR_INIT_VLC(ff_dca_vlc_rsd_amp,     rsd_amp,     9);
1121
+    LBR_INIT_VLC(ff_dca_vlc_avg_g3,      avg_g3,      9);
1122
+    LBR_INIT_VLC(ff_dca_vlc_st_grid,     st_grid,     9);
1123
+    LBR_INIT_VLC(ff_dca_vlc_grid_2,      grid_2,      9);
1124
+    LBR_INIT_VLC(ff_dca_vlc_grid_3,      grid_3,      9);
1125
+    LBR_INIT_VLC(ff_dca_vlc_rsd,         rsd,         6);
1126
+
1098 1127
     vlcs_initialized = 1;
1099 1128
 }
... ...
@@ -41,6 +41,19 @@ extern DCAVLC   ff_dca_vlc_transition_mode;
41 41
 extern DCAVLC   ff_dca_vlc_scale_factor;
42 42
 extern DCAVLC   ff_dca_vlc_quant_index[DCA_CODE_BOOKS];
43 43
 
44
+extern VLC  ff_dca_vlc_tnl_grp[5];
45
+extern VLC  ff_dca_vlc_tnl_scf;
46
+extern VLC  ff_dca_vlc_damp;
47
+extern VLC  ff_dca_vlc_dph;
48
+extern VLC  ff_dca_vlc_fst_rsd_amp;
49
+extern VLC  ff_dca_vlc_rsd_apprx;
50
+extern VLC  ff_dca_vlc_rsd_amp;
51
+extern VLC  ff_dca_vlc_avg_g3;
52
+extern VLC  ff_dca_vlc_st_grid;
53
+extern VLC  ff_dca_vlc_grid_2;
54
+extern VLC  ff_dca_vlc_grid_3;
55
+extern VLC  ff_dca_vlc_rsd;
56
+
44 57
 av_cold void ff_dca_init_vlcs(void);
45 58
 
46 59
 #endif /* AVCODEC_DCAHUFF_H */