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. */
11 * Describes one running instance of a filter.
14 /** The number in the array of available filters. */
16 /** The filter chain this filter node belongs to. */
17 struct filter_chain *fc;
19 * Each filter may store any filter-specific information about the particular
20 * instance of the filter here.
23 /** The output buffer. */
25 /** The size of the output buffer. */
27 /** The number of bytes currently loaded in \a buf. */
29 /** The list of registered callbacks. */
30 struct list_head callbacks;
31 /** A pointer to the configuration of this instance. */
35 /** Describes one running instance of a chain of filters */
37 /** The length of the filter chain. */
38 unsigned int num_filters;
40 * The number of channels of the current stream.
42 * Set by the decoding filter.
44 unsigned int channels;
46 * Current sample rate in Hz.
48 * Set by the decoding filter.
50 unsigned int samplerate;
51 /** The list containing all filter nodes in this filter chain. */
52 struct filter_node *filter_nodes;
54 * The input buffer of the filter chain.
56 * This is set to point to the output buffer of the receiving application (the
57 * buffer used to read from stdin for para_filter; the output buffer of the
58 * current receiver for para_audiod).
62 * The output buffer of the filter chain.
64 * Points to the output buffer of the last filter in the filter chain.
67 /** Contains the number of bytes loaded in the input buffer. */
69 /** Contains the number of bytes loaded in the output buffer. */
71 /** Pointer to the error variable of the receiving application. */
73 /** Pointer to the error variable of the writing application. */
75 /** The task associated with the filter chain. */
79 #define FOR_EACH_FILTER_NODE(fn, fc, i) for (i = 0; i < (fc)->num_filters \
80 && (fn = (fc)->filter_nodes + i); i++)
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);
134 * The structure associated with a paraslash filter.
136 * Paraslash filters are "modules" which are used to transform an audio stream.
137 * struct filter contains pointers to functions that must be supplied by the
138 * filter code in order to be used by the driving application (currently
139 * para_audiod and para_filter).
141 * Note: As several instances of the same filter may be running at the same
142 * time, all these filter functions must be reentrant; no static non-constant
143 * variables may be used.
144 * \sa mp3dec.c, oggdec.c, wav.c, compress.c, filter_node
147 /** The name of the filter. */
150 * Pointer to the filter init routine.
152 * This function is only called once at startup. It must initialize the
153 * other non-optional function pointers of this structure.
155 void (*init)(struct filter *f);
157 * Open one instance of this filter.
159 * This should allocate the output buffer of the given filter node and do any
160 * other filter-specific preparations like initializing the private_data member
161 * of \a fn suitably. The open function is assumed to succeed.
163 void (*open)(struct filter_node *fn);
165 * Convert (filter) the given data.
167 * Pointer to the converting function of the filter. It should convert the
168 * given input buffer \a inbuf which is of length \a len to the previously
169 * reserved output buffer of \a fn. On success, it must return the number of
170 * bytes it consumed from \a inbuf. On errors, a negative number indicating the
171 * kind of the error must be returned.
173 * A zero return value just means that nothing was converted (probably because
174 * the input buffer was too small). This is not interpreted as an error.
176 ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
178 * Close one instance of this filter.
180 * Free all resources of associated with \a fn that were previously allocated
181 * by the open() function.
183 void (*close)(struct filter_node *fn);
185 * Print the help text for this filter and exit.
187 * This is optional and it is not necessary to initialize this pointer if
188 * the filter does not have a help text.
190 void (*print_help)(void);
192 * A pointer to the filter's command line parser.
194 * If this optional function pointer is not NULL, any filter options are passed
195 * from the main program to this command line parser once at application
196 * startup. The command line parser should check its command line options given
197 * by \a argc and \a argv and abort on errors. On success, it should return a
198 * pointer to the filter-specific configuration data determined by \a argc and
201 void *(*parse_config)(int argc, char **argv);
204 void close_filters(struct filter_chain *fc);
205 void filter_init(struct filter *all_filters);
206 int check_filter_arg(char *filter_arg, void **conf);
207 void filter_pre_select(__a_unused struct sched *s, struct task *t);
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 DECLARE_EXTERN_FILTER_INIT(compress);
237 DECLARE_EXTERN_FILTER_INIT(amp);
239 /* next the optional filters */
241 DECLARE_EXTERN_FILTER_INIT(mp3dec);
242 #define MP3DEC_FILTER FILTER_INIT(mp3dec)
244 #define MP3DEC_FILTER
248 DECLARE_EXTERN_FILTER_INIT(aacdec);
249 #define AACDEC_FILTER FILTER_INIT(aacdec)
251 #define AACDEC_FILTER
254 #ifdef HAVE_OGGVORBIS
255 DECLARE_EXTERN_FILTER_INIT(oggdec);
256 #define OGGDEC_FILTER FILTER_INIT(oggdec)
258 #define OGGDEC_FILTER
262 /** define an array of all available filters */
263 #define DEFINE_FILTER_ARRAY(filters) struct filter filters[] = { \
265 FILTER_INIT(compress) \