Merge commit 'meins/next' into next
[paraslash.git] / fecdec_filter.c
index 5f37f1a81f9199f6080b6669cd455d706a96af06..7fe8743794d38540ce132218fa8f6cc41ae1727f 100644 (file)
@@ -6,6 +6,8 @@
 
 /** \file fecdec_filter.c A filter fec-decodes an audio stream. */
 
+#include <regex.h>
+
 #include <dirent.h>
 #include "para.h"
 #include "error.h"
  */
 #define NUM_FEC_GROUPS 3
 
-/** Size of the output buffer of the fecdec filter. */
-#define FECDEC_OUTBUF_SIZE (1024 * 1024) /* FIXME: This has to depend on the fec params */
+/** Default size of the output buffer of the fecdec filter. */
+#define FECDEC_DEFAULT_OUTBUF_SIZE (3 * 1024)
+/** Maximal size of the output buffer of the fecdec filter. */
+#define FECDEC_MAX_OUTBUF_SIZE (1024 * 1024)
 
 /** Data read from the header of a slice. */
 struct fec_header {
@@ -76,6 +80,7 @@ struct private_fecdec_data {
        struct fec_parms *fec;
        /** Keeps track of what was received so far. */
        struct fecdec_group groups[NUM_FEC_GROUPS];
+       /** Whether an audio file header was already received. */
        int have_header;
 };
 
@@ -256,7 +261,7 @@ static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
 static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
 {
        int i, ret, sb = fg->h.slice_bytes;
-       size_t written = 0;
+       size_t written = 0, need;
        struct private_fecdec_data *pfd = fn->private_data;
        enum fec_group_usability u = group_is_usable(fg, pfd);
 
@@ -279,12 +284,18 @@ static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
        PARA_DEBUG_LOG("writing group %d (%d/%d decoded data bytes)\n",
                fg->h.group_num, fg->h.group_bytes,
                fg->h.data_slices_per_group * sb);
+       need = fn->loaded + (fg->h.data_slices_per_group - i)* sb;
+       if (need > fn->bufsize) {
+               fn->bufsize = PARA_MAX(fn->bufsize * 2, need);
+               if (fn->bufsize > FECDEC_MAX_OUTBUF_SIZE)
+                       return -E_FECDEC_OVERRUN;
+               PARA_INFO_LOG("increasing fec buf to %zu\n", fn->bufsize);
+               fn->buf = para_realloc(fn->buf, fn->bufsize);
+       }
        for (; i < fg->h.data_slices_per_group; i++) {
                size_t n = sb;
                if (n + written > fg->h.group_bytes)
                        n = fg->h.group_bytes - written;
-               if (fn->loaded + n > fn->bufsize)
-                       return -E_FECDEC_OVERRUN;
                memcpy(fn->buf + fn->loaded, fg->data[i], n);
                fn->loaded += n;
                written += n;
@@ -363,7 +374,7 @@ static ssize_t fecdec(char *buf, size_t len, struct filter_node *fn)
        ret = read_fec_header(buf, len, &h);
        if (ret <= 0)
                return ret;
-       if (!h.slice_bytes || h.slice_bytes > fn->bufsize)
+       if (!h.slice_bytes)
                return -E_BAD_SLICE_SIZE;
        if (h.slice_num > h.slices_per_group)
                return -E_BAD_SLICE_NUM;
@@ -391,7 +402,7 @@ static void fecdec_close(struct filter_node *fn)
 
 static void fecdec_open(struct filter_node *fn)
 {
-       fn->bufsize = FECDEC_OUTBUF_SIZE;
+       fn->bufsize = FECDEC_DEFAULT_OUTBUF_SIZE;
        fn->buf = para_malloc(fn->bufsize);
        fn->private_data = para_calloc(sizeof(struct private_fecdec_data));
        fn->loaded = 0;