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.

774 lines
26KB

  1. /*
  2. * TwinVQ decoder
  3. * Copyright (c) 2009 Vitor Sessak
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include "libavutil/channel_layout.h"
  24. #include "libavutil/float_dsp.h"
  25. #include "avcodec.h"
  26. #include "fft.h"
  27. #include "internal.h"
  28. #include "lsp.h"
  29. #include "sinewin.h"
  30. #include "twinvq.h"
  31. /**
  32. * Evaluate a single LPC amplitude spectrum envelope coefficient from the line
  33. * spectrum pairs.
  34. *
  35. * @param lsp a vector of the cosine of the LSP values
  36. * @param cos_val cos(PI*i/N) where i is the index of the LPC amplitude
  37. * @param order the order of the LSP (and the size of the *lsp buffer). Must
  38. * be a multiple of four.
  39. * @return the LPC value
  40. *
  41. * @todo reuse code from Vorbis decoder: vorbis_floor0_decode
  42. */
  43. static float eval_lpc_spectrum(const float *lsp, float cos_val, int order)
  44. {
  45. int j;
  46. float p = 0.5f;
  47. float q = 0.5f;
  48. float two_cos_w = 2.0f * cos_val;
  49. for (j = 0; j + 1 < order; j += 2 * 2) {
  50. // Unroll the loop once since order is a multiple of four
  51. q *= lsp[j] - two_cos_w;
  52. p *= lsp[j + 1] - two_cos_w;
  53. q *= lsp[j + 2] - two_cos_w;
  54. p *= lsp[j + 3] - two_cos_w;
  55. }
  56. p *= p * (2.0f - two_cos_w);
  57. q *= q * (2.0f + two_cos_w);
  58. return 0.5 / (p + q);
  59. }
  60. /**
  61. * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
  62. */
  63. static void eval_lpcenv(TwinVQContext *tctx, const float *cos_vals, float *lpc)
  64. {
  65. int i;
  66. const TwinVQModeTab *mtab = tctx->mtab;
  67. int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
  68. for (i = 0; i < size_s / 2; i++) {
  69. float cos_i = tctx->cos_tabs[0][i];
  70. lpc[i] = eval_lpc_spectrum(cos_vals, cos_i, mtab->n_lsp);
  71. lpc[size_s - i - 1] = eval_lpc_spectrum(cos_vals, -cos_i, mtab->n_lsp);
  72. }
  73. }
  74. static void interpolate(float *out, float v1, float v2, int size)
  75. {
  76. int i;
  77. float step = (v1 - v2) / (size + 1);
  78. for (i = 0; i < size; i++) {
  79. v2 += step;
  80. out[i] = v2;
  81. }
  82. }
  83. static inline float get_cos(int idx, int part, const float *cos_tab, int size)
  84. {
  85. return part ? -cos_tab[size - idx - 1]
  86. : cos_tab[idx];
  87. }
  88. /**
  89. * Evaluate the LPC amplitude spectrum envelope from the line spectrum pairs.
  90. * Probably for speed reasons, the coefficients are evaluated as
  91. * siiiibiiiisiiiibiiiisiiiibiiiisiiiibiiiis ...
  92. * where s is an evaluated value, i is a value interpolated from the others
  93. * and b might be either calculated or interpolated, depending on an
  94. * unexplained condition.
  95. *
  96. * @param step the size of a block "siiiibiiii"
  97. * @param in the cosine of the LSP data
  98. * @param part is 0 for 0...PI (positive cosine values) and 1 for PI...2PI
  99. * (negative cosine values)
  100. * @param size the size of the whole output
  101. */
  102. static inline void eval_lpcenv_or_interp(TwinVQContext *tctx,
  103. enum TwinVQFrameType ftype,
  104. float *out, const float *in,
  105. int size, int step, int part)
  106. {
  107. int i;
  108. const TwinVQModeTab *mtab = tctx->mtab;
  109. const float *cos_tab = tctx->cos_tabs[ftype];
  110. // Fill the 's'
  111. for (i = 0; i < size; i += step)
  112. out[i] =
  113. eval_lpc_spectrum(in,
  114. get_cos(i, part, cos_tab, size),
  115. mtab->n_lsp);
  116. // Fill the 'iiiibiiii'
  117. for (i = step; i <= size - 2 * step; i += step) {
  118. if (out[i + step] + out[i - step] > 1.95 * out[i] ||
  119. out[i + step] >= out[i - step]) {
  120. interpolate(out + i - step + 1, out[i], out[i - step], step - 1);
  121. } else {
  122. out[i - step / 2] =
  123. eval_lpc_spectrum(in,
  124. get_cos(i - step / 2, part, cos_tab, size),
  125. mtab->n_lsp);
  126. interpolate(out + i - step + 1, out[i - step / 2],
  127. out[i - step], step / 2 - 1);
  128. interpolate(out + i - step / 2 + 1, out[i],
  129. out[i - step / 2], step / 2 - 1);
  130. }
  131. }
  132. interpolate(out + size - 2 * step + 1, out[size - step],
  133. out[size - 2 * step], step - 1);
  134. }
  135. static void eval_lpcenv_2parts(TwinVQContext *tctx, enum TwinVQFrameType ftype,
  136. const float *buf, float *lpc,
  137. int size, int step)
  138. {
  139. eval_lpcenv_or_interp(tctx, ftype, lpc, buf, size / 2, step, 0);
  140. eval_lpcenv_or_interp(tctx, ftype, lpc + size / 2, buf, size / 2,
  141. 2 * step, 1);
  142. interpolate(lpc + size / 2 - step + 1, lpc[size / 2],
  143. lpc[size / 2 - step], step);
  144. twinvq_memset_float(lpc + size - 2 * step + 1, lpc[size - 2 * step],
  145. 2 * step - 1);
  146. }
  147. /**
  148. * Inverse quantization. Read CB coefficients for cb1 and cb2 from the
  149. * bitstream, sum the corresponding vectors and write the result to *out
  150. * after permutation.
  151. */
  152. static void dequant(TwinVQContext *tctx, const uint8_t *cb_bits, float *out,
  153. enum TwinVQFrameType ftype,
  154. const int16_t *cb0, const int16_t *cb1, int cb_len)
  155. {
  156. int pos = 0;
  157. int i, j;
  158. for (i = 0; i < tctx->n_div[ftype]; i++) {
  159. int tmp0, tmp1;
  160. int sign0 = 1;
  161. int sign1 = 1;
  162. const int16_t *tab0, *tab1;
  163. int length = tctx->length[ftype][i >= tctx->length_change[ftype]];
  164. int bitstream_second_part = (i >= tctx->bits_main_spec_change[ftype]);
  165. int bits = tctx->bits_main_spec[0][ftype][bitstream_second_part];
  166. tmp0 = *cb_bits++;
  167. if (bits == 7) {
  168. if (tmp0 & 0x40)
  169. sign0 = -1;
  170. tmp0 &= 0x3F;
  171. }
  172. bits = tctx->bits_main_spec[1][ftype][bitstream_second_part];
  173. tmp1 = *cb_bits++;
  174. if (bits == 7) {
  175. if (tmp1 & 0x40)
  176. sign1 = -1;
  177. tmp1 &= 0x3F;
  178. }
  179. tab0 = cb0 + tmp0 * cb_len;
  180. tab1 = cb1 + tmp1 * cb_len;
  181. for (j = 0; j < length; j++)
  182. out[tctx->permut[ftype][pos + j]] = sign0 * tab0[j] +
  183. sign1 * tab1[j];
  184. pos += length;
  185. }
  186. }
  187. static void dec_gain(TwinVQContext *tctx,
  188. enum TwinVQFrameType ftype, float *out)
  189. {
  190. const TwinVQModeTab *mtab = tctx->mtab;
  191. const TwinVQFrameData *bits = &tctx->bits;
  192. int i, j;
  193. int sub = mtab->fmode[ftype].sub;
  194. float step = TWINVQ_AMP_MAX / ((1 << TWINVQ_GAIN_BITS) - 1);
  195. float sub_step = TWINVQ_SUB_AMP_MAX / ((1 << TWINVQ_SUB_GAIN_BITS) - 1);
  196. if (ftype == TWINVQ_FT_LONG) {
  197. for (i = 0; i < tctx->avctx->channels; i++)
  198. out[i] = (1.0 / (1 << 13)) *
  199. twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
  200. TWINVQ_AMP_MAX, TWINVQ_MULAW_MU);
  201. } else {
  202. for (i = 0; i < tctx->avctx->channels; i++) {
  203. float val = (1.0 / (1 << 23)) *
  204. twinvq_mulawinv(step * 0.5 + step * bits->gain_bits[i],
  205. TWINVQ_AMP_MAX, TWINVQ_MULAW_MU);
  206. for (j = 0; j < sub; j++)
  207. out[i * sub + j] =
  208. val * twinvq_mulawinv(sub_step * 0.5 +
  209. sub_step * bits->sub_gain_bits[i * sub + j],
  210. TWINVQ_SUB_AMP_MAX, TWINVQ_MULAW_MU);
  211. }
  212. }
  213. }
  214. /**
  215. * Rearrange the LSP coefficients so that they have a minimum distance of
  216. * min_dist. This function does it exactly as described in section of 3.2.4
  217. * of the G.729 specification (but interestingly is different from what the
  218. * reference decoder actually does).
  219. */
  220. static void rearrange_lsp(int order, float *lsp, float min_dist)
  221. {
  222. int i;
  223. float min_dist2 = min_dist * 0.5;
  224. for (i = 1; i < order; i++)
  225. if (lsp[i] - lsp[i - 1] < min_dist) {
  226. float avg = (lsp[i] + lsp[i - 1]) * 0.5;
  227. lsp[i - 1] = avg - min_dist2;
  228. lsp[i] = avg + min_dist2;
  229. }
  230. }
  231. static void decode_lsp(TwinVQContext *tctx, int lpc_idx1, uint8_t *lpc_idx2,
  232. int lpc_hist_idx, float *lsp, float *hist)
  233. {
  234. const TwinVQModeTab *mtab = tctx->mtab;
  235. int i, j;
  236. const float *cb = mtab->lspcodebook;
  237. const float *cb2 = cb + (1 << mtab->lsp_bit1) * mtab->n_lsp;
  238. const float *cb3 = cb2 + (1 << mtab->lsp_bit2) * mtab->n_lsp;
  239. const int8_t funny_rounding[4] = {
  240. -2,
  241. mtab->lsp_split == 4 ? -2 : 1,
  242. mtab->lsp_split == 4 ? -2 : 1,
  243. 0
  244. };
  245. j = 0;
  246. for (i = 0; i < mtab->lsp_split; i++) {
  247. int chunk_end = ((i + 1) * mtab->n_lsp + funny_rounding[i]) /
  248. mtab->lsp_split;
  249. for (; j < chunk_end; j++)
  250. lsp[j] = cb[lpc_idx1 * mtab->n_lsp + j] +
  251. cb2[lpc_idx2[i] * mtab->n_lsp + j];
  252. }
  253. rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
  254. for (i = 0; i < mtab->n_lsp; i++) {
  255. float tmp1 = 1.0 - cb3[lpc_hist_idx * mtab->n_lsp + i];
  256. float tmp2 = hist[i] * cb3[lpc_hist_idx * mtab->n_lsp + i];
  257. hist[i] = lsp[i];
  258. lsp[i] = lsp[i] * tmp1 + tmp2;
  259. }
  260. rearrange_lsp(mtab->n_lsp, lsp, 0.0001);
  261. rearrange_lsp(mtab->n_lsp, lsp, 0.000095);
  262. ff_sort_nearly_sorted_floats(lsp, mtab->n_lsp);
  263. }
  264. static void dec_lpc_spectrum_inv(TwinVQContext *tctx, float *lsp,
  265. enum TwinVQFrameType ftype, float *lpc)
  266. {
  267. int i;
  268. int size = tctx->mtab->size / tctx->mtab->fmode[ftype].sub;
  269. for (i = 0; i < tctx->mtab->n_lsp; i++)
  270. lsp[i] = 2 * cos(lsp[i]);
  271. switch (ftype) {
  272. case TWINVQ_FT_LONG:
  273. eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 8);
  274. break;
  275. case TWINVQ_FT_MEDIUM:
  276. eval_lpcenv_2parts(tctx, ftype, lsp, lpc, size, 2);
  277. break;
  278. case TWINVQ_FT_SHORT:
  279. eval_lpcenv(tctx, lsp, lpc);
  280. break;
  281. }
  282. }
  283. static const uint8_t wtype_to_wsize[] = { 0, 0, 2, 2, 2, 1, 0, 1, 1 };
  284. static void imdct_and_window(TwinVQContext *tctx, enum TwinVQFrameType ftype,
  285. int wtype, float *in, float *prev, int ch)
  286. {
  287. FFTContext *mdct = &tctx->mdct_ctx[ftype];
  288. const TwinVQModeTab *mtab = tctx->mtab;
  289. int bsize = mtab->size / mtab->fmode[ftype].sub;
  290. int size = mtab->size;
  291. float *buf1 = tctx->tmp_buf;
  292. int j, first_wsize, wsize; // Window size
  293. float *out = tctx->curr_frame + 2 * ch * mtab->size;
  294. float *out2 = out;
  295. float *prev_buf;
  296. int types_sizes[] = {
  297. mtab->size / mtab->fmode[TWINVQ_FT_LONG].sub,
  298. mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub,
  299. mtab->size / (mtab->fmode[TWINVQ_FT_SHORT].sub * 2),
  300. };
  301. wsize = types_sizes[wtype_to_wsize[wtype]];
  302. first_wsize = wsize;
  303. prev_buf = prev + (size - bsize) / 2;
  304. for (j = 0; j < mtab->fmode[ftype].sub; j++) {
  305. int sub_wtype = ftype == TWINVQ_FT_MEDIUM ? 8 : wtype;
  306. if (!j && wtype == 4)
  307. sub_wtype = 4;
  308. else if (j == mtab->fmode[ftype].sub - 1 && wtype == 7)
  309. sub_wtype = 7;
  310. wsize = types_sizes[wtype_to_wsize[sub_wtype]];
  311. mdct->imdct_half(mdct, buf1 + bsize * j, in + bsize * j);
  312. tctx->fdsp.vector_fmul_window(out2, prev_buf + (bsize - wsize) / 2,
  313. buf1 + bsize * j,
  314. ff_sine_windows[av_log2(wsize)],
  315. wsize / 2);
  316. out2 += wsize;
  317. memcpy(out2, buf1 + bsize * j + wsize / 2,
  318. (bsize - wsize / 2) * sizeof(float));
  319. out2 += ftype == TWINVQ_FT_MEDIUM ? (bsize - wsize) / 2 : bsize - wsize;
  320. prev_buf = buf1 + bsize * j + bsize / 2;
  321. }
  322. tctx->last_block_pos[ch] = (size + first_wsize) / 2;
  323. }
  324. static void imdct_output(TwinVQContext *tctx, enum TwinVQFrameType ftype,
  325. int wtype, float **out)
  326. {
  327. const TwinVQModeTab *mtab = tctx->mtab;
  328. float *prev_buf = tctx->prev_frame + tctx->last_block_pos[0];
  329. int size1, size2, i;
  330. for (i = 0; i < tctx->avctx->channels; i++)
  331. imdct_and_window(tctx, ftype, wtype,
  332. tctx->spectrum + i * mtab->size,
  333. prev_buf + 2 * i * mtab->size,
  334. i);
  335. if (!out)
  336. return;
  337. size2 = tctx->last_block_pos[0];
  338. size1 = mtab->size - size2;
  339. memcpy(&out[0][0], prev_buf, size1 * sizeof(out[0][0]));
  340. memcpy(&out[0][size1], tctx->curr_frame, size2 * sizeof(out[0][0]));
  341. if (tctx->avctx->channels == 2) {
  342. memcpy(&out[1][0], &prev_buf[2 * mtab->size],
  343. size1 * sizeof(out[1][0]));
  344. memcpy(&out[1][size1], &tctx->curr_frame[2 * mtab->size],
  345. size2 * sizeof(out[1][0]));
  346. tctx->fdsp.butterflies_float(out[0], out[1], mtab->size);
  347. }
  348. }
  349. static void read_and_decode_spectrum(TwinVQContext *tctx, float *out,
  350. enum TwinVQFrameType ftype)
  351. {
  352. const TwinVQModeTab *mtab = tctx->mtab;
  353. TwinVQFrameData *bits = &tctx->bits;
  354. int channels = tctx->avctx->channels;
  355. int sub = mtab->fmode[ftype].sub;
  356. int block_size = mtab->size / sub;
  357. float gain[TWINVQ_CHANNELS_MAX * TWINVQ_SUBBLOCKS_MAX];
  358. float ppc_shape[TWINVQ_PPC_SHAPE_LEN_MAX * TWINVQ_CHANNELS_MAX * 4];
  359. int i, j;
  360. dequant(tctx, bits->main_coeffs, out, ftype,
  361. mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
  362. mtab->fmode[ftype].cb_len_read);
  363. dec_gain(tctx, ftype, gain);
  364. if (ftype == TWINVQ_FT_LONG) {
  365. int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len * channels - 1) /
  366. tctx->n_div[3];
  367. dequant(tctx, bits->ppc_coeffs, ppc_shape,
  368. TWINVQ_FT_PPC, mtab->ppc_shape_cb,
  369. mtab->ppc_shape_cb + cb_len_p * TWINVQ_PPC_SHAPE_CB_SIZE,
  370. cb_len_p);
  371. }
  372. for (i = 0; i < channels; i++) {
  373. float *chunk = out + mtab->size * i;
  374. float lsp[TWINVQ_LSP_COEFS_MAX];
  375. for (j = 0; j < sub; j++) {
  376. tctx->dec_bark_env(tctx, bits->bark1[i][j],
  377. bits->bark_use_hist[i][j], i,
  378. tctx->tmp_buf, gain[sub * i + j], ftype);
  379. tctx->fdsp.vector_fmul(chunk + block_size * j,
  380. chunk + block_size * j,
  381. tctx->tmp_buf, block_size);
  382. }
  383. if (ftype == TWINVQ_FT_LONG)
  384. tctx->decode_ppc(tctx, bits->p_coef[i], bits->g_coef[i],
  385. ppc_shape + i * mtab->ppc_shape_len, chunk);
  386. decode_lsp(tctx, bits->lpc_idx1[i], bits->lpc_idx2[i],
  387. bits->lpc_hist_idx[i], lsp, tctx->lsp_hist[i]);
  388. dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
  389. for (j = 0; j < mtab->fmode[ftype].sub; j++) {
  390. tctx->fdsp.vector_fmul(chunk, chunk, tctx->tmp_buf, block_size);
  391. chunk += block_size;
  392. }
  393. }
  394. }
  395. const enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[] = {
  396. TWINVQ_FT_LONG, TWINVQ_FT_LONG, TWINVQ_FT_SHORT, TWINVQ_FT_LONG,
  397. TWINVQ_FT_MEDIUM, TWINVQ_FT_LONG, TWINVQ_FT_LONG, TWINVQ_FT_MEDIUM,
  398. TWINVQ_FT_MEDIUM
  399. };
  400. int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
  401. int *got_frame_ptr, AVPacket *avpkt)
  402. {
  403. AVFrame *frame = data;
  404. const uint8_t *buf = avpkt->data;
  405. int buf_size = avpkt->size;
  406. TwinVQContext *tctx = avctx->priv_data;
  407. const TwinVQModeTab *mtab = tctx->mtab;
  408. float **out = NULL;
  409. int ret;
  410. /* get output buffer */
  411. if (tctx->discarded_packets >= 2) {
  412. frame->nb_samples = mtab->size;
  413. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  414. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  415. return ret;
  416. }
  417. out = (float **)frame->extended_data;
  418. }
  419. if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
  420. return ret;
  421. read_and_decode_spectrum(tctx, tctx->spectrum, tctx->bits.ftype);
  422. imdct_output(tctx, tctx->bits.ftype, tctx->bits.window_type, out);
  423. FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
  424. if (tctx->discarded_packets < 2) {
  425. tctx->discarded_packets++;
  426. *got_frame_ptr = 0;
  427. return buf_size;
  428. }
  429. *got_frame_ptr = 1;
  430. return buf_size;
  431. }
  432. /**
  433. * Init IMDCT and windowing tables
  434. */
  435. static av_cold int init_mdct_win(TwinVQContext *tctx)
  436. {
  437. int i, j, ret;
  438. const TwinVQModeTab *mtab = tctx->mtab;
  439. int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
  440. int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
  441. int channels = tctx->avctx->channels;
  442. float norm = channels == 1 ? 2.0 : 1.0;
  443. for (i = 0; i < 3; i++) {
  444. int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
  445. if ((ret = ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
  446. -sqrt(norm / bsize) / (1 << 15))))
  447. return ret;
  448. }
  449. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->tmp_buf,
  450. mtab->size * sizeof(*tctx->tmp_buf), alloc_fail);
  451. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->spectrum,
  452. 2 * mtab->size * channels * sizeof(*tctx->spectrum),
  453. alloc_fail);
  454. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->curr_frame,
  455. 2 * mtab->size * channels * sizeof(*tctx->curr_frame),
  456. alloc_fail);
  457. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->prev_frame,
  458. 2 * mtab->size * channels * sizeof(*tctx->prev_frame),
  459. alloc_fail);
  460. for (i = 0; i < 3; i++) {
  461. int m = 4 * mtab->size / mtab->fmode[i].sub;
  462. double freq = 2 * M_PI / m;
  463. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
  464. (m / 4) * sizeof(*tctx->cos_tabs[i]), alloc_fail);
  465. for (j = 0; j <= m / 8; j++)
  466. tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
  467. for (j = 1; j < m / 8; j++)
  468. tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
  469. }
  470. ff_init_ff_sine_windows(av_log2(size_m));
  471. ff_init_ff_sine_windows(av_log2(size_s / 2));
  472. ff_init_ff_sine_windows(av_log2(mtab->size));
  473. return 0;
  474. alloc_fail:
  475. return AVERROR(ENOMEM);
  476. }
  477. /**
  478. * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
  479. * each line do a cyclic permutation, i.e.
  480. * abcdefghijklm -> defghijklmabc
  481. * where the amount to be shifted is evaluated depending on the column.
  482. */
  483. static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
  484. int block_size,
  485. const uint8_t line_len[2], int length_div,
  486. enum TwinVQFrameType ftype)
  487. {
  488. int i, j;
  489. for (i = 0; i < line_len[0]; i++) {
  490. int shift;
  491. if (num_blocks == 1 ||
  492. (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
  493. (ftype != TWINVQ_FT_LONG && num_vect & 1) ||
  494. i == line_len[1]) {
  495. shift = 0;
  496. } else if (ftype == TWINVQ_FT_LONG) {
  497. shift = i;
  498. } else
  499. shift = i * i;
  500. for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
  501. tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
  502. }
  503. }
  504. /**
  505. * Interpret the input data as in the following table:
  506. *
  507. * @verbatim
  508. *
  509. * abcdefgh
  510. * ijklmnop
  511. * qrstuvw
  512. * x123456
  513. *
  514. * @endverbatim
  515. *
  516. * and transpose it, giving the output
  517. * aiqxbjr1cks2dlt3emu4fvn5gow6hp
  518. */
  519. static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
  520. const uint8_t line_len[2], int length_div)
  521. {
  522. int i, j;
  523. int cont = 0;
  524. for (i = 0; i < num_vect; i++)
  525. for (j = 0; j < line_len[i >= length_div]; j++)
  526. out[cont++] = in[j * num_vect + i];
  527. }
  528. static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
  529. {
  530. int block_size = size / n_blocks;
  531. int i;
  532. for (i = 0; i < size; i++)
  533. out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
  534. }
  535. static av_cold void construct_perm_table(TwinVQContext *tctx,
  536. enum TwinVQFrameType ftype)
  537. {
  538. int block_size, size;
  539. const TwinVQModeTab *mtab = tctx->mtab;
  540. int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
  541. if (ftype == TWINVQ_FT_PPC) {
  542. size = tctx->avctx->channels;
  543. block_size = mtab->ppc_shape_len;
  544. } else {
  545. size = tctx->avctx->channels * mtab->fmode[ftype].sub;
  546. block_size = mtab->size / mtab->fmode[ftype].sub;
  547. }
  548. permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
  549. block_size, tctx->length[ftype],
  550. tctx->length_change[ftype], ftype);
  551. transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
  552. tctx->length[ftype], tctx->length_change[ftype]);
  553. linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
  554. size * block_size);
  555. }
  556. static av_cold void init_bitstream_params(TwinVQContext *tctx)
  557. {
  558. const TwinVQModeTab *mtab = tctx->mtab;
  559. int n_ch = tctx->avctx->channels;
  560. int total_fr_bits = tctx->avctx->bit_rate * mtab->size /
  561. tctx->avctx->sample_rate;
  562. int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
  563. mtab->lsp_split * mtab->lsp_bit2);
  564. int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
  565. mtab->ppc_period_bit);
  566. int bsize_no_main_cb[3], bse_bits[3], i;
  567. enum TwinVQFrameType frametype;
  568. for (i = 0; i < 3; i++)
  569. // +1 for history usage switch
  570. bse_bits[i] = n_ch *
  571. (mtab->fmode[i].bark_n_coef *
  572. mtab->fmode[i].bark_n_bit + 1);
  573. bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
  574. TWINVQ_WINDOW_TYPE_BITS + n_ch * TWINVQ_GAIN_BITS;
  575. for (i = 0; i < 2; i++)
  576. bsize_no_main_cb[i] =
  577. lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
  578. TWINVQ_WINDOW_TYPE_BITS +
  579. mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
  580. if (tctx->codec == TWINVQ_CODEC_METASOUND) {
  581. bsize_no_main_cb[1] += 2;
  582. bsize_no_main_cb[2] += 2;
  583. }
  584. // The remaining bits are all used for the main spectrum coefficients
  585. for (i = 0; i < 4; i++) {
  586. int bit_size, vect_size;
  587. int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
  588. if (i == 3) {
  589. bit_size = n_ch * mtab->ppc_shape_bit;
  590. vect_size = n_ch * mtab->ppc_shape_len;
  591. } else {
  592. bit_size = total_fr_bits - bsize_no_main_cb[i];
  593. vect_size = n_ch * mtab->size;
  594. }
  595. tctx->n_div[i] = (bit_size + 13) / 14;
  596. rounded_up = (bit_size + tctx->n_div[i] - 1) /
  597. tctx->n_div[i];
  598. rounded_down = (bit_size) / tctx->n_div[i];
  599. num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
  600. num_rounded_up = tctx->n_div[i] - num_rounded_down;
  601. tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;
  602. tctx->bits_main_spec[1][i][0] = rounded_up / 2;
  603. tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;
  604. tctx->bits_main_spec[1][i][1] = rounded_down / 2;
  605. tctx->bits_main_spec_change[i] = num_rounded_up;
  606. rounded_up = (vect_size + tctx->n_div[i] - 1) /
  607. tctx->n_div[i];
  608. rounded_down = (vect_size) / tctx->n_div[i];
  609. num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
  610. num_rounded_up = tctx->n_div[i] - num_rounded_down;
  611. tctx->length[i][0] = rounded_up;
  612. tctx->length[i][1] = rounded_down;
  613. tctx->length_change[i] = num_rounded_up;
  614. }
  615. for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
  616. construct_perm_table(tctx, frametype);
  617. }
  618. av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
  619. {
  620. TwinVQContext *tctx = avctx->priv_data;
  621. int i;
  622. for (i = 0; i < 3; i++) {
  623. ff_mdct_end(&tctx->mdct_ctx[i]);
  624. av_free(tctx->cos_tabs[i]);
  625. }
  626. av_free(tctx->curr_frame);
  627. av_free(tctx->spectrum);
  628. av_free(tctx->prev_frame);
  629. av_free(tctx->tmp_buf);
  630. return 0;
  631. }
  632. av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
  633. {
  634. int ret;
  635. TwinVQContext *tctx = avctx->priv_data;
  636. tctx->avctx = avctx;
  637. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  638. avpriv_float_dsp_init(&tctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  639. if ((ret = init_mdct_win(tctx))) {
  640. av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
  641. ff_twinvq_decode_close(avctx);
  642. return ret;
  643. }
  644. init_bitstream_params(tctx);
  645. twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
  646. FF_ARRAY_ELEMS(tctx->bark_hist));
  647. return 0;
  648. }