Implement the flac decoding filter.
[paraslash.git] / flacdec_filter.c
1 /*
2 * Copyright (C) 2011 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file flacdec_filter.c The flac decoder. */
8
9 #include <regex.h>
10 #include <stdbool.h>
11 #include <FLAC/stream_decoder.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 struct private_flacdec_data {
23 FLAC__StreamDecoder *decoder;
24 bool have_more;
25 /*
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.
30 */
31 size_t unconsumed;
32 };
33
34 static FLAC__StreamDecoderReadStatus read_cb(
35 __a_unused const FLAC__StreamDecoder *decoder,
36 FLAC__byte buffer[], size_t *bytes, void *client_data)
37 {
38 struct filter_node *fn = client_data;
39 struct private_flacdec_data *pfd = fn->private_data;
40 struct btr_node *btrn = fn->btrn;
41 char *btr_buf;
42 size_t copy, want = *bytes, have;
43 int ns;
44
45 *bytes = 0;
46 assert(want > 0);
47 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
48 if (ns < 0)
49 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
50 for (;;) {
51 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
52 if (have == 0)
53 break;
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;
59 *bytes += copy;
60 buffer += copy;
61 want -= copy;
62 if (want == 0)
63 break;
64 }
65 if (*bytes > 0)
66 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
67 /*
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.
74 *
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.
81 */
82 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
83 }
84
85 /*
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.
88 */
89 #define TELL_CB_DUMMY_VAL 1000000
90
91 /*
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.
96 */
97 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
98 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
99 {
100 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
101 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
102 }
103
104 /*
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().
109 */
110 static void flac_consume(struct filter_node *fn)
111 {
112 struct private_flacdec_data *pfd = fn->private_data;
113 struct btr_node *btrn = fn->btrn;
114 FLAC__uint64 x;
115
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);
121 pfd->unconsumed = x;
122 }
123
124 static FLAC__StreamDecoderWriteStatus write_cb(
125 const FLAC__StreamDecoder *decoder,
126 const FLAC__Frame *frame,
127 const FLAC__int32 *const buffer[],
128 void *client_data)
129 {
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);
135
136 if (channels == 1) {
137 for (k = 0; k < n; k++) {
138 int sample = buffer[0][k];
139 write_int16_host_endian(outbuffer + 2 * k, sample);
140 }
141 } else {
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);
146 }
147 }
148 btr_add_output(outbuffer, n * 4, btrn);
149 flac_consume(fn);
150 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
151 }
152
153 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
154 __a_unused const FLAC__StreamMetadata *metadata,
155 void *client_data)
156 {
157 flac_consume(client_data);
158 }
159
160 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
161 FLAC__StreamDecoderErrorStatus status,
162 __a_unused void *client_data)
163 {
164 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
165 }
166
167 static int flacdec_init(struct filter_node *fn)
168 {
169 struct private_flacdec_data *pfd = fn->private_data;
170 FLAC__StreamDecoderInitStatus init_status;
171
172 PARA_INFO_LOG("initializing flac decoder\n");
173 pfd->decoder = FLAC__stream_decoder_new();
174 if (!pfd->decoder)
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)
181 return 1;
182 FLAC__stream_decoder_delete(pfd->decoder);
183 return -E_FLACDEC_DECODER_INIT;
184 }
185
186 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
187 char **result)
188 {
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);
193
194 return decoder_execute(cmd, sample_rate, channels, result);
195 }
196
197 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
198
199 static bool output_queue_full(struct btr_node *btrn)
200 {
201 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
202 }
203
204 static void flacdec_pre_select(struct sched *s, struct task *t)
205 {
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;
209 int ret;
210
211 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
212 if (ret < 0)
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);
218 }
219
220 static void flacdec_post_select(__a_unused struct sched *s, struct task *t)
221 {
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;
225 int ret;
226
227 if (output_queue_full(btrn))
228 return;
229 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
230 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
231 goto out;
232 if (ret <= 0 && !pfd->have_more) /* nothing to do */
233 goto out;
234 if (!pfd->decoder) {
235 ret = flacdec_init(fn);
236 goto out;
237 }
238 pfd->unconsumed = 0;
239 for (;;) {
240 if (output_queue_full(btrn)) {
241 pfd->have_more = true;
242 break;
243 }
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)
251 goto out;
252 if (state == FLAC__STREAM_DECODER_ABORTED) {
253 FLAC__stream_decoder_flush(pfd->decoder);
254 fn->min_iqs = pfd->unconsumed + 1;
255 break;
256 }
257 fn->min_iqs = 0;
258 }
259 ret = 1;
260 out:
261 t->error = ret;
262 if (ret < 0)
263 btr_remove_node(btrn);
264 }
265
266 static void flacdec_close(struct filter_node *fn)
267 {
268 struct private_flacdec_data *pfd = fn->private_data;
269
270 FLAC__stream_decoder_finish(pfd->decoder);
271 FLAC__stream_decoder_delete(pfd->decoder);
272 free(pfd);
273 fn->private_data = NULL;
274 }
275
276 static void flacdec_open(struct filter_node *fn)
277 {
278 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
279 fn->private_data = pfd;
280 fn->min_iqs = 0;
281 }
282
283 /**
284 * The init function of the flacdec filter.
285 *
286 * \param f Pointer to the filter struct to initialize.
287 *
288 * \sa filter::init.
289 */
290 void flacdec_filter_init(struct filter *f)
291 {
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;
297 }