From faeabd31b4bea5c097acff4738a0626e3c84f1d9 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 19 Sep 2009 10:21:48 +0200 Subject: [PATCH] fecdec: Defer decoding until the first slice of the second group arrives. Otherwise, this could lead to buffer underruns in the decoding application in case slices are missed. So introdce group_completion_status which tracks whether we already have received the first group sucessfully and are waiting for the first slice of the next group. --- fecdec_filter.c | 71 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/fecdec_filter.c b/fecdec_filter.c index a3cba9bb..74e1b4f5 100644 --- a/fecdec_filter.c +++ b/fecdec_filter.c @@ -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 #include "para.h" @@ -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. */ @@ -80,6 +94,10 @@ struct private_fecdec_data { 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. */ @@ -153,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; } @@ -171,6 +196,8 @@ static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd) PARA_WARNING_LOG("Clearing incomplete group %d " "(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; } @@ -211,8 +238,8 @@ 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; } @@ -267,7 +294,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) @@ -339,7 +366,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 */ @@ -350,17 +377,34 @@ 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) { + 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; } @@ -400,9 +444,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; } -- 2.39.5