Consolidate decoder code by introducing prepare_filter_node().
[paraslash.git] / aacdec_filter.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /*
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
8 * Ahead Software AG
9 */
10
11 /** \file aacdec_filter.c paraslash's aac (m4a) decoder. */
12
13 #include <regex.h>
14 #include <stdbool.h>
15
16 #include "para.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "buffer_tree.h"
21 #include "filter.h"
22 #include "error.h"
23 #include "string.h"
24 #include "aac.h"
25
26 /** the output buffer size */
27 #define AAC_OUTBUF_SIZE (32 * 1024)
28
29 /** give up decoding after that many errors */
30 #define MAX_ERRORS 20
31
32 /**
33 * data specific to the aacdec filter
34 *
35 * \sa filter, filter_node
36 */
37 struct private_aacdec_data {
38 /** the return value of aac_open */
39 NeAACDecHandle handle;
40 /** info about the currently decoded frame */
41 NeAACDecFrameInfo frame_info;
42 /** whether this instance of the aac decoder is already initialized */
43 int initialized;
44 /**
45 * return value of aac_find_esds(). Used to call the right aacdec
46 * init function
47 */
48 unsigned long decoder_length;
49 /** number of times the decoder returned an error */
50 unsigned error_count;
51 /** number of bytes already consumed from the imput stream */
52 size_t consumed_total;
53 /** return value of aac_find_entry_point */
54 size_t entry;
55 /** The number of channels of the current stream. */
56 unsigned int channels;
57 /** Current sample rate in Hz. */
58 unsigned int samplerate;
59 };
60
61 #define AACDEC_MAX_PENDING (640 * 1024)
62
63 static void aacdec_pre_select(struct sched *s, struct task *t)
64 {
65 struct filter_node *fn = container_of(t, struct filter_node, task);
66 size_t iqs = btr_get_input_queue_size(fn->btrn);
67
68 t->error = 0;
69 if (iqs == 0)
70 return;
71 if (btr_bytes_pending(fn->btrn) > AACDEC_MAX_PENDING)
72 return; /* FIXME, should use reasonable bound on timeout */
73 s->timeout.tv_sec = 0;
74 s->timeout.tv_usec = 1;
75 }
76
77 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
78 {
79 struct private_aacdec_data *padd = fn->private_data;
80 struct filter_chain *fc = fn->fc;
81 int i, ret;
82 unsigned char *p, *outbuffer;
83 unsigned char *inbuf = (unsigned char*)input_buffer;
84 size_t skip, consumed = 0;
85
86 if (fn->loaded > fn->bufsize * 3 / 5)
87 return 0;
88 ret = *fc->input_error;
89 if (ret < 0)
90 return ret;
91 if (len < 2048)
92 return 0;
93
94 if (!padd->initialized) {
95 unsigned long rate = 0;
96 unsigned char channels = 0;
97 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
98 if (ret < 0) {
99 PARA_INFO_LOG("%s\n", para_strerror(-ret));
100 ret = NeAACDecInit(padd->handle, inbuf,
101 len, &rate, &channels);
102 PARA_INFO_LOG("decoder init: %d\n", ret);
103 if (ret < 0) {
104 ret = -E_AACDEC_INIT;
105 goto out;
106 }
107 consumed = ret;
108 } else {
109 PARA_INFO_LOG("decoder len: %lu\n",
110 padd->decoder_length);
111 consumed += skip;
112 p = inbuf + consumed;
113 ret = -E_AACDEC_INIT;
114 if (NeAACDecInit2(padd->handle, p,
115 padd->decoder_length, &rate,
116 &channels) < 0)
117 goto out;
118 }
119 fc->samplerate = rate;
120 fc->channels = channels;
121 PARA_INFO_LOG("rate: %u, channels: %d\n",
122 fc->samplerate, fc->channels);
123 padd->initialized = 1;
124 }
125 if (padd->decoder_length > 0) {
126 consumed = 0;
127 if (!padd->entry) {
128 ret = aac_find_entry_point(inbuf + consumed,
129 len - consumed, &skip);
130 if (ret < 0) {
131 ret = len;
132 goto out;
133 }
134 consumed += skip;
135 padd->entry = ret;
136 PARA_INFO_LOG("entry: %zu\n", padd->entry);
137 }
138 ret = len;
139 if (padd->consumed_total + len < padd->entry)
140 goto out;
141 if (padd->consumed_total < padd->entry)
142 consumed = padd->entry - padd->consumed_total;
143 }
144 for (; consumed < len; consumed++)
145 if ((inbuf[consumed] & 0xfe) == 0x20)
146 break;
147 if (consumed >= len)
148 goto success;
149 p = inbuf + consumed;
150 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
151 len - consumed);
152 if (padd->frame_info.error) {
153 ret = -E_AAC_DECODE;
154 if (padd->error_count++ > MAX_ERRORS)
155 goto out;
156 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
157 padd->frame_info.error, padd->consumed_total,
158 consumed, padd->frame_info.bytesconsumed);
159 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
160 padd->frame_info.error));
161 consumed++; /* catch 21 */
162 goto success;
163 }
164 padd->error_count = 0;
165 consumed += padd->frame_info.bytesconsumed;
166 ret = consumed;
167 if (!padd->frame_info.samples)
168 goto out;
169 ret = -E_AAC_OVERRUN;
170 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
171 goto out;
172 for (i = 0; i < padd->frame_info.samples; i++) {
173 short *s = (short *)outbuffer;
174 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
175 fn->loaded += 2;
176 }
177 success:
178 ret = consumed;
179 out:
180 if (ret > 0)
181 padd->consumed_total += ret;
182 return ret;
183 }
184
185 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
186 {
187 struct filter_node *fn = btr_context(btrn);
188 struct private_aacdec_data *padd = fn->private_data;
189
190 if (!strcmp(cmd, "samplerate")) {
191 if (padd->samplerate == 0)
192 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
193 *result = make_message("%u", padd->samplerate);
194 return 1;
195 }
196 if (!strcmp(cmd, "channels")) {
197 if (padd->channels == 0)
198 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
199 *result = make_message("%u", padd->channels);
200 return 1;
201 }
202 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
203 }
204
205 static void aacdec_open(struct filter_node *fn)
206 {
207 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
208
209 fn->private_data = padd;
210 fn->bufsize = AAC_OUTBUF_SIZE;
211 fn->buf = para_calloc(fn->bufsize);
212 padd->handle = aac_open();
213 }
214
215 static void aacdec_close(struct filter_node *fn)
216 {
217 struct private_aacdec_data *padd = fn->private_data;
218
219 NeAACDecClose(padd->handle);
220 free(fn->buf);
221 fn->buf = NULL;
222 free(padd);
223 fn->private_data = NULL;
224 }
225
226 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
227 {
228 struct filter_node *fn = container_of(t, struct filter_node, task);
229 struct btr_node *btrn = fn->btrn;
230 struct private_aacdec_data *padd = fn->private_data;
231 int i, ret;
232 unsigned char *p, *inbuf, *outbuffer;
233 char *btr_buf;
234 size_t len, skip, consumed = 0, loaded, iqs;
235
236 next_buffer:
237 t->error = 0;
238 ret = prepare_filter_node(btrn, 2048);
239 if (ret < 0)
240 goto err;
241 if (ret == 0)
242 return;
243 len = btr_next_buffer(btrn, (char **)&inbuf);
244 iqs = btr_get_input_queue_size(btrn);
245 if (!padd->initialized) {
246 unsigned long rate = 0;
247 unsigned char channels = 0;
248 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
249 if (ret < 0) {
250 PARA_INFO_LOG("%s\n", para_strerror(-ret));
251 ret = NeAACDecInit(padd->handle, inbuf,
252 len, &rate, &channels);
253 PARA_INFO_LOG("decoder init: %d\n", ret);
254 if (ret < 0) {
255 ret = -E_AACDEC_INIT;
256 goto out;
257 }
258 consumed = ret;
259 } else {
260 PARA_INFO_LOG("decoder len: %lu\n",
261 padd->decoder_length);
262 consumed += skip;
263 p = inbuf + consumed;
264 ret = -E_AACDEC_INIT;
265 if (NeAACDecInit2(padd->handle, p,
266 padd->decoder_length, &rate,
267 &channels) < 0)
268 goto out;
269 }
270 padd->samplerate = rate;
271 padd->channels = channels;
272 PARA_INFO_LOG("rate: %u, channels: %d\n",
273 padd->samplerate, padd->channels);
274 padd->initialized = 1;
275 }
276 if (padd->decoder_length > 0) {
277 consumed = 0;
278 if (!padd->entry) {
279 ret = aac_find_entry_point(inbuf + consumed,
280 len - consumed, &skip);
281 if (ret < 0) {
282 ret = len;
283 goto out;
284 }
285 consumed += skip;
286 padd->entry = ret;
287 PARA_INFO_LOG("entry: %zu\n", padd->entry);
288 }
289 ret = len;
290 if (padd->consumed_total + len < padd->entry)
291 goto out;
292 if (padd->consumed_total < padd->entry)
293 consumed = padd->entry - padd->consumed_total;
294 }
295 for (; consumed < len; consumed++)
296 if ((inbuf[consumed] & 0xfe) == 0x20)
297 break;
298 if (consumed >= len)
299 goto success;
300 p = inbuf + consumed;
301 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
302 // padd->consumed_total, consumed, len - consumed);
303 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
304 len - consumed);
305 if (padd->frame_info.error) {
306 int err = padd->frame_info.error;
307 ret = -E_AAC_DECODE;
308 if (padd->error_count++ > MAX_ERRORS)
309 goto err;
310 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
311 err, NeAACDecGetErrorMessage(padd->frame_info.error),
312 padd->consumed_total, consumed,
313 padd->frame_info.bytesconsumed);
314 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
315 padd->frame_info.error));
316 consumed++; /* catch 21 */
317 goto success;
318 }
319 padd->error_count = 0;
320 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
321 consumed += padd->frame_info.bytesconsumed;
322 ret = consumed;
323 if (!padd->frame_info.samples)
324 goto out;
325 btr_buf = para_malloc(2 * padd->frame_info.samples);
326 loaded = 0;
327 for (i = 0; i < padd->frame_info.samples; i++) {
328 short sh = ((short *)outbuffer)[i];
329 write_int16_host_endian(btr_buf + loaded, sh);
330 loaded += 2;
331 }
332 btr_add_output(btr_buf, loaded, btrn);
333 success:
334 ret = consumed;
335 out:
336 if (ret >= 0) {
337 padd->consumed_total += ret;
338 btr_consume(btrn, ret);
339 goto next_buffer;
340 }
341 err:
342 assert(ret < 0);
343 aacdec_close(fn);
344 t->error = ret;
345 btr_del_node(btrn);
346 }
347
348 /**
349 * the init function of the aacdec filter
350 *
351 * \param f pointer to the filter struct to initialize
352 *
353 * \sa filter::init
354 */
355 void aacdec_filter_init(struct filter *f)
356 {
357 f->open = aacdec_open;
358 f->convert = aacdec;
359 f->close = aacdec_close;
360 f->pre_select = aacdec_pre_select;
361 f->post_select = aacdec_post_select;
362 f->post_select = aacdec_post_select;
363 f->execute = aacdec_execute;
364 }