[2295] | 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 | |
---|
[2894] | 32 | #define _HAVE_CCFG(x) (self->codec_config != NULL && (self->codec_config->para_set & (x))) |
---|
| 33 | |
---|
[2296] | 34 | int roar_xcoder_speex_init (struct roar_xcoder * state) { |
---|
[2298] | 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 | |
---|
[2311] | 39 | ROAR_DBG("roar_xcoder_speex_init(*): sizeof(*self) = %lu", (unsigned long)sizeof(*self)); |
---|
| 40 | |
---|
[2298] | 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 | |
---|
[2305] | 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; |
---|
[2298] | 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) { |
---|
[2894] | 66 | self->codec_config = roar_libroar_config_codec_get(ROAR_CODEC_ROAR_SPEEX, 0); |
---|
| 67 | self->max_cc = ROAR_SPEEX_MAX_CC; |
---|
| 68 | |
---|
[2914] | 69 | ROAR_DBG("roar_xcoder_speex_init(*): self->codec_config=%p", self->codec_config); |
---|
| 70 | |
---|
[2894] | 71 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_MAX_CC) ) { |
---|
| 72 | self->max_cc = self->codec_config->max_cc; |
---|
| 73 | } |
---|
| 74 | |
---|
[2298] | 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 | } |
---|
[2894] | 80 | |
---|
| 81 | if ( _HAVE_CCFG(ROAR_LIBROAR_CONFIG_PSET_COMPLEXITY) ) { |
---|
| 82 | tmp = self->codec_config->complexity / 256; |
---|
| 83 | } else { |
---|
| 84 | tmp = 8; |
---|
| 85 | } |
---|
[2302] | 86 | speex_encoder_ctl(self->xcoder, SPEEX_SET_QUALITY, &tmp); |
---|
[2894] | 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 | |
---|
[2927] | 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 | |
---|
[2894] | 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 | |
---|
[2302] | 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)); |
---|
[2298] | 106 | } else { |
---|
| 107 | self->xcoder = NULL; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | speex_bits_init(&(self->bits)); |
---|
| 111 | |
---|
| 112 | return 0; |
---|
[2296] | 113 | } |
---|
| 114 | |
---|
| 115 | int roar_xcoder_speex_uninit (struct roar_xcoder * state) { |
---|
[2298] | 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; |
---|
[2296] | 131 | } |
---|
| 132 | |
---|
| 133 | int roar_xcoder_speex_packet_size(struct roar_xcoder * state, int samples) { |
---|
[2298] | 134 | struct roar_xcoder_speex * self = state->inst; |
---|
[2300] | 135 | |
---|
| 136 | if (!state->encode) |
---|
| 137 | if (state->stage != ROAR_XCODER_STAGE_OPENED) |
---|
| 138 | return -1; |
---|
[2298] | 139 | |
---|
[2305] | 140 | return _16BIT * self->frame_size * (self->stereo ? 2 : 1); |
---|
[2296] | 141 | } |
---|
| 142 | |
---|
[2923] | 143 | int roar_xcoder_speex_proc_header(struct roar_xcoder * state) { |
---|
| 144 | struct roar_xcoder_speex * self = state->inst; |
---|
[2924] | 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]; |
---|
[2923] | 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 ) { |
---|
[2299] | 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; |
---|
[2924] | 173 | } else { |
---|
[2311] | 174 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = INITED", state, buf, (unsigned long)len); |
---|
[2300] | 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; |
---|
[2311] | 182 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = MAGIC", state, buf, (unsigned long)len); |
---|
[2300] | 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; |
---|
[2311] | 190 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENING", state, buf, (unsigned long)len); |
---|
[2300] | 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 | |
---|
[2311] | 201 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): opened decoder state", state, buf, (unsigned long)len); |
---|
| 202 | |
---|
[2300] | 203 | tmp=1; |
---|
| 204 | speex_decoder_ctl(self->xcoder, SPEEX_SET_ENH, &tmp); |
---|
[2302] | 205 | tmp = state->info.pcm.rate; |
---|
[2315] | 206 | speex_decoder_ctl(self->xcoder, SPEEX_SET_SAMPLING_RATE, &tmp); |
---|
[2300] | 207 | speex_decoder_ctl(self->xcoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
| 208 | |
---|
[2311] | 209 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): self->stereo = %i", state, buf, (unsigned long)len, self->stereo); |
---|
| 210 | |
---|
[2305] | 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 | |
---|
[2300] | 221 | state->stage = ROAR_XCODER_STAGE_OPENED; |
---|
[2311] | 222 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu): state->stage = OPENED", state, buf, (unsigned long)len); |
---|
[2300] | 223 | } |
---|
| 224 | |
---|
[2924] | 225 | // on no error... |
---|
| 226 | return 0; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | int 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 |
---|
| 260 | int 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 | |
---|
[2311] | 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 | |
---|
[2300] | 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 | |
---|
[2311] | 283 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 284 | |
---|
[2300] | 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 | |
---|
[2305] | 292 | if ( self->stereo ) { |
---|
| 293 | speex_decode_stereo_int(buf, self->frame_size, &(self->stereo_state)); |
---|
| 294 | } |
---|
| 295 | |
---|
[2311] | 296 | ROAR_DBG("roar_xcoder_speex_decode(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len); |
---|
[2300] | 297 | return 0; |
---|
[2296] | 298 | } |
---|
| 299 | |
---|
[2295] | 300 | #endif |
---|
| 301 | |
---|
| 302 | //ll |
---|