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.

2608 lines
88KB

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