FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_lbr.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 17 1010 1.7%
Branches: 7 690 1.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2016 foo86
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #define BITSTREAM_READER_LE
22
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/mem_internal.h"
25
26 #include "dcadec.h"
27 #include "dcadata.h"
28 #include "dcahuff.h"
29 #include "dca_syncwords.h"
30 #include "bytestream.h"
31 #include "internal.h"
32
33 #define AMP_MAX 56
34
35 enum LBRFlags {
36 LBR_FLAG_24_BIT = 0x01,
37 LBR_FLAG_LFE_PRESENT = 0x02,
38 LBR_FLAG_BAND_LIMIT_2_3 = 0x04,
39 LBR_FLAG_BAND_LIMIT_1_2 = 0x08,
40 LBR_FLAG_BAND_LIMIT_1_3 = 0x0c,
41 LBR_FLAG_BAND_LIMIT_1_4 = 0x10,
42 LBR_FLAG_BAND_LIMIT_1_8 = 0x18,
43 LBR_FLAG_BAND_LIMIT_NONE = 0x14,
44 LBR_FLAG_BAND_LIMIT_MASK = 0x1c,
45 LBR_FLAG_DMIX_STEREO = 0x20,
46 LBR_FLAG_DMIX_MULTI_CH = 0x40
47 };
48
49 enum LBRChunkTypes {
50 LBR_CHUNK_NULL = 0x00,
51 LBR_CHUNK_PAD = 0x01,
52 LBR_CHUNK_FRAME = 0x04,
53 LBR_CHUNK_FRAME_NO_CSUM = 0x06,
54 LBR_CHUNK_LFE = 0x0a,
55 LBR_CHUNK_ECS = 0x0b,
56 LBR_CHUNK_RESERVED_1 = 0x0c,
57 LBR_CHUNK_RESERVED_2 = 0x0d,
58 LBR_CHUNK_SCF = 0x0e,
59 LBR_CHUNK_TONAL = 0x10,
60 LBR_CHUNK_TONAL_GRP_1 = 0x11,
61 LBR_CHUNK_TONAL_GRP_2 = 0x12,
62 LBR_CHUNK_TONAL_GRP_3 = 0x13,
63 LBR_CHUNK_TONAL_GRP_4 = 0x14,
64 LBR_CHUNK_TONAL_GRP_5 = 0x15,
65 LBR_CHUNK_TONAL_SCF = 0x16,
66 LBR_CHUNK_TONAL_SCF_GRP_1 = 0x17,
67 LBR_CHUNK_TONAL_SCF_GRP_2 = 0x18,
68 LBR_CHUNK_TONAL_SCF_GRP_3 = 0x19,
69 LBR_CHUNK_TONAL_SCF_GRP_4 = 0x1a,
70 LBR_CHUNK_TONAL_SCF_GRP_5 = 0x1b,
71 LBR_CHUNK_RES_GRID_LR = 0x30,
72 LBR_CHUNK_RES_GRID_LR_LAST = 0x3f,
73 LBR_CHUNK_RES_GRID_HR = 0x40,
74 LBR_CHUNK_RES_GRID_HR_LAST = 0x4f,
75 LBR_CHUNK_RES_TS_1 = 0x50,
76 LBR_CHUNK_RES_TS_1_LAST = 0x5f,
77 LBR_CHUNK_RES_TS_2 = 0x60,
78 LBR_CHUNK_RES_TS_2_LAST = 0x6f,
79 LBR_CHUNK_EXTENSION = 0x7f
80 };
81
82 typedef struct LBRChunk {
83 int id, len;
84 const uint8_t *data;
85 } LBRChunk;
86
87 static const int8_t channel_reorder_nolfe[7][5] = {
88 { 0, -1, -1, -1, -1 }, // C
89 { 0, 1, -1, -1, -1 }, // LR
90 { 0, 1, 2, -1, -1 }, // LR C
91 { 0, 1, -1, -1, -1 }, // LsRs
92 { 1, 2, 0, -1, -1 }, // LsRs C
93 { 0, 1, 2, 3, -1 }, // LR LsRs
94 { 0, 1, 3, 4, 2 }, // LR LsRs C
95 };
96
97 static const int8_t channel_reorder_lfe[7][5] = {
98 { 0, -1, -1, -1, -1 }, // C
99 { 0, 1, -1, -1, -1 }, // LR
100 { 0, 1, 2, -1, -1 }, // LR C
101 { 1, 2, -1, -1, -1 }, // LsRs
102 { 2, 3, 0, -1, -1 }, // LsRs C
103 { 0, 1, 3, 4, -1 }, // LR LsRs
104 { 0, 1, 4, 5, 2 }, // LR LsRs C
105 };
106
107 static const uint8_t lfe_index[7] = {
108 1, 2, 3, 0, 1, 2, 3
109 };
110
111 static const uint16_t channel_layouts[7] = {
112 AV_CH_LAYOUT_MONO,
113 AV_CH_LAYOUT_STEREO,
114 AV_CH_LAYOUT_SURROUND,
115 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
116 AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
117 AV_CH_LAYOUT_2_2,
118 AV_CH_LAYOUT_5POINT0
119 };
120
121 static float cos_tab[256];
122 static float lpc_tab[16];
123
124 47 av_cold void ff_dca_lbr_init_tables(void)
125 {
126 int i;
127
128
2/2
✓ Branch 0 taken 12032 times.
✓ Branch 1 taken 47 times.
12079 for (i = 0; i < 256; i++)
129 12032 cos_tab[i] = cos(M_PI * i / 128);
130
131
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 47 times.
799 for (i = 0; i < 16; i++)
132
2/2
✓ Branch 0 taken 376 times.
✓ Branch 1 taken 376 times.
752 lpc_tab[i] = sin((i - 8) * (M_PI / ((i < 8) ? 17 : 15)));
133 47 }
134
135 static int parse_lfe_24(DCALbrDecoder *s)
136 {
137 int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_24) - 1;
138 int i, ps, si, code, step_i;
139 float step, value, delta;
140
141 ps = get_bits(&s->gb, 24);
142 si = ps >> 23;
143
144 value = (((ps & 0x7fffff) ^ -si) + si) * (1.0f / 0x7fffff);
145
146 step_i = get_bits(&s->gb, 8);
147 if (step_i > step_max) {
148 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
149 return AVERROR_INVALIDDATA;
150 }
151
152 step = ff_dca_lfe_step_size_24[step_i];
153
154 for (i = 0; i < 64; i++) {
155 code = get_bits(&s->gb, 6);
156
157 delta = step * 0.03125f;
158 if (code & 16)
159 delta += step;
160 if (code & 8)
161 delta += step * 0.5f;
162 if (code & 4)
163 delta += step * 0.25f;
164 if (code & 2)
165 delta += step * 0.125f;
166 if (code & 1)
167 delta += step * 0.0625f;
168
169 if (code & 32) {
170 value -= delta;
171 if (value < -3.0f)
172 value = -3.0f;
173 } else {
174 value += delta;
175 if (value > 3.0f)
176 value = 3.0f;
177 }
178
179 step_i += ff_dca_lfe_delta_index_24[code & 31];
180 step_i = av_clip(step_i, 0, step_max);
181
182 step = ff_dca_lfe_step_size_24[step_i];
183 s->lfe_data[i] = value * s->lfe_scale;
184 }
185
186 return 0;
187 }
188
189 static int parse_lfe_16(DCALbrDecoder *s)
190 {
191 int step_max = FF_ARRAY_ELEMS(ff_dca_lfe_step_size_16) - 1;
192 int i, ps, si, code, step_i;
193 float step, value, delta;
194
195 ps = get_bits(&s->gb, 16);
196 si = ps >> 15;
197
198 value = (((ps & 0x7fff) ^ -si) + si) * (1.0f / 0x7fff);
199
200 step_i = get_bits(&s->gb, 8);
201 if (step_i > step_max) {
202 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE step size index\n");
203 return AVERROR_INVALIDDATA;
204 }
205
206 step = ff_dca_lfe_step_size_16[step_i];
207
208 for (i = 0; i < 64; i++) {
209 code = get_bits(&s->gb, 4);
210
211 delta = step * 0.125f;
212 if (code & 4)
213 delta += step;
214 if (code & 2)
215 delta += step * 0.5f;
216 if (code & 1)
217 delta += step * 0.25f;
218
219 if (code & 8) {
220 value -= delta;
221 if (value < -3.0f)
222 value = -3.0f;
223 } else {
224 value += delta;
225 if (value > 3.0f)
226 value = 3.0f;
227 }
228
229 step_i += ff_dca_lfe_delta_index_16[code & 7];
230 step_i = av_clip(step_i, 0, step_max);
231
232 step = ff_dca_lfe_step_size_16[step_i];
233 s->lfe_data[i] = value * s->lfe_scale;
234 }
235
236 return 0;
237 }
238
239 static int parse_lfe_chunk(DCALbrDecoder *s, LBRChunk *chunk)
240 {
241 int ret;
242
243 if (!(s->flags & LBR_FLAG_LFE_PRESENT))
244 return 0;
245
246 if (!chunk->len)
247 return 0;
248
249 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
250 if (ret < 0)
251 return ret;
252
253 // Determine bit depth from chunk size
254 if (chunk->len >= 52)
255 return parse_lfe_24(s);
256 if (chunk->len >= 35)
257 return parse_lfe_16(s);
258
259 av_log(s->avctx, AV_LOG_ERROR, "LFE chunk too short\n");
260 return AVERROR_INVALIDDATA;
261 }
262
263 static inline int parse_vlc(GetBitContext *s, VLC *vlc, int max_depth)
264 {
265 int v = get_vlc2(s, vlc->table, vlc->bits, max_depth);
266 if (v > 0)
267 return v - 1;
268 // Rare value
269 return get_bits(s, get_bits(s, 3) + 1);
270 }
271
272 static int parse_tonal(DCALbrDecoder *s, int group)
273 {
274 unsigned int amp[DCA_LBR_CHANNELS_TOTAL];
275 unsigned int phs[DCA_LBR_CHANNELS_TOTAL];
276 unsigned int diff, main_amp, shift;
277 int sf, sf_idx, ch, main_ch, freq;
278 int ch_nbits = av_ceil_log2(s->nchannels_total);
279
280 // Parse subframes for this group
281 for (sf = 0; sf < 1 << group; sf += diff ? 8 : 1) {
282 sf_idx = ((s->framenum << group) + sf) & 31;
283 s->tonal_bounds[group][sf_idx][0] = s->ntones;
284
285 // Parse tones for this subframe
286 for (freq = 1;; freq++) {
287 if (get_bits_left(&s->gb) < 1) {
288 av_log(s->avctx, AV_LOG_ERROR, "Tonal group chunk too short\n");
289 return AVERROR_INVALIDDATA;
290 }
291
292 diff = parse_vlc(&s->gb, &ff_dca_vlc_tnl_grp[group], 2);
293 if (diff >= FF_ARRAY_ELEMS(ff_dca_fst_amp)) {
294 av_log(s->avctx, AV_LOG_ERROR, "Invalid tonal frequency diff\n");
295 return AVERROR_INVALIDDATA;
296 }
297
298 diff = get_bitsz(&s->gb, diff >> 2) + ff_dca_fst_amp[diff];
299 if (diff <= 1)
300 break; // End of subframe
301
302 freq += diff - 2;
303 if (freq >> (5 - group) > s->nsubbands * 4 - 6) {
304 av_log(s->avctx, AV_LOG_ERROR, "Invalid spectral line offset\n");
305 return AVERROR_INVALIDDATA;
306 }
307
308 // Main channel
309 main_ch = get_bitsz(&s->gb, ch_nbits);
310 main_amp = parse_vlc(&s->gb, &ff_dca_vlc_tnl_scf, 2)
311 + s->tonal_scf[ff_dca_freq_to_sb[freq >> (7 - group)]]
312 + s->limited_range - 2;
313 amp[main_ch] = main_amp < AMP_MAX ? main_amp : 0;
314 phs[main_ch] = get_bits(&s->gb, 3);
315
316 // Secondary channels
317 for (ch = 0; ch < s->nchannels_total; ch++) {
318 if (ch == main_ch)
319 continue;
320 if (get_bits1(&s->gb)) {
321 amp[ch] = amp[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_damp, 1);
322 phs[ch] = phs[main_ch] - parse_vlc(&s->gb, &ff_dca_vlc_dph, 1);
323 } else {
324 amp[ch] = 0;
325 phs[ch] = 0;
326 }
327 }
328
329 if (amp[main_ch]) {
330 // Allocate new tone
331 DCALbrTone *t = &s->tones[s->ntones];
332 s->ntones = (s->ntones + 1) & (DCA_LBR_TONES - 1);
333
334 t->x_freq = freq >> (5 - group);
335 t->f_delt = (freq & ((1 << (5 - group)) - 1)) << group;
336 t->ph_rot = 256 - (t->x_freq & 1) * 128 - t->f_delt * 4;
337
338 shift = ff_dca_ph0_shift[(t->x_freq & 3) * 2 + (freq & 1)]
339 - ((t->ph_rot << (5 - group)) - t->ph_rot);
340
341 for (ch = 0; ch < s->nchannels; ch++) {
342 t->amp[ch] = amp[ch] < AMP_MAX ? amp[ch] : 0;
343 t->phs[ch] = 128 - phs[ch] * 32 + shift;
344 }
345 }
346 }
347
348 s->tonal_bounds[group][sf_idx][1] = s->ntones;
349 }
350
351 return 0;
352 }
353
354 static int parse_tonal_chunk(DCALbrDecoder *s, LBRChunk *chunk)
355 {
356 int sb, group, ret;
357
358 if (!chunk->len)
359 return 0;
360
361 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
362
363 if (ret < 0)
364 return ret;
365
366 // Scale factors
367 if (chunk->id == LBR_CHUNK_SCF || chunk->id == LBR_CHUNK_TONAL_SCF) {
368 if (get_bits_left(&s->gb) < 36) {
369 av_log(s->avctx, AV_LOG_ERROR, "Tonal scale factor chunk too short\n");
370 return AVERROR_INVALIDDATA;
371 }
372 for (sb = 0; sb < 6; sb++)
373 s->tonal_scf[sb] = get_bits(&s->gb, 6);
374 }
375
376 // Tonal groups
377 if (chunk->id == LBR_CHUNK_TONAL || chunk->id == LBR_CHUNK_TONAL_SCF)
378 for (group = 0; group < 5; group++) {
379 ret = parse_tonal(s, group);
380 if (ret < 0)
381 return ret;
382 }
383
384 return 0;
385 }
386
387 static int parse_tonal_group(DCALbrDecoder *s, LBRChunk *chunk)
388 {
389 int ret;
390
391 if (!chunk->len)
392 return 0;
393
394 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
395 if (ret < 0)
396 return ret;
397
398 return parse_tonal(s, chunk->id);
399 }
400
401 /**
402 * Check point to ensure that enough bits are left. Aborts decoding
403 * by skipping to the end of chunk otherwise.
404 */
405 static int ensure_bits(GetBitContext *s, int n)
406 {
407 int left = get_bits_left(s);
408 if (left < 0)
409 return AVERROR_INVALIDDATA;
410 if (left < n) {
411 skip_bits_long(s, left);
412 return 1;
413 }
414 return 0;
415 }
416
417 static int parse_scale_factors(DCALbrDecoder *s, uint8_t *scf)
418 {
419 int i, sf, prev, next, dist;
420
421 // Truncated scale factors remain zero
422 if (ensure_bits(&s->gb, 20))
423 return 0;
424
425 // Initial scale factor
426 prev = parse_vlc(&s->gb, &ff_dca_vlc_fst_rsd_amp, 2);
427
428 for (sf = 0; sf < 7; sf += dist) {
429 scf[sf] = prev; // Store previous value
430
431 if (ensure_bits(&s->gb, 20))
432 return 0;
433
434 // Interpolation distance
435 dist = parse_vlc(&s->gb, &ff_dca_vlc_rsd_apprx, 1) + 1;
436 if (dist > 7 - sf) {
437 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor distance\n");
438 return AVERROR_INVALIDDATA;
439 }
440
441 if (ensure_bits(&s->gb, 20))
442 return 0;
443
444 // Final interpolation point
445 next = parse_vlc(&s->gb, &ff_dca_vlc_rsd_amp, 2);
446
447 if (next & 1)
448 next = prev + ((next + 1) >> 1);
449 else
450 next = prev - ( next >> 1);
451
452 // Interpolate
453 switch (dist) {
454 case 2:
455 if (next > prev)
456 scf[sf + 1] = prev + ((next - prev) >> 1);
457 else
458 scf[sf + 1] = prev - ((prev - next) >> 1);
459 break;
460
461 case 4:
462 if (next > prev) {
463 scf[sf + 1] = prev + ( (next - prev) >> 2);
464 scf[sf + 2] = prev + ( (next - prev) >> 1);
465 scf[sf + 3] = prev + (((next - prev) * 3) >> 2);
466 } else {
467 scf[sf + 1] = prev - ( (prev - next) >> 2);
468 scf[sf + 2] = prev - ( (prev - next) >> 1);
469 scf[sf + 3] = prev - (((prev - next) * 3) >> 2);
470 }
471 break;
472
473 default:
474 for (i = 1; i < dist; i++)
475 scf[sf + i] = prev + (next - prev) * i / dist;
476 break;
477 }
478
479 prev = next;
480 }
481
482 scf[sf] = next; // Store final value
483
484 return 0;
485 }
486
487 static int parse_st_code(GetBitContext *s, int min_v)
488 {
489 unsigned int v = parse_vlc(s, &ff_dca_vlc_st_grid, 2) + min_v;
490
491 if (v & 1)
492 v = 16 + (v >> 1);
493 else
494 v = 16 - (v >> 1);
495
496 if (v >= FF_ARRAY_ELEMS(ff_dca_st_coeff))
497 v = 16;
498 return v;
499 }
500
501 static int parse_grid_1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
502 {
503 int ch, sb, sf, nsubbands, ret;
504
505 if (!chunk->len)
506 return 0;
507
508 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
509 if (ret < 0)
510 return ret;
511
512 // Scale factors
513 nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
514 for (sb = 2; sb < nsubbands; sb++) {
515 ret = parse_scale_factors(s, s->grid_1_scf[ch1][sb]);
516 if (ret < 0)
517 return ret;
518 if (ch1 != ch2 && ff_dca_grid_1_to_scf[sb] < s->min_mono_subband) {
519 ret = parse_scale_factors(s, s->grid_1_scf[ch2][sb]);
520 if (ret < 0)
521 return ret;
522 }
523 }
524
525 if (get_bits_left(&s->gb) < 1)
526 return 0; // Should not happen, but a sample exists that proves otherwise
527
528 // Average values for third grid
529 for (sb = 0; sb < s->nsubbands - 4; sb++) {
530 s->grid_3_avg[ch1][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
531 if (ch1 != ch2) {
532 if (sb + 4 < s->min_mono_subband)
533 s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
534 else
535 s->grid_3_avg[ch2][sb] = s->grid_3_avg[ch1][sb];
536 }
537 }
538
539 if (get_bits_left(&s->gb) < 0) {
540 av_log(s->avctx, AV_LOG_ERROR, "First grid chunk too short\n");
541 return AVERROR_INVALIDDATA;
542 }
543
544 // Stereo image for partial mono mode
545 if (ch1 != ch2) {
546 int min_v[2];
547
548 if (ensure_bits(&s->gb, 8))
549 return 0;
550
551 min_v[0] = get_bits(&s->gb, 4);
552 min_v[1] = get_bits(&s->gb, 4);
553
554 nsubbands = (s->nsubbands - s->min_mono_subband + 3) / 4;
555 for (sb = 0; sb < nsubbands; sb++)
556 for (ch = ch1; ch <= ch2; ch++)
557 for (sf = 1; sf <= 4; sf++)
558 s->part_stereo[ch][sb][sf] = parse_st_code(&s->gb, min_v[ch - ch1]);
559
560 if (get_bits_left(&s->gb) >= 0)
561 s->part_stereo_pres |= 1 << ch1;
562 }
563
564 // Low resolution spatial information is not decoded
565
566 return 0;
567 }
568
569 static int parse_grid_1_sec_ch(DCALbrDecoder *s, int ch2)
570 {
571 int sb, nsubbands, ret;
572
573 // Scale factors
574 nsubbands = ff_dca_scf_to_grid_1[s->nsubbands - 1] + 1;
575 for (sb = 2; sb < nsubbands; sb++) {
576 if (ff_dca_grid_1_to_scf[sb] >= s->min_mono_subband) {
577 ret = parse_scale_factors(s, s->grid_1_scf[ch2][sb]);
578 if (ret < 0)
579 return ret;
580 }
581 }
582
583 // Average values for third grid
584 for (sb = 0; sb < s->nsubbands - 4; sb++) {
585 if (sb + 4 >= s->min_mono_subband) {
586 if (ensure_bits(&s->gb, 20))
587 return 0;
588 s->grid_3_avg[ch2][sb] = parse_vlc(&s->gb, &ff_dca_vlc_avg_g3, 2) - 16;
589 }
590 }
591
592 return 0;
593 }
594
595 static void parse_grid_3(DCALbrDecoder *s, int ch1, int ch2, int sb, int flag)
596 {
597 int i, ch;
598
599 for (ch = ch1; ch <= ch2; ch++) {
600 if ((ch != ch1 && sb + 4 >= s->min_mono_subband) != flag)
601 continue;
602
603 if (s->grid_3_pres[ch] & (1U << sb))
604 continue; // Already parsed
605
606 for (i = 0; i < 8; i++) {
607 if (ensure_bits(&s->gb, 20))
608 return;
609 s->grid_3_scf[ch][sb][i] = parse_vlc(&s->gb, &ff_dca_vlc_grid_3, 2) - 16;
610 }
611
612 // Flag scale factors for this subband parsed
613 s->grid_3_pres[ch] |= 1U << sb;
614 }
615 }
616
617 static float lbr_rand(DCALbrDecoder *s, int sb)
618 {
619 s->lbr_rand = 1103515245U * s->lbr_rand + 12345U;
620 return s->lbr_rand * s->sb_scf[sb];
621 }
622
623 /**
624 * Parse time samples for one subband, filling truncated samples with randomness
625 */
626 static void parse_ch(DCALbrDecoder *s, int ch, int sb, int quant_level, int flag)
627 {
628 float *samples = s->time_samples[ch][sb];
629 int i, j, code, nblocks, coding_method;
630
631 if (ensure_bits(&s->gb, 20))
632 return; // Too few bits left
633
634 coding_method = get_bits1(&s->gb);
635
636 switch (quant_level) {
637 case 1:
638 nblocks = FFMIN(get_bits_left(&s->gb) / 8, DCA_LBR_TIME_SAMPLES / 8);
639 for (i = 0; i < nblocks; i++, samples += 8) {
640 code = get_bits(&s->gb, 8);
641 for (j = 0; j < 8; j++)
642 samples[j] = ff_dca_rsd_level_2a[(code >> j) & 1];
643 }
644 i = nblocks * 8;
645 break;
646
647 case 2:
648 if (coding_method) {
649 for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 2; i++) {
650 if (get_bits1(&s->gb))
651 samples[i] = ff_dca_rsd_level_2b[get_bits1(&s->gb)];
652 else
653 samples[i] = 0;
654 }
655 } else {
656 nblocks = FFMIN(get_bits_left(&s->gb) / 8, (DCA_LBR_TIME_SAMPLES + 4) / 5);
657 for (i = 0; i < nblocks; i++, samples += 5) {
658 code = ff_dca_rsd_pack_5_in_8[get_bits(&s->gb, 8)];
659 for (j = 0; j < 5; j++)
660 samples[j] = ff_dca_rsd_level_3[(code >> j * 2) & 3];
661 }
662 i = nblocks * 5;
663 }
664 break;
665
666 case 3:
667 nblocks = FFMIN(get_bits_left(&s->gb) / 7, (DCA_LBR_TIME_SAMPLES + 2) / 3);
668 for (i = 0; i < nblocks; i++, samples += 3) {
669 code = get_bits(&s->gb, 7);
670 for (j = 0; j < 3; j++)
671 samples[j] = ff_dca_rsd_level_5[ff_dca_rsd_pack_3_in_7[code][j]];
672 }
673 i = nblocks * 3;
674 break;
675
676 case 4:
677 for (i = 0; i < DCA_LBR_TIME_SAMPLES && get_bits_left(&s->gb) >= 6; i++)
678 samples[i] = ff_dca_rsd_level_8[get_vlc2(&s->gb, ff_dca_vlc_rsd.table, 6, 1)];
679 break;
680
681 case 5:
682 nblocks = FFMIN(get_bits_left(&s->gb) / 4, DCA_LBR_TIME_SAMPLES);
683 for (i = 0; i < nblocks; i++)
684 samples[i] = ff_dca_rsd_level_16[get_bits(&s->gb, 4)];
685 break;
686
687 default:
688 av_assert0(0);
689 }
690
691 if (flag && get_bits_left(&s->gb) < 20)
692 return; // Skip incomplete mono subband
693
694 for (; i < DCA_LBR_TIME_SAMPLES; i++)
695 s->time_samples[ch][sb][i] = lbr_rand(s, sb);
696
697 s->ch_pres[ch] |= 1U << sb;
698 }
699
700 static int parse_ts(DCALbrDecoder *s, int ch1, int ch2,
701 int start_sb, int end_sb, int flag)
702 {
703 int sb, sb_g3, sb_reorder, quant_level;
704
705 for (sb = start_sb; sb < end_sb; sb++) {
706 // Subband number before reordering
707 if (sb < 6) {
708 sb_reorder = sb;
709 } else if (flag && sb < s->max_mono_subband) {
710 sb_reorder = s->sb_indices[sb];
711 } else {
712 if (ensure_bits(&s->gb, 28))
713 break;
714 sb_reorder = get_bits(&s->gb, s->limited_range + 3);
715 if (sb_reorder < 6)
716 sb_reorder = 6;
717 s->sb_indices[sb] = sb_reorder;
718 }
719 if (sb_reorder >= s->nsubbands)
720 return AVERROR_INVALIDDATA;
721
722 // Third grid scale factors
723 if (sb == 12) {
724 for (sb_g3 = 0; sb_g3 < s->g3_avg_only_start_sb - 4; sb_g3++)
725 parse_grid_3(s, ch1, ch2, sb_g3, flag);
726 } else if (sb < 12 && sb_reorder >= 4) {
727 parse_grid_3(s, ch1, ch2, sb_reorder - 4, flag);
728 }
729
730 // Secondary channel flags
731 if (ch1 != ch2) {
732 if (ensure_bits(&s->gb, 20))
733 break;
734 if (!flag || sb_reorder >= s->max_mono_subband)
735 s->sec_ch_sbms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
736 if (flag && sb_reorder >= s->min_mono_subband)
737 s->sec_ch_lrms[ch1 / 2][sb_reorder] = get_bits(&s->gb, 8);
738 }
739
740 quant_level = s->quant_levels[ch1 / 2][sb];
741 if (!quant_level)
742 return AVERROR_INVALIDDATA;
743
744 // Time samples for one or both channels
745 if (sb < s->max_mono_subband && sb_reorder >= s->min_mono_subband) {
746 if (!flag)
747 parse_ch(s, ch1, sb_reorder, quant_level, 0);
748 else if (ch1 != ch2)
749 parse_ch(s, ch2, sb_reorder, quant_level, 1);
750 } else {
751 parse_ch(s, ch1, sb_reorder, quant_level, 0);
752 if (ch1 != ch2)
753 parse_ch(s, ch2, sb_reorder, quant_level, 0);
754 }
755 }
756
757 return 0;
758 }
759
760 /**
761 * Convert from reflection coefficients to direct form coefficients
762 */
763 static void convert_lpc(float *coeff, const int *codes)
764 {
765 int i, j;
766
767 for (i = 0; i < 8; i++) {
768 float rc = lpc_tab[codes[i]];
769 for (j = 0; j < (i + 1) / 2; j++) {
770 float tmp1 = coeff[ j ];
771 float tmp2 = coeff[i - j - 1];
772 coeff[ j ] = tmp1 + rc * tmp2;
773 coeff[i - j - 1] = tmp2 + rc * tmp1;
774 }
775 coeff[i] = rc;
776 }
777 }
778
779 static int parse_lpc(DCALbrDecoder *s, int ch1, int ch2, int start_sb, int end_sb)
780 {
781 int f = s->framenum & 1;
782 int i, sb, ch, codes[16];
783
784 // First two subbands have two sets of coefficients, third subband has one
785 for (sb = start_sb; sb < end_sb; sb++) {
786 int ncodes = 8 * (1 + (sb < 2));
787 for (ch = ch1; ch <= ch2; ch++) {
788 if (ensure_bits(&s->gb, 4 * ncodes))
789 return 0;
790 for (i = 0; i < ncodes; i++)
791 codes[i] = get_bits(&s->gb, 4);
792 for (i = 0; i < ncodes / 8; i++)
793 convert_lpc(s->lpc_coeff[f][ch][sb][i], &codes[i * 8]);
794 }
795 }
796
797 return 0;
798 }
799
800 static int parse_high_res_grid(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
801 {
802 int quant_levels[DCA_LBR_SUBBANDS];
803 int sb, ch, ol, st, max_sb, profile, ret;
804
805 if (!chunk->len)
806 return 0;
807
808 ret = init_get_bits8(&s->gb, chunk->data, chunk->len);
809 if (ret < 0)
810 return ret;
811
812 // Quantizer profile
813 profile = get_bits(&s->gb, 8);
814 // Overall level
815 ol = (profile >> 3) & 7;
816 // Steepness
817 st = profile >> 6;
818 // Max energy subband
819 max_sb = profile & 7;
820
821 // Calculate quantization levels
822 for (sb = 0; sb < s->nsubbands; sb++) {
823 int f = sb * s->limited_rate / s->nsubbands;
824 int a = 18000 / (12 * f / 1000 + 100 + 40 * st) + 20 * ol;
825 if (a <= 95)
826 quant_levels[sb] = 1;
827 else if (a <= 140)
828 quant_levels[sb] = 2;
829 else if (a <= 180)
830 quant_levels[sb] = 3;
831 else if (a <= 230)
832 quant_levels[sb] = 4;
833 else
834 quant_levels[sb] = 5;
835 }
836
837 // Reorder quantization levels for lower subbands
838 for (sb = 0; sb < 8; sb++)
839 s->quant_levels[ch1 / 2][sb] = quant_levels[ff_dca_sb_reorder[max_sb][sb]];
840 for (; sb < s->nsubbands; sb++)
841 s->quant_levels[ch1 / 2][sb] = quant_levels[sb];
842
843 // LPC for the first two subbands
844 ret = parse_lpc(s, ch1, ch2, 0, 2);
845 if (ret < 0)
846 return ret;
847
848 // Time-samples for the first two subbands of main channel
849 ret = parse_ts(s, ch1, ch2, 0, 2, 0);
850 if (ret < 0)
851 return ret;
852
853 // First two bands of the first grid
854 for (sb = 0; sb < 2; sb++)
855 for (ch = ch1; ch <= ch2; ch++)
856 if ((ret = parse_scale_factors(s, s->grid_1_scf[ch][sb])) < 0)
857 return ret;
858
859 return 0;
860 }
861
862 static int parse_grid_2(DCALbrDecoder *s, int ch1, int ch2,
863 int start_sb, int end_sb, int flag)
864 {
865 int i, j, sb, ch, nsubbands;
866
867 nsubbands = ff_dca_scf_to_grid_2[s->nsubbands - 1] + 1;
868 if (end_sb > nsubbands)
869 end_sb = nsubbands;
870
871 for (sb = start_sb; sb < end_sb; sb++) {
872 for (ch = ch1; ch <= ch2; ch++) {
873 uint8_t *g2_scf = s->grid_2_scf[ch][sb];
874
875 if ((ch != ch1 && ff_dca_grid_2_to_scf[sb] >= s->min_mono_subband) != flag) {
876 if (!flag)
877 memcpy(g2_scf, s->grid_2_scf[ch1][sb], 64);
878 continue;
879 }
880
881 // Scale factors in groups of 8
882 for (i = 0; i < 8; i++, g2_scf += 8) {
883 if (get_bits_left(&s->gb) < 1) {
884 memset(g2_scf, 0, 64 - i * 8);
885 break;
886 }
887 // Bit indicating if whole group has zero values
888 if (get_bits1(&s->gb)) {
889 for (j = 0; j < 8; j++) {
890 if (ensure_bits(&s->gb, 20))
891 break;
892 g2_scf[j] = parse_vlc(&s->gb, &ff_dca_vlc_grid_2, 2);
893 }
894 } else {
895 memset(g2_scf, 0, 8);
896 }
897 }
898 }
899 }
900
901 return 0;
902 }
903
904 static int parse_ts1_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
905 {
906 int ret;
907 if (!chunk->len)
908 return 0;
909 if ((ret = init_get_bits8(&s->gb, chunk->data, chunk->len)) < 0)
910 return ret;
911 if ((ret = parse_lpc(s, ch1, ch2, 2, 3)) < 0)
912 return ret;
913 if ((ret = parse_ts(s, ch1, ch2, 2, 4, 0)) < 0)
914 return ret;
915 if ((ret = parse_grid_2(s, ch1, ch2, 0, 1, 0)) < 0)
916 return ret;
917 if ((ret = parse_ts(s, ch1, ch2, 4, 6, 0)) < 0)
918 return ret;
919 return 0;
920 }
921
922 static int parse_ts2_chunk(DCALbrDecoder *s, LBRChunk *chunk, int ch1, int ch2)
923 {
924 int ret;
925
926 if (!chunk->len)
927 return 0;
928 if ((ret = init_get_bits8(&s->gb, chunk->data, chunk->len)) < 0)
929 return ret;
930 if ((ret = parse_grid_2(s, ch1, ch2, 1, 3, 0)) < 0)
931 return ret;
932 if ((ret = parse_ts(s, ch1, ch2, 6, s->max_mono_subband, 0)) < 0)
933 return ret;
934 if (ch1 != ch2) {
935 if ((ret = parse_grid_1_sec_ch(s, ch2)) < 0)
936 return ret;
937 if ((ret = parse_grid_2(s, ch1, ch2, 0, 3, 1)) < 0)
938 return ret;
939 }
940 if ((ret = parse_ts(s, ch1, ch2, s->min_mono_subband, s->nsubbands, 1)) < 0)
941 return ret;
942 return 0;
943 }
944
945 static int init_sample_rate(DCALbrDecoder *s)
946 {
947 double scale = (-1.0 / (1 << 17)) * sqrt(1 << (2 - s->limited_range));
948 int i, br_per_ch = s->bit_rate_scaled / s->nchannels_total;
949 int ret;
950
951 ff_mdct_end(&s->imdct);
952
953 ret = ff_mdct_init(&s->imdct, s->freq_range + 6, 1, scale);
954 if (ret < 0)
955 return ret;
956
957 for (i = 0; i < 32 << s->freq_range; i++)
958 s->window[i] = ff_dca_long_window[i << (2 - s->freq_range)];
959
960 if (br_per_ch < 14000)
961 scale = 0.85;
962 else if (br_per_ch < 32000)
963 scale = (br_per_ch - 14000) * (1.0 / 120000) + 0.85;
964 else
965 scale = 1.0;
966
967 scale *= 1.0 / INT_MAX;
968
969 for (i = 0; i < s->nsubbands; i++) {
970 if (i < 2)
971 s->sb_scf[i] = 0; // The first two subbands are always zero
972 else if (i < 5)
973 s->sb_scf[i] = (i - 1) * 0.25 * 0.785 * scale;
974 else
975 s->sb_scf[i] = 0.785 * scale;
976 }
977
978 s->lfe_scale = (16 << s->freq_range) * 0.0000078265894;
979
980 return 0;
981 }
982
983 static int alloc_sample_buffer(DCALbrDecoder *s)
984 {
985 // Reserve space for history and padding
986 int nchsamples = DCA_LBR_TIME_SAMPLES + DCA_LBR_TIME_HISTORY * 2;
987 int nsamples = nchsamples * s->nchannels * s->nsubbands;
988 int ch, sb;
989 float *ptr;
990
991 // Reallocate time sample buffer
992 av_fast_mallocz(&s->ts_buffer, &s->ts_size, nsamples * sizeof(float));
993 if (!s->ts_buffer)
994 return AVERROR(ENOMEM);
995
996 ptr = s->ts_buffer + DCA_LBR_TIME_HISTORY;
997 for (ch = 0; ch < s->nchannels; ch++) {
998 for (sb = 0; sb < s->nsubbands; sb++) {
999 s->time_samples[ch][sb] = ptr;
1000 ptr += nchsamples;
1001 }
1002 }
1003
1004 return 0;
1005 }
1006
1007 static int parse_decoder_init(DCALbrDecoder *s, GetByteContext *gb)
1008 {
1009 int old_rate = s->sample_rate;
1010 int old_band_limit = s->band_limit;
1011 int old_nchannels = s->nchannels;
1012 int version, bit_rate_hi;
1013 unsigned int sr_code;
1014
1015 // Sample rate of LBR audio
1016 sr_code = bytestream2_get_byte(gb);
1017 if (sr_code >= FF_ARRAY_ELEMS(ff_dca_sampling_freqs)) {
1018 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sample rate\n");
1019 return AVERROR_INVALIDDATA;
1020 }
1021 s->sample_rate = ff_dca_sampling_freqs[sr_code];
1022 if (s->sample_rate > 48000) {
1023 avpriv_report_missing_feature(s->avctx, "%d Hz LBR sample rate", s->sample_rate);
1024 return AVERROR_PATCHWELCOME;
1025 }
1026
1027 // LBR speaker mask
1028 s->ch_mask = bytestream2_get_le16(gb);
1029 if (!(s->ch_mask & 0x7)) {
1030 avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1031 return AVERROR_PATCHWELCOME;
1032 }
1033 if ((s->ch_mask & 0xfff0) && !(s->warned & 1)) {
1034 avpriv_report_missing_feature(s->avctx, "LBR channel mask %#x", s->ch_mask);
1035 s->warned |= 1;
1036 }
1037
1038 // LBR bitstream version
1039 version = bytestream2_get_le16(gb);
1040 if ((version & 0xff00) != 0x0800) {
1041 avpriv_report_missing_feature(s->avctx, "LBR stream version %#x", version);
1042 return AVERROR_PATCHWELCOME;
1043 }
1044
1045 // Flags for LBR decoder initialization
1046 s->flags = bytestream2_get_byte(gb);
1047 if (s->flags & LBR_FLAG_DMIX_MULTI_CH) {
1048 avpriv_report_missing_feature(s->avctx, "LBR multi-channel downmix");
1049 return AVERROR_PATCHWELCOME;
1050 }
1051 if ((s->flags & LBR_FLAG_LFE_PRESENT) && s->sample_rate != 48000) {
1052 if (!(s->warned & 2)) {
1053 avpriv_report_missing_feature(s->avctx, "%d Hz LFE interpolation", s->sample_rate);
1054 s->warned |= 2;
1055 }
1056 s->flags &= ~LBR_FLAG_LFE_PRESENT;
1057 }
1058
1059 // Most significant bit rate nibbles
1060 bit_rate_hi = bytestream2_get_byte(gb);
1061
1062 // Least significant original bit rate word
1063 s->bit_rate_orig = bytestream2_get_le16(gb) | ((bit_rate_hi & 0x0F) << 16);
1064
1065 // Least significant scaled bit rate word
1066 s->bit_rate_scaled = bytestream2_get_le16(gb) | ((bit_rate_hi & 0xF0) << 12);
1067
1068 // Setup number of fullband channels
1069 s->nchannels_total = ff_dca_count_chs_for_mask(s->ch_mask & ~DCA_SPEAKER_PAIR_LFE1);
1070 s->nchannels = FFMIN(s->nchannels_total, DCA_LBR_CHANNELS);
1071
1072 // Setup band limit
1073 switch (s->flags & LBR_FLAG_BAND_LIMIT_MASK) {
1074 case LBR_FLAG_BAND_LIMIT_NONE:
1075 s->band_limit = 0;
1076 break;
1077 case LBR_FLAG_BAND_LIMIT_1_2:
1078 s->band_limit = 1;
1079 break;
1080 case LBR_FLAG_BAND_LIMIT_1_4:
1081 s->band_limit = 2;
1082 break;
1083 default:
1084 avpriv_report_missing_feature(s->avctx, "LBR band limit %#x", s->flags & LBR_FLAG_BAND_LIMIT_MASK);
1085 return AVERROR_PATCHWELCOME;
1086 }
1087
1088 // Setup frequency range
1089 s->freq_range = ff_dca_freq_ranges[sr_code];
1090
1091 // Setup resolution profile
1092 if (s->bit_rate_orig >= 44000 * (s->nchannels_total + 2))
1093 s->res_profile = 2;
1094 else if (s->bit_rate_orig >= 25000 * (s->nchannels_total + 2))
1095 s->res_profile = 1;
1096 else
1097 s->res_profile = 0;
1098
1099 // Setup limited sample rate, number of subbands, etc
1100 s->limited_rate = s->sample_rate >> s->band_limit;
1101 s->limited_range = s->freq_range - s->band_limit;
1102 if (s->limited_range < 0) {
1103 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR band limit for frequency range\n");
1104 return AVERROR_INVALIDDATA;
1105 }
1106
1107 s->nsubbands = 8 << s->limited_range;
1108
1109 s->g3_avg_only_start_sb = s->nsubbands * ff_dca_avg_g3_freqs[s->res_profile] / (s->limited_rate / 2);
1110 if (s->g3_avg_only_start_sb > s->nsubbands)
1111 s->g3_avg_only_start_sb = s->nsubbands;
1112
1113 s->min_mono_subband = s->nsubbands * 2000 / (s->limited_rate / 2);
1114 if (s->min_mono_subband > s->nsubbands)
1115 s->min_mono_subband = s->nsubbands;
1116
1117 s->max_mono_subband = s->nsubbands * 14000 / (s->limited_rate / 2);
1118 if (s->max_mono_subband > s->nsubbands)
1119 s->max_mono_subband = s->nsubbands;
1120
1121 // Handle change of sample rate
1122 if ((old_rate != s->sample_rate || old_band_limit != s->band_limit) && init_sample_rate(s) < 0)
1123 return AVERROR(ENOMEM);
1124
1125 // Setup stereo downmix
1126 if (s->flags & LBR_FLAG_DMIX_STEREO) {
1127 DCAContext *dca = s->avctx->priv_data;
1128
1129 if (s->nchannels_total < 3 || s->nchannels_total > DCA_LBR_CHANNELS_TOTAL - 2) {
1130 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of channels for LBR stereo downmix\n");
1131 return AVERROR_INVALIDDATA;
1132 }
1133
1134 // This decoder doesn't support ECS chunk
1135 if (dca->request_channel_layout != DCA_SPEAKER_LAYOUT_STEREO && !(s->warned & 4)) {
1136 avpriv_report_missing_feature(s->avctx, "Embedded LBR stereo downmix");
1137 s->warned |= 4;
1138 }
1139
1140 // Account for extra downmixed channel pair
1141 s->nchannels_total += 2;
1142 s->nchannels = 2;
1143 s->ch_mask = DCA_SPEAKER_PAIR_LR;
1144 s->flags &= ~LBR_FLAG_LFE_PRESENT;
1145 }
1146
1147 // Handle change of sample rate or number of channels
1148 if (old_rate != s->sample_rate
1149 || old_band_limit != s->band_limit
1150 || old_nchannels != s->nchannels) {
1151 if (alloc_sample_buffer(s) < 0)
1152 return AVERROR(ENOMEM);
1153 ff_dca_lbr_flush(s);
1154 }
1155
1156 return 0;
1157 }
1158
1159 int ff_dca_lbr_parse(DCALbrDecoder *s, uint8_t *data, DCAExssAsset *asset)
1160 {
1161 struct {
1162 LBRChunk lfe;
1163 LBRChunk tonal;
1164 LBRChunk tonal_grp[5];
1165 LBRChunk grid1[DCA_LBR_CHANNELS / 2];
1166 LBRChunk hr_grid[DCA_LBR_CHANNELS / 2];
1167 LBRChunk ts1[DCA_LBR_CHANNELS / 2];
1168 LBRChunk ts2[DCA_LBR_CHANNELS / 2];
1169 } chunk = { {0} };
1170
1171 GetByteContext gb;
1172
1173 int i, ch, sb, sf, ret, group, chunk_id, chunk_len;
1174
1175 bytestream2_init(&gb, data + asset->lbr_offset, asset->lbr_size);
1176
1177 // LBR sync word
1178 if (bytestream2_get_be32(&gb) != DCA_SYNCWORD_LBR) {
1179 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR sync word\n");
1180 return AVERROR_INVALIDDATA;
1181 }
1182
1183 // LBR header type
1184 switch (bytestream2_get_byte(&gb)) {
1185 case DCA_LBR_HEADER_SYNC_ONLY:
1186 if (!s->sample_rate) {
1187 av_log(s->avctx, AV_LOG_ERROR, "LBR decoder not initialized\n");
1188 return AVERROR_INVALIDDATA;
1189 }
1190 break;
1191 case DCA_LBR_HEADER_DECODER_INIT:
1192 if ((ret = parse_decoder_init(s, &gb)) < 0) {
1193 s->sample_rate = 0;
1194 return ret;
1195 }
1196 break;
1197 default:
1198 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR header type\n");
1199 return AVERROR_INVALIDDATA;
1200 }
1201
1202 // LBR frame chunk header
1203 chunk_id = bytestream2_get_byte(&gb);
1204 chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1205
1206 if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1207 chunk_len = bytestream2_get_bytes_left(&gb);
1208 av_log(s->avctx, AV_LOG_WARNING, "LBR frame chunk was truncated\n");
1209 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1210 return AVERROR_INVALIDDATA;
1211 }
1212
1213 bytestream2_init(&gb, gb.buffer, chunk_len);
1214
1215 switch (chunk_id & 0x7f) {
1216 case LBR_CHUNK_FRAME:
1217 if (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL)) {
1218 int checksum = bytestream2_get_be16(&gb);
1219 uint16_t res = chunk_id;
1220 res += (chunk_len >> 8) & 0xff;
1221 res += chunk_len & 0xff;
1222 for (i = 0; i < chunk_len - 2; i++)
1223 res += gb.buffer[i];
1224 if (checksum != res) {
1225 av_log(s->avctx, AV_LOG_WARNING, "Invalid LBR checksum\n");
1226 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1227 return AVERROR_INVALIDDATA;
1228 }
1229 } else {
1230 bytestream2_skip(&gb, 2);
1231 }
1232 break;
1233 case LBR_CHUNK_FRAME_NO_CSUM:
1234 break;
1235 default:
1236 av_log(s->avctx, AV_LOG_ERROR, "Invalid LBR frame chunk ID\n");
1237 return AVERROR_INVALIDDATA;
1238 }
1239
1240 // Clear current frame
1241 memset(s->quant_levels, 0, sizeof(s->quant_levels));
1242 memset(s->sb_indices, 0xff, sizeof(s->sb_indices));
1243 memset(s->sec_ch_sbms, 0, sizeof(s->sec_ch_sbms));
1244 memset(s->sec_ch_lrms, 0, sizeof(s->sec_ch_lrms));
1245 memset(s->ch_pres, 0, sizeof(s->ch_pres));
1246 memset(s->grid_1_scf, 0, sizeof(s->grid_1_scf));
1247 memset(s->grid_2_scf, 0, sizeof(s->grid_2_scf));
1248 memset(s->grid_3_avg, 0, sizeof(s->grid_3_avg));
1249 memset(s->grid_3_scf, 0, sizeof(s->grid_3_scf));
1250 memset(s->grid_3_pres, 0, sizeof(s->grid_3_pres));
1251 memset(s->tonal_scf, 0, sizeof(s->tonal_scf));
1252 memset(s->lfe_data, 0, sizeof(s->lfe_data));
1253 s->part_stereo_pres = 0;
1254 s->framenum = (s->framenum + 1) & 31;
1255
1256 for (ch = 0; ch < s->nchannels; ch++) {
1257 for (sb = 0; sb < s->nsubbands / 4; sb++) {
1258 s->part_stereo[ch][sb][0] = s->part_stereo[ch][sb][4];
1259 s->part_stereo[ch][sb][4] = 16;
1260 }
1261 }
1262
1263 memset(s->lpc_coeff[s->framenum & 1], 0, sizeof(s->lpc_coeff[0]));
1264
1265 for (group = 0; group < 5; group++) {
1266 for (sf = 0; sf < 1 << group; sf++) {
1267 int sf_idx = ((s->framenum << group) + sf) & 31;
1268 s->tonal_bounds[group][sf_idx][0] =
1269 s->tonal_bounds[group][sf_idx][1] = s->ntones;
1270 }
1271 }
1272
1273 // Parse chunk headers
1274 while (bytestream2_get_bytes_left(&gb) > 0) {
1275 chunk_id = bytestream2_get_byte(&gb);
1276 chunk_len = (chunk_id & 0x80) ? bytestream2_get_be16(&gb) : bytestream2_get_byte(&gb);
1277 chunk_id &= 0x7f;
1278
1279 if (chunk_len > bytestream2_get_bytes_left(&gb)) {
1280 chunk_len = bytestream2_get_bytes_left(&gb);
1281 av_log(s->avctx, AV_LOG_WARNING, "LBR chunk %#x was truncated\n", chunk_id);
1282 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1283 return AVERROR_INVALIDDATA;
1284 }
1285
1286 switch (chunk_id) {
1287 case LBR_CHUNK_LFE:
1288 chunk.lfe.len = chunk_len;
1289 chunk.lfe.data = gb.buffer;
1290 break;
1291
1292 case LBR_CHUNK_SCF:
1293 case LBR_CHUNK_TONAL:
1294 case LBR_CHUNK_TONAL_SCF:
1295 chunk.tonal.id = chunk_id;
1296 chunk.tonal.len = chunk_len;
1297 chunk.tonal.data = gb.buffer;
1298 break;
1299
1300 case LBR_CHUNK_TONAL_GRP_1:
1301 case LBR_CHUNK_TONAL_GRP_2:
1302 case LBR_CHUNK_TONAL_GRP_3:
1303 case LBR_CHUNK_TONAL_GRP_4:
1304 case LBR_CHUNK_TONAL_GRP_5:
1305 i = LBR_CHUNK_TONAL_GRP_5 - chunk_id;
1306 chunk.tonal_grp[i].id = i;
1307 chunk.tonal_grp[i].len = chunk_len;
1308 chunk.tonal_grp[i].data = gb.buffer;
1309 break;
1310
1311 case LBR_CHUNK_TONAL_SCF_GRP_1:
1312 case LBR_CHUNK_TONAL_SCF_GRP_2:
1313 case LBR_CHUNK_TONAL_SCF_GRP_3:
1314 case LBR_CHUNK_TONAL_SCF_GRP_4:
1315 case LBR_CHUNK_TONAL_SCF_GRP_5:
1316 i = LBR_CHUNK_TONAL_SCF_GRP_5 - chunk_id;
1317 chunk.tonal_grp[i].id = i;
1318 chunk.tonal_grp[i].len = chunk_len;
1319 chunk.tonal_grp[i].data = gb.buffer;
1320 break;
1321
1322 case LBR_CHUNK_RES_GRID_LR:
1323 case LBR_CHUNK_RES_GRID_LR + 1:
1324 case LBR_CHUNK_RES_GRID_LR + 2:
1325 i = chunk_id - LBR_CHUNK_RES_GRID_LR;
1326 chunk.grid1[i].len = chunk_len;
1327 chunk.grid1[i].data = gb.buffer;
1328 break;
1329
1330 case LBR_CHUNK_RES_GRID_HR:
1331 case LBR_CHUNK_RES_GRID_HR + 1:
1332 case LBR_CHUNK_RES_GRID_HR + 2:
1333 i = chunk_id - LBR_CHUNK_RES_GRID_HR;
1334 chunk.hr_grid[i].len = chunk_len;
1335 chunk.hr_grid[i].data = gb.buffer;
1336 break;
1337
1338 case LBR_CHUNK_RES_TS_1:
1339 case LBR_CHUNK_RES_TS_1 + 1:
1340 case LBR_CHUNK_RES_TS_1 + 2:
1341 i = chunk_id - LBR_CHUNK_RES_TS_1;
1342 chunk.ts1[i].len = chunk_len;
1343 chunk.ts1[i].data = gb.buffer;
1344 break;
1345
1346 case LBR_CHUNK_RES_TS_2:
1347 case LBR_CHUNK_RES_TS_2 + 1:
1348 case LBR_CHUNK_RES_TS_2 + 2:
1349 i = chunk_id - LBR_CHUNK_RES_TS_2;
1350 chunk.ts2[i].len = chunk_len;
1351 chunk.ts2[i].data = gb.buffer;
1352 break;
1353 }
1354
1355 bytestream2_skip(&gb, chunk_len);
1356 }
1357
1358 // Parse the chunks
1359 ret = parse_lfe_chunk(s, &chunk.lfe);
1360
1361 ret |= parse_tonal_chunk(s, &chunk.tonal);
1362
1363 for (i = 0; i < 5; i++)
1364 ret |= parse_tonal_group(s, &chunk.tonal_grp[i]);
1365
1366 for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1367 int ch1 = i * 2;
1368 int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1369
1370 if (parse_grid_1_chunk (s, &chunk.grid1 [i], ch1, ch2) < 0 ||
1371 parse_high_res_grid(s, &chunk.hr_grid[i], ch1, ch2) < 0) {
1372 ret = -1;
1373 continue;
1374 }
1375
1376 // TS chunks depend on both grids. TS_2 depends on TS_1.
1377 if (!chunk.grid1[i].len || !chunk.hr_grid[i].len || !chunk.ts1[i].len)
1378 continue;
1379
1380 if (parse_ts1_chunk(s, &chunk.ts1[i], ch1, ch2) < 0 ||
1381 parse_ts2_chunk(s, &chunk.ts2[i], ch1, ch2) < 0) {
1382 ret = -1;
1383 continue;
1384 }
1385 }
1386
1387 if (ret < 0 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1388 return AVERROR_INVALIDDATA;
1389
1390 return 0;
1391 }
1392
1393 /**
1394 * Reconstruct high-frequency resolution grid from first and third grids
1395 */
1396 static void decode_grid(DCALbrDecoder *s, int ch1, int ch2)
1397 {
1398 int i, ch, sb;
1399
1400 for (ch = ch1; ch <= ch2; ch++) {
1401 for (sb = 0; sb < s->nsubbands; sb++) {
1402 int g1_sb = ff_dca_scf_to_grid_1[sb];
1403
1404 uint8_t *g1_scf_a = s->grid_1_scf[ch][g1_sb ];
1405 uint8_t *g1_scf_b = s->grid_1_scf[ch][g1_sb + 1];
1406
1407 int w1 = ff_dca_grid_1_weights[g1_sb ][sb];
1408 int w2 = ff_dca_grid_1_weights[g1_sb + 1][sb];
1409
1410 uint8_t *hr_scf = s->high_res_scf[ch][sb];
1411
1412 if (sb < 4) {
1413 for (i = 0; i < 8; i++) {
1414 int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1415 hr_scf[i] = scf >> 7;
1416 }
1417 } else {
1418 int8_t *g3_scf = s->grid_3_scf[ch][sb - 4];
1419 int g3_avg = s->grid_3_avg[ch][sb - 4];
1420
1421 for (i = 0; i < 8; i++) {
1422 int scf = w1 * g1_scf_a[i] + w2 * g1_scf_b[i];
1423 hr_scf[i] = (scf >> 7) - g3_avg - g3_scf[i];
1424 }
1425 }
1426 }
1427 }
1428 }
1429
1430 /**
1431 * Fill unallocated subbands with randomness
1432 */
1433 static void random_ts(DCALbrDecoder *s, int ch1, int ch2)
1434 {
1435 int i, j, k, ch, sb;
1436
1437 for (ch = ch1; ch <= ch2; ch++) {
1438 for (sb = 0; sb < s->nsubbands; sb++) {
1439 float *samples = s->time_samples[ch][sb];
1440
1441 if (s->ch_pres[ch] & (1U << sb))
1442 continue; // Skip allocated subband
1443
1444 if (sb < 2) {
1445 // The first two subbands are always zero
1446 memset(samples, 0, DCA_LBR_TIME_SAMPLES * sizeof(float));
1447 } else if (sb < 10) {
1448 for (i = 0; i < DCA_LBR_TIME_SAMPLES; i++)
1449 samples[i] = lbr_rand(s, sb);
1450 } else {
1451 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 8; i++, samples += 8) {
1452 float accum[8] = { 0 };
1453
1454 // Modulate by subbands 2-5 in blocks of 8
1455 for (k = 2; k < 6; k++) {
1456 float *other = &s->time_samples[ch][k][i * 8];
1457 for (j = 0; j < 8; j++)
1458 accum[j] += fabs(other[j]);
1459 }
1460
1461 for (j = 0; j < 8; j++)
1462 samples[j] = (accum[j] * 0.25f + 0.5f) * lbr_rand(s, sb);
1463 }
1464 }
1465 }
1466 }
1467 }
1468
1469 static void predict(float *samples, const float *coeff, int nsamples)
1470 {
1471 int i, j;
1472
1473 for (i = 0; i < nsamples; i++) {
1474 float res = 0;
1475 for (j = 0; j < 8; j++)
1476 res += coeff[j] * samples[i - j - 1];
1477 samples[i] -= res;
1478 }
1479 }
1480
1481 static void synth_lpc(DCALbrDecoder *s, int ch1, int ch2, int sb)
1482 {
1483 int f = s->framenum & 1;
1484 int ch;
1485
1486 for (ch = ch1; ch <= ch2; ch++) {
1487 float *samples = s->time_samples[ch][sb];
1488
1489 if (!(s->ch_pres[ch] & (1U << sb)))
1490 continue;
1491
1492 if (sb < 2) {
1493 predict(samples, s->lpc_coeff[f^1][ch][sb][1], 16);
1494 predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 64);
1495 predict(samples + 80, s->lpc_coeff[f ][ch][sb][1], 48);
1496 } else {
1497 predict(samples, s->lpc_coeff[f^1][ch][sb][0], 16);
1498 predict(samples + 16, s->lpc_coeff[f ][ch][sb][0], 112);
1499 }
1500 }
1501 }
1502
1503 static void filter_ts(DCALbrDecoder *s, int ch1, int ch2)
1504 {
1505 int i, j, sb, ch;
1506
1507 for (sb = 0; sb < s->nsubbands; sb++) {
1508 // Scale factors
1509 for (ch = ch1; ch <= ch2; ch++) {
1510 float *samples = s->time_samples[ch][sb];
1511 uint8_t *hr_scf = s->high_res_scf[ch][sb];
1512 if (sb < 4) {
1513 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++, samples += 16) {
1514 unsigned int scf = hr_scf[i];
1515 if (scf > AMP_MAX)
1516 scf = AMP_MAX;
1517 for (j = 0; j < 16; j++)
1518 samples[j] *= ff_dca_quant_amp[scf];
1519 }
1520 } else {
1521 uint8_t *g2_scf = s->grid_2_scf[ch][ff_dca_scf_to_grid_2[sb]];
1522 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 2; i++, samples += 2) {
1523 unsigned int scf = hr_scf[i / 8] - g2_scf[i];
1524 if (scf > AMP_MAX)
1525 scf = AMP_MAX;
1526 samples[0] *= ff_dca_quant_amp[scf];
1527 samples[1] *= ff_dca_quant_amp[scf];
1528 }
1529 }
1530 }
1531
1532 // Mid-side stereo
1533 if (ch1 != ch2) {
1534 float *samples_l = s->time_samples[ch1][sb];
1535 float *samples_r = s->time_samples[ch2][sb];
1536 int ch2_pres = s->ch_pres[ch2] & (1U << sb);
1537
1538 for (i = 0; i < DCA_LBR_TIME_SAMPLES / 16; i++) {
1539 int sbms = (s->sec_ch_sbms[ch1 / 2][sb] >> i) & 1;
1540 int lrms = (s->sec_ch_lrms[ch1 / 2][sb] >> i) & 1;
1541
1542 if (sb >= s->min_mono_subband) {
1543 if (lrms && ch2_pres) {
1544 if (sbms) {
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 } else {
1551 for (j = 0; j < 16; j++) {
1552 float tmp = samples_l[j];
1553 samples_l[j] = samples_r[j];
1554 samples_r[j] = tmp;
1555 }
1556 }
1557 } else if (!ch2_pres) {
1558 if (sbms && (s->part_stereo_pres & (1 << ch1))) {
1559 for (j = 0; j < 16; j++)
1560 samples_r[j] = -samples_l[j];
1561 } else {
1562 for (j = 0; j < 16; j++)
1563 samples_r[j] = samples_l[j];
1564 }
1565 }
1566 } else if (sbms && ch2_pres) {
1567 for (j = 0; j < 16; j++) {
1568 float tmp = samples_l[j];
1569 samples_l[j] = (tmp + samples_r[j]) * 0.5f;
1570 samples_r[j] = (tmp - samples_r[j]) * 0.5f;
1571 }
1572 }
1573
1574 samples_l += 16;
1575 samples_r += 16;
1576 }
1577 }
1578
1579 // Inverse prediction
1580 if (sb < 3)
1581 synth_lpc(s, ch1, ch2, sb);
1582 }
1583 }
1584
1585 /**
1586 * Modulate by interpolated partial stereo coefficients
1587 */
1588 static void decode_part_stereo(DCALbrDecoder *s, int ch1, int ch2)
1589 {
1590 int i, ch, sb, sf;
1591
1592 for (ch = ch1; ch <= ch2; ch++) {
1593 for (sb = s->min_mono_subband; sb < s->nsubbands; sb++) {
1594 uint8_t *pt_st = s->part_stereo[ch][(sb - s->min_mono_subband) / 4];
1595 float *samples = s->time_samples[ch][sb];
1596
1597 if (s->ch_pres[ch2] & (1U << sb))
1598 continue;
1599
1600 for (sf = 1; sf <= 4; sf++, samples += 32) {
1601 float prev = ff_dca_st_coeff[pt_st[sf - 1]];
1602 float next = ff_dca_st_coeff[pt_st[sf ]];
1603
1604 for (i = 0; i < 32; i++)
1605 samples[i] *= (32 - i) * prev + i * next;
1606 }
1607 }
1608 }
1609 }
1610
1611 /**
1612 * Synthesise tones in the given group for the given tonal subframe
1613 */
1614 static void synth_tones(DCALbrDecoder *s, int ch, float *values,
1615 int group, int group_sf, int synth_idx)
1616 {
1617 int i, start, count;
1618
1619 if (synth_idx < 0)
1620 return;
1621
1622 start = s->tonal_bounds[group][group_sf][0];
1623 count = (s->tonal_bounds[group][group_sf][1] - start) & (DCA_LBR_TONES - 1);
1624
1625 for (i = 0; i < count; i++) {
1626 DCALbrTone *t = &s->tones[(start + i) & (DCA_LBR_TONES - 1)];
1627
1628 if (t->amp[ch]) {
1629 float amp = ff_dca_synth_env[synth_idx] * ff_dca_quant_amp[t->amp[ch]];
1630 float c = amp * cos_tab[(t->phs[ch] ) & 255];
1631 float s = amp * cos_tab[(t->phs[ch] + 64) & 255];
1632 const float *cf = ff_dca_corr_cf[t->f_delt];
1633 int x_freq = t->x_freq;
1634
1635 switch (x_freq) {
1636 case 0:
1637 goto p0;
1638 case 1:
1639 values[3] += cf[0] * -s;
1640 values[2] += cf[1] * c;
1641 values[1] += cf[2] * s;
1642 values[0] += cf[3] * -c;
1643 goto p1;
1644 case 2:
1645 values[2] += cf[0] * -s;
1646 values[1] += cf[1] * c;
1647 values[0] += cf[2] * s;
1648 goto p2;
1649 case 3:
1650 values[1] += cf[0] * -s;
1651 values[0] += cf[1] * c;
1652 goto p3;
1653 case 4:
1654 values[0] += cf[0] * -s;
1655 goto p4;
1656 }
1657
1658 values[x_freq - 5] += cf[ 0] * -s;
1659 p4: values[x_freq - 4] += cf[ 1] * c;
1660 p3: values[x_freq - 3] += cf[ 2] * s;
1661 p2: values[x_freq - 2] += cf[ 3] * -c;
1662 p1: values[x_freq - 1] += cf[ 4] * -s;
1663 p0: values[x_freq ] += cf[ 5] * c;
1664 values[x_freq + 1] += cf[ 6] * s;
1665 values[x_freq + 2] += cf[ 7] * -c;
1666 values[x_freq + 3] += cf[ 8] * -s;
1667 values[x_freq + 4] += cf[ 9] * c;
1668 values[x_freq + 5] += cf[10] * s;
1669 }
1670
1671 t->phs[ch] += t->ph_rot;
1672 }
1673 }
1674
1675 /**
1676 * Synthesise all tones in all groups for the given residual subframe
1677 */
1678 static void base_func_synth(DCALbrDecoder *s, int ch, float *values, int sf)
1679 {
1680 int group;
1681
1682 // Tonal vs residual shift is 22 subframes
1683 for (group = 0; group < 5; group++) {
1684 int group_sf = (s->framenum << group) + ((sf - 22) >> (5 - group));
1685 int synth_idx = ((((sf - 22) & 31) << group) & 31) + (1 << group) - 1;
1686
1687 synth_tones(s, ch, values, group, (group_sf - 1) & 31, 30 - synth_idx);
1688 synth_tones(s, ch, values, group, (group_sf ) & 31, synth_idx);
1689 }
1690 }
1691
1692 static void transform_channel(DCALbrDecoder *s, int ch, float *output)
1693 {
1694 LOCAL_ALIGNED_32(float, values, [DCA_LBR_SUBBANDS ], [4]);
1695 LOCAL_ALIGNED_32(float, result, [DCA_LBR_SUBBANDS * 2], [4]);
1696 int sf, sb, nsubbands = s->nsubbands, noutsubbands = 8 << s->freq_range;
1697
1698 // Clear inactive subbands
1699 if (nsubbands < noutsubbands)
1700 memset(values[nsubbands], 0, (noutsubbands - nsubbands) * sizeof(values[0]));
1701
1702 for (sf = 0; sf < DCA_LBR_TIME_SAMPLES / 4; sf++) {
1703 // Hybrid filterbank
1704 s->dcadsp->lbr_bank(values, s->time_samples[ch],
1705 ff_dca_bank_coeff, sf * 4, nsubbands);
1706
1707 base_func_synth(s, ch, values[0], sf);
1708
1709 s->imdct.imdct_calc(&s->imdct, result[0], values[0]);
1710
1711 // Long window and overlap-add
1712 s->fdsp->vector_fmul_add(output, result[0], s->window,
1713 s->history[ch], noutsubbands * 4);
1714 s->fdsp->vector_fmul_reverse(s->history[ch], result[noutsubbands],
1715 s->window, noutsubbands * 4);
1716 output += noutsubbands * 4;
1717 }
1718
1719 // Update history for LPC and forward MDCT
1720 for (sb = 0; sb < nsubbands; sb++) {
1721 float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1722 memcpy(samples, samples + DCA_LBR_TIME_SAMPLES, DCA_LBR_TIME_HISTORY * sizeof(float));
1723 }
1724 }
1725
1726 int ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame)
1727 {
1728 AVCodecContext *avctx = s->avctx;
1729 int i, ret, nchannels, ch_conf = (s->ch_mask & 0x7) - 1;
1730 const int8_t *reorder;
1731 uint64_t channel_mask = channel_layouts[ch_conf];
1732
1733 nchannels = av_popcount64(channel_mask);
1734 avctx->sample_rate = s->sample_rate;
1735 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1736 avctx->bits_per_raw_sample = 0;
1737 avctx->profile = FF_PROFILE_DTS_EXPRESS;
1738 avctx->bit_rate = s->bit_rate_scaled;
1739
1740 if (s->flags & LBR_FLAG_LFE_PRESENT) {
1741 channel_mask |= AV_CH_LOW_FREQUENCY;
1742 reorder = channel_reorder_lfe[ch_conf];
1743 } else {
1744 reorder = channel_reorder_nolfe[ch_conf];
1745 }
1746
1747 av_channel_layout_uninit(&avctx->ch_layout);
1748 av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
1749
1750 frame->nb_samples = 1024 << s->freq_range;
1751 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1752 return ret;
1753
1754 // Filter fullband channels
1755 for (i = 0; i < (s->nchannels + 1) / 2; i++) {
1756 int ch1 = i * 2;
1757 int ch2 = FFMIN(ch1 + 1, s->nchannels - 1);
1758
1759 decode_grid(s, ch1, ch2);
1760
1761 random_ts(s, ch1, ch2);
1762
1763 filter_ts(s, ch1, ch2);
1764
1765 if (ch1 != ch2 && (s->part_stereo_pres & (1 << ch1)))
1766 decode_part_stereo(s, ch1, ch2);
1767
1768 if (ch1 < nchannels)
1769 transform_channel(s, ch1, (float *)frame->extended_data[reorder[ch1]]);
1770
1771 if (ch1 != ch2 && ch2 < nchannels)
1772 transform_channel(s, ch2, (float *)frame->extended_data[reorder[ch2]]);
1773 }
1774
1775 // Interpolate LFE channel
1776 if (s->flags & LBR_FLAG_LFE_PRESENT) {
1777 s->dcadsp->lfe_iir((float *)frame->extended_data[lfe_index[ch_conf]],
1778 s->lfe_data, ff_dca_lfe_iir,
1779 s->lfe_history, 16 << s->freq_range);
1780 }
1781
1782 if ((ret = ff_side_data_update_matrix_encoding(frame, AV_MATRIX_ENCODING_NONE)) < 0)
1783 return ret;
1784
1785 return 0;
1786 }
1787
1788 av_cold void ff_dca_lbr_flush(DCALbrDecoder *s)
1789 {
1790 int ch, sb;
1791
1792 if (!s->sample_rate)
1793 return;
1794
1795 // Clear history
1796 memset(s->part_stereo, 16, sizeof(s->part_stereo));
1797 memset(s->lpc_coeff, 0, sizeof(s->lpc_coeff));
1798 memset(s->history, 0, sizeof(s->history));
1799 memset(s->tonal_bounds, 0, sizeof(s->tonal_bounds));
1800 memset(s->lfe_history, 0, sizeof(s->lfe_history));
1801 s->framenum = 0;
1802 s->ntones = 0;
1803
1804 for (ch = 0; ch < s->nchannels; ch++) {
1805 for (sb = 0; sb < s->nsubbands; sb++) {
1806 float *samples = s->time_samples[ch][sb] - DCA_LBR_TIME_HISTORY;
1807 memset(samples, 0, DCA_LBR_TIME_HISTORY * sizeof(float));
1808 }
1809 }
1810 }
1811
1812 92 av_cold int ff_dca_lbr_init(DCALbrDecoder *s)
1813 {
1814
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (!(s->fdsp = avpriv_float_dsp_alloc(0)))
1815 return AVERROR(ENOMEM);
1816
1817 92 s->lbr_rand = 1;
1818 92 return 0;
1819 }
1820
1821 92 av_cold void ff_dca_lbr_close(DCALbrDecoder *s)
1822 {
1823 92 s->sample_rate = 0;
1824
1825 92 av_freep(&s->ts_buffer);
1826 92 s->ts_size = 0;
1827
1828 92 av_freep(&s->fdsp);
1829 92 ff_mdct_end(&s->imdct);
1830 92 }
1831