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