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 _MSC_VER
34 #pragma warning(disable:4244)
35 #endif
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include "misc.h"
45
46 #ifdef USER_MISC
47 #include "user_misc.h"
48 #endif
49
50 #ifdef BFIN_ASM
51 #include "misc_bfin.h"
52 #endif
53
54 #ifndef RELEASE
print_vec(float * vec,int len,char * name)55 void print_vec(float *vec, int len, char *name)
56 {
57 int i;
58 printf ("%s ", name);
59 for (i=0;i<len;i++)
60 printf (" %f", vec[i]);
61 printf ("\n");
62 }
63 #endif
64
65 #ifdef FIXED_DEBUG
66 long long spx_mips=0;
67 #endif
68
69
be_int(spx_uint32_t i)70 spx_uint32_t be_int(spx_uint32_t i)
71 {
72 spx_uint32_t ret=i;
73 #ifndef WORDS_BIGENDIAN
74 ret = i>>24;
75 ret += (i>>8)&0x0000ff00;
76 ret += (i<<8)&0x00ff0000;
77 ret += (i<<24);
78 #endif
79 return ret;
80 }
81
le_int(spx_uint32_t i)82 spx_uint32_t le_int(spx_uint32_t i)
83 {
84 spx_uint32_t ret=i;
85 #ifdef WORDS_BIGENDIAN
86 ret = i>>24;
87 ret += (i>>8)&0x0000ff00;
88 ret += (i<<8)&0x00ff0000;
89 ret += (i<<24);
90 #endif
91 return ret;
92 }
93
94 #if BYTES_PER_CHAR == 2
speex_memcpy_bytes(char * dst,char * src,int nbytes)95 void speex_memcpy_bytes(char *dst, char *src, int nbytes)
96 {
97 int i;
98 int nchars = nbytes/BYTES_PER_CHAR;
99 for (i=0;i<nchars;i++)
100 dst[i]=src[i];
101 if (nbytes & 1) {
102 /* copy in the last byte */
103 int last_i = nchars;
104 char last_dst_char = dst[last_i];
105 char last_src_char = src[last_i];
106 last_dst_char &= 0xff00;
107 last_dst_char |= (last_src_char & 0x00ff);
108 dst[last_i] = last_dst_char;
109 }
110 }
speex_memset_bytes(char * dst,char c,int nbytes)111 void speex_memset_bytes(char *dst, char c, int nbytes)
112 {
113 int i;
114 spx_int16_t cc = ((c << 8) | c);
115 int nchars = nbytes/BYTES_PER_CHAR;
116 for (i=0;i<nchars;i++)
117 dst[i]=cc;
118 if (nbytes & 1) {
119 /* copy in the last byte */
120 int last_i = nchars;
121 char last_dst_char = dst[last_i];
122 last_dst_char &= 0xff00;
123 last_dst_char |= (c & 0x00ff);
124 dst[last_i] = last_dst_char;
125 }
126 }
127 #else
speex_memcpy_bytes(char * dst,char * src,int nbytes)128 void speex_memcpy_bytes(char *dst, char *src, int nbytes)
129 {
130 memcpy(dst, src, nbytes);
131 }
speex_memset_bytes(char * dst,char src,int nbytes)132 void speex_memset_bytes(char *dst, char src, int nbytes)
133 {
134 memset(dst, src, nbytes);
135 }
136 #endif
137
138 #ifndef OVERRIDE_SPEEX_ALLOC
speex_alloc(int size)139 void *speex_alloc (int size)
140 {
141 return calloc(size,1);
142 }
143 #endif
144
145 #ifndef OVERRIDE_SPEEX_ALLOC_SCRATCH
speex_alloc_scratch(int size)146 void *speex_alloc_scratch (int size)
147 {
148 return calloc(size,1);
149 }
150 #endif
151
152 #ifndef OVERRIDE_SPEEX_REALLOC
speex_realloc(void * ptr,int size)153 void *speex_realloc (void *ptr, int size)
154 {
155 return realloc(ptr, size);
156 }
157 #endif
158
159 #ifndef OVERRIDE_SPEEX_FREE
speex_free(void * ptr)160 void speex_free (void *ptr)
161 {
162 free(ptr);
163 }
164 #endif
165
166 #ifndef OVERRIDE_SPEEX_FREE_SCRATCH
speex_free_scratch(void * ptr)167 void speex_free_scratch (void *ptr)
168 {
169 free(ptr);
170 }
171 #endif
172
173 #ifndef OVERRIDE_SPEEX_MOVE
speex_move(void * dest,void * src,int n)174 void *speex_move (void *dest, void *src, int n)
175 {
176 return memmove(dest,src,n);
177 }
178 #endif
179
180 #ifndef OVERRIDE_SPEEX_ERROR
speex_error(const char * str)181 void speex_error(const char *str)
182 {
183 fprintf (stderr, "Fatal error: %s\n", str);
184 exit(1);
185 }
186 #endif
187
188 #ifndef OVERRIDE_SPEEX_WARNING
speex_warning(const char * str)189 void speex_warning(const char *str)
190 {
191 fprintf (stderr, "warning: %s\n", str);
192 }
193 #endif
194
195 #ifndef OVERRIDE_SPEEX_WARNING_INT
speex_warning_int(const char * str,int val)196 void speex_warning_int(const char *str, int val)
197 {
198 fprintf (stderr, "warning: %s %d\n", str, val);
199 }
200 #endif
201
202 #ifdef FIXED_POINT
speex_rand(spx_word16_t std,spx_int32_t * seed)203 spx_word32_t speex_rand(spx_word16_t std, spx_int32_t *seed)
204 {
205 spx_word32_t res;
206 *seed = 1664525 * *seed + 1013904223;
207 res = MULT16_16(EXTRACT16(SHR32(*seed,16)),std);
208 return SUB32(res, SHR(res, 3));
209 }
210 #else
speex_rand(spx_word16_t std,spx_int32_t * seed)211 spx_word16_t speex_rand(spx_word16_t std, spx_int32_t *seed)
212 {
213 const unsigned int jflone = 0x3f800000;
214 const unsigned int jflmsk = 0x007fffff;
215 union {int i; float f;} ran;
216 *seed = 1664525 * *seed + 1013904223;
217 ran.i = jflone | (jflmsk & *seed);
218 ran.f -= 1.5;
219 return 3.4642*std*ran.f;
220 }
221 #endif
222
speex_rand_vec(float std,spx_sig_t * data,int len)223 void speex_rand_vec(float std, spx_sig_t *data, int len)
224 {
225 int i;
226 for (i=0;i<len;i++)
227 data[i]+=SIG_SCALING*3*std*((((float)rand())/RAND_MAX)-.5);
228 }
229
230
231 /*float speex_rand(float std)
232 {
233 return 3*std*((((float)rand())/RAND_MAX)-.5);
234 }*/
235
236 #ifndef OVERRIDE_SPEEX_PUTC
_speex_putc(int ch,void * file)237 void _speex_putc(int ch, void *file)
238 {
239 FILE *f = (FILE *)file;
240 fprintf(f, "%c", ch);
241 }
242 #endif
243
244 #ifdef _MSC_VER
245 #pragma warning(default:4244)
246 #endif