4 * Copyright (C) 2012 Paul B Mahol
5 * Copyright (C) 2014 Niels Möller
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/attributes.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
35 static inline int get_bits_sm(GetBitContext *s, unsigned n)
37 int x = get_bits(s, n);
44 /* Return -1 on error. */
45 static int32_t get_dmix_coeff(DCAContext *s, int inverse)
47 unsigned code = get_bits(&s->gb, 9);
48 int32_t sign = (int32_t) (code >> 8) - 1;
49 unsigned idx = code & 0xff;
50 int inv_offset = FF_DCA_DMIXTABLE_SIZE -FF_DCA_INV_DMIXTABLE_SIZE;
51 if (idx >= FF_DCA_DMIXTABLE_SIZE) {
52 av_log(s->avctx, AV_LOG_ERROR,
53 "XLL: Invalid channel set downmix code %x\n", code);
55 } else if (!inverse) {
56 return (ff_dca_dmixtable[idx] ^ sign) - sign;
57 } else if (idx < inv_offset) {
58 av_log(s->avctx, AV_LOG_ERROR,
59 "XLL: Invalid channel set inverse downmix code %x\n", code);
62 return (ff_dca_inv_dmixtable[idx - inv_offset] ^ sign) - sign;
66 static int32_t dca_get_dmix_coeff(DCAContext *s)
68 return get_dmix_coeff(s, 0);
71 static int32_t dca_get_inv_dmix_coeff(DCAContext *s)
73 return get_dmix_coeff(s, 1);
76 /* parse XLL header */
77 int ff_dca_xll_decode_header(DCAContext *s)
79 int hdr_pos, hdr_size;
80 av_unused int version, frame_size;
83 /* get bit position of sync header */
84 hdr_pos = get_bits_count(&s->gb) - 32;
86 version = get_bits(&s->gb, 4) + 1;
87 hdr_size = get_bits(&s->gb, 8) + 1;
89 frame_size = get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1) + 1;
92 s->xll_residual_channels = 0;
93 s->xll_nch_sets = get_bits(&s->gb, 4) + 1;
94 s->xll_segments = 1 << get_bits(&s->gb, 4);
95 s->xll_log_smpl_in_seg = get_bits(&s->gb, 4);
96 s->xll_smpl_in_seg = 1 << s->xll_log_smpl_in_seg;
97 s->xll_bits4seg_size = get_bits(&s->gb, 5) + 1;
98 s->xll_banddata_crc = get_bits(&s->gb, 2);
99 s->xll_scalable_lsb = get_bits1(&s->gb);
100 s->xll_bits4ch_mask = get_bits(&s->gb, 5) + 1;
102 if (s->xll_scalable_lsb) {
103 s->xll_fixed_lsb_width = get_bits(&s->gb, 4);
104 if (s->xll_fixed_lsb_width)
105 av_log(s->avctx, AV_LOG_WARNING,
106 "XLL: fixed lsb width = %d, non-zero not supported.\n",
107 s->xll_fixed_lsb_width);
109 /* skip to the end of the common header */
110 i = get_bits_count(&s->gb);
111 if (hdr_pos + hdr_size * 8 > i)
112 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
114 for (chset_index = 0; chset_index < s->xll_nch_sets; chset_index++) {
115 XllChSetSubHeader *chset = &s->xll_chsets[chset_index];
116 hdr_pos = get_bits_count(&s->gb);
117 hdr_size = get_bits(&s->gb, 10) + 1;
119 chset->channels = get_bits(&s->gb, 4) + 1;
120 chset->residual_encode = get_bits(&s->gb, chset->channels);
121 chset->bit_resolution = get_bits(&s->gb, 5) + 1;
122 chset->bit_width = get_bits(&s->gb, 5) + 1;
123 chset->sampling_frequency = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
124 chset->samp_freq_interp = get_bits(&s->gb, 2);
125 chset->replacement_set = get_bits(&s->gb, 2);
126 if (chset->replacement_set)
127 chset->active_replace_set = get_bits(&s->gb, 1);
129 if (s->one2one_map_chtospkr) {
130 chset->primary_ch_set = get_bits(&s->gb, 1);
131 chset->downmix_coeff_code_embedded = get_bits(&s->gb, 1);
132 if (chset->downmix_coeff_code_embedded) {
133 chset->downmix_embedded = get_bits(&s->gb, 1);
134 if (chset->primary_ch_set) {
135 chset->downmix_type = get_bits(&s->gb, 3);
136 if (chset->downmix_type > 6) {
137 av_log(s->avctx, AV_LOG_ERROR,
138 "XLL: Invalid channel set downmix type\n");
139 return AVERROR_INVALIDDATA;
143 chset->hier_chset = get_bits(&s->gb, 1);
145 if (chset->downmix_coeff_code_embedded) {
146 /* nDownmixCoeffs is specified as N * M. For a primary
147 * channel set, it appears that N = number of
148 * channels, and M is the number of downmix channels.
150 * For a non-primary channel set, N is specified as
151 * number of channels + 1, and M is derived from the
152 * channel set hierarchy, and at least in simple cases
153 * M is the number of channels in preceding channel
155 if (chset->primary_ch_set) {
156 static const char dmix_table[7] = { 1, 2, 2, 3, 3, 4, 4 };
157 chset->downmix_ncoeffs = chset->channels * dmix_table[chset->downmix_type];
159 chset->downmix_ncoeffs = (chset->channels + 1) * s->xll_channels;
161 if (chset->downmix_ncoeffs > DCA_XLL_DMIX_NCOEFFS_MAX) {
162 avpriv_request_sample(s->avctx,
163 "XLL: More than %d downmix coefficients",
164 DCA_XLL_DMIX_NCOEFFS_MAX);
165 return AVERROR_PATCHWELCOME;
166 } else if (chset->primary_ch_set) {
167 for (i = 0; i < chset->downmix_ncoeffs; i++)
168 if ((chset->downmix_coeffs[i] = dca_get_dmix_coeff(s)) == -1)
169 return AVERROR_INVALIDDATA;
172 for (c = 0, i = 0; c < s->xll_channels; c++, i += chset->channels + 1) {
173 if ((chset->downmix_coeffs[i] = dca_get_inv_dmix_coeff(s)) == -1)
174 return AVERROR_INVALIDDATA;
175 for (r = 1; r <= chset->channels; r++) {
176 int32_t coeff = dca_get_dmix_coeff(s);
178 return AVERROR_INVALIDDATA;
179 chset->downmix_coeffs[i + r] =
180 (chset->downmix_coeffs[i] * (int64_t) coeff + (1 << 15)) >> 16;
185 chset->ch_mask_enabled = get_bits(&s->gb, 1);
186 if (chset->ch_mask_enabled)
187 chset->ch_mask = get_bits(&s->gb, s->xll_bits4ch_mask);
189 /* Skip speaker configuration bits */
190 skip_bits_long(&s->gb, 25 * chset->channels);
192 chset->primary_ch_set = 1;
193 chset->downmix_coeff_code_embedded = 0;
194 /* Spec: NumChHierChSet = 0, NumDwnMixCodeCoeffs = 0, whatever that means. */
195 chset->mapping_coeffs_present = get_bits(&s->gb, 1);
196 if (chset->mapping_coeffs_present) {
197 avpriv_report_missing_feature(s->avctx, "XLL: mapping coefficients");
198 return AVERROR_PATCHWELCOME;
201 if (chset->sampling_frequency > 96000)
202 chset->num_freq_bands = 2 * (1 + get_bits(&s->gb, 1));
204 chset->num_freq_bands = 1;
206 if (chset->num_freq_bands > 1) {
207 avpriv_report_missing_feature(s->avctx, "XLL: num_freq_bands > 1");
208 return AVERROR_PATCHWELCOME;
211 if (get_bits(&s->gb, 1)) { /* pw_ch_decor_enabled */
212 int bits = av_ceil_log2(chset->channels);
213 for (i = 0; i < chset->channels; i++) {
214 unsigned j = get_bits(&s->gb, bits);
215 if (j >= chset->channels) {
216 av_log(s->avctx, AV_LOG_ERROR,
217 "Original channel order value %u too large, only %d channels.\n",
219 return AVERROR_INVALIDDATA;
221 chset->orig_chan_order[0][i] = j;
222 chset->orig_chan_order_inv[0][j] = i;
224 for (i = 0; i < chset->channels / 2; i++) {
225 if (get_bits(&s->gb, 1)) /* bChPFlag */
226 chset->pw_ch_pairs_coeffs[0][i] = get_bits_sm(&s->gb, 7);
228 chset->pw_ch_pairs_coeffs[0][i] = 0;
231 for (i = 0; i < chset->channels; i++)
232 chset->orig_chan_order[0][i] =
233 chset->orig_chan_order_inv[0][i] = i;
234 for (i = 0; i < chset->channels / 2; i++)
235 chset->pw_ch_pairs_coeffs[0][i] = 0;
237 /* Adaptive prediction order */
238 chset->adapt_order_max[0] = 0;
239 for (i = 0; i < chset->channels; i++) {
240 chset->adapt_order[0][i] = get_bits(&s->gb, 4);
241 if (chset->adapt_order_max[0] < chset->adapt_order[0][i])
242 chset->adapt_order_max[0] = chset->adapt_order[0][i];
244 /* Fixed prediction order, used in case the adaptive order
246 for (i = 0; i < chset->channels; i++)
247 chset->fixed_order[0][i] =
248 chset->adapt_order[0][i] ? 0 : get_bits(&s->gb, 2);
250 for (i = 0; i < chset->channels; i++) {
252 for (j = 0; j < chset->adapt_order[0][i]; j++)
253 chset->lpc_refl_coeffs_q_ind[0][i][j] = get_bits(&s->gb, 8);
256 if (s->xll_scalable_lsb) {
257 chset->lsb_fsize[0] = get_bits(&s->gb, s->xll_bits4seg_size);
259 for (i = 0; i < chset->channels; i++)
260 chset->scalable_lsbs[0][i] = get_bits(&s->gb, 4);
261 for (i = 0; i < chset->channels; i++)
262 chset->bit_width_adj_per_ch[0][i] = get_bits(&s->gb, 4);
264 memset(chset->scalable_lsbs[0], 0,
265 chset->channels * sizeof(chset->scalable_lsbs[0][0]));
266 memset(chset->bit_width_adj_per_ch[0], 0,
267 chset->channels * sizeof(chset->bit_width_adj_per_ch[0][0]));
270 s->xll_channels += chset->channels;
271 s->xll_residual_channels += chset->channels -
272 av_popcount(chset->residual_encode);
274 /* FIXME: Parse header data for extra frequency bands. */
276 /* Skip to end of channel set sub header. */
277 i = get_bits_count(&s->gb);
278 if (hdr_pos + 8 * hdr_size < i) {
279 av_log(s->avctx, AV_LOG_ERROR,
280 "chset header too large, %d bits, should be <= %d bits\n",
281 i - hdr_pos, 8 * hdr_size);
282 return AVERROR_INVALIDDATA;
284 if (hdr_pos + 8 * hdr_size > i)
285 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
290 /* parse XLL navigation table */
291 int ff_dca_xll_decode_navi(DCAContext *s, int asset_end)
293 int nbands, band, chset, seg, data_start;
295 /* FIXME: Supports only a single frequency band */
298 for (band = 0; band < nbands; band++) {
299 s->xll_navi.band_size[band] = 0;
300 for (seg = 0; seg < s->xll_segments; seg++) {
301 /* Note: The spec, ETSI TS 102 114 V1.4.1 (2012-09), says
302 * we should read a base value for segment_size from the
303 * stream, before reading the sizes of the channel sets.
304 * But that's apparently incorrect. */
305 s->xll_navi.segment_size[band][seg] = 0;
307 for (chset = 0; chset < s->xll_nch_sets; chset++)
308 if (band < s->xll_chsets[chset].num_freq_bands) {
309 s->xll_navi.chset_size[band][seg][chset] =
310 get_bits(&s->gb, s->xll_bits4seg_size) + 1;
311 s->xll_navi.segment_size[band][seg] +=
312 s->xll_navi.chset_size[band][seg][chset];
314 s->xll_navi.band_size[band] += s->xll_navi.segment_size[band][seg];
317 /* Align to 8 bits and skip 16-bit CRC. */
318 skip_bits_long(&s->gb, 16 + ((-get_bits_count(&s->gb)) & 7));
320 data_start = get_bits_count(&s->gb);
321 if (data_start + 8 * s->xll_navi.band_size[0] > asset_end) {
322 av_log(s->avctx, AV_LOG_ERROR,
323 "XLL: Data in NAVI table exceeds containing asset\n"
324 "start: %d (bit), size %u (bytes), end %d (bit), error %u\n",
325 data_start, s->xll_navi.band_size[0], asset_end,
326 data_start + 8 * s->xll_navi.band_size[0] - asset_end);
327 return AVERROR_INVALIDDATA;
329 init_get_bits(&s->xll_navi.gb, s->gb.buffer + data_start / 8,
330 8 * s->xll_navi.band_size[0]);
334 static void dca_xll_inv_adapt_pred(int *samples, int nsamples, unsigned order,
335 const int *prev, const uint8_t *q_ind)
337 static const uint16_t table[0x81] = {
338 0, 3070, 5110, 7140, 9156, 11154, 13132, 15085,
339 17010, 18904, 20764, 22588, 24373, 26117, 27818, 29474,
340 31085, 32648, 34164, 35631, 37049, 38418, 39738, 41008,
341 42230, 43404, 44530, 45609, 46642, 47630, 48575, 49477,
342 50337, 51157, 51937, 52681, 53387, 54059, 54697, 55302,
343 55876, 56421, 56937, 57426, 57888, 58326, 58741, 59132,
344 59502, 59852, 60182, 60494, 60789, 61066, 61328, 61576,
345 61809, 62029, 62236, 62431, 62615, 62788, 62951, 63105,
346 63250, 63386, 63514, 63635, 63749, 63855, 63956, 64051,
347 64140, 64224, 64302, 64376, 64446, 64512, 64573, 64631,
348 64686, 64737, 64785, 64830, 64873, 64913, 64950, 64986,
349 65019, 65050, 65079, 65107, 65133, 65157, 65180, 65202,
350 65222, 65241, 65259, 65275, 65291, 65306, 65320, 65333,
351 65345, 65357, 65368, 65378, 65387, 65396, 65405, 65413,
352 65420, 65427, 65434, 65440, 65446, 65451, 65456, 65461,
353 65466, 65470, 65474, 65478, 65481, 65485, 65488, 65491,
354 65535, /* Final value is for the -128 corner case, see below. */
356 int c[DCA_XLL_AORDER_MAX];
360 for (i = 0; i < order; i++) {
362 /* The index value 0xff corresponds to a lookup of entry 0x80 in
363 * the table, and no value is provided in the specification. */
364 c[i] = -table[(q_ind[i] >> 1) + 1];
366 c[i] = table[q_ind[i] >> 1];
368 /* The description in the spec is a bit convoluted. We can convert
369 * the reflected values to direct values in place, using a
370 * sequence of reflections operating on two values. */
371 for (i = 1; i < order; i++) {
373 * i = 2: reflect c[0] <-> c[1]
374 * i = 3: scale c[1], reflect c[0] <-> c[2]
375 * i = 4: reflect c[0] <-> c[3] reflect c[1] <-> c[2]
378 c[i / 2] += ((int64_t) c[i] * c[i / 2] + 0x8000) >> 16;
379 for (j = 0; j < i / 2; j++) {
381 int r1 = c[i - j - 1];
382 c[j] += ((int64_t) c[i] * r1 + 0x8000) >> 16;
383 c[i - j - 1] += ((int64_t) c[i] * r0 + 0x8000) >> 16;
386 /* Apply predictor. */
387 /* NOTE: Processing samples in this order means that the
388 * predictor is applied to the newly reconstructed samples. */
390 for (i = 0; i < order; i++) {
391 for (j = s = 0; j < i; j++)
392 s += (int64_t) c[j] * samples[i - 1 - j];
393 for (; j < order; j++)
394 s += (int64_t) c[j] * prev[DCA_XLL_AORDER_MAX + i - 1 - j];
396 samples[i] -= av_clip_intp2((s + 0x8000) >> 16, 24);
399 for (i = order; i < nsamples; i++) {
400 for (j = s = 0; j < order; j++)
401 s += (int64_t) c[j] * samples[i - 1 - j];
403 /* NOTE: Equations seem to imply addition, while the
404 * pseudocode seems to use subtraction.*/
405 samples[i] -= av_clip_intp2((s + 0x8000) >> 16, 24);
409 int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame)
411 /* FIXME: Decodes only the first frequency band. */
414 /* Coding parameters for each channel set. */
415 struct coding_params {
417 int rice_code_flag[16];
419 int pancABIT0[16]; /* Not sure what this is */
420 int pancABIT[16]; /* Not sure what this is */
424 GetBitContext *gb = &s->xll_navi.gb;
427 /* Layout: First the sample buffer for one segment per channel,
428 * followed by history buffers of DCA_XLL_AORDER_MAX samples for
430 av_fast_malloc(&s->xll_sample_buf, &s->xll_sample_buf_size,
431 (s->xll_smpl_in_seg + DCA_XLL_AORDER_MAX) *
432 s->xll_channels * sizeof(*s->xll_sample_buf));
433 if (!s->xll_sample_buf)
434 return AVERROR(ENOMEM);
436 history = s->xll_sample_buf + s->xll_smpl_in_seg * s->xll_channels;
438 for (seg = 0; seg < s->xll_segments; seg++) {
441 for (chset_i = in_channel = 0; chset_i < s->xll_nch_sets; chset_i++) {
442 /* The spec isn't very explicit, but I think the NAVI sizes are in bytes. */
443 int end_pos = get_bits_count(gb) +
444 8 * s->xll_navi.chset_size[0][seg][chset_i];
446 struct coding_params *params = ¶m_state[chset_i];
447 /* I think this flag means that we should keep seg_type and
448 * other parameters from the previous segment. */
449 int use_seg_state_code_param;
450 XllChSetSubHeader *chset = &s->xll_chsets[chset_i];
451 if (in_channel >= s->avctx->channels)
452 /* FIXME: Could go directly to next segment */
455 if (s->avctx->sample_rate != chset->sampling_frequency) {
456 av_log(s->avctx, AV_LOG_WARNING,
457 "XLL: unexpected chset sample rate %d, expected %d\n",
458 chset->sampling_frequency, s->avctx->sample_rate);
462 use_seg_state_code_param = get_bits(gb, 1);
464 use_seg_state_code_param = 0;
466 if (!use_seg_state_code_param) {
467 int num_param_sets, i;
470 params->seg_type = get_bits(gb, 1);
471 num_param_sets = params->seg_type ? 1 : chset->channels;
473 if (chset->bit_width > 16) {
476 if (chset->bit_width > 8)
480 if (s->xll_nch_sets > 1)
484 for (i = 0; i < num_param_sets; i++) {
485 params->rice_code_flag[i] = get_bits(gb, 1);
486 if (!params->seg_type && params->rice_code_flag[i] && get_bits(gb, 1))
487 params->pancAuxABIT[i] = get_bits(gb, bits4ABIT) + 1;
489 params->pancAuxABIT[i] = 0;
492 for (i = 0; i < num_param_sets; i++) {
494 /* Parameters for part 1 */
495 params->pancABIT0[i] = get_bits(gb, bits4ABIT);
496 if (params->rice_code_flag[i] == 0 && params->pancABIT0[i] > 0)
497 /* For linear code */
498 params->pancABIT0[i]++;
500 /* NOTE: In the spec, not indexed by band??? */
501 if (params->seg_type == 0)
502 params->nSamplPart0[i] = chset->adapt_order[0][i];
504 params->nSamplPart0[i] = chset->adapt_order_max[0];
506 params->nSamplPart0[i] = 0;
508 /* Parameters for part 2 */
509 params->pancABIT[i] = get_bits(gb, bits4ABIT);
510 if (params->rice_code_flag[i] == 0 && params->pancABIT[i] > 0)
511 /* For linear code */
512 params->pancABIT[i]++;
515 for (i = 0; i < chset->channels; i++) {
516 int param_index = params->seg_type ? 0 : i;
517 int part0 = params->nSamplPart0[param_index];
518 int bits = part0 ? params->pancABIT0[param_index] : 0;
519 int *sample_buf = s->xll_sample_buf +
520 (in_channel + i) * s->xll_smpl_in_seg;
522 if (!params->rice_code_flag[param_index]) {
525 for (j = 0; j < part0; j++)
526 sample_buf[j] = get_bits_sm(gb, bits);
528 memset(sample_buf, 0, part0 * sizeof(sample_buf[0]));
531 bits = params->pancABIT[param_index];
533 for (j = part0; j < s->xll_smpl_in_seg; j++)
534 sample_buf[j] = get_bits_sm(gb, bits);
536 memset(sample_buf + part0, 0,
537 (s->xll_smpl_in_seg - part0) * sizeof(sample_buf[0]));
539 int aux_bits = params->pancAuxABIT[param_index];
541 for (j = 0; j < part0; j++) {
542 /* FIXME: Is this identical to Golomb code? */
543 int t = get_unary(gb, 1, 33) << bits;
544 /* FIXME: Could move this test outside of the loop, for efficiency. */
546 t |= get_bits(gb, bits);
547 sample_buf[j] = (t & 1) ? -(t >> 1) - 1 : (t >> 1);
551 bits = params->pancABIT[param_index];
553 /* Follow the spec's suggestion of using the
554 * buffer also to store the hybrid-rice flags. */
555 memset(sample_buf + part0, 0,
556 (s->xll_smpl_in_seg - part0) * sizeof(sample_buf[0]));
559 /* For hybrid rice encoding, some samples are linearly
560 * coded. According to the spec, "nBits4SamplLoci" bits
561 * are used for each index, but this value is not
562 * defined. I guess we should use log2(xll_smpl_in_seg)
564 int count = get_bits(gb, s->xll_log_smpl_in_seg);
565 av_log(s->avctx, AV_LOG_DEBUG, "aux count %d (bits %d)\n",
566 count, s->xll_log_smpl_in_seg);
568 for (j = 0; j < count; j++)
569 sample_buf[get_bits(gb, s->xll_log_smpl_in_seg)] = 1;
571 for (j = part0; j < s->xll_smpl_in_seg; j++) {
572 if (!sample_buf[j]) {
573 int t = get_unary(gb, 1, 33);
575 t = (t << bits) | get_bits(gb, bits);
576 sample_buf[j] = (t & 1) ? -(t >> 1) - 1 : (t >> 1);
578 sample_buf[j] = get_bits_sm(gb, aux_bits);
583 for (i = 0; i < chset->channels; i++) {
584 unsigned adapt_order = chset->adapt_order[0][i];
585 int *sample_buf = s->xll_sample_buf +
586 (in_channel + i) * s->xll_smpl_in_seg;
587 int *prev = history + (in_channel + i) * DCA_XLL_AORDER_MAX;
591 for (order = chset->fixed_order[0][i]; order > 0; order--) {
593 for (j = 1; j < s->xll_smpl_in_seg; j++)
594 sample_buf[j] += sample_buf[j - 1];
597 /* Inverse adaptive prediction, in place. */
598 dca_xll_inv_adapt_pred(sample_buf, s->xll_smpl_in_seg,
599 adapt_order, seg ? prev : NULL,
600 chset->lpc_refl_coeffs_q_ind[0][i]);
601 memcpy(prev, sample_buf + s->xll_smpl_in_seg - DCA_XLL_AORDER_MAX,
602 DCA_XLL_AORDER_MAX * sizeof(*prev));
604 for (i = 1; i < chset->channels; i += 2) {
605 int coeff = chset->pw_ch_pairs_coeffs[0][i / 2];
607 int *sample_buf = s->xll_sample_buf +
608 (in_channel + i) * s->xll_smpl_in_seg;
609 int *prev = sample_buf - s->xll_smpl_in_seg;
611 for (j = 0; j < s->xll_smpl_in_seg; j++)
612 /* Shift is unspecified, but should apparently be 3. */
613 sample_buf[j] += ((int64_t) coeff * prev[j] + 4) >> 3;
617 if (s->xll_scalable_lsb) {
618 int lsb_start = end_pos - 8 * chset->lsb_fsize[0] -
619 8 * (s->xll_banddata_crc & 2);
621 i = get_bits_count(gb);
623 av_log(s->avctx, AV_LOG_ERROR,
624 "chset data lsb exceeds NAVI size, end_pos %d, lsb_start %d, pos %d\n",
625 end_pos, lsb_start, i);
626 return AVERROR_INVALIDDATA;
629 skip_bits_long(gb, lsb_start - i);
631 for (i = done = 0; i < chset->channels; i++) {
632 int bits = chset->scalable_lsbs[0][i];
634 /* The channel reordering is conceptually done
635 * before adding the lsb:s, so we need to do
636 * the inverse permutation here. */
637 unsigned pi = chset->orig_chan_order_inv[0][i];
638 int *sample_buf = s->xll_sample_buf +
639 (in_channel + pi) * s->xll_smpl_in_seg;
640 int adj = chset->bit_width_adj_per_ch[0][i];
641 int msb_shift = bits;
645 msb_shift += adj - 1;
647 for (j = 0; j < s->xll_smpl_in_seg; j++)
648 sample_buf[j] = (sample_buf[j] << msb_shift) +
649 (get_bits(gb, bits) << adj);
651 done += bits * s->xll_smpl_in_seg;
654 if (done > 8 * chset->lsb_fsize[0]) {
655 av_log(s->avctx, AV_LOG_ERROR,
656 "chset lsb exceeds lsb_size\n");
657 return AVERROR_INVALIDDATA;
662 for (i = 0; i < chset->channels; i++) {
663 int *sample_buf = s->xll_sample_buf +
664 (in_channel + i) * s->xll_smpl_in_seg;
665 int shift = 1 - chset->bit_resolution;
666 int out_channel = chset->orig_chan_order[0][i];
669 /* XLL uses the channel order C, L, R, and we want L,
670 * R, C. FIXME: Generalize. */
671 if (chset->ch_mask_enabled &&
672 (chset->ch_mask & 7) == 7 && out_channel < 3)
673 out_channel = out_channel ? out_channel - 1 : 2;
675 out_channel += in_channel;
676 if (out_channel >= s->avctx->channels)
679 out = (float *) frame->extended_data[out_channel];
680 out += seg * s->xll_smpl_in_seg;
682 /* NOTE: A one bit means residual encoding is *not* used. */
683 if ((chset->residual_encode >> i) & 1) {
684 /* Replace channel samples.
685 * FIXME: Most likely not the right thing to do. */
686 for (j = 0; j < s->xll_smpl_in_seg; j++)
687 out[j] = ldexpf(sample_buf[j], shift);
689 /* Add residual signal to core channel */
690 for (j = 0; j < s->xll_smpl_in_seg; j++)
691 out[j] += ldexpf(sample_buf[j], shift);
695 if (chset->downmix_coeff_code_embedded &&
696 !chset->primary_ch_set && chset->hier_chset) {
697 /* Undo hierarchical downmix of earlier channels. */
698 unsigned mix_channel;
699 for (mix_channel = 0; mix_channel < in_channel; mix_channel++) {
704 /* Similar channel reorder C, L, R vs L, R, C reorder. */
705 if (chset->ch_mask_enabled &&
706 (chset->ch_mask & 7) == 7 && mix_channel < 3)
707 mix_buf = (float *) frame->extended_data[mix_channel ? mix_channel - 1 : 2];
709 mix_buf = (float *) frame->extended_data[mix_channel];
711 mix_buf += seg * s->xll_smpl_in_seg;
712 col = &chset->downmix_coeffs[mix_channel * (chset->channels + 1)];
715 coeff = ldexpf(col[0], -16);
716 for (j = 0; j < s->xll_smpl_in_seg; j++)
720 row < chset->channels && in_channel + row < s->avctx->channels;
723 const float *new_channel =
724 (const float *) frame->extended_data[in_channel + row];
725 new_channel += seg * s->xll_smpl_in_seg;
726 coeff = ldexpf(col[row + 1], -15);
727 for (j = 0; j < s->xll_smpl_in_seg; j++)
728 mix_buf[j] -= coeff * new_channel[j];
734 in_channel += chset->channels;
735 /* Skip to next channel set using the NAVI info. */
736 i = get_bits_count(gb);
738 av_log(s->avctx, AV_LOG_ERROR,
739 "chset data exceeds NAVI size\n");
740 return AVERROR_INVALIDDATA;
743 skip_bits_long(gb, end_pos - i);