testenc_wb.c 3.5 KB

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