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