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