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.

780 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 (buf_size < avctx->block_align) {
  420. av_log(avctx, AV_LOG_ERROR,
  421. "Frame too small (%d bytes). Truncated file?\n", buf_size);
  422. return AVERROR(EINVAL);
  423. }
  424. if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
  425. return ret;
  426. read_and_decode_spectrum(tctx, tctx->spectrum, tctx->bits.ftype);
  427. imdct_output(tctx, tctx->bits.ftype, tctx->bits.window_type, out);
  428. FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
  429. if (tctx->discarded_packets < 2) {
  430. tctx->discarded_packets++;
  431. *got_frame_ptr = 0;
  432. return buf_size;
  433. }
  434. *got_frame_ptr = 1;
  435. return avctx->block_align;
  436. }
  437. /**
  438. * Init IMDCT and windowing tables
  439. */
  440. static av_cold int init_mdct_win(TwinVQContext *tctx)
  441. {
  442. int i, j, ret;
  443. const TwinVQModeTab *mtab = tctx->mtab;
  444. int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
  445. int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
  446. int channels = tctx->avctx->channels;
  447. float norm = channels == 1 ? 2.0 : 1.0;
  448. for (i = 0; i < 3; i++) {
  449. int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
  450. if ((ret = ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
  451. -sqrt(norm / bsize) / (1 << 15))))
  452. return ret;
  453. }
  454. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->tmp_buf,
  455. mtab->size * sizeof(*tctx->tmp_buf), alloc_fail);
  456. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->spectrum,
  457. 2 * mtab->size * channels * sizeof(*tctx->spectrum),
  458. alloc_fail);
  459. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->curr_frame,
  460. 2 * mtab->size * channels * sizeof(*tctx->curr_frame),
  461. alloc_fail);
  462. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->prev_frame,
  463. 2 * mtab->size * channels * sizeof(*tctx->prev_frame),
  464. alloc_fail);
  465. for (i = 0; i < 3; i++) {
  466. int m = 4 * mtab->size / mtab->fmode[i].sub;
  467. double freq = 2 * M_PI / m;
  468. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
  469. (m / 4) * sizeof(*tctx->cos_tabs[i]), alloc_fail);
  470. for (j = 0; j <= m / 8; j++)
  471. tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
  472. for (j = 1; j < m / 8; j++)
  473. tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
  474. }
  475. ff_init_ff_sine_windows(av_log2(size_m));
  476. ff_init_ff_sine_windows(av_log2(size_s / 2));
  477. ff_init_ff_sine_windows(av_log2(mtab->size));
  478. return 0;
  479. alloc_fail:
  480. return AVERROR(ENOMEM);
  481. }
  482. /**
  483. * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
  484. * each line do a cyclic permutation, i.e.
  485. * abcdefghijklm -> defghijklmabc
  486. * where the amount to be shifted is evaluated depending on the column.
  487. */
  488. static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
  489. int block_size,
  490. const uint8_t line_len[2], int length_div,
  491. enum TwinVQFrameType ftype)
  492. {
  493. int i, j;
  494. for (i = 0; i < line_len[0]; i++) {
  495. int shift;
  496. if (num_blocks == 1 ||
  497. (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
  498. (ftype != TWINVQ_FT_LONG && num_vect & 1) ||
  499. i == line_len[1]) {
  500. shift = 0;
  501. } else if (ftype == TWINVQ_FT_LONG) {
  502. shift = i;
  503. } else
  504. shift = i * i;
  505. for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
  506. tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
  507. }
  508. }
  509. /**
  510. * Interpret the input data as in the following table:
  511. *
  512. * @verbatim
  513. *
  514. * abcdefgh
  515. * ijklmnop
  516. * qrstuvw
  517. * x123456
  518. *
  519. * @endverbatim
  520. *
  521. * and transpose it, giving the output
  522. * aiqxbjr1cks2dlt3emu4fvn5gow6hp
  523. */
  524. static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
  525. const uint8_t line_len[2], int length_div)
  526. {
  527. int i, j;
  528. int cont = 0;
  529. for (i = 0; i < num_vect; i++)
  530. for (j = 0; j < line_len[i >= length_div]; j++)
  531. out[cont++] = in[j * num_vect + i];
  532. }
  533. static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
  534. {
  535. int block_size = size / n_blocks;
  536. int i;
  537. for (i = 0; i < size; i++)
  538. out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
  539. }
  540. static av_cold void construct_perm_table(TwinVQContext *tctx,
  541. enum TwinVQFrameType ftype)
  542. {
  543. int block_size, size;
  544. const TwinVQModeTab *mtab = tctx->mtab;
  545. int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
  546. if (ftype == TWINVQ_FT_PPC) {
  547. size = tctx->avctx->channels;
  548. block_size = mtab->ppc_shape_len;
  549. } else {
  550. size = tctx->avctx->channels * mtab->fmode[ftype].sub;
  551. block_size = mtab->size / mtab->fmode[ftype].sub;
  552. }
  553. permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
  554. block_size, tctx->length[ftype],
  555. tctx->length_change[ftype], ftype);
  556. transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
  557. tctx->length[ftype], tctx->length_change[ftype]);
  558. linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
  559. size * block_size);
  560. }
  561. static av_cold void init_bitstream_params(TwinVQContext *tctx)
  562. {
  563. const TwinVQModeTab *mtab = tctx->mtab;
  564. int n_ch = tctx->avctx->channels;
  565. int total_fr_bits = tctx->avctx->bit_rate * mtab->size /
  566. tctx->avctx->sample_rate;
  567. int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
  568. mtab->lsp_split * mtab->lsp_bit2);
  569. int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
  570. mtab->ppc_period_bit);
  571. int bsize_no_main_cb[3], bse_bits[3], i;
  572. enum TwinVQFrameType frametype;
  573. for (i = 0; i < 3; i++)
  574. // +1 for history usage switch
  575. bse_bits[i] = n_ch *
  576. (mtab->fmode[i].bark_n_coef *
  577. mtab->fmode[i].bark_n_bit + 1);
  578. bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
  579. TWINVQ_WINDOW_TYPE_BITS + n_ch * TWINVQ_GAIN_BITS;
  580. for (i = 0; i < 2; i++)
  581. bsize_no_main_cb[i] =
  582. lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
  583. TWINVQ_WINDOW_TYPE_BITS +
  584. mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
  585. if (tctx->codec == TWINVQ_CODEC_METASOUND) {
  586. bsize_no_main_cb[1] += 2;
  587. bsize_no_main_cb[2] += 2;
  588. }
  589. // The remaining bits are all used for the main spectrum coefficients
  590. for (i = 0; i < 4; i++) {
  591. int bit_size, vect_size;
  592. int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
  593. if (i == 3) {
  594. bit_size = n_ch * mtab->ppc_shape_bit;
  595. vect_size = n_ch * mtab->ppc_shape_len;
  596. } else {
  597. bit_size = total_fr_bits - bsize_no_main_cb[i];
  598. vect_size = n_ch * mtab->size;
  599. }
  600. tctx->n_div[i] = (bit_size + 13) / 14;
  601. rounded_up = (bit_size + tctx->n_div[i] - 1) /
  602. tctx->n_div[i];
  603. rounded_down = (bit_size) / tctx->n_div[i];
  604. num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
  605. num_rounded_up = tctx->n_div[i] - num_rounded_down;
  606. tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;
  607. tctx->bits_main_spec[1][i][0] = rounded_up / 2;
  608. tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;
  609. tctx->bits_main_spec[1][i][1] = rounded_down / 2;
  610. tctx->bits_main_spec_change[i] = num_rounded_up;
  611. rounded_up = (vect_size + tctx->n_div[i] - 1) /
  612. tctx->n_div[i];
  613. rounded_down = (vect_size) / tctx->n_div[i];
  614. num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
  615. num_rounded_up = tctx->n_div[i] - num_rounded_down;
  616. tctx->length[i][0] = rounded_up;
  617. tctx->length[i][1] = rounded_down;
  618. tctx->length_change[i] = num_rounded_up;
  619. }
  620. for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
  621. construct_perm_table(tctx, frametype);
  622. }
  623. av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
  624. {
  625. TwinVQContext *tctx = avctx->priv_data;
  626. int i;
  627. for (i = 0; i < 3; i++) {
  628. ff_mdct_end(&tctx->mdct_ctx[i]);
  629. av_free(tctx->cos_tabs[i]);
  630. }
  631. av_free(tctx->curr_frame);
  632. av_free(tctx->spectrum);
  633. av_free(tctx->prev_frame);
  634. av_free(tctx->tmp_buf);
  635. return 0;
  636. }
  637. av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
  638. {
  639. int ret;
  640. TwinVQContext *tctx = avctx->priv_data;
  641. tctx->avctx = avctx;
  642. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  643. avpriv_float_dsp_init(&tctx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
  644. if ((ret = init_mdct_win(tctx))) {
  645. av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
  646. ff_twinvq_decode_close(avctx);
  647. return ret;
  648. }
  649. init_bitstream_params(tctx);
  650. twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
  651. FF_ARRAY_ELEMS(tctx->bark_hist));
  652. return 0;
  653. }