You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2447 lines
84KB

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