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.

2519 lines
85KB

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