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