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