Add btr support to the oggdec filter.
[paraslash.git] / oggdec_filter.c
index 8d1abb99b4f9cf47e329fa74c3186b7e47030de8..fc03077bf4fc65505affd7db301f0b3dae4fd67c 100644 (file)
@@ -6,18 +6,20 @@
 
 /** \file oggdec_filter.c Paraslash's ogg vorbis decoder. */
 
-#include "para.h"
+#include <regex.h>
+#include <vorbis/vorbisfile.h>
+#include <stdbool.h>
 
+#include "para.h"
 #include "oggdec_filter.cmdline.h"
 #include "list.h"
 #include "sched.h"
 #include "ggo.h"
+#include "buffer_tree.h"
 #include "filter.h"
 #include "error.h"
 #include "string.h"
 
-#include <vorbis/vorbisfile.h>
-
 /** Determine byte sex. */
 #ifdef WORDS_BIGENDIAN
 #define ENDIAN 1
@@ -35,9 +37,15 @@ struct private_oggdec_data {
        size_t inbuf_len;
        /** The number of bytes consumed from the input buffer. */
        size_t converted;
+       /** When to start producing output. */
+       struct timeval stream_start;
+       /** The number of channels of the current stream. */
+       unsigned int channels;
+       /** Current sample rate in Hz. */
+       unsigned int samplerate;
 };
 
-static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
+static size_t cb_read_nobtr(void *buf, size_t size, size_t nmemb, void *datasource)
 {
        struct filter_node *fn = datasource;
        struct private_oggdec_data *pod = fn->private_data;
@@ -58,6 +66,44 @@ static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
        return ret;
 }
 
+static size_t cb_read_btr(void *buf, size_t size, size_t nmemb, void *datasource)
+{
+       struct filter_node *fn = datasource;
+       struct btr_node *btrn = fn->btrn;
+       size_t copied;
+
+       /**
+        * oggvorbis always uses size == 1. Other sizes would complicate the code
+        * for no real gain. So we simply don't support size != 1.
+        */
+       assert(size == 1);
+       //PARA_DEBUG_LOG("vorbis requests %zu x %zu = %zu bytes\n", size, nmemb, size * nmemb);
+       copied = 0;
+       for (;;) {
+               char *btr_buf;
+               size_t nbytes = btr_next_buffer(btrn, &btr_buf);
+               if (nbytes == 0)
+                       break;
+               nbytes = PARA_MIN(nmemb - copied, nbytes);
+               memcpy(buf + copied, btr_buf, nbytes);
+               copied += nbytes;
+               btr_consume(btrn, nbytes);
+               if (copied == nmemb)
+                       break;
+       }
+       return copied;
+}
+
+static size_t cb_read(void *buf, size_t size, size_t nmemb, void *datasource)
+{
+       struct filter_node *fn = datasource;
+
+       if (fn->btrn)
+               return cb_read_btr(buf, size, nmemb, datasource);
+       else
+               return cb_read_nobtr(buf, size, nmemb, datasource);
+}
+
 /*
  * Custom data seeking function.
  *
@@ -113,6 +159,120 @@ static void ogg_close(struct filter_node *fn)
        fn->private_data = NULL;
 }
 
+#define OGGDEC_MAX_PENDING (640 * 1024)
+#define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
+
+
+static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
+{
+       struct filter_node *fn = btr_context(btrn);
+       struct private_oggdec_data *pod = fn->private_data;
+
+       if (!strcmp(cmd, "samplerate")) {
+               if (pod->samplerate == 0)
+                       return -ERRNO_TO_PARA_ERROR(ENAVAIL);
+               *result = make_message("%u", pod->samplerate);
+               return 1;
+       }
+       if (!strcmp(cmd, "channels")) {
+               if (pod->channels == 0)
+                       return -ERRNO_TO_PARA_ERROR(ENAVAIL);
+               *result = make_message("%u", pod->channels);
+               return 1;
+       }
+       return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+}
+
+static void ogg_pre_select(struct sched *s, struct task *t)
+{
+       struct filter_node *fn = container_of(t, struct filter_node, task);
+       size_t iqs = btr_get_input_queue_size(fn->btrn);
+
+       t->error = 0;
+       if (iqs == 0)
+               return;
+       if (btr_bytes_pending(fn->btrn) > OGGDEC_MAX_PENDING)
+               return; /* FIXME, should use reasonable bound on timeout */
+       s->timeout.tv_sec = 0;
+       s->timeout.tv_usec = 1;
+}
+
+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);
+       int ret;
+
+       t->error = 0;
+       if (!pod->vf && iqs) {
+               struct oggdec_filter_args_info *conf = fn->conf;
+               int oret;
+
+               pod->vf = para_malloc(sizeof(struct OggVorbis_File));
+               PARA_NOTICE_LOG("input queue: %zu, opening ov callbacks\n", iqs);
+               oret = ov_open_callbacks(fn, pod->vf,
+                       NULL, /* no initial buffer */
+                       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 */
+                       int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
+                       if (iqs < ib) {
+                               free(pod->vf);
+                               pod->vf = NULL;
+                               return;
+                       }
+                       ret = (oret == OV_ENOTVORBIS)?
+                               -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
+                       goto err;
+               }
+               ret = -E_OGGDEC_READ;
+               if (oret == OV_EREAD)
+                       goto err;
+               ret = -E_OGGDEC_VERSION;
+               if (oret == OV_EVERSION)
+                       goto err;
+               ret = -E_OGGDEC_FAULT;
+               if (oret < 0)
+                       goto err;
+               pod->channels = ov_info(pod->vf, 0)->channels;
+               pod->samplerate = ov_info(pod->vf, 0)->rate;
+               PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
+                       pod->samplerate);
+               ///* wait a bit to avoid buffer underruns */
+               //tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
+               return;
+       }
+       for (;;) {
+               char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
+               ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
+                       ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
+               if (read_ret <= 0)
+                       free(out);
+               if (read_ret == 0) {
+                       ret = -E_OGGDEC_EOF;
+                       if (btr_no_parent(btrn))
+                               goto err;
+                       return;
+               }
+               if (read_ret == OV_HOLE)
+                       return;
+               if (read_ret < 0) {
+                       ret = -E_OGGDEC_BADLINK;
+                       goto err;
+               }
+               btr_add_output(out, read_ret, btrn);
+       }
+
+err:
+       assert(ret < 0);
+       ogg_close(fn);
+       t->error = ret;
+       btr_del_node(btrn);
+}
+
 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
 {
        ssize_t ret;
@@ -124,39 +284,60 @@ static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
        pod->converted = 0;
 
        if (!pod->vf) {
-               int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
-               if (len <ib && !*fn->fc->input_error) {
-                       PARA_DEBUG_LOG("initial input buffer %zd/%d, "
-                               "waiting for more data\n", len, ib);
+               if (*fn->fc->input_error < 0)
+                       return *fn->fc->input_error;
+               if (!len)
                        return 0;
-               }
                pod->vf = para_malloc(sizeof(struct OggVorbis_File));
                PARA_NOTICE_LOG("input buffer: %zd, opening ov callbacks\n", len);
                ret = ov_open_callbacks(fn, pod->vf,
                        NULL, /* no initial buffer */
                        0, /* no initial bytes */
                        ovc); /* the ov_open_callbacks */
+               if (ret == OV_ENOTVORBIS || ret == OV_EBADHEADER) {
+                       /* this might be due to the input buffer being too small */
+                       int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
+                       if (len < ib) {
+                               PARA_INFO_LOG("initial input buffer %zd/%d, "
+                                       "waiting for more data\n", len, ib);
+                               free(pod->vf);
+                               pod->vf = NULL;
+                               return 0;
+                       }
+                       return ret == OV_ENOTVORBIS?
+                               -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
+               }
                if (ret == OV_EREAD)
                        return -E_OGGDEC_READ;
-               if (ret == OV_ENOTVORBIS)
-                       return -E_OGGDEC_NOTVORBIS;
                if (ret == OV_EVERSION)
                        return -E_OGGDEC_VERSION;
-               if (ret == OV_EBADHEADER)
-                       return -E_OGGDEC_BADHEADER;
                if (ret < 0)
                        return -E_OGGDEC_FAULT;
                fn->fc->channels = ov_info(pod->vf, 0)->channels;
                fn->fc->samplerate = ov_info(pod->vf, 0)->rate;
                PARA_NOTICE_LOG("%d channels, %d Hz\n", fn->fc->channels,
                        fn->fc->samplerate);
+               /* wait a bit to avoid buffer underruns */
+               tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
+               return pod->converted;
+       }
+       if (tv_diff(now, &pod->stream_start, NULL) < 0) {
+               PARA_DEBUG_LOG("initial delay..\n");
+               return 0;
        }
        while (fn->loaded < fn->bufsize) {
                int length = fn->bufsize - fn->loaded;
                long read_ret = ov_read(pod->vf, fn->buf + fn->loaded, length,
                        ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
-               if (read_ret == OV_HOLE || !read_ret)
+               if (read_ret == 0)
+                       return pod->converted;
+               if (read_ret == OV_HOLE) {
+                       if (!fn->loaded) {
+                               PARA_INFO_LOG("hole, delaying playback\n");
+                               tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
+                       }
                        return pod->converted;
+               }
                if (read_ret < 0)
                        return -E_OGGDEC_BADLINK;
                fn->loaded += read_ret;
@@ -202,7 +383,10 @@ void oggdec_filter_init(struct filter *f)
        f->open = ogg_open;
        f->close = ogg_close;
        f->convert = ogg_convert;
+       f->pre_select = ogg_pre_select;
+       f->post_select = ogg_post_select;
        f->parse_config = oggdec_parse_config;
+       f->execute = oggdec_execute;
        f->help = (struct ggo_help) {
                .short_help = oggdec_filter_args_info_help,
                .detailed_help = oggdec_filter_args_info_detailed_help