aacdec: Try do decode raw packets if header was not found
[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 int decoder_length;
53 unsigned char *inbuf;
54 int inbuf_len;
55 int consumed;
56 long unsigned consumed_total;
57
58 unsigned noffsets;
59 unsigned *offset;
60 int offset_pos;
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 unsigned 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 static int fill_offset_table(struct private_mp4dec_data *padd)
126 {
127 int i, ret;
128
129 for (i = padd->offset_pos; i < padd->noffsets; i++) {
130 ret = read_int32(padd, &padd->offset[i]);
131 if (ret < 0)
132 return -1;
133 PARA_DEBUG_LOG("offset #%d: %d\n", i, padd->offset[i]);
134 padd->offset_pos++;
135 }
136 return 1;
137 }
138
139 static int find_stco(struct private_mp4dec_data *padd)
140 {
141 int ret;
142
143 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
144 unsigned char *p = padd->inbuf + padd->consumed;
145
146 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
147 continue;
148 PARA_INFO_LOG("found stco: %d\n", padd->consumed);
149 padd->consumed += 8;
150 ret = read_int32(padd, &padd->noffsets);
151 padd->offset = para_malloc(padd->noffsets * sizeof(int));
152 PARA_INFO_LOG("num entries: %d\n", padd->noffsets);
153 return 1;
154 }
155 return -E_STCO;
156 }
157
158 static ssize_t mp4dec(char *inbuffer, size_t len, struct filter_node *fn)
159 {
160 struct private_mp4dec_data *padd = fn->private_data;
161 struct filter_chain_info *fci = fn->fci;
162 unsigned long rate = 0;
163 unsigned char channels = 0;
164 int i, ret, nbytes;
165 unsigned char *p, *outbuffer;
166
167 if (fn->loaded > fn->bufsize * 4 / 5)
168 return 0;
169 if (len < 1000 && !*fci->eof)
170 return 0;
171 padd->consumed = 0;
172 padd->inbuf = (unsigned char*)inbuffer;
173 padd->inbuf_len = len;
174
175 if (!padd->initialized) {
176 padd->decoder_length = find_esds(padd);
177 if (padd->decoder_length < 0) {
178 ret = NeAACDecInit(padd->decoder, padd->inbuf,
179 padd->inbuf_len, &rate, &channels);
180 if (ret < 0) {
181 ret = E_AACDEC_INIT;
182 goto out;
183 }
184 padd->consumed = ret;
185 } else {
186 p = padd->inbuf + padd->consumed;
187 ret = E_AACDEC_INIT;
188 if (NeAACDecInit2(padd->decoder, p,
189 padd->decoder_length, &rate,
190 &channels) < 0)
191 goto out;
192 }
193 fci->samplerate = rate;
194 fci->channels = channels;
195 PARA_INFO_LOG("rate: %u, channels: %d\n", fci->samplerate,
196 fci->channels);
197 padd->initialized = 1;
198 }
199 if (padd->decoder_length > 0) {
200 padd->consumed = 0;
201 if (!padd->offset_pos) {
202 ret = len;
203 if (find_stco(padd) < 0)
204 goto out;
205 }
206 if (padd->offset_pos < padd->noffsets) {
207 fill_offset_table(padd);
208 ret = padd->consumed;
209 goto out;
210 }
211 PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
212 padd->consumed_total, padd->offset[0]);
213 ret = len;
214 if (padd->consumed_total + len < padd->offset[0])
215 goto out;
216 if (padd->consumed_total < padd->offset[0])
217 padd->consumed = padd->offset[0] - padd->consumed_total;
218 }
219 p = padd->inbuf + padd->consumed;
220 outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
221 len - padd->consumed);
222 PARA_INFO_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
223 padd->frame_info.error, padd->consumed_total,
224 padd->consumed, padd->frame_info.bytesconsumed);
225 ret = -E_AAC_DECODE;
226 if (padd->frame_info.error != 0) {
227 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
228 padd->frame_info.error));
229 goto out;
230 }
231 padd->consumed += padd->frame_info.bytesconsumed;
232 ret = padd->consumed;
233 if (!padd->frame_info.samples)
234 goto out;
235 nbytes = padd->frame_info.samples;
236 for (i = 0; i < padd->frame_info.samples; i++) {
237 short *s = (short *)outbuffer;
238 fn->buf[fn->loaded++] = s[i] & 0xff;
239 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
240 }
241 ret = padd->consumed;
242 out:
243 if (ret > 0)
244 padd->consumed_total += ret;
245 return ret;
246 }
247
248 static void mp4dec_open(struct filter_node *fn)
249 {
250 fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
251 struct private_mp4dec_data *padd = fn->private_data;
252
253 fn->bufsize = AAC_OUTBUF_SIZE;
254 fn->buf = para_calloc(fn->bufsize);
255
256 padd->decoder = NeAACDecOpen();
257 padd->config = NeAACDecGetCurrentConfiguration(padd->decoder);
258 padd->config->defObjectType = LC;
259 padd->config->outputFormat = FAAD_FMT_16BIT;
260 padd->config->downMatrix = 0;
261 NeAACDecSetConfiguration(padd->decoder, padd->config);
262 }
263
264 static void mp4dec_close(struct filter_node *fn)
265 {
266 struct private_mp4dec_data *padd = fn->private_data;
267
268 NeAACDecClose(padd->decoder);
269 free(fn->buf);
270 fn->buf = NULL;
271 free(padd);
272 fn->private_data = NULL;
273 }
274
275 /**
276 * the init function of the mp3dec filter
277 *
278 * \sa filter::init
279 */
280 void aacdec_init(struct filter *f)
281 {
282 f->open = mp4dec_open;
283 f->convert = mp4dec;
284 f->close = mp4dec_close;
285 }