Add M. Bakker to CREDITS
[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 /*
20 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
21 * Ahead Software AG
22 */
23
24 /** \file aacdec.c paraslash's mp3 decoder */
25
26 #include "para.h"
27
28 #include "list.h"
29 #include "filter.h"
30 #include "error.h"
31 #include <mad.h>
32 #include "string.h"
33
34 #include <neaacdec.h>
35
36 /** the output buffer size */
37 #define MAX_CHANNELS 6
38 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
39
40 /**
41 * data specific to the aacdec filter
42 *
43 * \sa filter, filter_node
44 */
45 struct private_mp4dec_data {
46 NeAACDecHandle decoder;
47 NeAACDecConfigurationPtr config;
48 NeAACDecFrameInfo frame_info;
49 mp4AudioSpecificConfig mp4ASC;
50
51 int initialized;
52 unsigned char *inbuf;
53 int inbuf_len;
54 int consumed;
55 long unsigned consumed_total;
56
57 unsigned noffsets;
58 unsigned *offset;
59 int offset_pos;
60 };
61
62 static int read_mp4_descr_length(struct private_mp4dec_data *padd)
63 {
64 uint8_t b;
65 uint8_t numBytes = 0;
66 uint32_t length = 0;
67
68 do {
69 b = padd->inbuf[padd->consumed + numBytes];
70 numBytes++;
71 length = (length << 7) | (b & 0x7F);
72 } while
73 ((b & 0x80) && numBytes < 4);
74 padd->consumed += numBytes;
75 return length;
76 }
77
78 static int find_esds(struct private_mp4dec_data *padd)
79 {
80 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
81 unsigned char *p = padd->inbuf + padd->consumed;
82 int decoder_length;
83
84 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
85 continue;
86 padd->consumed += 8;
87 p = padd->inbuf + padd->consumed;
88 PARA_INFO_LOG("found esds: %d, next: %x\n", padd->consumed, *p);
89 if (*p == 3)
90 padd->consumed += 8;
91 else
92 padd->consumed += 6;
93 p = padd->inbuf + padd->consumed;
94 PARA_INFO_LOG("next: %x\n", *p);
95 if (*p != 4)
96 continue;
97 padd->consumed += 18;
98 p = padd->inbuf + padd->consumed;
99 PARA_INFO_LOG("next: %x\n", *p);
100 if (*p != 5)
101 continue;
102 padd->consumed++;
103 decoder_length = read_mp4_descr_length(padd);
104 PARA_INFO_LOG("decoder length: %d\n", decoder_length);
105 p = padd->inbuf + padd->consumed;
106 PARA_INFO_LOG("decoder data0: %x\n", *p & 0xff);
107 p++;
108 PARA_INFO_LOG("decoder data1: %x\n", *p & 0xff);
109 return decoder_length;
110 }
111 return -E_ESDS;
112 }
113
114 static int read_int32(struct private_mp4dec_data *padd, unsigned *result)
115 {
116 uint8_t *d = (uint8_t*)(padd->inbuf + padd->consumed);
117 if (padd->consumed + 4 > padd->inbuf_len)
118 return -1;
119 padd->consumed += 4;
120 *result = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
121 return 1;
122 }
123
124 static int fill_offset_table(struct private_mp4dec_data *padd)
125 {
126 int i, ret;
127
128 for (i = padd->offset_pos; i < padd->noffsets; i++) {
129 ret = read_int32(padd, &padd->offset[i]);
130 if (ret < 0)
131 return -1;
132 PARA_DEBUG_LOG("offset #%d: %d\n", i, padd->offset[i]);
133 padd->offset_pos++;
134 }
135 return 1;
136 }
137
138 static int find_stco(struct private_mp4dec_data *padd)
139 {
140 int ret;
141
142 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
143 unsigned char *p = padd->inbuf + padd->consumed;
144
145 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
146 continue;
147 PARA_INFO_LOG("found stco: %d\n", padd->consumed);
148 padd->consumed += 8;
149 ret = read_int32(padd, &padd->noffsets);
150 padd->offset = para_malloc(padd->noffsets * sizeof(int));
151 PARA_INFO_LOG("num entries: %d\n", padd->noffsets);
152 return 1;
153 }
154 return -E_STCO;
155 }
156
157 static ssize_t mp4dec(char *inbuffer, size_t len, struct filter_node *fn)
158 {
159 struct private_mp4dec_data *padd = fn->private_data;
160 struct filter_chain_info *fci = fn->fci;
161 unsigned long rate = 0;
162 unsigned char channels = 0;
163 int i, ret, nbytes;
164 unsigned char *p, *outbuffer;
165
166 if (fn->loaded > fn->bufsize * 4 / 5)
167 return 0;
168 if (len < 1000 && !*fci->eof)
169 return 0;
170 padd->consumed = 0;
171 padd->inbuf = (unsigned char*)inbuffer;
172 padd->inbuf_len = len;
173
174 if (!padd->initialized) {
175 ret = find_esds(padd);
176 if (ret < 0)
177 goto out;
178
179 p = padd->inbuf + padd->consumed;
180 ret = E_AACDEC_INIT;
181 if (NeAACDecInit2(padd->decoder, p, ret, &rate, &channels) < 0) {
182 PARA_INFO_LOG("header not found, consumed: %d\n",
183 padd->consumed);
184 goto out;
185 }
186 fci->samplerate = rate;
187 fci->channels = channels;
188 PARA_INFO_LOG("rate: %u, channels: %d\n", fci->samplerate,
189 fci->channels);
190 padd->initialized = 1;
191 }
192 padd->consumed = 0;
193 if (!padd->offset_pos) {
194 ret = len;
195 if (find_stco(padd) < 0)
196 goto out;
197 }
198 if (padd->offset_pos < padd->noffsets) {
199 fill_offset_table(padd);
200 ret = padd->consumed;
201 goto out;
202 }
203 PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
204 padd->consumed_total, padd->offset[0]);
205 ret = len;
206 if (padd->consumed_total + len < padd->offset[0])
207 goto out;
208 if (padd->consumed_total < padd->offset[0])
209 padd->consumed = padd->offset[0] - padd->consumed_total;
210 p = padd->inbuf + padd->consumed;
211 outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
212 len - padd->consumed);
213 PARA_INFO_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
214 padd->frame_info.error, padd->consumed_total,
215 padd->consumed, padd->frame_info.bytesconsumed);
216 ret = -E_AAC_DECODE;
217 if (padd->frame_info.error != 0) {
218 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
219 padd->frame_info.error));
220 goto out;
221 }
222 padd->consumed += padd->frame_info.bytesconsumed;
223 ret = padd->consumed;
224 if (!padd->frame_info.samples)
225 goto out;
226 nbytes = padd->frame_info.samples;
227 for (i = 0; i < padd->frame_info.samples; i++) {
228 short *s = (short *)outbuffer;
229 fn->buf[fn->loaded++] = s[i] & 0xff;
230 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
231 }
232 ret = padd->consumed;
233 out:
234 if (ret > 0)
235 padd->consumed_total += ret;
236 return ret;
237 }
238
239 static void mp4dec_open(struct filter_node *fn)
240 {
241 fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
242 struct private_mp4dec_data *padd = fn->private_data;
243
244 fn->bufsize = AAC_OUTBUF_SIZE;
245 fn->buf = para_calloc(fn->bufsize);
246
247 padd->decoder = NeAACDecOpen();
248 padd->config = NeAACDecGetCurrentConfiguration(padd->decoder);
249 padd->config->defObjectType = LC;
250 padd->config->outputFormat = FAAD_FMT_16BIT;
251 padd->config->downMatrix = 0;
252 NeAACDecSetConfiguration(padd->decoder, padd->config);
253 }
254
255 static void mp4dec_close(struct filter_node *fn)
256 {
257 struct private_mp4dec_data *padd = fn->private_data;
258
259 NeAACDecClose(padd->decoder);
260 free(fn->buf);
261 fn->buf = NULL;
262 free(padd);
263 fn->private_data = NULL;
264 }
265
266 /**
267 * the init function of the mp3dec filter
268 *
269 * \sa filter::init
270 */
271 void aacdec_init(struct filter *f)
272 {
273 f->open = mp4dec_open;
274 f->convert = mp4dec;
275 f->close = mp4dec_close;
276 }