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 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
23 /** \file aacdec.c paraslash's aac (m4a) decoder */
33 #define MAX_CHANNELS 6
34 /** the output buffer size */
35 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
38 * data specific to the aacdec filter
40 * \sa filter, filter_node
42 struct private_aacdec_data
{
43 NeAACDecHandle handle
;
44 NeAACDecFrameInfo frame_info
;
48 long unsigned consumed_total
;
52 static ssize_t
aacdec(char *input_buffer
, size_t len
, struct filter_node
*fn
)
54 struct private_aacdec_data
*padd
= fn
->private_data
;
55 struct filter_chain_info
*fci
= fn
->fci
;
57 unsigned char *p
, *outbuffer
;
58 unsigned char *inbuf
= (unsigned char*)input_buffer
;
61 if (fn
->loaded
> fn
->bufsize
* 4 / 5)
63 if (len
< 1000 && !*fci
->eof
)
66 if (!padd
->initialized
) {
67 unsigned long rate
= 0;
68 unsigned char channels
= 0;
69 padd
->decoder_length
= aac_find_esds(inbuf
, len
, &skip
);
70 PARA_INFO_LOG("decoder len: %d\n", padd
->decoder_length
);
71 if (padd
->decoder_length
< 0) {
72 ret
= NeAACDecInit(padd
->handle
, inbuf
,
73 len
, &rate
, &channels
);
74 PARA_INFO_LOG("decoder init: %d\n", ret
);
84 if (NeAACDecInit2(padd
->handle
, p
,
85 padd
->decoder_length
, &rate
,
89 fci
->samplerate
= rate
;
90 fci
->channels
= channels
;
91 PARA_INFO_LOG("rate: %u, channels: %d\n",
92 fci
->samplerate
, fci
->channels
);
93 padd
->initialized
= 1;
95 if (padd
->decoder_length
> 0) {
98 ret
= aac_find_entry_point(inbuf
+ consumed
,
99 len
- consumed
, &skip
);
106 PARA_INFO_LOG("entry: %lu\n", padd
->entry
);
109 if (padd
->consumed_total
+ len
< padd
->entry
)
111 if (padd
->consumed_total
< padd
->entry
)
112 consumed
= padd
->entry
- padd
->consumed_total
;
114 for (; consumed
< len
;consumed
++)
115 if ((inbuf
[consumed
] & 0xfe) == 0x20)
119 p
= inbuf
+ consumed
;
120 outbuffer
= NeAACDecDecode(padd
->handle
, &padd
->frame_info
, p
,
123 if (padd
->frame_info
.error
!= 0) {
124 PARA_ERROR_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
125 padd
->frame_info
.error
, padd
->consumed_total
,
126 consumed
, padd
->frame_info
.bytesconsumed
);
127 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
128 padd
->frame_info
.error
));
129 consumed
++; /* catch 21 */
132 consumed
+= padd
->frame_info
.bytesconsumed
;
134 if (!padd
->frame_info
.samples
)
136 for (i
= 0; i
< padd
->frame_info
.samples
; i
++) {
137 short *s
= (short *)outbuffer
;
138 fn
->buf
[fn
->loaded
++] = s
[i
] & 0xff;
139 fn
->buf
[fn
->loaded
++] = (s
[i
] >> 8) & 0xff;
145 padd
->consumed_total
+= ret
;
149 static void aacdec_open(struct filter_node
*fn
)
151 fn
->private_data
= para_calloc(sizeof(struct private_aacdec_data
));
152 struct private_aacdec_data
*padd
= fn
->private_data
;
154 fn
->bufsize
= AAC_OUTBUF_SIZE
;
155 fn
->buf
= para_calloc(fn
->bufsize
);
156 padd
->handle
= aac_open();
159 static void aacdec_close(struct filter_node
*fn
)
161 struct private_aacdec_data
*padd
= fn
->private_data
;
163 NeAACDecClose(padd
->handle
);
167 fn
->private_data
= NULL
;
171 * the init function of the aacdec filter
175 void aacdec_init(struct filter
*f
)
177 f
->open
= aacdec_open
;
179 f
->close
= aacdec_close
;