testenc_wb.c 3.5 KB

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