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.

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