[371] | 1 | //codecfilter_speex.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 5 | |
---|
[373] | 6 | int cf_speex_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 7 | struct roar_stream_server * info, |
---|
| 8 | struct roar_codecfilter * filter) { |
---|
[391] | 9 | struct codecfilter_speex_inst * self = malloc(sizeof(struct codecfilter_speex_inst)); |
---|
[609] | 10 | struct roar_stream * s = ROAR_STREAM(info); |
---|
[391] | 11 | |
---|
[373] | 12 | *inst = NULL; |
---|
[391] | 13 | |
---|
| 14 | if (!self) |
---|
| 15 | return -1; |
---|
| 16 | |
---|
[392] | 17 | self->encoder = NULL; |
---|
| 18 | self->decoder = NULL; |
---|
| 19 | |
---|
[393] | 20 | self->stream = info; |
---|
| 21 | |
---|
[395] | 22 | self->cd = NULL; |
---|
| 23 | |
---|
| 24 | self->i_rest = NULL; |
---|
| 25 | self->fi_rest = 0; |
---|
[617] | 26 | self->o_rest = NULL; |
---|
| 27 | self->fo_rest = 0; |
---|
[395] | 28 | |
---|
[392] | 29 | speex_bits_init(&(self->bits)); |
---|
| 30 | |
---|
[395] | 31 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 32 | s->info.bits = 16; // speex hardcoded |
---|
[614] | 33 | s->info.channels = 1; // only mono support at the moment |
---|
[395] | 34 | |
---|
[391] | 35 | *inst = (void*) self; |
---|
| 36 | |
---|
| 37 | return 0; |
---|
[373] | 38 | } |
---|
| 39 | |
---|
| 40 | int cf_speex_close(CODECFILTER_USERDATA_T inst) { |
---|
[391] | 41 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
| 42 | |
---|
| 43 | if (!self) |
---|
| 44 | return -1; |
---|
| 45 | |
---|
[392] | 46 | if ( self->encoder ) |
---|
[616] | 47 | speex_encoder_destroy(self->encoder); |
---|
[392] | 48 | |
---|
| 49 | self->encoder = NULL; |
---|
| 50 | |
---|
| 51 | if ( self->decoder ) |
---|
| 52 | speex_decoder_destroy(self->decoder); |
---|
| 53 | |
---|
| 54 | self->decoder = NULL; |
---|
| 55 | |
---|
| 56 | speex_bits_destroy(&(self->bits)); |
---|
| 57 | |
---|
[395] | 58 | if ( self->cd ) |
---|
| 59 | free(self->cd); |
---|
| 60 | |
---|
| 61 | if ( self->i_rest ) |
---|
| 62 | free(self->i_rest); |
---|
| 63 | |
---|
[391] | 64 | free((void*)self); |
---|
| 65 | |
---|
| 66 | return 0; |
---|
[373] | 67 | } |
---|
| 68 | |
---|
| 69 | int cf_speex_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[391] | 70 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
[393] | 71 | int mode; |
---|
| 72 | uint16_t ui; |
---|
[395] | 73 | int tmp; |
---|
| 74 | int still_todo = len / 2 /* 16 bit */; |
---|
| 75 | int ret = 0; |
---|
[614] | 76 | int fs2; // = self->frame_size * 2; |
---|
| 77 | char magic[ROAR_SPEEX_MAGIC_LEN]; |
---|
[393] | 78 | |
---|
[614] | 79 | ROAR_DBG("cf_speex_read(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
[396] | 80 | |
---|
[393] | 81 | if ( ! self->decoder ) { |
---|
[395] | 82 | ROAR_DBG("cf_speex_read(*): no decoder, starting one!"); |
---|
[614] | 83 | |
---|
| 84 | if ( stream_vio_s_read(self->stream, magic, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
| 85 | return 0; |
---|
| 86 | |
---|
| 87 | if ( memcmp(magic, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != 0 ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
[608] | 90 | if ( stream_vio_s_read(self->stream, &ui, 2) != 2 ) |
---|
[393] | 91 | return 0; |
---|
| 92 | |
---|
| 93 | mode = ntohs(ui); |
---|
| 94 | |
---|
| 95 | if ( mode == ROAR_SPEEX_MODE_NB ) { |
---|
| 96 | self->decoder = speex_decoder_init(&speex_nb_mode); |
---|
| 97 | } else if ( mode == ROAR_SPEEX_MODE_WB ) { |
---|
| 98 | self->decoder = speex_decoder_init(&speex_wb_mode); |
---|
| 99 | } else if ( mode == ROAR_SPEEX_MODE_UWB ) { |
---|
| 100 | self->decoder = speex_decoder_init(&speex_uwb_mode); |
---|
| 101 | } else { |
---|
| 102 | return 0; |
---|
| 103 | } |
---|
[395] | 104 | |
---|
| 105 | tmp=1; |
---|
| 106 | speex_decoder_ctl(self->decoder, SPEEX_SET_ENH, &tmp); |
---|
| 107 | |
---|
| 108 | speex_decoder_ctl(self->decoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
| 109 | |
---|
[614] | 110 | fs2 = self->frame_size * 2; |
---|
[395] | 111 | |
---|
[614] | 112 | ROAR_DBG("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, fs2); |
---|
[395] | 113 | |
---|
| 114 | if ( !self->cd ) { |
---|
[614] | 115 | self->cd = malloc(fs2); |
---|
[395] | 116 | if ( !self->cd ) |
---|
| 117 | return 0; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | if ( !self->i_rest ) { |
---|
[614] | 121 | self->i_rest = malloc(fs2); |
---|
[395] | 122 | if ( !self->i_rest ) |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
[393] | 125 | } |
---|
[614] | 126 | fs2 = self->frame_size * 2; |
---|
[391] | 127 | |
---|
[396] | 128 | ROAR_DBG("cf_speex_read(*): Have a working decoder!"); |
---|
[395] | 129 | |
---|
[614] | 130 | ROAR_DBG("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, fs2); |
---|
[396] | 131 | ROAR_DBG("cf_speex_read(*): i_rest is %i bytes after cd", ((void*)self->i_rest - (void*)self->cd)); |
---|
[395] | 132 | |
---|
| 133 | |
---|
| 134 | if ( self->fi_rest ) { |
---|
[397] | 135 | if ( self->fi_rest > (still_todo*2) ) { |
---|
[614] | 136 | ROAR_DBG("cf_speex_read(*): using data from input rest buffer: len=%i (no need to read new data)", self->fi_rest); |
---|
| 137 | still_todo *= 2; // we will set this to zero one way or another, |
---|
| 138 | // so we don't need to care about soring a 'warong' value here. |
---|
| 139 | memcpy(buf, self->i_rest, still_todo); |
---|
| 140 | memmove(self->i_rest, self->i_rest + still_todo, self->fi_rest - still_todo); |
---|
| 141 | self->fi_rest -= still_todo; |
---|
| 142 | ret += still_todo; |
---|
| 143 | still_todo = 0; |
---|
[395] | 144 | } else { |
---|
[614] | 145 | ROAR_DBG("cf_speex_read(*): using data from input rest buffer: len=%i", self->fi_rest); |
---|
[395] | 146 | memcpy(buf, self->i_rest, self->fi_rest); |
---|
| 147 | buf += self->fi_rest; |
---|
[396] | 148 | still_todo -= self->fi_rest/2; |
---|
| 149 | ret += self->fi_rest; |
---|
[395] | 150 | self->fi_rest = 0; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | while (still_todo) { |
---|
[614] | 155 | ROAR_DBG("cf_speex_read(*): we sill need %i frames", still_todo); |
---|
[608] | 156 | if ( stream_vio_s_read(self->stream, &ui, 2) != 2 ) |
---|
[395] | 157 | return -1; |
---|
| 158 | |
---|
| 159 | ui = ntohs(ui); |
---|
| 160 | |
---|
| 161 | if ( ui > ROAR_SPEEX_MAX_CC ) |
---|
| 162 | return 0; |
---|
| 163 | |
---|
[608] | 164 | if ( stream_vio_s_read(self->stream, self->cc, ui) != ui ) |
---|
[395] | 165 | break; |
---|
| 166 | |
---|
| 167 | speex_bits_read_from(&(self->bits), self->cc, ui); |
---|
| 168 | |
---|
| 169 | speex_decode_int(self->decoder, &(self->bits), self->cd); |
---|
| 170 | |
---|
| 171 | if ( self->frame_size > still_todo ) { |
---|
| 172 | memcpy(buf, self->cd, still_todo*2); |
---|
| 173 | ret += still_todo*2; |
---|
| 174 | self->fi_rest = (self->frame_size - still_todo)*2; |
---|
[614] | 175 | ROAR_DBG("cf_speex_read(*): self->fi_rest=%i, off=%i", self->fi_rest, still_todo*2); |
---|
[395] | 176 | memcpy(self->i_rest, (self->cd)+(still_todo*2), self->fi_rest); |
---|
| 177 | still_todo = 0; |
---|
| 178 | } else { |
---|
[614] | 179 | memcpy(buf, self->cd, fs2); |
---|
| 180 | buf += fs2; |
---|
| 181 | ret += fs2; |
---|
[395] | 182 | still_todo -= self->frame_size; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
[396] | 186 | if ( still_todo ) { |
---|
[614] | 187 | ROAR_DBG("cf_speex_read(*): could not read all reqquested data, returning %i byte less", still_todo*2); |
---|
[396] | 188 | } |
---|
| 189 | |
---|
[614] | 190 | ROAR_DBG("cf_speex_read(*) = %i", ret); |
---|
[396] | 191 | |
---|
[395] | 192 | return ret; |
---|
[373] | 193 | } |
---|
[393] | 194 | |
---|
[373] | 195 | int cf_speex_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
[391] | 196 | struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst; |
---|
[615] | 197 | uint16_t mode = ROAR_SPEEX_MODE_UWB; |
---|
| 198 | int tmp; |
---|
[617] | 199 | int fs2; |
---|
| 200 | int ret = 0; |
---|
| 201 | int need_extra; |
---|
[615] | 202 | |
---|
[619] | 203 | ROAR_DBG("cf_speex_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
| 204 | |
---|
[615] | 205 | if ( ! self->encoder ) { |
---|
| 206 | if ( stream_vio_s_write(self->stream, ROAR_SPEEX_MAGIC, ROAR_SPEEX_MAGIC_LEN) != ROAR_SPEEX_MAGIC_LEN ) |
---|
| 207 | return -1; |
---|
| 208 | |
---|
| 209 | if ( mode == ROAR_SPEEX_MODE_NB ) { |
---|
| 210 | self->encoder = speex_encoder_init(&speex_nb_mode); |
---|
| 211 | } else if ( mode == ROAR_SPEEX_MODE_WB ) { |
---|
| 212 | self->encoder = speex_encoder_init(&speex_wb_mode); |
---|
| 213 | } else if ( mode == ROAR_SPEEX_MODE_UWB ) { |
---|
| 214 | self->encoder = speex_encoder_init(&speex_uwb_mode); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | mode = ROAR_HOST2NET16(mode); |
---|
| 218 | |
---|
| 219 | if ( stream_vio_s_write(self->stream, &mode, 2) != 2 ) |
---|
| 220 | return -1; |
---|
| 221 | |
---|
| 222 | tmp = 8; |
---|
| 223 | speex_encoder_ctl(self->encoder, SPEEX_SET_QUALITY, &tmp); |
---|
| 224 | speex_encoder_ctl(self->encoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size)); |
---|
| 225 | |
---|
[617] | 226 | fs2 = self->frame_size * 2; |
---|
| 227 | |
---|
| 228 | if ( !self->cd ) { |
---|
| 229 | self->cd = malloc(fs2); |
---|
| 230 | if ( !self->cd ) |
---|
| 231 | return 0; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | if ( !self->o_rest ) { |
---|
| 235 | self->o_rest = malloc(fs2); |
---|
| 236 | if ( !self->o_rest ) |
---|
| 237 | return 0; |
---|
| 238 | } |
---|
[615] | 239 | } |
---|
[391] | 240 | |
---|
[617] | 241 | fs2 = self->frame_size * 2; |
---|
| 242 | |
---|
| 243 | if ( self->fo_rest ) { // ignore the rest for the moment |
---|
| 244 | if ( (self->fo_rest + len) > fs2 ) { |
---|
| 245 | need_extra = fs2 - self->fo_rest; |
---|
| 246 | memcpy(self->o_rest + self->fo_rest, buf, need_extra); |
---|
| 247 | |
---|
| 248 | speex_bits_reset(&(self->bits)); |
---|
| 249 | |
---|
| 250 | speex_encode_int(self->encoder, (spx_int16_t *) self->o_rest, &(self->bits)); |
---|
| 251 | |
---|
[618] | 252 | tmp = mode = speex_bits_write(&(self->bits), self->cd, fs2); |
---|
[617] | 253 | |
---|
[618] | 254 | mode = ROAR_HOST2NET16(mode); |
---|
[617] | 255 | stream_vio_s_write(self->stream, &mode, 2); |
---|
| 256 | if ( stream_vio_s_write(self->stream, self->cd, tmp) != tmp ) |
---|
| 257 | return -1; |
---|
| 258 | |
---|
| 259 | buf += need_extra; |
---|
| 260 | ret += need_extra; |
---|
| 261 | len -= need_extra; |
---|
| 262 | self->fo_rest = 0; |
---|
| 263 | } else { // just add the data to o_rest |
---|
| 264 | memcpy(self->o_rest + self->fo_rest, buf, len); |
---|
| 265 | self->fo_rest += len; |
---|
| 266 | return len; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | // TODO: do we realy need such a loop? |
---|
| 271 | while (len > fs2) { |
---|
[619] | 272 | // ROAR_WARN("cf_speex_write(*): Discarding a full block of data as non-o_rest encoding is not supported!"); |
---|
| 273 | // ROAR_WARN("cf_speex_write(*): Block info: len=%i, fs2=%i", len, fs2); |
---|
| 274 | |
---|
| 275 | speex_bits_reset(&(self->bits)); |
---|
| 276 | |
---|
| 277 | speex_encode_int(self->encoder, (spx_int16_t *) buf, &(self->bits)); |
---|
| 278 | |
---|
| 279 | tmp = mode = speex_bits_write(&(self->bits), self->cd, fs2); |
---|
| 280 | |
---|
| 281 | mode = ROAR_HOST2NET16(mode); |
---|
| 282 | |
---|
| 283 | stream_vio_s_write(self->stream, &mode, 2); |
---|
| 284 | |
---|
| 285 | if ( stream_vio_s_write(self->stream, self->cd, tmp) != tmp ) |
---|
| 286 | return -1; |
---|
| 287 | |
---|
[617] | 288 | len -= fs2; |
---|
| 289 | buf += fs2; |
---|
| 290 | ret += fs2; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | if ( len ) { // we still have some data, add this to o_rest |
---|
| 294 | memcpy(self->o_rest, buf, len); |
---|
| 295 | self->fo_rest = len; |
---|
| 296 | ret += len; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | return ret; |
---|
[373] | 300 | } |
---|
| 301 | |
---|
[371] | 302 | #endif |
---|
| 303 | |
---|
| 304 | //ll |
---|