FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_core.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 904 1253 72.1%
Branches: 632 954 66.2%

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 #include "libavutil/channel_layout.h"
22 #include "dcaadpcm.h"
23 #include "dcadec.h"
24 #include "dcadata.h"
25 #include "dcahuff.h"
26 #include "dcamath.h"
27 #include "dca_syncwords.h"
28
29 #if ARCH_ARM
30 #include "arm/dca.h"
31 #endif
32
33 enum HeaderType {
34 HEADER_CORE,
35 HEADER_XCH,
36 HEADER_XXCH
37 };
38
39 static const int8_t prm_ch_to_spkr_map[DCA_AMODE_COUNT][5] = {
40 { DCA_SPEAKER_C, -1, -1, -1, -1 },
41 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
42 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
43 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
44 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
45 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
46 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
47 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
48 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
49 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
50 };
51
52 static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = {
53 DCA_SPEAKER_LAYOUT_MONO,
54 DCA_SPEAKER_LAYOUT_STEREO,
55 DCA_SPEAKER_LAYOUT_STEREO,
56 DCA_SPEAKER_LAYOUT_STEREO,
57 DCA_SPEAKER_LAYOUT_STEREO,
58 DCA_SPEAKER_LAYOUT_3_0,
59 DCA_SPEAKER_LAYOUT_2_1,
60 DCA_SPEAKER_LAYOUT_3_1,
61 DCA_SPEAKER_LAYOUT_2_2,
62 DCA_SPEAKER_LAYOUT_5POINT0
63 };
64
65 static const uint8_t block_code_nbits[7] = {
66 7, 10, 12, 13, 15, 17, 19
67 };
68
69 387206 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
70 {
71 387206 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
72 }
73
74 289302 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
75 {
76 int i;
77
78
2/2
✓ Branch 0 taken 2314416 times.
✓ Branch 1 taken 289302 times.
2603718 for (i = 0; i < size; i++)
79 2314416 array[i] = get_sbits(s, n);
80 289302 }
81
82 // 5.3.1 - Bit stream header
83 2464 static int parse_frame_header(DCACoreDecoder *s)
84 {
85 2464 DCACoreFrameHeader h = { 0 };
86 2464 int err = ff_dca_parse_core_frame_header(&h, &s->gb);
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
2464 if (err < 0) {
89 switch (err) {
90 case DCA_PARSE_ERROR_DEFICIT_SAMPLES:
91 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
92 return h.normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
93
94 case DCA_PARSE_ERROR_PCM_BLOCKS:
95 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", h.npcmblocks);
96 return (h.npcmblocks < 6 || h.normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
97
98 case DCA_PARSE_ERROR_FRAME_SIZE:
99 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", h.frame_size);
100 return AVERROR_INVALIDDATA;
101
102 case DCA_PARSE_ERROR_AMODE:
103 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode);
104 return AVERROR_PATCHWELCOME;
105
106 case DCA_PARSE_ERROR_SAMPLE_RATE:
107 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
108 return AVERROR_INVALIDDATA;
109
110 case DCA_PARSE_ERROR_RESERVED_BIT:
111 av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n");
112 return AVERROR_INVALIDDATA;
113
114 case DCA_PARSE_ERROR_LFE_FLAG:
115 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
116 return AVERROR_INVALIDDATA;
117
118 case DCA_PARSE_ERROR_PCM_RES:
119 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
120 return AVERROR_INVALIDDATA;
121
122 default:
123 av_log(s->avctx, AV_LOG_ERROR, "Unknown core frame header error\n");
124 return AVERROR_INVALIDDATA;
125 }
126 }
127
128 2464 s->crc_present = h.crc_present;
129 2464 s->npcmblocks = h.npcmblocks;
130 2464 s->frame_size = h.frame_size;
131 2464 s->audio_mode = h.audio_mode;
132 2464 s->sample_rate = avpriv_dca_sample_rates[h.sr_code];
133 2464 s->bit_rate = ff_dca_bit_rates[h.br_code];
134 2464 s->drc_present = h.drc_present;
135 2464 s->ts_present = h.ts_present;
136 2464 s->aux_present = h.aux_present;
137 2464 s->ext_audio_type = h.ext_audio_type;
138 2464 s->ext_audio_present = h.ext_audio_present;
139 2464 s->sync_ssf = h.sync_ssf;
140 2464 s->lfe_present = h.lfe_present;
141 2464 s->predictor_history = h.predictor_history;
142 2464 s->filter_perfect = h.filter_perfect;
143 2464 s->source_pcm_res = ff_dca_bits_per_sample[h.pcmr_code];
144 2464 s->es_format = h.pcmr_code & 1;
145 2464 s->sumdiff_front = h.sumdiff_front;
146 2464 s->sumdiff_surround = h.sumdiff_surround;
147
148 2464 return 0;
149 }
150
151 // 5.3.2 - Primary audio coding header
152 2764 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
153 {
154 2764 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
155 unsigned int mask, index;
156
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if (get_bits_left(&s->gb) < 0)
158 return AVERROR_INVALIDDATA;
159
160
3/4
✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 279 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
2764 switch (header) {
161 2464 case HEADER_CORE:
162 // Number of subframes
163 2464 s->nsubframes = get_bits(&s->gb, 4) + 1;
164
165 // Number of primary audio channels
166 2464 s->nchannels = get_bits(&s->gb, 3) + 1;
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
2464 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
168 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode);
169 return AVERROR_INVALIDDATA;
170 }
171 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
172
173 2464 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
174
175 // Add LFE channel if present
176
2/2
✓ Branch 0 taken 1946 times.
✓ Branch 1 taken 518 times.
2464 if (s->lfe_present)
177 1946 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
178 2464 break;
179
180 279 case HEADER_XCH:
181 279 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
182 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
183 279 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
184 279 break;
185
186 21 case HEADER_XXCH:
187 // Channel set header length
188 21 header_size = get_bits(&s->gb, 7) + 1;
189
190 // Check CRC
191
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 if (s->xxch_crc_present
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
193 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
194 return AVERROR_INVALIDDATA;
195 }
196
197 // Number of channels in a channel set
198 21 nchannels = get_bits(&s->gb, 3) + 1;
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
200 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
201 return AVERROR_PATCHWELCOME;
202 }
203 21 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
204 av_assert1(s->nchannels <= DCA_CHANNELS);
205
206 // Loudspeaker layout mask
207 21 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
208 21 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
209
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
211 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
212 return AVERROR_INVALIDDATA;
213 }
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (s->xxch_core_mask & s->xxch_spkr_mask) {
216 av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask);
217 return AVERROR_INVALIDDATA;
218 }
219
220 // Combine core and XXCH masks together
221 21 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
222
223 // Downmix coefficients present in stream
224
1/2
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
21 if (get_bits1(&s->gb)) {
225 21 int *coeff_ptr = s->xxch_dmix_coeff;
226
227 // Downmix already performed by encoder
228 21 s->xxch_dmix_embedded = get_bits1(&s->gb);
229
230 // Downmix scale factor
231 21 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
233 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
234 return AVERROR_INVALIDDATA;
235 }
236 21 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
237
238 // Downmix channel mapping mask
239
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (ch = 0; ch < nchannels; ch++) {
240 42 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if ((mask & s->xxch_core_mask) != mask) {
242 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
243 return AVERROR_INVALIDDATA;
244 }
245 42 s->xxch_dmix_mask[ch] = mask;
246 }
247
248 // Downmix coefficients
249
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (ch = 0; ch < nchannels; ch++) {
250
2/2
✓ Branch 0 taken 462 times.
✓ Branch 1 taken 42 times.
504 for (n = 0; n < s->xxch_mask_nbits; n++) {
251
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 420 times.
462 if (s->xxch_dmix_mask[ch] & (1U << n)) {
252 42 int code = get_bits(&s->gb, 7);
253 42 int sign = (code >> 6) - 1;
254
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (code &= 63) {
255 42 index = code * 4 - 3;
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (index >= FF_DCA_DMIXTABLE_SIZE) {
257 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
258 return AVERROR_INVALIDDATA;
259 }
260 42 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
261 } else {
262 *coeff_ptr++ = 0;
263 }
264 }
265 }
266 }
267 } else {
268 s->xxch_dmix_embedded = 0;
269 }
270
271 21 break;
272 }
273
274 // Subband activity count
275
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
276 11087 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
11087 if (s->nsubbands[ch] > DCA_SUBBANDS) {
278 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
279 return AVERROR_INVALIDDATA;
280 }
281 }
282
283 // High frequency VQ start subband
284
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++)
285 11087 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
286
287 // Joint intensity coding index
288
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
289
3/4
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 11073 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
11087 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
290 n += xch_base - 1;
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
11087 if (n > s->nchannels) {
292 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
293 return AVERROR_INVALIDDATA;
294 }
295 11087 s->joint_intensity_index[ch] = n;
296 }
297
298 // Transient mode code book
299
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++)
300 11087 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
301
302 // Scale factor code book
303
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
304 11087 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
11087 if (s->scale_factor_sel[ch] == 7) {
306 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
307 return AVERROR_INVALIDDATA;
308 }
309 }
310
311 // Bit allocation quantizer select
312
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
313 11087 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11087 times.
11087 if (s->bit_allocation_sel[ch] == 7) {
315 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
316 return AVERROR_INVALIDDATA;
317 }
318 }
319
320 // Quantization index codebook select
321
2/2
✓ Branch 0 taken 27640 times.
✓ Branch 1 taken 2764 times.
30404 for (n = 0; n < DCA_CODE_BOOKS; n++)
322
2/2
✓ Branch 0 taken 110870 times.
✓ Branch 1 taken 27640 times.
138510 for (ch = xch_base; ch < s->nchannels; ch++)
323 110870 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
324
325 // Scale factor adjustment index
326
2/2
✓ Branch 0 taken 27640 times.
✓ Branch 1 taken 2764 times.
30404 for (n = 0; n < DCA_CODE_BOOKS; n++)
327
2/2
✓ Branch 0 taken 110870 times.
✓ Branch 1 taken 27640 times.
138510 for (ch = xch_base; ch < s->nchannels; ch++)
328
2/2
✓ Branch 0 taken 2922 times.
✓ Branch 1 taken 107948 times.
110870 if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n])
329 2922 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
330
331
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2743 times.
2764 if (header == HEADER_XXCH) {
332 // Reserved
333 // Byte align
334 // CRC16 of channel set header
335
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
336 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
337 return AVERROR_INVALIDDATA;
338 }
339 } else {
340 // Audio header CRC check word
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2743 times.
2743 if (s->crc_present)
342 skip_bits(&s->gb, 16);
343 }
344
345 2764 return 0;
346 }
347
348 360692 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
349 {
350 const uint32_t *scale_table;
351 unsigned int scale_size;
352
353 // Select the root square table
354
2/2
✓ Branch 0 taken 290350 times.
✓ Branch 1 taken 70342 times.
360692 if (sel > 5) {
355 290350 scale_table = ff_dca_scale_factor_quant7;
356 290350 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
357 } else {
358 70342 scale_table = ff_dca_scale_factor_quant6;
359 70342 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
360 }
361
362 // If Huffman code was used, the difference of scales was encoded
363
2/2
✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 351557 times.
360692 if (sel < 5)
364 9135 *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
365 else
366 351557 *scale_index = get_bits(&s->gb, sel + 1);
367
368 // Look up scale factor from the root square table
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360692 times.
360692 if ((unsigned int)*scale_index >= scale_size) {
370 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
371 return AVERROR_INVALIDDATA;
372 }
373
374 360692 return scale_table[*scale_index];
375 }
376
377 2541 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
378 {
379 int scale_index;
380
381 // Absolute value was encoded even when Huffman code was used
382
1/2
✓ Branch 0 taken 2541 times.
✗ Branch 1 not taken.
2541 if (sel < 5)
383 2541 scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
384 else
385 scale_index = get_bits(&s->gb, sel + 1);
386
387 // Bias by 64
388 2541 scale_index += 64;
389
390 // Look up joint scale factor
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2541 times.
2541 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
392 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
393 return AVERROR_INVALIDDATA;
394 }
395
396 2541 return ff_dca_joint_scale_factors[scale_index];
397 }
398
399 // 5.4.1 - Primary audio coding side information
400 2764 static int parse_subframe_header(DCACoreDecoder *s, int sf,
401 enum HeaderType header, int xch_base)
402 {
403 int ch, band, ret;
404
405
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if (get_bits_left(&s->gb) < 0)
406 return AVERROR_INVALIDDATA;
407
408
2/2
✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 300 times.
2764 if (header == HEADER_CORE) {
409 // Subsubframe count
410 2464 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
411
412 // Partial subsubframe sample count
413 2464 skip_bits(&s->gb, 3);
414 }
415
416 // Prediction mode
417
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++)
418
2/2
✓ Branch 0 taken 341543 times.
✓ Branch 1 taken 11087 times.
352630 for (band = 0; band < s->nsubbands[ch]; band++)
419 341543 s->prediction_mode[ch][band] = get_bits1(&s->gb);
420
421 // Prediction coefficients VQ address
422
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++)
423
2/2
✓ Branch 0 taken 341543 times.
✓ Branch 1 taken 11087 times.
352630 for (band = 0; band < s->nsubbands[ch]; band++)
424
2/2
✓ Branch 0 taken 24484 times.
✓ Branch 1 taken 317059 times.
341543 if (s->prediction_mode[ch][band])
425 24484 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
426
427 // Bit allocation index
428
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
429 11087 int sel = s->bit_allocation_sel[ch];
430
431
2/2
✓ Branch 0 taken 298239 times.
✓ Branch 1 taken 11087 times.
309326 for (band = 0; band < s->subband_vq_start[ch]; band++) {
432 int abits;
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 298239 times.
298239 if (sel < 5)
435 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
436 else
437 298239 abits = get_bits(&s->gb, sel - 1);
438
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 298239 times.
298239 if (abits > DCA_ABITS_MAX) {
440 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
441 return AVERROR_INVALIDDATA;
442 }
443
444 298239 s->bit_allocation[ch][band] = abits;
445 }
446 }
447
448 // Transition mode
449
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
450 // Clear transition mode for all subbands
451 11087 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
452
453 // Transient possible only if more than one subsubframe
454
1/2
✓ Branch 0 taken 11087 times.
✗ Branch 1 not taken.
11087 if (s->nsubsubframes[sf] > 1) {
455 11087 int sel = s->transition_mode_sel[ch];
456
2/2
✓ Branch 0 taken 298239 times.
✓ Branch 1 taken 11087 times.
309326 for (band = 0; band < s->subband_vq_start[ch]; band++)
457
1/2
✓ Branch 0 taken 298239 times.
✗ Branch 1 not taken.
298239 if (s->bit_allocation[ch][band])
458 298239 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
459 }
460 }
461
462 // Scale factors
463
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
464 11087 int sel = s->scale_factor_sel[ch];
465 11087 int scale_index = 0;
466
467 // Extract scales for subbands up to VQ
468
2/2
✓ Branch 0 taken 298239 times.
✓ Branch 1 taken 11087 times.
309326 for (band = 0; band < s->subband_vq_start[ch]; band++) {
469
1/2
✓ Branch 0 taken 298239 times.
✗ Branch 1 not taken.
298239 if (s->bit_allocation[ch][band]) {
470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 298239 times.
298239 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
471 return ret;
472 298239 s->scale_factors[ch][band][0] = ret;
473
2/2
✓ Branch 0 taken 10014 times.
✓ Branch 1 taken 288225 times.
298239 if (s->transition_mode[sf][ch][band]) {
474
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10014 times.
10014 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
475 return ret;
476 10014 s->scale_factors[ch][band][1] = ret;
477 }
478 } else {
479 s->scale_factors[ch][band][0] = 0;
480 }
481 }
482
483 // High frequency VQ subbands
484
2/2
✓ Branch 0 taken 43304 times.
✓ Branch 1 taken 11087 times.
54391 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
485
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43304 times.
43304 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
486 return ret;
487 43304 s->scale_factors[ch][band][0] = ret;
488 }
489 }
490
491 // Joint subband codebook select
492
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
493
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
11087 if (s->joint_intensity_index[ch]) {
494 14 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (s->joint_scale_sel[ch] == 7) {
496 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
497 return AVERROR_INVALIDDATA;
498 }
499 }
500 }
501
502 // Scale factors for joint subband coding
503
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
504 11087 int src_ch = s->joint_intensity_index[ch] - 1;
505
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
11087 if (src_ch >= 0) {
506 14 int sel = s->joint_scale_sel[ch];
507
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 14 times.
91 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
508
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if ((ret = parse_joint_scale(s, sel)) < 0)
509 return ret;
510 77 s->joint_scale_factors[ch][band] = ret;
511 }
512 }
513 }
514
515 // Dynamic range coefficient
516
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2764 if (s->drc_present && header == HEADER_CORE)
517 skip_bits(&s->gb, 8);
518
519 // Side information CRC check word
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
2764 if (s->crc_present)
521 skip_bits(&s->gb, 16);
522
523 2764 return 0;
524 }
525
526 #ifndef decode_blockcodes
527 318078 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
528 {
529 318078 int offset = (levels - 1) / 2;
530 int n, div;
531
532
2/2
✓ Branch 0 taken 1272312 times.
✓ Branch 1 taken 318078 times.
1590390 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
533 1272312 div = FASTDIV(code1, levels);
534 1272312 audio[n] = code1 - div * levels - offset;
535 1272312 code1 = div;
536 }
537
2/2
✓ Branch 0 taken 1272312 times.
✓ Branch 1 taken 318078 times.
1590390 for (; n < DCA_SUBBAND_SAMPLES; n++) {
538 1272312 div = FASTDIV(code2, levels);
539 1272312 audio[n] = code2 - div * levels - offset;
540 1272312 code2 = div;
541 }
542
543 318078 return code1 | code2;
544 }
545 #endif
546
547 318078 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
548 {
549 // Extract block code indices from the bit stream
550 318078 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
551 318078 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
552 318078 int levels = ff_dca_quant_levels[abits];
553
554 // Look up samples from the block code book
555
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 318078 times.
318078 if (decode_blockcodes(code1, code2, levels, audio)) {
556 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
557 return AVERROR_INVALIDDATA;
558 }
559
560 318078 return 0;
561 }
562
563 8522 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
564 {
565 int i;
566
567 // Extract Huffman codes from the bit stream
568
2/2
✓ Branch 0 taken 68176 times.
✓ Branch 1 taken 8522 times.
76698 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
569 68176 audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
570
571 8522 return 1;
572 }
573
574 609294 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
575 {
576 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
577
578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 609294 times.
609294 if (abits == 0) {
579 // No bits allocated
580 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
581 return 0;
582 }
583
584
2/2
✓ Branch 0 taken 444098 times.
✓ Branch 1 taken 165196 times.
609294 if (abits <= DCA_CODE_BOOKS) {
585 444098 int sel = s->quant_index_sel[ch][abits - 1];
586
2/2
✓ Branch 0 taken 8522 times.
✓ Branch 1 taken 435576 times.
444098 if (sel < ff_dca_quant_index_group_size[abits - 1]) {
587 // Huffman codes
588 8522 return parse_huffman_codes(s, audio, abits, sel);
589 }
590
2/2
✓ Branch 0 taken 316984 times.
✓ Branch 1 taken 118592 times.
435576 if (abits <= 7) {
591 // Block codes
592 316984 return parse_block_codes(s, audio, abits);
593 }
594 }
595
596 // No further encoding
597 283788 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
598 283788 return 0;
599 }
600
601 11283 static inline void inverse_adpcm(int32_t **subband_samples,
602 const int16_t *vq_index,
603 const int8_t *prediction_mode,
604 int sb_start, int sb_end,
605 int ofs, int len)
606 {
607 int i, j;
608
609
2/2
✓ Branch 0 taken 350678 times.
✓ Branch 1 taken 11283 times.
361961 for (i = sb_start; i < sb_end; i++) {
610
2/2
✓ Branch 0 taken 24531 times.
✓ Branch 1 taken 326147 times.
350678 if (prediction_mode[i]) {
611 24531 const int pred_id = vq_index[i];
612 24531 int32_t *ptr = subband_samples[i] + ofs;
613
2/2
✓ Branch 0 taken 392496 times.
✓ Branch 1 taken 24531 times.
417027 for (j = 0; j < len; j++) {
614 392496 int32_t x = ff_dcaadpcm_predict(pred_id, ptr + j - DCA_ADPCM_COEFFS);
615 392496 ptr[j] = clip23(ptr[j] + x);
616 }
617 }
618 }
619 11283 }
620
621 // 5.5 - Primary audio data arrays
622 2764 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
623 int xch_base, int *sub_pos, int *lfe_pos)
624 {
625 int32_t audio[16], scale;
626 int n, ssf, ofs, ch, band;
627
628 // Check number of subband samples in this subframe
629 2764 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
2764 if (*sub_pos + nsamples > s->npcmblocks) {
631 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
632 return AVERROR_INVALIDDATA;
633 }
634
635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if (get_bits_left(&s->gb) < 0)
636 return AVERROR_INVALIDDATA;
637
638 // VQ encoded subbands
639
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
640 int32_t vq_index[DCA_SUBBANDS];
641
642
2/2
✓ Branch 0 taken 43304 times.
✓ Branch 1 taken 11087 times.
54391 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
643 // Extract the VQ address from the bit stream
644 43304 vq_index[band] = get_bits(&s->gb, 10);
645
646
2/2
✓ Branch 0 taken 9736 times.
✓ Branch 1 taken 1351 times.
11087 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
647 9736 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
648 9736 ff_dca_high_freq_vq, s->scale_factors[ch],
649 9736 s->subband_vq_start[ch], s->nsubbands[ch],
650 9736 *sub_pos, nsamples);
651 }
652 }
653
654 // Low frequency effect data
655
4/4
✓ Branch 0 taken 2246 times.
✓ Branch 1 taken 518 times.
✓ Branch 2 taken 1946 times.
✓ Branch 3 taken 300 times.
2764 if (s->lfe_present && header == HEADER_CORE) {
656 unsigned int index;
657
658 // Determine number of LFE samples in this subframe
659 1946 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
660 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
661
662 // Extract LFE samples from the bit stream
663 1946 get_array(&s->gb, audio, nlfesamples, 8);
664
665 // Extract scale factor index from the bit stream
666 1946 index = get_bits(&s->gb, 8);
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1946 times.
1946 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
668 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
669 return AVERROR_INVALIDDATA;
670 }
671
672 // Look up the 7-bit root square quantization table
673 1946 scale = ff_dca_scale_factor_quant7[index];
674
675 // Account for quantizer step size which is 0.035
676 1946 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
677
678 // Scale and take the LFE samples
679
2/2
✓ Branch 0 taken 15568 times.
✓ Branch 1 taken 1946 times.
17514 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
680 15568 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
681
682 // Advance LFE sample pointer for the next subframe
683 1946 *lfe_pos = ofs;
684 }
685
686 // Audio data
687
2/2
✓ Branch 0 taken 5528 times.
✓ Branch 1 taken 2764 times.
8292 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
688
2/2
✓ Branch 0 taken 22174 times.
✓ Branch 1 taken 5528 times.
27702 for (ch = xch_base; ch < s->nchannels; ch++) {
689
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22174 times.
22174 if (get_bits_left(&s->gb) < 0)
690 return AVERROR_INVALIDDATA;
691
692 // Not high frequency VQ subbands
693
2/2
✓ Branch 0 taken 596478 times.
✓ Branch 1 taken 22174 times.
618652 for (band = 0; band < s->subband_vq_start[ch]; band++) {
694 596478 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
695 int32_t step_size;
696
697 // Extract bits from the bit stream
698
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 596478 times.
596478 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
699 return ret;
700
701 // Select quantization step size table and look up
702 // quantization step size
703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 596478 times.
596478 if (s->bit_rate == 3)
704 step_size = ff_dca_lossless_quant[abits];
705 else
706 596478 step_size = ff_dca_lossy_quant[abits];
707
708 // Identify transient location
709 596478 trans_ssf = s->transition_mode[sf][ch][band];
710
711 // Determine proper scale factor
712
4/4
✓ Branch 0 taken 20028 times.
✓ Branch 1 taken 576450 times.
✓ Branch 2 taken 10014 times.
✓ Branch 3 taken 10014 times.
596478 if (trans_ssf == 0 || ssf < trans_ssf)
713 586464 scale = s->scale_factors[ch][band][0];
714 else
715 10014 scale = s->scale_factors[ch][band][1];
716
717 // Adjust scale factor when SEL indicates Huffman code
718
2/2
✓ Branch 0 taken 8054 times.
✓ Branch 1 taken 588424 times.
596478 if (ret > 0) {
719 8054 int64_t adj = s->scale_factor_adj[ch][abits - 1];
720 8054 scale = clip23(adj * scale >> 22);
721 }
722
723 596478 ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
724 audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
725 }
726 }
727
728 // DSYNC
729
5/6
✓ Branch 0 taken 2764 times.
✓ Branch 1 taken 2764 times.
✓ Branch 2 taken 2050 times.
✓ Branch 3 taken 714 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4814 times.
5528 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
730 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
731 return AVERROR_INVALIDDATA;
732 }
733
734 5528 ofs += DCA_SUBBAND_SAMPLES;
735 }
736
737 // Inverse ADPCM
738
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
739 11087 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
740 11087 s->prediction_mode[ch], 0, s->nsubbands[ch],
741 *sub_pos, nsamples);
742 }
743
744 // Joint subband coding
745
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
746 11087 int src_ch = s->joint_intensity_index[ch] - 1;
747
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
11087 if (src_ch >= 0) {
748 14 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
749 14 s->joint_scale_factors[ch], s->nsubbands[ch],
750 14 s->nsubbands[src_ch], *sub_pos, nsamples);
751 }
752 }
753
754 // Advance subband sample pointer for the next subframe
755 2764 *sub_pos = ofs;
756 2764 return 0;
757 }
758
759 static void erase_adpcm_history(DCACoreDecoder *s)
760 {
761 int ch, band;
762
763 // Erase ADPCM history from previous frame if
764 // predictor history switch was disabled
765 for (ch = 0; ch < DCA_CHANNELS; ch++)
766 for (band = 0; band < DCA_SUBBANDS; band++)
767 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
768
769 emms_c();
770 }
771
772 2464 static int alloc_sample_buffer(DCACoreDecoder *s)
773 {
774 2464 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
775 2464 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
776 2464 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
777 2464 unsigned int size = s->subband_size;
778 int ch, band;
779
780 // Reallocate subband sample buffer
781 2464 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
782 2464 (nframesamples + nlfesamples) * sizeof(int32_t));
783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
2464 if (!s->subband_buffer)
784 return AVERROR(ENOMEM);
785
786
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 2378 times.
2464 if (size != s->subband_size) {
787
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 86 times.
688 for (ch = 0; ch < DCA_CHANNELS; ch++)
788
2/2
✓ Branch 0 taken 19264 times.
✓ Branch 1 taken 602 times.
19866 for (band = 0; band < DCA_SUBBANDS; band++)
789 19264 s->subband_samples[ch][band] = s->subband_buffer +
790 19264 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
791 86 s->lfe_samples = s->subband_buffer + nframesamples;
792 }
793
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
2464 if (!s->predictor_history)
795 erase_adpcm_history(s);
796
797 2464 return 0;
798 }
799
800 2764 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
801 {
802 int sf, ch, ret, band, sub_pos, lfe_pos;
803
804
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
805 return ret;
806
807
2/2
✓ Branch 0 taken 2764 times.
✓ Branch 1 taken 2764 times.
5528 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
808
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
809 return ret;
810
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
811 return ret;
812 }
813
814
2/2
✓ Branch 0 taken 11087 times.
✓ Branch 1 taken 2764 times.
13851 for (ch = xch_base; ch < s->nchannels; ch++) {
815 // Determine number of active subbands for this channel
816 11087 int nsubbands = s->nsubbands[ch];
817
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 11073 times.
11087 if (s->joint_intensity_index[ch])
818 14 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
819
820 // Update history for ADPCM
821
2/2
✓ Branch 0 taken 341620 times.
✓ Branch 1 taken 11087 times.
352707 for (band = 0; band < nsubbands; band++) {
822 341620 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
823 341620 AV_COPY128(samples, samples + s->npcmblocks);
824 }
825
826 // Clear inactive subbands
827
2/2
✓ Branch 0 taken 13164 times.
✓ Branch 1 taken 11087 times.
24251 for (; band < DCA_SUBBANDS; band++) {
828 13164 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
829 13164 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
830 }
831 }
832
833 2764 emms_c();
834
835 2764 return 0;
836 }
837
838 279 static int parse_xch_frame(DCACoreDecoder *s)
839 {
840 int ret;
841
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
279 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
843 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
844 return AVERROR_INVALIDDATA;
845 }
846
847
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
279 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
848 return ret;
849
850 // Seek to the end of core frame, don't trust XCH frame size
851
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 279 times.
279 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
852 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
853 return AVERROR_INVALIDDATA;
854 }
855
856 279 return 0;
857 }
858
859 21 static int parse_xxch_frame(DCACoreDecoder *s)
860 {
861 int xxch_nchsets, xxch_frame_size;
862 21 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
863
864 // XXCH sync word
865
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
866 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
867 return AVERROR_INVALIDDATA;
868 }
869
870 // XXCH frame header length
871 21 header_size = get_bits(&s->gb, 6) + 1;
872
873 // Check XXCH frame header CRC
874
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
875 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
876 return AVERROR_INVALIDDATA;
877 }
878
879 // CRC presence flag for channel set header
880 21 s->xxch_crc_present = get_bits1(&s->gb);
881
882 // Number of bits for loudspeaker mask
883 21 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
885 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
886 return AVERROR_INVALIDDATA;
887 }
888
889 // Number of channel sets
890 21 xxch_nchsets = get_bits(&s->gb, 2) + 1;
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (xxch_nchsets > 1) {
892 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
893 return AVERROR_PATCHWELCOME;
894 }
895
896 // Channel set 0 data byte size
897 21 xxch_frame_size = get_bits(&s->gb, 14) + 1;
898
899 // Core loudspeaker activity mask
900 21 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
901
902 // Validate the core mask
903 21 mask = s->ch_mask;
904
905
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
906 21 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
907
908
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
909 21 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (mask != s->xxch_core_mask) {
912 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
913 return AVERROR_INVALIDDATA;
914 }
915
916 // Reserved
917 // Byte align
918 // CRC16 of XXCH frame header
919
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
920 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
921 return AVERROR_INVALIDDATA;
922 }
923
924 // Parse XXCH channel set 0
925
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
926 return ret;
927
928
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
929 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
930 return AVERROR_INVALIDDATA;
931 }
932
933 21 return 0;
934 }
935
936 28 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
937 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
938 {
939 int xbr_nabits[DCA_CHANNELS];
940 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
941 int xbr_scale_nbits[DCA_CHANNELS];
942 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
943 int ssf, ch, band, ofs;
944
945 // Check number of subband samples in this subframe
946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
947 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
948 return AVERROR_INVALIDDATA;
949 }
950
951
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (get_bits_left(&s->gb) < 0)
952 return AVERROR_INVALIDDATA;
953
954 // Number of bits for XBR bit allocation index
955
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
140 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
956 112 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
957
958 // XBR bit allocation index
959
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
140 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
960
2/2
✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 112 times.
3696 for (band = 0; band < xbr_nsubbands[ch]; band++) {
961 3584 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
963 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
964 return AVERROR_INVALIDDATA;
965 }
966 }
967 }
968
969 // Number of bits for scale indices
970
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
140 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
971 112 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (!xbr_scale_nbits[ch]) {
973 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
974 return AVERROR_INVALIDDATA;
975 }
976 }
977
978 // XBR scale factors
979
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
140 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
980 const uint32_t *scale_table;
981 int scale_size;
982
983 // Select the root square table
984
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 if (s->scale_factor_sel[ch] > 5) {
985 112 scale_table = ff_dca_scale_factor_quant7;
986 112 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
987 } else {
988 scale_table = ff_dca_scale_factor_quant6;
989 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
990 }
991
992 // Parse scale factor indices and look up scale factors from the root
993 // square table
994
2/2
✓ Branch 0 taken 3584 times.
✓ Branch 1 taken 112 times.
3696 for (band = 0; band < xbr_nsubbands[ch]; band++) {
995
2/2
✓ Branch 0 taken 2331 times.
✓ Branch 1 taken 1253 times.
3584 if (xbr_bit_allocation[ch][band]) {
996 2331 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2331 times.
2331 if (scale_index >= scale_size) {
998 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
999 return AVERROR_INVALIDDATA;
1000 }
1001 2331 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1002
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2331 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2331 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1003 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1004 if (scale_index >= scale_size) {
1005 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1006 return AVERROR_INVALIDDATA;
1007 }
1008 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1009 }
1010 }
1011 }
1012 }
1013
1014 // Audio data
1015
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
84 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1016
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 56 times.
280 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1017
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
224 if (get_bits_left(&s->gb) < 0)
1018 return AVERROR_INVALIDDATA;
1019
1020
2/2
✓ Branch 0 taken 7168 times.
✓ Branch 1 taken 224 times.
7392 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1021 7168 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1022 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1023
1024 // Extract bits from the bit stream
1025
2/2
✓ Branch 0 taken 3568 times.
✓ Branch 1 taken 3600 times.
7168 if (abits > 7) {
1026 // No further encoding
1027 3568 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1028
2/2
✓ Branch 0 taken 1094 times.
✓ Branch 1 taken 2506 times.
3600 } else if (abits > 0) {
1029 // Block codes
1030
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1094 times.
1094 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1031 return ret;
1032 } else {
1033 // No bits allocated
1034 2506 continue;
1035 }
1036
1037 // Look up quantization step size
1038 4662 step_size = ff_dca_lossless_quant[abits];
1039
1040 // Identify transient location
1041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4662 times.
4662 if (xbr_transition_mode)
1042 trans_ssf = s->transition_mode[sf][ch][band];
1043 else
1044 4662 trans_ssf = 0;
1045
1046 // Determine proper scale factor
1047
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4662 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4662 if (trans_ssf == 0 || ssf < trans_ssf)
1048 4662 scale = xbr_scale_factors[ch][band][0];
1049 else
1050 scale = xbr_scale_factors[ch][band][1];
1051
1052 4662 ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
1053 audio, step_size, scale, 1, DCA_SUBBAND_SAMPLES);
1054 }
1055 }
1056
1057 // DSYNC
1058
4/6
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 56 times.
56 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1059 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1060 return AVERROR_INVALIDDATA;
1061 }
1062
1063 56 ofs += DCA_SUBBAND_SAMPLES;
1064 }
1065
1066 // Advance subband sample pointer for the next subframe
1067 28 *sub_pos = ofs;
1068 28 return 0;
1069 }
1070
1071 21 static int parse_xbr_frame(DCACoreDecoder *s)
1072 {
1073 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1074 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1075 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1076 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1077 21 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1078
1079 // XBR sync word
1080
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1081 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1082 return AVERROR_INVALIDDATA;
1083 }
1084
1085 // XBR frame header length
1086 21 header_size = get_bits(&s->gb, 6) + 1;
1087
1088 // Check XBR frame header CRC
1089
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1090 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1091 return AVERROR_INVALIDDATA;
1092 }
1093
1094 // Number of channel sets
1095 21 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1096
1097 // Channel set data byte size
1098
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
49 for (i = 0; i < xbr_nchsets; i++)
1099 28 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1100
1101 // Transition mode flag
1102 21 xbr_transition_mode = get_bits1(&s->gb);
1103
1104 // Channel set headers
1105
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
49 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1106 28 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1107 28 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1108
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
140 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1109 112 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1111 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1112 return AVERROR_INVALIDDATA;
1113 }
1114 }
1115 }
1116
1117 // Reserved
1118 // Byte align
1119 // CRC16 of XBR frame header
1120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1121 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1122 return AVERROR_INVALIDDATA;
1123 }
1124
1125 // Channel set data
1126
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
49 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1127 28 header_pos = get_bits_count(&s->gb);
1128
1129
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1130 int sf, sub_pos;
1131
1132
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1134 28 xbr_base_ch + xbr_nchannels[i],
1135 xbr_nsubbands, xbr_transition_mode,
1136 sf, &sub_pos)) < 0)
1137 return ret;
1138 }
1139 }
1140
1141 28 xbr_base_ch += xbr_nchannels[i];
1142
1143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1144 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1145 return AVERROR_INVALIDDATA;
1146 }
1147 }
1148
1149 21 return 0;
1150 }
1151
1152 // Modified ISO/IEC 9899 linear congruential generator
1153 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1154 static int rand_x96(DCACoreDecoder *s)
1155 {
1156 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1157 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1158 }
1159
1160 49 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1161 {
1162 int n, ssf, ch, band, ofs;
1163
1164 // Check number of subband samples in this subframe
1165 49 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (*sub_pos + nsamples > s->npcmblocks) {
1167 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1168 return AVERROR_INVALIDDATA;
1169 }
1170
1171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if (get_bits_left(&s->gb) < 0)
1172 return AVERROR_INVALIDDATA;
1173
1174 // VQ encoded or unallocated subbands
1175
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1176
2/2
✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
9331 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1177 // Get the sample pointer and scale factor
1178 9135 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1179 9135 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1180
1181
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2727 times.
✓ Branch 2 taken 6408 times.
9135 switch (s->bit_allocation[ch][band]) {
1182 case 0: // No bits allocated for subband
1183 if (scale <= 1)
1184 memset(samples, 0, nsamples * sizeof(int32_t));
1185 else for (n = 0; n < nsamples; n++)
1186 // Generate scaled random samples
1187 samples[n] = mul31(rand_x96(s), scale);
1188 break;
1189
1190 2727 case 1: // VQ encoded subband
1191
2/2
✓ Branch 0 taken 2727 times.
✓ Branch 1 taken 2727 times.
5454 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1192 // Extract the VQ address from the bit stream and look up
1193 // the VQ code book for up to 16 subband samples
1194 2727 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1195 // Scale and take the samples
1196
2/2
✓ Branch 0 taken 43632 times.
✓ Branch 1 taken 2727 times.
46359 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1197 43632 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1198 }
1199 2727 break;
1200 }
1201 }
1202 }
1203
1204 // Audio data
1205
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 49 times.
147 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1206
2/2
✓ Branch 0 taken 392 times.
✓ Branch 1 taken 98 times.
490 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 392 times.
392 if (get_bits_left(&s->gb) < 0)
1208 return AVERROR_INVALIDDATA;
1209
1210
2/2
✓ Branch 0 taken 18270 times.
✓ Branch 1 taken 392 times.
18662 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1211 18270 int ret, abits = s->bit_allocation[ch][band] - 1;
1212 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1213
1214 // Not VQ encoded or unallocated subbands
1215
2/2
✓ Branch 0 taken 5454 times.
✓ Branch 1 taken 12816 times.
18270 if (abits < 1)
1216 5454 continue;
1217
1218 // Extract bits from the bit stream
1219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12816 times.
12816 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1220 return ret;
1221
1222 // Select quantization step size table and look up quantization
1223 // step size
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12816 times.
12816 if (s->bit_rate == 3)
1225 step_size = ff_dca_lossless_quant[abits];
1226 else
1227 12816 step_size = ff_dca_lossy_quant[abits];
1228
1229 // Get the scale factor
1230 12816 scale = s->scale_factors[ch][band >> 1][band & 1];
1231
1232 12816 ff_dca_core_dequantize(s->x96_subband_samples[ch][band] + ofs,
1233 audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
1234 }
1235 }
1236
1237 // DSYNC
1238
4/6
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 98 times.
98 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1239 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1240 return AVERROR_INVALIDDATA;
1241 }
1242
1243 98 ofs += DCA_SUBBAND_SAMPLES;
1244 }
1245
1246 // Inverse ADPCM
1247
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1248 196 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1249 196 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1250 *sub_pos, nsamples);
1251 }
1252
1253 // Joint subband coding
1254
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1255 196 int src_ch = s->joint_intensity_index[ch] - 1;
1256
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
196 if (src_ch >= 0) {
1257 77 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1258 77 s->joint_scale_factors[ch], s->nsubbands[ch],
1259 77 s->nsubbands[src_ch], *sub_pos, nsamples);
1260 }
1261 }
1262
1263 // Advance subband sample pointer for the next subframe
1264 49 *sub_pos = ofs;
1265 49 return 0;
1266 }
1267
1268 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1269 {
1270 int ch, band;
1271
1272 // Erase ADPCM history from previous frame if
1273 // predictor history switch was disabled
1274 for (ch = 0; ch < DCA_CHANNELS; ch++)
1275 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1276 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1277
1278 emms_c();
1279 }
1280
1281 35 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1282 {
1283 35 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1284 35 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1285 35 unsigned int size = s->x96_subband_size;
1286 int ch, band;
1287
1288 // Reallocate subband sample buffer
1289 35 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1290 nframesamples * sizeof(int32_t));
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!s->x96_subband_buffer)
1292 return AVERROR(ENOMEM);
1293
1294
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 25 times.
35 if (size != s->x96_subband_size) {
1295
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 10 times.
80 for (ch = 0; ch < DCA_CHANNELS; ch++)
1296
2/2
✓ Branch 0 taken 4480 times.
✓ Branch 1 taken 70 times.
4550 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1297 4480 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1298 4480 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1299 }
1300
1301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!s->predictor_history)
1302 erase_x96_adpcm_history(s);
1303
1304 35 return 0;
1305 }
1306
1307 49 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1308 {
1309 int ch, band, ret;
1310
1311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if (get_bits_left(&s->gb) < 0)
1312 return AVERROR_INVALIDDATA;
1313
1314 // Prediction mode
1315
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1316
2/2
✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
9331 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1317 9135 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1318
1319 // Prediction coefficients VQ address
1320
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1321
2/2
✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
9331 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1322
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 9088 times.
9135 if (s->prediction_mode[ch][band])
1323 47 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1324
1325 // Bit allocation index
1326
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1327 196 int sel = s->bit_allocation_sel[ch];
1328 196 int abits = 0;
1329
1330
2/2
✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
9331 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1331 // If Huffman code was used, the difference of abits was encoded
1332
2/2
✓ Branch 0 taken 9115 times.
✓ Branch 1 taken 20 times.
9135 if (sel < 7)
1333 9115 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1334 else
1335 20 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1336
1337
2/4
✓ Branch 0 taken 9135 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9135 times.
9135 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1338 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1339 return AVERROR_INVALIDDATA;
1340 }
1341
1342 9135 s->bit_allocation[ch][band] = abits;
1343 }
1344 }
1345
1346 // Scale factors
1347
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1348 196 int sel = s->scale_factor_sel[ch];
1349 196 int scale_index = 0;
1350
1351 // Extract scales for subbands which are transmitted even for
1352 // unallocated subbands
1353
2/2
✓ Branch 0 taken 9135 times.
✓ Branch 1 taken 196 times.
9331 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9135 times.
9135 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1355 return ret;
1356 9135 s->scale_factors[ch][band >> 1][band & 1] = ret;
1357 }
1358 }
1359
1360 // Joint subband codebook select
1361
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1362
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
196 if (s->joint_intensity_index[ch]) {
1363 77 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (s->joint_scale_sel[ch] == 7) {
1365 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1366 return AVERROR_INVALIDDATA;
1367 }
1368 }
1369 }
1370
1371 // Scale factors for joint subband coding
1372
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1373 196 int src_ch = s->joint_intensity_index[ch] - 1;
1374
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
196 if (src_ch >= 0) {
1375 77 int sel = s->joint_scale_sel[ch];
1376
2/2
✓ Branch 0 taken 2464 times.
✓ Branch 1 taken 77 times.
2541 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1377
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = parse_joint_scale(s, sel)) < 0)
1378 return ret;
1379 2464 s->joint_scale_factors[ch][band] = ret;
1380 }
1381 }
1382 }
1383
1384 // Side information CRC check word
1385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (s->crc_present)
1386 skip_bits(&s->gb, 16);
1387
1388 49 return 0;
1389 }
1390
1391 49 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1392 {
1393 49 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1394
1395
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if (get_bits_left(&s->gb) < 0)
1396 return AVERROR_INVALIDDATA;
1397
1398
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
49 if (exss) {
1399 // Channel set header length
1400 42 header_size = get_bits(&s->gb, 7) + 1;
1401
1402 // Check CRC
1403
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (s->x96_crc_present
1404
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1405 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1406 return AVERROR_INVALIDDATA;
1407 }
1408 }
1409
1410 // High resolution flag
1411 49 s->x96_high_res = get_bits1(&s->gb);
1412
1413 // First encoded subband
1414
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (s->x96_rev_no < 8) {
1415 49 s->x96_subband_start = get_bits(&s->gb, 5);
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (s->x96_subband_start > 27) {
1417 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1418 return AVERROR_INVALIDDATA;
1419 }
1420 } else {
1421 s->x96_subband_start = DCA_SUBBANDS;
1422 }
1423
1424 // Subband activity count
1425
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1426 196 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1428 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1429 return AVERROR_INVALIDDATA;
1430 }
1431 }
1432
1433 // Joint intensity coding index
1434
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1435
4/4
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 119 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 70 times.
196 if ((n = get_bits(&s->gb, 3)) && xch_base)
1436 7 n += xch_base - 1;
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (n > s->x96_nchannels) {
1438 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1439 return AVERROR_INVALIDDATA;
1440 }
1441 196 s->joint_intensity_index[ch] = n;
1442 }
1443
1444 // Scale factor code book
1445
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1446 196 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (s->scale_factor_sel[ch] >= 6) {
1448 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1449 return AVERROR_INVALIDDATA;
1450 }
1451 }
1452
1453 // Bit allocation quantizer select
1454
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1455 196 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1456
1457 // Quantization index codebook select
1458
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 49 times.
399 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1459
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 350 times.
1694 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1460 1344 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
1461
1462
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
49 if (exss) {
1463 // Reserved
1464 // Byte align
1465 // CRC16 of channel set header
1466
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1467 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1468 return AVERROR_INVALIDDATA;
1469 }
1470 } else {
1471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (s->crc_present)
1472 skip_bits(&s->gb, 16);
1473 }
1474
1475 49 return 0;
1476 }
1477
1478 49 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1479 {
1480 int sf, ch, ret, band, sub_pos;
1481
1482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1483 return ret;
1484
1485
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 49 times.
98 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1486
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1487 return ret;
1488
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
49 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1489 return ret;
1490 }
1491
1492
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 49 times.
245 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1493 // Determine number of active subbands for this channel
1494 196 int nsubbands = s->nsubbands[ch];
1495
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 119 times.
196 if (s->joint_intensity_index[ch])
1496 77 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1497
1498 // Update history for ADPCM and clear inactive subbands
1499
2/2
✓ Branch 0 taken 12544 times.
✓ Branch 1 taken 196 times.
12740 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1500 12544 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1501
3/4
✓ Branch 0 taken 11599 times.
✓ Branch 1 taken 945 times.
✓ Branch 2 taken 11599 times.
✗ Branch 3 not taken.
12544 if (band >= s->x96_subband_start && band < nsubbands)
1502 11599 AV_COPY128(samples, samples + s->npcmblocks);
1503 else
1504 945 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1505 }
1506 }
1507
1508 49 emms_c();
1509
1510 49 return 0;
1511 }
1512
1513 7 static int parse_x96_frame(DCACoreDecoder *s)
1514 {
1515 int ret;
1516
1517 // Revision number
1518 7 s->x96_rev_no = get_bits(&s->gb, 4);
1519
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1520 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1521 return AVERROR_INVALIDDATA;
1522 }
1523
1524 7 s->x96_crc_present = 0;
1525 7 s->x96_nchannels = s->nchannels;
1526
1527
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1528 return ret;
1529
1530
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1531 return ret;
1532
1533 // Seek to the end of core frame
1534
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1535 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1536 return AVERROR_INVALIDDATA;
1537 }
1538
1539 7 return 0;
1540 }
1541
1542 28 static int parse_x96_frame_exss(DCACoreDecoder *s)
1543 {
1544 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1545 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1546 int x96_nchsets, x96_base_ch;
1547 28 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1548
1549 // X96 sync word
1550
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1551 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1552 return AVERROR_INVALIDDATA;
1553 }
1554
1555 // X96 frame header length
1556 28 header_size = get_bits(&s->gb, 6) + 1;
1557
1558 // Check X96 frame header CRC
1559
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1560 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1561 return AVERROR_INVALIDDATA;
1562 }
1563
1564 // Revision number
1565 28 s->x96_rev_no = get_bits(&s->gb, 4);
1566
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
28 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1567 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1568 return AVERROR_INVALIDDATA;
1569 }
1570
1571 // CRC presence flag for channel set header
1572 28 s->x96_crc_present = get_bits1(&s->gb);
1573
1574 // Number of channel sets
1575 28 x96_nchsets = get_bits(&s->gb, 2) + 1;
1576
1577 // Channel set data byte size
1578
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
84 for (i = 0; i < x96_nchsets; i++)
1579 56 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1580
1581 // Number of channels in channel set
1582
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
84 for (i = 0; i < x96_nchsets; i++)
1583 56 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1584
1585 // Reserved
1586 // Byte align
1587 // CRC16 of X96 frame header
1588
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1589 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1590 return AVERROR_INVALIDDATA;
1591 }
1592
1593
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1594 return ret;
1595
1596 // Channel set data
1597 28 s->x96_nchannels = 0;
1598
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
84 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1599 56 header_pos = get_bits_count(&s->gb);
1600
1601
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 14 times.
56 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1602 42 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1603
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
42 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1604 return ret;
1605 }
1606
1607 56 x96_base_ch += x96_nchannels[i];
1608
1609
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
56 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1610 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1611 return AVERROR_INVALIDDATA;
1612 }
1613 }
1614
1615 28 return 0;
1616 }
1617
1618 77 static int parse_aux_data(DCACoreDecoder *s)
1619 {
1620 int aux_pos;
1621
1622
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if (get_bits_left(&s->gb) < 0)
1623 return AVERROR_INVALIDDATA;
1624
1625 // Auxiliary data byte count (can't be trusted)
1626 77 skip_bits(&s->gb, 6);
1627
1628 // 4-byte align
1629 77 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1630
1631 // Auxiliary data sync word
1632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1633 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1634 return AVERROR_INVALIDDATA;
1635 }
1636
1637 77 aux_pos = get_bits_count(&s->gb);
1638
1639 // Auxiliary decode time stamp flag
1640
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 if (get_bits1(&s->gb))
1641 skip_bits_long(&s->gb, 47);
1642
1643 // Auxiliary dynamic downmix flag
1644
1/2
✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
77 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1645 int i, m, n;
1646
1647 // Auxiliary primary channel downmix type
1648 77 s->prim_dmix_type = get_bits(&s->gb, 3);
1649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1650 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1651 return AVERROR_INVALIDDATA;
1652 }
1653
1654 // Size of downmix coefficients matrix
1655 77 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1656 77 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1657
1658 // Dynamic downmix code coefficients
1659
2/2
✓ Branch 0 taken 924 times.
✓ Branch 1 taken 77 times.
1001 for (i = 0; i < m * n; i++) {
1660 924 int code = get_bits(&s->gb, 9);
1661 924 int sign = (code >> 8) - 1;
1662 924 unsigned int index = code & 0xff;
1663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 924 times.
924 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1664 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1665 return AVERROR_INVALIDDATA;
1666 }
1667 924 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1668 }
1669 }
1670
1671 // Byte align
1672 77 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1673
1674 // CRC16 of auxiliary data
1675 77 skip_bits(&s->gb, 16);
1676
1677 // Check CRC
1678
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 77 times.
77 if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1679 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1680 return AVERROR_INVALIDDATA;
1681 }
1682
1683 77 return 0;
1684 }
1685
1686 2464 static int parse_optional_info(DCACoreDecoder *s)
1687 {
1688 2464 DCAContext *dca = s->avctx->priv_data;
1689 2464 int ret = -1;
1690
1691 // Time code stamp
1692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
2464 if (s->ts_present)
1693 skip_bits_long(&s->gb, 32);
1694
1695 // Auxiliary data
1696
3/4
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 2387 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 77 times.
2464 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1697 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1698 return ret;
1699
1700
2/2
✓ Branch 0 taken 2387 times.
✓ Branch 1 taken 77 times.
2464 if (ret < 0)
1701 2387 s->prim_dmix_embedded = 0;
1702
1703 // Core extensions
1704
3/4
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 2136 times.
✓ Branch 2 taken 328 times.
✗ Branch 3 not taken.
2464 if (s->ext_audio_present && !dca->core_only) {
1705 328 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1706 328 int last_pos = get_bits_count(&s->gb) / 32;
1707 int size, dist;
1708 328 uint32_t w1, w2 = 0;
1709
1710 // Search for extension sync words aligned on 4-byte boundary. Search
1711 // must be done backwards from the end of core frame to work around
1712 // sync word aliasing issues.
1713
2/4
✓ Branch 0 taken 300 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
328 switch (s->ext_audio_type) {
1714 300 case DCA_EXT_AUDIO_XCH:
1715
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 279 times.
300 if (dca->request_channel_layout)
1716 21 break;
1717
1718 // The distance between XCH sync word and end of the core frame
1719 // must be equal to XCH frame size. Off by one error is allowed for
1720 // compatibility with legacy bitstreams. Minimum XCH frame size is
1721 // 96 bytes. AMODE and PCHS are further checked to reduce
1722 // probability of alias sync detection.
1723
1/2
✓ Branch 0 taken 19253 times.
✗ Branch 1 not taken.
19253 for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1724 19253 w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1725
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 18974 times.
19253 if (w1 == DCA_SYNCWORD_XCH) {
1726 279 size = (w2 >> 22) + 1;
1727 279 dist = s->frame_size - sync_pos * 4;
1728
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 if (size >= 96
1729
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
279 && (size == dist || size - 1 == dist)
1730
1/2
✓ Branch 0 taken 279 times.
✗ Branch 1 not taken.
279 && (w2 >> 15 & 0x7f) == 0x08) {
1731 279 s->xch_pos = sync_pos * 32 + 49;
1732 279 break;
1733 }
1734 }
1735 }
1736
1737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 279 times.
279 if (!s->xch_pos) {
1738 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1739 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1740 return AVERROR_INVALIDDATA;
1741 }
1742 279 break;
1743
1744 28 case DCA_EXT_AUDIO_X96:
1745 // The distance between X96 sync word and end of the core frame
1746 // must be equal to X96 frame size. Minimum X96 frame size is 96
1747 // bytes.
1748
1/2
✓ Branch 0 taken 3172 times.
✗ Branch 1 not taken.
3172 for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1749 3172 w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1750
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3144 times.
3172 if (w1 == DCA_SYNCWORD_X96) {
1751 28 size = (w2 >> 20) + 1;
1752 28 dist = s->frame_size - sync_pos * 4;
1753
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 if (size >= 96 && size == dist) {
1754 28 s->x96_pos = sync_pos * 32 + 44;
1755 28 break;
1756 }
1757 }
1758 }
1759
1760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!s->x96_pos) {
1761 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1762 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1763 return AVERROR_INVALIDDATA;
1764 }
1765 28 break;
1766
1767 case DCA_EXT_AUDIO_XXCH:
1768 if (dca->request_channel_layout)
1769 break;
1770
1771 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1772 // size is 11 bytes.
1773 for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1774 w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1775 if (w1 == DCA_SYNCWORD_XXCH) {
1776 size = (w2 >> 26) + 1;
1777 dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1778 if (size >= 11 && size <= dist &&
1779 !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1780 (sync_pos + 1) * 4, size - 4)) {
1781 s->xxch_pos = sync_pos * 32;
1782 break;
1783 }
1784 }
1785 }
1786
1787 if (!s->xxch_pos) {
1788 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1789 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1790 return AVERROR_INVALIDDATA;
1791 }
1792 break;
1793 }
1794 }
1795
1796 2464 return 0;
1797 }
1798
1799 2464 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1800 {
1801 int ret;
1802
1803 2464 s->ext_audio_mask = 0;
1804 2464 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1805
1806
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1807 return ret;
1808 2464 s->gb_in = s->gb;
1809
1810
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = parse_frame_header(s)) < 0)
1811 return ret;
1812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = alloc_sample_buffer(s)) < 0)
1813 return ret;
1814
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1815 return ret;
1816
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if ((ret = parse_optional_info(s)) < 0)
1817 return ret;
1818
1819 // Workaround for DTS in WAV
1820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2464 times.
2464 if (s->frame_size > size)
1821 s->frame_size = size;
1822
1823
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2464 times.
2464 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1824 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1825 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1826 return AVERROR_INVALIDDATA;
1827 }
1828
1829 2464 return 0;
1830 }
1831
1832 2464 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1833 {
1834 2464 AVCodecContext *avctx = s->avctx;
1835 2464 DCAContext *dca = avctx->priv_data;
1836
2/2
✓ Branch 0 taken 1398 times.
✓ Branch 1 taken 1066 times.
2464 int exss_mask = asset ? asset->extension_mask : 0;
1837 2464 int ret = 0, ext = 0;
1838
1839 // Parse (X)XCH unless downmixing
1840
2/2
✓ Branch 0 taken 2324 times.
✓ Branch 1 taken 140 times.
2464 if (!dca->request_channel_layout) {
1841
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2303 times.
2324 if (exss_mask & DCA_EXSS_XXCH) {
1842
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1843 return ret;
1844 21 ret = parse_xxch_frame(s);
1845 21 ext = DCA_EXSS_XXCH;
1846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2303 times.
2303 } else if (s->xxch_pos) {
1847 s->gb = s->gb_in;
1848 skip_bits_long(&s->gb, s->xxch_pos);
1849 ret = parse_xxch_frame(s);
1850 ext = DCA_CSS_XXCH;
1851
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 2024 times.
2303 } else if (s->xch_pos) {
1852 279 s->gb = s->gb_in;
1853 279 skip_bits_long(&s->gb, s->xch_pos);
1854 279 ret = parse_xch_frame(s);
1855 279 ext = DCA_CSS_XCH;
1856 }
1857
1858 // Revert to primary channel set in case (X)XCH parsing fails
1859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2324 times.
2324 if (ret < 0) {
1860 if (avctx->err_recognition & AV_EF_EXPLODE)
1861 return ret;
1862 s->nchannels = ff_dca_channels[s->audio_mode];
1863 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1864 if (s->lfe_present)
1865 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1866 } else {
1867 2324 s->ext_audio_mask |= ext;
1868 }
1869 }
1870
1871 // Parse XBR
1872
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2443 times.
2464 if (exss_mask & DCA_EXSS_XBR) {
1873
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1874 return ret;
1875
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
21 if ((ret = parse_xbr_frame(s)) < 0) {
1876 if (avctx->err_recognition & AV_EF_EXPLODE)
1877 return ret;
1878 } else {
1879 21 s->ext_audio_mask |= DCA_EXSS_XBR;
1880 }
1881 }
1882
1883 // Parse X96 unless decoding XLL
1884
2/2
✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 1342 times.
2464 if (!(dca->packet & DCA_PACKET_XLL)) {
1885
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1094 times.
1122 if (exss_mask & DCA_EXSS_X96) {
1886
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1887 return ret;
1888
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
28 if ((ret = parse_x96_frame_exss(s)) < 0) {
1889 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1890 return ret;
1891 } else {
1892 28 s->ext_audio_mask |= DCA_EXSS_X96;
1893 }
1894
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1087 times.
1094 } else if (s->x96_pos) {
1895 7 s->gb = s->gb_in;
1896 7 skip_bits_long(&s->gb, s->x96_pos);
1897
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if ((ret = parse_x96_frame(s)) < 0) {
1898 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1899 return ret;
1900 } else {
1901 7 s->ext_audio_mask |= DCA_CSS_X96;
1902 }
1903 }
1904 }
1905
1906 2464 return 0;
1907 }
1908
1909 11129 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
1910 {
1911 int pos, spkr;
1912
1913 // Try to map this channel to core first
1914 11129 pos = ff_dca_channels[s->audio_mode];
1915
2/2
✓ Branch 0 taken 10766 times.
✓ Branch 1 taken 363 times.
11129 if (ch < pos) {
1916 10766 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
1917
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 10661 times.
10766 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1918
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 42 times.
105 if (s->xxch_core_mask & (1U << spkr))
1919 63 return spkr;
1920
3/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
42 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1921 21 return DCA_SPEAKER_Lss;
1922
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1923 21 return DCA_SPEAKER_Rss;
1924 return -1;
1925 }
1926 10661 return spkr;
1927 }
1928
1929 // Then XCH
1930
3/4
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
363 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
1931 279 return DCA_SPEAKER_Cs;
1932
1933 // Then XXCH
1934
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1935
1/2
✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
210 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
1936
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 84 times.
210 if (s->xxch_spkr_mask & (1U << spkr))
1937
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 42 times.
126 if (pos++ == ch)
1938 84 return spkr;
1939 }
1940
1941 // No mapping
1942 return -1;
1943 }
1944
1945 63 static void erase_dsp_history(DCACoreDecoder *s)
1946 {
1947 63 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
1948 63 s->output_history_lfe_fixed = 0;
1949 63 s->output_history_lfe_float = 0;
1950 63 }
1951
1952 2464 static void set_filter_mode(DCACoreDecoder *s, int mode)
1953 {
1954
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2401 times.
2464 if (s->filter_mode != mode) {
1955 63 erase_dsp_history(s);
1956 63 s->filter_mode = mode;
1957 }
1958 2464 }
1959
1960 1860 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
1961 {
1962 1860 int n, ch, spkr, nsamples, x96_nchannels = 0;
1963 const int32_t *filter_coeff;
1964 int32_t *ptr;
1965
1966 // Externally set x96_synth flag implies that X96 synthesis should be
1967 // enabled, yet actual X96 subband data should be discarded. This is a
1968 // special case for lossless residual decoder that ignores X96 data if
1969 // present.
1970
3/4
✓ Branch 0 taken 518 times.
✓ Branch 1 taken 1342 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 518 times.
1860 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
1971 x96_nchannels = s->x96_nchannels;
1972 x96_synth = 1;
1973 }
1974
2/2
✓ Branch 0 taken 1258 times.
✓ Branch 1 taken 602 times.
1860 if (x96_synth < 0)
1975 1258 x96_synth = 0;
1976
1977 1860 s->output_rate = s->sample_rate << x96_synth;
1978 1860 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
1979
1980 // Reallocate PCM output buffer
1981 1860 av_fast_malloc(&s->output_buffer, &s->output_size,
1982 1860 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
1983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1860 times.
1860 if (!s->output_buffer)
1984 return AVERROR(ENOMEM);
1985
1986 1860 ptr = (int32_t *)s->output_buffer;
1987
2/2
✓ Branch 0 taken 59520 times.
✓ Branch 1 taken 1860 times.
61380 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
1988
2/2
✓ Branch 0 taken 9099 times.
✓ Branch 1 taken 50421 times.
59520 if (s->ch_mask & (1U << spkr)) {
1989 9099 s->output_samples[spkr] = ptr;
1990 9099 ptr += nsamples;
1991 } else {
1992 50421 s->output_samples[spkr] = NULL;
1993 }
1994 }
1995
1996 // Handle change of filtering mode
1997 1860 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
1998
1999 // Select filter
2000
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1776 times.
1860 if (x96_synth)
2001 84 filter_coeff = ff_dca_fir_64bands_fixed;
2002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1776 times.
1776 else if (s->filter_perfect)
2003 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2004 else
2005 1776 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2006
2007 // Filter primary channels
2008
2/2
✓ Branch 0 taken 7756 times.
✓ Branch 1 taken 1860 times.
9616 for (ch = 0; ch < s->nchannels; ch++) {
2009 // Map this primary channel to speaker
2010 7756 spkr = map_prm_ch_to_spkr(s, ch);
2011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7756 times.
7756 if (spkr < 0)
2012 return AVERROR(EINVAL);
2013
2014 // Filter bank reconstruction
2015 7756 s->dcadsp->sub_qmf_fixed[x96_synth](
2016 &s->synth,
2017 &s->dcadct,
2018 s->output_samples[spkr],
2019 7756 s->subband_samples[ch],
2020 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2021 7756 s->dcadsp_data[ch].u.fix.hist1,
2022 &s->dcadsp_data[ch].offset,
2023 7756 s->dcadsp_data[ch].u.fix.hist2,
2024 filter_coeff,
2025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7756 times.
7756 s->npcmblocks);
2026 }
2027
2028 // Filter LFE channel
2029
2/2
✓ Branch 0 taken 1343 times.
✓ Branch 1 taken 517 times.
1860 if (s->lfe_present) {
2030 1343 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2031 1343 int nlfesamples = s->npcmblocks >> 1;
2032
2033 // Check LFF
2034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1343 times.
1343 if (s->lfe_present == DCA_LFE_FLAG_128) {
2035 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2036 return AVERROR(EINVAL);
2037 }
2038
2039 // Offset intermediate buffer for X96
2040
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1259 times.
1343 if (x96_synth)
2041 84 samples += nsamples / 2;
2042
2043 // Interpolate LFE channel
2044 1343 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2045 1343 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2046
2047
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1259 times.
1343 if (x96_synth) {
2048 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2049 // (47.6 - 48.0 kHz) components of interpolation image
2050 84 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2051 samples, &s->output_history_lfe_fixed,
2052 84 nsamples / 2);
2053
2054 }
2055
2056 // Update LFE history
2057
2/2
✓ Branch 0 taken 10744 times.
✓ Branch 1 taken 1343 times.
12087 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2058 10744 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2059 }
2060
2061 1860 return 0;
2062 }
2063
2064 518 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2065 {
2066 518 AVCodecContext *avctx = s->avctx;
2067 518 DCAContext *dca = avctx->priv_data;
2068 int i, n, ch, ret, spkr, nsamples;
2069
2070 // Don't filter twice when falling back from XLL
2071
2/4
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 518 times.
518 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2072 return ret;
2073
2074 518 avctx->sample_rate = s->output_rate;
2075 518 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2076 518 avctx->bits_per_raw_sample = 24;
2077
2078 518 frame->nb_samples = nsamples = s->npcmsamples;
2079
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 518 times.
518 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2080 return ret;
2081
2082 // Undo embedded XCH downmix
2083
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
518 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2084 && s->audio_mode >= DCA_AMODE_2F2R) {
2085 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2086 s->output_samples[DCA_SPEAKER_Rs],
2087 s->output_samples[DCA_SPEAKER_Cs],
2088 nsamples);
2089
2090 }
2091
2092 // Undo embedded XXCH downmix
2093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2094 && s->xxch_dmix_embedded) {
2095 int scale_inv = s->xxch_dmix_scale_inv;
2096 int *coeff_ptr = s->xxch_dmix_coeff;
2097 int xch_base = ff_dca_channels[s->audio_mode];
2098 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2099
2100 // Undo embedded core downmix pre-scaling
2101 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2102 if (s->xxch_core_mask & (1U << spkr)) {
2103 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2104 scale_inv, nsamples);
2105 }
2106 }
2107
2108 // Undo downmix
2109 for (ch = xch_base; ch < s->nchannels; ch++) {
2110 int src_spkr = map_prm_ch_to_spkr(s, ch);
2111 if (src_spkr < 0)
2112 return AVERROR(EINVAL);
2113 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2114 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2115 int coeff = mul16(*coeff_ptr++, scale_inv);
2116 if (coeff) {
2117 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2118 s->output_samples[src_spkr],
2119 coeff, nsamples);
2120 }
2121 }
2122 }
2123 }
2124 }
2125
2126
1/2
✓ Branch 0 taken 518 times.
✗ Branch 1 not taken.
518 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2127 // Front sum/difference decoding
2128
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
518 if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
2129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
2130 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2131 s->output_samples[DCA_SPEAKER_R],
2132 nsamples);
2133 }
2134
2135 // Surround sum/difference decoding
2136
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
518 if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
2137 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2138 s->output_samples[DCA_SPEAKER_Rs],
2139 nsamples);
2140 }
2141 }
2142
2143 // Downmix primary channel set to stereo
2144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518 times.
518 if (s->request_mask != s->ch_mask) {
2145 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2146 s->output_samples,
2147 s->prim_dmix_coeff,
2148 nsamples, s->ch_mask);
2149 }
2150
2151
2/2
✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 518 times.
1558 for (i = 0; i < avctx->channels; i++) {
2152 1040 int32_t *samples = s->output_samples[s->ch_remap[i]];
2153 1040 int32_t *plane = (int32_t *)frame->extended_data[i];
2154
2/2
✓ Branch 0 taken 532480 times.
✓ Branch 1 taken 1040 times.
533520 for (n = 0; n < nsamples; n++)
2155 532480 plane[n] = clip23(samples[n]) * (1 << 8);
2156 }
2157
2158 518 return 0;
2159 }
2160
2161 604 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2162 {
2163 604 AVCodecContext *avctx = s->avctx;
2164 604 int x96_nchannels = 0, x96_synth = 0;
2165 int i, n, ch, ret, spkr, nsamples, nchannels;
2166 604 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2167 const float *filter_coeff;
2168
2169
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 569 times.
604 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2170 35 x96_nchannels = s->x96_nchannels;
2171 35 x96_synth = 1;
2172 }
2173
2174 604 avctx->sample_rate = s->sample_rate << x96_synth;
2175 604 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2176 604 avctx->bits_per_raw_sample = 0;
2177
2178 604 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 604 times.
604 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2180 return ret;
2181
2182 // Build reverse speaker to channel mapping
2183
2/2
✓ Branch 0 taken 3906 times.
✓ Branch 1 taken 604 times.
4510 for (i = 0; i < avctx->channels; i++)
2184 3906 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2185
2186 // Allocate space for extra channels
2187 604 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2188
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 597 times.
604 if (nchannels > 0) {
2189 7 av_fast_malloc(&s->output_buffer, &s->output_size,
2190 7 nsamples * nchannels * sizeof(float));
2191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!s->output_buffer)
2192 return AVERROR(ENOMEM);
2193
2194 7 ptr = (float *)s->output_buffer;
2195
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 7 times.
231 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2196
2/2
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 42 times.
224 if (!(s->ch_mask & (1U << spkr)))
2197 182 continue;
2198
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
42 if (output_samples[spkr])
2199 14 continue;
2200 28 output_samples[spkr] = ptr;
2201 28 ptr += nsamples;
2202 }
2203 }
2204
2205 // Handle change of filtering mode
2206 604 set_filter_mode(s, x96_synth);
2207
2208 // Select filter
2209
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 569 times.
604 if (x96_synth)
2210 35 filter_coeff = ff_dca_fir_64bands;
2211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 569 times.
569 else if (s->filter_perfect)
2212 filter_coeff = ff_dca_fir_32bands_perfect;
2213 else
2214 569 filter_coeff = ff_dca_fir_32bands_nonperfect;
2215
2216 // Filter primary channels
2217
2/2
✓ Branch 0 taken 3331 times.
✓ Branch 1 taken 604 times.
3935 for (ch = 0; ch < s->nchannels; ch++) {
2218 // Map this primary channel to speaker
2219 3331 spkr = map_prm_ch_to_spkr(s, ch);
2220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3331 times.
3331 if (spkr < 0)
2221 return AVERROR(EINVAL);
2222
2223 // Filter bank reconstruction
2224 3331 s->dcadsp->sub_qmf_float[x96_synth](
2225 &s->synth,
2226 &s->imdct[x96_synth],
2227 output_samples[spkr],
2228 3331 s->subband_samples[ch],
2229 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2230 3331 s->dcadsp_data[ch].u.flt.hist1,
2231 &s->dcadsp_data[ch].offset,
2232 3331 s->dcadsp_data[ch].u.flt.hist2,
2233 filter_coeff,
2234 3331 s->npcmblocks,
2235
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 3135 times.
3331 1.0f / (1 << (17 - x96_synth)));
2236 }
2237
2238 // Filter LFE channel
2239
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 1 times.
604 if (s->lfe_present) {
2240 603 int dec_select = (s->lfe_present == DCA_LFE_FLAG_128);
2241 603 float *samples = output_samples[DCA_SPEAKER_LFE1];
2242 603 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2243
2244 // Offset intermediate buffer for X96
2245
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 568 times.
603 if (x96_synth)
2246 35 samples += nsamples / 2;
2247
2248 // Select filter
2249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 if (dec_select)
2250 filter_coeff = ff_dca_lfe_fir_128;
2251 else
2252 603 filter_coeff = ff_dca_lfe_fir_64;
2253
2254 // Interpolate LFE channel
2255 603 s->dcadsp->lfe_fir_float[dec_select](
2256 603 samples, s->lfe_samples + DCA_LFE_HISTORY,
2257 603 filter_coeff, s->npcmblocks);
2258
2259
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 568 times.
603 if (x96_synth) {
2260 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2261 // (47.6 - 48.0 kHz) components of interpolation image
2262 35 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2263 samples, &s->output_history_lfe_float,
2264 35 nsamples / 2);
2265 }
2266
2267 // Update LFE history
2268
2/2
✓ Branch 0 taken 4824 times.
✓ Branch 1 taken 603 times.
5427 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2269 4824 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2270 }
2271
2272 // Undo embedded XCH downmix
2273
4/4
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 325 times.
✓ Branch 2 taken 272 times.
✓ Branch 3 taken 7 times.
604 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2274
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
272 && s->audio_mode >= DCA_AMODE_2F2R) {
2275 272 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2276 272 output_samples[DCA_SPEAKER_Cs],
2277 -M_SQRT1_2, nsamples);
2278 272 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2279 272 output_samples[DCA_SPEAKER_Cs],
2280 -M_SQRT1_2, nsamples);
2281 }
2282
2283 // Undo embedded XXCH downmix
2284
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 583 times.
604 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2285
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 && s->xxch_dmix_embedded) {
2286 21 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2287 21 int *coeff_ptr = s->xxch_dmix_coeff;
2288 21 int xch_base = ff_dca_channels[s->audio_mode];
2289 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2290
2291 // Undo downmix
2292
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 21 times.
63 for (ch = xch_base; ch < s->nchannels; ch++) {
2293 42 int src_spkr = map_prm_ch_to_spkr(s, ch);
2294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (src_spkr < 0)
2295 return AVERROR(EINVAL);
2296
2/2
✓ Branch 0 taken 462 times.
✓ Branch 1 taken 42 times.
504 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2297
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 420 times.
462 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2298 42 int coeff = *coeff_ptr++;
2299
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 if (coeff) {
2300 42 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2301 42 output_samples[src_spkr],
2302 coeff * (-1.0f / (1 << 15)),
2303 nsamples);
2304 }
2305 }
2306 }
2307 }
2308
2309 // Undo embedded core downmix pre-scaling
2310
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 21 times.
252 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2311
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 105 times.
231 if (s->xxch_core_mask & (1U << spkr)) {
2312 126 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2313 126 output_samples[spkr],
2314 scale_inv, nsamples);
2315 }
2316 }
2317 }
2318
2319
2/2
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 293 times.
604 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2320 // Front sum/difference decoding
2321
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
311 if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
2322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
311 || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
2323 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2324 output_samples[DCA_SPEAKER_R],
2325 nsamples);
2326 }
2327
2328 // Surround sum/difference decoding
2329
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 311 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
311 if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
2330 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2331 output_samples[DCA_SPEAKER_Rs],
2332 nsamples);
2333 }
2334 }
2335
2336 // Downmix primary channel set to stereo
2337
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 597 times.
604 if (s->request_mask != s->ch_mask) {
2338 7 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2339 7 s->prim_dmix_coeff,
2340 nsamples, s->ch_mask);
2341 }
2342
2343 604 return 0;
2344 }
2345
2346 1122 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2347 {
2348 1122 AVCodecContext *avctx = s->avctx;
2349 1122 DCAContext *dca = avctx->priv_data;
2350 1122 DCAExssAsset *asset = &dca->exss.assets[0];
2351 enum AVMatrixEncoding matrix_encoding;
2352 int ret;
2353
2354 // Handle downmixing to stereo request
2355
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1108 times.
1122 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2356
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
14 && s->audio_mode > DCA_AMODE_MONO && s->prim_dmix_embedded
2357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2358 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2359 7 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2360 else
2361 1115 s->request_mask = s->ch_mask;
2362
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1122 times.
1122 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2363 return AVERROR(EINVAL);
2364
2365 // Force fixed point mode when falling back from XLL
2366
4/4
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 518 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 548 times.
1122 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 && (asset->extension_mask & DCA_EXSS_XLL)))
2368 518 ret = filter_frame_fixed(s, frame);
2369 else
2370 604 ret = filter_frame_float(s, frame);
2371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1122 times.
1122 if (ret < 0)
2372 return ret;
2373
2374 // Set profile, bit rate, etc
2375
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1066 times.
1122 if (s->ext_audio_mask & DCA_EXSS_MASK)
2376 56 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2377
2/2
✓ Branch 0 taken 258 times.
✓ Branch 1 taken 808 times.
1066 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2378 258 avctx->profile = FF_PROFILE_DTS_ES;
2379
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 801 times.
808 else if (s->ext_audio_mask & DCA_CSS_X96)
2380 7 avctx->profile = FF_PROFILE_DTS_96_24;
2381 else
2382 801 avctx->profile = FF_PROFILE_DTS;
2383
2384
3/4
✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1066 times.
✓ Branch 3 taken 56 times.
1122 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2385 1066 avctx->bit_rate = s->bit_rate;
2386 else
2387 56 avctx->bit_rate = 0;
2388
2389
3/4
✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 1115 times.
1122 if (s->audio_mode == DCA_AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2391 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2392 else
2393 1122 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2394
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1122 times.
1122 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2395 return ret;
2396
2397 1122 return 0;
2398 }
2399
2400 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2401 {
2402 if (s->subband_buffer) {
2403 erase_adpcm_history(s);
2404 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2405 }
2406
2407 if (s->x96_subband_buffer)
2408 erase_x96_adpcm_history(s);
2409
2410 erase_dsp_history(s);
2411 }
2412
2413 92 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2414 {
2415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2416 return -1;
2417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2418 return -1;
2419
2420 92 ff_dcadct_init(&s->dcadct);
2421
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2422 return -1;
2423
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 92 times.
92 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2424 return -1;
2425 92 ff_synth_filter_init(&s->synth);
2426
2427 92 s->x96_rand = 1;
2428 92 return 0;
2429 }
2430
2431 92 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2432 {
2433 92 av_freep(&s->float_dsp);
2434 92 av_freep(&s->fixed_dsp);
2435
2436 92 ff_mdct_end(&s->imdct[0]);
2437 92 ff_mdct_end(&s->imdct[1]);
2438
2439 92 av_freep(&s->subband_buffer);
2440 92 s->subband_size = 0;
2441
2442 92 av_freep(&s->x96_subband_buffer);
2443 92 s->x96_subband_size = 0;
2444
2445 92 av_freep(&s->output_buffer);
2446 92 s->output_size = 0;
2447 92 }
2448