1 | //transcode_speex.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
5 | * |
---|
6 | * This file is part of libroardsp a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroardsp is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "libroardsp.h" |
---|
27 | |
---|
28 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
29 | |
---|
30 | #define _16BIT (16/8) |
---|
31 | #define _SIZE_LEN 2 |
---|
32 | |
---|
33 | #define _HAVE_CCFG(x) (self->codec_config != NULL && (self->codec_config->para_set & (x))) |
---|
34 | |
---|
35 | int roar_xcoder_speex_init (struct roar_xcoder * state) { |
---|
36 | struct roar_xcoder_speex * self = roar_mm_malloc(sizeof(struct roar_xcoder_speex)); |
---|
37 | struct roar_audio_info * info = &(state->info.pcm); |
---|
38 | int tmp; |
---|
39 | |
---|
40 | ROAR_DBG("roar_xcoder_speex_init(*): sizeof(*self) = %lu", (unsigned long)sizeof(*self)); |
---|
41 | |
---|
42 | if ( self == NULL ) |
---|
43 | return -1; |
---|
44 | |
---|
45 | // curruntly only 16 bit mode is supported |
---|
46 | if ( info->bits != 16 ) { |
---|
47 | roar_mm_free(self); |
---|
48 | return -1; |
---|
49 | } |
---|
50 | |
---|
51 | // only mono/stereo mode is supported |
---|
52 | switch (info->channels) { |
---|
53 | case 1: self->stereo = 0; break; |
---|
54 | case 2: self->stereo = 1; break; |
---|
55 | default: |
---|
56 | roar_mm_free(self); |
---|
57 | return -1; |
---|
58 | } |
---|
59 | |
---|
60 | memset(self, 0, sizeof(struct roar_xcoder_speex)); |
---|
61 | |
---|
62 | state->inst = self; |
---|
63 | |
---|
64 | self->mode = ROAR_SPEEX_MODE_UWB; |
---|
65 | |
---|
66 | if (state->encode) { |
---|
67 | self->codec_config = roar_libroar_config_codec_get(ROAR_CODEC_ROAR_SPEEX, 0); |
---|
68 | self->max_cc = ROAR_SPEEX_MAX_CC; |
---|
69 | |
---|
70 | ROAR_DBG("roar_xcoder_speex_init(*): self->codec_config=%p", self->codec_config); |
---|
71 | |
---|
72 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_MAX_CC) ) { |
---|
73 | self->max_cc = self->codec_config->max_cc; |
---|
74 | } |
---|
75 | |
---|
76 | if ( info->rate < 12000 ) { |
---|
77 | self->mode = ROAR_SPEEX_MODE_NB; // optimized for 8kHz |
---|
78 | } else if ( info->rate < 24000 ) { |
---|
79 | self->mode = ROAR_SPEEX_MODE_WB; // optimized for 16kHz |
---|
80 | } else { |
---|
81 | self->mode = ROAR_SPEEX_MODE_UWB; // optimized for 32kHz |
---|
82 | } |
---|
83 | |
---|
84 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_MODE) ) { |
---|
85 | // NB, WB and UWB mode numbers are the same for Speex and codec config. |
---|
86 | self->mode = self->codec_config->mode; |
---|
87 | } |
---|
88 | |
---|
89 | switch (self->mode) { |
---|
90 | case ROAR_SPEEX_MODE_NB: self->xcoder = speex_encoder_init(&speex_nb_mode); break; |
---|
91 | case ROAR_SPEEX_MODE_WB: self->xcoder = speex_encoder_init(&speex_wb_mode); break; |
---|
92 | case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_encoder_init(&speex_uwb_mode); break; |
---|
93 | default: return -1; |
---|
94 | } |
---|
95 | |
---|
96 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY) ) { |
---|
97 | tmp = self->codec_config->complexity / 256; |
---|
98 | } else { |
---|
99 | tmp = 8; |
---|
100 | } |
---|
101 | speex_encoder_ctl(self->xcoder, SPEEX_SET_COMPLEXITY, &tmp); |
---|
102 | |
---|
103 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_Q) ) { |
---|
104 | tmp = self->codec_config->q / 256; |
---|
105 | speex_encoder_ctl(self->xcoder, SPEEX_SET_QUALITY, &tmp); |
---|
106 | } |
---|
107 | |
---|
108 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_VBR) ) { |
---|
109 | tmp = self->codec_config->vbr ? 1 : 0; |
---|
110 | speex_encoder_ctl(self->xcoder, SPEEX_SET_VBR, &tmp); |
---|
111 | } |
---|
112 | |
---|
113 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_DTX) ) { |
---|
114 | tmp = self->codec_config->dtx ? 1 : 0; |
---|
115 | speex_encoder_ctl(self->xcoder, SPEEX_SET_DTX, &tmp); |
---|
116 | } |
---|
117 | |
---|
118 | tmp = info->rate; |
---|
119 | speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp); |
---|
120 | speex_encoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
121 | } else { |
---|
122 | self->xcoder = NULL; |
---|
123 | } |
---|
124 | |
---|
125 | speex_bits_init(&(self->bits)); |
---|
126 | |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | int roar_xcoder_speex_uninit (struct roar_xcoder * state) { |
---|
131 | struct roar_xcoder_speex * self = state->inst; |
---|
132 | |
---|
133 | if ( self->xcoder != NULL ) { |
---|
134 | if (state->encode) { |
---|
135 | speex_encoder_destroy(self->xcoder); |
---|
136 | } else { |
---|
137 | speex_decoder_destroy(self->xcoder); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | speex_bits_destroy(&(self->bits)); |
---|
142 | |
---|
143 | roar_mm_free(self); |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|
148 | int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) { |
---|
149 | struct roar_xcoder_speex * self = state->inst; |
---|
150 | |
---|
151 | if (!state->encode) |
---|
152 | if (state->stage != ROAR_XCODER_STAGE_OPENED) |
---|
153 | return -1; |
---|
154 | |
---|
155 | return _16BIT * self->frame_size * (self->stereo ? 2 : 1); |
---|
156 | } |
---|
157 | |
---|
158 | int roar_xcoder_speex_proc_header(struct roar_xcoder * state) { |
---|
159 | struct roar_xcoder_speex * self = state->inst; |
---|
160 | uint16_t tmp_net; |
---|
161 | int tmp; |
---|
162 | SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT; |
---|
163 | SpeexCallback callback; |
---|
164 | char magic[ROAR_SPEEX_MAGIC_LEN]; |
---|
165 | |
---|
166 | // we do allready open streams not considder an error. |
---|
167 | if ( state->stage == ROAR_XCODER_STAGE_OPENED ) |
---|
168 | return 0; |
---|
169 | |
---|
170 | // everything else expected an INITED stream (OPENING, MAGIC,..) |
---|
171 | // is an error... |
---|
172 | if ( state->stage != ROAR_XCODER_STAGE_INITED ) |
---|
173 | return -1; |
---|
174 | |
---|
175 | if ( state->encode ) { |
---|
176 | if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
177 | return -1; |
---|
178 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
179 | ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC"); |
---|
180 | |
---|
181 | state->stage = ROAR_XCODER_STAGE_OPENING; |
---|
182 | |
---|
183 | tmp_net = ROAR_HOST2NET16(self->mode); |
---|
184 | if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 ) |
---|
185 | return -1; |
---|
186 | |
---|
187 | state->stage = ROAR_XCODER_STAGE_OPENED; |
---|
188 | } else { |
---|
189 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len); |
---|
190 | if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
191 | return -1; |
---|
192 | |
---|
193 | if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 ) |
---|
194 | return -1; |
---|
195 | |
---|
196 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
197 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len); |
---|
198 | |
---|
199 | if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 ) |
---|
200 | return -1; |
---|
201 | |
---|
202 | self->mode = ROAR_NET2HOST16(tmp_net); |
---|
203 | |
---|
204 | state->stage = ROAR_XCODER_STAGE_OPENING; |
---|
205 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len); |
---|
206 | |
---|
207 | switch (self->mode) { |
---|
208 | case ROAR_SPEEX_MODE_NB: self->xcoder = speex_decoder_init(&speex_nb_mode); break; |
---|
209 | case ROAR_SPEEX_MODE_WB: self->xcoder = speex_decoder_init(&speex_wb_mode); break; |
---|
210 | case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break; |
---|
211 | default: return -1; |
---|
212 | } |
---|
213 | |
---|
214 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len); |
---|
215 | |
---|
216 | tmp=1; |
---|
217 | speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp); |
---|
218 | tmp = state->info.pcm.rate; |
---|
219 | speex_decoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp); |
---|
220 | speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
221 | |
---|
222 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo); |
---|
223 | |
---|
224 | if ( self->stereo ) { |
---|
225 | memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state)); |
---|
226 | |
---|
227 | callback.callback_id = SPEEX_INBAND_STEREO; |
---|
228 | callback.func = speex_std_stereo_request_handler; |
---|
229 | callback.data = &(self->stereo_state); |
---|
230 | |
---|
231 | speex_decoder_ctl(self->xcoder, SPEEX_SET_HANDLER, &callback); |
---|
232 | } |
---|
233 | |
---|
234 | state->stage = ROAR_XCODER_STAGE_OPENED; |
---|
235 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len); |
---|
236 | } |
---|
237 | |
---|
238 | // on no error... |
---|
239 | return 0; |
---|
240 | } |
---|
241 | |
---|
242 | int roar_xcoder_speex_encode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
243 | struct roar_xcoder_speex * self = state->inst; |
---|
244 | int pkg_len; |
---|
245 | |
---|
246 | if (!state->encode) |
---|
247 | return -1; |
---|
248 | |
---|
249 | ROAR_DBG("roar_xcoder_speex_encode(*): Encoding..."); |
---|
250 | |
---|
251 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) |
---|
252 | if ( roar_xcoder_speex_proc_header(state) == -1 ) |
---|
253 | return -1; |
---|
254 | |
---|
255 | speex_bits_reset(&(self->bits)); |
---|
256 | |
---|
257 | if ( self->stereo ) |
---|
258 | speex_encode_stereo_int((spx_int16_t *) buf, self->frame_size, &(self->bits)); |
---|
259 | |
---|
260 | speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits)); |
---|
261 | |
---|
262 | pkg_len = speex_bits_write(&(self->bits), self->cc + 2, self->max_cc); |
---|
263 | |
---|
264 | *((uint16_t*)self->cc) = ROAR_HOST2NET16(pkg_len); |
---|
265 | |
---|
266 | if ( roar_vio_write(state->backend, self->cc, pkg_len + 2) != (pkg_len + 2) ) |
---|
267 | return -1; |
---|
268 | |
---|
269 | return 0; |
---|
270 | } |
---|
271 | |
---|
272 | // TODO: move all the init thingys into a seperate function |
---|
273 | int roar_xcoder_speex_decode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
274 | struct roar_xcoder_speex * self = state->inst; |
---|
275 | uint16_t tmp_net; |
---|
276 | int pkg_len; |
---|
277 | |
---|
278 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
279 | |
---|
280 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) |
---|
281 | if ( roar_xcoder_speex_proc_header(state) == -1 ) |
---|
282 | return -1; |
---|
283 | |
---|
284 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = %s", state, buf, (unsigned long)len, |
---|
285 | state->stage == ROAR_XCODER_STAGE_OPENED ? "OPENED" : "???" |
---|
286 | ); |
---|
287 | |
---|
288 | if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 ) |
---|
289 | return -1; |
---|
290 | |
---|
291 | pkg_len = ROAR_NET2HOST16(tmp_net); |
---|
292 | |
---|
293 | if ( pkg_len > ROAR_SPEEX_MAX_CC ) |
---|
294 | return -1; |
---|
295 | |
---|
296 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
297 | |
---|
298 | if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len ) |
---|
299 | return -1; |
---|
300 | |
---|
301 | speex_bits_read_from(&(self->bits), self->cc, pkg_len); |
---|
302 | |
---|
303 | speex_decode_int(self->xcoder, &(self->bits), buf); |
---|
304 | |
---|
305 | if ( self->stereo ) { |
---|
306 | speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state)); |
---|
307 | } |
---|
308 | |
---|
309 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len); |
---|
310 | return 0; |
---|
311 | } |
---|
312 | |
---|
313 | #endif |
---|
314 | |
---|
315 | //ll |
---|