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.

1488 lines
50KB

  1. /*
  2. * Copyright (c) 2013, The WebRTC project authors. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * * Neither the name of Google nor the names of its contributors may
  17. * be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. #include "get_bits.h"
  35. #include "ilbcdata.h"
  36. #define LPC_N_20MS 1
  37. #define LPC_N_30MS 2
  38. #define LPC_N_MAX 2
  39. #define LSF_NSPLIT 3
  40. #define NASUB_MAX 4
  41. #define LPC_FILTERORDER 10
  42. #define NSUB_MAX 6
  43. #define SUBL 40
  44. #define ST_MEM_L_TBL 85
  45. #define MEM_LF_TBL 147
  46. #define STATE_SHORT_LEN_20MS 57
  47. #define STATE_SHORT_LEN_30MS 58
  48. #define BLOCKL_MAX 240
  49. #define CB_MEML 147
  50. #define CB_NSTAGES 3
  51. #define CB_HALFFILTERLEN 4
  52. #define CB_FILTERLEN 8
  53. #define ENH_NBLOCKS_TOT 8
  54. #define ENH_BLOCKL 80
  55. #define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL
  56. #define ENH_BUFL_FILTEROVERHEAD 3
  57. #define BLOCKL_MAX 240
  58. #define NSUB_20MS 4
  59. #define NSUB_30MS 6
  60. #define NSUB_MAX 6
  61. #define NASUB_20MS 2
  62. #define NASUB_30MS 4
  63. #define NASUB_MAX 4
  64. #define STATE_LEN 80
  65. #define STATE_SHORT_LEN_30MS 58
  66. #define STATE_SHORT_LEN_20MS 57
  67. #define SPL_MUL_16_16(a, b) ((int32_t) (((int16_t)(a)) * ((int16_t)(b))))
  68. #define SPL_MUL_16_16_RSFT(a, b, c) (SPL_MUL_16_16(a, b) >> (c))
  69. typedef struct ILBCFrame {
  70. int16_t lsf[LSF_NSPLIT*LPC_N_MAX];
  71. int16_t cb_index[CB_NSTAGES*(NASUB_MAX + 1)];
  72. int16_t gain_index[CB_NSTAGES*(NASUB_MAX + 1)];
  73. int16_t ifm;
  74. int16_t state_first;
  75. int16_t idx[STATE_SHORT_LEN_30MS];
  76. int16_t firstbits;
  77. int16_t start;
  78. } ILBCFrame;
  79. typedef struct ILBCContext {
  80. AVClass *class;
  81. int enhancer;
  82. int mode;
  83. GetBitContext gb;
  84. ILBCFrame frame;
  85. int prev_enh_pl;
  86. int consPLICount;
  87. int last_lag;
  88. int state_short_len;
  89. int lpc_n;
  90. int16_t nasub;
  91. int16_t nsub;
  92. int block_samples;
  93. int16_t no_of_words;
  94. int16_t no_of_bytes;
  95. int16_t lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
  96. int16_t lsfold[LPC_FILTERORDER];
  97. int16_t syntMem[LPC_FILTERORDER];
  98. int16_t lsfdeqold[LPC_FILTERORDER];
  99. int16_t weightdenum[(LPC_FILTERORDER + 1) * NSUB_MAX];
  100. int16_t syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
  101. int16_t old_syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
  102. int16_t enh_buf[ENH_BUFL+ENH_BUFL_FILTEROVERHEAD];
  103. int16_t enh_period[ENH_NBLOCKS_TOT];
  104. int16_t prevResidual[NSUB_MAX*SUBL];
  105. int16_t decresidual[BLOCKL_MAX];
  106. int16_t plc_residual[BLOCKL_MAX + LPC_FILTERORDER];
  107. int16_t seed;
  108. int16_t prevPLI;
  109. int16_t prevScale;
  110. int16_t prevLag;
  111. int16_t per_square;
  112. int16_t prev_lpc[LPC_FILTERORDER + 1];
  113. int16_t plc_lpc[LPC_FILTERORDER + 1];
  114. int16_t hpimemx[2];
  115. int16_t hpimemy[4];
  116. } ILBCContext;
  117. static int unpack_frame(ILBCContext *s)
  118. {
  119. ILBCFrame *frame = &s->frame;
  120. GetBitContext *gb = &s->gb;
  121. int j;
  122. frame->lsf[0] = get_bits(gb, 6);
  123. frame->lsf[1] = get_bits(gb, 7);
  124. frame->lsf[2] = get_bits(gb, 7);
  125. if (s->mode = 20) {
  126. frame->start = get_bits(gb, 2);
  127. frame->state_first = get_bits1(gb);
  128. frame->ifm = get_bits(gb, 6);
  129. frame->cb_index[0] = get_bits(gb, 6) << 1;
  130. frame->gain_index[0] = get_bits(gb, 2) << 3;
  131. frame->gain_index[1] = get_bits1(gb) << 3;
  132. frame->cb_index[3] = get_bits(gb, 7) << 1;
  133. frame->gain_index[3] = get_bits1(gb) << 4;
  134. frame->gain_index[4] = get_bits1(gb) << 3;
  135. frame->gain_index[6] = get_bits1(gb) << 4;
  136. } else {
  137. frame->lsf[3] = get_bits(gb, 6);
  138. frame->lsf[4] = get_bits(gb, 7);
  139. frame->lsf[5] = get_bits(gb, 7);
  140. frame->start = get_bits(gb, 3);
  141. frame->state_first = get_bits1(gb);
  142. frame->ifm = get_bits(gb, 6);
  143. frame->cb_index[0] = get_bits(gb, 4) << 3;
  144. frame->gain_index[0] = get_bits1(gb) << 4;
  145. frame->gain_index[1] = get_bits1(gb) << 3;
  146. frame->cb_index[3] = get_bits(gb, 6) << 2;
  147. frame->gain_index[3] = get_bits1(gb) << 4;
  148. frame->gain_index[4] = get_bits1(gb) << 3;
  149. }
  150. for (j = 0; j < 48; j++)
  151. frame->idx[j] = get_bits1(gb) << 2;
  152. if (s->mode == 20) {
  153. for (; j < 57; j++)
  154. frame->idx[j] = get_bits1(gb) << 2;
  155. frame->gain_index[1] |= get_bits1(gb) << 2;
  156. frame->gain_index[3] |= get_bits(gb, 2) << 2;
  157. frame->gain_index[4] |= get_bits1(gb) << 2;
  158. frame->gain_index[6] |= get_bits1(gb) << 3;
  159. frame->gain_index[7] = get_bits(gb, 2) << 2;
  160. } else {
  161. for (; j < 58; j++)
  162. frame->idx[j] = get_bits1(gb) << 2;
  163. frame->cb_index[0] |= get_bits(gb, 2) << 1;
  164. frame->gain_index[0] |= get_bits1(gb) << 3;
  165. frame->gain_index[1] |= get_bits1(gb) << 2;
  166. frame->cb_index[3] |= get_bits1(gb) << 1;
  167. frame->cb_index[6] = get_bits1(gb) << 7;
  168. frame->cb_index[6] |= get_bits(gb, 6) << 1;
  169. frame->cb_index[9] = get_bits(gb, 7) << 1;
  170. frame->cb_index[12] = get_bits(gb, 3) << 5;
  171. frame->cb_index[12] |= get_bits(gb, 4) << 1;
  172. frame->gain_index[3] |= get_bits(gb, 2) << 2;
  173. frame->gain_index[4] |= get_bits(gb, 2) << 1;
  174. frame->gain_index[6] = get_bits(gb, 2) << 3;
  175. frame->gain_index[7] = get_bits(gb, 2) << 2;
  176. frame->gain_index[9] = get_bits1(gb) << 4;
  177. frame->gain_index[10] = get_bits1(gb) << 3;
  178. frame->gain_index[12] = get_bits1(gb) << 4;
  179. frame->gain_index[13] = get_bits1(gb) << 3;
  180. }
  181. for (j = 0; j < 56; j++)
  182. frame->idx[j] |= get_bits(gb, 2);
  183. if (s->mode == 20) {
  184. frame->idx[56] |= get_bits(gb, 2);
  185. frame->cb_index[0] |= get_bits1(gb);
  186. frame->cb_index[1] = get_bits(gb, 7);
  187. frame->cb_index[2] = get_bits(gb, 6) << 1;
  188. frame->cb_index[2] |= get_bits1(gb);
  189. frame->gain_index[0] |= get_bits(gb, 3);
  190. frame->gain_index[1] |= get_bits(gb, 2);
  191. frame->gain_index[2] = get_bits(gb, 3);
  192. frame->cb_index[3] |= get_bits1(gb);
  193. frame->cb_index[4] = get_bits(gb, 6) << 1;
  194. frame->cb_index[4] |= get_bits1(gb);
  195. frame->cb_index[5] = get_bits(gb, 7);
  196. frame->cb_index[6] = get_bits(gb, 8);
  197. frame->cb_index[7] = get_bits(gb, 8);
  198. frame->cb_index[8] = get_bits(gb, 8);
  199. frame->gain_index[3] |= get_bits(gb, 2);
  200. frame->gain_index[4] |= get_bits(gb, 2);
  201. frame->gain_index[5] = get_bits(gb, 3);
  202. frame->gain_index[6] |= get_bits(gb, 3);
  203. frame->gain_index[7] |= get_bits(gb, 2);
  204. frame->gain_index[8] = get_bits(gb, 3);
  205. } else {
  206. frame->idx[56] |= get_bits(gb, 2);
  207. frame->idx[57] |= get_bits(gb, 2);
  208. frame->cb_index[0] |= get_bits1(gb);
  209. frame->cb_index[1] = get_bits(gb, 7);
  210. frame->cb_index[2] = get_bits(gb, 4) << 3;
  211. frame->cb_index[2] |= get_bits(gb, 3);
  212. frame->gain_index[0] |= get_bits(gb, 3);
  213. frame->gain_index[1] |= get_bits(gb, 2);
  214. frame->gain_index[2] = get_bits(gb, 3);
  215. frame->cb_index[3] |= get_bits1(gb);
  216. frame->cb_index[4] = get_bits(gb, 4) << 3;
  217. frame->cb_index[4] |= get_bits(gb, 3);
  218. frame->cb_index[5] = get_bits(gb, 7);
  219. frame->cb_index[6] |= get_bits1(gb);
  220. frame->cb_index[7] = get_bits(gb, 5) << 3;
  221. frame->cb_index[7] |= get_bits(gb, 3);
  222. frame->cb_index[8] = get_bits(gb, 8);
  223. frame->cb_index[9] |= get_bits1(gb);
  224. frame->cb_index[10] = get_bits(gb, 4) << 4;
  225. frame->cb_index[10] |= get_bits(gb, 4);
  226. frame->cb_index[11] = get_bits(gb, 8);
  227. frame->cb_index[12] |= get_bits1(gb);
  228. frame->cb_index[13] = get_bits(gb, 3) << 5;
  229. frame->cb_index[13] |= get_bits(gb, 5);
  230. frame->cb_index[14] = get_bits(gb, 8);
  231. frame->gain_index[3] |= get_bits(gb, 2);
  232. frame->gain_index[4] |= get_bits1(gb);
  233. frame->gain_index[5] = get_bits(gb, 3);
  234. frame->gain_index[6] |= get_bits(gb, 3);
  235. frame->gain_index[7] |= get_bits(gb, 2);
  236. frame->gain_index[8] = get_bits(gb, 3);
  237. frame->gain_index[9] |= get_bits(gb, 4);
  238. frame->gain_index[10] |= get_bits1(gb) << 2;
  239. frame->gain_index[10] |= get_bits(gb, 2);
  240. frame->gain_index[11] = get_bits(gb, 3);
  241. frame->gain_index[12] |= get_bits(gb, 4);
  242. frame->gain_index[13] |= get_bits(gb, 3);
  243. frame->gain_index[14] = get_bits(gb, 3);
  244. }
  245. return get_bits1(gb);
  246. }
  247. static void index_conv(int16_t *index)
  248. {
  249. int k;
  250. for (k = 4; k < 6; k++) {
  251. if (index[k] >= 44 && index[k] < 108) {
  252. index[k] += 64;
  253. } else if (index[k] >= 108 && index[k] < 128) {
  254. index[k] += 128;
  255. }
  256. }
  257. }
  258. static void lsf_dequantization(int16_t *lsfdeq, int16_t *index, int16_t lpc_n)
  259. {
  260. int i, j, pos = 0, cb_pos = 0;
  261. for (i = 0; i < LSF_NSPLIT; i++) {
  262. for (j = 0; j < lsf_dim_codebook[i]; j++) {
  263. lsfdeq[pos + j] = lsf_codebook[cb_pos + index[i] * lsf_dim_codebook[i] + j];
  264. }
  265. pos += lsf_dim_codebook[i];
  266. cb_pos += lsf_size_codebook[i] * lsf_dim_codebook[i];
  267. }
  268. if (lpc_n > 1) {
  269. pos = 0;
  270. cb_pos = 0;
  271. for (i = 0; i < LSF_NSPLIT; i++) {
  272. for (j = 0; j < lsf_dim_codebook[i]; j++) {
  273. lsfdeq[LPC_FILTERORDER + pos + j] = lsf_codebook[cb_pos +
  274. index[LSF_NSPLIT + i] * lsf_dim_codebook[i] + j];
  275. }
  276. pos += lsf_dim_codebook[i];
  277. cb_pos += lsf_size_codebook[i] * lsf_dim_codebook[i];
  278. }
  279. }
  280. }
  281. static void lsf_check_stability(int16_t *lsf, int dim, int nb_vectors)
  282. {
  283. for (int n = 0; n < 2; n++) {
  284. for (int m = 0; m < nb_vectors; m++) {
  285. for (int k = 0; k < dim - 1; k++) {
  286. int i = m * dim + k;
  287. if ((lsf[i + 1] - lsf[i]) < 319) {
  288. if (lsf[i + 1] < lsf[i]) {
  289. lsf[i + 1] = lsf[i] + 160;
  290. lsf[i] = lsf[i + 1] - 160;
  291. } else {
  292. lsf[i] -= 160;
  293. lsf[i + 1] += 160;
  294. }
  295. }
  296. lsf[i] = av_clip(lsf[i], 82, 25723);
  297. }
  298. }
  299. }
  300. }
  301. static void lsf_interpolate(int16_t *out, int16_t *in1,
  302. int16_t *in2, int16_t coef,
  303. int size)
  304. {
  305. int invcoef = 16384 - coef, i;
  306. for (i = 0; i < size; i++)
  307. out[i] = (coef * in1[i] + invcoef * in2[i] + 8192) >> 14;
  308. }
  309. static void lsf2lsp(int16_t *lsf, int16_t *lsp, int order)
  310. {
  311. int16_t diff, freq;
  312. int32_t tmp;
  313. int i, k;
  314. for (i = 0; i < order; i++) {
  315. freq = (lsf[i] * 20861) >> 15;
  316. /* 20861: 1.0/(2.0*PI) in Q17 */
  317. /*
  318. Upper 8 bits give the index k and
  319. Lower 8 bits give the difference, which needs
  320. to be approximated linearly
  321. */
  322. k = FFMIN(freq >> 8, 63);
  323. diff = freq & 0xFF;
  324. /* Calculate linear approximation */
  325. tmp = cos_derivative_tbl[k] * diff;
  326. lsp[i] = cos_tbl[k] + (tmp >> 12);
  327. }
  328. }
  329. static void get_lsp_poly(int16_t *lsp, int32_t *f)
  330. {
  331. int16_t high, low;
  332. int i, j, k, l;
  333. int32_t tmp;
  334. f[0] = 16777216;
  335. f[1] = lsp[0] * -1024;
  336. for (i = 2, k = 2, l = 2; i <= 5; i++, k += 2) {
  337. f[l] = f[l - 2];
  338. for (j = i; j > 1; j--, l--) {
  339. high = f[l - 1] >> 16;
  340. low = (f[l - 1] - (high << 16)) >> 1;
  341. tmp = ((high * lsp[k]) << 2) + (((low * lsp[k]) >> 15) << 2);
  342. f[l] += f[l - 2];
  343. f[l] -= tmp;
  344. }
  345. f[l] -= lsp[k] << 10;
  346. l += i;
  347. }
  348. }
  349. static void lsf2poly(int16_t *a, int16_t *lsf)
  350. {
  351. int32_t f[2][6];
  352. int16_t lsp[10];
  353. int32_t tmp;
  354. int i;
  355. lsf2lsp(lsf, lsp, LPC_FILTERORDER);
  356. get_lsp_poly(&lsp[0], f[0]);
  357. get_lsp_poly(&lsp[1], f[1]);
  358. for (i = 5; i > 0; i--) {
  359. f[0][i] += f[0][i - 1];
  360. f[1][i] -= f[1][i - 1];
  361. }
  362. a[0] = 4096;
  363. for (i = 5; i > 0; i--) {
  364. tmp = f[0][6 - i] + f[1][6 - i];
  365. a[6 - i] = (tmp + 4096) >> 13;
  366. tmp = f[0][6 - i] - f[1][6 - i];
  367. a[5 + i] = (tmp + 4096) >> 13;
  368. }
  369. }
  370. static void lsp_interpolate2polydec(int16_t *a, int16_t *lsf1,
  371. int16_t *lsf2, int coef, int length)
  372. {
  373. int16_t lsftmp[LPC_FILTERORDER];
  374. lsf_interpolate(lsftmp, lsf1, lsf2, coef, length);
  375. lsf2poly(a, lsftmp);
  376. }
  377. static void bw_expand(int16_t *out, const int16_t *in, const int16_t *coef, int length)
  378. {
  379. int i;
  380. out[0] = in[0];
  381. for (i = 1; i < length; i++)
  382. out[i] = (coef[i] * in[i] + 16384) >> 15;
  383. }
  384. static void lsp_interpolate(int16_t *syntdenum, int16_t *weightdenum,
  385. int16_t *lsfdeq, int16_t length,
  386. ILBCContext *s)
  387. {
  388. int16_t lp[LPC_FILTERORDER + 1], *lsfdeq2;
  389. int i, pos, lp_length;
  390. lsfdeq2 = lsfdeq + length;
  391. lp_length = length + 1;
  392. if (s->mode == 30) {
  393. lsp_interpolate2polydec(lp, (*s).lsfdeqold, lsfdeq, lsf_weight_30ms[0], length);
  394. memcpy(syntdenum, lp, lp_length * 2);
  395. bw_expand(weightdenum, lp, kLpcChirpSyntDenum, lp_length);
  396. pos = lp_length;
  397. for (i = 1; i < 6; i++) {
  398. lsp_interpolate2polydec(lp, lsfdeq, lsfdeq2,
  399. lsf_weight_30ms[i],
  400. length);
  401. memcpy(syntdenum + pos, lp, lp_length * 2);
  402. bw_expand(weightdenum + pos, lp, kLpcChirpSyntDenum, lp_length);
  403. pos += lp_length;
  404. }
  405. } else {
  406. pos = 0;
  407. for (i = 0; i < s->nsub; i++) {
  408. lsp_interpolate2polydec(lp, s->lsfdeqold, lsfdeq,
  409. lsf_weight_20ms[i], length);
  410. memcpy(syntdenum + pos, lp, lp_length * 2);
  411. bw_expand(weightdenum + pos, lp, kLpcChirpSyntDenum, lp_length);
  412. pos += lp_length;
  413. }
  414. }
  415. if (s->mode == 30) {
  416. memcpy(s->lsfdeqold, lsfdeq2, length * 2);
  417. } else {
  418. memcpy(s->lsfdeqold, lsfdeq, length * 2);
  419. }
  420. }
  421. static void filter_mafq12(int16_t *in_ptr, int16_t *out_ptr,
  422. int16_t *B, int16_t B_length,
  423. int16_t length)
  424. {
  425. int o, i, j;
  426. for (i = 0; i < length; i++) {
  427. const int16_t *b_ptr = &B[0];
  428. const int16_t *x_ptr = &in_ptr[i];
  429. o = 0;
  430. for (j = 0; j < B_length; j++)
  431. o += b_ptr[j] * *x_ptr--;
  432. o = av_clip(o, -134217728, 134215679);
  433. out_ptr[i] = ((o + 2048) >> 12);
  434. }
  435. }
  436. static void filter_arfq12(const int16_t *data_in,
  437. int16_t *data_out,
  438. const int16_t *coefficients,
  439. int coefficients_length,
  440. int data_length)
  441. {
  442. int i, j;
  443. for (i = 0; i < data_length; i++) {
  444. int output = 0, sum = 0;
  445. for (j = coefficients_length - 1; j > 0; j--) {
  446. sum += coefficients[j] * data_out[i - j];
  447. }
  448. output = coefficients[0] * data_in[i] - sum;
  449. output = av_clip(output, -134217728, 134215679);
  450. data_out[i] = (output + 2048) >> 12;
  451. }
  452. }
  453. static void state_construct(int16_t ifm, int16_t *idx,
  454. int16_t *synt_denum, int16_t *Out_fix,
  455. int16_t len)
  456. {
  457. int k;
  458. int16_t maxVal;
  459. int16_t *tmp1, *tmp2, *tmp3;
  460. /* Stack based */
  461. int16_t numerator[1 + LPC_FILTERORDER];
  462. int16_t sampleValVec[2 * STATE_SHORT_LEN_30MS + LPC_FILTERORDER];
  463. int16_t sampleMaVec[2 * STATE_SHORT_LEN_30MS + LPC_FILTERORDER];
  464. int16_t *sampleVal = &sampleValVec[LPC_FILTERORDER];
  465. int16_t *sampleMa = &sampleMaVec[LPC_FILTERORDER];
  466. int16_t *sampleAr = &sampleValVec[LPC_FILTERORDER];
  467. /* initialization of coefficients */
  468. for (k = 0; k < LPC_FILTERORDER + 1; k++) {
  469. numerator[k] = synt_denum[LPC_FILTERORDER - k];
  470. }
  471. /* decoding of the maximum value */
  472. maxVal = frg_quant_mod[ifm];
  473. /* decoding of the sample values */
  474. tmp1 = sampleVal;
  475. tmp2 = &idx[len - 1];
  476. if (ifm < 37) {
  477. for (k = 0; k < len; k++) {
  478. /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 2097152 (= 0.5 << 22)
  479. maxVal is in Q8 and result is in Q(-1) */
  480. (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 2097152) >> 22);
  481. tmp1++;
  482. tmp2--;
  483. }
  484. } else if (ifm < 59) {
  485. for (k = 0; k < len; k++) {
  486. /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 262144 (= 0.5 << 19)
  487. maxVal is in Q5 and result is in Q(-1) */
  488. (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 262144) >> 19);
  489. tmp1++;
  490. tmp2--;
  491. }
  492. } else {
  493. for (k = 0; k < len; k++) {
  494. /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 65536 (= 0.5 << 17)
  495. maxVal is in Q3 and result is in Q(-1) */
  496. (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 65536) >> 17);
  497. tmp1++;
  498. tmp2--;
  499. }
  500. }
  501. /* Set the rest of the data to zero */
  502. memset(&sampleVal[len], 0, len * 2);
  503. /* circular convolution with all-pass filter */
  504. /* Set the state to zero */
  505. memset(sampleValVec, 0, LPC_FILTERORDER * 2);
  506. /* Run MA filter + AR filter */
  507. filter_mafq12(sampleVal, sampleMa, numerator, LPC_FILTERORDER + 1, len + LPC_FILTERORDER);
  508. memset(&sampleMa[len + LPC_FILTERORDER], 0, (len - LPC_FILTERORDER) * 2);
  509. filter_arfq12(sampleMa, sampleAr, synt_denum, LPC_FILTERORDER + 1, 2 * len);
  510. tmp1 = &sampleAr[len - 1];
  511. tmp2 = &sampleAr[2 * len - 1];
  512. tmp3 = Out_fix;
  513. for (k = 0; k < len; k++) {
  514. (*tmp3) = (*tmp1) + (*tmp2);
  515. tmp1--;
  516. tmp2--;
  517. tmp3++;
  518. }
  519. }
  520. static int16_t gain_dequantization(int index, int max_in, int stage)
  521. {
  522. int16_t scale = FFMAX(1638, FFABS(max_in));
  523. return ((scale * ilbc_gain[stage][index]) + 8192) >> 14;
  524. }
  525. static void vector_rmultiplication(int16_t *out, const int16_t *in,
  526. const int16_t *win,
  527. int length, int shift)
  528. {
  529. for (int i = 0; i < length; i++)
  530. out[i] = (in[i] * win[-i]) >> shift;
  531. }
  532. static void vector_multiplication(int16_t *out, const int16_t *in,
  533. const int16_t *win, int length,
  534. int shift)
  535. {
  536. for (int i = 0; i < length; i++)
  537. out[i] = (in[i] * win[i]) >> shift;
  538. }
  539. static void add_vector_and_shift(int16_t *out, const int16_t *in1,
  540. const int16_t *in2, int length,
  541. int shift)
  542. {
  543. for (int i = 0; i < length; i++)
  544. out[i] = (in1[i] + in2[i]) >> shift;
  545. }
  546. static void create_augmented_vector(int index, int16_t *buffer, int16_t *cbVec)
  547. {
  548. int16_t cbVecTmp[4];
  549. int16_t ilow = index - 4;
  550. memcpy(cbVec, buffer - index, index * 2);
  551. vector_multiplication(&cbVec[ilow], buffer - index - 4, alpha, 4, 15);
  552. vector_rmultiplication(cbVecTmp, buffer - 4, &alpha[3], 4, 15);
  553. add_vector_and_shift(&cbVec[ilow], &cbVec[ilow], cbVecTmp, 4, 0);
  554. memcpy(cbVec + index, buffer - index, (SUBL - index) * sizeof(*cbVec));
  555. }
  556. static void get_codebook(int16_t * cbvec, /* (o) Constructed codebook vector */
  557. int16_t * mem, /* (i) Codebook buffer */
  558. int16_t index, /* (i) Codebook index */
  559. int16_t lMem, /* (i) Length of codebook buffer */
  560. int16_t cbveclen /* (i) Codebook vector length */
  561. )
  562. {
  563. int16_t k, base_size;
  564. int16_t lag;
  565. /* Stack based */
  566. int16_t tempbuff2[SUBL + 5];
  567. /* Determine size of codebook sections */
  568. base_size = lMem - cbveclen + 1;
  569. if (cbveclen == SUBL) {
  570. base_size += cbveclen / 2;
  571. }
  572. /* No filter -> First codebook section */
  573. if (index < lMem - cbveclen + 1) {
  574. /* first non-interpolated vectors */
  575. k = index + cbveclen;
  576. /* get vector */
  577. memcpy(cbvec, mem + lMem - k, cbveclen * 2);
  578. } else if (index < base_size) {
  579. /* Calculate lag */
  580. k = (int16_t) SPL_MUL_16_16(2, (index - (lMem - cbveclen + 1))) + cbveclen;
  581. lag = k / 2;
  582. create_augmented_vector(lag, mem + lMem, cbvec);
  583. } else {
  584. int16_t memIndTest;
  585. /* first non-interpolated vectors */
  586. if (index - base_size < lMem - cbveclen + 1) {
  587. /* Set up filter memory, stuff zeros outside memory buffer */
  588. memIndTest = lMem - (index - base_size + cbveclen);
  589. memset(mem - CB_HALFFILTERLEN, 0, CB_HALFFILTERLEN * 2);
  590. memset(mem + lMem, 0, CB_HALFFILTERLEN * 2);
  591. /* do filtering to get the codebook vector */
  592. filter_mafq12(&mem[memIndTest + 4], cbvec, (int16_t *) kCbFiltersRev, CB_FILTERLEN, cbveclen);
  593. } else {
  594. /* interpolated vectors */
  595. /* Stuff zeros outside memory buffer */
  596. memIndTest = lMem - cbveclen - CB_FILTERLEN;
  597. memset(mem + lMem, 0, CB_HALFFILTERLEN * 2);
  598. /* do filtering */
  599. filter_mafq12(&mem[memIndTest + 7], tempbuff2, (int16_t *) kCbFiltersRev, CB_FILTERLEN, (int16_t) (cbveclen + 5));
  600. /* Calculate lag index */
  601. lag = (cbveclen << 1) - 20 + index - base_size - lMem - 1;
  602. create_augmented_vector(lag, tempbuff2 + SUBL + 5, cbvec);
  603. }
  604. }
  605. }
  606. static void construct_vector (
  607. int16_t *decvector, /* (o) Decoded vector */
  608. int16_t *index, /* (i) Codebook indices */
  609. int16_t *gain_index, /* (i) Gain quantization indices */
  610. int16_t *mem, /* (i) Buffer for codevector construction */
  611. int16_t lMem, /* (i) Length of buffer */
  612. int16_t veclen)
  613. {
  614. int16_t gain[CB_NSTAGES];
  615. int16_t cbvec0[SUBL];
  616. int16_t cbvec1[SUBL];
  617. int16_t cbvec2[SUBL];
  618. int32_t a32;
  619. int16_t *gainPtr;
  620. int j;
  621. /* gain de-quantization */
  622. gain[0] = gain_dequantization(gain_index[0], 16384, 0);
  623. gain[1] = gain_dequantization(gain_index[1], gain[0], 1);
  624. gain[2] = gain_dequantization(gain_index[2], gain[1], 2);
  625. /* codebook vector construction and construction of total vector */
  626. /* Stack based */
  627. get_codebook(cbvec0, mem, index[0], lMem, veclen);
  628. get_codebook(cbvec1, mem, index[1], lMem, veclen);
  629. get_codebook(cbvec2, mem, index[2], lMem, veclen);
  630. gainPtr = &gain[0];
  631. for (j = 0; j < veclen; j++) {
  632. a32 = SPL_MUL_16_16(*gainPtr++, cbvec0[j]);
  633. a32 += SPL_MUL_16_16(*gainPtr++, cbvec1[j]);
  634. a32 += SPL_MUL_16_16(*gainPtr, cbvec2[j]);
  635. gainPtr -= 2;
  636. decvector[j] = (a32 + 8192) >> 14;
  637. }
  638. }
  639. static void reverse_memcpy(int16_t *dest, int16_t *source, int length)
  640. {
  641. int16_t* destPtr = dest;
  642. int16_t* sourcePtr = source;
  643. int j;
  644. for (j = 0; j < length; j++)
  645. *destPtr-- = *sourcePtr++;
  646. }
  647. static void decode_residual(ILBCContext *s,
  648. ILBCFrame *encbits,
  649. int16_t *decresidual,
  650. int16_t *syntdenum)
  651. {
  652. int16_t meml_gotten, Nfor, Nback, diff, start_pos;
  653. int16_t subcount, subframe;
  654. int16_t *reverseDecresidual = s->enh_buf; /* Reversed decoded data, used for decoding backwards in time (reuse memory in state) */
  655. int16_t *memVec = s->prevResidual;
  656. int16_t *mem = &memVec[CB_HALFFILTERLEN]; /* Memory for codebook */
  657. diff = STATE_LEN - s->state_short_len;
  658. if (encbits->state_first == 1) {
  659. start_pos = (encbits->start - 1) * SUBL;
  660. } else {
  661. start_pos = (encbits->start - 1) * SUBL + diff;
  662. }
  663. /* decode scalar part of start state */
  664. state_construct(encbits->ifm, encbits->idx, &syntdenum[(encbits->start - 1) * (LPC_FILTERORDER + 1)], &decresidual[start_pos], s->state_short_len);
  665. if (encbits->state_first) { /* put adaptive part in the end */
  666. /* setup memory */
  667. memset(mem, 0, (int16_t) (CB_MEML - s->state_short_len) * 2);
  668. memcpy(mem + CB_MEML - s->state_short_len, decresidual + start_pos, s->state_short_len * 2);
  669. /* construct decoded vector */
  670. construct_vector(&decresidual[start_pos + s->state_short_len], encbits->cb_index, encbits->gain_index, mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, (int16_t) diff);
  671. } else { /* put adaptive part in the beginning */
  672. /* setup memory */
  673. meml_gotten = s->state_short_len;
  674. reverse_memcpy(mem + CB_MEML - 1, decresidual + start_pos, meml_gotten);
  675. memset(mem, 0, (int16_t) (CB_MEML - meml_gotten) * 2);
  676. /* construct decoded vector */
  677. construct_vector(reverseDecresidual, encbits->cb_index, encbits->gain_index, mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, diff);
  678. /* get decoded residual from reversed vector */
  679. reverse_memcpy(&decresidual[start_pos - 1], reverseDecresidual, diff);
  680. }
  681. /* counter for predicted subframes */
  682. subcount = 1;
  683. /* forward prediction of subframes */
  684. Nfor = s->nsub - encbits->start - 1;
  685. if (Nfor > 0) {
  686. /* setup memory */
  687. memset(mem, 0, (CB_MEML - STATE_LEN) * 2);
  688. memcpy(mem + CB_MEML - STATE_LEN, decresidual + (encbits->start - 1) * SUBL, STATE_LEN * 2);
  689. /* loop over subframes to encode */
  690. for (subframe = 0; subframe < Nfor; subframe++) {
  691. /* construct decoded vector */
  692. construct_vector(&decresidual[(encbits->start + 1 + subframe) * SUBL], encbits->cb_index + subcount * CB_NSTAGES, encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL, SUBL);
  693. /* update memory */
  694. memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
  695. memcpy(mem + CB_MEML - SUBL, &decresidual[(encbits->start + 1 + subframe) * SUBL], SUBL * 2);
  696. subcount++;
  697. }
  698. }
  699. /* backward prediction of subframes */
  700. Nback = encbits->start - 1;
  701. if (Nback > 0) {
  702. /* setup memory */
  703. meml_gotten = SUBL * (s->nsub + 1 - encbits->start);
  704. if (meml_gotten > CB_MEML) {
  705. meml_gotten = CB_MEML;
  706. }
  707. reverse_memcpy(mem + CB_MEML - 1, decresidual + (encbits->start - 1) * SUBL, meml_gotten);
  708. memset(mem, 0, (int16_t) (CB_MEML - meml_gotten) * 2);
  709. /* loop over subframes to decode */
  710. for (subframe = 0; subframe < Nback; subframe++) {
  711. /* construct decoded vector */
  712. construct_vector(&reverseDecresidual[subframe * SUBL], encbits->cb_index + subcount * CB_NSTAGES,
  713. encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL, SUBL);
  714. /* update memory */
  715. memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
  716. memcpy(mem + CB_MEML - SUBL, &reverseDecresidual[subframe * SUBL], SUBL * 2);
  717. subcount++;
  718. }
  719. /* get decoded residual from reversed vector */
  720. reverse_memcpy(decresidual + SUBL * Nback - 1, reverseDecresidual, SUBL * Nback);
  721. }
  722. }
  723. static int16_t max_abs_value_w16(const int16_t* vector, int length)
  724. {
  725. int i = 0, absolute = 0, maximum = 0;
  726. if (vector == NULL || length <= 0) {
  727. return -1;
  728. }
  729. for (i = 0; i < length; i++) {
  730. absolute = FFABS(vector[i]);
  731. if (absolute > maximum)
  732. maximum = absolute;
  733. }
  734. // Guard the case for abs(-32768).
  735. return FFMIN(maximum, INT16_MAX);
  736. }
  737. static int16_t get_size_in_bits(uint32_t n)
  738. {
  739. int16_t bits;
  740. if (0xFFFF0000 & n) {
  741. bits = 16;
  742. } else {
  743. bits = 0;
  744. }
  745. if (0x0000FF00 & (n >> bits)) bits += 8;
  746. if (0x000000F0 & (n >> bits)) bits += 4;
  747. if (0x0000000C & (n >> bits)) bits += 2;
  748. if (0x00000002 & (n >> bits)) bits += 1;
  749. if (0x00000001 & (n >> bits)) bits += 1;
  750. return bits;
  751. }
  752. static int32_t scale_dot_product(const int16_t *v1, const int16_t *v2, int length, int scaling)
  753. {
  754. int32_t sum = 0;
  755. for (int i = 0; i < length; i++)
  756. sum += (v1[i] * v2[i]) >> scaling;
  757. return sum;
  758. }
  759. static void correlation(int32_t *corr, int32_t *ener, int16_t *buffer,
  760. int16_t lag, int16_t blen, int16_t srange, int16_t scale)
  761. {
  762. int16_t *w16ptr;
  763. w16ptr = &buffer[blen - srange - lag];
  764. *corr = scale_dot_product(&buffer[blen - srange], w16ptr, srange, scale);
  765. *ener = scale_dot_product(w16ptr, w16ptr, srange, scale);
  766. if (*ener == 0) {
  767. *corr = 0;
  768. *ener = 1;
  769. }
  770. }
  771. #define SPL_SHIFT_W32(x, c) (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c))))
  772. static int16_t norm_w32(int32_t a)
  773. {
  774. if (a == 0) {
  775. return 0;
  776. } else if (a < 0) {
  777. a = ~a;
  778. }
  779. return ff_clz(a);
  780. }
  781. static int32_t div_w32_w16(int32_t num, int16_t den)
  782. {
  783. if (den != 0)
  784. return num / den;
  785. else
  786. return 0x7FFFFFFF;
  787. }
  788. static void do_plc(int16_t *plc_residual, /* (o) concealed residual */
  789. int16_t *plc_lpc, /* (o) concealed LP parameters */
  790. int16_t PLI, /* (i) packet loss indicator
  791. 0 - no PL, 1 = PL */
  792. int16_t *decresidual, /* (i) decoded residual */
  793. int16_t *lpc, /* (i) decoded LPC (only used for no PL) */
  794. int16_t inlag, /* (i) pitch lag */
  795. ILBCContext *s) /* (i/o) decoder instance */
  796. {
  797. int16_t i, pick;
  798. int32_t cross, ener, cross_comp, ener_comp = 0;
  799. int32_t measure, max_measure, energy;
  800. int16_t max, cross_square_max, cross_square;
  801. int16_t j, lag, tmp1, tmp2, randlag;
  802. int16_t shift1, shift2, shift3, shift_max;
  803. int16_t scale3;
  804. int16_t corrLen;
  805. int32_t tmpW32, tmp2W32;
  806. int16_t use_gain;
  807. int16_t tot_gain;
  808. int16_t max_perSquare;
  809. int16_t scale1, scale2;
  810. int16_t totscale;
  811. int32_t nom;
  812. int16_t denom;
  813. int16_t pitchfact;
  814. int16_t use_lag;
  815. int ind;
  816. int16_t randvec[BLOCKL_MAX];
  817. /* Packet Loss */
  818. if (PLI == 1) {
  819. s->consPLICount += 1;
  820. /* if previous frame not lost,
  821. determine pitch pred. gain */
  822. if (s->prevPLI != 1) {
  823. /* Maximum 60 samples are correlated, preserve as high accuracy
  824. as possible without getting overflow */
  825. max = max_abs_value_w16(s->prevResidual, s->block_samples);
  826. scale3 = (get_size_in_bits(max) << 1) - 25;
  827. if (scale3 < 0) {
  828. scale3 = 0;
  829. }
  830. /* Store scale for use when interpolating between the
  831. * concealment and the received packet */
  832. s->prevScale = scale3;
  833. /* Search around the previous lag +/-3 to find the
  834. best pitch period */
  835. lag = inlag - 3;
  836. /* Guard against getting outside the frame */
  837. corrLen = FFMIN(60, s->block_samples - (inlag + 3));
  838. correlation(&cross, &ener, s->prevResidual, lag, s->block_samples, corrLen, scale3);
  839. /* Normalize and store cross^2 and the number of shifts */
  840. shift_max = get_size_in_bits(FFABS(cross)) - 15;
  841. cross_square_max = (int16_t) SPL_MUL_16_16_RSFT(SPL_SHIFT_W32(cross, -shift_max), SPL_SHIFT_W32(cross, -shift_max), 15);
  842. for (j = inlag - 2; j <= inlag + 3; j++) {
  843. correlation(&cross_comp, &ener_comp, s->prevResidual, j, s->block_samples, corrLen, scale3);
  844. /* Use the criteria (corr*corr)/energy to compare if
  845. this lag is better or not. To avoid the division,
  846. do a cross multiplication */
  847. shift1 = get_size_in_bits(FFABS(cross_comp)) - 15;
  848. cross_square = (int16_t) SPL_MUL_16_16_RSFT(SPL_SHIFT_W32(cross_comp, -shift1), SPL_SHIFT_W32(cross_comp, -shift1), 15);
  849. shift2 = get_size_in_bits(ener) - 15;
  850. measure = SPL_MUL_16_16(SPL_SHIFT_W32(ener, -shift2), cross_square);
  851. shift3 = get_size_in_bits(ener_comp) - 15;
  852. max_measure = SPL_MUL_16_16(SPL_SHIFT_W32(ener_comp, -shift3), cross_square_max);
  853. /* Calculate shift value, so that the two measures can
  854. be put in the same Q domain */
  855. if (((shift_max << 1) + shift3) > ((shift1 << 1) + shift2)) {
  856. tmp1 = FFMIN(31, (shift_max << 1) + shift3 - (shift1 << 1) - shift2);
  857. tmp2 = 0;
  858. } else {
  859. tmp1 = 0;
  860. tmp2 = FFMIN(31, (shift1 << 1) + shift2 - (shift_max << 1) - shift3);
  861. }
  862. if ((measure >> tmp1) > (max_measure >> tmp2)) {
  863. /* New lag is better => record lag, measure and domain */
  864. lag = j;
  865. cross_square_max = cross_square;
  866. cross = cross_comp;
  867. shift_max = shift1;
  868. ener = ener_comp;
  869. }
  870. }
  871. /* Calculate the periodicity for the lag with the maximum correlation.
  872. Definition of the periodicity:
  873. abs(corr(vec1, vec2))/(sqrt(energy(vec1))*sqrt(energy(vec2)))
  874. Work in the Square domain to simplify the calculations
  875. max_perSquare is less than 1 (in Q15)
  876. */
  877. tmp2W32 = scale_dot_product(&s->prevResidual[s->block_samples - corrLen], &s->prevResidual[s->block_samples - corrLen], corrLen, scale3);
  878. if ((tmp2W32 > 0) && (ener_comp > 0)) {
  879. /* norm energies to int16_t, compute the product of the energies and
  880. use the upper int16_t as the denominator */
  881. scale1 = norm_w32(tmp2W32) - 16;
  882. tmp1 = SPL_SHIFT_W32(tmp2W32, scale1);
  883. scale2 = norm_w32(ener) - 16;
  884. tmp2 = SPL_SHIFT_W32(ener, scale2);
  885. denom = SPL_MUL_16_16_RSFT(tmp1, tmp2, 16); /* denom in Q(scale1+scale2-16) */
  886. /* Square the cross correlation and norm it such that max_perSquare
  887. will be in Q15 after the division */
  888. totscale = scale1 + scale2 - 1;
  889. tmp1 = SPL_SHIFT_W32(cross, (totscale >> 1));
  890. tmp2 = SPL_SHIFT_W32(cross, totscale - (totscale >> 1));
  891. nom = SPL_MUL_16_16(tmp1, tmp2);
  892. max_perSquare = div_w32_w16(nom, denom);
  893. } else {
  894. max_perSquare = 0;
  895. }
  896. } else {
  897. /* previous frame lost, use recorded lag and gain */
  898. lag = s->prevLag;
  899. max_perSquare = s->per_square;
  900. }
  901. /* Attenuate signal and scale down pitch pred gain if
  902. several frames lost consecutively */
  903. use_gain = 32767; /* 1.0 in Q15 */
  904. if (s->consPLICount * s->block_samples > 320) {
  905. use_gain = 29491; /* 0.9 in Q15 */
  906. } else if (s->consPLICount * s->block_samples > 640) {
  907. use_gain = 22938; /* 0.7 in Q15 */
  908. } else if (s->consPLICount * s->block_samples > 960) {
  909. use_gain = 16384; /* 0.5 in Q15 */
  910. } else if (s->consPLICount * s->block_samples > 1280) {
  911. use_gain = 0; /* 0.0 in Q15 */
  912. }
  913. /* Compute mixing factor of picth repeatition and noise:
  914. for max_per>0.7 set periodicity to 1.0
  915. 0.4<max_per<0.7 set periodicity to (maxper-0.4)/0.7-0.4)
  916. max_per<0.4 set periodicity to 0.0
  917. */
  918. if (max_perSquare > 7868) { /* periodicity > 0.7 (0.7^4=0.2401 in Q15) */
  919. pitchfact = 32767;
  920. } else if (max_perSquare > 839) { /* 0.4 < periodicity < 0.7 (0.4^4=0.0256 in Q15) */
  921. /* find best index and interpolate from that */
  922. ind = 5;
  923. while ((max_perSquare < kPlcPerSqr[ind]) && (ind > 0)) {
  924. ind--;
  925. }
  926. /* pitch fact is approximated by first order */
  927. tmpW32 = kPlcPitchFact[ind] + SPL_MUL_16_16_RSFT(kPlcPfSlope[ind], (max_perSquare - kPlcPerSqr[ind]), 11);
  928. pitchfact = FFMIN(tmpW32, 32767); /* guard against overflow */
  929. } else { /* periodicity < 0.4 */
  930. pitchfact = 0;
  931. }
  932. /* avoid repetition of same pitch cycle (buzzyness) */
  933. use_lag = lag;
  934. if (lag < 80) {
  935. use_lag = 2 * lag;
  936. }
  937. /* compute concealed residual */
  938. energy = 0;
  939. for (i = 0; i < s->block_samples; i++) {
  940. /* noise component - 52 < randlagFIX < 117 */
  941. s->seed = SPL_MUL_16_16(s->seed, 31821) + 13849;
  942. randlag = 53 + (s->seed & 63);
  943. pick = i - randlag;
  944. if (pick < 0) {
  945. randvec[i] = s->prevResidual[s->block_samples + pick];
  946. } else {
  947. randvec[i] = s->prevResidual[pick];
  948. }
  949. /* pitch repeatition component */
  950. pick = i - use_lag;
  951. if (pick < 0) {
  952. plc_residual[i] = s->prevResidual[s->block_samples + pick];
  953. } else {
  954. plc_residual[i] = plc_residual[pick];
  955. }
  956. /* Attinuate total gain for each 10 ms */
  957. if (i < 80) {
  958. tot_gain = use_gain;
  959. } else if (i < 160) {
  960. tot_gain = SPL_MUL_16_16_RSFT(31130, use_gain, 15); /* 0.95*use_gain */
  961. } else {
  962. tot_gain = SPL_MUL_16_16_RSFT(29491, use_gain, 15); /* 0.9*use_gain */
  963. }
  964. /* mix noise and pitch repeatition */
  965. plc_residual[i] = SPL_MUL_16_16_RSFT(tot_gain, (pitchfact * plc_residual[i] + (32767 - pitchfact) * randvec[i] + 16384) >> 15, 15);
  966. /* Shifting down the result one step extra to ensure that no overflow
  967. will occur */
  968. energy += SPL_MUL_16_16_RSFT(plc_residual[i], plc_residual[i], (s->prevScale + 1));
  969. }
  970. /* less than 30 dB, use only noise */
  971. if (energy < SPL_SHIFT_W32(s->block_samples * 900, -s->prevScale - 1)) {
  972. energy = 0;
  973. for (i = 0; i < s->block_samples; i++) {
  974. plc_residual[i] = randvec[i];
  975. }
  976. }
  977. /* use the old LPC */
  978. memcpy(plc_lpc, (*s).prev_lpc, (LPC_FILTERORDER + 1) * 2);
  979. /* Update state in case there are multiple frame losses */
  980. s->prevLag = lag;
  981. s->per_square = max_perSquare;
  982. } else { /* no packet loss, copy input */
  983. memcpy(plc_residual, decresidual, s->block_samples * 2);
  984. memcpy(plc_lpc, lpc, (LPC_FILTERORDER + 1) * 2);
  985. s->consPLICount = 0;
  986. }
  987. /* update state */
  988. s->prevPLI = PLI;
  989. memcpy(s->prev_lpc, plc_lpc, (LPC_FILTERORDER + 1) * 2);
  990. memcpy(s->prevResidual, plc_residual, s->block_samples * 2);
  991. return;
  992. }
  993. static int xcorr_coeff(int16_t *target, int16_t *regressor,
  994. int16_t subl, int16_t searchLen,
  995. int16_t offset, int16_t step)
  996. {
  997. int16_t maxlag;
  998. int16_t pos;
  999. int16_t max;
  1000. int16_t cross_corr_scale, energy_scale;
  1001. int16_t cross_corr_sg_mod, cross_corr_sg_mod_max;
  1002. int32_t cross_corr, energy;
  1003. int16_t cross_corr_mod, energy_mod, enery_mod_max;
  1004. int16_t *tp, *rp;
  1005. int16_t *rp_beg, *rp_end;
  1006. int16_t totscale, totscale_max;
  1007. int16_t scalediff;
  1008. int32_t new_crit, max_crit;
  1009. int shifts;
  1010. int k;
  1011. /* Initializations, to make sure that the first one is selected */
  1012. cross_corr_sg_mod_max = 0;
  1013. enery_mod_max = INT16_MAX;
  1014. totscale_max = -500;
  1015. maxlag = 0;
  1016. pos = 0;
  1017. /* Find scale value and start position */
  1018. if (step == 1) {
  1019. max = max_abs_value_w16(regressor, (int16_t) (subl + searchLen - 1));
  1020. rp_beg = regressor;
  1021. rp_end = &regressor[subl];
  1022. } else { /* step== -1 */
  1023. max = max_abs_value_w16(&regressor[-searchLen], (int16_t) (subl + searchLen - 1));
  1024. rp_beg = &regressor[-1];
  1025. rp_end = &regressor[subl - 1];
  1026. }
  1027. /* Introduce a scale factor on the energy in int32_t in
  1028. order to make sure that the calculation does not
  1029. overflow */
  1030. if (max > 5000) {
  1031. shifts = 2;
  1032. } else {
  1033. shifts = 0;
  1034. }
  1035. /* Calculate the first energy, then do a +/- to get the other energies */
  1036. energy = scale_dot_product(regressor, regressor, subl, shifts);
  1037. for (k = 0; k < searchLen; k++) {
  1038. tp = target;
  1039. rp = &regressor[pos];
  1040. cross_corr = scale_dot_product(tp, rp, subl, shifts);
  1041. if ((energy > 0) && (cross_corr > 0)) {
  1042. /* Put cross correlation and energy on 16 bit word */
  1043. cross_corr_scale = norm_w32(cross_corr) - 16;
  1044. cross_corr_mod = (int16_t) SPL_SHIFT_W32(cross_corr, cross_corr_scale);
  1045. energy_scale = norm_w32(energy) - 16;
  1046. energy_mod = (int16_t) SPL_SHIFT_W32(energy, energy_scale);
  1047. /* Square cross correlation and store upper int16_t */
  1048. cross_corr_sg_mod = (int16_t) SPL_MUL_16_16_RSFT(cross_corr_mod, cross_corr_mod, 16);
  1049. /* Calculate the total number of (dynamic) right shifts that have
  1050. been performed on (cross_corr*cross_corr)/energy
  1051. */
  1052. totscale = energy_scale - (cross_corr_scale << 1);
  1053. /* Calculate the shift difference in order to be able to compare the two
  1054. (cross_corr*cross_corr)/energy in the same domain
  1055. */
  1056. scalediff = totscale - totscale_max;
  1057. scalediff = FFMIN(scalediff, 31);
  1058. scalediff = FFMAX(scalediff, -31);
  1059. /* Compute the cross multiplication between the old best criteria
  1060. and the new one to be able to compare them without using a
  1061. division */
  1062. if (scalediff < 0) {
  1063. new_crit = ((int32_t) cross_corr_sg_mod * enery_mod_max) >> (-scalediff);
  1064. max_crit = ((int32_t) cross_corr_sg_mod_max * energy_mod);
  1065. } else {
  1066. new_crit = ((int32_t) cross_corr_sg_mod * enery_mod_max);
  1067. max_crit = ((int32_t) cross_corr_sg_mod_max * energy_mod) >> scalediff;
  1068. }
  1069. /* Store the new lag value if the new criteria is larger
  1070. than previous largest criteria */
  1071. if (new_crit > max_crit) {
  1072. cross_corr_sg_mod_max = cross_corr_sg_mod;
  1073. enery_mod_max = energy_mod;
  1074. totscale_max = totscale;
  1075. maxlag = k;
  1076. }
  1077. }
  1078. pos += step;
  1079. /* Do a +/- to get the next energy */
  1080. energy += step * ((*rp_end * *rp_end - *rp_beg * *rp_beg) >> shifts);
  1081. rp_beg += step;
  1082. rp_end += step;
  1083. }
  1084. return maxlag + offset;
  1085. }
  1086. static void hp_output(int16_t *signal, const int16_t *ba, int16_t *y,
  1087. int16_t *x, int16_t len)
  1088. {
  1089. int32_t tmp;
  1090. for (int i = 0; i < len; i++) {
  1091. tmp = SPL_MUL_16_16(y[1], ba[3]); /* (-a[1])*y[i-1] (low part) */
  1092. tmp += SPL_MUL_16_16(y[3], ba[4]); /* (-a[2])*y[i-2] (low part) */
  1093. tmp = (tmp >> 15);
  1094. tmp += SPL_MUL_16_16(y[0], ba[3]); /* (-a[1])*y[i-1] (high part) */
  1095. tmp += SPL_MUL_16_16(y[2], ba[4]); /* (-a[2])*y[i-2] (high part) */
  1096. tmp = (tmp << 1);
  1097. tmp += SPL_MUL_16_16(signal[i], ba[0]); /* b[0]*x[0] */
  1098. tmp += SPL_MUL_16_16(x[0], ba[1]); /* b[1]*x[i-1] */
  1099. tmp += SPL_MUL_16_16(x[1], ba[2]); /* b[2]*x[i-2] */
  1100. /* Update state (input part) */
  1101. x[1] = x[0];
  1102. x[0] = signal[i];
  1103. /* Convert back to Q0 and multiply with 2 */
  1104. signal[i] = av_clip_intp2(tmp + 1024, 26) >> 11;
  1105. /* Update state (filtered part) */
  1106. y[2] = y[0];
  1107. y[3] = y[1];
  1108. /* upshift tmp by 3 with saturation */
  1109. if (tmp > 268435455) {
  1110. tmp = INT32_MAX;
  1111. } else if (tmp < -268435456) {
  1112. tmp = INT32_MIN;
  1113. } else {
  1114. tmp = tmp << 3;
  1115. }
  1116. y[0] = tmp >> 16;
  1117. y[1] = (tmp - (y[0] << 16)) >> 1;
  1118. }
  1119. }
  1120. static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
  1121. int *got_frame_ptr, AVPacket *avpkt)
  1122. {
  1123. const uint8_t *buf = avpkt->data;
  1124. AVFrame *frame = data;
  1125. ILBCContext *s = avctx->priv_data;
  1126. int mode = s->mode, ret;
  1127. int16_t *plc_data = &s->plc_residual[LPC_FILTERORDER];
  1128. if ((ret = init_get_bits8(&s->gb, buf, avpkt->size)) < 0)
  1129. return ret;
  1130. memset(&s->frame, 0, sizeof(ILBCFrame));
  1131. frame->nb_samples = s->block_samples;
  1132. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1133. return ret;
  1134. if (unpack_frame(s))
  1135. mode = 0;
  1136. if (s->frame.start < 1)
  1137. mode = 0;
  1138. if (mode) {
  1139. index_conv(s->frame.cb_index);
  1140. lsf_dequantization(s->lsfdeq, s->frame.lsf, s->lpc_n);
  1141. lsf_check_stability(s->lsfdeq, LPC_FILTERORDER, s->lpc_n);
  1142. lsp_interpolate(s->syntdenum, s->weightdenum,
  1143. s->lsfdeq, LPC_FILTERORDER, s);
  1144. decode_residual(s, &s->frame, s->decresidual, s->syntdenum);
  1145. do_plc(s->plc_residual, s->plc_lpc, 0,
  1146. s->decresidual, s->syntdenum + (LPC_FILTERORDER + 1) * (s->nsub - 1),
  1147. s->last_lag, s);
  1148. memcpy(s->decresidual, s->plc_residual, s->block_samples * 2);
  1149. }
  1150. if (s->enhancer) {
  1151. /* TODO */
  1152. } else {
  1153. int16_t lag, i;
  1154. /* Find last lag (since the enhancer is not called to give this info) */
  1155. if (s->mode == 20) {
  1156. lag = xcorr_coeff(&s->decresidual[s->block_samples-60], &s->decresidual[s->block_samples-80],
  1157. 60, 80, 20, -1);
  1158. } else {
  1159. lag = xcorr_coeff(&s->decresidual[s->block_samples-ENH_BLOCKL],
  1160. &s->decresidual[s->block_samples-ENH_BLOCKL-20],
  1161. ENH_BLOCKL, 100, 20, -1);
  1162. }
  1163. /* Store lag (it is needed if next packet is lost) */
  1164. s->last_lag = lag;
  1165. /* copy data and run synthesis filter */
  1166. memcpy(plc_data, s->decresidual, s->block_samples * 2);
  1167. /* Set up the filter state */
  1168. memcpy(&plc_data[-LPC_FILTERORDER], s->syntMem, LPC_FILTERORDER * 2);
  1169. for (i = 0; i < s->nsub; i++) {
  1170. filter_arfq12(plc_data+i*SUBL, plc_data+i*SUBL,
  1171. s->syntdenum + i*(LPC_FILTERORDER + 1),
  1172. LPC_FILTERORDER + 1, SUBL);
  1173. }
  1174. /* Save the filter state */
  1175. memcpy(s->syntMem, &plc_data[s->block_samples-LPC_FILTERORDER], LPC_FILTERORDER * 2);
  1176. }
  1177. memcpy(frame->data[0], plc_data, s->block_samples * 2);
  1178. hp_output((int16_t *)frame->data[0], hp_out_coeffs,
  1179. s->hpimemy, s->hpimemx, s->block_samples);
  1180. memcpy(s->old_syntdenum, s->syntdenum, s->nsub*(LPC_FILTERORDER + 1) * 2);
  1181. s->prev_enh_pl = 0;
  1182. if (mode == 0)
  1183. s->prev_enh_pl = 1;
  1184. *got_frame_ptr = 1;
  1185. return avpkt->size;
  1186. }
  1187. static av_cold int ilbc_decode_init(AVCodecContext *avctx)
  1188. {
  1189. ILBCContext *s = avctx->priv_data;
  1190. if (avctx->block_align == 38)
  1191. s->mode = 20;
  1192. else if (avctx->block_align == 50)
  1193. s->mode = 30;
  1194. else if (avctx->bit_rate > 0)
  1195. s->mode = avctx->bit_rate <= 14000 ? 30 : 20;
  1196. else
  1197. return AVERROR_INVALIDDATA;
  1198. avctx->channels = 1;
  1199. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  1200. avctx->sample_rate = 8000;
  1201. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  1202. if (s->mode == 30) {
  1203. s->block_samples = 240;
  1204. s->nsub = NSUB_30MS;
  1205. s->nasub = NASUB_30MS;
  1206. s->lpc_n = LPC_N_30MS;
  1207. s->state_short_len = STATE_SHORT_LEN_30MS;
  1208. } else {
  1209. s->block_samples = 160;
  1210. s->nsub = NSUB_20MS;
  1211. s->nasub = NASUB_20MS;
  1212. s->lpc_n = LPC_N_20MS;
  1213. s->state_short_len = STATE_SHORT_LEN_20MS;
  1214. }
  1215. return 0;
  1216. }
  1217. AVCodec ff_ilbc_decoder = {
  1218. .name = "ilbc",
  1219. .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
  1220. .type = AVMEDIA_TYPE_AUDIO,
  1221. .id = AV_CODEC_ID_ILBC,
  1222. .init = ilbc_decode_init,
  1223. .decode = ilbc_decode_frame,
  1224. .capabilities = AV_CODEC_CAP_DR1,
  1225. .priv_data_size = sizeof(ILBCContext),
  1226. };