source: roaraudio/libroardsp/transcode_speex.c @ 2894:70f3e75119e6

Last change on this file since 2894:70f3e75119e6 was 2894:70f3e75119e6, checked in by phi, 15 years ago

make it possible to set q, complexity, dtx and max_cc option for speex

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