Introduce and use generic_filter_pre_select().
[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 = prepare_filter_node(fn);
224 if (ret < 0)
225 goto err;
226 if (ret == 0)
227 return;
228 len = btr_next_buffer(btrn, (char **)&inbuf);
229 iqs = btr_get_input_queue_size(btrn);
230 if (!padd->initialized) {
231 unsigned long rate = 0;
232 unsigned char channels = 0;
233 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
234 if (ret < 0) {
235 PARA_INFO_LOG("%s\n", para_strerror(-ret));
236 ret = NeAACDecInit(padd->handle, inbuf,
237 len, &rate, &channels);
238 PARA_INFO_LOG("decoder init: %d\n", ret);
239 if (ret < 0) {
240 ret = -E_AACDEC_INIT;
241 goto out;
242 }
243 consumed = ret;
244 } else {
245 PARA_INFO_LOG("decoder len: %lu\n",
246 padd->decoder_length);
247 consumed += skip;
248 p = inbuf + consumed;
249 ret = -E_AACDEC_INIT;
250 if (NeAACDecInit2(padd->handle, p,
251 padd->decoder_length, &rate,
252 &channels) < 0)
253 goto out;
254 }
255 padd->samplerate = rate;
256 padd->channels = channels;
257 PARA_INFO_LOG("rate: %u, channels: %d\n",
258 padd->samplerate, padd->channels);
259 padd->initialized = 1;
260 }
261 if (padd->decoder_length > 0) {
262 consumed = 0;
263 if (!padd->entry) {
264 ret = aac_find_entry_point(inbuf + consumed,
265 len - consumed, &skip);
266 if (ret < 0) {
267 ret = len;
268 goto out;
269 }
270 consumed += skip;
271 padd->entry = ret;
272 PARA_INFO_LOG("entry: %zu\n", padd->entry);
273 }
274 ret = len;
275 if (padd->consumed_total + len < padd->entry)
276 goto out;
277 if (padd->consumed_total < padd->entry)
278 consumed = padd->entry - padd->consumed_total;
279 }
280 for (; consumed < len; consumed++)
281 if ((inbuf[consumed] & 0xfe) == 0x20)
282 break;
283 if (consumed >= len)
284 goto success;
285 p = inbuf + consumed;
286 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
287 // padd->consumed_total, consumed, len - consumed);
288 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
289 len - consumed);
290 if (padd->frame_info.error) {
291 int err = padd->frame_info.error;
292 ret = -E_AAC_DECODE;
293 if (padd->error_count++ > MAX_ERRORS)
294 goto err;
295 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
296 err, NeAACDecGetErrorMessage(padd->frame_info.error),
297 padd->consumed_total, consumed,
298 padd->frame_info.bytesconsumed);
299 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
300 padd->frame_info.error));
301 consumed++; /* catch 21 */
302 goto success;
303 }
304 padd->error_count = 0;
305 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
306 consumed += padd->frame_info.bytesconsumed;
307 ret = consumed;
308 if (!padd->frame_info.samples)
309 goto out;
310 btr_buf = para_malloc(2 * padd->frame_info.samples);
311 loaded = 0;
312 for (i = 0; i < padd->frame_info.samples; i++) {
313 short sh = ((short *)outbuffer)[i];
314 write_int16_host_endian(btr_buf + loaded, sh);
315 loaded += 2;
316 }
317 btr_add_output(btr_buf, loaded, btrn);
318 success:
319 ret = consumed;
320 out:
321 if (ret >= 0) {
322 padd->consumed_total += ret;
323 btr_consume(btrn, ret);
324 goto next_buffer;
325 }
326 err:
327 assert(ret < 0);
328 aacdec_close(fn);
329 t->error = ret;
330 btr_del_node(btrn);
331 }
332
333 /**
334 * the init function of the aacdec filter
335 *
336 * \param f pointer to the filter struct to initialize
337 *
338 * \sa filter::init
339 */
340 void aacdec_filter_init(struct filter *f)
341 {
342 f->open = aacdec_open;
343 f->convert = aacdec;
344 f->close = aacdec_close;
345 f->pre_select = generic_filter_pre_select;
346 f->post_select = aacdec_post_select;
347 f->execute = aacdec_execute;
348 }