--- /dev/null
+/*
+ * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ */
+
+/** \file aacdec.c paraslash's mp3 decoder */
+
+#include "para.h"
+
+#include "list.h"
+#include "filter.h"
+#include "error.h"
+#include <mad.h>
+#include "string.h"
+
+#include <neaacdec.h>
+
+/** the output buffer size */
+#define MAX_CHANNELS 6
+#define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
+
+/**
+ * data specific to the aacdec filter
+ *
+ * \sa filter, filter_node
+ */
+struct private_mp4dec_data {
+ NeAACDecHandle decoder;
+ NeAACDecConfigurationPtr config;
+ NeAACDecFrameInfo frame_info;
+ mp4AudioSpecificConfig mp4ASC;
+
+ int initialized;
+ char *inbuf;
+ int inbuf_len;
+ int consumed;
+ long unsigned consumed_total;
+
+ int noffsets;
+ int *offset;
+ int offset_pos;
+
+#if 0
+ int nsamples;
+ int *table;
+ int table_pos;
+#endif
+};
+
+static int read_mp4_descr_length(struct private_mp4dec_data *padd)
+{
+ uint8_t b;
+ uint8_t numBytes = 0;
+ uint32_t length = 0;
+
+ do {
+ b = padd->inbuf[padd->consumed + numBytes];
+ numBytes++;
+ length = (length << 7) | (b & 0x7F);
+ } while
+ ((b & 0x80) && numBytes < 4);
+ padd->consumed += numBytes;
+ return length;
+}
+
+static int find_esds(struct private_mp4dec_data *padd)
+{
+ for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
+ char *p = padd->inbuf + padd->consumed;
+ int decoder_length;
+
+ if (p[0] != 'e' || p[1] != 's' || p[2] != 'd' || p[3] != 's')
+ continue;
+ padd->consumed += 8;
+ p = padd->inbuf + padd->consumed;
+ PARA_INFO_LOG("found esds: %d, next: %x\n", padd->consumed, *p);
+ if (*p == 3)
+ padd->consumed += 8;
+ else
+ padd->consumed += 6;
+ p = padd->inbuf + padd->consumed;
+ PARA_INFO_LOG("next: %x\n", *p);
+ if (*p != 4)
+ continue;
+ padd->consumed += 18;
+ p = padd->inbuf + padd->consumed;
+ PARA_INFO_LOG("next: %x\n", *p);
+ if (*p != 5)
+ continue;
+ padd->consumed++;
+ decoder_length = read_mp4_descr_length(padd);
+ PARA_INFO_LOG("decoder length: %d\n", decoder_length);
+ p = padd->inbuf + padd->consumed;
+ PARA_INFO_LOG("decoder data0: %x\n", *p & 0xff);
+ p++;
+ PARA_INFO_LOG("decoder data1: %x\n", *p & 0xff);
+ return decoder_length;
+ }
+ return -E_ESDS;
+}
+
+static int read_int32(struct private_mp4dec_data *padd, unsigned *result)
+{
+ uint8_t *d = (uint8_t*)(padd->inbuf + padd->consumed);
+ if (padd->consumed + 4 > padd->inbuf_len)
+ return -1;
+ padd->consumed += 4;
+ *result = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
+ return 1;
+}
+
+#if 0
+int fill_table(struct private_mp4dec_data *padd)
+{
+ int i, ret;
+
+ for (i = padd->table_pos; i < padd->nsamples; i++) {
+ ret = read_int32(padd, &padd->table[i]);
+ if (ret < 0)
+ return -1;
+ PARA_DEBUG_LOG("sample #%d: %d\n", i, padd->table[i]);
+ padd->table_pos++;
+ }
+ return 1;
+
+}
+int find_stsz(struct private_mp4dec_data *padd)
+{
+ int ret;
+
+ for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
+ char *p = padd->inbuf + padd->consumed;
+
+ if (p[0] != 's' || p[1] != 't' || p[2] != 's' || p[3] != 'z')
+ continue;
+ PARA_INFO_LOG("found stsz: %d\n", padd->consumed);
+ padd->consumed += 12;
+ ret = -E_STSZ;
+ if (read_int32(padd, &padd->nsamples) < 0)
+ goto out;
+ PARA_INFO_LOG("num samples: %d\n", padd->nsamples);
+ padd->table = para_malloc(padd->nsamples * sizeof(int));
+ return 1;
+ }
+ ret = -E_STSZ;
+out:
+ return ret;
+}
+#endif
+
+static int fill_offset_table(struct private_mp4dec_data *padd)
+{
+ int i, ret;
+
+ for (i = padd->offset_pos; i < padd->noffsets; i++) {
+ ret = read_int32(padd, &padd->offset[i]);
+ if (ret < 0)
+ return -1;
+ PARA_DEBUG_LOG("offset #%d: %d\n", i, padd->offset[i]);
+ padd->offset_pos++;
+ }
+ return 1;
+}
+
+static int find_stco(struct private_mp4dec_data *padd)
+{
+ int ret;
+
+ for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
+ char *p = padd->inbuf + padd->consumed;
+
+ if (p[0] != 's' || p[1] != 't' || p[2] != 'c' || p[3] != 'o')
+ continue;
+ PARA_INFO_LOG("found stco: %d\n", padd->consumed);
+ padd->consumed += 8;
+ ret = read_int32(padd, &padd->noffsets);
+ padd->offset = para_malloc(padd->noffsets * sizeof(int));
+ PARA_INFO_LOG("num entries: %d\n", padd->noffsets);
+ return 1;
+ }
+ return -E_STCO;
+}
+
+static ssize_t mp4dec(char *inbuffer, size_t len, struct filter_node *fn)
+{
+ struct private_mp4dec_data *padd = fn->private_data;
+ struct filter_chain_info *fci = fn->fci;
+ unsigned long rate = 0;
+ unsigned char channels = 0;
+ int i, ret, nbytes;
+ unsigned char *p, *outbuffer;
+
+ if (fn->loaded > fn->bufsize * 4 / 5)
+ return 0;
+ if (len < 10 && !*fci->eof)
+ return 0;
+ padd->consumed = 0;
+ padd->inbuf = inbuffer;
+ padd->inbuf_len = len;
+
+ if (!padd->initialized) {
+ ret = find_esds(padd);
+ if (ret < 0)
+ goto out;
+
+ p = inbuffer + padd->consumed;
+ ret = E_AACDEC_INIT;
+ if (NeAACDecInit2(padd->decoder, p, ret, &rate, &channels) < 0) {
+ PARA_INFO_LOG("header not found, consumed: %d\n",
+ padd->consumed);
+ goto out;
+ }
+ fci->samplerate = rate;
+ fci->channels = channels;
+ PARA_INFO_LOG("rate: %u, channels: %d\n", fci->samplerate,
+ fci->channels);
+ padd->initialized = 1;
+ }
+ padd->consumed = 0;
+#if 0
+ if (!padd->table_pos) {
+ ret = find_stsz(padd);
+ if (ret < 0)
+ goto out;
+ }
+ if (padd->table_pos < padd->nsamples) {
+ fill_table(padd);
+ ret = padd->consumed;
+ goto out;
+ }
+#endif
+ if (!padd->offset_pos) {
+// ret = find_stco(padd);
+// if (ret < 0)
+// goto out;
+ ret = len;
+ if (find_stco(padd) < 0)
+ goto out;
+ }
+ if (padd->offset_pos < padd->noffsets) {
+ fill_offset_table(padd);
+ ret = padd->consumed;
+ goto out;
+ }
+ PARA_INFO_LOG("consumed total: %lu, first_chunk: %d\n",
+ padd->consumed_total, padd->offset[0]);
+ ret = len;
+ if (padd->consumed_total + len < padd->offset[0])
+ goto out;
+ if (padd->consumed_total < padd->offset[0])
+ padd->consumed = padd->offset[0] - padd->consumed_total;
+ p = padd->inbuf + padd->consumed;
+ outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
+ len - padd->consumed);
+ PARA_INFO_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
+ padd->frame_info.error, padd->consumed_total,
+ padd->consumed, padd->frame_info.bytesconsumed);
+ ret = -E_AAC_DECODE;
+ if (padd->frame_info.error != 0) {
+ PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
+ padd->frame_info.error));
+ goto out;
+ }
+ padd->consumed += padd->frame_info.bytesconsumed;
+ ret = padd->consumed;
+ if (!padd->frame_info.samples)
+ goto out;
+ nbytes = padd->frame_info.samples;
+ for (i = 0; i < padd->frame_info.samples; i++) {
+ short *s = (short *)outbuffer;
+ fn->buf[fn->loaded++] = s[i] & 0xff;
+ fn->buf[fn->loaded++] = (s[i] >> 8) & 0xff;
+ }
+ ret = padd->consumed;
+out:
+ if (ret > 0)
+ padd->consumed_total += ret;
+ return ret;
+}
+
+static void mp4dec_open(struct filter_node *fn)
+{
+ fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
+ struct private_mp4dec_data *padd = fn->private_data;
+
+ fn->bufsize = AAC_OUTBUF_SIZE;
+ fn->buf = para_calloc(fn->bufsize);
+
+ padd->decoder = NeAACDecOpen();
+ padd->config = NeAACDecGetCurrentConfiguration(padd->decoder);
+ padd->config->defObjectType = LC;
+ padd->config->outputFormat = FAAD_FMT_16BIT;
+ padd->config->downMatrix = 0;
+ NeAACDecSetConfiguration(padd->decoder, padd->config);
+}
+
+static void mp4dec_close(struct filter_node *fn)
+{
+ struct private_mp4dec_data *padd = fn->private_data;
+
+ NeAACDecClose(padd->decoder);
+ free(fn->buf);
+ fn->buf = NULL;
+ free(padd);
+ fn->private_data = NULL;
+}
+
+/**
+ * the init function of the mp3dec filter
+ *
+ * \sa filter::init
+ */
+void aacdec_init(struct filter *f)
+{
+ f->open = mp4dec_open;
+ f->convert = mp4dec;
+ f->close = mp4dec_close;
+}