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