2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
11 /** \file aacdec.c paraslash's aac (m4a) decoder */
22 /** the output buffer size */
23 #define AAC_OUTBUF_SIZE (32 * 1024)
25 /** give up decoding after that many errors */
29 * data specific to the aacdec filter
31 * \sa filter, filter_node
33 struct private_aacdec_data {
34 /** the return value of aac_open */
35 NeAACDecHandle handle;
36 /** info about the currently decoded frame */
37 NeAACDecFrameInfo frame_info;
38 /** whether this instance of the aac decoder is already initialized */
41 * return value of aac_find_esds(). Used to call the right aacdec
44 unsigned long decoder_length;
45 /** number of times the decoder returned an error */
47 /** number of bytes already consumed from the imput stream */
48 size_t consumed_total;
49 /** return value of aac_find_entry_point */
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 * 3 / 5)
64 if (len < 2048 && !*fc->input_error)
67 if (!padd->initialized) {
68 unsigned long rate = 0;
69 unsigned char channels = 0;
70 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
72 PARA_INFO_LOG("%s\n", para_strerror(-ret));
73 ret = NeAACDecInit(padd->handle, inbuf,
74 len, &rate, &channels);
75 PARA_INFO_LOG("decoder init: %d\n", ret);
82 PARA_INFO_LOG("decoder len: %lu\n",
83 padd->decoder_length);
87 if (NeAACDecInit2(padd->handle, p,
88 padd->decoder_length, &rate,
92 fc->samplerate = rate;
93 fc->channels = channels;
94 PARA_INFO_LOG("rate: %u, channels: %d\n",
95 fc->samplerate, fc->channels);
96 padd->initialized = 1;
98 if (padd->decoder_length > 0) {
101 ret = aac_find_entry_point(inbuf + consumed,
102 len - consumed, &skip);
109 PARA_INFO_LOG("entry: %zu\n", padd->entry);
112 if (padd->consumed_total + len < padd->entry)
114 if (padd->consumed_total < padd->entry)
115 consumed = padd->entry - padd->consumed_total;
117 for (; consumed < len; consumed++)
118 if ((inbuf[consumed] & 0xfe) == 0x20)
122 p = inbuf + consumed;
123 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
125 if (padd->frame_info.error) {
127 if (padd->error_count++ > MAX_ERRORS)
129 PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
130 padd->frame_info.error, padd->consumed_total,
131 consumed, padd->frame_info.bytesconsumed);
132 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
133 padd->frame_info.error));
134 consumed++; /* catch 21 */
137 padd->error_count = 0;
138 consumed += padd->frame_info.bytesconsumed;
140 if (!padd->frame_info.samples)
142 ret = -E_AAC_OVERRUN;
143 if (padd->frame_info.samples * 2 + fn->loaded > fn->bufsize)
145 for (i = 0; i < padd->frame_info.samples; i++) {
146 short *s = (short *)outbuffer;
147 write_int16_host_endian(fn->buf + fn->loaded, s[i]);
154 padd->consumed_total += ret;
158 static void aacdec_open(struct filter_node *fn)
160 fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
161 struct private_aacdec_data *padd = fn->private_data;
163 fn->bufsize = AAC_OUTBUF_SIZE;
164 fn->buf = para_calloc(fn->bufsize);
165 padd->handle = aac_open();
168 static void aacdec_close(struct filter_node *fn)
170 struct private_aacdec_data *padd = fn->private_data;
172 NeAACDecClose(padd->handle);
176 fn->private_data = NULL;
180 * the init function of the aacdec filter
182 * \param f pointer to the filter struct to initialize
186 void aacdec_init(struct filter *f)
188 f->open = aacdec_open;
190 f->close = aacdec_close;