testenc_uwb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <speex/speex.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <speex/speex_callbacks.h>
  8. #ifdef FIXED_DEBUG
  9. extern long long spx_mips;
  10. #endif
  11. #define FRAME_SIZE 640
  12. #include <math.h>
  13. int main(int argc, char **argv)
  14. {
  15. char *inFile, *outFile, *bitsFile;
  16. FILE *fin, *fout, *fbits=NULL;
  17. short in_short[FRAME_SIZE];
  18. short out_short[FRAME_SIZE];
  19. float in_float[FRAME_SIZE];
  20. float sigpow,errpow,snr, seg_snr=0;
  21. int snr_frames = 0;
  22. char cbits[200];
  23. int nbBits;
  24. int i;
  25. void *st;
  26. void *dec;
  27. SpeexBits bits;
  28. spx_int32_t tmp;
  29. int bitCount=0;
  30. spx_int32_t skip_group_delay;
  31. SpeexCallback callback;
  32. sigpow = 0;
  33. errpow = 0;
  34. st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_UWB));
  35. dec = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_UWB));
  36. callback.callback_id = SPEEX_INBAND_CHAR;
  37. callback.func = speex_std_char_handler;
  38. callback.data = stderr;
  39. speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  40. callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
  41. callback.func = speex_std_mode_request_handler;
  42. callback.data = st;
  43. speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  44. tmp=0;
  45. speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
  46. tmp=0;
  47. speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
  48. tmp=7;
  49. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
  50. tmp=1;
  51. speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
  52. speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay);
  53. speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp);
  54. skip_group_delay += tmp;
  55. if (argc != 4 && argc != 3)
  56. {
  57. fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
  58. exit(1);
  59. }
  60. inFile = argv[1];
  61. fin = fopen(inFile, "rb");
  62. outFile = argv[2];
  63. fout = fopen(outFile, "wb+");
  64. if (argc==4)
  65. {
  66. bitsFile = argv[3];
  67. fbits = fopen(bitsFile, "wb");
  68. }
  69. speex_bits_init(&bits);
  70. while (!feof(fin))
  71. {
  72. fread(in_short, sizeof(short), FRAME_SIZE, fin);
  73. if (feof(fin))
  74. break;
  75. for (i=0;i<FRAME_SIZE;i++)
  76. in_float[i]=in_short[i];
  77. speex_bits_reset(&bits);
  78. speex_encode_int(st, in_short, &bits);
  79. nbBits = speex_bits_write(&bits, cbits, 200);
  80. bitCount+=bits.nbBits;
  81. if (argc==4)
  82. fwrite(cbits, 1, nbBits, fbits);
  83. speex_bits_rewind(&bits);
  84. speex_decode_int(dec, &bits, out_short);
  85. speex_bits_reset(&bits);
  86. fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
  87. skip_group_delay = 0;
  88. }
  89. fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
  90. speex_encoder_destroy(st);
  91. speex_decoder_destroy(dec);
  92. rewind(fin);
  93. rewind(fout);
  94. while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
  95. &&
  96. FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) )
  97. {
  98. float s=0, e=0;
  99. for (i=0;i<FRAME_SIZE;++i) {
  100. s += (float)in_short[i] * in_short[i];
  101. e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
  102. }
  103. seg_snr += 10*log10((s+1)/(e+1));
  104. sigpow += s;
  105. errpow += e;
  106. snr_frames++;
  107. }
  108. fclose(fin);
  109. fclose(fout);
  110. snr = 10 * log10( sigpow / errpow );
  111. seg_snr /= snr_frames;
  112. fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
  113. #ifdef FIXED_DEBUG
  114. printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
  115. #endif
  116. return 1;
  117. }