wamr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. *****************************************************************************
  3. * INCLUDE FILES
  4. *****************************************************************************
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include "typedef.h"
  11. #include "cnst.h"
  12. #include "n_proc.h"
  13. #include "mode.h"
  14. #include "frame.h"
  15. #include "strfunc.h"
  16. #include "sp_enc.h"
  17. #include "sp_dec.h"
  18. #include "pre_proc.h"
  19. #include "sid_sync.h"
  20. #include "vadname.h"
  21. #include "e_homing.h"
  22. #include "wamr.h"
  23. Speech_Encode_FrameState *speech_encoder_state = NULL;
  24. sid_syncState *sid_state = NULL;
  25. Speech_Decode_FrameState *speech_decoder_state = NULL;
  26. Word16 reset_flag_old;
  27. const Word16 packed_size[16]={12,13,15,17,19,20,26,31,5,0,0,0,0,0,0,0};
  28. int wamr_encoder_init()
  29. {//Initialisation of the coder
  30. Word16 dtx = 0; /* enable encoder DTX */
  31. if(Speech_Encode_Frame_init(&speech_encoder_state, dtx, "encoder") || sid_sync_init (&sid_state)) return AMRC_INIT_FAILED;
  32. else return AMRC_INIT_OK;
  33. }
  34. void wamr_encoder_uninit()
  35. {
  36. if(NULL != speech_encoder_state) Speech_Encode_Frame_exit(&speech_encoder_state);
  37. if(NULL != sid_state) sid_sync_exit (&sid_state);
  38. }
  39. #include "log.h"
  40. int wamr_encoder_encode_frame(Word16 *new_speech, int speech_size, unsigned char *outPacked, int outPacked_size, unsigned short *outLen, int mode)
  41. {
  42. UWord8 packed_bits[MAX_PACKED_SIZE];
  43. Word16 packed_size;
  44. Word16 serial[SERIAL_FRAMESIZE];
  45. int i;
  46. Word16 reset_flag;
  47. enum TXFrameType tx_type;
  48. enum Mode used_mode;
  49. if(L_FRAME != speech_size) return AMRC_CODEC_BUFFER_ERR;
  50. if(outPacked_size < MAX_PACKED_SIZE) return AMRC_CODEC_BUFFER_ERR;
  51. /* check for homing frame */
  52. reset_flag = encoder_homing_frame_test(new_speech);
  53. /* encode speech */
  54. Speech_Encode_Frame(speech_encoder_state, mode,new_speech, &serial[1], &used_mode);
  55. /* include frame type and mode information in serial bitstream */
  56. sid_sync (sid_state, used_mode, &tx_type);
  57. packed_size = PackBits(used_mode, mode, tx_type, &serial[1], packed_bits);
  58. for(i=0;i<packed_size;i++) outPacked[i]=packed_bits[i];
  59. *outLen=packed_size;
  60. if(reset_flag != 0){
  61. Speech_Encode_Frame_reset(speech_encoder_state);
  62. sid_sync_reset(sid_state);
  63. }
  64. return AMRC_CODEC_SUCC;
  65. }
  66. int wamr_decoder_init()
  67. {//Initialisation of the decoder
  68. reset_flag_old=1;
  69. if(Speech_Decode_Frame_init(&speech_decoder_state, "Decoder")) return AMRC_INIT_FAILED;
  70. else return AMRC_INIT_OK;
  71. }
  72. void wamr_decoder_uninit()
  73. {
  74. if(NULL != speech_decoder_state) Speech_Decode_Frame_exit(&speech_decoder_state);
  75. }
  76. int wamr_decoder_decode_frame(unsigned char *inpackedBits, unsigned short inpacked_size, short *outSpeech, unsigned short out_size)
  77. {
  78. unsigned char q;
  79. Word16 ft;
  80. UWord8 packed_bits[MAX_PACKED_SIZE];
  81. Word16 synth[L_FRAME];
  82. Word16 reset_flag=0;
  83. int i;
  84. int rxframetypeMode=0;
  85. enum Mode mode = (enum Mode)0;
  86. enum RXFrameType rx_type = (enum RXFrameType)0;
  87. Word16 serial[SERIAL_FRAMESIZE];
  88. if(32 < inpacked_size) return AMRC_CODEC_BUFFER_ERR;
  89. if(L_FRAME != out_size) return AMRC_CODEC_BUFFER_ERR;
  90. q = (inpackedBits[0]>>2) & 0x01;
  91. ft= (inpackedBits[0]>>3) & 0x0F;
  92. memset(packed_bits, 0, sizeof(packed_bits));
  93. memcpy(packed_bits, inpackedBits+1,packed_size[ft]);
  94. rx_type = UnpackBits(q, ft, packed_bits, &mode, &serial[1]);
  95. if(rx_type == RX_NO_DATA) mode = speech_decoder_state->prev_mode;
  96. else speech_decoder_state->prev_mode=mode;
  97. /* if homed: check if this frame is another homing frame */
  98. if (reset_flag_old == 1){
  99. /* only check until end of first subframe */
  100. reset_flag = decoder_homing_frame_test_first(&serial[1], mode);
  101. }
  102. /* produce encoder homing frame if homed & input=decoder homing frame */
  103. if ((reset_flag != 0) && (reset_flag_old != 0)){
  104. for (i = 0; i < L_FRAME; i++){
  105. synth[i] = EHF_MASK;
  106. }
  107. }else{
  108. /* decode frame */
  109. Speech_Decode_Frame(speech_decoder_state, mode, &serial[1],rx_type, synth);
  110. }
  111. for(i=0;i<L_FRAME;i++) outSpeech[i]=synth[i];
  112. /* if not homed: check whether current frame is a homing frame */
  113. if (reset_flag_old == 0){
  114. /* check whole frame */
  115. reset_flag = decoder_homing_frame_test(&serial[1], mode);
  116. }
  117. /* reset decoder if current frame is a homing frame */
  118. if (reset_flag != 0){
  119. Speech_Decode_Frame_reset(speech_decoder_state);
  120. }
  121. reset_flag_old = reset_flag;
  122. return AMRC_CODEC_SUCC;
  123. }