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