aac: Collect all interesting ID3 frames
[cmus.git] / flac.c
blob73cfb43486f765cc75ad9f07ca2ebe27448d9380
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 eof : 1;
61 unsigned int ignore_next_write : 1;
64 static T(ReadStatus) read_cb(const Dec *dec, unsigned char *buf, unsigned *size, void *data)
66 struct input_plugin_data *ip_data = data;
67 struct flac_private *priv = ip_data->private;
68 int rc;
70 if (priv->eof) {
71 *size = 0;
72 d_print("EOF! EOF! EOF!\n");
73 #ifdef FLAC_NEW_API
74 return E(READ_STATUS_END_OF_STREAM);
75 #else
76 return E(READ_STATUS_OK);
77 #endif
79 if (*size == 0)
80 #ifdef FLAC_NEW_API
81 return E(READ_STATUS_CONTINUE);
82 #else
83 return E(READ_STATUS_OK);
84 #endif
86 rc = read(ip_data->fd, buf, *size);
87 if (rc == -1) {
88 *size = 0;
89 if (errno == EINTR || errno == EAGAIN) {
90 /* FIXME: not sure how the flac decoder handles this */
91 d_print("interrupted\n");
92 #ifdef FLAC_NEW_API
93 return E(READ_STATUS_CONTINUE);
94 #else
95 return E(READ_STATUS_OK);
96 #endif
98 #ifdef FLAC_NEW_API
99 return E(READ_STATUS_ABORT);
100 #else
101 return E(READ_STATUS_ERROR);
102 #endif
104 if (*size != rc) {
105 d_print("%d != %d\n", rc, *size);
107 priv->pos += rc;
108 *size = rc;
109 if (rc == 0) {
110 /* never reached! */
111 priv->eof = 1;
112 d_print("EOF\n");
113 #ifdef FLAC_NEW_API
114 return E(READ_STATUS_END_OF_STREAM);
115 #else
116 return E(READ_STATUS_OK);
117 #endif
119 #ifdef FLAC_NEW_API
120 return E(READ_STATUS_CONTINUE);
121 #else
122 return E(READ_STATUS_OK);
123 #endif
126 static T(SeekStatus) seek_cb(const Dec *dec, uint64_t offset, void *data)
128 struct input_plugin_data *ip_data = data;
129 struct flac_private *priv = ip_data->private;
130 off_t off;
132 if (priv->len == UINT64_MAX)
133 return E(SEEK_STATUS_ERROR);
134 off = lseek(ip_data->fd, offset, SEEK_SET);
135 if (off == -1) {
136 return E(SEEK_STATUS_ERROR);
138 priv->pos = off;
139 return E(SEEK_STATUS_OK);
142 static T(TellStatus) tell_cb(const Dec *dec, uint64_t *offset, void *data)
144 struct input_plugin_data *ip_data = data;
145 struct flac_private *priv = ip_data->private;
147 d_print("\n");
148 *offset = priv->pos;
149 return E(TELL_STATUS_OK);
152 static T(LengthStatus) length_cb(const Dec *dec, uint64_t *len, void *data)
154 struct input_plugin_data *ip_data = data;
155 struct flac_private *priv = ip_data->private;
157 d_print("\n");
158 if (ip_data->remote) {
159 return E(LENGTH_STATUS_ERROR);
161 *len = priv->len;
162 return E(LENGTH_STATUS_OK);
165 static int eof_cb(const Dec *dec, void *data)
167 struct input_plugin_data *ip_data = data;
168 struct flac_private *priv = ip_data->private;
169 int eof = priv->eof;
171 if (eof)
172 d_print("EOF\n");
173 return eof;
176 #if defined(WORDS_BIGENDIAN)
178 static inline uint16_t LE16(uint16_t x)
180 return (x >> 8) | (x << 8);
183 static inline uint32_t LE32(uint32_t x)
185 uint32_t x3 = x << 24;
186 uint32_t x0 = x >> 24;
187 uint32_t x2 = (x & 0xff00) << 8;
188 uint32_t x1 = (x >> 8) & 0xff00;
189 return x3 | x2 | x1 | x0;
192 #else
194 #define LE16(x) (x)
195 #define LE32(x) (x)
197 #endif
199 static FLAC__StreamDecoderWriteStatus write_cb(const Dec *dec, const FLAC__Frame *frame,
200 const int32_t * const *buf, void *data)
202 struct input_plugin_data *ip_data = data;
203 struct flac_private *priv = ip_data->private;
204 int frames, bytes, size, channels, bits, depth;
205 int ch, i, j = 0;
207 if (ip_data->sf == 0) {
208 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
211 if (priv->ignore_next_write) {
212 priv->ignore_next_write = 0;
213 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
216 frames = frame->header.blocksize;
217 channels = sf_get_channels(ip_data->sf);
218 bits = sf_get_bits(ip_data->sf);
219 bytes = frames * bits / 8 * channels;
220 size = priv->buf_size;
222 if (size - priv->buf_wpos < bytes) {
223 if (size < bytes)
224 size = bytes;
225 size *= 2;
226 priv->buf = xrenew(char, priv->buf, size);
227 priv->buf_size = size;
230 depth = frame->header.bits_per_sample;
231 if (depth == 8) {
232 char *b = priv->buf + priv->buf_wpos;
234 for (i = 0; i < frames; i++) {
235 for (ch = 0; ch < channels; ch++)
236 b[j++] = buf[ch][i];
238 } else if (depth == 16) {
239 int16_t *b = (int16_t *)(priv->buf + priv->buf_wpos);
241 for (i = 0; i < frames; i++) {
242 for (ch = 0; ch < channels; ch++)
243 b[j++] = LE16(buf[ch][i]);
245 } else if (depth == 32) {
246 int32_t *b = (int32_t *)(priv->buf + priv->buf_wpos);
248 for (i = 0; i < frames; i++) {
249 for (ch = 0; ch < channels; ch++)
250 b[j++] = LE32(buf[ch][i]);
252 } else if (depth == 12) { /* -> 16 */
253 int16_t *b = (int16_t *)(priv->buf + priv->buf_wpos);
255 for (i = 0; i < frames; i++) {
256 for (ch = 0; ch < channels; ch++)
257 b[j++] = LE16(buf[ch][i] << 4);
259 } else if (depth == 20) { /* -> 32 */
260 int32_t *b = (int32_t *)(priv->buf + priv->buf_wpos);
262 for (i = 0; i < frames; i++) {
263 for (ch = 0; ch < channels; ch++)
264 b[j++] = LE32(buf[ch][i] << 12);
266 } else if (depth == 24) { /* -> 32 */
267 int32_t *b = (int32_t *)(priv->buf + priv->buf_wpos);
269 for (i = 0; i < frames; i++) {
270 for (ch = 0; ch < channels; ch++)
271 b[j++] = LE32(buf[ch][i] << 8);
273 } else {
274 d_print("bits per sample changed to %d\n", depth);
277 priv->buf_wpos += bytes;
278 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
281 /* You should make a copy of metadata with FLAC__metadata_object_clone() if you will
282 * need it elsewhere. Since metadata blocks can potentially be large, by
283 * default the decoder only calls the metadata callback for the STREAMINFO
284 * block; you can instruct the decoder to pass or filter other blocks with
285 * FLAC__stream_decoder_set_metadata_*() calls.
287 static void metadata_cb(const Dec *dec, const FLAC__StreamMetadata *metadata, void *data)
289 struct input_plugin_data *ip_data = data;
290 struct flac_private *priv = ip_data->private;
292 switch (metadata->type) {
293 case FLAC__METADATA_TYPE_STREAMINFO:
295 const FLAC__StreamMetadata_StreamInfo *si = &metadata->data.stream_info;
296 int bits = 0;
298 switch (si->bits_per_sample) {
299 case 8:
300 case 16:
301 case 32:
302 bits = si->bits_per_sample;
303 break;
304 case 12:
305 bits = 16;
306 break;
307 case 20:
308 case 24:
309 bits = 32;
310 break;
313 ip_data->sf = sf_rate(si->sample_rate) |
314 sf_bits(bits) |
315 sf_signed(1) |
316 sf_channels(si->channels);
317 if (!ip_data->remote && si->total_samples)
318 priv->duration = si->total_samples / si->sample_rate;
320 break;
321 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
322 d_print("VORBISCOMMENT\n");
323 if (priv->comments) {
324 d_print("Ignoring\n");
325 } else {
326 GROWING_KEYVALS(c);
327 int i, nr;
329 nr = metadata->data.vorbis_comment.num_comments;
330 for (i = 0; i < nr; i++) {
331 const char *str = metadata->data.vorbis_comment.comments[i].entry;
332 char *key, *val;
334 val = strchr(str, '=');
335 if (!val)
336 continue;
337 key = xstrndup(str, val - str);
338 val = xstrdup(val + 1);
339 comments_add(&c, key, val);
340 free(key);
342 comments_terminate(&c);
343 priv->comments = c.comments;
345 break;
346 default:
347 d_print("something else\n");
348 break;
352 static void error_cb(const Dec *dec, FLAC__StreamDecoderErrorStatus status, void *data)
354 static const char *strings[3] = {
355 "lost sync",
356 "bad header",
357 "frame crc mismatch"
359 const char *str = "unknown error";
361 if (status >= 0 && status < 3)
362 str = strings[status];
363 d_print("%d %s\n", status, str);
366 static void free_priv(struct input_plugin_data *ip_data)
368 struct flac_private *priv = ip_data->private;
369 int save = errno;
371 F(finish)(priv->dec);
372 F(delete)(priv->dec);
373 if (priv->comments)
374 comments_free(priv->comments);
375 free(priv->buf);
376 free(priv);
377 ip_data->private = NULL;
378 errno = save;
381 static int flac_open(struct input_plugin_data *ip_data)
383 struct flac_private *priv;
384 Dec *dec;
386 dec = F(new)();
387 if (dec == NULL)
388 return -IP_ERROR_INTERNAL;
390 priv = xnew0(struct flac_private, 1);
391 priv->dec = dec;
392 priv->duration = -1;
393 if (ip_data->remote) {
394 priv->len = UINT64_MAX;
395 } else {
396 off_t off = lseek(ip_data->fd, 0, SEEK_END);
398 if (off == -1 || lseek(ip_data->fd, 0, SEEK_SET) == -1) {
399 int save = errno;
401 F(delete)(dec);
402 free(priv);
403 errno = save;
404 return -IP_ERROR_ERRNO;
406 priv->len = off;
408 ip_data->private = priv;
410 #ifndef FLAC_NEW_API
411 F(set_read_callback)(dec, read_cb);
412 F(set_seek_callback)(dec, seek_cb);
413 F(set_tell_callback)(dec, tell_cb);
414 F(set_length_callback)(dec, length_cb);
415 F(set_eof_callback)(dec, eof_cb);
416 F(set_write_callback)(dec, write_cb);
417 F(set_metadata_callback)(dec, metadata_cb);
418 F(set_error_callback)(dec, error_cb);
419 F(set_client_data)(dec, ip_data);
420 #endif
422 #ifdef FLAC_NEW_API
423 FLAC__stream_decoder_set_metadata_respond_all(dec);
424 if (FLAC__stream_decoder_init_stream(dec, read_cb, seek_cb, tell_cb,
425 length_cb, eof_cb, write_cb, metadata_cb,
426 error_cb, ip_data) != E(INIT_STATUS_OK)) {
427 #else
428 /* FLAC__METADATA_TYPE_STREAMINFO already accepted */
429 F(set_metadata_respond)(dec, FLAC__METADATA_TYPE_VORBIS_COMMENT);
431 if (F(init)(dec) != E(OK)) {
432 #endif
433 int save = errno;
435 d_print("init failed\n");
436 F(delete)(priv->dec);
437 free(priv);
438 ip_data->private = NULL;
439 errno = save;
440 return -IP_ERROR_ERRNO;
443 ip_data->sf = 0;
444 while (priv->buf_wpos == 0 && !priv->eof) {
445 if (!F(process_single)(priv->dec)) {
446 free_priv(ip_data);
447 return -IP_ERROR_ERRNO;
451 if (!ip_data->sf) {
452 free_priv(ip_data);
453 return -IP_ERROR_FILE_FORMAT;
455 if (!sf_get_bits(ip_data->sf)) {
456 free_priv(ip_data);
457 return -IP_ERROR_SAMPLE_FORMAT;
460 d_print("sr: %d, ch: %d, bits: %d\n",
461 sf_get_rate(ip_data->sf),
462 sf_get_channels(ip_data->sf),
463 sf_get_bits(ip_data->sf));
464 return 0;
467 static int flac_close(struct input_plugin_data *ip_data)
469 free_priv(ip_data);
470 return 0;
473 static int flac_read(struct input_plugin_data *ip_data, char *buffer, int count)
475 struct flac_private *priv = ip_data->private;
476 int avail;
477 int libflac_suck_count = 0;
479 while (1) {
480 int old_pos = priv->buf_wpos;
482 avail = priv->buf_wpos - priv->buf_rpos;
483 BUG_ON(avail < 0);
484 if (avail > 0)
485 break;
486 if (priv->eof)
487 return 0;
488 if (!F(process_single)(priv->dec)) {
489 d_print("process_single failed\n");
490 return -1;
492 if (old_pos == priv->buf_wpos) {
493 libflac_suck_count++;
494 } else {
495 libflac_suck_count = 0;
497 if (libflac_suck_count > 5) {
498 d_print("libflac sucks\n");
499 priv->eof = 1;
500 return 0;
503 if (count > avail)
504 count = avail;
505 memcpy(buffer, priv->buf + priv->buf_rpos, count);
506 priv->buf_rpos += count;
507 BUG_ON(priv->buf_rpos > priv->buf_wpos);
508 if (priv->buf_rpos == priv->buf_wpos) {
509 priv->buf_rpos = 0;
510 priv->buf_wpos = 0;
512 return count;
515 /* Flush the input and seek to an absolute sample. Decoding will resume at the
516 * given sample. Note that because of this, the next write callback may contain
517 * a partial block.
519 static int flac_seek(struct input_plugin_data *ip_data, double offset)
521 struct flac_private *priv = ip_data->private;
522 uint64_t sample;
524 sample = (uint64_t)(offset * (double)sf_get_rate(ip_data->sf) + 0.5);
525 if (!F(seek_absolute)(priv->dec, sample)) {
526 return -IP_ERROR_ERRNO;
528 priv->ignore_next_write = 1;
529 priv->buf_rpos = 0;
530 priv->buf_wpos = 0;
531 return 0;
534 static int flac_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
536 struct flac_private *priv = ip_data->private;
538 if (priv->comments) {
539 *comments = comments_dup(priv->comments);
540 } else {
541 *comments = xnew0(struct keyval, 1);
543 return 0;
546 static int flac_duration(struct input_plugin_data *ip_data)
548 struct flac_private *priv = ip_data->private;
550 return priv->duration;
553 const struct input_plugin_ops ip_ops = {
554 .open = flac_open,
555 .close = flac_close,
556 .read = flac_read,
557 .seek = flac_seek,
558 .read_comments = flac_read_comments,
559 .duration = flac_duration
562 const char * const ip_extensions[] = { "flac", "fla", NULL };
563 const char * const ip_mime_types[] = { NULL };