FFmpeg coverage


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