Libav
dcadec.c
Go to the documentation of this file.
1 /*
2  * DCA compatible decoder
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  * Copyright (C) 2012 Paul B Mahol
8  * Copyright (C) 2014 Niels Möller
9  *
10  * This file is part of Libav.
11  *
12  * Libav is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * Libav is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with Libav; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 
27 #include <math.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 
31 #include "libavutil/attributes.h"
33 #include "libavutil/common.h"
34 #include "libavutil/float_dsp.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/samplefmt.h"
40 
41 #include "avcodec.h"
42 #include "dca.h"
43 #include "dca_syncwords.h"
44 #include "dcadata.h"
45 #include "dcadsp.h"
46 #include "dcahuff.h"
47 #include "fft.h"
48 #include "fmtconvert.h"
49 #include "get_bits.h"
50 #include "internal.h"
51 #include "mathops.h"
52 #include "profiles.h"
53 #include "put_bits.h"
54 #include "synth_filter.h"
55 
56 #if ARCH_ARM
57 # include "arm/dca.h"
58 #endif
59 
60 enum DCAMode {
61  DCA_MONO = 0,
72 };
73 
74 /* -1 are reserved or unknown */
75 static const int dca_ext_audio_descr_mask[] = {
77  -1,
80  -1,
81  -1,
83  -1,
84 };
85 
86 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
87  * Some compromises have been made for special configurations. Most configurations
88  * are never used so complete accuracy is not needed.
89  *
90  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
91  * S -> side, when both rear and back are configured move one of them to the side channel
92  * OV -> center back
93  * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
94  */
95 static const uint64_t dca_core_channel_layout[] = {
101  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER,
102  AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER,
103  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,
104  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
105 
106  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
108 
109  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
111 
112  AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
114 
118 
120  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
122 
124  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
126 
128  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
130 };
131 
132 #define DCA_DOLBY 101 /* FIXME */
133 
134 #define DCA_CHANNEL_BITS 6
135 #define DCA_CHANNEL_MASK 0x3F
136 
137 #define DCA_LFE 0x80
138 
139 #define HEADER_SIZE 14
140 
141 #define DCA_NSYNCAUX 0x9A1105A0
142 
144 typedef struct BitAlloc {
145  int offset;
146  int maxbits[8];
147  int wrap;
148  VLC vlc[8];
149 } BitAlloc;
150 
155 
157  int idx)
158 {
159  return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
160  ba->offset;
161 }
162 
163 static av_cold void dca_init_vlcs(void)
164 {
165  static int vlcs_initialized = 0;
166  int i, j, c = 14;
167  static VLC_TYPE dca_table[23622][2];
168 
169  if (vlcs_initialized)
170  return;
171 
172  dca_bitalloc_index.offset = 1;
173  dca_bitalloc_index.wrap = 2;
174  for (i = 0; i < 5; i++) {
175  dca_bitalloc_index.vlc[i].table = &dca_table[ff_dca_vlc_offs[i]];
176  dca_bitalloc_index.vlc[i].table_allocated = ff_dca_vlc_offs[i + 1] - ff_dca_vlc_offs[i];
177  init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
178  bitalloc_12_bits[i], 1, 1,
180  }
181  dca_scalefactor.offset = -64;
182  dca_scalefactor.wrap = 2;
183  for (i = 0; i < 5; i++) {
184  dca_scalefactor.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 5]];
185  dca_scalefactor.vlc[i].table_allocated = ff_dca_vlc_offs[i + 6] - ff_dca_vlc_offs[i + 5];
186  init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
187  scales_bits[i], 1, 1,
189  }
190  dca_tmode.offset = 0;
191  dca_tmode.wrap = 1;
192  for (i = 0; i < 4; i++) {
193  dca_tmode.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 10]];
194  dca_tmode.vlc[i].table_allocated = ff_dca_vlc_offs[i + 11] - ff_dca_vlc_offs[i + 10];
195  init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
196  tmode_bits[i], 1, 1,
198  }
199 
200  for (i = 0; i < 10; i++)
201  for (j = 0; j < 7; j++) {
202  if (!bitalloc_codes[i][j])
203  break;
204  dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
205  dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
206  dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[ff_dca_vlc_offs[c]];
207  dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = ff_dca_vlc_offs[c + 1] - ff_dca_vlc_offs[c];
208 
209  init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
210  bitalloc_sizes[i],
211  bitalloc_bits[i][j], 1, 1,
213  c++;
214  }
215  vlcs_initialized = 1;
216 }
217 
218 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
219 {
220  while (len--)
221  *dst++ = get_bits(gb, bits);
222 }
223 
224 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
225 {
226  int i, j;
227  static const uint8_t adj_table[4] = { 16, 18, 20, 23 };
228  static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
229  static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
230 
231  s->audio_header.total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
233 
236 
237  for (i = base_channel; i < s->audio_header.prim_channels; i++) {
238  s->audio_header.subband_activity[i] = get_bits(&s->gb, 5) + 2;
241  }
242  for (i = base_channel; i < s->audio_header.prim_channels; i++) {
243  s->audio_header.vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
246  }
247  get_array(&s->gb, s->audio_header.joint_intensity + base_channel,
248  s->audio_header.prim_channels - base_channel, 3);
249  get_array(&s->gb, s->audio_header.transient_huffman + base_channel,
250  s->audio_header.prim_channels - base_channel, 2);
251  get_array(&s->gb, s->audio_header.scalefactor_huffman + base_channel,
252  s->audio_header.prim_channels - base_channel, 3);
253  get_array(&s->gb, s->audio_header.bitalloc_huffman + base_channel,
254  s->audio_header.prim_channels - base_channel, 3);
255 
256  /* Get codebooks quantization indexes */
257  if (!base_channel)
259  for (j = 1; j < 11; j++)
260  for (i = base_channel; i < s->audio_header.prim_channels; i++)
261  s->audio_header.quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
262 
263  /* Get scale factor adjustment */
264  for (j = 0; j < 11; j++)
265  for (i = base_channel; i < s->audio_header.prim_channels; i++)
266  s->audio_header.scalefactor_adj[i][j] = 16;
267 
268  for (j = 1; j < 11; j++)
269  for (i = base_channel; i < s->audio_header.prim_channels; i++)
270  if (s->audio_header.quant_index_huffman[i][j] < thr[j])
271  s->audio_header.scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
272 
273  if (s->crc_present) {
274  /* Audio header CRC check */
275  get_bits(&s->gb, 16);
276  }
277 
278  s->current_subframe = 0;
279  s->current_subsubframe = 0;
280 
281  return 0;
282 }
283 
285 {
286  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
287 
288  /* Sync code */
289  skip_bits_long(&s->gb, 32);
290 
291  /* Frame header */
292  s->frame_type = get_bits(&s->gb, 1);
293  s->samples_deficit = get_bits(&s->gb, 5) + 1;
294  s->crc_present = get_bits(&s->gb, 1);
295  s->sample_blocks = get_bits(&s->gb, 7) + 1;
296  s->frame_size = get_bits(&s->gb, 14) + 1;
297  if (s->frame_size < 95)
298  return AVERROR_INVALIDDATA;
299  s->amode = get_bits(&s->gb, 6);
301  if (!s->sample_rate)
302  return AVERROR_INVALIDDATA;
303  s->bit_rate_index = get_bits(&s->gb, 5);
305  if (!s->bit_rate)
306  return AVERROR_INVALIDDATA;
307 
308  skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
309  s->dynrange = get_bits(&s->gb, 1);
310  s->timestamp = get_bits(&s->gb, 1);
311  s->aux_data = get_bits(&s->gb, 1);
312  s->hdcd = get_bits(&s->gb, 1);
313  s->ext_descr = get_bits(&s->gb, 3);
314  s->ext_coding = get_bits(&s->gb, 1);
315  s->aspf = get_bits(&s->gb, 1);
316  s->lfe = get_bits(&s->gb, 2);
317  s->predictor_history = get_bits(&s->gb, 1);
318 
319  if (s->lfe > 2) {
320  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
321  return AVERROR_INVALIDDATA;
322  }
323 
324  /* TODO: check CRC */
325  if (s->crc_present)
326  s->header_crc = get_bits(&s->gb, 16);
327 
328  s->multirate_inter = get_bits(&s->gb, 1);
329  s->version = get_bits(&s->gb, 4);
330  s->copy_history = get_bits(&s->gb, 2);
331  s->source_pcm_res = get_bits(&s->gb, 3);
332  s->front_sum = get_bits(&s->gb, 1);
333  s->surround_sum = get_bits(&s->gb, 1);
334  s->dialog_norm = get_bits(&s->gb, 4);
335 
336  /* FIXME: channels mixing levels */
337  s->output = s->amode;
338  if (s->lfe)
339  s->output |= DCA_LFE;
340 
341  /* Primary audio coding header */
342  s->audio_header.subframes = get_bits(&s->gb, 4) + 1;
343 
344  return dca_parse_audio_coding_header(s, 0);
345 }
346 
347 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
348 {
349  if (level < 5) {
350  /* huffman encoded */
351  value += get_bitalloc(gb, &dca_scalefactor, level);
352  value = av_clip(value, 0, (1 << log2range) - 1);
353  } else if (level < 8) {
354  if (level + 1 > log2range) {
355  skip_bits(gb, level + 1 - log2range);
356  value = get_bits(gb, log2range);
357  } else {
358  value = get_bits(gb, level + 1);
359  }
360  }
361  return value;
362 }
363 
364 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
365 {
366  /* Primary audio coding side information */
367  int j, k;
368 
369  if (get_bits_left(&s->gb) < 0)
370  return AVERROR_INVALIDDATA;
371 
372  if (!base_channel) {
373  s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
374  s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
375  }
376 
377  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
378  for (k = 0; k < s->audio_header.subband_activity[j]; k++)
379  s->dca_chan[j].prediction_mode[k] = get_bits(&s->gb, 1);
380  }
381 
382  /* Get prediction codebook */
383  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
384  for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
385  if (s->dca_chan[j].prediction_mode[k] > 0) {
386  /* (Prediction coefficient VQ address) */
387  s->dca_chan[j].prediction_vq[k] = get_bits(&s->gb, 12);
388  }
389  }
390  }
391 
392  /* Bit allocation index */
393  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
394  for (k = 0; k < s->audio_header.vq_start_subband[j]; k++) {
395  if (s->audio_header.bitalloc_huffman[j] == 6)
396  s->dca_chan[j].bitalloc[k] = get_bits(&s->gb, 5);
397  else if (s->audio_header.bitalloc_huffman[j] == 5)
398  s->dca_chan[j].bitalloc[k] = get_bits(&s->gb, 4);
399  else if (s->audio_header.bitalloc_huffman[j] == 7) {
401  "Invalid bit allocation index\n");
402  return AVERROR_INVALIDDATA;
403  } else {
404  s->dca_chan[j].bitalloc[k] =
405  get_bitalloc(&s->gb, &dca_bitalloc_index, s->audio_header.bitalloc_huffman[j]);
406  }
407 
408  if (s->dca_chan[j].bitalloc[k] > 26) {
409  ff_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
410  j, k, s->dca_chan[j].bitalloc[k]);
411  return AVERROR_INVALIDDATA;
412  }
413  }
414  }
415 
416  /* Transition mode */
417  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
418  for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
419  s->dca_chan[j].transition_mode[k] = 0;
420  if (s->subsubframes[s->current_subframe] > 1 &&
421  k < s->audio_header.vq_start_subband[j] && s->dca_chan[j].bitalloc[k] > 0) {
422  s->dca_chan[j].transition_mode[k] =
423  get_bitalloc(&s->gb, &dca_tmode, s->audio_header.transient_huffman[j]);
424  }
425  }
426  }
427 
428  if (get_bits_left(&s->gb) < 0)
429  return AVERROR_INVALIDDATA;
430 
431  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
432  const uint32_t *scale_table;
433  int scale_sum, log_size;
434 
435  memset(s->dca_chan[j].scale_factor, 0,
436  s->audio_header.subband_activity[j] * sizeof(s->dca_chan[j].scale_factor[0][0]) * 2);
437 
438  if (s->audio_header.scalefactor_huffman[j] == 6) {
439  scale_table = ff_dca_scale_factor_quant7;
440  log_size = 7;
441  } else {
442  scale_table = ff_dca_scale_factor_quant6;
443  log_size = 6;
444  }
445 
446  /* When huffman coded, only the difference is encoded */
447  scale_sum = 0;
448 
449  for (k = 0; k < s->audio_header.subband_activity[j]; k++) {
450  if (k >= s->audio_header.vq_start_subband[j] || s->dca_chan[j].bitalloc[k] > 0) {
451  scale_sum = get_scale(&s->gb, s->audio_header.scalefactor_huffman[j], scale_sum, log_size);
452  s->dca_chan[j].scale_factor[k][0] = scale_table[scale_sum];
453  }
454 
455  if (k < s->audio_header.vq_start_subband[j] && s->dca_chan[j].transition_mode[k]) {
456  /* Get second scale factor */
457  scale_sum = get_scale(&s->gb, s->audio_header.scalefactor_huffman[j], scale_sum, log_size);
458  s->dca_chan[j].scale_factor[k][1] = scale_table[scale_sum];
459  }
460  }
461  }
462 
463  /* Joint subband scale factor codebook select */
464  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
465  /* Transmitted only if joint subband coding enabled */
466  if (s->audio_header.joint_intensity[j] > 0)
467  s->dca_chan[j].joint_huff = get_bits(&s->gb, 3);
468  }
469 
470  if (get_bits_left(&s->gb) < 0)
471  return AVERROR_INVALIDDATA;
472 
473  /* Scale factors for joint subband coding */
474  for (j = base_channel; j < s->audio_header.prim_channels; j++) {
475  int source_channel;
476 
477  /* Transmitted only if joint subband coding enabled */
478  if (s->audio_header.joint_intensity[j] > 0) {
479  int scale = 0;
480  source_channel = s->audio_header.joint_intensity[j] - 1;
481 
482  /* When huffman coded, only the difference is encoded
483  * (is this valid as well for joint scales ???) */
484 
485  for (k = s->audio_header.subband_activity[j];
486  k < s->audio_header.subband_activity[source_channel]; k++) {
487  scale = get_scale(&s->gb, s->dca_chan[j].joint_huff, 64 /* bias */, 7);
488  s->dca_chan[j].joint_scale_factor[k] = scale; /*joint_scale_table[scale]; */
489  }
490 
491  if (!(s->debug_flag & 0x02)) {
493  "Joint stereo coding not supported\n");
494  s->debug_flag |= 0x02;
495  }
496  }
497  }
498 
499  /* Dynamic range coefficient */
500  if (!base_channel && s->dynrange)
501  s->dynrange_coef = get_bits(&s->gb, 8);
502 
503  /* Side information CRC check word */
504  if (s->crc_present) {
505  get_bits(&s->gb, 16);
506  }
507 
508  /*
509  * Primary audio data arrays
510  */
511 
512  /* VQ encoded high frequency subbands */
513  for (j = base_channel; j < s->audio_header.prim_channels; j++)
514  for (k = s->audio_header.vq_start_subband[j]; k < s->audio_header.subband_activity[j]; k++)
515  /* 1 vector -> 32 samples */
516  s->dca_chan[j].high_freq_vq[k] = get_bits(&s->gb, 10);
517 
518  /* Low frequency effect data */
519  if (!base_channel && s->lfe) {
520  /* LFE samples */
521  int lfe_samples = 2 * s->lfe * (4 + block_index);
522  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
523  float lfe_scale;
524 
525  for (j = lfe_samples; j < lfe_end_sample; j++) {
526  /* Signed 8 bits int */
527  s->lfe_data[j] = get_sbits(&s->gb, 8);
528  }
529 
530  /* Scale factor index */
531  skip_bits(&s->gb, 1);
533 
534  /* Quantization step size * scale factor */
535  lfe_scale = 0.035 * s->lfe_scale_factor;
536 
537  for (j = lfe_samples; j < lfe_end_sample; j++)
538  s->lfe_data[j] *= lfe_scale;
539  }
540 
541  return 0;
542 }
543 
544 static void qmf_32_subbands(DCAContext *s, int chans,
545  float samples_in[DCA_SUBBANDS][SAMPLES_PER_SUBBAND], float *samples_out,
546  float scale)
547 {
548  const float *prCoeff;
549 
550  int sb_act = s->audio_header.subband_activity[chans];
551 
552  scale *= sqrt(1 / 8.0);
553 
554  /* Select filter */
555  if (!s->multirate_inter) /* Non-perfect reconstruction */
557  else /* Perfect reconstruction */
558  prCoeff = ff_dca_fir_32bands_perfect;
559 
560  s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
561  s->dca_chan[chans].subband_fir_hist,
562  &s->dca_chan[chans].hist_index,
563  s->dca_chan[chans].subband_fir_noidea, prCoeff,
564  samples_out, s->raXin, scale);
565 }
566 
568 {
569  unsigned i, j;
570  QMF64_table *table = av_malloc(sizeof(*table));
571  if (!table)
572  return NULL;
573 
574  for (i = 0; i < 32; i++)
575  for (j = 0; j < 32; j++)
576  table->dct4_coeff[i][j] = cos((2 * i + 1) * (2 * j + 1) * M_PI / 128);
577  for (i = 0; i < 32; i++)
578  for (j = 0; j < 32; j++)
579  table->dct2_coeff[i][j] = cos((2 * i + 1) * j * M_PI / 64);
580 
581  /* FIXME: Is the factor 0.125 = 1/8 right? */
582  for (i = 0; i < 32; i++)
583  table->rcos[i] = 0.125 / cos((2 * i + 1) * M_PI / 256);
584  for (i = 0; i < 32; i++)
585  table->rsin[i] = -0.125 / sin((2 * i + 1) * M_PI / 256);
586 
587  return table;
588 }
589 
590 /* FIXME: Totally unoptimized. Based on the reference code and
591  * http://multimedia.cx/mirror/dca-transform.pdf, with guessed tweaks
592  * for doubling the size. */
593 static void qmf_64_subbands(DCAContext *s, int chans,
594  float samples_in[DCA_SUBBANDS_X96K][SAMPLES_PER_SUBBAND],
595  float *samples_out, float scale)
596 {
597  float raXin[64];
598  float A[32], B[32];
599  float *raX = s->dca_chan[chans].subband_fir_hist;
600  float *raZ = s->dca_chan[chans].subband_fir_noidea;
601  unsigned i, j, k, subindex;
602 
603  for (i = s->audio_header.subband_activity[chans]; i < DCA_SUBBANDS_X96K; i++)
604  raXin[i] = 0.0;
605  for (subindex = 0; subindex < SAMPLES_PER_SUBBAND; subindex++) {
606  for (i = 0; i < s->audio_header.subband_activity[chans]; i++)
607  raXin[i] = samples_in[i][subindex];
608 
609  for (k = 0; k < 32; k++) {
610  A[k] = 0.0;
611  for (i = 0; i < 32; i++)
612  A[k] += (raXin[2 * i] + raXin[2 * i + 1]) * s->qmf64_table->dct4_coeff[k][i];
613  }
614  for (k = 0; k < 32; k++) {
615  B[k] = raXin[0] * s->qmf64_table->dct2_coeff[k][0];
616  for (i = 1; i < 32; i++)
617  B[k] += (raXin[2 * i] + raXin[2 * i - 1]) * s->qmf64_table->dct2_coeff[k][i];
618  }
619  for (k = 0; k < 32; k++) {
620  raX[k] = s->qmf64_table->rcos[k] * (A[k] + B[k]);
621  raX[63 - k] = s->qmf64_table->rsin[k] * (A[k] - B[k]);
622  }
623 
624  for (i = 0; i < DCA_SUBBANDS_X96K; i++) {
625  float out = raZ[i];
626  for (j = 0; j < 1024; j += 128)
627  out += ff_dca_fir_64bands[j + i] * (raX[j + i] - raX[j + 63 - i]);
628  *samples_out++ = out * scale;
629  }
630 
631  for (i = 0; i < DCA_SUBBANDS_X96K; i++) {
632  float hist = 0.0;
633  for (j = 0; j < 1024; j += 128)
634  hist += ff_dca_fir_64bands[64 + j + i] * (-raX[i + j] - raX[j + 63 - i]);
635 
636  raZ[i] = hist;
637  }
638 
639  /* FIXME: Make buffer circular, to avoid this move. */
640  memmove(raX + 64, raX, (1024 - 64) * sizeof(*raX));
641  }
642 }
643 
644 static void lfe_interpolation_fir(DCAContext *s, const float *samples_in,
645  float *samples_out)
646 {
647  /* samples_in: An array holding decimated samples.
648  * Samples in current subframe starts from samples_in[0],
649  * while samples_in[-1], samples_in[-2], ..., stores samples
650  * from last subframe as history.
651  *
652  * samples_out: An array holding interpolated samples
653  */
654 
655  int idx;
656  const float *prCoeff;
657  int deciindex;
658 
659  /* Select decimation filter */
660  if (s->lfe == 1) {
661  idx = 1;
662  prCoeff = ff_dca_lfe_fir_128;
663  } else {
664  idx = 0;
666  prCoeff = ff_dca_lfe_xll_fir_64;
667  else
668  prCoeff = ff_dca_lfe_fir_64;
669  }
670  /* Interpolation */
671  for (deciindex = 0; deciindex < 2 * s->lfe; deciindex++) {
672  s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
673  samples_in++;
674  samples_out += 2 * 32 * (1 + idx);
675  }
676 }
677 
678 /* downmixing routines */
679 #define MIX_REAR1(samples, s1, rs, coef) \
680  samples[0][i] += samples[s1][i] * coef[rs][0]; \
681  samples[1][i] += samples[s1][i] * coef[rs][1];
682 
683 #define MIX_REAR2(samples, s1, s2, rs, coef) \
684  samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
685  samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
686 
687 #define MIX_FRONT3(samples, coef) \
688  t = samples[c][i]; \
689  u = samples[l][i]; \
690  v = samples[r][i]; \
691  samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
692  samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
693 
694 #define DOWNMIX_TO_STEREO(op1, op2) \
695  for (i = 0; i < 256; i++) { \
696  op1 \
697  op2 \
698  }
699 
700 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
701  float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
702  const int8_t *channel_mapping)
703 {
704  int c, l, r, sl, sr, s;
705  int i;
706  float t, u, v;
707 
708  switch (srcfmt) {
709  case DCA_MONO:
710  case DCA_4F2R:
711  av_log(NULL, 0, "Not implemented!\n");
712  break;
713  case DCA_CHANNEL:
714  case DCA_STEREO:
715  case DCA_STEREO_TOTAL:
716  case DCA_STEREO_SUMDIFF:
717  break;
718  case DCA_3F:
719  c = channel_mapping[0];
720  l = channel_mapping[1];
721  r = channel_mapping[2];
722  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
723  break;
724  case DCA_2F1R:
725  s = channel_mapping[2];
726  DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
727  break;
728  case DCA_3F1R:
729  c = channel_mapping[0];
730  l = channel_mapping[1];
731  r = channel_mapping[2];
732  s = channel_mapping[3];
733  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
734  MIX_REAR1(samples, s, 3, coef));
735  break;
736  case DCA_2F2R:
737  sl = channel_mapping[2];
738  sr = channel_mapping[3];
739  DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
740  break;
741  case DCA_3F2R:
742  c = channel_mapping[0];
743  l = channel_mapping[1];
744  r = channel_mapping[2];
745  sl = channel_mapping[3];
746  sr = channel_mapping[4];
747  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
748  MIX_REAR2(samples, sl, sr, 3, coef));
749  break;
750  }
751  if (lfe_present) {
752  int lf_buf = ff_dca_lfe_index[srcfmt];
753  int lf_idx = ff_dca_channels[srcfmt];
754  for (i = 0; i < 256; i++) {
755  samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
756  samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
757  }
758  }
759 }
760 
761 #ifndef decode_blockcodes
762 /* Very compact version of the block code decoder that does not use table
763  * look-up but is slightly slower */
764 static int decode_blockcode(int code, int levels, int32_t *values)
765 {
766  int i;
767  int offset = (levels - 1) >> 1;
768 
769  for (i = 0; i < 4; i++) {
770  int div = FASTDIV(code, levels);
771  values[i] = code - offset - div * levels;
772  code = div;
773  }
774 
775  return code;
776 }
777 
778 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
779 {
780  return decode_blockcode(code1, levels, values) |
781  decode_blockcode(code2, levels, values + 4);
782 }
783 #endif
784 
785 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
786 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
787 
788 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
789 {
790  int k, l;
791  int subsubframe = s->current_subsubframe;
792  const uint32_t *quant_step_table;
793 
794  /*
795  * Audio data
796  */
797 
798  /* Select quantization step size table */
799  if (s->bit_rate_index == 0x1f)
800  quant_step_table = ff_dca_lossless_quant;
801  else
802  quant_step_table = ff_dca_lossy_quant;
803 
804  for (k = base_channel; k < s->audio_header.prim_channels; k++) {
805  int32_t (*subband_samples)[8] = s->dca_chan[k].subband_samples[block_index];
806 
807  if (get_bits_left(&s->gb) < 0)
808  return AVERROR_INVALIDDATA;
809 
810  for (l = 0; l < s->audio_header.vq_start_subband[k]; l++) {
811  int m;
812 
813  /* Select the mid-tread linear quantizer */
814  int abits = s->dca_chan[k].bitalloc[l];
815 
816  uint32_t quant_step_size = quant_step_table[abits];
817 
818  /*
819  * Extract bits from the bit stream
820  */
821  if (!abits)
822  memset(subband_samples[l], 0, SAMPLES_PER_SUBBAND *
823  sizeof(subband_samples[l][0]));
824  else {
825  uint32_t rscale;
826  /* Deal with transients */
827  int sfi = s->dca_chan[k].transition_mode[l] &&
828  subsubframe >= s->dca_chan[k].transition_mode[l];
829  /* Determine quantization index code book and its type.
830  Select quantization index code book */
831  int sel = s->audio_header.quant_index_huffman[k][abits];
832 
833  rscale = (s->dca_chan[k].scale_factor[l][sfi] *
834  s->audio_header.scalefactor_adj[k][sel] + 8) >> 4;
835 
836  if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
837  if (abits <= 7) {
838  /* Block code */
839  int block_code1, block_code2, size, levels, err;
840 
841  size = abits_sizes[abits - 1];
842  levels = abits_levels[abits - 1];
843 
844  block_code1 = get_bits(&s->gb, size);
845  block_code2 = get_bits(&s->gb, size);
846  err = decode_blockcodes(block_code1, block_code2,
847  levels, subband_samples[l]);
848  if (err) {
850  "ERROR: block code look-up failed\n");
851  return AVERROR_INVALIDDATA;
852  }
853  } else {
854  /* no coding */
855  for (m = 0; m < SAMPLES_PER_SUBBAND; m++)
856  subband_samples[l][m] = get_sbits(&s->gb, abits - 3);
857  }
858  } else {
859  /* Huffman coded */
860  for (m = 0; m < SAMPLES_PER_SUBBAND; m++)
861  subband_samples[l][m] = get_bitalloc(&s->gb,
862  &dca_smpl_bitalloc[abits], sel);
863  }
864  s->dcadsp.dequantize(subband_samples[l], quant_step_size, rscale);
865  }
866  }
867 
868  for (l = 0; l < s->audio_header.vq_start_subband[k]; l++) {
869  int m;
870  /*
871  * Inverse ADPCM if in prediction mode
872  */
873  if (s->dca_chan[k].prediction_mode[l]) {
874  int n;
875  if (s->predictor_history)
876  subband_samples[l][0] += (ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][0] *
877  (int64_t)s->dca_chan[k].subband_samples_hist[l][3] +
878  ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][1] *
879  (int64_t)s->dca_chan[k].subband_samples_hist[l][2] +
880  ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][2] *
881  (int64_t)s->dca_chan[k].subband_samples_hist[l][1] +
882  ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][3] *
883  (int64_t)s->dca_chan[k].subband_samples_hist[l][0]) +
884  (1 << 12) >> 13;
885  for (m = 1; m < SAMPLES_PER_SUBBAND; m++) {
886  int64_t sum = ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][0] *
887  (int64_t)subband_samples[l][m - 1];
888  for (n = 2; n <= 4; n++)
889  if (m >= n)
890  sum += ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][n - 1] *
891  (int64_t)subband_samples[l][m - n];
892  else if (s->predictor_history)
893  sum += ff_dca_adpcm_vb[s->dca_chan[k].prediction_vq[l]][n - 1] *
894  (int64_t)s->dca_chan[k].subband_samples_hist[l][m - n + 4];
895  subband_samples[l][m] += (int32_t)(sum + (1 << 12) >> 13);
896  }
897  }
898 
899  }
900  /* Backup predictor history for adpcm */
901  for (l = 0; l < DCA_SUBBANDS; l++)
902  AV_COPY128(s->dca_chan[k].subband_samples_hist[l], &subband_samples[l][4]);
903 
904 
905  /*
906  * Decode VQ encoded high frequencies
907  */
909  if (!s->debug_flag & 0x01) {
911  "Stream with high frequencies VQ coding\n");
912  s->debug_flag |= 0x01;
913  }
914 
915  s->dcadsp.decode_hf(subband_samples, s->dca_chan[k].high_freq_vq,
917  subsubframe * SAMPLES_PER_SUBBAND,
918  s->dca_chan[k].scale_factor,
921  }
922  }
923 
924  /* Check for DSYNC after subsubframe */
925  if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
926  if (get_bits(&s->gb, 16) != 0xFFFF) {
927  av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
928  return AVERROR_INVALIDDATA;
929  }
930  }
931 
932  return 0;
933 }
934 
935 static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
936 {
937  int k;
938 
939  if (upsample) {
940  LOCAL_ALIGNED(32, float, samples, [DCA_SUBBANDS_X96K], [SAMPLES_PER_SUBBAND]);
941 
942  if (!s->qmf64_table) {
944  if (!s->qmf64_table)
945  return AVERROR(ENOMEM);
946  }
947 
948  /* 64 subbands QMF */
949  for (k = 0; k < s->audio_header.prim_channels; k++) {
950  int32_t (*subband_samples)[SAMPLES_PER_SUBBAND] =
951  s->dca_chan[k].subband_samples[block_index];
952 
953  s->fmt_conv.int32_to_float(samples[0], subband_samples[0],
955 
956  if (s->channel_order_tab[k] >= 0)
957  qmf_64_subbands(s, k, samples,
959  /* Upsampling needs a factor 2 here. */
960  M_SQRT2 / 32768.0);
961  }
962  } else {
963  /* 32 subbands QMF */
964  LOCAL_ALIGNED(32, float, samples, [DCA_SUBBANDS], [SAMPLES_PER_SUBBAND]);
965 
966  for (k = 0; k < s->audio_header.prim_channels; k++) {
967  int32_t (*subband_samples)[SAMPLES_PER_SUBBAND] =
968  s->dca_chan[k].subband_samples[block_index];
969 
970  s->fmt_conv.int32_to_float(samples[0], subband_samples[0],
972 
973  if (s->channel_order_tab[k] >= 0)
974  qmf_32_subbands(s, k, samples,
976  M_SQRT1_2 / 32768.0);
977  }
978  }
979 
980  /* Generate LFE samples for this subsubframe FIXME!!! */
981  if (s->lfe) {
982  float *samples = s->samples_chanptr[ff_dca_lfe_index[s->amode]];
984  s->lfe_data + 2 * s->lfe * (block_index + 4),
985  samples);
986  if (upsample) {
987  unsigned i;
988  /* Should apply the filter in Table 6-11 when upsampling. For
989  * now, just duplicate. */
990  for (i = 511; i > 0; i--) {
991  samples[2 * i] =
992  samples[2 * i + 1] = samples[i];
993  }
994  samples[1] = samples[0];
995  }
996  }
997 
998  /* FIXME: This downmixing is probably broken with upsample.
999  * Probably totally broken also with XLL in general. */
1000  /* Downmixing to Stereo */
1001  if (s->audio_header.prim_channels + !!s->lfe > 2 &&
1004  s->channel_order_tab);
1005  }
1006 
1007  return 0;
1008 }
1009 
1010 static int dca_subframe_footer(DCAContext *s, int base_channel)
1011 {
1012  int in, out, aux_data_count, aux_data_end, reserved;
1013  uint32_t nsyncaux;
1014 
1015  /*
1016  * Unpack optional information
1017  */
1018 
1019  /* presumably optional information only appears in the core? */
1020  if (!base_channel) {
1021  if (s->timestamp)
1022  skip_bits_long(&s->gb, 32);
1023 
1024  if (s->aux_data) {
1025  aux_data_count = get_bits(&s->gb, 6);
1026 
1027  // align (32-bit)
1028  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1029 
1030  aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1031 
1032  if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1033  av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1034  nsyncaux);
1035  return AVERROR_INVALIDDATA;
1036  }
1037 
1038  if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1040  "Auxiliary Decode Time Stamp Flag");
1041  // align (4-bit)
1042  skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1043  // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1044  skip_bits_long(&s->gb, 44);
1045  }
1046 
1047  if ((s->core_downmix = get_bits1(&s->gb))) {
1048  int am = get_bits(&s->gb, 3);
1049  switch (am) {
1050  case 0:
1052  break;
1053  case 1:
1055  break;
1056  case 2:
1058  break;
1059  case 3:
1061  break;
1062  case 4:
1064  break;
1065  case 5:
1067  break;
1068  case 6:
1070  break;
1071  default:
1073  "Invalid mode %d for embedded downmix coefficients\n",
1074  am);
1075  return AVERROR_INVALIDDATA;
1076  }
1077  for (out = 0; out < ff_dca_channels[s->core_downmix_amode]; out++) {
1078  for (in = 0; in < s->audio_header.prim_channels + !!s->lfe; in++) {
1079  uint16_t tmp = get_bits(&s->gb, 9);
1080  if ((tmp & 0xFF) > 241) {
1082  "Invalid downmix coefficient code %"PRIu16"\n",
1083  tmp);
1084  return AVERROR_INVALIDDATA;
1085  }
1086  s->core_downmix_codes[in][out] = tmp;
1087  }
1088  }
1089  }
1090 
1091  align_get_bits(&s->gb); // byte align
1092  skip_bits(&s->gb, 16); // nAUXCRC16
1093 
1094  /*
1095  * additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1096  *
1097  * Note: don't check for overreads, aux_data_count can't be trusted.
1098  */
1099  if ((reserved = (aux_data_end - get_bits_count(&s->gb))) > 0) {
1101  "Core auxiliary data reserved content");
1102  skip_bits_long(&s->gb, reserved);
1103  }
1104  }
1105 
1106  if (s->crc_present && s->dynrange)
1107  get_bits(&s->gb, 16);
1108  }
1109 
1110  return 0;
1111 }
1112 
1119 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1120 {
1121  int ret;
1122 
1123  /* Sanity check */
1124  if (s->current_subframe >= s->audio_header.subframes) {
1125  av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1127  return AVERROR_INVALIDDATA;
1128  }
1129 
1130  if (!s->current_subsubframe) {
1131  /* Read subframe header */
1132  if ((ret = dca_subframe_header(s, base_channel, block_index)))
1133  return ret;
1134  }
1135 
1136  /* Read subsubframe */
1137  if ((ret = dca_subsubframe(s, base_channel, block_index)))
1138  return ret;
1139 
1140  /* Update state */
1141  s->current_subsubframe++;
1143  s->current_subsubframe = 0;
1144  s->current_subframe++;
1145  }
1146  if (s->current_subframe >= s->audio_header.subframes) {
1147  /* Read subframe footer */
1148  if ((ret = dca_subframe_footer(s, base_channel)))
1149  return ret;
1150  }
1151 
1152  return 0;
1153 }
1154 
1155 static float dca_dmix_code(unsigned code)
1156 {
1157  int sign = (code >> 8) - 1;
1158  code &= 0xff;
1159  return ((ff_dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1160 }
1161 
1163 {
1164  DCAContext *s = avctx->priv_data;
1165  int core_ss_end, ret = 0;
1166 
1167  core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1168 
1169  /* only scan for extensions if ext_descr was unknown or indicated a
1170  * supported XCh extension */
1171  if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1172  /* if ext_descr was unknown, clear s->core_ext_mask so that the
1173  * extensions scan can fill it up */
1174  s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1175 
1176  /* extensions start at 32-bit boundaries into bitstream */
1177  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1178 
1179  while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1180  uint32_t bits = get_bits_long(&s->gb, 32);
1181  int i;
1182 
1183  switch (bits) {
1184  case DCA_SYNCWORD_XCH: {
1185  int ext_amode, xch_fsize;
1186 
1188 
1189  /* validate sync word using XCHFSIZE field */
1190  xch_fsize = show_bits(&s->gb, 10);
1191  if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1192  (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1193  continue;
1194 
1195  /* skip length-to-end-of-frame field for the moment */
1196  skip_bits(&s->gb, 10);
1197 
1198  s->core_ext_mask |= DCA_EXT_XCH;
1199 
1200  /* extension amode(number of channels in extension) should be 1 */
1201  /* AFAIK XCh is not used for more channels */
1202  if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1203  av_log(avctx, AV_LOG_ERROR,
1204  "XCh extension amode %d not supported!\n",
1205  ext_amode);
1206  continue;
1207  }
1208 
1209  /* much like core primary audio coding header */
1211 
1212  for (i = 0; i < (s->sample_blocks / 8); i++)
1213  if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1214  av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1215  continue;
1216  }
1217 
1218  s->xch_present = 1;
1219  break;
1220  }
1221  case DCA_SYNCWORD_XXCH:
1222  /* XXCh: extended channels */
1223  /* usually found either in core or HD part in DTS-HD HRA streams,
1224  * but not in DTS-ES which contains XCh extensions instead */
1226  break;
1227 
1228  case 0x1d95f262: {
1229  int fsize96 = show_bits(&s->gb, 12) + 1;
1230  if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1231  continue;
1232 
1233  av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1234  get_bits_count(&s->gb));
1235  skip_bits(&s->gb, 12);
1236  av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1237  av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1238 
1239  s->core_ext_mask |= DCA_EXT_X96;
1240  break;
1241  }
1242  }
1243 
1244  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1245  }
1246  } else {
1247  /* no supported extensions, skip the rest of the core substream */
1248  skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1249  }
1250 
1251  if (s->core_ext_mask & DCA_EXT_X96)
1253  else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1255 
1256  /* check for ExSS (HD part) */
1257  if (s->dca_buffer_size - s->frame_size > 32 &&
1260 
1261  return ret;
1262 }
1263 
1264 static int set_channel_layout(AVCodecContext *avctx, int channels, int num_core_channels)
1265 {
1266  DCAContext *s = avctx->priv_data;
1267  int i;
1268 
1269  if (s->amode < 16) {
1271 
1272  if (s->audio_header.prim_channels + !!s->lfe > 2 &&
1274  /*
1275  * Neither the core's auxiliary data nor our default tables contain
1276  * downmix coefficients for the additional channel coded in the XCh
1277  * extension, so when we're doing a Stereo downmix, don't decode it.
1278  */
1279  s->xch_disable = 1;
1280  }
1281 
1282  if (s->xch_present && !s->xch_disable) {
1284  if (s->lfe) {
1287  } else {
1289  }
1290  } else {
1291  channels = num_core_channels + !!s->lfe;
1292  s->xch_present = 0; /* disable further xch processing */
1293  if (s->lfe) {
1296  } else
1298  }
1299 
1300  if (channels < ff_dca_channels[s->amode] + !!s->lfe)
1301  return AVERROR_INVALIDDATA;
1302 
1303  if (channels > !!s->lfe &&
1304  s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1305  return AVERROR_INVALIDDATA;
1306 
1307  if (num_core_channels + !!s->lfe > 2 &&
1309  channels = 2;
1310  s->output = s->audio_header.prim_channels == 2 ? s->amode : DCA_STEREO;
1312 
1313  /* Stereo downmix coefficients
1314  *
1315  * The decoder can only downmix to 2-channel, so we need to ensure
1316  * embedded downmix coefficients are actually targeting 2-channel.
1317  */
1318  if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1320  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1321  /* Range checked earlier */
1322  s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1323  s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1324  }
1325  s->output = s->core_downmix_amode;
1326  } else {
1327  int am = s->amode & DCA_CHANNEL_MASK;
1330  "Invalid channel mode %d\n", am);
1331  return AVERROR_INVALIDDATA;
1332  }
1333  if (num_core_channels + !!s->lfe >
1335  avpriv_request_sample(s->avctx, "Downmixing %d channels",
1336  s->audio_header.prim_channels + !!s->lfe);
1337  return AVERROR_PATCHWELCOME;
1338  }
1339  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1340  s->downmix_coef[i][0] = ff_dca_default_coeffs[am][i][0];
1341  s->downmix_coef[i][1] = ff_dca_default_coeffs[am][i][1];
1342  }
1343  }
1344  ff_dlog(s->avctx, "Stereo downmix coeffs:\n");
1345  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1346  ff_dlog(s->avctx, "L, input channel %d = %f\n", i,
1347  s->downmix_coef[i][0]);
1348  ff_dlog(s->avctx, "R, input channel %d = %f\n", i,
1349  s->downmix_coef[i][1]);
1350  }
1351  ff_dlog(s->avctx, "\n");
1352  }
1353  } else {
1354  av_log(avctx, AV_LOG_ERROR, "Nonstandard configuration %d !\n", s->amode);
1355  return AVERROR_INVALIDDATA;
1356  }
1357 
1358  return 0;
1359 }
1360 
1365 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1366  int *got_frame_ptr, AVPacket *avpkt)
1367 {
1368  AVFrame *frame = data;
1369  const uint8_t *buf = avpkt->data;
1370  int buf_size = avpkt->size;
1371 
1372  int lfe_samples;
1373  int num_core_channels = 0;
1374  int i, ret;
1375  float **samples_flt;
1376  DCAContext *s = avctx->priv_data;
1377  int channels, full_channels;
1378  int upsample = 0;
1379 
1380  s->exss_ext_mask = 0;
1381  s->xch_present = 0;
1382 
1383  s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1386  av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1387  return AVERROR_INVALIDDATA;
1388  }
1389 
1390  if ((ret = dca_parse_frame_header(s)) < 0) {
1391  // seems like the frame is corrupt, try with the next one
1392  return ret;
1393  }
1394  // set AVCodec values with parsed data
1395  avctx->sample_rate = s->sample_rate;
1396  avctx->bit_rate = s->bit_rate;
1397 
1398  s->profile = FF_PROFILE_DTS;
1399 
1400  for (i = 0; i < (s->sample_blocks / SAMPLES_PER_SUBBAND); i++) {
1401  if ((ret = dca_decode_block(s, 0, i))) {
1402  av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1403  return ret;
1404  }
1405  }
1406 
1407  /* record number of core channels incase less than max channels are requested */
1408  num_core_channels = s->audio_header.prim_channels;
1409 
1410  if (s->ext_coding)
1412  else
1413  s->core_ext_mask = 0;
1414 
1415  ret = scan_for_extensions(avctx);
1416 
1417  avctx->profile = s->profile;
1418 
1419  full_channels = channels = s->audio_header.prim_channels + !!s->lfe;
1420 
1421  ret = set_channel_layout(avctx, channels, num_core_channels);
1422  if (ret < 0)
1423  return ret;
1424  avctx->channels = channels;
1425 
1426  /* get output buffer */
1427  frame->nb_samples = 256 * (s->sample_blocks / SAMPLES_PER_SUBBAND);
1428  if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
1429  int xll_nb_samples = s->xll_segments * s->xll_smpl_in_seg;
1430  /* Check for invalid/unsupported conditions first */
1431  if (s->xll_residual_channels > channels) {
1433  "DCA: too many residual channels (%d, core channels %d). Disabling XLL\n",
1434  s->xll_residual_channels, channels);
1436  } else if (xll_nb_samples != frame->nb_samples &&
1437  2 * frame->nb_samples != xll_nb_samples) {
1439  "DCA: unsupported upsampling (%d XLL samples, %d core samples). Disabling XLL\n",
1440  xll_nb_samples, frame->nb_samples);
1442  } else {
1443  if (2 * frame->nb_samples == xll_nb_samples) {
1444  av_log(s->avctx, AV_LOG_INFO,
1445  "XLL: upsampling core channels by a factor of 2\n");
1446  upsample = 1;
1447 
1448  frame->nb_samples = xll_nb_samples;
1449  // FIXME: Is it good enough to copy from the first channel set?
1450  avctx->sample_rate = s->xll_chsets[0].sampling_frequency;
1451  }
1452  /* If downmixing to stereo, don't decode additional channels.
1453  * FIXME: Using the xch_disable flag for this doesn't seem right. */
1454  if (!s->xch_disable)
1455  avctx->channels += s->xll_channels - s->xll_residual_channels;
1456  }
1457  }
1458 
1459  /* FIXME: This is an ugly hack, to just revert to the default
1460  * layout if we have additional channels. Need to convert the XLL
1461  * channel masks to libav channel_layout mask. */
1463  avctx->channel_layout = 0;
1464 
1465  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1466  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1467  return ret;
1468  }
1469  samples_flt = (float **) frame->extended_data;
1470 
1471  /* allocate buffer for extra channels if downmixing */
1472  if (avctx->channels < full_channels) {
1473  ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1474  frame->nb_samples,
1475  avctx->sample_fmt, 0);
1476  if (ret < 0)
1477  return ret;
1478 
1480  &s->extra_channels_buffer_size, ret);
1481  if (!s->extra_channels_buffer)
1482  return AVERROR(ENOMEM);
1483 
1486  full_channels - channels,
1487  frame->nb_samples, avctx->sample_fmt, 0);
1488  if (ret < 0)
1489  return ret;
1490  }
1491 
1492  /* filter to get final output */
1493  for (i = 0; i < (s->sample_blocks / SAMPLES_PER_SUBBAND); i++) {
1494  int ch;
1495  unsigned block = upsample ? 512 : 256;
1496  for (ch = 0; ch < channels; ch++)
1497  s->samples_chanptr[ch] = samples_flt[ch] + i * block;
1498  for (; ch < full_channels; ch++)
1499  s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * block;
1500 
1501  dca_filter_channels(s, i, upsample);
1502 
1503  /* If this was marked as a DTS-ES stream we need to subtract back- */
1504  /* channel from SL & SR to remove matrixed back-channel signal */
1505  if ((s->source_pcm_res & 1) && s->xch_present) {
1506  float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1507  float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1508  float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1509  s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1510  s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1511  }
1512  }
1513 
1514  /* update lfe history */
1515  lfe_samples = 2 * s->lfe * (s->sample_blocks / SAMPLES_PER_SUBBAND);
1516  for (i = 0; i < 2 * s->lfe * 4; i++)
1517  s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1518 
1520  ret = ff_dca_xll_decode_audio(s, frame);
1521  if (ret < 0)
1522  return ret;
1523  }
1524  /* AVMatrixEncoding
1525  *
1526  * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
1528  (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
1530  if (ret < 0)
1531  return ret;
1532 
1533  *got_frame_ptr = 1;
1534 
1535  return buf_size;
1536 }
1537 
1545 {
1546  DCAContext *s = avctx->priv_data;
1547 
1548  s->avctx = avctx;
1549  dca_init_vlcs();
1550 
1552  ff_mdct_init(&s->imdct, 6, 1, 1.0);
1554  ff_dcadsp_init(&s->dcadsp);
1555  ff_fmt_convert_init(&s->fmt_conv, avctx);
1556 
1557  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1558 
1559  /* allow downmixing to stereo */
1560  if (avctx->channels > 2 &&
1562  avctx->channels = 2;
1563 
1564  return 0;
1565 }
1566 
1568 {
1569  DCAContext *s = avctx->priv_data;
1570  ff_mdct_end(&s->imdct);
1572  av_freep(&s->xll_sample_buf);
1573  av_freep(&s->qmf64_table);
1574  return 0;
1575 }
1576 
1577 static const AVOption options[] = {
1578  { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
1579  { "disable_xll", "disable decoding of the XLL extension", offsetof(DCAContext, xll_disable), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
1580  { NULL },
1581 };
1582 
1583 static const AVClass dca_decoder_class = {
1584  .class_name = "DCA decoder",
1585  .item_name = av_default_item_name,
1586  .option = options,
1587  .version = LIBAVUTIL_VERSION_INT,
1588 };
1589 
1591  .name = "dca",
1592  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1593  .type = AVMEDIA_TYPE_AUDIO,
1594  .id = AV_CODEC_ID_DTS,
1595  .priv_data_size = sizeof(DCAContext),
1596  .init = dca_decode_init,
1598  .close = dca_decode_end,
1599  .capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
1600  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1603  .priv_class = &dca_decoder_class,
1604 };
int wrap
wrap for get_vlc2()
Definition: dcadec.c:147
const float ff_dca_lfe_xll_fir_64[256]
Definition: dcadata.c:7512
float, planar
Definition: samplefmt.h:71
int ext_descr
extension audio descriptor flag
Definition: dca.h:192
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
static const int8_t bitalloc_offsets[10]
Definition: dcahuff.h:988
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
DCAChan dca_chan[DCA_PRIM_CHANNELS_MAX]
Definition: dca.h:229
int crc_present
crc is present in the bitstream
Definition: dca.h:180
int size
static int set_channel_layout(AVCodecContext *avctx, int channels, int num_core_channels)
Definition: dcadec.c:1264
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
const int8_t ff_dca_channel_reorder_lfe_xch[16][9]
Definition: dcadata.c:8327
int timestamp
embedded time stamp flag
Definition: dca.h:189
int amode
audio channels arrangement
Definition: dca.h:183
AVOption.
Definition: opt.h:234
static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
Definition: dcadec.c:778
lossless extension in ExSS
Definition: dca.h:75
static const uint16_t tmode_codes[TMODE_COUNT][4]
Definition: dcahuff.h:31
Definition: vf_drawbox.c:37
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:228
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
const uint32_t ff_dca_lossy_quant[32]
Definition: dcadata.c:4183
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:187
FmtConvertContext fmt_conv
Definition: dca.h:288
static int dca_parse_frame_header(DCAContext *s)
Definition: dcadec.c:284
float dct2_coeff[32][32]
Definition: dca.h:128
void(* lfe_fir[2])(float *out, const float *in, const float *coefs)
Definition: dcadsp.h:31
int32_t subband_samples[DCA_BLOCKS_MAX][DCA_SUBBANDS][SAMPLES_PER_SUBBAND]
Definition: dca.h:150
int total_channels
number of channels including extensions
Definition: dca.h:145
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:269
int size
Definition: avcodec.h:1347
int prim_channels
number of primary audio channels
Definition: dca.h:146
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int samples_deficit
deficit sample count
Definition: dca.h:179
uint8_t core_downmix
embedded downmix coefficients available
Definition: dca.h:218
Definition: dcadec.c:66
#define DCA_MAX_FRAME_SIZE
Definition: dca.h:60
int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
Convert bitstream to one representation based on sync marker.
Definition: dca.c:35
int dynrange
embedded dynamic range flag
Definition: dca.h:188
int version
encoder software revision
Definition: dca.h:199
av_cold void ff_dcadsp_init(DCADSPContext *s)
Definition: dcadsp.c:120
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:889
#define FF_ARRAY_ELEMS(a)
int vq_start_subband[DCA_PRIM_CHANNELS_MAX]
high frequency vq start subband
Definition: dca.h:136
#define AV_CH_LAYOUT_STEREO
static BitAlloc dca_scalefactor
scalefactor VLCs
Definition: dcadec.c:153
#define init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition: vlc.h:38
static void lfe_interpolation_fir(DCAContext *s, const float *samples_in, float *samples_out)
Definition: dcadec.c:644
SynthFilterContext synth
Definition: dca.h:285
int profile
profile
Definition: avcodec.h:2880
AVCodec.
Definition: avcodec.h:3120
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:214
av_cold void ff_synth_filter_init(SynthFilterContext *c)
Definition: synth_filter.c:59
const float ff_dca_fir_32bands_nonperfect[512]
Definition: dcadata.c:6768
Macro definitions for various function/variable attributes.
int maxbits[8]
max bits in VLC
Definition: dcadec.c:146
const int8_t ff_dca_high_freq_vq[1024][32]
Definition: dcadata.c:4200
int32_t scale_factor[DCA_SUBBANDS][2]
scale factors (2 if transient)
Definition: dca.h:166
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
float * extra_channels[DCA_PRIM_CHANNELS_MAX+1]
Definition: dca.h:234
const float ff_dca_fir_64bands[1024]
Definition: dcadata.c:7580
static int16_t block[64]
Definition: dct.c:97
static int dca_subframe_footer(DCAContext *s, int base_channel)
Definition: dcadec.c:1010
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
int xll_channels
total number of channels (in all channel sets)
Definition: dca.h:258
static BitAlloc dca_tmode
transition mode VLCs
Definition: dcadec.c:152
#define DCA_MAX_EXSS_HEADER_SIZE
Definition: dca.h:61
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
Definition: float_dsp.h:54
#define MIX_FRONT3(samples, coef)
Definition: dcadec.c:687
int joint_scale_factor[DCA_SUBBANDS]
joint subband scale factors
Definition: dca.h:168
static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:364
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
Decode a dca frame block.
Definition: dcadec.c:1119
const uint32_t ff_dca_bit_rates[32]
Definition: dcadata.c:31
static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:788
uint8_t bits
Definition: crc.c:252
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
#define av_cold
Definition: attributes.h:66
int xch_base_channel
index of first (only) channel containing XCH data
Definition: dca.h:252
#define DCA_LFE
Definition: dcadec.c:137
AVOptions.
Definition: vf_drawbox.c:37
void(* decode_hf)(int32_t dst[DCA_SUBBANDS][SAMPLES_PER_SUBBAND], const int32_t vq_num[DCA_SUBBANDS], const int8_t hf_vq[1024][32], intptr_t vq_offset, int32_t scale[DCA_SUBBANDS][2], intptr_t start, intptr_t end)
Definition: dcadsp.h:38
int xll_residual_channels
number of residual channels
Definition: dca.h:259
int dca_buffer_size
how much data is in the dca_buffer
Definition: dca.h:239
#define MIX_REAR2(samples, s1, s2, rs, coef)
Definition: dcadec.c:683
static void get_array(GetBitContext *gb, int *dst, int len, int bits)
Definition: dcadec.c:218
96/24 extension in core substream
Definition: dca.h:68
static av_cold int dca_decode_end(AVCodecContext *avctx)
Definition: dcadec.c:1567
const uint16_t ff_dca_vlc_offs[63]
Definition: dcadata.c:8384
#define AV_CH_LOW_FREQUENCY
int header_crc
header crc check bytes
Definition: dca.h:197
DCAMode
Definition: dcadec.c:60
const char data[16]
Definition: mxf.c:70
XllChSetSubHeader xll_chsets[DCA_XLL_CHSETS_MAX]
Definition: dca.h:268
uint8_t * data
Definition: avcodec.h:1346
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:182
int sample_rate
audio sampling rate
Definition: dca.h:184
bitstream reader API header.
#define AV_CH_BACK_LEFT
AVCodecContext * avctx
Definition: dca.h:176
static BitAlloc dca_bitalloc_index
indexes for samples VLC select
Definition: dcadec.c:151
const int8_t ff_dca_channel_reorder_nolfe[16][9]
Definition: dcadata.c:8346
int lfe
low frequency effects flag
Definition: dca.h:195
uint32_t scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
scale factor adjustment
Definition: dca.h:142
#define B
Definition: huffyuv.h:49
static BitAlloc dca_smpl_bitalloc[11]
samples VLCs
Definition: dcadec.c:154
int bitalloc[DCA_SUBBANDS]
bit allocation index
Definition: dca.h:164
static const uint8_t bitalloc_sizes[10]
Definition: dcahuff.h:984
static void dca_downmix(float **samples, int srcfmt, int lfe_present, float coef[DCA_PRIM_CHANNELS_MAX+1][2], const int8_t *channel_mapping)
Definition: dcadec.c:700
#define r
Definition: input.c:51
#define DCA_CHANNEL_MASK
Definition: dcadec.c:135
int predictor_history
predictor history flag
Definition: dca.h:196
int dynrange_coef
dynamic range coefficient
Definition: dca.h:213
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:526
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX+1][4]
embedded downmix coefficients (9-bit codes)
Definition: dca.h:220
FFTContext imdct
Definition: dca.h:284
float raXin[32]
Definition: dca.h:227
int transition_mode[DCA_SUBBANDS]
transition mode (transients)
Definition: dca.h:165
const float ff_dca_lfe_fir_128[256]
Definition: dcadata.c:7442
#define MIX_REAR1(samples, s1, rs, coef)
Definition: dcadec.c:679
static const uint16_t bitalloc_12_codes[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:51
#define AVERROR(e)
Definition: error.h:43
int current_subsubframe
Definition: dca.h:245
const uint32_t ff_dca_lossless_quant[32]
Definition: dcadata.c:4191
int hdcd
source material is mastered in HDCD
Definition: dca.h:191
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition: dcadata.c:8116
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
const float ff_dca_lfe_fir_64[256]
Definition: dcadata.c:7299
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define FF_PROFILE_DTS_ES
Definition: avcodec.h:2896
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
static const uint8_t bitalloc_12_bits[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:64
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static int dca_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Main frame decoding function FIXME add arguments.
Definition: dcadec.c:1365
#define ff_mdct_init
Definition: fft.h:151
int debug_flag
used for suppressing repeated error messages output
Definition: dca.h:282
#define FFMAX(a, b)
Definition: common.h:64
float rcos[32]
Definition: dca.h:129
static void qmf_32_subbands(DCAContext *s, int chans, float samples_in[DCA_SUBBANDS][SAMPLES_PER_SUBBAND], float *samples_out, float scale)
Definition: dcadec.c:544
Definition: vlc.h:26
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2203
const int8_t * channel_order_tab
channel reordering table, lfe and non lfe
Definition: dca.h:241
float lfe_data[2 *DCA_LFE_MAX *(DCA_BLOCKS_MAX+4)]
Low frequency effect data.
Definition: dca.h:223
common internal API header
static const uint8_t tmode_bits[TMODE_COUNT][4]
Definition: dcahuff.h:38
int front_sum
front sum/difference flag
Definition: dca.h:202
int xch_disable
whether the XCh extension should be decoded or not
Definition: dca.h:253
int source_pcm_res
source pcm resolution
Definition: dca.h:201
int * xll_sample_buf
Definition: dca.h:270
int bit_rate
the average bitrate
Definition: avcodec.h:1473
audio channel layout utility functions
int subframes
number of subframes
Definition: dca.h:144
float subband_fir_noidea[64]
Definition: dca.h:159
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:788
#define FFMIN(a, b)
Definition: common.h:66
int32_t subband_samples_hist[DCA_SUBBANDS][4]
Definition: dca.h:153
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: avconv.c:1288
int surround_sum
surround sum/difference flag
Definition: dca.h:203
static av_cold void dca_init_vlcs(void)
Definition: dcadec.c:163
int32_t
uint8_t dca_buffer[DCA_MAX_FRAME_SIZE+DCA_MAX_EXSS_HEADER_SIZE+DCA_BUFFER_PADDING_SIZE]
Definition: dca.h:238
static const uint8_t scales_bits[SCALES_COUNT][129]
Definition: dcahuff.h:162
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:250
int prediction_mode[DCA_SUBBANDS]
prediction mode (ADPCM used or not)
Definition: dca.h:162
static const uint16_t *const bitalloc_codes[10][8]
Definition: dcahuff.h:1005
int multirate_inter
multirate interpolator switch
Definition: dca.h:198
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:493
#define FF_PROFILE_DTS
Definition: avcodec.h:2895
#define AV_CH_FRONT_LEFT_OF_CENTER
void(* qmf_32_subbands)(float samples_in[DCA_SUBBANDS][SAMPLES_PER_SUBBAND], int sb_act, SynthFilterContext *synth, FFTContext *imdct, float synth_buf_ptr[512], int *synth_buf_offset, float synth_buf2[32], const float window[512], float *samples_out, float raXin[32], float scale)
Definition: dcadsp.h:32
Bit allocation.
Definition: dcadec.c:144
#define AV_CH_FRONT_CENTER
#define DCA_SUBBANDS_X96K
Definition: dcadsp.h:25
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:52
uint8_t core_downmix_amode
audio channel arrangement of embedded downmix
Definition: dca.h:219
int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]
bit allocation quantizer select
Definition: dca.h:140
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:30
int joint_huff
joint subband scale factors codebook
Definition: dca.h:167
int bit_rate
transmission bit rate
Definition: dca.h:185
static const uint8_t *const bitalloc_bits[10][8]
Definition: dcahuff.h:1023
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int offset
code values offset
Definition: dcadec.c:145
static const uint8_t abits_levels[7]
Definition: dcadec.c:786
#define AV_CH_FRONT_RIGHT_OF_CENTER
int xch_present
XCh extension present and valid.
Definition: dca.h:251
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:161
int bits
Definition: vlc.h:27
if(ac->has_optimized_func)
#define SCALES_VLC_BITS
Definition: dcahuff.h:73
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
const int16_t ff_dca_adpcm_vb[4096][4]
Definition: dcadata.c:51
int table_allocated
Definition: vlc.h:29
const uint32_t ff_dca_scale_factor_quant7[128]
Definition: dcadata.c:4163
int core_ext_mask
present extensions in the core substream
Definition: dca.h:247
int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame)
Definition: dca_xll.c:409
#define FF_PROFILE_DTS_96_24
Definition: avcodec.h:2897
int lfe_scale_factor
Definition: dca.h:224
const uint8_t ff_dca_channels[16]
Definition: dcadata.c:40
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
Definition: dcadec.c:935
float dct4_coeff[32][32]
Definition: dca.h:127
Libavcodec external API header.
#define ff_dlog(ctx,...)
Definition: internal.h:60
int aux_data
auxiliary data flag
Definition: dca.h:190
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:60
int sample_rate
samples per second
Definition: avcodec.h:2152
int ext_coding
extended coding flag
Definition: dca.h:193
av_default_item_name
Definition: dnxhdenc.c:55
static float dca_dmix_code(unsigned code)
Definition: dcadec.c:1155
main external API structure.
Definition: avcodec.h:1409
#define FASTDIV(a, b)
Definition: mathops.h:190
int copy_history
copy history
Definition: dca.h:200
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
const float ff_dca_default_coeffs[10][6][2]
Definition: dcadata.c:8179
const int8_t ff_dca_channel_reorder_nolfe_xch[16][9]
Definition: dcadata.c:8365
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:292
VLC vlc[8]
actual codes
Definition: dcadec.c:148
Describe the class of an AVClass context structure.
Definition: log.h:34
int exss_ext_mask
Non-core extensions.
Definition: dca.h:248
int joint_intensity[DCA_PRIM_CHANNELS_MAX]
joint intensity coding index
Definition: dca.h:137
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:259
const uint32_t ff_dca_scale_factor_quant6[64]
Definition: dcadata.c:4152
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:108
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:362
static const uint64_t dca_core_channel_layout[]
Definition: dcadec.c:95
int sampling_frequency
sampling frequency
Definition: dca.h:83
int sample_blocks
number of PCM sample blocks
Definition: dca.h:181
DCAAudioHeader audio_header
Definition: dca.h:207
static const uint8_t abits_sizes[7]
Definition: dcadec.c:785
const int8_t ff_dca_channel_reorder_lfe[16][9]
Definition: dcadata.c:8308
#define u(width,...)
AVCodec ff_dca_decoder
Definition: dcadec.c:1590
int xll_segments
number of segments per frame
Definition: dca.h:260
GetBitContext gb
Definition: dca.h:242
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:300
int current_subframe
Definition: dca.h:244
static const AVOption options[]
Definition: dcadec.c:1577
int transient_huffman[DCA_PRIM_CHANNELS_MAX]
transient mode code book
Definition: dca.h:138
void(* dequantize)(int32_t *samples, uint32_t step_size, uint32_t scale)
Definition: dcadsp.h:43
static int decode_blockcode(int code, int levels, int32_t *values)
Definition: dcadec.c:764
#define AV_CH_BACK_CENTER
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
const AVProfile ff_dca_profiles[]
Definition: profiles.c:39
#define AV_CH_SIDE_RIGHT
uint8_t level
Definition: svq3.c:204
static av_cold int dca_decode_init(AVCodecContext *avctx)
DCA initialization.
Definition: dcadec.c:1544
static int scan_for_extensions(AVCodecContext *avctx)
Definition: dcadec.c:1162
DCADSPContext dcadsp
Definition: dca.h:286
int hist_index
Definition: dca.h:154
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:394
common internal api header.
static void qmf_64_subbands(DCAContext *s, int chans, float samples_in[DCA_SUBBANDS_X96K][SAMPLES_PER_SUBBAND], float *samples_out, float scale)
Definition: dcadec.c:593
common internal and external API header
#define LOCAL_ALIGNED(a, t, v,...)
Definition: internal.h:100
#define AV_COPY128(d, s)
Definition: intreadwrite.h:525
uint8_t * extra_channels_buffer
Definition: dca.h:235
#define ff_mdct_end
Definition: fft.h:152
#define INIT_VLC_USE_NEW_STATIC
Definition: vlc.h:55
static const uint16_t scales_codes[SCALES_COUNT][129]
Definition: dcahuff.h:74
int dialog_norm
dialog normalisation parameter
Definition: dca.h:204
int prediction_vq[DCA_SUBBANDS]
prediction VQ coefs
Definition: dca.h:163
int32_t high_freq_vq[DCA_SUBBANDS]
VQ encoded high frequency subbands.
Definition: dca.h:170
#define DCA_NSYNCAUX
Definition: dcadec.c:141
int xll_smpl_in_seg
samples in segment per one frequency band for the first channel set
Definition: dca.h:262
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
#define DCA_SUBBANDS
Definition: dcadsp.h:26
int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
quantization index codebook select
Definition: dca.h:141
int bit_rate_index
transmission bit rate index
Definition: dca.h:186
void ff_dca_exss_parse_header(DCAContext *s)
Parse extension substream header (HD)
Definition: dca_exss.c:245
void * priv_data
Definition: avcodec.h:1451
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill channel data pointers and linesize for samples with sample format sample_fmt.
Definition: samplefmt.c:140
static const uint8_t bitalloc_maxbits[10][7]
Definition: dcahuff.h:992
int len
int channels
number of audio channels
Definition: avcodec.h:2153
static uint8_t tmp[8]
Definition: des.c:38
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
AVFloatDSPContext fdsp
Definition: dca.h:283
float * samples_chanptr[DCA_PRIM_CHANNELS_MAX+1]
Definition: dca.h:233
int subsubframes[DCA_SUBFRAMES_MAX]
number of subsubframes
Definition: dca.h:210
QMF64_table * qmf64_table
Definition: dca.h:287
static const int dca_ext_audio_descr_mask[]
Definition: dcadec.c:75
XCh channel extension in core substream.
Definition: dca.h:69
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:403
#define DOWNMIX_TO_STEREO(op1, op2)
Definition: dcadec.c:694
void(* int32_to_float)(float *dst, const int32_t *src, intptr_t len)
Convert an array of int32_t to float.
Definition: fmtconvert.h:49
int frame_size
primary frame byte size
Definition: dca.h:182
static QMF64_table * qmf64_precompute(void)
Definition: dcadec.c:567
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:700
int subband_activity[DCA_PRIM_CHANNELS_MAX]
subband activity count
Definition: dca.h:135
FILE * out
Definition: movenc.c:54
int aspf
audio sync word insertion flag
Definition: dca.h:194
#define av_always_inline
Definition: attributes.h:40
int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]
scale factor code book
Definition: dca.h:139
#define VLC_TYPE
Definition: vlc.h:24
#define AV_CH_SIDE_LEFT
float downmix_coef[DCA_PRIM_CHANNELS_MAX+1][2]
stereo downmix coefficients
Definition: dca.h:212
static const uint8_t bitalloc_12_vlc_bits[BITALLOC_12_COUNT]
Definition: dcahuff.h:47
int output
type of output
Definition: dca.h:231
static int get_scale(GetBitContext *gb, int level, int value, int log2range)
Definition: dcadec.c:347
const float ff_dca_fir_32bands_perfect[512]
Definition: dcadata.c:6253
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:174
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:2210
const int8_t ff_dca_lfe_index[16]
Definition: dcadata.c:8304
int partial_samples[DCA_SUBFRAMES_MAX]
partial subsubframe samples count
Definition: dca.h:211
static const AVClass dca_decoder_class
Definition: dcadec.c:1583
float subband_fir_hist[1024]
Definition: dca.h:158
This structure stores compressed data.
Definition: avcodec.h:1323
float rsin[32]
Definition: dca.h:130
static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
Definition: dcadec.c:224
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838
#define SAMPLES_PER_SUBBAND
Definition: dcadsp.h:27
unsigned int extra_channels_buffer_size
Definition: dca.h:236
for(j=16;j >0;--j)
static const uint8_t tmode_vlc_bits[TMODE_COUNT]
Definition: dcahuff.h:30
static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba, int idx)
Definition: dcadec.c:156
#define DCA_PRIM_CHANNELS_MAX
Definition: dca.h:45
#define AV_CH_BACK_RIGHT
int frame_type
type of the current frame
Definition: dca.h:178
XXCh channels extension in core substream.
Definition: dca.h:67
int profile
Definition: dca.h:279
bitstream writer API