evrcc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "evrcc.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "globs.h"
  6. #include "typedefs.h"
  7. #include "dsp_math.h"
  8. #include "macro.h"
  9. #include "bqiir.h"
  10. #include "decode.h"
  11. #include "encode.h"
  12. #include "ns127.h"
  13. #include "rda.h"
  14. #include "evrcpacket.h"
  15. #define SPEECH_FRAME_SAMPLES 160
  16. #define LOOKAHEAD_LEN 80
  17. #define BITSTREAM_FRAME_MAX_BYTES 24 /* data + 1 word for rate */
  18. #define BITSTREAM_FRAME_MIN_BYTES 4
  19. #include "mallocSw.h"//if no this file,just include stdlib.h if system supported
  20. #define SWAP_16(x) \
  21. ((uint16_t)( \
  22. (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
  23. (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
  24. typedef struct EvrcEncoderContext {
  25. Shortword beta;
  26. Longword R[17];
  27. int16_t min_rate;
  28. int16_t max_rate;
  29. int16_t noise_suppression;
  30. } EvrcEncoderContext;
  31. void* evrc_encoder_init(int16_t min_rate,int16_t max_rate,int16_t noise_suppression) {
  32. EvrcEncoderContext* context = (EvrcEncoderContext*)wmalloc(sizeof(EvrcEncoderContext));
  33. memset(context,0,sizeof(EvrcEncoderContext));
  34. context->min_rate = min_rate;
  35. context->max_rate = max_rate;
  36. context->noise_suppression = noise_suppression;
  37. InitEncoder();
  38. return context;
  39. }
  40. void evrc_encoder_uninit(void* context) {
  41. if( context ) {
  42. wfree(context);
  43. }
  44. }
  45. const static size_t FRAME_SIZE[] = { 0,2+2,0,10+2,22+2,0,0,0,0,0,0,0,0,0,0,0,0 };
  46. const static size_t FRAME_DATA_SIZE[] = { 0,2,0,10,22,0,0,0,0,0,0,0,0,0,0,0,0 };
  47. const static size_t FRAME_DATA_WORDS[] = { 0,1,0,5,11,0,0,0,0,0,0,0,0,0,0,0,0 };
  48. int evrc_encoder_max_encode(void* c,size_t speech_samples) {
  49. EvrcEncoderContext* context = (EvrcEncoderContext*)c;
  50. if( context == NULL ) return 0;
  51. if( FRAME_SIZE[ context->max_rate ] == 0 ) return 0;
  52. return (speech_samples / SPEECH_FRAME_SAMPLES ) * FRAME_SIZE[ context->max_rate ];
  53. }
  54. static int evrc_encoder_encode_frame(EvrcEncoderContext* context,int16_t* speech,uint8_t* bits) {
  55. int k;
  56. uint8_t rate = 0;
  57. int16_t* bits_16 = NULL;
  58. int16_t bits_frame[22];
  59. if( speech == 0 || bits == 0 )
  60. return 0;
  61. bqiir(speech);
  62. if( context->noise_suppression ) {
  63. noise_suprs(speech);
  64. noise_suprs(speech + SPEECH_FRAME_SAMPLES/2);
  65. }
  66. else
  67. {
  68. for (k=0; k<SPEECH_FRAME_SAMPLES; k++)
  69. {
  70. speech[k] = shift_r(speech[k],-1);
  71. }
  72. }
  73. context->beta = pre_encode(speech, context->R);
  74. rate = (uint8_t)select_rate(context->R,context->max_rate,context->min_rate,context->beta);
  75. *bits = rate;
  76. bits += 1;
  77. *bits = FRAME_DATA_SIZE[rate];
  78. bits += 1;
  79. bits_16 = (int16_t*)bits;
  80. encode(rate,(int16_t*)bits_frame);
  81. post_encode();
  82. for(k=0;k<FRAME_DATA_WORDS[rate];++k){
  83. bits_16[k] = SWAP_16(bits_frame[k]);
  84. }
  85. return FRAME_SIZE[rate];
  86. }
  87. int evrc_encoder_encode_to_stream(void* c,int16_t* speech,size_t speech_samples,uint8_t* bits,size_t bits_max_bytes) {
  88. EvrcEncoderContext* context = (EvrcEncoderContext*)c;
  89. size_t encode_samples = 0;
  90. size_t output_bytes = 0;
  91. size_t bytes = 0;
  92. if( c == 0 || speech == 0 || speech_samples < SPEECH_FRAME_SAMPLES || bits == 0 || bits_max_bytes <= 0)
  93. return 0;
  94. while( encode_samples + SPEECH_FRAME_SAMPLES <= speech_samples &&
  95. output_bytes + BITSTREAM_FRAME_MAX_BYTES <= bits_max_bytes ){
  96. bytes = evrc_encoder_encode_frame(context,speech,bits);
  97. output_bytes += bytes;
  98. encode_samples += SPEECH_FRAME_SAMPLES;
  99. speech += SPEECH_FRAME_SAMPLES;
  100. bits += bytes;
  101. }
  102. return output_bytes;
  103. }
  104. void* evrc_decoder_init() {
  105. InitDecoder();
  106. return NULL;
  107. }
  108. void evrc_decoder_uninit(void* c) {
  109. }
  110. int evrc_get_stream_frame_count(const uint8_t* bits,size_t bits_bytes) {
  111. const uint8_t* end = bits + bits_bytes;
  112. size_t frame = 0;
  113. size_t size = 0;
  114. if( bits == NULL || bits_bytes <= 0 )
  115. return 0;
  116. while( bits < end ) {
  117. if( (*bits) > 0x04 )
  118. return 0;
  119. size = FRAME_SIZE[ (*bits) & 0x0F ];
  120. if( size == 0 ) {
  121. return 0;
  122. }
  123. ++frame;
  124. bits += size;
  125. }
  126. return frame;
  127. }
  128. int evrc_decoder_stream_max_sample(const uint8_t* bits,size_t bits_bytes) {
  129. return evrc_get_stream_frame_count(bits,bits_bytes) * SPEECH_FRAME_SAMPLES ;
  130. }
  131. static int evrc_decoder_decode_frame_raw(void* c, const uint8_t* bits,size_t bits_bytes,uint8_t rate,int16_t* speech) {
  132. const int16_t* bits_16 = (int16_t*)bits;
  133. size_t i = 0;
  134. int16_t bits_frame[11];
  135. if( rate > 0x4 || FRAME_DATA_SIZE[rate] == 0 || FRAME_DATA_SIZE[rate] > bits_bytes ) return 0;
  136. memset(bits_frame,0,sizeof(int16_t)*11);
  137. for(i=0;i< FRAME_DATA_WORDS[rate];++i) {
  138. bits_frame[i] = SWAP_16(bits_16[i]);
  139. }
  140. decode((int16_t*)bits_frame, rate, /*post_filter*/ 0,speech);
  141. return FRAME_SIZE[rate];
  142. }
  143. int evrc_decoder_decode_stream_frame(void* c, const uint8_t* bits,size_t bits_bytes,int16_t* speech) {
  144. int16_t rate = bits[0];
  145. return evrc_decoder_decode_frame_raw(c,bits + sizeof(int16_t),bits_bytes - sizeof(int16_t),rate,speech);
  146. }
  147. int evrc_decoder_stream_frame_bytes(const uint8_t* bits) {
  148. return FRAME_SIZE[ (*bits) & 0xF ];
  149. }
  150. int evrc_decoder_is_silence_stream_frame(const uint8_t* bits) {
  151. return FRAME_DATA_SIZE[ (*bits) & 0xF ] <= 2;
  152. }
  153. #define PACKET_TO_EVRC_RATE(rate) ((rate) + 1)
  154. #define EVRC_TO_PACKET_RATE(rate) ((rate) - 1)
  155. int evrc_packet_to_stream(const uint8_t* packet,size_t packet_size,uint8_t* stream,size_t stream_size) {
  156. Evrc8KPacketParser parser;
  157. size_t writedbytes = 0;
  158. const uint8_t* stream_end = stream + stream_size;
  159. if( NULL == packet || packet_size == 0 || NULL == stream || stream_size == 0 )
  160. return 0;
  161. if( ! evrc8k_packet_init_parser(packet,packet_size,&parser) ) {
  162. return 0;
  163. }
  164. do {
  165. if( stream + parser.frame_size + 2 >= stream_end )
  166. return 0;
  167. stream[0] = PACKET_TO_EVRC_RATE(parser.rate);
  168. stream[1] = FRAME_DATA_SIZE[ PACKET_TO_EVRC_RATE(parser.rate) & 0xF ];
  169. memcpy(&(stream[2]),parser.frame,parser.frame_size);
  170. stream += (2 + parser.frame_size);
  171. writedbytes += (2 + parser.frame_size);
  172. } while( evrc8k_packet_next_frame(&parser) );
  173. return writedbytes;
  174. }
  175. int evrc_stream_to_packet(const uint8_t* stream,size_t frame_count,uint8_t* packet,size_t packet_size) {
  176. Evrc8KPacketAppender appender;
  177. uint8_t rate = 0;
  178. if( NULL == packet || packet_size == 0 || NULL == stream || frame_count == 0 )
  179. return 0;
  180. if( ! evrc8k_packet_init_appender(packet,packet_size,frame_count,&appender) ) {
  181. return 0;
  182. }
  183. do {
  184. rate = stream[0];
  185. if( ! evrc8k_packet_append_frame_raw(&appender,EVRC_TO_PACKET_RATE(rate),&(stream[2])) )
  186. return 0;
  187. stream += FRAME_SIZE[ rate & 0xF ];
  188. --frame_count;
  189. } while( frame_count );
  190. return appender.packet_size;
  191. }
  192. int evrc_encoder_encode_to_packet(void* c,int16_t* speech,size_t speech_samples,uint8_t* packet,size_t packet_max_bytes) {
  193. uint8_t evrc_frame[32];
  194. size_t encode_samples = 0;
  195. int bytes = 0;
  196. uint8_t rate = 0;
  197. Evrc8KPacketAppender appender;
  198. EvrcEncoderContext* context = (EvrcEncoderContext*)c;
  199. if( NULL == c || NULL == speech || NULL == packet )
  200. return EVRC_CODEC_ERROR;
  201. speech_samples = (speech_samples / SPEECH_FRAME_SAMPLES ) * SPEECH_FRAME_SAMPLES ;
  202. if( speech_samples == 0 )
  203. return 0;
  204. if( ! evrc8k_packet_init_appender(packet,packet_max_bytes,speech_samples / SPEECH_FRAME_SAMPLES,&appender) ) {
  205. return EVRC_CODEC_BUFFER_SMALL;
  206. }
  207. while( encode_samples + SPEECH_FRAME_SAMPLES <= speech_samples ) {
  208. bytes = evrc_encoder_encode_frame(context,speech,evrc_frame);
  209. if( bytes <= 0 )
  210. return EVRC_CODEC_ERROR;
  211. encode_samples += SPEECH_FRAME_SAMPLES;
  212. speech += SPEECH_FRAME_SAMPLES;
  213. rate = evrc_frame[0];
  214. if( ! evrc8k_packet_append_frame_raw(&appender,EVRC_TO_PACKET_RATE(rate),&(evrc_frame[2])) )
  215. return EVRC_CODEC_ERROR;
  216. }
  217. return appender.packet_size;
  218. }
  219. int evrc_decoder_decode_from_stream(void* c,const uint8_t* stream,size_t stream_bytes,int16_t* speech,size_t speech_max_samples) {
  220. uint8_t rate = 0;
  221. size_t frame_size = 0;
  222. size_t frame_count = 0;
  223. const uint8_t* s_end = stream + stream_bytes;
  224. const int16_t* o_end = speech + speech_max_samples;
  225. if( NULL == stream || stream_bytes == 0 || NULL == speech || speech_max_samples < SPEECH_FRAME_SAMPLES )
  226. return 0;
  227. while( stream + 4 <= s_end && speech + SPEECH_FRAME_SAMPLES <= o_end) {
  228. rate = *stream;
  229. frame_size = FRAME_DATA_SIZE[ rate & 0xF ];
  230. if( stream + 2 + frame_size > s_end )
  231. return EVRC_CODEC_ERROR;
  232. if( evrc_decoder_decode_frame_raw(c,stream + 2,frame_size,rate,speech) <= 0 )
  233. return EVRC_CODEC_ERROR;
  234. stream += (2 + frame_size);
  235. speech += SPEECH_FRAME_SAMPLES ;
  236. ++frame_count;
  237. }
  238. return frame_count * 320;
  239. }
  240. int evrc_decoder_decode_from_packet(void* c,const uint8_t* packet,size_t packet_bytes,int16_t* speech,size_t speech_max_samples) {
  241. Evrc8KPacketParser parser;
  242. uint8_t rate = 0;
  243. size_t frame_size = 0;
  244. size_t writedbytes = 0;
  245. if( NULL == c || NULL == packet || packet_bytes == 0 || NULL == speech )
  246. return EVRC_CODEC_ERROR;
  247. if( ! evrc8k_packet_init_parser(packet,packet_bytes,&parser) ) {
  248. return 0;
  249. }
  250. if( speech_max_samples < parser.frame_count * SPEECH_FRAME_SAMPLES )
  251. return EVRC_CODEC_BUFFER_SMALL;
  252. do {
  253. rate = PACKET_TO_EVRC_RATE(parser.rate);
  254. frame_size = FRAME_DATA_SIZE[ rate & 0xF ];
  255. if( evrc_decoder_decode_frame_raw(c,parser.frame,parser.frame_size,rate,speech) <= 0 )
  256. return EVRC_CODEC_ERROR;
  257. speech += SPEECH_FRAME_SAMPLES ;
  258. } while( evrc8k_packet_next_frame(&parser) );
  259. return parser.frame_count * SPEECH_FRAME_SAMPLES * sizeof(int16_t);
  260. }
  261. /*
  262. int evrc_decoder_decode(void* c,uint8_t* bits,size_t bits_bytes,int16_t* speech,size_t speech_max_words) {
  263. size_t decode_bytes = 0;
  264. size_t output_samples = 0;
  265. size_t bytes = 0;
  266. if( bits == 0 || bits_bytes < BITSTREAM_FRAME_MAX_BYTES || speech == 0 || speech_max_words < SPEECH_FRAME_SAMPLES )
  267. return 0;
  268. while( decode_bytes + BITSTREAM_FRAME_MIN_BYTES <= bits_bytes &&
  269. output_samples + SPEECH_FRAME_SAMPLES <= speech_max_words) {
  270. bytes = evrc_decoder_decode_frame(c, bits,bits_bytes - decode_bytes,speech);
  271. if( bytes <= 0 ) break;
  272. decode_bytes += bytes;
  273. output_samples += SPEECH_FRAME_SAMPLES;
  274. bits += bytes;
  275. speech += SPEECH_FRAME_SAMPLES;
  276. }
  277. return output_samples;
  278. }
  279. */