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 */
34 /** the output buffer size */
35 #define AAC_OUTBUF_SIZE (32 * 1024)
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 size_t 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 *fc = fn->fc;
57 unsigned char *p, *outbuffer;
58 unsigned char *inbuf = (unsigned char*)input_buffer;
59 size_t skip, consumed = 0;
61 if (fn->loaded > fn->bufsize * 3 / 5)
63 if (len < 2048 && !*fc->input_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 fc->samplerate = rate;
90 fc->channels = channels;
91 PARA_INFO_LOG("rate: %u, channels: %d\n",
92 fc->samplerate, fc->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: %zu\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) {
124 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %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 ret = -E_AAC_OVERRUN;
137 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
139 for (i = 0; i < padd->frame_info.samples; i++) {
140 short *s = (short *)outbuffer;
141 fn->buf[fn->loaded++] = s[i] & 0xff;
142 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
148 padd->consumed_total += ret;
152 static void aacdec_open(struct filter_node *fn)
154 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
155 struct private_aacdec_data *padd = fn->private_data;
157 fn->bufsize = AAC_OUTBUF_SIZE;
158 fn->buf = para_calloc(fn->bufsize);
159 padd->handle = aac_open();
162 static void aacdec_close(struct filter_node *fn)
164 struct private_aacdec_data *padd = fn->private_data;
166 NeAACDecClose(padd->handle);
170 fn->private_data = NULL;
174 * the init function of the aacdec filter
178 void aacdec_init(struct filter *f)
180 f->open = aacdec_open;
182 f->close = aacdec_close;