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.
19 /** \file aacdec.c paraslash's mp3 decoder */
31 /** the output buffer size */
32 #define MAX_CHANNELS 6
33 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
36 * data specific to the aacdec filter
38 * \sa filter, filter_node
40 struct private_mp4dec_data
{
41 NeAACDecHandle decoder
;
42 NeAACDecConfigurationPtr config
;
43 NeAACDecFrameInfo frame_info
;
44 mp4AudioSpecificConfig mp4ASC
;
50 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 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];
126 int fill_table(struct private_mp4dec_data
*padd
)
130 for (i
= padd
->table_pos
; i
< padd
->nsamples
; i
++) {
131 ret
= read_int32(padd
, &padd
->table
[i
]);
134 PARA_DEBUG_LOG("sample #%d: %d\n", i
, padd
->table
[i
]);
140 int find_stsz(struct private_mp4dec_data
*padd
)
144 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
145 char *p
= padd
->inbuf
+ padd
->consumed
;
147 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 's' || p
[3] != 'z')
149 PARA_INFO_LOG("found stsz: %d\n", padd
->consumed
);
150 padd
->consumed
+= 12;
152 if (read_int32(padd
, &padd
->nsamples
) < 0)
154 PARA_INFO_LOG("num samples: %d\n", padd
->nsamples
);
155 padd
->table
= para_malloc(padd
->nsamples
* sizeof(int));
164 static int fill_offset_table(struct private_mp4dec_data
*padd
)
168 for (i
= padd
->offset_pos
; i
< padd
->noffsets
; i
++) {
169 ret
= read_int32(padd
, &padd
->offset
[i
]);
172 PARA_DEBUG_LOG("offset #%d: %d\n", i
, padd
->offset
[i
]);
178 static int find_stco(struct private_mp4dec_data
*padd
)
182 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
183 char *p
= padd
->inbuf
+ padd
->consumed
;
185 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 'c' || p
[3] != 'o')
187 PARA_INFO_LOG("found stco: %d\n", padd
->consumed
);
189 ret
= read_int32(padd
, &padd
->noffsets
);
190 padd
->offset
= para_malloc(padd
->noffsets
* sizeof(int));
191 PARA_INFO_LOG("num entries: %d\n", padd
->noffsets
);
197 static ssize_t
mp4dec(char *inbuffer
, size_t len
, struct filter_node
*fn
)
199 struct private_mp4dec_data
*padd
= fn
->private_data
;
200 struct filter_chain_info
*fci
= fn
->fci
;
201 unsigned long rate
= 0;
202 unsigned char channels
= 0;
204 unsigned char *p
, *outbuffer
;
206 if (fn
->loaded
> fn
->bufsize
* 4 / 5)
208 if (len
< 10 && !*fci
->eof
)
211 padd
->inbuf
= inbuffer
;
212 padd
->inbuf_len
= len
;
214 if (!padd
->initialized
) {
215 ret
= find_esds(padd
);
219 p
= inbuffer
+ padd
->consumed
;
221 if (NeAACDecInit2(padd
->decoder
, p
, ret
, &rate
, &channels
) < 0) {
222 PARA_INFO_LOG("header not found, consumed: %d\n",
226 fci
->samplerate
= rate
;
227 fci
->channels
= channels
;
228 PARA_INFO_LOG("rate: %u, channels: %d\n", fci
->samplerate
,
230 padd
->initialized
= 1;
234 if (!padd
->table_pos
) {
235 ret
= find_stsz(padd
);
239 if (padd
->table_pos
< padd
->nsamples
) {
241 ret
= padd
->consumed
;
245 if (!padd
->offset_pos
) {
246 // ret = find_stco(padd);
250 if (find_stco(padd
) < 0)
253 if (padd
->offset_pos
< padd
->noffsets
) {
254 fill_offset_table(padd
);
255 ret
= padd
->consumed
;
258 PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
259 padd
->consumed_total
, padd
->offset
[0]);
261 if (padd
->consumed_total
+ len
< padd
->offset
[0])
263 if (padd
->consumed_total
< padd
->offset
[0])
264 padd
->consumed
= padd
->offset
[0] - padd
->consumed_total
;
265 p
= padd
->inbuf
+ padd
->consumed
;
266 outbuffer
= NeAACDecDecode(padd
->decoder
, &padd
->frame_info
, p
,
267 len
- padd
->consumed
);
268 PARA_INFO_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
269 padd
->frame_info
.error
, padd
->consumed_total
,
270 padd
->consumed
, padd
->frame_info
.bytesconsumed
);
272 if (padd
->frame_info
.error
!= 0) {
273 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
274 padd
->frame_info
.error
));
277 padd
->consumed
+= padd
->frame_info
.bytesconsumed
;
278 ret
= padd
->consumed
;
279 if (!padd
->frame_info
.samples
)
281 nbytes
= padd
->frame_info
.samples
;
282 for (i
= 0; i
< padd
->frame_info
.samples
; i
++) {
283 short *s
= (short *)outbuffer
;
284 fn
->buf
[fn
->loaded
++] = s
[i
] & 0xff;
285 fn
->buf
[fn
->loaded
++] = (s
[i
] >> 8) & 0xff;
287 ret
= padd
->consumed
;
290 padd
->consumed_total
+= ret
;
294 static void mp4dec_open(struct filter_node
*fn
)
296 fn
->private_data
= para_calloc(sizeof(struct private_mp4dec_data
));
297 struct private_mp4dec_data
*padd
= fn
->private_data
;
299 fn
->bufsize
= AAC_OUTBUF_SIZE
;
300 fn
->buf
= para_calloc(fn
->bufsize
);
302 padd
->decoder
= NeAACDecOpen();
303 padd
->config
= NeAACDecGetCurrentConfiguration(padd
->decoder
);
304 padd
->config
->defObjectType
= LC
;
305 padd
->config
->outputFormat
= FAAD_FMT_16BIT
;
306 padd
->config
->downMatrix
= 0;
307 NeAACDecSetConfiguration(padd
->decoder
, padd
->config
);
310 static void mp4dec_close(struct filter_node
*fn
)
312 struct private_mp4dec_data
*padd
= fn
->private_data
;
314 NeAACDecClose(padd
->decoder
);
318 fn
->private_data
= NULL
;
322 * the init function of the mp3dec filter
326 void aacdec_init(struct filter
*f
)
328 f
->open
= mp4dec_open
;
330 f
->close
= mp4dec_close
;