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 
775 {
776  int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
777  int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
778  int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
779  unsigned int size = s->subband_size;
780  int ch, band;
781 
782  // Reallocate subband sample buffer
783  av_fast_mallocz(&s->subband_buffer, &s->subband_size,
784  (nframesamples + nlfesamples) * sizeof(int32_t));
785  if (!s->subband_buffer)
786  return AVERROR(ENOMEM);
787 
788  if (size != s->subband_size) {
789  for (ch = 0; ch < DCA_CHANNELS; ch++)
790  for (band = 0; band < DCA_SUBBANDS; band++)
791  s->subband_samples[ch][band] = s->subband_buffer +
792  (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
793  s->lfe_samples = s->subband_buffer + nframesamples;
794  }
795 
796  if (!s->predictor_history)
798 
799  return 0;
800 }
801 
802 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
803 {
804  int sf, ch, ret, band, sub_pos, lfe_pos;
805 
806  if ((ret = parse_coding_header(s, header, xch_base)) < 0)
807  return ret;
808 
809  for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
810  if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
811  return ret;
812  if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
813  return ret;
814  }
815 
816  for (ch = xch_base; ch < s->nchannels; ch++) {
817  // Determine number of active subbands for this channel
818  int nsubbands = s->nsubbands[ch];
819  if (s->joint_intensity_index[ch])
820  nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
821 
822  // Update history for ADPCM
823  for (band = 0; band < nsubbands; band++) {
824  int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
825  AV_COPY128(samples, samples + s->npcmblocks);
826  }
827 
828  // Clear inactive subbands
829  for (; band < DCA_SUBBANDS; band++) {
830  int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
831  memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
832  }
833  }
834 
835  return 0;
836 }
837 
839 {
840  int ret;
841 
842  if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
843  av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
844  return AVERROR_INVALIDDATA;
845  }
846 
847  if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
848  return ret;
849 
850  // Seek to the end of core frame, don't trust XCH frame size
851  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
852  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
853  return AVERROR_INVALIDDATA;
854  }
855 
856  return 0;
857 }
858 
860 {
861  int xxch_nchsets, xxch_frame_size;
862  int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
863 
864  // XXCH sync word
865  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
866  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
867  return AVERROR_INVALIDDATA;
868  }
869 
870  // XXCH frame header length
871  header_size = get_bits(&s->gb, 6) + 1;
872 
873  // Check XXCH frame header CRC
874  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
875  av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
876  return AVERROR_INVALIDDATA;
877  }
878 
879  // CRC presence flag for channel set header
880  s->xxch_crc_present = get_bits1(&s->gb);
881 
882  // Number of bits for loudspeaker mask
883  s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
884  if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
885  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
886  return AVERROR_INVALIDDATA;
887  }
888 
889  // Number of channel sets
890  xxch_nchsets = get_bits(&s->gb, 2) + 1;
891  if (xxch_nchsets > 1) {
892  avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
893  return AVERROR_PATCHWELCOME;
894  }
895 
896  // Channel set 0 data byte size
897  xxch_frame_size = get_bits(&s->gb, 14) + 1;
898 
899  // Core loudspeaker activity mask
900  s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
901 
902  // Validate the core mask
903  mask = s->ch_mask;
904 
905  if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
907 
908  if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
910 
911  if (mask != s->xxch_core_mask) {
912  av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
913  return AVERROR_INVALIDDATA;
914  }
915 
916  // Reserved
917  // Byte align
918  // CRC16 of XXCH frame header
919  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
920  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
921  return AVERROR_INVALIDDATA;
922  }
923 
924  // Parse XXCH channel set 0
925  if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
926  return ret;
927 
928  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
929  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
930  return AVERROR_INVALIDDATA;
931  }
932 
933  return 0;
934 }
935 
936 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
937  int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
938 {
939  int xbr_nabits[DCA_CHANNELS];
940  int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
941  int xbr_scale_nbits[DCA_CHANNELS];
942  int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
943  int ssf, ch, band, ofs;
944 
945  // Check number of subband samples in this subframe
946  if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
947  av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
948  return AVERROR_INVALIDDATA;
949  }
950 
951  if (get_bits_left(&s->gb) < 0)
952  return AVERROR_INVALIDDATA;
953 
954  // Number of bits for XBR bit allocation index
955  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
956  xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
957 
958  // XBR bit allocation index
959  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
960  for (band = 0; band < xbr_nsubbands[ch]; band++) {
961  xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
962  if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
963  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
964  return AVERROR_INVALIDDATA;
965  }
966  }
967  }
968 
969  // Number of bits for scale indices
970  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
971  xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
972  if (!xbr_scale_nbits[ch]) {
973  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
974  return AVERROR_INVALIDDATA;
975  }
976  }
977 
978  // XBR scale factors
979  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
980  const uint32_t *scale_table;
981  int scale_size;
982 
983  // Select the root square table
984  if (s->scale_factor_sel[ch] > 5) {
987  } else {
990  }
991 
992  // Parse scale factor indices and look up scale factors from the root
993  // square table
994  for (band = 0; band < xbr_nsubbands[ch]; band++) {
995  if (xbr_bit_allocation[ch][band]) {
996  int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
997  if (scale_index >= scale_size) {
998  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
999  return AVERROR_INVALIDDATA;
1000  }
1001  xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1002  if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1003  scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1004  if (scale_index >= scale_size) {
1005  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1006  return AVERROR_INVALIDDATA;
1007  }
1008  xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1009  }
1010  }
1011  }
1012  }
1013 
1014  // Audio data
1015  for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1016  for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1017  if (get_bits_left(&s->gb) < 0)
1018  return AVERROR_INVALIDDATA;
1019 
1020  for (band = 0; band < xbr_nsubbands[ch]; band++) {
1021  int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1022  int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1023 
1024  // Extract bits from the bit stream
1025  if (abits > 7) {
1026  // No further encoding
1027  get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1028  } else if (abits > 0) {
1029  // Block codes
1030  if ((ret = parse_block_codes(s, audio, abits)) < 0)
1031  return ret;
1032  } else {
1033  // No bits allocated
1034  continue;
1035  }
1036 
1037  // Look up quantization step size
1038  step_size = ff_dca_lossless_quant[abits];
1039 
1040  // Identify transient location
1041  if (xbr_transition_mode)
1042  trans_ssf = s->transition_mode[sf][ch][band];
1043  else
1044  trans_ssf = 0;
1045 
1046  // Determine proper scale factor
1047  if (trans_ssf == 0 || ssf < trans_ssf)
1048  scale = xbr_scale_factors[ch][band][0];
1049  else
1050  scale = xbr_scale_factors[ch][band][1];
1051 
1052  ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
1053  audio, step_size, scale, 1, DCA_SUBBAND_SAMPLES);
1054  }
1055  }
1056 
1057  // DSYNC
1058  if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1059  av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1060  return AVERROR_INVALIDDATA;
1061  }
1062 
1063  ofs += DCA_SUBBAND_SAMPLES;
1064  }
1065 
1066  // Advance subband sample pointer for the next subframe
1067  *sub_pos = ofs;
1068  return 0;
1069 }
1070 
1072 {
1073  int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1074  int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1075  int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1076  int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1077  int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1078 
1079  // XBR sync word
1080  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1081  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1082  return AVERROR_INVALIDDATA;
1083  }
1084 
1085  // XBR frame header length
1086  header_size = get_bits(&s->gb, 6) + 1;
1087 
1088  // Check XBR frame header CRC
1089  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1090  av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1091  return AVERROR_INVALIDDATA;
1092  }
1093 
1094  // Number of channel sets
1095  xbr_nchsets = get_bits(&s->gb, 2) + 1;
1096 
1097  // Channel set data byte size
1098  for (i = 0; i < xbr_nchsets; i++)
1099  xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1100 
1101  // Transition mode flag
1102  xbr_transition_mode = get_bits1(&s->gb);
1103 
1104  // Channel set headers
1105  for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1106  xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1107  xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1108  for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1109  xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1110  if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1111  av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1112  return AVERROR_INVALIDDATA;
1113  }
1114  }
1115  }
1116 
1117  // Reserved
1118  // Byte align
1119  // CRC16 of XBR frame header
1120  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1121  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1122  return AVERROR_INVALIDDATA;
1123  }
1124 
1125  // Channel set data
1126  for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1127  header_pos = get_bits_count(&s->gb);
1128 
1129  if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1130  int sf, sub_pos;
1131 
1132  for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1133  if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1134  xbr_base_ch + xbr_nchannels[i],
1135  xbr_nsubbands, xbr_transition_mode,
1136  sf, &sub_pos)) < 0)
1137  return ret;
1138  }
1139  }
1140 
1141  xbr_base_ch += xbr_nchannels[i];
1142 
1143  if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1144  av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1145  return AVERROR_INVALIDDATA;
1146  }
1147  }
1148 
1149  return 0;
1150 }
1151 
1152 // Modified ISO/IEC 9899 linear congruential generator
1153 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1155 {
1156  s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1157  return (s->x96_rand & 0x7fffffff) - 0x40000000;
1158 }
1159 
1160 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1161 {
1162  int n, ssf, ch, band, ofs;
1163 
1164  // Check number of subband samples in this subframe
1165  int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1166  if (*sub_pos + nsamples > s->npcmblocks) {
1167  av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1168  return AVERROR_INVALIDDATA;
1169  }
1170 
1171  if (get_bits_left(&s->gb) < 0)
1172  return AVERROR_INVALIDDATA;
1173 
1174  // VQ encoded or unallocated subbands
1175  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1176  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1177  // Get the sample pointer and scale factor
1178  int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1179  int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1180 
1181  switch (s->bit_allocation[ch][band]) {
1182  case 0: // No bits allocated for subband
1183  if (scale <= 1)
1184  memset(samples, 0, nsamples * sizeof(int32_t));
1185  else for (n = 0; n < nsamples; n++)
1186  // Generate scaled random samples
1187  samples[n] = mul31(rand_x96(s), scale);
1188  break;
1189 
1190  case 1: // VQ encoded subband
1191  for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1192  // Extract the VQ address from the bit stream and look up
1193  // the VQ code book for up to 16 subband samples
1194  const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1195  // Scale and take the samples
1196  for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1197  *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1198  }
1199  break;
1200  }
1201  }
1202  }
1203 
1204  // Audio data
1205  for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1206  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1207  if (get_bits_left(&s->gb) < 0)
1208  return AVERROR_INVALIDDATA;
1209 
1210  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1211  int ret, abits = s->bit_allocation[ch][band] - 1;
1212  int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1213 
1214  // Not VQ encoded or unallocated subbands
1215  if (abits < 1)
1216  continue;
1217 
1218  // Extract bits from the bit stream
1219  if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1220  return ret;
1221 
1222  // Select quantization step size table and look up quantization
1223  // step size
1224  if (s->bit_rate == 3)
1225  step_size = ff_dca_lossless_quant[abits];
1226  else
1227  step_size = ff_dca_lossy_quant[abits];
1228 
1229  // Get the scale factor
1230  scale = s->scale_factors[ch][band >> 1][band & 1];
1231 
1232  ff_dca_core_dequantize(s->x96_subband_samples[ch][band] + ofs,
1233  audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
1234  }
1235  }
1236 
1237  // DSYNC
1238  if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1239  av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1240  return AVERROR_INVALIDDATA;
1241  }
1242 
1243  ofs += DCA_SUBBAND_SAMPLES;
1244  }
1245 
1246  // Inverse ADPCM
1247  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1248  inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1249  s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1250  *sub_pos, nsamples);
1251  }
1252 
1253  // Joint subband coding
1254  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1255  int src_ch = s->joint_intensity_index[ch] - 1;
1256  if (src_ch >= 0) {
1257  s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1258  s->joint_scale_factors[ch], s->nsubbands[ch],
1259  s->nsubbands[src_ch], *sub_pos, nsamples);
1260  }
1261  }
1262 
1263  // Advance subband sample pointer for the next subframe
1264  *sub_pos = ofs;
1265  return 0;
1266 }
1267 
1269 {
1270  int ch, band;
1271 
1272  // Erase ADPCM history from previous frame if
1273  // predictor history switch was disabled
1274  for (ch = 0; ch < DCA_CHANNELS; ch++)
1275  for (band = 0; band < DCA_SUBBANDS_X96; band++)
1276  AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1277 }
1278 
1280 {
1281  int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1282  int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1283  unsigned int size = s->x96_subband_size;
1284  int ch, band;
1285 
1286  // Reallocate subband sample buffer
1287  av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1288  nframesamples * sizeof(int32_t));
1289  if (!s->x96_subband_buffer)
1290  return AVERROR(ENOMEM);
1291 
1292  if (size != s->x96_subband_size) {
1293  for (ch = 0; ch < DCA_CHANNELS; ch++)
1294  for (band = 0; band < DCA_SUBBANDS_X96; band++)
1295  s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1296  (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1297  }
1298 
1299  if (!s->predictor_history)
1301 
1302  return 0;
1303 }
1304 
1305 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1306 {
1307  int ch, band, ret;
1308 
1309  if (get_bits_left(&s->gb) < 0)
1310  return AVERROR_INVALIDDATA;
1311 
1312  // Prediction mode
1313  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1314  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1315  s->prediction_mode[ch][band] = get_bits1(&s->gb);
1316 
1317  // Prediction coefficients VQ address
1318  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1319  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1320  if (s->prediction_mode[ch][band])
1321  s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1322 
1323  // Bit allocation index
1324  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1325  int sel = s->bit_allocation_sel[ch];
1326  int abits = 0;
1327 
1328  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1329  // If Huffman code was used, the difference of abits was encoded
1330  if (sel < 7)
1331  abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res][sel]);
1332  else
1333  abits = get_bits(&s->gb, 3 + s->x96_high_res);
1334 
1335  if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1336  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1337  return AVERROR_INVALIDDATA;
1338  }
1339 
1340  s->bit_allocation[ch][band] = abits;
1341  }
1342  }
1343 
1344  // Scale factors
1345  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1346  int sel = s->scale_factor_sel[ch];
1347  int scale_index = 0;
1348 
1349  // Extract scales for subbands which are transmitted even for
1350  // unallocated subbands
1351  for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1352  if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1353  return ret;
1354  s->scale_factors[ch][band >> 1][band & 1] = ret;
1355  }
1356  }
1357 
1358  // Joint subband codebook select
1359  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1360  if (s->joint_intensity_index[ch]) {
1361  s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1362  if (s->joint_scale_sel[ch] == 7) {
1363  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1364  return AVERROR_INVALIDDATA;
1365  }
1366  }
1367  }
1368 
1369  // Scale factors for joint subband coding
1370  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1371  int src_ch = s->joint_intensity_index[ch] - 1;
1372  if (src_ch >= 0) {
1373  int sel = s->joint_scale_sel[ch];
1374  for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1375  if ((ret = parse_joint_scale(s, sel)) < 0)
1376  return ret;
1377  s->joint_scale_factors[ch][band] = ret;
1378  }
1379  }
1380  }
1381 
1382  // Side information CRC check word
1383  if (s->crc_present)
1384  skip_bits(&s->gb, 16);
1385 
1386  return 0;
1387 }
1388 
1389 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1390 {
1391  int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1392 
1393  if (get_bits_left(&s->gb) < 0)
1394  return AVERROR_INVALIDDATA;
1395 
1396  if (exss) {
1397  // Channel set header length
1398  header_size = get_bits(&s->gb, 7) + 1;
1399 
1400  // Check CRC
1401  if (s->x96_crc_present
1402  && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1403  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1404  return AVERROR_INVALIDDATA;
1405  }
1406  }
1407 
1408  // High resolution flag
1409  s->x96_high_res = get_bits1(&s->gb);
1410 
1411  // First encoded subband
1412  if (s->x96_rev_no < 8) {
1413  s->x96_subband_start = get_bits(&s->gb, 5);
1414  if (s->x96_subband_start > 27) {
1415  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1416  return AVERROR_INVALIDDATA;
1417  }
1418  } else {
1419  s->x96_subband_start = DCA_SUBBANDS;
1420  }
1421 
1422  // Subband activity count
1423  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1424  s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1425  if (s->nsubbands[ch] < DCA_SUBBANDS) {
1426  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1427  return AVERROR_INVALIDDATA;
1428  }
1429  }
1430 
1431  // Joint intensity coding index
1432  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1433  if ((n = get_bits(&s->gb, 3)) && xch_base)
1434  n += xch_base - 1;
1435  if (n > s->x96_nchannels) {
1436  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1437  return AVERROR_INVALIDDATA;
1438  }
1439  s->joint_intensity_index[ch] = n;
1440  }
1441 
1442  // Scale factor code book
1443  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1444  s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1445  if (s->scale_factor_sel[ch] >= 6) {
1446  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1447  return AVERROR_INVALIDDATA;
1448  }
1449  }
1450 
1451  // Bit allocation quantizer select
1452  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1453  s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1454 
1455  // Quantization index codebook select
1456  for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1457  for (ch = xch_base; ch < s->x96_nchannels; ch++)
1458  s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
1459 
1460  if (exss) {
1461  // Reserved
1462  // Byte align
1463  // CRC16 of channel set header
1464  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1465  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1466  return AVERROR_INVALIDDATA;
1467  }
1468  } else {
1469  if (s->crc_present)
1470  skip_bits(&s->gb, 16);
1471  }
1472 
1473  return 0;
1474 }
1475 
1476 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1477 {
1478  int sf, ch, ret, band, sub_pos;
1479 
1480  if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1481  return ret;
1482 
1483  for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1484  if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1485  return ret;
1486  if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1487  return ret;
1488  }
1489 
1490  for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1491  // Determine number of active subbands for this channel
1492  int nsubbands = s->nsubbands[ch];
1493  if (s->joint_intensity_index[ch])
1494  nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1495 
1496  // Update history for ADPCM and clear inactive subbands
1497  for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1498  int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1499  if (band >= s->x96_subband_start && band < nsubbands)
1500  AV_COPY128(samples, samples + s->npcmblocks);
1501  else
1502  memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1503  }
1504  }
1505 
1506  return 0;
1507 }
1508 
1510 {
1511  int ret;
1512 
1513  // Revision number
1514  s->x96_rev_no = get_bits(&s->gb, 4);
1515  if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1516  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1517  return AVERROR_INVALIDDATA;
1518  }
1519 
1520  s->x96_crc_present = 0;
1521  s->x96_nchannels = s->nchannels;
1522 
1523  if ((ret = alloc_x96_sample_buffer(s)) < 0)
1524  return ret;
1525 
1526  if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1527  return ret;
1528 
1529  // Seek to the end of core frame
1530  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1531  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1532  return AVERROR_INVALIDDATA;
1533  }
1534 
1535  return 0;
1536 }
1537 
1539 {
1540  int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1541  int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1542  int x96_nchsets, x96_base_ch;
1543  int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1544 
1545  // X96 sync word
1546  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1547  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1548  return AVERROR_INVALIDDATA;
1549  }
1550 
1551  // X96 frame header length
1552  header_size = get_bits(&s->gb, 6) + 1;
1553 
1554  // Check X96 frame header CRC
1555  if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1556  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1557  return AVERROR_INVALIDDATA;
1558  }
1559 
1560  // Revision number
1561  s->x96_rev_no = get_bits(&s->gb, 4);
1562  if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1563  av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1564  return AVERROR_INVALIDDATA;
1565  }
1566 
1567  // CRC presence flag for channel set header
1568  s->x96_crc_present = get_bits1(&s->gb);
1569 
1570  // Number of channel sets
1571  x96_nchsets = get_bits(&s->gb, 2) + 1;
1572 
1573  // Channel set data byte size
1574  for (i = 0; i < x96_nchsets; i++)
1575  x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1576 
1577  // Number of channels in channel set
1578  for (i = 0; i < x96_nchsets; i++)
1579  x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1580 
1581  // Reserved
1582  // Byte align
1583  // CRC16 of X96 frame header
1584  if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1585  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1586  return AVERROR_INVALIDDATA;
1587  }
1588 
1589  if ((ret = alloc_x96_sample_buffer(s)) < 0)
1590  return ret;
1591 
1592  // Channel set data
1593  s->x96_nchannels = 0;
1594  for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1595  header_pos = get_bits_count(&s->gb);
1596 
1597  if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1598  s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1599  if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1600  return ret;
1601  }
1602 
1603  x96_base_ch += x96_nchannels[i];
1604 
1605  if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1606  av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1607  return AVERROR_INVALIDDATA;
1608  }
1609  }
1610 
1611  return 0;
1612 }
1613 
1615 {
1616  int aux_pos;
1617 
1618  if (get_bits_left(&s->gb) < 0)
1619  return AVERROR_INVALIDDATA;
1620 
1621  // Auxiliary data byte count (can't be trusted)
1622  skip_bits(&s->gb, 6);
1623 
1624  // 4-byte align
1625  skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1626 
1627  // Auxiliary data sync word
1628  if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1629  av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1630  return AVERROR_INVALIDDATA;
1631  }
1632 
1633  aux_pos = get_bits_count(&s->gb);
1634 
1635  // Auxiliary decode time stamp flag
1636  if (get_bits1(&s->gb))
1637  skip_bits_long(&s->gb, 47);
1638 
1639  // Auxiliary dynamic downmix flag
1640  if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1641  int i, m, n;
1642 
1643  // Auxiliary primary channel downmix type
1644  s->prim_dmix_type = get_bits(&s->gb, 3);
1645  if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1646  av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1647  return AVERROR_INVALIDDATA;
1648  }
1649 
1650  // Size of downmix coefficients matrix
1651  m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1652  n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1653 
1654  // Dynamic downmix code coefficients
1655  for (i = 0; i < m * n; i++) {
1656  int code = get_bits(&s->gb, 9);
1657  int sign = (code >> 8) - 1;
1658  unsigned int index = code & 0xff;
1659  if (index >= FF_DCA_DMIXTABLE_SIZE) {
1660  av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1661  return AVERROR_INVALIDDATA;
1662  }
1663  s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1664  }
1665  }
1666 
1667  // Byte align
1668  skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1669 
1670  // CRC16 of auxiliary data
1671  skip_bits(&s->gb, 16);
1672 
1673  // Check CRC
1674  if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1675  av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1676  return AVERROR_INVALIDDATA;
1677  }
1678 
1679  return 0;
1680 }
1681 
1683 {
1684  DCAContext *dca = s->avctx->priv_data;
1685  int ret = -1;
1686 
1687  // Time code stamp
1688  if (s->ts_present)
1689  skip_bits_long(&s->gb, 32);
1690 
1691  // Auxiliary data
1692  if (s->aux_present && (ret = parse_aux_data(s)) < 0
1693  && (s->avctx->err_recognition & AV_EF_EXPLODE))
1694  return ret;
1695 
1696  if (ret < 0)
1697  s->prim_dmix_embedded = 0;
1698 
1699  // Core extensions
1700  if (s->ext_audio_present && !dca->core_only) {
1701  int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1702  int last_pos = get_bits_count(&s->gb) / 32;
1703  int size, dist;
1704  uint32_t w1, w2 = 0;
1705 
1706  // Search for extension sync words aligned on 4-byte boundary. Search
1707  // must be done backwards from the end of core frame to work around
1708  // sync word aliasing issues.
1709  switch (s->ext_audio_type) {
1710  case DCA_EXT_AUDIO_XCH:
1711  if (dca->request_channel_layout)
1712  break;
1713 
1714  // The distance between XCH sync word and end of the core frame
1715  // must be equal to XCH frame size. Off by one error is allowed for
1716  // compatibility with legacy bitstreams. Minimum XCH frame size is
1717  // 96 bytes. AMODE and PCHS are further checked to reduce
1718  // probability of alias sync detection.
1719  for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1720  w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1721  if (w1 == DCA_SYNCWORD_XCH) {
1722  size = (w2 >> 22) + 1;
1723  dist = s->frame_size - sync_pos * 4;
1724  if (size >= 96
1725  && (size == dist || size - 1 == dist)
1726  && (w2 >> 15 & 0x7f) == 0x08) {
1727  s->xch_pos = sync_pos * 32 + 49;
1728  break;
1729  }
1730  }
1731  }
1732 
1733  if (!s->xch_pos) {
1734  av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1735  if (s->avctx->err_recognition & AV_EF_EXPLODE)
1736  return AVERROR_INVALIDDATA;
1737  }
1738  break;
1739 
1740  case DCA_EXT_AUDIO_X96:
1741  // The distance between X96 sync word and end of the core frame
1742  // must be equal to X96 frame size. Minimum X96 frame size is 96
1743  // bytes.
1744  for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1745  w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1746  if (w1 == DCA_SYNCWORD_X96) {
1747  size = (w2 >> 20) + 1;
1748  dist = s->frame_size - sync_pos * 4;
1749  if (size >= 96 && size == dist) {
1750  s->x96_pos = sync_pos * 32 + 44;
1751  break;
1752  }
1753  }
1754  }
1755 
1756  if (!s->x96_pos) {
1757  av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1758  if (s->avctx->err_recognition & AV_EF_EXPLODE)
1759  return AVERROR_INVALIDDATA;
1760  }
1761  break;
1762 
1763  case DCA_EXT_AUDIO_XXCH:
1764  if (dca->request_channel_layout)
1765  break;
1766 
1767  // XXCH frame header CRC must be valid. Minimum XXCH frame header
1768  // size is 11 bytes.
1769  for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1770  w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1771  if (w1 == DCA_SYNCWORD_XXCH) {
1772  size = (w2 >> 26) + 1;
1773  dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1774  if (size >= 11 && size <= dist &&
1775  !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1776  (sync_pos + 1) * 4, size - 4)) {
1777  s->xxch_pos = sync_pos * 32;
1778  break;
1779  }
1780  }
1781  }
1782 
1783  if (!s->xxch_pos) {
1784  av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1785  if (s->avctx->err_recognition & AV_EF_EXPLODE)
1786  return AVERROR_INVALIDDATA;
1787  }
1788  break;
1789  }
1790  }
1791 
1792  return 0;
1793 }
1794 
1795 int ff_dca_core_parse(DCACoreDecoder *s, const uint8_t *data, int size)
1796 {
1797  int ret;
1798 
1799  s->ext_audio_mask = 0;
1800  s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1801 
1802  if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1803  return ret;
1804  s->gb_in = s->gb;
1805 
1806  if ((ret = parse_frame_header(s)) < 0)
1807  return ret;
1808  if ((ret = alloc_sample_buffer(s)) < 0)
1809  return ret;
1810  if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1811  return ret;
1812  if ((ret = parse_optional_info(s)) < 0)
1813  return ret;
1814 
1815  // Workaround for DTS in WAV
1816  if (s->frame_size > size)
1817  s->frame_size = size;
1818 
1819  if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1820  av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1821  if (s->avctx->err_recognition & AV_EF_EXPLODE)
1822  return AVERROR_INVALIDDATA;
1823  }
1824 
1825  return 0;
1826 }
1827 
1829 {
1830  AVCodecContext *avctx = s->avctx;
1831  DCAContext *dca = avctx->priv_data;
1832  int exss_mask = asset ? asset->extension_mask : 0;
1833  int ret = 0, ext = 0;
1834 
1835  // Parse (X)XCH unless downmixing
1836  if (!dca->request_channel_layout) {
1837  if (exss_mask & DCA_EXSS_XXCH) {
1838  if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1839  return ret;
1840  ret = parse_xxch_frame(s);
1841  ext = DCA_EXSS_XXCH;
1842  } else if (s->xxch_pos) {
1843  s->gb = s->gb_in;
1844  skip_bits_long(&s->gb, s->xxch_pos);
1845  ret = parse_xxch_frame(s);
1846  ext = DCA_CSS_XXCH;
1847  } else if (s->xch_pos) {
1848  s->gb = s->gb_in;
1849  skip_bits_long(&s->gb, s->xch_pos);
1850  ret = parse_xch_frame(s);
1851  ext = DCA_CSS_XCH;
1852  }
1853 
1854  // Revert to primary channel set in case (X)XCH parsing fails
1855  if (ret < 0) {
1856  if (avctx->err_recognition & AV_EF_EXPLODE)
1857  return ret;
1858  s->nchannels = ff_dca_channels[s->audio_mode];
1859  s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1860  if (s->lfe_present)
1861  s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1862  } else {
1863  s->ext_audio_mask |= ext;
1864  }
1865  }
1866 
1867  // Parse XBR
1868  if (exss_mask & DCA_EXSS_XBR) {
1869  if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1870  return ret;
1871  if ((ret = parse_xbr_frame(s)) < 0) {
1872  if (avctx->err_recognition & AV_EF_EXPLODE)
1873  return ret;
1874  } else {
1875  s->ext_audio_mask |= DCA_EXSS_XBR;
1876  }
1877  }
1878 
1879  // Parse X96 unless decoding XLL
1880  if (!(dca->packet & DCA_PACKET_XLL)) {
1881  if (exss_mask & DCA_EXSS_X96) {
1882  if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1883  return ret;
1884  if ((ret = parse_x96_frame_exss(s)) < 0) {
1885  if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1886  return ret;
1887  } else {
1888  s->ext_audio_mask |= DCA_EXSS_X96;
1889  }
1890  } else if (s->x96_pos) {
1891  s->gb = s->gb_in;
1892  skip_bits_long(&s->gb, s->x96_pos);
1893  if ((ret = parse_x96_frame(s)) < 0) {
1894  if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1895  return ret;
1896  } else {
1897  s->ext_audio_mask |= DCA_CSS_X96;
1898  }
1899  }
1900  }
1901 
1902  return 0;
1903 }
1904 
1906 {
1907  int pos, spkr;
1908 
1909  // Try to map this channel to core first
1910  pos = ff_dca_channels[s->audio_mode];
1911  if (ch < pos) {
1912  spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
1913  if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1914  if (s->xxch_core_mask & (1U << spkr))
1915  return spkr;
1916  if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1917  return DCA_SPEAKER_Lss;
1918  if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1919  return DCA_SPEAKER_Rss;
1920  return -1;
1921  }
1922  return spkr;
1923  }
1924 
1925  // Then XCH
1926  if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
1927  return DCA_SPEAKER_Cs;
1928 
1929  // Then XXCH
1930  if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1931  for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
1932  if (s->xxch_spkr_mask & (1U << spkr))
1933  if (pos++ == ch)
1934  return spkr;
1935  }
1936 
1937  // No mapping
1938  return -1;
1939 }
1940 
1942 {
1943  memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
1944  s->output_history_lfe_fixed = 0;
1945  s->output_history_lfe_float = 0;
1946 }
1947 
1949 {
1950  if (s->filter_mode != mode) {
1952  s->filter_mode = mode;
1953  }
1954 }
1955 
1957 {
1958  int n, ch, spkr, nsamples, x96_nchannels = 0;
1959  const int32_t *filter_coeff;
1960  int32_t *ptr;
1961 
1962  // Externally set x96_synth flag implies that X96 synthesis should be
1963  // enabled, yet actual X96 subband data should be discarded. This is a
1964  // special case for lossless residual decoder that ignores X96 data if
1965  // present.
1966  if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
1967  x96_nchannels = s->x96_nchannels;
1968  x96_synth = 1;
1969  }
1970  if (x96_synth < 0)
1971  x96_synth = 0;
1972 
1973  s->output_rate = s->sample_rate << x96_synth;
1974  s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
1975 
1976  // Reallocate PCM output buffer
1977  av_fast_malloc(&s->output_buffer, &s->output_size,
1978  nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
1979  if (!s->output_buffer)
1980  return AVERROR(ENOMEM);
1981 
1982  ptr = (int32_t *)s->output_buffer;
1983  for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
1984  if (s->ch_mask & (1U << spkr)) {
1985  s->output_samples[spkr] = ptr;
1986  ptr += nsamples;
1987  } else {
1988  s->output_samples[spkr] = NULL;
1989  }
1990  }
1991 
1992  // Handle change of filtering mode
1993  set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
1994 
1995  // Select filter
1996  if (x96_synth)
1997  filter_coeff = ff_dca_fir_64bands_fixed;
1998  else if (s->filter_perfect)
1999  filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2000  else
2001  filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2002 
2003  // Filter primary channels
2004  for (ch = 0; ch < s->nchannels; ch++) {
2005  // Map this primary channel to speaker
2006  spkr = map_prm_ch_to_spkr(s, ch);
2007  if (spkr < 0)
2008  return AVERROR(EINVAL);
2009 
2010  // Filter bank reconstruction
2011  s->dcadsp->sub_qmf_fixed[x96_synth](
2012  &s->synth,
2013  &s->dcadct,
2014  s->output_samples[spkr],
2015  s->subband_samples[ch],
2016  ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2017  s->dcadsp_data[ch].u.fix.hist1,
2018  &s->dcadsp_data[ch].offset,
2019  s->dcadsp_data[ch].u.fix.hist2,
2020  filter_coeff,
2021  s->npcmblocks);
2022  }
2023 
2024  // Filter LFE channel
2025  if (s->lfe_present) {
2026  int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2027  int nlfesamples = s->npcmblocks >> 1;
2028 
2029  // Check LFF
2030  if (s->lfe_present == DCA_LFE_FLAG_128) {
2031  av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2032  return AVERROR(EINVAL);
2033  }
2034 
2035  // Offset intermediate buffer for X96
2036  if (x96_synth)
2037  samples += nsamples / 2;
2038 
2039  // Interpolate LFE channel
2040  s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2041  ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2042 
2043  if (x96_synth) {
2044  // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2045  // (47.6 - 48.0 kHz) components of interpolation image
2046  s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2047  samples, &s->output_history_lfe_fixed,
2048  nsamples / 2);
2049 
2050  }
2051 
2052  // Update LFE history
2053  for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2054  s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2055  }
2056 
2057  return 0;
2058 }
2059 
2061 {
2062  AVCodecContext *avctx = s->avctx;
2063  DCAContext *dca = avctx->priv_data;
2064  int i, n, ch, ret, spkr, nsamples;
2065 
2066  // Don't filter twice when falling back from XLL
2067  if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2068  return ret;
2069 
2070  avctx->sample_rate = s->output_rate;
2071  avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2072  avctx->bits_per_raw_sample = 24;
2073 
2074  frame->nb_samples = nsamples = s->npcmsamples;
2075  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2076  return ret;
2077 
2078  // Undo embedded XCH downmix
2079  if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2080  && s->audio_mode >= DCA_AMODE_2F2R) {
2081  s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2082  s->output_samples[DCA_SPEAKER_Rs],
2083  s->output_samples[DCA_SPEAKER_Cs],
2084  nsamples);
2085 
2086  }
2087 
2088  // Undo embedded XXCH downmix
2089  if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2090  && s->xxch_dmix_embedded) {
2091  int scale_inv = s->xxch_dmix_scale_inv;
2092  int *coeff_ptr = s->xxch_dmix_coeff;
2093  int xch_base = ff_dca_channels[s->audio_mode];
2094  av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2095 
2096  // Undo embedded core downmix pre-scaling
2097  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2098  if (s->xxch_core_mask & (1U << spkr)) {
2099  s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2100  scale_inv, nsamples);
2101  }
2102  }
2103 
2104  // Undo downmix
2105  for (ch = xch_base; ch < s->nchannels; ch++) {
2106  int src_spkr = map_prm_ch_to_spkr(s, ch);
2107  if (src_spkr < 0)
2108  return AVERROR(EINVAL);
2109  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2110  if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2111  int coeff = mul16(*coeff_ptr++, scale_inv);
2112  if (coeff) {
2113  s->dcadsp->dmix_sub(s->output_samples[spkr ],
2114  s->output_samples[src_spkr],
2115  coeff, nsamples);
2116  }
2117  }
2118  }
2119  }
2120  }
2121 
2122  if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2123  // Front sum/difference decoding
2124  if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
2125  || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
2126  s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2127  s->output_samples[DCA_SPEAKER_R],
2128  nsamples);
2129  }
2130 
2131  // Surround sum/difference decoding
2132  if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
2133  s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2134  s->output_samples[DCA_SPEAKER_Rs],
2135  nsamples);
2136  }
2137  }
2138 
2139  // Downmix primary channel set to stereo
2140  if (s->request_mask != s->ch_mask) {
2142  s->output_samples,
2143  s->prim_dmix_coeff,
2144  nsamples, s->ch_mask);
2145  }
2146 
2147  for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
2148  int32_t *samples = s->output_samples[s->ch_remap[i]];
2149  int32_t *plane = (int32_t *)frame->extended_data[i];
2150  for (n = 0; n < nsamples; n++)
2151  plane[n] = clip23(samples[n]) * (1 << 8);
2152  }
2153 
2154  return 0;
2155 }
2156 
2158 {
2159  AVCodecContext *avctx = s->avctx;
2160  int x96_nchannels = 0, x96_synth = 0;
2161  int i, n, ch, ret, spkr, nsamples, nchannels;
2162  float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2163  const float *filter_coeff;
2164 
2165  if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2166  x96_nchannels = s->x96_nchannels;
2167  x96_synth = 1;
2168  }
2169 
2170  avctx->sample_rate = s->sample_rate << x96_synth;
2171  avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2172  avctx->bits_per_raw_sample = 0;
2173 
2174  frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2175  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2176  return ret;
2177 
2178  // Build reverse speaker to channel mapping
2179  for (i = 0; i < avctx->ch_layout.nb_channels; i++)
2180  output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2181 
2182  // Allocate space for extra channels
2183  nchannels = av_popcount(s->ch_mask) - avctx->ch_layout.nb_channels;
2184  if (nchannels > 0) {
2185  av_fast_malloc(&s->output_buffer, &s->output_size,
2186  nsamples * nchannels * sizeof(float));
2187  if (!s->output_buffer)
2188  return AVERROR(ENOMEM);
2189 
2190  ptr = (float *)s->output_buffer;
2191  for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2192  if (!(s->ch_mask & (1U << spkr)))
2193  continue;
2194  if (output_samples[spkr])
2195  continue;
2196  output_samples[spkr] = ptr;
2197  ptr += nsamples;
2198  }
2199  }
2200 
2201  // Handle change of filtering mode
2202  set_filter_mode(s, x96_synth);
2203 
2204  // Select filter
2205  if (x96_synth)
2206  filter_coeff = ff_dca_fir_64bands;
2207  else if (s->filter_perfect)
2208  filter_coeff = ff_dca_fir_32bands_perfect;
2209  else
2210  filter_coeff = ff_dca_fir_32bands_nonperfect;
2211 
2212  // Filter primary channels
2213  for (ch = 0; ch < s->nchannels; ch++) {
2214  // Map this primary channel to speaker
2215  spkr = map_prm_ch_to_spkr(s, ch);
2216  if (spkr < 0)
2217  return AVERROR(EINVAL);
2218 
2219  // Filter bank reconstruction
2220  s->dcadsp->sub_qmf_float[x96_synth](
2221  &s->synth,
2222  s->imdct[x96_synth],
2223  s->imdct_fn[x96_synth],
2224  output_samples[spkr],
2225  s->subband_samples[ch],
2226  ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2227  s->dcadsp_data[ch].u.flt.hist1,
2228  &s->dcadsp_data[ch].offset,
2229  s->dcadsp_data[ch].u.flt.hist2,
2230  filter_coeff,
2231  s->npcmblocks,
2232  1.0f / (1 << (17 - x96_synth)));
2233  }
2234 
2235  // Filter LFE channel
2236  if (s->lfe_present) {
2237  int dec_select = (s->lfe_present == DCA_LFE_FLAG_128);
2238  float *samples = output_samples[DCA_SPEAKER_LFE1];
2239  int nlfesamples = s->npcmblocks >> (dec_select + 1);
2240 
2241  // Offset intermediate buffer for X96
2242  if (x96_synth)
2243  samples += nsamples / 2;
2244 
2245  // Select filter
2246  if (dec_select)
2247  filter_coeff = ff_dca_lfe_fir_128;
2248  else
2249  filter_coeff = ff_dca_lfe_fir_64;
2250 
2251  // Interpolate LFE channel
2252  s->dcadsp->lfe_fir_float[dec_select](
2253  samples, s->lfe_samples + DCA_LFE_HISTORY,
2254  filter_coeff, s->npcmblocks);
2255 
2256  if (x96_synth) {
2257  // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2258  // (47.6 - 48.0 kHz) components of interpolation image
2259  s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2260  samples, &s->output_history_lfe_float,
2261  nsamples / 2);
2262  }
2263 
2264  // Update LFE history
2265  for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2266  s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2267  }
2268 
2269  // Undo embedded XCH downmix
2270  if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2271  && s->audio_mode >= DCA_AMODE_2F2R) {
2272  s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2273  output_samples[DCA_SPEAKER_Cs],
2274  -M_SQRT1_2, nsamples);
2275  s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2276  output_samples[DCA_SPEAKER_Cs],
2277  -M_SQRT1_2, nsamples);
2278  }
2279 
2280  // Undo embedded XXCH downmix
2281  if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2282  && s->xxch_dmix_embedded) {
2283  float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2284  int *coeff_ptr = s->xxch_dmix_coeff;
2285  int xch_base = ff_dca_channels[s->audio_mode];
2286  av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2287 
2288  // Undo downmix
2289  for (ch = xch_base; ch < s->nchannels; ch++) {
2290  int src_spkr = map_prm_ch_to_spkr(s, ch);
2291  if (src_spkr < 0)
2292  return AVERROR(EINVAL);
2293  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2294  if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2295  int coeff = *coeff_ptr++;
2296  if (coeff) {
2297  s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2298  output_samples[src_spkr],
2299  coeff * (-1.0f / (1 << 15)),
2300  nsamples);
2301  }
2302  }
2303  }
2304  }
2305 
2306  // Undo embedded core downmix pre-scaling
2307  for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2308  if (s->xxch_core_mask & (1U << spkr)) {
2309  s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2310  output_samples[spkr],
2311  scale_inv, nsamples);
2312  }
2313  }
2314  }
2315 
2316  if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2317  // Front sum/difference decoding
2318  if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
2319  || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
2320  s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2321  output_samples[DCA_SPEAKER_R],
2322  nsamples);
2323  }
2324 
2325  // Surround sum/difference decoding
2326  if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
2327  s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2328  output_samples[DCA_SPEAKER_Rs],
2329  nsamples);
2330  }
2331  }
2332 
2333  // Downmix primary channel set to stereo
2334  if (s->request_mask != s->ch_mask) {
2335  ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2336  s->prim_dmix_coeff,
2337  nsamples, s->ch_mask);
2338  }
2339 
2340  return 0;
2341 }
2342 
2344 {
2345  AVCodecContext *avctx = s->avctx;
2346  DCAContext *dca = avctx->priv_data;
2347  DCAExssAsset *asset = &dca->exss.assets[0];
2348  enum AVMatrixEncoding matrix_encoding;
2349  int ret;
2350 
2351  // Handle downmixing to stereo request
2353  && s->audio_mode > DCA_AMODE_MONO && s->prim_dmix_embedded
2354  && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2355  s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2356  s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2357  else
2358  s->request_mask = s->ch_mask;
2359  if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2360  return AVERROR(EINVAL);
2361 
2362  // Force fixed point mode when falling back from XLL
2363  if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2364  && (asset->extension_mask & DCA_EXSS_XLL)))
2366  else
2368  if (ret < 0)
2369  return ret;
2370 
2371  // Set profile, bit rate, etc
2372  if (s->ext_audio_mask & DCA_EXSS_MASK)
2373  avctx->profile = AV_PROFILE_DTS_HD_HRA;
2374  else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2375  avctx->profile = AV_PROFILE_DTS_ES;
2376  else if (s->ext_audio_mask & DCA_CSS_X96)
2377  avctx->profile = AV_PROFILE_DTS_96_24;
2378  else
2379  avctx->profile = AV_PROFILE_DTS;
2380 
2381  if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2382  avctx->bit_rate = s->bit_rate;
2383  else
2384  avctx->bit_rate = 0;
2385 
2386  if (s->audio_mode == DCA_AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2387  s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2388  matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2389  else
2390  matrix_encoding = AV_MATRIX_ENCODING_NONE;
2391  if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2392  return ret;
2393 
2394  return 0;
2395 }
2396 
2398 {
2399  if (s->subband_buffer) {
2401  memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2402  }
2403 
2404  if (s->x96_subband_buffer)
2406 
2408 }
2409 
2411 {
2412  int ret;
2413  float scale = 1.0f;
2414 
2415  if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2416  return -1;
2417  if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2418  return -1;
2419 
2420  ff_dcadct_init(&s->dcadct);
2421 
2422  if ((ret = av_tx_init(&s->imdct[0], &s->imdct_fn[0], AV_TX_FLOAT_MDCT,
2423  1, 32, &scale, 0)) < 0)
2424  return ret;
2425 
2426  if ((ret = av_tx_init(&s->imdct[1], &s->imdct_fn[1], AV_TX_FLOAT_MDCT,
2427  1, 64, &scale, 0)) < 0)
2428  return ret;
2429 
2430  ff_synth_filter_init(&s->synth);
2431 
2432  s->x96_rand = 1;
2433  return 0;
2434 }
2435 
2437 {
2438  av_freep(&s->float_dsp);
2439  av_freep(&s->fixed_dsp);
2440 
2441  av_tx_uninit(&s->imdct[0]);
2442  av_tx_uninit(&s->imdct[1]);
2443 
2444  av_freep(&s->subband_buffer);
2445  s->subband_size = 0;
2446 
2447  av_freep(&s->x96_subband_buffer);
2448  s->x96_subband_size = 0;
2449 
2450  av_freep(&s->output_buffer);
2451  s->output_size = 0;
2452 }
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:278
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:694
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:838
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:1279
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
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:1614
ff_dca_core_close
av_cold void ff_dca_core_close(DCACoreDecoder *s)
Definition: dca_core.c:2436
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:1420
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
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:859
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
parse_x96_subframe_audio
static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
Definition: dca_core.c:1160
parse_x96_frame_exss
static int parse_x96_frame_exss(DCACoreDecoder *s)
Definition: dca_core.c:1538
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
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:1905
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
data
const char data[16]
Definition: mxf.c:148
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:152
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:313
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:1476
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:902
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:381
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:2343
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
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:1305
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
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
GetBitContext
Definition: get_bits.h:108
DCA_EXT_AUDIO_XXCH
@ DCA_EXT_AUDIO_XXCH
Definition: dca_core.h:75
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
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:1682
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:936
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:124
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:246
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
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:198
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:1071
set_filter_mode
static void set_filter_mode(DCACoreDecoder *s, int mode)
Definition: dca_core.c:1948
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:1574
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
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
frame
static AVFrame * frame
Definition: demux_decode.c:54
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:244
NULL
#define NULL
Definition: coverity.c:32
alloc_sample_buffer
static int alloc_sample_buffer(DCACoreDecoder *s)
Definition: dca_core.c:774
AV_COPY128
#define AV_COPY128(d, s)
Definition: intreadwrite.h:605
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:1509
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
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:495
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:388
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:633
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:652
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:1154
ff_dca_inv_dmixtable
const uint32_t ff_dca_inv_dmixtable[FF_DCA_INV_DMIXTABLE_SIZE]
Definition: dcadata.c:8676
AV_PROFILE_DTS_HD_HRA
#define AV_PROFILE_DTS_HD_HRA
Definition: defs.h:89
DCACoreFrameHeader
Definition: dca.h:50
f
f
Definition: af_crystalizer.c:121
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1568
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
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
ff_dca_core_parse
int ff_dca_core_parse(DCACoreDecoder *s, const uint8_t *data, int size)
Definition: dca_core.c:1795
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:245
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:1057
size
int size
Definition: twinvq_data.h:10344
AV_PROFILE_DTS_ES
#define AV_PROFILE_DTS_ES
Definition: defs.h:87
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
erase_dsp_history
static void erase_dsp_history(DCACoreDecoder *s)
Definition: dca_core.c:1941
header
static const uint8_t header[24]
Definition: sdr2.c:68
parse_scale
static int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
Definition: dca_core.c:349
AV_PROFILE_DTS
#define AV_PROFILE_DTS
Definition: defs.h:86
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:2410
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
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
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:255
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
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
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:56
dca.h
filter_frame_fixed
static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
Definition: dca_core.c:2060
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:1956
DCA_SUBBANDS
#define DCA_SUBBANDS
Definition: dca_core.h:40
AV_PROFILE_DTS_96_24
#define AV_PROFILE_DTS_96_24
Definition: defs.h:88
VLC::bits
int bits
Definition: vlc.h:37
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:1828
DCA_PACKET_EXSS
#define DCA_PACKET_EXSS
Definition: dcadec.h:40
ff_dca_fir_32bands_nonperfect_fixed
const int32_t ff_dca_fir_32bands_nonperfect_fixed[512]
Definition: dcadata.c:8205
parse_frame_data
static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
Definition: dca_core.c:802
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:2157
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:103
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:1389
AVCodecContext
main external API structure.
Definition: avcodec.h:445
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:36
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1639
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:38
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:342
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:472
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:1268
DCA_SPEAKER_COUNT
@ DCA_SPEAKER_COUNT
Definition: dca.h:87
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:79
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:2397