2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
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. */
17 #include "portable_io.h"
21 #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 sample_rate;
59 * Get a new libfaad decoder handle.
61 * \return The handle returned by NeAACDecOpen().
63 static NeAACDecHandle aac_open(void)
65 NeAACDecHandle h = NeAACDecOpen();
66 NeAACDecConfigurationPtr c = NeAACDecGetCurrentConfiguration(h);
68 c->defObjectType = LC;
69 c->outputFormat = FAAD_FMT_16BIT;
71 NeAACDecSetConfiguration(h, c);
75 static unsigned long aac_read_decoder_length(char *buf, int *description_len)
79 unsigned long length = 0;
84 length = (length << 7) | (b & 0x7F);
86 ((b & 0x80) && numBytes < 4);
87 *description_len = numBytes;
92 * Search for the position and the length of the decoder configuration.
94 * \param buf Buffer to seach.
95 * \param buflen Length of \a buf.
96 * \param skip Upon succesful return, this contains the offset in \a buf where
97 * the decoder config starts.
98 * \param decoder_length Result pointer that is filled in with the length of
99 * the decoder configuration on success.
103 static int aac_find_esds(char *buf, size_t buflen, size_t *skip,
104 unsigned long *decoder_length)
108 for (i = 0; i + 4 < buflen; i++) {
112 if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
116 PARA_INFO_LOG("found esds@%zu, next: %x\n", i, *p);
122 PARA_INFO_LOG("next: %x\n", *p);
127 PARA_INFO_LOG("next: %x\n", *p);
132 *decoder_length = aac_read_decoder_length(p, &description_len);
133 PARA_INFO_LOG("decoder length: %lu\n", *decoder_length);
134 i += description_len;
142 * Search for the first entry in the stco table.
144 * \param buf Buffer to seach.
145 * \param buflen Length of \a buf.
146 * \param skip Upon succesful return, this contains the number
147 * of bytes to skip from the input buffer.
149 * \return The position of the first entry in the table on success,
152 static ssize_t aac_find_entry_point(char *buf, size_t buflen, size_t *skip)
157 for (i = 0; i + 20 < buflen; i++) {
160 if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
162 PARA_INFO_LOG("found stco@%zu\n", i);
164 ret = read_u32_be(buf + i); /* first offset */
166 PARA_INFO_LOG("entry point: %zd\n", ret);
170 PARA_WARNING_LOG("stco not found, buflen: %zu\n", buflen);
174 static int aacdec_execute(struct btr_node *btrn, const char *cmd, char **result)
176 struct filter_node *fn = btr_context(btrn);
177 struct private_aacdec_data *padd = fn->private_data;
179 return decoder_execute(cmd, padd->sample_rate, padd->channels, result);
182 static void aacdec_open(struct filter_node *fn)
184 struct private_aacdec_data *padd = para_calloc(sizeof(*padd));
186 fn->private_data = padd;
188 padd->handle = aac_open();
191 static void aacdec_close(struct filter_node *fn)
193 struct private_aacdec_data *padd = fn->private_data;
195 NeAACDecClose(padd->handle);
197 fn->private_data = NULL;
200 static int aacdec_post_select(__a_unused struct sched *s, void *context)
202 struct filter_node *fn = context;
203 struct btr_node *btrn = fn->btrn;
204 struct private_aacdec_data *padd = fn->private_data;
206 char *p, *inbuf, *outbuffer;
208 size_t len, skip, consumed, loaded;
211 ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
216 btr_merge(btrn, fn->min_iqs);
217 len = btr_next_buffer(btrn, &inbuf);
218 len = PARA_MIN(len, (size_t)8192);
220 if (!padd->initialized) {
221 unsigned long rate = 0;
222 unsigned char channels = 0;
223 ret = aac_find_esds(inbuf, len, &skip, &padd->decoder_length);
225 PARA_INFO_LOG("%s\n", para_strerror(-ret));
226 ret = NeAACDecInit(padd->handle, (unsigned char *)inbuf,
227 len, &rate, &channels);
228 PARA_INFO_LOG("decoder init: %d\n", ret);
230 ret = -E_AACDEC_INIT;
235 PARA_INFO_LOG("decoder len: %lu\n",
236 padd->decoder_length);
238 p = inbuf + consumed;
239 ret = -E_AACDEC_INIT;
240 if (NeAACDecInit2(padd->handle, (unsigned char *)p,
241 padd->decoder_length, &rate,
245 padd->sample_rate = rate;
246 padd->channels = channels;
247 PARA_INFO_LOG("rate: %u, channels: %u\n",
248 padd->sample_rate, padd->channels);
249 padd->initialized = 1;
251 if (padd->decoder_length > 0) {
254 ret = aac_find_entry_point(inbuf + consumed,
255 len - consumed, &skip);
262 PARA_INFO_LOG("entry: %zu\n", padd->entry);
265 if (padd->consumed_total + len < padd->entry)
267 if (padd->consumed_total < padd->entry)
268 consumed = padd->entry - padd->consumed_total;
272 p = inbuf + consumed;
273 //PARA_CRIT_LOG("consumed: %zu (%zu + %zu), have: %zu\n", padd->consumed_total + consumed,
274 // padd->consumed_total, consumed, len - consumed);
275 outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info,
276 (unsigned char *)p, len - consumed);
277 if (padd->frame_info.error) {
278 int err = padd->frame_info.error;
280 if (padd->error_count++ > MAX_ERRORS)
282 PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(err));
283 PARA_ERROR_LOG("consumed: %zu + %zu + %lu\n",
284 padd->consumed_total, consumed,
285 padd->frame_info.bytesconsumed);
287 consumed++; /* catch 21 */
290 padd->error_count = 0;
291 //PARA_CRIT_LOG("decoder ate %lu\n", padd->frame_info.bytesconsumed);
292 consumed += padd->frame_info.bytesconsumed;
294 if (!padd->frame_info.samples)
296 btr_buf = para_malloc(2 * padd->frame_info.samples);
298 for (i = 0; i < padd->frame_info.samples; i++) {
299 short sh = ((short *)outbuffer)[i];
300 write_int16_host_endian(btr_buf + loaded, sh);
303 btr_add_output(btr_buf, loaded, btrn);
308 padd->consumed_total += ret;
309 btr_consume(btrn, ret);
314 btr_remove_node(&fn->btrn);
319 * The init function of the aacdec filter.
321 * \param f Pointer to the filter struct to initialize.
325 void aacdec_filter_init(struct filter *f)
327 f->open = aacdec_open;
328 f->close = aacdec_close;
329 f->pre_select = generic_filter_pre_select;
330 f->post_select = aacdec_post_select;
331 f->execute = aacdec_execute;