Consolidate decoder code by introducing prepare_filter_node().
[paraslash.git] / filter.h
1 /*
2  * Copyright (C) 2005-2009 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_common.c. */
8
9 /** The list of supported filters. */
10 enum filter_enum {FILTER_ENUM};
11
12 /**
13  * Describes one running instance of a filter.
14 */
15 struct filter_node {
16         /** The number in the array of available filters. */
17         unsigned filter_num;
18         /** The filter chain this filter node belongs to. */
19         struct filter_chain *fc;
20         /**
21          * Each filter may store any filter-specific information about the particular
22          * instance of the filter here.
23          */
24         void *private_data;
25         /** The output buffer. */
26         char *buf;
27         /** The size of the output buffer. */
28         size_t bufsize;
29         /** The number of bytes currently loaded in \a buf. */
30         size_t loaded;
31         /** The list of registered callbacks. */
32         struct list_head callbacks;
33         /** A pointer to the configuration of this instance. */
34         void *conf;
35         struct btr_node *btrn;
36         struct task task;
37 };
38
39 /** Describes one running instance of a chain of filters */
40 struct filter_chain {
41         /** The length of the filter chain. */
42         unsigned int num_filters;
43         /**
44          * The number of channels of the current stream.
45          *
46          * Set by the decoding filter.
47          */
48         unsigned int channels;
49         /**
50          * Current sample rate in Hz.
51          *
52          * Set by the decoding filter.
53          */
54         unsigned int samplerate;
55         /** The list containing all filter nodes in this filter chain. */
56         struct filter_node *filter_nodes;
57         /**
58          * The input buffer of the filter chain.
59          *
60          * This is set to point to the output buffer of the receiving application (the
61          * buffer used to read from stdin for para_filter; the output buffer of the
62          * current receiver for para_audiod).
63          */
64         char **inbufp;
65         /**
66          * The output buffer of the filter chain.
67          *
68          * Points to the output buffer of the last filter in the filter chain.
69          */
70         char **outbufp;
71         /** Contains the number of bytes loaded in the input buffer. */
72         size_t *in_loaded;
73         /** Contains the number of bytes loaded in the output buffer. */
74         size_t *out_loaded;
75         /** Pointer to the error variable of the receiving application. */
76         int *input_error;
77         /** Pointer to the error variable of the writing application. */
78         int *output_error;
79         /** The task associated with the filter chain. */
80         struct task task;
81 };
82
83 #define FOR_EACH_FILTER_NODE(fn, fc, i) for (i = 0; i < (fc)->num_filters \
84         && (fn = (fc)->filter_nodes + i); i++)
85
86
87 /**
88  * Used to manage grab clients.
89  *
90  * An application using paraslash's filter subsystem may register any number of
91  * callbacks for each filter_node. It is possible to attach a filter callback
92  * while the filter is running. This is used for stream grabbing in
93  * para_audiod: Whenever a client sends the 'grab' command, para_audiod adds a
94  * filter callback to the list of callbacks for the filter node specified in
95  * the grab command.
96  */
97 struct filter_callback {
98         /** All callbacks are organized in a doubly linked list. */
99         struct list_head node;
100         /**
101          * The input callback.
102          *
103          * If not \p NULL, the filter subsystem calls this function whenever the filter
104          * consumed some or all of its input buffer. A pointer to the buffer of consumed
105          * data, its length and a pointer to the own \a filter_callback structure are passed
106          * to \a input_cb. The input callback is expected to return a negative value on errors.
107          */
108         int (*input_cb)(char *buf, size_t len, struct filter_callback *fc);
109         /**
110          * The output callback.
111          *
112          * If not NULL, this is called whenever the filter produces output. A pointer
113          * to the output data, its length and a pointer to the own \a filter_callback
114          * structure are passed to \a output_cb. Like the input callback, the output
115          * callback is expected to return a negative value on errors.
116          */
117         int (*output_cb)(char *buf, size_t len, struct filter_callback *fc);
118         /**
119          * The callback close function.
120          *
121          * This gets called whenever the input/output callback returned an error, or if
122          * the filter chain is going to be destroyed, e.g. because the end of the
123          * stream was encountered. It is assumed to succeed.
124          */
125         void (*close)(struct filter_callback *fc);
126 };
127
128
129 /**
130  * The structure associated with a paraslash filter.
131  *
132  * Paraslash filters are "modules" which are used to transform an audio stream.
133  * struct filter contains pointers to functions that must be supplied by the
134  * filter code in order to be used by the driving application (currently
135  * para_audiod and para_filter).
136  *
137  * Note: As several instances of the same filter may be running at the same
138  * time, all these filter functions must be reentrant; no static non-constant
139  * variables may be used.
140  * \sa mp3dec_filter.c, oggdec_filter.c, wav_filter.c, compress_filter.c, filter_node
141  */
142 struct filter {
143         /** The name of the filter. */
144         const char *name;
145         /**
146          * Pointer to the filter init routine.
147          *
148          * This function is only called once at startup. It must initialize the
149          * other non-optional function pointers of this structure.
150          */
151         void (*init)(struct filter *f);
152         /**
153          * Open one instance of this filter.
154          *
155          * This should allocate the output buffer of the given filter node and do any
156          * other filter-specific preparations like initializing the private_data member
157          * of \a fn suitably. The open function is assumed to succeed.
158          */
159         void (*open)(struct filter_node *fn);
160         /**
161          * Convert (filter) the given data.
162          *
163          * Pointer to the converting function of the filter. It should convert the
164          * given input buffer \a inbuf which is of length \a len to the previously
165          * reserved output buffer of \a fn. On success, it must return the number of
166          * bytes it consumed from \a inbuf. On errors, a negative number indicating the
167          * kind of the error must be returned.
168          *
169          * A zero return value just means that nothing was converted (probably because
170          * the input buffer was too small). This is not interpreted as an error.
171          */
172         ssize_t (*convert)(char *inbuf, size_t len, struct filter_node *fn);
173         /**
174          * Close one instance of this filter.
175          *
176          * Free all resources of associated with \a fn that were previously allocated
177          * by the open() function.
178          */
179         void (*close)(struct filter_node *fn);
180         /**
181          * A pointer to the filter's command line parser.
182          *
183          * If this optional function pointer is not NULL, any filter options
184          * are passed from the main program to this command line parser once at
185          * application startup. The command line parser should check its
186          * command line options given by \a argc and \a argv and abort on
187          * errors. Success must be indicated by a non-negative return value. In
188          * this case the function should return a pointer to the
189          * filter-specific configuration data determined by \a argc and \a
190          * argv. On failure, a negative paraslash error code must be returned.
191          */
192         int (*parse_config)(int argc, char **argv, void **config);
193
194         /** The help texts for this filter. */
195         struct ggo_help help;
196
197         void (*pre_select)(struct sched *s, struct task *t);
198         void (*post_select)(struct sched *s, struct task *t);
199         btr_command_handler execute;
200 };
201
202 void close_filters(struct filter_chain *fc);
203 void filter_init(void);
204 int check_filter_arg(char *filter_arg, void **conf);
205 void filter_post_select(__a_unused struct sched *s, struct task *t);
206 void print_filter_helps(int detailed);
207 int prepare_filter_node(struct btr_node *btrn, size_t min_len);
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 DECLARE_FILTER_INITS
221
222 #define FOR_EACH_SUPPORTED_FILTER(j)  for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
223
224 /** The filter array, one structure for each supported filter. */
225 extern struct filter filters[NUM_SUPPORTED_FILTERS];