aacdec: raise min buffer size to 1000 bytes.
[paraslash.git] / aacdec.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file aacdec.c paraslash's mp3 decoder */
20
21 #include "para.h"
22
23 #include "list.h"
24 #include "filter.h"
25 #include "error.h"
26 #include <mad.h>
27 #include "string.h"
28
29 #include <neaacdec.h>
30
31 /** the output buffer size */
32 #define MAX_CHANNELS 6
33 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
34
35 /**
36 * data specific to the aacdec filter
37 *
38 * \sa filter, filter_node
39 */
40 struct private_mp4dec_data {
41 NeAACDecHandle decoder;
42 NeAACDecConfigurationPtr config;
43 NeAACDecFrameInfo frame_info;
44 mp4AudioSpecificConfig mp4ASC;
45
46 int initialized;
47 char *inbuf;
48 int inbuf_len;
49 int consumed;
50 long unsigned consumed_total;
51
52 int noffsets;
53 int *offset;
54 int offset_pos;
55
56 #if 0
57 int nsamples;
58 int *table;
59 int table_pos;
60 #endif
61 };
62
63 static int read_mp4_descr_length(struct private_mp4dec_data *padd)
64 {
65 uint8_t b;
66 uint8_t numBytes = 0;
67 uint32_t length = 0;
68
69 do {
70 b = padd->inbuf[padd->consumed + numBytes];
71 numBytes++;
72 length = (length << 7) | (b & 0x7F);
73 } while
74 ((b & 0x80) && numBytes < 4);
75 padd->consumed += numBytes;
76 return length;
77 }
78
79 static int find_esds(struct private_mp4dec_data *padd)
80 {
81 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
82 char *p = padd->inbuf + padd->consumed;
83 int decoder_length;
84
85 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
86 continue;
87 padd->consumed += 8;
88 p = padd->inbuf + padd->consumed;
89 PARA_INFO_LOG("found esds: %d, next: %x\n", padd->consumed, *p);
90 if (*p == 3)
91 padd->consumed += 8;
92 else
93 padd->consumed += 6;
94 p = padd->inbuf + padd->consumed;
95 PARA_INFO_LOG("next: %x\n", *p);
96 if (*p != 4)
97 continue;
98 padd->consumed += 18;
99 p = padd->inbuf + padd->consumed;
100 PARA_INFO_LOG("next: %x\n", *p);
101 if (*p != 5)
102 continue;
103 padd->consumed++;
104 decoder_length = read_mp4_descr_length(padd);
105 PARA_INFO_LOG("decoder length: %d\n", decoder_length);
106 p = padd->inbuf + padd->consumed;
107 PARA_INFO_LOG("decoder data0: %x\n", *p & 0xff);
108 p++;
109 PARA_INFO_LOG("decoder data1: %x\n", *p & 0xff);
110 return decoder_length;
111 }
112 return -E_ESDS;
113 }
114
115 static int read_int32(struct private_mp4dec_data *padd, unsigned *result)
116 {
117 uint8_t *d = (uint8_t*)(padd->inbuf + padd->consumed);
118 if (padd->consumed + 4 > padd->inbuf_len)
119 return -1;
120 padd->consumed += 4;
121 *result = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
122 return 1;
123 }
124
125 #if 0
126 int fill_table(struct private_mp4dec_data *padd)
127 {
128 int i, ret;
129
130 for (i = padd->table_pos; i < padd->nsamples; i++) {
131 ret = read_int32(padd, &padd->table[i]);
132 if (ret < 0)
133 return -1;
134 PARA_DEBUG_LOG("sample #%d: %d\n", i, padd->table[i]);
135 padd->table_pos++;
136 }
137 return 1;
138
139 }
140 int find_stsz(struct private_mp4dec_data *padd)
141 {
142 int ret;
143
144 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
145 char *p = padd->inbuf + padd->consumed;
146
147 if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
148 continue;
149 PARA_INFO_LOG("found stsz: %d\n", padd->consumed);
150 padd->consumed += 12;
151 ret = -E_STSZ;
152 if (read_int32(padd, &padd->nsamples) < 0)
153 goto out;
154 PARA_INFO_LOG("num samples: %d\n", padd->nsamples);
155 padd->table = para_malloc(padd->nsamples * sizeof(int));
156 return 1;
157 }
158 ret = -E_STSZ;
159 out:
160 return ret;
161 }
162 #endif
163
164 static int fill_offset_table(struct private_mp4dec_data *padd)
165 {
166 int i, ret;
167
168 for (i = padd->offset_pos; i < padd->noffsets; i++) {
169 ret = read_int32(padd, &padd->offset[i]);
170 if (ret < 0)
171 return -1;
172 PARA_DEBUG_LOG("offset #%d: %d\n", i, padd->offset[i]);
173 padd->offset_pos++;
174 }
175 return 1;
176 }
177
178 static int find_stco(struct private_mp4dec_data *padd)
179 {
180 int ret;
181
182 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
183 char *p = padd->inbuf + padd->consumed;
184
185 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
186 continue;
187 PARA_INFO_LOG("found stco: %d\n", padd->consumed);
188 padd->consumed += 8;
189 ret = read_int32(padd, &padd->noffsets);
190 padd->offset = para_malloc(padd->noffsets * sizeof(int));
191 PARA_INFO_LOG("num entries: %d\n", padd->noffsets);
192 return 1;
193 }
194 return -E_STCO;
195 }
196
197 static ssize_t mp4dec(char *inbuffer, size_t len, struct filter_node *fn)
198 {
199 struct private_mp4dec_data *padd = fn->private_data;
200 struct filter_chain_info *fci = fn->fci;
201 unsigned long rate = 0;
202 unsigned char channels = 0;
203 int i, ret, nbytes;
204 unsigned char *p, *outbuffer;
205
206 if (fn->loaded > fn->bufsize * 4 / 5)
207 return 0;
208 if (len < 1000 && !*fci->eof)
209 return 0;
210 padd->consumed = 0;
211 padd->inbuf = inbuffer;
212 padd->inbuf_len = len;
213
214 if (!padd->initialized) {
215 ret = find_esds(padd);
216 if (ret < 0)
217 goto out;
218
219 p = inbuffer + padd->consumed;
220 ret = E_AACDEC_INIT;
221 if (NeAACDecInit2(padd->decoder, p, ret, &rate, &channels) < 0) {
222 PARA_INFO_LOG("header not found, consumed: %d\n",
223 padd->consumed);
224 goto out;
225 }
226 fci->samplerate = rate;
227 fci->channels = channels;
228 PARA_INFO_LOG("rate: %u, channels: %d\n", fci->samplerate,
229 fci->channels);
230 padd->initialized = 1;
231 }
232 padd->consumed = 0;
233 #if 0
234 if (!padd->table_pos) {
235 ret = find_stsz(padd);
236 if (ret < 0)
237 goto out;
238 }
239 if (padd->table_pos < padd->nsamples) {
240 fill_table(padd);
241 ret = padd->consumed;
242 goto out;
243 }
244 #endif
245 if (!padd->offset_pos) {
246 // ret = find_stco(padd);
247 // if (ret < 0)
248 // goto out;
249 ret = len;
250 if (find_stco(padd) < 0)
251 goto out;
252 }
253 if (padd->offset_pos < padd->noffsets) {
254 fill_offset_table(padd);
255 ret = padd->consumed;
256 goto out;
257 }
258 PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
259 padd->consumed_total, padd->offset[0]);
260 ret = len;
261 if (padd->consumed_total + len < padd->offset[0])
262 goto out;
263 if (padd->consumed_total < padd->offset[0])
264 padd->consumed = padd->offset[0] - padd->consumed_total;
265 p = padd->inbuf + padd->consumed;
266 outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
267 len - padd->consumed);
268 PARA_INFO_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
269 padd->frame_info.error, padd->consumed_total,
270 padd->consumed, padd->frame_info.bytesconsumed);
271 ret = -E_AAC_DECODE;
272 if (padd->frame_info.error != 0) {
273 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
274 padd->frame_info.error));
275 goto out;
276 }
277 padd->consumed += padd->frame_info.bytesconsumed;
278 ret = padd->consumed;
279 if (!padd->frame_info.samples)
280 goto out;
281 nbytes = padd->frame_info.samples;
282 for (i = 0; i < padd->frame_info.samples; i++) {
283 short *s = (short *)outbuffer;
284 fn->buf[fn->loaded++] = s[i] & 0xff;
285 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
286 }
287 ret = padd->consumed;
288 out:
289 if (ret > 0)
290 padd->consumed_total += ret;
291 return ret;
292 }
293
294 static void mp4dec_open(struct filter_node *fn)
295 {
296 fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
297 struct private_mp4dec_data *padd = fn->private_data;
298
299 fn->bufsize = AAC_OUTBUF_SIZE;
300 fn->buf = para_calloc(fn->bufsize);
301
302 padd->decoder = NeAACDecOpen();
303 padd->config = NeAACDecGetCurrentConfiguration(padd->decoder);
304 padd->config->defObjectType = LC;
305 padd->config->outputFormat = FAAD_FMT_16BIT;
306 padd->config->downMatrix = 0;
307 NeAACDecSetConfiguration(padd->decoder, padd->config);
308 }
309
310 static void mp4dec_close(struct filter_node *fn)
311 {
312 struct private_mp4dec_data *padd = fn->private_data;
313
314 NeAACDecClose(padd->decoder);
315 free(fn->buf);
316 fn->buf = NULL;
317 free(padd);
318 fn->private_data = NULL;
319 }
320
321 /**
322 * the init function of the mp3dec filter
323 *
324 * \sa filter::init
325 */
326 void aacdec_init(struct filter *f)
327 {
328 f->open = mp4dec_open;
329 f->convert = mp4dec;
330 f->close = mp4dec_close;
331 }