First draft of the aac audio format handler
[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 "string.h"
32 #include "aac.h"
33
34 /** the output buffer size */
35 #define MAX_CHANNELS 6
36 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
37
38 /**
39 * data specific to the aacdec filter
40 *
41 * \sa filter, filter_node
42 */
43 struct private_mp4dec_data {
44 NeAACDecHandle decoder;
45 NeAACDecFrameInfo frame_info;
46
47 int initialized;
48 int decoder_length;
49 unsigned char *inbuf;
50 int inbuf_len;
51 int consumed;
52 long unsigned consumed_total;
53
54 unsigned noffsets;
55 unsigned *offset;
56 unsigned offset0;
57 int offset_pos;
58 };
59
60 static int read_int32(struct private_mp4dec_data *padd, unsigned *result)
61 {
62 uint8_t *d = (uint8_t*)(padd->inbuf + padd->consumed);
63 if (padd->consumed + 4 > padd->inbuf_len)
64 return -1;
65 padd->consumed += 4;
66 *result = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
67 return 1;
68 }
69
70 static int fill_offset_table(struct private_mp4dec_data *padd)
71 {
72 int i, ret;
73
74 for (i = padd->offset_pos; i < padd->noffsets; i++) {
75 ret = read_int32(padd, &padd->offset[i]);
76 if (ret < 0)
77 return -1;
78 PARA_DEBUG_LOG("offset #%d: %d\n", i, padd->offset[i]);
79 padd->offset_pos++;
80 }
81 return 1;
82 }
83
84 static int find_stco(struct private_mp4dec_data *padd)
85 {
86 int ret;
87
88 for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
89 unsigned char *p = padd->inbuf + padd->consumed;
90
91 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
92 continue;
93 PARA_INFO_LOG("found stco: %d\n", padd->consumed);
94 padd->consumed += 8;
95 ret = read_int32(padd, &padd->noffsets);
96 padd->offset = para_malloc(padd->noffsets * sizeof(int));
97 PARA_INFO_LOG("num entries: %d\n", padd->noffsets);
98 return 1;
99 }
100 return -E_STCO;
101 }
102
103 static ssize_t mp4dec(char *inbuffer, size_t len, struct filter_node *fn)
104 {
105 struct private_mp4dec_data *padd = fn->private_data;
106 struct filter_chain_info *fci = fn->fci;
107 unsigned long rate = 0;
108 unsigned char channels = 0;
109 int i, ret, nbytes;
110 unsigned char *p, *outbuffer;
111
112 if (fn->loaded > fn->bufsize * 4 / 5)
113 return 0;
114 if (len < 1000 && !*fci->eof)
115 return 0;
116 padd->consumed = 0;
117 padd->inbuf = (unsigned char*)inbuffer;
118 padd->inbuf_len = len;
119
120 if (!padd->initialized) {
121 int skip;
122 padd->decoder_length = aac_find_esds(padd->inbuf, padd->inbuf_len,
123 &skip);
124 if (padd->decoder_length < 0) {
125 ret = NeAACDecInit(padd->decoder, padd->inbuf,
126 padd->inbuf_len, &rate, &channels);
127 if (ret < 0) {
128 ret = E_AACDEC_INIT;
129 goto out;
130 }
131 padd->consumed = ret;
132 } else {
133 padd->consumed += skip;
134 p = padd->inbuf + padd->consumed;
135 ret = E_AACDEC_INIT;
136 if (NeAACDecInit2(padd->decoder, p,
137 padd->decoder_length, &rate,
138 &channels) < 0)
139 goto out;
140 }
141 fci->samplerate = rate;
142 fci->channels = channels;
143 PARA_INFO_LOG("rate: %u, channels: %d\n", fci->samplerate,
144 fci->channels);
145 padd->initialized = 1;
146 }
147 if (padd->decoder_length > 0) {
148 padd->consumed = 0;
149 if (!padd->offset_pos) {
150 ret = len;
151 if (find_stco(padd) < 0)
152 goto out;
153 }
154 if (padd->offset_pos < padd->noffsets) {
155 fill_offset_table(padd);
156 ret = padd->consumed;
157 goto out;
158 }
159 // PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
160 // padd->consumed_total, padd->offset[0]);
161 ret = len;
162 if (padd->consumed_total + len < padd->offset[0])
163 goto out;
164 if (padd->consumed_total < padd->offset[0])
165 padd->consumed = padd->offset[0] - padd->consumed_total;
166 }
167 for (; padd->consumed < padd->inbuf_len; padd->consumed++)
168 if ((padd->inbuf[padd->consumed] & 0xfe) == 0x20)
169 break;
170 if (padd->consumed >= padd->inbuf_len)
171 goto success;
172 p = padd->inbuf + padd->consumed;
173 // PARA_NOTICE_LOG("p[0]: %lx\n", (long unsigned) *p);
174 outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
175 len - padd->consumed);
176 ret = -E_AAC_DECODE;
177 if (padd->frame_info.error != 0) {
178 PARA_ERROR_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
179 padd->frame_info.error, padd->consumed_total,
180 padd->consumed, padd->frame_info.bytesconsumed);
181 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
182 padd->frame_info.error));
183 padd->consumed++; /* catch 21 */
184 goto success;
185 }
186 padd->consumed += padd->frame_info.bytesconsumed;
187 ret = padd->consumed;
188 if (!padd->frame_info.samples)
189 goto out;
190 nbytes = padd->frame_info.samples;
191 for (i = 0; i < padd->frame_info.samples; i++) {
192 short *s = (short *)outbuffer;
193 fn->buf[fn->loaded++] = s[i] & 0xff;
194 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
195 }
196 success:
197 ret = padd->consumed;
198 out:
199 if (ret > 0)
200 padd->consumed_total += ret;
201 return ret;
202 }
203
204 static void mp4dec_open(struct filter_node *fn)
205 {
206 fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
207 struct private_mp4dec_data *padd = fn->private_data;
208
209 fn->bufsize = AAC_OUTBUF_SIZE;
210 fn->buf = para_calloc(fn->bufsize);
211 padd->decoder = aac_open();
212 }
213
214 static void mp4dec_close(struct filter_node *fn)
215 {
216 struct private_mp4dec_data *padd = fn->private_data;
217
218 NeAACDecClose(padd->decoder);
219 free(fn->buf);
220 fn->buf = NULL;
221 free(padd);
222 fn->private_data = NULL;
223 }
224
225 /**
226 * the init function of the mp3dec filter
227 *
228 * \sa filter::init
229 */
230 void aacdec_init(struct filter *f)
231 {
232 f->open = mp4dec_open;
233 f->convert = mp4dec;
234 f->close = mp4dec_close;
235 }