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.

748 lines
32KB

  1. /*
  2. * DCA XLL extension
  3. *
  4. * Copyright (C) 2012 Paul B Mahol
  5. * Copyright (C) 2014 Niels Möller
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/internal.h"
  26. #include "avcodec.h"
  27. #include "dca.h"
  28. #include "dcadata.h"
  29. #include "get_bits.h"
  30. #include "unary.h"
  31. /* Sign as bit 0 */
  32. static inline int get_bits_sm(GetBitContext *s, unsigned n)
  33. {
  34. int x = get_bits(s, n);
  35. if (x & 1)
  36. return -(x >> 1) - 1;
  37. else
  38. return x >> 1;
  39. }
  40. /* Return -1 on error. */
  41. static int32_t get_dmix_coeff(DCAContext *s, int inverse)
  42. {
  43. unsigned code = get_bits(&s->gb, 9);
  44. int32_t sign = (int32_t) (code >> 8) - 1;
  45. unsigned idx = code & 0xff;
  46. int inv_offset = FF_DCA_DMIXTABLE_SIZE -FF_DCA_INV_DMIXTABLE_SIZE;
  47. if (idx >= FF_DCA_DMIXTABLE_SIZE) {
  48. av_log(s->avctx, AV_LOG_ERROR,
  49. "XLL: Invalid channel set downmix code %x\n", code);
  50. return -1;
  51. } else if (!inverse) {
  52. return (ff_dca_dmixtable[idx] ^ sign) - sign;
  53. } else if (idx < inv_offset) {
  54. av_log(s->avctx, AV_LOG_ERROR,
  55. "XLL: Invalid channel set inverse downmix code %x\n", code);
  56. return -1;
  57. } else {
  58. return (ff_dca_inv_dmixtable[idx - inv_offset] ^ sign) - sign;
  59. }
  60. }
  61. static int32_t dca_get_dmix_coeff(DCAContext *s)
  62. {
  63. return get_dmix_coeff(s, 0);
  64. }
  65. static int32_t dca_get_inv_dmix_coeff(DCAContext *s)
  66. {
  67. return get_dmix_coeff(s, 1);
  68. }
  69. /* parse XLL header */
  70. int ff_dca_xll_decode_header(DCAContext *s)
  71. {
  72. int hdr_pos, hdr_size;
  73. av_unused int version, frame_size;
  74. int i, chset_index;
  75. /* get bit position of sync header */
  76. hdr_pos = get_bits_count(&s->gb) - 32;
  77. version = get_bits(&s->gb, 4) + 1;
  78. hdr_size = get_bits(&s->gb, 8) + 1;
  79. frame_size = get_bits_long(&s->gb, get_bits(&s->gb, 5) + 1) + 1;
  80. s->xll_channels =
  81. s->xll_residual_channels = 0;
  82. s->xll_nch_sets = get_bits(&s->gb, 4) + 1;
  83. s->xll_segments = 1 << get_bits(&s->gb, 4);
  84. s->xll_log_smpl_in_seg = get_bits(&s->gb, 4);
  85. s->xll_smpl_in_seg = 1 << s->xll_log_smpl_in_seg;
  86. s->xll_bits4seg_size = get_bits(&s->gb, 5) + 1;
  87. s->xll_banddata_crc = get_bits(&s->gb, 2);
  88. s->xll_scalable_lsb = get_bits1(&s->gb);
  89. s->xll_bits4ch_mask = get_bits(&s->gb, 5) + 1;
  90. if (s->xll_scalable_lsb) {
  91. s->xll_fixed_lsb_width = get_bits(&s->gb, 4);
  92. if (s->xll_fixed_lsb_width)
  93. av_log(s->avctx, AV_LOG_WARNING,
  94. "XLL: fixed lsb width = %d, non-zero not supported.\n",
  95. s->xll_fixed_lsb_width);
  96. }
  97. /* skip to the end of the common header */
  98. i = get_bits_count(&s->gb);
  99. if (hdr_pos + hdr_size * 8 > i)
  100. skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
  101. for (chset_index = 0; chset_index < s->xll_nch_sets; chset_index++) {
  102. XllChSetSubHeader *chset = &s->xll_chsets[chset_index];
  103. hdr_pos = get_bits_count(&s->gb);
  104. hdr_size = get_bits(&s->gb, 10) + 1;
  105. chset->channels = get_bits(&s->gb, 4) + 1;
  106. chset->residual_encode = get_bits(&s->gb, chset->channels);
  107. chset->bit_resolution = get_bits(&s->gb, 5) + 1;
  108. chset->bit_width = get_bits(&s->gb, 5) + 1;
  109. chset->sampling_frequency = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
  110. chset->samp_freq_interp = get_bits(&s->gb, 2);
  111. chset->replacement_set = get_bits(&s->gb, 2);
  112. if (chset->replacement_set)
  113. chset->active_replace_set = get_bits(&s->gb, 1);
  114. if (s->one2one_map_chtospkr) {
  115. chset->primary_ch_set = get_bits(&s->gb, 1);
  116. chset->downmix_coeff_code_embedded = get_bits(&s->gb, 1);
  117. if (chset->downmix_coeff_code_embedded) {
  118. chset->downmix_embedded = get_bits(&s->gb, 1);
  119. if (chset->primary_ch_set) {
  120. chset->downmix_type = get_bits(&s->gb, 3);
  121. if (chset->downmix_type > 6) {
  122. av_log(s->avctx, AV_LOG_ERROR,
  123. "XLL: Invalid channel set downmix type\n");
  124. return AVERROR_INVALIDDATA;
  125. }
  126. }
  127. }
  128. chset->hier_chset = get_bits(&s->gb, 1);
  129. if (chset->downmix_coeff_code_embedded) {
  130. /* nDownmixCoeffs is specified as N * M. For a primary
  131. * channel set, it appears that N = number of
  132. * channels, and M is the number of downmix channels.
  133. *
  134. * For a non-primary channel set, N is specified as
  135. * number of channels + 1, and M is derived from the
  136. * channel set hierarchy, and at least in simple cases
  137. * M is the number of channels in preceding channel
  138. * sets. */
  139. if (chset->primary_ch_set) {
  140. static const char dmix_table[7] = { 1, 2, 2, 3, 3, 4, 4 };
  141. chset->downmix_ncoeffs = chset->channels * dmix_table[chset->downmix_type];
  142. } else
  143. chset->downmix_ncoeffs = (chset->channels + 1) * s->xll_channels;
  144. if (chset->downmix_ncoeffs > DCA_XLL_DMIX_NCOEFFS_MAX) {
  145. avpriv_request_sample(s->avctx,
  146. "XLL: More than %d downmix coefficients",
  147. DCA_XLL_DMIX_NCOEFFS_MAX);
  148. return AVERROR_PATCHWELCOME;
  149. } else if (chset->primary_ch_set) {
  150. for (i = 0; i < chset->downmix_ncoeffs; i++)
  151. if ((chset->downmix_coeffs[i] = dca_get_dmix_coeff(s)) == -1)
  152. return AVERROR_INVALIDDATA;
  153. } else {
  154. unsigned c, r;
  155. for (c = 0, i = 0; c < s->xll_channels; c++, i += chset->channels + 1) {
  156. if ((chset->downmix_coeffs[i] = dca_get_inv_dmix_coeff(s)) == -1)
  157. return AVERROR_INVALIDDATA;
  158. for (r = 1; r <= chset->channels; r++) {
  159. int32_t coeff = dca_get_dmix_coeff(s);
  160. if (coeff == -1)
  161. return AVERROR_INVALIDDATA;
  162. chset->downmix_coeffs[i + r] =
  163. (chset->downmix_coeffs[i] * (int64_t) coeff + (1 << 15)) >> 16;
  164. }
  165. }
  166. }
  167. }
  168. chset->ch_mask_enabled = get_bits(&s->gb, 1);
  169. if (chset->ch_mask_enabled)
  170. chset->ch_mask = get_bits(&s->gb, s->xll_bits4ch_mask);
  171. else
  172. /* Skip speaker configuration bits */
  173. skip_bits_long(&s->gb, 25 * chset->channels);
  174. } else {
  175. chset->primary_ch_set = 1;
  176. chset->downmix_coeff_code_embedded = 0;
  177. /* Spec: NumChHierChSet = 0, NumDwnMixCodeCoeffs = 0, whatever that means. */
  178. chset->mapping_coeffs_present = get_bits(&s->gb, 1);
  179. if (chset->mapping_coeffs_present) {
  180. avpriv_report_missing_feature(s->avctx, "XLL: mapping coefficients");
  181. return AVERROR_PATCHWELCOME;
  182. }
  183. }
  184. if (chset->sampling_frequency > 96000)
  185. chset->num_freq_bands = 2 * (1 + get_bits(&s->gb, 1));
  186. else
  187. chset->num_freq_bands = 1;
  188. if (chset->num_freq_bands > 1) {
  189. avpriv_report_missing_feature(s->avctx, "XLL: num_freq_bands > 1");
  190. return AVERROR_PATCHWELCOME;
  191. }
  192. if (get_bits(&s->gb, 1)) { /* pw_ch_decor_enabled */
  193. int bits = av_ceil_log2(chset->channels);
  194. for (i = 0; i < chset->channels; i++) {
  195. unsigned j = get_bits(&s->gb, bits);
  196. if (j >= chset->channels) {
  197. av_log(s->avctx, AV_LOG_ERROR,
  198. "Original channel order value %u too large, only %d channels.\n",
  199. j, chset->channels);
  200. return AVERROR_INVALIDDATA;
  201. }
  202. chset->orig_chan_order[0][i] = j;
  203. chset->orig_chan_order_inv[0][j] = i;
  204. }
  205. for (i = 0; i < chset->channels / 2; i++) {
  206. if (get_bits(&s->gb, 1)) /* bChPFlag */
  207. chset->pw_ch_pairs_coeffs[0][i] = get_bits_sm(&s->gb, 7);
  208. else
  209. chset->pw_ch_pairs_coeffs[0][i] = 0;
  210. }
  211. } else {
  212. for (i = 0; i < chset->channels; i++)
  213. chset->orig_chan_order[0][i] =
  214. chset->orig_chan_order_inv[0][i] = i;
  215. for (i = 0; i < chset->channels / 2; i++)
  216. chset->pw_ch_pairs_coeffs[0][i] = 0;
  217. }
  218. /* Adaptive prediction order */
  219. chset->adapt_order_max[0] = 0;
  220. for (i = 0; i < chset->channels; i++) {
  221. chset->adapt_order[0][i] = get_bits(&s->gb, 4);
  222. if (chset->adapt_order_max[0] < chset->adapt_order[0][i])
  223. chset->adapt_order_max[0] = chset->adapt_order[0][i];
  224. }
  225. /* Fixed prediction order, used in case the adaptive order
  226. * above is zero */
  227. for (i = 0; i < chset->channels; i++)
  228. chset->fixed_order[0][i] =
  229. chset->adapt_order[0][i] ? 0 : get_bits(&s->gb, 2);
  230. for (i = 0; i < chset->channels; i++) {
  231. unsigned j;
  232. for (j = 0; j < chset->adapt_order[0][i]; j++)
  233. chset->lpc_refl_coeffs_q_ind[0][i][j] = get_bits(&s->gb, 8);
  234. }
  235. if (s->xll_scalable_lsb) {
  236. chset->lsb_fsize[0] = get_bits(&s->gb, s->xll_bits4seg_size);
  237. for (i = 0; i < chset->channels; i++)
  238. chset->scalable_lsbs[0][i] = get_bits(&s->gb, 4);
  239. for (i = 0; i < chset->channels; i++)
  240. chset->bit_width_adj_per_ch[0][i] = get_bits(&s->gb, 4);
  241. } else {
  242. memset(chset->scalable_lsbs[0], 0,
  243. chset->channels * sizeof(chset->scalable_lsbs[0][0]));
  244. memset(chset->bit_width_adj_per_ch[0], 0,
  245. chset->channels * sizeof(chset->bit_width_adj_per_ch[0][0]));
  246. }
  247. s->xll_channels += chset->channels;
  248. s->xll_residual_channels += chset->channels -
  249. av_popcount(chset->residual_encode);
  250. /* FIXME: Parse header data for extra frequency bands. */
  251. /* Skip to end of channel set sub header. */
  252. i = get_bits_count(&s->gb);
  253. if (hdr_pos + 8 * hdr_size < i) {
  254. av_log(s->avctx, AV_LOG_ERROR,
  255. "chset header too large, %d bits, should be <= %d bits\n",
  256. i - hdr_pos, 8 * hdr_size);
  257. return AVERROR_INVALIDDATA;
  258. }
  259. if (hdr_pos + 8 * hdr_size > i)
  260. skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
  261. }
  262. return 0;
  263. }
  264. /* parse XLL navigation table */
  265. int ff_dca_xll_decode_navi(DCAContext *s, int asset_end)
  266. {
  267. int nbands, band, chset, seg, data_start;
  268. /* FIXME: Supports only a single frequency band */
  269. nbands = 1;
  270. for (band = 0; band < nbands; band++) {
  271. s->xll_navi.band_size[band] = 0;
  272. for (seg = 0; seg < s->xll_segments; seg++) {
  273. /* Note: The spec, ETSI TS 102 114 V1.4.1 (2012-09), says
  274. * we should read a base value for segment_size from the
  275. * stream, before reading the sizes of the channel sets.
  276. * But that's apparently incorrect. */
  277. s->xll_navi.segment_size[band][seg] = 0;
  278. for (chset = 0; chset < s->xll_nch_sets; chset++)
  279. if (band < s->xll_chsets[chset].num_freq_bands) {
  280. s->xll_navi.chset_size[band][seg][chset] =
  281. get_bits(&s->gb, s->xll_bits4seg_size) + 1;
  282. s->xll_navi.segment_size[band][seg] +=
  283. s->xll_navi.chset_size[band][seg][chset];
  284. }
  285. s->xll_navi.band_size[band] += s->xll_navi.segment_size[band][seg];
  286. }
  287. }
  288. /* Align to 8 bits and skip 16-bit CRC. */
  289. skip_bits_long(&s->gb, 16 + ((-get_bits_count(&s->gb)) & 7));
  290. data_start = get_bits_count(&s->gb);
  291. if (data_start + 8 * s->xll_navi.band_size[0] > asset_end) {
  292. av_log(s->avctx, AV_LOG_ERROR,
  293. "XLL: Data in NAVI table exceeds containing asset\n"
  294. "start: %d (bit), size %u (bytes), end %d (bit), error %u\n",
  295. data_start, s->xll_navi.band_size[0], asset_end,
  296. data_start + 8 * s->xll_navi.band_size[0] - asset_end);
  297. return AVERROR_INVALIDDATA;
  298. }
  299. init_get_bits(&s->xll_navi.gb, s->gb.buffer + data_start / 8,
  300. 8 * s->xll_navi.band_size[0]);
  301. return 0;
  302. }
  303. static void dca_xll_inv_adapt_pred(int *samples, int nsamples, unsigned order,
  304. const int *prev, const uint8_t *q_ind)
  305. {
  306. static const uint16_t table[0x81] = {
  307. 0, 3070, 5110, 7140, 9156, 11154, 13132, 15085,
  308. 17010, 18904, 20764, 22588, 24373, 26117, 27818, 29474,
  309. 31085, 32648, 34164, 35631, 37049, 38418, 39738, 41008,
  310. 42230, 43404, 44530, 45609, 46642, 47630, 48575, 49477,
  311. 50337, 51157, 51937, 52681, 53387, 54059, 54697, 55302,
  312. 55876, 56421, 56937, 57426, 57888, 58326, 58741, 59132,
  313. 59502, 59852, 60182, 60494, 60789, 61066, 61328, 61576,
  314. 61809, 62029, 62236, 62431, 62615, 62788, 62951, 63105,
  315. 63250, 63386, 63514, 63635, 63749, 63855, 63956, 64051,
  316. 64140, 64224, 64302, 64376, 64446, 64512, 64573, 64631,
  317. 64686, 64737, 64785, 64830, 64873, 64913, 64950, 64986,
  318. 65019, 65050, 65079, 65107, 65133, 65157, 65180, 65202,
  319. 65222, 65241, 65259, 65275, 65291, 65306, 65320, 65333,
  320. 65345, 65357, 65368, 65378, 65387, 65396, 65405, 65413,
  321. 65420, 65427, 65434, 65440, 65446, 65451, 65456, 65461,
  322. 65466, 65470, 65474, 65478, 65481, 65485, 65488, 65491,
  323. 65535, /* Final value is for the -128 corner case, see below. */
  324. };
  325. int c[DCA_XLL_AORDER_MAX];
  326. int64_t s;
  327. unsigned i, j;
  328. for (i = 0; i < order; i++) {
  329. if (q_ind[i] & 1)
  330. /* The index value 0xff corresponds to a lookup of entry 0x80 in
  331. * the table, and no value is provided in the specification. */
  332. c[i] = -table[(q_ind[i] >> 1) + 1];
  333. else
  334. c[i] = table[q_ind[i] >> 1];
  335. }
  336. /* The description in the spec is a bit convoluted. We can convert
  337. * the reflected values to direct values in place, using a
  338. * sequence of reflections operating on two values. */
  339. for (i = 1; i < order; i++) {
  340. /* i = 1: scale c[0]
  341. * i = 2: reflect c[0] <-> c[1]
  342. * i = 3: scale c[1], reflect c[0] <-> c[2]
  343. * i = 4: reflect c[0] <-> c[3] reflect c[1] <-> c[2]
  344. * ... */
  345. if (i & 1)
  346. c[i / 2] += ((int64_t) c[i] * c[i / 2] + 0x8000) >> 16;
  347. for (j = 0; j < i / 2; j++) {
  348. int r0 = c[j];
  349. int r1 = c[i - j - 1];
  350. c[j] += ((int64_t) c[i] * r1 + 0x8000) >> 16;
  351. c[i - j - 1] += ((int64_t) c[i] * r0 + 0x8000) >> 16;
  352. }
  353. }
  354. /* Apply predictor. */
  355. /* NOTE: Processing samples in this order means that the
  356. * predictor is applied to the newly reconstructed samples. */
  357. if (prev) {
  358. for (i = 0; i < order; i++) {
  359. for (j = s = 0; j < i; j++)
  360. s += (int64_t) c[j] * samples[i - 1 - j];
  361. for (; j < order; j++)
  362. s += (int64_t) c[j] * prev[DCA_XLL_AORDER_MAX + i - 1 - j];
  363. samples[i] -= av_clip((s + 0x8000) >> 16, -0x1000000, 0xffffff);
  364. }
  365. }
  366. for (i = order; i < nsamples; i++) {
  367. for (j = s = 0; j < order; j++)
  368. s += (int64_t) c[j] * samples[i - 1 - j];
  369. /* NOTE: Equations seem to imply addition, while the
  370. * pseudocode seems to use subtraction.*/
  371. samples[i] -= av_clip((s + 0x8000) >> 16, -0x1000000, 0xffffff);
  372. }
  373. }
  374. int ff_dca_xll_decode_audio(DCAContext *s, AVFrame *frame)
  375. {
  376. /* FIXME: Decodes only the first frequency band. */
  377. int seg, chset_i;
  378. /* Coding parameters for each channel set. */
  379. struct coding_params {
  380. int seg_type;
  381. int rice_code_flag[16];
  382. int pancAuxABIT[16];
  383. int pancABIT0[16]; /* Not sure what this is */
  384. int pancABIT[16]; /* Not sure what this is */
  385. int nSamplPart0[16];
  386. } param_state[16];
  387. GetBitContext *gb = &s->xll_navi.gb;
  388. int *history;
  389. /* Layout: First the sample buffer for one segment per channel,
  390. * followed by history buffers of DCA_XLL_AORDER_MAX samples for
  391. * each channel. */
  392. av_fast_malloc(&s->xll_sample_buf, &s->xll_sample_buf_size,
  393. (s->xll_smpl_in_seg + DCA_XLL_AORDER_MAX) *
  394. s->xll_channels * sizeof(*s->xll_sample_buf));
  395. if (!s->xll_sample_buf)
  396. return AVERROR(ENOMEM);
  397. history = s->xll_sample_buf + s->xll_smpl_in_seg * s->xll_channels;
  398. for (seg = 0; seg < s->xll_segments; seg++) {
  399. unsigned in_channel;
  400. for (chset_i = in_channel = 0; chset_i < s->xll_nch_sets; chset_i++) {
  401. /* The spec isn't very explicit, but I think the NAVI sizes are in bytes. */
  402. int end_pos = get_bits_count(gb) +
  403. 8 * s->xll_navi.chset_size[0][seg][chset_i];
  404. int i, j;
  405. struct coding_params *params = &param_state[chset_i];
  406. /* I think this flag means that we should keep seg_type and
  407. * other parameters from the previous segment. */
  408. int use_seg_state_code_param;
  409. XllChSetSubHeader *chset = &s->xll_chsets[chset_i];
  410. if (in_channel >= s->avctx->channels)
  411. /* FIXME: Could go directly to next segment */
  412. goto next_chset;
  413. if (s->avctx->sample_rate != chset->sampling_frequency) {
  414. av_log(s->avctx, AV_LOG_WARNING,
  415. "XLL: unexpected chset sample rate %d, expected %d\n",
  416. chset->sampling_frequency, s->avctx->sample_rate);
  417. goto next_chset;
  418. }
  419. if (seg != 0)
  420. use_seg_state_code_param = get_bits(gb, 1);
  421. else
  422. use_seg_state_code_param = 0;
  423. if (!use_seg_state_code_param) {
  424. int num_param_sets, i;
  425. unsigned bits4ABIT;
  426. params->seg_type = get_bits(gb, 1);
  427. num_param_sets = params->seg_type ? 1 : chset->channels;
  428. if (chset->bit_width > 16) {
  429. bits4ABIT = 5;
  430. } else {
  431. if (chset->bit_width > 8)
  432. bits4ABIT = 4;
  433. else
  434. bits4ABIT = 3;
  435. if (s->xll_nch_sets > 1)
  436. bits4ABIT++;
  437. }
  438. for (i = 0; i < num_param_sets; i++) {
  439. params->rice_code_flag[i] = get_bits(gb, 1);
  440. if (!params->seg_type && params->rice_code_flag[i] && get_bits(gb, 1))
  441. params->pancAuxABIT[i] = get_bits(gb, bits4ABIT) + 1;
  442. else
  443. params->pancAuxABIT[i] = 0;
  444. }
  445. for (i = 0; i < num_param_sets; i++) {
  446. if (!seg) {
  447. /* Parameters for part 1 */
  448. params->pancABIT0[i] = get_bits(gb, bits4ABIT);
  449. if (params->rice_code_flag[i] == 0 && params->pancABIT0[i] > 0)
  450. /* For linear code */
  451. params->pancABIT0[i]++;
  452. /* NOTE: In the spec, not indexed by band??? */
  453. if (params->seg_type == 0)
  454. params->nSamplPart0[i] = chset->adapt_order[0][i];
  455. else
  456. params->nSamplPart0[i] = chset->adapt_order_max[0];
  457. } else
  458. params->nSamplPart0[i] = 0;
  459. /* Parameters for part 2 */
  460. params->pancABIT[i] = get_bits(gb, bits4ABIT);
  461. if (params->rice_code_flag[i] == 0 && params->pancABIT[i] > 0)
  462. /* For linear code */
  463. params->pancABIT[i]++;
  464. }
  465. }
  466. for (i = 0; i < chset->channels; i++) {
  467. int param_index = params->seg_type ? 0 : i;
  468. int part0 = params->nSamplPart0[param_index];
  469. int bits = part0 ? params->pancABIT0[param_index] : 0;
  470. int *sample_buf = s->xll_sample_buf +
  471. (in_channel + i) * s->xll_smpl_in_seg;
  472. if (!params->rice_code_flag[param_index]) {
  473. /* Linear code */
  474. if (bits)
  475. for (j = 0; j < part0; j++)
  476. sample_buf[j] = get_bits_sm(gb, bits);
  477. else
  478. memset(sample_buf, 0, part0 * sizeof(sample_buf[0]));
  479. /* Second part */
  480. bits = params->pancABIT[param_index];
  481. if (bits)
  482. for (j = part0; j < s->xll_smpl_in_seg; j++)
  483. sample_buf[j] = get_bits_sm(gb, bits);
  484. else
  485. memset(sample_buf + part0, 0,
  486. (s->xll_smpl_in_seg - part0) * sizeof(sample_buf[0]));
  487. } else {
  488. int aux_bits = params->pancAuxABIT[param_index];
  489. for (j = 0; j < part0; j++) {
  490. /* FIXME: Is this identical to Golomb code? */
  491. int t = get_unary(gb, 1, 33) << bits;
  492. /* FIXME: Could move this test outside of the loop, for efficiency. */
  493. if (bits)
  494. t |= get_bits(gb, bits);
  495. sample_buf[j] = (t & 1) ? -(t >> 1) - 1 : (t >> 1);
  496. }
  497. /* Second part */
  498. bits = params->pancABIT[param_index];
  499. /* Follow the spec's suggestion of using the
  500. * buffer also to store the hybrid-rice flags. */
  501. memset(sample_buf + part0, 0,
  502. (s->xll_smpl_in_seg - part0) * sizeof(sample_buf[0]));
  503. if (aux_bits > 0) {
  504. /* For hybrid rice encoding, some samples are linearly
  505. * coded. According to the spec, "nBits4SamplLoci" bits
  506. * are used for each index, but this value is not
  507. * defined. I guess we should use log2(xll_smpl_in_seg)
  508. * bits. */
  509. int count = get_bits(gb, s->xll_log_smpl_in_seg);
  510. av_log(s->avctx, AV_LOG_DEBUG, "aux count %d (bits %d)\n",
  511. count, s->xll_log_smpl_in_seg);
  512. for (j = 0; j < count; j++)
  513. sample_buf[get_bits(gb, s->xll_log_smpl_in_seg)] = 1;
  514. }
  515. for (j = part0; j < s->xll_smpl_in_seg; j++) {
  516. if (!sample_buf[j]) {
  517. int t = get_unary(gb, 1, 33);
  518. if (bits)
  519. t = (t << bits) | get_bits(gb, bits);
  520. sample_buf[j] = (t & 1) ? -(t >> 1) - 1 : (t >> 1);
  521. } else
  522. sample_buf[j] = get_bits_sm(gb, aux_bits);
  523. }
  524. }
  525. }
  526. for (i = 0; i < chset->channels; i++) {
  527. unsigned adapt_order = chset->adapt_order[0][i];
  528. int *sample_buf = s->xll_sample_buf +
  529. (in_channel + i) * s->xll_smpl_in_seg;
  530. int *prev = history + (in_channel + i) * DCA_XLL_AORDER_MAX;
  531. if (!adapt_order) {
  532. unsigned order;
  533. for (order = chset->fixed_order[0][i]; order > 0; order--) {
  534. unsigned j;
  535. for (j = 1; j < s->xll_smpl_in_seg; j++)
  536. sample_buf[j] += sample_buf[j - 1];
  537. }
  538. } else
  539. /* Inverse adaptive prediction, in place. */
  540. dca_xll_inv_adapt_pred(sample_buf, s->xll_smpl_in_seg,
  541. adapt_order, seg ? prev : NULL,
  542. chset->lpc_refl_coeffs_q_ind[0][i]);
  543. memcpy(prev, sample_buf + s->xll_smpl_in_seg - DCA_XLL_AORDER_MAX,
  544. DCA_XLL_AORDER_MAX * sizeof(*prev));
  545. }
  546. for (i = 1; i < chset->channels; i += 2) {
  547. int coeff = chset->pw_ch_pairs_coeffs[0][i / 2];
  548. if (coeff != 0) {
  549. int *sample_buf = s->xll_sample_buf +
  550. (in_channel + i) * s->xll_smpl_in_seg;
  551. int *prev = sample_buf - s->xll_smpl_in_seg;
  552. unsigned j;
  553. for (j = 0; j < s->xll_smpl_in_seg; j++)
  554. /* Shift is unspecified, but should apparently be 3. */
  555. sample_buf[j] += ((int64_t) coeff * prev[j] + 4) >> 3;
  556. }
  557. }
  558. if (s->xll_scalable_lsb) {
  559. int lsb_start = end_pos - 8 * chset->lsb_fsize[0] -
  560. 8 * (s->xll_banddata_crc & 2);
  561. int done;
  562. i = get_bits_count(gb);
  563. if (i > lsb_start) {
  564. av_log(s->avctx, AV_LOG_ERROR,
  565. "chset data lsb exceeds NAVI size, end_pos %d, lsb_start %d, pos %d\n",
  566. end_pos, lsb_start, i);
  567. return AVERROR_INVALIDDATA;
  568. }
  569. if (i < lsb_start)
  570. skip_bits_long(gb, lsb_start - i);
  571. for (i = done = 0; i < chset->channels; i++) {
  572. int bits = chset->scalable_lsbs[0][i];
  573. if (bits > 0) {
  574. /* The channel reordering is conceptually done
  575. * before adding the lsb:s, so we need to do
  576. * the inverse permutation here. */
  577. unsigned pi = chset->orig_chan_order_inv[0][i];
  578. int *sample_buf = s->xll_sample_buf +
  579. (in_channel + pi) * s->xll_smpl_in_seg;
  580. int adj = chset->bit_width_adj_per_ch[0][i];
  581. int msb_shift = bits;
  582. unsigned j;
  583. if (adj > 0)
  584. msb_shift += adj - 1;
  585. for (j = 0; j < s->xll_smpl_in_seg; j++)
  586. sample_buf[j] = (sample_buf[j] << msb_shift) +
  587. (get_bits(gb, bits) << adj);
  588. done += bits * s->xll_smpl_in_seg;
  589. }
  590. }
  591. if (done > 8 * chset->lsb_fsize[0]) {
  592. av_log(s->avctx, AV_LOG_ERROR,
  593. "chset lsb exceeds lsb_size\n");
  594. return AVERROR_INVALIDDATA;
  595. }
  596. }
  597. /* Store output. */
  598. for (i = 0; i < chset->channels; i++) {
  599. int *sample_buf = s->xll_sample_buf +
  600. (in_channel + i) * s->xll_smpl_in_seg;
  601. int shift = 1 - chset->bit_resolution;
  602. int out_channel = chset->orig_chan_order[0][i];
  603. float *out;
  604. /* XLL uses the channel order C, L, R, and we want L,
  605. * R, C. FIXME: Generalize. */
  606. if (chset->ch_mask_enabled &&
  607. (chset->ch_mask & 7) == 7 && out_channel < 3)
  608. out_channel = out_channel ? out_channel - 1 : 2;
  609. out_channel += in_channel;
  610. if (out_channel >= s->avctx->channels)
  611. continue;
  612. out = (float *) frame->extended_data[out_channel];
  613. out += seg * s->xll_smpl_in_seg;
  614. /* NOTE: A one bit means residual encoding is *not* used. */
  615. if ((chset->residual_encode >> i) & 1) {
  616. /* Replace channel samples.
  617. * FIXME: Most likely not the right thing to do. */
  618. for (j = 0; j < s->xll_smpl_in_seg; j++)
  619. out[j] = ldexpf(sample_buf[j], shift);
  620. } else {
  621. /* Add residual signal to core channel */
  622. for (j = 0; j < s->xll_smpl_in_seg; j++)
  623. out[j] += ldexpf(sample_buf[j], shift);
  624. }
  625. }
  626. if (chset->downmix_coeff_code_embedded &&
  627. !chset->primary_ch_set && chset->hier_chset) {
  628. /* Undo hierarchical downmix of earlier channels. */
  629. unsigned mix_channel;
  630. for (mix_channel = 0; mix_channel < in_channel; mix_channel++) {
  631. float *mix_buf;
  632. const int *col;
  633. float coeff;
  634. unsigned row;
  635. /* Similar channel reorder C, L, R vs L, R, C reorder. */
  636. if (chset->ch_mask_enabled &&
  637. (chset->ch_mask & 7) == 7 && mix_channel < 3)
  638. mix_buf = (float *) frame->extended_data[mix_channel ? mix_channel - 1 : 2];
  639. else
  640. mix_buf = (float *) frame->extended_data[mix_channel];
  641. mix_buf += seg * s->xll_smpl_in_seg;
  642. col = &chset->downmix_coeffs[mix_channel * (chset->channels + 1)];
  643. /* Scale */
  644. coeff = ldexpf(col[0], -16);
  645. for (j = 0; j < s->xll_smpl_in_seg; j++)
  646. mix_buf[j] *= coeff;
  647. for (row = 0;
  648. row < chset->channels && in_channel + row < s->avctx->channels;
  649. row++)
  650. if (col[row + 1]) {
  651. const float *new_channel =
  652. (const float *) frame->extended_data[in_channel + row];
  653. new_channel += seg * s->xll_smpl_in_seg;
  654. coeff = ldexpf(col[row + 1], -15);
  655. for (j = 0; j < s->xll_smpl_in_seg; j++)
  656. mix_buf[j] -= coeff * new_channel[j];
  657. }
  658. }
  659. }
  660. next_chset:
  661. in_channel += chset->channels;
  662. /* Skip to next channel set using the NAVI info. */
  663. i = get_bits_count(gb);
  664. if (i > end_pos) {
  665. av_log(s->avctx, AV_LOG_ERROR,
  666. "chset data exceeds NAVI size\n");
  667. return AVERROR_INVALIDDATA;
  668. }
  669. if (i < end_pos)
  670. skip_bits_long(gb, end_pos - i);
  671. }
  672. }
  673. return 0;
  674. }