Merge branch 'master' into next
[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 /** 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 samplerate;
56 };
57
58 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
59 {
60 struct filter_node *fn = btr_context(btrn);
61 struct private_aacdec_data *padd = fn->private_data;
62
63 if (!strcmp(cmd, "samplerate")) {
64 if (padd->samplerate == 0)
65 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
66 *result = make_message("%u", padd->samplerate);
67 return 1;
68 }
69 if (!strcmp(cmd, "channels")) {
70 if (padd->channels == 0)
71 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
72 *result = make_message("%u", padd->channels);
73 return 1;
74 }
75 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
76 }
77
78 static void aacdec_open(struct filter_node *fn)
79 {
80 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
81
82 fn->private_data = padd;
83 fn->min_iqs = 2048;
84 padd->handle = aac_open();
85 }
86
87 static void aacdec_close(struct filter_node *fn)
88 {
89 struct private_aacdec_data *padd = fn->private_data;
90
91 NeAACDecClose(padd->handle);
92 free(padd);
93 fn->private_data = NULL;
94 }
95
96 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
97 {
98 struct filter_node *fn = container_of(t, struct filter_node, task);
99 struct btr_node *btrn = fn->btrn;
100 struct private_aacdec_data *padd = fn->private_data;
101 int i, ret;
102 unsigned char *p, *inbuf, *outbuffer;
103 char *btr_buf;
104 size_t len, skip, consumed, loaded, iqs;
105
106 next_buffer:
107 t->error = 0;
108 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
109 if (ret < 0)
110 goto err;
111 if (ret == 0)
112 return;
113 btr_merge(btrn, fn->min_iqs);
114 len = btr_next_buffer(btrn, (char **)&inbuf);
115 consumed = 0;
116 iqs = btr_get_input_queue_size(btrn);
117 if (!padd->initialized) {
118 unsigned long rate = 0;
119 unsigned char channels = 0;
120 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
121 if (ret < 0) {
122 PARA_INFO_LOG("%s\n", para_strerror(-ret));
123 ret = NeAACDecInit(padd->handle, inbuf,
124 len, &rate, &channels);
125 PARA_INFO_LOG("decoder init: %d\n", ret);
126 if (ret < 0) {
127 ret = -E_AACDEC_INIT;
128 goto out;
129 }
130 consumed = ret;
131 } else {
132 PARA_INFO_LOG("decoder len: %lu\n",
133 padd->decoder_length);
134 consumed += skip;
135 p = inbuf + consumed;
136 ret = -E_AACDEC_INIT;
137 if (NeAACDecInit2(padd->handle, p,
138 padd->decoder_length, &rate,
139 &channels) < 0)
140 goto out;
141 }
142 padd->samplerate = rate;
143 padd->channels = channels;
144 PARA_INFO_LOG("rate: %u, channels: %d\n",
145 padd->samplerate, padd->channels);
146 padd->initialized = 1;
147 }
148 if (padd->decoder_length > 0) {
149 consumed = 0;
150 if (!padd->entry) {
151 ret = aac_find_entry_point(inbuf + consumed,
152 len - consumed, &skip);
153 if (ret < 0) {
154 ret = len;
155 goto out;
156 }
157 consumed += skip;
158 padd->entry = ret;
159 PARA_INFO_LOG("entry: %zu\n", padd->entry);
160 }
161 ret = len;
162 if (padd->consumed_total + len < padd->entry)
163 goto out;
164 if (padd->consumed_total < padd->entry)
165 consumed = padd->entry - padd->consumed_total;
166 }
167 for (; consumed < len; consumed++)
168 if ((inbuf[consumed] & 0xfe) == 0x20)
169 break;
170 if (consumed >= len)
171 goto success;
172 p = inbuf + consumed;
173 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
174 // padd->consumed_total, consumed, len - consumed);
175 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
176 len - consumed);
177 if (padd->frame_info.error) {
178 int err = padd->frame_info.error;
179 ret = -E_AAC_DECODE;
180 if (padd->error_count++ > MAX_ERRORS)
181 goto err;
182 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
183 err, NeAACDecGetErrorMessage(padd->frame_info.error),
184 padd->consumed_total, consumed,
185 padd->frame_info.bytesconsumed);
186 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
187 padd->frame_info.error));
188 consumed++; /* catch 21 */
189 goto success;
190 }
191 padd->error_count = 0;
192 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
193 consumed += padd->frame_info.bytesconsumed;
194 ret = consumed;
195 if (!padd->frame_info.samples)
196 goto out;
197 btr_buf = para_malloc(2 * padd->frame_info.samples);
198 loaded = 0;
199 for (i = 0; i < padd->frame_info.samples; i++) {
200 short sh = ((short *)outbuffer)[i];
201 write_int16_host_endian(btr_buf + loaded, sh);
202 loaded += 2;
203 }
204 btr_add_output(btr_buf, loaded, btrn);
205 success:
206 ret = consumed;
207 out:
208 if (ret >= 0) {
209 padd->consumed_total += ret;
210 btr_consume(btrn, ret);
211 goto next_buffer;
212 }
213 err:
214 assert(ret < 0);
215 t->error = ret;
216 btr_remove_node(btrn);
217 }
218
219 /**
220 * The init function of the aacdec filter.
221 *
222 * \param f Pointer to the filter struct to initialize.
223 *
224 * \sa filter::init
225 */
226 void aacdec_filter_init(struct filter *f)
227 {
228 f->open = aacdec_open;
229 f->close = aacdec_close;
230 f->pre_select = generic_filter_pre_select;
231 f->post_select = aacdec_post_select;
232 f->execute = aacdec_execute;
233 }