summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libspeex/speex.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libspeex/speex.c')
-rw-r--r--lib/rbcodec/codecs/libspeex/speex.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libspeex/speex.c b/lib/rbcodec/codecs/libspeex/speex.c
new file mode 100644
index 0000000000..0dcfb3f7f6
--- /dev/null
+++ b/lib/rbcodec/codecs/libspeex/speex.c
@@ -0,0 +1,252 @@
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-speex.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#ifndef SPEEX_DISABLE_ENCODER
50void *speex_encoder_init(const SpeexMode *mode)
51{
52 return mode->enc_init(mode);
53}
54#endif
55
56void *speex_decoder_init(const SpeexMode *mode)
57{
58 return mode->dec_init(mode);
59}
60
61#ifndef SPEEX_DISABLE_ENCODER
62void speex_encoder_destroy(void *state)
63{
64 (*((SpeexMode**)state))->enc_destroy(state);
65}
66#endif
67
68void speex_decoder_destroy(void *state)
69{
70 (*((SpeexMode**)state))->dec_destroy(state);
71}
72
73#ifndef SPEEX_DISABLE_ENCODER
74int speex_encode_native(void *state, spx_word16_t *in, SpeexBits *bits)
75{
76 return (*((SpeexMode**)state))->enc(state, in, bits);
77}
78#endif
79
80int speex_decode_native(void *state, SpeexBits *bits, spx_word16_t *out)
81{
82 return (*((SpeexMode**)state))->dec(state, bits, out);
83}
84
85
86
87#ifdef FIXED_POINT
88
89#ifndef SPEEX_DISABLE_ENCODER
90#ifndef DISABLE_FLOAT_API
91int speex_encode(void *state, float *in, SpeexBits *bits)
92{
93 int i;
94 spx_int32_t N;
95 spx_int16_t short_in[MAX_IN_SAMPLES];
96 speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
97 for (i=0;i<N;i++)
98 {
99 if (in[i]>32767.f)
100 short_in[i] = 32767;
101 else if (in[i]<-32768.f)
102 short_in[i] = -32768;
103 else
104 short_in[i] = (spx_int16_t)floor(.5+in[i]);
105 }
106 return (*((SpeexMode**)state))->enc(state, short_in, bits);
107}
108#endif /* #ifndef DISABLE_FLOAT_API */
109
110int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits)
111{
112 SpeexMode *mode;
113 mode = *(SpeexMode**)state;
114 return (mode)->enc(state, in, bits);
115}
116#endif /* SPEEX_DISABLE_ENCODER */
117
118#ifndef DISABLE_FLOAT_API
119int speex_decode(void *state, SpeexBits *bits, float *out)
120{
121 int i, ret;
122 spx_int32_t N;
123 spx_int16_t short_out[MAX_IN_SAMPLES];
124 speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
125 ret = (*((SpeexMode**)state))->dec(state, bits, short_out);
126 for (i=0;i<N;i++)
127 out[i] = short_out[i];
128 return ret;
129}
130#endif /* #ifndef DISABLE_FLOAT_API */
131
132int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out)
133{
134 SpeexMode *mode = *(SpeexMode**)state;
135 return (mode)->dec(state, bits, out);
136}
137
138#else
139
140int speex_encode(void *state, float *in, SpeexBits *bits)
141{
142 return (*((SpeexMode**)state))->enc(state, in, bits);
143}
144
145int speex_encode_int(void *state, spx_int16_t *in, SpeexBits *bits)
146{
147 int i;
148 spx_int32_t N;
149 float float_in[MAX_IN_SAMPLES];
150 speex_encoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
151 for (i=0;i<N;i++)
152 float_in[i] = in[i];
153 return (*((SpeexMode**)state))->enc(state, float_in, bits);
154}
155
156int speex_decode(void *state, SpeexBits *bits, float *out)
157{
158 return (*((SpeexMode**)state))->dec(state, bits, out);
159}
160
161int speex_decode_int(void *state, SpeexBits *bits, spx_int16_t *out)
162{
163 int i;
164 spx_int32_t N;
165 float float_out[MAX_IN_SAMPLES];
166 int ret;
167 speex_decoder_ctl(state, SPEEX_GET_FRAME_SIZE, &N);
168 ret = (*((SpeexMode**)state))->dec(state, bits, float_out);
169 for (i=0;i<N;i++)
170 {
171 if (float_out[i]>32767.f)
172 out[i] = 32767;
173 else if (float_out[i]<-32768.f)
174 out[i] = -32768;
175 else
176 out[i] = (spx_int16_t)floor(.5+float_out[i]);
177 }
178 return ret;
179}
180#endif
181
182#ifndef SPEEX_DISABLE_ENCODER
183int speex_encoder_ctl(void *state, int request, void *ptr)
184{
185 return (*((SpeexMode**)state))->enc_ctl(state, request, ptr);
186}
187#endif
188
189int speex_decoder_ctl(void *state, int request, void *ptr)
190{
191 return (*((SpeexMode**)state))->dec_ctl(state, request, ptr);
192}
193
194int nb_mode_query(const void *mode, int request, void *ptr)
195{
196 const SpeexNBMode *m = (const SpeexNBMode*)mode;
197
198 switch (request)
199 {
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;
214 }
215 return 0;
216}
217
218
219
220int speex_lib_ctl(int request, void *ptr)
221{
222 switch (request)
223 {
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;
250 }
251 return 0;
252}