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.

1500 lines
50KB

  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 "dcamath.h"
  23. #include "dca_syncwords.h"
  24. #include "unary.h"
  25. static int get_linear(GetBitContext *gb, int n)
  26. {
  27. unsigned int v = get_bits_long(gb, n);
  28. return (v >> 1) ^ -(v & 1);
  29. }
  30. static int get_rice_un(GetBitContext *gb, int k)
  31. {
  32. unsigned int v = get_unary(gb, 1, 128);
  33. return (v << k) | get_bits_long(gb, k);
  34. }
  35. static int get_rice(GetBitContext *gb, int k)
  36. {
  37. unsigned int v = get_rice_un(gb, k);
  38. return (v >> 1) ^ -(v & 1);
  39. }
  40. static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
  41. {
  42. int i;
  43. for (i = 0; i < size; i++)
  44. array[i] = get_bits(gb, n);
  45. }
  46. static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
  47. {
  48. int i;
  49. if (n == 0)
  50. memset(array, 0, sizeof(*array) * size);
  51. else for (i = 0; i < size; i++)
  52. array[i] = get_linear(gb, n);
  53. }
  54. static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
  55. {
  56. int i;
  57. for (i = 0; i < size; i++)
  58. array[i] = get_rice(gb, k);
  59. }
  60. static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
  61. {
  62. // Size of downmix coefficient matrix
  63. int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
  64. int i, j, *coeff_ptr = c->dmix_coeff;
  65. for (i = 0; i < m; i++) {
  66. int code, sign, coeff, scale, scale_inv = 0;
  67. unsigned int index;
  68. // Downmix scale (only for non-primary channel sets)
  69. if (!c->primary_chset) {
  70. code = get_bits(&s->gb, 9);
  71. sign = (code >> 8) - 1;
  72. index = (code & 0xff) - FF_DCA_DMIXTABLE_OFFSET;
  73. if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
  74. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");
  75. return AVERROR_INVALIDDATA;
  76. }
  77. scale = ff_dca_dmixtable[index + FF_DCA_DMIXTABLE_OFFSET];
  78. scale_inv = ff_dca_inv_dmixtable[index];
  79. c->dmix_scale[i] = (scale ^ sign) - sign;
  80. c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;
  81. }
  82. // Downmix coefficients
  83. for (j = 0; j < c->nchannels; j++) {
  84. code = get_bits(&s->gb, 9);
  85. sign = (code >> 8) - 1;
  86. index = code & 0xff;
  87. if (index >= FF_DCA_DMIXTABLE_SIZE) {
  88. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. coeff = ff_dca_dmixtable[index];
  92. if (!c->primary_chset)
  93. // Multiply by |InvDmixScale| to get |UndoDmixScale|
  94. coeff = mul16(scale_inv, coeff);
  95. *coeff_ptr++ = (coeff ^ sign) - sign;
  96. }
  97. }
  98. return 0;
  99. }
  100. static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
  101. {
  102. int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
  103. DCAXllChSet *p = &s->chset[0];
  104. DCAXllBand *b;
  105. // Size of channel set sub-header
  106. header_size = get_bits(&s->gb, 10) + 1;
  107. // Check CRC
  108. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  109. && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
  110. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");
  111. return AVERROR_INVALIDDATA;
  112. }
  113. // Number of channels in the channel set
  114. c->nchannels = get_bits(&s->gb, 4) + 1;
  115. if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
  116. avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
  117. return AVERROR_PATCHWELCOME;
  118. }
  119. // Residual type
  120. c->residual_encode = get_bits(&s->gb, c->nchannels);
  121. // PCM bit resolution
  122. c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
  123. // Storage unit width
  124. c->storage_bit_res = get_bits(&s->gb, 5) + 1;
  125. if (c->storage_bit_res != 16 && c->storage_bit_res != 24) {
  126. avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);
  127. return AVERROR_PATCHWELCOME;
  128. }
  129. if (c->pcm_bit_res > c->storage_bit_res) {
  130. av_log(s->avctx, AV_LOG_ERROR, "Invalid PCM bit resolution for XLL channel set (%d > %d)\n", c->pcm_bit_res, c->storage_bit_res);
  131. return AVERROR_INVALIDDATA;
  132. }
  133. // Original sampling frequency
  134. c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
  135. if (c->freq > 192000) {
  136. avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
  137. return AVERROR_PATCHWELCOME;
  138. }
  139. // Sampling frequency modifier
  140. if (get_bits(&s->gb, 2)) {
  141. avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
  142. return AVERROR_PATCHWELCOME;
  143. }
  144. // Which replacement set this channel set is member of
  145. if (get_bits(&s->gb, 2)) {
  146. avpriv_request_sample(s->avctx, "XLL replacement set");
  147. return AVERROR_PATCHWELCOME;
  148. }
  149. if (asset->one_to_one_map_ch_to_spkr) {
  150. // Primary channel set flag
  151. c->primary_chset = get_bits1(&s->gb);
  152. if (c->primary_chset != (c == p)) {
  153. av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");
  154. return AVERROR_INVALIDDATA;
  155. }
  156. // Downmix coefficients present in stream
  157. c->dmix_coeffs_present = get_bits1(&s->gb);
  158. // Downmix already performed by encoder
  159. c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
  160. // Downmix type
  161. if (c->dmix_coeffs_present && c->primary_chset) {
  162. c->dmix_type = get_bits(&s->gb, 3);
  163. if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {
  164. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");
  165. return AVERROR_INVALIDDATA;
  166. }
  167. }
  168. // Whether the channel set is part of a hierarchy
  169. c->hier_chset = get_bits1(&s->gb);
  170. if (!c->hier_chset && s->nchsets != 1) {
  171. avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
  172. return AVERROR_PATCHWELCOME;
  173. }
  174. // Downmix coefficients
  175. if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
  176. return ret;
  177. // Channel mask enabled
  178. if (!get_bits1(&s->gb)) {
  179. avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
  180. return AVERROR_PATCHWELCOME;
  181. }
  182. // Channel mask for set
  183. c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
  184. if (av_popcount(c->ch_mask) != c->nchannels) {
  185. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");
  186. return AVERROR_INVALIDDATA;
  187. }
  188. // Build the channel to speaker map
  189. for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
  190. if (c->ch_mask & (1U << i))
  191. c->ch_remap[j++] = i;
  192. } else {
  193. // Mapping coeffs present flag
  194. if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {
  195. avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");
  196. return AVERROR_PATCHWELCOME;
  197. }
  198. // Setup for LtRt decoding
  199. c->primary_chset = 1;
  200. c->dmix_coeffs_present = 0;
  201. c->dmix_embedded = 0;
  202. c->hier_chset = 0;
  203. c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;
  204. c->ch_remap[0] = DCA_SPEAKER_L;
  205. c->ch_remap[1] = DCA_SPEAKER_R;
  206. }
  207. if (c->freq > 96000) {
  208. // Extra frequency bands flag
  209. if (get_bits1(&s->gb)) {
  210. avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
  211. return AVERROR_PATCHWELCOME;
  212. }
  213. c->nfreqbands = 2;
  214. } else {
  215. c->nfreqbands = 1;
  216. }
  217. // Set the sampling frequency to that of the first frequency band.
  218. // Frequency will be doubled again after bands assembly.
  219. c->freq >>= c->nfreqbands - 1;
  220. // Verify that all channel sets have the same audio characteristics
  221. if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
  222. || c->pcm_bit_res != p->pcm_bit_res
  223. || c->storage_bit_res != p->storage_bit_res)) {
  224. avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
  225. return AVERROR_PATCHWELCOME;
  226. }
  227. // Determine number of bits to read bit allocation coding parameter
  228. if (c->storage_bit_res > 16)
  229. c->nabits = 5;
  230. else if (c->storage_bit_res > 8)
  231. c->nabits = 4;
  232. else
  233. c->nabits = 3;
  234. // Account for embedded downmix and decimator saturation
  235. if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
  236. c->nabits++;
  237. for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
  238. // Pairwise channel decorrelation
  239. if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
  240. int ch_nbits = av_ceil_log2(c->nchannels);
  241. // Original channel order
  242. for (i = 0; i < c->nchannels; i++) {
  243. b->orig_order[i] = get_bits(&s->gb, ch_nbits);
  244. if (b->orig_order[i] >= c->nchannels) {
  245. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");
  246. return AVERROR_INVALIDDATA;
  247. }
  248. }
  249. // Pairwise channel coefficients
  250. for (i = 0; i < c->nchannels / 2; i++)
  251. b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
  252. } else {
  253. for (i = 0; i < c->nchannels; i++)
  254. b->orig_order[i] = i;
  255. for (i = 0; i < c->nchannels / 2; i++)
  256. b->decor_coeff[i] = 0;
  257. }
  258. // Adaptive predictor order
  259. b->highest_pred_order = 0;
  260. for (i = 0; i < c->nchannels; i++) {
  261. b->adapt_pred_order[i] = get_bits(&s->gb, 4);
  262. if (b->adapt_pred_order[i] > b->highest_pred_order)
  263. b->highest_pred_order = b->adapt_pred_order[i];
  264. }
  265. if (b->highest_pred_order > s->nsegsamples) {
  266. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive predicition order\n");
  267. return AVERROR_INVALIDDATA;
  268. }
  269. // Fixed predictor order
  270. for (i = 0; i < c->nchannels; i++)
  271. b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
  272. // Adaptive predictor quantized reflection coefficients
  273. for (i = 0; i < c->nchannels; i++) {
  274. for (j = 0; j < b->adapt_pred_order[i]; j++) {
  275. k = get_linear(&s->gb, 8);
  276. if (k == -128) {
  277. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
  278. return AVERROR_INVALIDDATA;
  279. }
  280. if (k < 0)
  281. b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
  282. else
  283. b->adapt_refl_coeff[i][j] = (int)ff_dca_xll_refl_coeff[ k];
  284. }
  285. }
  286. // Downmix performed by encoder in extension frequency band
  287. b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
  288. // MSB/LSB split flag in extension frequency band
  289. if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
  290. // Size of LSB section in any segment
  291. b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
  292. if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {
  293. av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");
  294. return AVERROR_INVALIDDATA;
  295. }
  296. // Account for optional CRC bytes after LSB section
  297. if (b->lsb_section_size && (s->band_crc_present > 2 ||
  298. (band == 0 && s->band_crc_present > 1)))
  299. b->lsb_section_size += 2;
  300. // Number of bits to represent the samples in LSB part
  301. for (i = 0; i < c->nchannels; i++) {
  302. b->nscalablelsbs[i] = get_bits(&s->gb, 4);
  303. if (b->nscalablelsbs[i] && !b->lsb_section_size) {
  304. av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");
  305. return AVERROR_INVALIDDATA;
  306. }
  307. }
  308. } else {
  309. b->lsb_section_size = 0;
  310. for (i = 0; i < c->nchannels; i++)
  311. b->nscalablelsbs[i] = 0;
  312. }
  313. // Scalable resolution flag in extension frequency band
  314. if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
  315. // Number of bits discarded by authoring
  316. for (i = 0; i < c->nchannels; i++)
  317. b->bit_width_adjust[i] = get_bits(&s->gb, 4);
  318. } else {
  319. for (i = 0; i < c->nchannels; i++)
  320. b->bit_width_adjust[i] = 0;
  321. }
  322. }
  323. // Reserved
  324. // Byte align
  325. // CRC16 of channel set sub-header
  326. if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
  327. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");
  328. return AVERROR_INVALIDDATA;
  329. }
  330. return 0;
  331. }
  332. static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
  333. {
  334. int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
  335. int nchsamples = s->nframesamples + ndecisamples;
  336. int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
  337. int32_t *ptr;
  338. // Reallocate MSB sample buffer
  339. av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
  340. if (!c->sample_buffer[0])
  341. return AVERROR(ENOMEM);
  342. ptr = c->sample_buffer[0] + ndecisamples;
  343. for (i = 0; i < c->nfreqbands; i++) {
  344. for (j = 0; j < c->nchannels; j++) {
  345. c->bands[i].msb_sample_buffer[j] = ptr;
  346. ptr += nchsamples;
  347. }
  348. }
  349. return 0;
  350. }
  351. static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
  352. {
  353. int i, j, nsamples = 0;
  354. int32_t *ptr;
  355. // Determine number of frequency bands that have MSB/LSB split
  356. for (i = 0; i < c->nfreqbands; i++)
  357. if (c->bands[i].lsb_section_size)
  358. nsamples += s->nframesamples * c->nchannels;
  359. if (!nsamples)
  360. return 0;
  361. // Reallocate LSB sample buffer
  362. av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
  363. if (!c->sample_buffer[1])
  364. return AVERROR(ENOMEM);
  365. ptr = c->sample_buffer[1];
  366. for (i = 0; i < c->nfreqbands; i++) {
  367. if (c->bands[i].lsb_section_size) {
  368. for (j = 0; j < c->nchannels; j++) {
  369. c->bands[i].lsb_sample_buffer[j] = ptr;
  370. ptr += s->nframesamples;
  371. }
  372. } else {
  373. for (j = 0; j < c->nchannels; j++)
  374. c->bands[i].lsb_sample_buffer[j] = NULL;
  375. }
  376. }
  377. return 0;
  378. }
  379. static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
  380. {
  381. DCAXllBand *b = &c->bands[band];
  382. int i, j, k;
  383. // Start unpacking MSB portion of the segment
  384. if (!(seg && get_bits1(&s->gb))) {
  385. // Unpack segment type
  386. // 0 - distinct coding parameters for each channel
  387. // 1 - common coding parameters for all channels
  388. c->seg_common = get_bits1(&s->gb);
  389. // Determine number of coding parameters encoded in segment
  390. k = c->seg_common ? 1 : c->nchannels;
  391. // Unpack Rice coding parameters
  392. for (i = 0; i < k; i++) {
  393. // Unpack Rice coding flag
  394. // 0 - linear code, 1 - Rice code
  395. c->rice_code_flag[i] = get_bits1(&s->gb);
  396. if (!c->seg_common && c->rice_code_flag[i]) {
  397. // Unpack Hybrid Rice coding flag
  398. // 0 - Rice code, 1 - Hybrid Rice code
  399. if (get_bits1(&s->gb))
  400. // Unpack binary code length for isolated samples
  401. c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
  402. else
  403. // 0 indicates no Hybrid Rice coding
  404. c->bitalloc_hybrid_linear[i] = 0;
  405. } else {
  406. // 0 indicates no Hybrid Rice coding
  407. c->bitalloc_hybrid_linear[i] = 0;
  408. }
  409. }
  410. // Unpack coding parameters
  411. for (i = 0; i < k; i++) {
  412. if (seg == 0) {
  413. // Unpack coding parameter for part A of segment 0
  414. c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
  415. // Adjust for the linear code
  416. if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
  417. c->bitalloc_part_a[i]++;
  418. if (!c->seg_common)
  419. c->nsamples_part_a[i] = b->adapt_pred_order[i];
  420. else
  421. c->nsamples_part_a[i] = b->highest_pred_order;
  422. } else {
  423. c->bitalloc_part_a[i] = 0;
  424. c->nsamples_part_a[i] = 0;
  425. }
  426. // Unpack coding parameter for part B of segment
  427. c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
  428. // Adjust for the linear code
  429. if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
  430. c->bitalloc_part_b[i]++;
  431. }
  432. }
  433. // Unpack entropy codes
  434. for (i = 0; i < c->nchannels; i++) {
  435. int32_t *part_a, *part_b;
  436. int nsamples_part_b;
  437. // Select index of coding parameters
  438. k = c->seg_common ? 0 : i;
  439. // Slice the segment into parts A and B
  440. part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
  441. part_b = part_a + c->nsamples_part_a[k];
  442. nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
  443. if (get_bits_left(&s->gb) < 0)
  444. return AVERROR_INVALIDDATA;
  445. if (!c->rice_code_flag[k]) {
  446. // Linear codes
  447. // Unpack all residuals of part A of segment 0
  448. get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
  449. c->bitalloc_part_a[k]);
  450. // Unpack all residuals of part B of segment 0 and others
  451. get_linear_array(&s->gb, part_b, nsamples_part_b,
  452. c->bitalloc_part_b[k]);
  453. } else {
  454. // Rice codes
  455. // Unpack all residuals of part A of segment 0
  456. get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
  457. c->bitalloc_part_a[k]);
  458. if (c->bitalloc_hybrid_linear[k]) {
  459. // Hybrid Rice codes
  460. // Unpack the number of isolated samples
  461. int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
  462. // Set all locations to 0
  463. memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
  464. // Extract the locations of isolated samples and flag by -1
  465. for (j = 0; j < nisosamples; j++) {
  466. int loc = get_bits(&s->gb, s->nsegsamples_log2);
  467. if (loc >= nsamples_part_b) {
  468. av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
  469. return AVERROR_INVALIDDATA;
  470. }
  471. part_b[loc] = -1;
  472. }
  473. // Unpack all residuals of part B of segment 0 and others
  474. for (j = 0; j < nsamples_part_b; j++) {
  475. if (part_b[j])
  476. part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
  477. else
  478. part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
  479. }
  480. } else {
  481. // Rice codes
  482. // Unpack all residuals of part B of segment 0 and others
  483. get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
  484. }
  485. }
  486. }
  487. // Unpack decimator history for frequency band 1
  488. if (seg == 0 && band == 1) {
  489. int nbits = get_bits(&s->gb, 5) + 1;
  490. for (i = 0; i < c->nchannels; i++)
  491. for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
  492. c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
  493. }
  494. // Start unpacking LSB portion of the segment
  495. if (b->lsb_section_size) {
  496. // Skip to the start of LSB portion
  497. if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
  498. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
  499. return AVERROR_INVALIDDATA;
  500. }
  501. // Unpack all LSB parts of residuals of this segment
  502. for (i = 0; i < c->nchannels; i++) {
  503. if (b->nscalablelsbs[i]) {
  504. get_array(&s->gb,
  505. b->lsb_sample_buffer[i] + seg * s->nsegsamples,
  506. s->nsegsamples, b->nscalablelsbs[i]);
  507. }
  508. }
  509. }
  510. // Skip to the end of band data
  511. if (ff_dca_seek_bits(&s->gb, band_data_end)) {
  512. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
  513. return AVERROR_INVALIDDATA;
  514. }
  515. return 0;
  516. }
  517. static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
  518. {
  519. DCAXllBand *b = &c->bands[band];
  520. int i, offset, nsamples;
  521. if (seg < 0) {
  522. offset = 0;
  523. nsamples = s->nframesamples;
  524. } else {
  525. offset = seg * s->nsegsamples;
  526. nsamples = s->nsegsamples;
  527. }
  528. for (i = 0; i < c->nchannels; i++) {
  529. memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
  530. if (b->lsb_section_size)
  531. memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
  532. }
  533. if (seg <= 0 && band)
  534. memset(c->deci_history, 0, sizeof(c->deci_history));
  535. if (seg < 0) {
  536. memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
  537. memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
  538. }
  539. }
  540. static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
  541. {
  542. DCAXllBand *b = &c->bands[band];
  543. int nsamples = s->nframesamples;
  544. int i, j, k;
  545. // Inverse adaptive or fixed prediction
  546. for (i = 0; i < c->nchannels; i++) {
  547. int32_t *buf = b->msb_sample_buffer[i];
  548. int order = b->adapt_pred_order[i];
  549. if (order > 0) {
  550. int coeff[DCA_XLL_ADAPT_PRED_ORDER_MAX];
  551. // Conversion from reflection coefficients to direct form coefficients
  552. for (j = 0; j < order; j++) {
  553. int rc = b->adapt_refl_coeff[i][j];
  554. for (k = 0; k < (j + 1) / 2; k++) {
  555. int tmp1 = coeff[ k ];
  556. int tmp2 = coeff[j - k - 1];
  557. coeff[ k ] = tmp1 + mul16(rc, tmp2);
  558. coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
  559. }
  560. coeff[j] = rc;
  561. }
  562. // Inverse adaptive prediction
  563. for (j = 0; j < nsamples - order; j++) {
  564. int64_t err = 0;
  565. for (k = 0; k < order; k++)
  566. err += (int64_t)buf[j + k] * coeff[order - k - 1];
  567. buf[j + k] -= clip23(norm16(err));
  568. }
  569. } else {
  570. // Inverse fixed coefficient prediction
  571. for (j = 0; j < b->fixed_pred_order[i]; j++)
  572. for (k = 1; k < nsamples; k++)
  573. buf[k] += buf[k - 1];
  574. }
  575. }
  576. // Inverse pairwise channel decorrellation
  577. if (b->decor_enabled) {
  578. int32_t *tmp[DCA_XLL_CHANNELS_MAX];
  579. for (i = 0; i < c->nchannels / 2; i++) {
  580. int coeff = b->decor_coeff[i];
  581. if (coeff) {
  582. s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
  583. b->msb_sample_buffer[i * 2 ],
  584. coeff, nsamples);
  585. }
  586. }
  587. // Reorder channel pointers to the original order
  588. for (i = 0; i < c->nchannels; i++)
  589. tmp[i] = b->msb_sample_buffer[i];
  590. for (i = 0; i < c->nchannels; i++)
  591. b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
  592. }
  593. // Map output channel pointers for frequency band 0
  594. if (c->nfreqbands == 1)
  595. for (i = 0; i < c->nchannels; i++)
  596. s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
  597. }
  598. static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
  599. {
  600. int adj = c->bands[band].bit_width_adjust[ch];
  601. int shift = c->bands[band].nscalablelsbs[ch];
  602. if (s->fixed_lsb_width)
  603. shift = s->fixed_lsb_width;
  604. else if (shift && adj)
  605. shift += adj - 1;
  606. else
  607. shift += adj;
  608. return shift;
  609. }
  610. static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
  611. {
  612. DCAXllBand *b = &c->bands[band];
  613. int n, ch, nsamples = s->nframesamples;
  614. for (ch = 0; ch < c->nchannels; ch++) {
  615. int shift = chs_get_lsb_width(s, c, band, ch);
  616. if (shift) {
  617. int32_t *msb = b->msb_sample_buffer[ch];
  618. if (b->nscalablelsbs[ch]) {
  619. int32_t *lsb = b->lsb_sample_buffer[ch];
  620. int adj = b->bit_width_adjust[ch];
  621. for (n = 0; n < nsamples; n++)
  622. msb[n] = msb[n] * (1 << shift) + (lsb[n] << adj);
  623. } else {
  624. for (n = 0; n < nsamples; n++)
  625. msb[n] = msb[n] * (1 << shift);
  626. }
  627. }
  628. }
  629. }
  630. static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
  631. {
  632. int ch, nsamples = s->nframesamples;
  633. int32_t *ptr;
  634. av_assert1(c->nfreqbands > 1);
  635. // Reallocate frequency band assembly buffer
  636. av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
  637. 2 * nsamples * c->nchannels * sizeof(int32_t));
  638. if (!c->sample_buffer[2])
  639. return AVERROR(ENOMEM);
  640. // Assemble frequency bands 0 and 1
  641. ptr = c->sample_buffer[2];
  642. for (ch = 0; ch < c->nchannels; ch++) {
  643. int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
  644. int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
  645. // Copy decimator history
  646. memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
  647. c->deci_history[ch], sizeof(c->deci_history[0]));
  648. // Filter
  649. s->dcadsp->assemble_freq_bands(ptr, band0, band1,
  650. ff_dca_xll_band_coeff,
  651. nsamples);
  652. // Remap output channel pointer to assembly buffer
  653. s->output_samples[c->ch_remap[ch]] = ptr;
  654. ptr += nsamples * 2;
  655. }
  656. return 0;
  657. }
  658. static int parse_common_header(DCAXllDecoder *s)
  659. {
  660. int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
  661. // XLL extension sync word
  662. if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
  663. av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
  664. return AVERROR(EAGAIN);
  665. }
  666. // Version number
  667. stream_ver = get_bits(&s->gb, 4) + 1;
  668. if (stream_ver > 1) {
  669. avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
  670. return AVERROR_PATCHWELCOME;
  671. }
  672. // Lossless frame header length
  673. header_size = get_bits(&s->gb, 8) + 1;
  674. // Check CRC
  675. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  676. && ff_dca_check_crc(&s->gb, 32, header_size * 8)) {
  677. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
  678. return AVERROR_INVALIDDATA;
  679. }
  680. // Number of bits used to read frame size
  681. frame_size_nbits = get_bits(&s->gb, 5) + 1;
  682. // Number of bytes in a lossless frame
  683. s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
  684. if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
  685. av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
  686. return AVERROR_INVALIDDATA;
  687. }
  688. s->frame_size++;
  689. // Number of channels sets per frame
  690. s->nchsets = get_bits(&s->gb, 4) + 1;
  691. if (s->nchsets > DCA_XLL_CHSETS_MAX) {
  692. avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
  693. return AVERROR_PATCHWELCOME;
  694. }
  695. // Number of segments per frame
  696. nframesegs_log2 = get_bits(&s->gb, 4);
  697. s->nframesegs = 1 << nframesegs_log2;
  698. if (s->nframesegs > 1024) {
  699. av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
  700. return AVERROR_INVALIDDATA;
  701. }
  702. // Samples in segment per one frequency band for the first channel set
  703. // Maximum value is 256 for sampling frequencies <= 48 kHz
  704. // Maximum value is 512 for sampling frequencies > 48 kHz
  705. s->nsegsamples_log2 = get_bits(&s->gb, 4);
  706. if (!s->nsegsamples_log2) {
  707. av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
  708. return AVERROR_INVALIDDATA;
  709. }
  710. s->nsegsamples = 1 << s->nsegsamples_log2;
  711. if (s->nsegsamples > 512) {
  712. av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
  713. return AVERROR_INVALIDDATA;
  714. }
  715. // Samples in frame per one frequency band for the first channel set
  716. s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
  717. s->nframesamples = 1 << s->nframesamples_log2;
  718. if (s->nframesamples > 65536) {
  719. av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
  720. return AVERROR_INVALIDDATA;
  721. }
  722. // Number of bits used to read segment size
  723. s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
  724. // Presence of CRC16 within each frequency band
  725. // 0 - No CRC16 within band
  726. // 1 - CRC16 placed at the end of MSB0
  727. // 2 - CRC16 placed at the end of MSB0 and LSB0
  728. // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
  729. s->band_crc_present = get_bits(&s->gb, 2);
  730. // MSB/LSB split flag
  731. s->scalable_lsbs = get_bits1(&s->gb);
  732. // Channel position mask
  733. s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
  734. // Fixed LSB width
  735. if (s->scalable_lsbs)
  736. s->fixed_lsb_width = get_bits(&s->gb, 4);
  737. else
  738. s->fixed_lsb_width = 0;
  739. // Reserved
  740. // Byte align
  741. // Header CRC16 protection
  742. if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
  743. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
  744. return AVERROR_INVALIDDATA;
  745. }
  746. return 0;
  747. }
  748. static int is_hier_dmix_chset(DCAXllChSet *c)
  749. {
  750. return !c->primary_chset && c->dmix_embedded && c->hier_chset;
  751. }
  752. static DCAXllChSet *find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
  753. {
  754. if (c->hier_chset)
  755. while (++c < &s->chset[s->nchsets])
  756. if (is_hier_dmix_chset(c))
  757. return c;
  758. return NULL;
  759. }
  760. static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
  761. {
  762. int i, j, *coeff_ptr = c->dmix_coeff;
  763. for (i = 0; i < c->hier_ofs; i++) {
  764. int scale = o->dmix_scale[i];
  765. int scale_inv = o->dmix_scale_inv[i];
  766. c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
  767. c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
  768. for (j = 0; j < c->nchannels; j++) {
  769. int coeff = mul16(*coeff_ptr, scale_inv);
  770. *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
  771. }
  772. }
  773. }
  774. static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
  775. {
  776. DCAContext *dca = s->avctx->priv_data;
  777. DCAXllChSet *c;
  778. int i, ret;
  779. // Parse channel set headers
  780. s->nfreqbands = 0;
  781. s->nchannels = 0;
  782. s->nreschsets = 0;
  783. for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
  784. c->hier_ofs = s->nchannels;
  785. if ((ret = chs_parse_header(s, c, asset)) < 0)
  786. return ret;
  787. if (c->nfreqbands > s->nfreqbands)
  788. s->nfreqbands = c->nfreqbands;
  789. if (c->hier_chset)
  790. s->nchannels += c->nchannels;
  791. if (c->residual_encode != (1 << c->nchannels) - 1)
  792. s->nreschsets++;
  793. }
  794. // Pre-scale downmixing coefficients for all non-primary channel sets
  795. for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
  796. if (is_hier_dmix_chset(c)) {
  797. DCAXllChSet *o = find_next_hier_dmix_chset(s, c);
  798. if (o)
  799. prescale_down_mix(c, o);
  800. }
  801. }
  802. // Determine number of active channel sets to decode
  803. switch (dca->request_channel_layout) {
  804. case DCA_SPEAKER_LAYOUT_STEREO:
  805. s->nactivechsets = 1;
  806. break;
  807. case DCA_SPEAKER_LAYOUT_5POINT0:
  808. case DCA_SPEAKER_LAYOUT_5POINT1:
  809. s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
  810. break;
  811. default:
  812. s->nactivechsets = s->nchsets;
  813. break;
  814. }
  815. return 0;
  816. }
  817. static int parse_navi_table(DCAXllDecoder *s)
  818. {
  819. int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
  820. DCAXllChSet *c;
  821. // Determine size of NAVI table
  822. navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
  823. if (navi_nb > 1024) {
  824. av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
  825. return AVERROR_INVALIDDATA;
  826. }
  827. // Reallocate NAVI table
  828. av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
  829. if (!s->navi)
  830. return AVERROR(ENOMEM);
  831. // Parse NAVI
  832. navi_pos = get_bits_count(&s->gb);
  833. navi_ptr = s->navi;
  834. for (band = 0; band < s->nfreqbands; band++) {
  835. for (seg = 0; seg < s->nframesegs; seg++) {
  836. for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
  837. int size = 0;
  838. if (c->nfreqbands > band) {
  839. size = get_bits_long(&s->gb, s->seg_size_nbits);
  840. if (size < 0 || size >= s->frame_size) {
  841. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
  842. return AVERROR_INVALIDDATA;
  843. }
  844. size++;
  845. }
  846. *navi_ptr++ = size;
  847. }
  848. }
  849. }
  850. // Byte align
  851. // CRC16
  852. skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
  853. skip_bits(&s->gb, 16);
  854. // Check CRC
  855. if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
  856. && ff_dca_check_crc(&s->gb, navi_pos, get_bits_count(&s->gb))) {
  857. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
  858. return AVERROR_INVALIDDATA;
  859. }
  860. return 0;
  861. }
  862. static int parse_band_data(DCAXllDecoder *s)
  863. {
  864. int ret, chs, seg, band, navi_pos, *navi_ptr;
  865. DCAXllChSet *c;
  866. for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
  867. if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
  868. return ret;
  869. if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
  870. return ret;
  871. }
  872. navi_pos = get_bits_count(&s->gb);
  873. navi_ptr = s->navi;
  874. for (band = 0; band < s->nfreqbands; band++) {
  875. for (seg = 0; seg < s->nframesegs; seg++) {
  876. for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
  877. if (c->nfreqbands > band) {
  878. navi_pos += *navi_ptr * 8;
  879. if (navi_pos > s->gb.size_in_bits) {
  880. av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
  881. return AVERROR_INVALIDDATA;
  882. }
  883. if (chs < s->nactivechsets &&
  884. (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
  885. if (s->avctx->err_recognition & AV_EF_EXPLODE)
  886. return ret;
  887. chs_clear_band_data(s, c, band, seg);
  888. }
  889. s->gb.index = navi_pos;
  890. }
  891. navi_ptr++;
  892. }
  893. }
  894. }
  895. return 0;
  896. }
  897. static int parse_frame(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
  898. {
  899. int ret;
  900. if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
  901. return ret;
  902. if ((ret = parse_common_header(s)) < 0)
  903. return ret;
  904. if ((ret = parse_sub_headers(s, asset)) < 0)
  905. return ret;
  906. if ((ret = parse_navi_table(s)) < 0)
  907. return ret;
  908. if ((ret = parse_band_data(s)) < 0)
  909. return ret;
  910. if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
  911. av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
  912. return AVERROR_INVALIDDATA;
  913. }
  914. return ret;
  915. }
  916. static void clear_pbr(DCAXllDecoder *s)
  917. {
  918. s->pbr_length = 0;
  919. s->pbr_delay = 0;
  920. }
  921. static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
  922. {
  923. if (size > DCA_XLL_PBR_BUFFER_MAX)
  924. return AVERROR(ENOSPC);
  925. if (!s->pbr_buffer && !(s->pbr_buffer = av_malloc(DCA_XLL_PBR_BUFFER_MAX + DCA_BUFFER_PADDING_SIZE)))
  926. return AVERROR(ENOMEM);
  927. memcpy(s->pbr_buffer, data, size);
  928. s->pbr_length = size;
  929. s->pbr_delay = delay;
  930. return 0;
  931. }
  932. static int parse_frame_no_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
  933. {
  934. int ret = parse_frame(s, data, size, asset);
  935. // If XLL packet data didn't start with a sync word, we must have jumped
  936. // right into the middle of PBR smoothing period
  937. if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
  938. // Skip to the next sync word in this packet
  939. data += asset->xll_sync_offset;
  940. size -= asset->xll_sync_offset;
  941. // If decoding delay is set, put the frame into PBR buffer and return
  942. // failure code. Higher level decoder is expected to switch to lossy
  943. // core decoding or mute its output until decoding delay expires.
  944. if (asset->xll_delay_nframes > 0) {
  945. if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
  946. return ret;
  947. return AVERROR(EAGAIN);
  948. }
  949. // No decoding delay, just parse the frame in place
  950. ret = parse_frame(s, data, size, asset);
  951. }
  952. if (ret < 0)
  953. return ret;
  954. if (s->frame_size > size)
  955. return AVERROR(EINVAL);
  956. // If the XLL decoder didn't consume full packet, start PBR smoothing period
  957. if (s->frame_size < size)
  958. if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
  959. return ret;
  960. return 0;
  961. }
  962. static int parse_frame_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
  963. {
  964. int ret;
  965. if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
  966. ret = AVERROR(ENOSPC);
  967. goto fail;
  968. }
  969. memcpy(s->pbr_buffer + s->pbr_length, data, size);
  970. s->pbr_length += size;
  971. // Respect decoding delay after synchronization error
  972. if (s->pbr_delay > 0 && --s->pbr_delay)
  973. return AVERROR(EAGAIN);
  974. if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
  975. goto fail;
  976. if (s->frame_size > s->pbr_length) {
  977. ret = AVERROR(EINVAL);
  978. goto fail;
  979. }
  980. if (s->frame_size == s->pbr_length) {
  981. // End of PBR smoothing period
  982. clear_pbr(s);
  983. } else {
  984. s->pbr_length -= s->frame_size;
  985. memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
  986. }
  987. return 0;
  988. fail:
  989. // For now, throw out all PBR state on failure.
  990. // Perhaps we can be smarter and try to resync somehow.
  991. clear_pbr(s);
  992. return ret;
  993. }
  994. int ff_dca_xll_parse(DCAXllDecoder *s, uint8_t *data, DCAExssAsset *asset)
  995. {
  996. int ret;
  997. if (s->hd_stream_id != asset->hd_stream_id) {
  998. clear_pbr(s);
  999. s->hd_stream_id = asset->hd_stream_id;
  1000. }
  1001. if (s->pbr_length)
  1002. ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
  1003. else
  1004. ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
  1005. return ret;
  1006. }
  1007. static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
  1008. {
  1009. int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
  1010. DCAXllChSet *c;
  1011. for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
  1012. if (!c->hier_chset)
  1013. continue;
  1014. av_assert1(band < c->nfreqbands);
  1015. for (j = 0; j < c->nchannels; j++) {
  1016. for (k = 0; k < o->nchannels; k++) {
  1017. int coeff = *coeff_ptr++;
  1018. if (coeff) {
  1019. s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
  1020. o->bands[band].msb_sample_buffer[k],
  1021. coeff, s->nframesamples);
  1022. if (band)
  1023. s->dcadsp->dmix_sub(c->deci_history[j],
  1024. o->deci_history[k],
  1025. coeff, DCA_XLL_DECI_HISTORY_MAX);
  1026. }
  1027. }
  1028. }
  1029. nchannels += c->nchannels;
  1030. if (nchannels >= o->hier_ofs)
  1031. break;
  1032. }
  1033. }
  1034. static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
  1035. {
  1036. int i, j, nchannels = 0;
  1037. DCAXllChSet *c;
  1038. for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
  1039. if (!c->hier_chset)
  1040. continue;
  1041. av_assert1(band < c->nfreqbands);
  1042. for (j = 0; j < c->nchannels; j++) {
  1043. int scale = o->dmix_scale[nchannels++];
  1044. if (scale != (1 << 15)) {
  1045. s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
  1046. scale, s->nframesamples);
  1047. if (band)
  1048. s->dcadsp->dmix_scale(c->deci_history[j],
  1049. scale, DCA_XLL_DECI_HISTORY_MAX);
  1050. }
  1051. }
  1052. if (nchannels >= o->hier_ofs)
  1053. break;
  1054. }
  1055. }
  1056. // Clear all band data and replace non-residual encoded channels with lossy
  1057. // counterparts
  1058. static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
  1059. {
  1060. DCAContext *dca = s->avctx->priv_data;
  1061. int band, ch;
  1062. for (band = 0; band < c->nfreqbands; band++)
  1063. chs_clear_band_data(s, c, band, -1);
  1064. for (ch = 0; ch < c->nchannels; ch++) {
  1065. if (!(c->residual_encode & (1 << ch)))
  1066. continue;
  1067. if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
  1068. continue;
  1069. c->residual_encode &= ~(1 << ch);
  1070. }
  1071. }
  1072. static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
  1073. {
  1074. DCAContext *dca = s->avctx->priv_data;
  1075. int ch, nsamples = s->nframesamples;
  1076. DCAXllChSet *o;
  1077. // Verify that core is compatible
  1078. if (!(dca->packet & DCA_PACKET_CORE)) {
  1079. av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
  1080. return AVERROR(EINVAL);
  1081. }
  1082. if (c->freq != dca->core.output_rate) {
  1083. av_log(s->avctx, AV_LOG_WARNING, "Sample rate mismatch between core (%d Hz) and XLL (%d Hz)\n", dca->core.output_rate, c->freq);
  1084. return AVERROR_INVALIDDATA;
  1085. }
  1086. if (nsamples != dca->core.npcmsamples) {
  1087. av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
  1088. return AVERROR_INVALIDDATA;
  1089. }
  1090. // See if this channel set is downmixed and find the next channel set in
  1091. // hierarchy. If downmixed, undo core pre-scaling before combining with
  1092. // residual (residual is not scaled).
  1093. o = find_next_hier_dmix_chset(s, c);
  1094. // Reduce core bit width and combine with residual
  1095. for (ch = 0; ch < c->nchannels; ch++) {
  1096. int n, spkr, shift, round;
  1097. int32_t *src, *dst;
  1098. if (c->residual_encode & (1 << ch))
  1099. continue;
  1100. // Map this channel to core speaker
  1101. spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
  1102. if (spkr < 0) {
  1103. av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
  1104. return AVERROR_INVALIDDATA;
  1105. }
  1106. // Account for LSB width
  1107. shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
  1108. if (shift > 24) {
  1109. av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
  1110. return AVERROR_INVALIDDATA;
  1111. }
  1112. round = shift > 0 ? 1 << (shift - 1) : 0;
  1113. src = dca->core.output_samples[spkr];
  1114. dst = c->bands[0].msb_sample_buffer[ch];
  1115. if (o) {
  1116. // Undo embedded core downmix pre-scaling
  1117. int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
  1118. for (n = 0; n < nsamples; n++)
  1119. dst[n] += clip23((mul16(src[n], scale_inv) + round) >> shift);
  1120. } else {
  1121. // No downmix scaling
  1122. for (n = 0; n < nsamples; n++)
  1123. dst[n] += (src[n] + round) >> shift;
  1124. }
  1125. }
  1126. return 0;
  1127. }
  1128. int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
  1129. {
  1130. AVCodecContext *avctx = s->avctx;
  1131. DCAContext *dca = avctx->priv_data;
  1132. DCAExssAsset *asset = &dca->exss.assets[0];
  1133. DCAXllChSet *p = &s->chset[0], *c;
  1134. enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
  1135. int i, j, k, ret, shift, nsamples, request_mask;
  1136. int ch_remap[DCA_SPEAKER_COUNT];
  1137. // Force lossy downmixed output during recovery
  1138. if (dca->packet & DCA_PACKET_RECOVERY) {
  1139. for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
  1140. if (i < s->nactivechsets)
  1141. force_lossy_output(s, c);
  1142. if (!c->primary_chset)
  1143. c->dmix_embedded = 0;
  1144. }
  1145. s->scalable_lsbs = 0;
  1146. s->fixed_lsb_width = 0;
  1147. }
  1148. // Filter frequency bands for active channel sets
  1149. s->output_mask = 0;
  1150. for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
  1151. chs_filter_band_data(s, c, 0);
  1152. if (c->residual_encode != (1 << c->nchannels) - 1
  1153. && (ret = combine_residual_frame(s, c)) < 0)
  1154. return ret;
  1155. if (s->scalable_lsbs)
  1156. chs_assemble_msbs_lsbs(s, c, 0);
  1157. if (c->nfreqbands > 1) {
  1158. chs_filter_band_data(s, c, 1);
  1159. chs_assemble_msbs_lsbs(s, c, 1);
  1160. }
  1161. s->output_mask |= c->ch_mask;
  1162. }
  1163. // Undo hierarchial downmix and/or apply scaling
  1164. for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
  1165. if (!is_hier_dmix_chset(c))
  1166. continue;
  1167. if (i >= s->nactivechsets) {
  1168. for (j = 0; j < c->nfreqbands; j++)
  1169. if (c->bands[j].dmix_embedded)
  1170. scale_down_mix(s, c, j);
  1171. break;
  1172. }
  1173. for (j = 0; j < c->nfreqbands; j++)
  1174. if (c->bands[j].dmix_embedded)
  1175. undo_down_mix(s, c, j);
  1176. }
  1177. // Assemble frequency bands for active channel sets
  1178. if (s->nfreqbands > 1) {
  1179. for (i = 0; i < s->nactivechsets; i++)
  1180. if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
  1181. return ret;
  1182. }
  1183. // Normalize to regular 5.1 layout if downmixing
  1184. if (dca->request_channel_layout) {
  1185. if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
  1186. s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
  1187. s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
  1188. }
  1189. if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
  1190. s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
  1191. s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
  1192. }
  1193. }
  1194. // Handle downmixing to stereo request
  1195. if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
  1196. && DCA_HAS_STEREO(s->output_mask) && p->dmix_embedded
  1197. && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
  1198. p->dmix_type == DCA_DMIX_TYPE_LtRt))
  1199. request_mask = DCA_SPEAKER_LAYOUT_STEREO;
  1200. else
  1201. request_mask = s->output_mask;
  1202. if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
  1203. return AVERROR(EINVAL);
  1204. avctx->sample_rate = p->freq << (s->nfreqbands - 1);
  1205. switch (p->storage_bit_res) {
  1206. case 16:
  1207. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  1208. break;
  1209. case 24:
  1210. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  1211. break;
  1212. default:
  1213. return AVERROR(EINVAL);
  1214. }
  1215. avctx->bits_per_raw_sample = p->storage_bit_res;
  1216. avctx->profile = FF_PROFILE_DTS_HD_MA;
  1217. avctx->bit_rate = 0;
  1218. frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
  1219. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1220. return ret;
  1221. // Downmix primary channel set to stereo
  1222. if (request_mask != s->output_mask) {
  1223. ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
  1224. p->dmix_coeff, nsamples,
  1225. s->output_mask);
  1226. }
  1227. shift = p->storage_bit_res - p->pcm_bit_res;
  1228. for (i = 0; i < avctx->channels; i++) {
  1229. int32_t *samples = s->output_samples[ch_remap[i]];
  1230. if (frame->format == AV_SAMPLE_FMT_S16P) {
  1231. int16_t *plane = (int16_t *)frame->extended_data[i];
  1232. for (k = 0; k < nsamples; k++)
  1233. plane[k] = av_clip_int16(samples[k] * (1 << shift));
  1234. } else {
  1235. int32_t *plane = (int32_t *)frame->extended_data[i];
  1236. for (k = 0; k < nsamples; k++)
  1237. plane[k] = clip23(samples[k] * (1 << shift)) * (1 << 8);
  1238. }
  1239. }
  1240. if (!asset->one_to_one_map_ch_to_spkr) {
  1241. if (asset->representation_type == DCA_REPR_TYPE_LtRt)
  1242. matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
  1243. else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
  1244. matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
  1245. } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
  1246. matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
  1247. }
  1248. if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
  1249. return ret;
  1250. return 0;
  1251. }
  1252. av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
  1253. {
  1254. clear_pbr(s);
  1255. }
  1256. av_cold void ff_dca_xll_close(DCAXllDecoder *s)
  1257. {
  1258. DCAXllChSet *c;
  1259. int i, j;
  1260. for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
  1261. for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
  1262. av_freep(&c->sample_buffer[j]);
  1263. c->sample_size[j] = 0;
  1264. }
  1265. }
  1266. av_freep(&s->navi);
  1267. s->navi_size = 0;
  1268. av_freep(&s->pbr_buffer);
  1269. clear_pbr(s);
  1270. }