osl.c for_each_file_in_dir(): Move struct stat to inner loop.
[paraslash.git] / filter.h
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file filter.h filter-related structures and exported symbols from filter_chain.c */
8
9 /**
10  * describes one running instance of a chain of filters
11  *
12  */
13 struct filter_chain {
14         /**
15          *
16          *
17          * the number of channels of the current stream
18          *
19          * Set by the decoding filter
20          */
21                 unsigned int channels;
22         /**
23          *
24          *
25          * current samplerate in Hz
26          *
27          * Set by the decoding filter
28          */
29                 unsigned int samplerate;
30         /**
31          *
32          *
33          * the list containing all filter nodes in this filter chain
34          */
35                 struct list_head filters;
36         /**
37          *
38          *
39          * the input buffer of the filter chain
40          *
41          * This is set to point to the output buffer of the receiving application (the
42          * buffer used to read from stdin for para_filter; the output buffer of the
43          * current receiver for para_audiod)
44          */
45                 char *inbuf;
46         /**
47          *
48          *
49          * the output buffer of the filter chain
50          *
51          * Points to the output buffer of the last filter in the filter chain
52         **/
53                 char *outbuf;
54         /**
55          *
56          *
57          * pointer to variable containing the number of bytes loaded in the input buffer
58          */
59                 size_t *in_loaded;
60         /**
61          *
62          *
63          * pointer to variable containing the number of bytes loaded in the output buffer
64          */
65                 size_t *out_loaded;
66         /** non-zero if this filter wont' produce any more output */
67         int eof;
68         /** pointer to the eof flag of the receiving application */
69         int *input_eof;
70         /** pointer to the eof flag of the writing application */
71         int *output_eof;
72         /** the task associated with the filter chain */
73         struct task task;
74 };
75
76 /**
77  * describes one running instance of a filter
78 */
79 struct filter_node {
80 /**
81  *
82  *
83  * a pointer to the corresponding filter struct
84  */
85         struct filter *filter;
86 /**
87  *
88  *
89  * the filter chain this filter node belongs to
90  */
91         struct filter_chain *fc;
92 /**
93  *
94  *
95  * the position of the filter in the corresponding filter chain
96  *
97  * all filters that make up the filter chains are organized in a doubly
98  * linked list.
99  */
100         struct list_head node;
101 /**
102  *
103  *
104  * each filter may store any filter-specific information about the particular
105  * instance of the filter here.
106  */
107         void *private_data;
108 /**
109  *
110  *
111  * the output buffer
112  */
113         char *buf;
114 /**
115  * the size of the output buffer
116  */
117         size_t bufsize;
118 /**
119  *
120  *
121  * the number of bytes currently loaded in \a buf
122  */
123         size_t loaded;
124 /**
125  *
126  *
127  * the list of registered callbacks
128  */
129         struct list_head callbacks;
130 /**
131  *
132  * a pointer to the configuration of this instance
133  */
134         void *conf;
135 };
136
137 /**
138  * used to manage grab clients
139  *
140  * An application using paraslash's filter subsystem may register any number of
141  * callbacks for each filter_node. It is possible to attach a filter callback
142  * while the filter is running. This is used for stream grabbing in
143  * para_audiod: Whenever a client sends the 'grab' command, para_audiod adds a
144  * filter callback to the list of callbacks for the filter node specified in
145  * the grab command.
146  */
147 struct filter_callback {
148 /**
149  *
150  *
151  * all callbacks are organized in a doubly linked list
152  */
153         struct list_head node;
154 /**
155  *
156  *
157  * private data
158  *
159  * May be initialized by the application before registering the callback. This
160  * pointer is not used by the filter subsystem. It is provided for use within
161  * the input/ouput/close callback functions.
162  */
163         void *data;
164 /**
165  *
166  *
167  * the input callback
168  *
169  * In not \p NULL, the filter subsystem calls this function whenever the filter
170  * consumed some or all of its input buffer. A pointer to the buffer of consumed
171  * data, its length and a pointer to the own \a filter_callback structure are passed
172  * to \a input_cb. The input callback is expected to return a negative value on errors.
173  */
174         int (*input_cb)(char *buf, size_t len, struct filter_callback *fc);
175 /**
176  *
177  *
178  * the output callback
179  *
180  * If not NULL, this is called whenever the filter produces output. A pointer
181  * to the output data, its length and a pointer to the own \a filter_callback
182  * structure are passed to \a output_cb. Like the input callback, the output
183  * callback is expected to return a negative value on errors.
184  */
185         int (*output_cb)(char *buf, size_t len, struct filter_callback *fc);
186 /**
187  *
188  *
189  * the callback close function
190  *
191  * This gets called whenever the input/ouput callback returned an error, or if
192  * the filter chain is going to be destroyed, e.g. because the end of the
193  * stream was encounterd. It is assumed to succeed.
194  */
195         void (*close)(struct filter_callback *fc);
196 };
197
198
199 void close_filters(struct filter_chain *fc);
200 void filter_init(struct filter *all_filters);
201 int check_filter_arg(char *filter_arg, void **conf);
202 void filter_pre_select(__a_unused struct sched *s, struct task *t);
203
204 /**
205  * the structure associated with a paraslash filter
206  *
207  * Paraslash filters are "modules" which are used to transform an audio stream.
208  * struct filter contains pointers to functions that must be supplied by the
209  * filter code in order to be used by the driving application (currently
210  * para_audiod and para_filter).
211  *
212  * Note: As several instances of the same filter may be running at the same
213  * time, all these filter functions must be reentrant; no static non-constant
214  * variables may be used.
215  * \sa mp3dec.c, oggdec.c, wav.c, compress.c, filter_node
216  */
217 struct filter {
218 /**
219  *
220  *
221  * the name of the filter
222  */
223 const char *name;
224 /**
225  *
226  *
227  * pointer to the filter init routine
228  *
229  * This function is only called once at startup. It must initialize the
230  * other non-optional function pointers of \a f.
231  */
232 void (*init)(struct filter *f);
233 /**
234  *
235  *
236  * open one instance of this filter
237  *
238  * This should allocate the output buffer of the given filter node and do any
239  * other filter-specific preparations like initializing the private_data member
240  * of \a fn suitably. The open function is assumed to succeed.
241  */
242 void (*open)(struct filter_node *fn);
243 /**
244  *
245  *
246  * convert (filter) the given data
247  *
248  * Pointer to the converting function of the filter. It should convert the
249  * given input buffer \a inbuf which is of length \a len to the previoulsy
250  * reserved output buffer of \a fn. On success, it must return the number of
251  * bytes it consumed from \a inbuf. On errors, a negative number indicating the
252  * kind of the error must be returned.
253  *
254  * A zero return value just means that nothing was converted (probably because
255  * the input buffer was too small). This is not interpreted as an error.
256  */
257 ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
258 /**
259  *
260  *
261  * close one instance of this filter
262  *
263  * Free all resources of associated with \a fn that were previously allocated
264  * by the open() function.
265  */
266 void (*close)(struct filter_node *fn);
267 /**
268  *
269  *
270  * print the help text for this filter and exit
271  *
272  * This is optional and it is not necessary to initialize this pointer if
273  * the filter does not have a help text.
274  */
275 void (*print_help)(void);
276 /**
277  *
278  *
279  * a pointer to the filter's command line parser
280  *
281  * If this optional function pointer is not NULL, any filter options are passed
282  * from the main propgram to this command line parser once at application
283  * startup. The command line parser should check its command line options given
284  * by \a argc and \a argv and abort on errors. On success, it should return a
285  * pointer to the filter-specific configuration data determined by \a argc and
286  * \a argv.
287  */
288 void *(*parse_config)(int argc, char **argv);
289 };
290
291
292 static inline void write_int16_host_endian(char *buf, int val)
293 {
294 #ifdef WORDS_BIGENDIAN
295         *buf = val >> 8;
296         *(buf + 1) = val & 0xff;
297 #else
298         *buf = val & 0xff;
299         *(buf + 1) = val >> 8;
300 #endif
301 }
302
303
304 /** \cond */
305 extern struct filter filters[];
306 #define DECLARE_EXTERN_FILTER_INIT(name) \
307         extern void name ## _init(struct filter *f)
308
309 #define FILTER_INIT(filter) { \
310         .name = #filter, \
311         .init = filter ## _init, \
312         .parse_config = NULL, \
313         .print_help = NULL \
314 },
315
316 /* filters that are always present */
317 DECLARE_EXTERN_FILTER_INIT(wav);
318 /* wav is always the first filter */
319 #define WAV_FILTER_NUM 0
320 DECLARE_EXTERN_FILTER_INIT(compress);
321
322 /* next the optional filters */
323 #ifdef HAVE_MAD
324 DECLARE_EXTERN_FILTER_INIT(mp3dec);
325 #define MP3DEC_FILTER FILTER_INIT(mp3dec)
326 #else
327 #define MP3DEC_FILTER
328 #endif
329
330 #ifdef HAVE_FAAD
331 DECLARE_EXTERN_FILTER_INIT(aacdec);
332 #define AACDEC_FILTER FILTER_INIT(aacdec)
333 #else
334 #define AACDEC_FILTER
335 #endif
336
337 #ifdef HAVE_OGGVORBIS
338 DECLARE_EXTERN_FILTER_INIT(oggdec);
339 #define OGGDEC_FILTER FILTER_INIT(oggdec)
340 #else
341 #define OGGDEC_FILTER
342 #endif
343 /** \endcond */
344
345 /** define an array of all available filters */
346 #define DEFINE_FILTER_ARRAY(filters) struct filter filters[] = { \
347         FILTER_INIT(wav) \
348         FILTER_INIT(compress) \
349         MP3DEC_FILTER \
350         AACDEC_FILTER \
351         OGGDEC_FILTER \
352         { .name = NULL } };
353
354