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 #define MAX_CHANNELS 6
35 /** the output buffer size */
36 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
39 * data specific to the aacdec filter
41 * \sa filter, filter_node
43 struct private_aacdec_data {
44 NeAACDecHandle handle;
45 NeAACDecFrameInfo frame_info;
49 size_t consumed_total;
53 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
55 struct private_aacdec_data *padd = fn->private_data;
56 struct filter_chain *fc = fn->fc;
58 unsigned char *p, *outbuffer;
59 unsigned char *inbuf = (unsigned char*)input_buffer;
60 size_t skip, consumed = 0;
62 if (fn->loaded > fn->bufsize * 4 / 5)
64 if (len < 1000 && !*fc->input_eof)
67 if (!padd->initialized) {
68 unsigned long rate = 0;
69 unsigned char channels = 0;
70 padd->decoder_length = aac_find_esds(inbuf, len, &skip);
71 PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
72 if (padd->decoder_length < 0) {
73 ret = NeAACDecInit(padd->handle, inbuf,
74 len, &rate, &channels);
75 PARA_INFO_LOG("decoder init: %d\n", ret);
85 if (NeAACDecInit2(padd->handle, p,
86 padd->decoder_length, &rate,
90 fc->samplerate = rate;
91 fc->channels = channels;
92 PARA_INFO_LOG("rate: %u, channels: %d\n",
93 fc->samplerate, fc->channels);
94 padd->initialized = 1;
96 if (padd->decoder_length > 0) {
99 ret = aac_find_entry_point(inbuf + consumed,
100 len - consumed, &skip);
107 PARA_INFO_LOG("entry: %zu\n", padd->entry);
110 if (padd->consumed_total + len < padd->entry)
112 if (padd->consumed_total < padd->entry)
113 consumed = padd->entry - padd->consumed_total;
115 for (; consumed < len;consumed++)
116 if ((inbuf[consumed] & 0xfe) == 0x20)
120 p = inbuf + consumed;
121 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
124 if (padd->frame_info.error != 0) {
125 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
126 padd->frame_info.error, padd->consumed_total,
127 consumed, padd->frame_info.bytesconsumed);
128 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
129 padd->frame_info.error));
130 consumed++; /* catch 21 */
133 consumed += padd->frame_info.bytesconsumed;
135 if (!padd->frame_info.samples)
137 for (i = 0; i < padd->frame_info.samples; i++) {
138 short *s = (short *)outbuffer;
139 fn->buf[fn->loaded++] = s[i] & 0xff;
140 fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
146 padd->consumed_total += ret;
150 static void aacdec_open(struct filter_node *fn)
152 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
153 struct private_aacdec_data *padd = fn->private_data;
155 fn->bufsize = AAC_OUTBUF_SIZE;
156 fn->buf = para_calloc(fn->bufsize);
157 padd->handle = aac_open();
160 static void aacdec_close(struct filter_node *fn)
162 struct private_aacdec_data *padd = fn->private_data;
164 NeAACDecClose(padd->handle);
168 fn->private_data = NULL;
172 * the init function of the aacdec filter
176 void aacdec_init(struct filter *f)
178 f->open = aacdec_open;
180 f->close = aacdec_close;