FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dca_xll.c
Go to the documentation of this file.
1 /*
2  * DCA XLL extension
3  *
4  * Copyright (C) 2012 Paul B Mahol
5  * Copyright (C) 2014 Niels Möller
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "libavutil/attributes.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 
28 #include "avcodec.h"
29 #include "dca.h"
30 #include "dcadata.h"
31 #include "get_bits.h"
32 #include "unary.h"
33 
34 /* Sign as bit 0 */
35 static inline int get_bits_sm(GetBitContext *s, unsigned n)
36 {
37  int x = get_bits(s, n);
38  if (x & 1)
39  return -(x >> 1) - 1;
40  else
41  return x >> 1;
42 }
43 
44 /* Return -1 on error. */
46 {
47  unsigned code = get_bits(&s->gb, 9);
48  int32_t sign = (int32_t) (code >> 8) - 1;
49  unsigned idx = code & 0xff;
51  if (idx >= FF_DCA_DMIXTABLE_SIZE) {
53  "XLL: Invalid channel set downmix code %x\n", code);
54  return -1;
55  } else if (!inverse) {
56  return (ff_dca_dmixtable[idx] ^ sign) - sign;
57  } else if (idx < inv_offset) {
59  "XLL: Invalid channel set inverse downmix code %x\n", code);
60  return -1;
61  } else {
62  return (ff_dca_inv_dmixtable[idx - inv_offset] ^ sign) - sign;
63  }
64 }
65 
67 {
68  return get_dmix_coeff(s, 0);
69 }
70 
72 {
73  return get_dmix_coeff(s, 1);
74 }
75 
76 /* parse XLL header */
78 {
79  int hdr_pos, hdr_size;
81  int i, chset_index;
82 
83  /* get bit position of sync header */
84  hdr_pos = get_bits_count(&s->gb) - 32;
85 
86  version = get_bits(&s->gb, 4) + 1;
87  hdr_size = get_bits(&s->gb, 8) + 1;
88 
89  frame_size = get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1) + 1;
90 
91  s->xll_channels =
92  s->xll_residual_channels = 0;
93  s->xll_nch_sets = get_bits(&s->gb, 4) + 1;
94  s->xll_segments = 1 << get_bits(&s->gb, 4);
95  s->xll_log_smpl_in_seg = get_bits(&s->gb, 4);
97  s->xll_bits4seg_size = get_bits(&s->gb, 5) + 1;
98  s->xll_banddata_crc = get_bits(&s->gb, 2);
99  s->xll_scalable_lsb = get_bits1(&s->gb);
100  s->xll_bits4ch_mask = get_bits(&s->gb, 5) + 1;
101 
102  if (s->xll_scalable_lsb) {
103  s->xll_fixed_lsb_width = get_bits(&s->gb, 4);
104  if (s->xll_fixed_lsb_width)
106  "XLL: fixed lsb width = %d, non-zero not supported.\n",
108  }
109  /* skip to the end of the common header */
110  i = get_bits_count(&s->gb);
111  if (hdr_pos + hdr_size * 8 > i)
112  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
113 
114  for (chset_index = 0; chset_index < s->xll_nch_sets; chset_index++) {
115  XllChSetSubHeader *chset = &s->xll_chsets[chset_index];
116  hdr_pos = get_bits_count(&s->gb);
117  hdr_size = get_bits(&s->gb, 10) + 1;
118 
119  chset->channels = get_bits(&s->gb, 4) + 1;
120  chset->residual_encode = get_bits(&s->gb, chset->channels);
121  chset->bit_resolution = get_bits(&s->gb, 5) + 1;
122  chset->bit_width = get_bits(&s->gb, 5) + 1;
124  chset->samp_freq_interp = get_bits(&s->gb, 2);
125  chset->replacement_set = get_bits(&s->gb, 2);
126  if (chset->replacement_set)
127  chset->active_replace_set = get_bits(&s->gb, 1);
128 
129  if (s->one2one_map_chtospkr) {
130  chset->primary_ch_set = get_bits(&s->gb, 1);
131  chset->downmix_coeff_code_embedded = get_bits(&s->gb, 1);
132  if (chset->downmix_coeff_code_embedded) {
133  chset->downmix_embedded = get_bits(&s->gb, 1);
134  if (chset->primary_ch_set) {
135  chset->downmix_type = get_bits(&s->gb, 3);
136  if (chset->downmix_type > 6) {
138  "XLL: Invalid channel set downmix type\n");
139  return AVERROR_INVALIDDATA;
140  }
141  }
142  }
143  chset->hier_chset = get_bits(&s->gb, 1);
144 
145  if (chset->downmix_coeff_code_embedded) {
146  /* nDownmixCoeffs is specified as N * M. For a primary
147  * channel set, it appears that N = number of
148  * channels, and M is the number of downmix channels.
149  *
150  * For a non-primary channel set, N is specified as
151  * number of channels + 1, and M is derived from the
152  * channel set hierarchy, and at least in simple cases
153  * M is the number of channels in preceding channel
154  * sets. */
155  if (chset->primary_ch_set) {
156  static const char dmix_table[7] = { 1, 2, 2, 3, 3, 4, 4 };
157  chset->downmix_ncoeffs = chset->channels * dmix_table[chset->downmix_type];
158  } else
159  chset->downmix_ncoeffs = (chset->channels + 1) * s->xll_channels;
160 
163  "XLL: More than %d downmix coefficients",
165  return AVERROR_PATCHWELCOME;
166  } else if (chset->primary_ch_set) {
167  for (i = 0; i < chset->downmix_ncoeffs; i++)
168  if ((chset->downmix_coeffs[i] = dca_get_dmix_coeff(s)) == -1)
169  return AVERROR_INVALIDDATA;
170  } else {
171  unsigned c, r;
172  for (c = 0, i = 0; c < s->xll_channels; c++, i += chset->channels + 1) {
173  if ((chset->downmix_coeffs[i] = dca_get_inv_dmix_coeff(s)) == -1)
174  return AVERROR_INVALIDDATA;
175  for (r = 1; r <= chset->channels; r++) {
177  if (coeff == -1)
178  return AVERROR_INVALIDDATA;
179  chset->downmix_coeffs[i + r] =
180  (chset->downmix_coeffs[i] * (int64_t) coeff + (1 << 15)) >> 16;
181  }
182  }
183  }
184  }
185  chset->ch_mask_enabled = get_bits(&s->gb, 1);
186  if (chset->ch_mask_enabled)
187  chset->ch_mask = get_bits(&s->gb, s->xll_bits4ch_mask);
188  else
189  /* Skip speaker configuration bits */
190  skip_bits_long(&s->gb, 25 * chset->channels);
191  } else {
192  chset->primary_ch_set = 1;
193  chset->downmix_coeff_code_embedded = 0;
194  /* Spec: NumChHierChSet = 0, NumDwnMixCodeCoeffs = 0, whatever that means. */
195  chset->mapping_coeffs_present = get_bits(&s->gb, 1);
196  if (chset->mapping_coeffs_present) {
197  avpriv_report_missing_feature(s->avctx, "XLL: mapping coefficients");
198  return AVERROR_PATCHWELCOME;
199  }
200  }
201  if (chset->sampling_frequency > 96000)
202  chset->num_freq_bands = 2 * (1 + get_bits(&s->gb, 1));
203  else
204  chset->num_freq_bands = 1;
205 
206  if (chset->num_freq_bands > 1) {
207  avpriv_report_missing_feature(s->avctx, "XLL: num_freq_bands > 1");
208  return AVERROR_PATCHWELCOME;
209  }
210 
211  if (get_bits(&s->gb, 1)) { /* pw_ch_decor_enabled */
212  int bits = av_ceil_log2(chset->channels);
213  for (i = 0; i < chset->channels; i++) {
214  unsigned j = get_bits(&s->gb, bits);
215  if (j >= chset->channels) {
217  "Original channel order value %u too large, only %d channels.\n",
218  j, chset->channels);
219  return AVERROR_INVALIDDATA;
220  }
221  chset->orig_chan_order[0][i] = j;
222  chset->orig_chan_order_inv[0][j] = i;
223  }
224  for (i = 0; i < chset->channels / 2; i++) {
225  if (get_bits(&s->gb, 1)) /* bChPFlag */
226  chset->pw_ch_pairs_coeffs[0][i] = get_bits_sm(&s->gb, 7);
227  else
228  chset->pw_ch_pairs_coeffs[0][i] = 0;
229  }
230  } else {
231  for (i = 0; i < chset->channels; i++)
232  chset->orig_chan_order[0][i] =
233  chset->orig_chan_order_inv[0][i] = i;
234  for (i = 0; i < chset->channels / 2; i++)
235  chset->pw_ch_pairs_coeffs[0][i] = 0;
236  }
237  /* Adaptive prediction order */
238  chset->adapt_order_max[0] = 0;
239  for (i = 0; i < chset->channels; i++) {
240  chset->adapt_order[0][i] = get_bits(&s->gb, 4);
241  if (chset->adapt_order_max[0] < chset->adapt_order[0][i])
242  chset->adapt_order_max[0] = chset->adapt_order[0][i];
243  }
244  /* Fixed prediction order, used in case the adaptive order
245  * above is zero */
246  for (i = 0; i < chset->channels; i++)
247  chset->fixed_order[0][i] =
248  chset->adapt_order[0][i] ? 0 : get_bits(&s->gb, 2);
249 
250  for (i = 0; i < chset->channels; i++) {
251  unsigned j;
252  for (j = 0; j < chset->adapt_order[0][i]; j++)
253  chset->lpc_refl_coeffs_q_ind[0][i][j] = get_bits(&s->gb, 8);
254  }
255 
256  if (s->xll_scalable_lsb) {
257  chset->lsb_fsize[0] = get_bits(&s->gb, s->xll_bits4seg_size);
258 
259  for (i = 0; i < chset->channels; i++)
260  chset->scalable_lsbs[0][i] = get_bits(&s->gb, 4);
261  for (i = 0; i < chset->channels; i++)
262  chset->bit_width_adj_per_ch[0][i] = get_bits(&s->gb, 4);
263  } else {
264  memset(chset->scalable_lsbs[0], 0,
265  chset->channels * sizeof(chset->scalable_lsbs[0][0]));
266  memset(chset->bit_width_adj_per_ch[0], 0,
267  chset->channels * sizeof(chset->bit_width_adj_per_ch[0][0]));
268  }
269 
270  s->xll_channels += chset->channels;
271  s->xll_residual_channels += chset->channels -
272  av_popcount(chset->residual_encode);
273 
274  /* FIXME: Parse header data for extra frequency bands. */
275 
276  /* Skip to end of channel set sub header. */
277  i = get_bits_count(&s->gb);
278  if (hdr_pos + 8 * hdr_size < i) {
280  "chset header too large, %d bits, should be <= %d bits\n",
281  i - hdr_pos, 8 * hdr_size);
282  return AVERROR_INVALIDDATA;
283  }
284  if (hdr_pos + 8 * hdr_size > i)
285  skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
286  }
287  return 0;
288 }
289 
290 /* parse XLL navigation table */
291 int ff_dca_xll_decode_navi(DCAContext *s, int asset_end)
292 {
293  int nbands, band, chset, seg, data_start;
294 
295  /* FIXME: Supports only a single frequency band */
296  nbands = 1;
297 
298  for (band = 0; band < nbands; band++) {
299  s->xll_navi.band_size[band] = 0;
300  for (seg = 0; seg < s->xll_segments; seg++) {
301  /* Note: The spec, ETSI TS 102 114 V1.4.1 (2012-09), says
302  * we should read a base value for segment_size from the
303  * stream, before reading the sizes of the channel sets.
304  * But that's apparently incorrect. */
305  s->xll_navi.segment_size[band][seg] = 0;
306 
307  for (chset = 0; chset < s->xll_nch_sets; chset++)
308  if (band < s->xll_chsets[chset].num_freq_bands) {
309  s->xll_navi.chset_size[band][seg][chset] =
310  get_bits(&s->gb, s->xll_bits4seg_size) + 1;
311  s->xll_navi.segment_size[band][seg] +=
312  s->xll_navi.chset_size[band][seg][chset];
313  }
315  }
316  }
317  /* Align to 8 bits and skip 16-bit CRC. */
318  skip_bits_long(&s->gb, 16 + ((-get_bits_count(&s->gb)) & 7));
319 
320  data_start = get_bits_count(&s->gb);
321  if (data_start + 8 * s->xll_navi.band_size[0] > asset_end) {
323  "XLL: Data in NAVI table exceeds containing asset\n"
324  "start: %d (bit), size %u (bytes), end %d (bit), error %u\n",
325  data_start, s->xll_navi.band_size[0], asset_end,
326  data_start + 8 * s->xll_navi.band_size[0] - asset_end);
327  return AVERROR_INVALIDDATA;
328  }
329  init_get_bits(&s->xll_navi.gb, s->gb.buffer + data_start / 8,
330  8 * s->xll_navi.band_size[0]);
331  return 0;
332 }
333 
334 static void dca_xll_inv_adapt_pred(int *samples, int nsamples, unsigned order,
335  const int *prev, const uint8_t *q_ind)
336 {
337  static const uint16_t table[0x81] = {
338  0, 3070, 5110, 7140, 9156, 11154, 13132, 15085,
339  17010, 18904, 20764, 22588, 24373, 26117, 27818, 29474,
340  31085, 32648, 34164, 35631, 37049, 38418, 39738, 41008,
341  42230, 43404, 44530, 45609, 46642, 47630, 48575, 49477,
342  50337, 51157, 51937, 52681, 53387, 54059, 54697, 55302,
343  55876, 56421, 56937, 57426, 57888, 58326, 58741, 59132,
344  59502, 59852, 60182, 60494, 60789, 61066, 61328, 61576,
345  61809, 62029, 62236, 62431, 62615, 62788, 62951, 63105,
346  63250, 63386, 63514, 63635, 63749, 63855, 63956, 64051,
347  64140, 64224, 64302, 64376, 64446, 64512, 64573, 64631,
348  64686, 64737, 64785, 64830, 64873, 64913, 64950, 64986,
349  65019, 65050, 65079, 65107, 65133, 65157, 65180, 65202,
350  65222, 65241, 65259, 65275, 65291, 65306, 65320, 65333,
351  65345, 65357, 65368, 65378, 65387, 65396, 65405, 65413,
352  65420, 65427, 65434, 65440, 65446, 65451, 65456, 65461,
353  65466, 65470, 65474, 65478, 65481, 65485, 65488, 65491,
354  65535, /* Final value is for the -128 corner case, see below. */
355  };
356  int c[DCA_XLL_AORDER_MAX];
357  int64_t s;
358  unsigned i, j;
359 
360  for (i = 0; i < order; i++) {
361  if (q_ind[i] & 1)
362  /* The index value 0xff corresponds to a lookup of entry 0x80 in
363  * the table, and no value is provided in the specification. */
364  c[i] = -table[(q_ind[i] >> 1) + 1];
365  else
366  c[i] = table[q_ind[i] >> 1];
367  }
368  /* The description in the spec is a bit convoluted. We can convert
369  * the reflected values to direct values in place, using a
370  * sequence of reflections operating on two values. */
371  for (i = 1; i < order; i++) {
372  /* i = 1: scale c[0]
373  * i = 2: reflect c[0] <-> c[1]
374  * i = 3: scale c[1], reflect c[0] <-> c[2]
375  * i = 4: reflect c[0] <-> c[3] reflect c[1] <-> c[2]
376  * ... */
377  if (i & 1)
378  c[i / 2] += ((int64_t) c[i] * c[i / 2] + 0x8000) >> 16;
379  for (j = 0; j < i / 2; j++) {
380  int r0 = c[j];
381  int r1 = c[i - j - 1];
382  c[j] += ((int64_t) c[i] * r1 + 0x8000) >> 16;
383  c[i - j - 1] += ((int64_t) c[i] * r0 + 0x8000) >> 16;
384  }
385  }
386  /* Apply predictor. */
387  /* NOTE: Processing samples in this order means that the
388  * predictor is applied to the newly reconstructed samples. */
389  if (prev) {
390  for (i = 0; i < order; i++) {
391  for (j = s = 0; j < i; j++)
392  s += (int64_t) c[j] * samples[i - 1 - j];
393  for (; j < order; j++)
394  s += (int64_t) c[j] * prev[DCA_XLL_AORDER_MAX + i - 1 - j];
395 
396  samples[i] -= av_clip_intp2((s + 0x8000) >> 16, 24);
397  }
398  }
399  for (i = order; i < nsamples; i++) {
400  for (j = s = 0; j < order; j++)
401  s += (int64_t) c[j] * samples[i - 1 - j];
402 
403  /* NOTE: Equations seem to imply addition, while the
404  * pseudocode seems to use subtraction.*/
405  samples[i] -= av_clip_intp2((s + 0x8000) >> 16, 24);
406  }
407 }
408 
410 {
411  /* FIXME: Decodes only the first frequency band. */
412  int seg, chset_i;
413 
414  /* Coding parameters for each channel set. */
415  struct coding_params {
416  int seg_type;
417  int rice_code_flag[16];
418  int pancAuxABIT[16];
419  int pancABIT0[16]; /* Not sure what this is */
420  int pancABIT[16]; /* Not sure what this is */
421  int nSamplPart0[16];
422  } param_state[16];
423 
424  GetBitContext *gb = &s->xll_navi.gb;
425  int *history;
426 
427  /* Layout: First the sample buffer for one segment per channel,
428  * followed by history buffers of DCA_XLL_AORDER_MAX samples for
429  * each channel. */
432  s->xll_channels * sizeof(*s->xll_sample_buf));
433  if (!s->xll_sample_buf)
434  return AVERROR(ENOMEM);
435 
436  history = s->xll_sample_buf + s->xll_smpl_in_seg * s->xll_channels;
437 
438  for (seg = 0; seg < s->xll_segments; seg++) {
439  unsigned in_channel;
440 
441  for (chset_i = in_channel = 0; chset_i < s->xll_nch_sets; chset_i++) {
442  /* The spec isn't very explicit, but I think the NAVI sizes are in bytes. */
443  int end_pos = get_bits_count(gb) +
444  8 * s->xll_navi.chset_size[0][seg][chset_i];
445  int i, j;
446  struct coding_params *params = &param_state[chset_i];
447  /* I think this flag means that we should keep seg_type and
448  * other parameters from the previous segment. */
449  int use_seg_state_code_param;
450  XllChSetSubHeader *chset = &s->xll_chsets[chset_i];
451  if (in_channel >= s->avctx->channels)
452  /* FIXME: Could go directly to next segment */
453  goto next_chset;
454 
455  if (s->avctx->sample_rate != chset->sampling_frequency) {
457  "XLL: unexpected chset sample rate %d, expected %d\n",
458  chset->sampling_frequency, s->avctx->sample_rate);
459  goto next_chset;
460  }
461  if (seg != 0)
462  use_seg_state_code_param = get_bits(gb, 1);
463  else
464  use_seg_state_code_param = 0;
465 
466  if (!use_seg_state_code_param) {
467  int num_param_sets, i;
468  unsigned bits4ABIT;
469 
470  params->seg_type = get_bits(gb, 1);
471  num_param_sets = params->seg_type ? 1 : chset->channels;
472 
473  if (chset->bit_width > 16) {
474  bits4ABIT = 5;
475  } else {
476  if (chset->bit_width > 8)
477  bits4ABIT = 4;
478  else
479  bits4ABIT = 3;
480  if (s->xll_nch_sets > 1)
481  bits4ABIT++;
482  }
483 
484  for (i = 0; i < num_param_sets; i++) {
485  params->rice_code_flag[i] = get_bits(gb, 1);
486  if (!params->seg_type && params->rice_code_flag[i] && get_bits(gb, 1))
487  params->pancAuxABIT[i] = get_bits(gb, bits4ABIT) + 1;
488  else
489  params->pancAuxABIT[i] = 0;
490  }
491 
492  for (i = 0; i < num_param_sets; i++) {
493  if (!seg) {
494  /* Parameters for part 1 */
495  params->pancABIT0[i] = get_bits(gb, bits4ABIT);
496  if (params->rice_code_flag[i] == 0 && params->pancABIT0[i] > 0)
497  /* For linear code */
498  params->pancABIT0[i]++;
499 
500  /* NOTE: In the spec, not indexed by band??? */
501  if (params->seg_type == 0)
502  params->nSamplPart0[i] = chset->adapt_order[0][i];
503  else
504  params->nSamplPart0[i] = chset->adapt_order_max[0];
505  } else
506  params->nSamplPart0[i] = 0;
507 
508  /* Parameters for part 2 */
509  params->pancABIT[i] = get_bits(gb, bits4ABIT);
510  if (params->rice_code_flag[i] == 0 && params->pancABIT[i] > 0)
511  /* For linear code */
512  params->pancABIT[i]++;
513  }
514  }
515  for (i = 0; i < chset->channels; i++) {
516  int param_index = params->seg_type ? 0 : i;
517  int part0 = params->nSamplPart0[param_index];
518  int bits = part0 ? params->pancABIT0[param_index] : 0;
519  int *sample_buf = s->xll_sample_buf +
520  (in_channel + i) * s->xll_smpl_in_seg;
521 
522  if (!params->rice_code_flag[param_index]) {
523  /* Linear code */
524  if (bits)
525  for (j = 0; j < part0; j++)
526  sample_buf[j] = get_bits_sm(gb, bits);
527  else
528  memset(sample_buf, 0, part0 * sizeof(sample_buf[0]));
529 
530  /* Second part */
531  bits = params->pancABIT[param_index];
532  if (bits)
533  for (j = part0; j < s->xll_smpl_in_seg; j++)
534  sample_buf[j] = get_bits_sm(gb, bits);
535  else
536  memset(sample_buf + part0, 0,
537  (s->xll_smpl_in_seg - part0) * sizeof(sample_buf[0]));
538  } else {
539  int aux_bits = params->pancAuxABIT[param_index];
540 
541  for (j = 0; j < part0; j++) {
542  /* FIXME: Is this identical to Golomb code? */
543  int t = get_unary(gb, 1, 33) << bits;
544  /* FIXME: Could move this test outside of the loop, for efficiency. */
545  if (bits)
546  t |= get_bits(gb, bits);
547  sample_buf[j] = (t & 1) ? -(t >> 1) - 1 : (t >> 1);
548  }
549 
550  /* Second part */
551  bits = params->pancABIT[param_index];
552 
553  /* Follow the spec's suggestion of using the
554  * buffer also to store the hybrid-rice flags. */
555  memset(sample_buf + part0, 0,
556  (s->xll_smpl_in_seg - part0) * sizeof(sample_buf[0]));
557 
558  if (aux_bits > 0) {
559  /* For hybrid rice encoding, some samples are linearly
560  * coded. According to the spec, "nBits4SamplLoci" bits
561  * are used for each index, but this value is not
562  * defined. I guess we should use log2(xll_smpl_in_seg)
563  * bits. */
564  int count = get_bits(gb, s->xll_log_smpl_in_seg);
565  av_log(s->avctx, AV_LOG_DEBUG, "aux count %d (bits %d)\n",
566  count, s->xll_log_smpl_in_seg);
567 
568  for (j = 0; j < count; j++)
569  sample_buf[get_bits(gb, s->xll_log_smpl_in_seg)] = 1;
570  }
571  for (j = part0; j < s->xll_smpl_in_seg; j++) {
572  if (!sample_buf[j]) {
573  int t = get_unary(gb, 1, 33);
574  if (bits)
575  t = (t << bits) | get_bits(gb, bits);
576  sample_buf[j] = (t & 1) ? -(t >> 1) - 1 : (t >> 1);
577  } else
578  sample_buf[j] = get_bits_sm(gb, aux_bits);
579  }
580  }
581  }
582 
583  for (i = 0; i < chset->channels; i++) {
584  unsigned adapt_order = chset->adapt_order[0][i];
585  int *sample_buf = s->xll_sample_buf +
586  (in_channel + i) * s->xll_smpl_in_seg;
587  int *prev = history + (in_channel + i) * DCA_XLL_AORDER_MAX;
588 
589  if (!adapt_order) {
590  unsigned order;
591  for (order = chset->fixed_order[0][i]; order > 0; order--) {
592  unsigned j;
593  for (j = 1; j < s->xll_smpl_in_seg; j++)
594  sample_buf[j] += sample_buf[j - 1];
595  }
596  } else
597  /* Inverse adaptive prediction, in place. */
599  adapt_order, seg ? prev : NULL,
600  chset->lpc_refl_coeffs_q_ind[0][i]);
601  memcpy(prev, sample_buf + s->xll_smpl_in_seg - DCA_XLL_AORDER_MAX,
602  DCA_XLL_AORDER_MAX * sizeof(*prev));
603  }
604  for (i = 1; i < chset->channels; i += 2) {
605  int coeff = chset->pw_ch_pairs_coeffs[0][i / 2];
606  if (coeff != 0) {
607  int *sample_buf = s->xll_sample_buf +
608  (in_channel + i) * s->xll_smpl_in_seg;
609  int *prev = sample_buf - s->xll_smpl_in_seg;
610  unsigned j;
611  for (j = 0; j < s->xll_smpl_in_seg; j++)
612  /* Shift is unspecified, but should apparently be 3. */
613  sample_buf[j] += ((int64_t) coeff * prev[j] + 4) >> 3;
614  }
615  }
616 
617  if (s->xll_scalable_lsb) {
618  int lsb_start = end_pos - 8 * chset->lsb_fsize[0] -
619  8 * (s->xll_banddata_crc & 2);
620  int done;
621  i = get_bits_count(gb);
622  if (i > lsb_start) {
624  "chset data lsb exceeds NAVI size, end_pos %d, lsb_start %d, pos %d\n",
625  end_pos, lsb_start, i);
626  return AVERROR_INVALIDDATA;
627  }
628  if (i < lsb_start)
629  skip_bits_long(gb, lsb_start - i);
630 
631  for (i = done = 0; i < chset->channels; i++) {
632  int bits = chset->scalable_lsbs[0][i];
633  if (bits > 0) {
634  /* The channel reordering is conceptually done
635  * before adding the lsb:s, so we need to do
636  * the inverse permutation here. */
637  unsigned pi = chset->orig_chan_order_inv[0][i];
638  int *sample_buf = s->xll_sample_buf +
639  (in_channel + pi) * s->xll_smpl_in_seg;
640  int adj = chset->bit_width_adj_per_ch[0][i];
641  int msb_shift = bits;
642  unsigned j;
643 
644  if (adj > 0)
645  msb_shift += adj - 1;
646 
647  for (j = 0; j < s->xll_smpl_in_seg; j++)
648  sample_buf[j] = (sample_buf[j] << msb_shift) +
649  (get_bits(gb, bits) << adj);
650 
651  done += bits * s->xll_smpl_in_seg;
652  }
653  }
654  if (done > 8 * chset->lsb_fsize[0]) {
656  "chset lsb exceeds lsb_size\n");
657  return AVERROR_INVALIDDATA;
658  }
659  }
660 
661  /* Store output. */
662  for (i = 0; i < chset->channels; i++) {
663  int *sample_buf = s->xll_sample_buf +
664  (in_channel + i) * s->xll_smpl_in_seg;
665  int shift = 1 - chset->bit_resolution;
666  int out_channel = chset->orig_chan_order[0][i];
667  float *out;
668 
669  /* XLL uses the channel order C, L, R, and we want L,
670  * R, C. FIXME: Generalize. */
671  if (chset->ch_mask_enabled &&
672  (chset->ch_mask & 7) == 7 && out_channel < 3)
673  out_channel = out_channel ? out_channel - 1 : 2;
674 
675  out_channel += in_channel;
676  if (out_channel >= s->avctx->channels)
677  continue;
678 
679  out = (float *) frame->extended_data[out_channel];
680  out += seg * s->xll_smpl_in_seg;
681 
682  /* NOTE: A one bit means residual encoding is *not* used. */
683  if ((chset->residual_encode >> i) & 1) {
684  /* Replace channel samples.
685  * FIXME: Most likely not the right thing to do. */
686  for (j = 0; j < s->xll_smpl_in_seg; j++)
687  out[j] = ldexpf(sample_buf[j], shift);
688  } else {
689  /* Add residual signal to core channel */
690  for (j = 0; j < s->xll_smpl_in_seg; j++)
691  out[j] += ldexpf(sample_buf[j], shift);
692  }
693  }
694 
695  if (chset->downmix_coeff_code_embedded &&
696  !chset->primary_ch_set && chset->hier_chset) {
697  /* Undo hierarchical downmix of earlier channels. */
698  unsigned mix_channel;
699  for (mix_channel = 0; mix_channel < in_channel; mix_channel++) {
700  float *mix_buf;
701  const int *col;
702  float coeff;
703  unsigned row;
704  /* Similar channel reorder C, L, R vs L, R, C reorder. */
705  if (chset->ch_mask_enabled &&
706  (chset->ch_mask & 7) == 7 && mix_channel < 3)
707  mix_buf = (float *) frame->extended_data[mix_channel ? mix_channel - 1 : 2];
708  else
709  mix_buf = (float *) frame->extended_data[mix_channel];
710 
711  mix_buf += seg * s->xll_smpl_in_seg;
712  col = &chset->downmix_coeffs[mix_channel * (chset->channels + 1)];
713 
714  /* Scale */
715  coeff = ldexpf(col[0], -16);
716  for (j = 0; j < s->xll_smpl_in_seg; j++)
717  mix_buf[j] *= coeff;
718 
719  for (row = 0;
720  row < chset->channels && in_channel + row < s->avctx->channels;
721  row++)
722  if (col[row + 1]) {
723  const float *new_channel =
724  (const float *) frame->extended_data[in_channel + row];
725  new_channel += seg * s->xll_smpl_in_seg;
726  coeff = ldexpf(col[row + 1], -15);
727  for (j = 0; j < s->xll_smpl_in_seg; j++)
728  mix_buf[j] -= coeff * new_channel[j];
729  }
730  }
731  }
732 
733 next_chset:
734  in_channel += chset->channels;
735  /* Skip to next channel set using the NAVI info. */
736  i = get_bits_count(gb);
737  if (i > end_pos) {
739  "chset data exceeds NAVI size\n");
740  return AVERROR_INVALIDDATA;
741  }
742  if (i < end_pos)
743  skip_bits_long(gb, end_pos - i);
744  }
745  }
746  return 0;
747 }
uint8_t adapt_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX]
Definition: dca.h:109
uint8_t adapt_order_max[DCA_XLL_FBANDS_MAX]
Definition: dca.h:107
int samp_freq_interp
sampling frequency interpolation multiplier
Definition: dca.h:86
#define NULL
Definition: coverity.c:32
int downmix_coeff_code_embedded
Definition: dca.h:90
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
int one2one_map_chtospkr
Definition: dca.h:278
int ff_dca_xll_decode_header(DCAContext *s)
Definition: dca_xll.c:77
int hier_chset
hierarchical channel set
Definition: dca.h:93
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
int xll_fixed_lsb_width
Definition: dca.h:265
const uint8_t * buffer
Definition: get_bits.h:55
int8_t bit_width_adj_per_ch[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX]
Definition: dca.h:118
static int32_t dca_get_dmix_coeff(DCAContext *s)
Definition: dca_xll.c:66
int ch_mask
Definition: dca.h:97
int xll_bits4ch_mask
channel position mask
Definition: dca.h:264
int version
Definition: avisynth_c.h:629
int primary_ch_set
Definition: dca.h:89
int ch_mask_enabled
Definition: dca.h:96
static int get_bits_sm(GetBitContext *s, unsigned n)
Definition: dca_xll.c:35
int mapping_coeffs_present
Definition: dca.h:98
#define DCA_XLL_DMIX_NCOEFFS_MAX
Definition: dca.h:60
Macro definitions for various function/variable attributes.
int xll_channels
total number of channels (in all channel sets)
Definition: dca.h:256
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
if()
Definition: avfilter.c:975
uint8_t bits
Definition: crc.c:295
int xll_nch_sets
number of channel sets per frame
Definition: dca.h:255
uint8_t
XllNavi xll_navi
Definition: dca.h:267
int xll_residual_channels
number of residual channels
Definition: dca.h:257
unsigned int xll_sample_buf_size
Definition: dca.h:269
static AVFrame * frame
int ff_dca_xll_decode_navi(DCAContext *s, int asset_end)
Definition: dca_xll.c:291
int8_t pw_ch_pairs_coeffs[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX/2]
Definition: dca.h:105
XllChSetSubHeader xll_chsets[DCA_XLL_CHSETS_MAX]
Definition: dca.h:266
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
bitstream reader API header.
AVCodecContext * avctx
Definition: dca.h:137
uint8_t orig_chan_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX]
Definition: dca.h:102
#define av_log(a,...)
unsigned band_size[DCA_XLL_FBANDS_MAX]
Definition: dca.h:123
uint8_t orig_chan_order_inv[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX]
Definition: dca.h:103
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static void dca_xll_inv_adapt_pred(int *samples, int nsamples, unsigned order, const int *prev, const uint8_t *q_ind)
Definition: dca_xll.c:334
int downmix_embedded
Definition: dca.h:91
#define AVERROR(e)
Definition: error.h:43
static const struct endianess table[]
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition: dcadata.c:8133
const char * r
Definition: vf_curves.c:107
int xll_log_smpl_in_seg
supposedly this is "nBits4SamplLoci"
Definition: dca.h:259
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
GLenum GLint * params
Definition: opengl_enc.c:114
int channels
number of channels in channel set, at most 16
Definition: dca.h:81
int downmix_type
Definition: dca.h:92
int8_t scalable_lsbs[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX]
Definition: dca.h:117
GLsizei count
Definition: opengl_enc.c:109
Libavcodec external API header.
int replacement_set
replacement channel set group
Definition: dca.h:87
common internal API header
int * xll_sample_buf
Definition: dca.h:268
int32_t
static int32_t dca_get_inv_dmix_coeff(DCAContext *s)
Definition: dca_xll.c:71
int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame)
Definition: dca_xll.c:409
int n
Definition: avisynth_c.h:547
unsigned chset_size[DCA_XLL_FBANDS_MAX][DCA_XLL_SEGMENTS_MAX][DCA_XLL_CHSETS_MAX]
Definition: dca.h:125
const uint32_t ff_dca_inv_dmixtable[FF_DCA_INV_DMIXTABLE_SIZE]
Definition: dcadata.c:8167
uint8_t fixed_order[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX]
Definition: dca.h:111
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int frame_size
Definition: mxfenc.c:1803
int sample_rate
samples per second
Definition: avcodec.h:1985
GetBitContext gb
Definition: dca.h:122
#define FF_DCA_INV_DMIXTABLE_SIZE
Definition: dcadata.h:54
#define ldexpf(x, exp)
Definition: libm.h:107
int xll_bits4seg_size
number of bits used to read segment size
Definition: dca.h:261
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:410
int sampling_frequency
sampling frequency
Definition: dca.h:85
int residual_encode
residual channel encoding
Definition: dca.h:82
int xll_segments
number of segments per frame
Definition: dca.h:258
GetBitContext gb
Definition: dca.h:226
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:337
const int32_t ff_dca_sampling_freqs[16]
Definition: dcadata.c:8209
int num_freq_bands
Definition: dca.h:99
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int xll_banddata_crc
presence of CRC16 within each frequency band
Definition: dca.h:262
static int32_t get_dmix_coeff(DCAContext *s, int inverse)
Definition: dca_xll.c:45
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:513
common internal and external API header
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:33
static double c[64]
int xll_smpl_in_seg
samples in segment per one frequency band for the first channel set
Definition: dca.h:260
#define FF_DCA_DMIXTABLE_SIZE
Definition: dcadata.h:53
unsigned segment_size[DCA_XLL_FBANDS_MAX][DCA_XLL_SEGMENTS_MAX]
Definition: dca.h:124
int channels
number of audio channels
Definition: avcodec.h:1986
int xll_scalable_lsb
Definition: dca.h:263
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int bit_width
original input sample bit-width
Definition: dca.h:84
static uint32_t inverse(uint32_t v)
find multiplicative inverse modulo 2 ^ 32
Definition: asfcrypt.c:35
int bit_resolution
input sample bit-width
Definition: dca.h:83
int downmix_ncoeffs
Definition: dca.h:94
int active_replace_set
current channel set is active channel set
Definition: dca.h:88
#define DCA_XLL_AORDER_MAX
Definition: dca.h:57
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
uint8_t lpc_refl_coeffs_q_ind[DCA_XLL_FBANDS_MAX][DCA_XLL_CHANNELS_MAX][DCA_XLL_AORDER_MAX]
Definition: dca.h:114
int lsb_fsize[DCA_XLL_FBANDS_MAX]
Definition: dca.h:116
int downmix_coeffs[DCA_XLL_DMIX_NCOEFFS_MAX]
Definition: dca.h:95
for(j=16;j >0;--j)
#define av_unused
Definition: attributes.h:118