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.

2613 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. emms_c();
  822. return 0;
  823. }
  824. static int parse_xch_frame(DCACoreDecoder *s)
  825. {
  826. int ret;
  827. if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
  828. av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
  829. return AVERROR_INVALIDDATA;
  830. }
  831. if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
  832. return ret;
  833. // Seek to the end of core frame, don't trust XCH frame size
  834. if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
  835. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
  836. return AVERROR_INVALIDDATA;
  837. }
  838. return 0;
  839. }
  840. static int parse_xxch_frame(DCACoreDecoder *s)
  841. {
  842. int xxch_nchsets, xxch_frame_size;
  843. int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
  844. // XXCH sync word
  845. if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
  846. av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
  847. return AVERROR_INVALIDDATA;
  848. }
  849. // XXCH frame header length
  850. header_size = get_bits(&s->gb, 6) + 1;
  851. // Check XXCH frame header CRC
  852. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  853. && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
  854. av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
  855. return AVERROR_INVALIDDATA;
  856. }
  857. // CRC presence flag for channel set header
  858. s->xxch_crc_present = get_bits1(&s->gb);
  859. // Number of bits for loudspeaker mask
  860. s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
  861. if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
  862. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
  863. return AVERROR_INVALIDDATA;
  864. }
  865. // Number of channel sets
  866. xxch_nchsets = get_bits(&s->gb, 2) + 1;
  867. if (xxch_nchsets > 1) {
  868. avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
  869. return AVERROR_PATCHWELCOME;
  870. }
  871. // Channel set 0 data byte size
  872. xxch_frame_size = get_bits(&s->gb, 14) + 1;
  873. // Core loudspeaker activity mask
  874. s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
  875. // Validate the core mask
  876. mask = s->ch_mask;
  877. if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
  878. mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
  879. if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
  880. mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
  881. if (mask != s->xxch_core_mask) {
  882. av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
  883. return AVERROR_INVALIDDATA;
  884. }
  885. // Reserved
  886. // Byte align
  887. // CRC16 of XXCH frame header
  888. if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
  889. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
  890. return AVERROR_INVALIDDATA;
  891. }
  892. // Parse XXCH channel set 0
  893. if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
  894. return ret;
  895. if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
  896. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
  897. return AVERROR_INVALIDDATA;
  898. }
  899. return 0;
  900. }
  901. static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
  902. int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
  903. {
  904. int xbr_nabits[DCA_CHANNELS];
  905. int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
  906. int xbr_scale_nbits[DCA_CHANNELS];
  907. int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
  908. int ssf, ch, band, ofs;
  909. // Check number of subband samples in this subframe
  910. if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
  911. av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
  912. return AVERROR_INVALIDDATA;
  913. }
  914. if (get_bits_left(&s->gb) < 0)
  915. return AVERROR_INVALIDDATA;
  916. // Number of bits for XBR bit allocation index
  917. for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
  918. xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
  919. // XBR bit allocation index
  920. for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
  921. for (band = 0; band < xbr_nsubbands[ch]; band++) {
  922. xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
  923. if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
  924. av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
  925. return AVERROR_INVALIDDATA;
  926. }
  927. }
  928. }
  929. // Number of bits for scale indices
  930. for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
  931. xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
  932. if (!xbr_scale_nbits[ch]) {
  933. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
  934. return AVERROR_INVALIDDATA;
  935. }
  936. }
  937. // XBR scale factors
  938. for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
  939. const uint32_t *scale_table;
  940. int scale_size;
  941. // Select the root square table
  942. if (s->scale_factor_sel[ch] > 5) {
  943. scale_table = ff_dca_scale_factor_quant7;
  944. scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
  945. } else {
  946. scale_table = ff_dca_scale_factor_quant6;
  947. scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
  948. }
  949. // Parse scale factor indices and look up scale factors from the root
  950. // square table
  951. for (band = 0; band < xbr_nsubbands[ch]; band++) {
  952. if (xbr_bit_allocation[ch][band]) {
  953. int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
  954. if (scale_index >= scale_size) {
  955. av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
  956. return AVERROR_INVALIDDATA;
  957. }
  958. xbr_scale_factors[ch][band][0] = scale_table[scale_index];
  959. if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
  960. scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
  961. if (scale_index >= scale_size) {
  962. av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
  963. return AVERROR_INVALIDDATA;
  964. }
  965. xbr_scale_factors[ch][band][1] = scale_table[scale_index];
  966. }
  967. }
  968. }
  969. }
  970. // Audio data
  971. for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
  972. for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
  973. if (get_bits_left(&s->gb) < 0)
  974. return AVERROR_INVALIDDATA;
  975. for (band = 0; band < xbr_nsubbands[ch]; band++) {
  976. int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
  977. int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
  978. // Extract bits from the bit stream
  979. if (abits > 7) {
  980. // No further encoding
  981. get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
  982. } else if (abits > 0) {
  983. // Block codes
  984. if ((ret = parse_block_codes(s, audio, abits)) < 0)
  985. return ret;
  986. } else {
  987. // No bits allocated
  988. continue;
  989. }
  990. // Look up quantization step size
  991. step_size = ff_dca_lossless_quant[abits];
  992. // Identify transient location
  993. if (xbr_transition_mode)
  994. trans_ssf = s->transition_mode[sf][ch][band];
  995. else
  996. trans_ssf = 0;
  997. // Determine proper scale factor
  998. if (trans_ssf == 0 || ssf < trans_ssf)
  999. scale = xbr_scale_factors[ch][band][0];
  1000. else
  1001. scale = xbr_scale_factors[ch][band][1];
  1002. dequantize(s->subband_samples[ch][band] + ofs,
  1003. audio, step_size, scale, 1);
  1004. }
  1005. }
  1006. // DSYNC
  1007. if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
  1008. av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
  1009. return AVERROR_INVALIDDATA;
  1010. }
  1011. ofs += DCA_SUBBAND_SAMPLES;
  1012. }
  1013. // Advance subband sample pointer for the next subframe
  1014. *sub_pos = ofs;
  1015. return 0;
  1016. }
  1017. static int parse_xbr_frame(DCACoreDecoder *s)
  1018. {
  1019. int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
  1020. int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
  1021. int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
  1022. int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
  1023. int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
  1024. // XBR sync word
  1025. if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
  1026. av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
  1027. return AVERROR_INVALIDDATA;
  1028. }
  1029. // XBR frame header length
  1030. header_size = get_bits(&s->gb, 6) + 1;
  1031. // Check XBR frame header CRC
  1032. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  1033. && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
  1034. av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
  1035. return AVERROR_INVALIDDATA;
  1036. }
  1037. // Number of channel sets
  1038. xbr_nchsets = get_bits(&s->gb, 2) + 1;
  1039. // Channel set data byte size
  1040. for (i = 0; i < xbr_nchsets; i++)
  1041. xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
  1042. // Transition mode flag
  1043. xbr_transition_mode = get_bits1(&s->gb);
  1044. // Channel set headers
  1045. for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
  1046. xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
  1047. xbr_band_nbits = get_bits(&s->gb, 2) + 5;
  1048. for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
  1049. xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
  1050. if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
  1051. av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
  1052. return AVERROR_INVALIDDATA;
  1053. }
  1054. }
  1055. }
  1056. // Reserved
  1057. // Byte align
  1058. // CRC16 of XBR frame header
  1059. if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
  1060. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
  1061. return AVERROR_INVALIDDATA;
  1062. }
  1063. // Channel set data
  1064. for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
  1065. header_pos = get_bits_count(&s->gb);
  1066. if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
  1067. int sf, sub_pos;
  1068. for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
  1069. if ((ret = parse_xbr_subframe(s, xbr_base_ch,
  1070. xbr_base_ch + xbr_nchannels[i],
  1071. xbr_nsubbands, xbr_transition_mode,
  1072. sf, &sub_pos)) < 0)
  1073. return ret;
  1074. }
  1075. }
  1076. xbr_base_ch += xbr_nchannels[i];
  1077. if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
  1078. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
  1079. return AVERROR_INVALIDDATA;
  1080. }
  1081. }
  1082. return 0;
  1083. }
  1084. // Modified ISO/IEC 9899 linear congruential generator
  1085. // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
  1086. static int rand_x96(DCACoreDecoder *s)
  1087. {
  1088. s->x96_rand = 1103515245U * s->x96_rand + 12345U;
  1089. return (s->x96_rand & 0x7fffffff) - 0x40000000;
  1090. }
  1091. static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
  1092. {
  1093. int n, ssf, ch, band, ofs;
  1094. // Check number of subband samples in this subframe
  1095. int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
  1096. if (*sub_pos + nsamples > s->npcmblocks) {
  1097. av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
  1098. return AVERROR_INVALIDDATA;
  1099. }
  1100. if (get_bits_left(&s->gb) < 0)
  1101. return AVERROR_INVALIDDATA;
  1102. // VQ encoded or unallocated subbands
  1103. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1104. for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
  1105. // Get the sample pointer and scale factor
  1106. int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
  1107. int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
  1108. switch (s->bit_allocation[ch][band]) {
  1109. case 0: // No bits allocated for subband
  1110. if (scale <= 1)
  1111. memset(samples, 0, nsamples * sizeof(int32_t));
  1112. else for (n = 0; n < nsamples; n++)
  1113. // Generate scaled random samples
  1114. samples[n] = mul31(rand_x96(s), scale);
  1115. break;
  1116. case 1: // VQ encoded subband
  1117. for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
  1118. // Extract the VQ address from the bit stream and look up
  1119. // the VQ code book for up to 16 subband samples
  1120. const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
  1121. // Scale and take the samples
  1122. for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
  1123. *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
  1124. }
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. // Audio data
  1130. for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
  1131. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1132. if (get_bits_left(&s->gb) < 0)
  1133. return AVERROR_INVALIDDATA;
  1134. for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
  1135. int ret, abits = s->bit_allocation[ch][band] - 1;
  1136. int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
  1137. // Not VQ encoded or unallocated subbands
  1138. if (abits < 1)
  1139. continue;
  1140. // Extract bits from the bit stream
  1141. if ((ret = extract_audio(s, audio, abits, ch)) < 0)
  1142. return ret;
  1143. // Select quantization step size table and look up quantization
  1144. // step size
  1145. if (s->bit_rate == 3)
  1146. step_size = ff_dca_lossless_quant[abits];
  1147. else
  1148. step_size = ff_dca_lossy_quant[abits];
  1149. // Get the scale factor
  1150. scale = s->scale_factors[ch][band >> 1][band & 1];
  1151. dequantize(s->x96_subband_samples[ch][band] + ofs,
  1152. audio, step_size, scale, 0);
  1153. }
  1154. }
  1155. // DSYNC
  1156. if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
  1157. av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
  1158. return AVERROR_INVALIDDATA;
  1159. }
  1160. ofs += DCA_SUBBAND_SAMPLES;
  1161. }
  1162. // Inverse ADPCM
  1163. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1164. inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
  1165. s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
  1166. *sub_pos, nsamples);
  1167. }
  1168. // Joint subband coding
  1169. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1170. int src_ch = s->joint_intensity_index[ch] - 1;
  1171. if (src_ch >= 0) {
  1172. s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
  1173. s->joint_scale_factors[ch], s->nsubbands[ch],
  1174. s->nsubbands[src_ch], *sub_pos, nsamples);
  1175. }
  1176. }
  1177. // Advance subband sample pointer for the next subframe
  1178. *sub_pos = ofs;
  1179. return 0;
  1180. }
  1181. static void erase_x96_adpcm_history(DCACoreDecoder *s)
  1182. {
  1183. int ch, band;
  1184. // Erase ADPCM history from previous frame if
  1185. // predictor history switch was disabled
  1186. for (ch = 0; ch < DCA_CHANNELS; ch++)
  1187. for (band = 0; band < DCA_SUBBANDS_X96; band++)
  1188. AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
  1189. emms_c();
  1190. }
  1191. static int alloc_x96_sample_buffer(DCACoreDecoder *s)
  1192. {
  1193. int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
  1194. int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
  1195. unsigned int size = s->x96_subband_size;
  1196. int ch, band;
  1197. // Reallocate subband sample buffer
  1198. av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
  1199. nframesamples * sizeof(int32_t));
  1200. if (!s->x96_subband_buffer)
  1201. return AVERROR(ENOMEM);
  1202. if (size != s->x96_subband_size) {
  1203. for (ch = 0; ch < DCA_CHANNELS; ch++)
  1204. for (band = 0; band < DCA_SUBBANDS_X96; band++)
  1205. s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
  1206. (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
  1207. }
  1208. if (!s->predictor_history)
  1209. erase_x96_adpcm_history(s);
  1210. return 0;
  1211. }
  1212. static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
  1213. {
  1214. int ch, band, ret;
  1215. if (get_bits_left(&s->gb) < 0)
  1216. return AVERROR_INVALIDDATA;
  1217. // Prediction mode
  1218. for (ch = xch_base; ch < s->x96_nchannels; ch++)
  1219. for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
  1220. s->prediction_mode[ch][band] = get_bits1(&s->gb);
  1221. // Prediction coefficients VQ address
  1222. for (ch = xch_base; ch < s->x96_nchannels; ch++)
  1223. for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
  1224. if (s->prediction_mode[ch][band])
  1225. s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
  1226. // Bit allocation index
  1227. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1228. int sel = s->bit_allocation_sel[ch];
  1229. int abits = 0;
  1230. for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
  1231. // If Huffman code was used, the difference of abits was encoded
  1232. if (sel < 7)
  1233. abits += dca_get_vlc(&s->gb, &vlc_quant_index[5 + 2 * s->x96_high_res], sel);
  1234. else
  1235. abits = get_bits(&s->gb, 3 + s->x96_high_res);
  1236. if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
  1237. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
  1238. return AVERROR_INVALIDDATA;
  1239. }
  1240. s->bit_allocation[ch][band] = abits;
  1241. }
  1242. }
  1243. // Scale factors
  1244. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1245. int sel = s->scale_factor_sel[ch];
  1246. int scale_index = 0;
  1247. // Extract scales for subbands which are transmitted even for
  1248. // unallocated subbands
  1249. for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
  1250. if ((ret = parse_scale(s, &scale_index, sel)) < 0)
  1251. return ret;
  1252. s->scale_factors[ch][band >> 1][band & 1] = ret;
  1253. }
  1254. }
  1255. // Joint subband codebook select
  1256. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1257. if (s->joint_intensity_index[ch]) {
  1258. s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
  1259. if (s->joint_scale_sel[ch] == 7) {
  1260. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
  1261. return AVERROR_INVALIDDATA;
  1262. }
  1263. }
  1264. }
  1265. // Scale factors for joint subband coding
  1266. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1267. int src_ch = s->joint_intensity_index[ch] - 1;
  1268. if (src_ch >= 0) {
  1269. int sel = s->joint_scale_sel[ch];
  1270. for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
  1271. if ((ret = parse_joint_scale(s, sel)) < 0)
  1272. return ret;
  1273. s->joint_scale_factors[ch][band] = ret;
  1274. }
  1275. }
  1276. }
  1277. // Side information CRC check word
  1278. if (s->crc_present)
  1279. skip_bits(&s->gb, 16);
  1280. return 0;
  1281. }
  1282. static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
  1283. {
  1284. int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
  1285. if (get_bits_left(&s->gb) < 0)
  1286. return AVERROR_INVALIDDATA;
  1287. if (exss) {
  1288. // Channel set header length
  1289. header_size = get_bits(&s->gb, 7) + 1;
  1290. // Check CRC
  1291. if (s->x96_crc_present
  1292. && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  1293. && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
  1294. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
  1295. return AVERROR_INVALIDDATA;
  1296. }
  1297. }
  1298. // High resolution flag
  1299. s->x96_high_res = get_bits1(&s->gb);
  1300. // First encoded subband
  1301. if (s->x96_rev_no < 8) {
  1302. s->x96_subband_start = get_bits(&s->gb, 5);
  1303. if (s->x96_subband_start > 27) {
  1304. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
  1305. return AVERROR_INVALIDDATA;
  1306. }
  1307. } else {
  1308. s->x96_subband_start = DCA_SUBBANDS;
  1309. }
  1310. // Subband activity count
  1311. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1312. s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
  1313. if (s->nsubbands[ch] < DCA_SUBBANDS) {
  1314. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
  1315. return AVERROR_INVALIDDATA;
  1316. }
  1317. }
  1318. // Joint intensity coding index
  1319. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1320. if ((n = get_bits(&s->gb, 3)) && xch_base)
  1321. n += xch_base - 1;
  1322. if (n > s->x96_nchannels) {
  1323. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
  1324. return AVERROR_INVALIDDATA;
  1325. }
  1326. s->joint_intensity_index[ch] = n;
  1327. }
  1328. // Scale factor code book
  1329. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1330. s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
  1331. if (s->scale_factor_sel[ch] >= 6) {
  1332. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
  1333. return AVERROR_INVALIDDATA;
  1334. }
  1335. }
  1336. // Bit allocation quantizer select
  1337. for (ch = xch_base; ch < s->x96_nchannels; ch++)
  1338. s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
  1339. // Quantization index codebook select
  1340. for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
  1341. for (ch = xch_base; ch < s->x96_nchannels; ch++)
  1342. s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
  1343. if (exss) {
  1344. // Reserved
  1345. // Byte align
  1346. // CRC16 of channel set header
  1347. if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
  1348. av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
  1349. return AVERROR_INVALIDDATA;
  1350. }
  1351. } else {
  1352. if (s->crc_present)
  1353. skip_bits(&s->gb, 16);
  1354. }
  1355. return 0;
  1356. }
  1357. static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
  1358. {
  1359. int sf, ch, ret, band, sub_pos;
  1360. if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
  1361. return ret;
  1362. for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
  1363. if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
  1364. return ret;
  1365. if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
  1366. return ret;
  1367. }
  1368. for (ch = xch_base; ch < s->x96_nchannels; ch++) {
  1369. // Determine number of active subbands for this channel
  1370. int nsubbands = s->nsubbands[ch];
  1371. if (s->joint_intensity_index[ch])
  1372. nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
  1373. // Update history for ADPCM and clear inactive subbands
  1374. for (band = 0; band < DCA_SUBBANDS_X96; band++) {
  1375. int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
  1376. if (band >= s->x96_subband_start && band < nsubbands)
  1377. AV_COPY128(samples, samples + s->npcmblocks);
  1378. else
  1379. memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
  1380. }
  1381. }
  1382. emms_c();
  1383. return 0;
  1384. }
  1385. static int parse_x96_frame(DCACoreDecoder *s)
  1386. {
  1387. int ret;
  1388. // Revision number
  1389. s->x96_rev_no = get_bits(&s->gb, 4);
  1390. if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
  1391. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
  1392. return AVERROR_INVALIDDATA;
  1393. }
  1394. s->x96_crc_present = 0;
  1395. s->x96_nchannels = s->nchannels;
  1396. if ((ret = alloc_x96_sample_buffer(s)) < 0)
  1397. return ret;
  1398. if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
  1399. return ret;
  1400. // Seek to the end of core frame
  1401. if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
  1402. av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
  1403. return AVERROR_INVALIDDATA;
  1404. }
  1405. return 0;
  1406. }
  1407. static int parse_x96_frame_exss(DCACoreDecoder *s)
  1408. {
  1409. int x96_frame_size[DCA_EXSS_CHSETS_MAX];
  1410. int x96_nchannels[DCA_EXSS_CHSETS_MAX];
  1411. int x96_nchsets, x96_base_ch;
  1412. int i, ret, header_size, header_pos = get_bits_count(&s->gb);
  1413. // X96 sync word
  1414. if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
  1415. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
  1416. return AVERROR_INVALIDDATA;
  1417. }
  1418. // X96 frame header length
  1419. header_size = get_bits(&s->gb, 6) + 1;
  1420. // Check X96 frame header CRC
  1421. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  1422. && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
  1423. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
  1424. return AVERROR_INVALIDDATA;
  1425. }
  1426. // Revision number
  1427. s->x96_rev_no = get_bits(&s->gb, 4);
  1428. if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
  1429. av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
  1430. return AVERROR_INVALIDDATA;
  1431. }
  1432. // CRC presence flag for channel set header
  1433. s->x96_crc_present = get_bits1(&s->gb);
  1434. // Number of channel sets
  1435. x96_nchsets = get_bits(&s->gb, 2) + 1;
  1436. // Channel set data byte size
  1437. for (i = 0; i < x96_nchsets; i++)
  1438. x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
  1439. // Number of channels in channel set
  1440. for (i = 0; i < x96_nchsets; i++)
  1441. x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
  1442. // Reserved
  1443. // Byte align
  1444. // CRC16 of X96 frame header
  1445. if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
  1446. av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
  1447. return AVERROR_INVALIDDATA;
  1448. }
  1449. if ((ret = alloc_x96_sample_buffer(s)) < 0)
  1450. return ret;
  1451. // Channel set data
  1452. s->x96_nchannels = 0;
  1453. for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
  1454. header_pos = get_bits_count(&s->gb);
  1455. if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
  1456. s->x96_nchannels = x96_base_ch + x96_nchannels[i];
  1457. if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
  1458. return ret;
  1459. }
  1460. x96_base_ch += x96_nchannels[i];
  1461. if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
  1462. av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
  1463. return AVERROR_INVALIDDATA;
  1464. }
  1465. }
  1466. return 0;
  1467. }
  1468. static int parse_aux_data(DCACoreDecoder *s)
  1469. {
  1470. int aux_pos;
  1471. if (get_bits_left(&s->gb) < 0)
  1472. return AVERROR_INVALIDDATA;
  1473. // Auxiliary data byte count (can't be trusted)
  1474. skip_bits(&s->gb, 6);
  1475. // 4-byte align
  1476. skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
  1477. // Auxiliary data sync word
  1478. if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
  1479. av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
  1480. return AVERROR_INVALIDDATA;
  1481. }
  1482. aux_pos = get_bits_count(&s->gb);
  1483. // Auxiliary decode time stamp flag
  1484. if (get_bits1(&s->gb))
  1485. skip_bits_long(&s->gb, 47);
  1486. // Auxiliary dynamic downmix flag
  1487. if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
  1488. int i, m, n;
  1489. // Auxiliary primary channel downmix type
  1490. s->prim_dmix_type = get_bits(&s->gb, 3);
  1491. if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
  1492. av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
  1493. return AVERROR_INVALIDDATA;
  1494. }
  1495. // Size of downmix coefficients matrix
  1496. m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
  1497. n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
  1498. // Dynamic downmix code coefficients
  1499. for (i = 0; i < m * n; i++) {
  1500. int code = get_bits(&s->gb, 9);
  1501. int sign = (code >> 8) - 1;
  1502. unsigned int index = code & 0xff;
  1503. if (index >= FF_DCA_DMIXTABLE_SIZE) {
  1504. av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
  1505. return AVERROR_INVALIDDATA;
  1506. }
  1507. s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
  1508. }
  1509. }
  1510. // Byte align
  1511. skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
  1512. // CRC16 of auxiliary data
  1513. skip_bits(&s->gb, 16);
  1514. // Check CRC
  1515. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  1516. && ff_dca_check_crc(&s->gb, aux_pos, get_bits_count(&s->gb))) {
  1517. av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
  1518. return AVERROR_INVALIDDATA;
  1519. }
  1520. return 0;
  1521. }
  1522. static int parse_optional_info(DCACoreDecoder *s)
  1523. {
  1524. DCAContext *dca = s->avctx->priv_data;
  1525. int ret = -1;
  1526. // Time code stamp
  1527. if (s->ts_present)
  1528. skip_bits_long(&s->gb, 32);
  1529. // Auxiliary data
  1530. if (s->aux_present && (ret = parse_aux_data(s)) < 0
  1531. && (s->avctx->err_recognition & AV_EF_EXPLODE))
  1532. return ret;
  1533. if (ret < 0)
  1534. s->prim_dmix_embedded = 0;
  1535. // Core extensions
  1536. if (s->ext_audio_present && !dca->core_only) {
  1537. int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
  1538. int last_pos = get_bits_count(&s->gb) / 32;
  1539. int size, dist;
  1540. // Search for extension sync words aligned on 4-byte boundary. Search
  1541. // must be done backwards from the end of core frame to work around
  1542. // sync word aliasing issues.
  1543. switch (s->ext_audio_type) {
  1544. case EXT_AUDIO_XCH:
  1545. if (dca->request_channel_layout)
  1546. break;
  1547. // The distance between XCH sync word and end of the core frame
  1548. // must be equal to XCH frame size. Off by one error is allowed for
  1549. // compatibility with legacy bitstreams. Minimum XCH frame size is
  1550. // 96 bytes. AMODE and PCHS are further checked to reduce
  1551. // probability of alias sync detection.
  1552. for (; sync_pos >= last_pos; sync_pos--) {
  1553. if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
  1554. s->gb.index = (sync_pos + 1) * 32;
  1555. size = get_bits(&s->gb, 10) + 1;
  1556. dist = s->frame_size - sync_pos * 4;
  1557. if (size >= 96
  1558. && (size == dist || size - 1 == dist)
  1559. && get_bits(&s->gb, 7) == 0x08) {
  1560. s->xch_pos = get_bits_count(&s->gb);
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. if (s->avctx->err_recognition & AV_EF_EXPLODE) {
  1566. av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
  1567. return AVERROR_INVALIDDATA;
  1568. }
  1569. break;
  1570. case EXT_AUDIO_X96:
  1571. // The distance between X96 sync word and end of the core frame
  1572. // must be equal to X96 frame size. Minimum X96 frame size is 96
  1573. // bytes.
  1574. for (; sync_pos >= last_pos; sync_pos--) {
  1575. if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
  1576. s->gb.index = (sync_pos + 1) * 32;
  1577. size = get_bits(&s->gb, 12) + 1;
  1578. dist = s->frame_size - sync_pos * 4;
  1579. if (size >= 96 && size == dist) {
  1580. s->x96_pos = get_bits_count(&s->gb);
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. if (s->avctx->err_recognition & AV_EF_EXPLODE) {
  1586. av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
  1587. return AVERROR_INVALIDDATA;
  1588. }
  1589. break;
  1590. case EXT_AUDIO_XXCH:
  1591. if (dca->request_channel_layout)
  1592. break;
  1593. // XXCH frame header CRC must be valid. Minimum XXCH frame header
  1594. // size is 11 bytes.
  1595. for (; sync_pos >= last_pos; sync_pos--) {
  1596. if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
  1597. s->gb.index = (sync_pos + 1) * 32;
  1598. size = get_bits(&s->gb, 6) + 1;
  1599. if (size >= 11 &&
  1600. !ff_dca_check_crc(&s->gb, (sync_pos + 1) * 32,
  1601. sync_pos * 32 + size * 8)) {
  1602. s->xxch_pos = sync_pos * 32;
  1603. break;
  1604. }
  1605. }
  1606. }
  1607. if (s->avctx->err_recognition & AV_EF_EXPLODE) {
  1608. av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
  1609. return AVERROR_INVALIDDATA;
  1610. }
  1611. break;
  1612. }
  1613. }
  1614. return 0;
  1615. }
  1616. int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
  1617. {
  1618. int ret;
  1619. s->ext_audio_mask = 0;
  1620. s->xch_pos = s->xxch_pos = s->x96_pos = 0;
  1621. if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
  1622. return ret;
  1623. skip_bits_long(&s->gb, 32);
  1624. if ((ret = parse_frame_header(s)) < 0)
  1625. return ret;
  1626. if ((ret = alloc_sample_buffer(s)) < 0)
  1627. return ret;
  1628. if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
  1629. return ret;
  1630. if ((ret = parse_optional_info(s)) < 0)
  1631. return ret;
  1632. // Workaround for DTS in WAV
  1633. if (s->frame_size > size && s->frame_size < size + 4) {
  1634. av_log(s->avctx, AV_LOG_DEBUG, "Working around excessive core frame size (%d > %d)\n", s->frame_size, size);
  1635. s->frame_size = size;
  1636. }
  1637. if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
  1638. av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
  1639. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  1640. return AVERROR_INVALIDDATA;
  1641. }
  1642. return 0;
  1643. }
  1644. int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
  1645. {
  1646. AVCodecContext *avctx = s->avctx;
  1647. DCAContext *dca = avctx->priv_data;
  1648. GetBitContext gb = s->gb;
  1649. int exss_mask = asset ? asset->extension_mask : 0;
  1650. int ret = 0, ext = 0;
  1651. // Parse (X)XCH unless downmixing
  1652. if (!dca->request_channel_layout) {
  1653. if (exss_mask & DCA_EXSS_XXCH) {
  1654. if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
  1655. return ret;
  1656. ret = parse_xxch_frame(s);
  1657. ext = DCA_EXSS_XXCH;
  1658. } else if (s->xxch_pos) {
  1659. s->gb.index = s->xxch_pos;
  1660. ret = parse_xxch_frame(s);
  1661. ext = DCA_CSS_XXCH;
  1662. } else if (s->xch_pos) {
  1663. s->gb.index = s->xch_pos;
  1664. ret = parse_xch_frame(s);
  1665. ext = DCA_CSS_XCH;
  1666. }
  1667. // Revert to primary channel set in case (X)XCH parsing fails
  1668. if (ret < 0) {
  1669. if (avctx->err_recognition & AV_EF_EXPLODE)
  1670. return ret;
  1671. s->nchannels = ff_dca_channels[s->audio_mode];
  1672. s->ch_mask = audio_mode_ch_mask[s->audio_mode];
  1673. if (s->lfe_present)
  1674. s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
  1675. } else {
  1676. s->ext_audio_mask |= ext;
  1677. }
  1678. }
  1679. // Parse XBR
  1680. if (exss_mask & DCA_EXSS_XBR) {
  1681. if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
  1682. return ret;
  1683. if ((ret = parse_xbr_frame(s)) < 0) {
  1684. if (avctx->err_recognition & AV_EF_EXPLODE)
  1685. return ret;
  1686. } else {
  1687. s->ext_audio_mask |= DCA_EXSS_XBR;
  1688. }
  1689. }
  1690. // Parse X96 unless decoding XLL
  1691. if (!(dca->packet & DCA_PACKET_XLL)) {
  1692. if (exss_mask & DCA_EXSS_X96) {
  1693. if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
  1694. return ret;
  1695. if ((ret = parse_x96_frame_exss(s)) < 0) {
  1696. if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
  1697. return ret;
  1698. } else {
  1699. s->ext_audio_mask |= DCA_EXSS_X96;
  1700. }
  1701. } else if (s->x96_pos) {
  1702. s->gb = gb;
  1703. s->gb.index = s->x96_pos;
  1704. if ((ret = parse_x96_frame(s)) < 0) {
  1705. if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
  1706. return ret;
  1707. } else {
  1708. s->ext_audio_mask |= DCA_CSS_X96;
  1709. }
  1710. }
  1711. }
  1712. return 0;
  1713. }
  1714. static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
  1715. {
  1716. int pos, spkr;
  1717. // Try to map this channel to core first
  1718. pos = ff_dca_channels[s->audio_mode];
  1719. if (ch < pos) {
  1720. spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
  1721. if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
  1722. if (s->xxch_core_mask & (1U << spkr))
  1723. return spkr;
  1724. if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
  1725. return DCA_SPEAKER_Lss;
  1726. if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
  1727. return DCA_SPEAKER_Rss;
  1728. return -1;
  1729. }
  1730. return spkr;
  1731. }
  1732. // Then XCH
  1733. if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
  1734. return DCA_SPEAKER_Cs;
  1735. // Then XXCH
  1736. if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
  1737. for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
  1738. if (s->xxch_spkr_mask & (1U << spkr))
  1739. if (pos++ == ch)
  1740. return spkr;
  1741. }
  1742. // No mapping
  1743. return -1;
  1744. }
  1745. static void erase_dsp_history(DCACoreDecoder *s)
  1746. {
  1747. memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
  1748. s->output_history_lfe_fixed = 0;
  1749. s->output_history_lfe_float = 0;
  1750. }
  1751. static void set_filter_mode(DCACoreDecoder *s, int mode)
  1752. {
  1753. if (s->filter_mode != mode) {
  1754. erase_dsp_history(s);
  1755. s->filter_mode = mode;
  1756. }
  1757. }
  1758. int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
  1759. {
  1760. int n, ch, spkr, nsamples, x96_nchannels = 0;
  1761. const int32_t *filter_coeff;
  1762. int32_t *ptr;
  1763. // Externally set x96_synth flag implies that X96 synthesis should be
  1764. // enabled, yet actual X96 subband data should be discarded. This is a
  1765. // special case for lossless residual decoder that ignores X96 data if
  1766. // present.
  1767. if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
  1768. x96_nchannels = s->x96_nchannels;
  1769. x96_synth = 1;
  1770. }
  1771. if (x96_synth < 0)
  1772. x96_synth = 0;
  1773. s->output_rate = s->sample_rate << x96_synth;
  1774. s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
  1775. // Reallocate PCM output buffer
  1776. av_fast_malloc(&s->output_buffer, &s->output_size,
  1777. nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
  1778. if (!s->output_buffer)
  1779. return AVERROR(ENOMEM);
  1780. ptr = (int32_t *)s->output_buffer;
  1781. for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
  1782. if (s->ch_mask & (1U << spkr)) {
  1783. s->output_samples[spkr] = ptr;
  1784. ptr += nsamples;
  1785. } else {
  1786. s->output_samples[spkr] = NULL;
  1787. }
  1788. }
  1789. // Handle change of filtering mode
  1790. set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
  1791. // Select filter
  1792. if (x96_synth)
  1793. filter_coeff = ff_dca_fir_64bands_fixed;
  1794. else if (s->filter_perfect)
  1795. filter_coeff = ff_dca_fir_32bands_perfect_fixed;
  1796. else
  1797. filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
  1798. // Filter primary channels
  1799. for (ch = 0; ch < s->nchannels; ch++) {
  1800. // Map this primary channel to speaker
  1801. spkr = map_prm_ch_to_spkr(s, ch);
  1802. if (spkr < 0)
  1803. return AVERROR(EINVAL);
  1804. // Filter bank reconstruction
  1805. s->dcadsp->sub_qmf_fixed[x96_synth](
  1806. &s->synth,
  1807. &s->dcadct,
  1808. s->output_samples[spkr],
  1809. s->subband_samples[ch],
  1810. ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
  1811. s->dcadsp_data[ch].u.fix.hist1,
  1812. &s->dcadsp_data[ch].offset,
  1813. s->dcadsp_data[ch].u.fix.hist2,
  1814. filter_coeff,
  1815. s->npcmblocks);
  1816. }
  1817. // Filter LFE channel
  1818. if (s->lfe_present) {
  1819. int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
  1820. int nlfesamples = s->npcmblocks >> 1;
  1821. // Check LFF
  1822. if (s->lfe_present == LFE_FLAG_128) {
  1823. av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
  1824. return AVERROR(EINVAL);
  1825. }
  1826. // Offset intermediate buffer for X96
  1827. if (x96_synth)
  1828. samples += nsamples / 2;
  1829. // Interpolate LFE channel
  1830. s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
  1831. ff_dca_lfe_fir_64_fixed, s->npcmblocks);
  1832. if (x96_synth) {
  1833. // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
  1834. // (47.6 - 48.0 kHz) components of interpolation image
  1835. s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
  1836. samples, &s->output_history_lfe_fixed,
  1837. nsamples / 2);
  1838. }
  1839. // Update LFE history
  1840. for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
  1841. s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
  1842. }
  1843. return 0;
  1844. }
  1845. static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
  1846. {
  1847. AVCodecContext *avctx = s->avctx;
  1848. DCAContext *dca = avctx->priv_data;
  1849. int i, n, ch, ret, spkr, nsamples;
  1850. // Don't filter twice when falling back from XLL
  1851. if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
  1852. return ret;
  1853. avctx->sample_rate = s->output_rate;
  1854. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  1855. avctx->bits_per_raw_sample = 24;
  1856. frame->nb_samples = nsamples = s->npcmsamples;
  1857. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1858. return ret;
  1859. // Undo embedded XCH downmix
  1860. if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
  1861. && s->audio_mode >= AMODE_2F2R) {
  1862. s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
  1863. s->output_samples[DCA_SPEAKER_Rs],
  1864. s->output_samples[DCA_SPEAKER_Cs],
  1865. nsamples);
  1866. }
  1867. // Undo embedded XXCH downmix
  1868. if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
  1869. && s->xxch_dmix_embedded) {
  1870. int scale_inv = s->xxch_dmix_scale_inv;
  1871. int *coeff_ptr = s->xxch_dmix_coeff;
  1872. int xch_base = ff_dca_channels[s->audio_mode];
  1873. av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
  1874. // Undo embedded core downmix pre-scaling
  1875. for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
  1876. if (s->xxch_core_mask & (1U << spkr)) {
  1877. s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
  1878. scale_inv, nsamples);
  1879. }
  1880. }
  1881. // Undo downmix
  1882. for (ch = xch_base; ch < s->nchannels; ch++) {
  1883. int src_spkr = map_prm_ch_to_spkr(s, ch);
  1884. if (src_spkr < 0)
  1885. return AVERROR(EINVAL);
  1886. for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
  1887. if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
  1888. int coeff = mul16(*coeff_ptr++, scale_inv);
  1889. if (coeff) {
  1890. s->dcadsp->dmix_sub(s->output_samples[spkr ],
  1891. s->output_samples[src_spkr],
  1892. coeff, nsamples);
  1893. }
  1894. }
  1895. }
  1896. }
  1897. }
  1898. if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
  1899. // Front sum/difference decoding
  1900. if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
  1901. || s->audio_mode == AMODE_STEREO_SUMDIFF) {
  1902. s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
  1903. s->output_samples[DCA_SPEAKER_R],
  1904. nsamples);
  1905. }
  1906. // Surround sum/difference decoding
  1907. if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
  1908. s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
  1909. s->output_samples[DCA_SPEAKER_Rs],
  1910. nsamples);
  1911. }
  1912. }
  1913. // Downmix primary channel set to stereo
  1914. if (s->request_mask != s->ch_mask) {
  1915. ff_dca_downmix_to_stereo_fixed(s->dcadsp,
  1916. s->output_samples,
  1917. s->prim_dmix_coeff,
  1918. nsamples, s->ch_mask);
  1919. }
  1920. for (i = 0; i < avctx->channels; i++) {
  1921. int32_t *samples = s->output_samples[s->ch_remap[i]];
  1922. int32_t *plane = (int32_t *)frame->extended_data[i];
  1923. for (n = 0; n < nsamples; n++)
  1924. plane[n] = clip23(samples[n]) * (1 << 8);
  1925. }
  1926. return 0;
  1927. }
  1928. static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
  1929. {
  1930. AVCodecContext *avctx = s->avctx;
  1931. int x96_nchannels = 0, x96_synth = 0;
  1932. int i, n, ch, ret, spkr, nsamples, nchannels;
  1933. float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
  1934. const float *filter_coeff;
  1935. if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
  1936. x96_nchannels = s->x96_nchannels;
  1937. x96_synth = 1;
  1938. }
  1939. avctx->sample_rate = s->sample_rate << x96_synth;
  1940. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  1941. avctx->bits_per_raw_sample = 0;
  1942. frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
  1943. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1944. return ret;
  1945. // Build reverse speaker to channel mapping
  1946. for (i = 0; i < avctx->channels; i++)
  1947. output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
  1948. // Allocate space for extra channels
  1949. nchannels = av_popcount(s->ch_mask) - avctx->channels;
  1950. if (nchannels > 0) {
  1951. av_fast_malloc(&s->output_buffer, &s->output_size,
  1952. nsamples * nchannels * sizeof(float));
  1953. if (!s->output_buffer)
  1954. return AVERROR(ENOMEM);
  1955. ptr = (float *)s->output_buffer;
  1956. for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
  1957. if (!(s->ch_mask & (1U << spkr)))
  1958. continue;
  1959. if (output_samples[spkr])
  1960. continue;
  1961. output_samples[spkr] = ptr;
  1962. ptr += nsamples;
  1963. }
  1964. }
  1965. // Handle change of filtering mode
  1966. set_filter_mode(s, x96_synth);
  1967. // Select filter
  1968. if (x96_synth)
  1969. filter_coeff = ff_dca_fir_64bands;
  1970. else if (s->filter_perfect)
  1971. filter_coeff = ff_dca_fir_32bands_perfect;
  1972. else
  1973. filter_coeff = ff_dca_fir_32bands_nonperfect;
  1974. // Filter primary channels
  1975. for (ch = 0; ch < s->nchannels; ch++) {
  1976. // Map this primary channel to speaker
  1977. spkr = map_prm_ch_to_spkr(s, ch);
  1978. if (spkr < 0)
  1979. return AVERROR(EINVAL);
  1980. // Filter bank reconstruction
  1981. s->dcadsp->sub_qmf_float[x96_synth](
  1982. &s->synth,
  1983. &s->imdct[x96_synth],
  1984. output_samples[spkr],
  1985. s->subband_samples[ch],
  1986. ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
  1987. s->dcadsp_data[ch].u.flt.hist1,
  1988. &s->dcadsp_data[ch].offset,
  1989. s->dcadsp_data[ch].u.flt.hist2,
  1990. filter_coeff,
  1991. s->npcmblocks,
  1992. 1.0f / (1 << (17 - x96_synth)));
  1993. }
  1994. // Filter LFE channel
  1995. if (s->lfe_present) {
  1996. int dec_select = (s->lfe_present == LFE_FLAG_128);
  1997. float *samples = output_samples[DCA_SPEAKER_LFE1];
  1998. int nlfesamples = s->npcmblocks >> (dec_select + 1);
  1999. // Offset intermediate buffer for X96
  2000. if (x96_synth)
  2001. samples += nsamples / 2;
  2002. // Select filter
  2003. if (dec_select)
  2004. filter_coeff = ff_dca_lfe_fir_128;
  2005. else
  2006. filter_coeff = ff_dca_lfe_fir_64;
  2007. // Interpolate LFE channel
  2008. s->dcadsp->lfe_fir_float[dec_select](
  2009. samples, s->lfe_samples + DCA_LFE_HISTORY,
  2010. filter_coeff, s->npcmblocks);
  2011. if (x96_synth) {
  2012. // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
  2013. // (47.6 - 48.0 kHz) components of interpolation image
  2014. s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
  2015. samples, &s->output_history_lfe_float,
  2016. nsamples / 2);
  2017. }
  2018. // Update LFE history
  2019. for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
  2020. s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
  2021. }
  2022. // Undo embedded XCH downmix
  2023. if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
  2024. && s->audio_mode >= AMODE_2F2R) {
  2025. s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
  2026. output_samples[DCA_SPEAKER_Cs],
  2027. -M_SQRT1_2, nsamples);
  2028. s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
  2029. output_samples[DCA_SPEAKER_Cs],
  2030. -M_SQRT1_2, nsamples);
  2031. }
  2032. // Undo embedded XXCH downmix
  2033. if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
  2034. && s->xxch_dmix_embedded) {
  2035. float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
  2036. int *coeff_ptr = s->xxch_dmix_coeff;
  2037. int xch_base = ff_dca_channels[s->audio_mode];
  2038. av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
  2039. // Undo downmix
  2040. for (ch = xch_base; ch < s->nchannels; ch++) {
  2041. int src_spkr = map_prm_ch_to_spkr(s, ch);
  2042. if (src_spkr < 0)
  2043. return AVERROR(EINVAL);
  2044. for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
  2045. if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
  2046. int coeff = *coeff_ptr++;
  2047. if (coeff) {
  2048. s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
  2049. output_samples[src_spkr],
  2050. coeff * (-1.0f / (1 << 15)),
  2051. nsamples);
  2052. }
  2053. }
  2054. }
  2055. }
  2056. // Undo embedded core downmix pre-scaling
  2057. for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
  2058. if (s->xxch_core_mask & (1U << spkr)) {
  2059. s->float_dsp->vector_fmul_scalar(output_samples[spkr],
  2060. output_samples[spkr],
  2061. scale_inv, nsamples);
  2062. }
  2063. }
  2064. }
  2065. if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
  2066. // Front sum/difference decoding
  2067. if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
  2068. || s->audio_mode == AMODE_STEREO_SUMDIFF) {
  2069. s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
  2070. output_samples[DCA_SPEAKER_R],
  2071. nsamples);
  2072. }
  2073. // Surround sum/difference decoding
  2074. if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
  2075. s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
  2076. output_samples[DCA_SPEAKER_Rs],
  2077. nsamples);
  2078. }
  2079. }
  2080. // Downmix primary channel set to stereo
  2081. if (s->request_mask != s->ch_mask) {
  2082. ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
  2083. s->prim_dmix_coeff,
  2084. nsamples, s->ch_mask);
  2085. }
  2086. return 0;
  2087. }
  2088. int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
  2089. {
  2090. AVCodecContext *avctx = s->avctx;
  2091. DCAContext *dca = avctx->priv_data;
  2092. DCAExssAsset *asset = &dca->exss.assets[0];
  2093. enum AVMatrixEncoding matrix_encoding;
  2094. int ret;
  2095. // Handle downmixing to stereo request
  2096. if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
  2097. && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
  2098. && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
  2099. s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
  2100. s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
  2101. else
  2102. s->request_mask = s->ch_mask;
  2103. if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
  2104. return AVERROR(EINVAL);
  2105. // Force fixed point mode when falling back from XLL
  2106. if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
  2107. && (asset->extension_mask & DCA_EXSS_XLL)))
  2108. ret = filter_frame_fixed(s, frame);
  2109. else
  2110. ret = filter_frame_float(s, frame);
  2111. if (ret < 0)
  2112. return ret;
  2113. // Set profile, bit rate, etc
  2114. if (s->ext_audio_mask & DCA_EXSS_MASK)
  2115. avctx->profile = FF_PROFILE_DTS_HD_HRA;
  2116. else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
  2117. avctx->profile = FF_PROFILE_DTS_ES;
  2118. else if (s->ext_audio_mask & DCA_CSS_X96)
  2119. avctx->profile = FF_PROFILE_DTS_96_24;
  2120. else
  2121. avctx->profile = FF_PROFILE_DTS;
  2122. if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
  2123. avctx->bit_rate = s->bit_rate;
  2124. else
  2125. avctx->bit_rate = 0;
  2126. if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
  2127. s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
  2128. matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
  2129. else
  2130. matrix_encoding = AV_MATRIX_ENCODING_NONE;
  2131. if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
  2132. return ret;
  2133. return 0;
  2134. }
  2135. av_cold void ff_dca_core_flush(DCACoreDecoder *s)
  2136. {
  2137. if (s->subband_buffer) {
  2138. erase_adpcm_history(s);
  2139. memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
  2140. }
  2141. if (s->x96_subband_buffer)
  2142. erase_x96_adpcm_history(s);
  2143. erase_dsp_history(s);
  2144. }
  2145. av_cold int ff_dca_core_init(DCACoreDecoder *s)
  2146. {
  2147. dca_init_vlcs();
  2148. if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
  2149. return -1;
  2150. if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
  2151. return -1;
  2152. ff_dcadct_init(&s->dcadct);
  2153. if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
  2154. return -1;
  2155. if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
  2156. return -1;
  2157. ff_synth_filter_init(&s->synth);
  2158. s->x96_rand = 1;
  2159. return 0;
  2160. }
  2161. av_cold void ff_dca_core_close(DCACoreDecoder *s)
  2162. {
  2163. av_freep(&s->float_dsp);
  2164. av_freep(&s->fixed_dsp);
  2165. ff_mdct_end(&s->imdct[0]);
  2166. ff_mdct_end(&s->imdct[1]);
  2167. av_freep(&s->subband_buffer);
  2168. s->subband_size = 0;
  2169. av_freep(&s->x96_subband_buffer);
  2170. s->x96_subband_size = 0;
  2171. av_freep(&s->output_buffer);
  2172. s->output_size = 0;
  2173. }