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