iLBC_encode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. iLBC_encode.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "iLBC_define.h"
  11. #include "LPCencode.h"
  12. #include "FrameClassify.h"
  13. #include "StateSearchW.h"
  14. #include "StateConstructW.h"
  15. #include "helpfun.h"
  16. #include "constants.h"
  17. #include "packing.h"
  18. #include "iCBSearch.h"
  19. #include "iCBConstruct.h"
  20. #include "hpInput.h"
  21. #include "anaFilter.h"
  22. #include "syntFilter.h"
  23. /*----------------------------------------------------------------*
  24. * Initiation of encoder instance.
  25. *---------------------------------------------------------------*/
  26. short initEncode( /* (o) Number of bytes
  27. encoded */
  28. iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */
  29. int mode /* (i) frame size mode */
  30. ){
  31. iLBCenc_inst->mode = mode;
  32. if (mode==30) {
  33. iLBCenc_inst->blockl = BLOCKL_30MS;
  34. iLBCenc_inst->nsub = NSUB_30MS;
  35. iLBCenc_inst->nasub = NASUB_30MS;
  36. iLBCenc_inst->lpc_n = LPC_N_30MS;
  37. iLBCenc_inst->no_of_bytes = NO_OF_BYTES_30MS;
  38. iLBCenc_inst->no_of_words = NO_OF_WORDS_30MS;
  39. iLBCenc_inst->state_short_len=STATE_SHORT_LEN_30MS;
  40. /* ULP init */
  41. iLBCenc_inst->ULP_inst=&ULP_30msTbl;
  42. }
  43. else if (mode==20) {
  44. iLBCenc_inst->blockl = BLOCKL_20MS;
  45. iLBCenc_inst->nsub = NSUB_20MS;
  46. iLBCenc_inst->nasub = NASUB_20MS;
  47. iLBCenc_inst->lpc_n = LPC_N_20MS;
  48. iLBCenc_inst->no_of_bytes = NO_OF_BYTES_20MS;
  49. iLBCenc_inst->no_of_words = NO_OF_WORDS_20MS;
  50. iLBCenc_inst->state_short_len=STATE_SHORT_LEN_20MS;
  51. /* ULP init */
  52. iLBCenc_inst->ULP_inst=&ULP_20msTbl;
  53. }
  54. else {
  55. exit(2);
  56. }
  57. memset((*iLBCenc_inst).anaMem, 0,
  58. LPC_FILTERORDER*sizeof(float));
  59. memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl,
  60. LPC_FILTERORDER*sizeof(float));
  61. memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl,
  62. LPC_FILTERORDER*sizeof(float));
  63. memset((*iLBCenc_inst).lpc_buffer, 0,
  64. (LPC_LOOKBACK+BLOCKL_MAX)*sizeof(float));
  65. memset((*iLBCenc_inst).hpimem, 0, 4*sizeof(float));
  66. return (short)(iLBCenc_inst->no_of_bytes);
  67. }
  68. /*----------------------------------------------------------------*
  69. * main encoder function
  70. *---------------------------------------------------------------*/
  71. void iLBC_encode(
  72. unsigned char *bytes, /* (o) encoded data bits iLBC */
  73. float *block, /* (o) speech vector to
  74. encode */
  75. iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder
  76. state */
  77. ){
  78. float data[BLOCKL_MAX];
  79. float residual[BLOCKL_MAX], reverseResidual[BLOCKL_MAX];
  80. int start, idxForMax, idxVec[STATE_LEN];
  81. float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
  82. int n, k, meml_gotten, Nfor, Nback, i, pos;
  83. int gain_index[CB_NSTAGES*NASUB_MAX],
  84. extra_gain_index[CB_NSTAGES];
  85. int cb_index[CB_NSTAGES*NASUB_MAX],extra_cb_index[CB_NSTAGES];
  86. int lsf_i[LSF_NSPLIT*LPC_N_MAX];
  87. unsigned char *pbytes;
  88. int diff, start_pos, state_first;
  89. float en1, en2;
  90. int index, ulp, firstpart;
  91. int subcount, subframe;
  92. float weightState[LPC_FILTERORDER];
  93. float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
  94. float weightdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
  95. float decresidual[BLOCKL_MAX];
  96. /* high pass filtering of input signal if such is not done
  97. prior to calling this function */
  98. hpInput(block, iLBCenc_inst->blockl,
  99. data, (*iLBCenc_inst).hpimem);
  100. /* otherwise simply copy */
  101. /*memcpy(data,block,iLBCenc_inst->blockl*sizeof(float));*/
  102. /* LPC of hp filtered input data */
  103. LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst);
  104. /* inverse filter to get residual */
  105. for (n=0; n<iLBCenc_inst->nsub; n++) {
  106. anaFilter(&data[n*SUBL], &syntdenum[n*(LPC_FILTERORDER+1)],
  107. SUBL, &residual[n*SUBL], iLBCenc_inst->anaMem);
  108. }
  109. /* find state location */
  110. start = FrameClassify(iLBCenc_inst, residual);
  111. /* check if state should be in first or last part of the
  112. two subframes */
  113. diff = STATE_LEN - iLBCenc_inst->state_short_len;
  114. en1 = 0;
  115. index = (start-1)*SUBL;
  116. for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
  117. en1 += residual[index+i]*residual[index+i];
  118. }
  119. en2 = 0;
  120. index = (start-1)*SUBL+diff;
  121. for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
  122. en2 += residual[index+i]*residual[index+i];
  123. }
  124. if (en1 > en2) {
  125. state_first = 1;
  126. start_pos = (start-1)*SUBL;
  127. } else {
  128. state_first = 0;
  129. start_pos = (start-1)*SUBL + diff;
  130. }
  131. /* scalar quantization of state */
  132. StateSearchW(iLBCenc_inst, &residual[start_pos],
  133. &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
  134. &weightdenum[(start-1)*(LPC_FILTERORDER+1)], &idxForMax,
  135. idxVec, iLBCenc_inst->state_short_len, state_first);
  136. StateConstructW(idxForMax, idxVec,
  137. &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
  138. &decresidual[start_pos], iLBCenc_inst->state_short_len);
  139. /* predictive quantization in state */
  140. if (state_first) { /* put adaptive part in the end */
  141. /* setup memory */
  142. memset(mem, 0,
  143. (CB_MEML-iLBCenc_inst->state_short_len)*sizeof(float));
  144. memcpy(mem+CB_MEML-iLBCenc_inst->state_short_len,
  145. decresidual+start_pos,
  146. iLBCenc_inst->state_short_len*sizeof(float));
  147. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  148. /* encode sub-frames */
  149. iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
  150. &residual[start_pos+iLBCenc_inst->state_short_len],
  151. mem+CB_MEML-stMemLTbl,
  152. stMemLTbl, diff, CB_NSTAGES,
  153. &weightdenum[start*(LPC_FILTERORDER+1)],
  154. weightState, 0);
  155. /* construct decoded vector */
  156. iCBConstruct(
  157. &decresidual[start_pos+iLBCenc_inst->state_short_len],
  158. extra_cb_index, extra_gain_index,
  159. mem+CB_MEML-stMemLTbl,
  160. stMemLTbl, diff, CB_NSTAGES);
  161. }
  162. else { /* put adaptive part in the beginning */
  163. /* create reversed vectors for prediction */
  164. for (k=0; k<diff; k++) {
  165. reverseResidual[k] = residual[(start+1)*SUBL-1
  166. -(k+iLBCenc_inst->state_short_len)];
  167. }
  168. /* setup memory */
  169. meml_gotten = iLBCenc_inst->state_short_len;
  170. for (k=0; k<meml_gotten; k++) {
  171. mem[CB_MEML-1-k] = decresidual[start_pos + k];
  172. }
  173. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  174. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  175. /* encode sub-frames */
  176. iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
  177. reverseResidual, mem+CB_MEML-stMemLTbl, stMemLTbl,
  178. diff, CB_NSTAGES,
  179. &weightdenum[(start-1)*(LPC_FILTERORDER+1)],
  180. weightState, 0);
  181. /* construct decoded vector */
  182. iCBConstruct(reverseDecresidual, extra_cb_index,
  183. extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
  184. diff, CB_NSTAGES);
  185. /* get decoded residual from reversed vector */
  186. for (k=0; k<diff; k++) {
  187. decresidual[start_pos-1-k] = reverseDecresidual[k];
  188. }
  189. }
  190. /* counter for predicted sub-frames */
  191. subcount=0;
  192. /* forward prediction of sub-frames */
  193. Nfor = iLBCenc_inst->nsub-start-1;
  194. if ( Nfor > 0 ) {
  195. /* setup memory */
  196. memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
  197. memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
  198. STATE_LEN*sizeof(float));
  199. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  200. /* loop over sub-frames to encode */
  201. for (subframe=0; subframe<Nfor; subframe++) {
  202. /* encode sub-frame */
  203. iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
  204. gain_index+subcount*CB_NSTAGES,
  205. &residual[(start+1+subframe)*SUBL],
  206. mem+CB_MEML-memLfTbl[subcount],
  207. memLfTbl[subcount], SUBL, CB_NSTAGES,
  208. &weightdenum[(start+1+subframe)*
  209. (LPC_FILTERORDER+1)],
  210. weightState, subcount+1);
  211. /* construct decoded vector */
  212. iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
  213. cb_index+subcount*CB_NSTAGES,
  214. gain_index+subcount*CB_NSTAGES,
  215. mem+CB_MEML-memLfTbl[subcount],
  216. memLfTbl[subcount], SUBL, CB_NSTAGES);
  217. /* update memory */
  218. memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  219. memcpy(mem+CB_MEML-SUBL,
  220. &decresidual[(start+1+subframe)*SUBL],
  221. SUBL*sizeof(float));
  222. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  223. subcount++;
  224. }
  225. }
  226. /* backward prediction of sub-frames */
  227. Nback = start-1;
  228. if ( Nback > 0 ) {
  229. /* create reverse order vectors */
  230. for (n=0; n<Nback; n++) {
  231. for (k=0; k<SUBL; k++) {
  232. reverseResidual[n*SUBL+k] =
  233. residual[(start-1)*SUBL-1-n*SUBL-k];
  234. reverseDecresidual[n*SUBL+k] =
  235. decresidual[(start-1)*SUBL-1-n*SUBL-k];
  236. }
  237. }
  238. /* setup memory */
  239. meml_gotten = SUBL*(iLBCenc_inst->nsub+1-start);
  240. if ( meml_gotten > CB_MEML ) {
  241. meml_gotten=CB_MEML;
  242. }
  243. for (k=0; k<meml_gotten; k++) {
  244. mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
  245. }
  246. memset(mem, 0, (CB_MEML-k)*sizeof(float));
  247. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  248. /* loop over sub-frames to encode */
  249. for (subframe=0; subframe<Nback; subframe++) {
  250. /* encode sub-frame */
  251. iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
  252. gain_index+subcount*CB_NSTAGES,
  253. &reverseResidual[subframe*SUBL],
  254. mem+CB_MEML-memLfTbl[subcount],
  255. memLfTbl[subcount], SUBL, CB_NSTAGES,
  256. &weightdenum[(start-2-subframe)*
  257. (LPC_FILTERORDER+1)],
  258. weightState, subcount+1);
  259. /* construct decoded vector */
  260. iCBConstruct(&reverseDecresidual[subframe*SUBL],
  261. cb_index+subcount*CB_NSTAGES,
  262. gain_index+subcount*CB_NSTAGES,
  263. mem+CB_MEML-memLfTbl[subcount],
  264. memLfTbl[subcount], SUBL, CB_NSTAGES);
  265. /* update memory */
  266. memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
  267. memcpy(mem+CB_MEML-SUBL,
  268. &reverseDecresidual[subframe*SUBL],
  269. SUBL*sizeof(float));
  270. memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
  271. subcount++;
  272. }
  273. /* get decoded residual from reversed vector */
  274. for (i=0; i<SUBL*Nback; i++) {
  275. decresidual[SUBL*Nback - i - 1] =
  276. reverseDecresidual[i];
  277. }
  278. }
  279. /* end encoding part */
  280. /* adjust index */
  281. index_conv_enc(cb_index);
  282. /* pack bytes */
  283. pbytes=bytes;
  284. pos=0;
  285. /* loop over the 3 ULP classes */
  286. for (ulp=0; ulp<3; ulp++) {
  287. /* LSF */
  288. for (k=0; k<LSF_NSPLIT*iLBCenc_inst->lpc_n; k++) {
  289. packsplit(&lsf_i[k], &firstpart, &lsf_i[k],
  290. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp],
  291. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp]+
  292. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+1]+
  293. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+2]);
  294. dopack( &pbytes, firstpart,
  295. iLBCenc_inst->ULP_inst->lsf_bits[k][ulp], &pos);
  296. }
  297. /* Start block info */
  298. packsplit(&start, &firstpart, &start,
  299. iLBCenc_inst->ULP_inst->start_bits[ulp],
  300. iLBCenc_inst->ULP_inst->start_bits[ulp]+
  301. iLBCenc_inst->ULP_inst->start_bits[ulp+1]+
  302. iLBCenc_inst->ULP_inst->start_bits[ulp+2]);
  303. dopack( &pbytes, firstpart,
  304. iLBCenc_inst->ULP_inst->start_bits[ulp], &pos);
  305. packsplit(&state_first, &firstpart, &state_first,
  306. iLBCenc_inst->ULP_inst->startfirst_bits[ulp],
  307. iLBCenc_inst->ULP_inst->startfirst_bits[ulp]+
  308. iLBCenc_inst->ULP_inst->startfirst_bits[ulp+1]+
  309. iLBCenc_inst->ULP_inst->startfirst_bits[ulp+2]);
  310. dopack( &pbytes, firstpart,
  311. iLBCenc_inst->ULP_inst->startfirst_bits[ulp], &pos);
  312. packsplit(&idxForMax, &firstpart, &idxForMax,
  313. iLBCenc_inst->ULP_inst->scale_bits[ulp],
  314. iLBCenc_inst->ULP_inst->scale_bits[ulp]+
  315. iLBCenc_inst->ULP_inst->scale_bits[ulp+1]+
  316. iLBCenc_inst->ULP_inst->scale_bits[ulp+2]);
  317. dopack( &pbytes, firstpart,
  318. iLBCenc_inst->ULP_inst->scale_bits[ulp], &pos);
  319. for (k=0; k<iLBCenc_inst->state_short_len; k++) {
  320. packsplit(idxVec+k, &firstpart, idxVec+k,
  321. iLBCenc_inst->ULP_inst->state_bits[ulp],
  322. iLBCenc_inst->ULP_inst->state_bits[ulp]+
  323. iLBCenc_inst->ULP_inst->state_bits[ulp+1]+
  324. iLBCenc_inst->ULP_inst->state_bits[ulp+2]);
  325. dopack( &pbytes, firstpart,
  326. iLBCenc_inst->ULP_inst->state_bits[ulp], &pos);
  327. }
  328. /* 23/22 (20ms/30ms) sample block */
  329. for (k=0;k<CB_NSTAGES;k++) {
  330. packsplit(extra_cb_index+k, &firstpart,
  331. extra_cb_index+k,
  332. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
  333. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp]+
  334. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+1]+
  335. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+2]);
  336. dopack( &pbytes, firstpart,
  337. iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
  338. &pos);
  339. }
  340. for (k=0;k<CB_NSTAGES;k++) {
  341. packsplit(extra_gain_index+k, &firstpart,
  342. extra_gain_index+k,
  343. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
  344. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp]+
  345. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+1]+
  346. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+2]);
  347. dopack( &pbytes, firstpart,
  348. iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
  349. &pos);
  350. }
  351. /* The two/four (20ms/30ms) 40 sample sub-blocks */
  352. for (i=0; i<iLBCenc_inst->nasub; i++) {
  353. for (k=0; k<CB_NSTAGES; k++) {
  354. packsplit(cb_index+i*CB_NSTAGES+k, &firstpart,
  355. cb_index+i*CB_NSTAGES+k,
  356. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
  357. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp]+
  358. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+1]+
  359. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+2]);
  360. dopack( &pbytes, firstpart,
  361. iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
  362. &pos);
  363. }
  364. }
  365. for (i=0; i<iLBCenc_inst->nasub; i++) {
  366. for (k=0; k<CB_NSTAGES; k++) {
  367. packsplit(gain_index+i*CB_NSTAGES+k, &firstpart,
  368. gain_index+i*CB_NSTAGES+k,
  369. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
  370. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp]+
  371. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+1]+
  372. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+2]);
  373. dopack( &pbytes, firstpart,
  374. iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
  375. &pos);
  376. }
  377. }
  378. }
  379. /* set the last bit to zero (otherwise the decoder
  380. will treat it as a lost frame) */
  381. dopack( &pbytes, 0, 1, &pos);
  382. }