Simplify tab completion code by removing nr_tails variable
[cmus.git] / flac.c
blob10b4762004b8c67eb4691e4c751525cc09d89cc1
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 struct keyval *c;
327 int s, d, nr;
329 nr = metadata->data.vorbis_comment.num_comments;
330 c = xnew0(struct keyval, nr + 1);
331 for (s = 0, d = 0; s < nr; s++) {
332 char *key, *val;
334 /* until you have finished reading this function name
335 * you have already forgot WTF you're doing */
336 if (!FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(
337 metadata->data.vorbis_comment.comments[s],
338 &key, &val))
339 continue;
341 if (!is_interesting_key(key)) {
342 free(key);
343 free(val);
344 continue;
346 if (!strcasecmp(key, "tracknumber") || !strcasecmp(key, "discnumber"))
347 fix_track_or_disc(val);
349 d_print("comment: '%s=%s'\n", key, val);
350 c[d].key = key;
351 c[d].val = val;
352 d++;
354 priv->comments = c;
356 break;
357 default:
358 d_print("something else\n");
359 break;
363 static void error_cb(const Dec *dec, FLAC__StreamDecoderErrorStatus status, void *data)
365 static const char *strings[3] = {
366 "lost sync",
367 "bad header",
368 "frame crc mismatch"
370 const char *str = "unknown error";
372 if (status >= 0 && status < 3)
373 str = strings[status];
374 d_print("%d %s\n", status, str);
377 static void free_priv(struct input_plugin_data *ip_data)
379 struct flac_private *priv = ip_data->private;
380 int save = errno;
382 F(finish)(priv->dec);
383 F(delete)(priv->dec);
384 if (priv->comments)
385 comments_free(priv->comments);
386 free(priv->buf);
387 free(priv);
388 ip_data->private = NULL;
389 errno = save;
392 static int flac_open(struct input_plugin_data *ip_data)
394 struct flac_private *priv;
395 Dec *dec;
397 dec = F(new)();
398 if (dec == NULL)
399 return -IP_ERROR_INTERNAL;
401 priv = xnew0(struct flac_private, 1);
402 priv->dec = dec;
403 priv->duration = -1;
404 if (ip_data->remote) {
405 priv->len = UINT64_MAX;
406 } else {
407 off_t off = lseek(ip_data->fd, 0, SEEK_END);
409 if (off == -1 || lseek(ip_data->fd, 0, SEEK_SET) == -1) {
410 int save = errno;
412 F(delete)(dec);
413 free(priv);
414 errno = save;
415 return -IP_ERROR_ERRNO;
417 priv->len = off;
419 ip_data->private = priv;
421 #ifndef FLAC_NEW_API
422 F(set_read_callback)(dec, read_cb);
423 F(set_seek_callback)(dec, seek_cb);
424 F(set_tell_callback)(dec, tell_cb);
425 F(set_length_callback)(dec, length_cb);
426 F(set_eof_callback)(dec, eof_cb);
427 F(set_write_callback)(dec, write_cb);
428 F(set_metadata_callback)(dec, metadata_cb);
429 F(set_error_callback)(dec, error_cb);
430 F(set_client_data)(dec, ip_data);
431 #endif
433 #ifdef FLAC_NEW_API
434 if (FLAC__stream_decoder_init_stream(dec, read_cb, seek_cb, tell_cb,
435 length_cb, eof_cb, write_cb, metadata_cb,
436 error_cb, ip_data) != E(INIT_STATUS_OK)) {
437 #else
438 /* FLAC__METADATA_TYPE_STREAMINFO already accepted */
439 F(set_metadata_respond)(dec, FLAC__METADATA_TYPE_VORBIS_COMMENT);
441 if (F(init)(dec) != E(OK)) {
442 #endif
443 int save = errno;
445 d_print("init failed\n");
446 F(delete)(priv->dec);
447 free(priv);
448 ip_data->private = NULL;
449 errno = save;
450 return -IP_ERROR_ERRNO;
453 ip_data->sf = 0;
454 while (priv->buf_wpos == 0 && !priv->eof) {
455 if (!F(process_single)(priv->dec)) {
456 free_priv(ip_data);
457 return -IP_ERROR_ERRNO;
461 if (!ip_data->sf) {
462 free_priv(ip_data);
463 return -IP_ERROR_FILE_FORMAT;
465 if (!sf_get_bits(ip_data->sf)) {
466 free_priv(ip_data);
467 return -IP_ERROR_SAMPLE_FORMAT;
470 d_print("sr: %d, ch: %d, bits: %d\n",
471 sf_get_rate(ip_data->sf),
472 sf_get_channels(ip_data->sf),
473 sf_get_bits(ip_data->sf));
474 return 0;
477 static int flac_close(struct input_plugin_data *ip_data)
479 free_priv(ip_data);
480 return 0;
483 static int flac_read(struct input_plugin_data *ip_data, char *buffer, int count)
485 struct flac_private *priv = ip_data->private;
486 int avail;
487 int libflac_suck_count = 0;
489 while (1) {
490 int old_pos = priv->buf_wpos;
492 avail = priv->buf_wpos - priv->buf_rpos;
493 BUG_ON(avail < 0);
494 if (avail > 0)
495 break;
496 if (priv->eof)
497 return 0;
498 if (!F(process_single)(priv->dec)) {
499 d_print("process_single failed\n");
500 return -1;
502 if (old_pos == priv->buf_wpos) {
503 libflac_suck_count++;
504 } else {
505 libflac_suck_count = 0;
507 if (libflac_suck_count > 5) {
508 d_print("libflac sucks\n");
509 priv->eof = 1;
510 return 0;
513 if (count > avail)
514 count = avail;
515 memcpy(buffer, priv->buf + priv->buf_rpos, count);
516 priv->buf_rpos += count;
517 BUG_ON(priv->buf_rpos > priv->buf_wpos);
518 if (priv->buf_rpos == priv->buf_wpos) {
519 priv->buf_rpos = 0;
520 priv->buf_wpos = 0;
522 return count;
525 /* Flush the input and seek to an absolute sample. Decoding will resume at the
526 * given sample. Note that because of this, the next write callback may contain
527 * a partial block.
529 static int flac_seek(struct input_plugin_data *ip_data, double offset)
531 struct flac_private *priv = ip_data->private;
532 uint64_t sample;
534 sample = (uint64_t)(offset * (double)sf_get_rate(ip_data->sf) + 0.5);
535 if (!F(seek_absolute)(priv->dec, sample)) {
536 return -IP_ERROR_ERRNO;
538 priv->ignore_next_write = 1;
539 priv->buf_rpos = 0;
540 priv->buf_wpos = 0;
541 return 0;
544 static int flac_read_comments(struct input_plugin_data *ip_data, struct keyval **comments)
546 struct flac_private *priv = ip_data->private;
548 if (priv->comments) {
549 *comments = comments_dup(priv->comments);
550 } else {
551 *comments = xnew0(struct keyval, 1);
553 return 0;
556 static int flac_duration(struct input_plugin_data *ip_data)
558 struct flac_private *priv = ip_data->private;
560 return priv->duration;
563 const struct input_plugin_ops ip_ops = {
564 .open = flac_open,
565 .close = flac_close,
566 .read = flac_read,
567 .seek = flac_seek,
568 .read_comments = flac_read_comments,
569 .duration = flac_duration
572 const char * const ip_extensions[] = { "flac", "fla", NULL };
573 const char * const ip_mime_types[] = { NULL };