2 * Copyright (C) 2006-2009 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_filter.c paraslash's aac (m4a) decoder. */
20 #include "buffer_tree.h"
26 /** the output buffer size */
27 #define AAC_OUTBUF_SIZE (32 * 1024)
29 /** give up decoding after that many errors */
33 * data specific to the aacdec filter
35 * \sa filter, filter_node
37 struct private_aacdec_data {
38 /** the return value of aac_open */
39 NeAACDecHandle handle;
40 /** info about the currently decoded frame */
41 NeAACDecFrameInfo frame_info;
42 /** whether this instance of the aac decoder is already initialized */
45 * return value of aac_find_esds(). Used to call the right aacdec
48 unsigned long decoder_length;
49 /** number of times the decoder returned an error */
51 /** number of bytes already consumed from the imput stream */
52 size_t consumed_total;
53 /** return value of aac_find_entry_point */
55 /** The number of channels of the current stream. */
56 unsigned int channels;
57 /** Current sample rate in Hz. */
58 unsigned int samplerate;
61 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
63 struct filter_node *fn = btr_context(btrn);
64 struct private_aacdec_data *padd = fn->private_data;
66 if (!strcmp(cmd, "samplerate")) {
67 if (padd->samplerate == 0)
68 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
69 *result = make_message("%u", padd->samplerate);
72 if (!strcmp(cmd, "channels")) {
73 if (padd->channels == 0)
74 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
75 *result = make_message("%u", padd->channels);
78 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
81 static void aacdec_open(struct filter_node *fn)
83 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
85 fn->private_data = padd;
86 fn->bufsize = AAC_OUTBUF_SIZE;
87 fn->buf = para_calloc(fn->bufsize);
89 padd->handle = aac_open();
92 static void aacdec_close(struct filter_node *fn)
94 struct private_aacdec_data *padd = fn->private_data;
96 NeAACDecClose(padd->handle);
100 fn->private_data = NULL;
103 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
105 struct filter_node *fn = container_of(t, struct filter_node, task);
106 struct btr_node *btrn = fn->btrn;
107 struct private_aacdec_data *padd = fn->private_data;
109 unsigned char *p, *inbuf, *outbuffer;
111 size_t len, skip, consumed, loaded, iqs;
115 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
120 btr_merge(btrn, fn->min_iqs);
121 len = btr_next_buffer(btrn, (char **)&inbuf);
123 iqs = btr_get_input_queue_size(btrn);
124 if (!padd->initialized) {
125 unsigned long rate = 0;
126 unsigned char channels = 0;
127 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
129 PARA_INFO_LOG("%s\n", para_strerror(-ret));
130 ret = NeAACDecInit(padd->handle, inbuf,
131 len, &rate, &channels);
132 PARA_INFO_LOG("decoder init: %d\n", ret);
134 ret = -E_AACDEC_INIT;
139 PARA_INFO_LOG("decoder len: %lu\n",
140 padd->decoder_length);
142 p = inbuf + consumed;
143 ret = -E_AACDEC_INIT;
144 if (NeAACDecInit2(padd->handle, p,
145 padd->decoder_length, &rate,
149 padd->samplerate = rate;
150 padd->channels = channels;
151 PARA_INFO_LOG("rate: %u, channels: %d\n",
152 padd->samplerate, padd->channels);
153 padd->initialized = 1;
155 if (padd->decoder_length > 0) {
158 ret = aac_find_entry_point(inbuf + consumed,
159 len - consumed, &skip);
166 PARA_INFO_LOG("entry: %zu\n", padd->entry);
169 if (padd->consumed_total + len < padd->entry)
171 if (padd->consumed_total < padd->entry)
172 consumed = padd->entry - padd->consumed_total;
174 for (; consumed < len; consumed++)
175 if ((inbuf[consumed] & 0xfe) == 0x20)
179 p = inbuf + consumed;
180 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
181 // padd->consumed_total, consumed, len - consumed);
182 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
184 if (padd->frame_info.error) {
185 int err = padd->frame_info.error;
187 if (padd->error_count++ > MAX_ERRORS)
189 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
190 err, NeAACDecGetErrorMessage(padd->frame_info.error),
191 padd->consumed_total, consumed,
192 padd->frame_info.bytesconsumed);
193 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
194 padd->frame_info.error));
195 consumed++; /* catch 21 */
198 padd->error_count = 0;
199 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
200 consumed += padd->frame_info.bytesconsumed;
202 if (!padd->frame_info.samples)
204 btr_buf = para_malloc(2 * padd->frame_info.samples);
206 for (i = 0; i < padd->frame_info.samples; i++) {
207 short sh = ((short *)outbuffer)[i];
208 write_int16_host_endian(btr_buf + loaded, sh);
211 btr_add_output(btr_buf, loaded, btrn);
216 padd->consumed_total += ret;
217 btr_consume(btrn, ret);
223 btr_remove_node(btrn);
227 * The init function of the aacdec filter.
229 * \param f Pointer to the filter struct to initialize.
233 void aacdec_filter_init(struct filter *f)
235 f->open = aacdec_open;
236 f->close = aacdec_close;
237 f->pre_select = generic_filter_pre_select;
238 f->post_select = aacdec_post_select;
239 f->execute = aacdec_execute;