[2168] | 1 | //transcode.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[2168] | 5 | * |
---|
[2169] | 6 | * This file is part of libroardsp a part of RoarAudio, |
---|
[2168] | 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 | * |
---|
[2169] | 14 | * libroardsp is distributed in the hope that it will be useful, |
---|
[2168] | 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. |
---|
[2168] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[2169] | 26 | #include "libroardsp.h" |
---|
[2168] | 27 | |
---|
[2182] | 28 | #define _FUNC(func) (state->entry->func) |
---|
| 29 | #define _CHECK_STATE() (!(state == NULL || state->entry == NULL)) |
---|
| 30 | #define _CHECK_FUNC(func) (_CHECK_STATE() && _FUNC(func) != NULL) |
---|
[2174] | 31 | #define _CHECK_BASIC(func) if ( !_CHECK_FUNC(func) ) return -1 |
---|
[2182] | 32 | #define _CHECK() if ( !_CHECK_STATE() ) return -1 |
---|
[2174] | 33 | |
---|
| 34 | static struct roar_xcoder_entry g_xcoders[] = { |
---|
| 35 | {ROAR_CODEC_ALAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any, |
---|
[2922] | 36 | roar_xcoder_alaw_encode, roar_xcoder_alaw_decode, roar_xcoder_dummy_proc_header}, |
---|
[2174] | 37 | {ROAR_CODEC_MULAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any, |
---|
[2922] | 38 | roar_xcoder_mulaw_encode, roar_xcoder_mulaw_decode, roar_xcoder_dummy_proc_header}, |
---|
[2214] | 39 | #ifdef ROAR_HAVE_LIBCELT |
---|
[2189] | 40 | {ROAR_CODEC_ROAR_CELT, roar_xcoder_celt_init, roar_xcoder_celt_uninit, roar_xcoder_celt_packet_size, |
---|
[2921] | 41 | roar_xcoder_celt_encode, roar_xcoder_celt_decode, NULL}, |
---|
[2214] | 42 | #endif |
---|
[2297] | 43 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 44 | {ROAR_CODEC_ROAR_SPEEX, roar_xcoder_speex_init, roar_xcoder_speex_uninit, roar_xcoder_speex_packet_size, |
---|
[2923] | 45 | roar_xcoder_speex_encode, roar_xcoder_speex_decode, roar_xcoder_speex_proc_header}, |
---|
[2297] | 46 | #endif |
---|
[2921] | 47 | {-1, NULL, NULL, NULL, NULL, NULL, NULL} |
---|
[2174] | 48 | }; |
---|
| 49 | |
---|
[2176] | 50 | int roar_xcoder_init(struct roar_xcoder * state, int encoder, struct roar_audio_info * info, struct roar_vio_calls * vio) { |
---|
| 51 | int i; |
---|
| 52 | |
---|
| 53 | if ( state == NULL || info == NULL ) |
---|
| 54 | return -1; |
---|
| 55 | |
---|
| 56 | memset(state, 0, sizeof(struct roar_xcoder)); |
---|
| 57 | |
---|
| 58 | for (i = 0; g_xcoders[i].codec != -1; i++) { |
---|
[3634] | 59 | if ( g_xcoders[i].codec == (int)info->codec ) { |
---|
[2176] | 60 | state->entry = &(g_xcoders[i]); |
---|
| 61 | break; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if ( state->entry == NULL ) |
---|
| 66 | return -1; |
---|
| 67 | |
---|
| 68 | state->stage = ROAR_XCODER_STAGE_NONE; |
---|
| 69 | state->encode = encoder; |
---|
| 70 | state->packet_len = -1; |
---|
| 71 | |
---|
| 72 | if ( roar_xcoder_set_backend(state, vio) == -1 ) |
---|
| 73 | return -1; |
---|
| 74 | |
---|
| 75 | memcpy(&(state->info.coded), info, sizeof(struct roar_audio_info)); |
---|
| 76 | memcpy(&(state->info.pcm ), info, sizeof(struct roar_audio_info)); |
---|
| 77 | |
---|
| 78 | state->info.pcm.codec = ROAR_CODEC_DEFAULT; |
---|
| 79 | |
---|
| 80 | if ( _FUNC(init) == NULL ) |
---|
| 81 | return -1; |
---|
| 82 | |
---|
| 83 | if ( _FUNC(init)(state) != 0 ) |
---|
| 84 | return -1; |
---|
| 85 | |
---|
| 86 | state->stage = ROAR_XCODER_STAGE_INITED; |
---|
| 87 | |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | |
---|
[2174] | 91 | int roar_xcoder_set_backend(struct roar_xcoder * state, struct roar_vio_calls * vio) { |
---|
| 92 | _CHECK(); |
---|
| 93 | |
---|
| 94 | if ( vio == NULL && state->backend != NULL ) |
---|
| 95 | return -1; |
---|
| 96 | |
---|
| 97 | state->backend = vio; |
---|
| 98 | |
---|
| 99 | return 0; |
---|
| 100 | } |
---|
| 101 | int roar_xcoder_packet_size(struct roar_xcoder * state, int samples) { |
---|
| 102 | _CHECK_BASIC(packet_size); |
---|
| 103 | |
---|
| 104 | state->packet_len = state->entry->packet_size(state, samples); |
---|
| 105 | |
---|
| 106 | if ( state->packet_len == 0 ) { |
---|
| 107 | if ( samples < 1 ) { |
---|
| 108 | return ROAR_RATE_DEFAULT/100; |
---|
| 109 | } else { |
---|
| 110 | return samples; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | return state->packet_len; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | int roar_xcoder_close (struct roar_xcoder * state) { |
---|
| 118 | _CHECK_BASIC(uninit); |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | if ( state->iobuffer != NULL ) { |
---|
| 122 | roar_xcoder_proc(state, NULL, 0); // try to flush |
---|
| 123 | roar_buffer_free(state->iobuffer); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return _FUNC(uninit)(state); |
---|
| 127 | } |
---|
| 128 | |
---|
[2921] | 129 | int roar_xcoder_proc_header(struct roar_xcoder * state) { |
---|
| 130 | _CHECK_BASIC(proc_header); |
---|
| 131 | |
---|
| 132 | return _FUNC(proc_header)(state); |
---|
| 133 | } |
---|
| 134 | |
---|
[2176] | 135 | int roar_xcoder_proc_packet(struct roar_xcoder * state, void * buf, size_t len) { |
---|
| 136 | _CHECK(); |
---|
| 137 | |
---|
[2311] | 138 | ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 139 | |
---|
[2180] | 140 | if ( state->backend == NULL ) |
---|
| 141 | return -1; |
---|
| 142 | |
---|
[3634] | 143 | if ( state->packet_len > 0 && state->packet_len != (ssize_t)len ) |
---|
[2177] | 144 | return -1; |
---|
| 145 | |
---|
[2643] | 146 | ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 147 | |
---|
[2176] | 148 | if ( state->encode ) { |
---|
| 149 | _CHECK_BASIC(encode); |
---|
[2643] | 150 | ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ? // _CHECK_BASIC(encode) -> OK", state, buf, (unsigned long)len); |
---|
[2176] | 151 | return _FUNC(encode)(state, buf, len); |
---|
| 152 | } else { |
---|
| 153 | _CHECK_BASIC(decode); |
---|
[2643] | 154 | ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ? // _CHECK_BASIC(decode) -> OK", state, buf, (unsigned long)len); |
---|
[2176] | 155 | return _FUNC(decode)(state, buf, len); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
[2174] | 159 | int roar_xcoder_proc (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2637] | 160 | struct roar_buffer_stats ringstats; |
---|
| 161 | struct roar_buffer * bufbuf; |
---|
| 162 | void * bufdata; |
---|
| 163 | size_t curlen; |
---|
| 164 | |
---|
[2643] | 165 | ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 166 | |
---|
[2637] | 167 | if ( state == NULL ) |
---|
| 168 | return -1; |
---|
| 169 | |
---|
| 170 | if ( buf == NULL && len != 0 ) |
---|
| 171 | return -1; |
---|
| 172 | |
---|
| 173 | if ( state->packet_len == -1 ) |
---|
| 174 | if ( roar_xcoder_packet_size(state, -1) == -1 ) |
---|
| 175 | return -1; |
---|
| 176 | |
---|
[2643] | 177 | ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 178 | |
---|
[2637] | 179 | if ( state->packet_len == 0 ) |
---|
| 180 | return roar_xcoder_proc_packet(state, buf, len); |
---|
| 181 | |
---|
[2643] | 182 | ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu): state->packet_len=%li", state, buf, (unsigned long)len, (long int) state->packet_len); |
---|
| 183 | |
---|
[2637] | 184 | if ( state->iobuffer == NULL ) { |
---|
[3634] | 185 | while ( (ssize_t)len >= state->packet_len ) { |
---|
[2643] | 186 | if ( roar_xcoder_proc_packet(state, buf, state->packet_len) == -1 ) { |
---|
| 187 | ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = -1 // roar_xcoder_proc_packet() error", state, buf, (unsigned long)len); |
---|
[2637] | 188 | return -1; |
---|
[2643] | 189 | } |
---|
[2637] | 190 | |
---|
| 191 | buf += state->packet_len; |
---|
| 192 | len -= state->packet_len; |
---|
| 193 | } |
---|
| 194 | |
---|
[2643] | 195 | if ( !len ) { |
---|
| 196 | ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len); |
---|
[2637] | 197 | return 0; |
---|
[2643] | 198 | } |
---|
| 199 | |
---|
| 200 | ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
[2637] | 201 | |
---|
| 202 | if ( state->encode ) { |
---|
| 203 | curlen = len; |
---|
| 204 | } else { |
---|
| 205 | curlen = state->packet_len; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | if ( roar_buffer_new(&bufbuf, curlen) == -1 ) |
---|
| 209 | return -1; |
---|
| 210 | |
---|
| 211 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) { |
---|
| 212 | roar_buffer_free(bufbuf); |
---|
| 213 | return -1; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if ( state->encode ) { |
---|
| 217 | memcpy(bufdata, buf, len); |
---|
| 218 | } else { |
---|
| 219 | if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) { |
---|
| 220 | roar_buffer_free(bufbuf); |
---|
| 221 | return -1; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | curlen = len; |
---|
| 225 | |
---|
| 226 | if ( roar_buffer_shift_out(&bufbuf, buf, &curlen) == -1 ) { |
---|
| 227 | roar_buffer_free(bufbuf); |
---|
| 228 | return -1; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | if ( curlen < len ) { // this should never happen! |
---|
| 232 | roar_buffer_free(bufbuf); |
---|
| 233 | return -1; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | state->iobuffer = bufbuf; |
---|
| 238 | } else { |
---|
| 239 | if ( state->encode ) { |
---|
| 240 | if ( roar_buffer_new(&bufbuf, len) == -1 ) |
---|
| 241 | return -1; |
---|
| 242 | |
---|
| 243 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) { |
---|
| 244 | roar_buffer_free(bufbuf); |
---|
| 245 | return -1; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | memcpy(bufdata, buf, len); |
---|
| 249 | |
---|
| 250 | if ( roar_buffer_add(state->iobuffer, bufbuf) == -1 ) { |
---|
| 251 | roar_buffer_free(bufbuf); |
---|
| 252 | return -1; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 ) |
---|
| 256 | return -1; |
---|
| 257 | |
---|
| 258 | if ( roar_buffer_new(&bufbuf, state->packet_len) == -1 ) |
---|
| 259 | return -1; |
---|
| 260 | |
---|
| 261 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) { |
---|
| 262 | roar_buffer_free(bufbuf); |
---|
| 263 | return -1; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | while ( ringstats.bytes > state->packet_len ) { |
---|
| 267 | curlen = state->packet_len; |
---|
| 268 | if ( roar_buffer_shift_out(&(state->iobuffer), bufdata, &curlen) == -1 ) { |
---|
| 269 | roar_buffer_free(bufbuf); |
---|
| 270 | return -1; |
---|
| 271 | } |
---|
| 272 | |
---|
[3634] | 273 | if ( (ssize_t)curlen < state->packet_len ) { // this should not happen... |
---|
[2637] | 274 | roar_buffer_free(bufbuf); |
---|
| 275 | return -1; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) { |
---|
| 279 | roar_buffer_free(bufbuf); |
---|
| 280 | return -1; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 ) { |
---|
| 284 | roar_buffer_free(bufbuf); |
---|
| 285 | return -1; |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if ( roar_buffer_free(bufbuf) == -1 ) |
---|
| 290 | return -1; |
---|
| 291 | } else { |
---|
| 292 | curlen = len; |
---|
| 293 | |
---|
| 294 | if ( roar_buffer_shift_out(&(state->iobuffer), buf, &curlen) == -1 ) { |
---|
| 295 | return -1; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | if ( curlen == len ) |
---|
| 299 | return -1; |
---|
| 300 | |
---|
| 301 | // we now have curlen < len and state->iobuffer == NULL |
---|
| 302 | // as no data is left in the buffer, need to get some new data. |
---|
| 303 | // we simply call ourself to get some more data... |
---|
| 304 | |
---|
| 305 | if ( state->iobuffer == NULL ) { |
---|
| 306 | ROAR_WARN("roar_xcoder_proc(state=%p, buf=%p, len=%lu): iobuffer != NULL, " |
---|
| 307 | "This is a bug in libroar{dsp,} or some hardware is broken", |
---|
| 308 | state, buf, (unsigned long)len); |
---|
| 309 | return -1; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | len -= curlen; |
---|
| 313 | buf += curlen; |
---|
| 314 | |
---|
| 315 | return roar_xcoder_proc(state, buf, len); |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | return 0; |
---|
[2174] | 320 | } |
---|
| 321 | |
---|
[2178] | 322 | int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) { |
---|
| 323 | if ( state == NULL || info == NULL || vio == NULL ) |
---|
| 324 | return -1; |
---|
| 325 | |
---|
| 326 | memset(state, 0, sizeof(struct roar_bixcoder)); |
---|
| 327 | |
---|
| 328 | if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 ) |
---|
| 329 | return -1; |
---|
| 330 | |
---|
| 331 | if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) { |
---|
| 332 | roar_xcoder_close(&(state->encoder)); |
---|
| 333 | return -1; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | return 0; |
---|
| 337 | } |
---|
| 338 | |
---|
[2184] | 339 | int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples) { |
---|
| 340 | int ret; |
---|
| 341 | |
---|
[2311] | 342 | ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = ?", state, samples); |
---|
| 343 | |
---|
[2184] | 344 | if ( state == NULL ) |
---|
| 345 | return -1; |
---|
| 346 | |
---|
| 347 | if ( (ret = roar_xcoder_packet_size(&(state->encoder), samples)) == -1 ) |
---|
| 348 | return -1; |
---|
| 349 | |
---|
[2311] | 350 | ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i): ret=%i", state, samples, ret); |
---|
| 351 | |
---|
| 352 | // TODO: we need a lot hope here... |
---|
| 353 | /* |
---|
[2184] | 354 | if ( roar_xcoder_packet_size(&(state->decoder), ret) != ret ) |
---|
| 355 | return -1; |
---|
[2311] | 356 | */ |
---|
[2184] | 357 | |
---|
[2311] | 358 | ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = %i", state, samples, ret); |
---|
[2184] | 359 | return ret; |
---|
| 360 | } |
---|
| 361 | |
---|
[2178] | 362 | int roar_bixcoder_close (struct roar_bixcoder * state) { |
---|
| 363 | int ret = 0; |
---|
| 364 | |
---|
| 365 | if ( state == NULL ) |
---|
| 366 | return -1; |
---|
| 367 | |
---|
| 368 | ret = roar_xcoder_close(&(state->encoder)); |
---|
| 369 | |
---|
| 370 | if ( roar_xcoder_close(&(state->decoder)) == -1 ) |
---|
| 371 | return -1; |
---|
| 372 | |
---|
| 373 | return ret; |
---|
| 374 | } |
---|
| 375 | |
---|
[2921] | 376 | int roar_bixcoder_read_header (struct roar_bixcoder * state) { |
---|
| 377 | if ( state == NULL ) |
---|
| 378 | return -1; |
---|
| 379 | |
---|
| 380 | return roar_xcoder_proc_header(&(state->decoder)); |
---|
| 381 | } |
---|
| 382 | |
---|
[2178] | 383 | int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
[2311] | 384 | |
---|
| 385 | ROAR_DBG("roar_bixcoder_read_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 386 | |
---|
[2178] | 387 | if ( state == NULL ) |
---|
| 388 | return -1; |
---|
| 389 | |
---|
| 390 | return roar_xcoder_proc_packet(&(state->decoder), buf, len); |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | int roar_bixcoder_read (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 394 | if ( state == NULL ) |
---|
| 395 | return -1; |
---|
| 396 | |
---|
| 397 | return roar_xcoder_proc(&(state->decoder), buf, len); |
---|
| 398 | } |
---|
| 399 | |
---|
[2921] | 400 | int roar_bixcoder_write_header(struct roar_bixcoder * state) { |
---|
| 401 | if ( state == NULL ) |
---|
| 402 | return -1; |
---|
| 403 | |
---|
| 404 | return roar_xcoder_proc_header(&(state->decoder)); |
---|
| 405 | } |
---|
| 406 | |
---|
[2178] | 407 | int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 408 | if ( state == NULL ) |
---|
| 409 | return -1; |
---|
| 410 | |
---|
| 411 | return roar_xcoder_proc_packet(&(state->encoder), buf, len); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | int roar_bixcoder_write (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 415 | if ( state == NULL ) |
---|
| 416 | return -1; |
---|
| 417 | |
---|
| 418 | return roar_xcoder_proc(&(state->encoder), buf, len); |
---|
| 419 | } |
---|
[2174] | 420 | |
---|
| 421 | // dummy functions used by some de/encoders: |
---|
| 422 | int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) { |
---|
| 423 | return 0; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) { |
---|
| 427 | // the case samples=-1/samples!=-1 based things are done in the general func |
---|
| 428 | return 0; |
---|
| 429 | } |
---|
[2168] | 430 | |
---|
[2922] | 431 | int roar_xcoder_dummy_proc_header(struct roar_xcoder * state) { |
---|
| 432 | return 0; |
---|
| 433 | } |
---|
| 434 | |
---|
[2168] | 435 | //ll |
---|