testenc-TI-C64x.c 7.5 KB

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