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