source: roaraudio/libroardsp/transcode_speex.c @ 2924:fdaddc789f71

Last change on this file since 2924:fdaddc789f71 was 2924:fdaddc789f71, checked in by phi, 15 years ago

moved header stuff to roar_xcoder_speex_proc_header()

File size: 8.8 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_DTX) ) {
94   tmp = self->codec_config->dtx ? 1 : 0;
95   speex_encoder_ctl(self->xcoder, SPEEX_SET_DTX,       &tmp);
96  }
97
98  tmp = info->rate;
99  speex_encoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
100  speex_encoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE,    &(self->frame_size));
101 } else {
102  self->xcoder = NULL;
103 }
104
105 speex_bits_init(&(self->bits));
106
107 return 0;
108}
109
110int roar_xcoder_speex_uninit     (struct roar_xcoder * state) {
111 struct roar_xcoder_speex * self = state->inst;
112
113 if ( self->xcoder != NULL ) {
114  if (state->encode) {
115   speex_encoder_destroy(self->xcoder);
116  } else {
117   speex_decoder_destroy(self->xcoder);
118  }
119 }
120
121 speex_bits_destroy(&(self->bits));
122
123 free(self);
124
125 return 0;
126}
127
128int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) {
129 struct roar_xcoder_speex * self = state->inst;
130
131 if (!state->encode)
132  if (state->stage != ROAR_XCODER_STAGE_OPENED)
133   return -1;
134
135 return _16BIT * self->frame_size * (self->stereo ? 2 : 1);
136}
137
138int roar_xcoder_speex_proc_header(struct roar_xcoder * state) {
139 struct roar_xcoder_speex * self = state->inst;
140 uint16_t tmp_net;
141 int tmp;
142 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
143 SpeexCallback callback;
144 char magic[ROAR_SPEEX_MAGIC_LEN];
145
146 // we do allready open streams not considder an error.
147 if ( state->stage == ROAR_XCODER_STAGE_OPENED )
148  return 0;
149
150 // everything else expected an INITED stream (OPENING, MAGIC,..)
151 // is an error...
152 if ( state->stage != ROAR_XCODER_STAGE_INITED )
153  return -1;
154
155 if ( state->encode ) {
156  if ( roar_vio_write(state->backend, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
157   return -1;
158  state->stage = ROAR_XCODER_STAGE_MAGIC;
159  ROAR_DBG("roar_xcoder_speex_encode(*): Wrote MAGIC");
160
161  state->stage = ROAR_XCODER_STAGE_OPENING;
162
163  tmp_net = ROAR_HOST2NET16(self->mode);
164  if ( roar_vio_write(state->backend, &tmp_net, 2) != 2 )
165   return -1;
166
167  state->stage = ROAR_XCODER_STAGE_OPENED;
168 } else {
169  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len);
170  if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
171   return -1;
172
173  if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 )
174   return -1;
175
176  state->stage = ROAR_XCODER_STAGE_MAGIC;
177  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len);
178
179  if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
180   return -1;
181
182  self->mode = ROAR_NET2HOST16(tmp_net);
183
184  state->stage = ROAR_XCODER_STAGE_OPENING;
185  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len);
186
187  switch (self->mode) {
188   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_decoder_init(&speex_nb_mode);  break;
189   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_decoder_init(&speex_wb_mode);  break;
190   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break;
191   default:
192     return -1;
193    break;
194  }
195
196  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len);
197
198  tmp=1;
199  speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp);
200  tmp = state->info.pcm.rate;
201  speex_decoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
202  speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size));
203
204  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo);
205
206  if ( self->stereo ) {
207   memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state));
208
209   callback.callback_id = SPEEX_INBAND_STEREO;
210   callback.func = speex_std_stereo_request_handler;
211   callback.data = &(self->stereo_state);
212
213   speex_decoder_ctl(self->xcoder, SPEEX_SET_HANDLER, &callback);
214  }
215
216  state->stage = ROAR_XCODER_STAGE_OPENED;
217  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len);
218 }
219
220 // on no error...
221 return 0;
222}
223
224int roar_xcoder_speex_encode     (struct roar_xcoder * state, void * buf, size_t len) {
225 struct roar_xcoder_speex * self = state->inst;
226 int pkg_len;
227
228 if (!state->encode)
229  return -1;
230
231 ROAR_DBG("roar_xcoder_speex_encode(*): Encoding...");
232
233 if ( state->stage == ROAR_XCODER_STAGE_INITED )
234  if ( roar_xcoder_speex_proc_header(state) == -1 )
235   return -1;
236
237 speex_bits_reset(&(self->bits));
238
239 if ( self->stereo )
240  speex_encode_stereo_int((spx_int16_t *) buf, self->frame_size, &(self->bits));
241
242 speex_encode_int(self->xcoder, (spx_int16_t *) buf, &(self->bits));
243
244 pkg_len = speex_bits_write(&(self->bits), self->cc + 2, self->max_cc);
245
246 *((uint16_t*)self->cc) = ROAR_HOST2NET16(pkg_len);
247
248 if ( roar_vio_write(state->backend, self->cc, pkg_len + 2) != (pkg_len + 2) )
249   return -1;
250
251 return 0;
252}
253
254// TODO: move all the init thingys into a seperate function
255int roar_xcoder_speex_decode     (struct roar_xcoder * state, void * buf, size_t len) {
256 struct roar_xcoder_speex * self = state->inst;
257 uint16_t tmp_net;
258 int pkg_len;
259
260 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
261
262 if ( state->stage == ROAR_XCODER_STAGE_INITED )
263  if ( roar_xcoder_speex_proc_header(state) == -1 )
264   return -1;
265
266 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = %s", state, buf, (unsigned long)len,
267          state->stage == ROAR_XCODER_STAGE_OPENED ? "OPENED" : "???"
268         );
269
270 if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
271  return -1;
272
273 pkg_len = ROAR_NET2HOST16(tmp_net);
274
275 if ( pkg_len > ROAR_SPEEX_MAX_CC )
276  return -1;
277
278 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
279
280 if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len )
281  return -1;
282
283 speex_bits_read_from(&(self->bits), self->cc, pkg_len);
284
285 speex_decode_int(self->xcoder, &(self->bits), buf);
286
287 if ( self->stereo ) {
288  speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state));
289 }
290
291 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len);
292 return 0;
293}
294
295#endif
296
297//ll
Note: See TracBrowser for help on using the repository browser.