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