ilbcdec.c 50 KB

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