X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=fecdec_filter.c;h=25533b2428fc6777ba53eb093abed7d5b30c049d;hp=2c63fc6b00ac6d671f74142ac74060bacece8bed;hb=5f303cc5a96bfeaa66d2d6e899bf56d1d03ed085;hpb=29a8e4384c08cb4e295e5b82994e62bf247a0ef7 diff --git a/fecdec_filter.c b/fecdec_filter.c index 2c63fc6b..25533b24 100644 --- a/fecdec_filter.c +++ b/fecdec_filter.c @@ -7,6 +7,7 @@ /** \file fecdec_filter.c A filter that fec-decodes an audio stream. */ #include +#include #include #include "para.h" @@ -14,6 +15,7 @@ #include "list.h" #include "sched.h" #include "ggo.h" +#include "buffer_tree.h" #include "filter.h" #include "string.h" #include "portable_io.h" @@ -40,7 +42,7 @@ struct fec_header { uint8_t slices_per_group; /** Number of slices needed to start decoding. */ uint8_t data_slices_per_group; - /** Size of the ogg vorbis header (zero for mp3, aac). */ + /** Size of the ogg vorbis/wma header (zero for mp3, aac). */ uint32_t audio_header_size; /** Number of the FEC group this slice belongs to. */ uint32_t group_num; @@ -72,20 +74,6 @@ 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. */ @@ -96,10 +84,9 @@ 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; + struct btr_pool *btrp; }; /** Iterate over all fecdec groups. */ @@ -126,7 +113,9 @@ static void clear_group(struct fecdec_group *fg) 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; @@ -177,8 +166,7 @@ static struct fecdec_group *try_to_free_group(struct private_fecdec_data *pfd) * 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) + if (fg == pfd->first_complete_group) continue; clear_group(fg); return fg; @@ -198,8 +186,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); + if (oldest == pfd->first_complete_group) + pfd->first_complete_group = NULL; clear_group(oldest); return oldest; } @@ -260,10 +248,21 @@ static int add_slice(char *buf, struct fecdec_group *fg) return 1; } +/** + * The different states of a complete FEC group. + * + * Even if a FEC group has been received successfully, it probably can not be + * used right away because some streams (ogg, wma) need to receive an audio + * file header before decoding can start. + */ enum fec_group_usability { + /** Drop the group (because we did not receive the header yet). */ FEC_GROUP_UNUSABLE, + /** Use all data in the group. */ FEC_GROUP_USABLE, + /** Use the group, but drop its audio file header. */ FEC_GROUP_USABLE_SKIP_HEADER, + /** Use the group, including its header. */ FEC_GROUP_USABLE_WITH_HEADER }; @@ -292,6 +291,7 @@ static int decode_group(struct fecdec_group *fg, struct filter_node *fn) size_t written, need; struct private_fecdec_data *pfd = fn->private_data; enum fec_group_usability u = group_is_usable(fg, pfd); + char *buf = NULL, *p; if (u == FEC_GROUP_UNUSABLE) { PARA_INFO_LOG("dropping unusable group %d\n", fg->h.group_num); @@ -312,13 +312,22 @@ 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) + if (fn->btrn) { + need = (fg->h.data_slices_per_group - i) * sb; + if (need > btr_pool_unused(pfd->btrp)) return -E_FECDEC_OVERRUN; - PARA_INFO_LOG("increasing fec buf to %zu\n", fn->bufsize); - fn->buf = para_realloc(fn->buf, fn->bufsize); + btr_pool_get_buffer(pfd->btrp, &buf); + p = buf; + } else { + 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); + } + p = fn->buf + fn->loaded; } if (u == FEC_GROUP_USABLE_WITH_HEADER) { PARA_INFO_LOG("writing audio file header\n"); @@ -329,20 +338,28 @@ 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; - memcpy(fn->buf + fn->loaded, fg->data[i], n); + if (fn->btrn) + btr_copy(fg->data[i], n, pfd->btrp, fn->btrn); + else + memcpy(p + written, fg->data[i], n); fn->loaded += n; written += n; } + p += written; } written = 0; 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; - memcpy(fn->buf + fn->loaded, fg->data[i], n); + if (fn->btrn) + btr_copy(fg->data[i], n, pfd->btrp, fn->btrn); + else + memcpy(p + written, fg->data[i], n); fn->loaded += n; written += n; } + p += written; return 0; } @@ -387,46 +404,48 @@ static int dispatch_slice(char *buf, size_t len, struct fec_header *h, 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 */ + PARA_CRIT_LOG("sb: %d, len: %d\n", h->slice_bytes, len); + if (h->slice_bytes > len) { /* can not use the thing, try to read more */ + fn->min_iqs = h->slice_bytes + FEC_HEADER_SIZE; return 0; + } ret = get_group(h, pfd, &fg); if (ret < 0) return ret; - if (!add_slice(buf, fg)) + if (!add_slice(buf, fg)) /* group already complete */ return 1; - if (group_complete(fg)) { - 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; + if (!group_complete(fg)) + return 1; + /* this slice completed the group */ + if (pfd->fec) + goto decode; + /* it's either the first or the second complete group */ + if (!pfd->first_complete_group) { /* it's the first group */ + enum fec_group_usability u = group_is_usable(fg, pfd); + assert(u != FEC_GROUP_USABLE_SKIP_HEADER); + if (u == FEC_GROUP_UNUSABLE) /* forget it */ return 1; - } - assert(pfd->fec); - ret = decode_group(fg, fn); - if (ret < 0) - return ret; + pfd->first_complete_group = fg; /* remember it */ 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); + /* we have two complete groups, let's go */ 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; + pfd->btrp = btr_pool_new("fecdec", 20 * k * h->slice_bytes); + /* decode and clear the first group */ ret = decode_group(pfd->first_complete_group, fn); if (ret < 0) return ret; - pfd->completion_status = GCS_FIRST_GROUP_DECODED; + clear_group(pfd->first_complete_group); + pfd->first_complete_group = NULL; +decode: + ret = decode_group(fg, fn); + if (ret < 0) + return ret; return 1; } @@ -462,6 +481,44 @@ static void fecdec_close(struct filter_node *fn) fec_free(pfd->fec); free(fn->private_data); fn->private_data = NULL; + fn->conf = NULL; +} + +static void fecdec_post_select(__a_unused struct sched *s, struct task *t) +{ + struct filter_node *fn = container_of(t, struct filter_node, task); + struct btr_node *btrn = fn->btrn; + int ret; + struct fec_header h; + char *buf; + size_t len; + +next_buffer: + ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL); + if (ret <= 0) + goto out; + btr_merge(btrn, fn->min_iqs); + len = btr_next_buffer(btrn, &buf); + ret = read_fec_header(buf, len, &h); + if (ret <= 0) + goto out; + ret = -E_BAD_SLICE_SIZE; + if (!h.slice_bytes) + goto out; + ret = -E_BAD_SLICE_NUM; + if (h.slice_num > h.slices_per_group) + goto out; + ret = dispatch_slice(buf + FEC_HEADER_SIZE, len - FEC_HEADER_SIZE, + &h, fn); + //PARA_INFO_LOG("ret: %d, len: %d, slice_bytes: %d\n", ret, len, h.slice_bytes); + if (ret <= 0) + goto out; + btr_consume(btrn, FEC_HEADER_SIZE + h.slice_bytes); + goto next_buffer; +out: + t->error = ret; + if (ret < 0) + btr_remove_node(btrn); } static void fecdec_open(struct filter_node *fn) @@ -470,19 +527,21 @@ static void fecdec_open(struct filter_node *fn) fn->bufsize = FECDEC_DEFAULT_OUTBUF_SIZE; fn->buf = para_malloc(fn->bufsize); pfd = para_calloc(sizeof(*pfd)); - pfd->completion_status = GCS_NO_COMPLETE_GROUP; fn->private_data = pfd; fn->loaded = 0; + fn->min_iqs = FEC_HEADER_SIZE; } /** * The init function of the fecdec filter. * - * \param f struct to initialize. + * \param f Struct to initialize. */ void fecdec_filter_init(struct filter *f) { f->convert = fecdec; f->close = fecdec_close; f->open = fecdec_open; + f->pre_select = generic_filter_pre_select; + f->post_select = fecdec_post_select; }