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.

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