oggdec_filter.ggo: Add more documentation.
[paraslash.git] / filter.h
1 /*
2  * Copyright (C) 2005-2008 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 /**
11  * Describes one running instance of a filter.
12 */
13 struct filter_node {
14         /** The number in the array of available filters. */
15         unsigned filter_num;
16         /** The filter chain this filter node belongs to. */
17         struct filter_chain *fc;
18         /**
19          * Each filter may store any filter-specific information about the particular
20          * instance of the filter here.
21          */
22         void *private_data;
23         /** The output buffer. */
24         char *buf;
25         /** The size of the output buffer. */
26         size_t bufsize;
27         /** The number of bytes currently loaded in \a buf. */
28         size_t loaded;
29         /** The list of registered callbacks. */
30         struct list_head callbacks;
31         /** A pointer to the configuration of this instance. */
32         void *conf;
33 };
34
35 /** Describes one running instance of a chain of filters */
36 struct filter_chain {
37         /** The length of the filter chain. */
38         unsigned int num_filters;
39         /**
40          * The number of channels of the current stream.
41          *
42          * Set by the decoding filter.
43          */
44         unsigned int channels;
45         /**
46          * Current sample rate in Hz.
47          *
48          * Set by the decoding filter.
49          */
50         unsigned int samplerate;
51         /** The list containing all filter nodes in this filter chain. */
52         struct filter_node *filter_nodes;
53         /**
54          * The input buffer of the filter chain.
55          *
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).
59          */
60         char *inbuf;
61         /**
62          * The output buffer of the filter chain.
63          *
64          * Points to the output buffer of the last filter in the filter chain.
65          */
66         char *outbuf;
67         /** Contains the number of bytes loaded in the input buffer. */
68         size_t *in_loaded;
69         /** Contains the number of bytes loaded in the output buffer. */
70         size_t *out_loaded;
71         /** Pointer to the error variable of the receiving application. */
72         int *input_error;
73         /** Pointer to the error variable of the writing application. */
74         int *output_error;
75         /** The task associated with the filter chain. */
76         struct task task;
77 };
78
79 #define FOR_EACH_FILTER_NODE(fn, fc, i) for (i = 0; i < (fc)->num_filters \
80         && (fn = (fc)->filter_nodes + i); i++)
81
82
83 /**
84  * Used to manage grab clients.
85  *
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
91  * the grab command.
92  */
93 struct filter_callback {
94         /** All callbacks are organized in a doubly linked list. */
95         struct list_head node;
96         /**
97          * Private data.
98          *
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.
102          */
103         void *data;
104         /**
105          * The input callback.
106          *
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.
111          */
112         int (*input_cb)(char *buf, size_t len, struct filter_callback *fc);
113         /**
114          * The output callback.
115          *
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.
120          */
121         int (*output_cb)(char *buf, size_t len, struct filter_callback *fc);
122         /**
123          * The callback close function.
124          *
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.
128          */
129         void (*close)(struct filter_callback *fc);
130 };
131
132
133 /**
134  * The structure associated with a paraslash filter.
135  *
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).
140  *
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
145  */
146 struct filter {
147         /** The name of the filter. */
148         const char *name;
149         /**
150          * Pointer to the filter init routine.
151          *
152          * This function is only called once at startup. It must initialize the
153          * other non-optional function pointers of this structure.
154          */
155         void (*init)(struct filter *f);
156         /**
157          * Open one instance of this filter.
158          *
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.
162          */
163         void (*open)(struct filter_node *fn);
164         /**
165          * Convert (filter) the given data.
166          *
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.
172          *
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.
175          */
176         ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
177         /**
178          * Close one instance of this filter.
179          *
180          * Free all resources of associated with \a fn that were previously allocated
181          * by the open() function.
182          */
183         void (*close)(struct filter_node *fn);
184         /**
185          * Print the help text for this filter and exit.
186          *
187          * This is optional and it is not necessary to initialize this pointer if
188          * the filter does not have a help text.
189          */
190         void (*print_help)(void);
191         /**
192          * A pointer to the filter's command line parser.
193          *
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
199          * \a argv.
200          */
201         void *(*parse_config)(int argc, char **argv);
202 };
203
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);
208
209
210 static inline void write_int16_host_endian(char *buf, int val)
211 {
212 #ifdef WORDS_BIGENDIAN
213         *buf = val >> 8;
214         *(buf + 1) = val & 0xff;
215 #else
216         *buf = val & 0xff;
217         *(buf + 1) = val >> 8;
218 #endif
219 }
220
221
222 /** \cond */
223 extern struct filter filters[];
224 #define DECLARE_EXTERN_FILTER_INIT(name) \
225         extern void name ## _init(struct filter *f)
226
227 #define FILTER_INIT(filter) { \
228         .name = #filter, \
229         .init = filter ## _init, \
230         .parse_config = NULL, \
231         .print_help = NULL \
232 },
233
234 /* filters that are always present */
235 DECLARE_EXTERN_FILTER_INIT(wav);
236 DECLARE_EXTERN_FILTER_INIT(compress);
237 DECLARE_EXTERN_FILTER_INIT(amp);
238
239 /* next the optional filters */
240 #ifdef HAVE_MAD
241 DECLARE_EXTERN_FILTER_INIT(mp3dec);
242 #define MP3DEC_FILTER FILTER_INIT(mp3dec)
243 #else
244 #define MP3DEC_FILTER
245 #endif
246
247 #ifdef HAVE_FAAD
248 DECLARE_EXTERN_FILTER_INIT(aacdec);
249 #define AACDEC_FILTER FILTER_INIT(aacdec)
250 #else
251 #define AACDEC_FILTER
252 #endif
253
254 #ifdef HAVE_OGGVORBIS
255 DECLARE_EXTERN_FILTER_INIT(oggdec);
256 #define OGGDEC_FILTER FILTER_INIT(oggdec)
257 #else
258 #define OGGDEC_FILTER
259 #endif
260 /** \endcond */
261
262 /** define an array of all available filters */
263 #define DEFINE_FILTER_ARRAY(filters) struct filter filters[] = { \
264         FILTER_INIT(wav) \
265         FILTER_INIT(compress) \
266         FILTER_INIT(amp) \
267         MP3DEC_FILTER \
268         AACDEC_FILTER \
269         OGGDEC_FILTER \
270         { .name = NULL } };
271
272