Merge branch 'master' into next
[paraslash.git] / filter.h
index 45ade8d3697f86001c900ea3c7fe6541a44d48f1..0bb51f5ae1e45bcd3396a91bfbba367cbafdec9c 100644 (file)
--- a/filter.h
+++ b/filter.h
 /*
- * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file filter.h filter-related structures and exported symbols from filter_chain.c */
+/** \file filter.h Filter-related structures and exported symbols from filter_common.c. */
+
+/** The list of supported filters. */
+enum filter_enum {FILTER_ENUM};
 
 /**
- * describes one running instance of a chain of filters
- *
- */
-struct filter_chain {
+ * Describes one running instance of a filter.
+*/
+struct filter_node {
+       /** The number in the array of available filters. */
+       unsigned filter_num;
+       /** The filter chain this filter node belongs to. */
+       struct filter_chain *fc;
        /**
-        *
-        *
-        * the number of channels of the current stream
-        *
-        * Set by the decoding filter
+        * Each filter may store any filter-specific information about the particular
+        * instance of the filter here.
         */
-               unsigned int channels;
+       void *private_data;
+       /** The output buffer. */
+       char *buf;
+       /** The size of the output buffer. */
+       size_t bufsize;
+       /** The number of bytes currently loaded in \a buf. */
+       size_t loaded;
+       /** The list of registered callbacks. */
+       struct list_head callbacks;
+       /** A pointer to the configuration of this instance. */
+       void *conf;
+};
+
+/** Describes one running instance of a chain of filters */
+struct filter_chain {
+       /** The length of the filter chain. */
+       unsigned int num_filters;
        /**
+        * The number of channels of the current stream.
         *
-        *
-        * current samplerate in Hz
-        *
-        * Set by the decoding filter
+        * Set by the decoding filter.
         */
-               unsigned int samplerate;
+       unsigned int channels;
        /**
+        * Current sample rate in Hz.
         *
-        *
-        * the list containing all filter nodes in this filter chain
+        * Set by the decoding filter.
         */
-               struct list_head filters;
+       unsigned int samplerate;
+       /** The list containing all filter nodes in this filter chain. */
+       struct filter_node *filter_nodes;
        /**
-        *
-        *
-        * the input buffer of the filter chain
+        * The input buffer of the filter chain.
         *
         * This is set to point to the output buffer of the receiving application (the
         * buffer used to read from stdin for para_filter; the output buffer of the
-        * current receiver for para_audiod)
-        */
-               char *inbuf;
-       /**
-        *
-        *
-        * the output buffer of the filter chain
-        *
-        * Points to the output buffer of the last filter in the filter chain
-       **/
-               char *outbuf;
-       /**
-        *
-        *
-        * pointer to variable containing the number of bytes loaded in the input buffer
+        * current receiver for para_audiod).
         */
-               size_t *in_loaded;
+       char **inbufp;
        /**
+        * The output buffer of the filter chain.
         *
-        *
-        * pointer to variable containing the number of bytes loaded in the output buffer
+        * Points to the output buffer of the last filter in the filter chain.
         */
-               size_t *out_loaded;
-       /** non-zero if this filter wont' produce any more output */
-       int eof;
-       /** pointer to the eof flag of the receiving application */
-       int *input_eof;
-       /** pointer to the eof flag of the writing application */
-       int *output_eof;
-       /** the task associated with the filter chain */
+       char **outbufp;
+       /** Contains the number of bytes loaded in the input buffer. */
+       size_t *in_loaded;
+       /** Contains the number of bytes loaded in the output buffer. */
+       size_t *out_loaded;
+       /** Pointer to the error variable of the receiving application. */
+       int *input_error;
+       /** Pointer to the error variable of the writing application. */
+       int *output_error;
+       /** The task associated with the filter chain. */
        struct task task;
 };
 
-/**
- * describes one running instance of a filter
-*/
-struct filter_node {
-/**
- *
- *
- * a pointer to the corresponding filter struct
- */
-       struct filter *filter;
-/**
- *
- *
- * the filter chain this filter node belongs to
- */
-       struct filter_chain *fc;
-/**
- *
- *
- * the position of the filter in the corresponding filter chain
- *
- * all filters that make up the filter chains are organized in a doubly
- * linked list.
- */
-       struct list_head node;
-/**
- *
- *
- * each filter may store any filter-specific information about the particular
- * instance of the filter here.
- */
-       void *private_data;
-/**
- *
- *
- * the output buffer
- */
-       char *buf;
-/**
- * the size of the output buffer
- */
-       size_t bufsize;
-/**
- *
- *
- * the number of bytes currently loaded in \a buf
- */
-       size_t loaded;
-/**
- *
- *
- * the list of registered callbacks
- */
-       struct list_head callbacks;
-/**
- *
- * a pointer to the configuration of this instance
- */
-       void *conf;
-};
+#define FOR_EACH_FILTER_NODE(fn, fc, i) for (i = 0; i < (fc)->num_filters \
+       && (fn = (fc)->filter_nodes + i); i++)
+
 
 /**
- * used to manage grab clients
+ * Used to manage grab clients.
  *
  * An application using paraslash's filter subsystem may register any number of
  * callbacks for each filter_node. It is possible to attach a filter callback
@@ -157,66 +93,47 @@ struct filter_node {
  * the grab command.
  */
 struct filter_callback {
-/**
- *
- *
- * all callbacks are organized in a doubly linked list
- */
+       /** All callbacks are organized in a doubly linked list. */
        struct list_head node;
-/**
- *
- *
- * private data
- *
- * May be initialized by the application before registering the callback. This
- * pointer is not used by the filter subsystem. It is provided for use within
- * the input/ouput/close callback functions.
- */
+       /**
+        * Private data.
+        *
+        * May be initialized by the application before registering the callback. This
+        * pointer is not used by the filter subsystem. It is provided for use within
+        * the input/output/close callback functions.
+        */
        void *data;
-/**
- *
- *
- * the input callback
- *
- * In not \p NULL, the filter subsystem calls this function whenever the filter
- * consumed some or all of its input buffer. A pointer to the buffer of consumed
- * data, its length and a pointer to the own \a filter_callback structure are passed
- * to \a input_cb. The input callback is expected to return a negative value on errors.
- */
+       /**
+        * The input callback.
+        *
+        * In not \p NULL, the filter subsystem calls this function whenever the filter
+        * consumed some or all of its input buffer. A pointer to the buffer of consumed
+        * data, its length and a pointer to the own \a filter_callback structure are passed
+        * to \a input_cb. The input callback is expected to return a negative value on errors.
+        */
        int (*input_cb)(char *buf, size_t len, struct filter_callback *fc);
-/**
- *
- *
- * the output callback
- *
- * If not NULL, this is called whenever the filter produces output. A pointer
- * to the output data, its length and a pointer to the own \a filter_callback
- * structure are passed to \a output_cb. Like the input callback, the output
- * callback is expected to return a negative value on errors.
- */
+       /**
+        * The output callback.
+        *
+        * If not NULL, this is called whenever the filter produces output. A pointer
+        * to the output data, its length and a pointer to the own \a filter_callback
+        * structure are passed to \a output_cb. Like the input callback, the output
+        * callback is expected to return a negative value on errors.
+        */
        int (*output_cb)(char *buf, size_t len, struct filter_callback *fc);
-/**
- *
- *
- * the callback close function
- *
- * This gets called whenever the input/ouput callback returned an error, or if
- * the filter chain is going to be destroyed, e.g. because the end of the
- * stream was encounterd. It is assumed to succeed.
- */
+       /**
+        * The callback close function.
+        *
+        * This gets called whenever the input/output callback returned an error, or if
+        * the filter chain is going to be destroyed, e.g. because the end of the
+        * stream was encountered. It is assumed to succeed.
+        */
        void (*close)(struct filter_callback *fc);
 };
 
 
-void close_filters(struct filter_chain *fc);
-int filter_io(struct filter_chain *fc);
-void filter_init(struct filter *all_filters);
-int check_filter_arg(char *filter_arg, void **conf);
-int del_filter_callback(struct filter_callback *fcb);
-void filter_pre_select(struct sched *s, struct task *t);
-
 /**
- * the structure associated with a paraslash filter
+ * The structure associated with a paraslash filter.
  *
  * Paraslash filters are "modules" which are used to transform an audio stream.
  * struct filter contains pointers to functions that must be supplied by the
@@ -226,129 +143,84 @@ void filter_pre_select(struct sched *s, struct task *t);
  * Note: As several instances of the same filter may be running at the same
  * time, all these filter functions must be reentrant; no static non-constant
  * variables may be used.
- * \sa mp3dec.c, oggdec.c, wav.c, compress.c, filter_node
+ * \sa mp3dec_filter.c, oggdec_filter.c, wav_filter.c, compress_filter.c, filter_node
  */
 struct filter {
-/**
- *
- *
- * the name of the filter
- */
-const char *name;
-/**
- *
- *
- * pointer to the filter init routine
- *
- * This function is only called once at startup. It must initialize the
- * other non-optional function pointers of \a f.
- */
-void (*init)(struct filter *f);
-/**
- *
- *
- * open one instance of this filter
- *
- * This should allocate the output buffer of the given filter node and do any
- * other filter-specific preparations like initializing the private_data member
- * of \a fn suitably. The open function is assumed to succeed.
- */
-void (*open)(struct filter_node *fn);
-/**
- *
- *
- * convert (filter) the given data
- *
- * Pointer to the converting function of the filter. It should convert the
- * given input buffer \a inbuf which is of length \a len to the previoulsy
- * reserved output buffer of \a fn. On success, it must return the number of
- * bytes it consumed from \a inbuf. On errors, a negative number indicating the
- * kind of the error must be returned.
- *
- * A zero return value just means that nothing was converted (probably because
- * the input buffer was too small). This is not interpreted as an error.
- */
-ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
-/**
- *
- *
- * close one instance of this filter
- *
- * Free all resources of associated with \a fn that were previously allocated
- * by the open() function.
- */
-void (*close)(struct filter_node *fn);
-/**
- *
- *
- * print the help text for this filter and exit
- *
- * This is optional and it is not necessary to initialize this pointer if
- * the filter does not have a help text.
- */
-void (*print_help)(void);
-/**
- *
- *
- * a pointer to the filter's command line parser
- *
- * If this optional function pointer is not NULL, any filter options are passed
- * from the main propgram to this command line parser once at application
- * startup. The command line parser should check its command line options given
- * by \a argc and \a argv and abort on errors. On success, it should return a
- * pointer to the filter-specific configuration data determined by \a argc and
- * \a argv.
- */
-void *(*parse_config)(int argc, char **argv);
-};
-/** \cond */
-extern struct filter filters[];
-#define DECLARE_EXTERN_FILTER_INIT(name) \
-       extern void name ## _init(struct filter *f)
+       /** The name of the filter. */
+       const char *name;
+       /**
+        * Pointer to the filter init routine.
+        *
+        * This function is only called once at startup. It must initialize the
+        * other non-optional function pointers of this structure.
+        */
+       void (*init)(struct filter *f);
+       /**
+        * Open one instance of this filter.
+        *
+        * This should allocate the output buffer of the given filter node and do any
+        * other filter-specific preparations like initializing the private_data member
+        * of \a fn suitably. The open function is assumed to succeed.
+        */
+       void (*open)(struct filter_node *fn);
+       /**
+        * Convert (filter) the given data.
+        *
+        * Pointer to the converting function of the filter. It should convert the
+        * given input buffer \a inbuf which is of length \a len to the previously
+        * reserved output buffer of \a fn. On success, it must return the number of
+        * bytes it consumed from \a inbuf. On errors, a negative number indicating the
+        * kind of the error must be returned.
+        *
+        * A zero return value just means that nothing was converted (probably because
+        * the input buffer was too small). This is not interpreted as an error.
+        */
+       ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
+       /**
+        * Close one instance of this filter.
+        *
+        * Free all resources of associated with \a fn that were previously allocated
+        * by the open() function.
+        */
+       void (*close)(struct filter_node *fn);
+       /**
+        * A pointer to the filter's command line parser.
+        *
+        * If this optional function pointer is not NULL, any filter options
+        * are passed from the main program to this command line parser once at
+        * application startup. The command line parser should check its
+        * command line options given by \a argc and \a argv and abort on
+        * errors. Success must be indicated by a non-negative return value. In
+        * this case the function should return a pointer to the
+        * filter-specific configuration data determined by \a argc and \a
+        * argv. On failure, a negative paraslash error code must be returned.
+        */
+       int (*parse_config)(int argc, char **argv, void **config);
 
-#define FILTER_INIT(filter) { \
-       .name = #filter, \
-       .init = filter ## _init, \
-       .parse_config = NULL, \
-       .print_help = NULL \
-},
+       /** The help texts for this filter. */
+       struct ggo_help help;
+};
 
-/* filters that are always present */
-DECLARE_EXTERN_FILTER_INIT(wav);
-/* wav is always the first filter */
-#define WAV_FILTER_NUM 0
-DECLARE_EXTERN_FILTER_INIT(compress);
+void close_filters(struct filter_chain *fc);
+void filter_init(void);
+int check_filter_arg(char *filter_arg, void **conf);
+void filter_pre_select(__a_unused struct sched *s, struct task *t);
+void print_filter_helps(int detailed);
 
-/* next the optional filters */
-#ifdef HAVE_MAD
-DECLARE_EXTERN_FILTER_INIT(mp3dec);
-#define MP3DEC_FILTER FILTER_INIT(mp3dec)
+static inline void write_int16_host_endian(char *buf, int val)
+{
+#ifdef WORDS_BIGENDIAN
+       *buf = val >> 8;
+       *(buf + 1) = val & 0xff;
 #else
-#define MP3DEC_FILTER
+       *buf = val & 0xff;
+       *(buf + 1) = val >> 8;
 #endif
+}
 
-#ifdef HAVE_FAAD
-DECLARE_EXTERN_FILTER_INIT(aacdec);
-#define AACDEC_FILTER FILTER_INIT(aacdec)
-#else
-#define AACDEC_FILTER
-#endif
+DECLARE_FILTER_INITS
 
-#ifdef HAVE_OGGVORBIS
-DECLARE_EXTERN_FILTER_INIT(oggdec);
-#define OGGDEC_FILTER FILTER_INIT(oggdec)
-#else
-#define OGGDEC_FILTER
-#endif
+#define FOR_EACH_SUPPORTED_FILTER(j)  for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
 
-/*
- * a macro that defines an array of all available filters
- */
-#define DEFINE_FILTER_ARRAY(fa) struct filter fa[] = { \
-       FILTER_INIT(wav) \
-       FILTER_INIT(compress) \
-       MP3DEC_FILTER \
-       AACDEC_FILTER \
-       OGGDEC_FILTER \
-       { .name = NULL } };
-/** \endcond */
+/** The filter array, one structure for each supported filter. */
+extern struct filter filters[NUM_SUPPORTED_FILTERS];