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
;
60 unsigned int ignore_next_write
: 1;
64 static T(ReadStatus
) read_cb(const Dec
*dec
, unsigned char *buf
, size_t *size
, void *data
)
66 static T(ReadStatus
) read_cb(const Dec
*dec
, unsigned char *buf
, unsigned *size
, void *data
)
69 struct input_plugin_data
*ip_data
= data
;
70 struct flac_private
*priv
= ip_data
->private;
73 if (priv
->pos
== priv
->len
) {
76 return E(READ_STATUS_END_OF_STREAM
);
78 return E(READ_STATUS_OK
);
83 return E(READ_STATUS_CONTINUE
);
85 return E(READ_STATUS_OK
);
88 rc
= read(ip_data
->fd
, buf
, *size
);
91 if (errno
== EINTR
|| errno
== EAGAIN
) {
92 /* FIXME: not sure how the flac decoder handles this */
93 d_print("interrupted\n");
95 return E(READ_STATUS_CONTINUE
);
97 return E(READ_STATUS_OK
);
101 return E(READ_STATUS_ABORT
);
103 return E(READ_STATUS_ERROR
);
110 /* should not happen */
112 return E(READ_STATUS_END_OF_STREAM
);
114 return E(READ_STATUS_OK
);
118 return E(READ_STATUS_CONTINUE
);
120 return E(READ_STATUS_OK
);
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;
130 if (priv
->len
== UINT64_MAX
)
131 return E(SEEK_STATUS_ERROR
);
132 off
= lseek(ip_data
->fd
, offset
, SEEK_SET
);
134 return E(SEEK_STATUS_ERROR
);
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;
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;
156 if (ip_data
->remote
) {
157 return E(LENGTH_STATUS_ERROR
);
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
;
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
;
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
) {
221 priv
->buf
= xrenew(char, priv
->buf
, size
);
222 priv
->buf_size
= size
;
225 depth
= frame
->header
.bits_per_sample
;
227 char *b
= priv
->buf
+ priv
->buf_wpos
;
229 for (i
= 0; i
< frames
; i
++) {
230 for (ch
= 0; ch
< channels
; ch
++)
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);
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
;
293 switch (si
->bits_per_sample
) {
297 bits
= si
->bits_per_sample
;
308 ip_data
->sf
= sf_rate(si
->sample_rate
) |
311 sf_channels(si
->channels
);
312 if (!ip_data
->remote
&& si
->total_samples
)
313 priv
->duration
= si
->total_samples
/ si
->sample_rate
;
316 case FLAC__METADATA_TYPE_VORBIS_COMMENT
:
317 d_print("VORBISCOMMENT\n");
318 if (priv
->comments
) {
319 d_print("Ignoring\n");
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
;
329 val
= strchr(str
, '=');
332 key
= xstrndup(str
, val
- str
);
333 val
= xstrdup(val
+ 1);
334 comments_add(&c
, key
, val
);
337 keyvals_terminate(&c
);
338 priv
->comments
= c
.keyvals
;
342 d_print("something else\n");
347 static void error_cb(const Dec
*dec
, FLAC__StreamDecoderErrorStatus status
, void *data
)
349 static const char *strings
[3] = {
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;
366 F(finish
)(priv
->dec
);
367 F(delete)(priv
->dec
);
369 keyvals_free(priv
->comments
);
372 ip_data
->private = NULL
;
376 static int flac_open(struct input_plugin_data
*ip_data
)
378 struct flac_private
*priv
;
383 return -IP_ERROR_INTERNAL
;
385 priv
= xnew0(struct flac_private
, 1);
388 if (ip_data
->remote
) {
389 priv
->len
= UINT64_MAX
;
391 off_t off
= lseek(ip_data
->fd
, 0, SEEK_END
);
393 if (off
== -1 || lseek(ip_data
->fd
, 0, SEEK_SET
) == -1) {
399 return -IP_ERROR_ERRNO
;
403 ip_data
->private = priv
;
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
);
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
)) {
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
)) {
430 d_print("init failed\n");
431 F(delete)(priv
->dec
);
433 ip_data
->private = NULL
;
435 return -IP_ERROR_ERRNO
;
439 while (priv
->buf_wpos
== 0 && priv
->pos
< priv
->len
) {
440 if (!F(process_single
)(priv
->dec
)) {
442 return -IP_ERROR_ERRNO
;
448 return -IP_ERROR_FILE_FORMAT
;
450 if (!sf_get_bits(ip_data
->sf
)) {
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
));
462 static int flac_close(struct input_plugin_data
*ip_data
)
468 static int flac_read(struct input_plugin_data
*ip_data
, char *buffer
, int count
)
470 struct flac_private
*priv
= ip_data
->private;
474 avail
= priv
->buf_wpos
- priv
->buf_rpos
;
478 if (priv
->pos
== priv
->len
)
480 if (!F(process_single
)(priv
->dec
)) {
481 d_print("process_single failed\n");
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
) {
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
501 static int flac_seek(struct input_plugin_data
*ip_data
, double offset
)
503 struct flac_private
*priv
= ip_data
->private;
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;
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
);
523 *comments
= xnew0(struct keyval
, 1);
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
= {
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
};