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