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 /** Give up decoding after that many errors. */
30 * data specific to the aacdec filter
32 * \sa filter, filter_node
34 struct private_aacdec_data {
35 /** the return value of aac_open */
36 NeAACDecHandle handle;
37 /** info about the currently decoded frame */
38 NeAACDecFrameInfo frame_info;
39 /** whether this instance of the aac decoder is already initialized */
42 * return value of aac_find_esds(). Used to call the right aacdec
45 unsigned long decoder_length;
46 /** number of times the decoder returned an error */
48 /** number of bytes already consumed from the imput stream */
49 size_t consumed_total;
50 /** return value of aac_find_entry_point */
52 /** The number of channels of the current stream. */
53 unsigned int channels;
54 /** Current sample rate in Hz. */
55 unsigned int samplerate;
58 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
60 struct filter_node *fn = btr_context(btrn);
61 struct private_aacdec_data *padd = fn->private_data;
63 if (!strcmp(cmd, "samplerate")) {
64 if (padd->samplerate == 0)
65 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
66 *result = make_message("%u", padd->samplerate);
69 if (!strcmp(cmd, "channels")) {
70 if (padd->channels == 0)
71 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
72 *result = make_message("%u", padd->channels);
75 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
78 static void aacdec_open(struct filter_node *fn)
80 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
82 fn->private_data = padd;
84 padd->handle = aac_open();
87 static void aacdec_close(struct filter_node *fn)
89 struct private_aacdec_data *padd = fn->private_data;
91 NeAACDecClose(padd->handle);
93 fn->private_data = NULL;
96 static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
98 struct filter_node *fn = container_of(t, struct filter_node, task);
99 struct btr_node *btrn = fn->btrn;
100 struct private_aacdec_data *padd = fn->private_data;
102 unsigned char *p, *inbuf, *outbuffer;
104 size_t len, skip, consumed, loaded, iqs;
108 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
113 btr_merge(btrn, fn->min_iqs);
114 len = btr_next_buffer(btrn, (char **)&inbuf);
116 iqs = btr_get_input_queue_size(btrn);
117 if (!padd->initialized) {
118 unsigned long rate = 0;
119 unsigned char channels = 0;
120 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
122 PARA_INFO_LOG("%s\n", para_strerror(-ret));
123 ret = NeAACDecInit(padd->handle, inbuf,
124 len, &rate, &channels);
125 PARA_INFO_LOG("decoder init: %d\n", ret);
127 ret = -E_AACDEC_INIT;
132 PARA_INFO_LOG("decoder len: %lu\n",
133 padd->decoder_length);
135 p = inbuf + consumed;
136 ret = -E_AACDEC_INIT;
137 if (NeAACDecInit2(padd->handle, p,
138 padd->decoder_length, &rate,
142 padd->samplerate = rate;
143 padd->channels = channels;
144 PARA_INFO_LOG("rate: %u, channels: %d\n",
145 padd->samplerate, padd->channels);
146 padd->initialized = 1;
148 if (padd->decoder_length > 0) {
151 ret = aac_find_entry_point(inbuf + consumed,
152 len - consumed, &skip);
159 PARA_INFO_LOG("entry: %zu\n", padd->entry);
162 if (padd->consumed_total + len < padd->entry)
164 if (padd->consumed_total < padd->entry)
165 consumed = padd->entry - padd->consumed_total;
167 for (; consumed < len; consumed++)
168 if ((inbuf[consumed] & 0xfe) == 0x20)
172 p = inbuf + consumed;
173 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
174 // padd->consumed_total, consumed, len - consumed);
175 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
177 if (padd->frame_info.error) {
178 int err = padd->frame_info.error;
180 if (padd->error_count++ > MAX_ERRORS)
182 PARA_ERROR_LOG("frame_error: %d (%s), consumed: %zu + %zd + %lu\n",
183 err, NeAACDecGetErrorMessage(padd->frame_info.error),
184 padd->consumed_total, consumed,
185 padd->frame_info.bytesconsumed);
186 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
187 padd->frame_info.error));
188 consumed++; /* catch 21 */
191 padd->error_count = 0;
192 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
193 consumed += padd->frame_info.bytesconsumed;
195 if (!padd->frame_info.samples)
197 btr_buf = para_malloc(2 * padd->frame_info.samples);
199 for (i = 0; i < padd->frame_info.samples; i++) {
200 short sh = ((short *)outbuffer)[i];
201 write_int16_host_endian(btr_buf + loaded, sh);
204 btr_add_output(btr_buf, loaded, btrn);
209 padd->consumed_total += ret;
210 btr_consume(btrn, ret);
216 btr_remove_node(btrn);
220 * The init function of the aacdec filter.
222 * \param f Pointer to the filter struct to initialize.
226 void aacdec_filter_init(struct filter *f)
228 f->open = aacdec_open;
229 f->close = aacdec_close;
230 f->pre_select = generic_filter_pre_select;
231 f->post_select = aacdec_post_select;
232 f->execute = aacdec_execute;