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
;
57 static int read_mp4_descr_length(struct private_mp4dec_data
*padd
)
64 b
= padd
->inbuf
[padd
->consumed
+ numBytes
];
66 length
= (length
<< 7) | (b
& 0x7F);
68 ((b
& 0x80) && numBytes
< 4);
69 padd
->consumed
+= numBytes
;
73 static int find_esds(struct private_mp4dec_data
*padd
)
75 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
76 unsigned char *p
= padd
->inbuf
+ padd
->consumed
;
79 if (p
[0] != 'e' || p
[1] != 's' || p
[2] != 'd' || p
[3] != 's')
82 p
= padd
->inbuf
+ padd
->consumed
;
83 PARA_INFO_LOG("found esds: %d, next: %x\n", padd
->consumed
, *p
);
88 p
= padd
->inbuf
+ padd
->consumed
;
89 PARA_INFO_LOG("next: %x\n", *p
);
93 p
= padd
->inbuf
+ padd
->consumed
;
94 PARA_INFO_LOG("next: %x\n", *p
);
98 decoder_length
= read_mp4_descr_length(padd
);
99 PARA_INFO_LOG("decoder length: %d\n", decoder_length
);
100 p
= padd
->inbuf
+ padd
->consumed
;
101 PARA_INFO_LOG("decoder data0: %x\n", *p
& 0xff);
103 PARA_INFO_LOG("decoder data1: %x\n", *p
& 0xff);
104 return decoder_length
;
109 static int read_int32(struct private_mp4dec_data
*padd
, unsigned *result
)
111 uint8_t *d
= (uint8_t*)(padd
->inbuf
+ padd
->consumed
);
112 if (padd
->consumed
+ 4 > padd
->inbuf_len
)
115 *result
= (d
[0] << 24) | (d
[1] << 16) | (d
[2] << 8) | d
[3];
119 static int fill_offset_table(struct private_mp4dec_data
*padd
)
123 for (i
= padd
->offset_pos
; i
< padd
->noffsets
; i
++) {
124 ret
= read_int32(padd
, &padd
->offset
[i
]);
127 PARA_DEBUG_LOG("offset #%d: %d\n", i
, padd
->offset
[i
]);
133 static int find_stco(struct private_mp4dec_data
*padd
)
137 for (; padd
->consumed
< padd
->inbuf_len
; padd
->consumed
++) {
138 unsigned char *p
= padd
->inbuf
+ padd
->consumed
;
140 if (p
[0] != 's' || p
[1] != 't' || p
[2] != 'c' || p
[3] != 'o')
142 PARA_INFO_LOG("found stco: %d\n", padd
->consumed
);
144 ret
= read_int32(padd
, &padd
->noffsets
);
145 padd
->offset
= para_malloc(padd
->noffsets
* sizeof(int));
146 PARA_INFO_LOG("num entries: %d\n", padd
->noffsets
);
152 static ssize_t
mp4dec(char *inbuffer
, size_t len
, struct filter_node
*fn
)
154 struct private_mp4dec_data
*padd
= fn
->private_data
;
155 struct filter_chain_info
*fci
= fn
->fci
;
156 unsigned long rate
= 0;
157 unsigned char channels
= 0;
159 unsigned char *p
, *outbuffer
;
161 if (fn
->loaded
> fn
->bufsize
* 4 / 5)
163 if (len
< 1000 && !*fci
->eof
)
166 padd
->inbuf
= (unsigned char*)inbuffer
;
167 padd
->inbuf_len
= len
;
169 if (!padd
->initialized
) {
170 ret
= find_esds(padd
);
174 p
= padd
->inbuf
+ padd
->consumed
;
176 if (NeAACDecInit2(padd
->decoder
, p
, ret
, &rate
, &channels
) < 0) {
177 PARA_INFO_LOG("header not found, consumed: %d\n",
181 fci
->samplerate
= rate
;
182 fci
->channels
= channels
;
183 PARA_INFO_LOG("rate: %u, channels: %d\n", fci
->samplerate
,
185 padd
->initialized
= 1;
188 if (!padd
->offset_pos
) {
190 if (find_stco(padd
) < 0)
193 if (padd
->offset_pos
< padd
->noffsets
) {
194 fill_offset_table(padd
);
195 ret
= padd
->consumed
;
198 PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
199 padd
->consumed_total
, padd
->offset
[0]);
201 if (padd
->consumed_total
+ len
< padd
->offset
[0])
203 if (padd
->consumed_total
< padd
->offset
[0])
204 padd
->consumed
= padd
->offset
[0] - padd
->consumed_total
;
205 p
= padd
->inbuf
+ padd
->consumed
;
206 outbuffer
= NeAACDecDecode(padd
->decoder
, &padd
->frame_info
, p
,
207 len
- padd
->consumed
);
208 PARA_INFO_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
209 padd
->frame_info
.error
, padd
->consumed_total
,
210 padd
->consumed
, padd
->frame_info
.bytesconsumed
);
212 if (padd
->frame_info
.error
!= 0) {
213 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
214 padd
->frame_info
.error
));
217 padd
->consumed
+= padd
->frame_info
.bytesconsumed
;
218 ret
= padd
->consumed
;
219 if (!padd
->frame_info
.samples
)
221 nbytes
= padd
->frame_info
.samples
;
222 for (i
= 0; i
< padd
->frame_info
.samples
; i
++) {
223 short *s
= (short *)outbuffer
;
224 fn
->buf
[fn
->loaded
++] = s
[i
] & 0xff;
225 fn
->buf
[fn
->loaded
++] = (s
[i
] >> 8) & 0xff;
227 ret
= padd
->consumed
;
230 padd
->consumed_total
+= ret
;
234 static void mp4dec_open(struct filter_node
*fn
)
236 fn
->private_data
= para_calloc(sizeof(struct private_mp4dec_data
));
237 struct private_mp4dec_data
*padd
= fn
->private_data
;
239 fn
->bufsize
= AAC_OUTBUF_SIZE
;
240 fn
->buf
= para_calloc(fn
->bufsize
);
242 padd
->decoder
= NeAACDecOpen();
243 padd
->config
= NeAACDecGetCurrentConfiguration(padd
->decoder
);
244 padd
->config
->defObjectType
= LC
;
245 padd
->config
->outputFormat
= FAAD_FMT_16BIT
;
246 padd
->config
->downMatrix
= 0;
247 NeAACDecSetConfiguration(padd
->decoder
, padd
->config
);
250 static void mp4dec_close(struct filter_node
*fn
)
252 struct private_mp4dec_data
*padd
= fn
->private_data
;
254 NeAACDecClose(padd
->decoder
);
258 fn
->private_data
= NULL
;
262 * the init function of the mp3dec filter
266 void aacdec_init(struct filter
*f
)
268 f
->open
= mp4dec_open
;
270 f
->close
= mp4dec_close
;