FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dca_core.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 foo86
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "dcadec.h"
22 #include "dcadata.h"
23 #include "dcahuff.h"
24 #include "dcamath.h"
25 #include "dca_syncwords.h"
26 
27 #if ARCH_ARM
28 #include "arm/dca.h"
29 #endif
30 
31 enum HeaderType {
35 };
36 
37 enum AudioMode {
38  AMODE_MONO, // Mode 0: A (mono)
39  AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
40  AMODE_STEREO, // Mode 2: L + R (stereo)
41  AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
42  AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
43  AMODE_3F, // Mode 5: C + L + R
44  AMODE_2F1R, // Mode 6: L + R + S
45  AMODE_3F1R, // Mode 7: C + L + R + S
46  AMODE_2F2R, // Mode 8: L + R + SL + SR
47  AMODE_3F2R, // Mode 9: C + L + R + SL + SR
48 
50 };
51 
56 };
57 
58 enum LFEFlag {
63 };
64 
65 static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
66  { DCA_SPEAKER_C, -1, -1, -1, -1 },
67  { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
68  { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
69  { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
70  { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
76 };
77 
89 };
90 
91 static const uint8_t block_code_nbits[7] = {
92  7, 10, 12, 13, 15, 17, 19
93 };
94 
96  1, 2, 2, 2, 2, 3, 3, 3, 3, 3
97 };
98 
100  1, 3, 3, 3, 3, 7, 7, 7, 7, 7
101 };
102 
103 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
104 {
105  return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
106 }
107 
108 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
109 {
110  int i;
111 
112  for (i = 0; i < size; i++)
113  array[i] = get_sbits(s, n);
114 }
115 
116 // 5.3.1 - Bit stream header
118 {
119  int normal_frame, pcmr_index;
120 
121  // Frame type
122  normal_frame = get_bits1(&s->gb);
123 
124  // Deficit sample count
125  if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
126  av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
127  return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
128  }
129 
130  // CRC present flag
131  s->crc_present = get_bits1(&s->gb);
132 
133  // Number of PCM sample blocks
134  s->npcmblocks = get_bits(&s->gb, 7) + 1;
135  if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
136  av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
137  return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
138  }
139 
140  // Primary frame byte size
141  s->frame_size = get_bits(&s->gb, 14) + 1;
142  if (s->frame_size < 96) {
143  av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
144  return AVERROR_INVALIDDATA;
145  }
146 
147  // Audio channel arrangement
148  s->audio_mode = get_bits(&s->gb, 6);
149  if (s->audio_mode >= AMODE_COUNT) {
150  av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
151  return AVERROR_PATCHWELCOME;
152  }
153 
154  // Core audio sampling frequency
156  if (!s->sample_rate) {
157  av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
158  return AVERROR_INVALIDDATA;
159  }
160 
161  // Transmission bit rate
162  s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
163 
164  // Reserved field
165  skip_bits1(&s->gb);
166 
167  // Embedded dynamic range flag
168  s->drc_present = get_bits1(&s->gb);
169 
170  // Embedded time stamp flag
171  s->ts_present = get_bits1(&s->gb);
172 
173  // Auxiliary data flag
174  s->aux_present = get_bits1(&s->gb);
175 
176  // HDCD mastering flag
177  skip_bits1(&s->gb);
178 
179  // Extension audio descriptor flag
180  s->ext_audio_type = get_bits(&s->gb, 3);
181 
182  // Extended coding flag
183  s->ext_audio_present = get_bits1(&s->gb);
184 
185  // Audio sync word insertion flag
186  s->sync_ssf = get_bits1(&s->gb);
187 
188  // Low frequency effects flag
189  s->lfe_present = get_bits(&s->gb, 2);
190  if (s->lfe_present == LFE_FLAG_INVALID) {
191  av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
192  return AVERROR_INVALIDDATA;
193  }
194 
195  // Predictor history flag switch
196  s->predictor_history = get_bits1(&s->gb);
197 
198  // Header CRC check bytes
199  if (s->crc_present)
200  skip_bits(&s->gb, 16);
201 
202  // Multirate interpolator switch
203  s->filter_perfect = get_bits1(&s->gb);
204 
205  // Encoder software revision
206  skip_bits(&s->gb, 4);
207 
208  // Copy history
209  skip_bits(&s->gb, 2);
210 
211  // Source PCM resolution
212  s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
213  if (!s->source_pcm_res) {
214  av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
215  return AVERROR_INVALIDDATA;
216  }
217  s->es_format = pcmr_index & 1;
218 
219  // Front sum/difference flag
220  s->sumdiff_front = get_bits1(&s->gb);
221 
222  // Surround sum/difference flag
223  s->sumdiff_surround = get_bits1(&s->gb);
224 
225  // Dialog normalization / unspecified
226  skip_bits(&s->gb, 4);
227 
228  return 0;
229 }
230 
231 // 5.3.2 - Primary audio coding header
232 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
233 {
234  int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
235  unsigned int mask, index;
236 
237  if (get_bits_left(&s->gb) < 0)
238  return AVERROR_INVALIDDATA;
239 
240  switch (header) {
241  case HEADER_CORE:
242  // Number of subframes
243  s->nsubframes = get_bits(&s->gb, 4) + 1;
244 
245  // Number of primary audio channels
246  s->nchannels = get_bits(&s->gb, 3) + 1;
247  if (s->nchannels != ff_dca_channels[s->audio_mode]) {
248  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode);
249  return AVERROR_INVALIDDATA;
250  }
251  av_assert1(s->nchannels <= DCA_CHANNELS - 2);
252 
254 
255  // Add LFE channel if present
256  if (s->lfe_present)
258  break;
259 
260  case HEADER_XCH:
261  s->nchannels = ff_dca_channels[s->audio_mode] + 1;
262  av_assert1(s->nchannels <= DCA_CHANNELS - 1);
264  break;
265 
266  case HEADER_XXCH:
267  // Channel set header length
268  header_size = get_bits(&s->gb, 7) + 1;
269 
270  // Check CRC
271  if (s->xxch_crc_present
272  && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
273  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
274  return AVERROR_INVALIDDATA;
275  }
276 
277  // Number of channels in a channel set
278  nchannels = get_bits(&s->gb, 3) + 1;
279  if (nchannels > DCA_XXCH_CHANNELS_MAX) {
280  avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
281  return AVERROR_PATCHWELCOME;
282  }
283  s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
285 
286  // Loudspeaker layout mask
288  s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
289 
290  if (av_popcount(s->xxch_spkr_mask) != nchannels) {
291  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
292  return AVERROR_INVALIDDATA;
293  }
294 
295  if (s->xxch_core_mask & s->xxch_spkr_mask) {
296  av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask);
297  return AVERROR_INVALIDDATA;
298  }
299 
300  // Combine core and XXCH masks together
302 
303  // Downmix coefficients present in stream
304  if (get_bits1(&s->gb)) {
305  int *coeff_ptr = s->xxch_dmix_coeff;
306 
307  // Downmix already performed by encoder
308  s->xxch_dmix_embedded = get_bits1(&s->gb);
309 
310  // Downmix scale factor
311  index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
312  if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
313  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
314  return AVERROR_INVALIDDATA;
315  }
317 
318  // Downmix channel mapping mask
319  for (ch = 0; ch < nchannels; ch++) {
320  mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
321  if ((mask & s->xxch_core_mask) != mask) {
322  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
323  return AVERROR_INVALIDDATA;
324  }
325  s->xxch_dmix_mask[ch] = mask;
326  }
327 
328  // Downmix coefficients
329  for (ch = 0; ch < nchannels; ch++) {
330  for (n = 0; n < s->xxch_mask_nbits; n++) {
331  if (s->xxch_dmix_mask[ch] & (1U << n)) {
332  int code = get_bits(&s->gb, 7);
333  int sign = (code >> 6) - 1;
334  if (code &= 63) {
335  index = code * 4 - 3;
336  if (index >= FF_DCA_DMIXTABLE_SIZE) {
337  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
338  return AVERROR_INVALIDDATA;
339  }
340  *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
341  } else {
342  *coeff_ptr++ = 0;
343  }
344  }
345  }
346  }
347  } else {
348  s->xxch_dmix_embedded = 0;
349  }
350 
351  break;
352  }
353 
354  // Subband activity count
355  for (ch = xch_base; ch < s->nchannels; ch++) {
356  s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
357  if (s->nsubbands[ch] > DCA_SUBBANDS) {
358  av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
359  return AVERROR_INVALIDDATA;
360  }
361  }
362 
363  // High frequency VQ start subband
364  for (ch = xch_base; ch < s->nchannels; ch++)
365  s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
366 
367  // Joint intensity coding index
368  for (ch = xch_base; ch < s->nchannels; ch++) {
369  if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
370  n += xch_base - 1;
371  if (n > s->nchannels) {
372  av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
373  return AVERROR_INVALIDDATA;
374  }
375  s->joint_intensity_index[ch] = n;
376  }
377 
378  // Transient mode code book
379  for (ch = xch_base; ch < s->nchannels; ch++)
380  s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
381 
382  // Scale factor code book
383  for (ch = xch_base; ch < s->nchannels; ch++) {
384  s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
385  if (s->scale_factor_sel[ch] == 7) {
386  av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
387  return AVERROR_INVALIDDATA;
388  }
389  }
390 
391  // Bit allocation quantizer select
392  for (ch = xch_base; ch < s->nchannels; ch++) {
393  s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
394  if (s->bit_allocation_sel[ch] == 7) {
395  av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
396  return AVERROR_INVALIDDATA;
397  }
398  }
399 
400  // Quantization index codebook select
401  for (n = 0; n < DCA_CODE_BOOKS; n++)
402  for (ch = xch_base; ch < s->nchannels; ch++)
403  s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
404 
405  // Scale factor adjustment index
406  for (n = 0; n < DCA_CODE_BOOKS; n++)
407  for (ch = xch_base; ch < s->nchannels; ch++)
408  if (s->quant_index_sel[ch][n] < quant_index_group_size[n])
410 
411  if (header == HEADER_XXCH) {
412  // Reserved
413  // Byte align
414  // CRC16 of channel set header
415  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
416  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
417  return AVERROR_INVALIDDATA;
418  }
419  } else {
420  // Audio header CRC check word
421  if (s->crc_present)
422  skip_bits(&s->gb, 16);
423  }
424 
425  return 0;
426 }
427 
428 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
429 {
430  const uint32_t *scale_table;
431  unsigned int scale_size;
432 
433  // Select the root square table
434  if (sel > 5) {
435  scale_table = ff_dca_scale_factor_quant7;
437  } else {
438  scale_table = ff_dca_scale_factor_quant6;
440  }
441 
442  // If Huffman code was used, the difference of scales was encoded
443  if (sel < 5)
444  *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
445  else
446  *scale_index = get_bits(&s->gb, sel + 1);
447 
448  // Look up scale factor from the root square table
449  if ((unsigned int)*scale_index >= scale_size) {
450  av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
451  return AVERROR_INVALIDDATA;
452  }
453 
454  return scale_table[*scale_index];
455 }
456 
457 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
458 {
459  int scale_index;
460 
461  // Absolute value was encoded even when Huffman code was used
462  if (sel < 5)
463  scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
464  else
465  scale_index = get_bits(&s->gb, sel + 1);
466 
467  // Bias by 64
468  scale_index += 64;
469 
470  // Look up joint scale factor
471  if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
472  av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
473  return AVERROR_INVALIDDATA;
474  }
475 
476  return ff_dca_joint_scale_factors[scale_index];
477 }
478 
479 // 5.4.1 - Primary audio coding side information
481  enum HeaderType header, int xch_base)
482 {
483  int ch, band, ret;
484 
485  if (get_bits_left(&s->gb) < 0)
486  return AVERROR_INVALIDDATA;
487 
488  if (header == HEADER_CORE) {
489  // Subsubframe count
490  s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
491 
492  // Partial subsubframe sample count
493  skip_bits(&s->gb, 3);
494  }
495 
496  // Prediction mode
497  for (ch = xch_base; ch < s->nchannels; ch++)
498  for (band = 0; band < s->nsubbands[ch]; band++)
499  s->prediction_mode[ch][band] = get_bits1(&s->gb);
500 
501  // Prediction coefficients VQ address
502  for (ch = xch_base; ch < s->nchannels; ch++)
503  for (band = 0; band < s->nsubbands[ch]; band++)
504  if (s->prediction_mode[ch][band])
505  s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
506 
507  // Bit allocation index
508  for (ch = xch_base; ch < s->nchannels; ch++) {
509  int sel = s->bit_allocation_sel[ch];
510 
511  for (band = 0; band < s->subband_vq_start[ch]; band++) {
512  int abits;
513 
514  if (sel < 5)
515  abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
516  else
517  abits = get_bits(&s->gb, sel - 1);
518 
519  if (abits > DCA_ABITS_MAX) {
520  av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
521  return AVERROR_INVALIDDATA;
522  }
523 
524  s->bit_allocation[ch][band] = abits;
525  }
526  }
527 
528  // Transition mode
529  for (ch = xch_base; ch < s->nchannels; ch++) {
530  // Clear transition mode for all subbands
531  memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
532 
533  // Transient possible only if more than one subsubframe
534  if (s->nsubsubframes[sf] > 1) {
535  int sel = s->transition_mode_sel[ch];
536  for (band = 0; band < s->subband_vq_start[ch]; band++)
537  if (s->bit_allocation[ch][band])
539  }
540  }
541 
542  // Scale factors
543  for (ch = xch_base; ch < s->nchannels; ch++) {
544  int sel = s->scale_factor_sel[ch];
545  int scale_index = 0;
546 
547  // Extract scales for subbands up to VQ
548  for (band = 0; band < s->subband_vq_start[ch]; band++) {
549  if (s->bit_allocation[ch][band]) {
550  if ((ret = parse_scale(s, &scale_index, sel)) < 0)
551  return ret;
552  s->scale_factors[ch][band][0] = ret;
553  if (s->transition_mode[sf][ch][band]) {
554  if ((ret = parse_scale(s, &scale_index, sel)) < 0)
555  return ret;
556  s->scale_factors[ch][band][1] = ret;
557  }
558  } else {
559  s->scale_factors[ch][band][0] = 0;
560  }
561  }
562 
563  // High frequency VQ subbands
564  for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
565  if ((ret = parse_scale(s, &scale_index, sel)) < 0)
566  return ret;
567  s->scale_factors[ch][band][0] = ret;
568  }
569  }
570 
571  // Joint subband codebook select
572  for (ch = xch_base; ch < s->nchannels; ch++) {
573  if (s->joint_intensity_index[ch]) {
574  s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
575  if (s->joint_scale_sel[ch] == 7) {
576  av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
577  return AVERROR_INVALIDDATA;
578  }
579  }
580  }
581 
582  // Scale factors for joint subband coding
583  for (ch = xch_base; ch < s->nchannels; ch++) {
584  int src_ch = s->joint_intensity_index[ch] - 1;
585  if (src_ch >= 0) {
586  int sel = s->joint_scale_sel[ch];
587  for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
588  if ((ret = parse_joint_scale(s, sel)) < 0)
589  return ret;
590  s->joint_scale_factors[ch][band] = ret;
591  }
592  }
593  }
594 
595  // Dynamic range coefficient
596  if (s->drc_present && header == HEADER_CORE)
597  skip_bits(&s->gb, 8);
598 
599  // Side information CRC check word
600  if (s->crc_present)
601  skip_bits(&s->gb, 16);
602 
603  return 0;
604 }
605 
606 #ifndef decode_blockcodes
607 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
608 {
609  int offset = (levels - 1) / 2;
610  int n, div;
611 
612  for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
613  div = FASTDIV(code1, levels);
614  audio[n] = code1 - div * levels - offset;
615  code1 = div;
616  }
617  for (; n < DCA_SUBBAND_SAMPLES; n++) {
618  div = FASTDIV(code2, levels);
619  audio[n] = code2 - div * levels - offset;
620  code2 = div;
621  }
622 
623  return code1 | code2;
624 }
625 #endif
626 
627 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
628 {
629  // Extract block code indices from the bit stream
630  int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
631  int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
632  int levels = ff_dca_quant_levels[abits];
633 
634  // Look up samples from the block code book
635  if (decode_blockcodes(code1, code2, levels, audio)) {
636  av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
637  return AVERROR_INVALIDDATA;
638  }
639 
640  return 0;
641 }
642 
643 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
644 {
645  int i;
646 
647  // Extract Huffman codes from the bit stream
648  for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
649  audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
650 
651  return 1;
652 }
653 
654 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
655 {
656  av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
657 
658  if (abits == 0) {
659  // No bits allocated
660  memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
661  return 0;
662  }
663 
664  if (abits <= DCA_CODE_BOOKS) {
665  int sel = s->quant_index_sel[ch][abits - 1];
666  if (sel < quant_index_group_size[abits - 1]) {
667  // Huffman codes
668  return parse_huffman_codes(s, audio, abits, sel);
669  }
670  if (abits <= 7) {
671  // Block codes
672  return parse_block_codes(s, audio, abits);
673  }
674  }
675 
676  // No further encoding
677  get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
678  return 0;
679 }
680 
681 static inline void dequantize(int32_t *output, const int32_t *input,
682  int32_t step_size, int32_t scale, int residual)
683 {
684  // Account for quantizer step size
685  int64_t step_scale = (int64_t)step_size * scale;
686  int n, shift = 0;
687 
688  // Limit scale factor resolution to 22 bits
689  if (step_scale > (1 << 23)) {
690  shift = av_log2(step_scale >> 23) + 1;
691  step_scale >>= shift;
692  }
693 
694  // Scale the samples
695  if (residual) {
696  for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
697  output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
698  } else {
699  for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
700  output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
701  }
702 }
703 
704 static inline void inverse_adpcm(int32_t **subband_samples,
705  const int16_t *vq_index,
706  const int8_t *prediction_mode,
707  int sb_start, int sb_end,
708  int ofs, int len)
709 {
710  int i, j, k;
711 
712  for (i = sb_start; i < sb_end; i++) {
713  if (prediction_mode[i]) {
714  const int16_t *coeff = ff_dca_adpcm_vb[vq_index[i]];
715  int32_t *ptr = subband_samples[i] + ofs;
716  for (j = 0; j < len; j++) {
717  int64_t err = 0;
718  for (k = 0; k < DCA_ADPCM_COEFFS; k++)
719  err += (int64_t)ptr[j - k - 1] * coeff[k];
720  ptr[j] = clip23(ptr[j] + clip23(norm13(err)));
721  }
722  }
723  }
724 }
725 
726 // 5.5 - Primary audio data arrays
728  int xch_base, int *sub_pos, int *lfe_pos)
729 {
730  int32_t audio[16], scale;
731  int n, ssf, ofs, ch, band;
732 
733  // Check number of subband samples in this subframe
734  int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
735  if (*sub_pos + nsamples > s->npcmblocks) {
736  av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
737  return AVERROR_INVALIDDATA;
738  }
739 
740  if (get_bits_left(&s->gb) < 0)
741  return AVERROR_INVALIDDATA;
742 
743  // VQ encoded subbands
744  for (ch = xch_base; ch < s->nchannels; ch++) {
745  int32_t vq_index[DCA_SUBBANDS];
746 
747  for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
748  // Extract the VQ address from the bit stream
749  vq_index[band] = get_bits(&s->gb, 10);
750 
751  if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
752  s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
754  s->subband_vq_start[ch], s->nsubbands[ch],
755  *sub_pos, nsamples);
756  }
757  }
758 
759  // Low frequency effect data
760  if (s->lfe_present && header == HEADER_CORE) {
761  unsigned int index;
762 
763  // Determine number of LFE samples in this subframe
764  int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
765  av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
766 
767  // Extract LFE samples from the bit stream
768  get_array(&s->gb, audio, nlfesamples, 8);
769 
770  // Extract scale factor index from the bit stream
771  index = get_bits(&s->gb, 8);
773  av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
774  return AVERROR_INVALIDDATA;
775  }
776 
777  // Look up the 7-bit root square quantization table
779 
780  // Account for quantizer step size which is 0.035
781  scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
782 
783  // Scale and take the LFE samples
784  for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
785  s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
786 
787  // Advance LFE sample pointer for the next subframe
788  *lfe_pos = ofs;
789  }
790 
791  // Audio data
792  for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
793  for (ch = xch_base; ch < s->nchannels; ch++) {
794  if (get_bits_left(&s->gb) < 0)
795  return AVERROR_INVALIDDATA;
796 
797  // Not high frequency VQ subbands
798  for (band = 0; band < s->subband_vq_start[ch]; band++) {
799  int ret, trans_ssf, abits = s->bit_allocation[ch][band];
800  int32_t step_size;
801 
802  // Extract bits from the bit stream
803  if ((ret = extract_audio(s, audio, abits, ch)) < 0)
804  return ret;
805 
806  // Select quantization step size table and look up
807  // quantization step size
808  if (s->bit_rate == 3)
809  step_size = ff_dca_lossless_quant[abits];
810  else
811  step_size = ff_dca_lossy_quant[abits];
812 
813  // Identify transient location
814  trans_ssf = s->transition_mode[sf][ch][band];
815 
816  // Determine proper scale factor
817  if (trans_ssf == 0 || ssf < trans_ssf)
818  scale = s->scale_factors[ch][band][0];
819  else
820  scale = s->scale_factors[ch][band][1];
821 
822  // Adjust scale factor when SEL indicates Huffman code
823  if (ret > 0) {
824  int64_t adj = s->scale_factor_adj[ch][abits - 1];
825  scale = clip23(adj * scale >> 22);
826  }
827 
828  dequantize(s->subband_samples[ch][band] + ofs,
829  audio, step_size, scale, 0);
830  }
831  }
832 
833  // DSYNC
834  if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
835  av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
836  return AVERROR_INVALIDDATA;
837  }
838 
839  ofs += DCA_SUBBAND_SAMPLES;
840  }
841 
842  // Inverse ADPCM
843  for (ch = xch_base; ch < s->nchannels; ch++) {
845  s->prediction_mode[ch], 0, s->nsubbands[ch],
846  *sub_pos, nsamples);
847  }
848 
849  // Joint subband coding
850  for (ch = xch_base; ch < s->nchannels; ch++) {
851  int src_ch = s->joint_intensity_index[ch] - 1;
852  if (src_ch >= 0) {
853  s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
854  s->joint_scale_factors[ch], s->nsubbands[ch],
855  s->nsubbands[src_ch], *sub_pos, nsamples);
856  }
857  }
858 
859  // Advance subband sample pointer for the next subframe
860  *sub_pos = ofs;
861  return 0;
862 }
863 
865 {
866  int ch, band;
867 
868  // Erase ADPCM history from previous frame if
869  // predictor history switch was disabled
870  for (ch = 0; ch < DCA_CHANNELS; ch++)
871  for (band = 0; band < DCA_SUBBANDS; band++)
873 
874  emms_c();
875 }
876 
878 {
879  int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
880  int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
881  int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
882  unsigned int size = s->subband_size;
883  int ch, band;
884 
885  // Reallocate subband sample buffer
887  (nframesamples + nlfesamples) * sizeof(int32_t));
888  if (!s->subband_buffer)
889  return AVERROR(ENOMEM);
890 
891  if (size != s->subband_size) {
892  for (ch = 0; ch < DCA_CHANNELS; ch++)
893  for (band = 0; band < DCA_SUBBANDS; band++)
894  s->subband_samples[ch][band] = s->subband_buffer +
895  (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
896  s->lfe_samples = s->subband_buffer + nframesamples;
897  }
898 
899  if (!s->predictor_history)
901 
902  return 0;
903 }
904 
905 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
906 {
907  int sf, ch, ret, band, sub_pos, lfe_pos;
908 
909  if ((ret = parse_coding_header(s, header, xch_base)) < 0)
910  return ret;
911 
912  for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
913  if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
914  return ret;
915  if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
916  return ret;
917  }
918 
919  for (ch = xch_base; ch < s->nchannels; ch++) {
920  // Determine number of active subbands for this channel
921  int nsubbands = s->nsubbands[ch];
922  if (s->joint_intensity_index[ch])
923  nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
924 
925  // Update history for ADPCM
926  for (band = 0; band < nsubbands; band++) {
927  int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
928  AV_COPY128(samples, samples + s->npcmblocks);
929  }
930 
931  // Clear inactive subbands
932  for (; band < DCA_SUBBANDS; band++) {
933  int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
934  memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
935  }
936  }
937 
938  emms_c();
939 
940  return 0;
941 }
942 
944 {
945  int ret;
946 
947  if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
948  av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
949  return AVERROR_INVALIDDATA;
950  }
951 
952  if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
953  return ret;
954 
955  // Seek to the end of core frame, don't trust XCH frame size
956  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
957  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
958  return AVERROR_INVALIDDATA;
959  }
960 
961  return 0;
962 }
963 
965 {
966  int xxch_nchsets, xxch_frame_size;
967  int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
968 
969  // XXCH sync word
970  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
971  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
972  return AVERROR_INVALIDDATA;
973  }
974 
975  // XXCH frame header length
976  header_size = get_bits(&s->gb, 6) + 1;
977 
978  // Check XXCH frame header CRC
979  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
980  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
981  return AVERROR_INVALIDDATA;
982  }
983 
984  // CRC presence flag for channel set header
985  s->xxch_crc_present = get_bits1(&s->gb);
986 
987  // Number of bits for loudspeaker mask
988  s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
989  if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
990  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
991  return AVERROR_INVALIDDATA;
992  }
993 
994  // Number of channel sets
995  xxch_nchsets = get_bits(&s->gb, 2) + 1;
996  if (xxch_nchsets > 1) {
997  avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
998  return AVERROR_PATCHWELCOME;
999  }
1000 
1001  // Channel set 0 data byte size
1002  xxch_frame_size = get_bits(&s->gb, 14) + 1;
1003 
1004  // Core loudspeaker activity mask
1006 
1007  // Validate the core mask
1008  mask = s->ch_mask;
1009 
1011  mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
1012 
1014  mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
1015 
1016  if (mask != s->xxch_core_mask) {
1017  av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
1018  return AVERROR_INVALIDDATA;
1019  }
1020 
1021  // Reserved
1022  // Byte align
1023  // CRC16 of XXCH frame header
1024  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1025  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
1026  return AVERROR_INVALIDDATA;
1027  }
1028 
1029  // Parse XXCH channel set 0
1030  if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1031  return ret;
1032 
1033  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1034  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1035  return AVERROR_INVALIDDATA;
1036  }
1037 
1038  return 0;
1039 }
1040 
1041 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1042  int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1043 {
1044  int xbr_nabits[DCA_CHANNELS];
1045  int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1046  int xbr_scale_nbits[DCA_CHANNELS];
1047  int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1048  int ssf, ch, band, ofs;
1049 
1050  // Check number of subband samples in this subframe
1051  if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1052  av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1053  return AVERROR_INVALIDDATA;
1054  }
1055 
1056  if (get_bits_left(&s->gb) < 0)
1057  return AVERROR_INVALIDDATA;
1058 
1059  // Number of bits for XBR bit allocation index
1060  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1061  xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1062 
1063  // XBR bit allocation index
1064  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1065  for (band = 0; band < xbr_nsubbands[ch]; band++) {
1066  xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1067  if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1068  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1069  return AVERROR_INVALIDDATA;
1070  }
1071  }
1072  }
1073 
1074  // Number of bits for scale indices
1075  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1076  xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1077  if (!xbr_scale_nbits[ch]) {
1078  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1079  return AVERROR_INVALIDDATA;
1080  }
1081  }
1082 
1083  // XBR scale factors
1084  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1085  const uint32_t *scale_table;
1086  int scale_size;
1087 
1088  // Select the root square table
1089  if (s->scale_factor_sel[ch] > 5) {
1090  scale_table = ff_dca_scale_factor_quant7;
1092  } else {
1093  scale_table = ff_dca_scale_factor_quant6;
1095  }
1096 
1097  // Parse scale factor indices and look up scale factors from the root
1098  // square table
1099  for (band = 0; band < xbr_nsubbands[ch]; band++) {
1100  if (xbr_bit_allocation[ch][band]) {
1101  int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1102  if (scale_index >= scale_size) {
1103  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1104  return AVERROR_INVALIDDATA;
1105  }
1106  xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1107  if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1108  scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1109  if (scale_index >= scale_size) {
1110  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1111  return AVERROR_INVALIDDATA;
1112  }
1113  xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1114  }
1115  }
1116  }
1117  }
1118 
1119  // Audio data
1120  for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1121  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1122  if (get_bits_left(&s->gb) < 0)
1123  return AVERROR_INVALIDDATA;
1124 
1125  for (band = 0; band < xbr_nsubbands[ch]; band++) {
1126  int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1127  int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1128 
1129  // Extract bits from the bit stream
1130  if (abits > 7) {
1131  // No further encoding
1132  get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1133  } else if (abits > 0) {
1134  // Block codes
1135  if ((ret = parse_block_codes(s, audio, abits)) < 0)
1136  return ret;
1137  } else {
1138  // No bits allocated
1139  continue;
1140  }
1141 
1142  // Look up quantization step size
1143  step_size = ff_dca_lossless_quant[abits];
1144 
1145  // Identify transient location
1146  if (xbr_transition_mode)
1147  trans_ssf = s->transition_mode[sf][ch][band];
1148  else
1149  trans_ssf = 0;
1150 
1151  // Determine proper scale factor
1152  if (trans_ssf == 0 || ssf < trans_ssf)
1153  scale = xbr_scale_factors[ch][band][0];
1154  else
1155  scale = xbr_scale_factors[ch][band][1];
1156 
1157  dequantize(s->subband_samples[ch][band] + ofs,
1158  audio, step_size, scale, 1);
1159  }
1160  }
1161 
1162  // DSYNC
1163  if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1164  av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1165  return AVERROR_INVALIDDATA;
1166  }
1167 
1168  ofs += DCA_SUBBAND_SAMPLES;
1169  }
1170 
1171  // Advance subband sample pointer for the next subframe
1172  *sub_pos = ofs;
1173  return 0;
1174 }
1175 
1177 {
1178  int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1179  int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1180  int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1181  int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1182  int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1183 
1184  // XBR sync word
1185  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1186  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1187  return AVERROR_INVALIDDATA;
1188  }
1189 
1190  // XBR frame header length
1191  header_size = get_bits(&s->gb, 6) + 1;
1192 
1193  // Check XBR frame header CRC
1194  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1195  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1196  return AVERROR_INVALIDDATA;
1197  }
1198 
1199  // Number of channel sets
1200  xbr_nchsets = get_bits(&s->gb, 2) + 1;
1201 
1202  // Channel set data byte size
1203  for (i = 0; i < xbr_nchsets; i++)
1204  xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1205 
1206  // Transition mode flag
1207  xbr_transition_mode = get_bits1(&s->gb);
1208 
1209  // Channel set headers
1210  for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1211  xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1212  xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1213  for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1214  xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1215  if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1216  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1217  return AVERROR_INVALIDDATA;
1218  }
1219  }
1220  }
1221 
1222  // Reserved
1223  // Byte align
1224  // CRC16 of XBR frame header
1225  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1226  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1227  return AVERROR_INVALIDDATA;
1228  }
1229 
1230  // Channel set data
1231  for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1232  header_pos = get_bits_count(&s->gb);
1233 
1234  if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1235  int sf, sub_pos;
1236 
1237  for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1238  if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1239  xbr_base_ch + xbr_nchannels[i],
1240  xbr_nsubbands, xbr_transition_mode,
1241  sf, &sub_pos)) < 0)
1242  return ret;
1243  }
1244  }
1245 
1246  xbr_base_ch += xbr_nchannels[i];
1247 
1248  if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1249  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1250  return AVERROR_INVALIDDATA;
1251  }
1252  }
1253 
1254  return 0;
1255 }
1256 
1257 // Modified ISO/IEC 9899 linear congruential generator
1258 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1260 {
1261  s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1262  return (s->x96_rand & 0x7fffffff) - 0x40000000;
1263 }
1264 
1265 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1266 {
1267  int n, ssf, ch, band, ofs;
1268 
1269  // Check number of subband samples in this subframe
1270  int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1271  if (*sub_pos + nsamples > s->npcmblocks) {
1272  av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1273  return AVERROR_INVALIDDATA;
1274  }
1275 
1276  if (get_bits_left(&s->gb) < 0)
1277  return AVERROR_INVALIDDATA;
1278 
1279  // VQ encoded or unallocated subbands
1280  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1281  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1282  // Get the sample pointer and scale factor
1283  int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1284  int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1285 
1286  switch (s->bit_allocation[ch][band]) {
1287  case 0: // No bits allocated for subband
1288  if (scale <= 1)
1289  memset(samples, 0, nsamples * sizeof(int32_t));
1290  else for (n = 0; n < nsamples; n++)
1291  // Generate scaled random samples
1292  samples[n] = mul31(rand_x96(s), scale);
1293  break;
1294 
1295  case 1: // VQ encoded subband
1296  for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1297  // Extract the VQ address from the bit stream and look up
1298  // the VQ code book for up to 16 subband samples
1299  const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1300  // Scale and take the samples
1301  for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1302  *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1303  }
1304  break;
1305  }
1306  }
1307  }
1308 
1309  // Audio data
1310  for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1311  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1312  if (get_bits_left(&s->gb) < 0)
1313  return AVERROR_INVALIDDATA;
1314 
1315  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1316  int ret, abits = s->bit_allocation[ch][band] - 1;
1317  int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1318 
1319  // Not VQ encoded or unallocated subbands
1320  if (abits < 1)
1321  continue;
1322 
1323  // Extract bits from the bit stream
1324  if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1325  return ret;
1326 
1327  // Select quantization step size table and look up quantization
1328  // step size
1329  if (s->bit_rate == 3)
1330  step_size = ff_dca_lossless_quant[abits];
1331  else
1332  step_size = ff_dca_lossy_quant[abits];
1333 
1334  // Get the scale factor
1335  scale = s->scale_factors[ch][band >> 1][band & 1];
1336 
1337  dequantize(s->x96_subband_samples[ch][band] + ofs,
1338  audio, step_size, scale, 0);
1339  }
1340  }
1341 
1342  // DSYNC
1343  if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1344  av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1345  return AVERROR_INVALIDDATA;
1346  }
1347 
1348  ofs += DCA_SUBBAND_SAMPLES;
1349  }
1350 
1351  // Inverse ADPCM
1352  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1354  s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1355  *sub_pos, nsamples);
1356  }
1357 
1358  // Joint subband coding
1359  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1360  int src_ch = s->joint_intensity_index[ch] - 1;
1361  if (src_ch >= 0) {
1363  s->joint_scale_factors[ch], s->nsubbands[ch],
1364  s->nsubbands[src_ch], *sub_pos, nsamples);
1365  }
1366  }
1367 
1368  // Advance subband sample pointer for the next subframe
1369  *sub_pos = ofs;
1370  return 0;
1371 }
1372 
1374 {
1375  int ch, band;
1376 
1377  // Erase ADPCM history from previous frame if
1378  // predictor history switch was disabled
1379  for (ch = 0; ch < DCA_CHANNELS; ch++)
1380  for (band = 0; band < DCA_SUBBANDS_X96; band++)
1382 
1383  emms_c();
1384 }
1385 
1387 {
1388  int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1389  int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1390  unsigned int size = s->x96_subband_size;
1391  int ch, band;
1392 
1393  // Reallocate subband sample buffer
1395  nframesamples * sizeof(int32_t));
1396  if (!s->x96_subband_buffer)
1397  return AVERROR(ENOMEM);
1398 
1399  if (size != s->x96_subband_size) {
1400  for (ch = 0; ch < DCA_CHANNELS; ch++)
1401  for (band = 0; band < DCA_SUBBANDS_X96; band++)
1402  s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1403  (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1404  }
1405 
1406  if (!s->predictor_history)
1408 
1409  return 0;
1410 }
1411 
1412 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1413 {
1414  int ch, band, ret;
1415 
1416  if (get_bits_left(&s->gb) < 0)
1417  return AVERROR_INVALIDDATA;
1418 
1419  // Prediction mode
1420  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1421  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1422  s->prediction_mode[ch][band] = get_bits1(&s->gb);
1423 
1424  // Prediction coefficients VQ address
1425  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1426  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1427  if (s->prediction_mode[ch][band])
1428  s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1429 
1430  // Bit allocation index
1431  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1432  int sel = s->bit_allocation_sel[ch];
1433  int abits = 0;
1434 
1435  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1436  // If Huffman code was used, the difference of abits was encoded
1437  if (sel < 7)
1438  abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1439  else
1440  abits = get_bits(&s->gb, 3 + s->x96_high_res);
1441 
1442  if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1443  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1444  return AVERROR_INVALIDDATA;
1445  }
1446 
1447  s->bit_allocation[ch][band] = abits;
1448  }
1449  }
1450 
1451  // Scale factors
1452  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1453  int sel = s->scale_factor_sel[ch];
1454  int scale_index = 0;
1455 
1456  // Extract scales for subbands which are transmitted even for
1457  // unallocated subbands
1458  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1459  if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1460  return ret;
1461  s->scale_factors[ch][band >> 1][band & 1] = ret;
1462  }
1463  }
1464 
1465  // Joint subband codebook select
1466  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1467  if (s->joint_intensity_index[ch]) {
1468  s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1469  if (s->joint_scale_sel[ch] == 7) {
1470  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1471  return AVERROR_INVALIDDATA;
1472  }
1473  }
1474  }
1475 
1476  // Scale factors for joint subband coding
1477  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1478  int src_ch = s->joint_intensity_index[ch] - 1;
1479  if (src_ch >= 0) {
1480  int sel = s->joint_scale_sel[ch];
1481  for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1482  if ((ret = parse_joint_scale(s, sel)) < 0)
1483  return ret;
1484  s->joint_scale_factors[ch][band] = ret;
1485  }
1486  }
1487  }
1488 
1489  // Side information CRC check word
1490  if (s->crc_present)
1491  skip_bits(&s->gb, 16);
1492 
1493  return 0;
1494 }
1495 
1496 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1497 {
1498  int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1499 
1500  if (get_bits_left(&s->gb) < 0)
1501  return AVERROR_INVALIDDATA;
1502 
1503  if (exss) {
1504  // Channel set header length
1505  header_size = get_bits(&s->gb, 7) + 1;
1506 
1507  // Check CRC
1508  if (s->x96_crc_present
1509  && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1510  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1511  return AVERROR_INVALIDDATA;
1512  }
1513  }
1514 
1515  // High resolution flag
1516  s->x96_high_res = get_bits1(&s->gb);
1517 
1518  // First encoded subband
1519  if (s->x96_rev_no < 8) {
1520  s->x96_subband_start = get_bits(&s->gb, 5);
1521  if (s->x96_subband_start > 27) {
1522  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1523  return AVERROR_INVALIDDATA;
1524  }
1525  } else {
1527  }
1528 
1529  // Subband activity count
1530  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1531  s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1532  if (s->nsubbands[ch] < DCA_SUBBANDS) {
1533  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1534  return AVERROR_INVALIDDATA;
1535  }
1536  }
1537 
1538  // Joint intensity coding index
1539  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1540  if ((n = get_bits(&s->gb, 3)) && xch_base)
1541  n += xch_base - 1;
1542  if (n > s->x96_nchannels) {
1543  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1544  return AVERROR_INVALIDDATA;
1545  }
1546  s->joint_intensity_index[ch] = n;
1547  }
1548 
1549  // Scale factor code book
1550  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1551  s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1552  if (s->scale_factor_sel[ch] >= 6) {
1553  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1554  return AVERROR_INVALIDDATA;
1555  }
1556  }
1557 
1558  // Bit allocation quantizer select
1559  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1560  s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1561 
1562  // Quantization index codebook select
1563  for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1564  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1565  s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
1566 
1567  if (exss) {
1568  // Reserved
1569  // Byte align
1570  // CRC16 of channel set header
1571  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1572  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1573  return AVERROR_INVALIDDATA;
1574  }
1575  } else {
1576  if (s->crc_present)
1577  skip_bits(&s->gb, 16);
1578  }
1579 
1580  return 0;
1581 }
1582 
1583 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1584 {
1585  int sf, ch, ret, band, sub_pos;
1586 
1587  if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1588  return ret;
1589 
1590  for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1591  if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1592  return ret;
1593  if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1594  return ret;
1595  }
1596 
1597  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1598  // Determine number of active subbands for this channel
1599  int nsubbands = s->nsubbands[ch];
1600  if (s->joint_intensity_index[ch])
1601  nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1602 
1603  // Update history for ADPCM and clear inactive subbands
1604  for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1605  int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1606  if (band >= s->x96_subband_start && band < nsubbands)
1607  AV_COPY128(samples, samples + s->npcmblocks);
1608  else
1609  memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1610  }
1611  }
1612 
1613  emms_c();
1614 
1615  return 0;
1616 }
1617 
1619 {
1620  int ret;
1621 
1622  // Revision number
1623  s->x96_rev_no = get_bits(&s->gb, 4);
1624  if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1625  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1626  return AVERROR_INVALIDDATA;
1627  }
1628 
1629  s->x96_crc_present = 0;
1630  s->x96_nchannels = s->nchannels;
1631 
1632  if ((ret = alloc_x96_sample_buffer(s)) < 0)
1633  return ret;
1634 
1635  if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1636  return ret;
1637 
1638  // Seek to the end of core frame
1639  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1640  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1641  return AVERROR_INVALIDDATA;
1642  }
1643 
1644  return 0;
1645 }
1646 
1648 {
1649  int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1650  int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1651  int x96_nchsets, x96_base_ch;
1652  int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1653 
1654  // X96 sync word
1655  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1656  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1657  return AVERROR_INVALIDDATA;
1658  }
1659 
1660  // X96 frame header length
1661  header_size = get_bits(&s->gb, 6) + 1;
1662 
1663  // Check X96 frame header CRC
1664  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1665  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1666  return AVERROR_INVALIDDATA;
1667  }
1668 
1669  // Revision number
1670  s->x96_rev_no = get_bits(&s->gb, 4);
1671  if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1672  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1673  return AVERROR_INVALIDDATA;
1674  }
1675 
1676  // CRC presence flag for channel set header
1677  s->x96_crc_present = get_bits1(&s->gb);
1678 
1679  // Number of channel sets
1680  x96_nchsets = get_bits(&s->gb, 2) + 1;
1681 
1682  // Channel set data byte size
1683  for (i = 0; i < x96_nchsets; i++)
1684  x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1685 
1686  // Number of channels in channel set
1687  for (i = 0; i < x96_nchsets; i++)
1688  x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1689 
1690  // Reserved
1691  // Byte align
1692  // CRC16 of X96 frame header
1693  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1694  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1695  return AVERROR_INVALIDDATA;
1696  }
1697 
1698  if ((ret = alloc_x96_sample_buffer(s)) < 0)
1699  return ret;
1700 
1701  // Channel set data
1702  s->x96_nchannels = 0;
1703  for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1704  header_pos = get_bits_count(&s->gb);
1705 
1706  if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1707  s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1708  if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1709  return ret;
1710  }
1711 
1712  x96_base_ch += x96_nchannels[i];
1713 
1714  if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1715  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1716  return AVERROR_INVALIDDATA;
1717  }
1718  }
1719 
1720  return 0;
1721 }
1722 
1724 {
1725  int aux_pos;
1726 
1727  if (get_bits_left(&s->gb) < 0)
1728  return AVERROR_INVALIDDATA;
1729 
1730  // Auxiliary data byte count (can't be trusted)
1731  skip_bits(&s->gb, 6);
1732 
1733  // 4-byte align
1734  skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1735 
1736  // Auxiliary data sync word
1737  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1738  av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1739  return AVERROR_INVALIDDATA;
1740  }
1741 
1742  aux_pos = get_bits_count(&s->gb);
1743 
1744  // Auxiliary decode time stamp flag
1745  if (get_bits1(&s->gb))
1746  skip_bits_long(&s->gb, 47);
1747 
1748  // Auxiliary dynamic downmix flag
1749  if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1750  int i, m, n;
1751 
1752  // Auxiliary primary channel downmix type
1753  s->prim_dmix_type = get_bits(&s->gb, 3);
1754  if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1755  av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1756  return AVERROR_INVALIDDATA;
1757  }
1758 
1759  // Size of downmix coefficients matrix
1761  n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1762 
1763  // Dynamic downmix code coefficients
1764  for (i = 0; i < m * n; i++) {
1765  int code = get_bits(&s->gb, 9);
1766  int sign = (code >> 8) - 1;
1767  unsigned int index = code & 0xff;
1768  if (index >= FF_DCA_DMIXTABLE_SIZE) {
1769  av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1770  return AVERROR_INVALIDDATA;
1771  }
1772  s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1773  }
1774  }
1775 
1776  // Byte align
1777  skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1778 
1779  // CRC16 of auxiliary data
1780  skip_bits(&s->gb, 16);
1781 
1782  // Check CRC
1783  if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1784  av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1785  return AVERROR_INVALIDDATA;
1786  }
1787 
1788  return 0;
1789 }
1790 
1792 {
1793  DCAContext *dca = s->avctx->priv_data;
1794  int ret = -1;
1795 
1796  // Time code stamp
1797  if (s->ts_present)
1798  skip_bits_long(&s->gb, 32);
1799 
1800  // Auxiliary data
1801  if (s->aux_present && (ret = parse_aux_data(s)) < 0
1802  && (s->avctx->err_recognition & AV_EF_EXPLODE))
1803  return ret;
1804 
1805  if (ret < 0)
1806  s->prim_dmix_embedded = 0;
1807 
1808  // Core extensions
1809  if (s->ext_audio_present && !dca->core_only) {
1810  int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1811  int last_pos = get_bits_count(&s->gb) / 32;
1812  int size, dist;
1813 
1814  // Search for extension sync words aligned on 4-byte boundary. Search
1815  // must be done backwards from the end of core frame to work around
1816  // sync word aliasing issues.
1817  switch (s->ext_audio_type) {
1818  case EXT_AUDIO_XCH:
1819  if (dca->request_channel_layout)
1820  break;
1821 
1822  // The distance between XCH sync word and end of the core frame
1823  // must be equal to XCH frame size. Off by one error is allowed for
1824  // compatibility with legacy bitstreams. Minimum XCH frame size is
1825  // 96 bytes. AMODE and PCHS are further checked to reduce
1826  // probability of alias sync detection.
1827  for (; sync_pos >= last_pos; sync_pos--) {
1828  if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1829  s->gb.index = (sync_pos + 1) * 32;
1830  size = get_bits(&s->gb, 10) + 1;
1831  dist = s->frame_size - sync_pos * 4;
1832  if (size >= 96
1833  && (size == dist || size - 1 == dist)
1834  && get_bits(&s->gb, 7) == 0x08) {
1835  s->xch_pos = get_bits_count(&s->gb);
1836  break;
1837  }
1838  }
1839  }
1840 
1841  if (!s->xch_pos) {
1842  av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1844  return AVERROR_INVALIDDATA;
1845  }
1846  break;
1847 
1848  case EXT_AUDIO_X96:
1849  // The distance between X96 sync word and end of the core frame
1850  // must be equal to X96 frame size. Minimum X96 frame size is 96
1851  // bytes.
1852  for (; sync_pos >= last_pos; sync_pos--) {
1853  if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1854  s->gb.index = (sync_pos + 1) * 32;
1855  size = get_bits(&s->gb, 12) + 1;
1856  dist = s->frame_size - sync_pos * 4;
1857  if (size >= 96 && size == dist) {
1858  s->x96_pos = get_bits_count(&s->gb);
1859  break;
1860  }
1861  }
1862  }
1863 
1864  if (!s->x96_pos) {
1865  av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1867  return AVERROR_INVALIDDATA;
1868  }
1869  break;
1870 
1871  case EXT_AUDIO_XXCH:
1872  if (dca->request_channel_layout)
1873  break;
1874 
1875  // XXCH frame header CRC must be valid. Minimum XXCH frame header
1876  // size is 11 bytes.
1877  for (; sync_pos >= last_pos; sync_pos--) {
1878  if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1879  s->gb.index = (sync_pos + 1) * 32;
1880  size = get_bits(&s->gb, 6) + 1;
1881  dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1882  if (size >= 11 && size <= dist &&
1883  !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1884  (sync_pos + 1) * 4, size - 4)) {
1885  s->xxch_pos = sync_pos * 32;
1886  break;
1887  }
1888  }
1889  }
1890 
1891  if (!s->xxch_pos) {
1892  av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1894  return AVERROR_INVALIDDATA;
1895  }
1896  break;
1897  }
1898  }
1899 
1900  return 0;
1901 }
1902 
1904 {
1905  int ret;
1906 
1907  s->ext_audio_mask = 0;
1908  s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1909 
1910  if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1911  return ret;
1912 
1913  skip_bits_long(&s->gb, 32);
1914  if ((ret = parse_frame_header(s)) < 0)
1915  return ret;
1916  if ((ret = alloc_sample_buffer(s)) < 0)
1917  return ret;
1918  if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1919  return ret;
1920  if ((ret = parse_optional_info(s)) < 0)
1921  return ret;
1922 
1923  // Workaround for DTS in WAV
1924  if (s->frame_size > size && s->frame_size < size + 4)
1925  s->frame_size = size;
1926 
1927  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1928  av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1930  return AVERROR_INVALIDDATA;
1931  }
1932 
1933  return 0;
1934 }
1935 
1937 {
1938  AVCodecContext *avctx = s->avctx;
1939  DCAContext *dca = avctx->priv_data;
1940  GetBitContext gb = s->gb;
1941  int exss_mask = asset ? asset->extension_mask : 0;
1942  int ret = 0, ext = 0;
1943 
1944  // Parse (X)XCH unless downmixing
1945  if (!dca->request_channel_layout) {
1946  if (exss_mask & DCA_EXSS_XXCH) {
1947  if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1948  return ret;
1949  ret = parse_xxch_frame(s);
1950  ext = DCA_EXSS_XXCH;
1951  } else if (s->xxch_pos) {
1952  s->gb.index = s->xxch_pos;
1953  ret = parse_xxch_frame(s);
1954  ext = DCA_CSS_XXCH;
1955  } else if (s->xch_pos) {
1956  s->gb.index = s->xch_pos;
1957  ret = parse_xch_frame(s);
1958  ext = DCA_CSS_XCH;
1959  }
1960 
1961  // Revert to primary channel set in case (X)XCH parsing fails
1962  if (ret < 0) {
1963  if (avctx->err_recognition & AV_EF_EXPLODE)
1964  return ret;
1967  if (s->lfe_present)
1969  } else {
1970  s->ext_audio_mask |= ext;
1971  }
1972  }
1973 
1974  // Parse XBR
1975  if (exss_mask & DCA_EXSS_XBR) {
1976  if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1977  return ret;
1978  if ((ret = parse_xbr_frame(s)) < 0) {
1979  if (avctx->err_recognition & AV_EF_EXPLODE)
1980  return ret;
1981  } else {
1983  }
1984  }
1985 
1986  // Parse X96 unless decoding XLL
1987  if (!(dca->packet & DCA_PACKET_XLL)) {
1988  if (exss_mask & DCA_EXSS_X96) {
1989  if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1990  return ret;
1991  if ((ret = parse_x96_frame_exss(s)) < 0) {
1992  if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1993  return ret;
1994  } else {
1996  }
1997  } else if (s->x96_pos) {
1998  s->gb = gb;
1999  s->gb.index = s->x96_pos;
2000  if ((ret = parse_x96_frame(s)) < 0) {
2001  if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2002  return ret;
2003  } else {
2005  }
2006  }
2007  }
2008 
2009  return 0;
2010 }
2011 
2013 {
2014  int pos, spkr;
2015 
2016  // Try to map this channel to core first
2017  pos = ff_dca_channels[s->audio_mode];
2018  if (ch < pos) {
2019  spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
2020  if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2021  if (s->xxch_core_mask & (1U << spkr))
2022  return spkr;
2023  if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
2024  return DCA_SPEAKER_Lss;
2025  if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
2026  return DCA_SPEAKER_Rss;
2027  return -1;
2028  }
2029  return spkr;
2030  }
2031 
2032  // Then XCH
2033  if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2034  return DCA_SPEAKER_Cs;
2035 
2036  // Then XXCH
2037  if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2038  for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2039  if (s->xxch_spkr_mask & (1U << spkr))
2040  if (pos++ == ch)
2041  return spkr;
2042  }
2043 
2044  // No mapping
2045  return -1;
2046 }
2047 
2049 {
2050  memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2051  s->output_history_lfe_fixed = 0;
2052  s->output_history_lfe_float = 0;
2053 }
2054 
2056 {
2057  if (s->filter_mode != mode) {
2058  erase_dsp_history(s);
2059  s->filter_mode = mode;
2060  }
2061 }
2062 
2064 {
2065  int n, ch, spkr, nsamples, x96_nchannels = 0;
2066  const int32_t *filter_coeff;
2067  int32_t *ptr;
2068 
2069  // Externally set x96_synth flag implies that X96 synthesis should be
2070  // enabled, yet actual X96 subband data should be discarded. This is a
2071  // special case for lossless residual decoder that ignores X96 data if
2072  // present.
2073  if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2074  x96_nchannels = s->x96_nchannels;
2075  x96_synth = 1;
2076  }
2077  if (x96_synth < 0)
2078  x96_synth = 0;
2079 
2080  s->output_rate = s->sample_rate << x96_synth;
2081  s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2082 
2083  // Reallocate PCM output buffer
2085  nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2086  if (!s->output_buffer)
2087  return AVERROR(ENOMEM);
2088 
2089  ptr = (int32_t *)s->output_buffer;
2090  for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2091  if (s->ch_mask & (1U << spkr)) {
2092  s->output_samples[spkr] = ptr;
2093  ptr += nsamples;
2094  } else {
2095  s->output_samples[spkr] = NULL;
2096  }
2097  }
2098 
2099  // Handle change of filtering mode
2100  set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2101 
2102  // Select filter
2103  if (x96_synth)
2104  filter_coeff = ff_dca_fir_64bands_fixed;
2105  else if (s->filter_perfect)
2106  filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2107  else
2108  filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2109 
2110  // Filter primary channels
2111  for (ch = 0; ch < s->nchannels; ch++) {
2112  // Map this primary channel to speaker
2113  spkr = map_prm_ch_to_spkr(s, ch);
2114  if (spkr < 0)
2115  return AVERROR(EINVAL);
2116 
2117  // Filter bank reconstruction
2118  s->dcadsp->sub_qmf_fixed[x96_synth](
2119  &s->synth,
2120  &s->dcadct,
2121  s->output_samples[spkr],
2122  s->subband_samples[ch],
2123  ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2124  s->dcadsp_data[ch].u.fix.hist1,
2125  &s->dcadsp_data[ch].offset,
2126  s->dcadsp_data[ch].u.fix.hist2,
2127  filter_coeff,
2128  s->npcmblocks);
2129  }
2130 
2131  // Filter LFE channel
2132  if (s->lfe_present) {
2133  int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2134  int nlfesamples = s->npcmblocks >> 1;
2135 
2136  // Check LFF
2137  if (s->lfe_present == LFE_FLAG_128) {
2138  av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2139  return AVERROR(EINVAL);
2140  }
2141 
2142  // Offset intermediate buffer for X96
2143  if (x96_synth)
2144  samples += nsamples / 2;
2145 
2146  // Interpolate LFE channel
2147  s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2149 
2150  if (x96_synth) {
2151  // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2152  // (47.6 - 48.0 kHz) components of interpolation image
2154  samples, &s->output_history_lfe_fixed,
2155  nsamples / 2);
2156 
2157  }
2158 
2159  // Update LFE history
2160  for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2161  s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2162  }
2163 
2164  return 0;
2165 }
2166 
2168 {
2169  AVCodecContext *avctx = s->avctx;
2170  DCAContext *dca = avctx->priv_data;
2171  int i, n, ch, ret, spkr, nsamples;
2172 
2173  // Don't filter twice when falling back from XLL
2174  if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2175  return ret;
2176 
2177  avctx->sample_rate = s->output_rate;
2178  avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2179  avctx->bits_per_raw_sample = 24;
2180 
2181  frame->nb_samples = nsamples = s->npcmsamples;
2182  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2183  return ret;
2184 
2185  // Undo embedded XCH downmix
2186  if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2187  && s->audio_mode >= AMODE_2F2R) {
2191  nsamples);
2192 
2193  }
2194 
2195  // Undo embedded XXCH downmix
2197  && s->xxch_dmix_embedded) {
2198  int scale_inv = s->xxch_dmix_scale_inv;
2199  int *coeff_ptr = s->xxch_dmix_coeff;
2200  int xch_base = ff_dca_channels[s->audio_mode];
2201  av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2202 
2203  // Undo embedded core downmix pre-scaling
2204  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2205  if (s->xxch_core_mask & (1U << spkr)) {
2206  s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2207  scale_inv, nsamples);
2208  }
2209  }
2210 
2211  // Undo downmix
2212  for (ch = xch_base; ch < s->nchannels; ch++) {
2213  int src_spkr = map_prm_ch_to_spkr(s, ch);
2214  if (src_spkr < 0)
2215  return AVERROR(EINVAL);
2216  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2217  if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2218  int coeff = mul16(*coeff_ptr++, scale_inv);
2219  if (coeff) {
2220  s->dcadsp->dmix_sub(s->output_samples[spkr ],
2221  s->output_samples[src_spkr],
2222  coeff, nsamples);
2223  }
2224  }
2225  }
2226  }
2227  }
2228 
2230  // Front sum/difference decoding
2231  if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2232  || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2235  nsamples);
2236  }
2237 
2238  // Surround sum/difference decoding
2239  if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2242  nsamples);
2243  }
2244  }
2245 
2246  // Downmix primary channel set to stereo
2247  if (s->request_mask != s->ch_mask) {
2249  s->output_samples,
2250  s->prim_dmix_coeff,
2251  nsamples, s->ch_mask);
2252  }
2253 
2254  for (i = 0; i < avctx->channels; i++) {
2255  int32_t *samples = s->output_samples[s->ch_remap[i]];
2256  int32_t *plane = (int32_t *)frame->extended_data[i];
2257  for (n = 0; n < nsamples; n++)
2258  plane[n] = clip23(samples[n]) * (1 << 8);
2259  }
2260 
2261  return 0;
2262 }
2263 
2265 {
2266  AVCodecContext *avctx = s->avctx;
2267  int x96_nchannels = 0, x96_synth = 0;
2268  int i, n, ch, ret, spkr, nsamples, nchannels;
2269  float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2270  const float *filter_coeff;
2271 
2272  if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2273  x96_nchannels = s->x96_nchannels;
2274  x96_synth = 1;
2275  }
2276 
2277  avctx->sample_rate = s->sample_rate << x96_synth;
2278  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2279  avctx->bits_per_raw_sample = 0;
2280 
2281  frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2282  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2283  return ret;
2284 
2285  // Build reverse speaker to channel mapping
2286  for (i = 0; i < avctx->channels; i++)
2287  output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2288 
2289  // Allocate space for extra channels
2290  nchannels = av_popcount(s->ch_mask) - avctx->channels;
2291  if (nchannels > 0) {
2293  nsamples * nchannels * sizeof(float));
2294  if (!s->output_buffer)
2295  return AVERROR(ENOMEM);
2296 
2297  ptr = (float *)s->output_buffer;
2298  for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2299  if (!(s->ch_mask & (1U << spkr)))
2300  continue;
2301  if (output_samples[spkr])
2302  continue;
2303  output_samples[spkr] = ptr;
2304  ptr += nsamples;
2305  }
2306  }
2307 
2308  // Handle change of filtering mode
2309  set_filter_mode(s, x96_synth);
2310 
2311  // Select filter
2312  if (x96_synth)
2313  filter_coeff = ff_dca_fir_64bands;
2314  else if (s->filter_perfect)
2315  filter_coeff = ff_dca_fir_32bands_perfect;
2316  else
2317  filter_coeff = ff_dca_fir_32bands_nonperfect;
2318 
2319  // Filter primary channels
2320  for (ch = 0; ch < s->nchannels; ch++) {
2321  // Map this primary channel to speaker
2322  spkr = map_prm_ch_to_spkr(s, ch);
2323  if (spkr < 0)
2324  return AVERROR(EINVAL);
2325 
2326  // Filter bank reconstruction
2327  s->dcadsp->sub_qmf_float[x96_synth](
2328  &s->synth,
2329  &s->imdct[x96_synth],
2330  output_samples[spkr],
2331  s->subband_samples[ch],
2332  ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2333  s->dcadsp_data[ch].u.flt.hist1,
2334  &s->dcadsp_data[ch].offset,
2335  s->dcadsp_data[ch].u.flt.hist2,
2336  filter_coeff,
2337  s->npcmblocks,
2338  1.0f / (1 << (17 - x96_synth)));
2339  }
2340 
2341  // Filter LFE channel
2342  if (s->lfe_present) {
2343  int dec_select = (s->lfe_present == LFE_FLAG_128);
2344  float *samples = output_samples[DCA_SPEAKER_LFE1];
2345  int nlfesamples = s->npcmblocks >> (dec_select + 1);
2346 
2347  // Offset intermediate buffer for X96
2348  if (x96_synth)
2349  samples += nsamples / 2;
2350 
2351  // Select filter
2352  if (dec_select)
2353  filter_coeff = ff_dca_lfe_fir_128;
2354  else
2355  filter_coeff = ff_dca_lfe_fir_64;
2356 
2357  // Interpolate LFE channel
2358  s->dcadsp->lfe_fir_float[dec_select](
2359  samples, s->lfe_samples + DCA_LFE_HISTORY,
2360  filter_coeff, s->npcmblocks);
2361 
2362  if (x96_synth) {
2363  // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2364  // (47.6 - 48.0 kHz) components of interpolation image
2365  s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2366  samples, &s->output_history_lfe_float,
2367  nsamples / 2);
2368  }
2369 
2370  // Update LFE history
2371  for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2372  s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2373  }
2374 
2375  // Undo embedded XCH downmix
2376  if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2377  && s->audio_mode >= AMODE_2F2R) {
2378  s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2379  output_samples[DCA_SPEAKER_Cs],
2380  -M_SQRT1_2, nsamples);
2381  s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2382  output_samples[DCA_SPEAKER_Cs],
2383  -M_SQRT1_2, nsamples);
2384  }
2385 
2386  // Undo embedded XXCH downmix
2388  && s->xxch_dmix_embedded) {
2389  float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2390  int *coeff_ptr = s->xxch_dmix_coeff;
2391  int xch_base = ff_dca_channels[s->audio_mode];
2392  av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2393 
2394  // Undo downmix
2395  for (ch = xch_base; ch < s->nchannels; ch++) {
2396  int src_spkr = map_prm_ch_to_spkr(s, ch);
2397  if (src_spkr < 0)
2398  return AVERROR(EINVAL);
2399  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2400  if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2401  int coeff = *coeff_ptr++;
2402  if (coeff) {
2403  s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2404  output_samples[src_spkr],
2405  coeff * (-1.0f / (1 << 15)),
2406  nsamples);
2407  }
2408  }
2409  }
2410  }
2411 
2412  // Undo embedded core downmix pre-scaling
2413  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2414  if (s->xxch_core_mask & (1U << spkr)) {
2415  s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2416  output_samples[spkr],
2417  scale_inv, nsamples);
2418  }
2419  }
2420  }
2421 
2423  // Front sum/difference decoding
2424  if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2425  || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2426  s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2427  output_samples[DCA_SPEAKER_R],
2428  nsamples);
2429  }
2430 
2431  // Surround sum/difference decoding
2432  if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2433  s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2434  output_samples[DCA_SPEAKER_Rs],
2435  nsamples);
2436  }
2437  }
2438 
2439  // Downmix primary channel set to stereo
2440  if (s->request_mask != s->ch_mask) {
2441  ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2442  s->prim_dmix_coeff,
2443  nsamples, s->ch_mask);
2444  }
2445 
2446  return 0;
2447 }
2448 
2450 {
2451  AVCodecContext *avctx = s->avctx;
2452  DCAContext *dca = avctx->priv_data;
2453  DCAExssAsset *asset = &dca->exss.assets[0];
2454  enum AVMatrixEncoding matrix_encoding;
2455  int ret;
2456 
2457  // Handle downmixing to stereo request
2460  && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2463  else
2464  s->request_mask = s->ch_mask;
2465  if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2466  return AVERROR(EINVAL);
2467 
2468  // Force fixed point mode when falling back from XLL
2469  if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2470  && (asset->extension_mask & DCA_EXSS_XLL)))
2471  ret = filter_frame_fixed(s, frame);
2472  else
2473  ret = filter_frame_float(s, frame);
2474  if (ret < 0)
2475  return ret;
2476 
2477  // Set profile, bit rate, etc
2478  if (s->ext_audio_mask & DCA_EXSS_MASK)
2479  avctx->profile = FF_PROFILE_DTS_HD_HRA;
2480  else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2481  avctx->profile = FF_PROFILE_DTS_ES;
2482  else if (s->ext_audio_mask & DCA_CSS_X96)
2483  avctx->profile = FF_PROFILE_DTS_96_24;
2484  else
2485  avctx->profile = FF_PROFILE_DTS;
2486 
2487  if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2488  avctx->bit_rate = s->bit_rate;
2489  else
2490  avctx->bit_rate = 0;
2491 
2492  if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2494  matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2495  else
2496  matrix_encoding = AV_MATRIX_ENCODING_NONE;
2497  if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2498  return ret;
2499 
2500  return 0;
2501 }
2502 
2504 {
2505  if (s->subband_buffer) {
2507  memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2508  }
2509 
2510  if (s->x96_subband_buffer)
2512 
2513  erase_dsp_history(s);
2514 }
2515 
2517 {
2518  if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2519  return -1;
2520  if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2521  return -1;
2522 
2523  ff_dcadct_init(&s->dcadct);
2524  if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2525  return -1;
2526  if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2527  return -1;
2529 
2530  s->x96_rand = 1;
2531  return 0;
2532 }
2533 
2535 {
2536  av_freep(&s->float_dsp);
2537  av_freep(&s->fixed_dsp);
2538 
2539  ff_mdct_end(&s->imdct[0]);
2540  ff_mdct_end(&s->imdct[1]);
2541 
2542  av_freep(&s->subband_buffer);
2543  s->subband_size = 0;
2544 
2546  s->x96_subband_size = 0;
2547 
2548  av_freep(&s->output_buffer);
2549  s->output_size = 0;
2550 }
int audio_mode
Audio channel arrangement.
Definition: dca_core.h:81
int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
Definition: dca_core.c:2063
int plane
Definition: avisynth_c.h:291
VLC vlc[7]
Actual codes.
Definition: dcahuff.h:36
float, planar
Definition: samplefmt.h:69
static int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
Definition: dca_core.c:607
int16_t prediction_vq_index[DCA_CHANNELS][DCA_SUBBANDS_X96]
Prediction coefficients VQ address.
Definition: dca_core.h:114
#define DCA_SYNCWORD_X96
Definition: dca_syncwords.h:28
void(* lfe_fir_fixed)(int32_t *pcm_samples, int32_t *lfe_samples, const int32_t *filter_coeff, ptrdiff_t npcmblocks)
Definition: dcadsp.h:58
#define NULL
Definition: coverity.c:32
int xxch_mask_nbits
Number of bits for loudspeaker mask.
Definition: dca_core.h:134
static int ff_dca_seek_bits(GetBitContext *s, int p)
Definition: dcadec.h:89
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int offset
Definition: dca_core.h:70
static int shift(int a, int b)
Definition: sonic.c:82
static const uint8_t quant_index_sel_nbits[DCA_CODE_BOOKS]
Definition: dca_core.c:95
DCAVLC ff_dca_vlc_bit_allocation
Definition: dcahuff.c:1246
int prim_dmix_coeff[DCA_DMIX_CHANNELS_MAX *DCA_CORE_CHANNELS_MAX]
Dynamic downmix code coefficients.
Definition: dca_core.h:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
void(* sub_qmf_float[2])(SynthFilterContext *synth, FFTContext *imdct, float *pcm_samples, int32_t **subband_samples_lo, int32_t **subband_samples_hi, float *hist1, int *offset, float *hist2, const float *filter_coeff, ptrdiff_t npcmblocks, float scale)
Definition: dcadsp.h:49
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
const uint32_t ff_dca_lossy_quant[32]
Definition: dcadata.c:4219
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1714
int xxch_dmix_embedded
Downmix already performed by encoder.
Definition: dca_core.h:137
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
AudioMode
Definition: dca_core.c:37
int ext_audio_type
Extension audio descriptor flag.
Definition: dca_core.h:87
#define FF_PROFILE_DTS_HD_HRA
Definition: avcodec.h:3171
int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
Definition: dca_core.c:1936
#define M_SQRT1_2
Definition: mathematics.h:52
#define DCA_SPEAKER_LAYOUT_3_0
Definition: dca.h:82
int frame_size
Primary frame byte size.
Definition: dca_core.h:80
#define DCA_PACKET_EXSS
Definition: dcadec.h:38
static int parse_frame_header(DCACoreDecoder *s)
Definition: dca_core.c:117
static int alloc_x96_sample_buffer(DCACoreDecoder *s)
Definition: dca_core.c:1386
unsigned int output_size
Definition: dca_core.h:172
int8_t joint_scale_sel[DCA_CHANNELS]
Joint subband codebook select.
Definition: dca_core.h:118
static void set_filter_mode(DCACoreDecoder *s, int mode)
Definition: dca_core.c:2055
static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
Definition: dca_core.c:2264
const uint8_t * buffer
Definition: get_bits.h:56
int av_log2(unsigned v)
Definition: intmath.c:26
static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5]
Definition: dca_core.c:65
av_cold void ff_dca_core_close(DCACoreDecoder *s)
Definition: dca_core.c:2534
int8_t joint_intensity_index[DCA_CHANNELS]
Joint intensity coding index.
Definition: dca_core.h:104
static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
Definition: dca_core.c:232
int core_only
Core only decoding flag.
Definition: dcadec.h:65
#define DCA_FILTER_MODE_FIXED
Definition: dca_core.h:57
int packet
Packet flags.
Definition: dcadec.h:62
union DCADSPData::@40 u
int filter_perfect
Multirate interpolator switch.
Definition: dca_core.h:92
void(* dmix_scale_inv)(int32_t *dst, int scale_inv, ptrdiff_t len)
Definition: dcadsp.h:83
float output_history_lfe_float
LFE PCM history for X96 filter.
Definition: dca_core.h:176
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:3049
int profile
profile
Definition: avcodec.h:3153
static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
Definition: dca_core.c:2012
LFEFlag
Definition: dca_core.c:58
unsigned int x96_subband_size
Definition: dca_core.h:153
int nsubframes
Number of subframes.
Definition: dca_core.h:99
#define DCA_ADPCM_COEFFS
Definition: dca_core.h:46
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:232
av_cold void ff_synth_filter_init(SynthFilterContext *c)
Definition: synth_filter.c:171
static int parse_xch_frame(DCACoreDecoder *s)
Definition: dca_core.c:943
ExtAudioType
Definition: dca_core.c:52
const float ff_dca_fir_32bands_nonperfect[512]
Definition: dcadata.c:6804
const int8_t ff_dca_high_freq_vq[1024][32]
Definition: dcadata.c:4236
#define DCA_EXSS_CHANNELS_MAX
Definition: dca_core.h:53
static int parse_xbr_frame(DCACoreDecoder *s)
Definition: dca_core.c:1176
const float ff_dca_fir_64bands[1024]
Definition: dcadata.c:7546
void(* vector_fmac_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float and add to destination vector.
Definition: float_dsp.h:54
static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
Definition: dca_core.c:905
static int parse_subframe_header(DCACoreDecoder *s, int sf, enum HeaderType header, int xch_base)
Definition: dca_core.c:480
DCADSPData dcadsp_data[DCA_CHANNELS]
FIR history buffers.
Definition: dca_core.h:163
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
int32_t scale_factor_adj[DCA_CHANNELS][DCA_CODE_BOOKS]
Scale factor adjustment.
Definition: dca_core.h:109
static int32_t clip23(int32_t a)
Definition: dcamath.h:53
static int32_t mul23(int32_t a, int32_t b)
Definition: dcamath.h:50
int x96_rand
Random seed for generating samples for unallocated X96 subbands.
Definition: dca_core.h:149
const uint32_t ff_dca_bit_rates[32]
Definition: dcadata.c:32
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2418
uint8_t
int8_t prediction_mode[DCA_CHANNELS][DCA_SUBBANDS_X96]
Prediction mode.
Definition: dca_core.h:113
static int32_t mul16(int32_t a, int32_t b)
Definition: dcamath.h:47
#define av_cold
Definition: attributes.h:82
#define DCA_SYNCWORD_XBR
Definition: dca_syncwords.h:29
mode
Definition: f_perms.c:27
int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
Definition: dca_core.c:1903
int source_pcm_res
Source PCM resolution.
Definition: dca_core.h:93
#define FF_DCA_DMIXTABLE_OFFSET
Definition: dcadata.h:65
int xxch_dmix_scale_inv
Downmix scale factor.
Definition: dca_core.h:138
int32_t * subband_samples[DCA_CHANNELS][DCA_SUBBANDS]
Subband samples.
Definition: dca_core.h:159
#define DCA_SYNCWORD_XCH
Definition: dca_syncwords.h:26
static int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
Definition: dca_core.c:654
void av_fast_mallocz(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:504
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
int filter_mode
Previous filtering mode for detecting changes.
Definition: dca_core.h:184
const int32_t ff_dca_lfe_fir_64_fixed[256]
Definition: dcadata.c:8332
int extension_mask
Coding components used in asset.
Definition: dca_exss.h:45
static AVFrame * frame
void * output_buffer
PCM output buffer base.
Definition: dca_core.h:173
static int alloc_sample_buffer(DCACoreDecoder *s)
Definition: dca_core.c:877
static int ff_dca_check_crc(AVCodecContext *avctx, GetBitContext *s, int p1, int p2)
Definition: dcadec.h:75
#define DCA_SUBBANDS_X96
Definition: dca_core.h:42
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
int prim_dmix_type
Auxiliary primary channel downmix type.
Definition: dca_core.h:123
int32_t * x96_subband_buffer
X96 subband sample buffer base.
Definition: dca_core.h:154
ptrdiff_t size
Definition: opengl_enc.c:101
AVFixedDSPContext * avpriv_alloc_fixed_dsp(int bit_exact)
Allocate and initialize a fixed DSP context.
Definition: fixed_dsp.c:148
static const uint8_t header[24]
Definition: sdr2.c:67
static int parse_aux_data(DCACoreDecoder *s)
Definition: dca_core.c:1723
static void inverse_adpcm(int32_t **subband_samples, const int16_t *vq_index, const int8_t *prediction_mode, int sb_start, int sb_end, int ofs, int len)
Definition: dca_core.c:704
#define av_log(a,...)
static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels, int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
Definition: dca_core.c:1041
int sync_ssf
Audio sync word insertion flag.
Definition: dca_core.h:89
static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
Definition: dca_core.c:1265
DCAExssParser exss
EXSS parser context.
Definition: dcadec.h:51
unsigned m
Definition: audioconvert.c:187
static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
Definition: dca_core.c:1583
const int32_t ff_dca_fir_64bands_fixed[1024]
Definition: dcadata.c:8367
int32_t * subband_buffer
Subband sample buffer base.
Definition: dca_core.h:158
static void dequantize(int32_t *output, const int32_t *input, int32_t step_size, int32_t scale, int residual)
Definition: dca_core.c:681
void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, int *coeff_l, int nsamples, int ch_mask)
Definition: dcadec.c:79
static int parse_x96_frame_exss(DCACoreDecoder *s)
Definition: dca_core.c:1647
int npcmsamples
Number of PCM samples per channel.
Definition: dca_core.h:181
int8_t subband_vq_start[DCA_CHANNELS]
High frequency VQ start subband.
Definition: dca_core.h:103
#define U(x)
Definition: vp56_arith.h:37
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:568
int8_t nsubbands[DCA_CHANNELS]
Subband activity count.
Definition: dca_core.h:102
int xxch_offset
Offset to XXCH extension from start of substream.
Definition: dca_exss.h:53
void(* lfe_fir_float[2])(float *pcm_samples, int32_t *lfe_samples, const float *filter_coeff, ptrdiff_t npcmblocks)
Definition: dcadsp.h:43
int sumdiff_front
Front sum/difference flag.
Definition: dca_core.h:95
int8_t nsubsubframes[DCA_SUBFRAMES]
Subsubframe count for each subframe.
Definition: dca_core.h:112
#define DCA_PCMBLOCK_SAMPLES
Definition: dca_core.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
SynthFilterContext synth
Definition: dca_core.h:167
int8_t transition_mode_sel[DCA_CHANNELS]
Transient mode code book.
Definition: dca_core.h:105
static int rand_x96(DCACoreDecoder *s)
Definition: dca_core.c:1259
int bit_rate
Transmission bit rate.
Definition: dca_core.h:83
const float ff_dca_lfe_fir_128[256]
Definition: dcadata.c:7478
DCAVLC ff_dca_vlc_transition_mode
Definition: dcahuff.c:1247
static const uint16_t mask[17]
Definition: lzw.c:38
int xxch_crc_present
CRC presence flag for XXCH channel set header.
Definition: dca_core.h:133
DCADCTContext dcadct
Definition: dca_core.h:165
AVFloatDSPContext * float_dsp
Definition: dca_core.h:168
#define AVERROR(e)
Definition: error.h:43
int offset
Code values offset.
Definition: dcahuff.h:34
const uint32_t ff_dca_lossless_quant[32]
Definition: dcadata.c:4227
static int32_t mul31(int32_t a, int32_t b)
Definition: dcamath.h:51
const uint16_t ff_dca_dmixtable[FF_DCA_DMIXTABLE_SIZE]
Definition: dcadata.c:8638
void(* butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len)
Calculate the sum and difference of two vectors of floats.
Definition: float_dsp.h:148
const float ff_dca_lfe_fir_64[256]
Definition: dcadata.c:7335
#define FF_PROFILE_DTS_ES
Definition: avcodec.h:3169
#define DCA_ABITS_MAX
Definition: dca_core.h:48
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1744
void(* butterflies_fixed)(int *av_restrict v1, int *av_restrict v2, int len)
Calculate the sum and difference of two vectors of integers.
Definition: fixed_dsp.h:153
static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header, int xch_base, int *sub_pos, int *lfe_pos)
Definition: dca_core.c:727
#define DCA_SPEAKER_LAYOUT_3_1
Definition: dca.h:84
int aux_present
Auxiliary data flag.
Definition: dca_core.h:86
const uint32_t ff_dca_quant_levels[32]
Definition: dcadata.c:4211
#define ff_mdct_init
Definition: fft.h:169
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define DCA_SPEAKER_LAYOUT_STEREO
Definition: dca.h:80
#define FFMAX(a, b)
Definition: common.h:94
int32_t * lfe_samples
Decimated LFE samples.
Definition: dca_core.h:160
int xbr_offset
Offset to XBR extension from start of substream.
Definition: dca_exss.h:50
int prim_dmix_embedded
Auxiliary dynamic downmix flag.
Definition: dca_core.h:122
int sample_rate
Core audio sampling frequency.
Definition: dca_core.h:82
int request_mask
Requested channel layout (for stereo downmix)
Definition: dca_core.h:179
static int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
Definition: dca_core.c:643
int output_rate
Output sample rate (1x or 2x header rate)
Definition: dca_core.h:182
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:883
int nchannels
Number of primary audio channels (incl. extension channels)
Definition: dca_core.h:100
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2936
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
Definition: dca_core.c:103
#define FFMIN(a, b)
Definition: common.h:96
signed 32 bits, planar
Definition: samplefmt.h:68
const uint8_t ff_dca_dmix_primary_nch[8]
Definition: dcadata.c:49
int xxch_pos
Bit position of XXCH frame in core substream.
Definition: dca_core.h:141
const AVCRC * crctab
Definition: dcadec.h:57
HeaderType
Definition: dca_core.c:31
static int parse_joint_scale(DCACoreDecoder *s, int sel)
Definition: dca_core.c:457
#define DCA_PACKET_XLL
Definition: dcadec.h:39
struct DCADSPData::@40::@42 fix
int size_in_bits
Definition: get_bits.h:58
int sumdiff_surround
Surround sum/difference flag.
Definition: dca_core.h:96
int32_t
#define DCA_SPEAKER_LAYOUT_2_1
Definition: dca.h:83
struct DCADSPData::@40::@41 flt
static int parse_optional_info(DCACoreDecoder *s)
Definition: dca_core.c:1791
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:357
static int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
Definition: dca_core.c:627
AVCodecContext * avctx
Definition: dca_core.h:74
int x96_crc_present
CRC presence flag for X96 channel set header.
Definition: dca_core.h:145
int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
Definition: dcadec.c:32
void(* lfe_x96_float)(float *dst, const float *src, float *hist, ptrdiff_t len)
Definition: dcadsp.h:46
#define DCA_SPEAKER_LAYOUT_2_2
Definition: dca.h:85
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:535
static void get_array(GetBitContext *s, int32_t *array, int size, int n)
Definition: dca_core.c:108
void(* vector_fmul_scalar)(float *dst, const float *src, float mul, int len)
Multiply a vector of floats by a scalar float.
Definition: float_dsp.h:69
static const uint8_t block_code_nbits[7]
Definition: dca_core.c:91
#define FF_PROFILE_DTS
Definition: avcodec.h:3168
av_cold void ff_dca_core_flush(DCACoreDecoder *s)
Definition: dca_core.c:2503
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:2947
int n
Definition: avisynth_c.h:547
const uint32_t avpriv_dca_sample_rates[16]
Definition: dca.c:34
DCAExssAsset assets[1]
Audio asset descriptors.
Definition: dca_exss.h:87
static int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
Definition: dca_core.c:428
static void erase_x96_adpcm_history(DCACoreDecoder *s)
Definition: dca_core.c:1373
const uint32_t ff_dca_inv_dmixtable[FF_DCA_INV_DMIXTABLE_SIZE]
Definition: dcadata.c:8672
static int parse_x96_frame(DCACoreDecoder *s)
Definition: dca_core.c:1618
#define FF_ARRAY_ELEMS(a)
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:240
int bits
Definition: vlc.h:27
const int16_t ff_dca_adpcm_vb[4096][4]
Definition: dcadata.c:56
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
const uint32_t ff_dca_scale_factor_quant7[128]
Definition: dcadata.c:4168
int xxch_core_mask
Core loudspeaker activity mask.
Definition: dca_core.h:135
#define FF_PROFILE_DTS_96_24
Definition: avcodec.h:3170
#define DCA_LFE_HISTORY
Definition: dca_core.h:47
const uint8_t ff_dca_channels[16]
Definition: dcadata.c:41
void(* decode_hf)(int32_t **dst, const int32_t *vq_index, const int8_t hf_vq[1024][32], int32_t scale_factors[32][2], ptrdiff_t sb_start, ptrdiff_t sb_end, ptrdiff_t ofs, ptrdiff_t len)
Definition: dcadsp.h:31
void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples, int *coeff_l, int nsamples, int ch_mask)
Definition: dcadec.c:110
#define DCA_SUBBANDS
Definition: dca_core.h:41
int ts_present
Embedded time stamp flag.
Definition: dca_core.h:85
int xxch_spkr_mask
Loudspeaker layout mask.
Definition: dca_core.h:136
void(* decode_joint)(int32_t **dst, int32_t **src, const int32_t *scale_factors, ptrdiff_t sb_start, ptrdiff_t sb_end, ptrdiff_t ofs, ptrdiff_t len)
Definition: dcadsp.h:38
int crc_present
CRC present flag.
Definition: dca_core.h:78
#define DCA_CODE_BOOKS
Definition: dcahuff.h:31
int sample_rate
samples per second
Definition: avcodec.h:2410
const uint8_t ff_dca_bits_per_sample[8]
Definition: dcadata.c:45
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:437
static void erase_dsp_history(DCACoreDecoder *s)
Definition: dca_core.c:2048
main external API structure.
Definition: avcodec.h:1649
int8_t bit_allocation_sel[DCA_CHANNELS]
Bit allocation quantizer select.
Definition: dca_core.h:107
int ext_audio_present
Extended coding flag.
Definition: dca_core.h:88
#define FASTDIV(a, b)
Definition: mathops.h:211
int es_format
Extended surround (ES) mastering flag.
Definition: dca_core.h:94
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
#define FF_DCA_INV_DMIXTABLE_SIZE
Definition: dcadata.h:64
static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
Definition: dca_core.c:2167
static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
Definition: dca_core.c:1412
int x96_subband_start
First encoded subband in X96 extension.
Definition: dca_core.h:148
int xxch_dmix_coeff[DCA_XXCH_CHANNELS_MAX *DCA_CORE_CHANNELS_MAX]
Downmix coefficients.
Definition: dca_core.h:140
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:324
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
int x96_pos
Bit position of X96 frame in core substream.
Definition: dca_core.h:150
const uint32_t ff_dca_scale_factor_quant6[64]
Definition: dcadata.c:4157
int xch_pos
Bit position of XCH frame in core substream.
Definition: dca_core.h:130
int index
Definition: gxfenc.c:89
int x96_size
Size of X96 extension in extension substream.
Definition: dca_exss.h:57
int xxch_dmix_mask[DCA_XXCH_CHANNELS_MAX]
Downmix channel mapping mask.
Definition: dca_core.h:139
av_cold int ff_dca_core_init(DCACoreDecoder *s)
Definition: dca_core.c:2516
int32_t * x96_subband_samples[DCA_CHANNELS][DCA_SUBBANDS_X96]
X96 subband samples.
Definition: dca_core.h:155
int8_t bit_allocation[DCA_CHANNELS][DCA_SUBBANDS_X96]
Bit allocation index.
Definition: dca_core.h:115
static const uint8_t audio_mode_ch_mask[AMODE_COUNT]
Definition: dca_core.c:78
av_cold void ff_dcadct_init(DCADCTContext *c)
Definition: dcadct.c:358
DCADSPContext * dcadsp
Definition: dca_core.h:164
int x96_rev_no
X96 revision number.
Definition: dca_core.h:144
int8_t transition_mode[DCA_SUBFRAMES][DCA_CHANNELS][DCA_SUBBANDS]
Transition mode.
Definition: dca_core.h:116
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:119
int32_t * output_samples[DCA_SPEAKER_COUNT]
PCM output for fixed point mode.
Definition: dca_core.h:174
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:332
int x96_nchannels
Number of primary channels in X96 extension.
Definition: dca_core.h:146
int32_t joint_scale_factors[DCA_CHANNELS][DCA_SUBBANDS_X96]
Scale factors for joint subband coding.
Definition: dca_core.h:119
#define DCA_SPEAKER_LAYOUT_5POINT0
Definition: dca.h:86
static int parse_xxch_frame(DCACoreDecoder *s)
Definition: dca_core.c:964
#define DCA_CHANNELS
Definition: dca_core.h:40
#define DCA_SYNCWORD_REV1AUX
Definition: dca_syncwords.h:34
#define AV_ZERO128(d)
Definition: intreadwrite.h:622
void(* sub_qmf_fixed[2])(SynthFilterContext *synth, DCADCTContext *imdct, int32_t *pcm_samples, int32_t **subband_samples_lo, int32_t **subband_samples_hi, int32_t *hist1, int *offset, int32_t *hist2, const int32_t *filter_coeff, ptrdiff_t npcmblocks)
Definition: dcadsp.h:64
#define DCA_EXSS_CHSETS_MAX
Definition: dca_core.h:54
unsigned int subband_size
Definition: dca_core.h:157
int lfe_present
Low frequency effects flag.
Definition: dca_core.h:90
int xbr_size
Size of XBR extension in extension substream.
Definition: dca_exss.h:51
int8_t scale_factor_sel[DCA_CHANNELS]
Scale factor code book.
Definition: dca_core.h:106
int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
Definition: dca_core.c:2449
DCAVLC ff_dca_vlc_scale_factor
Definition: dcahuff.c:1248
#define DCA_SYNCWORD_XXCH
Definition: dca_syncwords.h:27
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:499
int32_t scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2]
Scale factors (2x for transients and X96)
Definition: dca_core.h:117
int ch_remap[DCA_SPEAKER_COUNT]
Channel to speaker map.
Definition: dca_core.h:178
#define DCA_XXCH_CHANNELS_MAX
Definition: dca_core.h:52
#define DCA_SPEAKER_LAYOUT_MONO
Definition: dca.h:79
#define AV_COPY128(d, s)
Definition: intreadwrite.h:594
#define ff_mdct_end
Definition: fft.h:170
int max_depth
Parameter for get_vlc2()
Definition: dcahuff.h:35
int request_channel_layout
Converted from avctx.request_channel_layout.
Definition: dcadec.h:64
const uint32_t ff_dca_joint_scale_factors[129]
Definition: dcadata.c:4187
DCAVLC ff_dca_vlc_quant_index[DCA_CODE_BOOKS]
Definition: dcahuff.c:1249
static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
Definition: dca_core.c:1496
#define FF_DCA_DMIXTABLE_SIZE
Definition: dcadata.h:63
void * priv_data
Definition: avcodec.h:1691
static const uint8_t quant_index_group_size[DCA_CODE_BOOKS]
Definition: dca_core.c:99
AVFixedDSPContext * fixed_dsp
Definition: dca_core.h:169
int xxch_size
Size of XXCH extension in extension substream.
Definition: dca_exss.h:54
static void erase_adpcm_history(DCACoreDecoder *s)
Definition: dca_core.c:864
void(* dmix_sub_xch)(int32_t *dst1, int32_t *dst2, const int32_t *src, ptrdiff_t len)
Definition: dcadsp.h:74
int len
int channels
number of audio channels
Definition: avcodec.h:2411
GetBitContext gb
Definition: dca_core.h:75
static int32_t norm__(int64_t a, int bits)
Definition: dcamath.h:27
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
int ch_mask
Speaker layout mask (incl. LFE and extension channels)
Definition: dca_core.h:101
void(* dmix_sub)(int32_t *dst, const int32_t *src, int coeff, ptrdiff_t len)
Definition: dcadsp.h:77
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
int x96_offset
Offset to X96 extension from start of substream.
Definition: dca_exss.h:56
int drc_present
Embedded dynamic range flag.
Definition: dca_core.h:84
const int32_t ff_dca_fir_32bands_nonperfect_fixed[512]
Definition: dcadata.c:8201
#define DCA_SUBBAND_SAMPLES
Definition: dca_core.h:44
#define av_freep(p)
void(* lfe_x96_fixed)(int32_t *dst, const int32_t *src, int32_t *hist, ptrdiff_t len)
Definition: dcadsp.h:61
int x96_high_res
X96 high resolution flag.
Definition: dca_core.h:147
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:106
int ext_audio_mask
Bit mask of fully decoded core extensions.
Definition: dca_core.h:127
AVMatrixEncoding
int predictor_history
Predictor history flag switch.
Definition: dca_core.h:91
const int32_t ff_dca_fir_32bands_perfect_fixed[512]
Definition: dcadata.c:8070
int32_t output_history_lfe_fixed
LFE PCM history for X96 filter.
Definition: dca_core.h:175
int8_t quant_index_sel[DCA_CHANNELS][DCA_CODE_BOOKS]
Quantization index codebook select.
Definition: dca_core.h:108
const float ff_dca_fir_32bands_perfect[512]
Definition: dcadata.c:6289
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:231
const uint32_t ff_dca_scale_factor_adj[4]
Definition: dcadata.c:4207
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
FFTContext imdct[2]
Definition: dca_core.h:166
static int32_t norm13(int64_t a)
Definition: dcamath.h:40
for(j=16;j >0;--j)
int npcmblocks
Number of PCM sample blocks.
Definition: dca_core.h:79
Definition: dcahuff.h:33