nb_celp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <math.h>
  2. #include "nb_celp.h"
  3. #include "lsp.h"
  4. #include "ltp.h"
  5. #include "quant_lsp.h"
  6. #include "cb_search.h"
  7. #include "filters.h"
  8. #include "os_support.h"
  9. #ifndef NULL
  10. #define NULL 0
  11. #endif
  12. #define LSP_MARGIN .002f
  13. #define SIG_SCALING 1.f
  14. #define NB_DEC_BUFFER (NB_FRAME_SIZE+2*NB_PITCH_END+NB_SUBFRAME_SIZE+12)
  15. #define NB_ORDER 10
  16. #define NB_FRAME_SIZE 160
  17. #define NB_SUBFRAME_SIZE 40
  18. #define NB_NB_SUBFRAMES 4
  19. #define NB_PITCH_START 17
  20. #define NB_PITCH_END 144
  21. struct speex_decode_state {
  22. float excBuf[NB_DEC_BUFFER]; /**< Excitation buffer */
  23. float *exc; /**< Start of excitation frame */
  24. float old_qlsp[10]; /**< Quantized LSPs for previous frame */
  25. float interp_qlpc[10]; /**< Interpolated quantized LPCs */
  26. float mem_sp[10]; /**< Filter memory for synthesis signal */
  27. int first; /**< Is this the first frame? */
  28. };
  29. static const float exc_gain_quant_scal1[2] = {0.70469f, 1.05127f};
  30. struct speex_decode_state *nb_decoder_init(void)
  31. {
  32. struct speex_decode_state *st;
  33. st = malloc(sizeof(*st));
  34. if (!st)
  35. return NULL;
  36. memset(st, 0, sizeof(*st));
  37. st->first = 1;
  38. return st;
  39. }
  40. void nb_decoder_destroy(struct speex_decode_state *state)
  41. {
  42. if (state)
  43. free(state);
  44. }
  45. /* basic decoder using mode3 only */
  46. int nb_decode(struct speex_decode_state *st, SpeexBits *bits, float *out)
  47. {
  48. int i, sub, wideband, mode, qe;
  49. float ol_gain;
  50. float innov[NB_SUBFRAME_SIZE];
  51. float exc32[NB_SUBFRAME_SIZE];
  52. float qlsp[NB_ORDER], interp_qlsp[NB_ORDER];
  53. float ak[NB_ORDER];
  54. if (!bits)
  55. return -1;
  56. st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
  57. /* Decode Sub-modes */
  58. do {
  59. if (speex_bits_remaining(bits) < 5)
  60. return -1;
  61. wideband = speex_bits_unpack_unsigned(bits, 1);
  62. if (wideband) {
  63. printf("wideband not supported\n");
  64. return -2;
  65. }
  66. mode = speex_bits_unpack_unsigned(bits, 4);
  67. if (mode == 15)
  68. return -1;
  69. } while (mode > 8);
  70. if (mode != 3) {
  71. printf("only mode 3 supported\n");
  72. return -2;
  73. }
  74. /* Shift all buffers by one frame */
  75. SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE,
  76. 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
  77. /* Unquantize LSPs */
  78. lsp_unquant_lbr(qlsp, NB_ORDER, bits);
  79. /* Handle first frame */
  80. if (st->first) {
  81. st->first = 0;
  82. for (i=0; i<NB_ORDER; i++)
  83. st->old_qlsp[i] = qlsp[i];
  84. }
  85. /* Get global excitation gain */
  86. qe = speex_bits_unpack_unsigned(bits, 5);
  87. ol_gain = SIG_SCALING*exp(qe/3.5);
  88. /* Loop on subframes */
  89. for (sub=0; sub<4; sub++) {
  90. int offset, q_energy;
  91. float *exc, *sp;
  92. float ener;
  93. offset = NB_SUBFRAME_SIZE*sub;
  94. exc = st->exc + offset;
  95. sp = out + offset;
  96. SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
  97. /* Adaptive codebook contribution */
  98. pitch_unquant_3tap(exc, exc32, NB_PITCH_START,
  99. NB_SUBFRAME_SIZE, bits, 0);
  100. sanitize_values32(exc32, -32000, 32000, NB_SUBFRAME_SIZE);
  101. /* Unquantize the innovation */
  102. SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
  103. /* Decode sub-frame gain correction */
  104. q_energy = speex_bits_unpack_unsigned(bits, 1);
  105. ener = exc_gain_quant_scal1[q_energy] * ol_gain;
  106. /* Fixed codebook contribution */
  107. split_cb_shape_sign_unquant(innov, bits);
  108. /* De-normalize innovation and update excitation */
  109. signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
  110. for (i=0; i<NB_SUBFRAME_SIZE; i++) {
  111. exc[i] = exc32[i] + innov[i];
  112. }
  113. }
  114. SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
  115. /* Loop on subframes */
  116. for (sub=0; sub<4; sub++) {
  117. const int offset = NB_SUBFRAME_SIZE*sub;
  118. float *sp, *exc;
  119. sp = out + offset;
  120. exc = st->exc + offset;
  121. /* LSP interpolation (quantized and unquantized) */
  122. lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER,
  123. sub, NB_NB_SUBFRAMES, LSP_MARGIN);
  124. /* Compute interpolated LPCs (unquantized) */
  125. lsp_to_lpc(interp_qlsp, ak, NB_ORDER);
  126. iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE,
  127. NB_ORDER, st->mem_sp);
  128. /* Save for interpolation in next frame */
  129. for (i=0; i<NB_ORDER; i++)
  130. st->interp_qlpc[i] = ak[i];
  131. }
  132. /* Store the LSPs for interpolation in the next frame */
  133. for (i=0; i<NB_ORDER; i++)
  134. st->old_qlsp[i] = qlsp[i];
  135. return 0;
  136. }