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>
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 have been fed to libflac but 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 * Nothing was copied. If the input queue of the btrn is smaller than
68 * the minimal input queue size, our parent must have been gone, so
69 * we're not going to get more input. Since our remaining data is not
70 * sufficient do decode a single frame, we have an EOF condition.
72 if (btr_get_input_queue_size(btrn) < fn->min_iqs) {
73 assert(btr_no_parent(btrn));
74 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
77 * We are kind of screwed here. Returning CONTINUE with a byte count of
78 * zero leads to an endless loop, so we must return either EOF or
79 * ABORT. Unfortunately, both options require to flush the decoder
80 * afterwards because libFLAC refuses to resume decoding if the decoder
81 * is in EOF or ABORT state. But flushing implies dropping the decoder
82 * input queue, so buffered data is lost.
84 * We work around this shortcoming by remembering the number of
85 * unconsumed bytes in pfd->unconsumed. In the write/meta callbacks,
86 * this number is decreased whenever a frame has been decoded
87 * successfully and btr_consume() has been called to consume the bytes
88 * corresponding to the decoded frame. After returning ABORT here, the
89 * decoder can be flushed, and we will feed the unconsumed bytes again.
91 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
95 * The exact value does not really matter. It just has to be larger than the
96 * size of the input buffer of the bitstream reader of libflac.
98 #define TELL_CB_DUMMY_VAL 1000000
101 * FLAC__stream_decoder_get_decode_position() invokes this callback. The flac
102 * library then gets the number of unconsumed bytes from the bitstream reader,
103 * subtracts this number from the offset returned here and returns the
104 * difference as the decode position.
106 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
107 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
109 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
110 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
114 * There is no API function that returns the number of unconsumed bytes
115 * directly. The trick is to define a tell callback which always returns a
116 * fixed dummy value and compute the number of unconsumed bytes from the return
117 * value of FLAC__stream_decoder_get_decode_position().
119 static void flac_consume(struct filter_node *fn)
121 struct private_flacdec_data *pfd = fn->private_data;
122 struct btr_node *btrn = fn->btrn;
125 FLAC__stream_decoder_get_decode_position(pfd->decoder, &x);
126 assert(x <= TELL_CB_DUMMY_VAL);
127 x = TELL_CB_DUMMY_VAL - x; /* number of unconsumed bytes */
128 assert(x <= pfd->unconsumed);
129 btr_consume(btrn, pfd->unconsumed - x);
133 static FLAC__StreamDecoderWriteStatus write_cb(
134 const FLAC__StreamDecoder *decoder,
135 const FLAC__Frame *frame,
136 const FLAC__int32 *const buffer[],
139 struct filter_node *fn = client_data;
140 struct btr_node *btrn = fn->btrn;
141 size_t k, n = frame->header.blocksize;
142 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
143 char *outbuffer = para_malloc(n * channels * 2);
146 for (k = 0; k < n; k++) {
147 int sample = buffer[0][k];
148 write_int16_host_endian(outbuffer + 2 * k, sample);
151 for (k = 0; k < n; k++) {
152 int left = buffer[0][k], right = buffer[1][k];
153 write_int16_host_endian(outbuffer + 4 * k, left);
154 write_int16_host_endian(outbuffer + 4 * k + 2, right);
157 btr_add_output(outbuffer, n * channels * 2, btrn);
159 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
162 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
163 __a_unused const FLAC__StreamMetadata *metadata,
166 flac_consume(client_data);
169 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
170 FLAC__StreamDecoderErrorStatus status,
171 __a_unused void *client_data)
173 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
176 static int flacdec_init(struct filter_node *fn)
178 struct private_flacdec_data *pfd = fn->private_data;
179 FLAC__StreamDecoderInitStatus init_status;
181 PARA_INFO_LOG("initializing flac decoder\n");
182 pfd->decoder = FLAC__stream_decoder_new();
184 return -E_FLACDEC_DECODER_ALLOC;
185 FLAC__stream_decoder_set_metadata_respond_all(pfd->decoder);
186 init_status = FLAC__stream_decoder_init_stream(pfd->decoder, read_cb,
187 NULL /* seek */, tell_cb, NULL /* length_cb */, NULL /* eof_cb */,
188 write_cb, meta_cb, error_cb, fn);
189 if (init_status == FLAC__STREAM_DECODER_INIT_STATUS_OK)
191 FLAC__stream_decoder_delete(pfd->decoder);
192 return -E_FLACDEC_DECODER_INIT;
195 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
198 struct filter_node *fn = btr_context(btrn);
199 struct private_flacdec_data *pfd = fn->private_data;
200 unsigned sample_rate = FLAC__stream_decoder_get_sample_rate(pfd->decoder);
201 unsigned channels = FLAC__stream_decoder_get_channels(pfd->decoder);
203 return decoder_execute(cmd, sample_rate, channels, result);
206 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
208 static bool output_queue_full(struct btr_node *btrn)
210 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
213 static void flacdec_pre_select(struct sched *s, void *context)
215 struct filter_node *fn = context;
216 struct private_flacdec_data *pfd = fn->private_data;
217 struct btr_node *btrn = fn->btrn;
220 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
222 return sched_min_delay(s);
223 if (output_queue_full(btrn))
224 return sched_request_timeout_ms(30, s);
225 if (ret > 0 || pfd->have_more)
226 return sched_min_delay(s);
229 static int flacdec_post_select(__a_unused struct sched *s, void *context)
231 struct filter_node *fn = context;
232 struct private_flacdec_data *pfd = fn->private_data;
233 struct btr_node *btrn = fn->btrn;
235 FLAC__StreamDecoderState state;
237 if (output_queue_full(btrn))
239 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
240 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
242 if (ret <= 0 && !pfd->have_more) /* nothing to do */
245 ret = flacdec_init(fn);
248 if (output_queue_full(btrn)) {
249 pfd->have_more = true;
253 pfd->have_more = false;
254 FLAC__stream_decoder_process_single(pfd->decoder);
255 state = FLAC__stream_decoder_get_state(pfd->decoder);
256 ret = -E_FLACDEC_EOF;
257 if (state == FLAC__STREAM_DECODER_END_OF_STREAM)
259 if (state == FLAC__STREAM_DECODER_ABORTED) {
260 FLAC__stream_decoder_flush(pfd->decoder);
261 pfd->unconsumed = 0; /* feed unconsumed bytes again */
262 fn->min_iqs = btr_get_input_queue_size(btrn) + 1;
266 pfd->have_more = true;
271 btr_remove_node(&fn->btrn);
275 static void flacdec_close(struct filter_node *fn)
277 struct private_flacdec_data *pfd;
281 pfd = fn->private_data;
285 FLAC__stream_decoder_finish(pfd->decoder);
286 FLAC__stream_decoder_delete(pfd->decoder);
289 fn->private_data = NULL;
292 static void flacdec_open(struct filter_node *fn)
294 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
295 fn->private_data = pfd;
300 * The init function of the flacdec filter.
302 * \param f Pointer to the filter struct to initialize.
306 void flacdec_filter_init(struct filter *f)
308 f->open = flacdec_open;
309 f->close = flacdec_close;
310 f->pre_select = flacdec_pre_select;
311 f->post_select = flacdec_post_select;
312 f->execute = flacdec_execute;