Debian Codegraph @

libspeex/testenc_uwb.c

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