FFmpeg
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, get_bits_left(gb));
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
74  int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
75  int i, j, *coeff_ptr = c->dmix_coeff;
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;
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  }
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
126  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
127  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");
128  return AVERROR_INVALIDDATA;
129  }
130 
131  // Number of channels in the channel set
132  c->nchannels = get_bits(&s->gb, 4) + 1;
133  if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
134  avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
135  return AVERROR_PATCHWELCOME;
136  }
137 
138  // Residual type
139  c->residual_encode = get_bits(&s->gb, c->nchannels);
140 
141  // PCM bit resolution
142  c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
143 
144  // Storage unit width
145  c->storage_bit_res = get_bits(&s->gb, 5) + 1;
146  if (c->storage_bit_res != 16 && c->storage_bit_res != 20 && c->storage_bit_res != 24) {
147  avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);
148  return AVERROR_PATCHWELCOME;
149  }
150 
151  if (c->pcm_bit_res > c->storage_bit_res) {
152  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);
153  return AVERROR_INVALIDDATA;
154  }
155 
156  // Original sampling frequency
157  c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
158  if (c->freq > 192000) {
159  avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
160  return AVERROR_PATCHWELCOME;
161  }
162 
163  // Sampling frequency modifier
164  if (get_bits(&s->gb, 2)) {
165  avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
166  return AVERROR_PATCHWELCOME;
167  }
168 
169  // Which replacement set this channel set is member of
170  if (get_bits(&s->gb, 2)) {
171  avpriv_request_sample(s->avctx, "XLL replacement set");
172  return AVERROR_PATCHWELCOME;
173  }
174 
175  if (asset->one_to_one_map_ch_to_spkr) {
176  // Primary channel set flag
177  c->primary_chset = get_bits1(&s->gb);
178  if (c->primary_chset != (c == p)) {
179  av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");
180  return AVERROR_INVALIDDATA;
181  }
182 
183  // Downmix coefficients present in stream
184  c->dmix_coeffs_present = get_bits1(&s->gb);
185 
186  // Downmix already performed by encoder
187  c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
188 
189  // Downmix type
190  if (c->dmix_coeffs_present && c->primary_chset) {
191  c->dmix_type = get_bits(&s->gb, 3);
192  if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {
193  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");
194  return AVERROR_INVALIDDATA;
195  }
196  }
197 
198  // Whether the channel set is part of a hierarchy
199  c->hier_chset = get_bits1(&s->gb);
200  if (!c->hier_chset && s->nchsets != 1) {
201  avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
202  return AVERROR_PATCHWELCOME;
203  }
204 
205  // Downmix coefficients
206  if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
207  return ret;
208 
209  // Channel mask enabled
210  if (!get_bits1(&s->gb)) {
211  avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
212  return AVERROR_PATCHWELCOME;
213  }
214 
215  // Channel mask for set
216  c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
217  if (av_popcount(c->ch_mask) != c->nchannels) {
218  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");
219  return AVERROR_INVALIDDATA;
220  }
221 
222  // Build the channel to speaker map
223  for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
224  if (c->ch_mask & (1U << i))
225  c->ch_remap[j++] = i;
226  } else {
227  // Mapping coeffs present flag
228  if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {
229  avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");
230  return AVERROR_PATCHWELCOME;
231  }
232 
233  // Setup for LtRt decoding
234  c->primary_chset = 1;
235  c->dmix_coeffs_present = 0;
236  c->dmix_embedded = 0;
237  c->hier_chset = 0;
238  c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;
239  c->ch_remap[0] = DCA_SPEAKER_L;
240  c->ch_remap[1] = DCA_SPEAKER_R;
241  }
242 
243  if (c->freq > 96000) {
244  // Extra frequency bands flag
245  if (get_bits1(&s->gb)) {
246  avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
247  return AVERROR_PATCHWELCOME;
248  }
249  c->nfreqbands = 2;
250  } else {
251  c->nfreqbands = 1;
252  }
253 
254  // Set the sampling frequency to that of the first frequency band.
255  // Frequency will be doubled again after bands assembly.
256  c->freq >>= c->nfreqbands - 1;
257 
258  // Verify that all channel sets have the same audio characteristics
259  if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
260  || c->pcm_bit_res != p->pcm_bit_res
261  || c->storage_bit_res != p->storage_bit_res)) {
262  avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
263  return AVERROR_PATCHWELCOME;
264  }
265 
266  // Determine number of bits to read bit allocation coding parameter
267  if (c->storage_bit_res > 16)
268  c->nabits = 5;
269  else if (c->storage_bit_res > 8)
270  c->nabits = 4;
271  else
272  c->nabits = 3;
273 
274  // Account for embedded downmix and decimator saturation
275  if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
276  c->nabits++;
277 
278  for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
279  // Pairwise channel decorrelation
280  if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
281  int ch_nbits = av_ceil_log2(c->nchannels);
282 
283  // Original channel order
284  for (i = 0; i < c->nchannels; i++) {
285  b->orig_order[i] = get_bits(&s->gb, ch_nbits);
286  if (b->orig_order[i] >= c->nchannels) {
287  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");
288  return AVERROR_INVALIDDATA;
289  }
290  }
291 
292  // Pairwise channel coefficients
293  for (i = 0; i < c->nchannels / 2; i++)
294  b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
295  } else {
296  for (i = 0; i < c->nchannels; i++)
297  b->orig_order[i] = i;
298  for (i = 0; i < c->nchannels / 2; i++)
299  b->decor_coeff[i] = 0;
300  }
301 
302  // Adaptive predictor order
303  b->highest_pred_order = 0;
304  for (i = 0; i < c->nchannels; i++) {
305  b->adapt_pred_order[i] = get_bits(&s->gb, 4);
306  if (b->adapt_pred_order[i] > b->highest_pred_order)
307  b->highest_pred_order = b->adapt_pred_order[i];
308  }
309  if (b->highest_pred_order > s->nsegsamples) {
310  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive predicition order\n");
311  return AVERROR_INVALIDDATA;
312  }
313 
314  // Fixed predictor order
315  for (i = 0; i < c->nchannels; i++)
316  b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
317 
318  // Adaptive predictor quantized reflection coefficients
319  for (i = 0; i < c->nchannels; i++) {
320  for (j = 0; j < b->adapt_pred_order[i]; j++) {
321  k = get_linear(&s->gb, 8);
322  if (k == -128) {
323  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
324  return AVERROR_INVALIDDATA;
325  }
326  if (k < 0)
327  b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
328  else
329  b->adapt_refl_coeff[i][j] = (int)ff_dca_xll_refl_coeff[ k];
330  }
331  }
332 
333  // Downmix performed by encoder in extension frequency band
334  b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
335 
336  // MSB/LSB split flag in extension frequency band
337  if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
338  // Size of LSB section in any segment
339  b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
340  if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {
341  av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");
342  return AVERROR_INVALIDDATA;
343  }
344 
345  // Account for optional CRC bytes after LSB section
346  if (b->lsb_section_size && (s->band_crc_present > 2 ||
347  (band == 0 && s->band_crc_present > 1)))
348  b->lsb_section_size += 2;
349 
350  // Number of bits to represent the samples in LSB part
351  for (i = 0; i < c->nchannels; i++) {
352  b->nscalablelsbs[i] = get_bits(&s->gb, 4);
353  if (b->nscalablelsbs[i] && !b->lsb_section_size) {
354  av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");
355  return AVERROR_INVALIDDATA;
356  }
357  }
358  } else {
359  b->lsb_section_size = 0;
360  for (i = 0; i < c->nchannels; i++)
361  b->nscalablelsbs[i] = 0;
362  }
363 
364  // Scalable resolution flag in extension frequency band
365  if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
366  // Number of bits discarded by authoring
367  for (i = 0; i < c->nchannels; i++)
368  b->bit_width_adjust[i] = get_bits(&s->gb, 4);
369  } else {
370  for (i = 0; i < c->nchannels; i++)
371  b->bit_width_adjust[i] = 0;
372  }
373  }
374 
375  // Reserved
376  // Byte align
377  // CRC16 of channel set sub-header
378  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
379  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");
380  return AVERROR_INVALIDDATA;
381  }
382 
383  return 0;
384 }
385 
387 {
388  int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
389  int nchsamples = s->nframesamples + ndecisamples;
390  int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
391  int32_t *ptr;
392 
393  // Reallocate MSB sample buffer
394  av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
395  if (!c->sample_buffer[0])
396  return AVERROR(ENOMEM);
397 
398  ptr = c->sample_buffer[0] + ndecisamples;
399  for (i = 0; i < c->nfreqbands; i++) {
400  for (j = 0; j < c->nchannels; j++) {
401  c->bands[i].msb_sample_buffer[j] = ptr;
402  ptr += nchsamples;
403  }
404  }
405 
406  return 0;
407 }
408 
410 {
411  int i, j, nsamples = 0;
412  int32_t *ptr;
413 
414  // Determine number of frequency bands that have MSB/LSB split
415  for (i = 0; i < c->nfreqbands; i++)
416  if (c->bands[i].lsb_section_size)
417  nsamples += s->nframesamples * c->nchannels;
418  if (!nsamples)
419  return 0;
420 
421  // Reallocate LSB sample buffer
422  av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
423  if (!c->sample_buffer[1])
424  return AVERROR(ENOMEM);
425 
426  ptr = c->sample_buffer[1];
427  for (i = 0; i < c->nfreqbands; i++) {
428  if (c->bands[i].lsb_section_size) {
429  for (j = 0; j < c->nchannels; j++) {
430  c->bands[i].lsb_sample_buffer[j] = ptr;
431  ptr += s->nframesamples;
432  }
433  } else {
434  for (j = 0; j < c->nchannels; j++)
435  c->bands[i].lsb_sample_buffer[j] = NULL;
436  }
437  }
438 
439  return 0;
440 }
441 
442 static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
443 {
444  DCAXllBand *b = &c->bands[band];
445  int i, j, k;
446 
447  // Start unpacking MSB portion of the segment
448  if (!(seg && get_bits1(&s->gb))) {
449  // Unpack segment type
450  // 0 - distinct coding parameters for each channel
451  // 1 - common coding parameters for all channels
452  c->seg_common = get_bits1(&s->gb);
453 
454  // Determine number of coding parameters encoded in segment
455  k = c->seg_common ? 1 : c->nchannels;
456 
457  // Unpack Rice coding parameters
458  for (i = 0; i < k; i++) {
459  // Unpack Rice coding flag
460  // 0 - linear code, 1 - Rice code
461  c->rice_code_flag[i] = get_bits1(&s->gb);
462  // Unpack Hybrid Rice coding flag
463  // 0 - Rice code, 1 - Hybrid Rice code
464  if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))
465  // Unpack binary code length for isolated samples
466  c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
467  else
468  // 0 indicates no Hybrid Rice coding
469  c->bitalloc_hybrid_linear[i] = 0;
470  }
471 
472  // Unpack coding parameters
473  for (i = 0; i < k; i++) {
474  if (seg == 0) {
475  // Unpack coding parameter for part A of segment 0
476  c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
477 
478  // Adjust for the linear code
479  if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
480  c->bitalloc_part_a[i]++;
481 
482  if (!c->seg_common)
483  c->nsamples_part_a[i] = b->adapt_pred_order[i];
484  else
485  c->nsamples_part_a[i] = b->highest_pred_order;
486  } else {
487  c->bitalloc_part_a[i] = 0;
488  c->nsamples_part_a[i] = 0;
489  }
490 
491  // Unpack coding parameter for part B of segment
492  c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
493 
494  // Adjust for the linear code
495  if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
496  c->bitalloc_part_b[i]++;
497  }
498  }
499 
500  // Unpack entropy codes
501  for (i = 0; i < c->nchannels; i++) {
502  int32_t *part_a, *part_b;
503  int nsamples_part_b;
504 
505  // Select index of coding parameters
506  k = c->seg_common ? 0 : i;
507 
508  // Slice the segment into parts A and B
509  part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
510  part_b = part_a + c->nsamples_part_a[k];
511  nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
512 
513  if (get_bits_left(&s->gb) < 0)
514  return AVERROR_INVALIDDATA;
515 
516  if (!c->rice_code_flag[k]) {
517  // Linear codes
518  // Unpack all residuals of part A of segment 0
519  get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
520  c->bitalloc_part_a[k]);
521 
522  // Unpack all residuals of part B of segment 0 and others
523  get_linear_array(&s->gb, part_b, nsamples_part_b,
524  c->bitalloc_part_b[k]);
525  } else {
526  // Rice codes
527  // Unpack all residuals of part A of segment 0
528  get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
529  c->bitalloc_part_a[k]);
530 
531  if (c->bitalloc_hybrid_linear[k]) {
532  // Hybrid Rice codes
533  // Unpack the number of isolated samples
534  int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
535 
536  // Set all locations to 0
537  memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
538 
539  // Extract the locations of isolated samples and flag by -1
540  for (j = 0; j < nisosamples; j++) {
541  int loc = get_bits(&s->gb, s->nsegsamples_log2);
542  if (loc >= nsamples_part_b) {
543  av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
544  return AVERROR_INVALIDDATA;
545  }
546  part_b[loc] = -1;
547  }
548 
549  // Unpack all residuals of part B of segment 0 and others
550  for (j = 0; j < nsamples_part_b; j++) {
551  if (part_b[j])
552  part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
553  else
554  part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
555  }
556  } else {
557  // Rice codes
558  // Unpack all residuals of part B of segment 0 and others
559  get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
560  }
561  }
562  }
563 
564  // Unpack decimator history for frequency band 1
565  if (seg == 0 && band == 1) {
566  int nbits = get_bits(&s->gb, 5) + 1;
567  for (i = 0; i < c->nchannels; i++)
568  for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
569  c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
570  }
571 
572  // Start unpacking LSB portion of the segment
573  if (b->lsb_section_size) {
574  // Skip to the start of LSB portion
575  if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
576  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
577  return AVERROR_INVALIDDATA;
578  }
579 
580  // Unpack all LSB parts of residuals of this segment
581  for (i = 0; i < c->nchannels; i++) {
582  if (b->nscalablelsbs[i]) {
583  get_array(&s->gb,
584  b->lsb_sample_buffer[i] + seg * s->nsegsamples,
585  s->nsegsamples, b->nscalablelsbs[i]);
586  }
587  }
588  }
589 
590  // Skip to the end of band data
591  if (ff_dca_seek_bits(&s->gb, band_data_end)) {
592  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
593  return AVERROR_INVALIDDATA;
594  }
595 
596  return 0;
597 }
598 
599 static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
600 {
601  DCAXllBand *b = &c->bands[band];
602  int i, offset, nsamples;
603 
604  if (seg < 0) {
605  offset = 0;
606  nsamples = s->nframesamples;
607  } else {
608  offset = seg * s->nsegsamples;
609  nsamples = s->nsegsamples;
610  }
611 
612  for (i = 0; i < c->nchannels; i++) {
613  memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
614  if (b->lsb_section_size)
615  memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
616  }
617 
618  if (seg <= 0 && band)
619  memset(c->deci_history, 0, sizeof(c->deci_history));
620 
621  if (seg < 0) {
622  memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
623  memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
624  }
625 }
626 
628 {
629  DCAXllBand *b = &c->bands[band];
630  int nsamples = s->nframesamples;
631  int i, j, k;
632 
633  // Inverse adaptive or fixed prediction
634  for (i = 0; i < c->nchannels; i++) {
635  int32_t *buf = b->msb_sample_buffer[i];
636  int order = b->adapt_pred_order[i];
637  if (order > 0) {
639  // Conversion from reflection coefficients to direct form coefficients
640  for (j = 0; j < order; j++) {
641  int rc = b->adapt_refl_coeff[i][j];
642  for (k = 0; k < (j + 1) / 2; k++) {
643  int tmp1 = coeff[ k ];
644  int tmp2 = coeff[j - k - 1];
645  coeff[ k ] = tmp1 + mul16(rc, tmp2);
646  coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
647  }
648  coeff[j] = rc;
649  }
650  // Inverse adaptive prediction
651  for (j = 0; j < nsamples - order; j++) {
652  int64_t err = 0;
653  for (k = 0; k < order; k++)
654  err += (int64_t)buf[j + k] * coeff[order - k - 1];
655  buf[j + k] -= (SUINT)clip23(norm16(err));
656  }
657  } else {
658  // Inverse fixed coefficient prediction
659  for (j = 0; j < b->fixed_pred_order[i]; j++)
660  for (k = 1; k < nsamples; k++)
661  buf[k] += (unsigned)buf[k - 1];
662  }
663  }
664 
665  // Inverse pairwise channel decorrellation
666  if (b->decor_enabled) {
668 
669  for (i = 0; i < c->nchannels / 2; i++) {
670  int coeff = b->decor_coeff[i];
671  if (coeff) {
672  s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
673  b->msb_sample_buffer[i * 2 ],
674  coeff, nsamples);
675  }
676  }
677 
678  // Reorder channel pointers to the original order
679  for (i = 0; i < c->nchannels; i++)
680  tmp[i] = b->msb_sample_buffer[i];
681 
682  for (i = 0; i < c->nchannels; i++)
683  b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
684  }
685 
686  // Map output channel pointers for frequency band 0
687  if (c->nfreqbands == 1)
688  for (i = 0; i < c->nchannels; i++)
689  s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
690 }
691 
692 static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
693 {
694  int adj = c->bands[band].bit_width_adjust[ch];
695  int shift = c->bands[band].nscalablelsbs[ch];
696 
697  if (s->fixed_lsb_width)
698  shift = s->fixed_lsb_width;
699  else if (shift && adj)
700  shift += adj - 1;
701  else
702  shift += adj;
703 
704  return shift;
705 }
706 
708 {
709  DCAXllBand *b = &c->bands[band];
710  int n, ch, nsamples = s->nframesamples;
711 
712  for (ch = 0; ch < c->nchannels; ch++) {
713  int shift = chs_get_lsb_width(s, c, band, ch);
714  if (shift) {
715  int32_t *msb = b->msb_sample_buffer[ch];
716  if (b->nscalablelsbs[ch]) {
717  int32_t *lsb = b->lsb_sample_buffer[ch];
718  int adj = b->bit_width_adjust[ch];
719  for (n = 0; n < nsamples; n++)
720  msb[n] = msb[n] * (SUINT)(1 << shift) + (lsb[n] << adj);
721  } else {
722  for (n = 0; n < nsamples; n++)
723  msb[n] = msb[n] * (SUINT)(1 << shift);
724  }
725  }
726  }
727 }
728 
730 {
731  int ch, nsamples = s->nframesamples;
732  int32_t *ptr;
733 
734  av_assert1(c->nfreqbands > 1);
735 
736  // Reallocate frequency band assembly buffer
737  av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
738  2 * nsamples * c->nchannels * sizeof(int32_t));
739  if (!c->sample_buffer[2])
740  return AVERROR(ENOMEM);
741 
742  // Assemble frequency bands 0 and 1
743  ptr = c->sample_buffer[2];
744  for (ch = 0; ch < c->nchannels; ch++) {
745  int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
746  int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
747 
748  // Copy decimator history
749  memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
750  c->deci_history[ch], sizeof(c->deci_history[0]));
751 
752  // Filter
753  s->dcadsp->assemble_freq_bands(ptr, band0, band1,
755  nsamples);
756 
757  // Remap output channel pointer to assembly buffer
758  s->output_samples[c->ch_remap[ch]] = ptr;
759  ptr += nsamples * 2;
760  }
761 
762  return 0;
763 }
764 
766 {
767  int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
768 
769  // XLL extension sync word
770  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
771  av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
772  return AVERROR(EAGAIN);
773  }
774 
775  // Version number
776  stream_ver = get_bits(&s->gb, 4) + 1;
777  if (stream_ver > 1) {
778  avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
779  return AVERROR_PATCHWELCOME;
780  }
781 
782  // Lossless frame header length
783  header_size = get_bits(&s->gb, 8) + 1;
784 
785  // Check CRC
786  if (ff_dca_check_crc(s->avctx, &s->gb, 32, header_size * 8)) {
787  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
788  return AVERROR_INVALIDDATA;
789  }
790 
791  // Number of bits used to read frame size
792  frame_size_nbits = get_bits(&s->gb, 5) + 1;
793 
794  // Number of bytes in a lossless frame
795  s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
796  if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
797  av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
798  return AVERROR_INVALIDDATA;
799  }
800  s->frame_size++;
801 
802  // Number of channels sets per frame
803  s->nchsets = get_bits(&s->gb, 4) + 1;
804  if (s->nchsets > DCA_XLL_CHSETS_MAX) {
805  avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
806  return AVERROR_PATCHWELCOME;
807  }
808 
809  // Number of segments per frame
810  nframesegs_log2 = get_bits(&s->gb, 4);
811  s->nframesegs = 1 << nframesegs_log2;
812  if (s->nframesegs > 1024) {
813  av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
814  return AVERROR_INVALIDDATA;
815  }
816 
817  // Samples in segment per one frequency band for the first channel set
818  // Maximum value is 256 for sampling frequencies <= 48 kHz
819  // Maximum value is 512 for sampling frequencies > 48 kHz
820  s->nsegsamples_log2 = get_bits(&s->gb, 4);
821  if (!s->nsegsamples_log2) {
822  av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
823  return AVERROR_INVALIDDATA;
824  }
825  s->nsegsamples = 1 << s->nsegsamples_log2;
826  if (s->nsegsamples > 512) {
827  av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
828  return AVERROR_INVALIDDATA;
829  }
830 
831  // Samples in frame per one frequency band for the first channel set
832  s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
833  s->nframesamples = 1 << s->nframesamples_log2;
834  if (s->nframesamples > 65536) {
835  av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
836  return AVERROR_INVALIDDATA;
837  }
838 
839  // Number of bits used to read segment size
840  s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
841 
842  // Presence of CRC16 within each frequency band
843  // 0 - No CRC16 within band
844  // 1 - CRC16 placed at the end of MSB0
845  // 2 - CRC16 placed at the end of MSB0 and LSB0
846  // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
847  s->band_crc_present = get_bits(&s->gb, 2);
848 
849  // MSB/LSB split flag
850  s->scalable_lsbs = get_bits1(&s->gb);
851 
852  // Channel position mask
853  s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
854 
855  // Fixed LSB width
856  if (s->scalable_lsbs)
857  s->fixed_lsb_width = get_bits(&s->gb, 4);
858  else
859  s->fixed_lsb_width = 0;
860 
861  // Reserved
862  // Byte align
863  // Header CRC16 protection
864  if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
865  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
866  return AVERROR_INVALIDDATA;
867  }
868 
869  return 0;
870 }
871 
873 {
874  return !c->primary_chset && c->dmix_embedded && c->hier_chset;
875 }
876 
878 {
879  if (c->hier_chset)
880  while (++c < &s->chset[s->nchsets])
881  if (is_hier_dmix_chset(c))
882  return c;
883 
884  return NULL;
885 }
886 
888 {
889  int i, j, *coeff_ptr = c->dmix_coeff;
890 
891  for (i = 0; i < c->hier_ofs; i++) {
892  int scale = o->dmix_scale[i];
893  int scale_inv = o->dmix_scale_inv[i];
894  c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
895  c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
896  for (j = 0; j < c->nchannels; j++) {
897  int coeff = mul16(*coeff_ptr, scale_inv);
898  *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
899  }
900  }
901 }
902 
904 {
905  DCAContext *dca = s->avctx->priv_data;
906  DCAXllChSet *c;
907  int i, ret;
908 
909  // Parse channel set headers
910  s->nfreqbands = 0;
911  s->nchannels = 0;
912  s->nreschsets = 0;
913  for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
914  c->hier_ofs = s->nchannels;
915  if ((ret = chs_parse_header(s, c, asset)) < 0)
916  return ret;
917  if (c->nfreqbands > s->nfreqbands)
918  s->nfreqbands = c->nfreqbands;
919  if (c->hier_chset)
920  s->nchannels += c->nchannels;
921  if (c->residual_encode != (1 << c->nchannels) - 1)
922  s->nreschsets++;
923  }
924 
925  // Pre-scale downmixing coefficients for all non-primary channel sets
926  for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
927  if (is_hier_dmix_chset(c)) {
929  if (o)
930  prescale_down_mix(c, o);
931  }
932  }
933 
934  // Determine number of active channel sets to decode
935  switch (dca->request_channel_layout) {
937  s->nactivechsets = 1;
938  break;
941  s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
942  break;
943  default:
944  s->nactivechsets = s->nchsets;
945  break;
946  }
947 
948  return 0;
949 }
950 
952 {
953  int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
954  DCAXllChSet *c;
955 
956  // Determine size of NAVI table
957  navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
958  if (navi_nb > 1024) {
959  av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
960  return AVERROR_INVALIDDATA;
961  }
962 
963  // Reallocate NAVI table
964  av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
965  if (!s->navi)
966  return AVERROR(ENOMEM);
967 
968  // Parse NAVI
969  navi_pos = get_bits_count(&s->gb);
970  navi_ptr = s->navi;
971  for (band = 0; band < s->nfreqbands; band++) {
972  for (seg = 0; seg < s->nframesegs; seg++) {
973  for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
974  int size = 0;
975  if (c->nfreqbands > band) {
976  size = get_bits_long(&s->gb, s->seg_size_nbits);
977  if (size < 0 || size >= s->frame_size) {
978  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
979  return AVERROR_INVALIDDATA;
980  }
981  size++;
982  }
983  *navi_ptr++ = size;
984  }
985  }
986  }
987 
988  // Byte align
989  // CRC16
990  skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
991  skip_bits(&s->gb, 16);
992 
993  // Check CRC
994  if (ff_dca_check_crc(s->avctx, &s->gb, navi_pos, get_bits_count(&s->gb))) {
995  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
996  return AVERROR_INVALIDDATA;
997  }
998 
999  return 0;
1000 }
1001 
1003 {
1004  int ret, chs, seg, band, navi_pos, *navi_ptr;
1005  DCAXllChSet *c;
1006 
1007  for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
1008  if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
1009  return ret;
1010  if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
1011  return ret;
1012  }
1013 
1014  navi_pos = get_bits_count(&s->gb);
1015  navi_ptr = s->navi;
1016  for (band = 0; band < s->nfreqbands; band++) {
1017  for (seg = 0; seg < s->nframesegs; seg++) {
1018  for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
1019  if (c->nfreqbands > band) {
1020  navi_pos += *navi_ptr * 8;
1021  if (navi_pos > s->gb.size_in_bits) {
1022  av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
1023  return AVERROR_INVALIDDATA;
1024  }
1025  if (chs < s->nactivechsets &&
1026  (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
1027  if (s->avctx->err_recognition & AV_EF_EXPLODE)
1028  return ret;
1029  chs_clear_band_data(s, c, band, seg);
1030  }
1031  skip_bits_long(&s->gb, navi_pos - get_bits_count(&s->gb));
1032  }
1033  navi_ptr++;
1034  }
1035  }
1036  }
1037 
1038  return 0;
1039 }
1040 
1042 {
1043  int ret;
1044 
1045  if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1046  return ret;
1047  if ((ret = parse_common_header(s)) < 0)
1048  return ret;
1049  if ((ret = parse_sub_headers(s, asset)) < 0)
1050  return ret;
1051  if ((ret = parse_navi_table(s)) < 0)
1052  return ret;
1053  if ((ret = parse_band_data(s)) < 0)
1054  return ret;
1055  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1056  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
1057  return AVERROR_INVALIDDATA;
1058  }
1059  return ret;
1060 }
1061 
1063 {
1064  s->pbr_length = 0;
1065  s->pbr_delay = 0;
1066 }
1067 
1068 static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
1069 {
1071  return AVERROR(ENOSPC);
1072 
1073  if (!s->pbr_buffer && !(s->pbr_buffer = av_malloc(DCA_XLL_PBR_BUFFER_MAX + AV_INPUT_BUFFER_PADDING_SIZE)))
1074  return AVERROR(ENOMEM);
1075 
1076  memcpy(s->pbr_buffer, data, size);
1077  s->pbr_length = size;
1078  s->pbr_delay = delay;
1079  return 0;
1080 }
1081 
1083 {
1084  int ret = parse_frame(s, data, size, asset);
1085 
1086  // If XLL packet data didn't start with a sync word, we must have jumped
1087  // right into the middle of PBR smoothing period
1088  if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
1089  // Skip to the next sync word in this packet
1090  data += asset->xll_sync_offset;
1091  size -= asset->xll_sync_offset;
1092 
1093  // If decoding delay is set, put the frame into PBR buffer and return
1094  // failure code. Higher level decoder is expected to switch to lossy
1095  // core decoding or mute its output until decoding delay expires.
1096  if (asset->xll_delay_nframes > 0) {
1097  if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
1098  return ret;
1099  return AVERROR(EAGAIN);
1100  }
1101 
1102  // No decoding delay, just parse the frame in place
1103  ret = parse_frame(s, data, size, asset);
1104  }
1105 
1106  if (ret < 0)
1107  return ret;
1108 
1109  if (s->frame_size > size)
1110  return AVERROR(EINVAL);
1111 
1112  // If the XLL decoder didn't consume full packet, start PBR smoothing period
1113  if (s->frame_size < size)
1114  if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
1115  return ret;
1116 
1117  return 0;
1118 }
1119 
1121 {
1122  int ret;
1123 
1124  if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
1125  ret = AVERROR(ENOSPC);
1126  goto fail;
1127  }
1128 
1129  memcpy(s->pbr_buffer + s->pbr_length, data, size);
1130  s->pbr_length += size;
1131 
1132  // Respect decoding delay after synchronization error
1133  if (s->pbr_delay > 0 && --s->pbr_delay)
1134  return AVERROR(EAGAIN);
1135 
1136  if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
1137  goto fail;
1138 
1139  if (s->frame_size > s->pbr_length) {
1140  ret = AVERROR(EINVAL);
1141  goto fail;
1142  }
1143 
1144  if (s->frame_size == s->pbr_length) {
1145  // End of PBR smoothing period
1146  clear_pbr(s);
1147  } else {
1148  s->pbr_length -= s->frame_size;
1149  memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
1150  }
1151 
1152  return 0;
1153 
1154 fail:
1155  // For now, throw out all PBR state on failure.
1156  // Perhaps we can be smarter and try to resync somehow.
1157  clear_pbr(s);
1158  return ret;
1159 }
1160 
1162 {
1163  int ret;
1164 
1165  if (s->hd_stream_id != asset->hd_stream_id) {
1166  clear_pbr(s);
1167  s->hd_stream_id = asset->hd_stream_id;
1168  }
1169 
1170  if (s->pbr_length)
1171  ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1172  else
1173  ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
1174 
1175  return ret;
1176 }
1177 
1178 static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1179 {
1180  int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
1181  DCAXllChSet *c;
1182 
1183  for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1184  if (!c->hier_chset)
1185  continue;
1186 
1187  av_assert1(band < c->nfreqbands);
1188  for (j = 0; j < c->nchannels; j++) {
1189  for (k = 0; k < o->nchannels; k++) {
1190  int coeff = *coeff_ptr++;
1191  if (coeff) {
1192  s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
1193  o->bands[band].msb_sample_buffer[k],
1194  coeff, s->nframesamples);
1195  if (band)
1196  s->dcadsp->dmix_sub(c->deci_history[j],
1197  o->deci_history[k],
1199  }
1200  }
1201  }
1202 
1203  nchannels += c->nchannels;
1204  if (nchannels >= o->hier_ofs)
1205  break;
1206  }
1207 }
1208 
1209 static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
1210 {
1211  int i, j, nchannels = 0;
1212  DCAXllChSet *c;
1213 
1214  for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1215  if (!c->hier_chset)
1216  continue;
1217 
1218  av_assert1(band < c->nfreqbands);
1219  for (j = 0; j < c->nchannels; j++) {
1220  int scale = o->dmix_scale[nchannels++];
1221  if (scale != (1 << 15)) {
1222  s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
1223  scale, s->nframesamples);
1224  if (band)
1225  s->dcadsp->dmix_scale(c->deci_history[j],
1226  scale, DCA_XLL_DECI_HISTORY_MAX);
1227  }
1228  }
1229 
1230  if (nchannels >= o->hier_ofs)
1231  break;
1232  }
1233 }
1234 
1235 // Clear all band data and replace non-residual encoded channels with lossy
1236 // counterparts
1238 {
1239  DCAContext *dca = s->avctx->priv_data;
1240  int band, ch;
1241 
1242  for (band = 0; band < c->nfreqbands; band++)
1243  chs_clear_band_data(s, c, band, -1);
1244 
1245  for (ch = 0; ch < c->nchannels; ch++) {
1246  if (!(c->residual_encode & (1 << ch)))
1247  continue;
1248  if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
1249  continue;
1250  c->residual_encode &= ~(1 << ch);
1251  }
1252 }
1253 
1255 {
1256  DCAContext *dca = s->avctx->priv_data;
1257  int ch, nsamples = s->nframesamples;
1258  DCAXllChSet *o;
1259 
1260  // Verify that core is compatible
1261  if (!(dca->packet & DCA_PACKET_CORE)) {
1262  av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
1263  return AVERROR(EINVAL);
1264  }
1265 
1266  if (c->freq != dca->core.output_rate) {
1267  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);
1268  return AVERROR_INVALIDDATA;
1269  }
1270 
1271  if (nsamples != dca->core.npcmsamples) {
1272  av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
1273  return AVERROR_INVALIDDATA;
1274  }
1275 
1276  // See if this channel set is downmixed and find the next channel set in
1277  // hierarchy. If downmixed, undo core pre-scaling before combining with
1278  // residual (residual is not scaled).
1280 
1281  // Reduce core bit width and combine with residual
1282  for (ch = 0; ch < c->nchannels; ch++) {
1283  int n, spkr, shift, round;
1284  int32_t *src, *dst;
1285 
1286  if (c->residual_encode & (1 << ch))
1287  continue;
1288 
1289  // Map this channel to core speaker
1290  spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
1291  if (spkr < 0) {
1292  av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
1293  return AVERROR_INVALIDDATA;
1294  }
1295 
1296  // Account for LSB width
1297  shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
1298  if (shift > 24) {
1299  av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
1300  return AVERROR_INVALIDDATA;
1301  }
1302 
1303  round = shift > 0 ? 1 << (shift - 1) : 0;
1304 
1305  src = dca->core.output_samples[spkr];
1306  dst = c->bands[0].msb_sample_buffer[ch];
1307  if (o) {
1308  // Undo embedded core downmix pre-scaling
1309  int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
1310  for (n = 0; n < nsamples; n++)
1311  dst[n] += (SUINT)clip23((mul16(src[n], scale_inv) + round) >> shift);
1312  } else {
1313  // No downmix scaling
1314  for (n = 0; n < nsamples; n++)
1315  dst[n] += (unsigned)((src[n] + round) >> shift);
1316  }
1317  }
1318 
1319  return 0;
1320 }
1321 
1323 {
1324  AVCodecContext *avctx = s->avctx;
1325  DCAContext *dca = avctx->priv_data;
1326  DCAExssAsset *asset = &dca->exss.assets[0];
1327  DCAXllChSet *p = &s->chset[0], *c;
1328  enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
1329  int i, j, k, ret, shift, nsamples, request_mask;
1330  int ch_remap[DCA_SPEAKER_COUNT];
1331 
1332  // Force lossy downmixed output during recovery
1333  if (dca->packet & DCA_PACKET_RECOVERY) {
1334  for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
1335  if (i < s->nactivechsets)
1337 
1338  if (!c->primary_chset)
1339  c->dmix_embedded = 0;
1340  }
1341 
1342  s->scalable_lsbs = 0;
1343  s->fixed_lsb_width = 0;
1344  }
1345 
1346  // Filter frequency bands for active channel sets
1347  s->output_mask = 0;
1348  for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
1349  chs_filter_band_data(s, c, 0);
1350 
1351  if (c->residual_encode != (1 << c->nchannels) - 1
1352  && (ret = combine_residual_frame(s, c)) < 0)
1353  return ret;
1354 
1355  if (s->scalable_lsbs)
1356  chs_assemble_msbs_lsbs(s, c, 0);
1357 
1358  if (c->nfreqbands > 1) {
1359  chs_filter_band_data(s, c, 1);
1360  chs_assemble_msbs_lsbs(s, c, 1);
1361  }
1362 
1363  s->output_mask |= c->ch_mask;
1364  }
1365 
1366  // Undo hierarchial downmix and/or apply scaling
1367  for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
1368  if (!is_hier_dmix_chset(c))
1369  continue;
1370 
1371  if (i >= s->nactivechsets) {
1372  for (j = 0; j < c->nfreqbands; j++)
1373  if (c->bands[j].dmix_embedded)
1374  scale_down_mix(s, c, j);
1375  break;
1376  }
1377 
1378  for (j = 0; j < c->nfreqbands; j++)
1379  if (c->bands[j].dmix_embedded)
1380  undo_down_mix(s, c, j);
1381  }
1382 
1383  // Assemble frequency bands for active channel sets
1384  if (s->nfreqbands > 1) {
1385  for (i = 0; i < s->nactivechsets; i++)
1386  if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
1387  return ret;
1388  }
1389 
1390  // Normalize to regular 5.1 layout if downmixing
1391  if (dca->request_channel_layout) {
1392  if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
1393  s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
1394  s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
1395  }
1396  if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
1397  s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
1398  s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
1399  }
1400  }
1401 
1402  // Handle downmixing to stereo request
1404  && DCA_HAS_STEREO(s->output_mask) && p->dmix_embedded
1405  && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
1407  request_mask = DCA_SPEAKER_LAYOUT_STEREO;
1408  else
1409  request_mask = s->output_mask;
1410  if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
1411  return AVERROR(EINVAL);
1412 
1413  avctx->sample_rate = p->freq << (s->nfreqbands - 1);
1414 
1415  switch (p->storage_bit_res) {
1416  case 16:
1417  avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1418  shift = 16 - p->pcm_bit_res;
1419  break;
1420  case 20:
1421  case 24:
1422  avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1423  shift = 24 - p->pcm_bit_res;
1424  break;
1425  default:
1426  return AVERROR(EINVAL);
1427  }
1428 
1430  avctx->profile = FF_PROFILE_DTS_HD_MA;
1431  avctx->bit_rate = 0;
1432 
1433  frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
1434  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1435  return ret;
1436 
1437  // Downmix primary channel set to stereo
1438  if (request_mask != s->output_mask) {
1439  ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
1440  p->dmix_coeff, nsamples,
1441  s->output_mask);
1442  }
1443 
1444  for (i = 0; i < avctx->channels; i++) {
1445  int32_t *samples = s->output_samples[ch_remap[i]];
1446  if (frame->format == AV_SAMPLE_FMT_S16P) {
1447  int16_t *plane = (int16_t *)frame->extended_data[i];
1448  for (k = 0; k < nsamples; k++)
1449  plane[k] = av_clip_int16(samples[k] * (SUINT)(1 << shift));
1450  } else {
1451  int32_t *plane = (int32_t *)frame->extended_data[i];
1452  for (k = 0; k < nsamples; k++)
1453  plane[k] = clip23(samples[k] * (SUINT)(1 << shift)) * (1 << 8);
1454  }
1455  }
1456 
1457  if (!asset->one_to_one_map_ch_to_spkr) {
1459  matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1460  else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
1461  matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
1462  } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
1463  matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
1464  }
1465  if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
1466  return ret;
1467 
1468  return 0;
1469 }
1470 
1472 {
1473  clear_pbr(s);
1474 }
1475 
1477 {
1478  DCAXllChSet *c;
1479  int i, j;
1480 
1481  for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
1482  for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
1483  av_freep(&c->sample_buffer[j]);
1484  c->sample_size[j] = 0;
1485  }
1486  }
1487 
1488  av_freep(&s->navi);
1489  s->navi_size = 0;
1490 
1491  av_freep(&s->pbr_buffer);
1492  clear_pbr(s);
1493 }
dcamath.h
DCA_SPEAKER_Lss
@ DCA_SPEAKER_Lss
Definition: dca.h:81
find_next_hier_dmix_chset
static DCAXllChSet * find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:877
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
parse_dmix_coeffs
static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
DCA_XLL_DECI_HISTORY_MAX
#define DCA_XLL_DECI_HISTORY_MAX
Definition: dca_xll.h:38
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2225
norm16
static int32_t norm16(int64_t a)
Definition: dcamath.h:41
DCA_SPEAKER_LAYOUT_5POINT0
#define DCA_SPEAKER_LAYOUT_5POINT0
Definition: dca.h:129
n
int n
Definition: avisynth_c.h:760
DCACoreDecoder::npcmsamples
int npcmsamples
Number of PCM samples per channel.
Definition: dca_core.h:210
mul15
static int32_t mul15(int32_t a, int32_t b)
Definition: dcamath.h:46
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
DCA_SPEAKER_MASK_Rs
@ DCA_SPEAKER_MASK_Rs
Definition: dca.h:96
ch
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
chs_assemble_freq_bands
static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:729
get_array
static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
Definition: dca_xll.c:45
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
scale_down_mix
static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
Definition: dca_xll.c:1209
ff_dca_xll_parse
int ff_dca_xll_parse(DCAXllDecoder *s, uint8_t *data, DCAExssAsset *asset)
Definition: dca_xll.c:1161
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
DCA_SPEAKER_MASK_Lss
@ DCA_SPEAKER_MASK_Lss
Definition: dca.h:101
b
#define b
Definition: input.c:41
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
data
const char data[16]
Definition: mxf.c:91
DCA_SPEAKER_LAYOUT_STEREO
#define DCA_SPEAKER_LAYOUT_STEREO
Definition: dca.h:123
is_hier_dmix_chset
static int is_hier_dmix_chset(DCAXllChSet *c)
Definition: dca_xll.c:872
DCAContext::request_channel_layout
int request_channel_layout
Converted from avctx.request_channel_layout.
Definition: dcadec.h:64
ff_dca_seek_bits
static int ff_dca_seek_bits(GetBitContext *s, int p)
Definition: dcadec.h:89
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
DCAExssAsset::xll_size
int xll_size
Size of XLL data in extension substream.
Definition: dca_exss.h:63
chs_assemble_msbs_lsbs
static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
Definition: dca_xll.c:707
ff_dca_check_crc
static int ff_dca_check_crc(AVCodecContext *avctx, GetBitContext *s, int p1, int p2)
Definition: dcadec.h:75
DCAXllChSet::hier_ofs
int hier_ofs
Number of preceding channels in a hierarchy (M)
Definition: dca_xll.h:77
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
FF_DCA_DMIXTABLE_OFFSET
#define FF_DCA_DMIXTABLE_OFFSET
Definition: dcadata.h:71
DCAExssAsset::one_to_one_map_ch_to_spkr
int one_to_one_map_ch_to_spkr
One to one channel to speaker mapping flag.
Definition: dca_exss.h:37
parse_navi_table
static int parse_navi_table(DCAXllDecoder *s)
Definition: dca_xll.c:951
DCA_XLL_PBR_BUFFER_MAX
#define DCA_XLL_PBR_BUFFER_MAX
Definition: dca_xll.h:41
DCAExssAsset
Definition: dca_exss.h:29
DCA_PACKET_RECOVERY
#define DCA_PACKET_RECOVERY
Sync error recovery flag.
Definition: dcadec.h:43
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
undo_down_mix
static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
Definition: dca_xll.c:1178
DCAXllChSet::dmix_coeff
int dmix_coeff[DCA_XLL_DMIX_COEFFS_MAX]
Downmixing coefficients.
Definition: dca_xll.h:78
U
#define U(x)
Definition: vp56_arith.h:37
fail
#define fail()
Definition: checkasm.h:120
plane
int plane
Definition: avisynth_c.h:384
GetBitContext
Definition: get_bits.h:61
DCAXllBand
Definition: dca_xll.h:44
parse_common_header
static int parse_common_header(DCAXllDecoder *s)
Definition: dca_xll.c:765
chs_alloc_msb_band_data
static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:386
dcadata.h
clip23
static int32_t clip23(int32_t a)
Definition: dcamath.h:54
src
#define src
Definition: vp8dsp.c:254
DCAXllChSet::nchannels
int nchannels
Number of channels in the channel set (N)
Definition: dca_xll.h:66
DCA_SYNCWORD_XLL
#define DCA_SYNCWORD_XLL
Definition: dca_syncwords.h:31
DCAContext::exss
DCAExssParser exss
EXSS parser context.
Definition: dcadec.h:51
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
buf
void * buf
Definition: avisynth_c.h:766
av_cold
#define av_cold
Definition: attributes.h:84
AV_MATRIX_ENCODING_DOLBY
@ AV_MATRIX_ENCODING_DOLBY
Definition: channel_layout.h:116
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
DCACoreDecoder::output_rate
int output_rate
Output sample rate (1x or 2x header rate)
Definition: dca_core.h:211
DCA_DMIX_TYPE_LoRo
@ DCA_DMIX_TYPE_LoRo
Definition: dca.h:188
ff_dca_downmix_to_stereo_fixed
void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, int *coeff_l, int nsamples, int ch_mask)
Definition: dcadec.c:79
DCA_SPEAKER_Rss
@ DCA_SPEAKER_Rss
Definition: dca.h:81
DCA_XLL_ADAPT_PRED_ORDER_MAX
#define DCA_XLL_ADAPT_PRED_ORDER_MAX
Definition: dca_xll.h:37
DCAContext::packet
int packet
Packet flags.
Definition: dcadec.h:62
s
#define s(width, name)
Definition: cbs_vp9.c:257
DCA_SPEAKER_LAYOUT_5POINT1
#define DCA_SPEAKER_LAYOUT_5POINT1
Definition: dca.h:130
DCA_SPEAKER_Ls
@ DCA_SPEAKER_Ls
Definition: dca.h:79
DCA_REPR_TYPE_LhRh
@ DCA_REPR_TYPE_LhRh
Definition: dca.h:166
chs_get_lsb_width
static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
Definition: dca_xll.c:692
ff_dca_xll_refl_coeff
const uint16_t ff_dca_xll_refl_coeff[128]
Definition: dcadata.c:8705
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2796
DCAXllChSet::storage_bit_res
int storage_bit_res
Storage bit resolution (16 or 24)
Definition: dca_xll.h:69
mul16
static int32_t mul16(int32_t a, int32_t b)
Definition: dcamath.h:47
DCAXllChSet::dmix_type
int dmix_type
Primary channel set downmix type.
Definition: dca_xll.h:75
DCAXllChSet::dmix_embedded
int dmix_embedded
Downmix already performed by encoder.
Definition: dca_xll.h:74
dcadec.h
parse_frame
static int parse_frame(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
Definition: dca_xll.c:1041
chs_alloc_lsb_band_data
static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:409
dca_syncwords.h
int32_t
int32_t
Definition: audio_convert.c:194
ff_dca_set_channel_layout
int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
Definition: dcadec.c:32
AVMatrixEncoding
AVMatrixEncoding
Definition: channel_layout.h:114
AV_MATRIX_ENCODING_DOLBYHEADPHONE
@ AV_MATRIX_ENCODING_DOLBYHEADPHONE
Definition: channel_layout.h:121
parse_frame_pbr
static int parse_frame_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
Definition: dca_xll.c:1120
clear_pbr
static void clear_pbr(DCAXllDecoder *s)
Definition: dca_xll.c:1062
DCA_HAS_STEREO
#define DCA_HAS_STEREO(mask)
Definition: dca.h:134
DCACoreDecoder::output_samples
int32_t * output_samples[DCA_SPEAKER_COUNT]
PCM output for fixed point mode.
Definition: dca_core.h:203
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
ff_dca_dmix_primary_nch
const uint8_t ff_dca_dmix_primary_nch[8]
Definition: dcadata.c:45
DCA_SPEAKER_Rs
@ DCA_SPEAKER_Rs
Definition: dca.h:80
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1615
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
DCA_XLL_SAMPLE_BUFFERS_MAX
#define DCA_XLL_SAMPLE_BUFFERS_MAX
Definition: dca_xll.h:42
get_rice
static int get_rice(GetBitContext *gb, int k)
Definition: dca_xll.c:39
parse_frame_no_pbr
static int parse_frame_no_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
Definition: dca_xll.c:1082
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2705
DCA_PACKET_CORE
#define DCA_PACKET_CORE
Definition: dcadec.h:37
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
DCAExssAsset::xll_offset
int xll_offset
Offset to XLL data from start of substream.
Definition: dca_exss.h:62
ff_dca_inv_dmixtable
const uint32_t ff_dca_inv_dmixtable[FF_DCA_INV_DMIXTABLE_SIZE]
Definition: dcadata.c:8676
combine_residual_frame
static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:1254
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1965
ff_dca_dmixtable
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition: dcadata.c:8642
ff_dca_xll_band_coeff
const int32_t ff_dca_xll_band_coeff[20]
Definition: dcadata.c:8724
copy_to_pbr
static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
Definition: dca_xll.c:1068
AV_MATRIX_ENCODING_NONE
@ AV_MATRIX_ENCODING_NONE
Definition: channel_layout.h:115
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
size
int size
Definition: twinvq_data.h:11134
chs_parse_band_data
static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
Definition: dca_xll.c:442
DCAExssAsset::xll_delay_nframes
int xll_delay_nframes
Initial XLL decoding delay in frames.
Definition: dca_exss.h:65
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
DCA_SPEAKER_MASK_Rss
@ DCA_SPEAKER_MASK_Rss
Definition: dca.h:102
DCA_SPEAKER_R
@ DCA_SPEAKER_R
Definition: dca.h:79
unary.h
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
chs_filter_band_data
static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
Definition: dca_xll.c:627
DCAXllChSet
Definition: dca_xll.h:64
DCAContext
Definition: dcadec.h:46
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
DCA_XLL_CHANNELS_MAX
#define DCA_XLL_CHANNELS_MAX
Definition: dca_xll.h:35
DCAXllBand::msb_sample_buffer
int32_t * msb_sample_buffer[DCA_XLL_CHANNELS_MAX]
MSB sample buffer pointers.
Definition: dca_xll.h:60
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
DCAXllChSet::pcm_bit_res
int pcm_bit_res
PCM bit resolution (variable)
Definition: dca_xll.h:68
SUINT
#define SUINT
Definition: dct32_template.c:30
uint8_t
uint8_t
Definition: audio_convert.c:194
DCA_DMIX_TYPE_LtRt
@ DCA_DMIX_TYPE_LtRt
Definition: dca.h:189
DCAExssAsset::xll_sync_offset
int xll_sync_offset
Number of bytes offset to XLL sync.
Definition: dca_exss.h:66
DCAExssParser::assets
DCAExssAsset assets[1]
Audio asset descriptors.
Definition: dca_exss.h:87
DCA_DMIX_TYPE_COUNT
@ DCA_DMIX_TYPE_COUNT
Definition: dca.h:195
DCAXllChSet::deci_history
int32_t deci_history[DCA_XLL_CHANNELS_MAX][DCA_XLL_DECI_HISTORY_MAX]
Decimator history for frequency band 1.
Definition: dca_xll.h:98
ff_dca_xll_filter_frame
int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
Definition: dca_xll.c:1322
ff_dca_core_map_spkr
static int ff_dca_core_map_spkr(DCACoreDecoder *core, int spkr)
Definition: dca_core.h:216
DCAXllChSet::dmix_scale_inv
int dmix_scale_inv[DCA_XLL_DMIX_SCALES_MAX]
Inverse downmixing scales.
Definition: dca_xll.h:80
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
ret
ret
Definition: filter_design.txt:187
DCAXllDecoder
Definition: dca_xll.h:105
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
DCAXllChSet::nfreqbands
int nfreqbands
Number of frequency bands (1 or 2)
Definition: dca_xll.h:84
DCAXllChSet::bands
DCAXllBand bands[DCA_XLL_BANDS_MAX]
Frequency bands.
Definition: dca_xll.h:87
DCA_XLL_CHSETS_MAX
#define DCA_XLL_CHSETS_MAX
Definition: dca_xll.h:34
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:790
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
DCAExssAsset::xll_sync_present
int xll_sync_present
XLL sync word present flag.
Definition: dca_exss.h:64
FF_DCA_DMIXTABLE_SIZE
#define FF_DCA_DMIXTABLE_SIZE
Definition: dcadata.h:69
DCA_REPR_TYPE_LtRt
@ DCA_REPR_TYPE_LtRt
Definition: dca.h:165
DCAXllChSet::dmix_scale
int dmix_scale[DCA_XLL_DMIX_SCALES_MAX]
Downmixing scales.
Definition: dca_xll.h:79
ff_dca_sampling_freqs
const uint32_t ff_dca_sampling_freqs[16]
Definition: dca.c:41
get_rice_un
static int get_rice_un(GetBitContext *gb, int k)
Definition: dca_xll.c:33
FF_DCA_INV_DMIXTABLE_SIZE
#define FF_DCA_INV_DMIXTABLE_SIZE
Definition: dcadata.h:70
prescale_down_mix
static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
Definition: dca_xll.c:887
chs_parse_header
static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
Definition: dca_xll.c:116
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:2898
get_linear_array
static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
Definition: dca_xll.c:53
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
parse_sub_headers
static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
Definition: dca_xll.c:903
DCAXllChSet::freq
int freq
Original sampling frequency (max. 96000 Hz)
Definition: dca_xll.h:70
shift
static int shift(int a, int b)
Definition: sonic.c:82
get_rice_array
static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
Definition: dca_xll.c:63
ff_side_data_update_matrix_encoding
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:134
DCA_SPEAKER_MASK_Ls
@ DCA_SPEAKER_MASK_Ls
Definition: dca.h:95
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
FF_PROFILE_DTS_HD_MA
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:2924
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_fast_malloc
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:500
ff_dca_xll_flush
av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
Definition: dca_xll.c:1471
force_lossy_output
static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
Definition: dca_xll.c:1237
DCAContext::core
DCACoreDecoder core
Core decoder context.
Definition: dcadec.h:50
DCA_SPEAKER_COUNT
@ DCA_SPEAKER_COUNT
Definition: dca.h:88
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
DCA_SPEAKER_L
@ DCA_SPEAKER_L
Definition: dca.h:79
get_sbits_long
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:590
chs_clear_band_data
static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
Definition: dca_xll.c:599
int
int
Definition: ffmpeg_filter.c:191
get_linear
static int get_linear(GetBitContext *gb, int n)
Definition: dca_xll.c:27
DCAExssAsset::representation_type
int representation_type
Representation type.
Definition: dca_exss.h:42
DCAExssAsset::hd_stream_id
int hd_stream_id
DTS-HD stream ID.
Definition: dca_exss.h:68
parse_band_data
static int parse_band_data(DCAXllDecoder *s)
Definition: dca_xll.c:1002
ff_dca_xll_close
av_cold void ff_dca_xll_close(DCAXllDecoder *s)
Definition: dca_xll.c:1476