Debian Codegraph @

addons/acodec/ogg.c

raw
1 /*
2 * Allegro5 Ogg Vorbis reader.
3 * Can load samples and do streaming
4 * author: Ryan Dickie (c) 2008
5 */
6
7
8 #include "allegro5/allegro.h"
9 #include "allegro5/allegro_acodec.h"
10 #include "allegro5/allegro_audio.h"
11 #include "allegro5/internal/aintern.h"
12 #include "allegro5/internal/aintern_audio.h"
13 #include "acodec.h"
14 #include "helper.h"
15
16 #ifndef ALLEGRO_CFG_ACODEC_VORBIS
17 #error configuration problem, ALLEGRO_CFG_ACODEC_VORBIS not set
18 #endif
19
20 ALLEGRO_DEBUG_CHANNEL("acodec")
21
22 #if defined(ALLEGRO_CFG_ACODEC_TREMOR)
23 #include <tremor/ivorbisfile.h>
24 #define TREMOR 1
25 #else
26 #include <vorbis/vorbisfile.h>
27 #endif
28
29 typedef struct AL_OV_DATA AL_OV_DATA;
30
31 struct AL_OV_DATA {
32 OggVorbis_File *vf;
33 vorbis_info *vi;
34 ALLEGRO_FILE *file;
35 int bitstream;
36 double loop_start;
37 double loop_end;
38 };
39
40
41 /* dynamic loading support (Windows only currently) */
42 #ifdef ALLEGRO_CFG_ACODEC_VORBISFILE_DLL
43 static void *ov_dll = NULL;
44 static bool ov_virgin = true;
45 #endif
46
47 static struct
48 {
49 int (*ov_clear)(OggVorbis_File *);
50 ogg_int64_t (*ov_pcm_total)(OggVorbis_File *, int);
51 vorbis_info *(*ov_info)(OggVorbis_File *, int);
52 #ifndef TREMOR
53 int (*ov_open_callbacks)(void *, OggVorbis_File *, const char *, long, ov_callbacks);
54 double (*ov_time_total)(OggVorbis_File *, int);
55 int (*ov_time_seek)(OggVorbis_File *, double);
56 double (*ov_time_tell)(OggVorbis_File *);
57 long (*ov_read)(OggVorbis_File *, char *, int, int, int, int, int *);
58 #else
59 int (*ov_open_callbacks)(void *, OggVorbis_File *, const char *, long, ov_callbacks);
60 ogg_int64_t (*ov_time_total)(OggVorbis_File *, int);
61 int (*ov_time_seek)(OggVorbis_File *, ogg_int64_t);
62 ogg_int64_t (*ov_time_tell)(OggVorbis_File *);
63 long (*ov_read)(OggVorbis_File *, char *, int, int *);
64 #endif
65 } lib;
66
67
68 #ifdef ALLEGRO_CFG_ACODEC_VORBISFILE_DLL
69 static void shutdown_dynlib(void)
70 {
71 if (ov_dll) {
72 _al_close_library(ov_dll);
73 ov_dll = NULL;
74 ov_virgin = true;
75 }
76 }
77 #endif
78
79
80 static bool init_dynlib(void)
81 {
82 #ifdef ALLEGRO_CFG_ACODEC_VORBISFILE_DLL
83 if (ov_dll) {
84 return true;
85 }
86
87 if (!ov_virgin) {
88 return false;
89 }
90
91 ov_virgin = false;
92
93 ov_dll = _al_open_library(ALLEGRO_CFG_ACODEC_VORBISFILE_DLL);
94 if (!ov_dll) {
95 ALLEGRO_ERROR("Could not load " ALLEGRO_CFG_ACODEC_VORBISFILE_DLL "\n");
96 return false;
97 }
98
99 _al_add_exit_func(shutdown_dynlib, "shutdown_dynlib");
100
101 #define INITSYM(x) \
102 do \
103 { \
104 lib.x = _al_import_symbol(ov_dll, #x); \
105 if (lib.x == 0) { \
106 ALLEGRO_ERROR("undefined symbol in lib structure: " #x "\n"); \
107 return false; \
108 } \
109 } while(0)
110 #else
111 #define INITSYM(x) (lib.x = (x))
112 #endif
113
114 memset(&lib, 0, sizeof(lib));
115
116 INITSYM(ov_clear);
117 INITSYM(ov_open_callbacks);
118 INITSYM(ov_pcm_total);
119 INITSYM(ov_info);
120 INITSYM(ov_time_total);
121 INITSYM(ov_time_seek);
122 INITSYM(ov_time_tell);
123 INITSYM(ov_read);
124
125 return true;
126
127 #undef INITSYM
128 }
129
130
131 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *dptr)
132 {
133 AL_OV_DATA *ov = (AL_OV_DATA *)dptr;
134 size_t ret = 0;
135
136 ret = al_fread(ov->file, ptr, size*nmemb);
137
138 return ret;
139 }
140
141
142 static int seek_callback(void *dptr, ogg_int64_t offset, int whence)
143 {
144 AL_OV_DATA *ov = (AL_OV_DATA *)dptr;
145
146 switch(whence) {
147 case SEEK_SET: whence = ALLEGRO_SEEK_SET; break;
148 case SEEK_CUR: whence = ALLEGRO_SEEK_CUR; break;
149 case SEEK_END: whence = ALLEGRO_SEEK_END; break;
150 }
151
152 if (!al_fseek(ov->file, offset, whence)) {
153 return -1;
154 }
155
156 return 0;
157 }
158
159
160 static long tell_callback(void *dptr)
161 {
162 AL_OV_DATA *ov = (AL_OV_DATA *)dptr;
163 int64_t ret = 0;
164
165 ret = al_ftell(ov->file);
166 if (ret == -1)
167 return -1;
168
169 return (long)ret;
170 }
171
172
173 static int close_callback(void *dptr)
174 {
175 /* Don't close dptr->file here. */
176 (void)dptr;
177 return 0;
178 }
179
180
181 static ov_callbacks callbacks = {
182 read_callback,
183 seek_callback,
184 close_callback,
185 tell_callback
186 };
187
188
189 ALLEGRO_SAMPLE *_al_load_ogg_vorbis(const char *filename)
190 {
191 ALLEGRO_FILE *f;
192 ALLEGRO_SAMPLE *spl;
193 ASSERT(filename);
194
195 ALLEGRO_INFO("Loading sample %s.\n", filename);
196 f = al_fopen(filename, "rb");
197 if (!f) {
198 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
199 return NULL;
200 }
201
202 spl = _al_load_ogg_vorbis_f(f);
203
204 al_fclose(f);
205
206 return spl;
207 }
208
209
210 ALLEGRO_SAMPLE *_al_load_ogg_vorbis_f(ALLEGRO_FILE *file)
211 {
212 /* Note: decoding library returns floats. I always return 16-bit (most
213 * commonly supported).
214 */
215 #ifdef ALLEGRO_LITTLE_ENDIAN
216 const int endian = 0; /* 0 for Little-Endian, 1 for Big-Endian */
217 #else
218 const int endian = 1; /* 0 for Little-Endian, 1 for Big-Endian */
219 #endif
220 int word_size = 2; /* 1 = 8bit, 2 = 16-bit. nothing else */
221 int signedness = 1; /* 0 for unsigned, 1 for signed */
222 const int packet_size = 4096; /* suggestion for size to read at a time */
223 OggVorbis_File vf;
224 vorbis_info* vi;
225 char *buffer;
226 long pos;
227 ALLEGRO_SAMPLE *sample;
228 int channels;
229 long rate;
230 long total_samples;
231 int bitstream;
232 long total_size;
233 AL_OV_DATA ov;
234 long read;
235
236 if (!init_dynlib()) {
237 return NULL;
238 }
239
240 ov.file = file;
241 if (lib.ov_open_callbacks(&ov, &vf, NULL, 0, callbacks) < 0) {
242 ALLEGRO_ERROR("Audio file does not appear to be an Ogg bitstream.\n");
243 return NULL;
244 }
245
246 vi = lib.ov_info(&vf, -1);
247
248 channels = vi->channels;
249 rate = vi->rate;
250 total_samples = lib.ov_pcm_total(&vf, -1);
251 bitstream = -1;
252 total_size = total_samples * channels * word_size;
253
254 ALLEGRO_DEBUG("channels %d\n", channels);
255 ALLEGRO_DEBUG("word_size %d\n", word_size);
256 ALLEGRO_DEBUG("rate %ld\n", rate);
257 ALLEGRO_DEBUG("total_samples %ld\n", total_samples);
258 ALLEGRO_DEBUG("total_size %ld\n", total_size);
259
260 buffer = al_malloc(total_size);
261 if (!buffer) {
262 ALLEGRO_ERROR("Unable to allocate buffer (%ld).\n", total_size);
263 return NULL;
264 }
265
266 pos = 0;
267 while (pos < total_size) {
268 const int read_size = _ALLEGRO_MIN(packet_size, total_size - pos);
269 ASSERT(pos + read_size <= total_size);
270
271 /* XXX error handling */
272 #ifndef TREMOR
273 read = lib.ov_read(&vf, buffer + pos, read_size, endian, word_size,
274 signedness, &bitstream);
275 #else
276 (void)endian;
277 (void)signedness;
278 read = lib.ov_read(&vf, buffer + pos, read_size, &bitstream);
279 #endif
280 pos += read;
281 if (read == 0)
282 break;
283 }
284
285 lib.ov_clear(&vf);
286
287 sample = al_create_sample(buffer, total_samples, rate,
288 _al_word_size_to_depth_conf(word_size),
289 _al_count_to_channel_conf(channels), true);
290
291 if (!sample) {
292 ALLEGRO_ERROR("Failed to create sample.\n");
293 al_free(buffer);
294 }
295
296 return sample;
297 }
298
299
300 static bool ogg_stream_seek(ALLEGRO_AUDIO_STREAM *stream, double time)
301 {
302 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
303 if (time >= extra->loop_end)
304 return false;
305 #ifndef TREMOR
306 return (lib.ov_time_seek(extra->vf, time) != -1);
307 #else
308 return lib.ov_time_seek(extra->vf, time*1000) != -1;
309 #endif
310 }
311
312
313 static bool ogg_stream_rewind(ALLEGRO_AUDIO_STREAM *stream)
314 {
315 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
316 return ogg_stream_seek(stream, extra->loop_start);
317 }
318
319
320 static double ogg_stream_get_position(ALLEGRO_AUDIO_STREAM *stream)
321 {
322 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
323 #ifndef TREMOR
324 return lib.ov_time_tell(extra->vf);
325 #else
326 return lib.ov_time_tell(extra->vf)/1000.0;
327 #endif
328 }
329
330
331 static double ogg_stream_get_length(ALLEGRO_AUDIO_STREAM *stream)
332 {
333 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
334 #ifndef TREMOR
335 double ret = lib.ov_time_total(extra->vf, -1);
336 #else
337 double ret = lib.ov_time_total(extra->vf, -1)/1000.0;
338 #endif
339 return ret;
340 }
341
342
343 static bool ogg_stream_set_loop(ALLEGRO_AUDIO_STREAM *stream, double start, double end)
344 {
345 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
346
347 extra->loop_start = start;
348 extra->loop_end = end;
349
350 return true;
351 }
352
353
354 /* To be called when stream is destroyed */
355 static void ogg_stream_close(ALLEGRO_AUDIO_STREAM *stream)
356 {
357 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
358
359 _al_acodec_stop_feed_thread(stream);
360
361 al_fclose(extra->file);
362
363 lib.ov_clear(extra->vf);
364 al_free(extra->vf);
365 al_free(extra);
366 stream->extra = NULL;
367 }
368
369
370 static size_t ogg_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
371 size_t buf_size)
372 {
373 AL_OV_DATA *extra = (AL_OV_DATA *) stream->extra;
374
375 #ifdef ALLEGRO_LITTLE_ENDIAN
376 const int endian = 0; /* 0 for Little-Endian, 1 for Big-Endian */
377 #else
378 const int endian = 1; /* 0 for Little-Endian, 1 for Big-Endian */
379 #endif
380 const int word_size = 2; /* 1 = 8bit, 2 = 16-bit. nothing else */
381 const int signedness = 1; /* 0 for unsigned, 1 for signed */
382
383 unsigned long pos = 0;
384 int read_length = buf_size;
385 #ifndef TREMOR
386 double ctime = lib.ov_time_tell(extra->vf);
387 #else
388 double ctime = lib.ov_time_tell(extra->vf)/1000.0;
389 #endif
390 double rate = extra->vi->rate;
391 double btime = ((double)buf_size / ((double)word_size * (double)extra->vi->channels)) / rate;
392 unsigned long read;
393
394 if (stream->spl.loop != _ALLEGRO_PLAYMODE_STREAM_ONCE && ctime + btime > extra->loop_end) {
395 const int frame_size = word_size * extra->vi->channels;
396 read_length = (extra->loop_end - ctime) * rate * (double)word_size * (double)extra->vi->channels;
397 if (read_length < 0)
398 return 0;
399 if (read_length % frame_size > 0) {
400 read_length += (frame_size - (read_length % frame_size));
401 }
402 }
403 while (pos < (unsigned long)read_length) {
404 #ifndef TREMOR
405 read = lib.ov_read(extra->vf, (char *)data + pos,
406 read_length - pos, endian, word_size, signedness, &extra->bitstream);
407 #else
408 (void)endian;
409 (void)signedness;
410 read = lib.ov_read(extra->vf, (char *)data + pos,
411 read_length - pos, &extra->bitstream);
412 #endif
413 pos += read;
414
415 if (read == 0) {
416 /* Return the number of useful bytes written. */
417 return pos;
418 }
419 }
420
421 return pos;
422 }
423
424
425 ALLEGRO_AUDIO_STREAM *_al_load_ogg_vorbis_audio_stream(const char *filename,
426 size_t buffer_count, unsigned int samples)
427 {
428 ALLEGRO_FILE *f;
429 ALLEGRO_AUDIO_STREAM *stream;
430 ASSERT(filename);
431
432 ALLEGRO_INFO("Loading stream %s.\n", filename);
433 f = al_fopen(filename, "rb");
434 if (!f) {
435 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
436 return NULL;
437 }
438
439 stream = _al_load_ogg_vorbis_audio_stream_f(f, buffer_count, samples);
440 if (!stream) {
441 al_fclose(f);
442 }
443
444 return stream;
445 }
446
447
448 ALLEGRO_AUDIO_STREAM *_al_load_ogg_vorbis_audio_stream_f(ALLEGRO_FILE *file,
449 size_t buffer_count, unsigned int samples)
450 {
451 const int word_size = 2; /* 1 = 8bit, 2 = 16-bit. nothing else */
452 OggVorbis_File* vf;
453 vorbis_info* vi;
454 int channels;
455 long rate;
456 long total_samples;
457 long total_size;
458 AL_OV_DATA* extra;
459 ALLEGRO_AUDIO_STREAM* stream;
460
461 if (!init_dynlib()) {
462 return NULL;
463 }
464
465 extra = al_malloc(sizeof(AL_OV_DATA));
466 if (extra == NULL) {
467 ALLEGRO_ERROR("Failed to allocate AL_OV_DATA struct.\n");
468 return NULL;
469 }
470
471 extra->file = file;
472
473 vf = al_malloc(sizeof(OggVorbis_File));
474 if (lib.ov_open_callbacks(extra, vf, NULL, 0, callbacks) < 0) {
475 ALLEGRO_ERROR("ogg: Input does not appear to be an Ogg bitstream.\n");
476 return NULL;
477 }
478
479 extra->vf = vf;
480
481 vi = lib.ov_info(vf, -1);
482 channels = vi->channels;
483 rate = vi->rate;
484 total_samples = lib.ov_pcm_total(vf, -1);
485 total_size = total_samples * channels * word_size;
486
487 extra->vi = vi;
488
489 extra->bitstream = -1;
490
491 ALLEGRO_DEBUG("channels %d\n", channels);
492 ALLEGRO_DEBUG("word_size %d\n", word_size);
493 ALLEGRO_DEBUG("rate %ld\n", rate);
494 ALLEGRO_DEBUG("total_samples %ld\n", total_samples);
495 ALLEGRO_DEBUG("total_size %ld\n", total_size);
496
497 stream = al_create_audio_stream(buffer_count, samples, rate,
498 _al_word_size_to_depth_conf(word_size),
499 _al_count_to_channel_conf(channels));
500 if (!stream) {
501 ALLEGRO_ERROR("Failed to create the stream.\n");
502 lib.ov_clear(vf);
503 al_free(vf);
504 return NULL;
505 }
506
507 stream->extra = extra;
508
509 extra->loop_start = 0.0;
510 extra->loop_end = ogg_stream_get_length(stream);
511 stream->quit_feed_thread = false;
512 stream->feeder = ogg_stream_update;
513 stream->rewind_feeder = ogg_stream_rewind;
514 stream->seek_feeder = ogg_stream_seek;
515 stream->get_feeder_position = ogg_stream_get_position;
516 stream->get_feeder_length = ogg_stream_get_length;
517 stream->set_feeder_loop = ogg_stream_set_loop;
518 stream->unload_feeder = ogg_stream_close;
519 _al_acodec_start_feed_thread(stream);
520
521 return stream;
522 }
523
524
525 bool _al_identify_ogg_vorbis(ALLEGRO_FILE *f)
526 {
527 uint8_t x[8];
528 if (al_fread(f, x, 4) < 4)
529 return false;
530 if (memcmp(x, "OggS", 4) != 0)
531 return false;
532 if (!al_fseek(f, 23, ALLEGRO_SEEK_CUR))
533 return false;
534 if (al_fread(f, x, 8) < 8)
535 return false;
536 if (memcmp(x, "\x1E\x01vorbis", 8) == 0)
537 return true;
538 return false;
539 }
540
541 /* vim: set sts=3 sw=3 et: */