FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "dcadec.h"
22 #include "dcadata.h"
23 #include "dcamath.h"
24 #include "dca_syncwords.h"
25 #include "unary.h"
26 
27 static int get_linear(GetBitContext *gb, int n)
28 {
29  unsigned int v = get_bits_long(gb, n);
30  return (v >> 1) ^ -(v & 1);
31 }
32 
33 static int get_rice_un(GetBitContext *gb, int k)
34 {
35  unsigned int v = get_unary(gb, 1, 128);
36  return (v << k) | get_bits_long(gb, k);
37 }
38 
39 static int get_rice(GetBitContext *gb, int k)
40 {
41  unsigned int v = get_rice_un(gb, k);
42  return (v >> 1) ^ -(v & 1);
43 }
44 
45 static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
46 {
47  int i;
48 
49  for (i = 0; i < size; i++)
50  array[i] = get_bits(gb, n);
51 }
52 
53 static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
54 {
55  int i;
56 
57  if (n == 0)
58  memset(array, 0, sizeof(*array) * size);
59  else for (i = 0; i < size; i++)
60  array[i] = get_linear(gb, n);
61 }
62 
63 static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
64 {
65  int i;
66 
67  for (i = 0; i < size; i++)
68  array[i] = get_rice(gb, k);
69 }
70 
72 {
73  // Size of downmix coefficient matrix
75  int i, j, *coeff_ptr = c->dmix_coeff;
76 
77  for (i = 0; i < m; i++) {
78  int code, sign, coeff, scale, scale_inv = 0;
79  unsigned int index;
80 
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;
89  }
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;
94  }
95 
96  // Downmix coefficients
97  for (j = 0; j < c->nchannels; j++) {
98  code = get_bits(&s->gb, 9);
99  sign = (code >> 8) - 1;
100  index = code & 0xff;
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;
104  }
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;
110  }
111  }
112 
113  return 0;
114 }
115 
117 {
118  int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
119  DCAXllChSet *p = &s->chset[0];
120  DCAXllBand *b;
121 
122  // Size of channel set sub-header
123  header_size = get_bits(&s->gb, 10) + 1;
124 
125  // Check CRC
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;
130  }
131 
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;
137  }
138 
139  // Residual type
140  c->residual_encode = get_bits(&s->gb, c->nchannels);
141 
142  // PCM bit resolution
143  c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
144 
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;
150  }
151 
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;
155  }
156 
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;
162  }
163 
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;
168  }
169 
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;
174  }
175 
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;
182  }
183 
184  // Downmix coefficients present in stream
185  c->dmix_coeffs_present = get_bits1(&s->gb);
186 
187  // Downmix already performed by encoder
189 
190  // Downmix type
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;
196  }
197  }
198 
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;
204  }
205 
206  // Downmix coefficients
207  if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
208  return ret;
209 
210  // Channel mask enabled
211  if (!get_bits1(&s->gb)) {
212  avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
213  return AVERROR_PATCHWELCOME;
214  }
215 
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;
221  }
222 
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;
227  } else {
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;
232  }
233 
234  // Setup for LtRt decoding
235  c->primary_chset = 1;
236  c->dmix_coeffs_present = 0;
237  c->dmix_embedded = 0;
238  c->hier_chset = 0;
240  c->ch_remap[0] = DCA_SPEAKER_L;
241  c->ch_remap[1] = DCA_SPEAKER_R;
242  }
243 
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;
249  }
250  c->nfreqbands = 2;
251  } else {
252  c->nfreqbands = 1;
253  }
254 
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;
258 
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;
265  }
266 
267  // Determine number of bits to read bit allocation coding parameter
268  if (c->storage_bit_res > 16)
269  c->nabits = 5;
270  else if (c->storage_bit_res > 8)
271  c->nabits = 4;
272  else
273  c->nabits = 3;
274 
275  // Account for embedded downmix and decimator saturation
276  if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
277  c->nabits++;
278 
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);
283 
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;
290  }
291  }
292 
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;
296  } else {
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;
301  }
302 
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)
309  }
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;
313  }
314 
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);
318 
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);
323  if (k == -128) {
324  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
325  return AVERROR_INVALIDDATA;
326  }
327  if (k < 0)
328  b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
329  else
330  b->adapt_refl_coeff[i][j] = (int)ff_dca_xll_refl_coeff[ k];
331  }
332  }
333 
334  // Downmix performed by encoder in extension frequency band
335  b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
336 
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
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;
344  }
345 
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;
350 
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;
357  }
358  }
359  } else {
360  b->lsb_section_size = 0;
361  for (i = 0; i < c->nchannels; i++)
362  b->nscalablelsbs[i] = 0;
363  }
364 
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);
370  } else {
371  for (i = 0; i < c->nchannels; i++)
372  b->bit_width_adjust[i] = 0;
373  }
374  }
375 
376  // Reserved
377  // Byte align
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;
382  }
383 
384  return 0;
385 }
386 
388 {
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;
392  int32_t *ptr;
393 
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);
398 
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;
403  ptr += nchsamples;
404  }
405  }
406 
407  return 0;
408 }
409 
411 {
412  int i, j, nsamples = 0;
413  int32_t *ptr;
414 
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;
419  if (!nsamples)
420  return 0;
421 
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);
426 
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;
433  }
434  } else {
435  for (j = 0; j < c->nchannels; j++)
436  c->bands[i].lsb_sample_buffer[j] = NULL;
437  }
438  }
439 
440  return 0;
441 }
442 
443 static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
444 {
445  DCAXllBand *b = &c->bands[band];
446  int i, j, k;
447 
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);
454 
455  // Determine number of coding parameters encoded in segment
456  k = c->seg_common ? 1 : c->nchannels;
457 
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  if (!c->seg_common && c->rice_code_flag[i]) {
464  // Unpack Hybrid Rice coding flag
465  // 0 - Rice code, 1 - Hybrid Rice code
466  if (get_bits1(&s->gb))
467  // Unpack binary code length for isolated samples
468  c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
469  else
470  // 0 indicates no Hybrid Rice coding
471  c->bitalloc_hybrid_linear[i] = 0;
472  } else {
473  // 0 indicates no Hybrid Rice coding
474  c->bitalloc_hybrid_linear[i] = 0;
475  }
476  }
477 
478  // Unpack coding parameters
479  for (i = 0; i < k; i++) {
480  if (seg == 0) {
481  // Unpack coding parameter for part A of segment 0
482  c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
483 
484  // Adjust for the linear code
485  if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
486  c->bitalloc_part_a[i]++;
487 
488  if (!c->seg_common)
489  c->nsamples_part_a[i] = b->adapt_pred_order[i];
490  else
492  } else {
493  c->bitalloc_part_a[i] = 0;
494  c->nsamples_part_a[i] = 0;
495  }
496 
497  // Unpack coding parameter for part B of segment
498  c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
499 
500  // Adjust for the linear code
501  if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
502  c->bitalloc_part_b[i]++;
503  }
504  }
505 
506  // Unpack entropy codes
507  for (i = 0; i < c->nchannels; i++) {
508  int32_t *part_a, *part_b;
509  int nsamples_part_b;
510 
511  // Select index of coding parameters
512  k = c->seg_common ? 0 : i;
513 
514  // Slice the segment into parts A and B
515  part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
516  part_b = part_a + c->nsamples_part_a[k];
517  nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
518 
519  if (get_bits_left(&s->gb) < 0)
520  return AVERROR_INVALIDDATA;
521 
522  if (!c->rice_code_flag[k]) {
523  // Linear codes
524  // Unpack all residuals of part A of segment 0
525  get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
526  c->bitalloc_part_a[k]);
527 
528  // Unpack all residuals of part B of segment 0 and others
529  get_linear_array(&s->gb, part_b, nsamples_part_b,
530  c->bitalloc_part_b[k]);
531  } else {
532  // Rice codes
533  // Unpack all residuals of part A of segment 0
534  get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
535  c->bitalloc_part_a[k]);
536 
537  if (c->bitalloc_hybrid_linear[k]) {
538  // Hybrid Rice codes
539  // Unpack the number of isolated samples
540  int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
541 
542  // Set all locations to 0
543  memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
544 
545  // Extract the locations of isolated samples and flag by -1
546  for (j = 0; j < nisosamples; j++) {
547  int loc = get_bits(&s->gb, s->nsegsamples_log2);
548  if (loc >= nsamples_part_b) {
549  av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
550  return AVERROR_INVALIDDATA;
551  }
552  part_b[loc] = -1;
553  }
554 
555  // Unpack all residuals of part B of segment 0 and others
556  for (j = 0; j < nsamples_part_b; j++) {
557  if (part_b[j])
558  part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
559  else
560  part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
561  }
562  } else {
563  // Rice codes
564  // Unpack all residuals of part B of segment 0 and others
565  get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
566  }
567  }
568  }
569 
570  // Unpack decimator history for frequency band 1
571  if (seg == 0 && band == 1) {
572  int nbits = get_bits(&s->gb, 5) + 1;
573  for (i = 0; i < c->nchannels; i++)
574  for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
575  c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
576  }
577 
578  // Start unpacking LSB portion of the segment
579  if (b->lsb_section_size) {
580  // Skip to the start of LSB portion
581  if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
582  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
583  return AVERROR_INVALIDDATA;
584  }
585 
586  // Unpack all LSB parts of residuals of this segment
587  for (i = 0; i < c->nchannels; i++) {
588  if (b->nscalablelsbs[i]) {
589  get_array(&s->gb,
590  b->lsb_sample_buffer[i] + seg * s->nsegsamples,
591  s->nsegsamples, b->nscalablelsbs[i]);
592  }
593  }
594  }
595 
596  // Skip to the end of band data
597  if (ff_dca_seek_bits(&s->gb, band_data_end)) {
598  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
599  return AVERROR_INVALIDDATA;
600  }
601 
602  return 0;
603 }
604 
606 {
607  DCAXllBand *b = &c->bands[band];
608  int i, offset, nsamples;
609 
610  if (seg < 0) {
611  offset = 0;
612  nsamples = s->nframesamples;
613  } else {
614  offset = seg * s->nsegsamples;
615  nsamples = s->nsegsamples;
616  }
617 
618  for (i = 0; i < c->nchannels; i++) {
619  memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
620  if (b->lsb_section_size)
621  memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
622  }
623 
624  if (seg <= 0 && band)
625  memset(c->deci_history, 0, sizeof(c->deci_history));
626 
627  if (seg < 0) {
628  memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
629  memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
630  }
631 }
632 
634 {
635  DCAXllBand *b = &c->bands[band];
636  int nsamples = s->nframesamples;
637  int i, j, k;
638 
639  // Inverse adaptive or fixed prediction
640  for (i = 0; i < c->nchannels; i++) {
641  int32_t *buf = b->msb_sample_buffer[i];
642  int order = b->adapt_pred_order[i];
643  if (order > 0) {
645  // Conversion from reflection coefficients to direct form coefficients
646  for (j = 0; j < order; j++) {
647  int rc = b->adapt_refl_coeff[i][j];
648  for (k = 0; k < (j + 1) / 2; k++) {
649  int tmp1 = coeff[ k ];
650  int tmp2 = coeff[j - k - 1];
651  coeff[ k ] = tmp1 + mul16(rc, tmp2);
652  coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
653  }
654  coeff[j] = rc;
655  }
656  // Inverse adaptive prediction
657  for (j = 0; j < nsamples - order; j++) {
658  int64_t err = 0;
659  for (k = 0; k < order; k++)
660  err += (int64_t)buf[j + k] * coeff[order - k - 1];
661  buf[j + k] -= clip23(norm16(err));
662  }
663  } else {
664  // Inverse fixed coefficient prediction
665  for (j = 0; j < b->fixed_pred_order[i]; j++)
666  for (k = 1; k < nsamples; k++)
667  buf[k] += buf[k - 1];
668  }
669  }
670 
671  // Inverse pairwise channel decorrellation
672  if (b->decor_enabled) {
674 
675  for (i = 0; i < c->nchannels / 2; i++) {
676  int coeff = b->decor_coeff[i];
677  if (coeff) {
678  s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
679  b->msb_sample_buffer[i * 2 ],
680  coeff, nsamples);
681  }
682  }
683 
684  // Reorder channel pointers to the original order
685  for (i = 0; i < c->nchannels; i++)
686  tmp[i] = b->msb_sample_buffer[i];
687 
688  for (i = 0; i < c->nchannels; i++)
689  b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
690  }
691 
692  // Map output channel pointers for frequency band 0
693  if (c->nfreqbands == 1)
694  for (i = 0; i < c->nchannels; i++)
695  s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
696 }
697 
698 static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
699 {
700  int adj = c->bands[band].bit_width_adjust[ch];
701  int shift = c->bands[band].nscalablelsbs[ch];
702 
703  if (s->fixed_lsb_width)
704  shift = s->fixed_lsb_width;
705  else if (shift && adj)
706  shift += adj - 1;
707  else
708  shift += adj;
709 
710  return shift;
711 }
712 
714 {
715  DCAXllBand *b = &c->bands[band];
716  int n, ch, nsamples = s->nframesamples;
717 
718  for (ch = 0; ch < c->nchannels; ch++) {
719  int shift = chs_get_lsb_width(s, c, band, ch);
720  if (shift) {
721  int32_t *msb = b->msb_sample_buffer[ch];
722  if (b->nscalablelsbs[ch]) {
723  int32_t *lsb = b->lsb_sample_buffer[ch];
724  int adj = b->bit_width_adjust[ch];
725  for (n = 0; n < nsamples; n++)
726  msb[n] = msb[n] * (1 << shift) + (lsb[n] << adj);
727  } else {
728  for (n = 0; n < nsamples; n++)
729  msb[n] = msb[n] * (1 << shift);
730  }
731  }
732  }
733 }
734 
736 {
737  int ch, nsamples = s->nframesamples;
738  int32_t *ptr;
739 
740  av_assert1(c->nfreqbands > 1);
741 
742  // Reallocate frequency band assembly buffer
744  2 * nsamples * c->nchannels * sizeof(int32_t));
745  if (!c->sample_buffer[2])
746  return AVERROR(ENOMEM);
747 
748  // Assemble frequency bands 0 and 1
749  ptr = c->sample_buffer[2];
750  for (ch = 0; ch < c->nchannels; ch++) {
751  int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
752  int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
753 
754  // Copy decimator history
755  memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
756  c->deci_history[ch], sizeof(c->deci_history[0]));
757 
758  // Filter
759  s->dcadsp->assemble_freq_bands(ptr, band0, band1,
761  nsamples);
762 
763  // Remap output channel pointer to assembly buffer
764  s->output_samples[c->ch_remap[ch]] = ptr;
765  ptr += nsamples * 2;
766  }
767 
768  return 0;
769 }
770 
772 {
773  int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
774 
775  // XLL extension sync word
776  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
777  av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
778  return AVERROR(EAGAIN);
779  }
780 
781  // Version number
782  stream_ver = get_bits(&s->gb, 4) + 1;
783  if (stream_ver > 1) {
784  avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
785  return AVERROR_PATCHWELCOME;
786  }
787 
788  // Lossless frame header length
789  header_size = get_bits(&s->gb, 8) + 1;
790 
791  // Check CRC
793  && ff_dca_check_crc(&s->gb, 32, header_size * 8)) {
794  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
795  return AVERROR_INVALIDDATA;
796  }
797 
798  // Number of bits used to read frame size
799  frame_size_nbits = get_bits(&s->gb, 5) + 1;
800 
801  // Number of bytes in a lossless frame
802  s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
803  if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
804  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
805  return AVERROR_INVALIDDATA;
806  }
807  s->frame_size++;
808 
809  // Number of channels sets per frame
810  s->nchsets = get_bits(&s->gb, 4) + 1;
811  if (s->nchsets > DCA_XLL_CHSETS_MAX) {
812  avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
813  return AVERROR_PATCHWELCOME;
814  }
815 
816  // Number of segments per frame
817  nframesegs_log2 = get_bits(&s->gb, 4);
818  s->nframesegs = 1 << nframesegs_log2;
819  if (s->nframesegs > 1024) {
820  av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
821  return AVERROR_INVALIDDATA;
822  }
823 
824  // Samples in segment per one frequency band for the first channel set
825  // Maximum value is 256 for sampling frequencies <= 48 kHz
826  // Maximum value is 512 for sampling frequencies > 48 kHz
827  s->nsegsamples_log2 = get_bits(&s->gb, 4);
828  if (!s->nsegsamples_log2) {
829  av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
830  return AVERROR_INVALIDDATA;
831  }
832  s->nsegsamples = 1 << s->nsegsamples_log2;
833  if (s->nsegsamples > 512) {
834  av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
835  return AVERROR_INVALIDDATA;
836  }
837 
838  // Samples in frame per one frequency band for the first channel set
839  s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
840  s->nframesamples = 1 << s->nframesamples_log2;
841  if (s->nframesamples > 65536) {
842  av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
843  return AVERROR_INVALIDDATA;
844  }
845 
846  // Number of bits used to read segment size
847  s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
848 
849  // Presence of CRC16 within each frequency band
850  // 0 - No CRC16 within band
851  // 1 - CRC16 placed at the end of MSB0
852  // 2 - CRC16 placed at the end of MSB0 and LSB0
853  // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
854  s->band_crc_present = get_bits(&s->gb, 2);
855 
856  // MSB/LSB split flag
857  s->scalable_lsbs = get_bits1(&s->gb);
858 
859  // Channel position mask
860  s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
861 
862  // Fixed LSB width
863  if (s->scalable_lsbs)
864  s->fixed_lsb_width = get_bits(&s->gb, 4);
865  else
866  s->fixed_lsb_width = 0;
867 
868  // Reserved
869  // Byte align
870  // Header CRC16 protection
871  if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
872  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
873  return AVERROR_INVALIDDATA;
874  }
875 
876  return 0;
877 }
878 
880 {
881  return !c->primary_chset && c->dmix_embedded && c->hier_chset;
882 }
883 
885 {
886  if (c->hier_chset)
887  while (++c < &s->chset[s->nchsets])
888  if (is_hier_dmix_chset(c))
889  return c;
890 
891  return NULL;
892 }
893 
895 {
896  int i, j, *coeff_ptr = c->dmix_coeff;
897 
898  for (i = 0; i < c->hier_ofs; i++) {
899  int scale = o->dmix_scale[i];
900  int scale_inv = o->dmix_scale_inv[i];
901  c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
902  c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
903  for (j = 0; j < c->nchannels; j++) {
904  int coeff = mul16(*coeff_ptr, scale_inv);
905  *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
906  }
907  }
908 }
909 
911 {
912  DCAContext *dca = s->avctx->priv_data;
913  DCAXllChSet *c;
914  int i, ret;
915 
916  // Parse channel set headers
917  s->nfreqbands = 0;
918  s->nchannels = 0;
919  s->nreschsets = 0;
920  for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
921  c->hier_ofs = s->nchannels;
922  if ((ret = chs_parse_header(s, c, asset)) < 0)
923  return ret;
924  if (c->nfreqbands > s->nfreqbands)
925  s->nfreqbands = c->nfreqbands;
926  if (c->hier_chset)
927  s->nchannels += c->nchannels;
928  if (c->residual_encode != (1 << c->nchannels) - 1)
929  s->nreschsets++;
930  }
931 
932  // Pre-scale downmixing coefficients for all non-primary channel sets
933  for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
934  if (is_hier_dmix_chset(c)) {
936  if (o)
937  prescale_down_mix(c, o);
938  }
939  }
940 
941  // Determine number of active channel sets to decode
942  switch (dca->request_channel_layout) {
944  s->nactivechsets = 1;
945  break;
948  s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
949  break;
950  default:
951  s->nactivechsets = s->nchsets;
952  break;
953  }
954 
955  return 0;
956 }
957 
959 {
960  int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
961  DCAXllChSet *c;
962 
963  // Determine size of NAVI table
964  navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
965  if (navi_nb > 1024) {
966  av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
967  return AVERROR_INVALIDDATA;
968  }
969 
970  // Reallocate NAVI table
971  av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
972  if (!s->navi)
973  return AVERROR(ENOMEM);
974 
975  // Parse NAVI
976  navi_pos = get_bits_count(&s->gb);
977  navi_ptr = s->navi;
978  for (band = 0; band < s->nfreqbands; band++) {
979  for (seg = 0; seg < s->nframesegs; seg++) {
980  for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
981  int size = 0;
982  if (c->nfreqbands > band) {
983  size = get_bits_long(&s->gb, s->seg_size_nbits);
984  if (size < 0 || size >= s->frame_size) {
985  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
986  return AVERROR_INVALIDDATA;
987  }
988  size++;
989  }
990  *navi_ptr++ = size;
991  }
992  }
993  }
994 
995  // Byte align
996  // CRC16
997  skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
998  skip_bits(&s->gb, 16);
999 
1000  // Check CRC
1002  && ff_dca_check_crc(&s->gb, navi_pos, get_bits_count(&s->gb))) {
1003  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
1004  return AVERROR_INVALIDDATA;
1005  }
1006 
1007  return 0;
1008 }
1009 
1011 {
1012  int ret, chs, seg, band, navi_pos, *navi_ptr;
1013  DCAXllChSet *c;
1014 
1015  for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
1016  if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1017  return ret;
1018  if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1019  return ret;
1020  }
1021 
1022  navi_pos = get_bits_count(&s->gb);
1023  navi_ptr = s->navi;
1024  for (band = 0; band < s->nfreqbands; band++) {
1025  for (seg = 0; seg < s->nframesegs; seg++) {
1026  for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1027  if (c->nfreqbands > band) {
1028  navi_pos += *navi_ptr * 8;
1029  if (navi_pos > s->gb.size_in_bits) {
1030  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
1031  return AVERROR_INVALIDDATA;
1032  }
1033  if (chs < s->nactivechsets &&
1034  (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
1036  return ret;
1037  chs_clear_band_data(s, c, band, seg);
1038  }
1039  s->gb.index = navi_pos;
1040  }
1041  navi_ptr++;
1042  }
1043  }
1044  }
1045 
1046  return 0;
1047 }
1048 
1050 {
1051  int ret;
1052 
1053  if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1054  return ret;
1055  if ((ret = parse_common_header(s)) < 0)
1056  return ret;
1057  if ((ret = parse_sub_headers(s, asset)) < 0)
1058  return ret;
1059  if ((ret = parse_navi_table(s)) < 0)
1060  return ret;
1061  if ((ret = parse_band_data(s)) < 0)
1062  return ret;
1063  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1064  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
1065  return AVERROR_INVALIDDATA;
1066  }
1067  return ret;
1068 }
1069 
1071 {
1072  s->pbr_length = 0;
1073  s->pbr_delay = 0;
1074 }
1075 
1076 static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
1077 {
1078  if (size > DCA_XLL_PBR_BUFFER_MAX)
1079  return AVERROR(ENOSPC);
1080 
1082  return AVERROR(ENOMEM);
1083 
1084  memcpy(s->pbr_buffer, data, size);
1085  s->pbr_length = size;
1086  s->pbr_delay = delay;
1087  return 0;
1088 }
1089 
1091 {
1092  int ret = parse_frame(s, data, size, asset);
1093 
1094  // If XLL packet data didn't start with a sync word, we must have jumped
1095  // right into the middle of PBR smoothing period
1096  if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
1097  // Skip to the next sync word in this packet
1098  data += asset->xll_sync_offset;
1099  size -= asset->xll_sync_offset;
1100 
1101  // If decoding delay is set, put the frame into PBR buffer and return
1102  // failure code. Higher level decoder is expected to switch to lossy
1103  // core decoding or mute its output until decoding delay expires.
1104  if (asset->xll_delay_nframes > 0) {
1105  if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
1106  return ret;
1107  return AVERROR(EAGAIN);
1108  }
1109 
1110  // No decoding delay, just parse the frame in place
1111  ret = parse_frame(s, data, size, asset);
1112  }
1113 
1114  if (ret < 0)
1115  return ret;
1116 
1117  if (s->frame_size > size)
1118  return AVERROR(EINVAL);
1119 
1120  // If the XLL decoder didn't consume full packet, start PBR smoothing period
1121  if (s->frame_size < size)
1122  if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
1123  return ret;
1124 
1125  return 0;
1126 }
1127 
1129 {
1130  int ret;
1131 
1132  if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
1133  ret = AVERROR(ENOSPC);
1134  goto fail;
1135  }
1136 
1137  memcpy(s->pbr_buffer + s->pbr_length, data, size);
1138  s->pbr_length += size;
1139 
1140  // Respect decoding delay after synchronization error
1141  if (s->pbr_delay > 0 && --s->pbr_delay)
1142  return AVERROR(EAGAIN);
1143 
1144  if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
1145  goto fail;
1146 
1147  if (s->frame_size > s->pbr_length) {
1148  ret = AVERROR(EINVAL);
1149  goto fail;
1150  }
1151 
1152  if (s->frame_size == s->pbr_length) {
1153  // End of PBR smoothing period
1154  clear_pbr(s);
1155  } else {
1156  s->pbr_length -= s->frame_size;
1157  memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
1158  }
1159 
1160  return 0;
1161 
1162 fail:
1163  // For now, throw out all PBR state on failure.
1164  // Perhaps we can be smarter and try to resync somehow.
1165  clear_pbr(s);
1166  return ret;
1167 }
1168 
1170 {
1171  int ret;
1172 
1173  if (s->hd_stream_id != asset->hd_stream_id) {
1174  clear_pbr(s);
1175  s->hd_stream_id = asset->hd_stream_id;
1176  }
1177 
1178  if (s->pbr_length)
1179  ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1180  else
1181  ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1182 
1183  return ret;
1184 }
1185 
1187 {
1188  int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
1189  DCAXllChSet *c;
1190 
1191  for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1192  if (!c->hier_chset)
1193  continue;
1194 
1195  av_assert1(band < c->nfreqbands);
1196  for (j = 0; j < c->nchannels; j++) {
1197  for (k = 0; k < o->nchannels; k++) {
1198  int coeff = *coeff_ptr++;
1199  if (coeff) {
1200  s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
1201  o->bands[band].msb_sample_buffer[k],
1202  coeff, s->nframesamples);
1203  if (band)
1204  s->dcadsp->dmix_sub(c->deci_history[j],
1205  o->deci_history[k],
1206  coeff, DCA_XLL_DECI_HISTORY_MAX);
1207  }
1208  }
1209  }
1210 
1211  nchannels += c->nchannels;
1212  if (nchannels >= o->hier_ofs)
1213  break;
1214  }
1215 }
1216 
1218 {
1219  int i, j, nchannels = 0;
1220  DCAXllChSet *c;
1221 
1222  for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1223  if (!c->hier_chset)
1224  continue;
1225 
1226  av_assert1(band < c->nfreqbands);
1227  for (j = 0; j < c->nchannels; j++) {
1228  int scale = o->dmix_scale[nchannels++];
1229  if (scale != (1 << 15)) {
1230  s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
1231  scale, s->nframesamples);
1232  if (band)
1233  s->dcadsp->dmix_scale(c->deci_history[j],
1234  scale, DCA_XLL_DECI_HISTORY_MAX);
1235  }
1236  }
1237 
1238  if (nchannels >= o->hier_ofs)
1239  break;
1240  }
1241 }
1242 
1243 // Clear all band data and replace non-residual encoded channels with lossy
1244 // counterparts
1246 {
1247  DCAContext *dca = s->avctx->priv_data;
1248  int band, ch;
1249 
1250  for (band = 0; band < c->nfreqbands; band++)
1251  chs_clear_band_data(s, c, band, -1);
1252 
1253  for (ch = 0; ch < c->nchannels; ch++) {
1254  if (!(c->residual_encode & (1 << ch)))
1255  continue;
1256  if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
1257  continue;
1258  c->residual_encode &= ~(1 << ch);
1259  }
1260 }
1261 
1263 {
1264  DCAContext *dca = s->avctx->priv_data;
1265  int ch, nsamples = s->nframesamples;
1266  DCAXllChSet *o;
1267 
1268  // Verify that core is compatible
1269  if (!(dca->packet & DCA_PACKET_CORE)) {
1270  av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
1271  return AVERROR(EINVAL);
1272  }
1273 
1274  if (c->freq != dca->core.output_rate) {
1275  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);
1276  return AVERROR_INVALIDDATA;
1277  }
1278 
1279  if (nsamples != dca->core.npcmsamples) {
1280  av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
1281  return AVERROR_INVALIDDATA;
1282  }
1283 
1284  // See if this channel set is downmixed and find the next channel set in
1285  // hierarchy. If downmixed, undo core pre-scaling before combining with
1286  // residual (residual is not scaled).
1287  o = find_next_hier_dmix_chset(s, c);
1288 
1289  // Reduce core bit width and combine with residual
1290  for (ch = 0; ch < c->nchannels; ch++) {
1291  int n, spkr, shift, round;
1292  int32_t *src, *dst;
1293 
1294  if (c->residual_encode & (1 << ch))
1295  continue;
1296 
1297  // Map this channel to core speaker
1298  spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
1299  if (spkr < 0) {
1300  av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
1301  return AVERROR_INVALIDDATA;
1302  }
1303 
1304  // Account for LSB width
1305  shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
1306  if (shift > 24) {
1307  av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
1308  return AVERROR_INVALIDDATA;
1309  }
1310 
1311  round = shift > 0 ? 1 << (shift - 1) : 0;
1312 
1313  src = dca->core.output_samples[spkr];
1314  dst = c->bands[0].msb_sample_buffer[ch];
1315  if (o) {
1316  // Undo embedded core downmix pre-scaling
1317  int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
1318  for (n = 0; n < nsamples; n++)
1319  dst[n] += clip23((mul16(src[n], scale_inv) + round) >> shift);
1320  } else {
1321  // No downmix scaling
1322  for (n = 0; n < nsamples; n++)
1323  dst[n] += (src[n] + round) >> shift;
1324  }
1325  }
1326 
1327  return 0;
1328 }
1329 
1331 {
1332  AVCodecContext *avctx = s->avctx;
1333  DCAContext *dca = avctx->priv_data;
1334  DCAExssAsset *asset = &dca->exss.assets[0];
1335  DCAXllChSet *p = &s->chset[0], *c;
1336  enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
1337  int i, j, k, ret, shift, nsamples, request_mask;
1338  int ch_remap[DCA_SPEAKER_COUNT];
1339 
1340  // Force lossy downmixed output during recovery
1341  if (dca->packet & DCA_PACKET_RECOVERY) {
1342  for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
1343  if (i < s->nactivechsets)
1344  force_lossy_output(s, c);
1345 
1346  if (!c->primary_chset)
1347  c->dmix_embedded = 0;
1348  }
1349 
1350  s->scalable_lsbs = 0;
1351  s->fixed_lsb_width = 0;
1352  }
1353 
1354  // Filter frequency bands for active channel sets
1355  s->output_mask = 0;
1356  for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1357  chs_filter_band_data(s, c, 0);
1358 
1359  if (c->residual_encode != (1 << c->nchannels) - 1
1360  && (ret = combine_residual_frame(s, c)) < 0)
1361  return ret;
1362 
1363  if (s->scalable_lsbs)
1364  chs_assemble_msbs_lsbs(s, c, 0);
1365 
1366  if (c->nfreqbands > 1) {
1367  chs_filter_band_data(s, c, 1);
1368  chs_assemble_msbs_lsbs(s, c, 1);
1369  }
1370 
1371  s->output_mask |= c->ch_mask;
1372  }
1373 
1374  // Undo hierarchial downmix and/or apply scaling
1375  for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
1376  if (!is_hier_dmix_chset(c))
1377  continue;
1378 
1379  if (i >= s->nactivechsets) {
1380  for (j = 0; j < c->nfreqbands; j++)
1381  if (c->bands[j].dmix_embedded)
1382  scale_down_mix(s, c, j);
1383  break;
1384  }
1385 
1386  for (j = 0; j < c->nfreqbands; j++)
1387  if (c->bands[j].dmix_embedded)
1388  undo_down_mix(s, c, j);
1389  }
1390 
1391  // Assemble frequency bands for active channel sets
1392  if (s->nfreqbands > 1) {
1393  for (i = 0; i < s->nactivechsets; i++)
1394  if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
1395  return ret;
1396  }
1397 
1398  // Normalize to regular 5.1 layout if downmixing
1399  if (dca->request_channel_layout) {
1400  if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
1403  }
1404  if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
1407  }
1408  }
1409 
1410  // Handle downmixing to stereo request
1413  && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1415  request_mask = DCA_SPEAKER_LAYOUT_STEREO;
1416  else
1417  request_mask = s->output_mask;
1418  if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
1419  return AVERROR(EINVAL);
1420 
1421  avctx->sample_rate = p->freq << (s->nfreqbands - 1);
1422 
1423  switch (p->storage_bit_res) {
1424  case 16:
1425  avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1426  break;
1427  case 24:
1428  avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1429  break;
1430  default:
1431  return AVERROR(EINVAL);
1432  }
1433 
1435  avctx->profile = FF_PROFILE_DTS_HD_MA;
1436  avctx->bit_rate = 0;
1437 
1438  frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
1439  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1440  return ret;
1441 
1442  // Downmix primary channel set to stereo
1443  if (request_mask != s->output_mask) {
1445  p->dmix_coeff, nsamples,
1446  s->output_mask);
1447  }
1448 
1449  shift = p->storage_bit_res - p->pcm_bit_res;
1450  for (i = 0; i < avctx->channels; i++) {
1451  int32_t *samples = s->output_samples[ch_remap[i]];
1452  if (frame->format == AV_SAMPLE_FMT_S16P) {
1453  int16_t *plane = (int16_t *)frame->extended_data[i];
1454  for (k = 0; k < nsamples; k++)
1455  plane[k] = av_clip_int16(samples[k] * (1 << shift));
1456  } else {
1457  int32_t *plane = (int32_t *)frame->extended_data[i];
1458  for (k = 0; k < nsamples; k++)
1459  plane[k] = clip23(samples[k] * (1 << shift)) * (1 << 8);
1460  }
1461  }
1462 
1463  if (!asset->one_to_one_map_ch_to_spkr) {
1465  matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1466  else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
1467  matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1468  } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
1469  matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1470  }
1471  if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1472  return ret;
1473 
1474  return 0;
1475 }
1476 
1478 {
1479  clear_pbr(s);
1480 }
1481 
1483 {
1484  DCAXllChSet *c;
1485  int i, j;
1486 
1487  for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
1488  for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1489  av_freep(&c->sample_buffer[j]);
1490  c->sample_size[j] = 0;
1491  }
1492  }
1493 
1494  av_freep(&s->navi);
1495  s->navi_size = 0;
1496 
1497  av_freep(&s->pbr_buffer);
1498  clear_pbr(s);
1499 }
int adapt_pred_order[DCA_XLL_CHANNELS_MAX]
Adaptive predictor order.
Definition: dca_xll.h:49
int plane
Definition: avisynth_c.h:291
int frame_size
Number of bytes in a lossless frame.
Definition: dca_xll.h:109
#define NULL
Definition: coverity.c:32
static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:1262
static int ff_dca_seek_bits(GetBitContext *s, int p)
Definition: dcadec.h:72
DCACoreDecoder core
Core decoder context.
Definition: dcadec.h:46
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void av_cold force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:1245
static int shift(int a, int b)
Definition: sonic.c:82
#define DCA_XLL_CHSETS_MAX
Definition: dca_xll.h:34
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
int primary_chset
Primary channel set flag.
Definition: dca_xll.h:72
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int pbr_delay
Delay in frames before decoding buffered data.
Definition: dca_xll.h:136
int ff_dca_check_crc(GetBitContext *s, int p1, int p2)
Definition: dcadec.c:97
static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:387
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1597
AVCodecContext * avctx
Definition: dca_xll.h:106
int nchannels
Number of channels in the channel set (N)
Definition: dca_xll.h:66
#define DCA_PACKET_RECOVERY
Definition: dcadec.h:40
#define DCA_XLL_PBR_BUFFER_MAX
Definition: dca_xll.h:41
int pbr_length
Length in bytes of data currently buffered.
Definition: dca_xll.h:135
const char * b
Definition: vf_curves.c:109
int * navi
NAVI table.
Definition: dca_xll.h:124
static int32_t mul15(int32_t a, int32_t b)
Definition: dcamath.h:46
int packet
Packet flags.
Definition: dcadec.h:55
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2924
int freq
Original sampling frequency (max. 96000 Hz)
Definition: dca_xll.h:70
int profile
profile
Definition: avcodec.h:3028
int dmix_scale[DCA_XLL_DMIX_SCALES_MAX]
Downmixing scales.
Definition: dca_xll.h:79
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:383
int bitalloc_part_a[DCA_XLL_CHANNELS_MAX]
Coding parameter for part A of segment.
Definition: dca_xll.h:93
static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
Definition: dca_xll.c:633
DCAXllBand bands[DCA_XLL_BANDS_MAX]
Frequency bands.
Definition: dca_xll.h:87
static int32_t norm16(int64_t a)
Definition: dcamath.h:41
int rice_code_flag[DCA_XLL_CHANNELS_MAX]
Rice coding flag.
Definition: dca_xll.h:91
int band_crc_present
Presence of CRC16 within each frequency band.
Definition: dca_xll.h:117
int highest_pred_order
Highest adaptive predictor order.
Definition: dca_xll.h:50
int32_t * sample_buffer[DCA_XLL_SAMPLE_BUFFERS_MAX]
Definition: dca_xll.h:102
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int32_t clip23(int32_t a)
Definition: dcamath.h:53
static int get_rice(GetBitContext *gb, int k)
Definition: dca_xll.c:39
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2295
uint8_t
static int32_t mul16(int32_t a, int32_t b)
Definition: dcamath.h:47
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
#define FF_DCA_DMIXTABLE_OFFSET
Definition: dcadata.h:65
int dmix_coeffs_present
Downmix coefficients present in stream.
Definition: dca_xll.h:73
int nfreqbands
Highest number of frequency bands.
Definition: dca_xll.h:127
#define DCA_XLL_SAMPLE_BUFFERS_MAX
Definition: dca_xll.h:42
int ch_remap[DCA_XLL_CHANNELS_MAX]
Channel to speaker map.
Definition: dca_xll.h:82
static AVFrame * frame
static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
Definition: dca_xll.c:63
int fixed_lsb_width
Fixed LSB width.
Definition: dca_xll.h:120
int fixed_pred_order[DCA_XLL_CHANNELS_MAX]
Fixed predictor order.
Definition: dca_xll.h:51
int nfreqbands
Number of frequency bands (1 or 2)
Definition: dca_xll.h:84
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
Definition: dca_xll.c:53
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
ptrdiff_t size
Definition: opengl_enc.c:101
int32_t * lsb_sample_buffer[DCA_XLL_CHANNELS_MAX]
LSB sample buffer pointers or NULL.
Definition: dca_xll.h:61
#define av_log(a,...)
static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
Definition: dca_xll.c:116
DCAExssParser exss
EXSS parser context.
Definition: dcadec.h:47
unsigned m
Definition: audioconvert.c:187
unsigned int navi_size
Definition: dca_xll.h:125
static int parse_band_data(DCAXllDecoder *s)
Definition: dca_xll.c:1010
void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, int *coeff_l, int nsamples, int ch_mask)
Definition: dcadec.c:106
int hd_stream_id
DTS-HD stream ID.
Definition: dca_exss.h:68
int npcmsamples
Number of PCM samples per channel.
Definition: dca_core.h:181
#define U(x)
Definition: vp56_arith.h:37
int adapt_refl_coeff[DCA_XLL_CHANNELS_MAX][DCA_XLL_ADAPT_PRED_ORDER_MAX]
Adaptive predictor reflection coefficients.
Definition: dca_xll.h:52
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:607
int orig_order[DCA_XLL_CHANNELS_MAX]
Original channel order.
Definition: dca_xll.h:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
Definition: dca_xll.c:1330
int representation_type
Representation type.
Definition: dca_exss.h:42
static int get_linear(GetBitContext *gb, int n)
Definition: dca_xll.c:27
static DCAXllChSet * find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:884
#define AVERROR(e)
Definition: error.h:43
int scalable_lsbs
MSB/LSB split flag.
Definition: dca_xll.h:118
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition: dcadata.c:8638
int nsegsamples_log2
log2(nsegsamples)
Definition: dca_xll.h:112
DCADSPContext * dcadsp
Definition: dca_xll.h:138
static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
Definition: dca_xll.c:910
int pcm_bit_res
PCM bit resolution (variable)
Definition: dca_xll.h:68
static av_always_inline av_const double round(double x)
Definition: libm.h:444
int hier_chset
Whether the channel set is part of a hierarchy.
Definition: dca_xll.h:76
void(* assemble_freq_bands)(int32_t *dst, int32_t *src0, int32_t *src1, const int32_t *coeff, ptrdiff_t len)
Definition: dcadsp.h:85
static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
Definition: dca_xll.c:894
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define DCA_SPEAKER_LAYOUT_STEREO
Definition: dca.h:79
int32_t * output_samples[DCA_SPEAKER_COUNT]
Definition: dca_xll.h:141
#define fail()
Definition: checkasm.h:80
#define DCA_BUFFER_PADDING_SIZE
Definition: dcadec.h:35
int decor_enabled
Pairwise channel decorrelation flag.
Definition: dca_xll.h:45
int hier_ofs
Number of preceding channels in a hierarchy (M)
Definition: dca_xll.h:77
int nchannels
Total number of channels in a hierarchy.
Definition: dca_xll.h:128
int seg_size_nbits
Number of bits used to read segment size.
Definition: dca_xll.h:116
int output_rate
Output sample rate (1x or 2x header rate)
Definition: dca_core.h:182
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2811
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
Definition: dca_xll.c:443
int dmix_type
Primary channel set downmix type.
Definition: dca_xll.h:75
const int32_t ff_dca_xll_band_coeff[20]
Definition: dcadata.c:8720
signed 32 bits, planar
Definition: samplefmt.h:69
int xll_offset
Offset to XLL data from start of substream.
Definition: dca_exss.h:62
static int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
Definition: dca_core.h:187
#define DCA_SYNCWORD_XLL
Definition: dca_syncwords.h:31
int nframesamples
Samples in frame per one frequency band.
Definition: dca_xll.h:115
int nabits
Number of bits to read bit allocation coding parameter.
Definition: dca_xll.h:85
const uint8_t ff_dca_dmix_primary_nch[8]
Definition: dcadata.c:49
void(* dmix_scale)(int32_t *dst, int scale, ptrdiff_t len)
Definition: dcadsp.h:81
static void av_cold chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
Definition: dca_xll.c:605
const uint16_t ff_dca_xll_refl_coeff[128]
Definition: dcadata.c:8701
int size_in_bits
Definition: get_bits.h:57
int32_t
#define DCA_HAS_STEREO(mask)
Definition: dca.h:90
#define DCA_XLL_ADAPT_PRED_ORDER_MAX
Definition: dca_xll.h:37
int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
Definition: dcadec.c:32
static int get_rice_un(GetBitContext *gb, int k)
Definition: dca_xll.c:33
int nsegsamples
Samples in segment per one frequency band.
Definition: dca_xll.h:113
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2822
int n
Definition: avisynth_c.h:547
int xll_delay_nframes
Initial XLL decoding delay in frames.
Definition: dca_exss.h:65
#define src
Definition: vp9dsp.c:530
DCAExssAsset assets[1]
Audio asset descriptors.
Definition: dca_exss.h:87
int xll_size
Size of XLL data in extension substream.
Definition: dca_exss.h:63
const uint32_t ff_dca_inv_dmixtable[FF_DCA_INV_DMIXTABLE_SIZE]
Definition: dcadata.c:8672
int dmix_scale_inv[DCA_XLL_DMIX_SCALES_MAX]
Inverse downmixing scales.
Definition: dca_xll.h:80
int one_to_one_map_ch_to_spkr
One to one channel to speaker mapping flag.
Definition: dca_exss.h:37
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:239
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:242
int output_mask
Definition: dca_xll.h:140
static int parse_frame_no_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
Definition: dca_xll.c:1090
int residual_encode
Residual encoding mask (0 - residual, 1 - full channel)
Definition: dca_xll.h:67
static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
Definition: dca_xll.c:1217
static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
Definition: dca_xll.c:45
int bitalloc_hybrid_linear[DCA_XLL_CHANNELS_MAX]
Binary code length for isolated samples.
Definition: dca_xll.h:92
#define DCA_PACKET_CORE
Definition: dcadec.h:37
int sample_rate
samples per second
Definition: avcodec.h:2287
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:449
int hd_stream_id
Previous DTS-HD stream ID for detecting changes.
Definition: dca_xll.h:132
main external API structure.
Definition: avcodec.h:1532
int lsb_section_size
Size of LSB section in any segment.
Definition: dca_xll.h:56
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:894
#define FF_DCA_INV_DMIXTABLE_SIZE
Definition: dcadata.h:64
unsigned int sample_size[DCA_XLL_SAMPLE_BUFFERS_MAX]
Definition: dca_xll.h:101
void * buf
Definition: avisynth_c.h:553
#define AV_EF_CAREFUL
consider things that violate the spec, are fast to calculate and have not been seen in the wild as er...
Definition: avcodec.h:2825
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:312
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:305
int index
Definition: gxfenc.c:89
static int parse_frame_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
Definition: dca_xll.c:1128
GetBitContext gb
Definition: dca_xll.h:107
int nreschsets
Number of channel sets that have residual encoded channels.
Definition: dca_xll.h:129
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data...
Definition: avcodec.h:2819
#define DCA_SPEAKER_LAYOUT_5POINT1
Definition: dca.h:86
int nframesegs
Number of segments per frame.
Definition: dca_xll.h:111
int32_t * output_samples[DCA_SPEAKER_COUNT]
PCM output for fixed point mode.
Definition: dca_core.h:174
static int parse_navi_table(DCAXllDecoder *s)
Definition: dca_xll.c:958
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:345
const int32_t ff_dca_sampling_freqs[16]
Definition: dcadata.c:8728
static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
Definition: dca_xll.c:698
#define DCA_SPEAKER_LAYOUT_5POINT0
Definition: dca.h:85
int ch_mask
Channel mask for set.
Definition: dca_xll.h:81
int32_t * msb_sample_buffer[DCA_XLL_CHANNELS_MAX]
MSB sample buffer pointers.
Definition: dca_xll.h:60
static int is_hier_dmix_chset(DCAXllChSet *c)
Definition: dca_xll.c:879
int nsamples_part_a[DCA_XLL_CHANNELS_MAX]
Number of samples in part A of segment.
Definition: dca_xll.h:95
static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:71
#define DCA_XLL_DECI_HISTORY_MAX
Definition: dca_xll.h:38
static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
Definition: dca_xll.c:713
static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
Definition: dca_xll.c:1186
av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
Definition: dca_xll.c:1477
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:499
static int parse_common_header(DCAXllDecoder *s)
Definition: dca_xll.c:771
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:33
int dmix_embedded
Downmix already performed by encoder.
Definition: dca_xll.h:74
static void clear_pbr(DCAXllDecoder *s)
Definition: dca_xll.c:1070
static double c[64]
int dmix_coeff[DCA_XLL_DMIX_COEFFS_MAX]
Downmixing coefficients.
Definition: dca_xll.h:78
int xll_sync_present
XLL sync word present flag.
Definition: dca_exss.h:64
int storage_bit_res
Storage bit resolution (16 or 24)
Definition: dca_xll.h:69
DCAXllChSet chset[DCA_XLL_CHSETS_MAX]
Channel sets.
Definition: dca_xll.h:122
int request_channel_layout
Converted from avctx.request_channel_layout.
Definition: dcadec.h:59
static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:735
int32_t deci_history[DCA_XLL_CHANNELS_MAX][DCA_XLL_DECI_HISTORY_MAX]
Decimator history for frequency band 1.
Definition: dca_xll.h:98
#define FF_DCA_DMIXTABLE_SIZE
Definition: dcadata.h:63
void * priv_data
Definition: avcodec.h:1574
av_cold void ff_dca_xll_close(DCAXllDecoder *s)
Definition: dca_xll.c:1482
uint8_t * pbr_buffer
Peak bit rate (PBR) smoothing buffer.
Definition: dca_xll.h:134
int channels
number of audio channels
Definition: avcodec.h:2288
int seg_common
Segment type.
Definition: dca_xll.h:90
void(* dmix_sub)(int32_t *dst, const int32_t *src, int coeff, ptrdiff_t len)
Definition: dcadsp.h:77
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:410
int ch_mask_nbits
Number of bits used to read channel mask.
Definition: dca_xll.h:119
#define DCA_XLL_CHANNELS_MAX
Definition: dca_xll.h:35
int ff_dca_xll_parse(DCAXllDecoder *s, uint8_t *data, DCAExssAsset *asset)
Definition: dca_xll.c:1169
int bit_width_adjust[DCA_XLL_CHANNELS_MAX]
Number of bits discarded by authoring.
Definition: dca_xll.h:58
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:68
int nframesamples_log2
log2(nframesamples)
Definition: dca_xll.h:114
AVMatrixEncoding
int nscalablelsbs[DCA_XLL_CHANNELS_MAX]
Number of bits to represent the samples in LSB part.
Definition: dca_xll.h:57
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:3047
static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
Definition: dca_xll.c:1076
static int parse_frame(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
Definition: dca_xll.c:1049
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:225
int xll_sync_offset
Number of bytes offset to XLL sync.
Definition: dca_exss.h:66
int nchsets
Number of channels sets per frame.
Definition: dca_xll.h:110
int dmix_embedded
Downmix performed by encoder in frequency band.
Definition: dca_xll.h:54
int decor_coeff[DCA_XLL_CHANNELS_MAX/2]
Pairwise channel coefficients.
Definition: dca_xll.h:47
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:235
void(* decor)(int32_t *dst, const int32_t *src, int coeff, ptrdiff_t len)
Definition: dcadsp.h:72
for(j=16;j >0;--j)
int nactivechsets
Number of active channel sets to decode.
Definition: dca_xll.h:130
int bitalloc_part_b[DCA_XLL_CHANNELS_MAX]
Coding parameter for part B of segment.
Definition: dca_xll.h:94