fecdec: Defer decoding until the next group after the first _usable_ fec group starts.
[paraslash.git] / fecdec_filter.c
index 30ac818c360873f6aedadd19e55ca7e0f6209550..18dcb34d4801bb24d70472ca66c80ef08af8e73f 100644 (file)
@@ -4,7 +4,7 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file fecdec_filter.c A filter fec-decodes an audio stream. */
+/** \file fecdec_filter.c A filter that fec-decodes an audio stream. */
 
 #include <dirent.h>
 #include "para.h"
@@ -28,7 +28,7 @@
 #define NUM_FEC_GROUPS 3
 
 /** Default size of the output buffer of the fecdec filter. */
-#define FECDEC_DEFAULT_OUTBUF_SIZE (16 * 1024)
+#define FECDEC_DEFAULT_OUTBUF_SIZE (3 * 1024)
 /** Maximal size of the output buffer of the fecdec filter. */
 #define FECDEC_MAX_OUTBUF_SIZE (1024 * 1024)
 
@@ -70,6 +70,20 @@ struct fecdec_group {
        unsigned char **data;
 };
 
+/**
+ * The fecdec filter defers decoding of the first group until the first slice
+ * of the next group was received. This avoids buffer underruns in subsequent
+ * filters of the filter chain.
+ */
+enum group_completion_status {
+       /** No complete group received so far. */
+       GCS_NO_COMPLETE_GROUP,
+       /** First group received, but not yet decoded. */
+       GCS_FIRST_GROUP_COMPLETE,
+       /** At least one complete group decoded. */
+       GCS_FIRST_GROUP_DECODED,
+};
+
 /**
  * Data private to the fecdec filter.
  */
@@ -78,12 +92,17 @@ 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;
+       /** See \ref group_completion_status. */
+       unsigned completion_status;
+       /** Points to the first received group. */
+       struct fecdec_group *first_complete_group;
 };
 
 /** Iterate over all fecdec groups. */
 #define FOR_EACH_FECDEC_GROUP(g, d) for (g = (d)->groups; \
-       (g) - (d)->groups < NUM_FEC_GROUPS; (g)++)
+       (g) < (d)->groups + NUM_FEC_GROUPS; (g)++)
 
 static int group_complete(struct fecdec_group *fg)
 {
@@ -119,10 +138,15 @@ static int find_group(struct fec_header *h,
        FOR_EACH_FECDEC_GROUP(fg, pfd) {
                if (fg->h.group_num != h->group_num)
                        continue;
+               if (fg->num_received_slices == 0)
+                       goto success;
                if (fg->h.slices_per_group != h->slices_per_group)
-                       continue;
+                       return -E_BAD_FEC_HEADER;
                if (fg->h.data_slices_per_group != h->data_slices_per_group)
-                       continue;
+                       return -E_BAD_FEC_HEADER;
+               if (fg->h.group_bytes != h->group_bytes)
+                       return -E_BAD_FEC_HEADER;
+success:
                *result = fg;
                return 1;
        }
@@ -147,6 +171,13 @@ static struct fecdec_group *try_to_free_group(struct private_fecdec_data *pfd)
        FOR_EACH_FECDEC_GROUP(fg, pfd) {
                if (!group_complete(fg))
                        continue;
+               /*
+                * Don't clear the first complete group if it has not yet been
+                * decoded.
+                */
+               if (pfd->completion_status == GCS_FIRST_GROUP_COMPLETE
+                               && pfd->first_complete_group == fg)
+                       continue;
                clear_group(fg);
                return fg;
        }
@@ -163,8 +194,10 @@ static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd)
        }
        if (!group_complete(oldest) && !group_empty(oldest))
                PARA_WARNING_LOG("Clearing incomplete group %d "
-                       "(contains %d slices)\n", fg->h.group_num,
-                       fg->num_received_slices);
+                       "(contains %d slices)\n", oldest->h.group_num,
+                       oldest->num_received_slices);
+       assert(pfd->completion_status != GCS_FIRST_GROUP_COMPLETE
+               || oldest != pfd->first_complete_group);
        clear_group(oldest);
        return oldest;
 }
@@ -205,21 +238,21 @@ static int add_slice(char *buf, struct fecdec_group *fg)
        int r, slice_num;
 
        if (group_complete(fg)) {
-               PARA_DEBUG_LOG("group complete, ignoring slice %d\n",
-                       fg->h.slice_num);
+               PARA_DEBUG_LOG("group %d complete, ignoring slice %d\n",
+                       fg->h.group_num, fg->h.slice_num);
                fg->num_received_slices++;
                return 0;
        }
        slice_num = fg->h.slice_num;
        if (fg->num_slices == 0) {
                fg->num_slices = fg->h.slices_per_group;
-               fg->idx = malloc(fg->num_slices * sizeof(int));
-               fg->data = malloc(fg->num_slices * sizeof(unsigned char *));
+               fg->idx = para_malloc(fg->num_slices * sizeof(int));
+               fg->data = para_malloc(fg->num_slices * sizeof(unsigned char *));
                memset(fg->data, 0, fg->num_slices * sizeof(unsigned char *));
        }
        r = fg->num_received_slices;
        fg->idx[r] = slice_num;
-       fg->data[r] = malloc(fg->h.slice_bytes);
+       fg->data[r] = para_malloc(fg->h.slice_bytes);
        memcpy(fg->data[r], buf, fg->h.slice_bytes);
        fg->num_received_slices++;
        return 1;
@@ -229,6 +262,7 @@ enum fec_group_usability {
        FEC_GROUP_UNUSABLE,
        FEC_GROUP_USABLE,
        FEC_GROUP_USABLE_SKIP_HEADER,
+       FEC_GROUP_USABLE_WITH_HEADER
 };
 
 static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
@@ -246,14 +280,14 @@ static enum fec_group_usability group_is_usable(struct fecdec_group *fg,
        if (fg->h.bos)
                return FEC_GROUP_USABLE;
        if (fg->h.audio_header_size)
-               return FEC_GROUP_USABLE;
+               return FEC_GROUP_USABLE_WITH_HEADER;
        return FEC_GROUP_UNUSABLE;
 }
 
 static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
 {
        int i, ret, sb = fg->h.slice_bytes;
-       size_t written = 0, need;
+       size_t written, need;
        struct private_fecdec_data *pfd = fn->private_data;
        enum fec_group_usability u = group_is_usable(fg, pfd);
 
@@ -261,7 +295,7 @@ static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
                PARA_INFO_LOG("dropping unusable group %d\n", fg->h.group_num);
                return 0;
        }
-       PARA_DEBUG_LOG("decoding group %d %d slices\n", fg->h.group_num,
+       PARA_DEBUG_LOG("decoding group %d (%d slices)\n", fg->h.group_num,
                fg->h.data_slices_per_group);
        ret = fec_decode(pfd->fec, fg->data, fg->idx, sb);
        if (ret < 0)
@@ -284,6 +318,21 @@ static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
                PARA_INFO_LOG("increasing fec buf to %zu\n", fn->bufsize);
                fn->buf = para_realloc(fn->buf, fn->bufsize);
        }
+       if (u == FEC_GROUP_USABLE_WITH_HEADER) {
+               PARA_INFO_LOG("writing audio file header\n");
+               written = 0;
+               for (i = 0; i < fg->h.data_slices_per_group; i++) {
+                       size_t n = sb;
+                       if (written >= fg->h.audio_header_size)
+                               break;
+                       if (sb + written > fg->h.audio_header_size)
+                               n = fg->h.audio_header_size - written;
+                       memcpy(fn->buf + fn->loaded, fg->data[i], n);
+                       fn->loaded += n;
+                       written += n;
+               }
+       }
+       written = 0;
        for (; i < fg->h.data_slices_per_group; i++) {
                size_t n = sb;
                if (n + written > fg->h.group_bytes)
@@ -333,7 +382,7 @@ static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
                struct filter_node *fn)
 {
        struct fecdec_group *fg;
-       int ret;
+       int ret, k, n;
        struct private_fecdec_data *pfd = fn->private_data;
 
        if (h->slice_bytes > len) /* can not use the thing, try to read more */
@@ -344,17 +393,38 @@ static int dispatch_slice(char *buf, size_t len, struct fec_header *h,
        if (!add_slice(buf, fg))
                return 1;
        if (group_complete(fg)) {
-               if (!pfd->fec) {
-                       int k = h->data_slices_per_group, n = h->slices_per_group;
-                       PARA_NOTICE_LOG("init fec (%d, %d)\n", k, n);
-                       ret = fec_new(k, n, &pfd->fec);
-                       if (ret < 0)
-                               return ret;
+               if (pfd->completion_status == GCS_NO_COMPLETE_GROUP) {
+                       enum fec_group_usability u = group_is_usable(fg, pfd);
+                       assert(u != FEC_GROUP_USABLE_SKIP_HEADER);
+                       if (u == FEC_GROUP_UNUSABLE)
+                               return 1;
+                       pfd->completion_status = GCS_FIRST_GROUP_COMPLETE;
+                       pfd->first_complete_group = fg;
+                       return 1;
                }
+               assert(pfd->fec);
                ret = decode_group(fg, fn);
                if (ret < 0)
                        return ret;
+               return 1;
        }
+       if (pfd->completion_status == GCS_NO_COMPLETE_GROUP)
+               return 1;
+       if (pfd->completion_status == GCS_FIRST_GROUP_DECODED)
+               return 1;
+       if (fg == pfd->first_complete_group)
+               return 1;
+       assert(!pfd->fec);
+       k = h->data_slices_per_group;
+       n = h->slices_per_group;
+       PARA_NOTICE_LOG("init fec (%d, %d)\n", k, n);
+       ret = fec_new(k, n, &pfd->fec);
+       if (ret < 0)
+               return ret;
+       ret = decode_group(pfd->first_complete_group, fn);
+       if (ret < 0)
+               return ret;
+       pfd->completion_status = GCS_FIRST_GROUP_DECODED;
        return 1;
 }
 
@@ -366,7 +436,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;
@@ -394,9 +464,12 @@ static void fecdec_close(struct filter_node *fn)
 
 static void fecdec_open(struct filter_node *fn)
 {
+       struct private_fecdec_data *pfd;
        fn->bufsize = FECDEC_DEFAULT_OUTBUF_SIZE;
        fn->buf = para_malloc(fn->bufsize);
-       fn->private_data = para_calloc(sizeof(struct private_fecdec_data));
+       pfd = para_calloc(sizeof(*pfd));
+       pfd->completion_status = GCS_NO_COMPLETE_GROUP;
+       fn->private_data = pfd;
        fn->loaded = 0;
 }