iLBC_decode.c 20 KB

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