2 * Copyright (C) 2006-2007 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)
37 /** give up decoding after that many errors */
41 * data specific to the aacdec filter
43 * \sa filter, filter_node
45 struct private_aacdec_data {
46 /** the return value of aac_open */
47 NeAACDecHandle handle;
48 /** info about the currently decoded frame */
49 NeAACDecFrameInfo frame_info;
50 /** whether this instance of the aac decoder is already initialized */
53 * return value of aac_find_esds(). Used to call the right aacdec
56 unsigned long decoder_length;
57 /** number of times the decoder returned an error */
59 /** number of bytes already consumed from the imput stream */
60 size_t consumed_total;
61 /** return value of aac_find_entry_point */
65 static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
67 struct private_aacdec_data *padd = fn->private_data;
68 struct filter_chain *fc = fn->fc;
70 unsigned char *p, *outbuffer;
71 unsigned char *inbuf = (unsigned char*)input_buffer;
72 size_t skip, consumed = 0;
74 if (fn->loaded > fn->bufsize * 3 / 5)
76 if (len < 2048 && !*fc->input_eof)
79 if (!padd->initialized) {
80 unsigned long rate = 0;
81 unsigned char channels = 0;
82 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
84 PARA_INFO_LOG("%s\n", PARA_STRERROR(-ret));
85 ret = NeAACDecInit(padd->handle, inbuf,
86 len, &rate, &channels);
87 PARA_INFO_LOG("decoder init: %d\n", ret);
94 PARA_INFO_LOG("decoder len: %lu\n",
95 padd->decoder_length);
99 if (NeAACDecInit2(padd->handle, p,
100 padd->decoder_length, &rate,
104 fc->samplerate = rate;
105 fc->channels = channels;
106 PARA_INFO_LOG("rate: %u, channels: %d\n",
107 fc->samplerate, fc->channels);
108 padd->initialized = 1;
110 if (padd->decoder_length > 0) {
113 ret = aac_find_entry_point(inbuf + consumed,
114 len - consumed, &skip);
121 PARA_INFO_LOG("entry: %zu\n", padd->entry);
124 if (padd->consumed_total + len < padd->entry)
126 if (padd->consumed_total < padd->entry)
127 consumed = padd->entry - padd->consumed_total;
129 for (; consumed < len; consumed++)
130 if ((inbuf[consumed] & 0xfe) == 0x20)
134 p = inbuf + consumed;
135 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
137 if (padd->frame_info.error) {
139 if (padd->error_count++ > MAX_ERRORS)
141 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
142 padd->frame_info.error, padd->consumed_total,
143 consumed, padd->frame_info.bytesconsumed);
144 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
145 padd->frame_info.error));
146 consumed++; /* catch 21 */
149 padd->error_count = 0;
150 consumed += padd->frame_info.bytesconsumed;
152 if (!padd->frame_info.samples)
154 ret = -E_AAC_OVERRUN;
155 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
157 for (i = 0; i < padd->frame_info.samples; i++) {
158 short *s = (short *)outbuffer;
159 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
166 padd->consumed_total += ret;
170 static void aacdec_open(struct filter_node *fn)
172 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
173 struct private_aacdec_data *padd = fn->private_data;
175 fn->bufsize = AAC_OUTBUF_SIZE;
176 fn->buf = para_calloc(fn->bufsize);
177 padd->handle = aac_open();
180 static void aacdec_close(struct filter_node *fn)
182 struct private_aacdec_data *padd = fn->private_data;
184 NeAACDecClose(padd->handle);
188 fn->private_data = NULL;
192 * the init function of the aacdec filter
194 * \param f pointer to the filter struct to initialize
198 void aacdec_init(struct filter *f)
200 f->open = aacdec_open;
202 f->close = aacdec_close;