sched: Kill old ->post_select variant.
[paraslash.git] / wav_filter.c
index 3fce556d5d1ecb514b0cffeec869103ff0bec178..c2a9329f15e931af76fa10cdcab9913e627b0a24 100644 (file)
@@ -1,77 +1,55 @@
 /*
- * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 /** \file wav_filter.c A filter that inserts a wave header. */
 
-#include "para.h"
+#include <regex.h>
 
+#include "para.h"
+#include "error.h"
 #include "list.h"
 #include "sched.h"
+#include "ggo.h"
+#include "buffer_tree.h"
 #include "filter.h"
 #include "string.h"
 #include "portable_io.h"
 
-/** size of the output buffer */
-#define WAV_OUTBUF_SIZE 81920
-/** a wav header is always 44 bytes */
+/** A wav header is always 44 bytes. */
 #define WAV_HEADER_LEN 44
-/** always write 16 bit header */
+/** Always write 16 bit header. */
 #define BITS 16
 
-static void make_wav_header(unsigned int channels, unsigned int samplerate,
-               struct filter_node *fn)
+static void make_wav_header(unsigned int channels, unsigned int sample_rate,
+               char *headbuf)
 {
 
-       char *headbuf = fn->buf;
        unsigned int size = 0x7fffffff;
-       int bytespersec = channels * samplerate * BITS / 8;
+       int bytespersec = channels * sample_rate * BITS / 8;
        int align = channels * BITS / 8;
 
-       assert(channels);
-       PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
+       PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, sample_rate);
        memset(headbuf, 0, WAV_HEADER_LEN);
        memcpy(headbuf, "RIFF", 4);
        write_u32(headbuf + 4, size - 8);
        memcpy(headbuf + 8, "WAVE", 4);
        memcpy(headbuf + 12, "fmt ", 4);
-       write_u32(headbuf + 16, 16);
-       write_u16(headbuf + 20, 1);     /* format */
+       write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
+       write_u16(headbuf + 20, 1);     /* format (1 == PCM/uncompressed) */
        write_u16(headbuf + 22, channels);
-       write_u32(headbuf + 24, samplerate);
+       write_u32(headbuf + 24, sample_rate);
        write_u32(headbuf + 28, bytespersec);
-       write_u16(headbuf + 32, align);
-       write_u16(headbuf + 34, BITS);
-       memcpy(headbuf + 36, "data", 4);
-       write_u32(headbuf + 40, size - 44);
-}
-
-static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
-{
-       size_t copy;
-       int *bof = fn->private_data;
-
-       if (*bof) {
-               if (!len)
-                       return 0;
-               make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
-               fn->loaded = WAV_HEADER_LEN;
-               *bof = 0;
-//             return 0;
-       }
-       copy = PARA_MIN(len, fn->bufsize - fn->loaded);
-       memmove(fn->buf + fn->loaded, inbuf, copy);
-       fn->loaded += copy;
-//     PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
-       return copy;
+       write_u16(headbuf + 32, align); /* number of bytes per sample slice */
+       write_u16(headbuf + 34, BITS); /* significant bits per sample */
+       memcpy(headbuf + 36, "data", 4); /* chunk ID */
+       write_u32(headbuf + 40, size - 44); /* chunk size */
 }
 
 static void wav_close(struct filter_node *fn)
 {
-       free(fn->buf);
-       fn->buf = NULL;
        free(fn->private_data);
        fn->private_data = NULL;
 }
@@ -80,24 +58,78 @@ static void wav_open(struct filter_node *fn)
 {
        int *bof;
 
-       fn->bufsize = WAV_OUTBUF_SIZE;
-       fn->buf = para_malloc(fn->bufsize);
        fn->private_data = para_malloc(sizeof(int));
        bof = fn->private_data;
-       fn->loaded = 0;
        *bof = 1;
-       PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
-               fn, fn->buf, fn->loaded);
+}
+
+static void wav_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;
+       sched_min_delay(s);
+}
+
+static int wav_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;
+       size_t iqs = btr_get_input_queue_size(btrn);
+       int ret;
+       char *header, *buf;
+       int32_t rate, ch;
+
+       if (iqs == 0) {
+               ret = -E_WAV_EOF;
+               if (btr_no_parent(btrn))
+                       goto err;
+               return 0;
+       }
+       ret = btr_exec_up(btrn, "sample_rate", &buf);
+       if (ret < 0) {
+               ret = -E_WAV_BAD_FC;
+               goto err;
+       }
+       ret = para_atoi32(buf, &rate);
+       free(buf);
+       if (ret < 0)
+               goto err;
+       ret = btr_exec_up(btrn, "channels", &buf);
+       if (ret < 0) {
+               ret = -E_WAV_BAD_FC;
+               goto err;
+       }
+       ret = para_atoi32(buf, &ch);
+       free(buf);
+       if (ret < 0)
+               goto err;
+       header = para_malloc(WAV_HEADER_LEN);
+       make_wav_header(ch, rate, header);
+       btr_add_output(header, WAV_HEADER_LEN, btrn);
+       ret = -E_WAV_SUCCESS;
+err:
+       if (ret == -E_WAV_SUCCESS)
+               btr_splice_out_node(btrn);
+       else {
+               btr_remove_node(&fn->btrn);
+               PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+       }
+       return ret;
 }
 
 /**
- * the init function of the wav filter
+ * The init function of the wav filter.
  *
- * \param f struct to initialize
+ * \param f Structure to initialize.
  */
 void wav_filter_init(struct filter *f)
 {
-       f->convert = wav_convert;
        f->close = wav_close;
        f->open = wav_open;
+       f->pre_select = wav_pre_select;
+       f->new_post_select = wav_post_select;
 }