Rename struct audio format to audio_format_handler
[paraslash.git] / aacdec.c
index c41a5ac..c36f687 100644 (file)
--- a/aacdec.c
+++ b/aacdec.c
  *     along with this program; if not, write to the Free Software
  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  */
-
 /*
  * based in parts on libfaad, Copyright (C) 2003-2005 M. Bakker,
  * Ahead Software AG
  */
 
-/** \file aacdec.c paraslash's mp3 decoder */
+/** \file aacdec.c paraslash's aac (m4a) decoder */
 
 #include "para.h"
 
@@ -31,8 +30,8 @@
 #include "string.h"
 #include "aac.h"
 
-/** the output buffer size */
 #define MAX_CHANNELS 6
+/** the output buffer size */
 #define AAC_OUTBUF_SIZE (FAAD_MIN_STREAMSIZE * MAX_CHANNELS)
 
 /**
  *
  * \sa filter, filter_node
  */
-struct private_mp4dec_data {
-       NeAACDecHandle decoder;
+struct private_aacdec_data {
+       NeAACDecHandle handle;
        NeAACDecFrameInfo frame_info;
 
        int initialized;
        int decoder_length;
-       unsigned char *inbuf;
-       int inbuf_len;
-       int consumed;
-       long unsigned consumed_total;
-
-       unsigned noffsets;
-       unsigned *offset;
-       unsigned offset0;
-       int offset_pos;
+       size_t consumed_total;
+       size_t entry;
 };
 
-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;
-}
-
-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)
+static ssize_t aacdec(char *input_buffer, size_t len, struct filter_node *fn)
 {
-       int ret;
-
-       for (; padd->consumed < padd->inbuf_len; padd->consumed++) {
-               unsigned 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 private_aacdec_data *padd = fn->private_data;
        struct filter_chain_info *fci = fn->fci;
-       unsigned long rate = 0;
-       unsigned char channels = 0;
-       int i, ret, nbytes, skip;
+       int i, ret;
        unsigned char *p, *outbuffer;
+       unsigned char *inbuf = (unsigned char*)input_buffer;
+       size_t skip, consumed = 0;
 
        if (fn->loaded > fn->bufsize * 4 / 5)
                return 0;
        if (len < 1000 && !*fci->eof)
                return 0;
-       padd->consumed = 0;
-       padd->inbuf = (unsigned char*)inbuffer;
-       padd->inbuf_len = len;
 
        if (!padd->initialized) {
-               padd->decoder_length = aac_find_esds(padd->inbuf, padd->inbuf_len,
-                       &skip);
+               unsigned long rate = 0;
+               unsigned char channels = 0;
+               padd->decoder_length = aac_find_esds(inbuf, len, &skip);
                PARA_INFO_LOG("decoder len: %d\n", padd->decoder_length);
                if (padd->decoder_length < 0) {
-                       ret = NeAACDecInit(padd->decoder, padd->inbuf,
-                               padd->inbuf_len, &rate, &channels);
+                       ret = NeAACDecInit(padd->handle, inbuf,
+                               len, &rate, &channels);
                        PARA_INFO_LOG("decoder init: %d\n", ret);
                        if (ret < 0) {
                                ret = -E_AACDEC_INIT;
                                goto out;
                        }
-                       padd->consumed = ret;
+                       consumed = ret;
                } else {
-                       padd->consumed += skip;
-                       p = padd->inbuf + padd->consumed;
+                       consumed += skip;
+                       p = inbuf + consumed;
                        ret = -E_AACDEC_INIT;
-                       if (NeAACDecInit2(padd->decoder, p,
+                       if (NeAACDecInit2(padd->handle, p,
                                        padd->decoder_length, &rate,
                                        &channels) < 0)
                                goto out;
@@ -146,83 +93,74 @@ static ssize_t mp4dec(char *inbuffer, size_t len, struct filter_node *fn)
                padd->initialized = 1;
        }
        if (padd->decoder_length > 0) {
-               padd->consumed = 0;
-               if (!padd->offset_pos) {
-                       ret = aac_find_stco(padd->inbuf + padd->consumed,
-                               padd->inbuf_len - padd->consumed, &skip);
+               consumed = 0;
+               if (!padd->entry) {
+                       ret = aac_find_entry_point(inbuf + consumed,
+                               len - consumed, &skip);
                        if (ret < 0) {
                                ret = len;
                                goto out;
                        }
-                       padd->noffsets = ret;
-                       padd->offset = para_malloc(padd->noffsets * sizeof(int));
-                       padd->consumed += skip;
-               }
-               if (padd->offset_pos < padd->noffsets) {
-                       fill_offset_table(padd);
-                       ret = padd->consumed;
-                       goto out;
+                       consumed += skip;
+                       padd->entry = ret;
+                       PARA_INFO_LOG("entry: %zu\n", padd->entry);
                }
-//             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])
+               if (padd->consumed_total + len < padd->entry)
                        goto out;
-               if (padd->consumed_total < padd->offset[0])
-                       padd->consumed = padd->offset[0] - padd->consumed_total;
+               if (padd->consumed_total < padd->entry)
+                       consumed = padd->entry - padd->consumed_total;
        }
-       for (; padd->consumed < padd->inbuf_len; padd->consumed++)
-               if ((padd->inbuf[padd->consumed] & 0xfe) == 0x20)
+       for (; consumed < len;consumed++)
+               if ((inbuf[consumed] & 0xfe) == 0x20)
                        break;
-       if (padd->consumed >= padd->inbuf_len)
+       if (consumed >= len)
                goto success;
-       p = padd->inbuf + padd->consumed;
-//     PARA_NOTICE_LOG("p[0]: %lx\n", (long unsigned) *p);
-       outbuffer = NeAACDecDecode(padd->decoder, &padd->frame_info, p,
-               len - padd->consumed);
+       p = inbuf + consumed;
+       outbuffer = NeAACDecDecode(padd->handle, &padd->frame_info, p,
+               len - consumed);
        ret = -E_AAC_DECODE;
        if (padd->frame_info.error != 0) {
-               PARA_ERROR_LOG("frame_error: %d, consumed: %lu + %d + %lu\n",
+               PARA_ERROR_LOG("frame_error: %d, consumed: %zu + %zd + %lu\n",
                        padd->frame_info.error, padd->consumed_total,
-                       padd->consumed, padd->frame_info.bytesconsumed);
+                       consumed, padd->frame_info.bytesconsumed);
                PARA_ERROR_LOG("%s\n", NeAACDecGetErrorMessage(
                        padd->frame_info.error));
-               padd->consumed++; /* catch 21 */
+               consumed++; /* catch 21 */
                goto success;
        }
-       padd->consumed += padd->frame_info.bytesconsumed;
-       ret = padd->consumed;
+       consumed += padd->frame_info.bytesconsumed;
+       ret = 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;
        }
 success:
-       ret = padd->consumed;
+       ret = consumed;
 out:
        if (ret > 0)
                padd->consumed_total += ret;
        return ret;
 }
 
-static void mp4dec_open(struct filter_node *fn)
+static void aacdec_open(struct filter_node *fn)
 {
-       fn->private_data = para_calloc(sizeof(struct private_mp4dec_data));
-       struct private_mp4dec_data *padd = fn->private_data;
+       fn->private_data = para_calloc(sizeof(struct private_aacdec_data));
+       struct private_aacdec_data *padd = fn->private_data;
 
        fn->bufsize = AAC_OUTBUF_SIZE;
        fn->buf = para_calloc(fn->bufsize);
-       padd->decoder = aac_open();
+       padd->handle = aac_open();
 }
 
-static void mp4dec_close(struct filter_node *fn)
+static void aacdec_close(struct filter_node *fn)
 {
-       struct private_mp4dec_data *padd = fn->private_data;
+       struct private_aacdec_data *padd = fn->private_data;
 
-       NeAACDecClose(padd->decoder);
+       NeAACDecClose(padd->handle);
        free(fn->buf);
        fn->buf = NULL;
        free(padd);
@@ -230,13 +168,13 @@ static void mp4dec_close(struct filter_node *fn)
 }
 
 /**
- * the init function of the mp3dec filter
+ * the init function of the aacdec filter
  *
  * \sa filter::init
  */
 void aacdec_init(struct filter *f)
 {
-       f->open = mp4dec_open;
-       f->convert = mp4dec;
-       f->close = mp4dec_close;
+       f->open = aacdec_open;
+       f->convert = aacdec;
+       f->close = aacdec_close;
 }