TRSpeex.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <malloc.h>
  2. #include "TRSpeex.h"
  3. int TRSpeexDecodeInit(TRSpeexDecodeContex* stDecode)
  4. {
  5. int modeID = -1;
  6. const SpeexMode *decmode=NULL;
  7. int nframes;
  8. int vbr_enabled;
  9. int chan;
  10. int rate;
  11. void *st;
  12. int quality;
  13. int dec_frame_size;
  14. int complexity;
  15. int nbBytes;
  16. int ret;
  17. int enh_enabled;
  18. int decrate;
  19. int declookahead;
  20. if(stDecode == NULL)
  21. return -1;
  22. modeID = SPEEX_MODEID_WB;
  23. speex_bits_init(&(stDecode->bits));
  24. decmode = speex_lib_get_mode (modeID);
  25. stDecode->st = speex_decoder_init(decmode);
  26. if(stDecode->st == NULL)
  27. return -1;
  28. enh_enabled = 1;
  29. decrate = 16000;
  30. speex_decoder_ctl(stDecode->st, SPEEX_SET_ENH, &enh_enabled);
  31. speex_decoder_ctl(stDecode->st, SPEEX_SET_SAMPLING_RATE, &decrate);
  32. speex_decoder_ctl(stDecode->st, SPEEX_GET_FRAME_SIZE, &dec_frame_size);
  33. speex_decoder_ctl(stDecode->st, SPEEX_GET_LOOKAHEAD, &declookahead);
  34. stDecode->frame_size = dec_frame_size;
  35. stDecode->pFifo = (PCMFifoBuffer*)malloc(sizeof(PCMFifoBuffer));
  36. if(stDecode->pFifo != NULL)
  37. {
  38. ret = pcm_fifo_init(stDecode->pFifo, 1024*10000);
  39. if(ret == -1)
  40. return -1;
  41. }
  42. else
  43. return -1;
  44. return 1;
  45. }
  46. int TRSpeexDecode(TRSpeexDecodeContex* stDecode,char* pInput, int nInputSize, char* pOutput, int* nOutSize)
  47. {
  48. int nbBytes;
  49. char aInputBuffer[MAX_FRAME_BYTES];
  50. int nFrameNo;
  51. int nDecSize;
  52. int nTmpSize;
  53. int ret = 0;
  54. if(stDecode == NULL)
  55. return -1;
  56. if(pInput == NULL)
  57. return -1;
  58. if(pOutput == NULL)
  59. return -1;
  60. if(nInputSize < 0)
  61. return -1;
  62. if(nInputSize > 1024*10000)
  63. return -1;
  64. if(stDecode->pFifo != NULL)
  65. pcm_fifo_write(stDecode->pFifo, (unsigned char*)pInput, nInputSize);
  66. else
  67. return -1;
  68. nFrameNo = 0;
  69. nDecSize = 0;
  70. nTmpSize = 0;
  71. while(pcm_fifo_size(stDecode->pFifo) >= 60)
  72. {
  73. pcm_fifo_read(stDecode->pFifo, (unsigned char*)aInputBuffer, 60);
  74. speex_bits_read_from(&(stDecode->bits), aInputBuffer, 60);
  75. ret = speex_decode_int(stDecode->st, &(stDecode->bits), (spx_int16_t*)pOutput+nFrameNo*(stDecode->frame_size));
  76. if(ret == -1 || ret == -2)
  77. {
  78. nOutSize = 0;
  79. return -1;
  80. }
  81. nTmpSize += stDecode->frame_size*2;
  82. nFrameNo ++;
  83. }
  84. *nOutSize = nTmpSize;
  85. return 1;
  86. }
  87. int TRSpeexDecodeRelease(TRSpeexDecodeContex* stDecode)
  88. {
  89. if(stDecode == NULL)
  90. return -1;
  91. if (stDecode->st != NULL)
  92. speex_decoder_destroy(stDecode->st);
  93. speex_bits_destroy(&(stDecode->bits));
  94. if(stDecode->pFifo != NULL)
  95. {
  96. pcm_fifo_free(stDecode->pFifo);
  97. free(stDecode->pFifo);
  98. stDecode->pFifo = NULL;
  99. }
  100. return 1;
  101. }