Debian Codegraph @

addons/acodec/flac.c

raw
1 /*
2 * Allegro FLAC reader
3 * author: Ryan Dickie, (c) 2008
4 * streaming support by Elias Pschernig
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_FLAC
17 #error configuration problem, ALLEGRO_CFG_ACODEC_FLAC not set
18 #endif
19
20 #include <FLAC/stream_decoder.h>
21 #include <stdio.h>
22
23 ALLEGRO_DEBUG_CHANNEL("acodec")
24
25
26 typedef struct FLACFILE {
27 FLAC__StreamDecoder *decoder;
28 double sample_rate;
29 int sample_size;
30 int channels;
31
32 /* The file buffer. */
33 uint64_t buffer_pos, buffer_size;
34 char *buffer;
35
36 /* Number of samples in the complete FLAC. */
37 uint64_t total_samples;
38
39 /* Sample position one past last decoded sample. */
40 uint64_t decoded_samples;
41
42 /* Sample position one past last streamed sample. */
43 uint64_t streamed_samples;
44
45 ALLEGRO_FILE *fh;
46 uint64_t loop_start, loop_end; /* in samples */
47 } FLACFILE;
48
49
50 /* dynamic loading support (Windows only currently) */
51 #ifdef ALLEGRO_CFG_ACODEC_FLAC_DLL
52 static void *flac_dll = NULL;
53 static bool flac_virgin = true;
54 #endif
55
56 static struct
57 {
58 FLAC__StreamDecoder *(*FLAC__stream_decoder_new)(void);
59 void (*FLAC__stream_decoder_delete)(FLAC__StreamDecoder *decoder);
60 FLAC__StreamDecoderInitStatus (*FLAC__stream_decoder_init_stream)(
61 FLAC__StreamDecoder *decoder,
62 FLAC__StreamDecoderReadCallback read_callback,
63 FLAC__StreamDecoderSeekCallback seek_callback,
64 FLAC__StreamDecoderTellCallback tell_callback,
65 FLAC__StreamDecoderLengthCallback length_callback,
66 FLAC__StreamDecoderEofCallback eof_callback,
67 FLAC__StreamDecoderWriteCallback write_callback,
68 FLAC__StreamDecoderMetadataCallback metadata_callback,
69 FLAC__StreamDecoderErrorCallback error_callback,
70 void *client_data);
71 FLAC__bool (*FLAC__stream_decoder_process_single)(FLAC__StreamDecoder *decoder);
72 FLAC__bool (*FLAC__stream_decoder_process_until_end_of_metadata)(FLAC__StreamDecoder *decoder);
73 FLAC__bool (*FLAC__stream_decoder_process_until_end_of_stream)(FLAC__StreamDecoder *decoder);
74 FLAC__bool (*FLAC__stream_decoder_seek_absolute)(FLAC__StreamDecoder *decoder, FLAC__uint64 sample);
75 FLAC__bool (*FLAC__stream_decoder_flush)(FLAC__StreamDecoder *decoder);
76 FLAC__bool (*FLAC__stream_decoder_finish)(FLAC__StreamDecoder *decoder);
77 } lib;
78
79
80 #ifdef ALLEGRO_CFG_ACODEC_FLAC_DLL
81 static void shutdown_dynlib(void)
82 {
83 if (flac_dll) {
84 _al_close_library(flac_dll);
85 flac_dll = NULL;
86 flac_virgin = true;
87 }
88 }
89 #endif
90
91
92 static bool init_dynlib(void)
93 {
94 #ifdef ALLEGRO_CFG_ACODEC_FLAC_DLL
95 if (flac_dll) {
96 return true;
97 }
98
99 if (!flac_virgin) {
100 return false;
101 }
102
103 flac_virgin = false;
104
105 flac_dll = _al_open_library(ALLEGRO_CFG_ACODEC_FLAC_DLL);
106 if (!flac_dll) {
107 ALLEGRO_ERROR("Could not load " ALLEGRO_CFG_ACODEC_FLAC_DLL "\n");
108 return false;
109 }
110
111 _al_add_exit_func(shutdown_dynlib, "shutdown_dynlib");
112
113 #define INITSYM(x) \
114 do \
115 { \
116 lib.x = _al_import_symbol(flac_dll, #x); \
117 if (lib.x == 0) { \
118 ALLEGRO_ERROR("undefined symbol in lib structure: " #x "\n"); \
119 return false; \
120 } \
121 } while(0)
122 #else
123 #define INITSYM(x) (lib.x = (x))
124 #endif
125
126 memset(&lib, 0, sizeof(lib));
127
128 INITSYM(FLAC__stream_decoder_new);
129 INITSYM(FLAC__stream_decoder_delete);
130 INITSYM(FLAC__stream_decoder_init_stream);
131 INITSYM(FLAC__stream_decoder_process_single);
132 INITSYM(FLAC__stream_decoder_process_until_end_of_metadata);
133 INITSYM(FLAC__stream_decoder_process_until_end_of_stream);
134 INITSYM(FLAC__stream_decoder_seek_absolute);
135 INITSYM(FLAC__stream_decoder_flush);
136 INITSYM(FLAC__stream_decoder_finish);
137
138 return true;
139
140 #undef INITSYM
141 }
142
143
144 static FLAC__StreamDecoderReadStatus read_callback(const FLAC__StreamDecoder *decoder,
145 FLAC__byte buffer[], size_t *bytes, void *dptr)
146 {
147 FLACFILE *ff = (FLACFILE *)dptr;
148 ALLEGRO_FILE *fh = ff->fh;
149 (void)decoder;
150 if (*bytes > 0) {
151 *bytes = al_fread(fh, buffer, *bytes);
152 if (al_ferror(fh))
153 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
154 else if (*bytes == 0)
155 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
156 else
157 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
158 }
159 else
160 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
161 }
162
163
164 static FLAC__StreamDecoderSeekStatus seek_callback(
165 const FLAC__StreamDecoder *decoder,
166 FLAC__uint64 absolute_byte_offset, void *dptr)
167 {
168 FLACFILE *ff = (FLACFILE *)dptr;
169 ALLEGRO_FILE *fh = ff->fh;
170 (void)decoder;
171
172 if (!al_fseek(fh, absolute_byte_offset, ALLEGRO_SEEK_SET))
173 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
174 else
175 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
176 }
177
178
179 static FLAC__StreamDecoderTellStatus tell_callback(
180 const FLAC__StreamDecoder *decoder,
181 FLAC__uint64 *absolute_byte_offset, void *dptr)
182 {
183 FLACFILE *ff = (FLACFILE *)dptr;
184 ALLEGRO_FILE *fh = ff->fh;
185 int64_t pos = 0;
186 (void)decoder;
187
188 pos = al_ftell(fh);
189 if (pos == -1)
190 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
191
192 *absolute_byte_offset = (FLAC__uint64)pos;
193 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
194 }
195
196
197 static FLAC__StreamDecoderLengthStatus length_callback(
198 const FLAC__StreamDecoder *decoder,
199 FLAC__uint64 *stream_length, void *dptr)
200 {
201 FLACFILE *ff = (FLACFILE *)dptr;
202 ALLEGRO_FILE *fh = ff->fh;
203 (void)decoder;
204
205 /* XXX check error */
206 *stream_length = (FLAC__uint64)al_fsize(fh);
207
208 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
209 }
210
211
212 static FLAC__bool eof_callback(const FLAC__StreamDecoder *decoder, void *dptr)
213 {
214 FLACFILE *ff = (FLACFILE *)dptr;
215 ALLEGRO_FILE *fh = ff->fh;
216 (void)decoder;
217
218 if (al_feof(fh))
219 return true;
220
221 return false;
222 }
223
224
225 static void metadata_callback(const FLAC__StreamDecoder *decoder,
226 const FLAC__StreamMetadata *metadata, void *client_data)
227 {
228 FLACFILE *out = (FLACFILE *)client_data;
229
230 (void)decoder;
231
232 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
233 out->total_samples = metadata->data.stream_info.total_samples;
234 out->sample_rate = metadata->data.stream_info.sample_rate;
235 out->channels = metadata->data.stream_info.channels;
236 out->sample_size = metadata->data.stream_info.bits_per_sample / 8;
237 }
238 }
239
240
241 static void error_callback(const FLAC__StreamDecoder *decoder,
242 FLAC__StreamDecoderErrorStatus status, void *client_data)
243 {
244 (void)decoder;
245 (void)client_data;
246
247 #ifdef ALLEGRO_CFG_ACODEC_FLAC_DLL
248 (void)status;
249 ALLEGRO_ERROR("Got FLAC error callback\n"); /* lazy */
250 #else
251 ALLEGRO_ERROR("Got FLAC error callback: %s\n",
252 FLAC__StreamDecoderErrorStatusString[status]);
253 #endif
254 }
255
256
257 static FLAC__StreamDecoderWriteStatus write_callback(
258 const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
259 const FLAC__int32 * const buffer[], void *client_data)
260 {
261 FLACFILE *ff = (FLACFILE *) client_data;
262 long len = frame->header.blocksize;
263 long bytes = len * ff->channels * ff->sample_size;
264 FLAC__uint8 *buf8;
265 FLAC__int16 *buf16;
266 float *buf32;
267 int sample_index;
268 int channel_index;
269 int out_index;
270
271 if (ff->buffer_pos + bytes > ff->buffer_size) {
272 ff->buffer = al_realloc(ff->buffer, ff->buffer_pos + bytes);
273 ff->buffer_size = ff->buffer_pos + bytes;
274 }
275
276 /* FLAC returns FLAC__int32 and I need to convert it to my own format. */
277 buf8 = (FLAC__uint8 *) (ff->buffer + ff->buffer_pos);
278 buf16 = (FLAC__int16 *) buf8;
279 buf32 = (float *) buf8;
280
281 (void)decoder;
282 (void)client_data;
283
284 /* Flatten the array */
285 /* TODO: test this array flattening process on 5.1 and higher flac files */
286 out_index = 0;
287 switch (ff->sample_size) {
288 case 1:
289 for (sample_index = 0; sample_index < len; sample_index++) {
290 for (channel_index = 0;
291 channel_index < ff->channels;
292 channel_index++) {
293 buf8[out_index++] =
294 (FLAC__uint8) buffer[channel_index][sample_index];
295 }
296 }
297 break;
298
299 case 2:
300 for (sample_index = 0; sample_index < len; sample_index++) {
301 for (channel_index = 0; channel_index < ff->channels;
302 channel_index++) {
303 buf16[out_index++] =
304 (FLAC__int16) buffer[channel_index][sample_index];
305 }
306 }
307 break;
308
309 case 3:
310 for (sample_index = 0; sample_index < len; sample_index++) {
311 for (channel_index = 0; channel_index < ff->channels;
312 channel_index++)
313 {
314 /* Little endian */
315 /* FIXME: does this work? I only have 16-bit sound card mixer
316 * garbages for other 24-bit codecs too.
317 */
318 buf8[out_index++] = (FLAC__uint8) (buffer[channel_index][sample_index] & 0xFF);
319 buf8[out_index++] = (FLAC__uint8) ((buffer[channel_index][sample_index] & 0xFF00) >> 8);
320 buf8[out_index++] = (FLAC__uint8) ((buffer[channel_index][sample_index] & 0xFF0000) >> 16);
321 }
322 }
323 break;
324
325 case 4:
326 for (sample_index = 0; sample_index < len; sample_index++) {
327 for (channel_index = 0; channel_index < ff->channels;
328 channel_index++) {
329 buf32[out_index++] =
330 (float) buffer[channel_index][sample_index];
331 }
332 }
333 break;
334
335 default:
336 /* Word_size not supported. */
337 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
338 }
339
340 ff->decoded_samples += len;
341 ff->buffer_pos += bytes;
342 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
343 }
344
345 static void flac_close(FLACFILE *ff)
346 {
347 lib.FLAC__stream_decoder_finish(ff->decoder);
348 lib.FLAC__stream_decoder_delete(ff->decoder);
349 /* Don't close ff->fh here. */
350 al_free(ff);
351 }
352
353 /* In seconds. */
354 static double flac_stream_get_position(ALLEGRO_AUDIO_STREAM *stream)
355 {
356 FLACFILE *ff = (FLACFILE *)stream->extra;
357 return ff->streamed_samples / ff->sample_rate;
358 }
359
360
361 /*
362 * Updates 'stream' with the next chunk of data.
363 * Returns the actual number of bytes written.
364 */
365 static size_t flac_stream_update(ALLEGRO_AUDIO_STREAM *stream, void *data,
366 size_t buf_size)
367 {
368 int bytes_per_sample;
369 uint64_t wanted_samples;
370 uint64_t read_samples;
371 size_t written_bytes = 0;
372 size_t read_bytes;
373 FLACFILE *ff = (FLACFILE *)stream->extra;
374
375 bytes_per_sample = ff->sample_size * ff->channels;
376 wanted_samples = buf_size / bytes_per_sample;
377
378 if (ff->streamed_samples + wanted_samples > ff->loop_end) {
379 if (ff->loop_end > ff->streamed_samples)
380 wanted_samples = ff->loop_end - ff->streamed_samples;
381 else
382 return 0;
383 }
384
385 while (wanted_samples > 0) {
386 read_samples = ff->decoded_samples - ff->streamed_samples;
387
388 /* If the buffer size is small, we shouldn't read a new frame or our
389 * buffer keeps growing - so only refill when needed.
390 */
391 if (!read_samples) {
392 if (!lib.FLAC__stream_decoder_process_single(ff->decoder))
393 break;
394 read_samples = ff->decoded_samples - ff->streamed_samples;
395 if (!read_samples) {
396 break;
397 }
398 }
399
400 if (read_samples > wanted_samples)
401 read_samples = wanted_samples;
402 ff->streamed_samples += read_samples;
403 wanted_samples -= read_samples;
404 read_bytes = read_samples * bytes_per_sample;
405 /* Copy data from the FLAC file buffer to the stream buffer. */
406 memcpy((uint8_t *)data + written_bytes, ff->buffer, read_bytes);
407 /* Make room in the FLACFILE buffer. */
408 memmove(ff->buffer, ff->buffer + read_bytes,
409 ff->buffer_pos - read_bytes);
410 ff->buffer_pos -= read_bytes;
411 written_bytes += read_bytes;
412 }
413
414 return written_bytes;
415 }
416
417 /* Called from al_destroy_audio_stream. */
418 static void flac_stream_close(ALLEGRO_AUDIO_STREAM *stream)
419 {
420 FLACFILE *ff = stream->extra;
421 _al_acodec_stop_feed_thread(stream);
422
423 al_fclose(ff->fh);
424 al_free(ff->buffer);
425 flac_close(ff);
426 }
427
428 static bool real_seek(ALLEGRO_AUDIO_STREAM *stream, uint64_t sample)
429 {
430 FLACFILE *ff = stream->extra;
431
432 /* We use ff->streamed_samples as the exact sample position for looping and
433 * returning the position. Therefore we also use it as reference position
434 * when seeking - that is, we call flush below to make the FLAC decoder
435 * discard any additional samples it may have buffered already.
436 * */
437 lib.FLAC__stream_decoder_flush(ff->decoder);
438 lib.FLAC__stream_decoder_seek_absolute(ff->decoder, sample);
439
440 ff->buffer_pos = 0;
441 ff->streamed_samples = sample;
442 ff->decoded_samples = sample;
443 return true;
444 }
445
446 static bool flac_stream_seek(ALLEGRO_AUDIO_STREAM *stream, double time)
447 {
448 FLACFILE *ff = stream->extra;
449 uint64_t sample = time * ff->sample_rate;
450 return real_seek(stream, sample);
451 }
452
453 static bool flac_stream_rewind(ALLEGRO_AUDIO_STREAM *stream)
454 {
455 FLACFILE *ff = stream->extra;
456 return real_seek(stream, ff->loop_start);
457 }
458
459 static double flac_stream_get_length(ALLEGRO_AUDIO_STREAM *stream)
460 {
461 FLACFILE *ff = stream->extra;
462 return ff->total_samples / ff->sample_rate;
463 }
464
465 static bool flac_stream_set_loop(ALLEGRO_AUDIO_STREAM *stream, double start,
466 double end)
467 {
468 FLACFILE *ff = stream->extra;
469 ff->loop_start = start * ff->sample_rate;
470 ff->loop_end = end * ff->sample_rate;
471 return true;
472 }
473
474 static FLACFILE *flac_open(ALLEGRO_FILE* f)
475 {
476 FLACFILE *ff;
477 FLAC__StreamDecoderInitStatus init_status;
478
479 if (!init_dynlib()) {
480 return NULL;
481 }
482
483 ff = al_calloc(1, sizeof *ff);
484
485 ff->decoder = lib.FLAC__stream_decoder_new();
486 if (!ff->decoder) {
487 ALLEGRO_ERROR("Error allocating FLAC decoder\n");
488 goto error;
489 }
490
491 ff->fh = f;
492 if (!ff->fh) {
493 ALLEGRO_ERROR("Error opening FLAC file\n");
494 goto error;
495 }
496
497 init_status = lib.FLAC__stream_decoder_init_stream(ff->decoder, read_callback,
498 seek_callback, tell_callback, length_callback, eof_callback,
499 write_callback, metadata_callback, error_callback, ff);
500 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
501 #ifdef ALLEGRO_CFG_ACODEC_FLAC_DLL
502 ALLEGRO_ERROR("Error initializing FLAC decoder\n"); /* lazy */
503 #else
504 ALLEGRO_ERROR("Error initializing FLAC decoder: %s\n",
505 FLAC__StreamDecoderInitStatusString[init_status]);
506 #endif
507 goto error;
508 }
509
510 lib.FLAC__stream_decoder_process_until_end_of_metadata(ff->decoder);
511
512 if (ff->sample_size == 0) {
513 ALLEGRO_ERROR("Error: don't support sub 8-bit sizes\n");
514 goto error;
515 }
516
517 ALLEGRO_DEBUG("Loaded FLAC sample with properties:\n");
518 ALLEGRO_DEBUG(" channels %d\n", ff->channels);
519 ALLEGRO_DEBUG(" sample_size %d\n", ff->sample_size);
520 ALLEGRO_DEBUG(" rate %.f\n", ff->sample_rate);
521 ALLEGRO_DEBUG(" total_samples %ld\n", (long) ff->total_samples);
522
523 return ff;
524
525 error:
526 if (ff) {
527 if (ff->decoder)
528 lib.FLAC__stream_decoder_delete(ff->decoder);
529 al_free(ff);
530 }
531 return NULL;
532 }
533
534 ALLEGRO_SAMPLE *_al_load_flac(const char *filename)
535 {
536 ALLEGRO_FILE *f;
537 ALLEGRO_SAMPLE *spl;
538 ASSERT(filename);
539
540 f = al_fopen(filename, "rb");
541 if (!f) {
542 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
543 return NULL;
544 }
545
546 spl = _al_load_flac_f(f);
547
548 al_fclose(f);
549
550 return spl;
551 }
552
553 ALLEGRO_SAMPLE *_al_load_flac_f(ALLEGRO_FILE *f)
554 {
555 ALLEGRO_SAMPLE *sample;
556 FLACFILE *ff;
557
558 ff = flac_open(f);
559 if (!ff) {
560 return NULL;
561 }
562
563 ff->buffer_size = ff->total_samples * ff->channels * ff->sample_size;
564 ff->buffer = al_malloc(ff->buffer_size);
565
566 lib.FLAC__stream_decoder_process_until_end_of_stream(ff->decoder);
567
568 sample = al_create_sample(ff->buffer, ff->total_samples, ff->sample_rate,
569 _al_word_size_to_depth_conf(ff->sample_size),
570 _al_count_to_channel_conf(ff->channels), true);
571
572 if (!sample) {
573 ALLEGRO_ERROR("Failed to create a sample.\n");
574 al_free(ff->buffer);
575 }
576
577 flac_close(ff);
578
579 return sample;
580 }
581
582 ALLEGRO_AUDIO_STREAM *_al_load_flac_audio_stream(const char *filename,
583 size_t buffer_count, unsigned int samples)
584 {
585 ALLEGRO_FILE *f;
586 ALLEGRO_AUDIO_STREAM *stream;
587 ASSERT(filename);
588
589 f = al_fopen(filename, "rb");
590 if (!f) {
591 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
592 return NULL;
593 }
594
595 stream = _al_load_flac_audio_stream_f(f, buffer_count, samples);
596 if (!stream) {
597 al_fclose(f);
598 }
599
600 return stream;
601 }
602
603 ALLEGRO_AUDIO_STREAM *_al_load_flac_audio_stream_f(ALLEGRO_FILE* f,
604 size_t buffer_count, unsigned int samples)
605 {
606 ALLEGRO_AUDIO_STREAM *stream;
607 FLACFILE *ff;
608
609 ff = flac_open(f);
610 if (!ff) {
611 return NULL;
612 }
613
614 stream = al_create_audio_stream(buffer_count, samples, ff->sample_rate,
615 _al_word_size_to_depth_conf(ff->sample_size),
616 _al_count_to_channel_conf(ff->channels));
617
618 if (stream) {
619 stream->extra = ff;
620 ff->loop_start = 0;
621 ff->loop_end = ff->total_samples;
622 stream->feeder = flac_stream_update;
623 stream->unload_feeder = flac_stream_close;
624 stream->rewind_feeder = flac_stream_rewind;
625 stream->seek_feeder = flac_stream_seek;
626 stream->get_feeder_position = flac_stream_get_position;
627 stream->get_feeder_length = flac_stream_get_length;
628 stream->set_feeder_loop = flac_stream_set_loop;
629 _al_acodec_start_feed_thread(stream);
630 }
631 else {
632 ALLEGRO_ERROR("Failed to create stream.\n");
633 al_fclose(ff->fh);
634 flac_close(ff);
635 }
636
637 return stream;
638 }
639
640
641 bool _al_identify_flac(ALLEGRO_FILE *f)
642 {
643 uint8_t x[4];
644 if (al_fread(f, x, 4) < 4)
645 return false;
646 if (memcmp(x, "fLaC", 4) == 0)
647 return true;
648 return false;
649 }
650
651 /* vim: set sts=3 sw=3 et: */