FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dca_xll.c
Date: 2026-11-01 05:14:56
Exec Total Coverage
Lines: 710 942 75.4%
Functions: 34 37 91.9%
Branches: 540 750 72.0%

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 #define DCA_XLL_X_HEADER_SIZE 22
32 #define DCA_XLL_X_HEIGHT_FOLD 23170
33 #define DCA_XLL_X_HEIGHT_MASK (DCA_SPEAKER_MASK_Lh | DCA_SPEAKER_MASK_Rh | \
34 DCA_SPEAKER_MASK_Lhr | DCA_SPEAKER_MASK_Rhr)
35
36 static const uint8_t x_height_spkr[4] = {
37 DCA_SPEAKER_Lh, DCA_SPEAKER_Rh, DCA_SPEAKER_Lhr, DCA_SPEAKER_Rhr
38 };
39
40 181313 static int get_linear(GetBitContext *gb, int n)
41 {
42 181313 unsigned int v = get_bits_long(gb, n);
43 181313 return (v >> 1) ^ -(v & 1);
44 }
45
46 9666010 static int get_rice_un(GetBitContext *gb, int k)
47 {
48 9666010 unsigned int v = get_unary(gb, 1, get_bits_left(gb));
49 9666010 return (v << k) | get_bits_long(gb, k);
50 }
51
52 9666010 static int get_rice(GetBitContext *gb, int k)
53 {
54 9666010 unsigned int v = get_rice_un(gb, k);
55 9666010 return (v >> 1) ^ -(v & 1);
56 }
57
58 1096 static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
59 {
60 int i;
61
62
2/2
✓ Branch 0 taken 88064 times.
✓ Branch 1 taken 1096 times.
89160 for (i = 0; i < size; i++)
63 88064 array[i] = get_bits(gb, n);
64 1096 }
65
66 26984 static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
67 {
68 int i;
69
70
2/2
✓ Branch 0 taken 26089 times.
✓ Branch 1 taken 895 times.
26984 if (n == 0)
71 26089 memset(array, 0, sizeof(*array) * size);
72
2/2
✓ Branch 0 taken 86338 times.
✓ Branch 1 taken 895 times.
87233 else for (i = 0; i < size; i++)
73 86338 array[i] = get_linear(gb, n);
74 26984 }
75
76 91757 static int get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
77 {
78 int i;
79
80
3/4
✓ Branch 0 taken 9411270 times.
✓ Branch 1 taken 91757 times.
✓ Branch 3 taken 9411270 times.
✗ Branch 4 not taken.
9503027 for (i = 0; i < size && get_bits_left(gb) > k; i++)
81 9411270 array[i] = get_rice(gb, k);
82
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91757 times.
91757 if (i < size)
84 ✗ return AVERROR_INVALIDDATA;
85 91757 return 0;
86 }
87
88 225 static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
89 {
90 // Size of downmix coefficient matrix
91
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 186 times.
225 int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
92 225 int i, j, *coeff_ptr = c->dmix_coeff;
93
94
2/2
✓ Branch 0 taken 954 times.
✓ Branch 1 taken 225 times.
1179 for (i = 0; i < m; i++) {
95 954 int code, sign, coeff, scale, scale_inv = 0;
96 unsigned int index;
97
98 // Downmix scale (only for non-primary channel sets)
99
2/2
✓ Branch 0 taken 876 times.
✓ Branch 1 taken 78 times.
954 if (!c->primary_chset) {
100 876 code = get_bits(&s->gb, 9);
101 876 sign = (code >> 8) - 1;
102 876 index = (code & 0xff) - FF_DCA_DMIXTABLE_OFFSET;
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 876 times.
876 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
104 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");
105 ✗ return AVERROR_INVALIDDATA;
106 }
107 876 scale = ff_dca_dmixtable[index + FF_DCA_DMIXTABLE_OFFSET];
108 876 scale_inv = ff_dca_inv_dmixtable[index];
109 876 c->dmix_scale[i] = (scale ^ sign) - sign;
110 876 c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;
111 }
112
113 // Downmix coefficients
114
2/2
✓ Branch 0 taken 2280 times.
✓ Branch 1 taken 954 times.
3234 for (j = 0; j < c->nchannels; j++) {
115 2280 code = get_bits(&s->gb, 9);
116 2280 sign = (code >> 8) - 1;
117 2280 index = code & 0xff;
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2280 times.
2280 if (index >= FF_DCA_DMIXTABLE_SIZE) {
119 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");
120 ✗ return AVERROR_INVALIDDATA;
121 }
122 2280 coeff = ff_dca_dmixtable[index];
123
2/2
✓ Branch 0 taken 1812 times.
✓ Branch 1 taken 468 times.
2280 if (!c->primary_chset)
124 // Multiply by |InvDmixScale| to get |UndoDmixScale|
125 1812 coeff = mul16(scale_inv, coeff);
126 2280 *coeff_ptr++ = (coeff ^ sign) - sign;
127 }
128 }
129
130 225 return 0;
131 }
132
133 5162 static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
134 {
135 5162 int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
136 5162 DCAXllChSet *p = &s->chset[0];
137 DCAXllBand *b;
138
139 // Size of channel set sub-header
140 5162 header_size = get_bits(&s->gb, 10) + 1;
141
142 // Check CRC
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5162 times.
5162 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
144 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");
145 ✗ return AVERROR_INVALIDDATA;
146 }
147
148 // Number of channels in the channel set
149 5162 c->nchannels = get_bits(&s->gb, 4) + 1;
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5162 times.
5162 if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
151 ✗ avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
152 ✗ return AVERROR_PATCHWELCOME;
153 }
154
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5159 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
5162 if (c == &s->x_chset && c->nchannels != 4) {
155 ✗ av_log(s->avctx, AV_LOG_ERROR, "DTS:X height channel set with %d channels\n", c->nchannels);
156 ✗ return AVERROR_INVALIDDATA;
157 }
158
159 // Residual type
160 5162 c->residual_encode = get_bits(&s->gb, c->nchannels);
161
162 // PCM bit resolution
163 5162 c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
164
165 // Storage unit width
166 5162 c->storage_bit_res = get_bits(&s->gb, 5) + 1;
167
4/6
✓ Branch 0 taken 5072 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 5072 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5072 times.
5162 if (c->storage_bit_res != 16 && c->storage_bit_res != 20 && c->storage_bit_res != 24) {
168 ✗ avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);
169 ✗ return AVERROR_PATCHWELCOME;
170 }
171
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5162 times.
5162 if (c->pcm_bit_res > c->storage_bit_res) {
173 ✗ 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);
174 ✗ return AVERROR_INVALIDDATA;
175 }
176
177 // Original sampling frequency
178 5162 c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5162 times.
5162 if (c->freq > 192000) {
180 ✗ avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
181 ✗ return AVERROR_PATCHWELCOME;
182 }
183
184 // Sampling frequency modifier
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5162 times.
5162 if (get_bits(&s->gb, 2)) {
186 ✗ avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
187 ✗ return AVERROR_PATCHWELCOME;
188 }
189
190 // Which replacement set this channel set is member of
191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5162 times.
5162 if (get_bits(&s->gb, 2)) {
192 ✗ avpriv_request_sample(s->avctx, "XLL replacement set");
193 ✗ return AVERROR_PATCHWELCOME;
194 }
195
196
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5159 times.
5162 if (c == &s->x_chset) {
197 // The height channel set of the DTS:X standard presentation carries
198 // no speaker mask: its channels are the front and rear height pairs.
199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits1(&s->gb)) {
200 ✗ avpriv_request_sample(s->avctx, "DTS:X height channel set with mapping coefficients");
201 ✗ return AVERROR_PATCHWELCOME;
202 }
203 3 c->primary_chset = 1;
204 3 c->dmix_coeffs_present = 0;
205 3 c->dmix_embedded = 0;
206 3 c->hier_chset = 0;
207 3 c->ch_mask = DCA_XLL_X_HEIGHT_MASK;
208
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (i = 0; i < 4; i++)
209 12 c->ch_remap[i] = x_height_spkr[i];
210
1/2
✓ Branch 0 taken 5159 times.
✗ Branch 1 not taken.
5159 } else if (asset->one_to_one_map_ch_to_spkr) {
211 // Primary channel set flag
212 5159 c->primary_chset = get_bits1(&s->gb);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5159 times.
5159 if (c->primary_chset != (c == p)) {
214 ✗ av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");
215 ✗ return AVERROR_INVALIDDATA;
216 }
217
218 // Downmix coefficients present in stream
219 5159 c->dmix_coeffs_present = get_bits1(&s->gb);
220
221 // Downmix already performed by encoder
222
3/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 4934 times.
✓ Branch 3 taken 225 times.
✗ Branch 4 not taken.
5159 c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
223
224 // Downmix type
225
4/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 4934 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 186 times.
5159 if (c->dmix_coeffs_present && c->primary_chset) {
226 39 c->dmix_type = get_bits(&s->gb, 3);
227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {
228 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");
229 ✗ return AVERROR_INVALIDDATA;
230 }
231 }
232
233 // Whether the channel set is part of a hierarchy
234 5159 c->hier_chset = get_bits1(&s->gb);
235
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5159 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5159 if (!c->hier_chset && s->nchsets != 1) {
236 ✗ avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
237 ✗ return AVERROR_PATCHWELCOME;
238 }
239
240 // Downmix coefficients
241
3/4
✓ Branch 0 taken 225 times.
✓ Branch 1 taken 4934 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 225 times.
5159 if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
242 ✗ return ret;
243
244 // Channel mask enabled
245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5159 times.
5159 if (!get_bits1(&s->gb)) {
246 ✗ avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
247 ✗ return AVERROR_PATCHWELCOME;
248 }
249
250 // Channel mask for set
251 5159 c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5159 times.
5159 if (av_popcount(c->ch_mask) != c->nchannels) {
253 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");
254 ✗ return AVERROR_INVALIDDATA;
255 }
256
257 // Build the channel to speaker map
258
2/2
✓ Branch 0 taken 93224 times.
✓ Branch 1 taken 5159 times.
98383 for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
259
2/2
✓ Branch 0 taken 20660 times.
✓ Branch 1 taken 72564 times.
93224 if (c->ch_mask & (1U << i))
260 20660 c->ch_remap[j++] = i;
261 } else {
262 // Mapping coeffs present flag
263 ✗ if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {
264 ✗ avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");
265 ✗ return AVERROR_PATCHWELCOME;
266 }
267
268 // Setup for LtRt decoding
269 ✗ c->primary_chset = 1;
270 ✗ c->dmix_coeffs_present = 0;
271 ✗ c->dmix_embedded = 0;
272 ✗ c->hier_chset = 0;
273 ✗ c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;
274 ✗ c->ch_remap[0] = DCA_SPEAKER_L;
275 ✗ c->ch_remap[1] = DCA_SPEAKER_R;
276 }
277
278
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 5072 times.
5162 if (c->freq > 96000) {
279 // Extra frequency bands flag
280
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 if (get_bits1(&s->gb)) {
281 ✗ avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
282 ✗ return AVERROR_PATCHWELCOME;
283 }
284 90 c->nfreqbands = 2;
285 } else {
286 5072 c->nfreqbands = 1;
287 }
288
289 // Set the sampling frequency to that of the first frequency band.
290 // Frequency will be doubled again after bands assembly.
291 5162 c->freq >>= c->nfreqbands - 1;
292
293 // Verify that all channel sets have the same audio characteristics
294 // The DTS:X height channel set may carry a lower PCM resolution.
295
4/6
✓ Branch 0 taken 2539 times.
✓ Branch 1 taken 2623 times.
✓ Branch 2 taken 2539 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2539 times.
✗ Branch 5 not taken.
5162 if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2539 times.
2539 || (c->pcm_bit_res != p->pcm_bit_res &&
297 ✗ (c != &s->x_chset || c->pcm_bit_res > p->pcm_bit_res))
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2539 times.
2539 || c->storage_bit_res != p->storage_bit_res)) {
299 ✗ avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
300 ✗ return AVERROR_PATCHWELCOME;
301 }
302
303 // Determine number of bits to read bit allocation coding parameter
304
2/2
✓ Branch 0 taken 5072 times.
✓ Branch 1 taken 90 times.
5162 if (c->storage_bit_res > 16)
305 5072 c->nabits = 5;
306
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 else if (c->storage_bit_res > 8)
307 90 c->nabits = 4;
308 else
309 ✗ c->nabits = 3;
310
311 // Account for embedded downmix and decimator saturation
312
6/6
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 5045 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 4985 times.
5162 if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
313 90 c->nabits++;
314
315
2/2
✓ Branch 0 taken 5252 times.
✓ Branch 1 taken 5162 times.
10414 for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
316 // Pairwise channel decorrelation
317
3/4
✓ Branch 1 taken 5222 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 5222 times.
✗ Branch 4 not taken.
10474 if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
318 5222 int ch_nbits = av_ceil_log2(c->nchannels);
319
320 // Original channel order
321
2/2
✓ Branch 0 taken 21002 times.
✓ Branch 1 taken 5222 times.
26224 for (i = 0; i < c->nchannels; i++) {
322 21002 b->orig_order[i] = get_bits(&s->gb, ch_nbits);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21002 times.
21002 if (b->orig_order[i] >= c->nchannels) {
324 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");
325 ✗ return AVERROR_INVALIDDATA;
326 }
327 }
328
329 // Pairwise channel coefficients
330
2/2
✓ Branch 0 taken 10501 times.
✓ Branch 1 taken 5222 times.
15723 for (i = 0; i < c->nchannels / 2; i++)
331
2/2
✓ Branch 1 taken 3699 times.
✓ Branch 2 taken 6802 times.
10501 b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
332 } else {
333
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
60 for (i = 0; i < c->nchannels; i++)
334 30 b->orig_order[i] = i;
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 for (i = 0; i < c->nchannels / 2; i++)
336 ✗ b->decor_coeff[i] = 0;
337 }
338
339 // Adaptive predictor order
340 5252 b->highest_pred_order = 0;
341
2/2
✓ Branch 0 taken 21032 times.
✓ Branch 1 taken 5252 times.
26284 for (i = 0; i < c->nchannels; i++) {
342 21032 b->adapt_pred_order[i] = get_bits(&s->gb, 4);
343
2/2
✓ Branch 0 taken 7988 times.
✓ Branch 1 taken 13044 times.
21032 if (b->adapt_pred_order[i] > b->highest_pred_order)
344 7988 b->highest_pred_order = b->adapt_pred_order[i];
345 }
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5252 times.
5252 if (b->highest_pred_order > s->nsegsamples) {
347 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive prediction order\n");
348 ✗ return AVERROR_INVALIDDATA;
349 }
350
351 // Fixed predictor order
352
2/2
✓ Branch 0 taken 21032 times.
✓ Branch 1 taken 5252 times.
26284 for (i = 0; i < c->nchannels; i++)
353
2/2
✓ Branch 0 taken 3442 times.
✓ Branch 1 taken 17590 times.
21032 b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
354
355 // Adaptive predictor quantized reflection coefficients
356
2/2
✓ Branch 0 taken 21032 times.
✓ Branch 1 taken 5252 times.
26284 for (i = 0; i < c->nchannels; i++) {
357
2/2
✓ Branch 0 taken 81510 times.
✓ Branch 1 taken 21032 times.
102542 for (j = 0; j < b->adapt_pred_order[i]; j++) {
358 81510 k = get_linear(&s->gb, 8);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81510 times.
81510 if (k == -128) {
360 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
361 ✗ return AVERROR_INVALIDDATA;
362 }
363
2/2
✓ Branch 0 taken 29252 times.
✓ Branch 1 taken 52258 times.
81510 if (k < 0)
364 29252 b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
365 else
366 52258 b->adapt_refl_coeff[i][j] = (int)ff_dca_xll_refl_coeff[ k];
367 }
368 }
369
370 // Downmix performed by encoder in extension frequency band
371
5/6
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 4997 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 225 times.
✓ Branch 5 taken 30 times.
✗ Branch 6 not taken.
5252 b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
372
373 // MSB/LSB split flag in extension frequency band
374
7/8
✓ Branch 0 taken 5162 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 4778 times.
✓ Branch 3 taken 384 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 4778 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 90 times.
5252 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
375 // Size of LSB section in any segment
376 384 b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
377
2/4
✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 384 times.
384 if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {
378 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");
379 ✗ return AVERROR_INVALIDDATA;
380 }
381
382 // Account for optional CRC bytes after LSB section
383
4/6
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 309 times.
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
✗ Branch 5 not taken.
384 if (b->lsb_section_size && (s->band_crc_present > 2 ||
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 (band == 0 && s->band_crc_present > 1)))
385 ✗ b->lsb_section_size += 2;
386
387 // Number of bits to represent the samples in LSB part
388
2/2
✓ Branch 0 taken 1404 times.
✓ Branch 1 taken 384 times.
1788 for (i = 0; i < c->nchannels; i++) {
389 1404 b->nscalablelsbs[i] = get_bits(&s->gb, 4);
390
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 1254 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 150 times.
1404 if (b->nscalablelsbs[i] && !b->lsb_section_size) {
391 ✗ av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");
392 ✗ return AVERROR_INVALIDDATA;
393 }
394 }
395 } else {
396 4868 b->lsb_section_size = 0;
397
2/2
✓ Branch 0 taken 19628 times.
✓ Branch 1 taken 4868 times.
24496 for (i = 0; i < c->nchannels; i++)
398 19628 b->nscalablelsbs[i] = 0;
399 }
400
401 // Scalable resolution flag in extension frequency band
402
7/8
✓ Branch 0 taken 5162 times.
✓ Branch 1 taken 90 times.
✓ Branch 2 taken 4778 times.
✓ Branch 3 taken 384 times.
✓ Branch 4 taken 90 times.
✓ Branch 5 taken 4778 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 90 times.
5252 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
403 // Number of bits discarded by authoring
404
2/2
✓ Branch 0 taken 1404 times.
✓ Branch 1 taken 384 times.
1788 for (i = 0; i < c->nchannels; i++)
405 1404 b->bit_width_adjust[i] = get_bits(&s->gb, 4);
406 } else {
407
2/2
✓ Branch 0 taken 19628 times.
✓ Branch 1 taken 4868 times.
24496 for (i = 0; i < c->nchannels; i++)
408 19628 b->bit_width_adjust[i] = 0;
409 }
410 }
411
412 // Reserved
413 // Byte align
414 // CRC16 of channel set sub-header
415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5162 times.
5162 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
416 ✗ av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");
417 ✗ return AVERROR_INVALIDDATA;
418 }
419
420 5162 return 0;
421 }
422
423 5092 static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
424 {
425
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 5009 times.
5092 int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
426 5092 int nchsamples = s->nframesamples + ndecisamples;
427 5092 int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
428 int32_t *ptr;
429
430 // Reallocate MSB sample buffer
431 5092 av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5092 times.
5092 if (!c->sample_buffer[0])
433 ✗ return AVERROR(ENOMEM);
434
435 5092 ptr = c->sample_buffer[0] + ndecisamples;
436
2/2
✓ Branch 0 taken 5175 times.
✓ Branch 1 taken 5092 times.
10267 for (i = 0; i < c->nfreqbands; i++) {
437
2/2
✓ Branch 0 taken 20850 times.
✓ Branch 1 taken 5175 times.
26025 for (j = 0; j < c->nchannels; j++) {
438 20850 c->bands[i].msb_sample_buffer[j] = ptr;
439 20850 ptr += nchsamples;
440 }
441 }
442
443 5092 return 0;
444 }
445
446 5092 static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
447 {
448 5092 int i, j, nsamples = 0;
449 int32_t *ptr;
450
451 // Determine number of frequency bands that have MSB/LSB split
452
2/2
✓ Branch 0 taken 5175 times.
✓ Branch 1 taken 5092 times.
10267 for (i = 0; i < c->nfreqbands; i++)
453
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 5104 times.
5175 if (c->bands[i].lsb_section_size)
454 71 nsamples += s->nframesamples * c->nchannels;
455
2/2
✓ Branch 0 taken 5021 times.
✓ Branch 1 taken 71 times.
5092 if (!nsamples)
456 5021 return 0;
457
458 // Reallocate LSB sample buffer
459 71 av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 71 times.
71 if (!c->sample_buffer[1])
461 ✗ return AVERROR(ENOMEM);
462
463 71 ptr = c->sample_buffer[1];
464
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 71 times.
142 for (i = 0; i < c->nfreqbands; i++) {
465
1/2
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
71 if (c->bands[i].lsb_section_size) {
466
2/2
✓ Branch 0 taken 344 times.
✓ Branch 1 taken 71 times.
415 for (j = 0; j < c->nchannels; j++) {
467 344 c->bands[i].lsb_sample_buffer[j] = ptr;
468 344 ptr += s->nframesamples;
469 }
470 } else {
471 ✗ for (j = 0; j < c->nchannels; j++)
472 ✗ c->bands[i].lsb_sample_buffer[j] = NULL;
473 }
474 }
475
476 71 return 0;
477 }
478
479 14842 static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
480 {
481 14842 DCAXllBand *b = &c->bands[band];
482 int i, j, k;
483
484 // Start unpacking MSB portion of the segment
485
3/4
✓ Branch 0 taken 9667 times.
✓ Branch 1 taken 5175 times.
✓ Branch 3 taken 9667 times.
✗ Branch 4 not taken.
14842 if (!(seg && get_bits1(&s->gb))) {
486 // Unpack segment type
487 // 0 - distinct coding parameters for each channel
488 // 1 - common coding parameters for all channels
489 14842 c->seg_common = get_bits1(&s->gb);
490
491 // Determine number of coding parameters encoded in segment
492
2/2
✓ Branch 0 taken 10397 times.
✓ Branch 1 taken 4445 times.
14842 k = c->seg_common ? 1 : c->nchannels;
493
494 // Unpack Rice coding parameters
495
2/2
✓ Branch 0 taken 47436 times.
✓ Branch 1 taken 14842 times.
62278 for (i = 0; i < k; i++) {
496 // Unpack Rice coding flag
497 // 0 - linear code, 1 - Rice code
498 47436 c->rice_code_flag[i] = get_bits1(&s->gb);
499 // Unpack Hybrid Rice coding flag
500 // 0 - Rice code, 1 - Hybrid Rice code
501
6/6
✓ Branch 0 taken 42991 times.
✓ Branch 1 taken 4445 times.
✓ Branch 2 taken 35639 times.
✓ Branch 3 taken 7352 times.
✓ Branch 5 taken 1655 times.
✓ Branch 6 taken 33984 times.
47436 if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))
502 // Unpack binary code length for isolated samples
503 1655 c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
504 else
505 // 0 indicates no Hybrid Rice coding
506 45781 c->bitalloc_hybrid_linear[i] = 0;
507 }
508
509 // Unpack coding parameters
510
2/2
✓ Branch 0 taken 47436 times.
✓ Branch 1 taken 14842 times.
62278 for (i = 0; i < k; i++) {
511
2/2
✓ Branch 0 taken 16898 times.
✓ Branch 1 taken 30538 times.
47436 if (seg == 0) {
512 // Unpack coding parameter for part A of segment 0
513 16898 c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
514
515 // Adjust for the linear code
516
4/4
✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 15559 times.
✓ Branch 2 taken 104 times.
✓ Branch 3 taken 1235 times.
16898 if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
517 104 c->bitalloc_part_a[i]++;
518
519
2/2
✓ Branch 0 taken 15540 times.
✓ Branch 1 taken 1358 times.
16898 if (!c->seg_common)
520 15540 c->nsamples_part_a[i] = b->adapt_pred_order[i];
521 else
522 1358 c->nsamples_part_a[i] = b->highest_pred_order;
523 } else {
524 30538 c->bitalloc_part_a[i] = 0;
525 30538 c->nsamples_part_a[i] = 0;
526 }
527
528 // Unpack coding parameter for part B of segment
529 47436 c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
530
531 // Adjust for the linear code
532
4/4
✓ Branch 0 taken 8850 times.
✓ Branch 1 taken 38586 times.
✓ Branch 2 taken 791 times.
✓ Branch 3 taken 8059 times.
47436 if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
533 791 c->bitalloc_part_b[i]++;
534 }
535 }
536
537 // Unpack entropy codes
538
2/2
✓ Branch 0 taken 60198 times.
✓ Branch 1 taken 14842 times.
75040 for (i = 0; i < c->nchannels; i++) {
539 int32_t *part_a, *part_b;
540 int nsamples_part_b;
541
542 // Select index of coding parameters
543
2/2
✓ Branch 0 taken 42991 times.
✓ Branch 1 taken 17207 times.
60198 k = c->seg_common ? 0 : i;
544
545 // Slice the segment into parts A and B
546 60198 part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
547 60198 part_b = part_a + c->nsamples_part_a[k];
548 60198 nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
549
550
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60198 times.
60198 if (get_bits_left(&s->gb) < 0)
551 ✗ return AVERROR_INVALIDDATA;
552
553
2/2
✓ Branch 0 taken 13492 times.
✓ Branch 1 taken 46706 times.
60198 if (!c->rice_code_flag[k]) {
554 // Linear codes
555 // Unpack all residuals of part A of segment 0
556 13492 get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
557 c->bitalloc_part_a[k]);
558
559 // Unpack all residuals of part B of segment 0 and others
560 13492 get_linear_array(&s->gb, part_b, nsamples_part_b,
561 c->bitalloc_part_b[k]);
562 } else {
563 // Rice codes
564 // Unpack all residuals of part A of segment 0
565 46706 int ret = get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
566 c->bitalloc_part_a[k]);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46706 times.
46706 if (ret < 0)
568 ✗ return ret;
569
570
2/2
✓ Branch 0 taken 1655 times.
✓ Branch 1 taken 45051 times.
46706 if (c->bitalloc_hybrid_linear[k]) {
571 // Hybrid Rice codes
572 // Unpack the number of isolated samples
573 1655 int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
574
575 // Set all locations to 0
576 1655 memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
577
578 // Extract the locations of isolated samples and flag by -1
579
2/2
✓ Branch 0 taken 9766 times.
✓ Branch 1 taken 1655 times.
11421 for (j = 0; j < nisosamples; j++) {
580 9766 int loc = get_bits(&s->gb, s->nsegsamples_log2);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9766 times.
9766 if (loc >= nsamples_part_b) {
582 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
583 ✗ return AVERROR_INVALIDDATA;
584 }
585 9766 part_b[loc] = -1;
586 }
587
588 // Unpack all residuals of part B of segment 0 and others
589
2/2
✓ Branch 0 taken 264506 times.
✓ Branch 1 taken 1655 times.
266161 for (j = 0; j < nsamples_part_b; j++) {
590
2/2
✓ Branch 0 taken 9766 times.
✓ Branch 1 taken 254740 times.
264506 if (part_b[j])
591 9766 part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
592 else
593 254740 part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
594 }
595 } else {
596 // Rice codes
597 // Unpack all residuals of part B of segment 0 and others
598 45051 ret = get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45051 times.
45051 if (ret < 0)
600 ✗ return ret;
601 }
602 }
603 }
604
605 // Unpack decimator history for frequency band 1
606
4/4
✓ Branch 0 taken 5175 times.
✓ Branch 1 taken 9667 times.
✓ Branch 2 taken 83 times.
✓ Branch 3 taken 5092 times.
14842 if (seg == 0 && band == 1) {
607 83 int nbits = get_bits(&s->gb, 5) + 1;
608
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 83 times.
415 for (i = 0; i < c->nchannels; i++)
609
2/2
✓ Branch 0 taken 2324 times.
✓ Branch 1 taken 332 times.
2656 for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
610 2324 c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
611 }
612
613 // Start unpacking LSB portion of the segment
614
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 14294 times.
14842 if (b->lsb_section_size) {
615 // Skip to the start of LSB portion
616
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 548 times.
548 if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
617 ✗ av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
618 ✗ return AVERROR_INVALIDDATA;
619 }
620
621 // Unpack all LSB parts of residuals of this segment
622
2/2
✓ Branch 0 taken 2696 times.
✓ Branch 1 taken 548 times.
3244 for (i = 0; i < c->nchannels; i++) {
623
2/2
✓ Branch 0 taken 1096 times.
✓ Branch 1 taken 1600 times.
2696 if (b->nscalablelsbs[i]) {
624 1096 get_array(&s->gb,
625 1096 b->lsb_sample_buffer[i] + seg * s->nsegsamples,
626 s->nsegsamples, b->nscalablelsbs[i]);
627 }
628 }
629 }
630
631 // Skip to the end of band data
632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14842 times.
14842 if (ff_dca_seek_bits(&s->gb, band_data_end)) {
633 ✗ av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
634 ✗ return AVERROR_INVALIDDATA;
635 }
636
637 14842 return 0;
638 }
639
640 119 static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
641 {
642 119 DCAXllBand *b = &c->bands[band];
643 int i, offset, nsamples;
644
645
1/2
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
119 if (seg < 0) {
646 119 offset = 0;
647 119 nsamples = s->nframesamples;
648 } else {
649 ✗ offset = seg * s->nsegsamples;
650 ✗ nsamples = s->nsegsamples;
651 }
652
653
2/2
✓ Branch 0 taken 431 times.
✓ Branch 1 taken 119 times.
550 for (i = 0; i < c->nchannels; i++) {
654 431 memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 431 times.
431 if (b->lsb_section_size)
656 ✗ memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
657 }
658
659
3/4
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 103 times.
119 if (seg <= 0 && band)
660 16 memset(c->deci_history, 0, sizeof(c->deci_history));
661
662
1/2
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
119 if (seg < 0) {
663 119 memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
664 119 memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
665 }
666 119 }
667
668 5173 static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
669 {
670 5173 DCAXllBand *b = &c->bands[band];
671 5173 int nsamples = s->nframesamples;
672 int i, j, k;
673
674 // Inverse adaptive or fixed prediction
675
2/2
✓ Branch 0 taken 20842 times.
✓ Branch 1 taken 5173 times.
26015 for (i = 0; i < c->nchannels; i++) {
676 20842 int32_t *buf = b->msb_sample_buffer[i];
677 20842 int order = b->adapt_pred_order[i];
678
2/2
✓ Branch 0 taken 17515 times.
✓ Branch 1 taken 3327 times.
20842 if (order > 0) {
679 int coeff[DCA_XLL_ADAPT_PRED_ORDER_MAX];
680 // Conversion from reflection coefficients to direct form coefficients
681
2/2
✓ Branch 0 taken 80922 times.
✓ Branch 1 taken 17515 times.
98437 for (j = 0; j < order; j++) {
682 80922 int rc = b->adapt_refl_coeff[i][j];
683
2/2
✓ Branch 0 taken 127729 times.
✓ Branch 1 taken 80922 times.
208651 for (k = 0; k < (j + 1) / 2; k++) {
684 127729 int tmp1 = coeff[ k ];
685 127729 int tmp2 = coeff[j - k - 1];
686 127729 coeff[ k ] = tmp1 + mul16(rc, tmp2);
687 127729 coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
688 }
689 80922 coeff[j] = rc;
690 }
691 // Inverse adaptive prediction
692
2/2
✓ Branch 0 taken 9083366 times.
✓ Branch 1 taken 17515 times.
9100881 for (j = 0; j < nsamples - order; j++) {
693 9083366 int64_t err = 0;
694
2/2
✓ Branch 0 taken 42421806 times.
✓ Branch 1 taken 9083366 times.
51505172 for (k = 0; k < order; k++)
695 42421806 err += (int64_t)buf[j + k] * coeff[order - k - 1];
696 9083366 buf[j + k] -= (SUINT)clip23(norm16(err));
697 }
698 } else {
699 // Inverse fixed coefficient prediction
700
2/2
✓ Branch 0 taken 1124 times.
✓ Branch 1 taken 3327 times.
4451 for (j = 0; j < b->fixed_pred_order[i]; j++)
701
2/2
✓ Branch 0 taken 605084 times.
✓ Branch 1 taken 1124 times.
606208 for (k = 1; k < nsamples; k++)
702 605084 buf[k] += (unsigned)buf[k - 1];
703 }
704 }
705
706 // Inverse pairwise channel decorrelation
707
2/2
✓ Branch 0 taken 5157 times.
✓ Branch 1 taken 16 times.
5173 if (b->decor_enabled) {
708 int32_t *tmp[DCA_XLL_CHANNELS_MAX];
709
710
2/2
✓ Branch 0 taken 10413 times.
✓ Branch 1 taken 5157 times.
15570 for (i = 0; i < c->nchannels / 2; i++) {
711 10413 int coeff = b->decor_coeff[i];
712
2/2
✓ Branch 0 taken 3441 times.
✓ Branch 1 taken 6972 times.
10413 if (coeff) {
713 3441 s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
714 3441 b->msb_sample_buffer[i * 2 ],
715 coeff, nsamples);
716 }
717 }
718
719 // Reorder channel pointers to the original order
720
2/2
✓ Branch 0 taken 20826 times.
✓ Branch 1 taken 5157 times.
25983 for (i = 0; i < c->nchannels; i++)
721 20826 tmp[i] = b->msb_sample_buffer[i];
722
723
2/2
✓ Branch 0 taken 20826 times.
✓ Branch 1 taken 5157 times.
25983 for (i = 0; i < c->nchannels; i++)
724 20826 b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
725 }
726
727 // Map output channel pointers for frequency band 0
728
2/2
✓ Branch 0 taken 5007 times.
✓ Branch 1 taken 166 times.
5173 if (c->nfreqbands == 1)
729
2/2
✓ Branch 0 taken 20178 times.
✓ Branch 1 taken 5007 times.
25185 for (i = 0; i < c->nchannels; i++)
730 20178 s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
731 5173 }
732
733 16333 static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
734 {
735 16333 int adj = c->bands[band].bit_width_adjust[ch];
736 16333 int shift = c->bands[band].nscalablelsbs[ch];
737
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16333 times.
16333 if (s->fixed_lsb_width)
739 ✗ shift = s->fixed_lsb_width;
740
3/4
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 16079 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 254 times.
16333 else if (shift && adj)
741 ✗ shift += adj - 1;
742 else
743 16333 shift += adj;
744
745 16333 return shift;
746 }
747
748 308 static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
749 {
750 308 DCAXllBand *b = &c->bands[band];
751 308 int n, ch, nsamples = s->nframesamples;
752
753
2/2
✓ Branch 0 taken 1253 times.
✓ Branch 1 taken 308 times.
1561 for (ch = 0; ch < c->nchannels; ch++) {
754 1253 int shift = chs_get_lsb_width(s, c, band, ch);
755
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 1111 times.
1253 if (shift) {
756 142 int32_t *msb = b->msb_sample_buffer[ch];
757
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
142 if (b->nscalablelsbs[ch]) {
758 142 int32_t *lsb = b->lsb_sample_buffer[ch];
759 142 int adj = b->bit_width_adjust[ch];
760
2/2
✓ Branch 0 taken 88064 times.
✓ Branch 1 taken 142 times.
88206 for (n = 0; n < nsamples; n++)
761 88064 msb[n] = msb[n] * (SUINT)(1 << shift) + (lsb[n] << adj);
762 } else {
763 ✗ for (n = 0; n < nsamples; n++)
764 ✗ msb[n] = msb[n] * (SUINT)(1 << shift);
765 }
766 }
767 }
768 308 }
769
770 83 static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
771 {
772 83 int ch, nsamples = s->nframesamples;
773 int32_t *ptr;
774
775 av_assert1(c->nfreqbands > 1);
776
777 // Reallocate frequency band assembly buffer
778 83 av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
779 83 2 * nsamples * c->nchannels * sizeof(int32_t));
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (!c->sample_buffer[2])
781 ✗ return AVERROR(ENOMEM);
782
783 // Assemble frequency bands 0 and 1
784 83 ptr = c->sample_buffer[2];
785
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 83 times.
415 for (ch = 0; ch < c->nchannels; ch++) {
786 332 int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
787 332 int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
788
789 // Copy decimator history
790 332 memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
791 332 c->deci_history[ch], sizeof(c->deci_history[0]));
792
793 // Filter
794 332 s->dcadsp->assemble_freq_bands(ptr, band0, band1,
795 ff_dca_xll_band_coeff,
796 nsamples);
797
798 // Remap output channel pointer to assembly buffer
799 332 s->output_samples[c->ch_remap[ch]] = ptr;
800 332 ptr += nsamples * 2;
801 }
802
803 83 return 0;
804 }
805
806 2623 static int parse_common_header(DCAXllDecoder *s)
807 {
808 int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
809
810 // XLL extension sync word
811
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
812 ✗ av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
813 ✗ return AVERROR(EAGAIN);
814 }
815
816 // Version number
817 2623 stream_ver = get_bits(&s->gb, 4) + 1;
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (stream_ver > 1) {
819 ✗ avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
820 ✗ return AVERROR_PATCHWELCOME;
821 }
822
823 // Lossless frame header length
824 2623 header_size = get_bits(&s->gb, 8) + 1;
825
826 // Check CRC
827
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if (ff_dca_check_crc(s->avctx, &s->gb, 32, header_size * 8)) {
828 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
829 ✗ return AVERROR_INVALIDDATA;
830 }
831
832 // Number of bits used to read frame size
833 2623 frame_size_nbits = get_bits(&s->gb, 5) + 1;
834
835 // Number of bytes in a lossless frame
836 2623 s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
837
2/4
✓ Branch 0 taken 2623 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2623 times.
2623 if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
838 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
839 ✗ return AVERROR_INVALIDDATA;
840 }
841 2623 s->frame_size++;
842
843 // Number of channels sets per frame
844 2623 s->nchsets = get_bits(&s->gb, 4) + 1;
845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->nchsets > DCA_XLL_CHSETS_MAX) {
846 ✗ avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
847 ✗ return AVERROR_PATCHWELCOME;
848 }
849
850 // Number of segments per frame
851 2623 nframesegs_log2 = get_bits(&s->gb, 4);
852 2623 s->nframesegs = 1 << nframesegs_log2;
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->nframesegs > 1024) {
854 ✗ av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
855 ✗ return AVERROR_INVALIDDATA;
856 }
857
858 // Samples in segment per one frequency band for the first channel set
859 // Maximum value is 256 for sampling frequencies <= 48 kHz
860 // Maximum value is 512 for sampling frequencies > 48 kHz
861 2623 s->nsegsamples_log2 = get_bits(&s->gb, 4);
862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (!s->nsegsamples_log2) {
863 ✗ av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
864 ✗ return AVERROR_INVALIDDATA;
865 }
866 2623 s->nsegsamples = 1 << s->nsegsamples_log2;
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->nsegsamples > 512) {
868 ✗ av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
869 ✗ return AVERROR_INVALIDDATA;
870 }
871
872 // Samples in frame per one frequency band for the first channel set
873 2623 s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
874 2623 s->nframesamples = 1 << s->nframesamples_log2;
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->nframesamples > 65536) {
876 ✗ av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
877 ✗ return AVERROR_INVALIDDATA;
878 }
879
880 // Number of bits used to read segment size
881 2623 s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
882
883 // Presence of CRC16 within each frequency band
884 // 0 - No CRC16 within band
885 // 1 - CRC16 placed at the end of MSB0
886 // 2 - CRC16 placed at the end of MSB0 and LSB0
887 // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
888 2623 s->band_crc_present = get_bits(&s->gb, 2);
889
890 // MSB/LSB split flag
891 2623 s->scalable_lsbs = get_bits1(&s->gb);
892
893 // Channel position mask
894 2623 s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
895
896 // Fixed LSB width
897
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 2428 times.
2623 if (s->scalable_lsbs)
898 195 s->fixed_lsb_width = get_bits(&s->gb, 4);
899 else
900 2428 s->fixed_lsb_width = 0;
901
902 // Reserved
903 // Byte align
904 // Header CRC16 protection
905
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
906 ✗ av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
907 ✗ return AVERROR_INVALIDDATA;
908 }
909
910 2623 return 0;
911 }
912
913 7570 static int is_hier_dmix_chset(DCAXllChSet *c)
914 {
915
4/6
✓ Branch 0 taken 7570 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 397 times.
✓ Branch 3 taken 7173 times.
✓ Branch 4 taken 397 times.
✗ Branch 5 not taken.
7570 return !c->primary_chset && c->dmix_embedded && c->hier_chset;
916 }
917
918 2770 static DCAXllChSet *find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
919 {
920
1/2
✓ Branch 0 taken 2770 times.
✗ Branch 1 not taken.
2770 if (c->hier_chset)
921
2/2
✓ Branch 0 taken 2503 times.
✓ Branch 1 taken 2682 times.
5185 while (++c < &s->chset[s->nchsets])
922
2/2
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 2415 times.
2503 if (is_hier_dmix_chset(c))
923 88 return c;
924
925 2682 return NULL;
926 }
927
928 30 static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
929 {
930 30 int i, j, *coeff_ptr = c->dmix_coeff;
931
932
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 30 times.
90 for (i = 0; i < c->hier_ofs; i++) {
933 60 int scale = o->dmix_scale[i];
934 60 int scale_inv = o->dmix_scale_inv[i];
935 60 c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
936 60 c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
937
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 60 times.
300 for (j = 0; j < c->nchannels; j++) {
938 240 int coeff = mul16(*coeff_ptr, scale_inv);
939 240 *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
940 }
941 }
942 30 }
943
944 2623 static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
945 {
946 2623 DCAContext *dca = s->avctx->priv_data;
947 DCAXllChSet *c;
948 int i, ret;
949
950 // Parse channel set headers
951 2623 s->nfreqbands = 0;
952 2623 s->nchannels = 0;
953 2623 s->nreschsets = 0;
954
2/2
✓ Branch 0 taken 5159 times.
✓ Branch 1 taken 2623 times.
7782 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
955 5159 c->hier_ofs = s->nchannels;
956
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5159 times.
5159 if ((ret = chs_parse_header(s, c, asset)) < 0)
957 ✗ return ret;
958
2/2
✓ Branch 0 taken 2623 times.
✓ Branch 1 taken 2536 times.
5159 if (c->nfreqbands > s->nfreqbands)
959 2623 s->nfreqbands = c->nfreqbands;
960
1/2
✓ Branch 0 taken 5159 times.
✗ Branch 1 not taken.
5159 if (c->hier_chset)
961 5159 s->nchannels += c->nchannels;
962
2/2
✓ Branch 0 taken 2591 times.
✓ Branch 1 taken 2568 times.
5159 if (c->residual_encode != (1 << c->nchannels) - 1)
963 2591 s->nreschsets++;
964 }
965
966 // Pre-scale downmixing coefficients for all non-primary channel sets
967
2/2
✓ Branch 0 taken 2536 times.
✓ Branch 1 taken 2623 times.
5159 for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
968
2/2
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 2350 times.
2536 if (is_hier_dmix_chset(c)) {
969 186 DCAXllChSet *o = find_next_hier_dmix_chset(s, c);
970
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 156 times.
186 if (o)
971 30 prescale_down_mix(c, o);
972 }
973 }
974
975 // Determine number of active channel sets to decode
976
3/3
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 2503 times.
2623 switch (dca->request_channel_layout) {
977 60 case DCA_SPEAKER_LAYOUT_STEREO:
978 60 s->nactivechsets = 1;
979 60 break;
980 60 case DCA_SPEAKER_LAYOUT_5POINT0:
981 case DCA_SPEAKER_LAYOUT_5POINT1:
982
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;
983 60 break;
984 2503 default:
985 2503 s->nactivechsets = s->nchsets;
986 2503 break;
987 }
988
989 2623 return 0;
990 }
991
992 2623 static int parse_navi_table(DCAXllDecoder *s)
993 {
994 int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
995 DCAXllChSet *c;
996
997 // Determine size of NAVI table
998 2623 navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (navi_nb > 1024) {
1000 ✗ av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
1001 ✗ return AVERROR_INVALIDDATA;
1002 }
1003
1004 // Reallocate NAVI table
1005 2623 av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (!s->navi)
1007 ✗ return AVERROR(ENOMEM);
1008
1009 // Parse NAVI
1010 2623 navi_pos = get_bits_count(&s->gb);
1011 2623 navi_ptr = s->navi;
1012
2/2
✓ Branch 0 taken 2683 times.
✓ Branch 1 taken 2623 times.
5306 for (band = 0; band < s->nfreqbands; band++) {
1013
2/2
✓ Branch 0 taken 7960 times.
✓ Branch 1 taken 2683 times.
10643 for (seg = 0; seg < s->nframesegs; seg++) {
1014
2/2
✓ Branch 0 taken 15254 times.
✓ Branch 1 taken 7960 times.
23214 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1015 15254 int size = 0;
1016
1/2
✓ Branch 0 taken 15254 times.
✗ Branch 1 not taken.
15254 if (c->nfreqbands > band) {
1017 15254 size = get_bits_long(&s->gb, s->seg_size_nbits);
1018
2/4
✓ Branch 0 taken 15254 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 15254 times.
15254 if (size < 0 || size >= s->frame_size) {
1019 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
1020 ✗ return AVERROR_INVALIDDATA;
1021 }
1022 15254 size++;
1023 }
1024 15254 *navi_ptr++ = size;
1025 }
1026 }
1027 }
1028
1029 // Byte align
1030 // CRC16
1031 2623 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1032 2623 skip_bits(&s->gb, 16);
1033
1034 // Check CRC
1035
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2623 times.
2623 if (ff_dca_check_crc(s->avctx, &s->gb, navi_pos, get_bits_count(&s->gb))) {
1036 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
1037 ✗ return AVERROR_INVALIDDATA;
1038 }
1039
1040 2623 return 0;
1041 }
1042
1043 2623 static int parse_band_data(DCAXllDecoder *s)
1044 {
1045 int ret, chs, seg, band, navi_pos, *navi_ptr;
1046 DCAXllChSet *c;
1047
1048
2/2
✓ Branch 0 taken 5089 times.
✓ Branch 1 taken 2623 times.
7712 for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
1049
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5089 times.
5089 if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1050 ✗ return ret;
1051
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5089 times.
5089 if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1052 ✗ return ret;
1053 }
1054
1055 2623 navi_pos = get_bits_count(&s->gb);
1056 2623 navi_ptr = s->navi;
1057
2/2
✓ Branch 0 taken 2683 times.
✓ Branch 1 taken 2623 times.
5306 for (band = 0; band < s->nfreqbands; band++) {
1058
2/2
✓ Branch 0 taken 7960 times.
✓ Branch 1 taken 2683 times.
10643 for (seg = 0; seg < s->nframesegs; seg++) {
1059
2/2
✓ Branch 0 taken 15254 times.
✓ Branch 1 taken 7960 times.
23214 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1060
1/2
✓ Branch 0 taken 15254 times.
✗ Branch 1 not taken.
15254 if (c->nfreqbands > band) {
1061 15254 navi_pos += *navi_ptr * 8;
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15254 times.
15254 if (navi_pos > s->gb.size_in_bits) {
1063 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
1064 ✗ return AVERROR_INVALIDDATA;
1065 }
1066
3/4
✓ Branch 0 taken 14836 times.
✓ Branch 1 taken 418 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14836 times.
30090 if (chs < s->nactivechsets &&
1067 14836 (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
1068 ✗ if (s->avctx->err_recognition & AV_EF_EXPLODE)
1069 ✗ return ret;
1070 ✗ chs_clear_band_data(s, c, band, seg);
1071 }
1072 15254 skip_bits_long(&s->gb, navi_pos - get_bits_count(&s->gb));
1073 }
1074 15254 navi_ptr++;
1075 }
1076 }
1077 }
1078
1079 2623 return 0;
1080 }
1081
1082 // The extension at the end of a DTS:X frame in the standard presentation is a
1083 // 22-byte header followed by a regular XLL channel set with its own NAVI table,
1084 // segmented like the frame's channel sets. Its four channels are the front and
1085 // rear height pairs; each is also folded into the 7.1 bed speaker below it at
1086 // -3 dB, which ff_dca_xll_filter_frame() removes.
1087 3 static int parse_x_heights(DCAXllDecoder *s, DCAExssAsset *asset)
1088 {
1089 3 DCAXllChSet *c = &s->x_chset;
1090 int ret, seg, navi_pos, band_pos, navi[1024];
1091
1092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->nfreqbands != 1)
1093 ✗ return AVERROR_PATCHWELCOME;
1094
1095 3 skip_bits_long(&s->gb, DCA_XLL_X_HEADER_SIZE * 8);
1096
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits_left(&s->gb) < 0)
1097 ✗ return AVERROR_INVALIDDATA;
1098
1099 3 c->hier_ofs = 0;
1100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = chs_parse_header(s, c, asset)) < 0)
1101 ✗ return ret;
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (c->residual_encode != (1 << c->nchannels) - 1) {
1103 ✗ avpriv_request_sample(s->avctx, "DTS:X height channels coded against the core");
1104 ✗ return AVERROR_PATCHWELCOME;
1105 }
1106
1107 3 navi_pos = get_bits_count(&s->gb);
1108
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (seg = 0; seg < s->nframesegs; seg++) {
1109 6 navi[seg] = get_bits_long(&s->gb, s->seg_size_nbits);
1110
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (navi[seg] < 0 || navi[seg] >= s->frame_size) {
1111 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid DTS:X NAVI segment size (%d bytes)\n", navi[seg]);
1112 ✗ return AVERROR_INVALIDDATA;
1113 }
1114 6 navi[seg]++;
1115 }
1116 3 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1117 3 skip_bits(&s->gb, 16);
1118
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (ff_dca_check_crc(s->avctx, &s->gb, navi_pos, get_bits_count(&s->gb))) {
1119 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid DTS:X NAVI checksum\n");
1120 ✗ return AVERROR_INVALIDDATA;
1121 }
1122
1123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1124 ✗ return ret;
1125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1126 ✗ return ret;
1127
1128 3 band_pos = get_bits_count(&s->gb);
1129
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (seg = 0; seg < s->nframesegs; seg++) {
1130 6 band_pos += navi[seg] * 8;
1131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (band_pos > s->frame_size * 8) {
1132 ✗ av_log(s->avctx, AV_LOG_ERROR, "Invalid DTS:X NAVI position\n");
1133 ✗ return AVERROR_INVALIDDATA;
1134 }
1135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = chs_parse_band_data(s, c, 0, seg, band_pos)) < 0)
1136 ✗ return ret;
1137 6 skip_bits_long(&s->gb, band_pos - get_bits_count(&s->gb));
1138 }
1139
1140 // A two-byte trailer and up to three bytes of dword padding follow
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->frame_size * 8 - band_pos > 5 * 8) {
1142 ✗ av_log(s->avctx, AV_LOG_ERROR, "DTS:X height channel set ends %d bytes before its frame\n",
1143 ✗ s->frame_size - band_pos / 8);
1144 ✗ return AVERROR_INVALIDDATA;
1145 }
1146
1147 3 return 0;
1148 }
1149
1150 2623 static int parse_frame(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1151 {
1152 2623 DCAContext *dca = s->avctx->priv_data;
1153 int ret;
1154
1155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1156 ✗ return ret;
1157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = parse_common_header(s)) < 0)
1158 ✗ return ret;
1159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = parse_sub_headers(s, asset)) < 0)
1160 ✗ return ret;
1161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = parse_navi_table(s)) < 0)
1162 ✗ return ret;
1163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = parse_band_data(s)) < 0)
1164 ✗ return ret;
1165
1166 2623 s->x_heights_decoded = 0;
1167
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2617 times.
2623 if (s->frame_size * 8 > FFALIGN(get_bits_count(&s->gb), 32)) {
1168 unsigned int extradata_syncword;
1169
1170 // Align to dword
1171 6 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1172
1173 6 extradata_syncword = show_bits_long(&s->gb, 32);
1174
1175
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (extradata_syncword == DCA_SYNCWORD_XLL_X) {
1176 6 s->x_syncword_present = 1;
1177
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
6 if (!dca->request_channel_layout && !dca->x_bed_only) {
1178 3 int x_pos = get_bits_count(&s->gb);
1179 3 int err = parse_x_heights(s, asset);
1180 // Whatever the heights did, the frame ends where it did before
1181 3 skip_bits_long(&s->gb, x_pos - get_bits_count(&s->gb));
1182 3 s->x_heights = 1;
1183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (err < 0) {
1184 ✗ if (s->avctx->err_recognition & AV_EF_EXPLODE)
1185 ✗ return err;
1186 ✗ av_log(s->avctx, AV_LOG_WARNING, "DTS:X height channels not decoded, output silent heights\n");
1187 } else {
1188 3 s->x_heights_decoded = 1;
1189 }
1190 }
1191 ✗ } else if ((extradata_syncword >> 1) == (DCA_SYNCWORD_XLL_X_IMAX >> 1)) {
1192 ✗ s->x_imax_syncword_present = 1;
1193 }
1194 }
1195
1196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1197 ✗ av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
1198 ✗ return AVERROR_INVALIDDATA;
1199 }
1200 2623 return ret;
1201 }
1202
1203 165 static void clear_pbr(DCAXllDecoder *s)
1204 {
1205 165 s->pbr_length = 0;
1206 165 s->pbr_delay = 0;
1207 165 }
1208
1209 ✗ static int copy_to_pbr(DCAXllDecoder *s, const uint8_t *data, int size, int delay)
1210 {
1211 ✗ if (size > DCA_XLL_PBR_BUFFER_MAX)
1212 ✗ return AVERROR(ENOSPC);
1213
1214 ✗ if (!s->pbr_buffer && !(s->pbr_buffer = av_malloc(DCA_XLL_PBR_BUFFER_MAX + AV_INPUT_BUFFER_PADDING_SIZE)))
1215 ✗ return AVERROR(ENOMEM);
1216
1217 ✗ memcpy(s->pbr_buffer, data, size);
1218 ✗ memset(s->pbr_buffer + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1219 ✗ s->pbr_length = size;
1220 ✗ s->pbr_delay = delay;
1221 ✗ return 0;
1222 }
1223
1224 2623 static int parse_frame_no_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1225 {
1226 2623 int ret = parse_frame(s, data, size, asset);
1227
1228 // If XLL packet data didn't start with a sync word, we must have jumped
1229 // right into the middle of PBR smoothing period
1230
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2623 if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
1231 // Skip to the next sync word in this packet
1232 ✗ data += asset->xll_sync_offset;
1233 ✗ size -= asset->xll_sync_offset;
1234
1235 // If decoding delay is set, put the frame into PBR buffer and return
1236 // failure code. Higher level decoder is expected to switch to lossy
1237 // core decoding or mute its output until decoding delay expires.
1238 ✗ if (asset->xll_delay_nframes > 0) {
1239 ✗ if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
1240 ✗ return ret;
1241 ✗ return AVERROR(EAGAIN);
1242 }
1243
1244 // No decoding delay, just parse the frame in place
1245 ✗ ret = parse_frame(s, data, size, asset);
1246 }
1247
1248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (ret < 0)
1249 ✗ return ret;
1250
1251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->frame_size > size)
1252 ✗ return AVERROR(EINVAL);
1253
1254 // If the XLL decoder didn't consume full packet, start PBR smoothing period
1255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->frame_size < size)
1256 ✗ if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
1257 ✗ return ret;
1258
1259 2623 return 0;
1260 }
1261
1262 ✗ static int parse_frame_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1263 {
1264 int ret;
1265
1266 ✗ if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
1267 ✗ ret = AVERROR(ENOSPC);
1268 ✗ goto fail;
1269 }
1270
1271 ✗ memcpy(s->pbr_buffer + s->pbr_length, data, size);
1272 ✗ s->pbr_length += size;
1273 ✗ memset(s->pbr_buffer + s->pbr_length, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1274
1275 // Respect decoding delay after synchronization error
1276 ✗ if (s->pbr_delay > 0 && --s->pbr_delay)
1277 ✗ return AVERROR(EAGAIN);
1278
1279 ✗ if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
1280 ✗ goto fail;
1281
1282 ✗ if (s->frame_size > s->pbr_length) {
1283 ✗ ret = AVERROR(EINVAL);
1284 ✗ goto fail;
1285 }
1286
1287 ✗ if (s->frame_size == s->pbr_length) {
1288 // End of PBR smoothing period
1289 ✗ clear_pbr(s);
1290 } else {
1291 ✗ s->pbr_length -= s->frame_size;
1292 ✗ memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
1293 }
1294
1295 ✗ return 0;
1296
1297 ✗ fail:
1298 // For now, throw out all PBR state on failure.
1299 // Perhaps we can be smarter and try to resync somehow.
1300 ✗ clear_pbr(s);
1301 ✗ return ret;
1302 }
1303
1304 2623 int ff_dca_xll_parse(DCAXllDecoder *s, const uint8_t *data, DCAExssAsset *asset)
1305 {
1306 int ret;
1307
1308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->hd_stream_id != asset->hd_stream_id) {
1309 ✗ clear_pbr(s);
1310 ✗ s->hd_stream_id = asset->hd_stream_id;
1311 ✗ s->x_heights = 0;
1312 }
1313
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->pbr_length)
1315 ✗ ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1316 else
1317 2623 ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1318
1319 2623 return ret;
1320 }
1321
1322 94 static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1323 {
1324 94 int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
1325 DCAXllChSet *c;
1326
1327
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (!c->hier_chset)
1329 ✗ continue;
1330
1331 av_assert1(band < c->nfreqbands);
1332
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 105 times.
477 for (j = 0; j < c->nchannels; j++) {
1333
2/2
✓ Branch 0 taken 870 times.
✓ Branch 1 taken 372 times.
1242 for (k = 0; k < o->nchannels; k++) {
1334 870 int coeff = *coeff_ptr++;
1335
2/2
✓ Branch 0 taken 188 times.
✓ Branch 1 taken 682 times.
870 if (coeff) {
1336 188 s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
1337 188 o->bands[band].msb_sample_buffer[k],
1338 188 coeff, s->nframesamples);
1339
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 156 times.
188 if (band)
1340 32 s->dcadsp->dmix_sub(c->deci_history[j],
1341 32 o->deci_history[k],
1342 coeff, DCA_XLL_DECI_HISTORY_MAX);
1343 }
1344 }
1345 }
1346
1347 105 nchannels += c->nchannels;
1348
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 11 times.
105 if (nchannels >= o->hier_ofs)
1349 94 break;
1350 }
1351 94 }
1352
1353 50 static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1354 {
1355 50 int i, j, nchannels = 0;
1356 DCAXllChSet *c;
1357
1358
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (!c->hier_chset)
1360 ✗ continue;
1361
1362 av_assert1(band < c->nfreqbands);
1363
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 55 times.
295 for (j = 0; j < c->nchannels; j++) {
1364 240 int scale = o->dmix_scale[nchannels++];
1365
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 60 times.
240 if (scale != (1 << 15)) {
1366 180 s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
1367 180 scale, s->nframesamples);
1368
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 170 times.
180 if (band)
1369 10 s->dcadsp->dmix_scale(c->deci_history[j],
1370 scale, DCA_XLL_DECI_HISTORY_MAX);
1371 }
1372 }
1373
1374
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 5 times.
55 if (nchannels >= o->hier_ofs)
1375 50 break;
1376 }
1377 50 }
1378
1379 // Clear all band data and replace non-residual encoded channels with lossy
1380 // counterparts
1381 103 static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
1382 {
1383 103 DCAContext *dca = s->avctx->priv_data;
1384 int band, ch;
1385
1386
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 103 times.
222 for (band = 0; band < c->nfreqbands; band++)
1387 119 chs_clear_band_data(s, c, band, -1);
1388
1389
2/2
✓ Branch 0 taken 385 times.
✓ Branch 1 taken 103 times.
488 for (ch = 0; ch < c->nchannels; ch++) {
1390
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 124 times.
385 if (!(c->residual_encode & (1 << ch)))
1391 261 continue;
1392
2/2
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 70 times.
124 if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
1393 54 continue;
1394 70 c->residual_encode &= ~(1 << ch);
1395 }
1396 103 }
1397
1398 2584 static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
1399 {
1400 2584 DCAContext *dca = s->avctx->priv_data;
1401 2584 int ch, nsamples = s->nframesamples;
1402 DCAXllChSet *o;
1403
1404 // Verify that core is compatible
1405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2584 times.
2584 if (!(dca->packet & DCA_PACKET_CORE)) {
1406 ✗ av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
1407 ✗ return AVERROR(EINVAL);
1408 }
1409
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2584 times.
2584 if (c->freq != dca->core.output_rate) {
1411 ✗ 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);
1412 ✗ return AVERROR_INVALIDDATA;
1413 }
1414
1415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2584 times.
2584 if (nsamples != dca->core.npcmsamples) {
1416 ✗ av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
1417 ✗ return AVERROR_INVALIDDATA;
1418 }
1419
1420 // See if this channel set is downmixed and find the next channel set in
1421 // hierarchy. If downmixed, undo core pre-scaling before combining with
1422 // residual (residual is not scaled).
1423 2584 o = find_next_hier_dmix_chset(s, c);
1424
1425 // Reduce core bit width and combine with residual
1426
2/2
✓ Branch 0 taken 15290 times.
✓ Branch 1 taken 2584 times.
17874 for (ch = 0; ch < c->nchannels; ch++) {
1427 int n, spkr, shift, round;
1428 int32_t *src, *dst;
1429
1430
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 15080 times.
15290 if (c->residual_encode & (1 << ch))
1431 210 continue;
1432
1433 // Map this channel to core speaker
1434 15080 spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15080 times.
15080 if (spkr < 0) {
1436 ✗ av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
1437 ✗ return AVERROR_INVALIDDATA;
1438 }
1439
1440 // Account for LSB width
1441 15080 shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
1442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15080 times.
15080 if (shift > 24) {
1443 ✗ av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
1444 ✗ return AVERROR_INVALIDDATA;
1445 }
1446
1447
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 14729 times.
15080 round = shift > 0 ? 1 << (shift - 1) : 0;
1448
1449 15080 src = dca->core.output_samples[spkr];
1450 15080 dst = c->bands[0].msb_sample_buffer[ch];
1451
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 14827 times.
15080 if (o) {
1452 // Undo embedded core downmix pre-scaling
1453 253 int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
1454
2/2
✓ Branch 0 taken 163328 times.
✓ Branch 1 taken 253 times.
163581 for (n = 0; n < nsamples; n++)
1455 163328 dst[n] += (SUINT)clip23((mul16(src[n], scale_inv) + round) >> shift);
1456 } else {
1457 // No downmix scaling
1458
2/2
✓ Branch 0 taken 7795200 times.
✓ Branch 1 taken 14827 times.
7810027 for (n = 0; n < nsamples; n++)
1459 7795200 dst[n] += (unsigned)((src[n] + round) >> shift);
1460 }
1461 }
1462
1463 2584 return 0;
1464 }
1465
1466 2623 int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
1467 {
1468 2623 AVCodecContext *avctx = s->avctx;
1469 2623 DCAContext *dca = avctx->priv_data;
1470 2623 DCAExssAsset *asset = &dca->exss.assets[0];
1471 2623 DCAXllChSet *p = &s->chset[0], *c;
1472 2623 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
1473 2623 int i, j, k, ret, shift, nsamples, request_mask, x_fold = 0;
1474 int ch_remap[DCA_SPEAKER_COUNT];
1475 static const uint8_t fold_spkr[4] = {
1476 DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Lsr, DCA_SPEAKER_Rsr
1477 };
1478
1479 // Force lossy downmixed output during recovery
1480
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2566 times.
2623 if (dca->packet & DCA_PACKET_RECOVERY) {
1481
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 57 times.
180 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
1482
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 20 times.
123 if (i < s->nactivechsets)
1483 103 force_lossy_output(s, c);
1484
1485
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 57 times.
123 if (!c->primary_chset)
1486 66 c->dmix_embedded = 0;
1487 }
1488
1489 57 s->scalable_lsbs = 0;
1490 57 s->fixed_lsb_width = 0;
1491 }
1492
1493 // Filter frequency bands for active channel sets
1494 2623 s->output_mask = 0;
1495
2/2
✓ Branch 0 taken 5089 times.
✓ Branch 1 taken 2623 times.
7712 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1496 5089 chs_filter_band_data(s, c, 0);
1497
1498
2/2
✓ Branch 0 taken 2584 times.
✓ Branch 1 taken 2505 times.
5089 if (c->residual_encode != (1 << c->nchannels) - 1
1499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2584 times.
2584 && (ret = combine_residual_frame(s, c)) < 0)
1500 ✗ return ret;
1501
1502
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 4865 times.
5089 if (s->scalable_lsbs)
1503 224 chs_assemble_msbs_lsbs(s, c, 0);
1504
1505
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 5006 times.
5089 if (c->nfreqbands > 1) {
1506 83 chs_filter_band_data(s, c, 1);
1507 83 chs_assemble_msbs_lsbs(s, c, 1);
1508 }
1509
1510 5089 s->output_mask |= c->ch_mask;
1511 }
1512
1513 // Undo hierarchial downmix and/or apply scaling
1514
2/2
✓ Branch 0 taken 2531 times.
✓ Branch 1 taken 2578 times.
5109 for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
1515
2/2
✓ Branch 1 taken 2408 times.
✓ Branch 2 taken 123 times.
2531 if (!is_hier_dmix_chset(c))
1516 2408 continue;
1517
1518
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 78 times.
123 if (i >= s->nactivechsets) {
1519
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 45 times.
95 for (j = 0; j < c->nfreqbands; j++)
1520
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (c->bands[j].dmix_embedded)
1521 50 scale_down_mix(s, c, j);
1522 45 break;
1523 }
1524
1525
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 78 times.
172 for (j = 0; j < c->nfreqbands; j++)
1526
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 if (c->bands[j].dmix_embedded)
1527 94 undo_down_mix(s, c, j);
1528 }
1529
1530 // Assemble frequency bands for active channel sets
1531
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2563 times.
2623 if (s->nfreqbands > 1) {
1532
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 60 times.
143 for (i = 0; i < s->nactivechsets; i++)
1533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 83 times.
83 if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
1534 ✗ return ret;
1535 }
1536
1537 // Heights of the DTS:X standard presentation, removed from the 7.1 bed
1538
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2620 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
2623 if (s->x_heights && !dca->request_channel_layout && s->nfreqbands == 1 &&
1539
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 (s->output_mask & ~(DCA_SPEAKER_MASK_Ls | DCA_SPEAKER_MASK_Rs |
1540 DCA_SPEAKER_MASK_Lss | DCA_SPEAKER_MASK_Rss)) ==
1541 (DCA_SPEAKER_LAYOUT_3_0 | DCA_SPEAKER_MASK_LFE1 |
1542 3 DCA_SPEAKER_MASK_Lsr | DCA_SPEAKER_MASK_Rsr) &&
1543
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 av_popcount(s->output_mask) == 8) {
1544 3 DCAXllChSet *x = &s->x_chset;
1545
1546
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
3 if (s->x_heights_decoded && !(dca->packet & DCA_PACKET_RECOVERY)) {
1547 1 int adj = p->pcm_bit_res - x->pcm_bit_res;
1548
1549 1 chs_filter_band_data(s, x, 0);
1550
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->scalable_lsbs)
1551 1 chs_assemble_msbs_lsbs(s, x, 0);
1552
1553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (adj) {
1554 ✗ for (i = 0; i < 4; i++) {
1555 ✗ int32_t *height = s->output_samples[x_height_spkr[i]];
1556 ✗ for (k = 0; k < s->nframesamples; k++)
1557 ✗ height[k] *= (SUINT)(1 << adj);
1558 }
1559 }
1560 1 x_fold = 1;
1561 } else {
1562 2 av_fast_malloc(&s->x_silence, &s->x_silence_size,
1563 2 s->nframesamples * sizeof(*s->x_silence));
1564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->x_silence)
1565 ✗ return AVERROR(ENOMEM);
1566 2 memset(s->x_silence, 0, s->nframesamples * sizeof(*s->x_silence));
1567
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < 4; i++)
1568 8 s->output_samples[x_height_spkr[i]] = s->x_silence;
1569 }
1570
1571 3 s->output_mask |= DCA_XLL_X_HEIGHT_MASK;
1572 }
1573
1574 // Normalize to regular 5.1 layout if downmixing
1575
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 2503 times.
2623 if (dca->request_channel_layout) {
1576
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 85 times.
120 if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
1577 35 s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
1578 35 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
1579 }
1580
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 85 times.
120 if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
1581 35 s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
1582 35 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
1583 }
1584 }
1585
1586 // Handle downmixing to stereo request
1587
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2563 times.
2623 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
1588
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
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1590 ✗ p->dmix_type == DCA_DMIX_TYPE_LtRt))
1591 7 request_mask = DCA_SPEAKER_LAYOUT_STEREO;
1592 else
1593 2616 request_mask = s->output_mask;
1594
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
1595 ✗ return AVERROR(EINVAL);
1596
1597 2623 avctx->sample_rate = p->freq << (s->nfreqbands - 1);
1598
1599
2/3
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2563 times.
✗ Branch 2 not taken.
2623 switch (p->storage_bit_res) {
1600 60 case 16:
1601 60 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1602 60 shift = 16 - p->pcm_bit_res;
1603 60 break;
1604 2563 case 20:
1605 case 24:
1606 2563 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1607 2563 shift = 24 - p->pcm_bit_res;
1608 2563 break;
1609 ✗ default:
1610 ✗ return AVERROR(EINVAL);
1611 }
1612
1613 // Remove the heights from the bed at output resolution: the bed may be
1614 // coded at fewer bits, and rounding the fold to those would cost up to
1615 // half of its least significant bit.
1616
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2622 times.
2623 if (x_fold) {
1617
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (i = 0; i < 4; i++) {
1618 4 int32_t *height = s->output_samples[x_height_spkr[i]];
1619 4 int32_t *bed = s->output_samples[fold_spkr[i]];
1620
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 4 times.
2052 for (k = 0; k < s->nframesamples; k++) {
1621 2048 height[k] *= (SUINT)(1 << shift);
1622 4096 bed[k] = bed[k] * (SUINT)(1 << shift) -
1623 2048 (SUINT)mul15(height[k], DCA_XLL_X_HEIGHT_FOLD);
1624 }
1625 }
1626 }
1627
1628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (s->x_imax_syncword_present) {
1629 ✗ avctx->profile = AV_PROFILE_DTS_HD_MA_X_IMAX;
1630
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2617 times.
2623 } else if (s->x_syncword_present) {
1631 6 avctx->profile = AV_PROFILE_DTS_HD_MA_X;
1632 } else {
1633 2617 avctx->profile = AV_PROFILE_DTS_HD_MA;
1634 }
1635
1636 2623 avctx->bits_per_raw_sample = p->storage_bit_res;
1637 2623 avctx->bit_rate = 0;
1638
1639 2623 frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
1640
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1641 ✗ return ret;
1642
1643 // Downmix primary channel set to stereo
1644
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2616 times.
2623 if (request_mask != s->output_mask) {
1645 7 ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
1646 7 p->dmix_coeff, nsamples,
1647 s->output_mask);
1648 }
1649
1650
2/2
✓ Branch 0 taken 20490 times.
✓ Branch 1 taken 2623 times.
23113 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
1651 20490 int32_t *samples = s->output_samples[ch_remap[i]];
1652 20490 int ch_shift = shift;
1653
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20478 times.
20490 if (x_fold && ((DCA_XLL_X_HEIGHT_MASK | DCA_SPEAKER_MASK_L | DCA_SPEAKER_MASK_R |
1654
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 DCA_SPEAKER_MASK_Lsr | DCA_SPEAKER_MASK_Rsr) & (1U << ch_remap[i])))
1655 8 ch_shift = 0;
1656
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 20158 times.
20490 if (frame->format == AV_SAMPLE_FMT_S16P) {
1657 332 int16_t *plane = (int16_t *)frame->extended_data[i];
1658
2/2
✓ Branch 0 taken 679936 times.
✓ Branch 1 taken 332 times.
680268 for (k = 0; k < nsamples; k++)
1659 679936 plane[k] = av_clip_int16(samples[k] * (SUINT)(1 << ch_shift));
1660 } else {
1661 20158 int32_t *plane = (int32_t *)frame->extended_data[i];
1662
2/2
✓ Branch 0 taken 10576896 times.
✓ Branch 1 taken 20158 times.
10597054 for (k = 0; k < nsamples; k++)
1663 10576896 plane[k] = clip23(samples[k] * (SUINT)(1 << ch_shift)) * (1 << 8);
1664 }
1665 }
1666
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2623 times.
2623 if (!asset->one_to_one_map_ch_to_spkr) {
1668 ✗ if (asset->representation_type == DCA_REPR_TYPE_LtRt)
1669 ✗ matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1670 ✗ else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
1671 ✗ matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1672
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2616 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
2623 } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
1673 ✗ matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1674 }
1675
1676 // TODO: Support downmix when the primary channel set lacks coeffs.
1677
4/4
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 2478 times.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 78 times.
2623 if (p->ch_mask == s->output_mask && !dca->request_channel_layout &&
1678
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 25 times.
67 p->dmix_embedded && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1679 ✗ p->dmix_type == DCA_DMIX_TYPE_LtRt)) {
1680 25 ret = ff_dca_export_downmix_matrix(avctx, frame, p->dmix_type, s->output_mask,
1681 25 p->dmix_coeff);
1682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
1683 ✗ return ret;
1684 }
1685
1686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2623 times.
2623 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1687 ✗ return ret;
1688
1689 2623 return 0;
1690 }
1691
1692 ✗ av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
1693 {
1694 ✗ clear_pbr(s);
1695 ✗ }
1696
1697 165 av_cold void ff_dca_xll_close(DCAXllDecoder *s)
1698 {
1699 DCAXllChSet *c;
1700 int i, j;
1701
1702
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 165 times.
660 for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
1703
2/2
✓ Branch 0 taken 1485 times.
✓ Branch 1 taken 495 times.
1980 for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1704 1485 av_freep(&c->sample_buffer[j]);
1705 1485 c->sample_size[j] = 0;
1706 }
1707 }
1708
1709
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 165 times.
660 for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1710 495 av_freep(&s->x_chset.sample_buffer[j]);
1711 495 s->x_chset.sample_size[j] = 0;
1712 }
1713 165 av_freep(&s->x_silence);
1714 165 s->x_silence_size = 0;
1715
1716 165 av_freep(&s->navi);
1717 165 s->navi_size = 0;
1718
1719 165 av_freep(&s->pbr_buffer);
1720 165 clear_pbr(s);
1721 165 }
1722