iLBC_encode.c 16 KB

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