compress: Fix off by one in help of --target-level.
[paraslash.git] / flacdec_filter.c
1 /* Copyright (C) 2011 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file flacdec_filter.c The flac decoder. */
4
5 #include <regex.h>
6 #include <FLAC/stream_decoder.h>
7
8 #include "para.h"
9 #include "list.h"
10 #include "sched.h"
11 #include "buffer_tree.h"
12 #include "filter.h"
13 #include "error.h"
14 #include "string.h"
15
16 struct private_flacdec_data {
17 FLAC__StreamDecoder *decoder;
18 bool have_more;
19 /*
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.
24 */
25 size_t unconsumed;
26 };
27
28 static FLAC__StreamDecoderReadStatus read_cb(
29 __a_unused const FLAC__StreamDecoder *decoder,
30 FLAC__byte buffer[], size_t *bytes, void *client_data)
31 {
32 struct filter_node *fn = client_data;
33 struct private_flacdec_data *pfd = fn->private_data;
34 struct btr_node *btrn = fn->btrn;
35 char *btr_buf;
36 size_t copy, want = *bytes, have;
37 int ns;
38
39 *bytes = 0;
40 assert(want > 0);
41 ns = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
42 if (ns < 0)
43 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
44 for (;;) {
45 have = btr_next_buffer_omit(btrn, pfd->unconsumed, &btr_buf);
46 if (have == 0)
47 break;
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;
53 *bytes += copy;
54 buffer += copy;
55 want -= copy;
56 if (want == 0)
57 break;
58 }
59 if (*bytes > 0)
60 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
61 /**
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.
66 */
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;
70 }
71 /*
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.
78 *
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.
85 */
86 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
87 }
88
89 /*
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.
92 */
93 #define TELL_CB_DUMMY_VAL 1000000
94
95 /*
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.
100 */
101 static FLAC__StreamDecoderTellStatus tell_cb(__a_unused const FLAC__StreamDecoder *decoder,
102 FLAC__uint64 *absolute_byte_offset, __a_unused void *client_data)
103 {
104 *absolute_byte_offset = TELL_CB_DUMMY_VAL;
105 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
106 }
107
108 /*
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().
113 */
114 static void flac_consume(struct filter_node *fn)
115 {
116 struct private_flacdec_data *pfd = fn->private_data;
117 struct btr_node *btrn = fn->btrn;
118 FLAC__uint64 x;
119
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);
125 pfd->unconsumed = x;
126 }
127
128 static FLAC__StreamDecoderWriteStatus write_cb(
129 const FLAC__StreamDecoder *decoder,
130 const FLAC__Frame *frame,
131 const FLAC__int32 *const buffer[],
132 void *client_data)
133 {
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 = para_malloc(n * channels * 2);
139
140 if (channels == 1) {
141 for (k = 0; k < n; k++) {
142 int sample = buffer[0][k];
143 write_int16_host_endian(outbuffer + 2 * k, sample);
144 }
145 } else {
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);
150 }
151 }
152 btr_add_output(outbuffer, n * channels * 2, btrn);
153 flac_consume(fn);
154 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
155 }
156
157 static void meta_cb (__a_unused const FLAC__StreamDecoder *decoder,
158 __a_unused const FLAC__StreamMetadata *metadata,
159 void *client_data)
160 {
161 flac_consume(client_data);
162 }
163
164 static void error_cb( __a_unused const FLAC__StreamDecoder *decoder,
165 FLAC__StreamDecoderErrorStatus status,
166 __a_unused void *client_data)
167 {
168 PARA_ERROR_LOG("%s\n", FLAC__StreamDecoderErrorStatusString[status]);
169 }
170
171 static int flacdec_init(struct filter_node *fn)
172 {
173 struct private_flacdec_data *pfd = fn->private_data;
174 FLAC__StreamDecoderInitStatus init_status;
175
176 PARA_INFO_LOG("initializing flac decoder\n");
177 pfd->decoder = FLAC__stream_decoder_new();
178 if (!pfd->decoder)
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)
185 return 1;
186 FLAC__stream_decoder_delete(pfd->decoder);
187 return -E_FLACDEC_DECODER_INIT;
188 }
189
190 static int flacdec_execute(struct btr_node *btrn, const char *cmd,
191 char **result)
192 {
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);
197
198 return decoder_execute(cmd, sample_rate, channels, result);
199 }
200
201 #define FLACDEC_MAX_OUTPUT_SIZE (640 * 1024)
202
203 static bool output_queue_full(struct btr_node *btrn)
204 {
205 return btr_get_output_queue_size(btrn) > FLACDEC_MAX_OUTPUT_SIZE;
206 }
207
208 static void flacdec_pre_select(struct sched *s, void *context)
209 {
210 struct filter_node *fn = context;
211 struct private_flacdec_data *pfd = fn->private_data;
212 struct btr_node *btrn = fn->btrn;
213 int ret;
214
215 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
216 if (ret < 0)
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);
222 }
223
224 static int flacdec_post_select(__a_unused struct sched *s, void *context)
225 {
226 struct filter_node *fn = context;
227 struct private_flacdec_data *pfd = fn->private_data;
228 struct btr_node *btrn = fn->btrn;
229 int ret;
230 FLAC__StreamDecoderState state;
231
232 if (output_queue_full(btrn))
233 return 0;
234 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
235 if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
236 goto out;
237 if (ret <= 0 && !pfd->have_more) /* nothing to do */
238 goto out;
239 if (!pfd->decoder) {
240 ret = flacdec_init(fn);
241 goto out;
242 }
243 if (output_queue_full(btrn)) {
244 pfd->have_more = true;
245 ret = 1;
246 goto out;
247 }
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)
253 goto out;
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;
258 ret = 1;
259 goto out;
260 }
261 pfd->have_more = true;
262 fn->min_iqs = 0;
263 ret = 1;
264 out:
265 if (ret < 0)
266 btr_remove_node(&fn->btrn);
267 return ret;
268 }
269
270 static void flacdec_close(struct filter_node *fn)
271 {
272 struct private_flacdec_data *pfd;
273
274 if (!fn)
275 return;
276 pfd = fn->private_data;
277 if (!pfd)
278 return;
279 if (pfd->decoder) {
280 FLAC__stream_decoder_finish(pfd->decoder);
281 FLAC__stream_decoder_delete(pfd->decoder);
282 }
283 free(pfd);
284 fn->private_data = NULL;
285 }
286
287 static void flacdec_open(struct filter_node *fn)
288 {
289 struct private_flacdec_data *pfd = para_calloc(sizeof(*pfd));
290 fn->private_data = pfd;
291 fn->min_iqs = 0;
292 }
293
294 const struct filter lsg_filter_cmd_com_flacdec_user_data = {
295 .open = flacdec_open,
296 .close = flacdec_close,
297 .pre_select = flacdec_pre_select,
298 .post_select = flacdec_post_select,
299 .execute = flacdec_execute,
300 };