/src/speex/libspeex/speex.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2002 Jean-Marc Valin |
2 | | File: speex.c |
3 | | |
4 | | Basic Speex functions |
5 | | |
6 | | Redistribution and use in source and binary forms, with or without |
7 | | modification, are permitted provided that the following conditions |
8 | | are met: |
9 | | |
10 | | - Redistributions of source code must retain the above copyright |
11 | | notice, this list of conditions and the following disclaimer. |
12 | | |
13 | | - Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in the |
15 | | documentation and/or other materials provided with the distribution. |
16 | | |
17 | | - Neither the name of the Xiph.org Foundation nor the names of its |
18 | | contributors may be used to endorse or promote products derived from |
19 | | this software without specific prior written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
25 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
26 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
27 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
28 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
29 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
30 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
31 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | |
33 | | */ |
34 | | |
35 | | #ifdef HAVE_CONFIG_H |
36 | | #include "config.h" |
37 | | #endif |
38 | | |
39 | | #include "modes.h" |
40 | | #include <math.h> |
41 | | #include "os_support.h" |
42 | | |
43 | | #ifndef NULL |
44 | | #define NULL 0 |
45 | | #endif |
46 | | |
47 | | #define MAX_IN_SAMPLES 640 |
48 | | |
49 | | |
50 | | |
51 | | EXPORT void *speex_encoder_init(const SpeexMode *mode) |
52 | 3.54k | { |
53 | 3.54k | return mode->enc_init(mode); |
54 | 3.54k | } |
55 | | |
56 | | EXPORT void *speex_decoder_init(const SpeexMode *mode) |
57 | 0 | { |
58 | 0 | return mode->dec_init(mode); |
59 | 0 | } |
60 | | |
61 | | EXPORT void speex_encoder_destroy(void *state) |
62 | 3.54k | { |
63 | 3.54k | (*((SpeexMode**)state))->enc_destroy(state); |
64 | 3.54k | } |
65 | | |
66 | | EXPORT void speex_decoder_destroy(void *state) |
67 | 0 | { |
68 | 0 | (*((SpeexMode**)state))->dec_destroy(state); |
69 | 0 | } |
70 | | |
71 | | |
72 | | |
73 | | int speex_encode_native(void *state, spx_word16_t *in, SpeexBits *bits) |
74 | 5.43k | { |
75 | 5.43k | return (*((SpeexMode**)state))->enc(state, in, bits); |
76 | 5.43k | } |
77 | | |
78 | | int speex_decode_native(void *state, SpeexBits *bits, spx_word16_t *out) |
79 | 0 | { |
80 | 0 | return (*((SpeexMode**)state))->dec(state, bits, out); |
81 | 0 | } |
82 | | |
83 | | |
84 | | |
85 | | #ifdef FIXED_POINT |
86 | | |
87 | | #ifndef DISABLE_FLOAT_API |
88 | | EXPORT int speex_encode(void *state, float *in, SpeexBits *bits) |
89 | 0 | { |
90 | 0 | int i; |
91 | 0 | spx_int32_t N; |
92 | 0 | spx_int16_t short_in[MAX_IN_SAMPLES]; |
93 | 0 | speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N); |
94 | 0 | for (i=0;i<N;i++) |
95 | 0 | { |
96 | 0 | if (in[i]>32767.f) |
97 | 0 | short_in[i] = 32767; |
98 | 0 | else if (in[i]<-32768.f) |
99 | 0 | short_in[i] = -32768; |
100 | 0 | else |
101 | 0 | short_in[i] = (spx_int16_t)floor(.5+in[i]); |
102 | 0 | } |
103 | 0 | return (*((SpeexMode**)state))->enc(state, short_in, bits); |
104 | 0 | } |
105 | | #endif /* #ifndef DISABLE_FLOAT_API */ |
106 | | |
107 | | EXPORT int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits) |
108 | 15.7k | { |
109 | 15.7k | SpeexMode *mode; |
110 | 15.7k | mode = *(SpeexMode**)state; |
111 | 15.7k | return (mode)->enc(state, in, bits); |
112 | 15.7k | } |
113 | | |
114 | | #ifndef DISABLE_FLOAT_API |
115 | | EXPORT int speex_decode(void *state, SpeexBits *bits, float *out) |
116 | 0 | { |
117 | 0 | int i, ret; |
118 | 0 | spx_int32_t N; |
119 | 0 | spx_int16_t short_out[MAX_IN_SAMPLES]; |
120 | 0 | speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N); |
121 | 0 | ret = (*((SpeexMode**)state))->dec(state, bits, short_out); |
122 | 0 | for (i=0;i<N;i++) |
123 | 0 | out[i] = short_out[i]; |
124 | 0 | return ret; |
125 | 0 | } |
126 | | #endif /* #ifndef DISABLE_FLOAT_API */ |
127 | | |
128 | | EXPORT int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out) |
129 | 0 | { |
130 | 0 | SpeexMode *mode = *(SpeexMode**)state; |
131 | 0 | return (mode)->dec(state, bits, out); |
132 | 0 | } |
133 | | |
134 | | #else |
135 | | |
136 | | EXPORT int speex_encode(void *state, float *in, SpeexBits *bits) |
137 | | { |
138 | | return (*((SpeexMode**)state))->enc(state, in, bits); |
139 | | } |
140 | | |
141 | | EXPORT int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits) |
142 | | { |
143 | | int i; |
144 | | spx_int32_t N; |
145 | | float float_in[MAX_IN_SAMPLES]; |
146 | | speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N); |
147 | | for (i=0;i<N;i++) |
148 | | float_in[i] = in[i]; |
149 | | return (*((SpeexMode**)state))->enc(state, float_in, bits); |
150 | | } |
151 | | |
152 | | EXPORT int speex_decode(void *state, SpeexBits *bits, float *out) |
153 | | { |
154 | | return (*((SpeexMode**)state))->dec(state, bits, out); |
155 | | } |
156 | | |
157 | | EXPORT int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out) |
158 | | { |
159 | | int i; |
160 | | spx_int32_t N; |
161 | | float float_out[MAX_IN_SAMPLES]; |
162 | | int ret; |
163 | | speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N); |
164 | | ret = (*((SpeexMode**)state))->dec(state, bits, float_out); |
165 | | if (ret == 0) |
166 | | { |
167 | | for (i=0;i<N;i++) |
168 | | { |
169 | | if (float_out[i]>32767.f) |
170 | | out[i] = 32767; |
171 | | else if (float_out[i]<-32768.f) |
172 | | out[i] = -32768; |
173 | | else |
174 | | out[i] = (spx_int16_t)floor(.5+float_out[i]); |
175 | | } |
176 | | } |
177 | | return ret; |
178 | | } |
179 | | #endif |
180 | | |
181 | | |
182 | | |
183 | | EXPORT int speex_encoder_ctl(void *state, int request, void *ptr) |
184 | 230k | { |
185 | 230k | return (*((SpeexMode**)state))->enc_ctl(state, request, ptr); |
186 | 230k | } |
187 | | |
188 | | EXPORT int speex_decoder_ctl(void *state, int request, void *ptr) |
189 | 0 | { |
190 | 0 | return (*((SpeexMode**)state))->dec_ctl(state, request, ptr); |
191 | 0 | } |
192 | | |
193 | | |
194 | | |
195 | | int nb_mode_query(const void *mode, int request, void *ptr) |
196 | 0 | { |
197 | 0 | const SpeexNBMode *m = (const SpeexNBMode*)mode; |
198 | |
|
199 | 0 | switch (request) |
200 | 0 | { |
201 | 0 | case SPEEX_MODE_FRAME_SIZE: |
202 | 0 | *((int*)ptr)=m->frameSize; |
203 | 0 | break; |
204 | 0 | case SPEEX_SUBMODE_BITS_PER_FRAME: |
205 | 0 | if (*((int*)ptr)==0) |
206 | 0 | *((int*)ptr) = NB_SUBMODE_BITS+1; |
207 | 0 | else if (m->submodes[*((int*)ptr)]==NULL) |
208 | 0 | *((int*)ptr) = -1; |
209 | 0 | else |
210 | 0 | *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame; |
211 | 0 | break; |
212 | 0 | default: |
213 | 0 | speex_warning_int("Unknown nb_mode_query request: ", request); |
214 | 0 | return -1; |
215 | 0 | } |
216 | 0 | return 0; |
217 | 0 | } |
218 | | |
219 | | |
220 | | |
221 | | EXPORT int speex_lib_ctl(int request, void *ptr) |
222 | 0 | { |
223 | 0 | switch (request) |
224 | 0 | { |
225 | 0 | case SPEEX_LIB_GET_MAJOR_VERSION: |
226 | 0 | *((int*)ptr) = SPEEX_MAJOR_VERSION; |
227 | 0 | break; |
228 | 0 | case SPEEX_LIB_GET_MINOR_VERSION: |
229 | 0 | *((int*)ptr) = SPEEX_MINOR_VERSION; |
230 | 0 | break; |
231 | 0 | case SPEEX_LIB_GET_MICRO_VERSION: |
232 | 0 | *((int*)ptr) = SPEEX_MICRO_VERSION; |
233 | 0 | break; |
234 | 0 | case SPEEX_LIB_GET_EXTRA_VERSION: |
235 | 0 | *((const char**)ptr) = SPEEX_EXTRA_VERSION; |
236 | 0 | break; |
237 | 0 | case SPEEX_LIB_GET_VERSION_STRING: |
238 | 0 | *((const char**)ptr) = SPEEX_VERSION; |
239 | 0 | break; |
240 | | /*case SPEEX_LIB_SET_ALLOC_FUNC: |
241 | | break; |
242 | | case SPEEX_LIB_GET_ALLOC_FUNC: |
243 | | break; |
244 | | case SPEEX_LIB_SET_FREE_FUNC: |
245 | | break; |
246 | | case SPEEX_LIB_GET_FREE_FUNC: |
247 | | break;*/ |
248 | 0 | default: |
249 | 0 | speex_warning_int("Unknown wb_mode_query request: ", request); |
250 | 0 | return -1; |
251 | 0 | } |
252 | 0 | return 0; |
253 | 0 | } |