/*
- * 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.
*/
/** \file fecdec_filter.c A filter that fec-decodes an audio stream. */
#include <regex.h>
+#include <stdbool.h>
#include <dirent.h>
#include "para.h"
#include "list.h"
#include "sched.h"
#include "ggo.h"
+#include "buffer_tree.h"
#include "filter.h"
#include "string.h"
#include "portable_io.h"
*/
#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. */
int have_header;
/** Points to the first received group. */
struct fecdec_group *first_complete_group;
+ struct btr_pool *btrp;
};
/** Iterate over all fecdec groups. */
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);
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)
- return -E_FECDEC_OVERRUN;
- PARA_INFO_LOG("increasing fec buf to %zu\n", fn->bufsize);
- fn->buf = para_realloc(fn->buf, fn->bufsize);
- }
+ need = (fg->h.data_slices_per_group - i) * sb;
+ if (need > btr_pool_unused(pfd->btrp))
+ return -E_FECDEC_OVERRUN;
+ btr_pool_get_buffer(pfd->btrp, &buf);
+ p = buf;
if (u == FEC_GROUP_USABLE_WITH_HEADER) {
PARA_INFO_LOG("writing audio file header\n");
written = 0;
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;
+ btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
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);
- fn->loaded += n;
+ btr_copy(fg->data[i], n, pfd->btrp, fn->btrn);
written += n;
}
+ p += written;
return 0;
}
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 */
+ 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;
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 1;
}
-static ssize_t fecdec(char *buf, size_t len, struct filter_node *fn)
+static void fecdec_close(struct filter_node *fn)
+{
+ struct private_fecdec_data *pfd = fn->private_data;
+ struct fecdec_group *fg;
+
+ FOR_EACH_FECDEC_GROUP(fg, pfd)
+ clear_group(fg);
+ fec_free(pfd->fec);
+ btr_pool_free(pfd->btrp);
+ free(fn->private_data);
+ fn->private_data = 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)
- return ret;
+ goto out;
+ ret = -E_BAD_SLICE_SIZE;
if (!h.slice_bytes)
- return -E_BAD_SLICE_SIZE;
+ goto out;
+ ret = -E_BAD_SLICE_NUM;
if (h.slice_num > h.slices_per_group)
- return -E_BAD_SLICE_NUM;
+ 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)
- return ret;
- return FEC_HEADER_SIZE + h.slice_bytes;
-}
-
-static void fecdec_close(struct filter_node *fn)
-{
- struct private_fecdec_data *pfd = fn->private_data;
- struct fecdec_group *fg;
-
- FOR_EACH_FECDEC_GROUP(fg, pfd)
- clear_group(fg);
- free(fn->buf);
- fn->buf = NULL;
- fec_free(pfd->fec);
- free(fn->private_data);
- fn->private_data = NULL;
+ 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)
{
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;
}
/**
* 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;
}