~ubuntu-branches/ubuntu/hardy/speex/hardy-security

« back to all changes in this revision

Viewing changes to libspeex/testenc_uwb.c

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2005-02-26 22:33:22 UTC
  • Revision ID: james.westby@ubuntu.com-20050226223322-vc6sdoshrqjxfh9c
Tags: upstream-1.1.6
ImportĀ upstreamĀ versionĀ 1.1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
   int tmp;
 
32
   int bitCount=0;
 
33
   int skip_group_delay;
 
34
   SpeexCallback callback;
 
35
 
 
36
   sigpow = 0;
 
37
   errpow = 0;
 
38
 
 
39
   st = speex_encoder_init(&speex_uwb_mode);
 
40
   dec = speex_decoder_init(&speex_uwb_mode);
 
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_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
 
62
   fprintf (stderr, "frame size: %d\n", tmp);
 
63
   skip_group_delay = 509;
 
64
 
 
65
   if (argc != 4 && argc != 3)
 
66
   {
 
67
      fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
 
68
      exit(1);
 
69
   }
 
70
   inFile = argv[1];
 
71
   fin = fopen(inFile, "r");
 
72
   outFile = argv[2];
 
73
   fout = fopen(outFile, "w+");
 
74
   if (argc==4)
 
75
   {
 
76
      bitsFile = argv[3];
 
77
      fbits = fopen(bitsFile, "w");
 
78
   }
 
79
   speex_bits_init(&bits);
 
80
   while (!feof(fin))
 
81
   {
 
82
      fread(in_short, sizeof(short), FRAME_SIZE, fin);
 
83
      if (feof(fin))
 
84
         break;
 
85
      for (i=0;i<FRAME_SIZE;i++)
 
86
         in_float[i]=in_short[i];
 
87
      speex_bits_reset(&bits);
 
88
 
 
89
      speex_encode_int(st, in_short, &bits);
 
90
      nbBits = speex_bits_write(&bits, cbits, 200);
 
91
      bitCount+=bits.nbBits;
 
92
 
 
93
      if (argc==4)
 
94
         fwrite(cbits, 1, nbBits, fbits);
 
95
      speex_bits_rewind(&bits);
 
96
 
 
97
      speex_decode_int(dec, &bits, out_short);
 
98
      speex_bits_reset(&bits);
 
99
 
 
100
      fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
 
101
      skip_group_delay = 0;
 
102
   }
 
103
   fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
 
104
   speex_encoder_destroy(st);
 
105
   speex_decoder_destroy(dec);
 
106
 
 
107
   rewind(fin);
 
108
   rewind(fout);
 
109
 
 
110
   while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin) 
 
111
           &&
 
112
           FRAME_SIZE ==  fread(out_short, sizeof(short), FRAME_SIZE,fout) )
 
113
   {
 
114
        float s=0, e=0;
 
115
        for (i=0;i<FRAME_SIZE;++i) {
 
116
            s += (float)in_short[i] * in_short[i];
 
117
            e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
 
118
        }
 
119
        seg_snr += 10*log10((s+1)/(e+1));
 
120
        sigpow += s;
 
121
        errpow += e;
 
122
        snr_frames++;
 
123
   }
 
124
   fclose(fin);
 
125
   fclose(fout);
 
126
 
 
127
   snr = 10 * log10( sigpow / errpow );
 
128
   seg_snr /= snr_frames;
 
129
   fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
 
130
 
 
131
#ifdef FIXED_DEBUG
 
132
   printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
 
133
#endif
 
134
   
 
135
   return 1;
 
136
}