misc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (C) 2002-2005 Jean-Marc Valin
  2. File: misc.c
  3. Various utility routines for Speex
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include "misc.h"
  34. #ifdef USER_MISC
  35. #include "user_misc.h"
  36. #endif
  37. #ifdef BFIN_ASM
  38. #include "misc_bfin.h"
  39. #endif
  40. #ifndef RELEASE
  41. void print_vec(float *vec, int len, char *name)
  42. {
  43. int i;
  44. printf ("%s ", name);
  45. for (i=0;i<len;i++)
  46. printf (" %f", vec[i]);
  47. printf ("\n");
  48. }
  49. #endif
  50. #ifdef FIXED_DEBUG
  51. long long spx_mips=0;
  52. #endif
  53. spx_uint32_t be_int(spx_uint32_t i)
  54. {
  55. spx_uint32_t ret=i;
  56. #ifndef WORDS_BIGENDIAN
  57. ret = i>>24;
  58. ret += (i>>8)&0x0000ff00;
  59. ret += (i<<8)&0x00ff0000;
  60. ret += (i<<24);
  61. #endif
  62. return ret;
  63. }
  64. spx_uint32_t le_int(spx_uint32_t i)
  65. {
  66. spx_uint32_t ret=i;
  67. #ifdef WORDS_BIGENDIAN
  68. ret = i>>24;
  69. ret += (i>>8)&0x0000ff00;
  70. ret += (i<<8)&0x00ff0000;
  71. ret += (i<<24);
  72. #endif
  73. return ret;
  74. }
  75. #if BYTES_PER_CHAR == 2
  76. void speex_memcpy_bytes(char *dst, char *src, int nbytes)
  77. {
  78. int i;
  79. int nchars = nbytes/BYTES_PER_CHAR;
  80. for (i=0;i<nchars;i++)
  81. dst[i]=src[i];
  82. if (nbytes & 1) {
  83. /* copy in the last byte */
  84. int last_i = nchars;
  85. char last_dst_char = dst[last_i];
  86. char last_src_char = src[last_i];
  87. last_dst_char &= 0xff00;
  88. last_dst_char |= (last_src_char & 0x00ff);
  89. dst[last_i] = last_dst_char;
  90. }
  91. }
  92. void speex_memset_bytes(char *dst, char c, int nbytes)
  93. {
  94. int i;
  95. spx_int16_t cc = ((c << 8) | c);
  96. int nchars = nbytes/BYTES_PER_CHAR;
  97. for (i=0;i<nchars;i++)
  98. dst[i]=cc;
  99. if (nbytes & 1) {
  100. /* copy in the last byte */
  101. int last_i = nchars;
  102. char last_dst_char = dst[last_i];
  103. last_dst_char &= 0xff00;
  104. last_dst_char |= (c & 0x00ff);
  105. dst[last_i] = last_dst_char;
  106. }
  107. }
  108. #else
  109. void speex_memcpy_bytes(char *dst, char *src, int nbytes)
  110. {
  111. memcpy(dst, src, nbytes);
  112. }
  113. void speex_memset_bytes(char *dst, char src, int nbytes)
  114. {
  115. memset(dst, src, nbytes);
  116. }
  117. #endif
  118. #ifndef OVERRIDE_SPEEX_ALLOC
  119. void *speex_alloc (int size)
  120. {
  121. return calloc(size,1);
  122. }
  123. #endif
  124. #ifndef OVERRIDE_SPEEX_ALLOC_SCRATCH
  125. void *speex_alloc_scratch (int size)
  126. {
  127. return calloc(size,1);
  128. }
  129. #endif
  130. #ifndef OVERRIDE_SPEEX_REALLOC
  131. void *speex_realloc (void *ptr, int size)
  132. {
  133. return realloc(ptr, size);
  134. }
  135. #endif
  136. #ifndef OVERRIDE_SPEEX_FREE
  137. void speex_free (void *ptr)
  138. {
  139. free(ptr);
  140. }
  141. #endif
  142. #ifndef OVERRIDE_SPEEX_FREE_SCRATCH
  143. void speex_free_scratch (void *ptr)
  144. {
  145. free(ptr);
  146. }
  147. #endif
  148. #ifndef OVERRIDE_SPEEX_MOVE
  149. void *speex_move (void *dest, void *src, int n)
  150. {
  151. return memmove(dest,src,n);
  152. }
  153. #endif
  154. #ifndef OVERRIDE_SPEEX_ERROR
  155. void speex_error(const char *str)
  156. {
  157. fprintf (stderr, "Fatal error: %s\n", str);
  158. exit(1);
  159. }
  160. #endif
  161. #ifndef OVERRIDE_SPEEX_WARNING
  162. void speex_warning(const char *str)
  163. {
  164. fprintf (stderr, "warning: %s\n", str);
  165. }
  166. #endif
  167. #ifndef OVERRIDE_SPEEX_WARNING_INT
  168. void speex_warning_int(const char *str, int val)
  169. {
  170. fprintf (stderr, "warning: %s %d\n", str, val);
  171. }
  172. #endif
  173. #ifdef FIXED_POINT
  174. spx_word32_t speex_rand(spx_word16_t std, spx_int32_t *seed)
  175. {
  176. spx_word32_t res;
  177. *seed = 1664525 * *seed + 1013904223;
  178. res = MULT16_16(EXTRACT16(SHR32(*seed,16)),std);
  179. return SUB32(res, SHR(res, 3));
  180. }
  181. #else
  182. spx_word16_t speex_rand(spx_word16_t std, spx_int32_t *seed)
  183. {
  184. const unsigned int jflone = 0x3f800000;
  185. const unsigned int jflmsk = 0x007fffff;
  186. union {int i; float f;} ran;
  187. *seed = 1664525 * *seed + 1013904223;
  188. ran.i = jflone | (jflmsk & *seed);
  189. ran.f -= 1.5;
  190. return 3.4642*std*ran.f;
  191. }
  192. #endif
  193. void speex_rand_vec(float std, spx_sig_t *data, int len)
  194. {
  195. int i;
  196. for (i=0;i<len;i++)
  197. data[i]+=SIG_SCALING*3*std*((((float)rand())/RAND_MAX)-.5);
  198. }
  199. /*float speex_rand(float std)
  200. {
  201. return 3*std*((((float)rand())/RAND_MAX)-.5);
  202. }*/
  203. #ifndef OVERRIDE_SPEEX_PUTC
  204. void _speex_putc(int ch, void *file)
  205. {
  206. FILE *f = (FILE *)file;
  207. fprintf(f, "%c", ch);
  208. }
  209. #endif
粤ICP备19079148号