/lib/libopenh323/src/iLBC/LPCencode.c

https://github.com/rhuitl/uClinux · C · 231 lines · 138 code · 41 blank · 52 comment · 10 complexity · e4c037509419f002c0dabd624d7d45ac MD5 · raw file

  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. LPCencode.c
  4. Copyright (c) 2001,
  5. Global IP Sound AB.
  6. All rights reserved.
  7. ******************************************************************/
  8. #include <string.h>
  9. #include "iLBC_define.h"
  10. #include "helpfun.h"
  11. #include "lsf.h"
  12. #include "constants.h"
  13. /*----------------------------------------------------------------*
  14. * lpc analysis (subrutine to LPCencode)
  15. *---------------------------------------------------------------*/
  16. void SimpleAnalysis(
  17. float *lsf, /* (o) lsf coefficients */
  18. float *data, /* (i) new data vector */
  19. iLBC_Enc_Inst_t *iLBCenc_inst
  20. /* (i/o) the encoder state structure */
  21. ){
  22. int k, is;
  23. float temp[BLOCKL_MAX], lp[LPC_FILTERORDER + 1];
  24. float lp2[LPC_FILTERORDER + 1];
  25. float r[LPC_FILTERORDER + 1];
  26. is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
  27. memcpy(iLBCenc_inst->lpc_buffer+is,data,
  28. iLBCenc_inst->blockl*sizeof(float));
  29. /* No lookahead, last window is asymmetric */
  30. for (k = 0; k < iLBCenc_inst->lpc_n; k++) {
  31. is = LPC_LOOKBACK;
  32. if (k < (iLBCenc_inst->lpc_n - 1)) {
  33. window(temp, lpc_winTbl,
  34. iLBCenc_inst->lpc_buffer, BLOCKL_MAX);
  35. } else {
  36. window(temp, lpc_asymwinTbl,
  37. iLBCenc_inst->lpc_buffer + is, BLOCKL_MAX);
  38. }
  39. iLBC_autocorr(r, temp, BLOCKL_MAX, LPC_FILTERORDER);
  40. window(r, r, lpc_lagwinTbl, LPC_FILTERORDER + 1);
  41. levdurb(lp, temp, r, LPC_FILTERORDER);
  42. bwexpand(lp2, lp, LPC_CHIRP_SYNTDENUM, LPC_FILTERORDER+1);
  43. a2lsf(lsf + k*LPC_FILTERORDER, lp2);
  44. }
  45. is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
  46. memcpy(iLBCenc_inst->lpc_buffer,
  47. iLBCenc_inst->lpc_buffer+LPC_LOOKBACK+BLOCKL_MAX-is,
  48. is*sizeof(float));
  49. }
  50. /*----------------------------------------------------------------*
  51. * lsf interpolator and conversion from lsf to a coefficients
  52. * (subrutine to SimpleInterpolateLSF)
  53. *---------------------------------------------------------------*/
  54. void LSFinterpolate2a_enc(
  55. float *a, /* (o) lpc coefficients */
  56. float *lsf1,/* (i) first set of lsf coefficients */
  57. float *lsf2,/* (i) second set of lsf coefficients */
  58. float coef, /* (i) weighting coefficient to use between lsf1
  59. and lsf2 */
  60. long length /* (i) length of coefficient vectors */
  61. ){
  62. float lsftmp[LPC_FILTERORDER];
  63. interpolate(lsftmp, lsf1, lsf2, coef, length);
  64. lsf2a(a, lsftmp);
  65. }
  66. /*----------------------------------------------------------------*
  67. * lsf interpolator (subrutine to LPCencode)
  68. *---------------------------------------------------------------*/
  69. void SimpleInterpolateLSF(
  70. float *syntdenum, /* (o) the synthesis filter denominator
  71. resulting from the quantized
  72. interpolated lsf */
  73. float *weightdenum, /* (o) the weighting filter denominator
  74. resulting from the unquantized
  75. interpolated lsf */
  76. float *lsf, /* (i) the unquantized lsf coefficients */
  77. float *lsfdeq, /* (i) the dequantized lsf coefficients */
  78. float *lsfold, /* (i) the unquantized lsf coefficients of
  79. the previous signal frame */
  80. float *lsfdeqold, /* (i) the dequantized lsf coefficients of
  81. the previous signal frame */
  82. int length, /* (i) should equate LPC_FILTERORDER */
  83. iLBC_Enc_Inst_t *iLBCenc_inst
  84. /* (i/o) the encoder state structure */
  85. ){
  86. int i, pos, lp_length;
  87. float lp[LPC_FILTERORDER + 1], *lsf2, *lsfdeq2;
  88. lsf2 = lsf + length;
  89. lsfdeq2 = lsfdeq + length;
  90. lp_length = length + 1;
  91. if (iLBCenc_inst->mode==30) {
  92. /* subframe 1: Interpolation between old and first set of
  93. lsf coefficients */
  94. LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
  95. lsf_weightTbl_30ms[0], length);
  96. memcpy(syntdenum,lp,lp_length*sizeof(float));
  97. LSFinterpolate2a_enc(lp, lsfold, lsf,
  98. lsf_weightTbl_30ms[0], length);
  99. bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM, lp_length);
  100. /* subframe 2 to 6: Interpolation between first and second
  101. set of lsf coefficients */
  102. pos = lp_length;
  103. for (i = 1; i < iLBCenc_inst->nsub; i++) {
  104. LSFinterpolate2a_enc(lp, lsfdeq, lsfdeq2,
  105. lsf_weightTbl_30ms[i], length);
  106. memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
  107. LSFinterpolate2a_enc(lp, lsf, lsf2,
  108. lsf_weightTbl_30ms[i], length);
  109. bwexpand(weightdenum + pos, lp,
  110. LPC_CHIRP_WEIGHTDENUM, lp_length);
  111. pos += lp_length;
  112. }
  113. }
  114. else {
  115. pos = 0;
  116. for (i = 0; i < iLBCenc_inst->nsub; i++) {
  117. LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
  118. lsf_weightTbl_20ms[i], length);
  119. memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
  120. LSFinterpolate2a_enc(lp, lsfold, lsf,
  121. lsf_weightTbl_20ms[i], length);
  122. bwexpand(weightdenum+pos, lp,
  123. LPC_CHIRP_WEIGHTDENUM, lp_length);
  124. pos += lp_length;
  125. }
  126. }
  127. /* update memory */
  128. if (iLBCenc_inst->mode==30) {
  129. memcpy(lsfold, lsf2, length*sizeof(float));
  130. memcpy(lsfdeqold, lsfdeq2, length*sizeof(float));
  131. }
  132. else {
  133. memcpy(lsfold, lsf, length*sizeof(float));
  134. memcpy(lsfdeqold, lsfdeq, length*sizeof(float));
  135. }
  136. }
  137. /*----------------------------------------------------------------*
  138. * lsf quantizer (subrutine to LPCencode)
  139. *---------------------------------------------------------------*/
  140. void SimplelsfQ(
  141. float *lsfdeq, /* (o) dequantized lsf coefficients
  142. (dimension FILTERORDER) */
  143. int *index, /* (o) quantization index */
  144. float *lsf, /* (i) the lsf coefficient vector to be
  145. quantized (dimension FILTERORDER ) */
  146. int lpc_n /* (i) number of lsf sets to quantize */
  147. ){
  148. /* Quantize first LSF with memoryless split VQ */
  149. SplitVQ(lsfdeq, index, lsf, lsfCbTbl, LSF_NSPLIT,
  150. dim_lsfCbTbl, size_lsfCbTbl);
  151. if (lpc_n==2) {
  152. /* Quantize second LSF with memoryless split VQ */
  153. SplitVQ(lsfdeq + LPC_FILTERORDER, index + LSF_NSPLIT,
  154. lsf + LPC_FILTERORDER, lsfCbTbl, LSF_NSPLIT,
  155. dim_lsfCbTbl, size_lsfCbTbl);
  156. }
  157. }
  158. /*----------------------------------------------------------------*
  159. * lpc encoder
  160. *---------------------------------------------------------------*/
  161. void LPCencode(
  162. float *syntdenum, /* (i/o) synthesis filter coefficients
  163. before/after encoding */
  164. float *weightdenum, /* (i/o) weighting denumerator coefficients
  165. before/after encoding */
  166. int *lsf_index, /* (o) lsf quantization index */
  167. float *data, /* (i) lsf coefficients to quantize */
  168. iLBC_Enc_Inst_t *iLBCenc_inst
  169. /* (i/o) the encoder state structure */
  170. ){
  171. float lsf[LPC_FILTERORDER * LPC_N_MAX];
  172. float lsfdeq[LPC_FILTERORDER * LPC_N_MAX];
  173. int change=0;
  174. SimpleAnalysis(lsf, data, iLBCenc_inst);
  175. SimplelsfQ(lsfdeq, lsf_index, lsf, iLBCenc_inst->lpc_n);
  176. change=LSF_check(lsfdeq, LPC_FILTERORDER, iLBCenc_inst->lpc_n);
  177. SimpleInterpolateLSF(syntdenum, weightdenum,
  178. lsf, lsfdeq, iLBCenc_inst->lsfold,
  179. iLBCenc_inst->lsfdeqold, LPC_FILTERORDER, iLBCenc_inst);
  180. }