1 | //codecfilter_speex.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard 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 | * RoarAudio 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, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
27 | |
---|
28 | /* |
---|
29 | Format: |
---|
30 | (all numbers are in network byte order) |
---|
31 | |
---|
32 | MAGIC of site MAGIC_LEN |
---|
33 | MODE NUMMBER of size 2 Byte |
---|
34 | { // frames |
---|
35 | FRAME LENGTH of size 2 Byte |
---|
36 | FRAME DATA of size FRAME LENGTH Byte |
---|
37 | } |
---|
38 | */ |
---|
39 | |
---|
40 | #define _FS (_16BIT * (self->stereo ? 2 : 1)) |
---|
41 | |
---|
42 | int cf_speex_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
43 | struct roar_stream_server * info, |
---|
44 | struct roar_codecfilter * filter) { |
---|
45 | struct codecfilter_speex_inst * self = malloc(sizeof(struct codecfilter_speex_inst)); |
---|
46 | struct roar_stream * s = ROAR_STREAM(info); |
---|
47 | |
---|
48 | *inst = NULL; |
---|
49 | |
---|
50 | if (!self) |
---|
51 | return -1; |
---|
52 | |
---|
53 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
54 | s->info.bits = 16; // speex hardcoded |
---|
55 | |
---|
56 | switch (s->info.channels) { |
---|
57 | case 1: self->stereo = 0; break; |
---|
58 | case 2: self->stereo = 1; break; |
---|
59 | default: |
---|
60 | free(self); |
---|
61 | return -1; |
---|
62 | } |
---|
63 | |
---|
64 | // do as much to preper the startup of stereo encoder as possible |
---|
65 | if ( self->stereo ) { |
---|
66 | self->stereo_callback.callback_id = SPEEX_INBAND_STEREO; |
---|
67 | self->stereo_callback.func = speex_std_stereo_request_handler; |
---|
68 | self->stereo_callback.data = &(self->stereo_state); |
---|
69 | } |
---|
70 | |
---|
71 | self->encoder = NULL; |
---|
72 | self->decoder = NULL; |
---|
73 | |
---|
74 | self->stream = info; |
---|
75 | |
---|
76 | self->cd = NULL; |
---|
77 | |
---|
78 | self->i_rest = NULL; |
---|
79 | self->fi_rest = 0; |
---|
80 | self->o_rest = NULL; |
---|
81 | self->fo_rest = 0; |
---|
82 | |
---|
83 | speex_bits_init(&(self->bits)); |
---|
84 | |
---|
85 | *inst = (void*) self; |
---|
86 | |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | int cf_speex_close(CODECFILTER_USERDATA_T inst) { |
---|
91 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
92 | |
---|
93 | if (!self) |
---|
94 | return -1; |
---|
95 | |
---|
96 | if ( self->encoder ) |
---|
97 | speex_encoder_destroy(self->encoder); |
---|
98 | |
---|
99 | self->encoder = NULL; |
---|
100 | |
---|
101 | if ( self->decoder ) |
---|
102 | speex_decoder_destroy(self->decoder); |
---|
103 | |
---|
104 | self->decoder = NULL; |
---|
105 | |
---|
106 | speex_bits_destroy(&(self->bits)); |
---|
107 | |
---|
108 | if ( self->cd ) |
---|
109 | free(self->cd); |
---|
110 | |
---|
111 | if ( self->i_rest ) |
---|
112 | free(self->i_rest); |
---|
113 | |
---|
114 | free((void*)self); |
---|
115 | |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|
119 | int cf_speex_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
120 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
121 | int mode; |
---|
122 | uint16_t ui; |
---|
123 | int tmp; |
---|
124 | int still_todo = len / _FS; |
---|
125 | int ret = 0; |
---|
126 | int fs2; // = self->frame_size * _16BIT * channels; |
---|
127 | char magic[ROAR_SPEEX_MAGIC_LEN]; |
---|
128 | SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT; |
---|
129 | |
---|
130 | ROAR_DBG("cf_speex_read(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
131 | |
---|
132 | if ( ! self->decoder ) { |
---|
133 | ROAR_DBG("cf_speex_read(*): no decoder, starting one!"); |
---|
134 | |
---|
135 | if ( stream_vio_s_read(self->stream, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
136 | return 0; |
---|
137 | |
---|
138 | if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 ) |
---|
139 | return -1; |
---|
140 | |
---|
141 | if ( stream_vio_s_read(self->stream, &ui, 2) != 2 ) |
---|
142 | return 0; |
---|
143 | |
---|
144 | mode = ROAR_NET2HOST16(ui); |
---|
145 | |
---|
146 | if ( mode == ROAR_SPEEX_MODE_NB ) { |
---|
147 | self->decoder = speex_decoder_init(&speex_nb_mode); |
---|
148 | } else if ( mode == ROAR_SPEEX_MODE_WB ) { |
---|
149 | self->decoder = speex_decoder_init(&speex_wb_mode); |
---|
150 | } else if ( mode == ROAR_SPEEX_MODE_UWB ) { |
---|
151 | self->decoder = speex_decoder_init(&speex_uwb_mode); |
---|
152 | } else { |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | tmp=1; |
---|
157 | speex_decoder_ctl(self->decoder, SPEEX_SET_ENH, &tmp); |
---|
158 | |
---|
159 | if ( self->stereo ) { |
---|
160 | memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state)); |
---|
161 | speex_decoder_ctl(self->decoder, SPEEX_SET_HANDLER, &(self->stereo_callback)); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | speex_decoder_ctl(self->decoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
166 | |
---|
167 | fs2 = self->frame_size * _FS; |
---|
168 | |
---|
169 | ROAR_DBG("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, fs2); |
---|
170 | |
---|
171 | if ( !self->cd ) { |
---|
172 | self->cd = malloc(fs2); |
---|
173 | if ( !self->cd ) |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | if ( !self->i_rest ) { |
---|
178 | self->i_rest = malloc(fs2); |
---|
179 | if ( !self->i_rest ) |
---|
180 | return 0; |
---|
181 | } |
---|
182 | } |
---|
183 | fs2 = self->frame_size * _FS; |
---|
184 | |
---|
185 | ROAR_DBG("cf_speex_read(*): Have a working decoder!"); |
---|
186 | |
---|
187 | ROAR_DBG("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, fs2); |
---|
188 | ROAR_DBG("cf_speex_read(*): i_rest is %i bytes after cd", ((void*)self->i_rest - (void*)self->cd)); |
---|
189 | |
---|
190 | |
---|
191 | if ( self->fi_rest ) { |
---|
192 | if ( self->fi_rest > (still_todo*_FS) ) { |
---|
193 | ROAR_DBG("cf_speex_read(*): using data from input rest buffer: len=%i (no need to read new data)", self->fi_rest); |
---|
194 | still_todo *= _FS; // we will set this to zero one way or another, |
---|
195 | // so we don't need to care about soring a 'wrong' value here. |
---|
196 | memcpy(buf, self->i_rest, still_todo); |
---|
197 | memmove(self->i_rest, self->i_rest + still_todo, self->fi_rest - still_todo); |
---|
198 | self->fi_rest -= still_todo; |
---|
199 | ret += still_todo; |
---|
200 | still_todo = 0; |
---|
201 | } else { |
---|
202 | ROAR_DBG("cf_speex_read(*): using data from input rest buffer: len=%i", self->fi_rest); |
---|
203 | memcpy(buf, self->i_rest, self->fi_rest); |
---|
204 | buf += self->fi_rest; |
---|
205 | still_todo -= self->fi_rest/_FS; |
---|
206 | ret += self->fi_rest; |
---|
207 | self->fi_rest = 0; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | while (still_todo) { |
---|
212 | ROAR_DBG("cf_speex_read(*): we sill need %i frames", still_todo); |
---|
213 | if ( stream_vio_s_read(self->stream, &ui, 2) != 2 ) |
---|
214 | return -1; |
---|
215 | |
---|
216 | ui = ROAR_NET2HOST16(ui); |
---|
217 | |
---|
218 | if ( ui > ROAR_SPEEX_MAX_CC ) |
---|
219 | return 0; |
---|
220 | |
---|
221 | if ( stream_vio_s_read(self->stream, self->cc, ui) != ui ) |
---|
222 | break; |
---|
223 | |
---|
224 | speex_bits_read_from(&(self->bits), self->cc, ui); |
---|
225 | |
---|
226 | speex_decode_int(self->decoder, &(self->bits), self->cd); |
---|
227 | |
---|
228 | if ( self->stereo) { |
---|
229 | speex_decode_stereo_int(self->cd, self->frame_size, &(self->stereo_state)); |
---|
230 | } |
---|
231 | |
---|
232 | if ( self->frame_size > still_todo ) { |
---|
233 | memcpy(buf, self->cd, still_todo*_FS); |
---|
234 | ret += still_todo*_FS; |
---|
235 | self->fi_rest = (self->frame_size - still_todo)*_FS; |
---|
236 | ROAR_DBG("cf_speex_read(*): self->fi_rest=%i, off=%i", self->fi_rest, still_todo*_FS); |
---|
237 | memcpy(self->i_rest, (self->cd)+(still_todo*_FS), self->fi_rest); |
---|
238 | still_todo = 0; |
---|
239 | } else { |
---|
240 | memcpy(buf, self->cd, fs2); |
---|
241 | buf += fs2; |
---|
242 | ret += fs2; |
---|
243 | still_todo -= self->frame_size; |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | if ( still_todo ) { |
---|
248 | ROAR_DBG("cf_speex_read(*): could not read all reqquested data, returning %i byte less", still_todo*_FS); |
---|
249 | } |
---|
250 | |
---|
251 | ROAR_DBG("cf_speex_read(*) = %i", ret); |
---|
252 | |
---|
253 | return ret; |
---|
254 | } |
---|
255 | |
---|
256 | int cf_speex_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
257 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
258 | uint16_t mode = ROAR_SPEEX_MODE_UWB; |
---|
259 | int tmp; |
---|
260 | int fs2; |
---|
261 | int ret = 0; |
---|
262 | int need_extra; |
---|
263 | int sid; |
---|
264 | void * prethru; |
---|
265 | |
---|
266 | /* |
---|
267 | TODO: Befor this realy works there must be a working way to set the number of channels and bits |
---|
268 | for monetoring clients. Else this will produce some thing stange as a 'mono' file that realy |
---|
269 | contains stereo. |
---|
270 | */ |
---|
271 | |
---|
272 | ROAR_DBG("cf_speex_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
273 | |
---|
274 | if ( ! self->encoder ) { |
---|
275 | sid = ROAR_STREAM(self->stream)->id; |
---|
276 | |
---|
277 | if ( stream_prethru_destroy(sid) == -1 ) { |
---|
278 | return -1; |
---|
279 | } |
---|
280 | |
---|
281 | if ( stream_prethru_add_data(sid, &prethru, ROAR_SPEEX_MAGIC_LEN) == -1 ) { |
---|
282 | return -1; |
---|
283 | } |
---|
284 | |
---|
285 | memcpy(prethru, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN); |
---|
286 | |
---|
287 | if ( stream_vio_s_write(self->stream, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
288 | return -1; |
---|
289 | |
---|
290 | if ( mode == ROAR_SPEEX_MODE_NB ) { |
---|
291 | self->encoder = speex_encoder_init(&speex_nb_mode); |
---|
292 | } else if ( mode == ROAR_SPEEX_MODE_WB ) { |
---|
293 | self->encoder = speex_encoder_init(&speex_wb_mode); |
---|
294 | } else if ( mode == ROAR_SPEEX_MODE_UWB ) { |
---|
295 | self->encoder = speex_encoder_init(&speex_uwb_mode); |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | mode = ROAR_HOST2NET16(mode); |
---|
300 | |
---|
301 | if ( stream_prethru_add_data(sid, &prethru, 2) == -1 ) { |
---|
302 | return -1; |
---|
303 | } |
---|
304 | |
---|
305 | *(uint16_t*)prethru = mode; |
---|
306 | |
---|
307 | if ( stream_vio_s_write(self->stream, &mode, 2) != 2 ) |
---|
308 | return -1; |
---|
309 | |
---|
310 | tmp = 8; |
---|
311 | speex_encoder_ctl(self->encoder, SPEEX_SET_QUALITY, &tmp); |
---|
312 | speex_encoder_ctl(self->encoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
313 | |
---|
314 | fs2 = self->frame_size * _FS; |
---|
315 | |
---|
316 | if ( !self->cd ) { |
---|
317 | self->cd = malloc(fs2 + 2); |
---|
318 | if ( !self->cd ) |
---|
319 | return 0; |
---|
320 | } |
---|
321 | |
---|
322 | if ( !self->o_rest ) { |
---|
323 | self->o_rest = malloc(fs2); |
---|
324 | if ( !self->o_rest ) |
---|
325 | return 0; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | fs2 = self->frame_size * _FS; |
---|
330 | |
---|
331 | if ( self->fo_rest ) { // ignore the rest for the moment |
---|
332 | if ( (self->fo_rest + len) > fs2 ) { |
---|
333 | need_extra = fs2 - self->fo_rest; |
---|
334 | memcpy(self->o_rest + self->fo_rest, buf, need_extra); |
---|
335 | |
---|
336 | speex_bits_reset(&(self->bits)); |
---|
337 | |
---|
338 | if ( self->stereo ) |
---|
339 | speex_encode_stereo_int((spx_int16_t *) self->o_rest, self->frame_size, &(self->bits)); |
---|
340 | |
---|
341 | speex_encode_int(self->encoder, (spx_int16_t *) self->o_rest, &(self->bits)); |
---|
342 | |
---|
343 | tmp = mode = speex_bits_write(&(self->bits), self->cd + 2, fs2); |
---|
344 | |
---|
345 | mode = ROAR_HOST2NET16(mode); |
---|
346 | *(uint16_t*)(self->cd) = mode; |
---|
347 | if ( stream_vio_s_write(self->stream, self->cd, tmp + 2) != (tmp + 2) ) |
---|
348 | return -1; |
---|
349 | |
---|
350 | buf += need_extra; |
---|
351 | ret += need_extra; |
---|
352 | len -= need_extra; |
---|
353 | self->fo_rest = 0; |
---|
354 | } else { // just add the data to o_rest |
---|
355 | memcpy(self->o_rest + self->fo_rest, buf, len); |
---|
356 | self->fo_rest += len; |
---|
357 | return len; |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | // TODO: do we realy need such a loop? |
---|
362 | while (len > fs2) { |
---|
363 | // ROAR_WARN("cf_speex_write(*): Discarding a full block of data as non-o_rest encoding is not supported!"); |
---|
364 | // ROAR_WARN("cf_speex_write(*): Block info: len=%i, fs2=%i", len, fs2); |
---|
365 | |
---|
366 | speex_bits_reset(&(self->bits)); |
---|
367 | |
---|
368 | if ( self->stereo ) |
---|
369 | speex_encode_stereo_int((spx_int16_t *) buf, self->frame_size, &(self->bits)); |
---|
370 | |
---|
371 | speex_encode_int(self->encoder, (spx_int16_t *) buf, &(self->bits)); |
---|
372 | |
---|
373 | tmp = mode = speex_bits_write(&(self->bits), self->cd + 2, fs2); |
---|
374 | |
---|
375 | mode = ROAR_HOST2NET16(mode); |
---|
376 | |
---|
377 | *(uint16_t*)(self->cd) = mode; |
---|
378 | |
---|
379 | if ( stream_vio_s_write(self->stream, self->cd, tmp + 2) != (tmp + 2) ) |
---|
380 | return -1; |
---|
381 | |
---|
382 | len -= fs2; |
---|
383 | buf += fs2; |
---|
384 | ret += fs2; |
---|
385 | } |
---|
386 | |
---|
387 | if ( len ) { // we still have some data, add this to o_rest |
---|
388 | memcpy(self->o_rest, buf, len); |
---|
389 | self->fo_rest = len; |
---|
390 | ret += len; |
---|
391 | } |
---|
392 | |
---|
393 | return ret; |
---|
394 | } |
---|
395 | |
---|
396 | #endif |
---|
397 | |
---|
398 | //ll |
---|