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.

806 lines
27KB

  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[tctx->cur_frame];
  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, int offset)
  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. float *out1, *out2;
  331. for (i = 0; i < tctx->avctx->channels; i++)
  332. imdct_and_window(tctx, ftype, wtype,
  333. tctx->spectrum + i * mtab->size,
  334. prev_buf + 2 * i * mtab->size,
  335. i);
  336. if (!out)
  337. return;
  338. size2 = tctx->last_block_pos[0];
  339. size1 = mtab->size - size2;
  340. out1 = &out[0][0] + offset;
  341. memcpy(out1, prev_buf, size1 * sizeof(*out1));
  342. memcpy(out1 + size1, tctx->curr_frame, size2 * sizeof(*out1));
  343. if (tctx->avctx->channels == 2) {
  344. out2 = &out[1][0] + offset;
  345. memcpy(out2, &prev_buf[2 * mtab->size],
  346. size1 * sizeof(*out2));
  347. memcpy(out2 + size1, &tctx->curr_frame[2 * mtab->size],
  348. size2 * sizeof(*out2));
  349. tctx->fdsp.butterflies_float(out1, out2, mtab->size);
  350. }
  351. }
  352. static void read_and_decode_spectrum(TwinVQContext *tctx, float *out,
  353. enum TwinVQFrameType ftype)
  354. {
  355. const TwinVQModeTab *mtab = tctx->mtab;
  356. TwinVQFrameData *bits = &tctx->bits[tctx->cur_frame];
  357. int channels = tctx->avctx->channels;
  358. int sub = mtab->fmode[ftype].sub;
  359. int block_size = mtab->size / sub;
  360. float gain[TWINVQ_CHANNELS_MAX * TWINVQ_SUBBLOCKS_MAX];
  361. float ppc_shape[TWINVQ_PPC_SHAPE_LEN_MAX * TWINVQ_CHANNELS_MAX * 4];
  362. int i, j;
  363. dequant(tctx, bits->main_coeffs, out, ftype,
  364. mtab->fmode[ftype].cb0, mtab->fmode[ftype].cb1,
  365. mtab->fmode[ftype].cb_len_read);
  366. dec_gain(tctx, ftype, gain);
  367. if (ftype == TWINVQ_FT_LONG) {
  368. int cb_len_p = (tctx->n_div[3] + mtab->ppc_shape_len * channels - 1) /
  369. tctx->n_div[3];
  370. dequant(tctx, bits->ppc_coeffs, ppc_shape,
  371. TWINVQ_FT_PPC, mtab->ppc_shape_cb,
  372. mtab->ppc_shape_cb + cb_len_p * TWINVQ_PPC_SHAPE_CB_SIZE,
  373. cb_len_p);
  374. }
  375. for (i = 0; i < channels; i++) {
  376. float *chunk = out + mtab->size * i;
  377. float lsp[TWINVQ_LSP_COEFS_MAX];
  378. for (j = 0; j < sub; j++) {
  379. tctx->dec_bark_env(tctx, bits->bark1[i][j],
  380. bits->bark_use_hist[i][j], i,
  381. tctx->tmp_buf, gain[sub * i + j], ftype);
  382. tctx->fdsp.vector_fmul(chunk + block_size * j,
  383. chunk + block_size * j,
  384. tctx->tmp_buf, block_size);
  385. }
  386. if (ftype == TWINVQ_FT_LONG)
  387. tctx->decode_ppc(tctx, bits->p_coef[i], bits->g_coef[i],
  388. ppc_shape + i * mtab->ppc_shape_len, chunk);
  389. decode_lsp(tctx, bits->lpc_idx1[i], bits->lpc_idx2[i],
  390. bits->lpc_hist_idx[i], lsp, tctx->lsp_hist[i]);
  391. dec_lpc_spectrum_inv(tctx, lsp, ftype, tctx->tmp_buf);
  392. for (j = 0; j < mtab->fmode[ftype].sub; j++) {
  393. tctx->fdsp.vector_fmul(chunk, chunk, tctx->tmp_buf, block_size);
  394. chunk += block_size;
  395. }
  396. }
  397. }
  398. const enum TwinVQFrameType ff_twinvq_wtype_to_ftype_table[] = {
  399. TWINVQ_FT_LONG, TWINVQ_FT_LONG, TWINVQ_FT_SHORT, TWINVQ_FT_LONG,
  400. TWINVQ_FT_MEDIUM, TWINVQ_FT_LONG, TWINVQ_FT_LONG, TWINVQ_FT_MEDIUM,
  401. TWINVQ_FT_MEDIUM
  402. };
  403. int ff_twinvq_decode_frame(AVCodecContext *avctx, void *data,
  404. int *got_frame_ptr, AVPacket *avpkt)
  405. {
  406. AVFrame *frame = data;
  407. const uint8_t *buf = avpkt->data;
  408. int buf_size = avpkt->size;
  409. TwinVQContext *tctx = avctx->priv_data;
  410. const TwinVQModeTab *mtab = tctx->mtab;
  411. float **out = NULL;
  412. int ret;
  413. /* get output buffer */
  414. if (tctx->discarded_packets >= 2) {
  415. frame->nb_samples = mtab->size * tctx->frames_per_packet;
  416. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  417. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  418. return ret;
  419. }
  420. out = (float **)frame->extended_data;
  421. }
  422. if (buf_size < avctx->block_align) {
  423. av_log(avctx, AV_LOG_ERROR,
  424. "Frame too small (%d bytes). Truncated file?\n", buf_size);
  425. return AVERROR(EINVAL);
  426. }
  427. if ((ret = tctx->read_bitstream(avctx, tctx, buf, buf_size)) < 0)
  428. return ret;
  429. for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
  430. tctx->cur_frame++) {
  431. read_and_decode_spectrum(tctx, tctx->spectrum,
  432. tctx->bits[tctx->cur_frame].ftype);
  433. imdct_output(tctx, tctx->bits[tctx->cur_frame].ftype,
  434. tctx->bits[tctx->cur_frame].window_type, out,
  435. tctx->cur_frame * mtab->size);
  436. FFSWAP(float *, tctx->curr_frame, tctx->prev_frame);
  437. }
  438. if (tctx->discarded_packets < 2) {
  439. tctx->discarded_packets++;
  440. *got_frame_ptr = 0;
  441. return buf_size;
  442. }
  443. *got_frame_ptr = 1;
  444. // VQF can deliver packets 1 byte greater than block align
  445. if (buf_size == avctx->block_align + 1)
  446. return buf_size;
  447. return avctx->block_align;
  448. }
  449. /**
  450. * Init IMDCT and windowing tables
  451. */
  452. static av_cold int init_mdct_win(TwinVQContext *tctx)
  453. {
  454. int i, j, ret;
  455. const TwinVQModeTab *mtab = tctx->mtab;
  456. int size_s = mtab->size / mtab->fmode[TWINVQ_FT_SHORT].sub;
  457. int size_m = mtab->size / mtab->fmode[TWINVQ_FT_MEDIUM].sub;
  458. int channels = tctx->avctx->channels;
  459. float norm = channels == 1 ? 2.0 : 1.0;
  460. for (i = 0; i < 3; i++) {
  461. int bsize = tctx->mtab->size / tctx->mtab->fmode[i].sub;
  462. if ((ret = ff_mdct_init(&tctx->mdct_ctx[i], av_log2(bsize) + 1, 1,
  463. -sqrt(norm / bsize) / (1 << 15))))
  464. return ret;
  465. }
  466. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->tmp_buf,
  467. mtab->size * sizeof(*tctx->tmp_buf), alloc_fail);
  468. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->spectrum,
  469. 2 * mtab->size * channels * sizeof(*tctx->spectrum),
  470. alloc_fail);
  471. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->curr_frame,
  472. 2 * mtab->size * channels * sizeof(*tctx->curr_frame),
  473. alloc_fail);
  474. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->prev_frame,
  475. 2 * mtab->size * channels * sizeof(*tctx->prev_frame),
  476. alloc_fail);
  477. for (i = 0; i < 3; i++) {
  478. int m = 4 * mtab->size / mtab->fmode[i].sub;
  479. double freq = 2 * M_PI / m;
  480. FF_ALLOC_OR_GOTO(tctx->avctx, tctx->cos_tabs[i],
  481. (m / 4) * sizeof(*tctx->cos_tabs[i]), alloc_fail);
  482. for (j = 0; j <= m / 8; j++)
  483. tctx->cos_tabs[i][j] = cos((2 * j + 1) * freq);
  484. for (j = 1; j < m / 8; j++)
  485. tctx->cos_tabs[i][m / 4 - j] = tctx->cos_tabs[i][j];
  486. }
  487. ff_init_ff_sine_windows(av_log2(size_m));
  488. ff_init_ff_sine_windows(av_log2(size_s / 2));
  489. ff_init_ff_sine_windows(av_log2(mtab->size));
  490. return 0;
  491. alloc_fail:
  492. return AVERROR(ENOMEM);
  493. }
  494. /**
  495. * Interpret the data as if it were a num_blocks x line_len[0] matrix and for
  496. * each line do a cyclic permutation, i.e.
  497. * abcdefghijklm -> defghijklmabc
  498. * where the amount to be shifted is evaluated depending on the column.
  499. */
  500. static void permutate_in_line(int16_t *tab, int num_vect, int num_blocks,
  501. int block_size,
  502. const uint8_t line_len[2], int length_div,
  503. enum TwinVQFrameType ftype)
  504. {
  505. int i, j;
  506. for (i = 0; i < line_len[0]; i++) {
  507. int shift;
  508. if (num_blocks == 1 ||
  509. (ftype == TWINVQ_FT_LONG && num_vect % num_blocks) ||
  510. (ftype != TWINVQ_FT_LONG && num_vect & 1) ||
  511. i == line_len[1]) {
  512. shift = 0;
  513. } else if (ftype == TWINVQ_FT_LONG) {
  514. shift = i;
  515. } else
  516. shift = i * i;
  517. for (j = 0; j < num_vect && (j + num_vect * i < block_size * num_blocks); j++)
  518. tab[i * num_vect + j] = i * num_vect + (j + shift) % num_vect;
  519. }
  520. }
  521. /**
  522. * Interpret the input data as in the following table:
  523. *
  524. * @verbatim
  525. *
  526. * abcdefgh
  527. * ijklmnop
  528. * qrstuvw
  529. * x123456
  530. *
  531. * @endverbatim
  532. *
  533. * and transpose it, giving the output
  534. * aiqxbjr1cks2dlt3emu4fvn5gow6hp
  535. */
  536. static void transpose_perm(int16_t *out, int16_t *in, int num_vect,
  537. const uint8_t line_len[2], int length_div)
  538. {
  539. int i, j;
  540. int cont = 0;
  541. for (i = 0; i < num_vect; i++)
  542. for (j = 0; j < line_len[i >= length_div]; j++)
  543. out[cont++] = in[j * num_vect + i];
  544. }
  545. static void linear_perm(int16_t *out, int16_t *in, int n_blocks, int size)
  546. {
  547. int block_size = size / n_blocks;
  548. int i;
  549. for (i = 0; i < size; i++)
  550. out[i] = block_size * (in[i] % n_blocks) + in[i] / n_blocks;
  551. }
  552. static av_cold void construct_perm_table(TwinVQContext *tctx,
  553. enum TwinVQFrameType ftype)
  554. {
  555. int block_size, size;
  556. const TwinVQModeTab *mtab = tctx->mtab;
  557. int16_t *tmp_perm = (int16_t *)tctx->tmp_buf;
  558. if (ftype == TWINVQ_FT_PPC) {
  559. size = tctx->avctx->channels;
  560. block_size = mtab->ppc_shape_len;
  561. } else {
  562. size = tctx->avctx->channels * mtab->fmode[ftype].sub;
  563. block_size = mtab->size / mtab->fmode[ftype].sub;
  564. }
  565. permutate_in_line(tmp_perm, tctx->n_div[ftype], size,
  566. block_size, tctx->length[ftype],
  567. tctx->length_change[ftype], ftype);
  568. transpose_perm(tctx->permut[ftype], tmp_perm, tctx->n_div[ftype],
  569. tctx->length[ftype], tctx->length_change[ftype]);
  570. linear_perm(tctx->permut[ftype], tctx->permut[ftype], size,
  571. size * block_size);
  572. }
  573. static av_cold void init_bitstream_params(TwinVQContext *tctx)
  574. {
  575. const TwinVQModeTab *mtab = tctx->mtab;
  576. int n_ch = tctx->avctx->channels;
  577. int total_fr_bits = tctx->avctx->bit_rate * mtab->size /
  578. tctx->avctx->sample_rate;
  579. int lsp_bits_per_block = n_ch * (mtab->lsp_bit0 + mtab->lsp_bit1 +
  580. mtab->lsp_split * mtab->lsp_bit2);
  581. int ppc_bits = n_ch * (mtab->pgain_bit + mtab->ppc_shape_bit +
  582. mtab->ppc_period_bit);
  583. int bsize_no_main_cb[3], bse_bits[3], i;
  584. enum TwinVQFrameType frametype;
  585. for (i = 0; i < 3; i++)
  586. // +1 for history usage switch
  587. bse_bits[i] = n_ch *
  588. (mtab->fmode[i].bark_n_coef *
  589. mtab->fmode[i].bark_n_bit + 1);
  590. bsize_no_main_cb[2] = bse_bits[2] + lsp_bits_per_block + ppc_bits +
  591. TWINVQ_WINDOW_TYPE_BITS + n_ch * TWINVQ_GAIN_BITS;
  592. for (i = 0; i < 2; i++)
  593. bsize_no_main_cb[i] =
  594. lsp_bits_per_block + n_ch * TWINVQ_GAIN_BITS +
  595. TWINVQ_WINDOW_TYPE_BITS +
  596. mtab->fmode[i].sub * (bse_bits[i] + n_ch * TWINVQ_SUB_GAIN_BITS);
  597. if (tctx->codec == TWINVQ_CODEC_METASOUND && !tctx->is_6kbps) {
  598. bsize_no_main_cb[1] += 2;
  599. bsize_no_main_cb[2] += 2;
  600. }
  601. // The remaining bits are all used for the main spectrum coefficients
  602. for (i = 0; i < 4; i++) {
  603. int bit_size, vect_size;
  604. int rounded_up, rounded_down, num_rounded_down, num_rounded_up;
  605. if (i == 3) {
  606. bit_size = n_ch * mtab->ppc_shape_bit;
  607. vect_size = n_ch * mtab->ppc_shape_len;
  608. } else {
  609. bit_size = total_fr_bits - bsize_no_main_cb[i];
  610. vect_size = n_ch * mtab->size;
  611. }
  612. tctx->n_div[i] = (bit_size + 13) / 14;
  613. rounded_up = (bit_size + tctx->n_div[i] - 1) /
  614. tctx->n_div[i];
  615. rounded_down = (bit_size) / tctx->n_div[i];
  616. num_rounded_down = rounded_up * tctx->n_div[i] - bit_size;
  617. num_rounded_up = tctx->n_div[i] - num_rounded_down;
  618. tctx->bits_main_spec[0][i][0] = (rounded_up + 1) / 2;
  619. tctx->bits_main_spec[1][i][0] = rounded_up / 2;
  620. tctx->bits_main_spec[0][i][1] = (rounded_down + 1) / 2;
  621. tctx->bits_main_spec[1][i][1] = rounded_down / 2;
  622. tctx->bits_main_spec_change[i] = num_rounded_up;
  623. rounded_up = (vect_size + tctx->n_div[i] - 1) /
  624. tctx->n_div[i];
  625. rounded_down = (vect_size) / tctx->n_div[i];
  626. num_rounded_down = rounded_up * tctx->n_div[i] - vect_size;
  627. num_rounded_up = tctx->n_div[i] - num_rounded_down;
  628. tctx->length[i][0] = rounded_up;
  629. tctx->length[i][1] = rounded_down;
  630. tctx->length_change[i] = num_rounded_up;
  631. }
  632. for (frametype = TWINVQ_FT_SHORT; frametype <= TWINVQ_FT_PPC; frametype++)
  633. construct_perm_table(tctx, frametype);
  634. }
  635. av_cold int ff_twinvq_decode_close(AVCodecContext *avctx)
  636. {
  637. TwinVQContext *tctx = avctx->priv_data;
  638. int i;
  639. for (i = 0; i < 3; i++) {
  640. ff_mdct_end(&tctx->mdct_ctx[i]);
  641. av_free(tctx->cos_tabs[i]);
  642. }
  643. av_free(tctx->curr_frame);
  644. av_free(tctx->spectrum);
  645. av_free(tctx->prev_frame);
  646. av_free(tctx->tmp_buf);
  647. return 0;
  648. }
  649. av_cold int ff_twinvq_decode_init(AVCodecContext *avctx)
  650. {
  651. int ret;
  652. TwinVQContext *tctx = avctx->priv_data;
  653. tctx->avctx = avctx;
  654. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  655. if (!avctx->block_align) {
  656. avctx->block_align = tctx->frame_size + 7 >> 3;
  657. } else if (avctx->block_align * 8 < tctx->frame_size) {
  658. av_log(avctx, AV_LOG_ERROR, "Block align is %d bits, expected %d\n",
  659. avctx->block_align * 8, tctx->frame_size);
  660. return AVERROR_INVALIDDATA;
  661. }
  662. tctx->frames_per_packet = avctx->block_align * 8 / tctx->frame_size;
  663. if (tctx->frames_per_packet > TWINVQ_MAX_FRAMES_PER_PACKET) {
  664. av_log(avctx, AV_LOG_ERROR, "Too many frames per packet (%d)\n",
  665. tctx->frames_per_packet);
  666. return AVERROR_INVALIDDATA;
  667. }
  668. avpriv_float_dsp_init(&tctx->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
  669. if ((ret = init_mdct_win(tctx))) {
  670. av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n");
  671. ff_twinvq_decode_close(avctx);
  672. return ret;
  673. }
  674. init_bitstream_params(tctx);
  675. twinvq_memset_float(tctx->bark_hist[0][0], 0.1,
  676. FF_ARRAY_ELEMS(tctx->bark_hist));
  677. return 0;
  678. }