Consolidate decoder code by introducing prepare_filter_node().
authorAndre Noll <maan@systemlinux.org>
Sun, 3 Jan 2010 02:00:46 +0000 (03:00 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 3 Jan 2010 02:00:46 +0000 (03:00 +0100)
aacdec_filter.c
buffer_tree.c
buffer_tree.h
filter.h
filter_common.c
mp3dec_filter.c
oggdec_filter.c
wmadec_filter.c

index 0b520da..f41fe8b 100644 (file)
@@ -235,29 +235,13 @@ static void aacdec_post_select(__a_unused struct sched *s, struct task *t)
 
 next_buffer:
        t->error = 0;
-       iqs = btr_get_input_queue_size(btrn);
-       if (iqs < 2048) {
-               if (!btr_no_parent(btrn)) /* wait for more data */
-                       return;
-       }
-       if (btr_bytes_pending(btrn) > AACDEC_MAX_PENDING)
-               return;
-       /* avoid "buffer too small" errors from the decoder */
-       for (;;) {
-               len = btr_next_buffer(btrn, (char **)&inbuf);
-               if (len >= 2048)
-                       break;
-               if (btr_merge(btrn) < 2) /* only one buffer left */
-                       break;
-       }
-       //PARA_CRIT_LOG("next buffer: %zu\n", len);
-       if (len == 0) {
-               if (btr_no_parent(btrn)) {
-                       ret = -E_AACDEC_EOF;
-                       goto err;
-               }
+       ret = prepare_filter_node(btrn, 2048);
+       if (ret < 0)
+               goto err;
+       if (ret == 0)
                return;
-       }
+       len = btr_next_buffer(btrn, (char **)&inbuf);
+       iqs = btr_get_input_queue_size(btrn);
        if (!padd->initialized) {
                unsigned long rate = 0;
                unsigned char channels = 0;
index 2c02c69..7e0ec27 100644 (file)
@@ -363,3 +363,11 @@ void btr_merge_to(struct btr_node *btrn, size_t dest_size)
                        return;
        }
 }
+
+bool btr_eof(struct btr_node *btrn)
+{
+       char *buf;
+       size_t len = btr_next_buffer(btrn, &buf);
+
+       return (len == 0 && btr_no_parent(btrn));
+}
index 4d3502d..094bfea 100644 (file)
@@ -21,3 +21,4 @@ void btr_pushdown(struct btr_node *btrn);
 void *btr_context(struct btr_node *btrn);
 int btr_merge(struct btr_node *btrn);
 void btr_merge_to(struct btr_node *btrn, size_t dest_size);
+bool btr_eof(struct btr_node *btrn);
index ab187df..ec8631f 100644 (file)
--- a/filter.h
+++ b/filter.h
@@ -204,6 +204,7 @@ void filter_init(void);
 int check_filter_arg(char *filter_arg, void **conf);
 void filter_post_select(__a_unused struct sched *s, struct task *t);
 void print_filter_helps(int detailed);
+int prepare_filter_node(struct btr_node *btrn, size_t min_len);
 
 static inline void write_int16_host_endian(char *buf, int val)
 {
index 94e553b..bc5539d 100644 (file)
@@ -275,3 +275,23 @@ void print_filter_helps(int detailed)
        }
 
 }
+
+/** 640K ought to be enough for everybody ;) */
+#define FILTER_MAX_PENDING (640 * 1024)
+
+int prepare_filter_node(struct btr_node *btrn, size_t min_len)
+{
+       size_t iqs;
+
+       if (btr_eof(btrn))
+               return -E_FC_EOF;
+       if (btr_bytes_pending(btrn) > FILTER_MAX_PENDING)
+               return 0;
+       iqs = btr_get_input_queue_size(btrn);
+       if (iqs < min_len && !btr_no_parent(btrn))
+               return 0;
+       assert(iqs != 0);
+       /* avoid "buffer too small" errors from the decoder */
+       btr_merge_to(btrn, min_len);
+       return 1;
+}
index 9eed639..dfec0c4 100644 (file)
@@ -207,23 +207,21 @@ static void mp3dec_post_select(__a_unused struct sched *s, struct task *t)
        int i, ret;
        struct private_mp3dec_data *pmd = fn->private_data;
        struct btr_node *btrn = fn->btrn;
-       size_t loaded, used, len = btr_get_input_queue_size(btrn);
+       size_t loaded, used, len, iqs;
        char *inbuffer, *outbuffer;
 
+next_buffer:
        pmd->stream.error = 0;
        t->error = 0;
-       if (btr_bytes_pending(btrn) > MP3DEC_MAX_PENDING)
-               return;
-       if (need_bad_data_delay(pmd, len))
+       iqs = btr_get_input_queue_size(btrn);
+       if (need_bad_data_delay(pmd, iqs))
                return;
-       if (len <= pmd->input_len_barrier && btr_no_parent(btrn)) {
-               ret = -E_MP3DEC_EOF;
+       ret = prepare_filter_node(btrn, pmd->input_len_barrier);
+       if (ret < 0)
                goto err;
-       }
-next_buffer:
-       len = btr_next_buffer(btrn, &inbuffer);
-       if (len == 0)
+       if (ret == 0)
                return;
+       len = btr_next_buffer(btrn, &inbuffer);
        mad_stream_buffer(&pmd->stream, (unsigned char *)inbuffer, len);
 next_frame:
        ret = mad_header_decode(&pmd->frame.header, &pmd->stream);
@@ -231,10 +229,12 @@ next_frame:
                used = used_mad_buffer_bytes(&pmd->stream, len);
                btr_consume(btrn, used);
                if (pmd->stream.error == MAD_ERROR_BUFLEN) {
-                       pmd->input_len_barrier = len - used;
-                       ret = btr_merge(btrn);
-                       if (ret != 2)
-                               return;
+                       if (len == iqs && btr_no_parent(btrn)) {
+                               ret = -E_MP3DEC_EOF;
+                               goto err;
+                       }
+                       pmd->input_len_barrier += 100;
+                       goto next_buffer;
                } else if (pmd->stream.error != MAD_ERROR_LOSTSYNC)
                        PARA_DEBUG_LOG("header decode: %s\n",
                                mad_stream_errorstr(&pmd->stream));
index fc03077..f968bf1 100644 (file)
@@ -43,6 +43,7 @@ struct private_oggdec_data {
        unsigned int channels;
        /** Current sample rate in Hz. */
        unsigned int samplerate;
+       size_t min_iqs;
 };
 
 static size_t cb_read_nobtr(void *buf, size_t size, size_t nmemb, void *datasource)
@@ -202,12 +203,19 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
        struct filter_node *fn = container_of(t, struct filter_node, task);
        struct private_oggdec_data *pod = fn->private_data;
        struct btr_node *btrn = fn->btrn;
-       size_t iqs = btr_get_input_queue_size(btrn);
+       size_t iqs, len;
        int ret;
+       char *in;
 
        t->error = 0;
-       if (!pod->vf && iqs) {
-               struct oggdec_filter_args_info *conf = fn->conf;
+       ret = prepare_filter_node(btrn, pod->min_iqs);
+       if (ret < 0)
+               goto err;
+       if (ret == 0)
+               return;
+       len = btr_next_buffer(btrn, &in);
+       iqs = btr_get_input_queue_size(btrn);
+       if (!pod->vf) {
                int oret;
 
                pod->vf = para_malloc(sizeof(struct OggVorbis_File));
@@ -218,10 +226,10 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
                        ovc); /* the ov_open_callbacks */
                if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
                        /* this might be due to the input buffer being too small */
-                       int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
-                       if (iqs < ib) {
+                       if (!btr_no_parent(btrn)) {
                                free(pod->vf);
                                pod->vf = NULL;
+                               pod->min_iqs = iqs + 1;
                                return;
                        }
                        ret = (oret == OV_ENOTVORBIS)?
index 4bbc241..7681a76 100644 (file)
@@ -1249,25 +1249,21 @@ static void wmadec_post_select(__a_unused struct sched *s, struct task *t)
        int ret, converted = 0;
        struct private_wmadec_data *pwd = fn->private_data;
        struct btr_node *btrn = fn->btrn;
-       size_t iqs, len, min_len;
+       size_t len, min_len;
        char *in;
 
        min_len = pwd? WMA_FRAME_SKIP + pwd->ahi.block_align : 8192;
 next_buffer:
        t->error = 0;
-       if (btr_bytes_pending(btrn) > WMADEC_MAX_PENDING)
+       ret = prepare_filter_node(btrn, min_len);
+       if (ret < 0)
+               goto err;
+       if (ret == 0)
                return;
-       iqs = btr_get_input_queue_size(btrn);
-       if (iqs < min_len) {
-               if (!btr_no_parent(btrn)) /* wait for more data */
-                       return;
-               ret = -E_WMADEC_EOF;
+       len = btr_next_buffer(btrn, (char **)&in);
+       ret = -E_WMADEC_EOF;
+       if (len < min_len)
                goto err;
-       }
-       /* avoid "buffer too small" errors from the decoder */
-       btr_merge_to(btrn, min_len);
-       len = btr_next_buffer(btrn, &in);
-       assert(len >= min_len);
        if (!pwd) {
                ret = wma_decode_init(in, len, &pwd);
                if (ret < 0)