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 for (i
= 0; i
< nr
; i
++) {
333 /* until you have finished reading this function name
334 * you have already forgot WTF you're doing */
335 if (FLAC__metadata_object_vorbiscomment_entry_to_name_value_pair(
336 metadata
->data
.vorbis_comment
.comments
[i
],
338 comments_add(&c
, key
, val
);
342 comments_terminate(&c
);
343 priv
->comments
= c
.comments
;
347 d_print("something else\n");
352 static void error_cb(const Dec
*dec
, FLAC__StreamDecoderErrorStatus status
, void *data
)
354 static const char *strings
[3] = {
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;
371 F(finish
)(priv
->dec
);
372 F(delete)(priv
->dec
);
374 comments_free(priv
->comments
);
377 ip_data
->private = NULL
;
381 static int flac_open(struct input_plugin_data
*ip_data
)
383 struct flac_private
*priv
;
388 return -IP_ERROR_INTERNAL
;
390 priv
= xnew0(struct flac_private
, 1);
393 if (ip_data
->remote
) {
394 priv
->len
= UINT64_MAX
;
396 off_t off
= lseek(ip_data
->fd
, 0, SEEK_END
);
398 if (off
== -1 || lseek(ip_data
->fd
, 0, SEEK_SET
) == -1) {
404 return -IP_ERROR_ERRNO
;
408 ip_data
->private = priv
;
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
);
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
)) {
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
)) {
435 d_print("init failed\n");
436 F(delete)(priv
->dec
);
438 ip_data
->private = NULL
;
440 return -IP_ERROR_ERRNO
;
444 while (priv
->buf_wpos
== 0 && !priv
->eof
) {
445 if (!F(process_single
)(priv
->dec
)) {
447 return -IP_ERROR_ERRNO
;
453 return -IP_ERROR_FILE_FORMAT
;
455 if (!sf_get_bits(ip_data
->sf
)) {
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
));
467 static int flac_close(struct input_plugin_data
*ip_data
)
473 static int flac_read(struct input_plugin_data
*ip_data
, char *buffer
, int count
)
475 struct flac_private
*priv
= ip_data
->private;
477 int libflac_suck_count
= 0;
480 int old_pos
= priv
->buf_wpos
;
482 avail
= priv
->buf_wpos
- priv
->buf_rpos
;
488 if (!F(process_single
)(priv
->dec
)) {
489 d_print("process_single failed\n");
492 if (old_pos
== priv
->buf_wpos
) {
493 libflac_suck_count
++;
495 libflac_suck_count
= 0;
497 if (libflac_suck_count
> 5) {
498 d_print("libflac sucks\n");
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
) {
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
519 static int flac_seek(struct input_plugin_data
*ip_data
, double offset
)
521 struct flac_private
*priv
= ip_data
->private;
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;
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
);
541 *comments
= xnew0(struct keyval
, 1);
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
= {
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
};