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