]> git.tuebingen.mpg.de Git - paraslash.git/blob - aacdec_filter.c
Combine aacdec and aac_common.
[paraslash.git] / aacdec_filter.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
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 <neaacdec.h>
15
16 #include "para.h"
17 #include "portable_io.h"
18 #include "list.h"
19 #include "sched.h"
20 #include "ggo.h"
21 #include "buffer_tree.h"
22 #include "filter.h"
23 #include "error.h"
24 #include "string.h"
25
26 /** Give up decoding after that many errors. */
27 #define MAX_ERRORS 20
28
29 /**
30  * data specific to the aacdec filter
31  *
32  * \sa filter, filter_node
33  */
34 struct private_aacdec_data {
35         /** the return value of aac_open */
36         NeAACDecHandle handle;
37         /** info about the currently decoded frame */
38         NeAACDecFrameInfo frame_info;
39         /** whether this instance of the aac decoder is already initialized */
40         int initialized;
41         /**
42          * return value of aac_find_esds(). Used to call the right aacdec
43          * init function
44          */
45         unsigned long decoder_length;
46         /** number of times the decoder returned an error */
47         unsigned error_count;
48         /** number of bytes already consumed from the imput stream */
49         size_t consumed_total;
50         /** return value of aac_find_entry_point */
51         size_t entry;
52         /** The number of channels of the current stream. */
53         unsigned int channels;
54         /** Current sample rate in Hz. */
55         unsigned int sample_rate;
56 };
57
58 /*
59  * Get a new libfaad decoder handle.
60  *
61  * \return The handle returned by NeAACDecOpen().
62  */
63 static NeAACDecHandle aac_open(void)
64 {
65         NeAACDecHandle h = NeAACDecOpen();
66         NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
67
68         c->defObjectType = LC;
69         c->outputFormat = FAAD_FMT_16BIT;
70         c->downMatrix = 0;
71         NeAACDecSetConfiguration(h, c);
72         return h;
73 }
74
75 static unsigned long aac_read_decoder_length(char *buf, int *description_len)
76 {
77         uint8_t b;
78         uint8_t numBytes = 0;
79         unsigned long length = 0;
80
81         do {
82                 b = buf[numBytes];
83                 numBytes++;
84                 length = (length << 7) | (b & 0x7F);
85         } while
86                 ((b & 0x80) && numBytes < 4);
87         *description_len = numBytes;
88         return length;
89 }
90
91 /*
92  * Search for the position and the length of the decoder configuration.
93  *
94  * \param buf Buffer to seach.
95  * \param buflen Length of \a buf.
96  * \param skip Upon succesful return, this contains the offset in \a buf where
97  * the decoder config starts.
98  * \param decoder_length Result pointer that is filled in with the length of
99  * the decoder configuration on success.
100  *
101  * \return Standard.
102  */
103 static int aac_find_esds(char *buf, size_t buflen, size_t *skip,
104                 unsigned long *decoder_length)
105 {
106         size_t i;
107
108         for (i = 0; i + 4 < buflen; i++) {
109                 char *p = buf + i;
110                 int description_len;
111
112                 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
113                         continue;
114                 i += 8;
115                 p = buf + i;
116                 PARA_INFO_LOG("found esds@%zu, next: %x\n", i, *p);
117                 if (*p == 3)
118                         i += 8;
119                 else
120                         i += 6;
121                 p = buf + i;
122                 PARA_INFO_LOG("next: %x\n", *p);
123                 if (*p != 4)
124                         continue;
125                 i += 18;
126                 p = buf + i;
127                 PARA_INFO_LOG("next: %x\n", *p);
128                 if (*p != 5)
129                         continue;
130                 i++;
131                 p = buf + i;
132                 *decoder_length = aac_read_decoder_length(p, &description_len);
133                 PARA_INFO_LOG("decoder length: %lu\n", *decoder_length);
134                 i += description_len;
135                 *skip = i;
136                 return 1;
137         }
138         return -E_ESDS;
139 }
140
141 /*
142  * Search for the first entry in the stco table.
143  *
144  * \param buf Buffer to seach.
145  * \param buflen Length of \a buf.
146  * \param skip Upon succesful return, this contains the number
147  * of bytes to skip from the input buffer.
148  *
149  * \return The position of the first entry in the table on success,
150  * -E_STCO on errors.
151  */
152 static ssize_t aac_find_entry_point(char *buf, size_t buflen, size_t *skip)
153 {
154         ssize_t ret;
155         size_t i;
156
157         for (i = 0; i + 20 < buflen; i++) {
158                 char *p = buf + i;
159
160                 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
161                         continue;
162                 PARA_INFO_LOG("found stco@%zu\n", i);
163                 i += 12;
164                 ret = read_u32_be(buf + i); /* first offset */
165                 i += 4;
166                 PARA_INFO_LOG("entry point: %zd\n", ret);
167                 *skip = i;
168                 return ret;
169         }
170         PARA_WARNING_LOG("stco not found, buflen: %zu\n", buflen);
171         return -E_STCO;
172 }
173
174 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
175 {
176         struct filter_node *fn = btr_context(btrn);
177         struct private_aacdec_data *padd = fn->private_data;
178
179         return decoder_execute(cmd, padd->sample_rate, padd->channels, result);
180 }
181
182 static void aacdec_open(struct filter_node *fn)
183 {
184         struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
185
186         fn->private_data = padd;
187         fn->min_iqs = 2048;
188         padd->handle = aac_open();
189 }
190
191 static void aacdec_close(struct filter_node *fn)
192 {
193         struct private_aacdec_data *padd = fn->private_data;
194
195         NeAACDecClose(padd->handle);
196         free(padd);
197         fn->private_data = NULL;
198 }
199
200 static int aacdec_post_select(__a_unused struct sched *s, void *context)
201 {
202         struct filter_node *fn = context;
203         struct btr_node *btrn = fn->btrn;
204         struct private_aacdec_data *padd = fn->private_data;
205         int i, ret;
206         char *p, *inbuf, *outbuffer;
207         char *btr_buf;
208         size_t len, skip, consumed, loaded;
209
210 next_buffer:
211         ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
212         if (ret < 0)
213                 goto err;
214         if (ret == 0)
215                 return 0;
216         btr_merge(btrn, fn->min_iqs);
217         len = btr_next_buffer(btrn, &inbuf);
218         len = PARA_MIN(len, (size_t)8192);
219         consumed = 0;
220         if (!padd->initialized) {
221                 unsigned long rate = 0;
222                 unsigned char channels = 0;
223                 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
224                 if (ret < 0) {
225                         PARA_INFO_LOG("%s\n", para_strerror(-ret));
226                         ret = NeAACDecInit(padd->handle, (unsigned char *)inbuf,
227                                 len, &rate, &channels);
228                         PARA_INFO_LOG("decoder init: %d\n", ret);
229                         if (ret < 0) {
230                                 ret = -E_AACDEC_INIT;
231                                 goto out;
232                         }
233                         consumed = ret;
234                 } else {
235                         PARA_INFO_LOG("decoder len: %lu\n",
236                                 padd->decoder_length);
237                         consumed += skip;
238                         p = inbuf + consumed;
239                         ret = -E_AACDEC_INIT;
240                         if (NeAACDecInit2(padd->handle, (unsigned char *)p,
241                                         padd->decoder_length, &rate,
242                                         &channels) != 0)
243                                 goto out;
244                 }
245                 padd->sample_rate = rate;
246                 padd->channels = channels;
247                 PARA_INFO_LOG("rate: %u, channels: %u\n",
248                         padd->sample_rate, padd->channels);
249                 padd->initialized = 1;
250         }
251         if (padd->decoder_length > 0) {
252                 consumed = 0;
253                 if (!padd->entry) {
254                         ret = aac_find_entry_point(inbuf + consumed,
255                                 len - consumed, &skip);
256                         if (ret < 0) {
257                                 ret = len;
258                                 goto out;
259                         }
260                         consumed += skip;
261                         padd->entry = ret;
262                         PARA_INFO_LOG("entry: %zu\n", padd->entry);
263                 }
264                 ret = len;
265                 if (padd->consumed_total + len < padd->entry)
266                         goto out;
267                 if (padd->consumed_total < padd->entry)
268                         consumed = padd->entry - padd->consumed_total;
269         }
270         if (consumed >= len)
271                 goto success;
272         p = inbuf + consumed;
273         //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
274         //      padd->consumed_total, consumed, len - consumed);
275         outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info,
276                 (unsigned char *)p, len - consumed);
277         if (padd->frame_info.error) {
278                 int err = padd->frame_info.error;
279                 ret = -E_AAC_DECODE;
280                 if (padd->error_count++ > MAX_ERRORS)
281                         goto err;
282                 /* Suppress non-fatal bitstream error message at BOF/EOF */
283                 if (len < fn->min_iqs || padd->consumed_total == 0) {
284                         consumed = len;
285                         goto success;
286                 }
287                 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(err));
288                 PARA_ERROR_LOG("consumed: %zu + %zu + %lu\n",
289                         padd->consumed_total, consumed,
290                         padd->frame_info.bytesconsumed);
291                 if (consumed < len)
292                         consumed++; /* catch 21 */
293                 goto success;
294         }
295         padd->error_count = 0;
296         //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
297         consumed += padd->frame_info.bytesconsumed;
298         ret = consumed;
299         if (!padd->frame_info.samples)
300                 goto out;
301         btr_buf = para_malloc(2 * padd->frame_info.samples);
302         loaded = 0;
303         for (i = 0; i < padd->frame_info.samples; i++) {
304                 short sh = ((short *)outbuffer)[i];
305                 write_int16_host_endian(btr_buf + loaded, sh);
306                 loaded += 2;
307         }
308         btr_add_output(btr_buf, loaded, btrn);
309 success:
310         ret = consumed;
311 out:
312         if (ret >= 0) {
313                 padd->consumed_total += ret;
314                 btr_consume(btrn, ret);
315                 goto next_buffer;
316         }
317 err:
318         assert(ret < 0);
319         btr_remove_node(&fn->btrn);
320         return ret;
321 }
322
323 /**
324  * The init function of the aacdec filter.
325  *
326  * \param f Pointer to the filter struct to initialize.
327  *
328  * \sa filter::init
329  */
330 void aacdec_filter_init(struct filter *f)
331 {
332         f->open = aacdec_open;
333         f->close = aacdec_close;
334         f->pre_select = generic_filter_pre_select;
335         f->post_select = aacdec_post_select;
336         f->execute = aacdec_execute;
337 }