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