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

« back to all changes in this revision

Viewing changes to ti/testenc-TI-C64x.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2005-12-07 23:22:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051207232221-nme7vf9m182p7dpe
Tags: 1.1.11.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005 Psi Systems, Inc.
 
2
   Author:  Jean-Marc Valin 
 
3
   File: testenc-TI-C64x.c
 
4
   Encoder/Decoder Loop Main file for TI TMS320C64xx processor
 
5
   for use with TI Code Composer (TM) DSP development tools.
 
6
   Modified from speexlib/testenc.c
 
7
 
 
8
 
 
9
   Redistribution and use in source and binary forms, with or without
 
10
   modification, are permitted provided that the following conditions
 
11
   are met:
 
12
   
 
13
   - Redistributions of source code must retain the above copyright
 
14
   notice, this list of conditions and the following disclaimer.
 
15
   
 
16
   - Redistributions in binary form must reproduce the above copyright
 
17
   notice, this list of conditions and the following disclaimer in the
 
18
   documentation and/or other materials provided with the distribution.
 
19
   
 
20
   - Neither the name of the Xiph.org Foundation nor the names of its
 
21
   contributors may be used to endorse or promote products derived from
 
22
   this software without specific prior written permission.
 
23
   
 
24
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
25
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
26
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
27
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 
28
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
29
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
30
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
31
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
32
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
33
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
34
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
35
*/
 
36
 
 
37
#ifdef HAVE_CONFIG_H
 
38
#include "config.h"
 
39
#endif
 
40
 
 
41
#include <speex/speex.h>
 
42
#include <stdio.h>
 
43
#include <stdlib.h>
 
44
#include <speex/speex_callbacks.h>
 
45
 
 
46
#ifdef FIXED_DEBUG
 
47
extern long long spx_mips;
 
48
#endif
 
49
 
 
50
#ifdef MANUAL_ALLOC
 
51
#pragma DATA_SECTION(spxHeap, ".myheap"); 
 
52
static char spxHeap[SPEEX_PERSIST_STACK_SIZE];
 
53
 
 
54
#pragma DATA_SECTION(spxScratch, ".myheap"); 
 
55
static char spxScratch[SPEEX_SCRATCH_STACK_SIZE];
 
56
 
 
57
char *spxGlobalHeapPtr, *spxGlobalHeapEnd;
 
58
char *spxGlobalScratchPtr, *spxGlobalScratchEnd;
 
59
#endif    /* MANUAL_ALLOC */
 
60
 
 
61
#include <math.h>
 
62
void main()
 
63
{
 
64
   char *outFile, *bitsFile;
 
65
   FILE *fout, *fbits=NULL;
 
66
#ifndef DECODE_ONLY
 
67
   char *inFile;
 
68
   FILE *fin;
 
69
#endif
 
70
#if 0
 
71
   char *dbgoutFile;
 
72
   FILE *fdbgout;
 
73
#endif
 
74
   short out_short[FRAME_SIZE];
 
75
#ifndef DECODE_ONLY
 
76
   short in_short[FRAME_SIZE];
 
77
   float sigpow,errpow,snr, seg_snr=0;
 
78
   int snr_frames = 0;
 
79
   int nbBits;
 
80
   int i;
 
81
#endif
 
82
   char cbits[200];
 
83
   void *st;
 
84
   void *dec;
 
85
   SpeexBits bits;
 
86
   int tmp;
 
87
   int bitCount=0;
 
88
   int skip_group_delay;
 
89
   SpeexCallback callback;
 
90
 
 
91
#ifndef DECODE_ONLY
 
92
   sigpow = 0;
 
93
   errpow = 0;
 
94
#endif
 
95
 
 
96
#ifdef MANUAL_ALLOC
 
97
        spxGlobalHeapPtr = spxHeap;
 
98
        spxGlobalHeapEnd = spxHeap + sizeof(spxHeap);
 
99
 
 
100
        spxGlobalScratchPtr = spxScratch;
 
101
        spxGlobalScratchEnd = spxScratch + sizeof(spxScratch);
 
102
#endif
 
103
   st = speex_encoder_init(&speex_nb_mode);
 
104
#ifdef MANUAL_ALLOC
 
105
        spxGlobalScratchPtr = spxScratch;               /* Reuse scratch for decoder */
 
106
#endif
 
107
   dec = speex_decoder_init(&speex_nb_mode);
 
108
 
 
109
   callback.callback_id = SPEEX_INBAND_CHAR;
 
110
   callback.func = speex_std_char_handler;
 
111
   callback.data = stderr;
 
112
   speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
 
113
 
 
114
   callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
 
115
   callback.func = speex_std_mode_request_handler;
 
116
   callback.data = st;
 
117
   speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
 
118
 
 
119
   tmp=0;
 
120
   speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
 
121
   tmp=0;
 
122
   speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
 
123
   tmp=4;
 
124
   speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
 
125
   tmp=1;
 
126
   speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
 
127
 
 
128
   speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
 
129
   fprintf (stderr, "frame size: %d\n", tmp);
 
130
   skip_group_delay = tmp / 2;
 
131
 
 
132
#ifdef DECODE_ONLY
 
133
   bitsFile = "e:\\speextrunktest\\samples\\malebitsin54.dat";
 
134
   fbits = fopen(bitsFile, "rb");
 
135
#else
 
136
   bitsFile = "e:\\speextrunktest\\samples\\malebits.dat";
 
137
   fbits = fopen(bitsFile, "wb");
 
138
#endif
 
139
   inFile = "e:\\speextrunktest\\samples\\male.snd";
 
140
   fin = fopen(inFile, "rb");
 
141
   outFile = "e:\\speextrunktest\\samples\\maleout.snd";
 
142
   fout = fopen(outFile, "wb+");
 
143
#if 0
 
144
   dbgoutFile = "e:\\speextrunktest\\samples\\maledbgout.snd";
 
145
   fdbgout = fopen(dbgoutFile, "wb+");
 
146
#endif
 
147
 
 
148
   speex_bits_init(&bits);
 
149
#ifndef DECODE_ONLY
 
150
   while (!feof(fin))
 
151
   {
 
152
      fread(in_short, sizeof(short), FRAME_SIZE, fin);
 
153
#if 0
 
154
      fwrite(in_short, sizeof(short), FRAME_SIZE, fdbgout);
 
155
#endif
 
156
      if (feof(fin))
 
157
         break;
 
158
      speex_bits_reset(&bits);
 
159
 
 
160
      speex_encode_int(st, in_short, &bits);
 
161
      nbBits = speex_bits_write(&bits, cbits, 200);
 
162
      bitCount+=bits.nbBits;
 
163
 
 
164
      fwrite(cbits, 1, nbBits, fbits);
 
165
      speex_bits_rewind(&bits);
 
166
 
 
167
#else /* DECODE_ONLY */
 
168
   while (!feof(fbits))
 
169
   {
 
170
      fread(cbits, 1, 20, fbits);
 
171
 
 
172
      if (feof(fbits))
 
173
         break;
 
174
 
 
175
      speex_bits_read_from(&bits, cbits, 20);
 
176
      bitCount+=160;
 
177
#endif
 
178
      
 
179
      speex_decode_int(dec, &bits, out_short);
 
180
      speex_bits_reset(&bits);
 
181
 
 
182
      fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
 
183
      skip_group_delay = 0;
 
184
#if 1
 
185
   fprintf (stderr, "Bits so far: %d \n", bitCount);
 
186
#endif
 
187
   }
 
188
   fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
 
189
   speex_encoder_destroy(st);
 
190
   speex_decoder_destroy(dec);
 
191
 
 
192
#ifndef DECODE_ONLY
 
193
   rewind(fin);
 
194
   rewind(fout);
 
195
 
 
196
   while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin) 
 
197
           &&
 
198
           FRAME_SIZE ==  fread(out_short, sizeof(short), FRAME_SIZE,fout) )
 
199
   {
 
200
        float s=0, e=0;
 
201
        for (i=0;i<FRAME_SIZE;++i) {
 
202
            s += (float)in_short[i] * in_short[i];
 
203
            e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
 
204
        }
 
205
        seg_snr += 10*log10((s+160)/(e+160));
 
206
        sigpow += s;
 
207
        errpow += e;
 
208
        snr_frames++;
 
209
   }
 
210
   fclose(fin);
 
211
#endif
 
212
   fclose(fout);
 
213
   fclose(fbits);
 
214
 
 
215
#ifndef DECODE_ONLY
 
216
   snr = 10 * log10( sigpow / errpow );
 
217
   seg_snr /= snr_frames;
 
218
   fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
 
219
 
 
220
#ifdef FIXED_DEBUG
 
221
   printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
 
222
#endif
 
223
#endif   
 
224
}