Build doom on clipv2 and clip+
[kugel-rb.git] / apps / codecs / libspeex / speex.c
bloba77653f21a7363addff91b01f53b51eb5816f5a6
1 /* Copyright (C) 2002 Jean-Marc Valin
2 File: speex.c
4 Basic Speex functions
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 - Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
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.
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.
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.
35 #ifdef HAVE_CONFIG_H
36 #include "config-speex.h"
37 #endif
39 #include "modes.h"
40 #include <math.h>
41 #include "os_support.h"
43 #ifndef NULL
44 #define NULL 0
45 #endif
47 #define MAX_IN_SAMPLES 640
51 void *speex_encoder_init(const SpeexMode *mode)
53 return mode->enc_init(mode);
56 void *speex_decoder_init(const SpeexMode *mode)
58 return mode->dec_init(mode);
61 void speex_encoder_destroy(void *state)
63 (*((SpeexMode**)state))->enc_destroy(state);
66 void speex_decoder_destroy(void *state)
68 (*((SpeexMode**)state))->dec_destroy(state);
73 int speex_encode_native(void *state, spx_word16_t *in, SpeexBits *bits)
75 return (*((SpeexMode**)state))->enc(state, in, bits);
78 int speex_decode_native(void *state, SpeexBits *bits, spx_word16_t *out)
80 return (*((SpeexMode**)state))->dec(state, bits, out);
85 #ifdef FIXED_POINT
87 #ifndef SPEEX_DISABLE_ENCODER
88 #ifndef DISABLE_FLOAT_API
89 int speex_encode(void *state, float *in, SpeexBits *bits)
91 int i;
92 spx_int32_t N;
93 spx_int16_t short_in[MAX_IN_SAMPLES];
94 speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
95 for (i=0;i<N;i++)
97 if (in[i]>32767.f)
98 short_in[i] = 32767;
99 else if (in[i]<-32768.f)
100 short_in[i] = -32768;
101 else
102 short_in[i] = (spx_int16_t)floor(.5+in[i]);
104 return (*((SpeexMode**)state))->enc(state, short_in, bits);
106 #endif /* #ifndef DISABLE_FLOAT_API */
108 int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits)
110 SpeexMode *mode;
111 mode = *(SpeexMode**)state;
112 return (mode)->enc(state, in, bits);
114 #endif /* SPEEX_DISABLE_ENCODER */
116 #ifndef DISABLE_FLOAT_API
117 int speex_decode(void *state, SpeexBits *bits, float *out)
119 int i, ret;
120 spx_int32_t N;
121 spx_int16_t short_out[MAX_IN_SAMPLES];
122 speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
123 ret = (*((SpeexMode**)state))->dec(state, bits, short_out);
124 for (i=0;i<N;i++)
125 out[i] = short_out[i];
126 return ret;
128 #endif /* #ifndef DISABLE_FLOAT_API */
130 int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out)
132 SpeexMode *mode = *(SpeexMode**)state;
133 return (mode)->dec(state, bits, out);
136 #else
138 int speex_encode(void *state, float *in, SpeexBits *bits)
140 return (*((SpeexMode**)state))->enc(state, in, bits);
143 int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits)
145 int i;
146 spx_int32_t N;
147 float float_in[MAX_IN_SAMPLES];
148 speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
149 for (i=0;i<N;i++)
150 float_in[i] = in[i];
151 return (*((SpeexMode**)state))->enc(state, float_in, bits);
154 int speex_decode(void *state, SpeexBits *bits, float *out)
156 return (*((SpeexMode**)state))->dec(state, bits, out);
159 int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out)
161 int i;
162 spx_int32_t N;
163 float float_out[MAX_IN_SAMPLES];
164 int ret;
165 speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
166 ret = (*((SpeexMode**)state))->dec(state, bits, float_out);
167 for (i=0;i<N;i++)
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]);
176 return ret;
178 #endif
182 int speex_encoder_ctl(void *state, int request, void *ptr)
184 return (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
187 int speex_decoder_ctl(void *state, int request, void *ptr)
189 return (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
194 int nb_mode_query(const void *mode, int request, void *ptr)
196 const SpeexNBMode *m = (const SpeexNBMode*)mode;
198 switch (request)
200 case SPEEX_MODE_FRAME_SIZE:
201 *((int*)ptr)=m->frameSize;
202 break;
203 case SPEEX_SUBMODE_BITS_PER_FRAME:
204 if (*((int*)ptr)==0)
205 *((int*)ptr) = NB_SUBMODE_BITS+1;
206 else if (m->submodes[*((int*)ptr)]==NULL)
207 *((int*)ptr) = -1;
208 else
209 *((int*)ptr) = m->submodes[*((int*)ptr)]->bits_per_frame;
210 break;
211 default:
212 speex_warning_int("Unknown nb_mode_query request: ", request);
213 return -1;
215 return 0;
220 int speex_lib_ctl(int request, void *ptr)
222 switch (request)
224 case SPEEX_LIB_GET_MAJOR_VERSION:
225 *((int*)ptr) = SPEEX_MAJOR_VERSION;
226 break;
227 case SPEEX_LIB_GET_MINOR_VERSION:
228 *((int*)ptr) = SPEEX_MINOR_VERSION;
229 break;
230 case SPEEX_LIB_GET_MICRO_VERSION:
231 *((int*)ptr) = SPEEX_MICRO_VERSION;
232 break;
233 case SPEEX_LIB_GET_EXTRA_VERSION:
234 *((const char**)ptr) = SPEEX_EXTRA_VERSION;
235 break;
236 case SPEEX_LIB_GET_VERSION_STRING:
237 *((const char**)ptr) = SPEEX_VERSION;
238 break;
239 /*case SPEEX_LIB_SET_ALLOC_FUNC:
240 break;
241 case SPEEX_LIB_GET_ALLOC_FUNC:
242 break;
243 case SPEEX_LIB_SET_FREE_FUNC:
244 break;
245 case SPEEX_LIB_GET_FREE_FUNC:
246 break;*/
247 default:
248 speex_warning_int("Unknown wb_mode_query request: ", request);
249 return -1;
251 return 0;