source: roaraudio/libroardsp/transcode_speex.c @ 2927:1c2c516cbdf9

Last change on this file since 2927:1c2c516cbdf9 was 2927:1c2c516cbdf9, checked in by phi, 15 years ago

added support for ROAR_LIBROAR_CONFIG_PSET_VBR

File size: 9.0 KB
Line 
1//transcode_speex.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "libroardsp.h"
26
27#ifdef ROAR_HAVE_LIBSPEEX
28
29#define _16BIT (16/8)
30#define _SIZE_LEN 2
31
32#define _HAVE_CCFG(x) (self->codec_config != NULL && (self->codec_config->para_set & (x)))
33
34int roar_xcoder_speex_init       (struct roar_xcoder * state) {
35 struct roar_xcoder_speex * self = malloc(sizeof(struct roar_xcoder_speex));
36 struct roar_audio_info  * info = &(state->info.pcm);
37 int tmp;
38
39 ROAR_DBG("roar_xcoder_speex_init(*): sizeof(*self) = %lu", (unsigned long)sizeof(*self));
40
41 if ( self == NULL )
42  return -1;
43
44 // curruntly only 16 bit mode is supported
45 if ( info->bits != 16 ) {
46  free(self);
47  return -1;
48 }
49
50 // only mono/stereo mode is supported
51 switch (info->channels) {
52  case 1: self->stereo = 0; break;
53  case 2: self->stereo = 1; break;
54  default:
55    free(self);
56    return -1;
57 }
58
59 memset(self, 0, sizeof(struct roar_xcoder_speex));
60
61 state->inst = self;
62
63 self->mode  = ROAR_SPEEX_MODE_UWB;
64
65 if (state->encode) {
66  self->codec_config = roar_libroar_config_codec_get(ROAR_CODEC_ROAR_SPEEX, 0);
67  self->max_cc = ROAR_SPEEX_MAX_CC;
68
69  ROAR_DBG("roar_xcoder_speex_init(*): self->codec_config=%p", self->codec_config);
70
71  if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_MAX_CC) ) {
72   self->max_cc = self->codec_config->max_cc;
73  }
74
75  switch (self->mode) {
76   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_encoder_init(&speex_nb_mode);  break;
77   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_encoder_init(&speex_wb_mode);  break;
78   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_encoder_init(&speex_uwb_mode); break;
79  }
80
81  if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY) ) {
82   tmp = self->codec_config->complexity / 256;
83  } else {
84   tmp = 8;
85  }
86  speex_encoder_ctl(self->xcoder, SPEEX_SET_QUALITY,       &tmp);
87
88  if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_Q) ) {
89   tmp = self->codec_config->q / 256;
90   speex_encoder_ctl(self->xcoder, SPEEX_SET_QUALITY,       &tmp);
91  }
92
93  if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_VBR) ) {
94   tmp = self->codec_config->vbr ? 1 : 0;
95   speex_encoder_ctl(self->xcoder, SPEEX_SET_VBR,       &tmp);
96  }
97
98  if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_DTX) ) {
99   tmp = self->codec_config->dtx ? 1 : 0;
100   speex_encoder_ctl(self->xcoder, SPEEX_SET_DTX,       &tmp);
101  }
102
103  tmp = info->rate;
104  speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
105  speex_encoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE,    &(self->frame_size));
106 } else {
107  self->xcoder = NULL;
108 }
109
110 speex_bits_init(&(self->bits));
111
112 return 0;
113}
114
115int roar_xcoder_speex_uninit     (struct roar_xcoder * state) {
116 struct roar_xcoder_speex * self = state->inst;
117
118 if ( self->xcoder != NULL ) {
119  if (state->encode) {
120   speex_encoder_destroy(self->xcoder);
121  } else {
122   speex_decoder_destroy(self->xcoder);
123  }
124 }
125
126 speex_bits_destroy(&(self->bits));
127
128 free(self);
129
130 return 0;
131}
132
133int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) {
134 struct roar_xcoder_speex * self = state->inst;
135
136 if (!state->encode)
137  if (state->stage != ROAR_XCODER_STAGE_OPENED)
138   return -1;
139
140 return _16BIT * self->frame_size * (self->stereo ? 2 : 1);
141}
142
143int roar_xcoder_speex_proc_header(struct roar_xcoder * state) {
144 struct roar_xcoder_speex * self = state->inst;
145 uint16_t tmp_net;
146 int tmp;
147 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
148 SpeexCallback callback;
149 char magic[ROAR_SPEEX_MAGIC_LEN];
150
151 // we do allready open streams not considder an error.
152 if ( state->stage == ROAR_XCODER_STAGE_OPENED )
153  return 0;
154
155 // everything else expected an INITED stream (OPENING, MAGIC,..)
156 // is an error...
157 if ( state->stage != ROAR_XCODER_STAGE_INITED )
158  return -1;
159
160 if ( state->encode ) {
161  if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
162   return -1;
163  state->stage = ROAR_XCODER_STAGE_MAGIC;
164  ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC");
165
166  state->stage = ROAR_XCODER_STAGE_OPENING;
167
168  tmp_net = ROAR_HOST2NET16(self->mode);
169  if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 )
170   return -1;
171
172  state->stage = ROAR_XCODER_STAGE_OPENED;
173 } else {
174  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len);
175  if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
176   return -1;
177
178  if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 )
179   return -1;
180
181  state->stage = ROAR_XCODER_STAGE_MAGIC;
182  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len);
183
184  if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
185   return -1;
186
187  self->mode = ROAR_NET2HOST16(tmp_net);
188
189  state->stage = ROAR_XCODER_STAGE_OPENING;
190  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len);
191
192  switch (self->mode) {
193   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_decoder_init(&speex_nb_mode);  break;
194   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_decoder_init(&speex_wb_mode);  break;
195   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break;
196   default:
197     return -1;
198    break;
199  }
200
201  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len);
202
203  tmp=1;
204  speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp);
205  tmp = state->info.pcm.rate;
206  speex_decoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
207  speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size));
208
209  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo);
210
211  if ( self->stereo ) {
212   memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state));
213
214   callback.callback_id = SPEEX_INBAND_STEREO;
215   callback.func = speex_std_stereo_request_handler;
216   callback.data = &(self->stereo_state);
217
218   speex_decoder_ctl(self->xcoder, SPEEX_SET_HANDLER, &callback);
219  }
220
221  state->stage = ROAR_XCODER_STAGE_OPENED;
222  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len);
223 }
224
225 // on no error...
226 return 0;
227}
228
229int roar_xcoder_speex_encode     (struct roar_xcoder * state, void * buf, size_t len) {
230 struct roar_xcoder_speex * self = state->inst;
231 int pkg_len;
232
233 if (!state->encode)
234  return -1;
235
236 ROAR_DBG("roar_xcoder_speex_encode(*): Encoding...");
237
238 if ( state->stage == ROAR_XCODER_STAGE_INITED )
239  if ( roar_xcoder_speex_proc_header(state) == -1 )
240   return -1;
241
242 speex_bits_reset(&(self->bits));
243
244 if ( self->stereo )
245  speex_encode_stereo_int((spx_int16_t *) buf, self->frame_size, &(self->bits));
246
247 speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits));
248
249 pkg_len = speex_bits_write(&(self->bits), self->cc + 2, self->max_cc);
250
251 *((uint16_t*)self->cc) = ROAR_HOST2NET16(pkg_len);
252
253 if ( roar_vio_write(state->backend, self->cc, pkg_len + 2) != (pkg_len + 2) )
254   return -1;
255
256 return 0;
257}
258
259// TODO: move all the init thingys into a seperate function
260int roar_xcoder_speex_decode     (struct roar_xcoder * state, void * buf, size_t len) {
261 struct roar_xcoder_speex * self = state->inst;
262 uint16_t tmp_net;
263 int pkg_len;
264
265 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
266
267 if ( state->stage == ROAR_XCODER_STAGE_INITED )
268  if ( roar_xcoder_speex_proc_header(state) == -1 )
269   return -1;
270
271 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = %s", state, buf, (unsigned long)len,
272          state->stage == ROAR_XCODER_STAGE_OPENED ? "OPENED" : "???"
273         );
274
275 if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
276  return -1;
277
278 pkg_len = ROAR_NET2HOST16(tmp_net);
279
280 if ( pkg_len > ROAR_SPEEX_MAX_CC )
281  return -1;
282
283 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
284
285 if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len )
286  return -1;
287
288 speex_bits_read_from(&(self->bits), self->cc, pkg_len);
289
290 speex_decode_int(self->xcoder, &(self->bits), buf);
291
292 if ( self->stereo ) {
293  speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state));
294 }
295
296 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len);
297 return 0;
298}
299
300#endif
301
302//ll
Note: See TracBrowser for help on using the repository browser.