Libav
dcadec.c
Go to the documentation of this file.
1 /*
2  * DCA compatible decoder
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
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 
30 #include "libavutil/common.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/samplefmt.h"
37 #include "avcodec.h"
38 #include "fft.h"
39 #include "get_bits.h"
40 #include "put_bits.h"
41 #include "dcadata.h"
42 #include "dcahuff.h"
43 #include "dca.h"
44 #include "mathops.h"
45 #include "synth_filter.h"
46 #include "dcadsp.h"
47 #include "fmtconvert.h"
48 #include "internal.h"
49 
50 #if ARCH_ARM
51 # include "arm/dca.h"
52 #endif
53 
54 //#define TRACE
55 
56 #define DCA_PRIM_CHANNELS_MAX (7)
57 #define DCA_ABITS_MAX (32) /* Should be 28 */
58 #define DCA_SUBSUBFRAMES_MAX (4)
59 #define DCA_SUBFRAMES_MAX (16)
60 #define DCA_BLOCKS_MAX (16)
61 #define DCA_LFE_MAX (3)
62 
63 enum DCAMode {
64  DCA_MONO = 0,
75 };
76 
77 /* these are unconfirmed but should be mostly correct */
82  DCA_EXSS_LFE = 0x0008,
91  DCA_EXSS_LFE2 = 0x1000,
95 };
96 
98  DCA_EXT_CORE = 0x001,
99  DCA_EXT_XXCH = 0x002,
100  DCA_EXT_X96 = 0x004,
101  DCA_EXT_XCH = 0x008,
108 };
109 
110 /* -1 are reserved or unknown */
111 static const int dca_ext_audio_descr_mask[] = {
112  DCA_EXT_XCH,
113  -1,
114  DCA_EXT_X96,
116  -1,
117  -1,
118  DCA_EXT_XXCH,
119  -1,
120 };
121 
122 /* extensions that reside in core substream */
123 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
124 
125 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
126  * Some compromises have been made for special configurations. Most configurations
127  * are never used so complete accuracy is not needed.
128  *
129  * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
130  * S -> side, when both rear and back are configured move one of them to the side channel
131  * OV -> center back
132  * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
133  */
134 static const uint64_t dca_core_channel_layout[] = {
140  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER,
141  AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER,
142  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER,
143  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT,
144 
145  AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
147 
148  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
150 
151  AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
153 
157 
159  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
161 
163  AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
165 
167  AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
169 };
170 
171 static const int8_t dca_lfe_index[] = {
172  1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
173 };
174 
175 static const int8_t dca_channel_reorder_lfe[][9] = {
176  { 0, -1, -1, -1, -1, -1, -1, -1, -1},
177  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
178  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
179  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
180  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
181  { 2, 0, 1, -1, -1, -1, -1, -1, -1},
182  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
183  { 2, 0, 1, 4, -1, -1, -1, -1, -1},
184  { 0, 1, 3, 4, -1, -1, -1, -1, -1},
185  { 2, 0, 1, 4, 5, -1, -1, -1, -1},
186  { 3, 4, 0, 1, 5, 6, -1, -1, -1},
187  { 2, 0, 1, 4, 5, 6, -1, -1, -1},
188  { 0, 6, 4, 5, 2, 3, -1, -1, -1},
189  { 4, 2, 5, 0, 1, 6, 7, -1, -1},
190  { 5, 6, 0, 1, 7, 3, 8, 4, -1},
191  { 4, 2, 5, 0, 1, 6, 8, 7, -1},
192 };
193 
194 static const int8_t dca_channel_reorder_lfe_xch[][9] = {
195  { 0, 2, -1, -1, -1, -1, -1, -1, -1},
196  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
197  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
198  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
199  { 0, 1, 3, -1, -1, -1, -1, -1, -1},
200  { 2, 0, 1, 4, -1, -1, -1, -1, -1},
201  { 0, 1, 3, 4, -1, -1, -1, -1, -1},
202  { 2, 0, 1, 4, 5, -1, -1, -1, -1},
203  { 0, 1, 4, 5, 3, -1, -1, -1, -1},
204  { 2, 0, 1, 5, 6, 4, -1, -1, -1},
205  { 3, 4, 0, 1, 6, 7, 5, -1, -1},
206  { 2, 0, 1, 4, 5, 6, 7, -1, -1},
207  { 0, 6, 4, 5, 2, 3, 7, -1, -1},
208  { 4, 2, 5, 0, 1, 7, 8, 6, -1},
209  { 5, 6, 0, 1, 8, 3, 9, 4, 7},
210  { 4, 2, 5, 0, 1, 6, 9, 8, 7},
211 };
212 
213 static const int8_t dca_channel_reorder_nolfe[][9] = {
214  { 0, -1, -1, -1, -1, -1, -1, -1, -1},
215  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
216  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
217  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
218  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
219  { 2, 0, 1, -1, -1, -1, -1, -1, -1},
220  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
221  { 2, 0, 1, 3, -1, -1, -1, -1, -1},
222  { 0, 1, 2, 3, -1, -1, -1, -1, -1},
223  { 2, 0, 1, 3, 4, -1, -1, -1, -1},
224  { 2, 3, 0, 1, 4, 5, -1, -1, -1},
225  { 2, 0, 1, 3, 4, 5, -1, -1, -1},
226  { 0, 5, 3, 4, 1, 2, -1, -1, -1},
227  { 3, 2, 4, 0, 1, 5, 6, -1, -1},
228  { 4, 5, 0, 1, 6, 2, 7, 3, -1},
229  { 3, 2, 4, 0, 1, 5, 7, 6, -1},
230 };
231 
232 static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
233  { 0, 1, -1, -1, -1, -1, -1, -1, -1},
234  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
235  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
236  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
237  { 0, 1, 2, -1, -1, -1, -1, -1, -1},
238  { 2, 0, 1, 3, -1, -1, -1, -1, -1},
239  { 0, 1, 2, 3, -1, -1, -1, -1, -1},
240  { 2, 0, 1, 3, 4, -1, -1, -1, -1},
241  { 0, 1, 3, 4, 2, -1, -1, -1, -1},
242  { 2, 0, 1, 4, 5, 3, -1, -1, -1},
243  { 2, 3, 0, 1, 5, 6, 4, -1, -1},
244  { 2, 0, 1, 3, 4, 5, 6, -1, -1},
245  { 0, 5, 3, 4, 1, 2, 6, -1, -1},
246  { 3, 2, 4, 0, 1, 6, 7, 5, -1},
247  { 4, 5, 0, 1, 7, 2, 8, 3, 6},
248  { 3, 2, 4, 0, 1, 5, 8, 7, 6},
249 };
250 
251 #define DCA_DOLBY 101 /* FIXME */
252 
253 #define DCA_CHANNEL_BITS 6
254 #define DCA_CHANNEL_MASK 0x3F
255 
256 #define DCA_LFE 0x80
257 
258 #define HEADER_SIZE 14
259 
260 #define DCA_MAX_FRAME_SIZE 16384
261 #define DCA_MAX_EXSS_HEADER_SIZE 4096
262 
263 #define DCA_BUFFER_PADDING_SIZE 1024
264 
265 #define DCA_NSYNCAUX 0x9A1105A0
266 
268 typedef struct {
269  int offset;
270  int maxbits[8];
271  int wrap;
272  VLC vlc[8];
273 } BitAlloc;
274 
279 
281  int idx)
282 {
283  return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
284  ba->offset;
285 }
286 
287 typedef struct {
288  AVClass *class;
290  /* Frame header */
296  int amode;
298  int bit_rate;
300 
301  int dynrange;
302  int timestamp;
303  int aux_data;
304  int hdcd;
305  int ext_descr;
307  int aspf;
308  int lfe;
312  int version;
315  int front_sum;
318 
319  /* Primary audio coding header */
320  int subframes;
323  int subband_activity[DCA_PRIM_CHANNELS_MAX];
324  int vq_start_subband[DCA_PRIM_CHANNELS_MAX];
325  int joint_intensity[DCA_PRIM_CHANNELS_MAX];
326  int transient_huffman[DCA_PRIM_CHANNELS_MAX];
327  int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX];
328  int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];
329  int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
330  float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];
331 
332  /* Primary audio coding side information */
333  int subsubframes[DCA_SUBFRAMES_MAX];
334  int partial_samples[DCA_SUBFRAMES_MAX];
335  int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
336  int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
338  int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
340  int joint_huff[DCA_PRIM_CHANNELS_MAX];
341  int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];
342  float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2];
344 
345  /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
346  * Input: primary audio channels (incl. LFE if present)
347  * Output: downmix audio channels (up to 4, no LFE) */
350  uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4];
351 
353 
354  float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];
356 
357  /* Subband samples history (for ADPCM) */
358  DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
359  DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
360  DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
361  int hist_index[DCA_PRIM_CHANNELS_MAX];
362  DECLARE_ALIGNED(32, float, raXin)[32];
363 
364  int output;
365 
367  float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
368  float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
371 
374 
375  const int8_t *channel_order_tab;
377  /* Current position in DCA frame */
380 
382 
383  /* XCh extension information */
387 
388  /* ExSS header parser */
392  int mix_config_num_ch[4];
393 
394  int profile;
395 
402 } DCAContext;
403 
404 static const uint16_t dca_vlc_offs[] = {
405  0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
406  5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
407  5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
408  7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
409  12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
410  18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
411 };
412 
413 static av_cold void dca_init_vlcs(void)
414 {
415  static int vlcs_initialized = 0;
416  int i, j, c = 14;
417  static VLC_TYPE dca_table[23622][2];
418 
419  if (vlcs_initialized)
420  return;
421 
422  dca_bitalloc_index.offset = 1;
423  dca_bitalloc_index.wrap = 2;
424  for (i = 0; i < 5; i++) {
425  dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
426  dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
427  init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
428  bitalloc_12_bits[i], 1, 1,
430  }
431  dca_scalefactor.offset = -64;
432  dca_scalefactor.wrap = 2;
433  for (i = 0; i < 5; i++) {
434  dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
435  dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
436  init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
437  scales_bits[i], 1, 1,
439  }
440  dca_tmode.offset = 0;
441  dca_tmode.wrap = 1;
442  for (i = 0; i < 4; i++) {
443  dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
444  dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
445  init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
446  tmode_bits[i], 1, 1,
448  }
449 
450  for (i = 0; i < 10; i++)
451  for (j = 0; j < 7; j++) {
452  if (!bitalloc_codes[i][j])
453  break;
454  dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
455  dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
456  dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
457  dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
458 
459  init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
460  bitalloc_sizes[i],
461  bitalloc_bits[i][j], 1, 1,
463  c++;
464  }
465  vlcs_initialized = 1;
466 }
467 
468 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
469 {
470  while (len--)
471  *dst++ = get_bits(gb, bits);
472 }
473 
474 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
475 {
476  int i, j;
477  static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
478  static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
479  static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
480 
481  s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
483 
486 
487 
488  for (i = base_channel; i < s->prim_channels; i++) {
489  s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
490  if (s->subband_activity[i] > DCA_SUBBANDS)
492  }
493  for (i = base_channel; i < s->prim_channels; i++) {
494  s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
495  if (s->vq_start_subband[i] > DCA_SUBBANDS)
497  }
498  get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
499  get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
500  get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
501  get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
502 
503  /* Get codebooks quantization indexes */
504  if (!base_channel)
505  memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
506  for (j = 1; j < 11; j++)
507  for (i = base_channel; i < s->prim_channels; i++)
508  s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
509 
510  /* Get scale factor adjustment */
511  for (j = 0; j < 11; j++)
512  for (i = base_channel; i < s->prim_channels; i++)
513  s->scalefactor_adj[i][j] = 1;
514 
515  for (j = 1; j < 11; j++)
516  for (i = base_channel; i < s->prim_channels; i++)
517  if (s->quant_index_huffman[i][j] < thr[j])
518  s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
519 
520  if (s->crc_present) {
521  /* Audio header CRC check */
522  get_bits(&s->gb, 16);
523  }
524 
525  s->current_subframe = 0;
526  s->current_subsubframe = 0;
527 
528 #ifdef TRACE
529  av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
530  av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
531  for (i = base_channel; i < s->prim_channels; i++) {
532  av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
533  s->subband_activity[i]);
534  av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
535  s->vq_start_subband[i]);
536  av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
537  s->joint_intensity[i]);
538  av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
539  s->transient_huffman[i]);
540  av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
541  s->scalefactor_huffman[i]);
542  av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
543  s->bitalloc_huffman[i]);
544  av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
545  for (j = 0; j < 11; j++)
546  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
547  av_log(s->avctx, AV_LOG_DEBUG, "\n");
548  av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
549  for (j = 0; j < 11; j++)
550  av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
551  av_log(s->avctx, AV_LOG_DEBUG, "\n");
552  }
553 #endif
554 
555  return 0;
556 }
557 
559 {
560  init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
561 
562  /* Sync code */
563  skip_bits_long(&s->gb, 32);
564 
565  /* Frame header */
566  s->frame_type = get_bits(&s->gb, 1);
567  s->samples_deficit = get_bits(&s->gb, 5) + 1;
568  s->crc_present = get_bits(&s->gb, 1);
569  s->sample_blocks = get_bits(&s->gb, 7) + 1;
570  s->frame_size = get_bits(&s->gb, 14) + 1;
571  if (s->frame_size < 95)
572  return AVERROR_INVALIDDATA;
573  s->amode = get_bits(&s->gb, 6);
575  if (!s->sample_rate)
576  return AVERROR_INVALIDDATA;
577  s->bit_rate_index = get_bits(&s->gb, 5);
579  if (!s->bit_rate)
580  return AVERROR_INVALIDDATA;
581 
582  skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
583  s->dynrange = get_bits(&s->gb, 1);
584  s->timestamp = get_bits(&s->gb, 1);
585  s->aux_data = get_bits(&s->gb, 1);
586  s->hdcd = get_bits(&s->gb, 1);
587  s->ext_descr = get_bits(&s->gb, 3);
588  s->ext_coding = get_bits(&s->gb, 1);
589  s->aspf = get_bits(&s->gb, 1);
590  s->lfe = get_bits(&s->gb, 2);
591  s->predictor_history = get_bits(&s->gb, 1);
592 
593  if (s->lfe > 2) {
594  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
595  return AVERROR_INVALIDDATA;
596  }
597 
598  /* TODO: check CRC */
599  if (s->crc_present)
600  s->header_crc = get_bits(&s->gb, 16);
601 
602  s->multirate_inter = get_bits(&s->gb, 1);
603  s->version = get_bits(&s->gb, 4);
604  s->copy_history = get_bits(&s->gb, 2);
605  s->source_pcm_res = get_bits(&s->gb, 3);
606  s->front_sum = get_bits(&s->gb, 1);
607  s->surround_sum = get_bits(&s->gb, 1);
608  s->dialog_norm = get_bits(&s->gb, 4);
609 
610  /* FIXME: channels mixing levels */
611  s->output = s->amode;
612  if (s->lfe)
613  s->output |= DCA_LFE;
614 
615 #ifdef TRACE
616  av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
617  av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
618  av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
619  av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
620  s->sample_blocks, s->sample_blocks * 32);
621  av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
622  av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
623  s->amode, dca_channels[s->amode]);
624  av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
625  s->sample_rate);
626  av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
627  s->bit_rate);
628  av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
629  av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
630  av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
631  av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
632  av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
633  av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
634  av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
635  av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
636  av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
637  s->predictor_history);
638  av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
639  av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
640  s->multirate_inter);
641  av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
642  av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
644  "source pcm resolution: %i (%i bits/sample)\n",
646  av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
647  av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
648  av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
649  av_log(s->avctx, AV_LOG_DEBUG, "\n");
650 #endif
651 
652  /* Primary audio coding header */
653  s->subframes = get_bits(&s->gb, 4) + 1;
654 
655  return dca_parse_audio_coding_header(s, 0);
656 }
657 
658 
659 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
660 {
661  if (level < 5) {
662  /* huffman encoded */
663  value += get_bitalloc(gb, &dca_scalefactor, level);
664  value = av_clip(value, 0, (1 << log2range) - 1);
665  } else if (level < 8) {
666  if (level + 1 > log2range) {
667  skip_bits(gb, level + 1 - log2range);
668  value = get_bits(gb, log2range);
669  } else {
670  value = get_bits(gb, level + 1);
671  }
672  }
673  return value;
674 }
675 
676 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
677 {
678  /* Primary audio coding side information */
679  int j, k;
680 
681  if (get_bits_left(&s->gb) < 0)
682  return AVERROR_INVALIDDATA;
683 
684  if (!base_channel) {
685  s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
686  s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
687  }
688 
689  for (j = base_channel; j < s->prim_channels; j++) {
690  for (k = 0; k < s->subband_activity[j]; k++)
691  s->prediction_mode[j][k] = get_bits(&s->gb, 1);
692  }
693 
694  /* Get prediction codebook */
695  for (j = base_channel; j < s->prim_channels; j++) {
696  for (k = 0; k < s->subband_activity[j]; k++) {
697  if (s->prediction_mode[j][k] > 0) {
698  /* (Prediction coefficient VQ address) */
699  s->prediction_vq[j][k] = get_bits(&s->gb, 12);
700  }
701  }
702  }
703 
704  /* Bit allocation index */
705  for (j = base_channel; j < s->prim_channels; j++) {
706  for (k = 0; k < s->vq_start_subband[j]; k++) {
707  if (s->bitalloc_huffman[j] == 6)
708  s->bitalloc[j][k] = get_bits(&s->gb, 5);
709  else if (s->bitalloc_huffman[j] == 5)
710  s->bitalloc[j][k] = get_bits(&s->gb, 4);
711  else if (s->bitalloc_huffman[j] == 7) {
713  "Invalid bit allocation index\n");
714  return AVERROR_INVALIDDATA;
715  } else {
716  s->bitalloc[j][k] =
717  get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
718  }
719 
720  if (s->bitalloc[j][k] > 26) {
721  av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
722  j, k, s->bitalloc[j][k]);
723  return AVERROR_INVALIDDATA;
724  }
725  }
726  }
727 
728  /* Transition mode */
729  for (j = base_channel; j < s->prim_channels; j++) {
730  for (k = 0; k < s->subband_activity[j]; k++) {
731  s->transition_mode[j][k] = 0;
732  if (s->subsubframes[s->current_subframe] > 1 &&
733  k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
734  s->transition_mode[j][k] =
735  get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
736  }
737  }
738  }
739 
740  if (get_bits_left(&s->gb) < 0)
741  return AVERROR_INVALIDDATA;
742 
743  for (j = base_channel; j < s->prim_channels; j++) {
744  const uint32_t *scale_table;
745  int scale_sum, log_size;
746 
747  memset(s->scale_factor[j], 0,
748  s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
749 
750  if (s->scalefactor_huffman[j] == 6) {
751  scale_table = scale_factor_quant7;
752  log_size = 7;
753  } else {
754  scale_table = scale_factor_quant6;
755  log_size = 6;
756  }
757 
758  /* When huffman coded, only the difference is encoded */
759  scale_sum = 0;
760 
761  for (k = 0; k < s->subband_activity[j]; k++) {
762  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
763  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
764  s->scale_factor[j][k][0] = scale_table[scale_sum];
765  }
766 
767  if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
768  /* Get second scale factor */
769  scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
770  s->scale_factor[j][k][1] = scale_table[scale_sum];
771  }
772  }
773  }
774 
775  /* Joint subband scale factor codebook select */
776  for (j = base_channel; j < s->prim_channels; j++) {
777  /* Transmitted only if joint subband coding enabled */
778  if (s->joint_intensity[j] > 0)
779  s->joint_huff[j] = get_bits(&s->gb, 3);
780  }
781 
782  if (get_bits_left(&s->gb) < 0)
783  return AVERROR_INVALIDDATA;
784 
785  /* Scale factors for joint subband coding */
786  for (j = base_channel; j < s->prim_channels; j++) {
787  int source_channel;
788 
789  /* Transmitted only if joint subband coding enabled */
790  if (s->joint_intensity[j] > 0) {
791  int scale = 0;
792  source_channel = s->joint_intensity[j] - 1;
793 
794  /* When huffman coded, only the difference is encoded
795  * (is this valid as well for joint scales ???) */
796 
797  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
798  scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
799  s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
800  }
801 
802  if (!(s->debug_flag & 0x02)) {
804  "Joint stereo coding not supported\n");
805  s->debug_flag |= 0x02;
806  }
807  }
808  }
809 
810  /* Dynamic range coefficient */
811  if (!base_channel && s->dynrange)
812  s->dynrange_coef = get_bits(&s->gb, 8);
813 
814  /* Side information CRC check word */
815  if (s->crc_present) {
816  get_bits(&s->gb, 16);
817  }
818 
819  /*
820  * Primary audio data arrays
821  */
822 
823  /* VQ encoded high frequency subbands */
824  for (j = base_channel; j < s->prim_channels; j++)
825  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
826  /* 1 vector -> 32 samples */
827  s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
828 
829  /* Low frequency effect data */
830  if (!base_channel && s->lfe) {
831  /* LFE samples */
832  int lfe_samples = 2 * s->lfe * (4 + block_index);
833  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
834  float lfe_scale;
835 
836  for (j = lfe_samples; j < lfe_end_sample; j++) {
837  /* Signed 8 bits int */
838  s->lfe_data[j] = get_sbits(&s->gb, 8);
839  }
840 
841  /* Scale factor index */
842  skip_bits(&s->gb, 1);
844 
845  /* Quantization step size * scale factor */
846  lfe_scale = 0.035 * s->lfe_scale_factor;
847 
848  for (j = lfe_samples; j < lfe_end_sample; j++)
849  s->lfe_data[j] *= lfe_scale;
850  }
851 
852 #ifdef TRACE
853  av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
855  av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
857 
858  for (j = base_channel; j < s->prim_channels; j++) {
859  av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
860  for (k = 0; k < s->subband_activity[j]; k++)
861  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
862  av_log(s->avctx, AV_LOG_DEBUG, "\n");
863  }
864  for (j = base_channel; j < s->prim_channels; j++) {
865  for (k = 0; k < s->subband_activity[j]; k++)
867  "prediction coefs: %f, %f, %f, %f\n",
868  (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
869  (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
870  (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
871  (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
872  }
873  for (j = base_channel; j < s->prim_channels; j++) {
874  av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
875  for (k = 0; k < s->vq_start_subband[j]; k++)
876  av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
877  av_log(s->avctx, AV_LOG_DEBUG, "\n");
878  }
879  for (j = base_channel; j < s->prim_channels; j++) {
880  av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
881  for (k = 0; k < s->subband_activity[j]; k++)
882  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
883  av_log(s->avctx, AV_LOG_DEBUG, "\n");
884  }
885  for (j = base_channel; j < s->prim_channels; j++) {
886  av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
887  for (k = 0; k < s->subband_activity[j]; k++) {
888  if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
889  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
890  if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
891  av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
892  }
893  av_log(s->avctx, AV_LOG_DEBUG, "\n");
894  }
895  for (j = base_channel; j < s->prim_channels; j++) {
896  if (s->joint_intensity[j] > 0) {
897  int source_channel = s->joint_intensity[j] - 1;
898  av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
899  for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
900  av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
901  av_log(s->avctx, AV_LOG_DEBUG, "\n");
902  }
903  }
904  for (j = base_channel; j < s->prim_channels; j++)
905  for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
906  av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
907  if (!base_channel && s->lfe) {
908  int lfe_samples = 2 * s->lfe * (4 + block_index);
909  int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
910 
911  av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
912  for (j = lfe_samples; j < lfe_end_sample; j++)
913  av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
914  av_log(s->avctx, AV_LOG_DEBUG, "\n");
915  }
916 #endif
917 
918  return 0;
919 }
920 
921 static void qmf_32_subbands(DCAContext *s, int chans,
922  float samples_in[32][8], float *samples_out,
923  float scale)
924 {
925  const float *prCoeff;
926 
927  int sb_act = s->subband_activity[chans];
928 
929  scale *= sqrt(1 / 8.0);
930 
931  /* Select filter */
932  if (!s->multirate_inter) /* Non-perfect reconstruction */
933  prCoeff = fir_32bands_nonperfect;
934  else /* Perfect reconstruction */
935  prCoeff = fir_32bands_perfect;
936 
937  s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
938  s->subband_fir_hist[chans],
939  &s->hist_index[chans],
940  s->subband_fir_noidea[chans], prCoeff,
941  samples_out, s->raXin, scale);
942 }
943 
944 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
945  int num_deci_sample, float *samples_in,
946  float *samples_out)
947 {
948  /* samples_in: An array holding decimated samples.
949  * Samples in current subframe starts from samples_in[0],
950  * while samples_in[-1], samples_in[-2], ..., stores samples
951  * from last subframe as history.
952  *
953  * samples_out: An array holding interpolated samples
954  */
955 
956  int idx;
957  const float *prCoeff;
958  int deciindex;
959 
960  /* Select decimation filter */
961  if (decimation_select == 1) {
962  idx = 1;
963  prCoeff = lfe_fir_128;
964  } else {
965  idx = 0;
966  prCoeff = lfe_fir_64;
967  }
968  /* Interpolation */
969  for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
970  s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
971  samples_in++;
972  samples_out += 2 * 32 * (1 + idx);
973  }
974 }
975 
976 /* downmixing routines */
977 #define MIX_REAR1(samples, s1, rs, coef) \
978  samples[0][i] += samples[s1][i] * coef[rs][0]; \
979  samples[1][i] += samples[s1][i] * coef[rs][1];
980 
981 #define MIX_REAR2(samples, s1, s2, rs, coef) \
982  samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
983  samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
984 
985 #define MIX_FRONT3(samples, coef) \
986  t = samples[c][i]; \
987  u = samples[l][i]; \
988  v = samples[r][i]; \
989  samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
990  samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
991 
992 #define DOWNMIX_TO_STEREO(op1, op2) \
993  for (i = 0; i < 256; i++) { \
994  op1 \
995  op2 \
996  }
997 
998 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
999  float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
1000  const int8_t *channel_mapping)
1001 {
1002  int c, l, r, sl, sr, s;
1003  int i;
1004  float t, u, v;
1005 
1006  switch (srcfmt) {
1007  case DCA_MONO:
1008  case DCA_4F2R:
1009  av_log(NULL, 0, "Not implemented!\n");
1010  break;
1011  case DCA_CHANNEL:
1012  case DCA_STEREO:
1013  case DCA_STEREO_TOTAL:
1014  case DCA_STEREO_SUMDIFF:
1015  break;
1016  case DCA_3F:
1017  c = channel_mapping[0];
1018  l = channel_mapping[1];
1019  r = channel_mapping[2];
1020  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1021  break;
1022  case DCA_2F1R:
1023  s = channel_mapping[2];
1024  DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
1025  break;
1026  case DCA_3F1R:
1027  c = channel_mapping[0];
1028  l = channel_mapping[1];
1029  r = channel_mapping[2];
1030  s = channel_mapping[3];
1031  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1032  MIX_REAR1(samples, s, 3, coef));
1033  break;
1034  case DCA_2F2R:
1035  sl = channel_mapping[2];
1036  sr = channel_mapping[3];
1037  DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
1038  break;
1039  case DCA_3F2R:
1040  c = channel_mapping[0];
1041  l = channel_mapping[1];
1042  r = channel_mapping[2];
1043  sl = channel_mapping[3];
1044  sr = channel_mapping[4];
1045  DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1046  MIX_REAR2(samples, sl, sr, 3, coef));
1047  break;
1048  }
1049  if (lfe_present) {
1050  int lf_buf = dca_lfe_index[srcfmt];
1051  int lf_idx = dca_channels [srcfmt];
1052  for (i = 0; i < 256; i++) {
1053  samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
1054  samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
1055  }
1056  }
1057 }
1058 
1059 
1060 #ifndef decode_blockcodes
1061 /* Very compact version of the block code decoder that does not use table
1062  * look-up but is slightly slower */
1063 static int decode_blockcode(int code, int levels, int32_t *values)
1064 {
1065  int i;
1066  int offset = (levels - 1) >> 1;
1067 
1068  for (i = 0; i < 4; i++) {
1069  int div = FASTDIV(code, levels);
1070  values[i] = code - offset - div * levels;
1071  code = div;
1072  }
1073 
1074  return code;
1075 }
1076 
1077 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
1078 {
1079  return decode_blockcode(code1, levels, values) |
1080  decode_blockcode(code2, levels, values + 4);
1081 }
1082 #endif
1083 
1084 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1085 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1086 
1087 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1088 {
1089  int k, l;
1090  int subsubframe = s->current_subsubframe;
1091 
1092  const float *quant_step_table;
1093 
1094  /* FIXME */
1095  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1097 
1098  /*
1099  * Audio data
1100  */
1101 
1102  /* Select quantization step size table */
1103  if (s->bit_rate_index == 0x1f)
1104  quant_step_table = lossless_quant_d;
1105  else
1106  quant_step_table = lossy_quant_d;
1107 
1108  for (k = base_channel; k < s->prim_channels; k++) {
1109  float rscale[DCA_SUBBANDS];
1110 
1111  if (get_bits_left(&s->gb) < 0)
1112  return AVERROR_INVALIDDATA;
1113 
1114  for (l = 0; l < s->vq_start_subband[k]; l++) {
1115  int m;
1116 
1117  /* Select the mid-tread linear quantizer */
1118  int abits = s->bitalloc[k][l];
1119 
1120  float quant_step_size = quant_step_table[abits];
1121 
1122  /*
1123  * Determine quantization index code book and its type
1124  */
1125 
1126  /* Select quantization index code book */
1127  int sel = s->quant_index_huffman[k][abits];
1128 
1129  /*
1130  * Extract bits from the bit stream
1131  */
1132  if (!abits) {
1133  rscale[l] = 0;
1134  memset(block + 8 * l, 0, 8 * sizeof(block[0]));
1135  } else {
1136  /* Deal with transients */
1137  int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1138  rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
1139  s->scalefactor_adj[k][sel];
1140 
1141  if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1142  if (abits <= 7) {
1143  /* Block code */
1144  int block_code1, block_code2, size, levels, err;
1145 
1146  size = abits_sizes[abits - 1];
1147  levels = abits_levels[abits - 1];
1148 
1149  block_code1 = get_bits(&s->gb, size);
1150  block_code2 = get_bits(&s->gb, size);
1151  err = decode_blockcodes(block_code1, block_code2,
1152  levels, block + 8 * l);
1153  if (err) {
1155  "ERROR: block code look-up failed\n");
1156  return AVERROR_INVALIDDATA;
1157  }
1158  } else {
1159  /* no coding */
1160  for (m = 0; m < 8; m++)
1161  block[8 * l + m] = get_sbits(&s->gb, abits - 3);
1162  }
1163  } else {
1164  /* Huffman coded */
1165  for (m = 0; m < 8; m++)
1166  block[8 * l + m] = get_bitalloc(&s->gb,
1167  &dca_smpl_bitalloc[abits], sel);
1168  }
1169 
1170  }
1171  }
1172 
1173  s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
1174  block, rscale, 8 * s->vq_start_subband[k]);
1175 
1176  for (l = 0; l < s->vq_start_subband[k]; l++) {
1177  int m;
1178  /*
1179  * Inverse ADPCM if in prediction mode
1180  */
1181  if (s->prediction_mode[k][l]) {
1182  int n;
1183  if (s->predictor_history)
1184  subband_samples[k][l][0] += (adpcm_vb[s->prediction_vq[k][l]][0] *
1185  s->subband_samples_hist[k][l][3] +
1186  adpcm_vb[s->prediction_vq[k][l]][1] *
1187  s->subband_samples_hist[k][l][2] +
1188  adpcm_vb[s->prediction_vq[k][l]][2] *
1189  s->subband_samples_hist[k][l][1] +
1190  adpcm_vb[s->prediction_vq[k][l]][3] *
1191  s->subband_samples_hist[k][l][0]) *
1192  (1.0f / 8192);
1193  for (m = 1; m < 8; m++) {
1194  float sum = adpcm_vb[s->prediction_vq[k][l]][0] *
1195  subband_samples[k][l][m - 1];
1196  for (n = 2; n <= 4; n++)
1197  if (m >= n)
1198  sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1199  subband_samples[k][l][m - n];
1200  else if (s->predictor_history)
1201  sum += adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1202  s->subband_samples_hist[k][l][m - n + 4];
1203  subband_samples[k][l][m] += sum * 1.0f / 8192;
1204  }
1205  }
1206  }
1207 
1208  /*
1209  * Decode VQ encoded high frequencies
1210  */
1211  if (s->subband_activity[k] > s->vq_start_subband[k]) {
1212  if (!s->debug_flag & 0x01) {
1214  "Stream with high frequencies VQ coding\n");
1215  s->debug_flag |= 0x01;
1216  }
1217  s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
1218  high_freq_vq, subsubframe * 8,
1219  s->scale_factor[k], s->vq_start_subband[k],
1220  s->subband_activity[k]);
1221  }
1222  }
1223 
1224  /* Check for DSYNC after subsubframe */
1225  if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1226  if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1227 #ifdef TRACE
1228  av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1229 #endif
1230  } else {
1231  av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1232  return AVERROR_INVALIDDATA;
1233  }
1234  }
1235 
1236  /* Backup predictor history for adpcm */
1237  for (k = base_channel; k < s->prim_channels; k++)
1238  for (l = 0; l < s->vq_start_subband[k]; l++)
1239  AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1240 
1241  return 0;
1242 }
1243 
1244 static int dca_filter_channels(DCAContext *s, int block_index)
1245 {
1246  float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1247  int k;
1248 
1249  /* 32 subbands QMF */
1250  for (k = 0; k < s->prim_channels; k++) {
1251 /* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1252  0, 8388608.0, 8388608.0 };*/
1253  if (s->channel_order_tab[k] >= 0)
1254  qmf_32_subbands(s, k, subband_samples[k],
1256  M_SQRT1_2 / 32768.0 /* pcm_to_double[s->source_pcm_res] */);
1257  }
1258 
1259  /* Generate LFE samples for this subsubframe FIXME!!! */
1260  if (s->lfe) {
1261  lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1262  s->lfe_data + 2 * s->lfe * (block_index + 4),
1264  /* Outputs 20bits pcm samples */
1265  }
1266 
1267  /* Downmixing to Stereo */
1268  if (s->prim_channels + !!s->lfe > 2 &&
1271  s->channel_order_tab);
1272  }
1273 
1274  return 0;
1275 }
1276 
1277 
1278 static int dca_subframe_footer(DCAContext *s, int base_channel)
1279 {
1280  int in, out, aux_data_count, aux_data_end, reserved;
1281  uint32_t nsyncaux;
1282 
1283  /*
1284  * Unpack optional information
1285  */
1286 
1287  /* presumably optional information only appears in the core? */
1288  if (!base_channel) {
1289  if (s->timestamp)
1290  skip_bits_long(&s->gb, 32);
1291 
1292  if (s->aux_data) {
1293  aux_data_count = get_bits(&s->gb, 6);
1294 
1295  // align (32-bit)
1296  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1297 
1298  aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1299 
1300  if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1301  av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1302  nsyncaux);
1303  return AVERROR_INVALIDDATA;
1304  }
1305 
1306  if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1308  "Auxiliary Decode Time Stamp Flag");
1309  // align (4-bit)
1310  skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1311  // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1312  skip_bits_long(&s->gb, 44);
1313  }
1314 
1315  if ((s->core_downmix = get_bits1(&s->gb))) {
1316  int am = get_bits(&s->gb, 3);
1317  switch (am) {
1318  case 0:
1320  break;
1321  case 1:
1323  break;
1324  case 2:
1326  break;
1327  case 3:
1329  break;
1330  case 4:
1332  break;
1333  case 5:
1335  break;
1336  case 6:
1338  break;
1339  default:
1341  "Invalid mode %d for embedded downmix coefficients\n",
1342  am);
1343  return AVERROR_INVALIDDATA;
1344  }
1345  for (out = 0; out < dca_channels[s->core_downmix_amode]; out++) {
1346  for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1347  uint16_t tmp = get_bits(&s->gb, 9);
1348  if ((tmp & 0xFF) > 241) {
1350  "Invalid downmix coefficient code %"PRIu16"\n",
1351  tmp);
1352  return AVERROR_INVALIDDATA;
1353  }
1354  s->core_downmix_codes[in][out] = tmp;
1355  }
1356  }
1357  }
1358 
1359  align_get_bits(&s->gb); // byte align
1360  skip_bits(&s->gb, 16); // nAUXCRC16
1361 
1362  // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1363  if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1365  "Overread auxiliary data by %d bits\n", -reserved);
1366  return AVERROR_INVALIDDATA;
1367  } else if (reserved) {
1369  "Core auxiliary data reserved content");
1370  skip_bits_long(&s->gb, reserved);
1371  }
1372  }
1373 
1374  if (s->crc_present && s->dynrange)
1375  get_bits(&s->gb, 16);
1376  }
1377 
1378  return 0;
1379 }
1380 
1387 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1388 {
1389  int ret;
1390 
1391  /* Sanity check */
1392  if (s->current_subframe >= s->subframes) {
1393  av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1394  s->current_subframe, s->subframes);
1395  return AVERROR_INVALIDDATA;
1396  }
1397 
1398  if (!s->current_subsubframe) {
1399 #ifdef TRACE
1400  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1401 #endif
1402  /* Read subframe header */
1403  if ((ret = dca_subframe_header(s, base_channel, block_index)))
1404  return ret;
1405  }
1406 
1407  /* Read subsubframe */
1408 #ifdef TRACE
1409  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1410 #endif
1411  if ((ret = dca_subsubframe(s, base_channel, block_index)))
1412  return ret;
1413 
1414  /* Update state */
1415  s->current_subsubframe++;
1417  s->current_subsubframe = 0;
1418  s->current_subframe++;
1419  }
1420  if (s->current_subframe >= s->subframes) {
1421 #ifdef TRACE
1422  av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1423 #endif
1424  /* Read subframe footer */
1425  if ((ret = dca_subframe_footer(s, base_channel)))
1426  return ret;
1427  }
1428 
1429  return 0;
1430 }
1431 
1435 static int dca_exss_mask2count(int mask)
1436 {
1437  /* count bits that mean speaker pairs twice */
1438  return av_popcount(mask) +
1439  av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1448 }
1449 
1453 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1454 {
1455  int i;
1456 
1457  for (i = 0; i < channels; i++) {
1458  int mix_map_mask = get_bits(gb, out_ch);
1459  int num_coeffs = av_popcount(mix_map_mask);
1460  skip_bits_long(gb, num_coeffs * 6);
1461  }
1462 }
1463 
1468 {
1469  int header_pos = get_bits_count(&s->gb);
1470  int header_size;
1471  int channels;
1472  int embedded_stereo = 0;
1473  int embedded_6ch = 0;
1474  int drc_code_present;
1475  int extensions_mask;
1476  int i, j;
1477 
1478  if (get_bits_left(&s->gb) < 16)
1479  return -1;
1480 
1481  /* We will parse just enough to get to the extensions bitmask with which
1482  * we can set the profile value. */
1483 
1484  header_size = get_bits(&s->gb, 9) + 1;
1485  skip_bits(&s->gb, 3); // asset index
1486 
1487  if (s->static_fields) {
1488  if (get_bits1(&s->gb))
1489  skip_bits(&s->gb, 4); // asset type descriptor
1490  if (get_bits1(&s->gb))
1491  skip_bits_long(&s->gb, 24); // language descriptor
1492 
1493  if (get_bits1(&s->gb)) {
1494  /* How can one fit 1024 bytes of text here if the maximum value
1495  * for the asset header size field above was 512 bytes? */
1496  int text_length = get_bits(&s->gb, 10) + 1;
1497  if (get_bits_left(&s->gb) < text_length * 8)
1498  return -1;
1499  skip_bits_long(&s->gb, text_length * 8); // info text
1500  }
1501 
1502  skip_bits(&s->gb, 5); // bit resolution - 1
1503  skip_bits(&s->gb, 4); // max sample rate code
1504  channels = get_bits(&s->gb, 8) + 1;
1505 
1506  if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1507  int spkr_remap_sets;
1508  int spkr_mask_size = 16;
1509  int num_spkrs[7];
1510 
1511  if (channels > 2)
1512  embedded_stereo = get_bits1(&s->gb);
1513  if (channels > 6)
1514  embedded_6ch = get_bits1(&s->gb);
1515 
1516  if (get_bits1(&s->gb)) {
1517  spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1518  skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1519  }
1520 
1521  spkr_remap_sets = get_bits(&s->gb, 3);
1522 
1523  for (i = 0; i < spkr_remap_sets; i++) {
1524  /* std layout mask for each remap set */
1525  num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1526  }
1527 
1528  for (i = 0; i < spkr_remap_sets; i++) {
1529  int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1530  if (get_bits_left(&s->gb) < 0)
1531  return -1;
1532 
1533  for (j = 0; j < num_spkrs[i]; j++) {
1534  int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1535  int num_dec_ch = av_popcount(remap_dec_ch_mask);
1536  skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1537  }
1538  }
1539 
1540  } else {
1541  skip_bits(&s->gb, 3); // representation type
1542  }
1543  }
1544 
1545  drc_code_present = get_bits1(&s->gb);
1546  if (drc_code_present)
1547  get_bits(&s->gb, 8); // drc code
1548 
1549  if (get_bits1(&s->gb))
1550  skip_bits(&s->gb, 5); // dialog normalization code
1551 
1552  if (drc_code_present && embedded_stereo)
1553  get_bits(&s->gb, 8); // drc stereo code
1554 
1555  if (s->mix_metadata && get_bits1(&s->gb)) {
1556  skip_bits(&s->gb, 1); // external mix
1557  skip_bits(&s->gb, 6); // post mix gain code
1558 
1559  if (get_bits(&s->gb, 2) != 3) // mixer drc code
1560  skip_bits(&s->gb, 3); // drc limit
1561  else
1562  skip_bits(&s->gb, 8); // custom drc code
1563 
1564  if (get_bits1(&s->gb)) // channel specific scaling
1565  for (i = 0; i < s->num_mix_configs; i++)
1566  skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1567  else
1568  skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1569 
1570  for (i = 0; i < s->num_mix_configs; i++) {
1571  if (get_bits_left(&s->gb) < 0)
1572  return -1;
1573  dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1574  if (embedded_6ch)
1576  if (embedded_stereo)
1578  }
1579  }
1580 
1581  switch (get_bits(&s->gb, 2)) {
1582  case 0: extensions_mask = get_bits(&s->gb, 12); break;
1583  case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1584  case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1585  case 3: extensions_mask = 0; /* aux coding */ break;
1586  }
1587 
1588  /* not parsed further, we were only interested in the extensions mask */
1589 
1590  if (get_bits_left(&s->gb) < 0)
1591  return -1;
1592 
1593  if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1594  av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1595  return -1;
1596  }
1597  skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1598 
1599  if (extensions_mask & DCA_EXT_EXSS_XLL)
1601  else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1604 
1605  if (!(extensions_mask & DCA_EXT_CORE))
1606  av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1607  if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1609  "DTS extensions detection mismatch (%d, %d)\n",
1610  extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1611 
1612  return 0;
1613 }
1614 
1619 {
1620  int ss_index;
1621  int blownup;
1622  int num_audiop = 1;
1623  int num_assets = 1;
1624  int active_ss_mask[8];
1625  int i, j;
1626 
1627  if (get_bits_left(&s->gb) < 52)
1628  return;
1629 
1630  skip_bits(&s->gb, 8); // user data
1631  ss_index = get_bits(&s->gb, 2);
1632 
1633  blownup = get_bits1(&s->gb);
1634  skip_bits(&s->gb, 8 + 4 * blownup); // header_size
1635  skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1636 
1637  s->static_fields = get_bits1(&s->gb);
1638  if (s->static_fields) {
1639  skip_bits(&s->gb, 2); // reference clock code
1640  skip_bits(&s->gb, 3); // frame duration code
1641 
1642  if (get_bits1(&s->gb))
1643  skip_bits_long(&s->gb, 36); // timestamp
1644 
1645  /* a single stream can contain multiple audio assets that can be
1646  * combined to form multiple audio presentations */
1647 
1648  num_audiop = get_bits(&s->gb, 3) + 1;
1649  if (num_audiop > 1) {
1651  "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  avpriv_request_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 
1698 static float dca_dmix_code(unsigned code)
1699 {
1700  int sign = (code >> 8) - 1;
1701  code &= 0xff;
1702  return ((dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1703 }
1704 
1709 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1710  int *got_frame_ptr, AVPacket *avpkt)
1711 {
1712  AVFrame *frame = data;
1713  const uint8_t *buf = avpkt->data;
1714  int buf_size = avpkt->size;
1715 
1716  int lfe_samples;
1717  int num_core_channels = 0;
1718  int i, ret;
1719  float **samples_flt;
1720  DCAContext *s = avctx->priv_data;
1721  int channels, full_channels;
1722  int core_ss_end;
1723 
1724 
1725  s->xch_present = 0;
1726 
1727  s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1730  av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1731  return AVERROR_INVALIDDATA;
1732  }
1733 
1734  if ((ret = dca_parse_frame_header(s)) < 0) {
1735  //seems like the frame is corrupt, try with the next one
1736  return ret;
1737  }
1738  //set AVCodec values with parsed data
1739  avctx->sample_rate = s->sample_rate;
1740  avctx->bit_rate = s->bit_rate;
1741 
1742  s->profile = FF_PROFILE_DTS;
1743 
1744  for (i = 0; i < (s->sample_blocks / 8); i++) {
1745  if ((ret = dca_decode_block(s, 0, i))) {
1746  av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1747  return ret;
1748  }
1749  }
1750 
1751  /* record number of core channels incase less than max channels are requested */
1752  num_core_channels = s->prim_channels;
1753 
1754  if (s->ext_coding)
1756  else
1757  s->core_ext_mask = 0;
1758 
1759  core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1760 
1761  /* only scan for extensions if ext_descr was unknown or indicated a
1762  * supported XCh extension */
1763  if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1764 
1765  /* if ext_descr was unknown, clear s->core_ext_mask so that the
1766  * extensions scan can fill it up */
1767  s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1768 
1769  /* extensions start at 32-bit boundaries into bitstream */
1770  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1771 
1772  while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1773  uint32_t bits = get_bits_long(&s->gb, 32);
1774 
1775  switch (bits) {
1776  case 0x5a5a5a5a: {
1777  int ext_amode, xch_fsize;
1778 
1780 
1781  /* validate sync word using XCHFSIZE field */
1782  xch_fsize = show_bits(&s->gb, 10);
1783  if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1784  (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1785  continue;
1786 
1787  /* skip length-to-end-of-frame field for the moment */
1788  skip_bits(&s->gb, 10);
1789 
1790  s->core_ext_mask |= DCA_EXT_XCH;
1791 
1792  /* extension amode(number of channels in extension) should be 1 */
1793  /* AFAIK XCh is not used for more channels */
1794  if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1795  av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
1796  " supported!\n", ext_amode);
1797  continue;
1798  }
1799 
1800  /* much like core primary audio coding header */
1802 
1803  for (i = 0; i < (s->sample_blocks / 8); i++)
1804  if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1805  av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1806  continue;
1807  }
1808 
1809  s->xch_present = 1;
1810  break;
1811  }
1812  case 0x47004a03:
1813  /* XXCh: extended channels */
1814  /* usually found either in core or HD part in DTS-HD HRA streams,
1815  * but not in DTS-ES which contains XCh extensions instead */
1817  break;
1818 
1819  case 0x1d95f262: {
1820  int fsize96 = show_bits(&s->gb, 12) + 1;
1821  if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1822  continue;
1823 
1824  av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1825  get_bits_count(&s->gb));
1826  skip_bits(&s->gb, 12);
1827  av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1828  av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1829 
1830  s->core_ext_mask |= DCA_EXT_X96;
1831  break;
1832  }
1833  }
1834 
1835  skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1836  }
1837  } else {
1838  /* no supported extensions, skip the rest of the core substream */
1839  skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1840  }
1841 
1842  if (s->core_ext_mask & DCA_EXT_X96)
1844  else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1846 
1847  /* check for ExSS (HD part) */
1848  if (s->dca_buffer_size - s->frame_size > 32 &&
1849  get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1851 
1852  avctx->profile = s->profile;
1853 
1854  full_channels = channels = s->prim_channels + !!s->lfe;
1855 
1856  if (s->amode < 16) {
1858 
1859  if (s->prim_channels + !!s->lfe > 2 &&
1861  /*
1862  * Neither the core's auxiliary data nor our default tables contain
1863  * downmix coefficients for the additional channel coded in the XCh
1864  * extension, so when we're doing a Stereo downmix, don't decode it.
1865  */
1866  s->xch_disable = 1;
1867  }
1868 
1869 #if FF_API_REQUEST_CHANNELS
1871  if (s->xch_present && !s->xch_disable &&
1872  (!avctx->request_channels ||
1873  avctx->request_channels > num_core_channels + !!s->lfe)) {
1875 #else
1876  if (s->xch_present && !s->xch_disable) {
1877 #endif
1879  if (s->lfe) {
1882  } else {
1884  }
1885  } else {
1886  channels = num_core_channels + !!s->lfe;
1887  s->xch_present = 0; /* disable further xch processing */
1888  if (s->lfe) {
1891  } else
1893  }
1894 
1895  if (channels > !!s->lfe &&
1896  s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1897  return AVERROR_INVALIDDATA;
1898 
1899  if (num_core_channels + !!s->lfe > 2 &&
1901  channels = 2;
1902  s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1904 
1905  /* Stereo downmix coefficients
1906  *
1907  * The decoder can only downmix to 2-channel, so we need to ensure
1908  * embedded downmix coefficients are actually targeting 2-channel.
1909  */
1910  if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1912  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1913  /* Range checked earlier */
1914  s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1915  s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1916  }
1917  s->output = s->core_downmix_amode;
1918  } else {
1919  int am = s->amode & DCA_CHANNEL_MASK;
1920  if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
1922  "Invalid channel mode %d\n", am);
1923  return AVERROR_INVALIDDATA;
1924  }
1925  if (num_core_channels + !!s->lfe >
1927  avpriv_request_sample(s->avctx, "Downmixing %d channels",
1928  s->prim_channels + !!s->lfe);
1929  return AVERROR_PATCHWELCOME;
1930  }
1931  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1932  s->downmix_coef[i][0] = dca_default_coeffs[am][i][0];
1933  s->downmix_coef[i][1] = dca_default_coeffs[am][i][1];
1934  }
1935  }
1936  av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1937  for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1938  av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1939  s->downmix_coef[i][0]);
1940  av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1941  s->downmix_coef[i][1]);
1942  }
1943  av_dlog(s->avctx, "\n");
1944  }
1945  } else {
1946  av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1947  return AVERROR_INVALIDDATA;
1948  }
1949  avctx->channels = channels;
1950 
1951  /* get output buffer */
1952  frame->nb_samples = 256 * (s->sample_blocks / 8);
1953  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1954  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1955  return ret;
1956  }
1957  samples_flt = (float **)frame->extended_data;
1958 
1959  /* allocate buffer for extra channels if downmixing */
1960  if (avctx->channels < full_channels) {
1961  ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1962  frame->nb_samples,
1963  avctx->sample_fmt, 0);
1964  if (ret < 0)
1965  return ret;
1966 
1968  &s->extra_channels_buffer_size, ret);
1969  if (!s->extra_channels_buffer)
1970  return AVERROR(ENOMEM);
1971 
1974  full_channels - channels,
1975  frame->nb_samples, avctx->sample_fmt, 0);
1976  if (ret < 0)
1977  return ret;
1978  }
1979 
1980  /* filter to get final output */
1981  for (i = 0; i < (s->sample_blocks / 8); i++) {
1982  int ch;
1983 
1984  for (ch = 0; ch < channels; ch++)
1985  s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1986  for (; ch < full_channels; ch++)
1987  s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1988 
1989  dca_filter_channels(s, i);
1990 
1991  /* If this was marked as a DTS-ES stream we need to subtract back- */
1992  /* channel from SL & SR to remove matrixed back-channel signal */
1993  if ((s->source_pcm_res & 1) && s->xch_present) {
1994  float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1995  float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1996  float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1997  s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1998  s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1999  }
2000  }
2001 
2002  /* update lfe history */
2003  lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2004  for (i = 0; i < 2 * s->lfe * 4; i++)
2005  s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2006 
2007  /* AVMatrixEncoding
2008  *
2009  * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
2011  (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
2013  if (ret < 0)
2014  return ret;
2015 
2016  *got_frame_ptr = 1;
2017 
2018  return buf_size;
2019 }
2020 
2021 
2022 
2030 {
2031  DCAContext *s = avctx->priv_data;
2032 
2033  s->avctx = avctx;
2034  dca_init_vlcs();
2035 
2037  ff_mdct_init(&s->imdct, 6, 1, 1.0);
2039  ff_dcadsp_init(&s->dcadsp);
2040  ff_fmt_convert_init(&s->fmt_conv, avctx);
2041 
2042  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2043 
2044  /* allow downmixing to stereo */
2045 #if FF_API_REQUEST_CHANNELS
2047  if (avctx->request_channels == 2)
2050 #endif
2051  if (avctx->channels > 2 &&
2053  avctx->channels = 2;
2054 
2055  return 0;
2056 }
2057 
2059 {
2060  DCAContext *s = avctx->priv_data;
2061  ff_mdct_end(&s->imdct);
2063  return 0;
2064 }
2065 
2066 static const AVProfile profiles[] = {
2067  { FF_PROFILE_DTS, "DTS" },
2068  { FF_PROFILE_DTS_ES, "DTS-ES" },
2069  { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2070  { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2071  { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2072  { FF_PROFILE_UNKNOWN },
2073 };
2074 
2075 static const AVOption options[] = {
2076  { "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 },
2077  { NULL },
2078 };
2079 
2080 static const AVClass dca_decoder_class = {
2081  .class_name = "DCA decoder",
2082  .item_name = av_default_item_name,
2083  .option = options,
2084  .version = LIBAVUTIL_VERSION_INT,
2085 };
2086 
2088  .name = "dca",
2089  .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2090  .type = AVMEDIA_TYPE_AUDIO,
2091  .id = AV_CODEC_ID_DTS,
2092  .priv_data_size = sizeof(DCAContext),
2093  .init = dca_decode_init,
2095  .close = dca_decode_end,
2096  .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2097  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2099  .profiles = NULL_IF_CONFIG_SMALL(profiles),
2100  .priv_class = &dca_decoder_class,
2101 };
int wrap
wrap for get_vlc2()
Definition: dcadec.c:271
float, planar
Definition: samplefmt.h:72
int ext_descr
extension audio descriptor flag
Definition: dcadec.c:305
static const int8_t bitalloc_offsets[10]
Definition: dcahuff.h:1024
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
quantization index codebook select
Definition: dcadec.c:329
#define DCA_BLOCKS_MAX
Definition: dcadec.c:60
int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]
bit allocation quantizer select
Definition: dcadec.c:328
int crc_present
crc is present in the bitstream
Definition: dcadec.c:293
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
low bitrate component in ExSS
Definition: dcadec.c:106
int timestamp
embedded time stamp flag
Definition: dcadec.c:302
int amode
audio channels arrangement
Definition: dcadec.c:296
AVOption.
Definition: opt.h:234
static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
Definition: dcadec.c:1077
static const AVProfile profiles[]
Definition: dcadec.c:2066
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: dcadec.c:326
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static const uint32_t scale_factor_quant6[64]
Definition: dcadata.h:4157
int mix_config_num_ch[4]
number of channels in each mix out configuration
Definition: dcadec.c:392
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:199
attribute_deprecated int request_channels
Decoder should decode to this many channels if it can (0 for default)
Definition: avcodec.h:1844
FmtConvertContext fmt_conv
Definition: dcadec.c:401
static int dca_parse_frame_header(DCAContext *s)
Definition: dcadec.c:558
static int dca_exss_parse_asset_header(DCAContext *s)
Parse extension substream asset header (HD)
Definition: dcadec.c:1467
static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
Skip mixing coefficients of a single mix out configuration (HD)
Definition: dcadec.c:1453
void(* lfe_fir[2])(float *out, const float *in, const float *coefs)
Definition: dcadsp.h:28
#define FF_PROFILE_DTS_HD_HRA
Definition: avcodec.h:2640
int vq_start_subband[DCA_PRIM_CHANNELS_MAX]
high frequency vq start subband
Definition: dcadec.c:324
XXCh channels extension in core substream.
Definition: dcadec.c:99
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:269
int size
Definition: avcodec.h:974
float subband_fir_noidea[DCA_PRIM_CHANNELS_MAX][32]
Definition: dcadec.c:360
int hist_index[DCA_PRIM_CHANNELS_MAX]
Definition: dcadec.c:361
int samples_deficit
deficit sample count
Definition: dcadec.c:292
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:58
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_dlog(ac->avr,"%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
uint8_t core_downmix
embedded downmix coefficients available
Definition: dcadec.c:348
Definition: dcadec.c:69
static const float lossless_quant_d[32]
Definition: dcadata.h:4210
int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
Convert bitstream to one representation based on sync marker.
Definition: dca.c:35
int dynrange
embedded dynamic range flag
Definition: dcadec.c:301
int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
joint subband scale factors
Definition: dcadec.c:341
int version
encoder software revision
Definition: dcadec.c:312
static const int8_t dca_channel_reorder_lfe_xch[][9]
Definition: dcadec.c:194
#define VLC_TYPE
Definition: get_bits.h:62
#define FF_ARRAY_ELEMS(a)
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
core in core substream
Definition: dcadec.c:98
#define AV_CH_LAYOUT_STEREO
static BitAlloc dca_scalefactor
scalefactor VLCs
Definition: dcadec.c:277
SynthFilterContext synth
Definition: dcadec.c:399
int profile
profile
Definition: avcodec.h:2622
AVCodec.
Definition: avcodec.h:2796
#define DCA_BUFFER_PADDING_SIZE
Definition: dcadec.c:263
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:226
float subband_fir_hist[DCA_PRIM_CHANNELS_MAX][512]
Definition: dcadec.c:359
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
float * extra_channels[DCA_PRIM_CHANNELS_MAX+1]
Definition: dcadec.c:368
#define DCA_HD_MARKER
DCA-HD specific block starts with this marker.
Definition: dca.h:38
static int dca_subframe_footer(DCAContext *s, int base_channel)
Definition: dcadec.c:1278
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:38
static BitAlloc dca_tmode
transition mode VLCs
Definition: dcadec.c:276
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
Definition: float_dsp.h:54
#define MIX_FRONT3(samples, coef)
Definition: dcadec.c:985
static int dca_exss_mask2count(int mask)
Return the number of channels in an ExSS speaker mask (HD)
Definition: dcadec.c:1435
static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:676
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
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: dcadec.c:327
static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
Decode a dca frame block.
Definition: dcadec.c:1387
static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
Definition: dcadec.c:1087
uint8_t bits
Definition: crc.c:251
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
uint8_t
#define av_cold
Definition: attributes.h:66
static const uint16_t dca_vlc_offs[]
Definition: dcadec.c:404
int xch_base_channel
index of first (only) channel containing XCH data
Definition: dcadec.c:385
float subband_samples_hist[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4]
Definition: dcadec.c:358
#define DCA_LFE
Definition: dcadec.c:256
AVOptions.
int dca_buffer_size
how much data is in the dca_buffer
Definition: dcadec.c:373
static const uint32_t scale_factor_quant7[128]
Definition: dcadata.h:4168
#define MIX_REAR2(samples, s1, s2, rs, coef)
Definition: dcadec.c:981
static void get_array(GetBitContext *gb, int *dst, int len, int bits)
Definition: dcadec.c:468
static av_cold int dca_decode_end(AVCodecContext *avctx)
Definition: dcadec.c:2058
#define AV_CH_LOW_FREQUENCY
int header_crc
header crc check bytes
Definition: dcadec.c:310
DCAMode
Definition: dcadec.c:63
int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
transition mode (transients)
Definition: dcadec.c:338
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
av_cold void ff_dcadsp_init(DCADSPContext *s)
Definition: dcadsp.c:104
uint8_t * data
Definition: avcodec.h:973
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:194
int sample_rate
audio sampling rate
Definition: dcadec.c:297
bitstream reader API header.
#define AV_CH_BACK_LEFT
AVCodecContext * avctx
Definition: dcadec.c:289
XCh channel extension in core substream.
Definition: dcadec.c:101
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:658
static BitAlloc dca_bitalloc_index
indexes for samples VLC select
Definition: dcadec.c:275
int lfe
low frequency effects flag
Definition: dcadec.c:308
static const float fir_32bands_nonperfect[]
Definition: dcadata.h:6792
static BitAlloc dca_smpl_bitalloc[11]
samples VLCs
Definition: dcadec.c:278
XXCh channels extension in ExSS.
Definition: dcadec.c:104
static const uint8_t bitalloc_sizes[10]
Definition: dcahuff.h:1022
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:998
#define r
Definition: input.c:51
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
#define DCA_CHANNEL_MASK
Definition: dcadec.c:254
int predictor_history
predictor history flag
Definition: dcadec.c:309
int dynrange_coef
dynamic range coefficient
Definition: dcadec.c:343
int joint_huff[DCA_PRIM_CHANNELS_MAX]
joint subband scale factors codebook
Definition: dcadec.c:340
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX+1][4]
embedded downmix coefficients (9-bit codes)
Definition: dcadec.c:350
FFTContext imdct
Definition: dcadec.c:398
static void lfe_interpolation_fir(DCAContext *s, int decimation_select, int num_deci_sample, float *samples_in, float *samples_out)
Definition: dcadec.c:944
float raXin[32]
Definition: dcadec.c:362
float subband_samples[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8]
Definition: dcadec.c:366
static const uint16_t mask[17]
Definition: lzw.c:38
#define MIX_REAR1(samples, s1, rs, coef)
Definition: dcadec.c:977
#define DCA_PRIM_CHANNELS_MAX
Definition: dcadec.c:56
static const uint16_t bitalloc_12_codes[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:50
#define AVERROR(e)
Definition: error.h:43
int current_subsubframe
Definition: dcadec.c:379
sample_fmts
Definition: avconv_filter.c:68
int hdcd
source material is mastered in HDCD
Definition: dcadec.c:304
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int mix_metadata
mixing metadata present
Definition: dcadec.c:390
#define FF_PROFILE_DTS_ES
Definition: avcodec.h:2638
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static const uint8_t bitalloc_12_bits[BITALLOC_12_COUNT][12]
Definition: dcahuff.h:72
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
bit allocation index
Definition: dcadec.c:337
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:1709
#define ff_mdct_init
Definition: fft.h:151
int debug_flag
used for suppressing repeated error messages output
Definition: dcadec.c:396
#define FFMAX(a, b)
Definition: common.h:55
#define DCA_CORE_EXTS
Definition: dcadec.c:123
DCAExtensionMask
Definition: dcadec.c:97
Definition: get_bits.h:64
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
static const int8_t dca_lfe_index[]
Definition: dcadec.c:171
const int8_t * channel_order_tab
channel reordering table, lfe and non lfe
Definition: dcadec.c:375
float lfe_data[2 *DCA_LFE_MAX *(DCA_BLOCKS_MAX+4)]
Low frequency effect data.
Definition: dcadec.c:354
common internal API header
static const uint8_t tmode_bits[TMODE_COUNT][4]
Definition: dcahuff.h:37
static void qmf_32_subbands(DCAContext *s, int chans, float samples_in[32][8], float *samples_out, float scale)
Definition: dcadec.c:921
int front_sum
front sum/difference flag
Definition: dcadec.c:315
#define DCA_ABITS_MAX
Definition: dcadec.c:57
int xch_disable
whether the XCh extension should be decoded or not
Definition: dcadec.c:386
int source_pcm_res
source pcm resolution
Definition: dcadec.c:314
Definition: fft.h:73
int bit_rate
the average bitrate
Definition: avcodec.h:1114
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:57
static const int8_t dca_channel_reorder_lfe[][9]
Definition: dcadec.c:175
static const int8_t dca_channel_reorder_nolfe[][9]
Definition: dcadec.c:213
int surround_sum
surround sum/difference flag
Definition: dcadec.c:316
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2623
static av_cold void dca_init_vlcs(void)
Definition: dcadec.c:413
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: dcadec.c:372
static const uint8_t scales_bits[SCALES_COUNT][129]
Definition: dcahuff.h:181
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:254
int joint_intensity[DCA_PRIM_CHANNELS_MAX]
joint intensity coding index
Definition: dcadec.c:325
static const int16_t adpcm_vb[4096][4]
Definition: dcadata.h:55
static const uint16_t *const bitalloc_codes[10][8]
Definition: dcahuff.h:1040
int multirate_inter
multirate interpolator switch
Definition: dcadec.c:311
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:522
#define FF_PROFILE_DTS
Definition: avcodec.h:2637
static const uint8_t dca_channels[16]
Definition: dcadata.h:41
#define AV_CH_FRONT_LEFT_OF_CENTER
Bit allocation.
Definition: dcadec.c:268
lossless extension in ExSS
Definition: dcadec.c:107
#define AV_CH_FRONT_CENTER
uint8_t core_downmix_amode
audio channel arrangement of embedded downmix
Definition: dcadec.c:349
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:29
int bit_rate
transmission bit rate
Definition: dcadec.c:298
static const uint8_t *const bitalloc_bits[10][8]
Definition: dcahuff.h:1058
#define INIT_VLC_USE_NEW_STATIC
Definition: get_bits.h:441
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int offset
code values offset
Definition: dcadec.c:269
static const uint8_t abits_levels[7]
Definition: dcadec.c:1085
#define AV_CH_FRONT_RIGHT_OF_CENTER
static const float lfe_fir_64[]
Definition: dcadata.h:7310
int xch_present
XCh extension present and valid.
Definition: dcadec.c:384
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:160
int bits
Definition: get_bits.h:65
if(ac->has_optimized_func)
#define SCALES_VLC_BITS
Definition: dcahuff.h:82
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int table_allocated
Definition: get_bits.h:67
int core_ext_mask
present extensions in the core substream
Definition: dcadec.c:381
#define FF_PROFILE_DTS_96_24
Definition: avcodec.h:2639
int lfe_scale_factor
Definition: dcadec.c:355
NULL
Definition: eval.c:55
#define DCA_MAX_FRAME_SIZE
Definition: dcadec.c:260
Libavcodec external API header.
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
int aux_data
auxiliary data flag
Definition: dcadec.c:303
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int sample_rate
samples per second
Definition: avcodec.h:1791
int ext_coding
extended coding flag
Definition: dcadec.c:306
av_default_item_name
Definition: dnxhdenc.c:52
static float dca_dmix_code(unsigned code)
Definition: dcadec.c:1698
int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]
scale factors (2 if transient)
Definition: dcadec.c:339
int subband_activity[DCA_PRIM_CHANNELS_MAX]
subband activity count
Definition: dcadec.c:323
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
#define FASTDIV(a, b)
Definition: mathops.h:199
float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]
scale factor adjustment
Definition: dcadec.c:330
int copy_history
copy history
Definition: dcadec.c:313
static void dca_exss_parse_header(DCAContext *s)
Parse extension substream header (HD)
Definition: dcadec.c:1618
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
#define init_vlc(vlc, nb_bits, nb_codes,bits, bits_wrap, bits_size,codes, codes_wrap, codes_size,flags)
Definition: get_bits.h:424
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:296
VLC vlc[8]
actual codes
Definition: dcadec.c:272
core in ExSS (extension substream)
Definition: dcadec.c:102
Describe the class of an AVClass context structure.
Definition: log.h:33
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:108
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
int32_t high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
VQ encoded high frequency subbands.
Definition: dcadec.c:352
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
static const uint64_t dca_core_channel_layout[]
Definition: dcadec.c:134
static const float lossy_quant_d[32]
Definition: dcadata.h:4195
extended bitrate extension in ExSS
Definition: dcadec.c:103
DCAExSSSpeakerMask
Definition: dcadec.c:78
int sample_blocks
number of PCM sample blocks
Definition: dcadec.c:294
static const uint8_t abits_sizes[7]
Definition: dcadec.c:1084
static const int8_t dca_channel_reorder_nolfe_xch[][9]
Definition: dcadec.c:232
AVCodec ff_dca_decoder
Definition: dcadec.c:2087
GetBitContext gb
Definition: dcadec.c:376
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:304
int current_subframe
Definition: dcadec.c:378
static const AVOption options[]
Definition: dcadec.c:2075
static int decode_blockcode(int code, int levels, int32_t *values)
Definition: dcadec.c:1063
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
#define AV_CH_BACK_CENTER
av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
Initialize a float DSP context.
Definition: float_dsp.c:115
#define AV_CH_SIDE_RIGHT
uint8_t level
Definition: svq3.c:147
int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
prediction mode (ADPCM used or not)
Definition: dcadec.c:335
static av_cold int dca_decode_init(AVCodecContext *avctx)
DCA initialization.
Definition: dcadec.c:2029
static const float fir_32bands_perfect[]
Definition: dcadata.h:6276
int num_mix_configs
number of mix out configurations
Definition: dcadec.c:391
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_dlog(ac->avr,"%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
DCADSPContext dcadsp
Definition: dcadec.c:400
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:388
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
96/24 extension in core substream
Definition: dcadec.c:100
common internal api header.
common internal and external API header
#define AV_COPY128(d, s)
Definition: intreadwrite.h:514
uint8_t * extra_channels_buffer
Definition: dcadec.c:369
#define DCA_LFE_MAX
Definition: dcadec.c:61
#define CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:745
#define ff_mdct_end
Definition: fft.h:152
int total_channels
number of channels including extensions
Definition: dcadec.c:321
static const uint16_t scales_codes[SCALES_COUNT][129]
Definition: dcahuff.h:83
static int dca_filter_channels(DCAContext *s, int block_index)
Definition: dcadec.c:1244
int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]
prediction VQ coefs
Definition: dcadec.c:336
int dialog_norm
dialog normalisation parameter
Definition: dcadec.c:317
#define DCA_NSYNCAUX
Definition: dcadec.c:265
AVProfile.
Definition: avcodec.h:2784
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
#define DCA_SUBBANDS
Definition: dcadsp.h:25
int bit_rate_index
transmission bit rate index
Definition: dcadec.c:299
void * priv_data
Definition: avcodec.h:1092
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill channel data pointers and linesize for samples with sample format sample_fmt.
Definition: samplefmt.c:140
static const uint8_t bitalloc_maxbits[10][7]
Definition: dcahuff.h:1027
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
av_cold void ff_fmt_convert_init(FmtConvertContext *c, AVCodecContext *avctx)
Definition: fmtconvert.c:90
int len
int channels
number of audio channels
Definition: avcodec.h:1792
static const uint32_t dca_bit_rates[32]
Definition: dcadata.h:31
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:66
float * samples_chanptr[DCA_PRIM_CHANNELS_MAX+1]
Definition: dcadec.c:367
AVFloatDSPContext fdsp
Definition: dcadec.c:397
static const int8_t high_freq_vq[1024][32]
Definition: dcadata.h:4221
int subsubframes[DCA_SUBFRAMES_MAX]
number of subsubframes
Definition: dcadec.c:333
#define DCA_MAX_EXSS_HEADER_SIZE
Definition: dcadec.c:261
static const float dca_default_coeffs[10][6][2]
Definition: dcadata.h:7557
static const float lfe_fir_128[]
Definition: dcadata.h:7442
static const int dca_ext_audio_descr_mask[]
Definition: dcadec.c:111
96/24 extension in ExSS
Definition: dcadec.c:105
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:416
#define DOWNMIX_TO_STEREO(op1, op2)
Definition: dcadec.c:992
int frame_size
primary frame byte size
Definition: dcadec.c:295
#define DCA_SUBFRAMES_MAX
Definition: dcadec.c:59
int aspf
audio sync word insertion flag
Definition: dcadec.c:307
#define LOCAL_ALIGNED_16(t, v,...)
Definition: internal.h:114
#define av_always_inline
Definition: attributes.h:40
int prim_channels
number of primary audio channels
Definition: dcadec.c:322
#define AV_CH_SIDE_LEFT
float downmix_coef[DCA_PRIM_CHANNELS_MAX+1][2]
stereo downmix coefficients
Definition: dcadec.c:342
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:2641
static const uint8_t bitalloc_12_vlc_bits[BITALLOC_12_COUNT]
Definition: dcahuff.h:47
int output
type of output
Definition: dcadec.c:364
static int get_scale(GetBitContext *gb, int level, int value, int log2range)
Definition: dcadec.c:659
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
uint64_t request_channel_layout
Request decoder to use this channel layout if it can (0 for default)
Definition: avcodec.h:1859
int partial_samples[DCA_SUBFRAMES_MAX]
partial subsubframe samples count
Definition: dcadec.c:334
static const AVClass dca_decoder_class
Definition: dcadec.c:2080
This structure stores compressed data.
Definition: avcodec.h:950
int subframes
number of subframes
Definition: dcadec.c:320
static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
Definition: dcadec.c:474
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179
static const uint16_t dca_dmixtable[242]
Definition: dcadata.h:7523
unsigned int extra_channels_buffer_size
Definition: dcadec.c:370
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:280
int static_fields
static fields present
Definition: dcadec.c:389
#define AV_CH_BACK_RIGHT
int frame_type
type of the current frame
Definition: dcadec.c:291
static const uint8_t dca_bits_per_sample[7]
Definition: dcadata.h:46
Definition: vf_drawbox.c:37
int profile
Definition: dcadec.c:394
bitstream writer API
static int16_t block[64]
Definition: dct-test.c:88