FFmpeg coverage


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