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