aacdec: Reset "consumed" at each iteration.
[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, loaded, iqs;
220
221 next_buffer:
222 t->error = 0;
223 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
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 consumed = 0;
231 iqs = btr_get_input_queue_size(btrn);
232 if (!padd->initialized) {
233 unsigned long rate = 0;
234 unsigned char channels = 0;
235 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
236 if (ret < 0) {
237 PARA_INFO_LOG("%s\n", para_strerror(-ret));
238 ret = NeAACDecInit(padd->handle, inbuf,
239 len, &rate, &channels);
240 PARA_INFO_LOG("decoder init: %d\n", ret);
241 if (ret < 0) {
242 ret = -E_AACDEC_INIT;
243 goto out;
244 }
245 consumed = ret;
246 } else {
247 PARA_INFO_LOG("decoder len: %lu\n",
248 padd->decoder_length);
249 consumed += skip;
250 p = inbuf + consumed;
251 ret = -E_AACDEC_INIT;
252 if (NeAACDecInit2(padd->handle, p,
253 padd->decoder_length, &rate,
254 &channels) < 0)
255 goto out;
256 }
257 padd->samplerate = rate;
258 padd->channels = channels;
259 PARA_INFO_LOG("rate: %u, channels: %d\n",
260 padd->samplerate, padd->channels);
261 padd->initialized = 1;
262 }
263 if (padd->decoder_length > 0) {
264 consumed = 0;
265 if (!padd->entry) {
266 ret = aac_find_entry_point(inbuf + consumed,
267 len - consumed, &skip);
268 if (ret < 0) {
269 ret = len;
270 goto out;
271 }
272 consumed += skip;
273 padd->entry = ret;
274 PARA_INFO_LOG("entry: %zu\n", padd->entry);
275 }
276 ret = len;
277 if (padd->consumed_total + len < padd->entry)
278 goto out;
279 if (padd->consumed_total < padd->entry)
280 consumed = padd->entry - padd->consumed_total;
281 }
282 for (; consumed < len; consumed++)
283 if ((inbuf[consumed] & 0xfe) == 0x20)
284 break;
285 if (consumed >= len)
286 goto success;
287 p = inbuf + consumed;
288 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
289 // padd->consumed_total, consumed, len - consumed);
290 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
291 len - consumed);
292 if (padd->frame_info.error) {
293 int err = padd->frame_info.error;
294 ret = -E_AAC_DECODE;
295 if (padd->error_count++ > MAX_ERRORS)
296 goto err;
297 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
298 err, NeAACDecGetErrorMessage(padd->frame_info.error),
299 padd->consumed_total, consumed,
300 padd->frame_info.bytesconsumed);
301 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
302 padd->frame_info.error));
303 consumed++; /* catch 21 */
304 goto success;
305 }
306 padd->error_count = 0;
307 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
308 consumed += padd->frame_info.bytesconsumed;
309 ret = consumed;
310 if (!padd->frame_info.samples)
311 goto out;
312 btr_buf = para_malloc(2 * padd->frame_info.samples);
313 loaded = 0;
314 for (i = 0; i < padd->frame_info.samples; i++) {
315 short sh = ((short *)outbuffer)[i];
316 write_int16_host_endian(btr_buf + loaded, sh);
317 loaded += 2;
318 }
319 btr_add_output(btr_buf, loaded, btrn);
320 success:
321 ret = consumed;
322 out:
323 if (ret >= 0) {
324 padd->consumed_total += ret;
325 btr_consume(btrn, ret);
326 goto next_buffer;
327 }
328 err:
329 assert(ret < 0);
330 t->error = ret;
331 btr_remove_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 }