Introduce btr_node_status() and add btr support to the file writer.
[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 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
62 {
63 struct private_aacdec_data *padd = fn->private_data;
64 struct filter_chain *fc = fn->fc;
65 int i, ret;
66 unsigned char *p, *outbuffer;
67 unsigned char *inbuf = (unsigned char*)input_buffer;
68 size_t skip, consumed = 0;
69
70 if (fn->loaded > fn->bufsize * 3 / 5)
71 return 0;
72 ret = *fc->input_error;
73 if (ret < 0)
74 return ret;
75 if (len < 2048)
76 return 0;
77
78 if (!padd->initialized) {
79 unsigned long rate = 0;
80 unsigned char channels = 0;
81 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
82 if (ret < 0) {
83 PARA_INFO_LOG("%s\n", para_strerror(-ret));
84 ret = NeAACDecInit(padd->handle, inbuf,
85 len, &rate, &channels);
86 PARA_INFO_LOG("decoder init: %d\n", ret);
87 if (ret < 0) {
88 ret = -E_AACDEC_INIT;
89 goto out;
90 }
91 consumed = ret;
92 } else {
93 PARA_INFO_LOG("decoder len: %lu\n",
94 padd->decoder_length);
95 consumed += skip;
96 p = inbuf + consumed;
97 ret = -E_AACDEC_INIT;
98 if (NeAACDecInit2(padd->handle, p,
99 padd->decoder_length, &rate,
100 &channels) < 0)
101 goto out;
102 }
103 fc->samplerate = rate;
104 fc->channels = channels;
105 PARA_INFO_LOG("rate: %u, channels: %d\n",
106 fc->samplerate, fc->channels);
107 padd->initialized = 1;
108 }
109 if (padd->decoder_length > 0) {
110 consumed = 0;
111 if (!padd->entry) {
112 ret = aac_find_entry_point(inbuf + consumed,
113 len - consumed, &skip);
114 if (ret < 0) {
115 ret = len;
116 goto out;
117 }
118 consumed += skip;
119 padd->entry = ret;
120 PARA_INFO_LOG("entry: %zu\n", padd->entry);
121 }
122 ret = len;
123 if (padd->consumed_total + len < padd->entry)
124 goto out;
125 if (padd->consumed_total < padd->entry)
126 consumed = padd->entry - padd->consumed_total;
127 }
128 for (; consumed < len; consumed++)
129 if ((inbuf[consumed] & 0xfe) == 0x20)
130 break;
131 if (consumed >= len)
132 goto success;
133 p = inbuf + consumed;
134 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
135 len - consumed);
136 if (padd->frame_info.error) {
137 ret = -E_AAC_DECODE;
138 if (padd->error_count++ > MAX_ERRORS)
139 goto out;
140 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
141 padd->frame_info.error, padd->consumed_total,
142 consumed, padd->frame_info.bytesconsumed);
143 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
144 padd->frame_info.error));
145 consumed++; /* catch 21 */
146 goto success;
147 }
148 padd->error_count = 0;
149 consumed += padd->frame_info.bytesconsumed;
150 ret = consumed;
151 if (!padd->frame_info.samples)
152 goto out;
153 ret = -E_AAC_OVERRUN;
154 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
155 goto out;
156 for (i = 0; i < padd->frame_info.samples; i++) {
157 short *s = (short *)outbuffer;
158 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
159 fn->loaded += 2;
160 }
161 success:
162 ret = consumed;
163 out:
164 if (ret > 0)
165 padd->consumed_total += ret;
166 return ret;
167 }
168
169 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
170 {
171 struct filter_node *fn = btr_context(btrn);
172 struct private_aacdec_data *padd = fn->private_data;
173
174 if (!strcmp(cmd, "samplerate")) {
175 if (padd->samplerate == 0)
176 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
177 *result = make_message("%u", padd->samplerate);
178 return 1;
179 }
180 if (!strcmp(cmd, "channels")) {
181 if (padd->channels == 0)
182 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
183 *result = make_message("%u", padd->channels);
184 return 1;
185 }
186 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
187 }
188
189 static void aacdec_open(struct filter_node *fn)
190 {
191 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
192
193 fn->private_data = padd;
194 fn->bufsize = AAC_OUTBUF_SIZE;
195 fn->buf = para_calloc(fn->bufsize);
196 fn->min_iqs = 2048;
197 padd->handle = aac_open();
198 }
199
200 static void aacdec_close(struct filter_node *fn)
201 {
202 struct private_aacdec_data *padd = fn->private_data;
203
204 NeAACDecClose(padd->handle);
205 free(fn->buf);
206 fn->buf = NULL;
207 free(padd);
208 fn->private_data = NULL;
209 }
210
211 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
212 {
213 struct filter_node *fn = container_of(t, struct filter_node, task);
214 struct btr_node *btrn = fn->btrn;
215 struct private_aacdec_data *padd = fn->private_data;
216 int i, ret;
217 unsigned char *p, *inbuf, *outbuffer;
218 char *btr_buf;
219 size_t len, skip, consumed = 0, loaded, iqs;
220
221 next_buffer:
222 t->error = 0;
223 ret = btr_node_status(btrn, fn->min_iqs);
224 if (ret < 0)
225 goto err;
226 if (ret == 0)
227 return;
228 btr_merge(btrn, fn->min_iqs);
229 len = btr_next_buffer(btrn, (char **)&inbuf);
230 iqs = btr_get_input_queue_size(btrn);
231 if (!padd->initialized) {
232 unsigned long rate = 0;
233 unsigned char channels = 0;
234 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
235 if (ret < 0) {
236 PARA_INFO_LOG("%s\n", para_strerror(-ret));
237 ret = NeAACDecInit(padd->handle, inbuf,
238 len, &rate, &channels);
239 PARA_INFO_LOG("decoder init: %d\n", ret);
240 if (ret < 0) {
241 ret = -E_AACDEC_INIT;
242 goto out;
243 }
244 consumed = ret;
245 } else {
246 PARA_INFO_LOG("decoder len: %lu\n",
247 padd->decoder_length);
248 consumed += skip;
249 p = inbuf + consumed;
250 ret = -E_AACDEC_INIT;
251 if (NeAACDecInit2(padd->handle, p,
252 padd->decoder_length, &rate,
253 &channels) < 0)
254 goto out;
255 }
256 padd->samplerate = rate;
257 padd->channels = channels;
258 PARA_INFO_LOG("rate: %u, channels: %d\n",
259 padd->samplerate, padd->channels);
260 padd->initialized = 1;
261 }
262 if (padd->decoder_length > 0) {
263 consumed = 0;
264 if (!padd->entry) {
265 ret = aac_find_entry_point(inbuf + consumed,
266 len - consumed, &skip);
267 if (ret < 0) {
268 ret = len;
269 goto out;
270 }
271 consumed += skip;
272 padd->entry = ret;
273 PARA_INFO_LOG("entry: %zu\n", padd->entry);
274 }
275 ret = len;
276 if (padd->consumed_total + len < padd->entry)
277 goto out;
278 if (padd->consumed_total < padd->entry)
279 consumed = padd->entry - padd->consumed_total;
280 }
281 for (; consumed < len; consumed++)
282 if ((inbuf[consumed] & 0xfe) == 0x20)
283 break;
284 if (consumed >= len)
285 goto success;
286 p = inbuf + consumed;
287 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
288 // padd->consumed_total, consumed, len - consumed);
289 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
290 len - consumed);
291 if (padd->frame_info.error) {
292 int err = padd->frame_info.error;
293 ret = -E_AAC_DECODE;
294 if (padd->error_count++ > MAX_ERRORS)
295 goto err;
296 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
297 err, NeAACDecGetErrorMessage(padd->frame_info.error),
298 padd->consumed_total, consumed,
299 padd->frame_info.bytesconsumed);
300 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
301 padd->frame_info.error));
302 consumed++; /* catch 21 */
303 goto success;
304 }
305 padd->error_count = 0;
306 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
307 consumed += padd->frame_info.bytesconsumed;
308 ret = consumed;
309 if (!padd->frame_info.samples)
310 goto out;
311 btr_buf = para_malloc(2 * padd->frame_info.samples);
312 loaded = 0;
313 for (i = 0; i < padd->frame_info.samples; i++) {
314 short sh = ((short *)outbuffer)[i];
315 write_int16_host_endian(btr_buf + loaded, sh);
316 loaded += 2;
317 }
318 btr_add_output(btr_buf, loaded, btrn);
319 success:
320 ret = consumed;
321 out:
322 if (ret >= 0) {
323 padd->consumed_total += ret;
324 btr_consume(btrn, ret);
325 goto next_buffer;
326 }
327 err:
328 assert(ret < 0);
329 aacdec_close(fn);
330 t->error = ret;
331 btr_del_node(btrn);
332 }
333
334 /**
335 * the init function of the aacdec filter
336 *
337 * \param f pointer to the filter struct to initialize
338 *
339 * \sa filter::init
340 */
341 void aacdec_filter_init(struct filter *f)
342 {
343 f->open = aacdec_open;
344 f->convert = aacdec;
345 f->close = aacdec_close;
346 f->pre_select = generic_filter_pre_select;
347 f->post_select = aacdec_post_select;
348 f->execute = aacdec_execute;
349 }