2 * Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>
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>
15 #include "buffer_tree.h"
20 struct private_flacdec_data {
21 FLAC__StreamDecoder *decoder;
24 * We can not consume directly what was copied by the read callback
25 * because we might need to feed unconsumend bytes to the decoder again
26 * after the read callback ran out of data and returned ABORT. So we
27 * track how many bytes have been fed to libflac but are unconsumed so far.
32 static FLAC__StreamDecoderReadStatus read_cb(
33 __a_unused const FLAC__StreamDecoder *decoder,
34 FLAC__byte buffer[], size_t *bytes, void *client_data)
36 struct filter_node *fn = client_data;
37 struct private_flacdec_data *pfd = fn->private_data;
38 struct btr_node *btrn = fn->btrn;
40 size_t copy, want = *bytes, have;
45 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
47 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
49 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
52 copy = PARA_MIN(want, have);
53 //PARA_CRIT_LOG("want: %zu, have: %zu, unconsumed %zu\n",
54 // want, have, pfd->unconsumed);
55 memcpy(buffer, btr_buf, copy);
56 pfd->unconsumed += copy;
64 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
66 * Nothing was copied. If the input queue of the btrn is smaller than
67 * the minimal input queue size, our parent must have been gone, so
68 * we're not going to get more input. Since our remaining data is not
69 * sufficient do decode a single frame, we have an EOF condition.
71 if (btr_get_input_queue_size(btrn) < fn->min_iqs) {
72 assert(btr_no_parent(btrn));
73 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
76 * We are kind of screwed here. Returning CONTINUE with a byte count of
77 * zero leads to an endless loop, so we must return either EOF or
78 * ABORT. Unfortunately, both options require to flush the decoder
79 * afterwards because libFLAC refuses to resume decoding if the decoder
80 * is in EOF or ABORT state. But flushing implies dropping the decoder
81 * input queue, so buffered data is lost.
83 * We work around this shortcoming by remembering the number of
84 * unconsumed bytes in pfd->unconsumed. In the write/meta callbacks,
85 * this number is decreased whenever a frame has been decoded
86 * successfully and btr_consume() has been called to consume the bytes
87 * corresponding to the decoded frame. After returning ABORT here, the
88 * decoder can be flushed, and we will feed the unconsumed bytes again.
90 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
94 * The exact value does not really matter. It just has to be larger than the
95 * size of the input buffer of the bitstream reader of libflac.
97 #define TELL_CB_DUMMY_VAL 1000000
100 * FLAC__stream_decoder_get_decode_position() invokes this callback. The flac
101 * library then gets the number of unconsumed bytes from the bitstream reader,
102 * subtracts this number from the offset returned here and returns the
103 * difference as the decode position.
105 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
106 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
108 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
109 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
113 * There is no API function that returns the number of unconsumed bytes
114 * directly. The trick is to define a tell callback which always returns a
115 * fixed dummy value and compute the number of unconsumed bytes from the return
116 * value of FLAC__stream_decoder_get_decode_position().
118 static void flac_consume(struct filter_node *fn)
120 struct private_flacdec_data *pfd = fn->private_data;
121 struct btr_node *btrn = fn->btrn;
124 FLAC__stream_decoder_get_decode_position(pfd->decoder, &x);
125 assert(x <= TELL_CB_DUMMY_VAL);
126 x = TELL_CB_DUMMY_VAL - x; /* number of unconsumed bytes */
127 assert(x <= pfd->unconsumed);
128 btr_consume(btrn, pfd->unconsumed - x);
132 static FLAC__StreamDecoderWriteStatus write_cb(
133 const FLAC__StreamDecoder *decoder,
134 const FLAC__Frame *frame,
135 const FLAC__int32 *const buffer[],
138 struct filter_node *fn = client_data;
139 struct btr_node *btrn = fn->btrn;
140 size_t k, n = frame->header.blocksize;
141 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
142 char *outbuffer = para_malloc(n * channels * 2);
145 for (k = 0; k < n; k++) {
146 int sample = buffer[0][k];
147 write_int16_host_endian(outbuffer + 2 * k, sample);
150 for (k = 0; k < n; k++) {
151 int left = buffer[0][k], right = buffer[1][k];
152 write_int16_host_endian(outbuffer + 4 * k, left);
153 write_int16_host_endian(outbuffer + 4 * k + 2, right);
156 btr_add_output(outbuffer, n * channels * 2, btrn);
158 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
161 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
162 __a_unused const FLAC__StreamMetadata *metadata,
165 flac_consume(client_data);
168 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
169 FLAC__StreamDecoderErrorStatus status,
170 __a_unused void *client_data)
172 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
175 static int flacdec_init(struct filter_node *fn)
177 struct private_flacdec_data *pfd = fn->private_data;
178 FLAC__StreamDecoderInitStatus init_status;
180 PARA_INFO_LOG("initializing flac decoder\n");
181 pfd->decoder = FLAC__stream_decoder_new();
183 return -E_FLACDEC_DECODER_ALLOC;
184 FLAC__stream_decoder_set_metadata_respond_all(pfd->decoder);
185 init_status = FLAC__stream_decoder_init_stream(pfd->decoder, read_cb,
186 NULL /* seek */, tell_cb, NULL /* length_cb */, NULL /* eof_cb */,
187 write_cb, meta_cb, error_cb, fn);
188 if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_OK)
190 FLAC__stream_decoder_delete(pfd->decoder);
191 return -E_FLACDEC_DECODER_INIT;
194 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
197 struct filter_node *fn = btr_context(btrn);
198 struct private_flacdec_data *pfd = fn->private_data;
199 unsigned sample_rate = FLAC__stream_decoder_get_sample_rate(pfd->decoder);
200 unsigned channels = FLAC__stream_decoder_get_channels(pfd->decoder);
202 return decoder_execute(cmd, sample_rate, channels, result);
205 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
207 static bool output_queue_full(struct btr_node *btrn)
209 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
212 static void flacdec_pre_select(struct sched *s, void *context)
214 struct filter_node *fn = context;
215 struct private_flacdec_data *pfd = fn->private_data;
216 struct btr_node *btrn = fn->btrn;
219 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
221 return sched_min_delay(s);
222 if (output_queue_full(btrn))
223 return sched_request_timeout_ms(30, s);
224 if (ret > 0 || pfd->have_more)
225 return sched_min_delay(s);
228 static int flacdec_post_select(__a_unused struct sched *s, void *context)
230 struct filter_node *fn = context;
231 struct private_flacdec_data *pfd = fn->private_data;
232 struct btr_node *btrn = fn->btrn;
234 FLAC__StreamDecoderState state;
236 if (output_queue_full(btrn))
238 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
239 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
241 if (ret <= 0 && !pfd->have_more) /* nothing to do */
244 ret = flacdec_init(fn);
247 if (output_queue_full(btrn)) {
248 pfd->have_more = true;
252 pfd->have_more = false;
253 FLAC__stream_decoder_process_single(pfd->decoder);
254 state = FLAC__stream_decoder_get_state(pfd->decoder);
255 ret = -E_FLACDEC_EOF;
256 if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
258 if (state == FLAC__STREAM_DECODER_ABORTED) {
259 FLAC__stream_decoder_flush(pfd->decoder);
260 pfd->unconsumed = 0; /* feed unconsumed bytes again */
261 fn->min_iqs = btr_get_input_queue_size(btrn) + 1;
265 pfd->have_more = true;
270 btr_remove_node(&fn->btrn);
274 static void flacdec_close(struct filter_node *fn)
276 struct private_flacdec_data *pfd;
280 pfd = fn->private_data;
284 FLAC__stream_decoder_finish(pfd->decoder);
285 FLAC__stream_decoder_delete(pfd->decoder);
288 fn->private_data = NULL;
291 static void flacdec_open(struct filter_node *fn)
293 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
294 fn->private_data = pfd;
298 const struct filter lsg_filter_cmd_com_flacdec_user_data = {
299 .open = flacdec_open,
300 .close = flacdec_close,
301 .pre_select = flacdec_pre_select,
302 .post_select = flacdec_post_select,
303 .execute = flacdec_execute,