[2168] | 1 | //transcode.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013 |
---|
[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 | |
---|
[5300] | 208 | if ( roar_buffer_new_data(&bufbuf, curlen, &bufdata) == -1 ) |
---|
[2637] | 209 | return -1; |
---|
| 210 | |
---|
| 211 | if ( state->encode ) { |
---|
| 212 | memcpy(bufdata, buf, len); |
---|
| 213 | } else { |
---|
| 214 | if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) { |
---|
| 215 | roar_buffer_free(bufbuf); |
---|
| 216 | return -1; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | curlen = len; |
---|
| 220 | |
---|
| 221 | if ( roar_buffer_shift_out(&bufbuf, buf, &curlen) == -1 ) { |
---|
| 222 | roar_buffer_free(bufbuf); |
---|
| 223 | return -1; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | if ( curlen < len ) { // this should never happen! |
---|
| 227 | roar_buffer_free(bufbuf); |
---|
| 228 | return -1; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | state->iobuffer = bufbuf; |
---|
| 233 | } else { |
---|
| 234 | if ( state->encode ) { |
---|
[5300] | 235 | if ( roar_buffer_new_data(&bufbuf, len, &bufdata) == -1 ) |
---|
[2637] | 236 | return -1; |
---|
| 237 | |
---|
| 238 | memcpy(bufdata, buf, len); |
---|
| 239 | |
---|
[5300] | 240 | if ( roar_buffer_moveinto(state->iobuffer, &bufbuf) == -1 ) { |
---|
[2637] | 241 | roar_buffer_free(bufbuf); |
---|
| 242 | return -1; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 ) |
---|
| 246 | return -1; |
---|
| 247 | |
---|
[5300] | 248 | if ( roar_buffer_new_data(&bufbuf, state->packet_len, &bufdata) == -1 ) |
---|
[2637] | 249 | return -1; |
---|
| 250 | |
---|
[5270] | 251 | while ( (ssize_t)ringstats.bytes > state->packet_len ) { |
---|
[2637] | 252 | curlen = state->packet_len; |
---|
| 253 | if ( roar_buffer_shift_out(&(state->iobuffer), bufdata, &curlen) == -1 ) { |
---|
| 254 | roar_buffer_free(bufbuf); |
---|
| 255 | return -1; |
---|
| 256 | } |
---|
| 257 | |
---|
[3634] | 258 | if ( (ssize_t)curlen < state->packet_len ) { // this should not happen... |
---|
[2637] | 259 | roar_buffer_free(bufbuf); |
---|
| 260 | return -1; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) { |
---|
| 264 | roar_buffer_free(bufbuf); |
---|
| 265 | return -1; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 ) { |
---|
| 269 | roar_buffer_free(bufbuf); |
---|
| 270 | return -1; |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | if ( roar_buffer_free(bufbuf) == -1 ) |
---|
| 275 | return -1; |
---|
| 276 | } else { |
---|
| 277 | curlen = len; |
---|
| 278 | |
---|
| 279 | if ( roar_buffer_shift_out(&(state->iobuffer), buf, &curlen) == -1 ) { |
---|
| 280 | return -1; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | if ( curlen == len ) |
---|
| 284 | return -1; |
---|
| 285 | |
---|
| 286 | // we now have curlen < len and state->iobuffer == NULL |
---|
| 287 | // as no data is left in the buffer, need to get some new data. |
---|
| 288 | // we simply call ourself to get some more data... |
---|
| 289 | |
---|
| 290 | if ( state->iobuffer == NULL ) { |
---|
| 291 | ROAR_WARN("roar_xcoder_proc(state=%p, buf=%p, len=%lu): iobuffer != NULL, " |
---|
| 292 | "This is a bug in libroar{dsp,} or some hardware is broken", |
---|
| 293 | state, buf, (unsigned long)len); |
---|
| 294 | return -1; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | len -= curlen; |
---|
| 298 | buf += curlen; |
---|
| 299 | |
---|
| 300 | return roar_xcoder_proc(state, buf, len); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | return 0; |
---|
[2174] | 305 | } |
---|
| 306 | |
---|
[2178] | 307 | int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) { |
---|
| 308 | if ( state == NULL || info == NULL || vio == NULL ) |
---|
| 309 | return -1; |
---|
| 310 | |
---|
| 311 | memset(state, 0, sizeof(struct roar_bixcoder)); |
---|
| 312 | |
---|
| 313 | if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 ) |
---|
| 314 | return -1; |
---|
| 315 | |
---|
| 316 | if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) { |
---|
| 317 | roar_xcoder_close(&(state->encoder)); |
---|
| 318 | return -1; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | return 0; |
---|
| 322 | } |
---|
| 323 | |
---|
[2184] | 324 | int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples) { |
---|
| 325 | int ret; |
---|
| 326 | |
---|
[2311] | 327 | ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = ?", state, samples); |
---|
| 328 | |
---|
[2184] | 329 | if ( state == NULL ) |
---|
| 330 | return -1; |
---|
| 331 | |
---|
| 332 | if ( (ret = roar_xcoder_packet_size(&(state->encoder), samples)) == -1 ) |
---|
| 333 | return -1; |
---|
| 334 | |
---|
[2311] | 335 | ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i): ret=%i", state, samples, ret); |
---|
| 336 | |
---|
| 337 | // TODO: we need a lot hope here... |
---|
| 338 | /* |
---|
[2184] | 339 | if ( roar_xcoder_packet_size(&(state->decoder), ret) != ret ) |
---|
| 340 | return -1; |
---|
[2311] | 341 | */ |
---|
[2184] | 342 | |
---|
[2311] | 343 | ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = %i", state, samples, ret); |
---|
[2184] | 344 | return ret; |
---|
| 345 | } |
---|
| 346 | |
---|
[2178] | 347 | int roar_bixcoder_close (struct roar_bixcoder * state) { |
---|
| 348 | int ret = 0; |
---|
| 349 | |
---|
| 350 | if ( state == NULL ) |
---|
| 351 | return -1; |
---|
| 352 | |
---|
| 353 | ret = roar_xcoder_close(&(state->encoder)); |
---|
| 354 | |
---|
| 355 | if ( roar_xcoder_close(&(state->decoder)) == -1 ) |
---|
| 356 | return -1; |
---|
| 357 | |
---|
| 358 | return ret; |
---|
| 359 | } |
---|
| 360 | |
---|
[2921] | 361 | int roar_bixcoder_read_header (struct roar_bixcoder * state) { |
---|
| 362 | if ( state == NULL ) |
---|
| 363 | return -1; |
---|
| 364 | |
---|
| 365 | return roar_xcoder_proc_header(&(state->decoder)); |
---|
| 366 | } |
---|
| 367 | |
---|
[2178] | 368 | int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
[2311] | 369 | |
---|
| 370 | ROAR_DBG("roar_bixcoder_read_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len); |
---|
| 371 | |
---|
[2178] | 372 | if ( state == NULL ) |
---|
| 373 | return -1; |
---|
| 374 | |
---|
| 375 | return roar_xcoder_proc_packet(&(state->decoder), buf, len); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | int roar_bixcoder_read (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 379 | if ( state == NULL ) |
---|
| 380 | return -1; |
---|
| 381 | |
---|
| 382 | return roar_xcoder_proc(&(state->decoder), buf, len); |
---|
| 383 | } |
---|
| 384 | |
---|
[2921] | 385 | int roar_bixcoder_write_header(struct roar_bixcoder * state) { |
---|
| 386 | if ( state == NULL ) |
---|
| 387 | return -1; |
---|
| 388 | |
---|
| 389 | return roar_xcoder_proc_header(&(state->decoder)); |
---|
| 390 | } |
---|
| 391 | |
---|
[2178] | 392 | int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 393 | if ( state == NULL ) |
---|
| 394 | return -1; |
---|
| 395 | |
---|
| 396 | return roar_xcoder_proc_packet(&(state->encoder), buf, len); |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | int roar_bixcoder_write (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 400 | if ( state == NULL ) |
---|
| 401 | return -1; |
---|
| 402 | |
---|
| 403 | return roar_xcoder_proc(&(state->encoder), buf, len); |
---|
| 404 | } |
---|
[2174] | 405 | |
---|
| 406 | // dummy functions used by some de/encoders: |
---|
| 407 | int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) { |
---|
[5270] | 408 | (void)state; |
---|
[2174] | 409 | return 0; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) { |
---|
| 413 | // the case samples=-1/samples!=-1 based things are done in the general func |
---|
[5270] | 414 | (void)state, (void)samples; |
---|
[2174] | 415 | return 0; |
---|
| 416 | } |
---|
[2168] | 417 | |
---|
[2922] | 418 | int roar_xcoder_dummy_proc_header(struct roar_xcoder * state) { |
---|
[5270] | 419 | (void)state; |
---|
[2922] | 420 | return 0; |
---|
| 421 | } |
---|
| 422 | |
---|
[2168] | 423 | //ll |
---|