iLBC_decode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. iLBC_decode.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include "iLBC_define.h"
  10. #include "iLBC_decode.h"
  11. #include "StateConstructW.h"
  12. #include "LPCdecode.h"
  13. #include "iCBConstruct.h"
  14. #include "doCPLC.h"
  15. #include "helpfun.h"
  16. #include "constants.h"
  17. #include "packing.h"
  18. #include "string.h"
  19. #include "enhancer.h"
  20. #include "hpOutput.h"
  21. #include "syntFilter.h"
  22. /*----------------------------------------------------------------*
  23. * Initiation of decoder instance.
  24. *---------------------------------------------------------------*/
  25. short initDecode( /* (o) Number of decoded
  26. samples */
  27. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
  28. int mode, /* (i) frame size mode */
  29. int use_enhancer /* (i) 1 to use enhancer
  30. 0 to run without
  31. enhancer */
  32. ){
  33. int i;
  34. iLBCdec_inst->mode = mode;
  35. if (mode==30) {
  36. iLBCdec_inst->blockl = BLOCKL_30MS;
  37. iLBCdec_inst->nsub = NSUB_30MS;
  38. iLBCdec_inst->nasub = NASUB_30MS;
  39. iLBCdec_inst->lpc_n = LPC_N_30MS;
  40. iLBCdec_inst->no_of_bytes = NO_OF_BYTES_30MS;
  41. iLBCdec_inst->no_of_words = NO_OF_WORDS_30MS;
  42. iLBCdec_inst->state_short_len=STATE_SHORT_LEN_30MS;
  43. /* ULP init */
  44. iLBCdec_inst->ULP_inst=&ULP_30msTbl;
  45. }
  46. else if (mode==20) {
  47. iLBCdec_inst->blockl = BLOCKL_20MS;
  48. iLBCdec_inst->nsub = NSUB_20MS;
  49. iLBCdec_inst->nasub = NASUB_20MS;
  50. iLBCdec_inst->lpc_n = LPC_N_20MS;
  51. iLBCdec_inst->no_of_bytes = NO_OF_BYTES_20MS;
  52. iLBCdec_inst->no_of_words = NO_OF_WORDS_20MS;
  53. iLBCdec_inst->state_short_len=STATE_SHORT_LEN_20MS;
  54. /* ULP init */
  55. iLBCdec_inst->ULP_inst=&ULP_20msTbl;
  56. }
  57. else {
  58. exit(2);
  59. }
  60. memset(iLBCdec_inst->syntMem, 0,
  61. LPC_FILTERORDER*sizeof(float));
  62. memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
  63. LPC_FILTERORDER*sizeof(float));
  64. memset(iLBCdec_inst->old_syntdenum, 0,
  65. ((LPC_FILTERORDER + 1)*NSUB_MAX)*sizeof(float));
  66. for (i=0; i<NSUB_MAX; i++)
  67. iLBCdec_inst->old_syntdenum[i*(LPC_FILTERORDER+1)]=1.0;
  68. iLBCdec_inst->last_lag = 20;
  69. iLBCdec_inst->prevLag = 120;
  70. iLBCdec_inst->per = 0.0;
  71. iLBCdec_inst->consPLICount = 0;
  72. iLBCdec_inst->prevPLI = 0;
  73. iLBCdec_inst->prevLpc[0] = 1.0;
  74. memset(iLBCdec_inst->prevLpc+1,0,
  75. LPC_FILTERORDER*sizeof(float));
  76. memset(iLBCdec_inst->prevResidual, 0, BLOCKL_MAX*sizeof(float));
  77. iLBCdec_inst->seed=777;
  78. memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
  79. iLBCdec_inst->use_enhancer = use_enhancer;
  80. memset(iLBCdec_inst->enh_buf, 0, ENH_BUFL*sizeof(float));
  81. for (i=0;i<ENH_NBLOCKS_TOT;i++)
  82. iLBCdec_inst->enh_period[i]=(float)40.0;
  83. iLBCdec_inst->prev_enh_pl = 0;
  84. return (iLBCdec_inst->blockl);
  85. }
  86. /*----------------------------------------------------------------*
  87. * frame residual decoder function (subrutine to iLBC_decode)
  88. *---------------------------------------------------------------*/
  89. static void Decode(
  90. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
  91. structure */
  92. float *decresidual, /* (o) decoded residual frame */
  93. int start, /* (i) location of start
  94. state */
  95. int idxForMax, /* (i) codebook index for the
  96. maximum value */
  97. int *idxVec, /* (i) codebook indexes for the
  98. samples in the start
  99. state */
  100. float *syntdenum, /* (i) the decoded synthesis
  101. filter coefficients */
  102. int *cb_index, /* (i) the indexes for the
  103. adaptive codebook */
  104. int *gain_index, /* (i) the indexes for the
  105. corresponding gains */
  106. int *extra_cb_index, /* (i) the indexes for the
  107. adaptive codebook part
  108. of start state */
  109. int *extra_gain_index, /* (i) the indexes for the
  110. corresponding gains */
  111. int state_first /* (i) 1 if non adaptive part
  112. of start state comes
  113. first 0 if that part
  114. comes last */
  115. ){
  116. float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
  117. int k, meml_gotten, Nfor, Nback, i;
  118. int diff, start_pos;
  119. int subcount, subframe;
  120. diff = STATE_LEN - iLBCdec_inst->state_short_len;
  121. if (state_first == 1) {
  122. start_pos = (start-1)*SUBL;
  123. } else {
  124. start_pos = (start-1)*SUBL + diff;
  125. }
  126. /* decode scalar part of start state */
  127. StateConstructW(idxForMax, idxVec,
  128. &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
  129. &decresidual[start_pos], iLBCdec_inst->state_short_len);
  130. if (state_first) { /* put adaptive part in the end */
  131. /* setup memory */
  132. memset(mem, 0,
  133. (CB_MEML-iLBCdec_inst->state_short_len)*sizeof(float));
  134. memcpy(mem+CB_MEML-iLBCdec_inst->state_short_len,
  135. decresidual+start_pos,
  136. iLBCdec_inst->state_short_len*sizeof(float));
  137. /* construct decoded vector */
  138. iCBConstruct(
  139. &decresidual[start_pos+iLBCdec_inst->state_short_len],
  140. extra_cb_index, extra_gain_index, mem+CB_MEML-stMemLTbl,
  141. stMemLTbl, diff, CB_NSTAGES);
  142. }
  143. else {/* put adaptive part in the beginning */
  144. /* create reversed vectors for prediction */
  145. for (k=0; k<diff; k++) {
  146. reverseDecresidual[k] =
  147. decresidual[(start+1)*SUBL-1-
  148. (k+iLBCdec_inst->state_short_len)];
  149. }
  150. /* setup memory */
  151. meml_gotten = iLBCdec_inst->state_short_len;
  152. for (k=0; k<meml_gotten; k++){
  153. mem[CB_MEML-1-k] = decresidual[start_pos + k];
  154. }
  155. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  156. /* construct decoded vector */
  157. iCBConstruct(reverseDecresidual, extra_cb_index,
  158. extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
  159. diff, CB_NSTAGES);
  160. /* get decoded residual from reversed vector */
  161. for (k=0; k<diff; k++) {
  162. decresidual[start_pos-1-k] = reverseDecresidual[k];
  163. }
  164. }
  165. /* counter for predicted sub-frames */
  166. subcount=0;
  167. /* forward prediction of sub-frames */
  168. Nfor = iLBCdec_inst->nsub-start-1;
  169. if ( Nfor > 0 ){
  170. /* setup memory */
  171. memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
  172. memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
  173. STATE_LEN*sizeof(float));
  174. /* loop over sub-frames to encode */
  175. for (subframe=0; subframe<Nfor; subframe++) {
  176. /* construct decoded vector */
  177. iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
  178. cb_index+subcount*CB_NSTAGES,
  179. gain_index+subcount*CB_NSTAGES,
  180. mem+CB_MEML-memLfTbl[subcount],
  181. memLfTbl[subcount], SUBL, CB_NSTAGES);
  182. /* update memory */
  183. memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  184. memcpy(mem+CB_MEML-SUBL,
  185. &decresidual[(start+1+subframe)*SUBL],
  186. SUBL*sizeof(float));
  187. subcount++;
  188. }
  189. }
  190. /* backward prediction of sub-frames */
  191. Nback = start-1;
  192. if ( Nback > 0 ) {
  193. /* setup memory */
  194. meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
  195. if ( meml_gotten > CB_MEML ) {
  196. meml_gotten=CB_MEML;
  197. }
  198. for (k=0; k<meml_gotten; k++) {
  199. mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
  200. }
  201. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  202. /* loop over subframes to decode */
  203. for (subframe=0; subframe<Nback; subframe++) {
  204. /* construct decoded vector */
  205. iCBConstruct(&reverseDecresidual[subframe*SUBL],
  206. cb_index+subcount*CB_NSTAGES,
  207. gain_index+subcount*CB_NSTAGES,
  208. mem+CB_MEML-memLfTbl[subcount], memLfTbl[subcount],
  209. SUBL, CB_NSTAGES);
  210. /* update memory */
  211. memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  212. memcpy(mem+CB_MEML-SUBL,
  213. &reverseDecresidual[subframe*SUBL],
  214. SUBL*sizeof(float));
  215. subcount++;
  216. }
  217. /* get decoded residual from reversed vector */
  218. for (i=0; i<SUBL*Nback; i++)
  219. decresidual[SUBL*Nback - i - 1] =
  220. reverseDecresidual[i];
  221. }
  222. }
  223. /*----------------------------------------------------------------*
  224. * main decoder function
  225. *---------------------------------------------------------------*/
  226. void iLBC_decode(
  227. float *decblock, /* (o) decoded signal block */
  228. unsigned char *bytes, /* (i) encoded signal bits */
  229. iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
  230. structure */
  231. int mode /* (i) 0: bad packet, PLC,
  232. 1: normal */
  233. ){
  234. float data[BLOCKL_MAX];
  235. float lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
  236. float PLCresidual[BLOCKL_MAX], PLClpc[LPC_FILTERORDER + 1];
  237. float zeros[BLOCKL_MAX], one[LPC_FILTERORDER + 1];
  238. int k, i, start, idxForMax, pos, lastpart, ulp;
  239. int lag, ilag;
  240. float cc, maxcc;
  241. int idxVec[STATE_LEN];
  242. int check;
  243. int gain_index[NASUB_MAX*CB_NSTAGES],
  244. extra_gain_index[CB_NSTAGES];
  245. int cb_index[CB_NSTAGES*NASUB_MAX], extra_cb_index[CB_NSTAGES];
  246. int lsf_i[LSF_NSPLIT*LPC_N_MAX];
  247. int state_first;
  248. int last_bit;
  249. unsigned char *pbytes;
  250. float weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
  251. int order_plus_one;
  252. float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
  253. float decresidual[BLOCKL_MAX];
  254. if (mode>0) { /* the data are good */
  255. /* decode data */
  256. pbytes=bytes;
  257. pos=0;
  258. /* Set everything to zero before decoding */
  259. for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
  260. lsf_i[k]=0;
  261. }
  262. start=0;
  263. state_first=0;
  264. idxForMax=0;
  265. for (k=0; k<iLBCdec_inst->state_short_len; k++) {
  266. idxVec[k]=0;
  267. }
  268. for (k=0; k<CB_NSTAGES; k++) {
  269. extra_cb_index[k]=0;
  270. }
  271. for (k=0; k<CB_NSTAGES; k++) {
  272. extra_gain_index[k]=0;
  273. }
  274. for (i=0; i<iLBCdec_inst->nasub; i++) {
  275. for (k=0; k<CB_NSTAGES; k++) {
  276. cb_index[i*CB_NSTAGES+k]=0;
  277. }
  278. }
  279. for (i=0; i<iLBCdec_inst->nasub; i++) {
  280. for (k=0; k<CB_NSTAGES; k++) {
  281. gain_index[i*CB_NSTAGES+k]=0;
  282. }
  283. }
  284. /* loop over ULP classes */
  285. for (ulp=0; ulp<3; ulp++) {
  286. /* LSF */
  287. for (k=0; k<LSF_NSPLIT*iLBCdec_inst->lpc_n; k++){
  288. unpack( &pbytes, &lastpart,
  289. iLBCdec_inst->ULP_inst->lsf_bits[k][ulp], &pos);
  290. packcombine(&lsf_i[k], lastpart,
  291. iLBCdec_inst->ULP_inst->lsf_bits[k][ulp]);
  292. }
  293. /* Start block info */
  294. unpack( &pbytes, &lastpart,
  295. iLBCdec_inst->ULP_inst->start_bits[ulp], &pos);
  296. packcombine(&start, lastpart,
  297. iLBCdec_inst->ULP_inst->start_bits[ulp]);
  298. unpack( &pbytes, &lastpart,
  299. iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
  300. packcombine(&state_first, lastpart,
  301. iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
  302. unpack( &pbytes, &lastpart,
  303. iLBCdec_inst->ULP_inst->scale_bits[ulp], &pos);
  304. packcombine(&idxForMax, lastpart,
  305. iLBCdec_inst->ULP_inst->scale_bits[ulp]);
  306. for (k=0; k<iLBCdec_inst->state_short_len; k++) {
  307. unpack( &pbytes, &lastpart,
  308. iLBCdec_inst->ULP_inst->state_bits[ulp], &pos);
  309. packcombine(idxVec+k, lastpart,
  310. iLBCdec_inst->ULP_inst->state_bits[ulp]);
  311. }
  312. /* 23/22 (20ms/30ms) sample block */
  313. for (k=0; k<CB_NSTAGES; k++) {
  314. unpack( &pbytes, &lastpart,
  315. iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp],
  316. &pos);
  317. packcombine(extra_cb_index+k, lastpart,
  318. iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
  319. }
  320. for (k=0; k<CB_NSTAGES; k++) {
  321. unpack( &pbytes, &lastpart,
  322. iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp],
  323. &pos);
  324. packcombine(extra_gain_index+k, lastpart,
  325. iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
  326. }
  327. /* The two/four (20ms/30ms) 40 sample sub-blocks */
  328. for (i=0; i<iLBCdec_inst->nasub; i++) {
  329. for (k=0; k<CB_NSTAGES; k++) {
  330. unpack( &pbytes, &lastpart,
  331. iLBCdec_inst->ULP_inst->cb_index[i][k][ulp],
  332. &pos);
  333. packcombine(cb_index+i*CB_NSTAGES+k, lastpart,
  334. iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
  335. }
  336. }
  337. for (i=0; i<iLBCdec_inst->nasub; i++) {
  338. for (k=0; k<CB_NSTAGES; k++) {
  339. unpack( &pbytes, &lastpart,
  340. iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp],
  341. &pos);
  342. packcombine(gain_index+i*CB_NSTAGES+k, lastpart,
  343. iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
  344. }
  345. }
  346. }
  347. /* Extract last bit. If it is 1 this indicates an
  348. empty/lost frame */
  349. unpack( &pbytes, &last_bit, 1, &pos);
  350. /* Check for bit errors or empty/lost frames */
  351. if (start<1)
  352. mode = 0;
  353. if (iLBCdec_inst->mode==20 && start>3)
  354. mode = 0;
  355. if (iLBCdec_inst->mode==30 && start>5)
  356. mode = 0;
  357. if (last_bit==1)
  358. mode = 0;
  359. if (mode==1) { /* No bit errors was detected,
  360. continue decoding */
  361. /* adjust index */
  362. index_conv_dec(cb_index);
  363. /* decode the lsf */
  364. SimplelsfDEQ(lsfdeq, lsf_i, iLBCdec_inst->lpc_n);
  365. check=LSF_check(lsfdeq, LPC_FILTERORDER,
  366. iLBCdec_inst->lpc_n);
  367. DecoderInterpolateLSF(syntdenum, weightdenum,
  368. lsfdeq, LPC_FILTERORDER, iLBCdec_inst);
  369. Decode(iLBCdec_inst, decresidual, start, idxForMax,
  370. idxVec, syntdenum, cb_index, gain_index,
  371. extra_cb_index, extra_gain_index,
  372. state_first);
  373. /* preparing the plc for a future loss! */
  374. doThePLC(PLCresidual, PLClpc, 0, decresidual,
  375. syntdenum +
  376. (LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
  377. (*iLBCdec_inst).last_lag, iLBCdec_inst);
  378. memcpy(decresidual, PLCresidual,
  379. iLBCdec_inst->blockl*sizeof(float));
  380. }
  381. }
  382. if (mode == 0) {
  383. /* the data is bad (either a PLC call
  384. * was made or a severe bit error was detected)
  385. */
  386. /* packet loss conceal */
  387. memset(zeros, 0, BLOCKL_MAX*sizeof(float));
  388. one[0] = 1;
  389. memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
  390. start=0;
  391. doThePLC(PLCresidual, PLClpc, 1, zeros, one,
  392. (*iLBCdec_inst).last_lag, iLBCdec_inst);
  393. memcpy(decresidual, PLCresidual,
  394. iLBCdec_inst->blockl*sizeof(float));
  395. order_plus_one = LPC_FILTERORDER + 1;
  396. for (i = 0; i < iLBCdec_inst->nsub; i++) {
  397. memcpy(syntdenum+(i*order_plus_one), PLClpc,
  398. order_plus_one*sizeof(float));
  399. }
  400. }
  401. if (iLBCdec_inst->use_enhancer == 1) {
  402. /* post filtering */
  403. iLBCdec_inst->last_lag =
  404. enhancerInterface(data, decresidual, iLBCdec_inst);
  405. /* synthesis filtering */
  406. if (iLBCdec_inst->mode==20) {
  407. /* Enhancer has 40 samples delay */
  408. i=0;
  409. syntFilter(data + i*SUBL,
  410. iLBCdec_inst->old_syntdenum +
  411. (i+iLBCdec_inst->nsub-1)*(LPC_FILTERORDER+1),
  412. SUBL, iLBCdec_inst->syntMem);
  413. for (i=1; i < iLBCdec_inst->nsub; i++) {
  414. syntFilter(data + i*SUBL,
  415. syntdenum + (i-1)*(LPC_FILTERORDER+1),
  416. SUBL, iLBCdec_inst->syntMem);
  417. }
  418. } else if (iLBCdec_inst->mode==30) {
  419. /* Enhancer has 80 samples delay */
  420. for (i=0; i < 2; i++) {
  421. syntFilter(data + i*SUBL,
  422. iLBCdec_inst->old_syntdenum +
  423. (i+iLBCdec_inst->nsub-2)*(LPC_FILTERORDER+1),
  424. SUBL, iLBCdec_inst->syntMem);
  425. }
  426. for (i=2; i < iLBCdec_inst->nsub; i++) {
  427. syntFilter(data + i*SUBL,
  428. syntdenum + (i-2)*(LPC_FILTERORDER+1), SUBL,
  429. iLBCdec_inst->syntMem);
  430. }
  431. }
  432. } else {
  433. /* Find last lag */
  434. lag = 20;
  435. maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
  436. &decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
  437. for (ilag=21; ilag<120; ilag++) {
  438. cc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
  439. &decresidual[BLOCKL_MAX-ENH_BLOCKL-ilag],
  440. ENH_BLOCKL);
  441. if (cc > maxcc) {
  442. maxcc = cc;
  443. lag = ilag;
  444. }
  445. }
  446. iLBCdec_inst->last_lag = lag;
  447. /* copy data and run synthesis filter */
  448. memcpy(data, decresidual,
  449. iLBCdec_inst->blockl*sizeof(float));
  450. for (i=0; i < iLBCdec_inst->nsub; i++) {
  451. syntFilter(data + i*SUBL,
  452. syntdenum + i*(LPC_FILTERORDER+1), SUBL,
  453. iLBCdec_inst->syntMem);
  454. }
  455. }
  456. /* high pass filtering on output if desired, otherwise
  457. copy to out */
  458. hpOutput(data, iLBCdec_inst->blockl,
  459. decblock,iLBCdec_inst->hpomem);
  460. /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
  461. memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
  462. iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
  463. iLBCdec_inst->prev_enh_pl=0;
  464. if (mode==0) { /* PLC was used */
  465. iLBCdec_inst->prev_enh_pl=1;
  466. }
  467. }