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.

1492 lines
49KB

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