testenc_uwb.c 3.3 KB

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