FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_xll.c
Date: 2026-04-24 05:04:01
Exec Total Coverage
Lines: 618 822 75.2%
Functions: 33 36 91.7%
Branches: 475 650 73.1%

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 "avcodec.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/mem.h"
24 #include "dcadec.h"
25 #include "dcadata.h"
26 #include "dcamath.h"
27 #include "dca_syncwords.h"
28 #include "decode.h"
29 #include "unary.h"
30
31 135518 static int get_linear(GetBitContext *gb, int n)
32 {
33 135518 unsigned int v = get_bits_long(gb, n);
34 135518 return (v >> 1) ^ -(v & 1);
35 }
36
37 5082830 static int get_rice_un(GetBitContext *gb, int k)
38 {
39 5082830 unsigned int v = get_unary(gb, 1, get_bits_left(gb));
40 5082830 return (v << k) | get_bits_long(gb, k);
41 }
42
43 5082830 static int get_rice(GetBitContext *gb, int k)
44 {
45 5082830 unsigned int v = get_rice_un(gb, k);
46 5082830 return (v >> 1) ^ -(v & 1);
47 }
48
49 1096 static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
50 {
51 int i;
52
53
2/2
✓ Branch 0 taken 88064 times.
✓ Branch 1 taken 1096 times.
89160 for (i = 0; i < size; i++)
54 88064 array[i] = get_bits(gb, n);
55 1096 }
56
57 18736 static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
58 {
59 int i;
60
61
2/2
✓ Branch 0 taken 17876 times.
✓ Branch 1 taken 860 times.
18736 if (n == 0)
62 17876 memset(array, 0, sizeof(*array) * size);
63
2/2
✓ Branch 0 taken 84225 times.
✓ Branch 1 taken 860 times.
85085 else for (i = 0; i < size; i++)
64 84225 array[i] = get_linear(gb, n);
65 18736 }
66
67 50581 static int get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
68 {
69 int i;
70
71
3/4
✓ Branch 0 taken 4935289 times.
✓ Branch 1 taken 50581 times.
✓ Branch 3 taken 4935289 times.
✗ Branch 4 not taken.
4985870 for (i = 0; i < size && get_bits_left(gb) > k; i++)
72 4935289 array[i] = get_rice(gb, k);
73
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50581 times.
50581 if (i < size)
75 return AVERROR_INVALIDDATA;
76 50581 return 0;
77 }
78
79 210 static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
80 {
81 // Size of downmix coefficient matrix
82
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 180 times.
210 int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
83 210 int i, j, *coeff_ptr = c->dmix_coeff;
84
85
2/2
✓ Branch 0 taken 900 times.
✓ Branch 1 taken 210 times.
1110 for (i = 0; i < m; i++) {
86 900 int code, sign, coeff, scale, scale_inv = 0;
87 unsigned int index;
88
89 // Downmix scale (only for non-primary channel sets)
90
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 60 times.
900 if (!c->primary_chset) {
91 840 code = get_bits(&s->gb, 9);
92 840 sign = (code >> 8) - 1;
93 840 index = (code & 0xff) - FF_DCA_DMIXTABLE_OFFSET;
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 840 times.
840 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
95 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");
96 return AVERROR_INVALIDDATA;
97 }
98 840 scale = ff_dca_dmixtable[index + FF_DCA_DMIXTABLE_OFFSET];
99 840 scale_inv = ff_dca_inv_dmixtable[index];
100 840 c->dmix_scale[i] = (scale ^ sign) - sign;
101 840 c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;
102 }
103
104 // Downmix coefficients
105
2/2
✓ Branch 0 taken 2100 times.
✓ Branch 1 taken 900 times.
3000 for (j = 0; j < c->nchannels; j++) {
106 2100 code = get_bits(&s->gb, 9);
107 2100 sign = (code >> 8) - 1;
108 2100 index = code & 0xff;
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2100 times.
2100 if (index >= FF_DCA_DMIXTABLE_SIZE) {
110 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");
111 return AVERROR_INVALIDDATA;
112 }
113 2100 coeff = ff_dca_dmixtable[index];
114
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 360 times.
2100 if (!c->primary_chset)
115 // Multiply by |InvDmixScale| to get |UndoDmixScale|
116 1740 coeff = mul16(scale_inv, coeff);
117 2100 *coeff_ptr++ = (coeff ^ sign) - sign;
118 }
119 }
120
121 210 return 0;
122 }
123
124 2792 static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
125 {
126 2792 int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
127 2792 DCAXllChSet *p = &s->chset[0];
128 DCAXllBand *b;
129
130 // Size of channel set sub-header
131 2792 header_size = get_bits(&s->gb, 10) + 1;
132
133 // Check CRC
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2792 times.
2792 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
135 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");
136 return AVERROR_INVALIDDATA;
137 }
138
139 // Number of channels in the channel set
140 2792 c->nchannels = get_bits(&s->gb, 4) + 1;
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2792 times.
2792 if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
142 avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
143 return AVERROR_PATCHWELCOME;
144 }
145
146 // Residual type
147 2792 c->residual_encode = get_bits(&s->gb, c->nchannels);
148
149 // PCM bit resolution
150 2792 c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
151
152 // Storage unit width
153 2792 c->storage_bit_res = get_bits(&s->gb, 5) + 1;
154
4/6
✓ Branch 0 taken 2702 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 2702 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2702 times.
2792 if (c->storage_bit_res != 16 && c->storage_bit_res != 20 && c->storage_bit_res != 24) {
155 avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);
156 return AVERROR_PATCHWELCOME;
157 }
158
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2792 times.
2792 if (c->pcm_bit_res > c->storage_bit_res) {
160 av_log(s->avctx, AV_LOG_ERROR, "Invalid PCM bit resolution for XLL channel set (%d > %d)\n", c->pcm_bit_res, c->storage_bit_res);
161 return AVERROR_INVALIDDATA;
162 }
163
164 // Original sampling frequency
165 2792 c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2792 times.
2792 if (c->freq > 192000) {
167 avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
168 return AVERROR_PATCHWELCOME;
169 }
170
171 // Sampling frequency modifier
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2792 times.
2792 if (get_bits(&s->gb, 2)) {
173 avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
174 return AVERROR_PATCHWELCOME;
175 }
176
177 // Which replacement set this channel set is member of
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2792 times.
2792 if (get_bits(&s->gb, 2)) {
179 avpriv_request_sample(s->avctx, "XLL replacement set");
180 return AVERROR_PATCHWELCOME;
181 }
182
183
1/2
✓ Branch 0 taken 2792 times.
✗ Branch 1 not taken.
2792 if (asset->one_to_one_map_ch_to_spkr) {
184 // Primary channel set flag
185 2792 c->primary_chset = get_bits1(&s->gb);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2792 times.
2792 if (c->primary_chset != (c == p)) {
187 av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");
188 return AVERROR_INVALIDDATA;
189 }
190
191 // Downmix coefficients present in stream
192 2792 c->dmix_coeffs_present = get_bits1(&s->gb);
193
194 // Downmix already performed by encoder
195
3/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 2582 times.
✓ Branch 3 taken 210 times.
✗ Branch 4 not taken.
2792 c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
196
197 // Downmix type
198
4/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 2582 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 180 times.
2792 if (c->dmix_coeffs_present && c->primary_chset) {
199 30 c->dmix_type = get_bits(&s->gb, 3);
200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {
201 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");
202 return AVERROR_INVALIDDATA;
203 }
204 }
205
206 // Whether the channel set is part of a hierarchy
207 2792 c->hier_chset = get_bits1(&s->gb);
208
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2792 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2792 if (!c->hier_chset && s->nchsets != 1) {
209 avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
210 return AVERROR_PATCHWELCOME;
211 }
212
213 // Downmix coefficients
214
3/4
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 2582 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 210 times.
2792 if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
215 return ret;
216
217 // Channel mask enabled
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2792 times.
2792 if (!get_bits1(&s->gb)) {
219 avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
220 return AVERROR_PATCHWELCOME;
221 }
222
223 // Channel mask for set
224 2792 c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2792 times.
2792 if (av_popcount(c->ch_mask) != c->nchannels) {
226 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");
227 return AVERROR_INVALIDDATA;
228 }
229
230 // Build the channel to speaker map
231
2/2
✓ Branch 0 taken 48464 times.
✓ Branch 1 taken 2792 times.
51256 for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
232
2/2
✓ Branch 0 taken 11174 times.
✓ Branch 1 taken 37290 times.
48464 if (c->ch_mask & (1U << i))
233 11174 c->ch_remap[j++] = i;
234 } else {
235 // Mapping coeffs present flag
236 if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {
237 avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");
238 return AVERROR_PATCHWELCOME;
239 }
240
241 // Setup for LtRt decoding
242 c->primary_chset = 1;
243 c->dmix_coeffs_present = 0;
244 c->dmix_embedded = 0;
245 c->hier_chset = 0;
246 c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;
247 c->ch_remap[0] = DCA_SPEAKER_L;
248 c->ch_remap[1] = DCA_SPEAKER_R;
249 }
250
251
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 2702 times.
2792 if (c->freq > 96000) {
252 // Extra frequency bands flag
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (get_bits1(&s->gb)) {
254 avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
255 return AVERROR_PATCHWELCOME;
256 }
257 90 c->nfreqbands = 2;
258 } else {
259 2702 c->nfreqbands = 1;
260 }
261
262 // Set the sampling frequency to that of the first frequency band.
263 // Frequency will be doubled again after bands assembly.
264 2792 c->freq >>= c->nfreqbands - 1;
265
266 // Verify that all channel sets have the same audio characteristics
267
4/6
✓ Branch 0 taken 1357 times.
✓ Branch 1 taken 1435 times.
✓ Branch 2 taken 1357 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1357 times.
✗ Branch 5 not taken.
2792 if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
268
1/2
✓ Branch 0 taken 1357 times.
✗ Branch 1 not taken.
1357 || c->pcm_bit_res != p->pcm_bit_res
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1357 times.
1357 || c->storage_bit_res != p->storage_bit_res)) {
270 avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
271 return AVERROR_PATCHWELCOME;
272 }
273
274 // Determine number of bits to read bit allocation coding parameter
275
2/2
✓ Branch 0 taken 2702 times.
✓ Branch 1 taken 90 times.
2792 if (c->storage_bit_res > 16)
276 2702 c->nabits = 5;
277
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 else if (c->storage_bit_res > 8)
278 90 c->nabits = 4;
279 else
280 c->nabits = 3;
281
282 // Account for embedded downmix and decimator saturation
283
6/6
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 2684 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 2624 times.
2792 if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
284 90 c->nabits++;
285
286
2/2
✓ Branch 0 taken 2882 times.
✓ Branch 1 taken 2792 times.
5674 for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
287 // Pairwise channel decorrelation
288
3/4
✓ Branch 1 taken 2852 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 2852 times.
✗ Branch 4 not taken.
5734 if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
289 2852 int ch_nbits = av_ceil_log2(c->nchannels);
290
291 // Original channel order
292
2/2
✓ Branch 0 taken 11504 times.
✓ Branch 1 taken 2852 times.
14356 for (i = 0; i < c->nchannels; i++) {
293 11504 b->orig_order[i] = get_bits(&s->gb, ch_nbits);
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11504 times.
11504 if (b->orig_order[i] >= c->nchannels) {
295 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");
296 return AVERROR_INVALIDDATA;
297 }
298 }
299
300 // Pairwise channel coefficients
301
2/2
✓ Branch 0 taken 5752 times.
✓ Branch 1 taken 2852 times.
8604 for (i = 0; i < c->nchannels / 2; i++)
302
2/2
✓ Branch 1 taken 2116 times.
✓ Branch 2 taken 3636 times.
5752 b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
303 } else {
304
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
60 for (i = 0; i < c->nchannels; i++)
305 30 b->orig_order[i] = i;
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 for (i = 0; i < c->nchannels / 2; i++)
307 b->decor_coeff[i] = 0;
308 }
309
310 // Adaptive predictor order
311 2882 b->highest_pred_order = 0;
312
2/2
✓ Branch 0 taken 11534 times.
✓ Branch 1 taken 2882 times.
14416 for (i = 0; i < c->nchannels; i++) {
313 11534 b->adapt_pred_order[i] = get_bits(&s->gb, 4);
314
2/2
✓ Branch 0 taken 4149 times.
✓ Branch 1 taken 7385 times.
11534 if (b->adapt_pred_order[i] > b->highest_pred_order)
315 4149 b->highest_pred_order = b->adapt_pred_order[i];
316 }
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2882 times.
2882 if (b->highest_pred_order > s->nsegsamples) {
318 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive prediction order\n");
319 return AVERROR_INVALIDDATA;
320 }
321
322 // Fixed predictor order
323
2/2
✓ Branch 0 taken 11534 times.
✓ Branch 1 taken 2882 times.
14416 for (i = 0; i < c->nchannels; i++)
324
2/2
✓ Branch 0 taken 2405 times.
✓ Branch 1 taken 9129 times.
11534 b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
325
326 // Adaptive predictor quantized reflection coefficients
327
2/2
✓ Branch 0 taken 11534 times.
✓ Branch 1 taken 2882 times.
14416 for (i = 0; i < c->nchannels; i++) {
328
2/2
✓ Branch 0 taken 43559 times.
✓ Branch 1 taken 11534 times.
55093 for (j = 0; j < b->adapt_pred_order[i]; j++) {
329 43559 k = get_linear(&s->gb, 8);
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43559 times.
43559 if (k == -128) {
331 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
332 return AVERROR_INVALIDDATA;
333 }
334
2/2
✓ Branch 0 taken 15550 times.
✓ Branch 1 taken 28009 times.
43559 if (k < 0)
335 15550 b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
336 else
337 28009 b->adapt_refl_coeff[i][j] = (int)ff_dca_xll_refl_coeff[ k];
338 }
339 }
340
341 // Downmix performed by encoder in extension frequency band
342
5/6
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 2642 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 210 times.
✓ Branch 5 taken 30 times.
✗ Branch 6 not taken.
2882 b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
343
344 // MSB/LSB split flag in extension frequency band
345
7/8
✓ Branch 0 taken 2792 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 2432 times.
✓ Branch 3 taken 360 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 2432 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 90 times.
2882 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
346 // Size of LSB section in any segment
347 360 b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
348
2/4
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 360 times.
360 if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {
349 av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");
350 return AVERROR_INVALIDDATA;
351 }
352
353 // Account for optional CRC bytes after LSB section
354
4/6
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 285 times.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
360 if (b->lsb_section_size && (s->band_crc_present > 2 ||
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 (band == 0 && s->band_crc_present > 1)))
356 b->lsb_section_size += 2;
357
358 // Number of bits to represent the samples in LSB part
359
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 360 times.
1650 for (i = 0; i < c->nchannels; i++) {
360 1290 b->nscalablelsbs[i] = get_bits(&s->gb, 4);
361
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 1140 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 150 times.
1290 if (b->nscalablelsbs[i] && !b->lsb_section_size) {
362 av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");
363 return AVERROR_INVALIDDATA;
364 }
365 }
366 } else {
367 2522 b->lsb_section_size = 0;
368
2/2
✓ Branch 0 taken 10244 times.
✓ Branch 1 taken 2522 times.
12766 for (i = 0; i < c->nchannels; i++)
369 10244 b->nscalablelsbs[i] = 0;
370 }
371
372 // Scalable resolution flag in extension frequency band
373
7/8
✓ Branch 0 taken 2792 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 2432 times.
✓ Branch 3 taken 360 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 2432 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 90 times.
2882 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
374 // Number of bits discarded by authoring
375
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 360 times.
1650 for (i = 0; i < c->nchannels; i++)
376 1290 b->bit_width_adjust[i] = get_bits(&s->gb, 4);
377 } else {
378
2/2
✓ Branch 0 taken 10244 times.
✓ Branch 1 taken 2522 times.
12766 for (i = 0; i < c->nchannels; i++)
379 10244 b->bit_width_adjust[i] = 0;
380 }
381 }
382
383 // Reserved
384 // Byte align
385 // CRC16 of channel set sub-header
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2792 times.
2792 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
387 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");
388 return AVERROR_INVALIDDATA;
389 }
390
391 2792 return 0;
392 }
393
394 2722 static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
395 {
396
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 2639 times.
2722 int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
397 2722 int nchsamples = s->nframesamples + ndecisamples;
398 2722 int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
399 int32_t *ptr;
400
401 // Reallocate MSB sample buffer
402 2722 av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2722 times.
2722 if (!c->sample_buffer[0])
404 return AVERROR(ENOMEM);
405
406 2722 ptr = c->sample_buffer[0] + ndecisamples;
407
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 2722 times.
5527 for (i = 0; i < c->nfreqbands; i++) {
408
2/2
✓ Branch 0 taken 11352 times.
✓ Branch 1 taken 2805 times.
14157 for (j = 0; j < c->nchannels; j++) {
409 11352 c->bands[i].msb_sample_buffer[j] = ptr;
410 11352 ptr += nchsamples;
411 }
412 }
413
414 2722 return 0;
415 }
416
417 2722 static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
418 {
419 2722 int i, j, nsamples = 0;
420 int32_t *ptr;
421
422 // Determine number of frequency bands that have MSB/LSB split
423
2/2
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 2722 times.
5527 for (i = 0; i < c->nfreqbands; i++)
424
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 2734 times.
2805 if (c->bands[i].lsb_section_size)
425 71 nsamples += s->nframesamples * c->nchannels;
426
2/2
✓ Branch 0 taken 2651 times.
✓ Branch 1 taken 71 times.
2722 if (!nsamples)
427 2651 return 0;
428
429 // Reallocate LSB sample buffer
430 71 av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 if (!c->sample_buffer[1])
432 return AVERROR(ENOMEM);
433
434 71 ptr = c->sample_buffer[1];
435
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 71 times.
142 for (i = 0; i < c->nfreqbands; i++) {
436
1/2
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
71 if (c->bands[i].lsb_section_size) {
437
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 71 times.
415 for (j = 0; j < c->nchannels; j++) {
438 344 c->bands[i].lsb_sample_buffer[j] = ptr;
439 344 ptr += s->nframesamples;
440 }
441 } else {
442 for (j = 0; j < c->nchannels; j++)
443 c->bands[i].lsb_sample_buffer[j] = NULL;
444 }
445 }
446
447 71 return 0;
448 }
449
450 8612 static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
451 {
452 8612 DCAXllBand *b = &c->bands[band];
453 int i, j, k;
454
455 // Start unpacking MSB portion of the segment
456
3/4
✓ Branch 0 taken 5807 times.
✓ Branch 1 taken 2805 times.
✓ Branch 3 taken 5807 times.
✗ Branch 4 not taken.
8612 if (!(seg && get_bits1(&s->gb))) {
457 // Unpack segment type
458 // 0 - distinct coding parameters for each channel
459 // 1 - common coding parameters for all channels
460 8612 c->seg_common = get_bits1(&s->gb);
461
462 // Determine number of coding parameters encoded in segment
463
2/2
✓ Branch 0 taken 6081 times.
✓ Branch 1 taken 2531 times.
8612 k = c->seg_common ? 1 : c->nchannels;
464
465 // Unpack Rice coding parameters
466
2/2
✓ Branch 0 taken 27886 times.
✓ Branch 1 taken 8612 times.
36498 for (i = 0; i < k; i++) {
467 // Unpack Rice coding flag
468 // 0 - linear code, 1 - Rice code
469 27886 c->rice_code_flag[i] = get_bits1(&s->gb);
470 // Unpack Hybrid Rice coding flag
471 // 0 - Rice code, 1 - Hybrid Rice code
472
6/6
✓ Branch 0 taken 25355 times.
✓ Branch 1 taken 2531 times.
✓ Branch 2 taken 20263 times.
✓ Branch 3 taken 5092 times.
✓ Branch 5 taken 1047 times.
✓ Branch 6 taken 19216 times.
27886 if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))
473 // Unpack binary code length for isolated samples
474 1047 c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
475 else
476 // 0 indicates no Hybrid Rice coding
477 26839 c->bitalloc_hybrid_linear[i] = 0;
478 }
479
480 // Unpack coding parameters
481
2/2
✓ Branch 0 taken 27886 times.
✓ Branch 1 taken 8612 times.
36498 for (i = 0; i < k; i++) {
482
2/2
✓ Branch 0 taken 9012 times.
✓ Branch 1 taken 18874 times.
27886 if (seg == 0) {
483 // Unpack coding parameter for part A of segment 0
484 9012 c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
485
486 // Adjust for the linear code
487
4/4
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 8052 times.
✓ Branch 2 taken 99 times.
✓ Branch 3 taken 861 times.
9012 if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
488 99 c->bitalloc_part_a[i]++;
489
490
2/2
✓ Branch 0 taken 8222 times.
✓ Branch 1 taken 790 times.
9012 if (!c->seg_common)
491 8222 c->nsamples_part_a[i] = b->adapt_pred_order[i];
492 else
493 790 c->nsamples_part_a[i] = b->highest_pred_order;
494 } else {
495 18874 c->bitalloc_part_a[i] = 0;
496 18874 c->nsamples_part_a[i] = 0;
497 }
498
499 // Unpack coding parameter for part B of segment
500 27886 c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
501
502 // Adjust for the linear code
503
4/4
✓ Branch 0 taken 6128 times.
✓ Branch 1 taken 21758 times.
✓ Branch 2 taken 761 times.
✓ Branch 3 taken 5367 times.
27886 if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
504 761 c->bitalloc_part_b[i]++;
505 }
506 }
507
508 // Unpack entropy codes
509
2/2
✓ Branch 0 taken 35182 times.
✓ Branch 1 taken 8612 times.
43794 for (i = 0; i < c->nchannels; i++) {
510 int32_t *part_a, *part_b;
511 int nsamples_part_b;
512
513 // Select index of coding parameters
514
2/2
✓ Branch 0 taken 25355 times.
✓ Branch 1 taken 9827 times.
35182 k = c->seg_common ? 0 : i;
515
516 // Slice the segment into parts A and B
517 35182 part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
518 35182 part_b = part_a + c->nsamples_part_a[k];
519 35182 nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
520
521
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35182 times.
35182 if (get_bits_left(&s->gb) < 0)
522 return AVERROR_INVALIDDATA;
523
524
2/2
✓ Branch 0 taken 9368 times.
✓ Branch 1 taken 25814 times.
35182 if (!c->rice_code_flag[k]) {
525 // Linear codes
526 // Unpack all residuals of part A of segment 0
527 9368 get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
528 c->bitalloc_part_a[k]);
529
530 // Unpack all residuals of part B of segment 0 and others
531 9368 get_linear_array(&s->gb, part_b, nsamples_part_b,
532 c->bitalloc_part_b[k]);
533 } else {
534 // Rice codes
535 // Unpack all residuals of part A of segment 0
536 25814 int ret = get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
537 c->bitalloc_part_a[k]);
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25814 times.
25814 if (ret < 0)
539 return ret;
540
541
2/2
✓ Branch 0 taken 1047 times.
✓ Branch 1 taken 24767 times.
25814 if (c->bitalloc_hybrid_linear[k]) {
542 // Hybrid Rice codes
543 // Unpack the number of isolated samples
544 1047 int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
545
546 // Set all locations to 0
547 1047 memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
548
549 // Extract the locations of isolated samples and flag by -1
550
2/2
✓ Branch 0 taken 5618 times.
✓ Branch 1 taken 1047 times.
6665 for (j = 0; j < nisosamples; j++) {
551 5618 int loc = get_bits(&s->gb, s->nsegsamples_log2);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5618 times.
5618 if (loc >= nsamples_part_b) {
553 av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
554 return AVERROR_INVALIDDATA;
555 }
556 5618 part_b[loc] = -1;
557 }
558
559 // Unpack all residuals of part B of segment 0 and others
560
2/2
✓ Branch 0 taken 153159 times.
✓ Branch 1 taken 1047 times.
154206 for (j = 0; j < nsamples_part_b; j++) {
561
2/2
✓ Branch 0 taken 5618 times.
✓ Branch 1 taken 147541 times.
153159 if (part_b[j])
562 5618 part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
563 else
564 147541 part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
565 }
566 } else {
567 // Rice codes
568 // Unpack all residuals of part B of segment 0 and others
569 24767 ret = get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24767 times.
24767 if (ret < 0)
571 return ret;
572 }
573 }
574 }
575
576 // Unpack decimator history for frequency band 1
577
4/4
✓ Branch 0 taken 2805 times.
✓ Branch 1 taken 5807 times.
✓ Branch 2 taken 83 times.
✓ Branch 3 taken 2722 times.
8612 if (seg == 0 && band == 1) {
578 83 int nbits = get_bits(&s->gb, 5) + 1;
579
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 83 times.
415 for (i = 0; i < c->nchannels; i++)
580
2/2
✓ Branch 0 taken 2324 times.
✓ Branch 1 taken 332 times.
2656 for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
581 2324 c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
582 }
583
584 // Start unpacking LSB portion of the segment
585
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 8064 times.
8612 if (b->lsb_section_size) {
586 // Skip to the start of LSB portion
587
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 548 times.
548 if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
588 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
589 return AVERROR_INVALIDDATA;
590 }
591
592 // Unpack all LSB parts of residuals of this segment
593
2/2
✓ Branch 0 taken 2696 times.
✓ Branch 1 taken 548 times.
3244 for (i = 0; i < c->nchannels; i++) {
594
2/2
✓ Branch 0 taken 1096 times.
✓ Branch 1 taken 1600 times.
2696 if (b->nscalablelsbs[i]) {
595 1096 get_array(&s->gb,
596 1096 b->lsb_sample_buffer[i] + seg * s->nsegsamples,
597 s->nsegsamples, b->nscalablelsbs[i]);
598 }
599 }
600 }
601
602 // Skip to the end of band data
603
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8612 times.
8612 if (ff_dca_seek_bits(&s->gb, band_data_end)) {
604 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
605 return AVERROR_INVALIDDATA;
606 }
607
608 8612 return 0;
609 }
610
611 107 static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
612 {
613 107 DCAXllBand *b = &c->bands[band];
614 int i, offset, nsamples;
615
616
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107 if (seg < 0) {
617 107 offset = 0;
618 107 nsamples = s->nframesamples;
619 } else {
620 offset = seg * s->nsegsamples;
621 nsamples = s->nsegsamples;
622 }
623
624
2/2
✓ Branch 0 taken 383 times.
✓ Branch 1 taken 107 times.
490 for (i = 0; i < c->nchannels; i++) {
625 383 memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 383 times.
383 if (b->lsb_section_size)
627 memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
628 }
629
630
3/4
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 91 times.
107 if (seg <= 0 && band)
631 16 memset(c->deci_history, 0, sizeof(c->deci_history));
632
633
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107 if (seg < 0) {
634 107 memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
635 107 memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
636 }
637 107 }
638
639 2805 static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
640 {
641 2805 DCAXllBand *b = &c->bands[band];
642 2805 int nsamples = s->nframesamples;
643 int i, j, k;
644
645 // Inverse adaptive or fixed prediction
646
2/2
✓ Branch 0 taken 11352 times.
✓ Branch 1 taken 2805 times.
14157 for (i = 0; i < c->nchannels; i++) {
647 11352 int32_t *buf = b->msb_sample_buffer[i];
648 11352 int order = b->adapt_pred_order[i];
649
2/2
✓ Branch 0 taken 9062 times.
✓ Branch 1 taken 2290 times.
11352 if (order > 0) {
650 int coeff[DCA_XLL_ADAPT_PRED_ORDER_MAX];
651 // Conversion from reflection coefficients to direct form coefficients
652
2/2
✓ Branch 0 taken 43003 times.
✓ Branch 1 taken 9062 times.
52065 for (j = 0; j < order; j++) {
653 43003 int rc = b->adapt_refl_coeff[i][j];
654
2/2
✓ Branch 0 taken 70153 times.
✓ Branch 1 taken 43003 times.
113156 for (k = 0; k < (j + 1) / 2; k++) {
655 70153 int tmp1 = coeff[ k ];
656 70153 int tmp2 = coeff[j - k - 1];
657 70153 coeff[ k ] = tmp1 + mul16(rc, tmp2);
658 70153 coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
659 }
660 43003 coeff[j] = rc;
661 }
662 // Inverse adaptive prediction
663
2/2
✓ Branch 0 taken 4793349 times.
✓ Branch 1 taken 9062 times.
4802411 for (j = 0; j < nsamples - order; j++) {
664 4793349 int64_t err = 0;
665
2/2
✓ Branch 0 taken 23241571 times.
✓ Branch 1 taken 4793349 times.
28034920 for (k = 0; k < order; k++)
666 23241571 err += (int64_t)buf[j + k] * coeff[order - k - 1];
667 4793349 buf[j + k] -= (SUINT)clip23(norm16(err));
668 }
669 } else {
670 // Inverse fixed coefficient prediction
671
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2290 times.
2892 for (j = 0; j < b->fixed_pred_order[i]; j++)
672
2/2
✓ Branch 0 taken 338342 times.
✓ Branch 1 taken 602 times.
338944 for (k = 1; k < nsamples; k++)
673 338342 buf[k] += (unsigned)buf[k - 1];
674 }
675 }
676
677 // Inverse pairwise channel decorrelation
678
2/2
✓ Branch 0 taken 2789 times.
✓ Branch 1 taken 16 times.
2805 if (b->decor_enabled) {
679 int32_t *tmp[DCA_XLL_CHANNELS_MAX];
680
681
2/2
✓ Branch 0 taken 5668 times.
✓ Branch 1 taken 2789 times.
8457 for (i = 0; i < c->nchannels / 2; i++) {
682 5668 int coeff = b->decor_coeff[i];
683
2/2
✓ Branch 0 taken 1926 times.
✓ Branch 1 taken 3742 times.
5668 if (coeff) {
684 1926 s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
685 1926 b->msb_sample_buffer[i * 2 ],
686 coeff, nsamples);
687 }
688 }
689
690 // Reorder channel pointers to the original order
691
2/2
✓ Branch 0 taken 11336 times.
✓ Branch 1 taken 2789 times.
14125 for (i = 0; i < c->nchannels; i++)
692 11336 tmp[i] = b->msb_sample_buffer[i];
693
694
2/2
✓ Branch 0 taken 11336 times.
✓ Branch 1 taken 2789 times.
14125 for (i = 0; i < c->nchannels; i++)
695 11336 b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
696 }
697
698 // Map output channel pointers for frequency band 0
699
2/2
✓ Branch 0 taken 2639 times.
✓ Branch 1 taken 166 times.
2805 if (c->nfreqbands == 1)
700
2/2
✓ Branch 0 taken 10688 times.
✓ Branch 1 taken 2639 times.
13327 for (i = 0; i < c->nchannels; i++)
701 10688 s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
702 2805 }
703
704 9154 static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
705 {
706 9154 int adj = c->bands[band].bit_width_adjust[ch];
707 9154 int shift = c->bands[band].nscalablelsbs[ch];
708
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9154 times.
9154 if (s->fixed_lsb_width)
710 shift = s->fixed_lsb_width;
711
3/4
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 8900 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 254 times.
9154 else if (shift && adj)
712 shift += adj - 1;
713 else
714 9154 shift += adj;
715
716 9154 return shift;
717 }
718
719 294 static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
720 {
721 294 DCAXllBand *b = &c->bands[band];
722 294 int n, ch, nsamples = s->nframesamples;
723
724
2/2
✓ Branch 0 taken 1179 times.
✓ Branch 1 taken 294 times.
1473 for (ch = 0; ch < c->nchannels; ch++) {
725 1179 int shift = chs_get_lsb_width(s, c, band, ch);
726
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 1037 times.
1179 if (shift) {
727 142 int32_t *msb = b->msb_sample_buffer[ch];
728
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
142 if (b->nscalablelsbs[ch]) {
729 142 int32_t *lsb = b->lsb_sample_buffer[ch];
730 142 int adj = b->bit_width_adjust[ch];
731
2/2
✓ Branch 0 taken 88064 times.
✓ Branch 1 taken 142 times.
88206 for (n = 0; n < nsamples; n++)
732 88064 msb[n] = msb[n] * (SUINT)(1 << shift) + (lsb[n] << adj);
733 } else {
734 for (n = 0; n < nsamples; n++)
735 msb[n] = msb[n] * (SUINT)(1 << shift);
736 }
737 }
738 }
739 294 }
740
741 83 static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
742 {
743 83 int ch, nsamples = s->nframesamples;
744 int32_t *ptr;
745
746 av_assert1(c->nfreqbands > 1);
747
748 // Reallocate frequency band assembly buffer
749 83 av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
750 83 2 * nsamples * c->nchannels * sizeof(int32_t));
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!c->sample_buffer[2])
752 return AVERROR(ENOMEM);
753
754 // Assemble frequency bands 0 and 1
755 83 ptr = c->sample_buffer[2];
756
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 83 times.
415 for (ch = 0; ch < c->nchannels; ch++) {
757 332 int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
758 332 int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
759
760 // Copy decimator history
761 332 memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
762 332 c->deci_history[ch], sizeof(c->deci_history[0]));
763
764 // Filter
765 332 s->dcadsp->assemble_freq_bands(ptr, band0, band1,
766 ff_dca_xll_band_coeff,
767 nsamples);
768
769 // Remap output channel pointer to assembly buffer
770 332 s->output_samples[c->ch_remap[ch]] = ptr;
771 332 ptr += nsamples * 2;
772 }
773
774 83 return 0;
775 }
776
777 1435 static int parse_common_header(DCAXllDecoder *s)
778 {
779 int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
780
781 // XLL extension sync word
782
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
783 av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
784 return AVERROR(EAGAIN);
785 }
786
787 // Version number
788 1435 stream_ver = get_bits(&s->gb, 4) + 1;
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (stream_ver > 1) {
790 avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
791 return AVERROR_PATCHWELCOME;
792 }
793
794 // Lossless frame header length
795 1435 header_size = get_bits(&s->gb, 8) + 1;
796
797 // Check CRC
798
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if (ff_dca_check_crc(s->avctx, &s->gb, 32, header_size * 8)) {
799 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
800 return AVERROR_INVALIDDATA;
801 }
802
803 // Number of bits used to read frame size
804 1435 frame_size_nbits = get_bits(&s->gb, 5) + 1;
805
806 // Number of bytes in a lossless frame
807 1435 s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
808
2/4
✓ Branch 0 taken 1435 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1435 times.
1435 if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
809 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
810 return AVERROR_INVALIDDATA;
811 }
812 1435 s->frame_size++;
813
814 // Number of channels sets per frame
815 1435 s->nchsets = get_bits(&s->gb, 4) + 1;
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->nchsets > DCA_XLL_CHSETS_MAX) {
817 avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
818 return AVERROR_PATCHWELCOME;
819 }
820
821 // Number of segments per frame
822 1435 nframesegs_log2 = get_bits(&s->gb, 4);
823 1435 s->nframesegs = 1 << nframesegs_log2;
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->nframesegs > 1024) {
825 av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
826 return AVERROR_INVALIDDATA;
827 }
828
829 // Samples in segment per one frequency band for the first channel set
830 // Maximum value is 256 for sampling frequencies <= 48 kHz
831 // Maximum value is 512 for sampling frequencies > 48 kHz
832 1435 s->nsegsamples_log2 = get_bits(&s->gb, 4);
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (!s->nsegsamples_log2) {
834 av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
835 return AVERROR_INVALIDDATA;
836 }
837 1435 s->nsegsamples = 1 << s->nsegsamples_log2;
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->nsegsamples > 512) {
839 av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
840 return AVERROR_INVALIDDATA;
841 }
842
843 // Samples in frame per one frequency band for the first channel set
844 1435 s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
845 1435 s->nframesamples = 1 << s->nframesamples_log2;
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->nframesamples > 65536) {
847 av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
848 return AVERROR_INVALIDDATA;
849 }
850
851 // Number of bits used to read segment size
852 1435 s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
853
854 // Presence of CRC16 within each frequency band
855 // 0 - No CRC16 within band
856 // 1 - CRC16 placed at the end of MSB0
857 // 2 - CRC16 placed at the end of MSB0 and LSB0
858 // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
859 1435 s->band_crc_present = get_bits(&s->gb, 2);
860
861 // MSB/LSB split flag
862 1435 s->scalable_lsbs = get_bits1(&s->gb);
863
864 // Channel position mask
865 1435 s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
866
867 // Fixed LSB width
868
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 1255 times.
1435 if (s->scalable_lsbs)
869 180 s->fixed_lsb_width = get_bits(&s->gb, 4);
870 else
871 1255 s->fixed_lsb_width = 0;
872
873 // Reserved
874 // Byte align
875 // Header CRC16 protection
876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
877 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
878 return AVERROR_INVALIDDATA;
879 }
880
881 1435 return 0;
882 }
883
884 4033 static int is_hier_dmix_chset(DCAXllChSet *c)
885 {
886
4/6
✓ Branch 0 taken 4033 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 387 times.
✓ Branch 3 taken 3646 times.
✓ Branch 4 taken 387 times.
✗ Branch 5 not taken.
4033 return !c->primary_chset && c->dmix_embedded && c->hier_chset;
887 }
888
889 1578 static DCAXllChSet *find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
890 {
891
1/2
✓ Branch 0 taken 1578 times.
✗ Branch 1 not taken.
1578 if (c->hier_chset)
892
2/2
✓ Branch 0 taken 1324 times.
✓ Branch 1 taken 1492 times.
2816 while (++c < &s->chset[s->nchsets])
893
2/2
✓ Branch 1 taken 86 times.
✓ Branch 2 taken 1238 times.
1324 if (is_hier_dmix_chset(c))
894 86 return c;
895
896 1492 return NULL;
897 }
898
899 30 static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
900 {
901 30 int i, j, *coeff_ptr = c->dmix_coeff;
902
903
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (i = 0; i < c->hier_ofs; i++) {
904 60 int scale = o->dmix_scale[i];
905 60 int scale_inv = o->dmix_scale_inv[i];
906 60 c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
907 60 c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
908
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 60 times.
300 for (j = 0; j < c->nchannels; j++) {
909 240 int coeff = mul16(*coeff_ptr, scale_inv);
910 240 *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
911 }
912 }
913 30 }
914
915 1435 static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
916 {
917 1435 DCAContext *dca = s->avctx->priv_data;
918 DCAXllChSet *c;
919 int i, ret;
920
921 // Parse channel set headers
922 1435 s->nfreqbands = 0;
923 1435 s->nchannels = 0;
924 1435 s->nreschsets = 0;
925
2/2
✓ Branch 0 taken 2792 times.
✓ Branch 1 taken 1435 times.
4227 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
926 2792 c->hier_ofs = s->nchannels;
927
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2792 times.
2792 if ((ret = chs_parse_header(s, c, asset)) < 0)
928 return ret;
929
2/2
✓ Branch 0 taken 1435 times.
✓ Branch 1 taken 1357 times.
2792 if (c->nfreqbands > s->nfreqbands)
930 1435 s->nfreqbands = c->nfreqbands;
931
1/2
✓ Branch 0 taken 2792 times.
✗ Branch 1 not taken.
2792 if (c->hier_chset)
932 2792 s->nchannels += c->nchannels;
933
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 1387 times.
2792 if (c->residual_encode != (1 << c->nchannels) - 1)
934 1405 s->nreschsets++;
935 }
936
937 // Pre-scale downmixing coefficients for all non-primary channel sets
938
2/2
✓ Branch 0 taken 1357 times.
✓ Branch 1 taken 1435 times.
2792 for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
939
2/2
✓ Branch 1 taken 180 times.
✓ Branch 2 taken 1177 times.
1357 if (is_hier_dmix_chset(c)) {
940 180 DCAXllChSet *o = find_next_hier_dmix_chset(s, c);
941
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 150 times.
180 if (o)
942 30 prescale_down_mix(c, o);
943 }
944 }
945
946 // Determine number of active channel sets to decode
947
3/3
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 1315 times.
1435 switch (dca->request_channel_layout) {
948 60 case DCA_SPEAKER_LAYOUT_STEREO:
949 60 s->nactivechsets = 1;
950 60 break;
951 60 case DCA_SPEAKER_LAYOUT_5POINT0:
952 case DCA_SPEAKER_LAYOUT_5POINT1:
953
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
60 s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
954 60 break;
955 1315 default:
956 1315 s->nactivechsets = s->nchsets;
957 1315 break;
958 }
959
960 1435 return 0;
961 }
962
963 1435 static int parse_navi_table(DCAXllDecoder *s)
964 {
965 int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
966 DCAXllChSet *c;
967
968 // Determine size of NAVI table
969 1435 navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (navi_nb > 1024) {
971 av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
972 return AVERROR_INVALIDDATA;
973 }
974
975 // Reallocate NAVI table
976 1435 av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
977
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (!s->navi)
978 return AVERROR(ENOMEM);
979
980 // Parse NAVI
981 1435 navi_pos = get_bits_count(&s->gb);
982 1435 navi_ptr = s->navi;
983
2/2
✓ Branch 0 taken 1495 times.
✓ Branch 1 taken 1435 times.
2930 for (band = 0; band < s->nfreqbands; band++) {
984
2/2
✓ Branch 0 taken 4824 times.
✓ Branch 1 taken 1495 times.
6319 for (seg = 0; seg < s->nframesegs; seg++) {
985
2/2
✓ Branch 0 taken 9030 times.
✓ Branch 1 taken 4824 times.
13854 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
986 9030 int size = 0;
987
1/2
✓ Branch 0 taken 9030 times.
✗ Branch 1 not taken.
9030 if (c->nfreqbands > band) {
988 9030 size = get_bits_long(&s->gb, s->seg_size_nbits);
989
2/4
✓ Branch 0 taken 9030 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9030 times.
9030 if (size < 0 || size >= s->frame_size) {
990 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
991 return AVERROR_INVALIDDATA;
992 }
993 9030 size++;
994 }
995 9030 *navi_ptr++ = size;
996 }
997 }
998 }
999
1000 // Byte align
1001 // CRC16
1002 1435 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1003 1435 skip_bits(&s->gb, 16);
1004
1005 // Check CRC
1006
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1435 times.
1435 if (ff_dca_check_crc(s->avctx, &s->gb, navi_pos, get_bits_count(&s->gb))) {
1007 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
1008 return AVERROR_INVALIDDATA;
1009 }
1010
1011 1435 return 0;
1012 }
1013
1014 1435 static int parse_band_data(DCAXllDecoder *s)
1015 {
1016 int ret, chs, seg, band, navi_pos, *navi_ptr;
1017 DCAXllChSet *c;
1018
1019
2/2
✓ Branch 0 taken 2722 times.
✓ Branch 1 taken 1435 times.
4157 for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
1020
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2722 times.
2722 if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1021 return ret;
1022
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2722 times.
2722 if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1023 return ret;
1024 }
1025
1026 1435 navi_pos = get_bits_count(&s->gb);
1027 1435 navi_ptr = s->navi;
1028
2/2
✓ Branch 0 taken 1495 times.
✓ Branch 1 taken 1435 times.
2930 for (band = 0; band < s->nfreqbands; band++) {
1029
2/2
✓ Branch 0 taken 4824 times.
✓ Branch 1 taken 1495 times.
6319 for (seg = 0; seg < s->nframesegs; seg++) {
1030
2/2
✓ Branch 0 taken 9030 times.
✓ Branch 1 taken 4824 times.
13854 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1031
1/2
✓ Branch 0 taken 9030 times.
✗ Branch 1 not taken.
9030 if (c->nfreqbands > band) {
1032 9030 navi_pos += *navi_ptr * 8;
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9030 times.
9030 if (navi_pos > s->gb.size_in_bits) {
1034 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
1035 return AVERROR_INVALIDDATA;
1036 }
1037
3/4
✓ Branch 0 taken 8612 times.
✓ Branch 1 taken 418 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8612 times.
17642 if (chs < s->nactivechsets &&
1038 8612 (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
1039 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1040 return ret;
1041 chs_clear_band_data(s, c, band, seg);
1042 }
1043 9030 skip_bits_long(&s->gb, navi_pos - get_bits_count(&s->gb));
1044 }
1045 9030 navi_ptr++;
1046 }
1047 }
1048 }
1049
1050 1435 return 0;
1051 }
1052
1053 1435 static int parse_frame(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1054 {
1055 int ret;
1056
1057
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1058 return ret;
1059
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = parse_common_header(s)) < 0)
1060 return ret;
1061
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = parse_sub_headers(s, asset)) < 0)
1062 return ret;
1063
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = parse_navi_table(s)) < 0)
1064 return ret;
1065
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = parse_band_data(s)) < 0)
1066 return ret;
1067
1068
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if (s->frame_size * 8 > FFALIGN(get_bits_count(&s->gb), 32)) {
1069 unsigned int extradata_syncword;
1070
1071 // Align to dword
1072 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1073
1074 extradata_syncword = show_bits_long(&s->gb, 32);
1075
1076 if (extradata_syncword == DCA_SYNCWORD_XLL_X) {
1077 s->x_syncword_present = 1;
1078 } else if ((extradata_syncword >> 1) == (DCA_SYNCWORD_XLL_X_IMAX >> 1)) {
1079 s->x_imax_syncword_present = 1;
1080 }
1081 }
1082
1083
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1084 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
1085 return AVERROR_INVALIDDATA;
1086 }
1087 1435 return ret;
1088 }
1089
1090 155 static void clear_pbr(DCAXllDecoder *s)
1091 {
1092 155 s->pbr_length = 0;
1093 155 s->pbr_delay = 0;
1094 155 }
1095
1096 static int copy_to_pbr(DCAXllDecoder *s, const uint8_t *data, int size, int delay)
1097 {
1098 if (size > DCA_XLL_PBR_BUFFER_MAX)
1099 return AVERROR(ENOSPC);
1100
1101 if (!s->pbr_buffer && !(s->pbr_buffer = av_malloc(DCA_XLL_PBR_BUFFER_MAX + AV_INPUT_BUFFER_PADDING_SIZE)))
1102 return AVERROR(ENOMEM);
1103
1104 memcpy(s->pbr_buffer, data, size);
1105 memset(s->pbr_buffer + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1106 s->pbr_length = size;
1107 s->pbr_delay = delay;
1108 return 0;
1109 }
1110
1111 1435 static int parse_frame_no_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1112 {
1113 1435 int ret = parse_frame(s, data, size, asset);
1114
1115 // If XLL packet data didn't start with a sync word, we must have jumped
1116 // right into the middle of PBR smoothing period
1117
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1435 if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
1118 // Skip to the next sync word in this packet
1119 data += asset->xll_sync_offset;
1120 size -= asset->xll_sync_offset;
1121
1122 // If decoding delay is set, put the frame into PBR buffer and return
1123 // failure code. Higher level decoder is expected to switch to lossy
1124 // core decoding or mute its output until decoding delay expires.
1125 if (asset->xll_delay_nframes > 0) {
1126 if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
1127 return ret;
1128 return AVERROR(EAGAIN);
1129 }
1130
1131 // No decoding delay, just parse the frame in place
1132 ret = parse_frame(s, data, size, asset);
1133 }
1134
1135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (ret < 0)
1136 return ret;
1137
1138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->frame_size > size)
1139 return AVERROR(EINVAL);
1140
1141 // If the XLL decoder didn't consume full packet, start PBR smoothing period
1142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->frame_size < size)
1143 if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
1144 return ret;
1145
1146 1435 return 0;
1147 }
1148
1149 static int parse_frame_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1150 {
1151 int ret;
1152
1153 if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
1154 ret = AVERROR(ENOSPC);
1155 goto fail;
1156 }
1157
1158 memcpy(s->pbr_buffer + s->pbr_length, data, size);
1159 s->pbr_length += size;
1160 memset(s->pbr_buffer + s->pbr_length, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1161
1162 // Respect decoding delay after synchronization error
1163 if (s->pbr_delay > 0 && --s->pbr_delay)
1164 return AVERROR(EAGAIN);
1165
1166 if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
1167 goto fail;
1168
1169 if (s->frame_size > s->pbr_length) {
1170 ret = AVERROR(EINVAL);
1171 goto fail;
1172 }
1173
1174 if (s->frame_size == s->pbr_length) {
1175 // End of PBR smoothing period
1176 clear_pbr(s);
1177 } else {
1178 s->pbr_length -= s->frame_size;
1179 memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
1180 }
1181
1182 return 0;
1183
1184 fail:
1185 // For now, throw out all PBR state on failure.
1186 // Perhaps we can be smarter and try to resync somehow.
1187 clear_pbr(s);
1188 return ret;
1189 }
1190
1191 1435 int ff_dca_xll_parse(DCAXllDecoder *s, const uint8_t *data, DCAExssAsset *asset)
1192 {
1193 int ret;
1194
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->hd_stream_id != asset->hd_stream_id) {
1196 clear_pbr(s);
1197 s->hd_stream_id = asset->hd_stream_id;
1198 }
1199
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->pbr_length)
1201 ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1202 else
1203 1435 ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1204
1205 1435 return ret;
1206 }
1207
1208 92 static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1209 {
1210 92 int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
1211 DCAXllChSet *c;
1212
1213
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103 times.
103 if (!c->hier_chset)
1215 continue;
1216
1217 av_assert1(band < c->nfreqbands);
1218
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 103 times.
463 for (j = 0; j < c->nchannels; j++) {
1219
2/2
✓ Branch 0 taken 846 times.
✓ Branch 1 taken 360 times.
1206 for (k = 0; k < o->nchannels; k++) {
1220 846 int coeff = *coeff_ptr++;
1221
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 662 times.
846 if (coeff) {
1222 184 s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
1223 184 o->bands[band].msb_sample_buffer[k],
1224 184 coeff, s->nframesamples);
1225
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 152 times.
184 if (band)
1226 32 s->dcadsp->dmix_sub(c->deci_history[j],
1227 32 o->deci_history[k],
1228 coeff, DCA_XLL_DECI_HISTORY_MAX);
1229 }
1230 }
1231 }
1232
1233 103 nchannels += c->nchannels;
1234
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 11 times.
103 if (nchannels >= o->hier_ofs)
1235 92 break;
1236 }
1237 92 }
1238
1239 50 static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1240 {
1241 50 int i, j, nchannels = 0;
1242 DCAXllChSet *c;
1243
1244
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!c->hier_chset)
1246 continue;
1247
1248 av_assert1(band < c->nfreqbands);
1249
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 55 times.
295 for (j = 0; j < c->nchannels; j++) {
1250 240 int scale = o->dmix_scale[nchannels++];
1251
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 60 times.
240 if (scale != (1 << 15)) {
1252 180 s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
1253 180 scale, s->nframesamples);
1254
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 170 times.
180 if (band)
1255 10 s->dcadsp->dmix_scale(c->deci_history[j],
1256 scale, DCA_XLL_DECI_HISTORY_MAX);
1257 }
1258 }
1259
1260
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
55 if (nchannels >= o->hier_ofs)
1261 50 break;
1262 }
1263 50 }
1264
1265 // Clear all band data and replace non-residual encoded channels with lossy
1266 // counterparts
1267 91 static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
1268 {
1269 91 DCAContext *dca = s->avctx->priv_data;
1270 int band, ch;
1271
1272
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 91 times.
198 for (band = 0; band < c->nfreqbands; band++)
1273 107 chs_clear_band_data(s, c, band, -1);
1274
1275
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 91 times.
428 for (ch = 0; ch < c->nchannels; ch++) {
1276
2/2
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 112 times.
337 if (!(c->residual_encode & (1 << ch)))
1277 225 continue;
1278
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 70 times.
112 if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
1279 42 continue;
1280 70 c->residual_encode &= ~(1 << ch);
1281 }
1282 91 }
1283
1284 1398 static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
1285 {
1286 1398 DCAContext *dca = s->avctx->priv_data;
1287 1398 int ch, nsamples = s->nframesamples;
1288 DCAXllChSet *o;
1289
1290 // Verify that core is compatible
1291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1398 times.
1398 if (!(dca->packet & DCA_PACKET_CORE)) {
1292 av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
1293 return AVERROR(EINVAL);
1294 }
1295
1296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1398 times.
1398 if (c->freq != dca->core.output_rate) {
1297 av_log(s->avctx, AV_LOG_WARNING, "Sample rate mismatch between core (%d Hz) and XLL (%d Hz)\n", dca->core.output_rate, c->freq);
1298 return AVERROR_INVALIDDATA;
1299 }
1300
1301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1398 times.
1398 if (nsamples != dca->core.npcmsamples) {
1302 av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
1303 return AVERROR_INVALIDDATA;
1304 }
1305
1306 // See if this channel set is downmixed and find the next channel set in
1307 // hierarchy. If downmixed, undo core pre-scaling before combining with
1308 // residual (residual is not scaled).
1309 1398 o = find_next_hier_dmix_chset(s, c);
1310
1311 // Reduce core bit width and combine with residual
1312
2/2
✓ Branch 0 taken 8174 times.
✓ Branch 1 taken 1398 times.
9572 for (ch = 0; ch < c->nchannels; ch++) {
1313 int n, spkr, shift, round;
1314 int32_t *src, *dst;
1315
1316
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 7975 times.
8174 if (c->residual_encode & (1 << ch))
1317 199 continue;
1318
1319 // Map this channel to core speaker
1320 7975 spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7975 times.
7975 if (spkr < 0) {
1322 av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
1323 return AVERROR_INVALIDDATA;
1324 }
1325
1326 // Account for LSB width
1327 7975 shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7975 times.
7975 if (shift > 24) {
1329 av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
1330 return AVERROR_INVALIDDATA;
1331 }
1332
1333
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 7624 times.
7975 round = shift > 0 ? 1 << (shift - 1) : 0;
1334
1335 7975 src = dca->core.output_samples[spkr];
1336 7975 dst = c->bands[0].msb_sample_buffer[ch];
1337
2/2
✓ Branch 0 taken 241 times.
✓ Branch 1 taken 7734 times.
7975 if (o) {
1338 // Undo embedded core downmix pre-scaling
1339 241 int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
1340
2/2
✓ Branch 0 taken 157184 times.
✓ Branch 1 taken 241 times.
157425 for (n = 0; n < nsamples; n++)
1341 157184 dst[n] += (SUINT)clip23((mul16(src[n], scale_inv) + round) >> shift);
1342 } else {
1343 // No downmix scaling
1344
2/2
✓ Branch 0 taken 4163584 times.
✓ Branch 1 taken 7734 times.
4171318 for (n = 0; n < nsamples; n++)
1345 4163584 dst[n] += (unsigned)((src[n] + round) >> shift);
1346 }
1347 }
1348
1349 1398 return 0;
1350 }
1351
1352 1435 int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
1353 {
1354 1435 AVCodecContext *avctx = s->avctx;
1355 1435 DCAContext *dca = avctx->priv_data;
1356 1435 DCAExssAsset *asset = &dca->exss.assets[0];
1357 1435 DCAXllChSet *p = &s->chset[0], *c;
1358 1435 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
1359 int i, j, k, ret, shift, nsamples, request_mask;
1360 int ch_remap[DCA_SPEAKER_COUNT];
1361
1362 // Force lossy downmixed output during recovery
1363
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1384 times.
1435 if (dca->packet & DCA_PACKET_RECOVERY) {
1364
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 51 times.
162 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
1365
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 20 times.
111 if (i < s->nactivechsets)
1366 91 force_lossy_output(s, c);
1367
1368
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 51 times.
111 if (!c->primary_chset)
1369 60 c->dmix_embedded = 0;
1370 }
1371
1372 51 s->scalable_lsbs = 0;
1373 51 s->fixed_lsb_width = 0;
1374 }
1375
1376 // Filter frequency bands for active channel sets
1377 1435 s->output_mask = 0;
1378
2/2
✓ Branch 0 taken 2722 times.
✓ Branch 1 taken 1435 times.
4157 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1379 2722 chs_filter_band_data(s, c, 0);
1380
1381
2/2
✓ Branch 0 taken 1398 times.
✓ Branch 1 taken 1324 times.
2722 if (c->residual_encode != (1 << c->nchannels) - 1
1382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1398 times.
1398 && (ret = combine_residual_frame(s, c)) < 0)
1383 return ret;
1384
1385
2/2
✓ Branch 0 taken 211 times.
✓ Branch 1 taken 2511 times.
2722 if (s->scalable_lsbs)
1386 211 chs_assemble_msbs_lsbs(s, c, 0);
1387
1388
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 2639 times.
2722 if (c->nfreqbands > 1) {
1389 83 chs_filter_band_data(s, c, 1);
1390 83 chs_assemble_msbs_lsbs(s, c, 1);
1391 }
1392
1393 2722 s->output_mask |= c->ch_mask;
1394 }
1395
1396 // Undo hierarchial downmix and/or apply scaling
1397
2/2
✓ Branch 0 taken 1352 times.
✓ Branch 1 taken 1390 times.
2742 for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
1398
2/2
✓ Branch 1 taken 1231 times.
✓ Branch 2 taken 121 times.
1352 if (!is_hier_dmix_chset(c))
1399 1231 continue;
1400
1401
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 76 times.
121 if (i >= s->nactivechsets) {
1402
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 45 times.
95 for (j = 0; j < c->nfreqbands; j++)
1403
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (c->bands[j].dmix_embedded)
1404 50 scale_down_mix(s, c, j);
1405 45 break;
1406 }
1407
1408
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 76 times.
168 for (j = 0; j < c->nfreqbands; j++)
1409
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 if (c->bands[j].dmix_embedded)
1410 92 undo_down_mix(s, c, j);
1411 }
1412
1413 // Assemble frequency bands for active channel sets
1414
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1375 times.
1435 if (s->nfreqbands > 1) {
1415
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 60 times.
143 for (i = 0; i < s->nactivechsets; i++)
1416
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
83 if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
1417 return ret;
1418 }
1419
1420 // Normalize to regular 5.1 layout if downmixing
1421
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 1315 times.
1435 if (dca->request_channel_layout) {
1422
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 85 times.
120 if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
1423 35 s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
1424 35 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
1425 }
1426
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 85 times.
120 if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
1427 35 s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
1428 35 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
1429 }
1430 }
1431
1432 // Handle downmixing to stereo request
1433
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1375 times.
1435 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
1434
3/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 53 times.
60 && DCA_HAS_STEREO(s->output_mask) && p->dmix_embedded
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1436 p->dmix_type == DCA_DMIX_TYPE_LtRt))
1437 7 request_mask = DCA_SPEAKER_LAYOUT_STEREO;
1438 else
1439 1428 request_mask = s->output_mask;
1440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
1441 return AVERROR(EINVAL);
1442
1443 1435 avctx->sample_rate = p->freq << (s->nfreqbands - 1);
1444
1445
2/3
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 1375 times.
✗ Branch 2 not taken.
1435 switch (p->storage_bit_res) {
1446 60 case 16:
1447 60 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1448 60 shift = 16 - p->pcm_bit_res;
1449 60 break;
1450 1375 case 20:
1451 case 24:
1452 1375 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1453 1375 shift = 24 - p->pcm_bit_res;
1454 1375 break;
1455 default:
1456 return AVERROR(EINVAL);
1457 }
1458
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (s->x_imax_syncword_present) {
1460 avctx->profile = AV_PROFILE_DTS_HD_MA_X_IMAX;
1461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 } else if (s->x_syncword_present) {
1462 avctx->profile = AV_PROFILE_DTS_HD_MA_X;
1463 } else {
1464 1435 avctx->profile = AV_PROFILE_DTS_HD_MA;
1465 }
1466
1467 1435 avctx->bits_per_raw_sample = p->storage_bit_res;
1468 1435 avctx->bit_rate = 0;
1469
1470 1435 frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
1471
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1472 return ret;
1473
1474 // Downmix primary channel set to stereo
1475
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1428 times.
1435 if (request_mask != s->output_mask) {
1476 7 ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
1477 7 p->dmix_coeff, nsamples,
1478 s->output_mask);
1479 }
1480
1481
2/2
✓ Branch 0 taken 10992 times.
✓ Branch 1 taken 1435 times.
12427 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
1482 10992 int32_t *samples = s->output_samples[ch_remap[i]];
1483
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 10660 times.
10992 if (frame->format == AV_SAMPLE_FMT_S16P) {
1484 332 int16_t *plane = (int16_t *)frame->extended_data[i];
1485
2/2
✓ Branch 0 taken 679936 times.
✓ Branch 1 taken 332 times.
680268 for (k = 0; k < nsamples; k++)
1486 679936 plane[k] = av_clip_int16(samples[k] * (SUINT)(1 << shift));
1487 } else {
1488 10660 int32_t *plane = (int32_t *)frame->extended_data[i];
1489
2/2
✓ Branch 0 taken 5713920 times.
✓ Branch 1 taken 10660 times.
5724580 for (k = 0; k < nsamples; k++)
1490 5713920 plane[k] = clip23(samples[k] * (SUINT)(1 << shift)) * (1 << 8);
1491 }
1492 }
1493
1494
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1435 times.
1435 if (!asset->one_to_one_map_ch_to_spkr) {
1495 if (asset->representation_type == DCA_REPR_TYPE_LtRt)
1496 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1497 else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
1498 matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1499
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1428 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
1435 } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
1500 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1501 }
1502
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1435 times.
1435 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1503 return ret;
1504
1505 1435 return 0;
1506 }
1507
1508 av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
1509 {
1510 clear_pbr(s);
1511 }
1512
1513 155 av_cold void ff_dca_xll_close(DCAXllDecoder *s)
1514 {
1515 DCAXllChSet *c;
1516 int i, j;
1517
1518
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 155 times.
620 for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
1519
2/2
✓ Branch 0 taken 1395 times.
✓ Branch 1 taken 465 times.
1860 for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1520 1395 av_freep(&c->sample_buffer[j]);
1521 1395 c->sample_size[j] = 0;
1522 }
1523 }
1524
1525 155 av_freep(&s->navi);
1526 155 s->navi_size = 0;
1527
1528 155 av_freep(&s->pbr_buffer);
1529 155 clear_pbr(s);
1530 155 }
1531