1 /* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file flacdec_filter.c The flac decoder. */
6 #include <FLAC/stream_decoder.h>
11 #include "buffer_tree.h"
16 struct private_flacdec_data {
17 FLAC__StreamDecoder *decoder;
20 * We can not consume directly what was copied by the read callback
21 * because we might need to feed unconsumend bytes to the decoder again
22 * after the read callback ran out of data and returned ABORT. So we
23 * track how many bytes have been fed to libflac but are unconsumed so far.
28 static FLAC__StreamDecoderReadStatus read_cb(
29 __a_unused const FLAC__StreamDecoder *decoder,
30 FLAC__byte buffer[], size_t *bytes, void *client_data)
32 struct filter_node *fn = client_data;
33 struct private_flacdec_data *pfd = fn->private_data;
34 struct btr_node *btrn = fn->btrn;
36 size_t copy, want = *bytes, have;
41 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
43 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
45 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
48 copy = PARA_MIN(want, have);
49 //PARA_CRIT_LOG("want: %zu, have: %zu, unconsumed %zu\n",
50 // want, have, pfd->unconsumed);
51 memcpy(buffer, btr_buf, copy);
52 pfd->unconsumed += copy;
60 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
62 * Nothing was copied. If the input queue of the btrn is smaller than
63 * the minimal input queue size, our parent must have been gone, so
64 * we're not going to get more input. Since our remaining data is not
65 * sufficient do decode a single frame, we have an EOF condition.
67 if (btr_get_input_queue_size(btrn) < fn->min_iqs) {
68 assert(btr_no_parent(btrn));
69 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
72 * We are kind of screwed here. Returning CONTINUE with a byte count of
73 * zero leads to an endless loop, so we must return either EOF or
74 * ABORT. Unfortunately, both options require to flush the decoder
75 * afterwards because libFLAC refuses to resume decoding if the decoder
76 * is in EOF or ABORT state. But flushing implies dropping the decoder
77 * input queue, so buffered data is lost.
79 * We work around this shortcoming by remembering the number of
80 * unconsumed bytes in pfd->unconsumed. In the write/meta callbacks,
81 * this number is decreased whenever a frame has been decoded
82 * successfully and btr_consume() has been called to consume the bytes
83 * corresponding to the decoded frame. After returning ABORT here, the
84 * decoder can be flushed, and we will feed the unconsumed bytes again.
86 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
90 * The exact value does not really matter. It just has to be larger than the
91 * size of the input buffer of the bitstream reader of libflac.
93 #define TELL_CB_DUMMY_VAL 1000000
96 * FLAC__stream_decoder_get_decode_position() invokes this callback. The flac
97 * library then gets the number of unconsumed bytes from the bitstream reader,
98 * subtracts this number from the offset returned here and returns the
99 * difference as the decode position.
101 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
102 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
104 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
105 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
109 * There is no API function that returns the number of unconsumed bytes
110 * directly. The trick is to define a tell callback which always returns a
111 * fixed dummy value and compute the number of unconsumed bytes from the return
112 * value of FLAC__stream_decoder_get_decode_position().
114 static void flac_consume(struct filter_node *fn)
116 struct private_flacdec_data *pfd = fn->private_data;
117 struct btr_node *btrn = fn->btrn;
120 FLAC__stream_decoder_get_decode_position(pfd->decoder, &x);
121 assert(x <= TELL_CB_DUMMY_VAL);
122 x = TELL_CB_DUMMY_VAL - x; /* number of unconsumed bytes */
123 assert(x <= pfd->unconsumed);
124 btr_consume(btrn, pfd->unconsumed - x);
128 static FLAC__StreamDecoderWriteStatus write_cb(
129 const FLAC__StreamDecoder *decoder,
130 const FLAC__Frame *frame,
131 const FLAC__int32 *const buffer[],
134 struct filter_node *fn = client_data;
135 struct btr_node *btrn = fn->btrn;
136 size_t k, n = frame->header.blocksize;
137 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
138 char *outbuffer = arr_alloc(n, channels * 2);
141 for (k = 0; k < n; k++) {
142 int sample = buffer[0][k];
143 write_int16_host_endian(outbuffer + 2 * k, sample);
146 for (k = 0; k < n; k++) {
147 int left = buffer[0][k], right = buffer[1][k];
148 write_int16_host_endian(outbuffer + 4 * k, left);
149 write_int16_host_endian(outbuffer + 4 * k + 2, right);
152 btr_add_output(outbuffer, n * channels * 2, btrn);
154 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
157 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
158 __a_unused const FLAC__StreamMetadata *metadata,
161 flac_consume(client_data);
164 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
165 FLAC__StreamDecoderErrorStatus status,
166 __a_unused void *client_data)
168 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
171 static int flacdec_init(struct filter_node *fn)
173 struct private_flacdec_data *pfd = fn->private_data;
174 FLAC__StreamDecoderInitStatus init_status;
176 PARA_INFO_LOG("initializing flac decoder\n");
177 pfd->decoder = FLAC__stream_decoder_new();
179 return -E_FLACDEC_DECODER_ALLOC;
180 FLAC__stream_decoder_set_metadata_respond_all(pfd->decoder);
181 init_status = FLAC__stream_decoder_init_stream(pfd->decoder, read_cb,
182 NULL /* seek */, tell_cb, NULL /* length_cb */, NULL /* eof_cb */,
183 write_cb, meta_cb, error_cb, fn);
184 if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_OK)
186 FLAC__stream_decoder_delete(pfd->decoder);
187 return -E_FLACDEC_DECODER_INIT;
190 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
193 struct filter_node *fn = btr_context(btrn);
194 struct private_flacdec_data *pfd = fn->private_data;
195 unsigned sample_rate = FLAC__stream_decoder_get_sample_rate(pfd->decoder);
196 unsigned channels = FLAC__stream_decoder_get_channels(pfd->decoder);
198 return decoder_execute(cmd, sample_rate, channels, result);
201 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
203 static bool output_queue_full(struct btr_node *btrn)
205 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
208 static void flacdec_pre_monitor(struct sched *s, void *context)
210 struct filter_node *fn = context;
211 struct private_flacdec_data *pfd = fn->private_data;
212 struct btr_node *btrn = fn->btrn;
215 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
217 return sched_min_delay(s);
218 if (output_queue_full(btrn))
219 return sched_request_timeout_ms(30, s);
220 if (ret > 0 || pfd->have_more)
221 return sched_min_delay(s);
224 static int flacdec_post_monitor(__a_unused struct sched *s, void *context)
226 struct filter_node *fn = context;
227 struct private_flacdec_data *pfd = fn->private_data;
228 struct btr_node *btrn = fn->btrn;
230 FLAC__StreamDecoderState state;
232 if (output_queue_full(btrn))
234 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
235 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
237 if (ret <= 0 && !pfd->have_more) /* nothing to do */
240 ret = flacdec_init(fn);
243 if (output_queue_full(btrn)) {
244 pfd->have_more = true;
248 pfd->have_more = false;
249 FLAC__stream_decoder_process_single(pfd->decoder);
250 state = FLAC__stream_decoder_get_state(pfd->decoder);
251 ret = -E_FLACDEC_EOF;
252 if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
254 if (state == FLAC__STREAM_DECODER_ABORTED) {
255 FLAC__stream_decoder_flush(pfd->decoder);
256 pfd->unconsumed = 0; /* feed unconsumed bytes again */
257 fn->min_iqs = btr_get_input_queue_size(btrn) + 1;
261 pfd->have_more = true;
266 btr_remove_node(&fn->btrn);
270 static void flacdec_close(struct filter_node *fn)
272 struct private_flacdec_data *pfd;
276 pfd = fn->private_data;
280 FLAC__stream_decoder_finish(pfd->decoder);
281 FLAC__stream_decoder_delete(pfd->decoder);
284 fn->private_data = NULL;
287 static void flacdec_open(struct filter_node *fn)
289 struct private_flacdec_data *pfd = zalloc(sizeof(*pfd));
290 fn->private_data = pfd;
294 const struct filter lsg_filter_cmd_com_flacdec_user_data = {
295 .open = flacdec_open,
296 .close = flacdec_close,
297 .pre_monitor = flacdec_pre_monitor,
298 .post_monitor = flacdec_post_monitor,
299 .execute = flacdec_execute,