afh: Move audio_format_name() up.
[paraslash.git] / oggdec_filter.c
index 07e4cec1c1d6ac99fcc482beedf77914d1d157b7..708a27e52b68c1c2add01a3b3fe39f721485e39f 100644 (file)
@@ -1,19 +1,13 @@
-/*
- * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
 
 #include <regex.h>
 #include <vorbis/vorbisfile.h>
-#include <stdbool.h>
 
 #include "para.h"
 #include "list.h"
 #include "sched.h"
-#include "ggo.h"
 #include "buffer_tree.h"
 #include "filter.h"
 #include "error.h"
@@ -137,14 +131,13 @@ open:
                0, /* no initial bytes */
                ovc); /* the ov_open_callbacks */
        if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
-               /* this might be due to the input buffer being too small */
+               /* maybe the input buffer is too small */
                if (!btr_no_parent(btrn)) {
                        fn->min_iqs += 1000;
                        iqs = btr_get_input_queue_size(btrn);
                        ret = 0;
                        if (iqs < fn->min_iqs)
                                goto out;
-                       PARA_CRIT_LOG("iqs: %zu\n", iqs);
                        btr_merge(btrn, fn->min_iqs);
                        pod->converted = 0;
                        goto open;
@@ -164,7 +157,7 @@ open:
                goto out;
        pod->channels = ov_info(vf, 0)->channels;
        pod->sample_rate = ov_info(vf, 0)->rate;
-       PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
+       PARA_NOTICE_LOG("%u channels, %u Hz\n", pod->channels,
                pod->sample_rate);
        ret = 1;
 out:
@@ -180,12 +173,20 @@ out:
        return ret;
 }
 
+/** Suspend decoding if output queue size is larger than that. */
 #define OGGDEC_MAX_OUTPUT_SIZE (96 * 1024)
+
+/**
+  * Allocate chunks of this size and produce at most one chunk of output per
+  * ->post_select() invocation. If the buffer could only be filled partially
+  * due to insufficient input being available, it is shrunk to the real output
+  * size and the resized buffer is fed into the output queue.
+  */
 #define OGGDEC_OUTPUT_CHUNK_SIZE (32 * 1024)
 
-static void ogg_pre_select(struct sched *s, struct task *t)
+static void ogg_pre_select(struct sched *s, void *context)
 {
-       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct filter_node *fn = context;
        struct private_oggdec_data *pod = fn->private_data;
        struct btr_node *btrn = fn->btrn;
        int ret;
@@ -200,19 +201,25 @@ static void ogg_pre_select(struct sched *s, struct task *t)
        sched_min_delay(s);
 }
 
-static void ogg_post_select(__a_unused struct sched *s, struct task *t)
+static int ogg_post_select(__a_unused struct sched *s, void *context)
 {
-       struct filter_node *fn = container_of(t, struct filter_node, task);
+       struct filter_node *fn = context;
        struct private_oggdec_data *pod = fn->private_data;
        struct btr_node *btrn = fn->btrn;
        int ret, have;
        char *buf;
 
        ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
-       if (ret < 0 && ret != -E_BTR_EOF) /* fatal error */
-               goto out;
-       if (ret <= 0 && !pod->have_more) /* nothing to do */
+       if (ret < 0) {
+               if (ret != -E_BTR_EOF) /* fatal error */
+                       goto out;
+               if (fn->min_iqs == 0 && !pod->have_more) /* EOF */
+                       goto out;
+               /* last ov_read() returned OV_HOLE */
+       } else if (ret == 0 && !pod->have_more) /* nothing to do */
                goto out;
+       if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
+               return 0;
        if (!pod->vf) {
                if (ret <= 0)
                        goto out;
@@ -231,40 +238,31 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
                        break;
                fn->min_iqs = 0;
                have += ret;
-               if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
-                       continue;
-               if (btr_get_output_queue_size(btrn) > OGGDEC_MAX_OUTPUT_SIZE)
+               if (have >= OGGDEC_OUTPUT_CHUNK_SIZE)
                        break;
-               btr_add_output(buf, have, btrn);
-               buf = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
-               have = 0;
        }
        pod->have_more = (ret > 0);
-       if (have > 0)
+       if (have > 0) {
+               if (have < OGGDEC_OUTPUT_CHUNK_SIZE)
+                       buf = para_realloc(buf, have);
                btr_add_output(buf, have, btrn);
-       else
+       else
                free(buf);
        if (ret == OV_HOLE) /* avoid buffer underruns */
                fn->min_iqs = 9000;
        if (ret >= 0 || ret == OV_HOLE)
-               return;
+               return 0;
        ret = -E_OGGDEC_BADLINK;
 out:
-       t->error = ret;
        if (ret < 0)
-               btr_remove_node(btrn);
+               btr_remove_node(&fn->btrn);
+       return ret;
 }
 
-/**
- * The init function of the ogg vorbis decoder.
- *
- * \param f Its fields are filled in by the function.
- */
-void oggdec_filter_init(struct filter *f)
-{
-       f->open = ogg_open;
-       f->close = ogg_close;
-       f->pre_select = ogg_pre_select;
-       f->post_select = ogg_post_select;
-       f->execute = oggdec_execute;
-}
+const struct filter lsg_filter_cmd_com_oggdec_user_data = {
+       .open = ogg_open,
+       .close = ogg_close,
+       .pre_select = ogg_pre_select,
+       .post_select = ogg_post_select,
+       .execute = oggdec_execute
+};