2 * Copyright (C) 2016 foo86
4 * This file is part of FFmpeg.
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.
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.
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
24 #include "dca_syncwords.h"
27 static int get_linear(GetBitContext *gb, int n)
29 unsigned int v = get_bits_long(gb, n);
30 return (v >> 1) ^ -(v & 1);
33 static int get_rice_un(GetBitContext *gb, int k)
35 unsigned int v = get_unary(gb, 1, 128);
36 return (v << k) | get_bits_long(gb, k);
39 static int get_rice(GetBitContext *gb, int k)
41 unsigned int v = get_rice_un(gb, k);
42 return (v >> 1) ^ -(v & 1);
45 static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
49 for (i = 0; i < size; i++)
50 array[i] = get_bits(gb, n);
53 static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
58 memset(array, 0, sizeof(*array) * size);
59 else for (i = 0; i < size; i++)
60 array[i] = get_linear(gb, n);
63 static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
67 for (i = 0; i < size; i++)
68 array[i] = get_rice(gb, k);
71 static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
73 // Size of downmix coefficient matrix
74 int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
75 int i, j, *coeff_ptr = c->dmix_coeff;
77 for (i = 0; i < m; i++) {
78 int code, sign, coeff, scale, scale_inv = 0;
81 // Downmix scale (only for non-primary channel sets)
82 if (!c->primary_chset) {
83 code = get_bits(&s->gb, 9);
84 sign = (code >> 8) - 1;
85 index = (code & 0xff) - FF_DCA_DMIXTABLE_OFFSET;
86 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
87 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");
88 return AVERROR_INVALIDDATA;
90 scale = ff_dca_dmixtable[index + FF_DCA_DMIXTABLE_OFFSET];
91 scale_inv = ff_dca_inv_dmixtable[index];
92 c->dmix_scale[i] = (scale ^ sign) - sign;
93 c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;
96 // Downmix coefficients
97 for (j = 0; j < c->nchannels; j++) {
98 code = get_bits(&s->gb, 9);
99 sign = (code >> 8) - 1;
101 if (index >= FF_DCA_DMIXTABLE_SIZE) {
102 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");
103 return AVERROR_INVALIDDATA;
105 coeff = ff_dca_dmixtable[index];
106 if (!c->primary_chset)
107 // Multiply by |InvDmixScale| to get |UndoDmixScale|
108 coeff = mul16(scale_inv, coeff);
109 *coeff_ptr++ = (coeff ^ sign) - sign;
116 static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
118 int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
119 DCAXllChSet *p = &s->chset[0];
122 // Size of channel set sub-header
123 header_size = get_bits(&s->gb, 10) + 1;
126 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
127 && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
128 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");
129 return AVERROR_INVALIDDATA;
132 // Number of channels in the channel set
133 c->nchannels = get_bits(&s->gb, 4) + 1;
134 if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
135 avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
136 return AVERROR_PATCHWELCOME;
140 c->residual_encode = get_bits(&s->gb, c->nchannels);
142 // PCM bit resolution
143 c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
145 // Storage unit width
146 c->storage_bit_res = get_bits(&s->gb, 5) + 1;
147 if (c->storage_bit_res != 16 && c->storage_bit_res != 24) {
148 avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);
149 return AVERROR_PATCHWELCOME;
152 if (c->pcm_bit_res > c->storage_bit_res) {
153 av_log(s->avctx, AV_LOG_ERROR, "Invalid PCM bit resolution for XLL channel set (%d > %d)\n", c->pcm_bit_res, c->storage_bit_res);
154 return AVERROR_INVALIDDATA;
157 // Original sampling frequency
158 c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
159 if (c->freq > 192000) {
160 avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
161 return AVERROR_PATCHWELCOME;
164 // Sampling frequency modifier
165 if (get_bits(&s->gb, 2)) {
166 avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
167 return AVERROR_PATCHWELCOME;
170 // Which replacement set this channel set is member of
171 if (get_bits(&s->gb, 2)) {
172 avpriv_request_sample(s->avctx, "XLL replacement set");
173 return AVERROR_PATCHWELCOME;
176 if (asset->one_to_one_map_ch_to_spkr) {
177 // Primary channel set flag
178 c->primary_chset = get_bits1(&s->gb);
179 if (c->primary_chset != (c == p)) {
180 av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");
181 return AVERROR_INVALIDDATA;
184 // Downmix coefficients present in stream
185 c->dmix_coeffs_present = get_bits1(&s->gb);
187 // Downmix already performed by encoder
188 c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
191 if (c->dmix_coeffs_present && c->primary_chset) {
192 c->dmix_type = get_bits(&s->gb, 3);
193 if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {
194 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");
195 return AVERROR_INVALIDDATA;
199 // Whether the channel set is part of a hierarchy
200 c->hier_chset = get_bits1(&s->gb);
201 if (!c->hier_chset && s->nchsets != 1) {
202 avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
203 return AVERROR_PATCHWELCOME;
206 // Downmix coefficients
207 if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
210 // Channel mask enabled
211 if (!get_bits1(&s->gb)) {
212 avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
213 return AVERROR_PATCHWELCOME;
216 // Channel mask for set
217 c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
218 if (av_popcount(c->ch_mask) != c->nchannels) {
219 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");
220 return AVERROR_INVALIDDATA;
223 // Build the channel to speaker map
224 for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
225 if (c->ch_mask & (1U << i))
226 c->ch_remap[j++] = i;
228 // Mapping coeffs present flag
229 if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {
230 avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");
231 return AVERROR_PATCHWELCOME;
234 // Setup for LtRt decoding
235 c->primary_chset = 1;
236 c->dmix_coeffs_present = 0;
237 c->dmix_embedded = 0;
239 c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;
240 c->ch_remap[0] = DCA_SPEAKER_L;
241 c->ch_remap[1] = DCA_SPEAKER_R;
244 if (c->freq > 96000) {
245 // Extra frequency bands flag
246 if (get_bits1(&s->gb)) {
247 avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
248 return AVERROR_PATCHWELCOME;
255 // Set the sampling frequency to that of the first frequency band.
256 // Frequency will be doubled again after bands assembly.
257 c->freq >>= c->nfreqbands - 1;
259 // Verify that all channel sets have the same audio characteristics
260 if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
261 || c->pcm_bit_res != p->pcm_bit_res
262 || c->storage_bit_res != p->storage_bit_res)) {
263 avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
264 return AVERROR_PATCHWELCOME;
267 // Determine number of bits to read bit allocation coding parameter
268 if (c->storage_bit_res > 16)
270 else if (c->storage_bit_res > 8)
275 // Account for embedded downmix and decimator saturation
276 if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
279 for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
280 // Pairwise channel decorrelation
281 if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
282 int ch_nbits = av_ceil_log2(c->nchannels);
284 // Original channel order
285 for (i = 0; i < c->nchannels; i++) {
286 b->orig_order[i] = get_bits(&s->gb, ch_nbits);
287 if (b->orig_order[i] >= c->nchannels) {
288 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");
289 return AVERROR_INVALIDDATA;
293 // Pairwise channel coefficients
294 for (i = 0; i < c->nchannels / 2; i++)
295 b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
297 for (i = 0; i < c->nchannels; i++)
298 b->orig_order[i] = i;
299 for (i = 0; i < c->nchannels / 2; i++)
300 b->decor_coeff[i] = 0;
303 // Adaptive predictor order
304 b->highest_pred_order = 0;
305 for (i = 0; i < c->nchannels; i++) {
306 b->adapt_pred_order[i] = get_bits(&s->gb, 4);
307 if (b->adapt_pred_order[i] > b->highest_pred_order)
308 b->highest_pred_order = b->adapt_pred_order[i];
310 if (b->highest_pred_order > s->nsegsamples) {
311 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive predicition order\n");
312 return AVERROR_INVALIDDATA;
315 // Fixed predictor order
316 for (i = 0; i < c->nchannels; i++)
317 b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
319 // Adaptive predictor quantized reflection coefficients
320 for (i = 0; i < c->nchannels; i++) {
321 for (j = 0; j < b->adapt_pred_order[i]; j++) {
322 k = get_linear(&s->gb, 8);
324 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
325 return AVERROR_INVALIDDATA;
328 b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
330 b->adapt_refl_coeff[i][j] = (int)ff_dca_xll_refl_coeff[ k];
334 // Downmix performed by encoder in extension frequency band
335 b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
337 // MSB/LSB split flag in extension frequency band
338 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
339 // Size of LSB section in any segment
340 b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
341 if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {
342 av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");
343 return AVERROR_INVALIDDATA;
346 // Account for optional CRC bytes after LSB section
347 if (b->lsb_section_size && (s->band_crc_present > 2 ||
348 (band == 0 && s->band_crc_present > 1)))
349 b->lsb_section_size += 2;
351 // Number of bits to represent the samples in LSB part
352 for (i = 0; i < c->nchannels; i++) {
353 b->nscalablelsbs[i] = get_bits(&s->gb, 4);
354 if (b->nscalablelsbs[i] && !b->lsb_section_size) {
355 av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");
356 return AVERROR_INVALIDDATA;
360 b->lsb_section_size = 0;
361 for (i = 0; i < c->nchannels; i++)
362 b->nscalablelsbs[i] = 0;
365 // Scalable resolution flag in extension frequency band
366 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
367 // Number of bits discarded by authoring
368 for (i = 0; i < c->nchannels; i++)
369 b->bit_width_adjust[i] = get_bits(&s->gb, 4);
371 for (i = 0; i < c->nchannels; i++)
372 b->bit_width_adjust[i] = 0;
378 // CRC16 of channel set sub-header
379 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
380 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");
381 return AVERROR_INVALIDDATA;
387 static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
389 int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
390 int nchsamples = s->nframesamples + ndecisamples;
391 int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
394 // Reallocate MSB sample buffer
395 av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
396 if (!c->sample_buffer[0])
397 return AVERROR(ENOMEM);
399 ptr = c->sample_buffer[0] + ndecisamples;
400 for (i = 0; i < c->nfreqbands; i++) {
401 for (j = 0; j < c->nchannels; j++) {
402 c->bands[i].msb_sample_buffer[j] = ptr;
410 static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
412 int i, j, nsamples = 0;
415 // Determine number of frequency bands that have MSB/LSB split
416 for (i = 0; i < c->nfreqbands; i++)
417 if (c->bands[i].lsb_section_size)
418 nsamples += s->nframesamples * c->nchannels;
422 // Reallocate LSB sample buffer
423 av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
424 if (!c->sample_buffer[1])
425 return AVERROR(ENOMEM);
427 ptr = c->sample_buffer[1];
428 for (i = 0; i < c->nfreqbands; i++) {
429 if (c->bands[i].lsb_section_size) {
430 for (j = 0; j < c->nchannels; j++) {
431 c->bands[i].lsb_sample_buffer[j] = ptr;
432 ptr += s->nframesamples;
435 for (j = 0; j < c->nchannels; j++)
436 c->bands[i].lsb_sample_buffer[j] = NULL;
443 static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
445 DCAXllBand *b = &c->bands[band];
448 // Start unpacking MSB portion of the segment
449 if (!(seg && get_bits1(&s->gb))) {
450 // Unpack segment type
451 // 0 - distinct coding parameters for each channel
452 // 1 - common coding parameters for all channels
453 c->seg_common = get_bits1(&s->gb);
455 // Determine number of coding parameters encoded in segment
456 k = c->seg_common ? 1 : c->nchannels;
458 // Unpack Rice coding parameters
459 for (i = 0; i < k; i++) {
460 // Unpack Rice coding flag
461 // 0 - linear code, 1 - Rice code
462 c->rice_code_flag[i] = get_bits1(&s->gb);
463 // Unpack Hybrid Rice coding flag
464 // 0 - Rice code, 1 - Hybrid Rice code
465 if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))
466 // Unpack binary code length for isolated samples
467 c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
469 // 0 indicates no Hybrid Rice coding
470 c->bitalloc_hybrid_linear[i] = 0;
473 // Unpack coding parameters
474 for (i = 0; i < k; i++) {
476 // Unpack coding parameter for part A of segment 0
477 c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
479 // Adjust for the linear code
480 if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
481 c->bitalloc_part_a[i]++;
484 c->nsamples_part_a[i] = b->adapt_pred_order[i];
486 c->nsamples_part_a[i] = b->highest_pred_order;
488 c->bitalloc_part_a[i] = 0;
489 c->nsamples_part_a[i] = 0;
492 // Unpack coding parameter for part B of segment
493 c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
495 // Adjust for the linear code
496 if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
497 c->bitalloc_part_b[i]++;
501 // Unpack entropy codes
502 for (i = 0; i < c->nchannels; i++) {
503 int32_t *part_a, *part_b;
506 // Select index of coding parameters
507 k = c->seg_common ? 0 : i;
509 // Slice the segment into parts A and B
510 part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
511 part_b = part_a + c->nsamples_part_a[k];
512 nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
514 if (get_bits_left(&s->gb) < 0)
515 return AVERROR_INVALIDDATA;
517 if (!c->rice_code_flag[k]) {
519 // Unpack all residuals of part A of segment 0
520 get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
521 c->bitalloc_part_a[k]);
523 // Unpack all residuals of part B of segment 0 and others
524 get_linear_array(&s->gb, part_b, nsamples_part_b,
525 c->bitalloc_part_b[k]);
528 // Unpack all residuals of part A of segment 0
529 get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
530 c->bitalloc_part_a[k]);
532 if (c->bitalloc_hybrid_linear[k]) {
534 // Unpack the number of isolated samples
535 int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
537 // Set all locations to 0
538 memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
540 // Extract the locations of isolated samples and flag by -1
541 for (j = 0; j < nisosamples; j++) {
542 int loc = get_bits(&s->gb, s->nsegsamples_log2);
543 if (loc >= nsamples_part_b) {
544 av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
545 return AVERROR_INVALIDDATA;
550 // Unpack all residuals of part B of segment 0 and others
551 for (j = 0; j < nsamples_part_b; j++) {
553 part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
555 part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
559 // Unpack all residuals of part B of segment 0 and others
560 get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
565 // Unpack decimator history for frequency band 1
566 if (seg == 0 && band == 1) {
567 int nbits = get_bits(&s->gb, 5) + 1;
568 for (i = 0; i < c->nchannels; i++)
569 for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
570 c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
573 // Start unpacking LSB portion of the segment
574 if (b->lsb_section_size) {
575 // Skip to the start of LSB portion
576 if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
577 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
578 return AVERROR_INVALIDDATA;
581 // Unpack all LSB parts of residuals of this segment
582 for (i = 0; i < c->nchannels; i++) {
583 if (b->nscalablelsbs[i]) {
585 b->lsb_sample_buffer[i] + seg * s->nsegsamples,
586 s->nsegsamples, b->nscalablelsbs[i]);
591 // Skip to the end of band data
592 if (ff_dca_seek_bits(&s->gb, band_data_end)) {
593 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
594 return AVERROR_INVALIDDATA;
600 static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
602 DCAXllBand *b = &c->bands[band];
603 int i, offset, nsamples;
607 nsamples = s->nframesamples;
609 offset = seg * s->nsegsamples;
610 nsamples = s->nsegsamples;
613 for (i = 0; i < c->nchannels; i++) {
614 memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
615 if (b->lsb_section_size)
616 memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
619 if (seg <= 0 && band)
620 memset(c->deci_history, 0, sizeof(c->deci_history));
623 memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
624 memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
628 static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
630 DCAXllBand *b = &c->bands[band];
631 int nsamples = s->nframesamples;
634 // Inverse adaptive or fixed prediction
635 for (i = 0; i < c->nchannels; i++) {
636 int32_t *buf = b->msb_sample_buffer[i];
637 int order = b->adapt_pred_order[i];
639 int coeff[DCA_XLL_ADAPT_PRED_ORDER_MAX];
640 // Conversion from reflection coefficients to direct form coefficients
641 for (j = 0; j < order; j++) {
642 int rc = b->adapt_refl_coeff[i][j];
643 for (k = 0; k < (j + 1) / 2; k++) {
644 int tmp1 = coeff[ k ];
645 int tmp2 = coeff[j - k - 1];
646 coeff[ k ] = tmp1 + mul16(rc, tmp2);
647 coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
651 // Inverse adaptive prediction
652 for (j = 0; j < nsamples - order; j++) {
654 for (k = 0; k < order; k++)
655 err += (int64_t)buf[j + k] * coeff[order - k - 1];
656 buf[j + k] -= clip23(norm16(err));
659 // Inverse fixed coefficient prediction
660 for (j = 0; j < b->fixed_pred_order[i]; j++)
661 for (k = 1; k < nsamples; k++)
662 buf[k] += buf[k - 1];
666 // Inverse pairwise channel decorrellation
667 if (b->decor_enabled) {
668 int32_t *tmp[DCA_XLL_CHANNELS_MAX];
670 for (i = 0; i < c->nchannels / 2; i++) {
671 int coeff = b->decor_coeff[i];
673 s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
674 b->msb_sample_buffer[i * 2 ],
679 // Reorder channel pointers to the original order
680 for (i = 0; i < c->nchannels; i++)
681 tmp[i] = b->msb_sample_buffer[i];
683 for (i = 0; i < c->nchannels; i++)
684 b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
687 // Map output channel pointers for frequency band 0
688 if (c->nfreqbands == 1)
689 for (i = 0; i < c->nchannels; i++)
690 s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
693 static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
695 int adj = c->bands[band].bit_width_adjust[ch];
696 int shift = c->bands[band].nscalablelsbs[ch];
698 if (s->fixed_lsb_width)
699 shift = s->fixed_lsb_width;
700 else if (shift && adj)
708 static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
710 DCAXllBand *b = &c->bands[band];
711 int n, ch, nsamples = s->nframesamples;
713 for (ch = 0; ch < c->nchannels; ch++) {
714 int shift = chs_get_lsb_width(s, c, band, ch);
716 int32_t *msb = b->msb_sample_buffer[ch];
717 if (b->nscalablelsbs[ch]) {
718 int32_t *lsb = b->lsb_sample_buffer[ch];
719 int adj = b->bit_width_adjust[ch];
720 for (n = 0; n < nsamples; n++)
721 msb[n] = msb[n] * (1 << shift) + (lsb[n] << adj);
723 for (n = 0; n < nsamples; n++)
724 msb[n] = msb[n] * (1 << shift);
730 static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
732 int ch, nsamples = s->nframesamples;
735 av_assert1(c->nfreqbands > 1);
737 // Reallocate frequency band assembly buffer
738 av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
739 2 * nsamples * c->nchannels * sizeof(int32_t));
740 if (!c->sample_buffer[2])
741 return AVERROR(ENOMEM);
743 // Assemble frequency bands 0 and 1
744 ptr = c->sample_buffer[2];
745 for (ch = 0; ch < c->nchannels; ch++) {
746 int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
747 int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
749 // Copy decimator history
750 memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
751 c->deci_history[ch], sizeof(c->deci_history[0]));
754 s->dcadsp->assemble_freq_bands(ptr, band0, band1,
755 ff_dca_xll_band_coeff,
758 // Remap output channel pointer to assembly buffer
759 s->output_samples[c->ch_remap[ch]] = ptr;
766 static int parse_common_header(DCAXllDecoder *s)
768 int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
770 // XLL extension sync word
771 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
772 av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
773 return AVERROR(EAGAIN);
777 stream_ver = get_bits(&s->gb, 4) + 1;
778 if (stream_ver > 1) {
779 avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
780 return AVERROR_PATCHWELCOME;
783 // Lossless frame header length
784 header_size = get_bits(&s->gb, 8) + 1;
787 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
788 && ff_dca_check_crc(&s->gb, 32, header_size * 8)) {
789 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
790 return AVERROR_INVALIDDATA;
793 // Number of bits used to read frame size
794 frame_size_nbits = get_bits(&s->gb, 5) + 1;
796 // Number of bytes in a lossless frame
797 s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
798 if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
799 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
800 return AVERROR_INVALIDDATA;
804 // Number of channels sets per frame
805 s->nchsets = get_bits(&s->gb, 4) + 1;
806 if (s->nchsets > DCA_XLL_CHSETS_MAX) {
807 avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
808 return AVERROR_PATCHWELCOME;
811 // Number of segments per frame
812 nframesegs_log2 = get_bits(&s->gb, 4);
813 s->nframesegs = 1 << nframesegs_log2;
814 if (s->nframesegs > 1024) {
815 av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
816 return AVERROR_INVALIDDATA;
819 // Samples in segment per one frequency band for the first channel set
820 // Maximum value is 256 for sampling frequencies <= 48 kHz
821 // Maximum value is 512 for sampling frequencies > 48 kHz
822 s->nsegsamples_log2 = get_bits(&s->gb, 4);
823 if (!s->nsegsamples_log2) {
824 av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
825 return AVERROR_INVALIDDATA;
827 s->nsegsamples = 1 << s->nsegsamples_log2;
828 if (s->nsegsamples > 512) {
829 av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
830 return AVERROR_INVALIDDATA;
833 // Samples in frame per one frequency band for the first channel set
834 s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
835 s->nframesamples = 1 << s->nframesamples_log2;
836 if (s->nframesamples > 65536) {
837 av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
838 return AVERROR_INVALIDDATA;
841 // Number of bits used to read segment size
842 s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
844 // Presence of CRC16 within each frequency band
845 // 0 - No CRC16 within band
846 // 1 - CRC16 placed at the end of MSB0
847 // 2 - CRC16 placed at the end of MSB0 and LSB0
848 // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
849 s->band_crc_present = get_bits(&s->gb, 2);
851 // MSB/LSB split flag
852 s->scalable_lsbs = get_bits1(&s->gb);
854 // Channel position mask
855 s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
858 if (s->scalable_lsbs)
859 s->fixed_lsb_width = get_bits(&s->gb, 4);
861 s->fixed_lsb_width = 0;
865 // Header CRC16 protection
866 if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
867 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
868 return AVERROR_INVALIDDATA;
874 static int is_hier_dmix_chset(DCAXllChSet *c)
876 return !c->primary_chset && c->dmix_embedded && c->hier_chset;
879 static DCAXllChSet *find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
882 while (++c < &s->chset[s->nchsets])
883 if (is_hier_dmix_chset(c))
889 static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
891 int i, j, *coeff_ptr = c->dmix_coeff;
893 for (i = 0; i < c->hier_ofs; i++) {
894 int scale = o->dmix_scale[i];
895 int scale_inv = o->dmix_scale_inv[i];
896 c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
897 c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
898 for (j = 0; j < c->nchannels; j++) {
899 int coeff = mul16(*coeff_ptr, scale_inv);
900 *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
905 static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
907 DCAContext *dca = s->avctx->priv_data;
911 // Parse channel set headers
915 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
916 c->hier_ofs = s->nchannels;
917 if ((ret = chs_parse_header(s, c, asset)) < 0)
919 if (c->nfreqbands > s->nfreqbands)
920 s->nfreqbands = c->nfreqbands;
922 s->nchannels += c->nchannels;
923 if (c->residual_encode != (1 << c->nchannels) - 1)
927 // Pre-scale downmixing coefficients for all non-primary channel sets
928 for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
929 if (is_hier_dmix_chset(c)) {
930 DCAXllChSet *o = find_next_hier_dmix_chset(s, c);
932 prescale_down_mix(c, o);
936 // Determine number of active channel sets to decode
937 switch (dca->request_channel_layout) {
938 case DCA_SPEAKER_LAYOUT_STEREO:
939 s->nactivechsets = 1;
941 case DCA_SPEAKER_LAYOUT_5POINT0:
942 case DCA_SPEAKER_LAYOUT_5POINT1:
943 s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
946 s->nactivechsets = s->nchsets;
953 static int parse_navi_table(DCAXllDecoder *s)
955 int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
958 // Determine size of NAVI table
959 navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
960 if (navi_nb > 1024) {
961 av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
962 return AVERROR_INVALIDDATA;
965 // Reallocate NAVI table
966 av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
968 return AVERROR(ENOMEM);
971 navi_pos = get_bits_count(&s->gb);
973 for (band = 0; band < s->nfreqbands; band++) {
974 for (seg = 0; seg < s->nframesegs; seg++) {
975 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
977 if (c->nfreqbands > band) {
978 size = get_bits_long(&s->gb, s->seg_size_nbits);
979 if (size < 0 || size >= s->frame_size) {
980 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
981 return AVERROR_INVALIDDATA;
992 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
993 skip_bits(&s->gb, 16);
996 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
997 && ff_dca_check_crc(&s->gb, navi_pos, get_bits_count(&s->gb))) {
998 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
999 return AVERROR_INVALIDDATA;
1005 static int parse_band_data(DCAXllDecoder *s)
1007 int ret, chs, seg, band, navi_pos, *navi_ptr;
1010 for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
1011 if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1013 if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1017 navi_pos = get_bits_count(&s->gb);
1019 for (band = 0; band < s->nfreqbands; band++) {
1020 for (seg = 0; seg < s->nframesegs; seg++) {
1021 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1022 if (c->nfreqbands > band) {
1023 navi_pos += *navi_ptr * 8;
1024 if (navi_pos > s->gb.size_in_bits) {
1025 av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
1026 return AVERROR_INVALIDDATA;
1028 if (chs < s->nactivechsets &&
1029 (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
1030 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1032 chs_clear_band_data(s, c, band, seg);
1034 s->gb.index = navi_pos;
1044 static int parse_frame(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
1048 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1050 if ((ret = parse_common_header(s)) < 0)
1052 if ((ret = parse_sub_headers(s, asset)) < 0)
1054 if ((ret = parse_navi_table(s)) < 0)
1056 if ((ret = parse_band_data(s)) < 0)
1058 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1059 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
1060 return AVERROR_INVALIDDATA;
1065 static void clear_pbr(DCAXllDecoder *s)
1071 static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
1073 if (size > DCA_XLL_PBR_BUFFER_MAX)
1074 return AVERROR(ENOSPC);
1076 if (!s->pbr_buffer && !(s->pbr_buffer = av_malloc(DCA_XLL_PBR_BUFFER_MAX + DCA_BUFFER_PADDING_SIZE)))
1077 return AVERROR(ENOMEM);
1079 memcpy(s->pbr_buffer, data, size);
1080 s->pbr_length = size;
1081 s->pbr_delay = delay;
1085 static int parse_frame_no_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
1087 int ret = parse_frame(s, data, size, asset);
1089 // If XLL packet data didn't start with a sync word, we must have jumped
1090 // right into the middle of PBR smoothing period
1091 if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
1092 // Skip to the next sync word in this packet
1093 data += asset->xll_sync_offset;
1094 size -= asset->xll_sync_offset;
1096 // If decoding delay is set, put the frame into PBR buffer and return
1097 // failure code. Higher level decoder is expected to switch to lossy
1098 // core decoding or mute its output until decoding delay expires.
1099 if (asset->xll_delay_nframes > 0) {
1100 if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
1102 return AVERROR(EAGAIN);
1105 // No decoding delay, just parse the frame in place
1106 ret = parse_frame(s, data, size, asset);
1112 if (s->frame_size > size)
1113 return AVERROR(EINVAL);
1115 // If the XLL decoder didn't consume full packet, start PBR smoothing period
1116 if (s->frame_size < size)
1117 if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
1123 static int parse_frame_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
1127 if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
1128 ret = AVERROR(ENOSPC);
1132 memcpy(s->pbr_buffer + s->pbr_length, data, size);
1133 s->pbr_length += size;
1135 // Respect decoding delay after synchronization error
1136 if (s->pbr_delay > 0 && --s->pbr_delay)
1137 return AVERROR(EAGAIN);
1139 if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
1142 if (s->frame_size > s->pbr_length) {
1143 ret = AVERROR(EINVAL);
1147 if (s->frame_size == s->pbr_length) {
1148 // End of PBR smoothing period
1151 s->pbr_length -= s->frame_size;
1152 memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
1158 // For now, throw out all PBR state on failure.
1159 // Perhaps we can be smarter and try to resync somehow.
1164 int ff_dca_xll_parse(DCAXllDecoder *s, uint8_t *data, DCAExssAsset *asset)
1168 if (s->hd_stream_id != asset->hd_stream_id) {
1170 s->hd_stream_id = asset->hd_stream_id;
1174 ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1176 ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1181 static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1183 int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
1186 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1190 av_assert1(band < c->nfreqbands);
1191 for (j = 0; j < c->nchannels; j++) {
1192 for (k = 0; k < o->nchannels; k++) {
1193 int coeff = *coeff_ptr++;
1195 s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
1196 o->bands[band].msb_sample_buffer[k],
1197 coeff, s->nframesamples);
1199 s->dcadsp->dmix_sub(c->deci_history[j],
1201 coeff, DCA_XLL_DECI_HISTORY_MAX);
1206 nchannels += c->nchannels;
1207 if (nchannels >= o->hier_ofs)
1212 static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1214 int i, j, nchannels = 0;
1217 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1221 av_assert1(band < c->nfreqbands);
1222 for (j = 0; j < c->nchannels; j++) {
1223 int scale = o->dmix_scale[nchannels++];
1224 if (scale != (1 << 15)) {
1225 s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
1226 scale, s->nframesamples);
1228 s->dcadsp->dmix_scale(c->deci_history[j],
1229 scale, DCA_XLL_DECI_HISTORY_MAX);
1233 if (nchannels >= o->hier_ofs)
1238 // Clear all band data and replace non-residual encoded channels with lossy
1240 static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
1242 DCAContext *dca = s->avctx->priv_data;
1245 for (band = 0; band < c->nfreqbands; band++)
1246 chs_clear_band_data(s, c, band, -1);
1248 for (ch = 0; ch < c->nchannels; ch++) {
1249 if (!(c->residual_encode & (1 << ch)))
1251 if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
1253 c->residual_encode &= ~(1 << ch);
1257 static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
1259 DCAContext *dca = s->avctx->priv_data;
1260 int ch, nsamples = s->nframesamples;
1263 // Verify that core is compatible
1264 if (!(dca->packet & DCA_PACKET_CORE)) {
1265 av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
1266 return AVERROR(EINVAL);
1269 if (c->freq != dca->core.output_rate) {
1270 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);
1271 return AVERROR_INVALIDDATA;
1274 if (nsamples != dca->core.npcmsamples) {
1275 av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
1276 return AVERROR_INVALIDDATA;
1279 // See if this channel set is downmixed and find the next channel set in
1280 // hierarchy. If downmixed, undo core pre-scaling before combining with
1281 // residual (residual is not scaled).
1282 o = find_next_hier_dmix_chset(s, c);
1284 // Reduce core bit width and combine with residual
1285 for (ch = 0; ch < c->nchannels; ch++) {
1286 int n, spkr, shift, round;
1289 if (c->residual_encode & (1 << ch))
1292 // Map this channel to core speaker
1293 spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
1295 av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
1296 return AVERROR_INVALIDDATA;
1299 // Account for LSB width
1300 shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
1302 av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
1303 return AVERROR_INVALIDDATA;
1306 round = shift > 0 ? 1 << (shift - 1) : 0;
1308 src = dca->core.output_samples[spkr];
1309 dst = c->bands[0].msb_sample_buffer[ch];
1311 // Undo embedded core downmix pre-scaling
1312 int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
1313 for (n = 0; n < nsamples; n++)
1314 dst[n] += clip23((mul16(src[n], scale_inv) + round) >> shift);
1316 // No downmix scaling
1317 for (n = 0; n < nsamples; n++)
1318 dst[n] += (src[n] + round) >> shift;
1325 int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
1327 AVCodecContext *avctx = s->avctx;
1328 DCAContext *dca = avctx->priv_data;
1329 DCAExssAsset *asset = &dca->exss.assets[0];
1330 DCAXllChSet *p = &s->chset[0], *c;
1331 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
1332 int i, j, k, ret, shift, nsamples, request_mask;
1333 int ch_remap[DCA_SPEAKER_COUNT];
1335 // Force lossy downmixed output during recovery
1336 if (dca->packet & DCA_PACKET_RECOVERY) {
1337 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
1338 if (i < s->nactivechsets)
1339 force_lossy_output(s, c);
1341 if (!c->primary_chset)
1342 c->dmix_embedded = 0;
1345 s->scalable_lsbs = 0;
1346 s->fixed_lsb_width = 0;
1349 // Filter frequency bands for active channel sets
1351 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1352 chs_filter_band_data(s, c, 0);
1354 if (c->residual_encode != (1 << c->nchannels) - 1
1355 && (ret = combine_residual_frame(s, c)) < 0)
1358 if (s->scalable_lsbs)
1359 chs_assemble_msbs_lsbs(s, c, 0);
1361 if (c->nfreqbands > 1) {
1362 chs_filter_band_data(s, c, 1);
1363 chs_assemble_msbs_lsbs(s, c, 1);
1366 s->output_mask |= c->ch_mask;
1369 // Undo hierarchial downmix and/or apply scaling
1370 for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
1371 if (!is_hier_dmix_chset(c))
1374 if (i >= s->nactivechsets) {
1375 for (j = 0; j < c->nfreqbands; j++)
1376 if (c->bands[j].dmix_embedded)
1377 scale_down_mix(s, c, j);
1381 for (j = 0; j < c->nfreqbands; j++)
1382 if (c->bands[j].dmix_embedded)
1383 undo_down_mix(s, c, j);
1386 // Assemble frequency bands for active channel sets
1387 if (s->nfreqbands > 1) {
1388 for (i = 0; i < s->nactivechsets; i++)
1389 if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
1393 // Normalize to regular 5.1 layout if downmixing
1394 if (dca->request_channel_layout) {
1395 if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
1396 s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
1397 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
1399 if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
1400 s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
1401 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
1405 // Handle downmixing to stereo request
1406 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
1407 && DCA_HAS_STEREO(s->output_mask) && p->dmix_embedded
1408 && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1409 p->dmix_type == DCA_DMIX_TYPE_LtRt))
1410 request_mask = DCA_SPEAKER_LAYOUT_STEREO;
1412 request_mask = s->output_mask;
1413 if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
1414 return AVERROR(EINVAL);
1416 avctx->sample_rate = p->freq << (s->nfreqbands - 1);
1418 switch (p->storage_bit_res) {
1420 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1423 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1426 return AVERROR(EINVAL);
1429 avctx->bits_per_raw_sample = p->storage_bit_res;
1430 avctx->profile = FF_PROFILE_DTS_HD_MA;
1431 avctx->bit_rate = 0;
1433 frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
1434 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1437 // Downmix primary channel set to stereo
1438 if (request_mask != s->output_mask) {
1439 ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
1440 p->dmix_coeff, nsamples,
1444 shift = p->storage_bit_res - p->pcm_bit_res;
1445 for (i = 0; i < avctx->channels; i++) {
1446 int32_t *samples = s->output_samples[ch_remap[i]];
1447 if (frame->format == AV_SAMPLE_FMT_S16P) {
1448 int16_t *plane = (int16_t *)frame->extended_data[i];
1449 for (k = 0; k < nsamples; k++)
1450 plane[k] = av_clip_int16(samples[k] * (1 << shift));
1452 int32_t *plane = (int32_t *)frame->extended_data[i];
1453 for (k = 0; k < nsamples; k++)
1454 plane[k] = clip23(samples[k] * (1 << shift)) * (1 << 8);
1458 if (!asset->one_to_one_map_ch_to_spkr) {
1459 if (asset->representation_type == DCA_REPR_TYPE_LtRt)
1460 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1461 else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
1462 matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1463 } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
1464 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1466 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1472 av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
1477 av_cold void ff_dca_xll_close(DCAXllDecoder *s)
1482 for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
1483 for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1484 av_freep(&c->sample_buffer[j]);
1485 c->sample_size[j] = 0;
1492 av_freep(&s->pbr_buffer);