source: roaraudio/libroardsp/transcode_speex.c @ 2898:8d125311b6c1

Last change on this file since 2898:8d125311b6c1 was 2898:8d125311b6c1, checked in by phi, 15 years ago

patch to reduce TCP overhead

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 + 2, self->max_cc);
169
170 *((uint16_t*)self->cc) = ROAR_HOST2NET16(pkg_len);
171
172 if ( roar_vio_write(state->backend, self->cc, pkg_len + 2) != (pkg_len + 2) )
173   return -1;
174
175 return 0;
176}
177
178// TODO: move all the init thingys into a seperate function
179int roar_xcoder_speex_decode     (struct roar_xcoder * state, void * buf, size_t len) {
180 struct roar_xcoder_speex * self = state->inst;
181 char magic[ROAR_SPEEX_MAGIC_LEN];
182 uint16_t tmp_net;
183 int pkg_len;
184 int tmp;
185 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
186 SpeexCallback callback;
187
188 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
189
190 if ( state->stage == ROAR_XCODER_STAGE_INITED ) {
191  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len);
192  if ( roar_vio_read(state->backend, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN )
193   return -1;
194
195  if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 )
196   return -1;
197
198  state->stage = ROAR_XCODER_STAGE_MAGIC;
199  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len);
200
201  if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
202   return -1;
203
204  self->mode = ROAR_NET2HOST16(tmp_net);
205
206  state->stage = ROAR_XCODER_STAGE_OPENING;
207  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len);
208
209  switch (self->mode) {
210   case ROAR_SPEEX_MODE_NB:  self->xcoder = speex_decoder_init(&speex_nb_mode);  break;
211   case ROAR_SPEEX_MODE_WB:  self->xcoder = speex_decoder_init(&speex_wb_mode);  break;
212   case ROAR_SPEEX_MODE_UWB: self->xcoder = speex_decoder_init(&speex_uwb_mode); break;
213   default:
214     return -1;
215    break;
216  }
217
218  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len);
219
220  tmp=1;
221  speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp);
222  tmp = state->info.pcm.rate;
223  speex_decoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp);
224  speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size));
225
226  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo);
227
228  if ( self->stereo ) {
229   memcpy(&(self->stereo_state), &stereo, sizeof(self->stereo_state));
230
231   callback.callback_id = SPEEX_INBAND_STEREO;
232   callback.func = speex_std_stereo_request_handler;
233   callback.data = &(self->stereo_state);
234
235   speex_decoder_ctl(self->xcoder, SPEEX_SET_HANDLER, &callback);
236  }
237
238  state->stage = ROAR_XCODER_STAGE_OPENED;
239  ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len);
240 }
241
242 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = %s", state, buf, (unsigned long)len,
243          state->stage == ROAR_XCODER_STAGE_OPENED ? "OPENED" : "???"
244         );
245
246 if ( roar_vio_read(state->backend, &tmp_net, 2) != 2 )
247  return -1;
248
249 pkg_len = ROAR_NET2HOST16(tmp_net);
250
251 if ( pkg_len > ROAR_SPEEX_MAX_CC )
252  return -1;
253
254 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
255
256 if ( roar_vio_read(state->backend, self->cc, pkg_len) != pkg_len )
257  return -1;
258
259 speex_bits_read_from(&(self->bits), self->cc, pkg_len);
260
261 speex_decode_int(self->xcoder, &(self->bits), buf);
262
263 if ( self->stereo ) {
264  speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state));
265 }
266
267 ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len);
268 return 0;
269}
270
271#endif
272
273//ll
Note: See TracBrowser for help on using the repository browser.