X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=fecdec_filter.c;h=bc5300627dbdfb7a1ab1bf37f0ac5aa681dbd4a2;hp=5c01a0395e79139a0bbd09633f127b06321d5268;hb=52e437d1054c5f62f0538b3934419a3b71aa3d83;hpb=2b3cc3af64d5574473c2f1ea9c32d7589abbb811 diff --git a/fecdec_filter.c b/fecdec_filter.c index 5c01a039..bc530062 100644 --- a/fecdec_filter.c +++ b/fecdec_filter.c @@ -28,7 +28,7 @@ #define NUM_FEC_GROUPS 3 /** Size of the output buffer of the fecdec filter. */ -#define FECDEC_OUTBUF_SIZE (128 * 1024) +#define FECDEC_OUTBUF_SIZE (1024 * 1024) /* FIXME: This has to depend on the fec params */ /** Data read from the header of a slice. */ struct fec_header { @@ -46,6 +46,10 @@ struct fec_header { uint8_t slice_num; /** Used data bytes of this slice. */ uint16_t slice_bytes; + /** Non-zero if this group is the beginning of the stream. */ + uint8_t bos; + /** Non-zero if this stream embedds audio headers into fec groups. */ + uint8_t header_stream; }; /** @@ -72,6 +76,7 @@ struct private_fecdec_data { struct fec_parms *fec; /** Keeps track of what was received so far. */ struct fecdec_group groups[NUM_FEC_GROUPS]; + int have_header; }; /** Iterate over all fecdec groups. */ @@ -162,6 +167,7 @@ static struct fecdec_group *free_oldest_group(struct private_fecdec_data *pfd) return oldest; } +/* returns 1 if the group was found, 0 if not, negative on errors */ static int get_group(struct fec_header *h, struct private_fecdec_data *pfd, struct fecdec_group **result) { @@ -180,18 +186,28 @@ static int get_group(struct fec_header *h, struct private_fecdec_data *pfd, if (fg) goto success; fg = free_oldest_group(pfd); + ret = 0; success: fg->h = *h; *result = fg; - return 1; + return ret; } +/* + * 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. + */ static int add_slice(char *buf, struct fecdec_group *fg) { int r, slice_num; - if (group_complete(fg)) + if (group_complete(fg)) { + PARA_DEBUG_LOG("group complete, ignoring slice %d\n", + 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; @@ -207,19 +223,58 @@ static int add_slice(char *buf, struct fecdec_group *fg) return 1; } +enum fec_group_usability { + FEC_GROUP_UNUSABLE, + FEC_GROUP_USABLE, + FEC_GROUP_USABLE_SKIP_HEADER, +}; + +static enum fec_group_usability group_is_usable(struct fecdec_group *fg, + struct private_fecdec_data *pfd) +{ + struct fec_header *h = &fg->h; + + if (!h->header_stream) + return FEC_GROUP_USABLE; + if (pfd->have_header) { + if (h->audio_header_size) + return FEC_GROUP_USABLE_SKIP_HEADER; + return FEC_GROUP_USABLE; + } + if (fg->h.bos) + return FEC_GROUP_USABLE; + if (fg->h.audio_header_size) + return FEC_GROUP_USABLE; + 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; struct private_fecdec_data *pfd = fn->private_data; + enum fec_group_usability u = group_is_usable(fg, pfd); + if (u == FEC_GROUP_UNUSABLE) { + 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, + fg->h.data_slices_per_group); ret = fec_decode(pfd->fec, fg->data, fg->idx, sb); if (ret < 0) return ret; + pfd->have_header = 1; + i = 0; + if (u == FEC_GROUP_USABLE_SKIP_HEADER) { + i = ROUND_UP(fg->h.audio_header_size, fg->h.slice_bytes) + / fg->h.slice_bytes; + PARA_DEBUG_LOG("skipping %d header slices\n", i); + } 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); - for (i = 0; i < fg->h.data_slices_per_group; i++) { + 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; @@ -256,13 +311,16 @@ static int read_fec_header(char *buf, size_t len, struct fec_header *h) h->slice_num = read_u8(buf + 18); h->slice_bytes = read_u16(buf + 20); - if (!h->group_bytes && & h->slice_bytes) + h->bos = read_u8(buf + 22); + h->header_stream = read_u8(buf + 23); + if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) return -E_FECDEC_EOF; // PARA_DEBUG_LOG("group %u, slize %u, slices per group: %u\n", // h->group_num, h->slice_num, h->slices_per_group); return 1; } +/* returns 1 if we used the buffer, 0 if we didn't, negative on errors */ static int dispatch_slice(char *buf, size_t len, struct fec_header *h, struct filter_node *fn) { @@ -275,14 +333,8 @@ static int dispatch_slice(char *buf, size_t len, struct fec_header *h, ret = get_group(h, pfd, &fg); if (ret < 0) return ret; - if (group_complete(fg)) { - PARA_DEBUG_LOG("group complete, ignoring slice %d\n", - h->slice_num); + if (!add_slice(buf, fg)) return 1; - } - ret = add_slice(buf, fg); - if (ret < 0) - return ret; if (group_complete(fg)) { if (!pfd->fec) { int k = h->data_slices_per_group, n = h->slices_per_group; @@ -298,7 +350,7 @@ static int dispatch_slice(char *buf, size_t len, struct fec_header *h, return 1; } -static int fecdec(char *buf, size_t len, struct filter_node *fn) +static ssize_t fecdec(char *buf, size_t len, struct filter_node *fn) { int ret; struct fec_header h; @@ -306,7 +358,7 @@ static int 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 > fn->bufsize) + if (!h.slice_bytes || h.slice_bytes > fn->bufsize) return -E_BAD_SLICE_SIZE; if (h.slice_num > h.slices_per_group) return -E_BAD_SLICE_NUM; @@ -327,6 +379,7 @@ static void fecdec_close(struct filter_node *fn) clear_group(fg); free(fn->buf); fn->buf = NULL; + fec_free(pfd->fec); free(fn->private_data); fn->private_data = NULL; }