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