dca.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  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 
29 #include "libavutil/common.h"
30 #include "libavutil/intmath.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/audioconvert.h"
34 #include "avcodec.h"
35 #include "dsputil.h"
36 #include "fft.h"
37 #include "get_bits.h"
38 #include "put_bits.h"
39 #include "dcadata.h"
40 #include "dcahuff.h"
41 #include "dca.h"
42 #include "synth_filter.h"
43 #include "dcadsp.h"
44 #include "fmtconvert.h"
45 
46 #if ARCH_ARM
47 # include "arm/dca.h"
48 #endif
49 
50 //#define TRACE
51 
52 #define DCA_PRIM_CHANNELS_MAX (7)
53 #define DCA_SUBBANDS (32)
54 #define DCA_ABITS_MAX (32) /* Should be 28 */
55 #define DCA_SUBSUBFRAMES_MAX (4)
56 #define DCA_SUBFRAMES_MAX (16)
57 #define DCA_BLOCKS_MAX (16)
58 #define DCA_LFE_MAX (3)
59 
60 enum DCAMode {
61  DCA_MONO = 0,
72 };
73 
74 /* these are unconfirmed but should be mostly correct */
79  DCA_EXSS_LFE = 0x0008,
88  DCA_EXSS_LFE2 = 0x1000,
92 };
93 
95  DCA_EXT_CORE = 0x001,
96  DCA_EXT_XXCH = 0x002,
97  DCA_EXT_X96 = 0x004,
98  DCA_EXT_XCH = 0x008,
105 };
106 
107 /* -1 are reserved or unknown */
108 static const int dca_ext_audio_descr_mask[] = {
109  DCA_EXT_XCH,
110  -1,
111  DCA_EXT_X96,
113  -1,
114  -1,
115  DCA_EXT_XXCH,
116  -1,
117 };
118 
119 /* extensions that reside in core substream */
120 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
121 
122 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
123  * Some compromises have been made for special configurations. Most configurations
124  * are never used so complete accuracy is not needed.
125  *
126  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
127  * S -> side, when both rear and back are configured move one of them to the side channel
128  * OV -> center back
129  * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
130  */
131 static const uint64_t dca_core_channel_layout[] = {
137  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER,
138  AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER,
139  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,
140  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
141 
142  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
144 
145  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
147 
148  AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
150 
154 
156  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
158 
160  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
162 
164  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
166 };
167 
168 static const int8_t dca_lfe_index[] = {
169  1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
170 };
171 
172 static const int8_t dca_channel_reorder_lfe[][9] = {
173  { 0, -1, -1, -1, -1, -1, -1, -1, -1},
174  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
175  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
176  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
177  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
178  { 2, 0, 1, -1, -1, -1, -1, -1, -1},
179  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
180  { 2, 0, 1, 4, -1, -1, -1, -1, -1},
181  { 0, 1, 3, 4, -1, -1, -1, -1, -1},
182  { 2, 0, 1, 4, 5, -1, -1, -1, -1},
183  { 3, 4, 0, 1, 5, 6, -1, -1, -1},
184  { 2, 0, 1, 4, 5, 6, -1, -1, -1},
185  { 0, 6, 4, 5, 2, 3, -1, -1, -1},
186  { 4, 2, 5, 0, 1, 6, 7, -1, -1},
187  { 5, 6, 0, 1, 7, 3, 8, 4, -1},
188  { 4, 2, 5, 0, 1, 6, 8, 7, -1},
189 };
190 
191 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
192  { 0, 2, -1, -1, -1, -1, -1, -1, -1},
193  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
194  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
195  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
196  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
197  { 2, 0, 1, 4, -1, -1, -1, -1, -1},
198  { 0, 1, 3, 4, -1, -1, -1, -1, -1},
199  { 2, 0, 1, 4, 5, -1, -1, -1, -1},
200  { 0, 1, 4, 5, 3, -1, -1, -1, -1},
201  { 2, 0, 1, 5, 6, 4, -1, -1, -1},
202  { 3, 4, 0, 1, 6, 7, 5, -1, -1},
203  { 2, 0, 1, 4, 5, 6, 7, -1, -1},
204  { 0, 6, 4, 5, 2, 3, 7, -1, -1},
205  { 4, 2, 5, 0, 1, 7, 8, 6, -1},
206  { 5, 6, 0, 1, 8, 3, 9, 4, 7},
207  { 4, 2, 5, 0, 1, 6, 9, 8, 7},
208 };
209 
210 static const int8_t dca_channel_reorder_nolfe[][9] = {
211  { 0, -1, -1, -1, -1, -1, -1, -1, -1},
212  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
213  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
214  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
215  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
216  { 2, 0, 1, -1, -1, -1, -1, -1, -1},
217  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
218  { 2, 0, 1, 3, -1, -1, -1, -1, -1},
219  { 0, 1, 2, 3, -1, -1, -1, -1, -1},
220  { 2, 0, 1, 3, 4, -1, -1, -1, -1},
221  { 2, 3, 0, 1, 4, 5, -1, -1, -1},
222  { 2, 0, 1, 3, 4, 5, -1, -1, -1},
223  { 0, 5, 3, 4, 1, 2, -1, -1, -1},
224  { 3, 2, 4, 0, 1, 5, 6, -1, -1},
225  { 4, 5, 0, 1, 6, 2, 7, 3, -1},
226  { 3, 2, 4, 0, 1, 5, 7, 6, -1},
227 };
228 
229 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
230  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
231  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
232  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
233  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
234  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
235  { 2, 0, 1, 3, -1, -1, -1, -1, -1},
236  { 0, 1, 2, 3, -1, -1, -1, -1, -1},
237  { 2, 0, 1, 3, 4, -1, -1, -1, -1},
238  { 0, 1, 3, 4, 2, -1, -1, -1, -1},
239  { 2, 0, 1, 4, 5, 3, -1, -1, -1},
240  { 2, 3, 0, 1, 5, 6, 4, -1, -1},
241  { 2, 0, 1, 3, 4, 5, 6, -1, -1},
242  { 0, 5, 3, 4, 1, 2, 6, -1, -1},
243  { 3, 2, 4, 0, 1, 6, 7, 5, -1},
244  { 4, 5, 0, 1, 7, 2, 8, 3, 6},
245  { 3, 2, 4, 0, 1, 5, 8, 7, 6},
246 };
247 
248 #define DCA_DOLBY 101 /* FIXME */
249 
250 #define DCA_CHANNEL_BITS 6
251 #define DCA_CHANNEL_MASK 0x3F
252 
253 #define DCA_LFE 0x80
254 
255 #define HEADER_SIZE 14
256 
257 #define DCA_MAX_FRAME_SIZE 16384
258 #define DCA_MAX_EXSS_HEADER_SIZE 4096
259 
260 #define DCA_BUFFER_PADDING_SIZE 1024
261 
263 typedef struct {
264  int offset;
265  int maxbits[8];
266  int wrap;
267  VLC vlc[8];
268 } BitAlloc;
269 
274 
276  int idx)
277 {
278  return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
279  ba->offset;
280 }
281 
282 typedef struct {
285  /* Frame header */
291  int amode;
293  int bit_rate;
295 
296  int downmix;
297  int dynrange;
298  int timestamp;
299  int aux_data;
300  int hdcd;
301  int ext_descr;
303  int aspf;
304  int lfe;
308  int version;
311  int front_sum;
314 
315  /* Primary audio coding header */
316  int subframes;
320  int subband_activity[DCA_PRIM_CHANNELS_MAX];
321  int vq_start_subband[DCA_PRIM_CHANNELS_MAX];
322  int joint_intensity[DCA_PRIM_CHANNELS_MAX];
323  int transient_huffman[DCA_PRIM_CHANNELS_MAX];
324  int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX];
325  int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];
326  int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
327  float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
328 
329  /* Primary audio coding side information */
330  int subsubframes[DCA_SUBFRAMES_MAX];
331  int partial_samples[DCA_SUBFRAMES_MAX];
332  int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
333  int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
335  int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
336  int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];
337  int joint_huff[DCA_PRIM_CHANNELS_MAX];
338  int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
339  int downmix_coef[DCA_PRIM_CHANNELS_MAX][2];
341 
343 
344  float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];
346 
347  /* Subband samples history (for ADPCM) */
348  DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
349  DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
350  DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
351  int hist_index[DCA_PRIM_CHANNELS_MAX];
352  DECLARE_ALIGNED(32, float, raXin)[32];
353 
354  int output;
355  float scale_bias;
356 
359  const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
360 
363 
364  const int8_t *channel_order_tab;
366  /* Current position in DCA frame */
369 
371 
372  /* XCh extension information */
375 
376  /* ExSS header parser */
380  int mix_config_num_ch[4];
381 
382  int profile;
383 
390 } DCAContext;
391 
392 static const uint16_t dca_vlc_offs[] = {
393  0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
394  5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
395  5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
396  7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
397  12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
398  18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
399 };
400 
401 static av_cold void dca_init_vlcs(void)
402 {
403  static int vlcs_initialized = 0;
404  int i, j, c = 14;
405  static VLC_TYPE dca_table[23622][2];
406 
407  if (vlcs_initialized)
408  return;
409 
410  dca_bitalloc_index.offset = 1;
411  dca_bitalloc_index.wrap = 2;
412  for (i = 0; i < 5; i++) {
413  dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
414  dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
415  init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
416  bitalloc_12_bits[i], 1, 1,
418  }
419  dca_scalefactor.offset = -64;
420  dca_scalefactor.wrap = 2;
421  for (i = 0; i < 5; i++) {
422  dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
423  dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
424  init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
425  scales_bits[i], 1, 1,
427  }
428  dca_tmode.offset = 0;
429  dca_tmode.wrap = 1;
430  for (i = 0; i < 4; i++) {
431  dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
432  dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
433  init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
434  tmode_bits[i], 1, 1,
436  }
437 
438  for (i = 0; i < 10; i++)
439  for (j = 0; j < 7; j++) {
440  if (!bitalloc_codes[i][j])
441  break;
442  dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
443  dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
444  dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
445  dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
446 
447  init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
448  bitalloc_sizes[i],
449  bitalloc_bits[i][j], 1, 1,
451  c++;
452  }
453  vlcs_initialized = 1;
454 }
455 
456 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
457 {
458  while (len--)
459  *dst++ = get_bits(gb, bits);
460 }
461 
462 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
463 {
464  int i, j;
465  static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
466  static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
467  static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
468 
469  s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
471 
474 
475 
476  for (i = base_channel; i < s->prim_channels; i++) {
477  s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
478  if (s->subband_activity[i] > DCA_SUBBANDS)
480  }
481  for (i = base_channel; i < s->prim_channels; i++) {
482  s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
483  if (s->vq_start_subband[i] > DCA_SUBBANDS)
485  }
486  get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
487  get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
488  get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
489  get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
490 
491  /* Get codebooks quantization indexes */
492  if (!base_channel)
493  memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
494  for (j = 1; j < 11; j++)
495  for (i = base_channel; i < s->prim_channels; i++)
496  s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
497 
498  /* Get scale factor adjustment */
499  for (j = 0; j < 11; j++)
500  for (i = base_channel; i < s->prim_channels; i++)
501  s->scalefactor_adj[i][j] = 1;
502 
503  for (j = 1; j < 11; j++)
504  for (i = base_channel; i < s->prim_channels; i++)
505  if (s->quant_index_huffman[i][j] < thr[j])
506  s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
507 
508  if (s->crc_present) {
509  /* Audio header CRC check */
510  get_bits(&s->gb, 16);
511  }
512 
513  s->current_subframe = 0;
514  s->current_subsubframe = 0;
515 
516 #ifdef TRACE
517  av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
518  av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
519  for (i = base_channel; i < s->prim_channels; i++) {
520  av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
521  s->subband_activity[i]);
522  av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
523  s->vq_start_subband[i]);
524  av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
525  s->joint_intensity[i]);
526  av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
527  s->transient_huffman[i]);
528  av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
529  s->scalefactor_huffman[i]);
530  av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
531  s->bitalloc_huffman[i]);
532  av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
533  for (j = 0; j < 11; j++)
534  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
535  av_log(s->avctx, AV_LOG_DEBUG, "\n");
536  av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
537  for (j = 0; j < 11; j++)
538  av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
539  av_log(s->avctx, AV_LOG_DEBUG, "\n");
540  }
541 #endif
542 
543  return 0;
544 }
545 
547 {
548  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
549 
550  /* Sync code */
551  skip_bits_long(&s->gb, 32);
552 
553  /* Frame header */
554  s->frame_type = get_bits(&s->gb, 1);
555  s->samples_deficit = get_bits(&s->gb, 5) + 1;
556  s->crc_present = get_bits(&s->gb, 1);
557  s->sample_blocks = get_bits(&s->gb, 7) + 1;
558  s->frame_size = get_bits(&s->gb, 14) + 1;
559  if (s->frame_size < 95)
560  return AVERROR_INVALIDDATA;
561  s->amode = get_bits(&s->gb, 6);
562  s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
563  if (!s->sample_rate)
564  return AVERROR_INVALIDDATA;
565  s->bit_rate_index = get_bits(&s->gb, 5);
567  if (!s->bit_rate)
568  return AVERROR_INVALIDDATA;
569 
570  s->downmix = get_bits(&s->gb, 1);
571  s->dynrange = get_bits(&s->gb, 1);
572  s->timestamp = get_bits(&s->gb, 1);
573  s->aux_data = get_bits(&s->gb, 1);
574  s->hdcd = get_bits(&s->gb, 1);
575  s->ext_descr = get_bits(&s->gb, 3);
576  s->ext_coding = get_bits(&s->gb, 1);
577  s->aspf = get_bits(&s->gb, 1);
578  s->lfe = get_bits(&s->gb, 2);
579  s->predictor_history = get_bits(&s->gb, 1);
580 
581  if (s->lfe > 2) {
582  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
583  return AVERROR_INVALIDDATA;
584  }
585 
586  /* TODO: check CRC */
587  if (s->crc_present)
588  s->header_crc = get_bits(&s->gb, 16);
589 
590  s->multirate_inter = get_bits(&s->gb, 1);
591  s->version = get_bits(&s->gb, 4);
592  s->copy_history = get_bits(&s->gb, 2);
593  s->source_pcm_res = get_bits(&s->gb, 3);
594  s->front_sum = get_bits(&s->gb, 1);
595  s->surround_sum = get_bits(&s->gb, 1);
596  s->dialog_norm = get_bits(&s->gb, 4);
597 
598  /* FIXME: channels mixing levels */
599  s->output = s->amode;
600  if (s->lfe)
601  s->output |= DCA_LFE;
602 
603 #ifdef TRACE
604  av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
605  av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
606  av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
607  av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
608  s->sample_blocks, s->sample_blocks * 32);
609  av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
610  av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
611  s->amode, dca_channels[s->amode]);
612  av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
613  s->sample_rate);
614  av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
615  s->bit_rate);
616  av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
617  av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
618  av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
619  av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
620  av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
621  av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
622  av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
623  av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
624  av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
625  av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
626  s->predictor_history);
627  av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
628  av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
629  s->multirate_inter);
630  av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
631  av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
633  "source pcm resolution: %i (%i bits/sample)\n",
635  av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
636  av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
637  av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
638  av_log(s->avctx, AV_LOG_DEBUG, "\n");
639 #endif
640 
641  /* Primary audio coding header */
642  s->subframes = get_bits(&s->gb, 4) + 1;
643 
644  return dca_parse_audio_coding_header(s, 0);
645 }
646 
647 
648 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
649 {
650  if (level < 5) {
651  /* huffman encoded */
652  value += get_bitalloc(gb, &dca_scalefactor, level);
653  value = av_clip(value, 0, (1 << log2range) - 1);
654  } else if (level < 8) {
655  if (level + 1 > log2range) {
656  skip_bits(gb, level + 1 - log2range);
657  value = get_bits(gb, log2range);
658  } else {
659  value = get_bits(gb, level + 1);
660  }
661  }
662  return value;
663 }
664 
665 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
666 {
667  /* Primary audio coding side information */
668  int j, k;
669 
670  if (get_bits_left(&s->gb) < 0)
671  return AVERROR_INVALIDDATA;
672 
673  if (!base_channel) {
674  s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
675  s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
676  }
677 
678  for (j = base_channel; j < s->prim_channels; j++) {
679  for (k = 0; k < s->subband_activity[j]; k++)
680  s->prediction_mode[j][k] = get_bits(&s->gb, 1);
681  }
682 
683  /* Get prediction codebook */
684  for (j = base_channel; j < s->prim_channels; j++) {
685  for (k = 0; k < s->subband_activity[j]; k++) {
686  if (s->prediction_mode[j][k] > 0) {
687  /* (Prediction coefficient VQ address) */
688  s->prediction_vq[j][k] = get_bits(&s->gb, 12);
689  }
690  }
691  }
692 
693  /* Bit allocation index */
694  for (j = base_channel; j < s->prim_channels; j++) {
695  for (k = 0; k < s->vq_start_subband[j]; k++) {
696  if (s->bitalloc_huffman[j] == 6)
697  s->bitalloc[j][k] = get_bits(&s->gb, 5);
698  else if (s->bitalloc_huffman[j] == 5)
699  s->bitalloc[j][k] = get_bits(&s->gb, 4);
700  else if (s->bitalloc_huffman[j] == 7) {
702  "Invalid bit allocation index\n");
703  return AVERROR_INVALIDDATA;
704  } else {
705  s->bitalloc[j][k] =
706  get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
707  }
708 
709  if (s->bitalloc[j][k] > 26) {
710  // av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index [%i][%i] too big (%i)\n",
711  // j, k, s->bitalloc[j][k]);
712  return AVERROR_INVALIDDATA;
713  }
714  }
715  }
716 
717  /* Transition mode */
718  for (j = base_channel; j < s->prim_channels; j++) {
719  for (k = 0; k < s->subband_activity[j]; k++) {
720  s->transition_mode[j][k] = 0;
721  if (s->subsubframes[s->current_subframe] > 1 &&
722  k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
723  s->transition_mode[j][k] =
724  get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
725  }
726  }
727  }
728 
729  if (get_bits_left(&s->gb) < 0)
730  return AVERROR_INVALIDDATA;
731 
732  for (j = base_channel; j < s->prim_channels; j++) {
733  const uint32_t *scale_table;
734  int scale_sum, log_size;
735 
736  memset(s->scale_factor[j], 0,
737  s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
738 
739  if (s->scalefactor_huffman[j] == 6) {
740  scale_table = scale_factor_quant7;
741  log_size = 7;
742  } else {
743  scale_table = scale_factor_quant6;
744  log_size = 6;
745  }
746 
747  /* When huffman coded, only the difference is encoded */
748  scale_sum = 0;
749 
750  for (k = 0; k < s->subband_activity[j]; k++) {
751  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
752  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
753  s->scale_factor[j][k][0] = scale_table[scale_sum];
754  }
755 
756  if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
757  /* Get second scale factor */
758  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
759  s->scale_factor[j][k][1] = scale_table[scale_sum];
760  }
761  }
762  }
763 
764  /* Joint subband scale factor codebook select */
765  for (j = base_channel; j < s->prim_channels; j++) {
766  /* Transmitted only if joint subband coding enabled */
767  if (s->joint_intensity[j] > 0)
768  s->joint_huff[j] = get_bits(&s->gb, 3);
769  }
770 
771  if (get_bits_left(&s->gb) < 0)
772  return AVERROR_INVALIDDATA;
773 
774  /* Scale factors for joint subband coding */
775  for (j = base_channel; j < s->prim_channels; j++) {
776  int source_channel;
777 
778  /* Transmitted only if joint subband coding enabled */
779  if (s->joint_intensity[j] > 0) {
780  int scale = 0;
781  source_channel = s->joint_intensity[j] - 1;
782 
783  /* When huffman coded, only the difference is encoded
784  * (is this valid as well for joint scales ???) */
785 
786  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
787  scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
788  s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
789  }
790 
791  if (!(s->debug_flag & 0x02)) {
793  "Joint stereo coding not supported\n");
794  s->debug_flag |= 0x02;
795  }
796  }
797  }
798 
799  /* Stereo downmix coefficients */
800  if (!base_channel && s->prim_channels > 2) {
801  if (s->downmix) {
802  for (j = base_channel; j < s->prim_channels; j++) {
803  s->downmix_coef[j][0] = get_bits(&s->gb, 7);
804  s->downmix_coef[j][1] = get_bits(&s->gb, 7);
805  }
806  } else {
807  int am = s->amode & DCA_CHANNEL_MASK;
808  if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
810  "Invalid channel mode %d\n", am);
811  return AVERROR_INVALIDDATA;
812  }
813 
815  av_log_ask_for_sample(s->avctx, "Downmixing %d channels",
816  s->prim_channels);
817  return AVERROR_PATCHWELCOME;
818  }
819 
820  for (j = base_channel; j < s->prim_channels; j++) {
821  s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
822  s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
823  }
824  }
825  }
826 
827  /* Dynamic range coefficient */
828  if (!base_channel && s->dynrange)
829  s->dynrange_coef = get_bits(&s->gb, 8);
830 
831  /* Side information CRC check word */
832  if (s->crc_present) {
833  get_bits(&s->gb, 16);
834  }
835 
836  /*
837  * Primary audio data arrays
838  */
839 
840  /* VQ encoded high frequency subbands */
841  for (j = base_channel; j < s->prim_channels; j++)
842  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
843  /* 1 vector -> 32 samples */
844  s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
845 
846  /* Low frequency effect data */
847  if (!base_channel && s->lfe) {
848  /* LFE samples */
849  int lfe_samples = 2 * s->lfe * (4 + block_index);
850  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
851  float lfe_scale;
852 
853  for (j = lfe_samples; j < lfe_end_sample; j++) {
854  /* Signed 8 bits int */
855  s->lfe_data[j] = get_sbits(&s->gb, 8);
856  }
857 
858  /* Scale factor index */
859  skip_bits(&s->gb, 1);
861 
862  /* Quantization step size * scale factor */
863  lfe_scale = 0.035 * s->lfe_scale_factor;
864 
865  for (j = lfe_samples; j < lfe_end_sample; j++)
866  s->lfe_data[j] *= lfe_scale;
867  }
868 
869 #ifdef TRACE
870  av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
872  av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
874 
875  for (j = base_channel; j < s->prim_channels; j++) {
876  av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
877  for (k = 0; k < s->subband_activity[j]; k++)
878  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
879  av_log(s->avctx, AV_LOG_DEBUG, "\n");
880  }
881  for (j = base_channel; j < s->prim_channels; j++) {
882  for (k = 0; k < s->subband_activity[j]; k++)
884  "prediction coefs: %f, %f, %f, %f\n",
885  (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
886  (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
887  (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
888  (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
889  }
890  for (j = base_channel; j < s->prim_channels; j++) {
891  av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
892  for (k = 0; k < s->vq_start_subband[j]; k++)
893  av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
894  av_log(s->avctx, AV_LOG_DEBUG, "\n");
895  }
896  for (j = base_channel; j < s->prim_channels; j++) {
897  av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
898  for (k = 0; k < s->subband_activity[j]; k++)
899  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
900  av_log(s->avctx, AV_LOG_DEBUG, "\n");
901  }
902  for (j = base_channel; j < s->prim_channels; j++) {
903  av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
904  for (k = 0; k < s->subband_activity[j]; k++) {
905  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
906  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
907  if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
908  av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
909  }
910  av_log(s->avctx, AV_LOG_DEBUG, "\n");
911  }
912  for (j = base_channel; j < s->prim_channels; j++) {
913  if (s->joint_intensity[j] > 0) {
914  int source_channel = s->joint_intensity[j] - 1;
915  av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
916  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
917  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
918  av_log(s->avctx, AV_LOG_DEBUG, "\n");
919  }
920  }
921  if (!base_channel && s->prim_channels > 2 && s->downmix) {
922  av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
923  for (j = 0; j < s->prim_channels; j++) {
924  av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
925  dca_downmix_coeffs[s->downmix_coef[j][0]]);
926  av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
927  dca_downmix_coeffs[s->downmix_coef[j][1]]);
928  }
929  av_log(s->avctx, AV_LOG_DEBUG, "\n");
930  }
931  for (j = base_channel; j < s->prim_channels; j++)
932  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
933  av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
934  if (!base_channel && s->lfe) {
935  int lfe_samples = 2 * s->lfe * (4 + block_index);
936  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
937 
938  av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
939  for (j = lfe_samples; j < lfe_end_sample; j++)
940  av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
941  av_log(s->avctx, AV_LOG_DEBUG, "\n");
942  }
943 #endif
944 
945  return 0;
946 }
947 
948 static void qmf_32_subbands(DCAContext *s, int chans,
949  float samples_in[32][8], float *samples_out,
950  float scale)
951 {
952  const float *prCoeff;
953  int i;
954 
955  int sb_act = s->subband_activity[chans];
956  int subindex;
957 
958  scale *= sqrt(1 / 8.0);
959 
960  /* Select filter */
961  if (!s->multirate_inter) /* Non-perfect reconstruction */
962  prCoeff = fir_32bands_nonperfect;
963  else /* Perfect reconstruction */
964  prCoeff = fir_32bands_perfect;
965 
966  for (i = sb_act; i < 32; i++)
967  s->raXin[i] = 0.0;
968 
969  /* Reconstructed channel sample index */
970  for (subindex = 0; subindex < 8; subindex++) {
971  /* Load in one sample from each subband and clear inactive subbands */
972  for (i = 0; i < sb_act; i++) {
973  unsigned sign = (i - 1) & 2;
974  uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
975  AV_WN32A(&s->raXin[i], v);
976  }
977 
979  s->subband_fir_hist[chans],
980  &s->hist_index[chans],
981  s->subband_fir_noidea[chans], prCoeff,
982  samples_out, s->raXin, scale);
983  samples_out += 32;
984  }
985 }
986 
987 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
988  int num_deci_sample, float *samples_in,
989  float *samples_out, float scale)
990 {
991  /* samples_in: An array holding decimated samples.
992  * Samples in current subframe starts from samples_in[0],
993  * while samples_in[-1], samples_in[-2], ..., stores samples
994  * from last subframe as history.
995  *
996  * samples_out: An array holding interpolated samples
997  */
998 
999  int decifactor;
1000  const float *prCoeff;
1001  int deciindex;
1002 
1003  /* Select decimation filter */
1004  if (decimation_select == 1) {
1005  decifactor = 64;
1006  prCoeff = lfe_fir_128;
1007  } else {
1008  decifactor = 32;
1009  prCoeff = lfe_fir_64;
1010  }
1011  /* Interpolation */
1012  for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1013  s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1014  samples_in++;
1015  samples_out += 2 * decifactor;
1016  }
1017 }
1018 
1019 /* downmixing routines */
1020 #define MIX_REAR1(samples, si1, rs, coef) \
1021  samples[i] += samples[si1] * coef[rs][0]; \
1022  samples[i+256] += samples[si1] * coef[rs][1];
1023 
1024 #define MIX_REAR2(samples, si1, si2, rs, coef) \
1025  samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \
1026  samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1];
1027 
1028 #define MIX_FRONT3(samples, coef) \
1029  t = samples[i + c]; \
1030  u = samples[i + l]; \
1031  v = samples[i + r]; \
1032  samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1033  samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1034 
1035 #define DOWNMIX_TO_STEREO(op1, op2) \
1036  for (i = 0; i < 256; i++) { \
1037  op1 \
1038  op2 \
1039  }
1040 
1041 static void dca_downmix(float *samples, int srcfmt,
1042  int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1043  const int8_t *channel_mapping)
1044 {
1045  int c, l, r, sl, sr, s;
1046  int i;
1047  float t, u, v;
1048  float coef[DCA_PRIM_CHANNELS_MAX][2];
1049 
1050  for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1051  coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1052  coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1053  }
1054 
1055  switch (srcfmt) {
1056  case DCA_MONO:
1057  case DCA_CHANNEL:
1058  case DCA_STEREO_TOTAL:
1059  case DCA_STEREO_SUMDIFF:
1060  case DCA_4F2R:
1061  av_log(NULL, 0, "Not implemented!\n");
1062  break;
1063  case DCA_STEREO:
1064  break;
1065  case DCA_3F:
1066  c = channel_mapping[0] * 256;
1067  l = channel_mapping[1] * 256;
1068  r = channel_mapping[2] * 256;
1069  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1070  break;
1071  case DCA_2F1R:
1072  s = channel_mapping[2] * 256;
1073  DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), );
1074  break;
1075  case DCA_3F1R:
1076  c = channel_mapping[0] * 256;
1077  l = channel_mapping[1] * 256;
1078  r = channel_mapping[2] * 256;
1079  s = channel_mapping[3] * 256;
1080  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1081  MIX_REAR1(samples, i + s, 3, coef));
1082  break;
1083  case DCA_2F2R:
1084  sl = channel_mapping[2] * 256;
1085  sr = channel_mapping[3] * 256;
1086  DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), );
1087  break;
1088  case DCA_3F2R:
1089  c = channel_mapping[0] * 256;
1090  l = channel_mapping[1] * 256;
1091  r = channel_mapping[2] * 256;
1092  sl = channel_mapping[3] * 256;
1093  sr = channel_mapping[4] * 256;
1094  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1095  MIX_REAR2(samples, i + sl, i + sr, 3, coef));
1096  break;
1097  }
1098 }
1099 
1100 
1101 #ifndef decode_blockcodes
1102 /* Very compact version of the block code decoder that does not use table
1103  * look-up but is slightly slower */
1104 static int decode_blockcode(int code, int levels, int *values)
1105 {
1106  int i;
1107  int offset = (levels - 1) >> 1;
1108 
1109  for (i = 0; i < 4; i++) {
1110  int div = FASTDIV(code, levels);
1111  values[i] = code - offset - div * levels;
1112  code = div;
1113  }
1114 
1115  return code;
1116 }
1117 
1118 static int decode_blockcodes(int code1, int code2, int levels, int *values)
1119 {
1120  return decode_blockcode(code1, levels, values) |
1121  decode_blockcode(code2, levels, values + 4);
1122 }
1123 #endif
1124 
1125 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1126 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1127 
1128 #ifndef int8x8_fmul_int32
1129 static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1130 {
1131  float fscale = scale / 16.0;
1132  int i;
1133  for (i = 0; i < 8; i++)
1134  dst[i] = src[i] * fscale;
1135 }
1136 #endif
1137 
1138 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1139 {
1140  int k, l;
1141  int subsubframe = s->current_subsubframe;
1142 
1143  const float *quant_step_table;
1144 
1145  /* FIXME */
1146  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1147  LOCAL_ALIGNED_16(int, block, [8]);
1148 
1149  /*
1150  * Audio data
1151  */
1152 
1153  /* Select quantization step size table */
1154  if (s->bit_rate_index == 0x1f)
1155  quant_step_table = lossless_quant_d;
1156  else
1157  quant_step_table = lossy_quant_d;
1158 
1159  for (k = base_channel; k < s->prim_channels; k++) {
1160  if (get_bits_left(&s->gb) < 0)
1161  return AVERROR_INVALIDDATA;
1162 
1163  for (l = 0; l < s->vq_start_subband[k]; l++) {
1164  int m;
1165 
1166  /* Select the mid-tread linear quantizer */
1167  int abits = s->bitalloc[k][l];
1168 
1169  float quant_step_size = quant_step_table[abits];
1170 
1171  /*
1172  * Determine quantization index code book and its type
1173  */
1174 
1175  /* Select quantization index code book */
1176  int sel = s->quant_index_huffman[k][abits];
1177 
1178  /*
1179  * Extract bits from the bit stream
1180  */
1181  if (!abits) {
1182  memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1183  } else {
1184  /* Deal with transients */
1185  int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1186  float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1187  s->scalefactor_adj[k][sel];
1188 
1189  if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1190  if (abits <= 7) {
1191  /* Block code */
1192  int block_code1, block_code2, size, levels, err;
1193 
1194  size = abits_sizes[abits - 1];
1195  levels = abits_levels[abits - 1];
1196 
1197  block_code1 = get_bits(&s->gb, size);
1198  block_code2 = get_bits(&s->gb, size);
1199  err = decode_blockcodes(block_code1, block_code2,
1200  levels, block);
1201  if (err) {
1203  "ERROR: block code look-up failed\n");
1204  return AVERROR_INVALIDDATA;
1205  }
1206  } else {
1207  /* no coding */
1208  for (m = 0; m < 8; m++)
1209  block[m] = get_sbits(&s->gb, abits - 3);
1210  }
1211  } else {
1212  /* Huffman coded */
1213  for (m = 0; m < 8; m++)
1214  block[m] = get_bitalloc(&s->gb,
1215  &dca_smpl_bitalloc[abits], sel);
1216  }
1217 
1218  s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1219  block, rscale, 8);
1220  }
1221 
1222  /*
1223  * Inverse ADPCM if in prediction mode
1224  */
1225  if (s->prediction_mode[k][l]) {
1226  int n;
1227  for (m = 0; m < 8; m++) {
1228  for (n = 1; n <= 4; n++)
1229  if (m >= n)
1230  subband_samples[k][l][m] +=
1231  (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1232  subband_samples[k][l][m - n] / 8192);
1233  else if (s->predictor_history)
1234  subband_samples[k][l][m] +=
1235  (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1236  s->subband_samples_hist[k][l][m - n + 4] / 8192);
1237  }
1238  }
1239  }
1240 
1241  /*
1242  * Decode VQ encoded high frequencies
1243  */
1244  for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1245  /* 1 vector -> 32 samples but we only need the 8 samples
1246  * for this subsubframe. */
1247  int hfvq = s->high_freq_vq[k][l];
1248 
1249  if (!s->debug_flag & 0x01) {
1251  "Stream with high frequencies VQ coding\n");
1252  s->debug_flag |= 0x01;
1253  }
1254 
1255  int8x8_fmul_int32(subband_samples[k][l],
1256  &high_freq_vq[hfvq][subsubframe * 8],
1257  s->scale_factor[k][l][0]);
1258  }
1259  }
1260 
1261  /* Check for DSYNC after subsubframe */
1262  if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1263  if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1264 #ifdef TRACE
1265  av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1266 #endif
1267  } else {
1268  av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1269  return AVERROR_INVALIDDATA;
1270  }
1271  }
1272 
1273  /* Backup predictor history for adpcm */
1274  for (k = base_channel; k < s->prim_channels; k++)
1275  for (l = 0; l < s->vq_start_subband[k]; l++)
1276  memcpy(s->subband_samples_hist[k][l],
1277  &subband_samples[k][l][4],
1278  4 * sizeof(subband_samples[0][0][0]));
1279 
1280  return 0;
1281 }
1282 
1283 static int dca_filter_channels(DCAContext *s, int block_index)
1284 {
1285  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1286  int k;
1287 
1288  /* 32 subbands QMF */
1289  for (k = 0; k < s->prim_channels; k++) {
1290 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1291  0, 8388608.0, 8388608.0 };*/
1292  qmf_32_subbands(s, k, subband_samples[k],
1293  &s->samples[256 * s->channel_order_tab[k]],
1294  M_SQRT1_2 * s->scale_bias /* pcm_to_double[s->source_pcm_res] */);
1295  }
1296 
1297  /* Down mixing */
1298  if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1300  }
1301 
1302  /* Generate LFE samples for this subsubframe FIXME!!! */
1303  if (s->output & DCA_LFE) {
1304  lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1305  s->lfe_data + 2 * s->lfe * (block_index + 4),
1306  &s->samples[256 * dca_lfe_index[s->amode]],
1307  (1.0 / 256.0) * s->scale_bias);
1308  /* Outputs 20bits pcm samples */
1309  }
1310 
1311  return 0;
1312 }
1313 
1314 
1315 static int dca_subframe_footer(DCAContext *s, int base_channel)
1316 {
1317  int aux_data_count = 0, i;
1318 
1319  /*
1320  * Unpack optional information
1321  */
1322 
1323  /* presumably optional information only appears in the core? */
1324  if (!base_channel) {
1325  if (s->timestamp)
1326  skip_bits_long(&s->gb, 32);
1327 
1328  if (s->aux_data)
1329  aux_data_count = get_bits(&s->gb, 6);
1330 
1331  for (i = 0; i < aux_data_count; i++)
1332  get_bits(&s->gb, 8);
1333 
1334  if (s->crc_present && (s->downmix || s->dynrange))
1335  get_bits(&s->gb, 16);
1336  }
1337 
1338  return 0;
1339 }
1340 
1347 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1348 {
1349  int ret;
1350 
1351  /* Sanity check */
1352  if (s->current_subframe >= s->subframes) {
1353  av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1354  s->current_subframe, s->subframes);
1355  return AVERROR_INVALIDDATA;
1356  }
1357 
1358  if (!s->current_subsubframe) {
1359 #ifdef TRACE
1360  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1361 #endif
1362  /* Read subframe header */
1363  if ((ret = dca_subframe_header(s, base_channel, block_index)))
1364  return ret;
1365  }
1366 
1367  /* Read subsubframe */
1368 #ifdef TRACE
1369  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1370 #endif
1371  if ((ret = dca_subsubframe(s, base_channel, block_index)))
1372  return ret;
1373 
1374  /* Update state */
1375  s->current_subsubframe++;
1377  s->current_subsubframe = 0;
1378  s->current_subframe++;
1379  }
1380  if (s->current_subframe >= s->subframes) {
1381 #ifdef TRACE
1382  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1383 #endif
1384  /* Read subframe footer */
1385  if ((ret = dca_subframe_footer(s, base_channel)))
1386  return ret;
1387  }
1388 
1389  return 0;
1390 }
1391 
1395 static int dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
1396  int max_size)
1397 {
1398  uint32_t mrk;
1399  int i, tmp;
1400  const uint16_t *ssrc = (const uint16_t *) src;
1401  uint16_t *sdst = (uint16_t *) dst;
1402  PutBitContext pb;
1403 
1404  if ((unsigned) src_size > (unsigned) max_size) {
1405 // av_log(NULL, AV_LOG_ERROR, "Input frame size larger than DCA_MAX_FRAME_SIZE!\n");
1406 // return -1;
1407  src_size = max_size;
1408  }
1409 
1410  mrk = AV_RB32(src);
1411  switch (mrk) {
1412  case DCA_MARKER_RAW_BE:
1413  memcpy(dst, src, src_size);
1414  return src_size;
1415  case DCA_MARKER_RAW_LE:
1416  for (i = 0; i < (src_size + 1) >> 1; i++)
1417  *sdst++ = av_bswap16(*ssrc++);
1418  return src_size;
1419  case DCA_MARKER_14B_BE:
1420  case DCA_MARKER_14B_LE:
1421  init_put_bits(&pb, dst, max_size);
1422  for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
1423  tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
1424  put_bits(&pb, 14, tmp);
1425  }
1426  flush_put_bits(&pb);
1427  return (put_bits_count(&pb) + 7) >> 3;
1428  default:
1429  return AVERROR_INVALIDDATA;
1430  }
1431 }
1432 
1436 static int dca_exss_mask2count(int mask)
1437 {
1438  /* count bits that mean speaker pairs twice */
1439  return av_popcount(mask) +
1440  av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1449 }
1450 
1454 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1455 {
1456  int i;
1457 
1458  for (i = 0; i < channels; i++) {
1459  int mix_map_mask = get_bits(gb, out_ch);
1460  int num_coeffs = av_popcount(mix_map_mask);
1461  skip_bits_long(gb, num_coeffs * 6);
1462  }
1463 }
1464 
1469 {
1470  int header_pos = get_bits_count(&s->gb);
1471  int header_size;
1472  int channels;
1473  int embedded_stereo = 0;
1474  int embedded_6ch = 0;
1475  int drc_code_present;
1476  int extensions_mask;
1477  int i, j;
1478 
1479  if (get_bits_left(&s->gb) < 16)
1480  return -1;
1481 
1482  /* We will parse just enough to get to the extensions bitmask with which
1483  * we can set the profile value. */
1484 
1485  header_size = get_bits(&s->gb, 9) + 1;
1486  skip_bits(&s->gb, 3); // asset index
1487 
1488  if (s->static_fields) {
1489  if (get_bits1(&s->gb))
1490  skip_bits(&s->gb, 4); // asset type descriptor
1491  if (get_bits1(&s->gb))
1492  skip_bits_long(&s->gb, 24); // language descriptor
1493 
1494  if (get_bits1(&s->gb)) {
1495  /* How can one fit 1024 bytes of text here if the maximum value
1496  * for the asset header size field above was 512 bytes? */
1497  int text_length = get_bits(&s->gb, 10) + 1;
1498  if (get_bits_left(&s->gb) < text_length * 8)
1499  return -1;
1500  skip_bits_long(&s->gb, text_length * 8); // info text
1501  }
1502 
1503  skip_bits(&s->gb, 5); // bit resolution - 1
1504  skip_bits(&s->gb, 4); // max sample rate code
1505  channels = get_bits(&s->gb, 8) + 1;
1506 
1507  if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1508  int spkr_remap_sets;
1509  int spkr_mask_size = 16;
1510  int num_spkrs[7];
1511 
1512  if (channels > 2)
1513  embedded_stereo = get_bits1(&s->gb);
1514  if (channels > 6)
1515  embedded_6ch = get_bits1(&s->gb);
1516 
1517  if (get_bits1(&s->gb)) {
1518  spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1519  skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1520  }
1521 
1522  spkr_remap_sets = get_bits(&s->gb, 3);
1523 
1524  for (i = 0; i < spkr_remap_sets; i++) {
1525  /* std layout mask for each remap set */
1526  num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1527  }
1528 
1529  for (i = 0; i < spkr_remap_sets; i++) {
1530  int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1531  if (get_bits_left(&s->gb) < 0)
1532  return -1;
1533 
1534  for (j = 0; j < num_spkrs[i]; j++) {
1535  int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1536  int num_dec_ch = av_popcount(remap_dec_ch_mask);
1537  skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1538  }
1539  }
1540 
1541  } else {
1542  skip_bits(&s->gb, 3); // representation type
1543  }
1544  }
1545 
1546  drc_code_present = get_bits1(&s->gb);
1547  if (drc_code_present)
1548  get_bits(&s->gb, 8); // drc code
1549 
1550  if (get_bits1(&s->gb))
1551  skip_bits(&s->gb, 5); // dialog normalization code
1552 
1553  if (drc_code_present && embedded_stereo)
1554  get_bits(&s->gb, 8); // drc stereo code
1555 
1556  if (s->mix_metadata && get_bits1(&s->gb)) {
1557  skip_bits(&s->gb, 1); // external mix
1558  skip_bits(&s->gb, 6); // post mix gain code
1559 
1560  if (get_bits(&s->gb, 2) != 3) // mixer drc code
1561  skip_bits(&s->gb, 3); // drc limit
1562  else
1563  skip_bits(&s->gb, 8); // custom drc code
1564 
1565  if (get_bits1(&s->gb)) // channel specific scaling
1566  for (i = 0; i < s->num_mix_configs; i++)
1567  skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1568  else
1569  skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1570 
1571  for (i = 0; i < s->num_mix_configs; i++) {
1572  if (get_bits_left(&s->gb) < 0)
1573  return -1;
1574  dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1575  if (embedded_6ch)
1577  if (embedded_stereo)
1579  }
1580  }
1581 
1582  switch (get_bits(&s->gb, 2)) {
1583  case 0: extensions_mask = get_bits(&s->gb, 12); break;
1584  case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1585  case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1586  case 3: extensions_mask = 0; /* aux coding */ break;
1587  }
1588 
1589  /* not parsed further, we were only interested in the extensions mask */
1590 
1591  if (get_bits_left(&s->gb) < 0)
1592  return -1;
1593 
1594  if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1595  av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1596  return -1;
1597  }
1598  skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1599 
1600  if (extensions_mask & DCA_EXT_EXSS_XLL)
1602  else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1605 
1606  if (!(extensions_mask & DCA_EXT_CORE))
1607  av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1608  if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1610  "DTS extensions detection mismatch (%d, %d)\n",
1611  extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1612 
1613  return 0;
1614 }
1615 
1620 {
1621  int ss_index;
1622  int blownup;
1623  int num_audiop = 1;
1624  int num_assets = 1;
1625  int active_ss_mask[8];
1626  int i, j;
1627 
1628  if (get_bits_left(&s->gb) < 52)
1629  return;
1630 
1631  skip_bits(&s->gb, 8); // user data
1632  ss_index = get_bits(&s->gb, 2);
1633 
1634  blownup = get_bits1(&s->gb);
1635  skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1636  skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1637 
1638  s->static_fields = get_bits1(&s->gb);
1639  if (s->static_fields) {
1640  skip_bits(&s->gb, 2); // reference clock code
1641  skip_bits(&s->gb, 3); // frame duration code
1642 
1643  if (get_bits1(&s->gb))
1644  skip_bits_long(&s->gb, 36); // timestamp
1645 
1646  /* a single stream can contain multiple audio assets that can be
1647  * combined to form multiple audio presentations */
1648 
1649  num_audiop = get_bits(&s->gb, 3) + 1;
1650  if (num_audiop > 1) {
1651  av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1652  /* ignore such streams for now */
1653  return;
1654  }
1655 
1656  num_assets = get_bits(&s->gb, 3) + 1;
1657  if (num_assets > 1) {
1658  av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1659  /* ignore such streams for now */
1660  return;
1661  }
1662 
1663  for (i = 0; i < num_audiop; i++)
1664  active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1665 
1666  for (i = 0; i < num_audiop; i++)
1667  for (j = 0; j <= ss_index; j++)
1668  if (active_ss_mask[i] & (1 << j))
1669  skip_bits(&s->gb, 8); // active asset mask
1670 
1671  s->mix_metadata = get_bits1(&s->gb);
1672  if (s->mix_metadata) {
1673  int mix_out_mask_size;
1674 
1675  skip_bits(&s->gb, 2); // adjustment level
1676  mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1677  s->num_mix_configs = get_bits(&s->gb, 2) + 1;
1678 
1679  for (i = 0; i < s->num_mix_configs; i++) {
1680  int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
1681  s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
1682  }
1683  }
1684  }
1685 
1686  for (i = 0; i < num_assets; i++)
1687  skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
1688 
1689  for (i = 0; i < num_assets; i++) {
1691  return;
1692  }
1693 
1694  /* not parsed further, we were only interested in the extensions mask
1695  * from the asset header */
1696 }
1697 
1702 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1703  int *got_frame_ptr, AVPacket *avpkt)
1704 {
1705  const uint8_t *buf = avpkt->data;
1706  int buf_size = avpkt->size;
1707 
1708  int lfe_samples;
1709  int num_core_channels = 0;
1710  int i, ret;
1711  float *samples_flt;
1712  int16_t *samples_s16;
1713  DCAContext *s = avctx->priv_data;
1714  int channels;
1715  int core_ss_end;
1716 
1717 
1718  s->xch_present = 0;
1719 
1720  s->dca_buffer_size = dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1723  av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1724  return AVERROR_INVALIDDATA;
1725  }
1726 
1727  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
1728  if ((ret = dca_parse_frame_header(s)) < 0) {
1729  //seems like the frame is corrupt, try with the next one
1730  return ret;
1731  }
1732  //set AVCodec values with parsed data
1733  avctx->sample_rate = s->sample_rate;
1734  avctx->bit_rate = s->bit_rate;
1735  avctx->frame_size = s->sample_blocks * 32;
1736 
1737  s->profile = FF_PROFILE_DTS;
1738 
1739  for (i = 0; i < (s->sample_blocks / 8); i++) {
1740  if ((ret = dca_decode_block(s, 0, i))) {
1741  av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1742  return ret;
1743  }
1744  }
1745 
1746  /* record number of core channels incase less than max channels are requested */
1747  num_core_channels = s->prim_channels;
1748 
1749  if (s->ext_coding)
1751  else
1752  s->core_ext_mask = 0;
1753 
1754  core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1755 
1756  /* only scan for extensions if ext_descr was unknown or indicated a
1757  * supported XCh extension */
1758  if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1759 
1760  /* if ext_descr was unknown, clear s->core_ext_mask so that the
1761  * extensions scan can fill it up */
1762  s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1763 
1764  /* extensions start at 32-bit boundaries into bitstream */
1765  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1766 
1767  while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1768  uint32_t bits = get_bits_long(&s->gb, 32);
1769 
1770  switch (bits) {
1771  case 0x5a5a5a5a: {
1772  int ext_amode, xch_fsize;
1773 
1775 
1776  /* validate sync word using XCHFSIZE field */
1777  xch_fsize = show_bits(&s->gb, 10);
1778  if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1779  (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1780  continue;
1781 
1782  /* skip length-to-end-of-frame field for the moment */
1783  skip_bits(&s->gb, 10);
1784 
1785  s->core_ext_mask |= DCA_EXT_XCH;
1786 
1787  /* extension amode(number of channels in extension) should be 1 */
1788  /* AFAIK XCh is not used for more channels */
1789  if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1790  av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1791  " supported!\n", ext_amode);
1792  continue;
1793  }
1794 
1795  /* much like core primary audio coding header */
1797 
1798  for (i = 0; i < (s->sample_blocks / 8); i++)
1799  if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1800  av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1801  continue;
1802  }
1803 
1804  s->xch_present = 1;
1805  break;
1806  }
1807  case 0x47004a03:
1808  /* XXCh: extended channels */
1809  /* usually found either in core or HD part in DTS-HD HRA streams,
1810  * but not in DTS-ES which contains XCh extensions instead */
1812  break;
1813 
1814  case 0x1d95f262: {
1815  int fsize96 = show_bits(&s->gb, 12) + 1;
1816  if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1817  continue;
1818 
1819  av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1820  get_bits_count(&s->gb));
1821  skip_bits(&s->gb, 12);
1822  av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1823  av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1824 
1825  s->core_ext_mask |= DCA_EXT_X96;
1826  break;
1827  }
1828  }
1829 
1830  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1831  }
1832  } else {
1833  /* no supported extensions, skip the rest of the core substream */
1834  skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1835  }
1836 
1837  if (s->core_ext_mask & DCA_EXT_X96)
1839  else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1841 
1842  /* check for ExSS (HD part) */
1843  if (s->dca_buffer_size - s->frame_size > 32 &&
1844  get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1846 
1847  avctx->profile = s->profile;
1848 
1849  channels = s->prim_channels + !!s->lfe;
1850 
1851  if (s->amode < 16) {
1853 
1854  if (s->xch_present && (!avctx->request_channels ||
1855  avctx->request_channels > num_core_channels + !!s->lfe)) {
1857  if (s->lfe) {
1860  } else {
1862  }
1863  } else {
1864  channels = num_core_channels + !!s->lfe;
1865  s->xch_present = 0; /* disable further xch processing */
1866  if (s->lfe) {
1869  } else
1871  }
1872 
1873  if (channels > !!s->lfe &&
1874  s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1875  return AVERROR_INVALIDDATA;
1876 
1877  if (avctx->request_channels == 2 && s->prim_channels > 2) {
1878  channels = 2;
1879  s->output = DCA_STEREO;
1881  }
1882  } else {
1883  av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1884  return AVERROR_INVALIDDATA;
1885  }
1886 
1887 
1888  /* There is nothing that prevents a dts frame to change channel configuration
1889  but Libav doesn't support that so only set the channels if it is previously
1890  unset. Ideally during the first probe for channels the crc should be checked
1891  and only set avctx->channels when the crc is ok. Right now the decoder could
1892  set the channels based on a broken first frame.*/
1893  if (s->is_channels_set == 0) {
1894  s->is_channels_set = 1;
1895  avctx->channels = channels;
1896  }
1897  if (avctx->channels != channels) {
1898  av_log(avctx, AV_LOG_ERROR, "DCA decoder does not support number of "
1899  "channels changing in stream. Skipping frame.\n");
1900  return AVERROR_PATCHWELCOME;
1901  }
1902 
1903  /* get output buffer */
1904  s->frame.nb_samples = 256 * (s->sample_blocks / 8);
1905  if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
1906  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1907  return ret;
1908  }
1909  samples_flt = (float *) s->frame.data[0];
1910  samples_s16 = (int16_t *) s->frame.data[0];
1911 
1912  /* filter to get final output */
1913  for (i = 0; i < (s->sample_blocks / 8); i++) {
1914  dca_filter_channels(s, i);
1915 
1916  /* If this was marked as a DTS-ES stream we need to subtract back- */
1917  /* channel from SL & SR to remove matrixed back-channel signal */
1918  if ((s->source_pcm_res & 1) && s->xch_present) {
1919  float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
1920  float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
1921  float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
1922  s->dsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1923  s->dsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1924  }
1925 
1926  if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
1927  s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
1928  channels);
1929  samples_flt += 256 * channels;
1930  } else {
1931  s->fmt_conv.float_to_int16_interleave(samples_s16,
1932  s->samples_chanptr, 256,
1933  channels);
1934  samples_s16 += 256 * channels;
1935  }
1936  }
1937 
1938  /* update lfe history */
1939  lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1940  for (i = 0; i < 2 * s->lfe * 4; i++)
1941  s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1942 
1943  *got_frame_ptr = 1;
1944  *(AVFrame *) data = s->frame;
1945 
1946  return buf_size;
1947 }
1948 
1949 
1950 
1958 {
1959  DCAContext *s = avctx->priv_data;
1960  int i;
1961 
1962  s->avctx = avctx;
1963  dca_init_vlcs();
1964 
1965  dsputil_init(&s->dsp, avctx);
1966  ff_mdct_init(&s->imdct, 6, 1, 1.0);
1968  ff_dcadsp_init(&s->dcadsp);
1969  ff_fmt_convert_init(&s->fmt_conv, avctx);
1970 
1971  for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++)
1972  s->samples_chanptr[i] = s->samples + i * 256;
1973 
1974  if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
1975  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1976  s->scale_bias = 1.0 / 32768.0;
1977  } else {
1978  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1979  s->scale_bias = 1.0;
1980  }
1981 
1982  /* allow downmixing to stereo */
1983  if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
1984  avctx->request_channels == 2) {
1985  avctx->channels = avctx->request_channels;
1986  }
1987 
1989  avctx->coded_frame = &s->frame;
1990 
1991  return 0;
1992 }
1993 
1995 {
1996  DCAContext *s = avctx->priv_data;
1997  ff_mdct_end(&s->imdct);
1998  return 0;
1999 }
2000 
2001 static const AVProfile profiles[] = {
2002  { FF_PROFILE_DTS, "DTS" },
2003  { FF_PROFILE_DTS_ES, "DTS-ES" },
2004  { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2005  { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2006  { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2007  { FF_PROFILE_UNKNOWN },
2008 };
2009 
2011  .name = "dca",
2012  .type = AVMEDIA_TYPE_AUDIO,
2013  .id = CODEC_ID_DTS,
2014  .priv_data_size = sizeof(DCAContext),
2015  .init = dca_decode_init,
2017  .close = dca_decode_end,
2018  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2019  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2020  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
2023  .profiles = NULL_IF_CONFIG_SMALL(profiles),
2024 };