FFmpeg coverage


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