FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
11  *
12  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "synth_filter.h"
53 
54 #if ARCH_ARM
55 # include "arm/dca.h"
56 #endif
57 
58 enum DCAMode {
59  DCA_MONO = 0,
70 };
71 
72 
74  DCA_XXCH_FRONT_CENTER = 0x0000001,
75  DCA_XXCH_FRONT_LEFT = 0x0000002,
76  DCA_XXCH_FRONT_RIGHT = 0x0000004,
79  DCA_XXCH_LFE1 = 0x0000020,
80  DCA_XXCH_REAR_CENTER = 0x0000040,
90  DCA_XXCH_LFE2 = 0x0010000,
93  DCA_XXCH_OVERHEAD = 0x0080000,
102 };
103 
104 #define DCA_DOLBY 101 /* FIXME */
105 
106 #define DCA_CHANNEL_BITS 6
107 #define DCA_CHANNEL_MASK 0x3F
108 
109 #define DCA_LFE 0x80
110 
111 #define HEADER_SIZE 14
112 
113 #define DCA_NSYNCAUX 0x9A1105A0
114 
115 
116 /** Bit allocation */
117 typedef struct BitAlloc {
118  int offset; ///< code values offset
119  int maxbits[8]; ///< max bits in VLC
120  int wrap; ///< wrap for get_vlc2()
121  VLC vlc[8]; ///< actual codes
122 } BitAlloc;
123 
124 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
125 static BitAlloc dca_tmode; ///< transition mode VLCs
126 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
127 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
128 
130  int idx)
131 {
132  return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
133  ba->offset;
134 }
135 
136 static float dca_dmix_code(unsigned code);
137 
138 static av_cold void dca_init_vlcs(void)
139 {
140  static int vlcs_initialized = 0;
141  int i, j, c = 14;
142  static VLC_TYPE dca_table[23622][2];
143 
144  if (vlcs_initialized)
145  return;
146 
147  dca_bitalloc_index.offset = 1;
148  dca_bitalloc_index.wrap = 2;
149  for (i = 0; i < 5; i++) {
150  dca_bitalloc_index.vlc[i].table = &dca_table[ff_dca_vlc_offs[i]];
151  dca_bitalloc_index.vlc[i].table_allocated = ff_dca_vlc_offs[i + 1] - ff_dca_vlc_offs[i];
152  init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
153  bitalloc_12_bits[i], 1, 1,
155  }
156  dca_scalefactor.offset = -64;
157  dca_scalefactor.wrap = 2;
158  for (i = 0; i < 5; i++) {
159  dca_scalefactor.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 5]];
160  dca_scalefactor.vlc[i].table_allocated = ff_dca_vlc_offs[i + 6] - ff_dca_vlc_offs[i + 5];
161  init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
162  scales_bits[i], 1, 1,
164  }
165  dca_tmode.offset = 0;
166  dca_tmode.wrap = 1;
167  for (i = 0; i < 4; i++) {
168  dca_tmode.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 10]];
169  dca_tmode.vlc[i].table_allocated = ff_dca_vlc_offs[i + 11] - ff_dca_vlc_offs[i + 10];
170  init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
171  tmode_bits[i], 1, 1,
173  }
174 
175  for (i = 0; i < 10; i++)
176  for (j = 0; j < 7; j++) {
177  if (!bitalloc_codes[i][j])
178  break;
179  dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
180  dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
181  dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[ff_dca_vlc_offs[c]];
182  dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = ff_dca_vlc_offs[c + 1] - ff_dca_vlc_offs[c];
183 
184  init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
185  bitalloc_sizes[i],
186  bitalloc_bits[i][j], 1, 1,
188  c++;
189  }
190  vlcs_initialized = 1;
191 }
192 
193 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
194 {
195  while (len--)
196  *dst++ = get_bits(gb, bits);
197 }
198 
199 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
200 {
201  int i, base, mask;
202 
203  /* locate channel set containing the channel */
204  for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
205  i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
206  base += av_popcount(mask);
207 
208  return base + av_popcount(mask & (xxch_ch - 1));
209 }
210 
211 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
212  int xxch)
213 {
214  int i, j;
215  static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
216  static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
217  static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
218  int hdr_pos = 0, hdr_size = 0;
219  float scale_factor;
220  int this_chans, acc_mask;
221  int embedded_downmix;
222  int nchans, mask[8];
223  int coeff, ichan;
224 
225  /* xxch has arbitrary sized audio coding headers */
226  if (xxch) {
227  hdr_pos = get_bits_count(&s->gb);
228  hdr_size = get_bits(&s->gb, 7) + 1;
229  }
230 
231  nchans = get_bits(&s->gb, 3) + 1;
232  if (xxch && nchans >= 3) {
233  av_log(s->avctx, AV_LOG_ERROR, "nchans %d is too large\n", nchans);
234  return AVERROR_INVALIDDATA;
235  } else if (nchans + base_channel > DCA_PRIM_CHANNELS_MAX) {
236  av_log(s->avctx, AV_LOG_ERROR, "channel sum %d + %d is too large\n", nchans, base_channel);
237  return AVERROR_INVALIDDATA;
238  }
239 
240  s->total_channels = nchans + base_channel;
242 
243  /* obtain speaker layout mask & downmix coefficients for XXCH */
244  if (xxch) {
245  acc_mask = s->xxch_core_spkmask;
246 
247  this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
248  s->xxch_spk_masks[s->xxch_chset] = this_chans;
249  s->xxch_chset_nch[s->xxch_chset] = nchans;
250 
251  for (i = 0; i <= s->xxch_chset; i++)
252  acc_mask |= s->xxch_spk_masks[i];
253 
254  /* check for downmixing information */
255  if (get_bits1(&s->gb)) {
256  embedded_downmix = get_bits1(&s->gb);
257  coeff = get_bits(&s->gb, 6);
258 
259  if (coeff<1 || coeff>61) {
260  av_log(s->avctx, AV_LOG_ERROR, "6bit coeff %d is out of range\n", coeff);
261  return AVERROR_INVALIDDATA;
262  }
263 
264  scale_factor = -1.0f / dca_dmix_code((coeff<<2)-3);
265 
266  s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
267 
268  for (i = base_channel; i < s->prim_channels; i++) {
269  mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
270  }
271 
272  for (j = base_channel; j < s->prim_channels; j++) {
273  memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
274  s->xxch_dmix_embedded |= (embedded_downmix << j);
275  for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
276  if (mask[j] & (1 << i)) {
277  if ((1 << i) == DCA_XXCH_LFE1) {
279  "DCA-XXCH: dmix to LFE1 not supported.\n");
280  continue;
281  }
282 
283  coeff = get_bits(&s->gb, 7);
284  ichan = dca_xxch2index(s, 1 << i);
285  if ((coeff&63)<1 || (coeff&63)>61) {
286  av_log(s->avctx, AV_LOG_ERROR, "7bit coeff %d is out of range\n", coeff);
287  return AVERROR_INVALIDDATA;
288  }
289  s->xxch_dmix_coeff[j][ichan] = dca_dmix_code((coeff<<2)-3);
290  }
291  }
292  }
293  }
294  }
295 
298 
299  for (i = base_channel; i < s->prim_channels; i++) {
300  s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
301  if (s->subband_activity[i] > DCA_SUBBANDS)
303  }
304  for (i = base_channel; i < s->prim_channels; i++) {
305  s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
306  if (s->vq_start_subband[i] > DCA_SUBBANDS)
308  }
309  get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
310  get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
311  get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
312  get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
313 
314  /* Get codebooks quantization indexes */
315  if (!base_channel)
316  memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
317  for (j = 1; j < 11; j++)
318  for (i = base_channel; i < s->prim_channels; i++)
319  s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
320 
321  /* Get scale factor adjustment */
322  for (j = 0; j < 11; j++)
323  for (i = base_channel; i < s->prim_channels; i++)
324  s->scalefactor_adj[i][j] = 1;
325 
326  for (j = 1; j < 11; j++)
327  for (i = base_channel; i < s->prim_channels; i++)
328  if (s->quant_index_huffman[i][j] < thr[j])
329  s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
330 
331  if (!xxch) {
332  if (s->crc_present) {
333  /* Audio header CRC check */
334  get_bits(&s->gb, 16);
335  }
336  } else {
337  /* Skip to the end of the header, also ignore CRC if present */
338  i = get_bits_count(&s->gb);
339  if (hdr_pos + 8 * hdr_size > i)
340  skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
341  }
342 
343  s->current_subframe = 0;
344  s->current_subsubframe = 0;
345 
346  return 0;
347 }
348 
350 {
351  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
352 
353  /* Sync code */
354  skip_bits_long(&s->gb, 32);
355 
356  /* Frame header */
357  s->frame_type = get_bits(&s->gb, 1);
358  s->samples_deficit = get_bits(&s->gb, 5) + 1;
359  s->crc_present = get_bits(&s->gb, 1);
360  s->sample_blocks = get_bits(&s->gb, 7) + 1;
361  s->frame_size = get_bits(&s->gb, 14) + 1;
362  if (s->frame_size < 95)
363  return AVERROR_INVALIDDATA;
364  s->amode = get_bits(&s->gb, 6);
366  if (!s->sample_rate)
367  return AVERROR_INVALIDDATA;
368  s->bit_rate_index = get_bits(&s->gb, 5);
370  if (!s->bit_rate)
371  return AVERROR_INVALIDDATA;
372 
373  skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
374  s->dynrange = get_bits(&s->gb, 1);
375  s->timestamp = get_bits(&s->gb, 1);
376  s->aux_data = get_bits(&s->gb, 1);
377  s->hdcd = get_bits(&s->gb, 1);
378  s->ext_descr = get_bits(&s->gb, 3);
379  s->ext_coding = get_bits(&s->gb, 1);
380  s->aspf = get_bits(&s->gb, 1);
381  s->lfe = get_bits(&s->gb, 2);
382  s->predictor_history = get_bits(&s->gb, 1);
383 
384  if (s->lfe > 2) {
385  s->lfe = 0;
386  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
387  return AVERROR_INVALIDDATA;
388  }
389 
390  /* TODO: check CRC */
391  if (s->crc_present)
392  s->header_crc = get_bits(&s->gb, 16);
393 
394  s->multirate_inter = get_bits(&s->gb, 1);
395  s->version = get_bits(&s->gb, 4);
396  s->copy_history = get_bits(&s->gb, 2);
397  s->source_pcm_res = get_bits(&s->gb, 3);
398  s->front_sum = get_bits(&s->gb, 1);
399  s->surround_sum = get_bits(&s->gb, 1);
400  s->dialog_norm = get_bits(&s->gb, 4);
401 
402  /* FIXME: channels mixing levels */
403  s->output = s->amode;
404  if (s->lfe)
405  s->output |= DCA_LFE;
406 
407  /* Primary audio coding header */
408  s->subframes = get_bits(&s->gb, 4) + 1;
409 
410  return dca_parse_audio_coding_header(s, 0, 0);
411 }
412 
413 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
414 {
415  if (level < 5) {
416  /* huffman encoded */
417  value += get_bitalloc(gb, &dca_scalefactor, level);
418  value = av_clip(value, 0, (1 << log2range) - 1);
419  } else if (level < 8) {
420  if (level + 1 > log2range) {
421  skip_bits(gb, level + 1 - log2range);
422  value = get_bits(gb, log2range);
423  } else {
424  value = get_bits(gb, level + 1);
425  }
426  }
427  return value;
428 }
429 
430 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
431 {
432  /* Primary audio coding side information */
433  int j, k;
434 
435  if (get_bits_left(&s->gb) < 0)
436  return AVERROR_INVALIDDATA;
437 
438  if (!base_channel) {
439  s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
440  if (block_index + s->subsubframes[s->current_subframe] > s->sample_blocks/8) {
441  s->subsubframes[s->current_subframe] = 1;
442  return AVERROR_INVALIDDATA;
443  }
444  s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
445  }
446 
447  for (j = base_channel; j < s->prim_channels; j++) {
448  for (k = 0; k < s->subband_activity[j]; k++)
449  s->prediction_mode[j][k] = get_bits(&s->gb, 1);
450  }
451 
452  /* Get prediction codebook */
453  for (j = base_channel; j < s->prim_channels; j++) {
454  for (k = 0; k < s->subband_activity[j]; k++) {
455  if (s->prediction_mode[j][k] > 0) {
456  /* (Prediction coefficient VQ address) */
457  s->prediction_vq[j][k] = get_bits(&s->gb, 12);
458  }
459  }
460  }
461 
462  /* Bit allocation index */
463  for (j = base_channel; j < s->prim_channels; j++) {
464  for (k = 0; k < s->vq_start_subband[j]; k++) {
465  if (s->bitalloc_huffman[j] == 6)
466  s->bitalloc[j][k] = get_bits(&s->gb, 5);
467  else if (s->bitalloc_huffman[j] == 5)
468  s->bitalloc[j][k] = get_bits(&s->gb, 4);
469  else if (s->bitalloc_huffman[j] == 7) {
471  "Invalid bit allocation index\n");
472  return AVERROR_INVALIDDATA;
473  } else {
474  s->bitalloc[j][k] =
475  get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
476  }
477 
478  if (s->bitalloc[j][k] > 26) {
479  ff_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
480  j, k, s->bitalloc[j][k]);
481  return AVERROR_INVALIDDATA;
482  }
483  }
484  }
485 
486  /* Transition mode */
487  for (j = base_channel; j < s->prim_channels; j++) {
488  for (k = 0; k < s->subband_activity[j]; k++) {
489  s->transition_mode[j][k] = 0;
490  if (s->subsubframes[s->current_subframe] > 1 &&
491  k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
492  s->transition_mode[j][k] =
493  get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
494  }
495  }
496  }
497 
498  if (get_bits_left(&s->gb) < 0)
499  return AVERROR_INVALIDDATA;
500 
501  for (j = base_channel; j < s->prim_channels; j++) {
502  const uint32_t *scale_table;
503  int scale_sum, log_size;
504 
505  memset(s->scale_factor[j], 0,
506  s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
507 
508  if (s->scalefactor_huffman[j] == 6) {
509  scale_table = ff_dca_scale_factor_quant7;
510  log_size = 7;
511  } else {
512  scale_table = ff_dca_scale_factor_quant6;
513  log_size = 6;
514  }
515 
516  /* When huffman coded, only the difference is encoded */
517  scale_sum = 0;
518 
519  for (k = 0; k < s->subband_activity[j]; k++) {
520  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
521  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
522  s->scale_factor[j][k][0] = scale_table[scale_sum];
523  }
524 
525  if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
526  /* Get second scale factor */
527  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
528  s->scale_factor[j][k][1] = scale_table[scale_sum];
529  }
530  }
531  }
532 
533  /* Joint subband scale factor codebook select */
534  for (j = base_channel; j < s->prim_channels; j++) {
535  /* Transmitted only if joint subband coding enabled */
536  if (s->joint_intensity[j] > 0)
537  s->joint_huff[j] = get_bits(&s->gb, 3);
538  }
539 
540  if (get_bits_left(&s->gb) < 0)
541  return AVERROR_INVALIDDATA;
542 
543  /* Scale factors for joint subband coding */
544  for (j = base_channel; j < s->prim_channels; j++) {
545  int source_channel;
546 
547  /* Transmitted only if joint subband coding enabled */
548  if (s->joint_intensity[j] > 0) {
549  int scale = 0;
550  source_channel = s->joint_intensity[j] - 1;
551 
552  /* When huffman coded, only the difference is encoded
553  * (is this valid as well for joint scales ???) */
554 
555  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
556  scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
557  s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
558  }
559 
560  if (!(s->debug_flag & 0x02)) {
562  "Joint stereo coding not supported\n");
563  s->debug_flag |= 0x02;
564  }
565  }
566  }
567 
568  /* Dynamic range coefficient */
569  if (!base_channel && s->dynrange)
570  s->dynrange_coef = get_bits(&s->gb, 8);
571 
572  /* Side information CRC check word */
573  if (s->crc_present) {
574  get_bits(&s->gb, 16);
575  }
576 
577  /*
578  * Primary audio data arrays
579  */
580 
581  /* VQ encoded high frequency subbands */
582  for (j = base_channel; j < s->prim_channels; j++)
583  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
584  /* 1 vector -> 32 samples */
585  s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
586 
587  /* Low frequency effect data */
588  if (!base_channel && s->lfe) {
589  int quant7;
590  /* LFE samples */
591  int lfe_samples = 2 * s->lfe * (4 + block_index);
592  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
593  float lfe_scale;
594 
595  for (j = lfe_samples; j < lfe_end_sample; j++) {
596  /* Signed 8 bits int */
597  s->lfe_data[j] = get_sbits(&s->gb, 8);
598  }
599 
600  /* Scale factor index */
601  quant7 = get_bits(&s->gb, 8);
602  if (quant7 > 127) {
603  avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
604  return AVERROR_INVALIDDATA;
605  }
607 
608  /* Quantization step size * scale factor */
609  lfe_scale = 0.035 * s->lfe_scale_factor;
610 
611  for (j = lfe_samples; j < lfe_end_sample; j++)
612  s->lfe_data[j] *= lfe_scale;
613  }
614 
615  return 0;
616 }
617 
618 static void qmf_32_subbands(DCAContext *s, int chans,
619  float samples_in[32][8], float *samples_out,
620  float scale)
621 {
622  const float *prCoeff;
623 
624  int sb_act = s->subband_activity[chans];
625 
626  scale *= sqrt(1 / 8.0);
627 
628  /* Select filter */
629  if (!s->multirate_inter) /* Non-perfect reconstruction */
631  else /* Perfect reconstruction */
632  prCoeff = ff_dca_fir_32bands_perfect;
633 
634  s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
635  s->subband_fir_hist[chans],
636  &s->hist_index[chans],
637  s->subband_fir_noidea[chans], prCoeff,
638  samples_out, s->raXin, scale);
639 }
640 
642 {
643  unsigned i, j;
644  QMF64_table *table = av_malloc(sizeof(*table));
645  if (!table)
646  return NULL;
647 
648  for (i = 0; i < 32; i++)
649  for (j = 0; j < 32; j++)
650  table->dct4_coeff[i][j] = cos((2 * i + 1) * (2 * j + 1) * M_PI / 128);
651  for (i = 0; i < 32; i++)
652  for (j = 0; j < 32; j++)
653  table->dct2_coeff[i][j] = cos((2 * i + 1) * j * M_PI / 64);
654 
655  /* FIXME: Is the factor 0.125 = 1/8 right? */
656  for (i = 0; i < 32; i++)
657  table->rcos[i] = 0.125 / cos((2 * i + 1) * M_PI / 256);
658  for (i = 0; i < 32; i++)
659  table->rsin[i] = -0.125 / sin((2 * i + 1) * M_PI / 256);
660 
661  return table;
662 }
663 
664 /* FIXME: Totally unoptimized. Based on the reference code and
665  * http://multimedia.cx/mirror/dca-transform.pdf, with guessed tweaks
666  * for doubling the size. */
667 static void qmf_64_subbands(DCAContext *s, int chans, float samples_in[64][8],
668  float *samples_out, float scale)
669 {
670  float raXin[64];
671  float A[32], B[32];
672  float *raX = s->subband_fir_hist[chans];
673  float *raZ = s->subband_fir_noidea[chans];
674  unsigned i, j, k, subindex;
675 
676  for (i = s->subband_activity[chans]; i < 64; i++)
677  raXin[i] = 0.0;
678  for (subindex = 0; subindex < 8; subindex++) {
679  for (i = 0; i < s->subband_activity[chans]; i++)
680  raXin[i] = samples_in[i][subindex];
681 
682  for (k = 0; k < 32; k++) {
683  A[k] = 0.0;
684  for (i = 0; i < 32; i++)
685  A[k] += (raXin[2 * i] + raXin[2 * i + 1]) * s->qmf64_table->dct4_coeff[k][i];
686  }
687  for (k = 0; k < 32; k++) {
688  B[k] = raXin[0] * s->qmf64_table->dct2_coeff[k][0];
689  for (i = 1; i < 32; i++)
690  B[k] += (raXin[2 * i] + raXin[2 * i - 1]) * s->qmf64_table->dct2_coeff[k][i];
691  }
692  for (k = 0; k < 32; k++) {
693  raX[k] = s->qmf64_table->rcos[k] * (A[k] + B[k]);
694  raX[63 - k] = s->qmf64_table->rsin[k] * (A[k] - B[k]);
695  }
696 
697  for (i = 0; i < 64; i++) {
698  float out = raZ[i];
699  for (j = 0; j < 1024; j += 128)
700  out += ff_dca_fir_64bands[j + i] * (raX[j + i] - raX[j + 63 - i]);
701  *samples_out++ = out * scale;
702  }
703 
704  for (i = 0; i < 64; i++) {
705  float hist = 0.0;
706  for (j = 0; j < 1024; j += 128)
707  hist += ff_dca_fir_64bands[64 + j + i] * (-raX[i + j] - raX[j + 63 - i]);
708 
709  raZ[i] = hist;
710  }
711 
712  /* FIXME: Make buffer circular, to avoid this move. */
713  memmove(raX + 64, raX, (1024 - 64) * sizeof(*raX));
714  }
715 }
716 
717 static void lfe_interpolation_fir(DCAContext *s, const float *samples_in,
718  float *samples_out)
719 {
720  /* samples_in: An array holding decimated samples.
721  * Samples in current subframe starts from samples_in[0],
722  * while samples_in[-1], samples_in[-2], ..., stores samples
723  * from last subframe as history.
724  *
725  * samples_out: An array holding interpolated samples
726  */
727 
728  int idx;
729  const float *prCoeff;
730  int deciindex;
731 
732  /* Select decimation filter */
733  if (s->lfe == 1) {
734  idx = 1;
735  prCoeff = ff_dca_lfe_fir_128;
736  } else {
737  idx = 0;
739  prCoeff = ff_dca_lfe_xll_fir_64;
740  else
741  prCoeff = ff_dca_lfe_fir_64;
742  }
743  /* Interpolation */
744  for (deciindex = 0; deciindex < 2 * s->lfe; deciindex++) {
745  s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
746  samples_in++;
747  samples_out += 2 * 32 * (1 + idx);
748  }
749 }
750 
751 /* downmixing routines */
752 #define MIX_REAR1(samples, s1, rs, coef) \
753  samples[0][i] += samples[s1][i] * coef[rs][0]; \
754  samples[1][i] += samples[s1][i] * coef[rs][1];
755 
756 #define MIX_REAR2(samples, s1, s2, rs, coef) \
757  samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
758  samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
759 
760 #define MIX_FRONT3(samples, coef) \
761  t = samples[c][i]; \
762  u = samples[l][i]; \
763  v = samples[r][i]; \
764  samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
765  samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
766 
767 #define DOWNMIX_TO_STEREO(op1, op2) \
768  for (i = 0; i < 256; i++) { \
769  op1 \
770  op2 \
771  }
772 
773 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
774  float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
775  const int8_t *channel_mapping)
776 {
777  int c, l, r, sl, sr, s;
778  int i;
779  float t, u, v;
780 
781  switch (srcfmt) {
782  case DCA_MONO:
783  case DCA_4F2R:
784  av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
785  break;
786  case DCA_CHANNEL:
787  case DCA_STEREO:
788  case DCA_STEREO_TOTAL:
789  case DCA_STEREO_SUMDIFF:
790  break;
791  case DCA_3F:
792  c = channel_mapping[0];
793  l = channel_mapping[1];
794  r = channel_mapping[2];
795  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
796  break;
797  case DCA_2F1R:
798  s = channel_mapping[2];
799  DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
800  break;
801  case DCA_3F1R:
802  c = channel_mapping[0];
803  l = channel_mapping[1];
804  r = channel_mapping[2];
805  s = channel_mapping[3];
806  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
807  MIX_REAR1(samples, s, 3, coef));
808  break;
809  case DCA_2F2R:
810  sl = channel_mapping[2];
811  sr = channel_mapping[3];
812  DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
813  break;
814  case DCA_3F2R:
815  c = channel_mapping[0];
816  l = channel_mapping[1];
817  r = channel_mapping[2];
818  sl = channel_mapping[3];
819  sr = channel_mapping[4];
820  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
821  MIX_REAR2(samples, sl, sr, 3, coef));
822  break;
823  }
824  if (lfe_present) {
825  int lf_buf = ff_dca_lfe_index[srcfmt];
826  int lf_idx = ff_dca_channels[srcfmt];
827  for (i = 0; i < 256; i++) {
828  samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
829  samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
830  }
831  }
832 }
833 
834 #ifndef decode_blockcodes
835 /* Very compact version of the block code decoder that does not use table
836  * look-up but is slightly slower */
837 static int decode_blockcode(int code, int levels, int32_t *values)
838 {
839  int i;
840  int offset = (levels - 1) >> 1;
841 
842  for (i = 0; i < 4; i++) {
843  int div = FASTDIV(code, levels);
844  values[i] = code - offset - div * levels;
845  code = div;
846  }
847 
848  return code;
849 }
850 
851 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
852 {
853  return decode_blockcode(code1, levels, values) |
854  decode_blockcode(code2, levels, values + 4);
855 }
856 #endif
857 
858 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
859 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
860 
861 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
862 {
863  int k, l;
864  int subsubframe = s->current_subsubframe;
865 
866  const float *quant_step_table;
867 
868  /* FIXME */
869  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
871 
872  /*
873  * Audio data
874  */
875 
876  /* Select quantization step size table */
877  if (s->bit_rate_index == 0x1f)
878  quant_step_table = ff_dca_lossless_quant_d;
879  else
880  quant_step_table = ff_dca_lossy_quant_d;
881 
882  for (k = base_channel; k < s->prim_channels; k++) {
883  float rscale[DCA_SUBBANDS];
884 
885  if (get_bits_left(&s->gb) < 0)
886  return AVERROR_INVALIDDATA;
887 
888  for (l = 0; l < s->vq_start_subband[k]; l++) {
889  int m;
890 
891  /* Select the mid-tread linear quantizer */
892  int abits = s->bitalloc[k][l];
893 
894  float quant_step_size = quant_step_table[abits];
895 
896  /*
897  * Determine quantization index code book and its type
898  */
899 
900  /* Select quantization index code book */
901  int sel = s->quant_index_huffman[k][abits];
902 
903  /*
904  * Extract bits from the bit stream
905  */
906  if (!abits) {
907  rscale[l] = 0;
908  memset(block + 8 * l, 0, 8 * sizeof(block[0]));
909  } else {
910  /* Deal with transients */
911  int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
912  rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
913  s->scalefactor_adj[k][sel];
914 
915  if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
916  if (abits <= 7) {
917  /* Block code */
918  int block_code1, block_code2, size, levels, err;
919 
920  size = abits_sizes[abits - 1];
921  levels = abits_levels[abits - 1];
922 
923  block_code1 = get_bits(&s->gb, size);
924  block_code2 = get_bits(&s->gb, size);
925  err = decode_blockcodes(block_code1, block_code2,
926  levels, block + 8 * l);
927  if (err) {
929  "ERROR: block code look-up failed\n");
930  return AVERROR_INVALIDDATA;
931  }
932  } else {
933  /* no coding */
934  for (m = 0; m < 8; m++)
935  block[8 * l + m] = get_sbits(&s->gb, abits - 3);
936  }
937  } else {
938  /* Huffman coded */
939  for (m = 0; m < 8; m++)
940  block[8 * l + m] = get_bitalloc(&s->gb,
941  &dca_smpl_bitalloc[abits], sel);
942  }
943  }
944  }
945 
946  s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
947  block, rscale, 8 * s->vq_start_subband[k]);
948 
949  for (l = 0; l < s->vq_start_subband[k]; l++) {
950  int m;
951  /*
952  * Inverse ADPCM if in prediction mode
953  */
954  if (s->prediction_mode[k][l]) {
955  int n;
956  if (s->predictor_history)
957  subband_samples[k][l][0] += (ff_dca_adpcm_vb[s->prediction_vq[k][l]][0] *
958  s->subband_samples_hist[k][l][3] +
959  ff_dca_adpcm_vb[s->prediction_vq[k][l]][1] *
960  s->subband_samples_hist[k][l][2] +
961  ff_dca_adpcm_vb[s->prediction_vq[k][l]][2] *
962  s->subband_samples_hist[k][l][1] +
963  ff_dca_adpcm_vb[s->prediction_vq[k][l]][3] *
964  s->subband_samples_hist[k][l][0]) *
965  (1.0f / 8192);
966  for (m = 1; m < 8; m++) {
967  float sum = ff_dca_adpcm_vb[s->prediction_vq[k][l]][0] *
968  subband_samples[k][l][m - 1];
969  for (n = 2; n <= 4; n++)
970  if (m >= n)
971  sum += ff_dca_adpcm_vb[s->prediction_vq[k][l]][n - 1] *
972  subband_samples[k][l][m - n];
973  else if (s->predictor_history)
974  sum += ff_dca_adpcm_vb[s->prediction_vq[k][l]][n - 1] *
975  s->subband_samples_hist[k][l][m - n + 4];
976  subband_samples[k][l][m] += sum * (1.0f / 8192);
977  }
978  }
979  }
980 
981  /*
982  * Decode VQ encoded high frequencies
983  */
984  if (s->subband_activity[k] > s->vq_start_subband[k]) {
985  if (!(s->debug_flag & 0x01)) {
987  "Stream with high frequencies VQ coding\n");
988  s->debug_flag |= 0x01;
989  }
990  s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
991  ff_dca_high_freq_vq, subsubframe * 8,
992  s->scale_factor[k], s->vq_start_subband[k],
993  s->subband_activity[k]);
994  }
995  }
996 
997  /* Check for DSYNC after subsubframe */
998  if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
999  if (get_bits(&s->gb, 16) != 0xFFFF) {
1000  av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1001  return AVERROR_INVALIDDATA;
1002  }
1003  }
1004 
1005  /* Backup predictor history for adpcm */
1006  for (k = base_channel; k < s->prim_channels; k++)
1007  for (l = 0; l < s->vq_start_subband[k]; l++)
1008  AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1009 
1010  return 0;
1011 }
1012 
1013 static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
1014 {
1015  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1016  int k;
1017 
1018  if (upsample) {
1019  if (!s->qmf64_table) {
1021  if (!s->qmf64_table)
1022  return AVERROR(ENOMEM);
1023  }
1024 
1025  /* 64 subbands QMF */
1026  for (k = 0; k < s->prim_channels; k++) {
1027  if (s->channel_order_tab[k] >= 0)
1028  qmf_64_subbands(s, k, subband_samples[k],
1030  /* Upsampling needs a factor 2 here. */
1031  M_SQRT2 / 32768.0);
1032  }
1033  } else {
1034  /* 32 subbands QMF */
1035  for (k = 0; k < s->prim_channels; k++) {
1036  if (s->channel_order_tab[k] >= 0)
1037  qmf_32_subbands(s, k, subband_samples[k],
1039  M_SQRT1_2 / 32768.0);
1040  }
1041  }
1042 
1043  /* Generate LFE samples for this subsubframe FIXME!!! */
1044  if (s->lfe) {
1045  float *samples = s->samples_chanptr[s->lfe_index];
1047  s->lfe_data + 2 * s->lfe * (block_index + 4),
1048  samples);
1049  if (upsample) {
1050  unsigned i;
1051  /* Should apply the filter in Table 6-11 when upsampling. For
1052  * now, just duplicate. */
1053  for (i = 255; i > 0; i--) {
1054  samples[2 * i] =
1055  samples[2 * i + 1] = samples[i];
1056  }
1057  samples[1] = samples[0];
1058  }
1059  }
1060 
1061  /* FIXME: This downmixing is probably broken with upsample.
1062  * Probably totally broken also with XLL in general. */
1063  /* Downmixing to Stereo */
1064  if (s->prim_channels + !!s->lfe > 2 &&
1067  s->channel_order_tab);
1068  }
1069 
1070  return 0;
1071 }
1072 
1073 static int dca_subframe_footer(DCAContext *s, int base_channel)
1074 {
1075  int in, out, aux_data_count, aux_data_end, reserved;
1076  uint32_t nsyncaux;
1077 
1078  /*
1079  * Unpack optional information
1080  */
1081 
1082  /* presumably optional information only appears in the core? */
1083  if (!base_channel) {
1084  if (s->timestamp)
1085  skip_bits_long(&s->gb, 32);
1086 
1087  if (s->aux_data) {
1088  aux_data_count = get_bits(&s->gb, 6);
1089 
1090  // align (32-bit)
1091  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1092 
1093  aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1094 
1095  if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1096  av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1097  nsyncaux);
1098  return AVERROR_INVALIDDATA;
1099  }
1100 
1101  if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1103  "Auxiliary Decode Time Stamp Flag");
1104  // align (4-bit)
1105  skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1106  // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1107  skip_bits_long(&s->gb, 44);
1108  }
1109 
1110  if ((s->core_downmix = get_bits1(&s->gb))) {
1111  int am = get_bits(&s->gb, 3);
1112  switch (am) {
1113  case 0:
1115  break;
1116  case 1:
1118  break;
1119  case 2:
1121  break;
1122  case 3:
1124  break;
1125  case 4:
1127  break;
1128  case 5:
1130  break;
1131  case 6:
1133  break;
1134  default:
1136  "Invalid mode %d for embedded downmix coefficients\n",
1137  am);
1138  return AVERROR_INVALIDDATA;
1139  }
1140  for (out = 0; out < ff_dca_channels[s->core_downmix_amode]; out++) {
1141  for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1142  uint16_t tmp = get_bits(&s->gb, 9);
1143  if ((tmp & 0xFF) > 241) {
1145  "Invalid downmix coefficient code %"PRIu16"\n",
1146  tmp);
1147  return AVERROR_INVALIDDATA;
1148  }
1149  s->core_downmix_codes[in][out] = tmp;
1150  }
1151  }
1152  }
1153 
1154  align_get_bits(&s->gb); // byte align
1155  skip_bits(&s->gb, 16); // nAUXCRC16
1156 
1157  // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1158  if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1160  "Overread auxiliary data by %d bits\n", -reserved);
1161  return AVERROR_INVALIDDATA;
1162  } else if (reserved) {
1164  "Core auxiliary data reserved content");
1165  skip_bits_long(&s->gb, reserved);
1166  }
1167  }
1168 
1169  if (s->crc_present && s->dynrange)
1170  get_bits(&s->gb, 16);
1171  }
1172 
1173  return 0;
1174 }
1175 
1176 /**
1177  * Decode a dca frame block
1178  *
1179  * @param s pointer to the DCAContext
1180  */
1181 
1182 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1183 {
1184  int ret;
1185 
1186  /* Sanity check */
1187  if (s->current_subframe >= s->subframes) {
1188  av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1189  s->current_subframe, s->subframes);
1190  return AVERROR_INVALIDDATA;
1191  }
1192 
1193  if (!s->current_subsubframe) {
1194  /* Read subframe header */
1195  if ((ret = dca_subframe_header(s, base_channel, block_index)))
1196  return ret;
1197  }
1198 
1199  /* Read subsubframe */
1200  if ((ret = dca_subsubframe(s, base_channel, block_index)))
1201  return ret;
1202 
1203  /* Update state */
1204  s->current_subsubframe++;
1206  s->current_subsubframe = 0;
1207  s->current_subframe++;
1208  }
1209  if (s->current_subframe >= s->subframes) {
1210  /* Read subframe footer */
1211  if ((ret = dca_subframe_footer(s, base_channel)))
1212  return ret;
1213  }
1214 
1215  return 0;
1216 }
1217 
1219 {
1220  int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1221  int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1222  int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1223  int anctemp[DCA_CHSET_CHANS_MAX];
1224  int chset_fsize[DCA_CHSETS_MAX];
1225  int n_xbr_ch[DCA_CHSETS_MAX];
1226  int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1227  int i, j, k, l, chset, chan_base;
1228 
1229  av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1230 
1231  /* get bit position of sync header */
1232  hdr_pos = get_bits_count(&s->gb) - 32;
1233 
1234  hdr_size = get_bits(&s->gb, 6) + 1;
1235  num_chsets = get_bits(&s->gb, 2) + 1;
1236 
1237  for(i = 0; i < num_chsets; i++)
1238  chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1239 
1240  xbr_tmode = get_bits1(&s->gb);
1241 
1242  for(i = 0; i < num_chsets; i++) {
1243  n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1244  k = get_bits(&s->gb, 2) + 5;
1245  for(j = 0; j < n_xbr_ch[i]; j++) {
1246  active_bands[i][j] = get_bits(&s->gb, k) + 1;
1247  if (active_bands[i][j] > DCA_SUBBANDS) {
1248  av_log(s->avctx, AV_LOG_ERROR, "too many active subbands (%d)\n", active_bands[i][j]);
1249  return AVERROR_INVALIDDATA;
1250  }
1251  }
1252  }
1253 
1254  /* skip to the end of the header */
1255  i = get_bits_count(&s->gb);
1256  if(hdr_pos + hdr_size * 8 > i)
1257  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1258 
1259  /* loop over the channel data sets */
1260  /* only decode as many channels as we've decoded base data for */
1261  for(chset = 0, chan_base = 0;
1262  chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1263  chan_base += n_xbr_ch[chset++]) {
1264  int start_posn = get_bits_count(&s->gb);
1265  int subsubframe = 0;
1266  int subframe = 0;
1267 
1268  /* loop over subframes */
1269  for (k = 0; k < (s->sample_blocks / 8); k++) {
1270  /* parse header if we're on first subsubframe of a block */
1271  if(subsubframe == 0) {
1272  /* Parse subframe header */
1273  for(i = 0; i < n_xbr_ch[chset]; i++) {
1274  anctemp[i] = get_bits(&s->gb, 2) + 2;
1275  }
1276 
1277  for(i = 0; i < n_xbr_ch[chset]; i++) {
1278  get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1279  }
1280 
1281  for(i = 0; i < n_xbr_ch[chset]; i++) {
1282  anctemp[i] = get_bits(&s->gb, 3);
1283  if(anctemp[i] < 1) {
1284  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1285  return AVERROR_INVALIDDATA;
1286  }
1287  }
1288 
1289  /* generate scale factors */
1290  for(i = 0; i < n_xbr_ch[chset]; i++) {
1291  const uint32_t *scale_table;
1292  int nbits;
1293  int scale_table_size;
1294 
1295  if (s->scalefactor_huffman[chan_base+i] == 6) {
1296  scale_table = ff_dca_scale_factor_quant7;
1297  scale_table_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1298  } else {
1299  scale_table = ff_dca_scale_factor_quant6;
1300  scale_table_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1301  }
1302 
1303  nbits = anctemp[i];
1304 
1305  for(j = 0; j < active_bands[chset][i]; j++) {
1306  if(abits_high[i][j] > 0) {
1307  int index = get_bits(&s->gb, nbits);
1308  if (index >= scale_table_size) {
1309  av_log(s->avctx, AV_LOG_ERROR, "scale table index %d invalid\n", index);
1310  return AVERROR_INVALIDDATA;
1311  }
1312  scale_table_high[i][j][0] = scale_table[index];
1313 
1314  if(xbr_tmode && s->transition_mode[i][j]) {
1315  int index = get_bits(&s->gb, nbits);
1316  if (index >= scale_table_size) {
1317  av_log(s->avctx, AV_LOG_ERROR, "scale table index %d invalid\n", index);
1318  return AVERROR_INVALIDDATA;
1319  }
1320  scale_table_high[i][j][1] = scale_table[index];
1321  }
1322  }
1323  }
1324  }
1325  }
1326 
1327  /* decode audio array for this block */
1328  for(i = 0; i < n_xbr_ch[chset]; i++) {
1329  for(j = 0; j < active_bands[chset][i]; j++) {
1330  const int xbr_abits = abits_high[i][j];
1331  const float quant_step_size = ff_dca_lossless_quant_d[xbr_abits];
1332  const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1333  const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1334  float *subband_samples = s->subband_samples[k][chan_base+i][j];
1335  int block[8];
1336 
1337  if(xbr_abits <= 0)
1338  continue;
1339 
1340  if(xbr_abits > 7) {
1341  get_array(&s->gb, block, 8, xbr_abits - 3);
1342  } else {
1343  int block_code1, block_code2, size, levels, err;
1344 
1345  size = abits_sizes[xbr_abits - 1];
1346  levels = abits_levels[xbr_abits - 1];
1347 
1348  block_code1 = get_bits(&s->gb, size);
1349  block_code2 = get_bits(&s->gb, size);
1350  err = decode_blockcodes(block_code1, block_code2,
1351  levels, block);
1352  if (err) {
1354  "ERROR: DTS-XBR: block code look-up failed\n");
1355  return AVERROR_INVALIDDATA;
1356  }
1357  }
1358 
1359  /* scale & sum into subband */
1360  for(l = 0; l < 8; l++)
1361  subband_samples[l] += (float)block[l] * rscale;
1362  }
1363  }
1364 
1365  /* check DSYNC marker */
1366  if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1367  if(get_bits(&s->gb, 16) != 0xffff) {
1368  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1369  return AVERROR_INVALIDDATA;
1370  }
1371  }
1372 
1373  /* advance sub-sub-frame index */
1374  if(++subsubframe >= s->subsubframes[subframe]) {
1375  subsubframe = 0;
1376  subframe++;
1377  }
1378  }
1379 
1380  /* skip to next channel set */
1381  i = get_bits_count(&s->gb);
1382  if(start_posn + chset_fsize[chset] * 8 != i) {
1383  j = start_posn + chset_fsize[chset] * 8 - i;
1384  if(j < 0 || j >= 8)
1385  av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1386  " skipping further than expected (%d bits)\n", j);
1387  skip_bits_long(&s->gb, j);
1388  }
1389  }
1390 
1391  return 0;
1392 }
1393 
1394 
1395 /* parse initial header for XXCH and dump details */
1397 {
1398  int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1399  int i, chset, base_channel, chstart, fsize[8];
1400 
1401  /* assume header word has already been parsed */
1402  hdr_pos = get_bits_count(&s->gb) - 32;
1403  hdr_size = get_bits(&s->gb, 6) + 1;
1404  /*chhdr_crc =*/ skip_bits1(&s->gb);
1405  spkmsk_bits = get_bits(&s->gb, 5) + 1;
1406  num_chsets = get_bits(&s->gb, 2) + 1;
1407 
1408  for (i = 0; i < num_chsets; i++)
1409  fsize[i] = get_bits(&s->gb, 14) + 1;
1410 
1411  core_spk = get_bits(&s->gb, spkmsk_bits);
1412  s->xxch_core_spkmask = core_spk;
1413  s->xxch_nbits_spk_mask = spkmsk_bits;
1414  s->xxch_dmix_embedded = 0;
1415 
1416  /* skip to the end of the header */
1417  i = get_bits_count(&s->gb);
1418  if (hdr_pos + hdr_size * 8 > i)
1419  skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1420 
1421  for (chset = 0; chset < num_chsets; chset++) {
1422  chstart = get_bits_count(&s->gb);
1423  base_channel = s->prim_channels;
1424  s->xxch_chset = chset;
1425 
1426  /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1427  5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1428  dca_parse_audio_coding_header(s, base_channel, 1);
1429 
1430  /* decode channel data */
1431  for (i = 0; i < (s->sample_blocks / 8); i++) {
1432  if (dca_decode_block(s, base_channel, i)) {
1434  "Error decoding DTS-XXCH extension\n");
1435  continue;
1436  }
1437  }
1438 
1439  /* skip to end of this section */
1440  i = get_bits_count(&s->gb);
1441  if (chstart + fsize[chset] * 8 > i)
1442  skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1443  }
1444  s->xxch_chset = num_chsets;
1445 
1446  return 0;
1447 }
1448 
1449 static float dca_dmix_code(unsigned code)
1450 {
1451  int sign = (code >> 8) - 1;
1452  code &= 0xff;
1453  return ((ff_dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1 << 15));
1454 }
1455 
1456 /**
1457  * Main frame decoding function
1458  * FIXME add arguments
1459  */
1460 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1461  int *got_frame_ptr, AVPacket *avpkt)
1462 {
1463  AVFrame *frame = data;
1464  const uint8_t *buf = avpkt->data;
1465  int buf_size = avpkt->size;
1466  int channel_mask;
1467  int channel_layout;
1468  int lfe_samples;
1469  int num_core_channels = 0;
1470  int i, ret;
1471  float **samples_flt;
1472  float *src_chan;
1473  float *dst_chan;
1474  DCAContext *s = avctx->priv_data;
1475  int core_ss_end;
1476  int channels, full_channels;
1477  float scale;
1478  int achan;
1479  int chset;
1480  int mask;
1481  int lavc;
1482  int posn;
1483  int j, k;
1484  int endch;
1485  int upsample = 0;
1486 
1487  s->exss_ext_mask = 0;
1488  s->xch_present = 0;
1489 
1491  for (i = 0; i < buf_size - 3 && s->dca_buffer_size == AVERROR_INVALIDDATA; i++)
1492  s->dca_buffer_size = avpriv_dca_convert_bitstream(buf + i, buf_size - i, s->dca_buffer,
1494 
1496  av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1497  return AVERROR_INVALIDDATA;
1498  }
1499 
1500  if ((ret = dca_parse_frame_header(s)) < 0) {
1501  // seems like the frame is corrupt, try with the next one
1502  return ret;
1503  }
1504  // set AVCodec values with parsed data
1505  avctx->sample_rate = s->sample_rate;
1506 
1507  s->profile = FF_PROFILE_DTS;
1508 
1509  for (i = 0; i < (s->sample_blocks / 8); i++) {
1510  if ((ret = dca_decode_block(s, 0, i))) {
1511  av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1512  return ret;
1513  }
1514  }
1515 
1516  /* record number of core channels incase less than max channels are requested */
1517  num_core_channels = s->prim_channels;
1518 
1519  if (s->prim_channels + !!s->lfe > 2 &&
1521  /* Stereo downmix coefficients
1522  *
1523  * The decoder can only downmix to 2-channel, so we need to ensure
1524  * embedded downmix coefficients are actually targeting 2-channel.
1525  */
1526  if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1528  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1529  /* Range checked earlier */
1530  s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1531  s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1532  }
1533  s->output = s->core_downmix_amode;
1534  } else {
1535  int am = s->amode & DCA_CHANNEL_MASK;
1538  "Invalid channel mode %d\n", am);
1539  return AVERROR_INVALIDDATA;
1540  }
1541  if (num_core_channels + !!s->lfe >
1543  avpriv_request_sample(s->avctx, "Downmixing %d channels",
1544  s->prim_channels + !!s->lfe);
1545  return AVERROR_PATCHWELCOME;
1546  }
1547  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1548  s->downmix_coef[i][0] = ff_dca_default_coeffs[am][i][0];
1549  s->downmix_coef[i][1] = ff_dca_default_coeffs[am][i][1];
1550  }
1551  }
1552  ff_dlog(s->avctx, "Stereo downmix coeffs:\n");
1553  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1554  ff_dlog(s->avctx, "L, input channel %d = %f\n", i,
1555  s->downmix_coef[i][0]);
1556  ff_dlog(s->avctx, "R, input channel %d = %f\n", i,
1557  s->downmix_coef[i][1]);
1558  }
1559  ff_dlog(s->avctx, "\n");
1560  }
1561 
1562  if (s->ext_coding)
1564  else
1565  s->core_ext_mask = 0;
1566 
1567  core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1568 
1569  /* only scan for extensions if ext_descr was unknown or indicated a
1570  * supported XCh extension */
1571  if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
1572  /* if ext_descr was unknown, clear s->core_ext_mask so that the
1573  * extensions scan can fill it up */
1574  s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1575 
1576  /* extensions start at 32-bit boundaries into bitstream */
1577  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1578 
1579  while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1580  uint32_t bits = get_bits_long(&s->gb, 32);
1581 
1582  switch (bits) {
1583  case DCA_SYNCWORD_XCH: {
1584  int ext_amode, xch_fsize;
1585 
1587 
1588  /* validate sync word using XCHFSIZE field */
1589  xch_fsize = show_bits(&s->gb, 10);
1590  if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1591  (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1592  continue;
1593 
1594  /* skip length-to-end-of-frame field for the moment */
1595  skip_bits(&s->gb, 10);
1596 
1597  s->core_ext_mask |= DCA_EXT_XCH;
1598 
1599  /* extension amode(number of channels in extension) should be 1 */
1600  /* AFAIK XCh is not used for more channels */
1601  if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1602  av_log(avctx, AV_LOG_ERROR,
1603  "XCh extension amode %d not supported!\n",
1604  ext_amode);
1605  continue;
1606  }
1607 
1608  if (s->xch_base_channel < 2) {
1609  avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
1610  continue;
1611  }
1612 
1613  /* much like core primary audio coding header */
1615 
1616  for (i = 0; i < (s->sample_blocks / 8); i++)
1617  if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1618  av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1619  continue;
1620  }
1621 
1622  s->xch_present = 1;
1623  break;
1624  }
1625  case DCA_SYNCWORD_XXCH:
1626  /* XXCh: extended channels */
1627  /* usually found either in core or HD part in DTS-HD HRA streams,
1628  * but not in DTS-ES which contains XCh extensions instead */
1631  break;
1632 
1633  case 0x1d95f262: {
1634  int fsize96 = show_bits(&s->gb, 12) + 1;
1635  if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1636  continue;
1637 
1638  av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1639  get_bits_count(&s->gb));
1640  skip_bits(&s->gb, 12);
1641  av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1642  av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1643 
1644  s->core_ext_mask |= DCA_EXT_X96;
1645  break;
1646  }
1647  }
1648 
1649  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1650  }
1651  } else {
1652  /* no supported extensions, skip the rest of the core substream */
1653  skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1654  }
1655 
1656  if (s->core_ext_mask & DCA_EXT_X96)
1658  else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1660 
1661  /* check for ExSS (HD part) */
1662  if (s->dca_buffer_size - s->frame_size > 32 &&
1665 
1666  avctx->profile = s->profile;
1667 
1668  full_channels = channels = s->prim_channels + !!s->lfe;
1669 
1670  /* If we have XXCH then the channel layout is managed differently */
1671  /* note that XLL will also have another way to do things */
1672  if (!(s->core_ext_mask & DCA_EXT_XXCH)
1673  || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
1674  && avctx->request_channels
1675  < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
1676  { /* xxx should also do MA extensions */
1677  if (s->amode < 16) {
1679 
1680  if (s->prim_channels + !!s->lfe > 2 &&
1682  /*
1683  * Neither the core's auxiliary data nor our default tables contain
1684  * downmix coefficients for the additional channel coded in the XCh
1685  * extension, so when we're doing a Stereo downmix, don't decode it.
1686  */
1687  s->xch_disable = 1;
1688  }
1689 
1690 #if FF_API_REQUEST_CHANNELS
1692  if (s->xch_present && !s->xch_disable &&
1693  (!avctx->request_channels ||
1694  avctx->request_channels > num_core_channels + !!s->lfe)) {
1696 #else
1697  if (s->xch_present && !s->xch_disable) {
1698 #endif
1699  if (avctx->channel_layout & AV_CH_BACK_CENTER) {
1700  avpriv_request_sample(avctx, "XCh with Back center channel");
1701  return AVERROR_INVALIDDATA;
1702  }
1704  if (s->lfe) {
1707  } else {
1709  }
1710  if (s->channel_order_tab[s->xch_base_channel] < 0)
1711  return AVERROR_INVALIDDATA;
1712  } else {
1713  channels = num_core_channels + !!s->lfe;
1714  s->xch_present = 0; /* disable further xch processing */
1715  if (s->lfe) {
1718  } else
1720  }
1721 
1722  if (channels > !!s->lfe &&
1723  s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1724  return AVERROR_INVALIDDATA;
1725 
1726  if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
1727  av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
1728  return AVERROR_INVALIDDATA;
1729  }
1730 
1731  if (num_core_channels + !!s->lfe > 2 &&
1733  channels = 2;
1734  s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1736  }
1737  else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
1738  static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
1739  s->channel_order_tab = dca_channel_order_native;
1740  }
1741  s->lfe_index = ff_dca_lfe_index[s->amode];
1742  } else {
1743  av_log(avctx, AV_LOG_ERROR,
1744  "Non standard configuration %d !\n", s->amode);
1745  return AVERROR_INVALIDDATA;
1746  }
1747 
1748  s->xxch_dmix_embedded = 0;
1749  } else {
1750  /* we only get here if an XXCH channel set can be added to the mix */
1751  channel_mask = s->xxch_core_spkmask;
1752 
1753  if (avctx->request_channels > 0
1754  && avctx->request_channels < s->prim_channels) {
1755  channels = num_core_channels + !!s->lfe;
1756  for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
1757  <= avctx->request_channels; i++) {
1758  channels += s->xxch_chset_nch[i];
1759  channel_mask |= s->xxch_spk_masks[i];
1760  }
1761  } else {
1762  channels = s->prim_channels + !!s->lfe;
1763  for (i = 0; i < s->xxch_chset; i++) {
1764  channel_mask |= s->xxch_spk_masks[i];
1765  }
1766  }
1767 
1768  /* Given the DTS spec'ed channel mask, generate an avcodec version */
1769  channel_layout = 0;
1770  for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
1771  if (channel_mask & (1 << i)) {
1772  channel_layout |= ff_dca_map_xxch_to_native[i];
1773  }
1774  }
1775 
1776  /* make sure that we have managed to get equivalent dts/avcodec channel
1777  * masks in some sense -- unfortunately some channels could overlap */
1778  if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
1779  av_log(avctx, AV_LOG_DEBUG,
1780  "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
1781  return AVERROR_INVALIDDATA;
1782  }
1783 
1784  avctx->channel_layout = channel_layout;
1785 
1786  if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
1787  /* Estimate DTS --> avcodec ordering table */
1788  for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
1789  mask = chset >= 0 ? s->xxch_spk_masks[chset]
1790  : s->xxch_core_spkmask;
1791  for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
1792  if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
1793  lavc = ff_dca_map_xxch_to_native[i];
1794  posn = av_popcount(channel_layout & (lavc - 1));
1795  s->xxch_order_tab[j++] = posn;
1796  }
1797  }
1798 
1799  }
1800 
1801  s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
1802  } else { /* native ordering */
1803  for (i = 0; i < channels; i++)
1804  s->xxch_order_tab[i] = i;
1805 
1806  s->lfe_index = channels - 1;
1807  }
1808 
1810  }
1811 
1812  /* get output buffer */
1813  frame->nb_samples = 256 * (s->sample_blocks / 8);
1814  if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
1815  int xll_nb_samples = s->xll_segments * s->xll_smpl_in_seg;
1816  /* Check for invalid/unsupported conditions first */
1817  if (s->xll_residual_channels > channels) {
1819  "DCA: too many residual channels (%d, core channels %d). Disabling XLL\n",
1820  s->xll_residual_channels, channels);
1822  } else if (xll_nb_samples != frame->nb_samples &&
1823  2 * frame->nb_samples != xll_nb_samples) {
1825  "DCA: unsupported upsampling (%d XLL samples, %d core samples). Disabling XLL\n",
1826  xll_nb_samples, frame->nb_samples);
1828  } else {
1829  if (2 * frame->nb_samples == xll_nb_samples) {
1830  av_log(s->avctx, AV_LOG_INFO,
1831  "XLL: upsampling core channels by a factor of 2\n");
1832  upsample = 1;
1833 
1834  frame->nb_samples = xll_nb_samples;
1835  // FIXME: Is it good enough to copy from the first channel set?
1836  avctx->sample_rate = s->xll_chsets[0].sampling_frequency;
1837  }
1838  /* If downmixing to stereo, don't decode additional channels.
1839  * FIXME: Using the xch_disable flag for this doesn't seem right. */
1840  if (!s->xch_disable)
1841  channels = s->xll_channels;
1842  }
1843  }
1844 
1845  if (avctx->channels != channels) {
1846  if (avctx->channels)
1847  av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
1848  avctx->channels = channels;
1849  }
1850 
1851  /* FIXME: This is an ugly hack, to just revert to the default
1852  * layout if we have additional channels. Need to convert the XLL
1853  * channel masks to ffmpeg channel_layout mask. */
1855  avctx->channel_layout = 0;
1856 
1857  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1858  return ret;
1859  samples_flt = (float **) frame->extended_data;
1860 
1861  /* allocate buffer for extra channels if downmixing */
1862  if (avctx->channels < full_channels) {
1863  ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1864  frame->nb_samples,
1865  avctx->sample_fmt, 0);
1866  if (ret < 0)
1867  return ret;
1868 
1870  &s->extra_channels_buffer_size, ret);
1871  if (!s->extra_channels_buffer)
1872  return AVERROR(ENOMEM);
1873 
1876  full_channels - channels,
1877  frame->nb_samples, avctx->sample_fmt, 0);
1878  if (ret < 0)
1879  return ret;
1880  }
1881 
1882  /* filter to get final output */
1883  for (i = 0; i < (s->sample_blocks / 8); i++) {
1884  int ch;
1885  unsigned block = upsample ? 512 : 256;
1886  for (ch = 0; ch < channels; ch++)
1887  s->samples_chanptr[ch] = samples_flt[ch] + i * block;
1888  for (; ch < full_channels; ch++)
1889  s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * block;
1890 
1891  dca_filter_channels(s, i, upsample);
1892 
1893  /* If this was marked as a DTS-ES stream we need to subtract back- */
1894  /* channel from SL & SR to remove matrixed back-channel signal */
1895  if ((s->source_pcm_res & 1) && s->xch_present) {
1896  float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1897  float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1898  float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1899  s->fdsp->vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1900  s->fdsp->vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1901  }
1902 
1903  /* If stream contains XXCH, we might need to undo an embedded downmix */
1904  if (s->xxch_dmix_embedded) {
1905  /* Loop over channel sets in turn */
1906  ch = num_core_channels;
1907  for (chset = 0; chset < s->xxch_chset; chset++) {
1908  endch = ch + s->xxch_chset_nch[chset];
1909  mask = s->xxch_dmix_embedded;
1910 
1911  /* undo downmix */
1912  for (j = ch; j < endch; j++) {
1913  if (mask & (1 << j)) { /* this channel has been mixed-out */
1914  src_chan = s->samples_chanptr[s->channel_order_tab[j]];
1915  for (k = 0; k < endch; k++) {
1916  achan = s->channel_order_tab[k];
1917  scale = s->xxch_dmix_coeff[j][k];
1918  if (scale != 0.0) {
1919  dst_chan = s->samples_chanptr[achan];
1920  s->fdsp->vector_fmac_scalar(dst_chan, src_chan,
1921  -scale, 256);
1922  }
1923  }
1924  }
1925  }
1926 
1927  /* if a downmix has been embedded then undo the pre-scaling */
1928  if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
1929  scale = s->xxch_dmix_sf[chset];
1930 
1931  for (j = 0; j < ch; j++) {
1932  src_chan = s->samples_chanptr[s->channel_order_tab[j]];
1933  for (k = 0; k < 256; k++)
1934  src_chan[k] *= scale;
1935  }
1936 
1937  /* LFE channel is always part of core, scale if it exists */
1938  if (s->lfe) {
1939  src_chan = s->samples_chanptr[s->lfe_index];
1940  for (k = 0; k < 256; k++)
1941  src_chan[k] *= scale;
1942  }
1943  }
1944 
1945  ch = endch;
1946  }
1947 
1948  }
1949  }
1950 
1951  /* update lfe history */
1952  lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1953  for (i = 0; i < 2 * s->lfe * 4; i++)
1954  s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1955 
1957  ret = ff_dca_xll_decode_audio(s, frame);
1958  if (ret < 0)
1959  return ret;
1960  }
1961  /* AVMatrixEncoding
1962  *
1963  * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
1965  (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
1967  if (ret < 0)
1968  return ret;
1969 
1970  if ( avctx->profile != FF_PROFILE_DTS_HD_MA
1971  && avctx->profile != FF_PROFILE_DTS_HD_HRA)
1972  avctx->bit_rate = s->bit_rate;
1973  *got_frame_ptr = 1;
1974 
1975  return buf_size;
1976 }
1977 
1978 /**
1979  * DCA initialization
1980  *
1981  * @param avctx pointer to the AVCodecContext
1982  */
1983 
1985 {
1986  DCAContext *s = avctx->priv_data;
1987 
1988  s->avctx = avctx;
1989  dca_init_vlcs();
1990 
1992  if (!s->fdsp)
1993  return AVERROR(ENOMEM);
1994 
1995  ff_mdct_init(&s->imdct, 6, 1, 1.0);
1997  ff_dcadsp_init(&s->dcadsp);
1998  ff_fmt_convert_init(&s->fmt_conv, avctx);
1999 
2000  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2001 
2002  /* allow downmixing to stereo */
2003 #if FF_API_REQUEST_CHANNELS
2005  if (avctx->request_channels == 2)
2008 #endif
2009  if (avctx->channels > 2 &&
2011  avctx->channels = 2;
2012 
2013  return 0;
2014 }
2015 
2017 {
2018  DCAContext *s = avctx->priv_data;
2019  ff_mdct_end(&s->imdct);
2021  av_freep(&s->fdsp);
2022  av_freep(&s->xll_sample_buf);
2023  av_freep(&s->qmf64_table);
2024  return 0;
2025 }
2026 
2027 static const AVProfile profiles[] = {
2028  { FF_PROFILE_DTS, "DTS" },
2029  { FF_PROFILE_DTS_ES, "DTS-ES" },
2030  { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2031  { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2032  { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2033  { FF_PROFILE_UNKNOWN },
2034 };
2035 
2036 static const AVOption options[] = {
2037  { "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 },
2038  { "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 },
2039  { NULL },
2040 };
2041 
2042 static const AVClass dca_decoder_class = {
2043  .class_name = "DCA decoder",
2044  .item_name = av_default_item_name,
2045  .option = options,
2046  .version = LIBAVUTIL_VERSION_INT,
2047  .category = AV_CLASS_CATEGORY_DECODER,
2048 };
2049 
2051  .name = "dca",
2052  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2053  .type = AVMEDIA_TYPE_AUDIO,
2054  .id = AV_CODEC_ID_DTS,
2055  .priv_data_size = sizeof(DCAContext),
2056  .init = dca_decode_init,
2058  .close = dca_decode_end,
2059  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2060  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2062  .profiles = NULL_IF_CONFIG_SMALL(profiles),
2063  .priv_class = &dca_decoder_class,
2064 };
int wrap
wrap for get_vlc2()
Definition: dcadec.c:120
const float ff_dca_lfe_xll_fir_64[256]
Definition: dcadata.c:7529
float, planar
Definition: samplefmt.h:70
int ext_descr
extension audio descriptor flag
Definition: dca.h:153
#define NULL
Definition: coverity.c:32
const float ff_dca_lossless_quant_d[32]
Definition: dcadata.c:4207
static const int8_t bitalloc_offsets[10]
Definition: dcahuff.h:988
float v
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
quantization index codebook select
Definition: dca.h:177
int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]
bit allocation quantizer select
Definition: dca.h:176
int crc_present
crc is present in the bitstream
Definition: dca.h:141
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
const int8_t ff_dca_channel_reorder_lfe_xch[16][9]
Definition: dcadata.c:8433
int timestamp
embedded time stamp flag
Definition: dca.h:150
int amode
audio channels arrangement
Definition: dca.h:144
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
Definition: dcadec.c:851
lossless extension in ExSS
Definition: dca.h:77
static const AVProfile profiles[]
Definition: dcadec.c:2027
const float ff_dca_lossy_quant_d[32]
Definition: dcadata.c:4192
static const uint16_t tmode_codes[TMODE_COUNT][4]
Definition: dcahuff.h:31
int transient_huffman[DCA_PRIM_CHANNELS_MAX]
transient mode code book
Definition: dca.h:174
const uint32_t ff_dca_map_xxch_to_native[28]
Definition: dcadata.c:8321
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
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
static void qmf_64_subbands(DCAContext *s, int chans, float samples_in[64][8], float *samples_out, float scale)
Definition: dcadec.c:667
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
attribute_deprecated int request_channels
Decoder should decode to this many channels if it can (0 for default)
Definition: avcodec.h:2038
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
FmtConvertContext fmt_conv
Definition: dca.h:286
static int dca_parse_frame_header(DCAContext *s)
Definition: dcadec.c:349
float dct2_coeff[32][32]
Definition: dca.h:130
void(* lfe_fir[2])(float *out, const float *in, const float *coefs)
Definition: dcadsp.h:28
#define M_SQRT1_2
Definition: mathematics.h:52
int vq_start_subband[DCA_PRIM_CHANNELS_MAX]
high frequency vq start subband
Definition: dca.h:172
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:290
int size
Definition: avcodec.h:1163
int hist_index[DCA_PRIM_CHANNELS_MAX]
Definition: dca.h:211
int samples_deficit
deficit sample count
Definition: dca.h:140
uint8_t core_downmix
embedded downmix coefficients available
Definition: dca.h:196
Definition: dcadec.c:64
#define DCA_MAX_FRAME_SIZE
Definition: dca.h:62
int dynrange
embedded dynamic range flag
Definition: dca.h:149
int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
joint subband scale factors
Definition: dca.h:189
int version
encoder software revision
Definition: dca.h:160
#define VLC_TYPE
Definition: get_bits.h:61
#define FF_ARRAY_ELEMS(a)
#define AV_CH_LAYOUT_STEREO
static BitAlloc dca_scalefactor
scalefactor VLCs
Definition: dcadec.c:126
static void lfe_interpolation_fir(DCAContext *s, const float *samples_in, float *samples_out)
Definition: dcadec.c:717
SynthFilterContext synth
Definition: dca.h:283
int profile
profile
Definition: avcodec.h:2835
int ff_dca_xbr_parse_frame(DCAContext *s)
Definition: dcadec.c:1218
AVCodec.
Definition: avcodec.h:3181
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:245
const float ff_dca_fir_32bands_nonperfect[512]
Definition: dcadata.c:6785
Macro definitions for various function/variable attributes.
int maxbits[8]
max bits in VLC
Definition: dcadec.c:119
float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]
Definition: dca.h:248
const int8_t ff_dca_high_freq_vq[1024][32]
Definition: dcadata.c:4217
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
float * extra_channels[DCA_PRIM_CHANNELS_MAX+1]
Definition: dca.h:218
const float ff_dca_fir_64bands[1024]
Definition: dcadata.c:7597
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
static int dca_subframe_footer(DCAContext *s, int base_channel)
Definition: dcadec.c:1073
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:72
int xll_channels
total number of channels (in all channel sets)
Definition: dca.h:256
static BitAlloc dca_tmode
transition mode VLCs
Definition: dcadec.c:125
#define DCA_MAX_EXSS_HEADER_SIZE
Definition: dca.h:63
#define MIX_FRONT3(samples, coef)
Definition: dcadec.c:760
#define FF_PROFILE_DTS_ES
Definition: avcodec.h:2851
#define DCA_CHSETS_MAX
Definition: dca.h:44
static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:430
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]
scale factor code book
Definition: dca.h:175
static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
Decode a dca frame block.
Definition: dcadec.c:1182
const uint32_t ff_dca_bit_rates[32]
Definition: dcadata.c:33
if()
Definition: avfilter.c:975
AVFloatDSPContext * fdsp
Definition: dca.h:281
static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:861
uint8_t bits
Definition: crc.c:295
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
uint8_t
#define av_cold
Definition: attributes.h:74
#define av_malloc(s)
uint32_t xxch_core_spkmask
Definition: dca.h:242
int8_t lfe_index
Definition: dca.h:251
int xch_base_channel
index of first (only) channel containing XCH data
Definition: dca.h:236
float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4]
Definition: dca.h:206
#define DCA_LFE
Definition: dcadec.c:109
AVOptions.
int xll_residual_channels
number of residual channels
Definition: dca.h:257
static int dca_parse_audio_coding_header(DCAContext *s, int base_channel, int xxch)
Definition: dcadec.c:211
int dca_buffer_size
how much data is in the dca_buffer
Definition: dca.h:223
#define MIX_REAR2(samples, s1, s2, rs, coef)
Definition: dcadec.c:756
static void get_array(GetBitContext *gb, int *dst, int len, int bits)
Definition: dcadec.c:193
96/24 extension in core substream
Definition: dca.h:70
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2836
int8_t xxch_order_tab[32]
Definition: dca.h:250
static av_cold int dca_decode_end(AVCodecContext *avctx)
Definition: dcadec.c:2016
void(* int32_to_float_fmul_array8)(struct FmtConvertContext *c, float *dst, const int32_t *src, const float *mul, int len)
Convert an array of int32_t to float and multiply by a float value from another array, stepping along the float array once for each 8 integers.
Definition: fmtconvert.h:53
const uint16_t ff_dca_vlc_offs[63]
Definition: dcadata.c:8490
#define AV_CH_LOW_FREQUENCY
int header_crc
header crc check bytes
Definition: dca.h:158
DCAMode
Definition: dcadec.c:58
#define FF_PROFILE_DTS_96_24
Definition: avcodec.h:2852
int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
transition mode (transients)
Definition: dca.h:186
static int dca_xxch2index(DCAContext *s, int xxch_ch)
Definition: dcadec.c:199
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:789
static AVFrame * frame
XllChSetSubHeader xll_chsets[DCA_XLL_CHSETS_MAX]
Definition: dca.h:266
av_cold void ff_dcadsp_init(DCADSPContext *s)
Definition: dcadsp.c:106
uint8_t * data
Definition: avcodec.h:1162
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
int sample_rate
audio sampling rate
Definition: dca.h:145
bitstream reader API header.
uint32_t xxch_spk_masks[4]
Definition: dca.h:243
AVCodecContext * avctx
Definition: dca.h:137
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:759
static BitAlloc dca_bitalloc_index
indexes for samples VLC select
Definition: dcadec.c:124
const int8_t ff_dca_channel_reorder_nolfe[16][9]
Definition: dcadata.c:8452
int lfe
low frequency effects flag
Definition: dca.h:156
ptrdiff_t size
Definition: opengl_enc.c:101
static BitAlloc dca_smpl_bitalloc[11]
samples VLCs
Definition: dcadec.c:127
#define A(x)
Definition: vp56_arith.h:28
static const uint8_t bitalloc_sizes[10]
Definition: dcahuff.h:984
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
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:773
#define DCA_CHANNEL_MASK
Definition: dcadec.c:107
int predictor_history
predictor history flag
Definition: dca.h:157
int dynrange_coef
dynamic range coefficient
Definition: dca.h:191
int joint_huff[DCA_PRIM_CHANNELS_MAX]
joint subband scale factors codebook
Definition: dca.h:188
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:588
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX+1][4]
embedded downmix coefficients (9-bit codes)
Definition: dca.h:198
FFTContext imdct
Definition: dca.h:282
float raXin[32]
Definition: dca.h:212
const float ff_dca_lfe_fir_128[256]
Definition: dcadata.c:7459
void(* decode_hf)(float dst[DCA_SUBBANDS][8], 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:35
float subband_samples[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]
Definition: dca.h:216
static const uint16_t mask[17]
Definition: lzw.c:38
#define MIX_REAR1(samples, s1, rs, coef)
Definition: dcadec.c:752
av_default_item_name
static const uint16_t bitalloc_12_codes[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:51
#define AVERROR(e)
Definition: error.h:43
#define FF_PROFILE_DTS
Definition: avcodec.h:2850
int current_subsubframe
Definition: dca.h:229
static const struct endianess table[]
int hdcd
source material is mastered in HDCD
Definition: dca.h:152
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition: dcadata.c:8133
void(* qmf_32_subbands)(float samples_in[32][8], 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:29
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const float ff_dca_lfe_fir_64[256]
Definition: dcadata.c:7316
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1335
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:3188
int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
bit allocation index
Definition: dca.h:185
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:1460
#define ff_mdct_init
Definition: fft.h:167
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int debug_flag
used for suppressing repeated error messages output
Definition: dca.h:280
#define FFMAX(a, b)
Definition: common.h:64
Libavcodec external API header.
float rcos[32]
Definition: dca.h:131
Definition: get_bits.h:63
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
const int8_t * channel_order_tab
channel reordering table, lfe and non lfe
Definition: dca.h:225
float lfe_data[2 *DCA_LFE_MAX *(DCA_BLOCKS_MAX+4)]
Low frequency effect data.
Definition: dca.h:202
common internal API header
static const uint8_t tmode_bits[TMODE_COUNT][4]
Definition: dcahuff.h:38
static void qmf_32_subbands(DCAContext *s, int chans, float samples_in[32][8], float *samples_out, float scale)
Definition: dcadec.c:618
int front_sum
front sum/difference flag
Definition: dca.h:163
int xch_disable
whether the XCh extension should be decoded or not
Definition: dca.h:237
int source_pcm_res
source pcm resolution
Definition: dca.h:162
int * xll_sample_buf
Definition: dca.h:268
#define FF_PROFILE_DTS_HD_HRA
Definition: avcodec.h:2853
int bit_rate
the average bitrate
Definition: avcodec.h:1305
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:66
ret
Definition: avfilter.c:974
int surround_sum
surround sum/difference flag
Definition: dca.h:164
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
static av_cold void dca_init_vlcs(void)
Definition: dcadec.c:138
av_cold void ff_synth_filter_init(SynthFilterContext *c)
Definition: synth_filter.c:59
int32_t
uint8_t dca_buffer[DCA_MAX_FRAME_SIZE+DCA_MAX_EXSS_HEADER_SIZE+DCA_BUFFER_PADDING_SIZE]
Definition: dca.h:222
int avpriv_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:39
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:287
int joint_intensity[DCA_PRIM_CHANNELS_MAX]
joint intensity coding index
Definition: dca.h:173
static const uint16_t *const bitalloc_codes[10][8]
Definition: dcahuff.h:1005
int multirate_inter
multirate interpolator switch
Definition: dca.h:159
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:555
float u
int n
Definition: avisynth_c.h:547
Bit allocation.
Definition: dcadec.c:117
uint8_t core_downmix_amode
audio channel arrangement of embedded downmix
Definition: dca.h:197
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:34
int bit_rate
transmission bit rate
Definition: dca.h:146
const uint64_t ff_dca_core_channel_layout[16]
Definition: dcadata.c:8373
static const uint8_t *const bitalloc_bits[10][8]
Definition: dcahuff.h:1023
#define INIT_VLC_USE_NEW_STATIC
Definition: get_bits.h:474
int offset
code values offset
Definition: dcadec.c:118
static const uint8_t abits_levels[7]
Definition: dcadec.c:859
#define DCA_CHSET_CHANS_MAX
Definition: dca.h:45
int xch_present
XCh extension present and valid.
Definition: dca.h:235
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:259
int bits
Definition: get_bits.h:64
#define SCALES_VLC_BITS
Definition: dcahuff.h:73
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
const int16_t ff_dca_adpcm_vb[4096][4]
Definition: dcadata.c:53
int table_allocated
Definition: get_bits.h:66
const uint32_t ff_dca_scale_factor_quant7[128]
Definition: dcadata.c:4165
int core_ext_mask
present extensions in the core substream
Definition: dca.h:231
int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame)
Definition: dca_xll.c:409
int lfe_scale_factor
Definition: dca.h:203
const uint8_t ff_dca_channels[16]
Definition: dcadata.c:42
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
Definition: dcadec.c:1013
float dct4_coeff[32][32]
Definition: dca.h:129
#define ff_dlog(ctx,...)
Definition: internal.h:54
int aux_data
auxiliary data flag
Definition: dca.h:151
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int sample_rate
samples per second
Definition: avcodec.h:1985
int ext_coding
extended coding flag
Definition: dca.h:154
#define AV_CH_LAYOUT_NATIVE
Channel mask value used for AVCodecContext.request_channel_layout to indicate that the user requests ...
static float dca_dmix_code(unsigned code)
Definition: dcadec.c:1449
int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]
scale factors (2 if transient)
Definition: dca.h:187
int xxch_chset_nch[4]
Definition: dca.h:244
int subband_activity[DCA_PRIM_CHANNELS_MAX]
subband activity count
Definition: dca.h:171
main external API structure.
Definition: avcodec.h:1241
#define FASTDIV(a, b)
Definition: mathops.h:211
const int ff_dca_ext_audio_descr_mask[8]
Definition: dcadata.c:8353
float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
scale factor adjustment
Definition: dca.h:178
int copy_history
copy history
Definition: dca.h:161
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
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-> in
const float ff_dca_default_coeffs[10][6][2]
Definition: dcadata.c:8196
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:457
void * buf
Definition: avisynth_c.h:553
const int8_t ff_dca_channel_reorder_nolfe_xch[16][9]
Definition: dcadata.c:8471
DCAXxchSpeakerMask
Definition: dcadec.c:73
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:329
VLC vlc[8]
actual codes
Definition: dcadec.c:121
Describe the class of an AVClass context structure.
Definition: log.h:67
int exss_ext_mask
Non-core extensions.
Definition: dca.h:232
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:297
const uint32_t ff_dca_scale_factor_quant6[64]
Definition: dcadata.c:4154
int index
Definition: gxfenc.c:89
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:117
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:286
int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
VQ encoded high frequency subbands.
Definition: dca.h:200
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 sample_blocks
number of PCM sample blocks
Definition: dca.h:142
static const uint8_t abits_sizes[7]
Definition: dcadec.c:858
const int8_t ff_dca_channel_reorder_lfe[16][9]
Definition: dcadata.c:8414
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:143
AVCodec ff_dca_decoder
Definition: dcadec.c:2050
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
int current_subframe
Definition: dca.h:228
static const AVOption options[]
Definition: dcadec.c:2036
float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][64]
Definition: dca.h:210
static int decode_blockcode(int code, int levels, int32_t *values)
Definition: dcadec.c:837
#define AV_CH_BACK_CENTER
uint8_t level
Definition: svq3.c:150
int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
prediction mode (ADPCM used or not)
Definition: dca.h:183
static av_cold int dca_decode_init(AVCodecContext *avctx)
DCA initialization.
Definition: dcadec.c:1984
#define M_SQRT2
Definition: mathematics.h:55
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:522
int xxch_chset
Definition: dca.h:240
DCADSPContext dcadsp
Definition: dca.h:284
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
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
common internal api header.
common internal and external API header
#define AV_COPY128(d, s)
Definition: intreadwrite.h:594
uint8_t * extra_channels_buffer
Definition: dca.h:219
#define CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:856
#define ff_mdct_end
Definition: fft.h:168
int total_channels
number of channels including extensions
Definition: dca.h:169
static double c[64]
static const uint16_t scales_codes[SCALES_COUNT][129]
Definition: dcahuff.h:74
int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
prediction VQ coefs
Definition: dca.h:184
int dialog_norm
dialog normalisation parameter
Definition: dca.h:165
#define DCA_NSYNCAUX
Definition: dcadec.c:113
AVProfile.
Definition: avcodec.h:3169
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:2854
int xll_smpl_in_seg
samples in segment per one frequency band for the first channel set
Definition: dca.h:260
float subband_fir_hist[DCA_PRIM_CHANNELS_MAX][1024]
Definition: dca.h:209
#define DCA_SUBBANDS
Definition: dcadsp.h:25
int bit_rate_index
transmission bit rate index
Definition: dca.h:147
void ff_dca_exss_parse_header(DCAContext *s)
Parse extension substream header (HD)
Definition: dca_exss.c:245
void * priv_data
Definition: avcodec.h:1283
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 plane data pointers and linesize for samples with sample format sample_fmt.
Definition: samplefmt.c:149
static const uint8_t bitalloc_maxbits[10][7]
Definition: dcahuff.h:992
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:44
int len
int channels
number of audio channels
Definition: avcodec.h:1986
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:65
float * samples_chanptr[DCA_PRIM_CHANNELS_MAX+1]
Definition: dca.h:217
int ff_dca_xxch_decode_frame(DCAContext *s)
Definition: dcadec.c:1396
int subsubframes[DCA_SUBFRAMES_MAX]
number of subsubframes
Definition: dca.h:181
QMF64_table * qmf64_table
Definition: dca.h:285
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
XCh channel extension in core substream.
Definition: dca.h:71
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:449
#define DOWNMIX_TO_STEREO(op1, op2)
Definition: dcadec.c:767
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 frame_size
primary frame byte size
Definition: dca.h:143
static QMF64_table * qmf64_precompute(void)
Definition: dcadec.c:641
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
float xxch_dmix_sf[DCA_CHSETS_MAX]
Definition: dca.h:245
int aspf
audio sync word insertion flag
Definition: dca.h:155
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:120
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:37
#define M_PI
Definition: mathematics.h:46
int prim_channels
number of primary audio channels
Definition: dca.h:170
float downmix_coef[DCA_PRIM_CHANNELS_MAX+1][2]
stereo downmix coefficients
Definition: dca.h:190
static const uint8_t bitalloc_12_vlc_bits[BITALLOC_12_COUNT]
Definition: dcahuff.h:47
int output
type of output
Definition: dca.h:214
static int get_scale(GetBitContext *gb, int level, int value, int log2range)
Definition: dcadec.c:413
const float ff_dca_fir_32bands_perfect[512]
Definition: dcadata.c:6270
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
int xxch_nbits_spk_mask
Definition: dca.h:241
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:2053
const int8_t ff_dca_lfe_index[16]
Definition: dcadata.c:8410
int partial_samples[DCA_SUBFRAMES_MAX]
partial subsubframe samples count
Definition: dca.h:182
static const AVClass dca_decoder_class
Definition: dcadec.c:2042
This structure stores compressed data.
Definition: avcodec.h:1139
float rsin[32]
Definition: dca.h:132
uint32_t xxch_dmix_embedded
Definition: dca.h:247
int subframes
number of subframes
Definition: dca.h:168
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
Definition: vf_geq.c:45
unsigned int extra_channels_buffer_size
Definition: dca.h:220
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:129
#define DCA_PRIM_CHANNELS_MAX
Definition: dca.h:47
int frame_type
type of the current frame
Definition: dca.h:139
XXCh channels extension in core substream.
Definition: dca.h:69
int profile
Definition: dca.h:277
static int16_t block[64]
Definition: dct-test.c:110