| 1 |
/* |
| 2 |
* Allegro5 Opus reader. |
| 3 |
* author: Boris Carvajal (c) 2016 |
| 4 |
* Based on ogg.c code |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "allegro5/allegro.h" |
| 8 |
#include "allegro5/allegro_acodec.h" |
| 9 |
#include "allegro5/allegro_audio.h" |
| 10 |
#include "allegro5/internal/aintern.h" |
| 11 |
#include "allegro5/internal/aintern_audio.h" |
| 12 |
#include "acodec.h" |
| 13 |
#include "helper.h" |
| 14 |
|
| 15 |
#ifndef ALLEGRO_CFG_ACODEC_OPUS |
| 16 |
#error configuration problem, ALLEGRO_CFG_ACODEC_OPUS not set |
| 17 |
#endif |
| 18 |
|
| 19 |
ALLEGRO_DEBUG_CHANNEL("acodec") |
| 20 |
|
| 21 |
#include <opus/opusfile.h> |
| 22 |
|
| 23 |
typedef struct AL_OP_DATA AL_OP_DATA; |
| 24 |
|
| 25 |
struct AL_OP_DATA { |
| 26 |
OggOpusFile *of; |
| 27 |
ALLEGRO_FILE *file; |
| 28 |
int channels; |
| 29 |
int bitstream; |
| 30 |
double loop_start; |
| 31 |
double loop_end; |
| 32 |
}; |
| 33 |
|
| 34 |
/* dynamic loading support (Windows only currently) */ |
| 35 |
#ifdef ALLEGRO_CFG_ACODEC_OPUSFILE_DLL |
| 36 |
static void *op_dll = NULL; |
| 37 |
static bool op_virgin = true; |
| 38 |
#endif |
| 39 |
|
| 40 |
static struct |
| 41 |
{ |
| 42 |
void (*op_free)(OggOpusFile *_of); |
| 43 |
int (*op_channel_count)(const OggOpusFile *_of,int _li); |
| 44 |
OggOpusFile *(*op_open_callbacks)(void *_source, const OpusFileCallbacks *_cb, const unsigned char *_initial_data, size_t _initial_bytes, int *_error); |
| 45 |
ogg_int64_t (*op_pcm_total)(const OggOpusFile *_of, int _li); |
| 46 |
int (*op_pcm_seek)(OggOpusFile *_of, ogg_int64_t _pcm_offset); |
| 47 |
ogg_int64_t (*op_pcm_tell)(const OggOpusFile *_of); |
| 48 |
int (*op_read)(OggOpusFile *_of, opus_int16 *_pcm, int _buf_size, int *_li); |
| 49 |
} lib; |
| 50 |
|
| 51 |
|
| 52 |
#ifdef ALLEGRO_CFG_ACODEC_OPUSFILE_DLL |
| 53 |
static void shutdown_dynlib(void) |
| 54 |
{ |
| 55 |
if (op_dll) { |
| 56 |
_al_close_library(op_dll); |
| 57 |
op_dll = NULL; |
| 58 |
op_virgin = true; |
| 59 |
} |
| 60 |
} |
| 61 |
#endif |
| 62 |
|
| 63 |
|
| 64 |
static bool init_dynlib(void) |
| 65 |
{ |
| 66 |
#ifdef ALLEGRO_CFG_ACODEC_OPUSFILE_DLL |
| 67 |
if (op_dll) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
if (!op_virgin) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
op_virgin = false; |
| 76 |
|
| 77 |
op_dll = _al_open_library(ALLEGRO_CFG_ACODEC_OPUSFILE_DLL); |
| 78 |
if (!op_dll) { |
| 79 |
ALLEGRO_WARN("Could not load " ALLEGRO_CFG_ACODEC_OPUSFILE_DLL "\n"); |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
_al_add_exit_func(shutdown_dynlib, "shutdown_dynlib"); |
| 84 |
|
| 85 |
#define INITSYM(x) \ |
| 86 |
do \ |
| 87 |
{ \ |
| 88 |
lib.x = _al_import_symbol(op_dll, #x); \ |
| 89 |
if (lib.x == 0) { \ |
| 90 |
ALLEGRO_ERROR("undefined symbol in lib structure: " #x "\n"); \ |
| 91 |
return false; \ |
| 92 |
} \ |
| 93 |
} while(0) |
| 94 |
#else |
| 95 |
#define INITSYM(x) (lib.x = (x)) |
| 96 |
#endif |
| 97 |
|
| 98 |
memset(&lib, 0, sizeof(lib)); |
| 99 |
|
| 100 |
INITSYM(op_free); |
| 101 |
INITSYM(op_channel_count); |
| 102 |
INITSYM(op_open_callbacks); |
| 103 |
INITSYM(op_pcm_total); |
| 104 |
INITSYM(op_pcm_seek); |
| 105 |
INITSYM(op_pcm_tell); |
| 106 |
INITSYM(op_read); |
| 107 |
|
| 108 |
return true; |
| 109 |
|
| 110 |
#undef INITSYM |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static int read_callback(void *stream, unsigned char *ptr, int nbytes) |
| 115 |
{ |
| 116 |
AL_OP_DATA *op = (AL_OP_DATA *)stream; |
| 117 |
size_t ret = 0; |
| 118 |
|
| 119 |
ret = al_fread(op->file, ptr, nbytes); |
| 120 |
|
| 121 |
return ret; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
static int seek_callback(void *stream, opus_int64 offset, int whence) |
| 126 |
{ |
| 127 |
AL_OP_DATA *op = (AL_OP_DATA *)stream; |
| 128 |
|
| 129 |
switch(whence) { |
| 130 |
case SEEK_SET: whence = ALLEGRO_SEEK_SET; break; |
| 131 |
case SEEK_CUR: whence = ALLEGRO_SEEK_CUR; break; |
| 132 |
case SEEK_END: whence = ALLEGRO_SEEK_END; break; |
| 133 |
} |
| 134 |
|
| 135 |
if (!al_fseek(op->file, offset, whence)) { |
| 136 |
return -1; |
| 137 |
} |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
static opus_int64 tell_callback(void *stream) |
| 144 |
{ |
| 145 |
AL_OP_DATA *op = (AL_OP_DATA *)stream; |
| 146 |
int64_t ret = 0; |
| 147 |
|
| 148 |
ret = al_ftell(op->file); |
| 149 |
if (ret == -1) |
| 150 |
return -1; |
| 151 |
|
| 152 |
return ret; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static int close_callback(void *stream) |
| 157 |
{ |
| 158 |
/* Don't close stream->file here. */ |
| 159 |
(void)stream; |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static OpusFileCallbacks callbacks = { |
| 165 |
read_callback, |
| 166 |
seek_callback, |
| 167 |
tell_callback, |
| 168 |
close_callback |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
ALLEGRO_SAMPLE *_al_load_ogg_opus(const char *filename) |
| 173 |
{ |
| 174 |
ALLEGRO_FILE *f; |
| 175 |
ALLEGRO_SAMPLE *spl; |
| 176 |
ASSERT(filename); |
| 177 |
|
| 178 |
ALLEGRO_INFO("Loading sample %s.\n", filename); |
| 179 |
f = al_fopen(filename, "rb"); |
| 180 |
if (!f) { |
| 181 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 182 |
return NULL; |
| 183 |
} |
| 184 |
|
| 185 |
spl = _al_load_ogg_opus_f(f); |
| 186 |
|
| 187 |
al_fclose(f); |
| 188 |
|
| 189 |
return spl; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
ALLEGRO_SAMPLE *_al_load_ogg_opus_f(ALLEGRO_FILE *file) |
| 194 |
{ |
| 195 |
/* Note: decoding library can return 16-bit or floating-point output, |
| 196 |
* both using native endian ordering. (TODO: Implement float output, |
| 197 |
* individual links in the stream...) |
| 198 |
*/ |
| 199 |
int word_size = 2; /* 2 = 16-bit. */ |
| 200 |
const int packet_size = 5760; /* suggestion for size to read at a time */ |
| 201 |
OggOpusFile *of; |
| 202 |
opus_int16 *buffer; |
| 203 |
ALLEGRO_SAMPLE *sample; |
| 204 |
int channels; |
| 205 |
long rate; |
| 206 |
ogg_int64_t total_samples; |
| 207 |
int bitstream; |
| 208 |
ogg_int64_t total_size; |
| 209 |
AL_OP_DATA op; |
| 210 |
ogg_int64_t pos; |
| 211 |
long read; |
| 212 |
|
| 213 |
if (!init_dynlib()) { |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
|
| 217 |
op.file = file; |
| 218 |
of = lib.op_open_callbacks(&op, &callbacks, NULL, 0, NULL); |
| 219 |
if (!of) { |
| 220 |
ALLEGRO_ERROR("Audio file does not appear to be an Ogg bitstream.\n"); |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
|
| 224 |
bitstream = -1; |
| 225 |
channels = lib.op_channel_count(of, bitstream); |
| 226 |
rate = 48000; |
| 227 |
total_samples = lib.op_pcm_total(of, bitstream); |
| 228 |
total_size = total_samples * channels * word_size; |
| 229 |
|
| 230 |
ALLEGRO_DEBUG("channels %d\n", channels); |
| 231 |
ALLEGRO_DEBUG("word_size %d\n", word_size); |
| 232 |
ALLEGRO_DEBUG("rate %ld\n", rate); |
| 233 |
ALLEGRO_DEBUG("total_samples %ld\n", (long)total_samples); |
| 234 |
ALLEGRO_DEBUG("total_size %ld\n", (long)total_size); |
| 235 |
|
| 236 |
buffer = al_malloc(total_size); |
| 237 |
if (!buffer) { |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
pos = 0; |
| 242 |
while (pos < total_samples) { |
| 243 |
const int read_size = _ALLEGRO_MIN(packet_size, total_samples - pos); |
| 244 |
ASSERT(pos + read_size <= total_samples); |
| 245 |
|
| 246 |
/* XXX error handling */ |
| 247 |
read = lib.op_read(of, buffer + pos * channels, read_size, NULL); |
| 248 |
|
| 249 |
pos += read; |
| 250 |
if (read == 0) |
| 251 |
break; |
| 252 |
} |
| 253 |
|
| 254 |
lib.op_free(of); |
| 255 |
|
| 256 |
sample = al_create_sample(buffer, total_samples, rate, |
| 257 |
_al_word_size_to_depth_conf(word_size), |
| 258 |
_al_count_to_channel_conf(channels), true); |
| 259 |
|
| 260 |
if (!sample) { |
| 261 |
al_free(buffer); |
| 262 |
} |
| 263 |
|
| 264 |
return sample; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
static bool ogg_stream_seek(ALLEGRO_AUDIO_STREAM *stream, double time) |
| 269 |
{ |
| 270 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 271 |
|
| 272 |
if (time >= extra->loop_end) |
| 273 |
return false; |
| 274 |
|
| 275 |
return !lib.op_pcm_seek(extra->of, time * 48000);; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
static bool ogg_stream_rewind(ALLEGRO_AUDIO_STREAM *stream) |
| 280 |
{ |
| 281 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 282 |
|
| 283 |
return ogg_stream_seek(stream, extra->loop_start); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
static double ogg_stream_get_position(ALLEGRO_AUDIO_STREAM *stream) |
| 288 |
{ |
| 289 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 290 |
|
| 291 |
return lib.op_pcm_tell(extra->of)/48000.0; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
static double ogg_stream_get_length(ALLEGRO_AUDIO_STREAM *stream) |
| 296 |
{ |
| 297 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 298 |
|
| 299 |
return lib.op_pcm_total(extra->of, -1)/48000.0; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
static bool ogg_stream_set_loop(ALLEGRO_AUDIO_STREAM *stream, double start, double end) |
| 304 |
{ |
| 305 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 306 |
|
| 307 |
extra->loop_start = start; |
| 308 |
extra->loop_end = end; |
| 309 |
|
| 310 |
return true; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
/* To be called when stream is destroyed */ |
| 315 |
static void ogg_stream_close(ALLEGRO_AUDIO_STREAM *stream) |
| 316 |
{ |
| 317 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 318 |
|
| 319 |
_al_acodec_stop_feed_thread(stream); |
| 320 |
|
| 321 |
al_fclose(extra->file); |
| 322 |
|
| 323 |
lib.op_free(extra->of); |
| 324 |
al_free(extra); |
| 325 |
stream->extra = NULL; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
static size_t ogg_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data, |
| 330 |
size_t buf_size) |
| 331 |
{ |
| 332 |
AL_OP_DATA *extra = (AL_OP_DATA *) stream->extra; |
| 333 |
|
| 334 |
const int word_size = 2; /* 2 = 16-bit. opus_int16 size, for use in op_read */ |
| 335 |
|
| 336 |
unsigned long pos = 0; |
| 337 |
int read_length = buf_size; |
| 338 |
int buf_in_word; |
| 339 |
long rate = 48000; |
| 340 |
int channels = extra->channels; |
| 341 |
|
| 342 |
double ctime = lib.op_pcm_tell(extra->of)/(double)rate; |
| 343 |
|
| 344 |
double btime = ((double)buf_size / (word_size * channels)) / rate; |
| 345 |
long read; |
| 346 |
|
| 347 |
if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > extra->loop_end) { |
| 348 |
read_length = (extra->loop_end - ctime) * rate * word_size * channels; |
| 349 |
if (read_length < 0) |
| 350 |
return 0; |
| 351 |
read_length += read_length % word_size; |
| 352 |
} |
| 353 |
|
| 354 |
buf_in_word= read_length/word_size; |
| 355 |
|
| 356 |
while (pos < (unsigned long) read_length) { |
| 357 |
read = lib.op_read(extra->of, (opus_int16 *)data + pos, buf_in_word - pos, NULL); |
| 358 |
pos += read * channels; |
| 359 |
|
| 360 |
if (read == 0) { |
| 361 |
/* Return the number of useful bytes written. */ |
| 362 |
return pos*word_size; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return pos; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
ALLEGRO_AUDIO_STREAM *_al_load_ogg_opus_audio_stream(const char *filename, |
| 371 |
size_t buffer_count, unsigned int samples) |
| 372 |
{ |
| 373 |
ALLEGRO_FILE *f; |
| 374 |
ALLEGRO_AUDIO_STREAM *stream; |
| 375 |
ASSERT(filename); |
| 376 |
|
| 377 |
ALLEGRO_INFO("Loading stream %s.\n", filename); |
| 378 |
f = al_fopen(filename, "rb"); |
| 379 |
if (!f) { |
| 380 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 381 |
return NULL; |
| 382 |
} |
| 383 |
|
| 384 |
stream = _al_load_ogg_opus_audio_stream_f(f, buffer_count, samples); |
| 385 |
if (!stream) { |
| 386 |
al_fclose(f); |
| 387 |
} |
| 388 |
|
| 389 |
return stream; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
ALLEGRO_AUDIO_STREAM *_al_load_ogg_opus_audio_stream_f(ALLEGRO_FILE *file, |
| 394 |
size_t buffer_count, unsigned int samples) |
| 395 |
{ |
| 396 |
const int word_size = 2; /* 1 = 8bit, 2 = 16-bit. nothing else */ |
| 397 |
OggOpusFile* of; |
| 398 |
int channels; |
| 399 |
long rate; |
| 400 |
long total_samples; |
| 401 |
long total_size; |
| 402 |
int bitstream; |
| 403 |
AL_OP_DATA* extra; |
| 404 |
ALLEGRO_AUDIO_STREAM* stream; |
| 405 |
|
| 406 |
if (!init_dynlib()) { |
| 407 |
return NULL; |
| 408 |
} |
| 409 |
|
| 410 |
extra = al_malloc(sizeof(AL_OP_DATA)); |
| 411 |
if (extra == NULL) { |
| 412 |
ALLEGRO_ERROR("Failed to allocate AL_OP_DATA struct.\n"); |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
|
| 416 |
extra->file = file; |
| 417 |
|
| 418 |
of = lib.op_open_callbacks(extra, &callbacks, NULL, 0, NULL); |
| 419 |
if (!of) { |
| 420 |
ALLEGRO_ERROR("ogg: Input does not appear to be an Ogg bitstream.\n"); |
| 421 |
return NULL; |
| 422 |
} |
| 423 |
|
| 424 |
extra->of = of; |
| 425 |
extra->bitstream = -1; |
| 426 |
bitstream = extra->bitstream; |
| 427 |
|
| 428 |
extra->channels = lib.op_channel_count(of, bitstream); |
| 429 |
channels = extra->channels; |
| 430 |
|
| 431 |
rate = 48000; |
| 432 |
total_samples = lib.op_pcm_total(of, bitstream); |
| 433 |
total_size = total_samples * channels * word_size; |
| 434 |
|
| 435 |
|
| 436 |
ALLEGRO_DEBUG("channels %d\n", channels); |
| 437 |
ALLEGRO_DEBUG("word_size %d\n", word_size); |
| 438 |
ALLEGRO_DEBUG("rate %ld\n", rate); |
| 439 |
ALLEGRO_DEBUG("total_samples %ld\n", total_samples); |
| 440 |
ALLEGRO_DEBUG("total_size %ld\n", total_size); |
| 441 |
|
| 442 |
stream = al_create_audio_stream(buffer_count, samples, rate, |
| 443 |
_al_word_size_to_depth_conf(word_size), |
| 444 |
_al_count_to_channel_conf(channels)); |
| 445 |
if (!stream) { |
| 446 |
lib.op_free(of); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
|
| 450 |
stream->extra = extra; |
| 451 |
|
| 452 |
extra->loop_start = 0.0; |
| 453 |
extra->loop_end = ogg_stream_get_length(stream); |
| 454 |
stream->quit_feed_thread = false; |
| 455 |
stream->feeder = ogg_stream_update; |
| 456 |
stream->rewind_feeder = ogg_stream_rewind; |
| 457 |
stream->seek_feeder = ogg_stream_seek; |
| 458 |
stream->get_feeder_position = ogg_stream_get_position; |
| 459 |
stream->get_feeder_length = ogg_stream_get_length; |
| 460 |
stream->set_feeder_loop = ogg_stream_set_loop; |
| 461 |
stream->unload_feeder = ogg_stream_close; |
| 462 |
_al_acodec_start_feed_thread(stream); |
| 463 |
|
| 464 |
return stream; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
bool _al_identify_ogg_opus(ALLEGRO_FILE *f) |
| 469 |
{ |
| 470 |
uint8_t x[10]; |
| 471 |
if (al_fread(f, x, 4) < 4) |
| 472 |
return false; |
| 473 |
if (memcmp(x, "OggS", 4) != 0) |
| 474 |
return false; |
| 475 |
if (!al_fseek(f, 22, ALLEGRO_SEEK_CUR)) |
| 476 |
return false; |
| 477 |
if (al_fread(f, x, 10) < 10) |
| 478 |
return false; |
| 479 |
if (memcmp(x, "\x01\x13OpusHead", 10) == 0) |
| 480 |
return true; |
| 481 |
return false; |
| 482 |
} |
| 483 |
|
| 484 |
/* vim: set sts=3 sw=3 et: */ |