Rename struct audio format to audio_format_handler
[paraslash.git] / aacdec.c
index 315f338..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;
-       int consumed;
-       long unsigned consumed_total;
-
-       long unsigned entry;
+       size_t consumed_total;
+       size_t entry;
 };
 
-static ssize_t mp4dec(char *input_buffer, size_t len, struct filter_node *fn)
+static ssize_t aacdec(char *input_buffer, 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, 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;
 
        if (!padd->initialized) {
+               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, inbuf,
+                       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 = 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;
@@ -96,44 +93,44 @@ static ssize_t mp4dec(char *input_buffer, size_t len, struct filter_node *fn)
                padd->initialized = 1;
        }
        if (padd->decoder_length > 0) {
-               padd->consumed = 0;
+               consumed = 0;
                if (!padd->entry) {
-                       ret = aac_find_stco(inbuf + padd->consumed,
-                               len - padd->consumed, &skip);
+                       ret = aac_find_entry_point(inbuf + consumed,
+                               len - consumed, &skip);
                        if (ret < 0) {
                                ret = len;
                                goto out;
                        }
-                       padd->consumed += skip;
-                       padd->entry = aac_read_int32(inbuf + padd->consumed);
-                       PARA_INFO_LOG("entry: %lu\n", padd->entry);
+                       consumed += skip;
+                       padd->entry = ret;
+                       PARA_INFO_LOG("entry: %zu\n", padd->entry);
                }
                ret = len;
                if (padd->consumed_total + len < padd->entry)
                        goto out;
                if (padd->consumed_total < padd->entry)
-                       padd->consumed = padd->entry - padd->consumed_total;
+                       consumed = padd->entry - padd->consumed_total;
        }
-       for (; padd->consumed < len; padd->consumed++)
-               if ((inbuf[padd->consumed] & 0xfe) == 0x20)
+       for (; consumed < len;consumed++)
+               if ((inbuf[consumed] & 0xfe) == 0x20)
                        break;
-       if (padd->consumed >= len)
+       if (consumed >= len)
                goto success;
-       p = inbuf + padd->consumed;
-       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;
        for (i = 0; i < padd->frame_info.samples; i++) {
@@ -142,28 +139,28 @@ static ssize_t mp4dec(char *input_buffer, size_t len, struct filter_node *fn)
                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);
@@ -171,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;
 }