testenc-TI-C5x.c 8.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/* Copyright (C) 2005 Psi Systems, Inc.
   File: testenc-TI-C5x.c
   Encoder/Decoder Loop Main file for TI C54xx and C55xx processors
   for use with TI Code Composer (TM) DSP development tools.
   Modified from speexlib/testenc.c


   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* Modified from speexlib/testenc.c for Code Composer simulator */

#include <speex/speex.h>
#include <stdio.h>
#include <stdlib.h>
#include <speex/speex_callbacks.h>
#include <..\libspeex\arch.h>

#undef DECODE_ONLY
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#define CHECK_RESULT  /* Compares original file with encoded/decoder file */
#define TESTENC_BYTES_PER_FRAME 20  /* 8kbps */
#define TESTENC_QUALITY 4 /* 8kbps */
//#define TESTENC_BYTES_PER_FRAME 28  /* 11kbps */
//#define TESTENC_QUALITY 5	      /* 11 kbps */

/* For narrowband, QUALITY maps to these bit rates (see modes.c, manual.pdf) 
 *   {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
 * 0 -> 2150
 * 1 -> 3950
 * 2 -> 5950
 * 3 -> 8000
 * 4 -> 8000
 * 5 -> 11000
 * 6 -> 11000
 * 7 -> 15000
 * 8 -> 15000
 * 9 -> 18200
 *10 -> 26400  */
68 69 70 71 72 73 74

#ifdef FIXED_DEBUG
extern long long spx_mips;
#endif
#include <math.h>

#ifdef MANUAL_ALLOC
75 76
/* Take all Speex space from this private heap */ 
/* This is useful for multichannel applications */
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#pragma DATA_SECTION(spxHeap, ".myheap"); 
static char spxHeap[SPEEX_PERSIST_STACK_SIZE];

#pragma DATA_SECTION(spxScratch, ".myheap"); 
static char spxScratch[SPEEX_SCRATCH_STACK_SIZE];

char *spxGlobalHeapPtr, *spxGlobalHeapEnd;
char *spxGlobalScratchPtr, *spxGlobalScratchEnd;
#endif

void byte2word(short* pInBuf, short* pOutBuf, int nWords)
{
   short *pIn, *pOut, sNext;
   int i;
   
   pIn = pInBuf;
   pOut = pOutBuf;
   for(i=0;i<nWords;i++)
   {
      sNext = *pIn++;			// Get low byte
      *pOut++ = (sNext & 0x00ff) | (*pIn++ << 8);	// insert high byte
   }
}
   
void word2byte(short* pInBuf, short* pOutBuf, int nWords)
{
   short *pIn, *pOut;
   int i;
   
   pIn = pInBuf;
   pOut = pOutBuf;
   for(i=0;i<nWords;i++)
   {
      *pOut++ = *pIn & 0x00ff;	// Get low byte
      *pOut++ = (short) ((unsigned short) *pIn++ >> 8);
   }
}
   
void main()
{
   char *outFile, *bitsFile;
   FILE *fout, *fbits=NULL;
119
#if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
120 121
   char *inFile;
   FILE *fin;
122
   short in_short[FRAME_SIZE];
123 124 125 126
#endif
   short out_short[FRAME_SIZE];
   short inout_byte[2*FRAME_SIZE];
#ifndef DECODE_ONLY
127 128 129
   int nbChars;
#endif
#ifdef CHECK_RESULT
130 131 132 133
   float sigpow,errpow,snr, seg_snr=0;
   int snr_frames = 0;
   int i;
#endif
134
   char cbits[TESTENC_BYTES_PER_FRAME/2 + 2];  /* temp store for encoded data */
135 136 137
   void *st;
   void *dec;
   SpeexBits bits;
jm's avatar
jm committed
138
   spx_int32_t tmp;
139
   unsigned long bitCount=0;
jm's avatar
jm committed
140
   spx_int32_t skip_group_delay;
141 142 143 144 145 146 147 148 149
   SpeexCallback callback;

   /* C54xx defaults to max wait states, even for parts like C5416 with 
      larger internal memory.  Need to force the wait state register to zero */

#ifdef CONFIG_TI_C54X
   asm("	STM	#0,SWWSR");
#endif

150
#ifdef CHECK_RESULT
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
   sigpow = 0;
   errpow = 0;
#endif

#ifdef MANUAL_ALLOC
	spxGlobalHeapPtr = spxHeap;
	spxGlobalHeapEnd = spxHeap + sizeof(spxHeap);

	spxGlobalScratchPtr = spxScratch;
	spxGlobalScratchEnd = spxScratch + sizeof(spxScratch);
#endif
   st = speex_encoder_init(&speex_nb_mode);
#ifdef MANUAL_ALLOC
	spxGlobalScratchPtr = spxScratch;		/* Reuse scratch for decoder */
#endif
   dec = speex_decoder_init(&speex_nb_mode);

   callback.callback_id = SPEEX_INBAND_CHAR;
   callback.func = speex_std_char_handler;
   callback.data = stderr;
   speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);

   callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
   callback.func = speex_std_mode_request_handler;
   callback.data = st;
   speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);

   tmp=0;
   speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
   tmp=0;
   speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
182
   tmp=TESTENC_QUALITY;
183
   speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
184
   tmp=1;  /* Lowest */
185 186
   speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);

187 188 189 190 191 192 193 194 195 196 197
#ifdef DISABLE_HIGHPASS
   /* Turn this off if you want to measure SNR (on by default) */
   tmp=0;
   speex_encoder_ctl(st, SPEEX_SET_HIGHPASS, &tmp);
   speex_decoder_ctl(dec, SPEEX_SET_HIGHPASS, &tmp);
#endif

   speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay);
   speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp);
   skip_group_delay += tmp;
   fprintf (stderr, "decoder lookahead = %ld\n", skip_group_delay);
198 199

#ifdef DECODE_ONLY
200
   bitsFile = "c:\\speextrunktest\\samples\\malebitsin.dat";
201 202
   fbits = fopen(bitsFile, "rb");
#else
203
   bitsFile = "c:\\speextrunktest\\samples\\malebits5x.dat";
204 205
   fbits = fopen(bitsFile, "wb");
#endif
206
#if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
207 208
   inFile = "c:\\speextrunktest\\samples\\male.snd";
   fin = fopen(inFile, "rb");
209
#endif
210
   outFile = "c:\\speextrunktest\\samples\\maleout5x.snd";
211 212 213 214 215 216 217 218 219 220 221 222 223 224
   fout = fopen(outFile, "wb+");
 
   speex_bits_init(&bits);
#ifndef DECODE_ONLY
   while (!feof(fin))
   {
      fread(inout_byte, 2, FRAME_SIZE, fin);
      byte2word(inout_byte, in_short, FRAME_SIZE);  /* C5x has 16-bit char */

      if (feof(fin))
         break;
      speex_bits_reset(&bits);

      speex_encode_int(st, in_short, &bits);
225 226
      nbChars = speex_bits_write(&bits, cbits, 
                         sizeof(cbits)*BYTES_PER_CHAR) /BYTES_PER_CHAR;
227 228 229 230 231 232 233 234 235
      bitCount+=bits.nbBits;

      word2byte((short *) cbits, inout_byte, nbChars);
      fwrite(inout_byte, 2, nbChars, fbits);
      speex_bits_rewind(&bits);

#else /* DECODE_ONLY */
   while (!feof(fbits))
   {
236
      fread(inout_byte, 1, TESTENC_BYTES_PER_FRAME, fbits);
237 238 239 240

      if (feof(fbits))
         break;

241 242
      byte2word(inout_byte, (short *)cbits, TESTENC_BYTES_PER_FRAME/2);
      speex_bits_read_from(&bits, cbits, TESTENC_BYTES_PER_FRAME);
243 244 245 246 247 248 249 250 251
      bitCount+=160;
#endif
      speex_decode_int(dec, &bits, out_short);
      speex_bits_reset(&bits);

      word2byte(&out_short[skip_group_delay], inout_byte, FRAME_SIZE-skip_group_delay);
      fwrite(inout_byte, 2, FRAME_SIZE-skip_group_delay, fout);
      skip_group_delay = 0;
#if 1
252
   fprintf (stderr, "Bits so far: %lu \n", bitCount);
253 254
#endif
   }
255
   fprintf (stderr, "Total encoded size: %lu bits\n", bitCount);
256 257 258
   speex_encoder_destroy(st);
   speex_decoder_destroy(dec);

259
#ifdef CHECK_RESULT
260 261 262
   rewind(fin);
   rewind(fout);

263
   while ( FRAME_SIZE == fread(inout_byte, 2, FRAME_SIZE, fin)) 
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
   {
	float s=0, e=0;

    byte2word(inout_byte, in_short, FRAME_SIZE);
    fread(inout_byte, 2, FRAME_SIZE, fout);
    byte2word(inout_byte, out_short, FRAME_SIZE);

        for (i=0;i<FRAME_SIZE;++i) {
            s += (float)in_short[i] * in_short[i];
            e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
        }
	seg_snr += 10*log10((s+160)/(e+160));
	sigpow += s;
	errpow += e;
	snr_frames++;
   }

   snr = 10 * log10( sigpow / errpow );
   seg_snr /= snr_frames;
   fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);

#ifdef FIXED_DEBUG
   printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
#endif
288 289 290 291 292 293
#endif /* CHECK_RESULT */
#if !defined(DECODE_ONLY) || defined(CHECK_RESULT)
   fclose(fin);
#endif
   fclose(fout);
   fclose(fbits);
294
}