flac: Saner EOF handling
[cmus.git] / flac.c
blob373c8cf81b9f118f1d7e729ccd752e47751e1116
1 #include "ip.h"
2 #include "comment.h"
3 #include "xmalloc.h"
4 #include "debug.h"
6 #include <FLAC/export.h>
8 #ifdef FLAC_API_VERSION_CURRENT
9 /* flac 1.1.3 */
10 #define FLAC_NEW_API 1
11 #endif
13 #ifdef FLAC_NEW_API
14 #include <FLAC/stream_decoder.h>
15 #else
16 #include <FLAC/seekable_stream_decoder.h>
17 #endif
19 #include <FLAC/metadata.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <errno.h>
25 #ifndef UINT64_MAX
26 #define UINT64_MAX ((uint64_t)-1)
27 #endif
29 /* Reduce typing. Namespaces are nice but FLAC API is fscking ridiculous. */
31 /* functions, types, enums */
32 #ifdef FLAC_NEW_API
33 #define F(s) FLAC__stream_decoder_ ## s
34 #define T(s) FLAC__StreamDecoder ## s
35 #define Dec FLAC__StreamDecoder
36 #define E(s) FLAC__STREAM_DECODER_ ## s
37 #else
38 #define F(s) FLAC__seekable_stream_decoder_ ## s
39 #define T(s) FLAC__SeekableStreamDecoder ## s
40 #define Dec FLAC__SeekableStreamDecoder
41 #define E(s) FLAC__SEEKABLE_STREAM_DECODER_ ## s
42 #endif
44 struct flac_private {
45 /* file/stream position and length */
46 uint64_t pos;
47 uint64_t len;
49 Dec *dec;
51 /* PCM data */
52 char *buf;
53 unsigned int buf_size;
54 unsigned int buf_wpos;
55 unsigned int buf_rpos;
57 struct keyval *comments;
58 int duration;
60 unsigned int ignore_next_write : 1;
63 #ifdef FLAC_NEW_API
64 static T(ReadStatus) read_cb(const Dec *dec, unsigned char *buf, size_t *size, void *data)
65 #else
66 static T(ReadStatus) read_cb(const Dec *dec, unsigned char *buf, unsigned *size, void *data)
67 #endif
69 struct input_plugin_data *ip_data = data;
70 struct flac_private *priv = ip_data->private;
71 int rc;
73 if (priv->pos == priv->len) {
74 *size = 0;
75 #ifdef FLAC_NEW_API
76 return E(READ_STATUS_END_OF_STREAM);
77 #else
78 return E(READ_STATUS_OK);
79 #endif
81 if (*size == 0)
82 #ifdef FLAC_NEW_API
83 return E(READ_STATUS_CONTINUE);
84 #else
85 return E(READ_STATUS_OK);
86 #endif
88 rc = read(ip_data->fd, buf, *size);
89 if (rc == -1) {
90 *size = 0;
91 if (errno == EINTR || errno == EAGAIN) {
92 /* FIXME: not sure how the flac decoder handles this */
93 d_print("interrupted\n");
94 #ifdef FLAC_NEW_API
95 return E(READ_STATUS_CONTINUE);
96 #else
97 return E(READ_STATUS_OK);
98 #endif
100 #ifdef FLAC_NEW_API
101 return E(READ_STATUS_ABORT);
102 #else
103 return E(READ_STATUS_ERROR);
104 #endif
107 priv->pos += rc;
108 *size = rc;
109 if (rc == 0) {
110 /* should not happen */
111 #ifdef FLAC_NEW_API
112 return E(READ_STATUS_END_OF_STREAM);
113 #else
114 return E(READ_STATUS_OK);
115 #endif
117 #ifdef FLAC_NEW_API
118 return E(READ_STATUS_CONTINUE);
119 #else
120 return E(READ_STATUS_OK);
121 #endif
124 static T(SeekStatus) seek_cb(const Dec *dec, uint64_t offset, void *data)
126 struct input_plugin_data *ip_data = data;
127 struct flac_private *priv = ip_data->private;
128 off_t off;
130 if (priv->len == UINT64_MAX)
131 return E(SEEK_STATUS_ERROR);
132 off = lseek(ip_data->fd, offset, SEEK_SET);
133 if (off == -1) {
134 return E(SEEK_STATUS_ERROR);
136 priv->pos = off;
137 return E(SEEK_STATUS_OK);
140 static T(TellStatus) tell_cb(const Dec *dec, uint64_t *offset, void *data)
142 struct input_plugin_data *ip_data = data;
143 struct flac_private *priv = ip_data->private;
145 d_print("\n");
146 *offset = priv->pos;
147 return E(TELL_STATUS_OK);
150 static T(LengthStatus) length_cb(const Dec *dec, uint64_t *len, void *data)
152 struct input_plugin_data *ip_data = data;
153 struct flac_private *priv = ip_data->private;
155 d_print("\n");
156 if (ip_data->remote) {
157 return E(LENGTH_STATUS_ERROR);
159 *len = priv->len;
160 return E(LENGTH_STATUS_OK);
163 static int eof_cb(const Dec *dec, void *data)
165 struct input_plugin_data *ip_data = data;
166 struct flac_private *priv = ip_data->private;
168 return priv->pos == priv->len;;
171 #if defined(WORDS_BIGENDIAN)
173 static inline uint16_t LE16(uint16_t x)
175 return (x >> 8) | (x << 8);
178 static inline uint32_t LE32(uint32_t x)
180 uint32_t x3 = x << 24;
181 uint32_t x0 = x >> 24;
182 uint32_t x2 = (x & 0xff00) << 8;
183 uint32_t x1 = (x >> 8) & 0xff00;
184 return x3 | x2 | x1 | x0;
187 #else
189 #define LE16(x) (x)
190 #define LE32(x) (x)
192 #endif
194 static FLAC__StreamDecoderWriteStatus write_cb(const Dec *dec, const FLAC__Frame *frame,
195 const int32_t * const *buf, void *data)
197 struct input_plugin_data *ip_data = data;
198 struct flac_private *priv = ip_data->private;
199 int frames, bytes, size, channels, bits, depth;
200 int ch, i, j = 0;
202 if (ip_data->sf == 0) {
203 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
206 if (priv->ignore_next_write) {
207 priv->ignore_next_write = 0;
208 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
211 frames = frame->header.blocksize;
212 channels = sf_get_channels(ip_data->sf);
213 bits = sf_get_bits(ip_data->sf);
214 bytes = frames * bits / 8 * channels;
215 size = priv->buf_size;
217 if (size - priv->buf_wpos < bytes) {
218 if (size < bytes)
219 size = bytes;
220 size *= 2;
221 priv->buf = xrenew(char, priv->buf, size);
222 priv->buf_size = size;
225 depth = frame->header.bits_per_sample;
226 if (depth == 8) {
227 char *b = priv->buf + priv->buf_wpos;
229 for (i = 0; i < frames; i++) {
230 for (ch = 0; ch < channels; ch++)
231 b[j++] = buf[ch][i];
233 } else if (depth == 16) {
234 int16_t *b = (int16_t *)(priv->buf + priv->buf_wpos);
236 for (i = 0; i < frames; i++) {
237 for (ch = 0; ch < channels; ch++)
238 b[j++] = LE16(buf[ch][i]);
240 } else if (depth == 32) {
241 int32_t *b = (int32_t *)(priv->buf + priv->buf_wpos);
243 for (i = 0; i < frames; i++) {
244 for (ch = 0; ch < channels; ch++)
245 b[j++] = LE32(buf[ch][i]);
247 } else if (depth == 12) { /* -> 16 */
248 int16_t *b = (int16_t *)(priv->buf + priv->buf_wpos);
250 for (i = 0; i < frames; i++) {
251 for (ch = 0; ch < channels; ch++)
252 b[j++] = LE16(buf[ch][i] << 4);
254 } else if (depth == 20) { /* -> 32 */
255 int32_t *b = (int32_t *)(priv->buf + priv->buf_wpos);
257 for (i = 0; i < frames; i++) {
258 for (ch = 0; ch < channels; ch++)
259 b[j++] = LE32(buf[ch][i] << 12);
261 } else if (depth == 24) { /* -> 32 */
262 int32_t *b = (int32_t *)(priv->buf + priv->buf_wpos);
264 for (i = 0; i < frames; i++) {
265 for (ch = 0; ch < channels; ch++)
266 b[j++] = LE32(buf[ch][i] << 8);
268 } else {
269 d_print("bits per sample changed to %d\n", depth);
272 priv->buf_wpos += bytes;
273 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
276 /* You should make a copy of metadata with FLAC__metadata_object_clone() if you will
277 * need it elsewhere. Since metadata blocks can potentially be large, by
278 * default the decoder only calls the metadata callback for the STREAMINFO
279 * block; you can instruct the decoder to pass or filter other blocks with
280 * FLAC__stream_decoder_set_metadata_*() calls.
282 static void metadata_cb(const Dec *dec, const FLAC__StreamMetadata *metadata, void *data)
284 struct input_plugin_data *ip_data = data;
285 struct flac_private *priv = ip_data->private;
287 switch (metadata->type) {
288 case FLAC__METADATA_TYPE_STREAMINFO:
290 const FLAC__StreamMetadata_StreamInfo *si = &metadata->data.stream_info;
291 int bits = 0;
293 switch (si->bits_per_sample) {
294 case 8:
295 case 16:
296 case 32:
297 bits = si->bits_per_sample;
298 break;
299 case 12:
300 bits = 16;
301 break;
302 case 20:
303 case 24:
304 bits = 32;
305 break;
308 ip_data->sf = sf_rate(si->sample_rate) |
309 sf_bits(bits) |
310 sf_signed(1) |
311 sf_channels(si->channels);
312 if (!ip_data->remote && si->total_samples)
313 priv->duration = si->total_samples / si->sample_rate;
315 break;
316 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
317 d_print("VORBISCOMMENT\n");
318 if (priv->comments) {
319 d_print("Ignoring\n");
320 } else {
321 GROWING_KEYVALS(c);
322 int i, nr;
324 nr = metadata->data.vorbis_comment.num_comments;
325 for (i = 0; i < nr; i++) {
326 const char *str = metadata->data.vorbis_comment.comments[i].entry;
327 char *key, *val;
329 val = strchr(str, '=');
330 if (!val)
331 continue;
332 key = xstrndup(str, val - str);
333 val = xstrdup(val + 1);
334 comments_add(&c, key, val);
335 free(key);
337 keyvals_terminate(&c);
338 priv->comments = c.keyvals;
340 break;
341 default:
342 d_print("something else\n");
343 break;
347 static void error_cb(const Dec *dec, FLAC__StreamDecoderErrorStatus status, void *data)
349 static const char *strings[3] = {
350 "lost sync",
351 "bad header",
352 "frame crc mismatch"
354 const char *str = "unknown error";
356 if (status >= 0 && status < 3)
357 str = strings[status];
358 d_print("%d %s\n", status, str);
361 static void free_priv(struct input_plugin_data *ip_data)
363 struct flac_private *priv = ip_data->private;
364 int save = errno;
366 F(finish)(priv->dec);
367 F(delete)(priv->dec);
368 if (priv->comments)
369 keyvals_free(priv->comments);
370 free(priv->buf);
371 free(priv);
372 ip_data->private = NULL;
373 errno = save;
376 static int flac_open(struct input_plugin_data *ip_data)
378 struct flac_private *priv;
379 Dec *dec;
381 dec = F(new)();
382 if (dec == NULL)
383 return -IP_ERROR_INTERNAL;
385 priv = xnew0(struct flac_private, 1);
386 priv->dec = dec;
387 priv->duration = -1;
388 if (ip_data->remote) {
389 priv->len = UINT64_MAX;
390 } else {
391 off_t off = lseek(ip_data->fd, 0, SEEK_END);
393 if (off == -1 || lseek(ip_data->fd, 0, SEEK_SET) == -1) {
394 int save = errno;
396 F(delete)(dec);
397 free(priv);
398 errno = save;
399 return -IP_ERROR_ERRNO;
401 priv->len = off;
403 ip_data->private = priv;
405 #ifndef FLAC_NEW_API
406 F(set_read_callback)(dec, read_cb);
407 F(set_seek_callback)(dec, seek_cb);
408 F(set_tell_callback)(dec, tell_cb);
409 F(set_length_callback)(dec, length_cb);
410 F(set_eof_callback)(dec, eof_cb);
411 F(set_write_callback)(dec, write_cb);
412 F(set_metadata_callback)(dec, metadata_cb);
413 F(set_error_callback)(dec, error_cb);
414 F(set_client_data)(dec, ip_data);
415 #endif
417 #ifdef FLAC_NEW_API
418 FLAC__stream_decoder_set_metadata_respond_all(dec);
419 if (FLAC__stream_decoder_init_stream(dec, read_cb, seek_cb, tell_cb,
420 length_cb, eof_cb, write_cb, metadata_cb,
421 error_cb, ip_data) != E(INIT_STATUS_OK)) {
422 #else
423 /* FLAC__METADATA_TYPE_STREAMINFO already accepted */
424 F(set_metadata_respond)(dec, FLAC__METADATA_TYPE_VORBIS_COMMENT);
426 if (F(init)(dec) != E(OK)) {
427 #endif
428 int save = errno;
430 d_print("init failed\n");
431 F(delete)(priv->dec);
432 free(priv);
433 ip_data->private = NULL;
434 errno = save;
435 return -IP_ERROR_ERRNO;
438 ip_data->sf = 0;
439 while (priv->buf_wpos == 0 && priv->pos < priv->len) {
440 if (!F(process_single)(priv->dec)) {
441 free_priv(ip_data);
442 return -IP_ERROR_ERRNO;
446 if (!ip_data->sf) {
447 free_priv(ip_data);
448 return -IP_ERROR_FILE_FORMAT;
450 if (!sf_get_bits(ip_data->sf)) {
451 free_priv(ip_data);
452 return -IP_ERROR_SAMPLE_FORMAT;
455 d_print("sr: %d, ch: %d, bits: %d\n",
456 sf_get_rate(ip_data->sf),
457 sf_get_channels(ip_data->sf),
458 sf_get_bits(ip_data->sf));
459 return 0;
462 static int flac_close(struct input_plugin_data *ip_data)
464 free_priv(ip_data);
465 return 0;
468 static int flac_read(struct input_plugin_data *ip_data, char *buffer, int count)
470 struct flac_private *priv = ip_data->private;
471 int avail;
473 while (1) {
474 avail = priv->buf_wpos - priv->buf_rpos;
475 BUG_ON(avail < 0);
476 if (avail > 0)
477 break;
478 if (priv->pos == priv->len)
479 return 0;
480 if (!F(process_single)(priv->dec)) {
481 d_print("process_single failed\n");
482 return -1;
485 if (count > avail)
486 count = avail;
487 memcpy(buffer, priv->buf + priv->buf_rpos, count);
488 priv->buf_rpos += count;
489 BUG_ON(priv->buf_rpos > priv->buf_wpos);
490 if (priv->buf_rpos == priv->buf_wpos) {
491 priv->buf_rpos = 0;
492 priv->buf_wpos = 0;
494 return count;
497 /* Flush the input and seek to an absolute sample. Decoding will resume at the
498 * given sample. Note that because of this, the next write callback may contain
499 * a partial block.
501 static int flac_seek(struct input_plugin_data *ip_data, double offset)
503 struct flac_private *priv = ip_data->private;
504 uint64_t sample;
506 sample = (uint64_t)(offset * (double)sf_get_rate(ip_data->sf) + 0.5);
507 if (!F(seek_absolute)(priv->dec, sample)) {
508 return -IP_ERROR_ERRNO;
510 priv->ignore_next_write = 1;
511 priv->buf_rpos = 0;
512 priv->buf_wpos = 0;
513 return 0;
516 static int flac_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
518 struct flac_private *priv = ip_data->private;
520 if (priv->comments) {
521 *comments = keyvals_dup(priv->comments);
522 } else {
523 *comments = xnew0(struct keyval, 1);
525 return 0;
528 static int flac_duration(struct input_plugin_data *ip_data)
530 struct flac_private *priv = ip_data->private;
532 return priv->duration;
535 const struct input_plugin_ops ip_ops = {
536 .open = flac_open,
537 .close = flac_close,
538 .read = flac_read,
539 .seek = flac_seek,
540 .read_comments = flac_read_comments,
541 .duration = flac_duration
544 const char * const ip_extensions[] = { "flac", "fla", NULL };
545 const char * const ip_mime_types[] = { NULL };