~ubuntu-branches/ubuntu/quantal/linphone/quantal

« back to all changes in this revision

Viewing changes to speex/libspeex/testenc.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2004-06-30 13:58:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040630135816-wwx75gdlodkqbabb
Tags: upstream-0.12.2
ImportĀ upstreamĀ versionĀ 0.12.2

Show diffs side-by-side

added added

removed removed

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