FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_xll.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 615 814 75.6%
Functions: 33 36 91.7%
Branches: 471 642 73.4%

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