2 * Copyright (C) 2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file flacdec_filter.c The flac decoder. */
11 #include <FLAC/stream_decoder.h>
17 #include "buffer_tree.h"
22 struct private_flacdec_data {
23 FLAC__StreamDecoder *decoder;
26 * We can not consume directly what was copied by the read callback
27 * because we might need to feed unconsumend bytes to the decoder again
28 * after the read callback ran out of data and returned ABORT. So we
29 * track how many bytes are unconsumed so far.
34 static FLAC__StreamDecoderReadStatus read_cb(
35 __a_unused const FLAC__StreamDecoder *decoder,
36 FLAC__byte buffer[], size_t *bytes, void *client_data)
38 struct filter_node *fn = client_data;
39 struct private_flacdec_data *pfd = fn->private_data;
40 struct btr_node *btrn = fn->btrn;
42 size_t copy, want = *bytes, have;
47 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
49 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
51 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
54 copy = PARA_MIN(want, have);
55 //PARA_CRIT_LOG("want: %zu, have: %zu, unconsumed %zu\n",
56 // want, have, pfd->unconsumed);
57 memcpy(buffer, btr_buf, copy);
58 pfd->unconsumed += copy;
66 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
68 * We are kind of screwed here. Returning CONTINUE with a byte count of
69 * zero leads to an endless loop, so we must return either EOF or
70 * ABORT. Unfortunately, both options require to flush the decoder
71 * afterwards because libFLAC refuses to resume decoding if the decoder
72 * is in EOF or ABORT state. But flushing implies dropping the decoder
73 * input queue, so buffered data is lost.
75 * We work around this shortcoming by remembering the number of
76 * unconsumed bytes in pfd->unconsumed. In the write/meta callbacks,
77 * this number is decreased whenever a frame has been decoded
78 * successfully and btr_consume() has been called to consume the bytes
79 * corresponding to the decoded frame. After returning ABORT here, the
80 * decoder can be flushed, and we will feed the unconsumed bytes again.
82 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
86 * The exact value does not really matter. It just has to be larger than the
87 * size of the input buffer of the bitstream reader of libflac.
89 #define TELL_CB_DUMMY_VAL 1000000
92 * FLAC__stream_decoder_get_decode_position() invokes this callback. The flac
93 * library then gets the number of unconsumed bytes from the bitstream reader,
94 * subtracts this number from the offset returned here and returns the
95 * difference as the decode position.
97 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
98 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
100 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
101 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
105 * There is no API function that returns the number of unconsumed bytes
106 * directly. The trick is to define a tell callback which always returns a
107 * fixed dummy value and compute the number of unconsumed bytes from the return
108 * value of FLAC__stream_decoder_get_decode_position().
110 static void flac_consume(struct filter_node *fn)
112 struct private_flacdec_data *pfd = fn->private_data;
113 struct btr_node *btrn = fn->btrn;
116 FLAC__stream_decoder_get_decode_position(pfd->decoder, &x);
117 assert(x <= TELL_CB_DUMMY_VAL);
118 x = TELL_CB_DUMMY_VAL - x; /* number of unconsumed bytes */
119 assert(x <= pfd->unconsumed);
120 btr_consume(btrn, pfd->unconsumed - x);
124 static FLAC__StreamDecoderWriteStatus write_cb(
125 const FLAC__StreamDecoder *decoder,
126 const FLAC__Frame *frame,
127 const FLAC__int32 *const buffer[],
130 struct filter_node *fn = client_data;
131 struct btr_node *btrn = fn->btrn;
132 size_t k, n = frame->header.blocksize;
133 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
134 char *outbuffer = para_malloc(n * channels * 2);
137 for (k = 0; k < n; k++) {
138 int sample = buffer[0][k];
139 write_int16_host_endian(outbuffer + 2 * k, sample);
142 for (k = 0; k < n; k++) {
143 int left = buffer[0][k], right = buffer[1][k];
144 write_int16_host_endian(outbuffer + 4 * k, left);
145 write_int16_host_endian(outbuffer + 4 * k + 2, right);
148 btr_add_output(outbuffer, n * 4, btrn);
150 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
153 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
154 __a_unused const FLAC__StreamMetadata *metadata,
157 flac_consume(client_data);
160 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
161 FLAC__StreamDecoderErrorStatus status,
162 __a_unused void *client_data)
164 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
167 static int flacdec_init(struct filter_node *fn)
169 struct private_flacdec_data *pfd = fn->private_data;
170 FLAC__StreamDecoderInitStatus init_status;
172 PARA_INFO_LOG("initializing flac decoder\n");
173 pfd->decoder = FLAC__stream_decoder_new();
175 return -E_FLACDEC_DECODER_ALLOC;
176 FLAC__stream_decoder_set_metadata_respond_all(pfd->decoder);
177 init_status = FLAC__stream_decoder_init_stream(pfd->decoder, read_cb,
178 NULL /* seek */, tell_cb, NULL /* length_cb */, NULL /* eof_cb */,
179 write_cb, meta_cb, error_cb, fn);
180 if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_OK)
182 FLAC__stream_decoder_delete(pfd->decoder);
183 return -E_FLACDEC_DECODER_INIT;
186 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
189 struct filter_node *fn = btr_context(btrn);
190 struct private_flacdec_data *pfd = fn->private_data;
191 unsigned sample_rate = FLAC__stream_decoder_get_sample_rate(pfd->decoder);
192 unsigned channels = FLAC__stream_decoder_get_channels(pfd->decoder);
194 return decoder_execute(cmd, sample_rate, channels, result);
197 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
199 static bool output_queue_full(struct btr_node *btrn)
201 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
204 static void flacdec_pre_select(struct sched *s, struct task *t)
206 struct filter_node *fn = container_of(t, struct filter_node, task);
207 struct private_flacdec_data *pfd = fn->private_data;
208 struct btr_node *btrn = fn->btrn;
211 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
213 return sched_min_delay(s);
214 if (output_queue_full(btrn))
215 return sched_request_timeout_ms(30, s);
216 if (ret > 0 || pfd->have_more)
217 return sched_min_delay(s);
220 static void flacdec_post_select(__a_unused struct sched *s, struct task *t)
222 struct filter_node *fn = container_of(t, struct filter_node, task);
223 struct private_flacdec_data *pfd = fn->private_data;
224 struct btr_node *btrn = fn->btrn;
227 if (output_queue_full(btrn))
229 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
230 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
232 if (ret <= 0 && !pfd->have_more) /* nothing to do */
235 ret = flacdec_init(fn);
240 if (output_queue_full(btrn)) {
241 pfd->have_more = true;
244 pfd->have_more = false;
245 FLAC__StreamDecoderState state;
246 FLAC__stream_decoder_process_single(pfd->decoder);
247 state = FLAC__stream_decoder_get_state(pfd->decoder);
248 //PARA_CRIT_LOG("state: %s\n", FLAC__stream_decoder_get_resolved_state_string(pfd->decoder));
249 ret = -E_FLACDEC_EOF;
250 if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
252 if (state == FLAC__STREAM_DECODER_ABORTED) {
253 FLAC__stream_decoder_flush(pfd->decoder);
254 fn->min_iqs = pfd->unconsumed + 1;
263 btr_remove_node(btrn);
266 static void flacdec_close(struct filter_node *fn)
268 struct private_flacdec_data *pfd = fn->private_data;
270 FLAC__stream_decoder_finish(pfd->decoder);
271 FLAC__stream_decoder_delete(pfd->decoder);
273 fn->private_data = NULL;
276 static void flacdec_open(struct filter_node *fn)
278 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
279 fn->private_data = pfd;
284 * The init function of the flacdec filter.
286 * \param f Pointer to the filter struct to initialize.
290 void flacdec_filter_init(struct filter *f)
292 f->open = flacdec_open;
293 f->close = flacdec_close;
294 f->pre_select = flacdec_pre_select;
295 f->post_select = flacdec_post_select;
296 f->execute = flacdec_execute;