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
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.
41 #include <speex/speex.h>
44 #include <speex/speex_callbacks.h>
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}
68 extern long long spx_mips
;
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 */
87 char *outFile
, *bitsFile
;
88 FILE *fout
, *fbits
=NULL
;
89 #if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
92 short in_short
[FRAME_SIZE
];
94 short out_short
[FRAME_SIZE
];
99 float sigpow
,errpow
,snr
, seg_snr
=0;
108 unsigned long bitCount
=0;
109 int skip_group_delay
;
110 SpeexCallback callback
;
118 spxGlobalHeapPtr
= spxHeap
;
119 spxGlobalHeapEnd
= spxHeap
+ sizeof(spxHeap
);
121 spxGlobalScratchPtr
= spxScratch
;
122 spxGlobalScratchEnd
= spxScratch
+ sizeof(spxScratch
);
124 st
= speex_encoder_init(&speex_nb_mode
);
126 spxGlobalScratchPtr
= spxScratch
; /* Reuse scratch for decoder */
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
;
138 speex_decoder_ctl(dec
, SPEEX_SET_HANDLER
, &callback
);
141 speex_decoder_ctl(dec
, SPEEX_SET_ENH
, &tmp
);
143 speex_encoder_ctl(st
, SPEEX_SET_VBR
, &tmp
);
145 speex_encoder_ctl(st
, SPEEX_SET_QUALITY
, &tmp
);
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 */
154 bitsFile
= "c:\\speextrunktest\\samples\\malebitsin.dat";
155 fbits
= fopen(bitsFile
, "rb");
157 bitsFile
= "c:\\speextrunktest\\samples\\malebits.dat";
158 fbits
= fopen(bitsFile
, "wb");
160 #if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
161 inFile
= "c:\\speextrunktest\\samples\\male.snd";
162 fin
= fopen(inFile
, "rb");
164 outFile
= "c:\\speextrunktest\\samples\\maleout.snd";
165 fout
= fopen(outFile
, "wb+");
167 speex_bits_init(&bits
);
171 fread(in_short
, sizeof(short), FRAME_SIZE
, fin
);
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 */
186 fread(cbits
, 1, TESTENC_BYTES_PER_FRAME
, fbits
);
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
;
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;
202 fprintf (stderr
, "Bits so far: %lu \n", bitCount
);
205 fprintf (stderr
, "Total encoded size: %lu bits\n", bitCount
);
206 speex_encoder_destroy(st
);
207 speex_decoder_destroy(dec
);
213 while ( FRAME_SIZE
== fread(in_short
, sizeof(short), FRAME_SIZE
, fin
)
215 FRAME_SIZE
== fread(out_short
, sizeof(short), FRAME_SIZE
,fout
) )
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));
228 snr
= 10 * log10( sigpow
/ errpow
);
229 seg_snr
/= snr_frames
;
230 fprintf(stderr
,"SNR = %f\nsegmental SNR = %f\n",snr
, seg_snr
);
233 printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips
/snr_frames
));
235 #endif /* CHECK_RESULT */
236 #if !defined(DECODE_ONLY) || defined(CHECK_RESULT)