FFmpeg coverage


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