2 * Copyright (C) 2011-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file flacdec_filter.c The flac decoder. */
10 #include <FLAC/stream_decoder.h>
16 #include "buffer_tree.h"
21 struct private_flacdec_data {
22 FLAC__StreamDecoder *decoder;
25 * We can not consume directly what was copied by the read callback
26 * because we might need to feed unconsumend bytes to the decoder again
27 * after the read callback ran out of data and returned ABORT. So we
28 * track how many bytes are unconsumed so far.
33 static FLAC__StreamDecoderReadStatus read_cb(
34 __a_unused const FLAC__StreamDecoder *decoder,
35 FLAC__byte buffer[], size_t *bytes, void *client_data)
37 struct filter_node *fn = client_data;
38 struct private_flacdec_data *pfd = fn->private_data;
39 struct btr_node *btrn = fn->btrn;
41 size_t copy, want = *bytes, have;
46 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
48 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
50 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
53 copy = PARA_MIN(want, have);
54 //PARA_CRIT_LOG("want: %zu, have: %zu, unconsumed %zu\n",
55 // want, have, pfd->unconsumed);
56 memcpy(buffer, btr_buf, copy);
57 pfd->unconsumed += copy;
65 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
67 * We are kind of screwed here. Returning CONTINUE with a byte count of
68 * zero leads to an endless loop, so we must return either EOF or
69 * ABORT. Unfortunately, both options require to flush the decoder
70 * afterwards because libFLAC refuses to resume decoding if the decoder
71 * is in EOF or ABORT state. But flushing implies dropping the decoder
72 * input queue, so buffered data is lost.
74 * We work around this shortcoming by remembering the number of
75 * unconsumed bytes in pfd->unconsumed. In the write/meta callbacks,
76 * this number is decreased whenever a frame has been decoded
77 * successfully and btr_consume() has been called to consume the bytes
78 * corresponding to the decoded frame. After returning ABORT here, the
79 * decoder can be flushed, and we will feed the unconsumed bytes again.
81 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
85 * The exact value does not really matter. It just has to be larger than the
86 * size of the input buffer of the bitstream reader of libflac.
88 #define TELL_CB_DUMMY_VAL 1000000
91 * FLAC__stream_decoder_get_decode_position() invokes this callback. The flac
92 * library then gets the number of unconsumed bytes from the bitstream reader,
93 * subtracts this number from the offset returned here and returns the
94 * difference as the decode position.
96 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
97 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
99 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
100 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
104 * There is no API function that returns the number of unconsumed bytes
105 * directly. The trick is to define a tell callback which always returns a
106 * fixed dummy value and compute the number of unconsumed bytes from the return
107 * value of FLAC__stream_decoder_get_decode_position().
109 static void flac_consume(struct filter_node *fn)
111 struct private_flacdec_data *pfd = fn->private_data;
112 struct btr_node *btrn = fn->btrn;
115 FLAC__stream_decoder_get_decode_position(pfd->decoder, &x);
116 assert(x <= TELL_CB_DUMMY_VAL);
117 x = TELL_CB_DUMMY_VAL - x; /* number of unconsumed bytes */
118 assert(x <= pfd->unconsumed);
119 btr_consume(btrn, pfd->unconsumed - x);
123 static FLAC__StreamDecoderWriteStatus write_cb(
124 const FLAC__StreamDecoder *decoder,
125 const FLAC__Frame *frame,
126 const FLAC__int32 *const buffer[],
129 struct filter_node *fn = client_data;
130 struct btr_node *btrn = fn->btrn;
131 size_t k, n = frame->header.blocksize;
132 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
133 char *outbuffer = para_malloc(n * channels * 2);
136 for (k = 0; k < n; k++) {
137 int sample = buffer[0][k];
138 write_int16_host_endian(outbuffer + 2 * k, sample);
141 for (k = 0; k < n; k++) {
142 int left = buffer[0][k], right = buffer[1][k];
143 write_int16_host_endian(outbuffer + 4 * k, left);
144 write_int16_host_endian(outbuffer + 4 * k + 2, right);
147 btr_add_output(outbuffer, n * 4, btrn);
149 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
152 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
153 __a_unused const FLAC__StreamMetadata *metadata,
156 flac_consume(client_data);
159 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
160 FLAC__StreamDecoderErrorStatus status,
161 __a_unused void *client_data)
163 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
166 static int flacdec_init(struct filter_node *fn)
168 struct private_flacdec_data *pfd = fn->private_data;
169 FLAC__StreamDecoderInitStatus init_status;
171 PARA_INFO_LOG("initializing flac decoder\n");
172 pfd->decoder = FLAC__stream_decoder_new();
174 return -E_FLACDEC_DECODER_ALLOC;
175 FLAC__stream_decoder_set_metadata_respond_all(pfd->decoder);
176 init_status = FLAC__stream_decoder_init_stream(pfd->decoder, read_cb,
177 NULL /* seek */, tell_cb, NULL /* length_cb */, NULL /* eof_cb */,
178 write_cb, meta_cb, error_cb, fn);
179 if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_OK)
181 FLAC__stream_decoder_delete(pfd->decoder);
182 return -E_FLACDEC_DECODER_INIT;
185 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
188 struct filter_node *fn = btr_context(btrn);
189 struct private_flacdec_data *pfd = fn->private_data;
190 unsigned sample_rate = FLAC__stream_decoder_get_sample_rate(pfd->decoder);
191 unsigned channels = FLAC__stream_decoder_get_channels(pfd->decoder);
193 return decoder_execute(cmd, sample_rate, channels, result);
196 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
198 static bool output_queue_full(struct btr_node *btrn)
200 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
203 static void flacdec_pre_select(struct sched *s, struct task *t)
205 struct filter_node *fn = container_of(t, struct filter_node, task);
206 struct private_flacdec_data *pfd = fn->private_data;
207 struct btr_node *btrn = fn->btrn;
210 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
212 return sched_min_delay(s);
213 if (output_queue_full(btrn))
214 return sched_request_timeout_ms(30, s);
215 if (ret > 0 || pfd->have_more)
216 return sched_min_delay(s);
219 static void flacdec_post_select(__a_unused struct sched *s, struct task *t)
221 struct filter_node *fn = container_of(t, struct filter_node, task);
222 struct private_flacdec_data *pfd = fn->private_data;
223 struct btr_node *btrn = fn->btrn;
226 if (output_queue_full(btrn))
228 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
229 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
231 if (ret <= 0 && !pfd->have_more) /* nothing to do */
234 ret = flacdec_init(fn);
239 if (output_queue_full(btrn)) {
240 pfd->have_more = true;
243 pfd->have_more = false;
244 FLAC__StreamDecoderState state;
245 FLAC__stream_decoder_process_single(pfd->decoder);
246 state = FLAC__stream_decoder_get_state(pfd->decoder);
247 //PARA_CRIT_LOG("state: %s\n", FLAC__stream_decoder_get_resolved_state_string(pfd->decoder));
248 ret = -E_FLACDEC_EOF;
249 if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
251 if (state == FLAC__STREAM_DECODER_ABORTED) {
252 FLAC__stream_decoder_flush(pfd->decoder);
253 fn->min_iqs = pfd->unconsumed + 1;
262 btr_remove_node(btrn);
265 static void flacdec_close(struct filter_node *fn)
267 struct private_flacdec_data *pfd = fn->private_data;
269 FLAC__stream_decoder_finish(pfd->decoder);
270 FLAC__stream_decoder_delete(pfd->decoder);
272 fn->private_data = NULL;
275 static void flacdec_open(struct filter_node *fn)
277 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
278 fn->private_data = pfd;
283 * The init function of the flacdec filter.
285 * \param f Pointer to the filter struct to initialize.
289 void flacdec_filter_init(struct filter *f)
291 f->open = flacdec_open;
292 f->close = flacdec_close;
293 f->pre_select = flacdec_pre_select;
294 f->post_select = flacdec_post_select;
295 f->execute = flacdec_execute;