[324] | 1 | //codecfilter_celt.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[668] | 5 | * |
---|
| 6 | * This file is part of roard 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 | * RoarAudio 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[324] | 26 | #include "roard.h" |
---|
[361] | 27 | #ifdef ROAR_HAVE_LIBCELT |
---|
[324] | 28 | |
---|
[4152] | 29 | #ifdef ROAR_HAVE_CELT_VERSION_0_7_1 |
---|
| 30 | typedef celt_int16 celt_int16_t; |
---|
| 31 | #endif |
---|
| 32 | |
---|
[324] | 33 | int cf_celt_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 34 | struct roar_stream_server * info, |
---|
| 35 | struct roar_codecfilter * filter) { |
---|
| 36 | struct codecfilter_celt_inst * self = malloc(sizeof(struct codecfilter_celt_inst)); |
---|
[609] | 37 | struct roar_stream * s = ROAR_STREAM(info); |
---|
[324] | 38 | |
---|
| 39 | if ( !self ) |
---|
| 40 | return -1; |
---|
| 41 | |
---|
[325] | 42 | /* |
---|
| 43 | CELTMode * mode; |
---|
| 44 | CELTEncoder * encoder; |
---|
| 45 | CELTDecoder * decoder; |
---|
| 46 | int frame_size; |
---|
| 47 | int lookahead; |
---|
| 48 | int out_size; |
---|
| 49 | char * ibuf; |
---|
| 50 | char * obuf; |
---|
| 51 | char * rest; |
---|
| 52 | int s_buf; |
---|
| 53 | int f_rest; /-* how much is in rest? *-/ |
---|
| 54 | */ |
---|
| 55 | |
---|
[324] | 56 | self->stream = info; |
---|
[325] | 57 | self->frame_size = 256; |
---|
| 58 | self->lookahead = self->frame_size; |
---|
| 59 | self->encoder = NULL; |
---|
| 60 | self->decoder = NULL; |
---|
[2120] | 61 | self->opened_encoder = 0; |
---|
| 62 | self->opened_decoder = 0; |
---|
[331] | 63 | self->s_buf = s->info.channels * self->frame_size * 2; |
---|
[326] | 64 | self->ibuf = malloc(self->s_buf); |
---|
| 65 | self->obuf = malloc(self->s_buf); |
---|
[331] | 66 | self->i_rest = malloc(self->s_buf); |
---|
| 67 | self->o_rest = malloc(self->s_buf); |
---|
| 68 | self->fi_rest = 0; |
---|
| 69 | self->fo_rest = 0; |
---|
[326] | 70 | |
---|
[331] | 71 | if ( !(self->ibuf && self->obuf && self->i_rest && self->o_rest) ) { |
---|
[326] | 72 | if ( self->ibuf ) |
---|
| 73 | free(self->ibuf); |
---|
| 74 | |
---|
| 75 | if ( self->obuf ) |
---|
| 76 | free(self->obuf); |
---|
| 77 | |
---|
[331] | 78 | if ( self->i_rest ) |
---|
| 79 | free(self->o_rest); |
---|
| 80 | |
---|
| 81 | if ( self->o_rest ) |
---|
| 82 | free(self->o_rest); |
---|
[326] | 83 | |
---|
| 84 | free(self); |
---|
| 85 | return -1; |
---|
| 86 | } |
---|
[4152] | 87 | |
---|
| 88 | #ifdef ROAR_HAVE_CELT_VERSION_0_7_1 |
---|
| 89 | self->mode = celt_mode_create(s->info.rate, self->frame_size, NULL); |
---|
| 90 | #else |
---|
[350] | 91 | self->mode = celt_mode_create(s->info.rate, s->info.channels, self->frame_size, NULL); |
---|
[4152] | 92 | #endif |
---|
[325] | 93 | |
---|
| 94 | if ( !self->mode ) { |
---|
| 95 | free(self); |
---|
| 96 | return -1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | if ( s->dir == ROAR_DIR_PLAY ) { |
---|
[4152] | 100 | #ifdef ROAR_HAVE_CELT_VERSION_0_7_1 |
---|
| 101 | self->decoder = celt_decoder_create(self->mode, s->info.channels, NULL); |
---|
| 102 | #else |
---|
[325] | 103 | self->decoder = celt_decoder_create(self->mode); |
---|
[4152] | 104 | #endif |
---|
[634] | 105 | } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) { |
---|
[4152] | 106 | #ifdef ROAR_HAVE_CELT_VERSION_0_7_1 |
---|
| 107 | self->encoder = celt_encoder_create(self->mode, s->info.channels, NULL); |
---|
| 108 | #else |
---|
[325] | 109 | self->encoder = celt_encoder_create(self->mode); |
---|
[4152] | 110 | #endif |
---|
[2120] | 111 | } else if ( s->dir == ROAR_DIR_BIDIR ) { |
---|
[4152] | 112 | #ifdef ROAR_HAVE_CELT_VERSION_0_7_1 |
---|
| 113 | self->decoder = celt_decoder_create(self->mode, s->info.channels, NULL); |
---|
| 114 | self->encoder = celt_encoder_create(self->mode, s->info.channels, NULL); |
---|
| 115 | #else |
---|
[2120] | 116 | self->decoder = celt_decoder_create(self->mode); |
---|
| 117 | self->encoder = celt_encoder_create(self->mode); |
---|
[4152] | 118 | #endif |
---|
[325] | 119 | } else { |
---|
| 120 | celt_mode_destroy(self->mode); |
---|
| 121 | free(self); |
---|
| 122 | return -1; |
---|
| 123 | } |
---|
[324] | 124 | |
---|
| 125 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
| 126 | |
---|
[326] | 127 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
| 128 | s->info.bits = 16; // CELT hardcoded |
---|
[324] | 129 | |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | int cf_celt_close(CODECFILTER_USERDATA_T inst) { |
---|
| 134 | struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst; |
---|
| 135 | |
---|
| 136 | if ( !inst ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
[325] | 139 | if ( self->encoder ) |
---|
[2200] | 140 | celt_encoder_destroy(self->encoder); |
---|
[325] | 141 | |
---|
| 142 | if ( self->decoder ) |
---|
[2200] | 143 | celt_decoder_destroy(self->decoder); |
---|
[325] | 144 | |
---|
| 145 | if ( self->mode ) |
---|
| 146 | celt_mode_destroy(self->mode); |
---|
| 147 | |
---|
[327] | 148 | if ( self->ibuf ) |
---|
| 149 | free(self->ibuf); |
---|
| 150 | |
---|
| 151 | if ( self->obuf ) |
---|
| 152 | free(self->obuf); |
---|
| 153 | |
---|
[331] | 154 | if ( self->i_rest ) |
---|
| 155 | free(self->i_rest); |
---|
| 156 | |
---|
| 157 | if ( self->o_rest ) |
---|
| 158 | free(self->o_rest); |
---|
[327] | 159 | |
---|
[324] | 160 | free(inst); |
---|
| 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | int cf_celt_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 165 | struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst; |
---|
[328] | 166 | int r = 0; |
---|
| 167 | uint16_t fs; |
---|
[331] | 168 | char * cbuf; |
---|
[635] | 169 | char magic[ROAR_CELT_MAGIC_LEN]; |
---|
[331] | 170 | |
---|
| 171 | // printf("buf=%p, len=%i\n", buf, len); |
---|
[324] | 172 | |
---|
[2120] | 173 | if ( !self->opened_decoder ) { |
---|
[635] | 174 | errno = ENOSYS; |
---|
| 175 | if ( stream_vio_s_read(self->stream, magic, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN ) |
---|
| 176 | return -1; |
---|
| 177 | if ( memcmp(magic, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != 0 ) |
---|
| 178 | return -1; |
---|
| 179 | |
---|
| 180 | errno = 0; |
---|
[2120] | 181 | self->opened_decoder = 1; |
---|
[635] | 182 | } |
---|
| 183 | |
---|
[332] | 184 | if ( self->fi_rest ) { |
---|
| 185 | memcpy(buf, self->i_rest, self->fi_rest); |
---|
| 186 | r += self->fi_rest; |
---|
| 187 | self->fi_rest = 0; |
---|
| 188 | } |
---|
| 189 | |
---|
[328] | 190 | while ( r <= (len - self->s_buf) ) { |
---|
[608] | 191 | if ( stream_vio_s_read(self->stream, &fs, 2) != 2 ) |
---|
[328] | 192 | break; |
---|
| 193 | |
---|
| 194 | fs = ROAR_NET2HOST16(fs); |
---|
| 195 | |
---|
[2165] | 196 | // ROAR_WARN("0:fs=%i", fs); |
---|
| 197 | |
---|
[637] | 198 | if ( fs > self->s_buf ) |
---|
[638] | 199 | return -1; |
---|
[637] | 200 | |
---|
[608] | 201 | if ( stream_vio_s_read(self->stream, self->ibuf, fs) != fs ) |
---|
[638] | 202 | return -1; |
---|
[328] | 203 | |
---|
[331] | 204 | cbuf = buf + r; |
---|
| 205 | |
---|
[332] | 206 | // printf("buf=%p, r=%i // cbuf=%p\n", buf, r, cbuf); |
---|
[331] | 207 | if ( celt_decode(self->decoder, (unsigned char *) self->ibuf, fs, (celt_int16_t *) cbuf) < 0 ) |
---|
[638] | 208 | return -1; |
---|
[328] | 209 | |
---|
| 210 | r += self->s_buf; |
---|
| 211 | } |
---|
| 212 | |
---|
[332] | 213 | if ( r < len ) { |
---|
| 214 | // printf("r < len!\n"); |
---|
[608] | 215 | if ( stream_vio_s_read(self->stream, &fs, 2) == 2 ) { |
---|
[332] | 216 | fs = ROAR_NET2HOST16(fs); |
---|
[2165] | 217 | // ROAR_WARN("1:fs=%i", fs); |
---|
[332] | 218 | // printf("next: fs=%i\n", fs); |
---|
[638] | 219 | if ( fs > self->s_buf ) |
---|
| 220 | return -1; |
---|
[608] | 221 | if ( stream_vio_s_read(self->stream, self->ibuf, fs) == fs ) { |
---|
[332] | 222 | // printf("got data!\n"); |
---|
| 223 | if ( celt_decode(self->decoder, (unsigned char *) self->ibuf, fs, (celt_int16_t *) self->obuf) >= 0 ) { |
---|
| 224 | // printf("{ // decode rest\n"); |
---|
| 225 | // printf(" r=%i // need %i Bytes\n", r, len - r); |
---|
| 226 | // printf(" memcpy(buf+%i, self->obuf, %i) = ?\n", r, len - r); |
---|
| 227 | memcpy(buf+r, self->obuf, len - r); |
---|
| 228 | self->fi_rest = self->s_buf + r - len; |
---|
| 229 | memcpy(self->i_rest, self->obuf + len - r, self->fi_rest); |
---|
| 230 | // printf(" len=%i, r=%i, fi_rest=%i, s_buf=%i\n", len, r, self->fi_rest, self->s_buf); |
---|
| 231 | r = len; |
---|
| 232 | // printf("}\n"); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
[333] | 238 | ROAR_DBG("cf_celt_read(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, r); |
---|
[328] | 239 | return r; |
---|
[324] | 240 | } |
---|
| 241 | |
---|
[2204] | 242 | #define BS (ROAR_STREAM(self->stream)->info.channels * 32) |
---|
[324] | 243 | int cf_celt_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 244 | struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst; |
---|
[611] | 245 | int have = 0; |
---|
| 246 | int org_len = len; |
---|
| 247 | int diff; |
---|
| 248 | int fs2 = self->frame_size * 2 * ROAR_STREAM(self->stream)->info.channels; |
---|
[2816] | 249 | int sid; |
---|
[611] | 250 | uint16_t pkglen_net, pkglen; |
---|
| 251 | unsigned char cbits[BS+2]; |
---|
[2816] | 252 | void * prethru; |
---|
[324] | 253 | |
---|
[2120] | 254 | if ( !self->opened_encoder ) { |
---|
[2816] | 255 | sid = ROAR_STREAM(self->stream)->id; |
---|
| 256 | |
---|
| 257 | if ( stream_prethru_destroy(sid) == -1 ) { |
---|
| 258 | return -1; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | if ( stream_prethru_add_data(sid, &prethru, ROAR_CELT_MAGIC_LEN) == -1 ) { |
---|
| 262 | return -1; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | memcpy(prethru, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN); |
---|
| 266 | |
---|
[639] | 267 | if ( stream_vio_s_write(self->stream, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN ) |
---|
| 268 | return -1; |
---|
[2120] | 269 | self->opened_encoder = 1; |
---|
[639] | 270 | } |
---|
| 271 | |
---|
[611] | 272 | if ( (self->fo_rest + len) > fs2 ) { |
---|
| 273 | if ( self->fo_rest ) { |
---|
| 274 | memcpy(self->obuf, self->o_rest, self->fo_rest); |
---|
| 275 | have = self->fo_rest; |
---|
| 276 | self->fo_rest = 0; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | memcpy(self->obuf+have, buf, (diff=fs2-have)); |
---|
| 280 | buf += diff; |
---|
| 281 | len -= diff; |
---|
| 282 | |
---|
[967] | 283 | pkglen = celt_encode(self->encoder, (celt_int16_t *) self->obuf, NULL, cbits+2, BS); |
---|
[611] | 284 | pkglen_net = ROAR_HOST2NET16(pkglen); |
---|
| 285 | *(uint16_t*)cbits = pkglen_net; |
---|
| 286 | |
---|
| 287 | if ( stream_vio_s_write(self->stream, cbits, pkglen+2) == -1 ) |
---|
| 288 | return -1; |
---|
| 289 | |
---|
| 290 | while (len >= fs2) { |
---|
[967] | 291 | pkglen = celt_encode(self->encoder, (celt_int16_t *) buf, NULL, cbits+2, BS); |
---|
[611] | 292 | pkglen_net = ROAR_HOST2NET16(pkglen); |
---|
| 293 | *(uint16_t*)cbits = pkglen_net; |
---|
| 294 | |
---|
| 295 | if ( stream_vio_s_write(self->stream, cbits, pkglen+2) == -1 ) |
---|
| 296 | return -1; |
---|
| 297 | len -= fs2; |
---|
| 298 | buf += fs2; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | if ( len ) { |
---|
| 303 | memcpy(self->o_rest + self->fo_rest, buf, len); |
---|
| 304 | self->fo_rest += len; |
---|
| 305 | len = 0; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | return org_len; |
---|
[324] | 309 | } |
---|
| 310 | |
---|
[1144] | 311 | int cf_celt_delay(CODECFILTER_USERDATA_T inst, uint_least32_t * delay) { |
---|
| 312 | struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst; |
---|
| 313 | |
---|
| 314 | ROAR_DBG("cf_celt_delay(*) = ?"); |
---|
| 315 | |
---|
| 316 | if ( self == NULL ) { |
---|
| 317 | *delay = (1000000 * 256) / ROAR_RATE_DEFAULT; |
---|
| 318 | return 0; |
---|
| 319 | } else { |
---|
| 320 | *delay = (1000000 * self->frame_size) / ROAR_STREAM(self->stream)->info.rate; |
---|
| 321 | ROAR_DBG("cf_celt_delay(*): frame_size=%i, rate=%i, *delay=%i", |
---|
| 322 | self->frame_size, ROAR_STREAM(self->stream)->info.rate, *delay); |
---|
| 323 | return 0; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | return -1; |
---|
| 327 | } |
---|
| 328 | |
---|
[361] | 329 | #endif |
---|
[324] | 330 | //ll |
---|