6 #include <FLAC/export.h>
8 #ifdef FLAC_API_VERSION_CURRENT
10 #define FLAC_NEW_API 1
14 #include <FLAC/stream_decoder.h>
16 #include <FLAC/seekable_stream_decoder.h>
19 #include <FLAC/metadata.h>
21 #include <sys/types.h>
26 #define UINT64_MAX ((uint64_t)-1)
29 /* Reduce typing. Namespaces are nice but FLAC API is fscking ridiculous. */
31 /* functions, types, enums */
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
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
45 /* file/stream position and length */
53 unsigned int buf_size
;
54 unsigned int buf_wpos
;
55 unsigned int buf_rpos
;
57 struct keyval
*comments
;
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;
72 d_print("EOF! EOF! EOF!\n");
74 return E(READ_STATUS_END_OF_STREAM
);
76 return E(READ_STATUS_OK
);
81 return E(READ_STATUS_CONTINUE
);
83 return E(READ_STATUS_OK
);
86 rc
= read(ip_data
->fd
, buf
, *size
);
89 if (errno
== EINTR
|| errno
== EAGAIN
) {
90 /* FIXME: not sure how the flac decoder handles this */
91 d_print("interrupted\n");
93 return E(READ_STATUS_CONTINUE
);
95 return E(READ_STATUS_OK
);
99 return E(READ_STATUS_ABORT
);
101 return E(READ_STATUS_ERROR
);
105 d_print("%d != %d\n", rc
, *size
);
114 return E(READ_STATUS_END_OF_STREAM
);
116 return E(READ_STATUS_OK
);
120 return E(READ_STATUS_CONTINUE
);
122 return E(READ_STATUS_OK
);
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;
132 if (priv
->len
== UINT64_MAX
)
133 return E(SEEK_STATUS_ERROR
);
134 off
= lseek(ip_data
->fd
, offset
, SEEK_SET
);
136 return E(SEEK_STATUS_ERROR
);
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;
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;
158 if (ip_data
->remote
) {
159 return E(LENGTH_STATUS_ERROR
);
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;
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
;
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
;
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
) {
226 priv
->buf
= xrenew(char, priv
->buf
, size
);
227 priv
->buf_size
= size
;
230 depth
= frame
->header
.bits_per_sample
;
232 char *b
= priv
->buf
+ priv
->buf_wpos
;
234 for (i
= 0; i
< frames
; i
++) {
235 for (ch
= 0; ch
< channels
; ch
++)
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);
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
;
298 switch (si
->bits_per_sample
) {
302 bits
= si
->bits_per_sample
;
313 ip_data
->sf
= sf_rate(si
->sample_rate
) |
316 sf_channels(si
->channels
);
317 if (!ip_data
->remote
&& si
->total_samples
)
318 priv
->duration
= si
->total_samples
/ si
->sample_rate
;
321 case FLAC__METADATA_TYPE_VORBIS_COMMENT
:
322 d_print("VORBISCOMMENT\n");
323 if (priv
->comments
) {
324 d_print("Ignoring\n");
329 nr
= metadata
->data
.vorbis_comment
.num_comments
;
330 c
= xnew0(struct keyval
, nr
+ 1);
331 for (s
= 0, d
= 0; s
< nr
; s
++) {
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
],
341 if (!is_interesting_key(key
)) {
346 if (!strcasecmp(key
, "tracknumber") || !strcasecmp(key
, "discnumber"))
347 fix_track_or_disc(val
);
349 d_print("comment: '%s=%s'\n", key
, val
);
358 d_print("something else\n");
363 static void error_cb(const Dec
*dec
, FLAC__StreamDecoderErrorStatus status
, void *data
)
365 static const char *strings
[3] = {
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;
382 F(finish
)(priv
->dec
);
383 F(delete)(priv
->dec
);
385 comments_free(priv
->comments
);
388 ip_data
->private = NULL
;
392 static int flac_open(struct input_plugin_data
*ip_data
)
394 struct flac_private
*priv
;
399 return -IP_ERROR_INTERNAL
;
401 priv
= xnew0(struct flac_private
, 1);
404 if (ip_data
->remote
) {
405 priv
->len
= UINT64_MAX
;
407 off_t off
= lseek(ip_data
->fd
, 0, SEEK_END
);
409 if (off
== -1 || lseek(ip_data
->fd
, 0, SEEK_SET
) == -1) {
415 return -IP_ERROR_ERRNO
;
419 ip_data
->private = priv
;
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
);
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
)) {
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
)) {
445 d_print("init failed\n");
446 F(delete)(priv
->dec
);
448 ip_data
->private = NULL
;
450 return -IP_ERROR_ERRNO
;
454 while (priv
->buf_wpos
== 0 && !priv
->eof
) {
455 if (!F(process_single
)(priv
->dec
)) {
457 return -IP_ERROR_ERRNO
;
463 return -IP_ERROR_FILE_FORMAT
;
465 if (!sf_get_bits(ip_data
->sf
)) {
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
));
477 static int flac_close(struct input_plugin_data
*ip_data
)
483 static int flac_read(struct input_plugin_data
*ip_data
, char *buffer
, int count
)
485 struct flac_private
*priv
= ip_data
->private;
487 int libflac_suck_count
= 0;
490 int old_pos
= priv
->buf_wpos
;
492 avail
= priv
->buf_wpos
- priv
->buf_rpos
;
498 if (!F(process_single
)(priv
->dec
)) {
499 d_print("process_single failed\n");
502 if (old_pos
== priv
->buf_wpos
) {
503 libflac_suck_count
++;
505 libflac_suck_count
= 0;
507 if (libflac_suck_count
> 5) {
508 d_print("libflac sucks\n");
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
) {
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
529 static int flac_seek(struct input_plugin_data
*ip_data
, double offset
)
531 struct flac_private
*priv
= ip_data
->private;
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;
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
);
551 *comments
= xnew0(struct keyval
, 1);
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
= {
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
};