FFmpeg
Loading...
Searching...
No Matches
dca_xll.c
Go to the documentation of this file.
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"
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
39
40static int get_linear(GetBitContext *gb, int n)
41{
42 unsigned int v = get_bits_long(gb, n);
43 return (v >> 1) ^ -(v & 1);
44}
45
46static int get_rice_un(GetBitContext *gb, int k)
47{
48 unsigned int v = get_unary(gb, 1, get_bits_left(gb));
49 return (v << k) | get_bits_long(gb, k);
50}
51
52static int get_rice(GetBitContext *gb, int k)
53{
54 unsigned int v = get_rice_un(gb, k);
55 return (v >> 1) ^ -(v & 1);
56}
57
58static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
59{
60 int i;
61
62 for (i = 0; i < size; i++)
63 array[i] = get_bits(gb, n);
64}
65
66static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
67{
68 int i;
69
70 if (n == 0)
71 memset(array, 0, sizeof(*array) * size);
72 else for (i = 0; i < size; i++)
73 array[i] = get_linear(gb, n);
74}
75
76static int get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
77{
78 int i;
79
80 for (i = 0; i < size && get_bits_left(gb) > k; i++)
81 array[i] = get_rice(gb, k);
82
83 if (i < size)
85 return 0;
86}
87
89{
90 // Size of downmix coefficient matrix
91 int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
92 int i, j, *coeff_ptr = c->dmix_coeff;
93
94 for (i = 0; i < m; i++) {
95 int code, sign, coeff, scale, scale_inv = 0;
96 unsigned int index;
97
98 // Downmix scale (only for non-primary channel sets)
99 if (!c->primary_chset) {
100 code = get_bits(&s->gb, 9);
101 sign = (code >> 8) - 1;
104 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");
105 return AVERROR_INVALIDDATA;
106 }
108 scale_inv = ff_dca_inv_dmixtable[index];
109 c->dmix_scale[i] = (scale ^ sign) - sign;
110 c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;
111 }
112
113 // Downmix coefficients
114 for (j = 0; j < c->nchannels; j++) {
115 code = get_bits(&s->gb, 9);
116 sign = (code >> 8) - 1;
117 index = code & 0xff;
119 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");
120 return AVERROR_INVALIDDATA;
121 }
122 coeff = ff_dca_dmixtable[index];
123 if (!c->primary_chset)
124 // Multiply by |InvDmixScale| to get |UndoDmixScale|
125 coeff = mul16(scale_inv, coeff);
126 *coeff_ptr++ = (coeff ^ sign) - sign;
127 }
128 }
129
130 return 0;
131}
132
134{
135 int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
136 DCAXllChSet *p = &s->chset[0];
137 DCAXllBand *b;
138
139 // Size of channel set sub-header
140 header_size = get_bits(&s->gb, 10) + 1;
141
142 // Check CRC
143 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 c->nchannels = get_bits(&s->gb, 4) + 1;
150 if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
151 avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
153 }
154 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 c->residual_encode = get_bits(&s->gb, c->nchannels);
161
162 // PCM bit resolution
163 c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
164
165 // Storage unit width
166 c->storage_bit_res = get_bits(&s->gb, 5) + 1;
167 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);
170 }
171
172 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 c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
179 if (c->freq > 192000) {
180 avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
182 }
183
184 // Sampling frequency modifier
185 if (get_bits(&s->gb, 2)) {
186 avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
188 }
189
190 // Which replacement set this channel set is member of
191 if (get_bits(&s->gb, 2)) {
192 avpriv_request_sample(s->avctx, "XLL replacement set");
194 }
195
196 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 if (get_bits1(&s->gb)) {
200 avpriv_request_sample(s->avctx, "DTS:X height channel set with mapping coefficients");
202 }
203 c->primary_chset = 1;
204 c->dmix_coeffs_present = 0;
205 c->dmix_embedded = 0;
206 c->hier_chset = 0;
207 c->ch_mask = DCA_XLL_X_HEIGHT_MASK;
208 for (i = 0; i < 4; i++)
209 c->ch_remap[i] = x_height_spkr[i];
210 } else if (asset->one_to_one_map_ch_to_spkr) {
211 // Primary channel set flag
212 c->primary_chset = get_bits1(&s->gb);
213 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 c->dmix_coeffs_present = get_bits1(&s->gb);
220
221 // Downmix already performed by encoder
222 c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
223
224 // Downmix type
225 if (c->dmix_coeffs_present && c->primary_chset) {
226 c->dmix_type = get_bits(&s->gb, 3);
227 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 c->hier_chset = get_bits1(&s->gb);
235 if (!c->hier_chset && s->nchsets != 1) {
236 avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
238 }
239
240 // Downmix coefficients
241 if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
242 return ret;
243
244 // Channel mask enabled
245 if (!get_bits1(&s->gb)) {
246 avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
248 }
249
250 // Channel mask for set
251 c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
252 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 for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
259 if (c->ch_mask & (1U << i))
260 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");
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 if (c->freq > 96000) {
279 // Extra frequency bands flag
280 if (get_bits1(&s->gb)) {
281 avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
283 }
284 c->nfreqbands = 2;
285 } else {
286 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 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 if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
296 || (c->pcm_bit_res != p->pcm_bit_res &&
297 (c != &s->x_chset || c->pcm_bit_res > p->pcm_bit_res))
298 || c->storage_bit_res != p->storage_bit_res)) {
299 avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
301 }
302
303 // Determine number of bits to read bit allocation coding parameter
304 if (c->storage_bit_res > 16)
305 c->nabits = 5;
306 else if (c->storage_bit_res > 8)
307 c->nabits = 4;
308 else
309 c->nabits = 3;
310
311 // Account for embedded downmix and decimator saturation
312 if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
313 c->nabits++;
314
315 for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
316 // Pairwise channel decorrelation
317 if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
318 int ch_nbits = av_ceil_log2(c->nchannels);
319
320 // Original channel order
321 for (i = 0; i < c->nchannels; i++) {
322 b->orig_order[i] = get_bits(&s->gb, ch_nbits);
323 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 for (i = 0; i < c->nchannels / 2; i++)
331 b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
332 } else {
333 for (i = 0; i < c->nchannels; i++)
334 b->orig_order[i] = i;
335 for (i = 0; i < c->nchannels / 2; i++)
336 b->decor_coeff[i] = 0;
337 }
338
339 // Adaptive predictor order
340 b->highest_pred_order = 0;
341 for (i = 0; i < c->nchannels; i++) {
342 b->adapt_pred_order[i] = get_bits(&s->gb, 4);
343 if (b->adapt_pred_order[i] > b->highest_pred_order)
344 b->highest_pred_order = b->adapt_pred_order[i];
345 }
346 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 for (i = 0; i < c->nchannels; i++)
353 b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
354
355 // Adaptive predictor quantized reflection coefficients
356 for (i = 0; i < c->nchannels; i++) {
357 for (j = 0; j < b->adapt_pred_order[i]; j++) {
358 k = get_linear(&s->gb, 8);
359 if (k == -128) {
360 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
361 return AVERROR_INVALIDDATA;
362 }
363 if (k < 0)
364 b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
365 else
366 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 b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
372
373 // MSB/LSB split flag in extension frequency band
374 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
375 // Size of LSB section in any segment
376 b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
377 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 if (b->lsb_section_size && (s->band_crc_present > 2 ||
384 (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 for (i = 0; i < c->nchannels; i++) {
389 b->nscalablelsbs[i] = get_bits(&s->gb, 4);
390 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 b->lsb_section_size = 0;
397 for (i = 0; i < c->nchannels; i++)
398 b->nscalablelsbs[i] = 0;
399 }
400
401 // Scalable resolution flag in extension frequency band
402 if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
403 // Number of bits discarded by authoring
404 for (i = 0; i < c->nchannels; i++)
405 b->bit_width_adjust[i] = get_bits(&s->gb, 4);
406 } else {
407 for (i = 0; i < c->nchannels; i++)
408 b->bit_width_adjust[i] = 0;
409 }
410 }
411
412 // Reserved
413 // Byte align
414 // CRC16 of channel set sub-header
415 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 return 0;
421}
422
424{
425 int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
426 int nchsamples = s->nframesamples + ndecisamples;
427 int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
428 int32_t *ptr;
429
430 // Reallocate MSB sample buffer
431 av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
432 if (!c->sample_buffer[0])
433 return AVERROR(ENOMEM);
434
435 ptr = c->sample_buffer[0] + ndecisamples;
436 for (i = 0; i < c->nfreqbands; i++) {
437 for (j = 0; j < c->nchannels; j++) {
438 c->bands[i].msb_sample_buffer[j] = ptr;
439 ptr += nchsamples;
440 }
441 }
442
443 return 0;
444}
445
447{
448 int i, j, nsamples = 0;
449 int32_t *ptr;
450
451 // Determine number of frequency bands that have MSB/LSB split
452 for (i = 0; i < c->nfreqbands; i++)
453 if (c->bands[i].lsb_section_size)
454 nsamples += s->nframesamples * c->nchannels;
455 if (!nsamples)
456 return 0;
457
458 // Reallocate LSB sample buffer
459 av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
460 if (!c->sample_buffer[1])
461 return AVERROR(ENOMEM);
462
463 ptr = c->sample_buffer[1];
464 for (i = 0; i < c->nfreqbands; i++) {
465 if (c->bands[i].lsb_section_size) {
466 for (j = 0; j < c->nchannels; j++) {
467 c->bands[i].lsb_sample_buffer[j] = ptr;
468 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 return 0;
477}
478
479static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
480{
481 DCAXllBand *b = &c->bands[band];
482 int i, j, k;
483
484 // Start unpacking MSB portion of the segment
485 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 c->seg_common = get_bits1(&s->gb);
490
491 // Determine number of coding parameters encoded in segment
492 k = c->seg_common ? 1 : c->nchannels;
493
494 // Unpack Rice coding parameters
495 for (i = 0; i < k; i++) {
496 // Unpack Rice coding flag
497 // 0 - linear code, 1 - Rice code
498 c->rice_code_flag[i] = get_bits1(&s->gb);
499 // Unpack Hybrid Rice coding flag
500 // 0 - Rice code, 1 - Hybrid Rice code
501 if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))
502 // Unpack binary code length for isolated samples
503 c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
504 else
505 // 0 indicates no Hybrid Rice coding
506 c->bitalloc_hybrid_linear[i] = 0;
507 }
508
509 // Unpack coding parameters
510 for (i = 0; i < k; i++) {
511 if (seg == 0) {
512 // Unpack coding parameter for part A of segment 0
513 c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
514
515 // Adjust for the linear code
516 if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
517 c->bitalloc_part_a[i]++;
518
519 if (!c->seg_common)
520 c->nsamples_part_a[i] = b->adapt_pred_order[i];
521 else
522 c->nsamples_part_a[i] = b->highest_pred_order;
523 } else {
524 c->bitalloc_part_a[i] = 0;
525 c->nsamples_part_a[i] = 0;
526 }
527
528 // Unpack coding parameter for part B of segment
529 c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
530
531 // Adjust for the linear code
532 if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
533 c->bitalloc_part_b[i]++;
534 }
535 }
536
537 // Unpack entropy codes
538 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 k = c->seg_common ? 0 : i;
544
545 // Slice the segment into parts A and B
546 part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
547 part_b = part_a + c->nsamples_part_a[k];
548 nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
549
550 if (get_bits_left(&s->gb) < 0)
551 return AVERROR_INVALIDDATA;
552
553 if (!c->rice_code_flag[k]) {
554 // Linear codes
555 // Unpack all residuals of part A of segment 0
556 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 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 int ret = get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
566 c->bitalloc_part_a[k]);
567 if (ret < 0)
568 return ret;
569
570 if (c->bitalloc_hybrid_linear[k]) {
571 // Hybrid Rice codes
572 // Unpack the number of isolated samples
573 int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
574
575 // Set all locations to 0
576 memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
577
578 // Extract the locations of isolated samples and flag by -1
579 for (j = 0; j < nisosamples; j++) {
580 int loc = get_bits(&s->gb, s->nsegsamples_log2);
581 if (loc >= nsamples_part_b) {
582 av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
583 return AVERROR_INVALIDDATA;
584 }
585 part_b[loc] = -1;
586 }
587
588 // Unpack all residuals of part B of segment 0 and others
589 for (j = 0; j < nsamples_part_b; j++) {
590 if (part_b[j])
591 part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
592 else
593 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 ret = get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
599 if (ret < 0)
600 return ret;
601 }
602 }
603 }
604
605 // Unpack decimator history for frequency band 1
606 if (seg == 0 && band == 1) {
607 int nbits = get_bits(&s->gb, 5) + 1;
608 for (i = 0; i < c->nchannels; i++)
609 for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
610 c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
611 }
612
613 // Start unpacking LSB portion of the segment
614 if (b->lsb_section_size) {
615 // Skip to the start of LSB portion
616 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 for (i = 0; i < c->nchannels; i++) {
623 if (b->nscalablelsbs[i]) {
624 get_array(&s->gb,
625 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 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 return 0;
638}
639
640static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
641{
642 DCAXllBand *b = &c->bands[band];
643 int i, offset, nsamples;
644
645 if (seg < 0) {
646 offset = 0;
647 nsamples = s->nframesamples;
648 } else {
649 offset = seg * s->nsegsamples;
650 nsamples = s->nsegsamples;
651 }
652
653 for (i = 0; i < c->nchannels; i++) {
654 memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
655 if (b->lsb_section_size)
656 memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
657 }
658
659 if (seg <= 0 && band)
660 memset(c->deci_history, 0, sizeof(c->deci_history));
661
662 if (seg < 0) {
663 memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
664 memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
665 }
666}
667
669{
670 DCAXllBand *b = &c->bands[band];
671 int nsamples = s->nframesamples;
672 int i, j, k;
673
674 // Inverse adaptive or fixed prediction
675 for (i = 0; i < c->nchannels; i++) {
676 int32_t *buf = b->msb_sample_buffer[i];
677 int order = b->adapt_pred_order[i];
678 if (order > 0) {
680 // Conversion from reflection coefficients to direct form coefficients
681 for (j = 0; j < order; j++) {
682 int rc = b->adapt_refl_coeff[i][j];
683 for (k = 0; k < (j + 1) / 2; k++) {
684 int tmp1 = coeff[ k ];
685 int tmp2 = coeff[j - k - 1];
686 coeff[ k ] = tmp1 + mul16(rc, tmp2);
687 coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
688 }
689 coeff[j] = rc;
690 }
691 // Inverse adaptive prediction
692 for (j = 0; j < nsamples - order; j++) {
693 int64_t err = 0;
694 for (k = 0; k < order; k++)
695 err += (int64_t)buf[j + k] * coeff[order - k - 1];
696 buf[j + k] -= (SUINT)clip23(norm16(err));
697 }
698 } else {
699 // Inverse fixed coefficient prediction
700 for (j = 0; j < b->fixed_pred_order[i]; j++)
701 for (k = 1; k < nsamples; k++)
702 buf[k] += (unsigned)buf[k - 1];
703 }
704 }
705
706 // Inverse pairwise channel decorrelation
707 if (b->decor_enabled) {
709
710 for (i = 0; i < c->nchannels / 2; i++) {
711 int coeff = b->decor_coeff[i];
712 if (coeff) {
713 s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
714 b->msb_sample_buffer[i * 2 ],
715 coeff, nsamples);
716 }
717 }
718
719 // Reorder channel pointers to the original order
720 for (i = 0; i < c->nchannels; i++)
721 tmp[i] = b->msb_sample_buffer[i];
722
723 for (i = 0; i < c->nchannels; i++)
724 b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
725 }
726
727 // Map output channel pointers for frequency band 0
728 if (c->nfreqbands == 1)
729 for (i = 0; i < c->nchannels; i++)
730 s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
731}
732
733static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
734{
735 int adj = c->bands[band].bit_width_adjust[ch];
736 int shift = c->bands[band].nscalablelsbs[ch];
737
738 if (s->fixed_lsb_width)
739 shift = s->fixed_lsb_width;
740 else if (shift && adj)
741 shift += adj - 1;
742 else
743 shift += adj;
744
745 return shift;
746}
747
749{
750 DCAXllBand *b = &c->bands[band];
751 int n, ch, nsamples = s->nframesamples;
752
753 for (ch = 0; ch < c->nchannels; ch++) {
754 int shift = chs_get_lsb_width(s, c, band, ch);
755 if (shift) {
756 int32_t *msb = b->msb_sample_buffer[ch];
757 if (b->nscalablelsbs[ch]) {
758 int32_t *lsb = b->lsb_sample_buffer[ch];
759 int adj = b->bit_width_adjust[ch];
760 for (n = 0; n < nsamples; n++)
761 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}
769
771{
772 int ch, nsamples = s->nframesamples;
773 int32_t *ptr;
774
775 av_assert1(c->nfreqbands > 1);
776
777 // Reallocate frequency band assembly buffer
778 av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
779 2 * nsamples * c->nchannels * sizeof(int32_t));
780 if (!c->sample_buffer[2])
781 return AVERROR(ENOMEM);
782
783 // Assemble frequency bands 0 and 1
784 ptr = c->sample_buffer[2];
785 for (ch = 0; ch < c->nchannels; ch++) {
786 int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
787 int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
788
789 // Copy decimator history
790 memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
791 c->deci_history[ch], sizeof(c->deci_history[0]));
792
793 // Filter
794 s->dcadsp->assemble_freq_bands(ptr, band0, band1,
796 nsamples);
797
798 // Remap output channel pointer to assembly buffer
799 s->output_samples[c->ch_remap[ch]] = ptr;
800 ptr += nsamples * 2;
801 }
802
803 return 0;
804}
805
807{
808 int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
809
810 // XLL extension sync word
811 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 stream_ver = get_bits(&s->gb, 4) + 1;
818 if (stream_ver > 1) {
819 avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
821 }
822
823 // Lossless frame header length
824 header_size = get_bits(&s->gb, 8) + 1;
825
826 // Check CRC
827 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 frame_size_nbits = get_bits(&s->gb, 5) + 1;
834
835 // Number of bytes in a lossless frame
836 s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
837 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 s->frame_size++;
842
843 // Number of channels sets per frame
844 s->nchsets = get_bits(&s->gb, 4) + 1;
845 if (s->nchsets > DCA_XLL_CHSETS_MAX) {
846 avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
848 }
849
850 // Number of segments per frame
851 nframesegs_log2 = get_bits(&s->gb, 4);
852 s->nframesegs = 1 << nframesegs_log2;
853 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 s->nsegsamples_log2 = get_bits(&s->gb, 4);
862 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 s->nsegsamples = 1 << s->nsegsamples_log2;
867 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 s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
874 s->nframesamples = 1 << s->nframesamples_log2;
875 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 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 s->band_crc_present = get_bits(&s->gb, 2);
889
890 // MSB/LSB split flag
891 s->scalable_lsbs = get_bits1(&s->gb);
892
893 // Channel position mask
894 s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
895
896 // Fixed LSB width
897 if (s->scalable_lsbs)
898 s->fixed_lsb_width = get_bits(&s->gb, 4);
899 else
900 s->fixed_lsb_width = 0;
901
902 // Reserved
903 // Byte align
904 // Header CRC16 protection
905 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 return 0;
911}
912
914{
915 return !c->primary_chset && c->dmix_embedded && c->hier_chset;
916}
917
919{
920 if (c->hier_chset)
921 while (++c < &s->chset[s->nchsets])
923 return c;
924
925 return NULL;
926}
927
929{
930 int i, j, *coeff_ptr = c->dmix_coeff;
931
932 for (i = 0; i < c->hier_ofs; i++) {
933 int scale = o->dmix_scale[i];
934 int scale_inv = o->dmix_scale_inv[i];
935 c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
936 c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
937 for (j = 0; j < c->nchannels; j++) {
938 int coeff = mul16(*coeff_ptr, scale_inv);
939 *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
940 }
941 }
942}
943
945{
946 DCAContext *dca = s->avctx->priv_data;
947 DCAXllChSet *c;
948 int i, ret;
949
950 // Parse channel set headers
951 s->nfreqbands = 0;
952 s->nchannels = 0;
953 s->nreschsets = 0;
954 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
955 c->hier_ofs = s->nchannels;
956 if ((ret = chs_parse_header(s, c, asset)) < 0)
957 return ret;
958 if (c->nfreqbands > s->nfreqbands)
959 s->nfreqbands = c->nfreqbands;
960 if (c->hier_chset)
961 s->nchannels += c->nchannels;
962 if (c->residual_encode != (1 << c->nchannels) - 1)
963 s->nreschsets++;
964 }
965
966 // Pre-scale downmixing coefficients for all non-primary channel sets
967 for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
968 if (is_hier_dmix_chset(c)) {
970 if (o)
972 }
973 }
974
975 // Determine number of active channel sets to decode
976 switch (dca->request_channel_layout) {
978 s->nactivechsets = 1;
979 break;
982 s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
983 break;
984 default:
985 s->nactivechsets = s->nchsets;
986 break;
987 }
988
989 return 0;
990}
991
993{
994 int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
995 DCAXllChSet *c;
996
997 // Determine size of NAVI table
998 navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
999 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 av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
1006 if (!s->navi)
1007 return AVERROR(ENOMEM);
1008
1009 // Parse NAVI
1010 navi_pos = get_bits_count(&s->gb);
1011 navi_ptr = s->navi;
1012 for (band = 0; band < s->nfreqbands; band++) {
1013 for (seg = 0; seg < s->nframesegs; seg++) {
1014 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1015 int size = 0;
1016 if (c->nfreqbands > band) {
1017 size = get_bits_long(&s->gb, s->seg_size_nbits);
1018 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 size++;
1023 }
1024 *navi_ptr++ = size;
1025 }
1026 }
1027 }
1028
1029 // Byte align
1030 // CRC16
1031 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1032 skip_bits(&s->gb, 16);
1033
1034 // Check CRC
1035 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 return 0;
1041}
1042
1044{
1045 int ret, chs, seg, band, navi_pos, *navi_ptr;
1046 DCAXllChSet *c;
1047
1048 for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
1049 if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1050 return ret;
1051 if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1052 return ret;
1053 }
1054
1055 navi_pos = get_bits_count(&s->gb);
1056 navi_ptr = s->navi;
1057 for (band = 0; band < s->nfreqbands; band++) {
1058 for (seg = 0; seg < s->nframesegs; seg++) {
1059 for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1060 if (c->nfreqbands > band) {
1061 navi_pos += *navi_ptr * 8;
1062 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 if (chs < s->nactivechsets &&
1067 (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 skip_bits_long(&s->gb, navi_pos - get_bits_count(&s->gb));
1073 }
1074 navi_ptr++;
1075 }
1076 }
1077 }
1078
1079 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.
1088{
1089 DCAXllChSet *c = &s->x_chset;
1090 int ret, seg, navi_pos, band_pos, navi[1024];
1091
1092 if (s->nfreqbands != 1)
1093 return AVERROR_PATCHWELCOME;
1094
1096 if (get_bits_left(&s->gb) < 0)
1097 return AVERROR_INVALIDDATA;
1098
1099 c->hier_ofs = 0;
1100 if ((ret = chs_parse_header(s, c, asset)) < 0)
1101 return ret;
1102 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 navi_pos = get_bits_count(&s->gb);
1108 for (seg = 0; seg < s->nframesegs; seg++) {
1109 navi[seg] = get_bits_long(&s->gb, s->seg_size_nbits);
1110 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 navi[seg]++;
1115 }
1116 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1117 skip_bits(&s->gb, 16);
1118 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 if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1124 return ret;
1125 if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1126 return ret;
1127
1128 band_pos = get_bits_count(&s->gb);
1129 for (seg = 0; seg < s->nframesegs; seg++) {
1130 band_pos += navi[seg] * 8;
1131 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 if ((ret = chs_parse_band_data(s, c, 0, seg, band_pos)) < 0)
1136 return ret;
1137 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 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 return 0;
1148}
1149
1150static int parse_frame(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1151{
1152 DCAContext *dca = s->avctx->priv_data;
1153 int ret;
1154
1155 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1156 return ret;
1157 if ((ret = parse_common_header(s)) < 0)
1158 return ret;
1159 if ((ret = parse_sub_headers(s, asset)) < 0)
1160 return ret;
1161 if ((ret = parse_navi_table(s)) < 0)
1162 return ret;
1163 if ((ret = parse_band_data(s)) < 0)
1164 return ret;
1165
1166 s->x_heights_decoded = 0;
1167 if (s->frame_size * 8 > FFALIGN(get_bits_count(&s->gb), 32)) {
1168 unsigned int extradata_syncword;
1169
1170 // Align to dword
1171 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1172
1173 extradata_syncword = show_bits_long(&s->gb, 32);
1174
1175 if (extradata_syncword == DCA_SYNCWORD_XLL_X) {
1176 s->x_syncword_present = 1;
1177 if (!dca->request_channel_layout && !dca->x_bed_only) {
1178 int x_pos = get_bits_count(&s->gb);
1179 int err = parse_x_heights(s, asset);
1180 // Whatever the heights did, the frame ends where it did before
1181 skip_bits_long(&s->gb, x_pos - get_bits_count(&s->gb));
1182 s->x_heights = 1;
1183 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 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 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 return ret;
1201}
1202
1204{
1205 s->pbr_length = 0;
1206 s->pbr_delay = 0;
1207}
1208
1209static int copy_to_pbr(DCAXllDecoder *s, const uint8_t *data, int size, int delay)
1210{
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
1224static int parse_frame_no_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
1225{
1226 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 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 if (ret < 0)
1249 return ret;
1250
1251 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 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 return 0;
1260}
1261
1262static 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
1297fail:
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
1304int ff_dca_xll_parse(DCAXllDecoder *s, const uint8_t *data, DCAExssAsset *asset)
1305{
1306 int ret;
1307
1308 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 if (s->pbr_length)
1315 ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1316 else
1317 ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1318
1319 return ret;
1320}
1321
1322static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1323{
1324 int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
1325 DCAXllChSet *c;
1326
1327 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1328 if (!c->hier_chset)
1329 continue;
1330
1331 av_assert1(band < c->nfreqbands);
1332 for (j = 0; j < c->nchannels; j++) {
1333 for (k = 0; k < o->nchannels; k++) {
1334 int coeff = *coeff_ptr++;
1335 if (coeff) {
1336 s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
1337 o->bands[band].msb_sample_buffer[k],
1338 coeff, s->nframesamples);
1339 if (band)
1340 s->dcadsp->dmix_sub(c->deci_history[j],
1341 o->deci_history[k],
1343 }
1344 }
1345 }
1346
1347 nchannels += c->nchannels;
1348 if (nchannels >= o->hier_ofs)
1349 break;
1350 }
1351}
1352
1353static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1354{
1355 int i, j, nchannels = 0;
1356 DCAXllChSet *c;
1357
1358 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1359 if (!c->hier_chset)
1360 continue;
1361
1362 av_assert1(band < c->nfreqbands);
1363 for (j = 0; j < c->nchannels; j++) {
1364 int scale = o->dmix_scale[nchannels++];
1365 if (scale != (1 << 15)) {
1366 s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
1367 scale, s->nframesamples);
1368 if (band)
1369 s->dcadsp->dmix_scale(c->deci_history[j],
1371 }
1372 }
1373
1374 if (nchannels >= o->hier_ofs)
1375 break;
1376 }
1377}
1378
1379// Clear all band data and replace non-residual encoded channels with lossy
1380// counterparts
1382{
1383 DCAContext *dca = s->avctx->priv_data;
1384 int band, ch;
1385
1386 for (band = 0; band < c->nfreqbands; band++)
1387 chs_clear_band_data(s, c, band, -1);
1388
1389 for (ch = 0; ch < c->nchannels; ch++) {
1390 if (!(c->residual_encode & (1 << ch)))
1391 continue;
1392 if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
1393 continue;
1394 c->residual_encode &= ~(1 << ch);
1395 }
1396}
1397
1399{
1400 DCAContext *dca = s->avctx->priv_data;
1401 int ch, nsamples = s->nframesamples;
1402 DCAXllChSet *o;
1403
1404 // Verify that core is compatible
1405 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 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 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).
1424
1425 // Reduce core bit width and combine with residual
1426 for (ch = 0; ch < c->nchannels; ch++) {
1427 int n, spkr, shift, round;
1428 int32_t *src, *dst;
1429
1430 if (c->residual_encode & (1 << ch))
1431 continue;
1432
1433 // Map this channel to core speaker
1434 spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
1435 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 shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
1442 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 round = shift > 0 ? 1 << (shift - 1) : 0;
1448
1449 src = dca->core.output_samples[spkr];
1450 dst = c->bands[0].msb_sample_buffer[ch];
1451 if (o) {
1452 // Undo embedded core downmix pre-scaling
1453 int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
1454 for (n = 0; n < nsamples; n++)
1455 dst[n] += (SUINT)clip23((mul16(src[n], scale_inv) + round) >> shift);
1456 } else {
1457 // No downmix scaling
1458 for (n = 0; n < nsamples; n++)
1459 dst[n] += (unsigned)((src[n] + round) >> shift);
1460 }
1461 }
1462
1463 return 0;
1464}
1465
1467{
1468 AVCodecContext *avctx = s->avctx;
1469 DCAContext *dca = avctx->priv_data;
1470 DCAExssAsset *asset = &dca->exss.assets[0];
1471 DCAXllChSet *p = &s->chset[0], *c;
1472 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
1473 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] = {
1477 };
1478
1479 // Force lossy downmixed output during recovery
1480 if (dca->packet & DCA_PACKET_RECOVERY) {
1481 for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
1482 if (i < s->nactivechsets)
1484
1485 if (!c->primary_chset)
1486 c->dmix_embedded = 0;
1487 }
1488
1489 s->scalable_lsbs = 0;
1490 s->fixed_lsb_width = 0;
1491 }
1492
1493 // Filter frequency bands for active channel sets
1494 s->output_mask = 0;
1495 for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1497
1498 if (c->residual_encode != (1 << c->nchannels) - 1
1499 && (ret = combine_residual_frame(s, c)) < 0)
1500 return ret;
1501
1502 if (s->scalable_lsbs)
1504
1505 if (c->nfreqbands > 1) {
1508 }
1509
1510 s->output_mask |= c->ch_mask;
1511 }
1512
1513 // Undo hierarchial downmix and/or apply scaling
1514 for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
1515 if (!is_hier_dmix_chset(c))
1516 continue;
1517
1518 if (i >= s->nactivechsets) {
1519 for (j = 0; j < c->nfreqbands; j++)
1520 if (c->bands[j].dmix_embedded)
1521 scale_down_mix(s, c, j);
1522 break;
1523 }
1524
1525 for (j = 0; j < c->nfreqbands; j++)
1526 if (c->bands[j].dmix_embedded)
1527 undo_down_mix(s, c, j);
1528 }
1529
1530 // Assemble frequency bands for active channel sets
1531 if (s->nfreqbands > 1) {
1532 for (i = 0; i < s->nactivechsets; i++)
1533 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 if (s->x_heights && !dca->request_channel_layout && s->nfreqbands == 1 &&
1539 (s->output_mask & ~(DCA_SPEAKER_MASK_Ls | DCA_SPEAKER_MASK_Rs |
1543 av_popcount(s->output_mask) == 8) {
1544 DCAXllChSet *x = &s->x_chset;
1545
1546 if (s->x_heights_decoded && !(dca->packet & DCA_PACKET_RECOVERY)) {
1547 int adj = p->pcm_bit_res - x->pcm_bit_res;
1548
1549 chs_filter_band_data(s, x, 0);
1550 if (s->scalable_lsbs)
1552
1553 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 x_fold = 1;
1561 } else {
1562 av_fast_malloc(&s->x_silence, &s->x_silence_size,
1563 s->nframesamples * sizeof(*s->x_silence));
1564 if (!s->x_silence)
1565 return AVERROR(ENOMEM);
1566 memset(s->x_silence, 0, s->nframesamples * sizeof(*s->x_silence));
1567 for (i = 0; i < 4; i++)
1568 s->output_samples[x_height_spkr[i]] = s->x_silence;
1569 }
1570
1571 s->output_mask |= DCA_XLL_X_HEIGHT_MASK;
1572 }
1573
1574 // Normalize to regular 5.1 layout if downmixing
1575 if (dca->request_channel_layout) {
1576 if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
1577 s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
1578 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
1579 }
1580 if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
1581 s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
1582 s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
1583 }
1584 }
1585
1586 // Handle downmixing to stereo request
1588 && DCA_HAS_STEREO(s->output_mask) && p->dmix_embedded
1589 && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1590 p->dmix_type == DCA_DMIX_TYPE_LtRt))
1591 request_mask = DCA_SPEAKER_LAYOUT_STEREO;
1592 else
1593 request_mask = s->output_mask;
1594 if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
1595 return AVERROR(EINVAL);
1596
1597 avctx->sample_rate = p->freq << (s->nfreqbands - 1);
1598
1599 switch (p->storage_bit_res) {
1600 case 16:
1602 shift = 16 - p->pcm_bit_res;
1603 break;
1604 case 20:
1605 case 24:
1607 shift = 24 - p->pcm_bit_res;
1608 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 if (x_fold) {
1617 for (i = 0; i < 4; i++) {
1618 int32_t *height = s->output_samples[x_height_spkr[i]];
1619 int32_t *bed = s->output_samples[fold_spkr[i]];
1620 for (k = 0; k < s->nframesamples; k++) {
1621 height[k] *= (SUINT)(1 << shift);
1622 bed[k] = bed[k] * (SUINT)(1 << shift) -
1624 }
1625 }
1626 }
1627
1628 if (s->x_imax_syncword_present) {
1630 } else if (s->x_syncword_present) {
1632 } else {
1634 }
1635
1636 avctx->bits_per_raw_sample = p->storage_bit_res;
1637 avctx->bit_rate = 0;
1638
1639 frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
1640 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1641 return ret;
1642
1643 // Downmix primary channel set to stereo
1644 if (request_mask != s->output_mask) {
1645 ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
1646 p->dmix_coeff, nsamples,
1647 s->output_mask);
1648 }
1649
1650 for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
1651 int32_t *samples = s->output_samples[ch_remap[i]];
1652 int ch_shift = shift;
1654 DCA_SPEAKER_MASK_Lsr | DCA_SPEAKER_MASK_Rsr) & (1U << ch_remap[i])))
1655 ch_shift = 0;
1656 if (frame->format == AV_SAMPLE_FMT_S16P) {
1657 int16_t *plane = (int16_t *)frame->extended_data[i];
1658 for (k = 0; k < nsamples; k++)
1659 plane[k] = av_clip_int16(samples[k] * (SUINT)(1 << ch_shift));
1660 } else {
1661 int32_t *plane = (int32_t *)frame->extended_data[i];
1662 for (k = 0; k < nsamples; k++)
1663 plane[k] = clip23(samples[k] * (SUINT)(1 << ch_shift)) * (1 << 8);
1664 }
1665 }
1666
1667 if (!asset->one_to_one_map_ch_to_spkr) {
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 } 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 if (p->ch_mask == s->output_mask && !dca->request_channel_layout &&
1678 p->dmix_embedded && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1679 p->dmix_type == DCA_DMIX_TYPE_LtRt)) {
1680 ret = ff_dca_export_downmix_matrix(avctx, frame, p->dmix_type, s->output_mask,
1681 p->dmix_coeff);
1682 if (ret < 0)
1683 return ret;
1684 }
1685
1686 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1687 return ret;
1688
1689 return 0;
1690}
1691
1696
1698{
1699 DCAXllChSet *c;
1700 int i, j;
1701
1702 for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
1703 for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1704 av_freep(&c->sample_buffer[j]);
1705 c->sample_size[j] = 0;
1706 }
1707 }
1708
1709 for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1710 av_freep(&s->x_chset.sample_buffer[j]);
1711 s->x_chset.sample_size[j] = 0;
1712 }
1713 av_freep(&s->x_silence);
1714 s->x_silence_size = 0;
1715
1716 av_freep(&s->navi);
1717 s->navi_size = 0;
1718
1719 av_freep(&s->pbr_buffer);
1720 clear_pbr(s);
1721}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
int32_t
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define av_popcount
Definition common.h:154
#define av_clip_int16
Definition common.h:115
#define av_ceil_log2
Definition common.h:97
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
const uint32_t ff_dca_sampling_freqs[16]
Definition dca.c:36
@ DCA_SPEAKER_Lh
Definition dca.h:81
@ DCA_SPEAKER_Rh
Definition dca.h:81
@ DCA_SPEAKER_L
Definition dca.h:78
@ DCA_SPEAKER_Rsr
Definition dca.h:80
@ DCA_SPEAKER_Rs
Definition dca.h:79
@ DCA_SPEAKER_COUNT
Definition dca.h:87
@ DCA_SPEAKER_Rhr
Definition dca.h:84
@ DCA_SPEAKER_Lhr
Definition dca.h:83
@ DCA_SPEAKER_Lss
Definition dca.h:80
@ DCA_SPEAKER_Rss
Definition dca.h:80
@ DCA_SPEAKER_Ls
Definition dca.h:78
@ DCA_SPEAKER_R
Definition dca.h:78
@ DCA_SPEAKER_Lsr
Definition dca.h:79
#define DCA_SPEAKER_LAYOUT_5POINT0
Definition dca.h:128
@ DCA_DMIX_TYPE_COUNT
Definition dca.h:194
@ DCA_DMIX_TYPE_LoRo
Definition dca.h:187
@ DCA_DMIX_TYPE_LtRt
Definition dca.h:188
#define DCA_SPEAKER_LAYOUT_STEREO
Definition dca.h:122
#define DCA_HAS_STEREO(mask)
Definition dca.h:133
@ DCA_REPR_TYPE_LtRt
Definition dca.h:164
@ DCA_REPR_TYPE_LhRh
Definition dca.h:165
#define DCA_SPEAKER_LAYOUT_3_0
Definition dca.h:124
#define DCA_SPEAKER_LAYOUT_5POINT1
Definition dca.h:129
@ DCA_SPEAKER_MASK_Lsr
Definition dca.h:98
@ DCA_SPEAKER_MASK_LFE1
Definition dca.h:96
@ DCA_SPEAKER_MASK_Ls
Definition dca.h:94
@ DCA_SPEAKER_MASK_L
Definition dca.h:92
@ DCA_SPEAKER_MASK_Rs
Definition dca.h:95
@ DCA_SPEAKER_MASK_R
Definition dca.h:93
@ DCA_SPEAKER_MASK_Rss
Definition dca.h:101
@ DCA_SPEAKER_MASK_Lss
Definition dca.h:100
@ DCA_SPEAKER_MASK_Rsr
Definition dca.h:99
static int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
Definition dca_core.h:215
#define DCA_SYNCWORD_XLL_X_IMAX
#define DCA_SYNCWORD_XLL
#define DCA_SYNCWORD_XLL_X
static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:1398
static int get_rice(GetBitContext *gb, int k)
Definition dca_xll.c:52
static int parse_navi_table(DCAXllDecoder *s)
Definition dca_xll.c:992
#define DCA_XLL_X_HEIGHT_MASK
Definition dca_xll.c:33
int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
Definition dca_xll.c:1466
static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
Definition dca_xll.c:944
static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
Definition dca_xll.c:928
static int get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
Definition dca_xll.c:76
static const uint8_t x_height_spkr[4]
Definition dca_xll.c:36
int ff_dca_xll_parse(DCAXllDecoder *s, const uint8_t *data, DCAExssAsset *asset)
Definition dca_xll.c:1304
static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
Definition dca_xll.c:668
static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:770
#define DCA_XLL_X_HEADER_SIZE
Definition dca_xll.c:31
static DCAXllChSet * find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:918
static int parse_frame_no_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
Definition dca_xll.c:1224
static int parse_frame_pbr(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
Definition dca_xll.c:1262
static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:88
static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
Definition dca_xll.c:133
static void clear_pbr(DCAXllDecoder *s)
Definition dca_xll.c:1203
static int parse_x_heights(DCAXllDecoder *s, DCAExssAsset *asset)
Definition dca_xll.c:1087
static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:423
static int get_linear(GetBitContext *gb, int n)
Definition dca_xll.c:40
static int get_rice_un(GetBitContext *gb, int k)
Definition dca_xll.c:46
av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
Definition dca_xll.c:1692
static int copy_to_pbr(DCAXllDecoder *s, const uint8_t *data, int size, int delay)
Definition dca_xll.c:1209
static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
Definition dca_xll.c:66
static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
Definition dca_xll.c:640
static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
Definition dca_xll.c:733
static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
Definition dca_xll.c:1353
static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
Definition dca_xll.c:58
#define DCA_XLL_X_HEIGHT_FOLD
Definition dca_xll.c:32
static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
Definition dca_xll.c:1322
static int parse_common_header(DCAXllDecoder *s)
Definition dca_xll.c:806
static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:1381
static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
Definition dca_xll.c:748
static int parse_frame(DCAXllDecoder *s, const uint8_t *data, int size, DCAExssAsset *asset)
Definition dca_xll.c:1150
static int is_hier_dmix_chset(DCAXllChSet *c)
Definition dca_xll.c:913
static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
Definition dca_xll.c:446
static int parse_band_data(DCAXllDecoder *s)
Definition dca_xll.c:1043
av_cold void ff_dca_xll_close(DCAXllDecoder *s)
Definition dca_xll.c:1697
static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
Definition dca_xll.c:479
#define DCA_XLL_CHANNELS_MAX
Definition dca_xll.h:33
#define DCA_XLL_DECI_HISTORY_MAX
Definition dca_xll.h:36
#define DCA_XLL_CHSETS_MAX
Definition dca_xll.h:32
#define DCA_XLL_SAMPLE_BUFFERS_MAX
Definition dca_xll.h:40
#define DCA_XLL_PBR_BUFFER_MAX
Definition dca_xll.h:39
#define DCA_XLL_ADAPT_PRED_ORDER_MAX
Definition dca_xll.h:35
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition dcadata.c:8642
const uint16_t ff_dca_xll_refl_coeff[128]
Definition dcadata.c:8705
const int32_t ff_dca_xll_band_coeff[20]
Definition dcadata.c:8724
const uint8_t ff_dca_dmix_primary_nch[8]
Definition dcadata.c:45
const uint32_t ff_dca_inv_dmixtable[FF_DCA_INV_DMIXTABLE_SIZE]
Definition dcadata.c:8676
#define FF_DCA_INV_DMIXTABLE_SIZE
Definition dcadata.h:70
#define FF_DCA_DMIXTABLE_SIZE
Definition dcadata.h:69
#define FF_DCA_DMIXTABLE_OFFSET
Definition dcadata.h:71
int ff_dca_export_downmix_matrix(AVCodecContext *avctx, AVFrame *frame, enum DCADownMixType downmix_type, int output_mask, const int *coeff_l)
Definition dcadec.c:162
int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
Definition dcadec.c:48
void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, int *coeff_l, int nsamples, int ch_mask)
Definition dcadec.c:97
#define DCA_PACKET_CORE
Definition dcadec.h:39
#define DCA_PACKET_RECOVERY
Sync error recovery flag.
Definition dcadec.h:45
static int ff_dca_check_crc(AVCodecContext *avctx, GetBitContext *s, int p1, int p2)
Definition dcadec.h:88
static int ff_dca_seek_bits(GetBitContext *s, int p)
Definition dcadec.h:102
static int32_t mul16(int32_t a, int32_t b)
Definition dcamath.h:47
static int32_t norm16(int64_t a)
Definition dcamath.h:41
static int32_t clip23(int32_t a)
Definition dcamath.h:54
static int32_t mul15(int32_t a, int32_t b)
Definition dcamath.h:46
#define SUINT
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1814
int ff_side_data_update_matrix_encoding(AVFrame *frame, enum AVMatrixEncoding matrix_encoding)
Add or update AV_FRAME_DATA_MATRIXENCODING side data.
Definition utils.c:121
#define AV_PROFILE_DTS_HD_MA
Definition defs.h:100
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
#define AV_PROFILE_DTS_HD_MA_X
Definition defs.h:102
#define AV_PROFILE_DTS_HD_MA_X_IMAX
Definition defs.h:103
static AVFrame * frame
int ret
Definition ffplay.c:391
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition get_bits.h:474
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition get_bits.h:498
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define fail
Definition test.h:479
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
AVMatrixEncoding
@ AV_MATRIX_ENCODING_NONE
@ AV_MATRIX_ENCODING_DOLBY
@ AV_MATRIX_ENCODING_DOLBYHEADPHONE
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:661
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
int index
Definition gxfenc.c:90
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
#define b
Definition input.c:43
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
const uint8_t * code
Definition spdifenc.c:433
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1059
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1051
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int profile
profile
Definition avcodec.h:1646
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1581
int sample_rate
samples per second
Definition avcodec.h:1044
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
DCACoreDecoder core
Core decoder context.
Definition dcadec.h:57
DCAExssParser exss
EXSS parser context.
Definition dcadec.h:58
int packet
Packet flags.
Definition dcadec.h:69
int request_channel_layout
Converted from avctx.request_channel_layout.
Definition dcadec.h:71
int x_bed_only
Keep a DTS:X 7.1 bed as coded, without its heights.
Definition dcadec.h:75
int npcmsamples
Number of PCM samples per channel.
Definition dca_core.h:209
int output_rate
Output sample rate (1x or 2x header rate)
Definition dca_core.h:210
int32_t * output_samples[DCA_SPEAKER_COUNT]
PCM output for fixed point mode.
Definition dca_core.h:202
int xll_offset
Offset to XLL data from start of substream.
Definition dca_exss.h:62
int hd_stream_id
DTS-HD stream ID.
Definition dca_exss.h:68
int xll_sync_present
XLL sync word present flag.
Definition dca_exss.h:64
int xll_size
Size of XLL data in extension substream.
Definition dca_exss.h:63
int xll_delay_nframes
Initial XLL decoding delay in frames.
Definition dca_exss.h:65
int representation_type
Representation type.
Definition dca_exss.h:42
int xll_sync_offset
Number of bytes offset to XLL sync.
Definition dca_exss.h:66
int one_to_one_map_ch_to_spkr
One to one channel to speaker mapping flag.
Definition dca_exss.h:37
DCAExssAsset assets[1]
Audio asset descriptors.
Definition dca_exss.h:87
int32_t * msb_sample_buffer[DCA_XLL_CHANNELS_MAX]
MSB sample buffer pointers.
Definition dca_xll.h:58
int32_t deci_history[DCA_XLL_CHANNELS_MAX][DCA_XLL_DECI_HISTORY_MAX]
Decimator history for frequency band 1.
Definition dca_xll.h:96
int nchannels
Number of channels in the channel set (N)
Definition dca_xll.h:64
int pcm_bit_res
PCM bit resolution (variable)
Definition dca_xll.h:66
DCAXllBand bands[DCA_XLL_BANDS_MAX]
Frequency bands.
Definition dca_xll.h:85
int dmix_scale[DCA_XLL_DMIX_SCALES_MAX]
Downmixing scales.
Definition dca_xll.h:77
int dmix_coeff[DCA_XLL_DMIX_COEFFS_MAX]
Downmixing coefficients.
Definition dca_xll.h:76
int dmix_scale_inv[DCA_XLL_DMIX_SCALES_MAX]
Inverse downmixing scales.
Definition dca_xll.h:78
int hier_ofs
Number of preceding channels in a hierarchy (M)
Definition dca_xll.h:75
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static int array[MAX_W *MAX_W]
#define height
Definition dsp.h:99
int size
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition unary.h:46
static double c[64]