write: Make get_btr_value() return void.
[paraslash.git] / fecdec_filter.c
index fd35d5f5d6a278a615c34e02c9c096d775330c0a..939f7e3d06b5061e70275244ddcc3e112b7ce1ea 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2009-2010 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
  */
 #define NUM_FEC_GROUPS 3
 
-/** 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 {
        /** Total number of slices in this group. */
@@ -66,6 +61,8 @@ struct fecdec_group {
        struct fec_header h;
        /** How many slices received so far. */
        int num_received_slices;
+       /** Bitmap of received slices. */
+       uint8_t received_slices[32];
        /** The size of the \a idx and the \a data arrays below. */
        int num_slices;
        /** Array of indices of the received slices. */
@@ -107,18 +104,11 @@ static void clear_group(struct fecdec_group *fg)
 {
        int i;
 
-       for (i = 0; i < fg->num_slices; i++) {
+       for (i = 0; i < fg->num_slices; i++)
                free(fg->data[i]);
-               fg->data[i] = NULL;
-               fg->idx[i] = -1;
-       }
        free(fg->data);
-       fg->data = NULL;
        free(fg->idx);
-       fg->idx = NULL;
-       fg->num_slices = 0;
-       memset(&fg->h, 0, sizeof(struct fec_header));
-       fg->num_received_slices = 0;
+       memset(fg, 0, sizeof(*fg));
 }
 
 static int find_group(struct fec_header *h,
@@ -218,29 +208,40 @@ success:
        return ret;
 }
 
+static bool test_and_set_slice_bit(struct fecdec_group *fg, uint8_t slice_num)
+{
+       uint8_t *p = fg->received_slices + slice_num / 8, old = *p;
+
+       *p |= 1 << (slice_num % 8);
+       return old == *p;
+}
+
 /*
  * returns 1 if slice was added, zero otherwise (because the group was already
- * complete). In any case the number of received slices is being increased by
- * one.
+ * complete or a slice has been received twice).
  */
 static int add_slice(char *buf, struct fecdec_group *fg)
 {
-       int r, slice_num;
+       int r;
+       uint8_t slice_num = fg->h.slice_num;
 
        if (group_complete(fg)) {
                PARA_DEBUG_LOG("group %d complete, ignoring slice %d\n",
-                       fg->h.group_num, fg->h.slice_num);
-               fg->num_received_slices++;
+                       fg->h.group_num, slice_num);
                return 0;
        }
-       slice_num = fg->h.slice_num;
        if (fg->num_slices == 0) {
                fg->num_slices = fg->h.slices_per_group;
                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 *));
+               fg->data = para_calloc(fg->num_slices * sizeof(unsigned char *));
        }
        r = fg->num_received_slices;
+       /* Check if we already have this slice. */
+       if (test_and_set_slice_bit(fg, slice_num)) {
+               PARA_INFO_LOG("ignoring duplicate slice %d:%d\n", fg->h.group_num,
+                       slice_num);
+               return 0;
+       }
        fg->idx[r] = slice_num;
        fg->data[r] = para_malloc(fg->h.slice_bytes);
        memcpy(fg->data[r], buf, fg->h.slice_bytes);
@@ -326,11 +327,7 @@ static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
                                break;
                        if (sb + written > fg->h.audio_header_size)
                                n = fg->h.audio_header_size - written;
-                       if (fn->btrn)
-                               btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
-                       else
-                               memcpy(p + written, fg->data[i], n);
-                       fn->loaded += n;
+                       btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
                        written += n;
                }
                p += written;
@@ -340,11 +337,7 @@ static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
                size_t n = sb;
                if (n + written > fg->h.group_bytes)
                        n = fg->h.group_bytes - written;
-               if (fn->btrn)
-                       btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
-               else
-                       memcpy(p + written, fg->data[i], n);
-               fn->loaded += n;
+               btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
                written += n;
        }
        p += written;
@@ -443,12 +436,10 @@ static void fecdec_close(struct filter_node *fn)
 
        FOR_EACH_FECDEC_GROUP(fg, pfd)
                clear_group(fg);
-       free(fn->buf);
-       fn->buf = NULL;
        fec_free(pfd->fec);
+       btr_pool_free(pfd->btrp);
        free(fn->private_data);
        fn->private_data = NULL;
-       fn->conf = NULL;
 }
 
 static void fecdec_post_select(__a_unused struct sched *s, struct task *t)
@@ -491,11 +482,8 @@ out:
 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);
        pfd = para_calloc(sizeof(*pfd));
        fn->private_data = pfd;
-       fn->loaded = 0;
        fn->min_iqs = FEC_HEADER_SIZE;
 }