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.

802 lines
27KB

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