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.

2551 lines
86KB

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