iCBConstruct.c 2.8 KB

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