2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
20 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
24 /** \file aacdec.c paraslash's mp3 decoder */
36 /** the output buffer size */
37 #define MAX_CHANNELS 6
38 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
41 * data specific to the aacdec filter
43 * \sa filter, filter_node
45 struct private_mp4dec_data
{
46 NeAACDecHandle decoder
;
47 NeAACDecConfigurationPtr config
;
48 NeAACDecFrameInfo frame_info
;
49 mp4AudioSpecificConfig mp4ASC
;
55 long unsigned consumed_total
;
62 static int read_mp4_descr_length(struct private_mp4dec_data
*padd
)
69 b
= padd
->inbuf
[padd
->consumed
+ numBytes
];
71 length
= (length
<< 7) | (b
& 0x7F);
73 ((b
& 0x80) && numBytes
< 4);
74 padd
->consumed
+= numBytes
;
78 static int find_esds(struct private_mp4dec_data
*padd
)
80 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
81 unsigned char *p
= padd
->inbuf
+ padd
->consumed
;
84 if (p
[0] != 'e' || p
[1] != 's' || p
[2] != 'd' || p
[3] != 's')
87 p
= padd
->inbuf
+ padd
->consumed
;
88 PARA_INFO_LOG("found esds: %d, next: %x\n", padd
->consumed
, *p
);
93 p
= padd
->inbuf
+ padd
->consumed
;
94 PARA_INFO_LOG("next: %x\n", *p
);
98 p
= padd
->inbuf
+ padd
->consumed
;
99 PARA_INFO_LOG("next: %x\n", *p
);
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);
108 PARA_INFO_LOG("decoder data1: %x\n", *p
& 0xff);
109 return decoder_length
;
114 static int read_int32(struct private_mp4dec_data
*padd
, unsigned *result
)
116 uint8_t *d
= (uint8_t*)(padd
->inbuf
+ padd
->consumed
);
117 if (padd
->consumed
+ 4 > padd
->inbuf_len
)
120 *result
= (d
[0] << 24) | (d
[1] << 16) | (d
[2] << 8) | d
[3];
124 static int fill_offset_table(struct private_mp4dec_data
*padd
)
128 for (i
= padd
->offset_pos
; i
< padd
->noffsets
; i
++) {
129 ret
= read_int32(padd
, &padd
->offset
[i
]);
132 PARA_DEBUG_LOG("offset #%d: %d\n", i
, padd
->offset
[i
]);
138 static int find_stco(struct private_mp4dec_data
*padd
)
142 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
143 unsigned char *p
= padd
->inbuf
+ padd
->consumed
;
145 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 'c' || p
[3] != 'o')
147 PARA_INFO_LOG("found stco: %d\n", padd
->consumed
);
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
);
157 static ssize_t
mp4dec(char *inbuffer
, size_t len
, struct filter_node
*fn
)
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;
164 unsigned char *p
, *outbuffer
;
166 if (fn
->loaded
> fn
->bufsize
* 4 / 5)
168 if (len
< 1000 && !*fci
->eof
)
171 padd
->inbuf
= (unsigned char*)inbuffer
;
172 padd
->inbuf_len
= len
;
174 if (!padd
->initialized
) {
175 ret
= find_esds(padd
);
179 p
= padd
->inbuf
+ padd
->consumed
;
181 if (NeAACDecInit2(padd
->decoder
, p
, ret
, &rate
, &channels
) < 0) {
182 PARA_INFO_LOG("header not found, consumed: %d\n",
186 fci
->samplerate
= rate
;
187 fci
->channels
= channels
;
188 PARA_INFO_LOG("rate: %u, channels: %d\n", fci
->samplerate
,
190 padd
->initialized
= 1;
193 if (!padd
->offset_pos
) {
195 if (find_stco(padd
) < 0)
198 if (padd
->offset_pos
< padd
->noffsets
) {
199 fill_offset_table(padd
);
200 ret
= padd
->consumed
;
203 PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
204 padd
->consumed_total
, padd
->offset
[0]);
206 if (padd
->consumed_total
+ len
< padd
->offset
[0])
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
);
217 if (padd
->frame_info
.error
!= 0) {
218 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
219 padd
->frame_info
.error
));
222 padd
->consumed
+= padd
->frame_info
.bytesconsumed
;
223 ret
= padd
->consumed
;
224 if (!padd
->frame_info
.samples
)
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;
232 ret
= padd
->consumed
;
235 padd
->consumed_total
+= ret
;
239 static void mp4dec_open(struct filter_node
*fn
)
241 fn
->private_data
= para_calloc(sizeof(struct private_mp4dec_data
));
242 struct private_mp4dec_data
*padd
= fn
->private_data
;
244 fn
->bufsize
= AAC_OUTBUF_SIZE
;
245 fn
->buf
= para_calloc(fn
->bufsize
);
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
);
255 static void mp4dec_close(struct filter_node
*fn
)
257 struct private_mp4dec_data
*padd
= fn
->private_data
;
259 NeAACDecClose(padd
->decoder
);
263 fn
->private_data
= NULL
;
267 * the init function of the mp3dec filter
271 void aacdec_init(struct filter
*f
)
273 f
->open
= mp4dec_open
;
275 f
->close
= mp4dec_close
;