Swap code/variable declaration to be pre-C99 compliant.
[xiph/unicode.git] / speex / ti / testenc-TI-C64x.c
blobb523da2b932c8004aaa85ddc1af1f6aec713a6a9
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
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
13 - Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
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.
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.
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.
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
41 #include <speex/speex.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <speex/speex_callbacks.h>
46 #undef DECODE_ONLY
47 #define CHECK_RESULT /* Compares original file with encoded/decoder file */
48 #define TESTENC_BYTES_PER_FRAME 20 /* 8kbps */
49 #define TESTENC_QUALITY 4 /* 8kbps */
50 //#define TESTENC_BYTES_PER_FRAME 28 /* 11kbps */
51 //#define TESTENC_QUALITY 5 /* 11 kbps */
53 /* For narrowband, QUALITY maps to these bit rates (see modes.c, manual.pdf)
54 * {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
55 * 0 -> 2150
56 * 1 -> 3950
57 * 2 -> 5950
58 * 3 -> 8000
59 * 4 -> 8000
60 * 5 -> 11000
61 * 6 -> 11000
62 * 7 -> 15000
63 * 8 -> 15000
64 * 9 -> 18200
65 *10 -> 26400 */
67 #ifdef FIXED_DEBUG
68 extern long long spx_mips;
69 #endif
71 #ifdef MANUAL_ALLOC
72 /* Take all Speex space from this private heap */
73 /* This is useful for multichannel applications */
74 #pragma DATA_SECTION(spxHeap, ".myheap");
75 static char spxHeap[SPEEX_PERSIST_STACK_SIZE];
77 #pragma DATA_SECTION(spxScratch, ".myheap");
78 static char spxScratch[SPEEX_SCRATCH_STACK_SIZE];
80 char *spxGlobalHeapPtr, *spxGlobalHeapEnd;
81 char *spxGlobalScratchPtr, *spxGlobalScratchEnd;
82 #endif /* MANUAL_ALLOC */
84 #include <math.h>
85 void main()
87 char *outFile, *bitsFile;
88 FILE *fout, *fbits=NULL;
89 #if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
90 char *inFile;
91 FILE *fin;
92 short in_short[FRAME_SIZE];
93 #endif
94 short out_short[FRAME_SIZE];
95 #ifndef DECODE_ONLY
96 int nbBits;
97 #endif
98 #ifdef CHECK_RESULT
99 float sigpow,errpow,snr, seg_snr=0;
100 int snr_frames = 0;
101 int i;
102 #endif
103 char cbits[200];
104 void *st;
105 void *dec;
106 SpeexBits bits;
107 int tmp;
108 unsigned long bitCount=0;
109 int skip_group_delay;
110 SpeexCallback callback;
112 #ifdef CHECK_RESULT
113 sigpow = 0;
114 errpow = 0;
115 #endif
117 #ifdef MANUAL_ALLOC
118 spxGlobalHeapPtr = spxHeap;
119 spxGlobalHeapEnd = spxHeap + sizeof(spxHeap);
121 spxGlobalScratchPtr = spxScratch;
122 spxGlobalScratchEnd = spxScratch + sizeof(spxScratch);
123 #endif
124 st = speex_encoder_init(&speex_nb_mode);
125 #ifdef MANUAL_ALLOC
126 spxGlobalScratchPtr = spxScratch; /* Reuse scratch for decoder */
127 #endif
128 dec = speex_decoder_init(&speex_nb_mode);
130 callback.callback_id = SPEEX_INBAND_CHAR;
131 callback.func = speex_std_char_handler;
132 callback.data = stderr;
133 speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
135 callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
136 callback.func = speex_std_mode_request_handler;
137 callback.data = st;
138 speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
140 tmp=0;
141 speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
142 tmp=0;
143 speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
144 tmp=TESTENC_QUALITY;
145 speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
146 tmp=1; /* Lowest */
147 speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
149 speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
150 fprintf (stderr, "frame size: %d\n", tmp);
151 skip_group_delay = tmp / 4; /* 5ms algorithmic delay */
153 #ifdef DECODE_ONLY
154 bitsFile = "c:\\speextrunktest\\samples\\malebitsin.dat";
155 fbits = fopen(bitsFile, "rb");
156 #else
157 bitsFile = "c:\\speextrunktest\\samples\\malebits.dat";
158 fbits = fopen(bitsFile, "wb");
159 #endif
160 #if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
161 inFile = "c:\\speextrunktest\\samples\\male.snd";
162 fin = fopen(inFile, "rb");
163 #endif
164 outFile = "c:\\speextrunktest\\samples\\maleout.snd";
165 fout = fopen(outFile, "wb+");
167 speex_bits_init(&bits);
168 #ifndef DECODE_ONLY
169 while (!feof(fin))
171 fread(in_short, sizeof(short), FRAME_SIZE, fin);
172 if (feof(fin))
173 break;
174 speex_bits_reset(&bits);
176 speex_encode_int(st, in_short, &bits);
177 nbBits = speex_bits_write(&bits, cbits, 200);
178 bitCount+=bits.nbBits;
180 fwrite(cbits, 1, nbBits, fbits);
181 speex_bits_rewind(&bits);
183 #else /* DECODE_ONLY */
184 while (!feof(fbits))
186 fread(cbits, 1, TESTENC_BYTES_PER_FRAME, fbits);
188 if (feof(fbits))
189 break;
191 speex_bits_read_from(&bits, cbits, TESTENC_BYTES_PER_FRAME);
192 // bitCount+=160; /* only correct for 8kbps, but just for the printf */
193 bitCount+=bits.nbBits;
194 #endif
196 speex_decode_int(dec, &bits, out_short);
197 speex_bits_reset(&bits);
199 fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
200 skip_group_delay = 0;
201 #if 1
202 fprintf (stderr, "Bits so far: %lu \n", bitCount);
203 #endif
205 fprintf (stderr, "Total encoded size: %lu bits\n", bitCount);
206 speex_encoder_destroy(st);
207 speex_decoder_destroy(dec);
209 #ifdef CHECK_RESULT
210 rewind(fin);
211 rewind(fout);
213 while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
215 FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) )
217 float s=0, e=0;
218 for (i=0;i<FRAME_SIZE;++i) {
219 s += (float)in_short[i] * in_short[i];
220 e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
222 seg_snr += 10*log10((s+160)/(e+160));
223 sigpow += s;
224 errpow += e;
225 snr_frames++;
228 snr = 10 * log10( sigpow / errpow );
229 seg_snr /= snr_frames;
230 fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
232 #ifdef FIXED_DEBUG
233 printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
234 #endif
235 #endif /* CHECK_RESULT */
236 #if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
237 fclose(fin);
238 #endif
239 fclose(fout);
240 fclose(fbits);