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