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