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
;
56 long unsigned consumed_total
;
63 static int read_mp4_descr_length(struct private_mp4dec_data
*padd
)
70 b
= padd
->inbuf
[padd
->consumed
+ numBytes
];
72 length
= (length
<< 7) | (b
& 0x7F);
74 ((b
& 0x80) && numBytes
< 4);
75 padd
->consumed
+= numBytes
;
79 static int find_esds(struct private_mp4dec_data
*padd
)
81 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
82 unsigned char *p
= padd
->inbuf
+ padd
->consumed
;
85 if (p
[0] != 'e' || p
[1] != 's' || p
[2] != 'd' || p
[3] != 's')
88 p
= padd
->inbuf
+ padd
->consumed
;
89 PARA_INFO_LOG("found esds: %d, next: %x\n", padd
->consumed
, *p
);
94 p
= padd
->inbuf
+ padd
->consumed
;
95 PARA_INFO_LOG("next: %x\n", *p
);
99 p
= padd
->inbuf
+ padd
->consumed
;
100 PARA_INFO_LOG("next: %x\n", *p
);
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);
109 PARA_INFO_LOG("decoder data1: %x\n", *p
& 0xff);
110 return decoder_length
;
115 static int read_int32(struct private_mp4dec_data
*padd
, unsigned *result
)
117 uint8_t *d
= (uint8_t*)(padd
->inbuf
+ padd
->consumed
);
118 if (padd
->consumed
+ 4 > padd
->inbuf_len
)
121 *result
= (d
[0] << 24) | (d
[1] << 16) | (d
[2] << 8) | d
[3];
125 static int fill_offset_table(struct private_mp4dec_data
*padd
)
129 for (i
= padd
->offset_pos
; i
< padd
->noffsets
; i
++) {
130 ret
= read_int32(padd
, &padd
->offset
[i
]);
133 PARA_DEBUG_LOG("offset #%d: %d\n", i
, padd
->offset
[i
]);
139 static int find_stco(struct private_mp4dec_data
*padd
)
143 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
144 unsigned char *p
= padd
->inbuf
+ padd
->consumed
;
146 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 'c' || p
[3] != 'o')
148 PARA_INFO_LOG("found stco: %d\n", padd
->consumed
);
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
);
158 static ssize_t
mp4dec(char *inbuffer
, size_t len
, struct filter_node
*fn
)
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;
165 unsigned char *p
, *outbuffer
;
167 if (fn
->loaded
> fn
->bufsize
* 4 / 5)
169 if (len
< 1000 && !*fci
->eof
)
172 padd
->inbuf
= (unsigned char*)inbuffer
;
173 padd
->inbuf_len
= len
;
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
);
184 padd
->consumed
= ret
;
186 p
= padd
->inbuf
+ padd
->consumed
;
188 if (NeAACDecInit2(padd
->decoder
, p
,
189 padd
->decoder_length
, &rate
,
193 fci
->samplerate
= rate
;
194 fci
->channels
= channels
;
195 PARA_INFO_LOG("rate: %u, channels: %d\n", fci
->samplerate
,
197 padd
->initialized
= 1;
199 if (padd
->decoder_length
> 0) {
201 if (!padd
->offset_pos
) {
203 if (find_stco(padd
) < 0)
206 if (padd
->offset_pos
< padd
->noffsets
) {
207 fill_offset_table(padd
);
208 ret
= padd
->consumed
;
211 // PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
212 // padd->consumed_total, padd->offset[0]);
214 if (padd
->consumed_total
+ len
< padd
->offset
[0])
216 if (padd
->consumed_total
< padd
->offset
[0])
217 padd
->consumed
= padd
->offset
[0] - padd
->consumed_total
;
219 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++)
220 if ((padd
->inbuf
[padd
->consumed
] & 0xfe) == 0x20)
222 if (padd
->consumed
>= padd
->inbuf_len
)
224 p
= padd
->inbuf
+ padd
->consumed
;
225 // PARA_NOTICE_LOG("p[0]: %lx\n", (long unsigned) *p);
226 outbuffer
= NeAACDecDecode(padd
->decoder
, &padd
->frame_info
, p
,
227 len
- padd
->consumed
);
229 if (padd
->frame_info
.error
!= 0) {
230 PARA_ERROR_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
231 padd
->frame_info
.error
, padd
->consumed_total
,
232 padd
->consumed
, padd
->frame_info
.bytesconsumed
);
233 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
234 padd
->frame_info
.error
));
235 padd
->consumed
++; /* catch 21 */
238 padd
->consumed
+= padd
->frame_info
.bytesconsumed
;
239 ret
= padd
->consumed
;
240 if (!padd
->frame_info
.samples
)
242 nbytes
= padd
->frame_info
.samples
;
243 for (i
= 0; i
< padd
->frame_info
.samples
; i
++) {
244 short *s
= (short *)outbuffer
;
245 fn
->buf
[fn
->loaded
++] = s
[i
] & 0xff;
246 fn
->buf
[fn
->loaded
++] = (s
[i
] >> 8) & 0xff;
249 ret
= padd
->consumed
;
252 padd
->consumed_total
+= ret
;
256 static void mp4dec_open(struct filter_node
*fn
)
258 fn
->private_data
= para_calloc(sizeof(struct private_mp4dec_data
));
259 struct private_mp4dec_data
*padd
= fn
->private_data
;
261 fn
->bufsize
= AAC_OUTBUF_SIZE
;
262 fn
->buf
= para_calloc(fn
->bufsize
);
264 padd
->decoder
= NeAACDecOpen();
265 padd
->config
= NeAACDecGetCurrentConfiguration(padd
->decoder
);
266 padd
->config
->defObjectType
= LC
;
267 padd
->config
->outputFormat
= FAAD_FMT_16BIT
;
268 padd
->config
->downMatrix
= 0;
269 NeAACDecSetConfiguration(padd
->decoder
, padd
->config
);
272 static void mp4dec_close(struct filter_node
*fn
)
274 struct private_mp4dec_data
*padd
= fn
->private_data
;
276 NeAACDecClose(padd
->decoder
);
280 fn
->private_data
= NULL
;
284 * the init function of the mp3dec filter
288 void aacdec_init(struct filter
*f
)
290 f
->open
= mp4dec_open
;
292 f
->close
= mp4dec_close
;