1b916eecf632cc834b46703007097d6a02e6413a
[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 unsigned int num_filters;
38 /**
39 * The number of channels of the current stream.
40 *
41 * Set by the decoding filter.
42 */
43 unsigned int channels;
44 /**
45 * Current sample rate in Hz.
46 *
47 * Set by the decoding filter.
48 */
49 unsigned int samplerate;
50 /** The list containing all filter nodes in this filter chain. */
51 struct filter_node *filter_nodes;
52 /**
53 * The input buffer of the filter chain.
54 *
55 * This is set to point to the output buffer of the receiving application (the
56 * buffer used to read from stdin for para_filter; the output buffer of the
57 * current receiver for para_audiod).
58 */
59 char *inbuf;
60 /**
61 * The output buffer of the filter chain.
62 *
63 * Points to the output buffer of the last filter in the filter chain.
64 */
65 char *outbuf;
66 /** Contains the number of bytes loaded in the input buffer. */
67 size_t *in_loaded;
68 /** Contains the number of bytes loaded in the output buffer. */
69 size_t *out_loaded;
70 /** Pointer to the error variable of the receiving application. */
71 int *input_error;
72 /** Pointer to the error variable of the writing application. */
73 int *output_error;
74 /** The task associated with the filter chain. */
75 struct task task;
76 };
77
78 #define FOR_EACH_FILTER_NODE(fn, fc, i) for (i = 0; i < (fc)->num_filters \
79 && (fn = (fc)->filter_nodes + i); i++)
80
81
82 /**
83 * Used to manage grab clients.
84 *
85 * An application using paraslash's filter subsystem may register any number of
86 * callbacks for each filter_node. It is possible to attach a filter callback
87 * while the filter is running. This is used for stream grabbing in
88 * para_audiod: Whenever a client sends the 'grab' command, para_audiod adds a
89 * filter callback to the list of callbacks for the filter node specified in
90 * the grab command.
91 */
92 struct filter_callback {
93 /** All callbacks are organized in a doubly linked list. */
94 struct list_head node;
95 /**
96 * Private data.
97 *
98 * May be initialized by the application before registering the callback. This
99 * pointer is not used by the filter subsystem. It is provided for use within
100 * the input/output/close callback functions.
101 */
102 void *data;
103 /**
104 * The input callback.
105 *
106 * In not \p NULL, the filter subsystem calls this function whenever the filter
107 * consumed some or all of its input buffer. A pointer to the buffer of consumed
108 * data, its length and a pointer to the own \a filter_callback structure are passed
109 * to \a input_cb. The input callback is expected to return a negative value on errors.
110 */
111 int (*input_cb)(char *buf, size_t len, struct filter_callback *fc);
112 /**
113 * The output callback.
114 *
115 * If not NULL, this is called whenever the filter produces output. A pointer
116 * to the output data, its length and a pointer to the own \a filter_callback
117 * structure are passed to \a output_cb. Like the input callback, the output
118 * callback is expected to return a negative value on errors.
119 */
120 int (*output_cb)(char *buf, size_t len, struct filter_callback *fc);
121 /**
122 * The callback close function.
123 *
124 * This gets called whenever the input/output callback returned an error, or if
125 * the filter chain is going to be destroyed, e.g. because the end of the
126 * stream was encountered. It is assumed to succeed.
127 */
128 void (*close)(struct filter_callback *fc);
129 };
130
131
132 /**
133 * The structure associated with a paraslash filter.
134 *
135 * Paraslash filters are "modules" which are used to transform an audio stream.
136 * struct filter contains pointers to functions that must be supplied by the
137 * filter code in order to be used by the driving application (currently
138 * para_audiod and para_filter).
139 *
140 * Note: As several instances of the same filter may be running at the same
141 * time, all these filter functions must be reentrant; no static non-constant
142 * variables may be used.
143 * \sa mp3dec.c, oggdec.c, wav.c, compress.c, filter_node
144 */
145 struct filter {
146 /** The name of the filter. */
147 const char *name;
148 /**
149 * Pointer to the filter init routine.
150 *
151 * This function is only called once at startup. It must initialize the
152 * other non-optional function pointers of this structure.
153 */
154 void (*init)(struct filter *f);
155 /**
156 * Open one instance of this filter.
157 *
158 * This should allocate the output buffer of the given filter node and do any
159 * other filter-specific preparations like initializing the private_data member
160 * of \a fn suitably. The open function is assumed to succeed.
161 */
162 void (*open)(struct filter_node *fn);
163 /**
164 * Convert (filter) the given data.
165 *
166 * Pointer to the converting function of the filter. It should convert the
167 * given input buffer \a inbuf which is of length \a len to the previously
168 * reserved output buffer of \a fn. On success, it must return the number of
169 * bytes it consumed from \a inbuf. On errors, a negative number indicating the
170 * kind of the error must be returned.
171 *
172 * A zero return value just means that nothing was converted (probably because
173 * the input buffer was too small). This is not interpreted as an error.
174 */
175 ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
176 /**
177 * Close one instance of this filter.
178 *
179 * Free all resources of associated with \a fn that were previously allocated
180 * by the open() function.
181 */
182 void (*close)(struct filter_node *fn);
183 /**
184 * Print the help text for this filter and exit.
185 *
186 * This is optional and it is not necessary to initialize this pointer if
187 * the filter does not have a help text.
188 */
189 void (*print_help)(void);
190 /**
191 * A pointer to the filter's command line parser.
192 *
193 * If this optional function pointer is not NULL, any filter options are passed
194 * from the main program to this command line parser once at application
195 * startup. The command line parser should check its command line options given
196 * by \a argc and \a argv and abort on errors. On success, it should return a
197 * pointer to the filter-specific configuration data determined by \a argc and
198 * \a argv.
199 */
200 void *(*parse_config)(int argc, char **argv);
201 };
202
203 void close_filters(struct filter_chain *fc);
204 void filter_init(struct filter *all_filters);
205 int check_filter_arg(char *filter_arg, void **conf);
206 void filter_pre_select(__a_unused struct sched *s, struct task *t);
207
208
209 static inline void write_int16_host_endian(char *buf, int val)
210 {
211 #ifdef WORDS_BIGENDIAN
212 *buf = val >> 8;
213 *(buf + 1) = val & 0xff;
214 #else
215 *buf = val & 0xff;
216 *(buf + 1) = val >> 8;
217 #endif
218 }
219
220
221 /** \cond */
222 extern struct filter filters[];
223 #define DECLARE_EXTERN_FILTER_INIT(name) \
224 extern void name ## _init(struct filter *f)
225
226 #define FILTER_INIT(filter) { \
227 .name = #filter, \
228 .init = filter ## _init, \
229 .parse_config = NULL, \
230 .print_help = NULL \
231 },
232
233 /* filters that are always present */
234 DECLARE_EXTERN_FILTER_INIT(wav);
235 /* wav is always the first filter */
236 #define WAV_FILTER_NUM 0
237 DECLARE_EXTERN_FILTER_INIT(compress);
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 MP3DEC_FILTER \
267 AACDEC_FILTER \
268 OGGDEC_FILTER \
269 { .name = NULL } };
270
271