2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter.h Filter-related structures and exported symbols from filter_chain.c. */
9 /** Describes one running instance of a chain of filters */
12 * The number of channels of the current stream.
14 * Set by the decoding filter.
16 unsigned int channels;
18 * Current sample rate in Hz.
20 * Set by the decoding filter.
22 unsigned int samplerate;
23 /** The list containing all filter nodes in this filter chain. */
24 struct list_head filters;
26 * The input buffer of the filter chain.
28 * This is set to point to the output buffer of the receiving application (the
29 * buffer used to read from stdin for para_filter; the output buffer of the
30 * current receiver for para_audiod).
34 * The output buffer of the filter chain.
36 * Points to the output buffer of the last filter in the filter chain.
39 /** Contains the number of bytes loaded in the input buffer. */
41 /** Contains the number of bytes loaded in the output buffer. */
43 /** Pointer to the error variable of the receiving application. */
45 /** Pointer to the error variable of the writing application. */
47 /** The task associated with the filter chain. */
52 * Describes one running instance of a filter.
55 /** A pointer to the corresponding filter struct. */
56 struct filter *filter;
57 /** The filter chain this filter node belongs to. */
58 struct filter_chain *fc;
60 * The position of the filter in the corresponding filter chain.
62 * All filters that make up the filter chains are organized in a doubly
65 struct list_head node;
67 * Each filter may store any filter-specific information about the particular
68 * instance of the filter here.
71 /** The output buffer. */
73 /** The size of the output buffer. */
75 /** The number of bytes currently loaded in \a buf. */
77 /** The list of registered callbacks. */
78 struct list_head callbacks;
79 /** A pointer to the configuration of this instance. */
84 * Used to manage grab clients.
86 * An application using paraslash's filter subsystem may register any number of
87 * callbacks for each filter_node. It is possible to attach a filter callback
88 * while the filter is running. This is used for stream grabbing in
89 * para_audiod: Whenever a client sends the 'grab' command, para_audiod adds a
90 * filter callback to the list of callbacks for the filter node specified in
93 struct filter_callback {
94 /** All callbacks are organized in a doubly linked list. */
95 struct list_head node;
99 * May be initialized by the application before registering the callback. This
100 * pointer is not used by the filter subsystem. It is provided for use within
101 * the input/output/close callback functions.
105 * The input callback.
107 * In not \p NULL, the filter subsystem calls this function whenever the filter
108 * consumed some or all of its input buffer. A pointer to the buffer of consumed
109 * data, its length and a pointer to the own \a filter_callback structure are passed
110 * to \a input_cb. The input callback is expected to return a negative value on errors.
112 int (*input_cb)(char *buf, size_t len, struct filter_callback *fc);
114 * The output callback.
116 * If not NULL, this is called whenever the filter produces output. A pointer
117 * to the output data, its length and a pointer to the own \a filter_callback
118 * structure are passed to \a output_cb. Like the input callback, the output
119 * callback is expected to return a negative value on errors.
121 int (*output_cb)(char *buf, size_t len, struct filter_callback *fc);
123 * The callback close function.
125 * This gets called whenever the input/output callback returned an error, or if
126 * the filter chain is going to be destroyed, e.g. because the end of the
127 * stream was encountered. It is assumed to succeed.
129 void (*close)(struct filter_callback *fc);
133 void close_filters(struct filter_chain *fc);
134 void filter_init(struct filter *all_filters);
135 int check_filter_arg(char *filter_arg, void **conf);
136 void filter_pre_select(__a_unused struct sched *s, struct task *t);
139 * The structure associated with a paraslash filter.
141 * Paraslash filters are "modules" which are used to transform an audio stream.
142 * struct filter contains pointers to functions that must be supplied by the
143 * filter code in order to be used by the driving application (currently
144 * para_audiod and para_filter).
146 * Note: As several instances of the same filter may be running at the same
147 * time, all these filter functions must be reentrant; no static non-constant
148 * variables may be used.
149 * \sa mp3dec.c, oggdec.c, wav.c, compress.c, filter_node
152 /** The name of the filter. */
155 * Pointer to the filter init routine.
157 * This function is only called once at startup. It must initialize the
158 * other non-optional function pointers of this structure.
160 void (*init)(struct filter *f);
162 * Open one instance of this filter.
164 * This should allocate the output buffer of the given filter node and do any
165 * other filter-specific preparations like initializing the private_data member
166 * of \a fn suitably. The open function is assumed to succeed.
168 void (*open)(struct filter_node *fn);
170 * Convert (filter) the given data.
172 * Pointer to the converting function of the filter. It should convert the
173 * given input buffer \a inbuf which is of length \a len to the previously
174 * reserved output buffer of \a fn. On success, it must return the number of
175 * bytes it consumed from \a inbuf. On errors, a negative number indicating the
176 * kind of the error must be returned.
178 * A zero return value just means that nothing was converted (probably because
179 * the input buffer was too small). This is not interpreted as an error.
181 ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
183 * Close one instance of this filter.
185 * Free all resources of associated with \a fn that were previously allocated
186 * by the open() function.
188 void (*close)(struct filter_node *fn);
190 * Print the help text for this filter and exit.
192 * This is optional and it is not necessary to initialize this pointer if
193 * the filter does not have a help text.
195 void (*print_help)(void);
197 * A pointer to the filter's command line parser.
199 * If this optional function pointer is not NULL, any filter options are passed
200 * from the main program to this command line parser once at application
201 * startup. The command line parser should check its command line options given
202 * by \a argc and \a argv and abort on errors. On success, it should return a
203 * pointer to the filter-specific configuration data determined by \a argc and
206 void *(*parse_config)(int argc, char **argv);
210 static inline void write_int16_host_endian(char *buf, int val)
212 #ifdef WORDS_BIGENDIAN
214 *(buf + 1) = val & 0xff;
217 *(buf + 1) = val >> 8;
223 extern struct filter filters[];
224 #define DECLARE_EXTERN_FILTER_INIT(name) \
225 extern void name ## _init(struct filter *f)
227 #define FILTER_INIT(filter) { \
229 .init = filter ## _init, \
230 .parse_config = NULL, \
234 /* filters that are always present */
235 DECLARE_EXTERN_FILTER_INIT(wav);
236 /* wav is always the first filter */
237 #define WAV_FILTER_NUM 0
238 DECLARE_EXTERN_FILTER_INIT(compress);
240 /* next the optional filters */
242 DECLARE_EXTERN_FILTER_INIT(mp3dec);
243 #define MP3DEC_FILTER FILTER_INIT(mp3dec)
245 #define MP3DEC_FILTER
249 DECLARE_EXTERN_FILTER_INIT(aacdec);
250 #define AACDEC_FILTER FILTER_INIT(aacdec)
252 #define AACDEC_FILTER
255 #ifdef HAVE_OGGVORBIS
256 DECLARE_EXTERN_FILTER_INIT(oggdec);
257 #define OGGDEC_FILTER FILTER_INIT(oggdec)
259 #define OGGDEC_FILTER
263 /** define an array of all available filters */
264 #define DEFINE_FILTER_ARRAY(filters) struct filter filters[] = { \
266 FILTER_INIT(compress) \