iCBConstruct.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /******************************************************************
  2. iLBC Speech Coder ANSI-C Source Code
  3. iCBConstruct.c
  4. Copyright (C) The Internet Society (2004).
  5. All Rights Reserved.
  6. ******************************************************************/
  7. #include <math.h>
  8. #include "iLBC_define.h"
  9. #include "iCBConstruct.h"
  10. #include "gainquant.h"
  11. #include "getCBvec.h"
  12. /*----------------------------------------------------------------*
  13. * Convert the codebook indexes to make the search easier
  14. *---------------------------------------------------------------*/
  15. void index_conv_enc(
  16. int *index /* (i/o) Codebook indexes */
  17. ){
  18. int k;
  19. for (k=1; k<CB_NSTAGES; k++) {
  20. if ((index[k]>=108)&&(index[k]<172)) {
  21. index[k]-=64;
  22. } else if (index[k]>=236) {
  23. index[k]-=128;
  24. } else {
  25. /* ERROR */
  26. }
  27. }
  28. }
  29. void index_conv_dec(
  30. int *index /* (i/o) Codebook indexes */
  31. ){
  32. int k;
  33. for (k=1; k<CB_NSTAGES; k++) {
  34. if ((index[k]>=44)&&(index[k]<108)) {
  35. index[k]+=64;
  36. } else if ((index[k]>=108)&&(index[k]<128)) {
  37. index[k]+=128;
  38. } else {
  39. /* ERROR */
  40. }
  41. }
  42. }
  43. /*----------------------------------------------------------------*
  44. * Construct decoded vector from codebook and gains.
  45. *---------------------------------------------------------------*/
  46. void iCBConstruct(
  47. float *decvector, /* (o) Decoded vector */
  48. int *index, /* (i) Codebook indices */
  49. int *gain_index,/* (i) Gain quantization indices */
  50. float *mem, /* (i) Buffer for codevector construction */
  51. int lMem, /* (i) Length of buffer */
  52. int veclen, /* (i) Length of vector */
  53. int nStages /* (i) Number of codebook stages */
  54. ){
  55. int j,k;
  56. float gain[CB_NSTAGES];
  57. float cbvec[SUBL];
  58. /* gain de-quantization */
  59. gain[0] = gaindequant(gain_index[0], 1.0, 32);
  60. if (nStages > 1) {
  61. gain[1] = gaindequant(gain_index[1],
  62. (float)fabs(gain[0]), 16);
  63. }
  64. if (nStages > 2) {
  65. gain[2] = gaindequant(gain_index[2],
  66. (float)fabs(gain[1]), 8);
  67. }
  68. /* codebook vector construction and construction of
  69. total vector */
  70. getCBvec(cbvec, mem, index[0], lMem, veclen);
  71. for (j=0;j<veclen;j++){
  72. decvector[j] = gain[0]*cbvec[j];
  73. }
  74. if (nStages > 1) {
  75. for (k=1; k<nStages; k++) {
  76. getCBvec(cbvec, mem, index[k], lMem, veclen);
  77. for (j=0;j<veclen;j++) {
  78. decvector[j] += gain[k]*cbvec[j];
  79. }
  80. }
  81. }
  82. }