Merge branch 'refs/heads/t/logo-0.6'
[paraslash.git] / flacdec_filter.c
1 /*
2 * Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>
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 <FLAC/stream_decoder.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "buffer_tree.h"
16 #include "filter.h"
17 #include "error.h"
18 #include "string.h"
19
20 struct private_flacdec_data {
21 FLAC__StreamDecoder *decoder;
22 bool have_more;
23 /*
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.
28 */
29 size_t unconsumed;
30 };
31
32 static FLAC__StreamDecoderReadStatus read_cb(
33 __a_unused const FLAC__StreamDecoder *decoder,
34 FLAC__byte buffer[], size_t *bytes, void *client_data)
35 {
36 struct filter_node *fn = client_data;
37 struct private_flacdec_data *pfd = fn->private_data;
38 struct btr_node *btrn = fn->btrn;
39 char *btr_buf;
40 size_t copy, want = *bytes, have;
41 int ns;
42
43 *bytes = 0;
44 assert(want > 0);
45 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
46 if (ns < 0)
47 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
48 for (;;) {
49 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
50 if (have == 0)
51 break;
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;
57 *bytes += copy;
58 buffer += copy;
59 want -= copy;
60 if (want == 0)
61 break;
62 }
63 if (*bytes > 0)
64 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
65 /**
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.
70 */
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;
74 }
75 /*
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.
82 *
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.
89 */
90 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
91 }
92
93 /*
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.
96 */
97 #define TELL_CB_DUMMY_VAL 1000000
98
99 /*
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.
104 */
105 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
106 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
107 {
108 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
109 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
110 }
111
112 /*
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().
117 */
118 static void flac_consume(struct filter_node *fn)
119 {
120 struct private_flacdec_data *pfd = fn->private_data;
121 struct btr_node *btrn = fn->btrn;
122 FLAC__uint64 x;
123
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);
129 pfd->unconsumed = x;
130 }
131
132 static FLAC__StreamDecoderWriteStatus write_cb(
133 const FLAC__StreamDecoder *decoder,
134 const FLAC__Frame *frame,
135 const FLAC__int32 *const buffer[],
136 void *client_data)
137 {
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);
143
144 if (channels == 1) {
145 for (k = 0; k < n; k++) {
146 int sample = buffer[0][k];
147 write_int16_host_endian(outbuffer + 2 * k, sample);
148 }
149 } else {
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);
154 }
155 }
156 btr_add_output(outbuffer, n * channels * 2, btrn);
157 flac_consume(fn);
158 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
159 }
160
161 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
162 __a_unused const FLAC__StreamMetadata *metadata,
163 void *client_data)
164 {
165 flac_consume(client_data);
166 }
167
168 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
169 FLAC__StreamDecoderErrorStatus status,
170 __a_unused void *client_data)
171 {
172 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
173 }
174
175 static int flacdec_init(struct filter_node *fn)
176 {
177 struct private_flacdec_data *pfd = fn->private_data;
178 FLAC__StreamDecoderInitStatus init_status;
179
180 PARA_INFO_LOG("initializing flac decoder\n");
181 pfd->decoder = FLAC__stream_decoder_new();
182 if (!pfd->decoder)
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)
189 return 1;
190 FLAC__stream_decoder_delete(pfd->decoder);
191 return -E_FLACDEC_DECODER_INIT;
192 }
193
194 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
195 char **result)
196 {
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);
201
202 return decoder_execute(cmd, sample_rate, channels, result);
203 }
204
205 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
206
207 static bool output_queue_full(struct btr_node *btrn)
208 {
209 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
210 }
211
212 static void flacdec_pre_select(struct sched *s, void *context)
213 {
214 struct filter_node *fn = context;
215 struct private_flacdec_data *pfd = fn->private_data;
216 struct btr_node *btrn = fn->btrn;
217 int ret;
218
219 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
220 if (ret < 0)
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);
226 }
227
228 static int flacdec_post_select(__a_unused struct sched *s, void *context)
229 {
230 struct filter_node *fn = context;
231 struct private_flacdec_data *pfd = fn->private_data;
232 struct btr_node *btrn = fn->btrn;
233 int ret;
234 FLAC__StreamDecoderState state;
235
236 if (output_queue_full(btrn))
237 return 0;
238 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
239 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
240 goto out;
241 if (ret <= 0 && !pfd->have_more) /* nothing to do */
242 goto out;
243 if (!pfd->decoder) {
244 ret = flacdec_init(fn);
245 goto out;
246 }
247 if (output_queue_full(btrn)) {
248 pfd->have_more = true;
249 ret = 1;
250 goto out;
251 }
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)
257 goto out;
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;
262 ret = 1;
263 goto out;
264 }
265 pfd->have_more = true;
266 fn->min_iqs = 0;
267 ret = 1;
268 out:
269 if (ret < 0)
270 btr_remove_node(&fn->btrn);
271 return ret;
272 }
273
274 static void flacdec_close(struct filter_node *fn)
275 {
276 struct private_flacdec_data *pfd;
277
278 if (!fn)
279 return;
280 pfd = fn->private_data;
281 if (!pfd)
282 return;
283 if (pfd->decoder) {
284 FLAC__stream_decoder_finish(pfd->decoder);
285 FLAC__stream_decoder_delete(pfd->decoder);
286 }
287 free(pfd);
288 fn->private_data = NULL;
289 }
290
291 static void flacdec_open(struct filter_node *fn)
292 {
293 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
294 fn->private_data = pfd;
295 fn->min_iqs = 0;
296 }
297
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,
304 };