bf99a677b7c0b6e5cab95111834ad031c19c7619
[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 size_t min_iqs;
38 };
39
40 /** Describes one running instance of a chain of filters */
41 struct filter_chain {
42 /** The length of the filter chain. */
43 unsigned int num_filters;
44 /**
45 * The number of channels of the current stream.
46 *
47 * Set by the decoding filter.
48 */
49 unsigned int channels;
50 /**
51 * Current sample rate in Hz.
52 *
53 * Set by the decoding filter.
54 */
55 unsigned int samplerate;
56 /** The list containing all filter nodes in this filter chain. */
57 struct filter_node *filter_nodes;
58 /**
59 * The input buffer of the filter chain.
60 *
61 * This is set to point to the output buffer of the receiving application (the
62 * buffer used to read from stdin for para_filter; the output buffer of the
63 * current receiver for para_audiod).
64 */
65 char **inbufp;
66 /**
67 * The output buffer of the filter chain.
68 *
69 * Points to the output buffer of the last filter in the filter chain.
70 */
71 char **outbufp;
72 /** Contains the number of bytes loaded in the input buffer. */
73 size_t *in_loaded;
74 /** Contains the number of bytes loaded in the output buffer. */
75 size_t *out_loaded;
76 /** Pointer to the error variable of the receiving application. */
77 int *input_error;
78 /** Pointer to the error variable of the writing application. */
79 int *output_error;
80 /** The task associated with the filter chain. */
81 struct task task;
82 };
83
84 #define FOR_EACH_FILTER_NODE(fn, fc, i) for (i = 0; i < (fc)->num_filters \
85 && (fn = (fc)->filter_nodes + i); i++)
86
87 /**
88 * The structure associated with a paraslash filter.
89 *
90 * Paraslash filters are "modules" which are used to transform an audio stream.
91 * struct filter contains pointers to functions that must be supplied by the
92 * filter code in order to be used by the driving application (currently
93 * para_audiod and para_filter).
94 *
95 * Note: As several instances of the same filter may be running at the same
96 * time, all these filter functions must be reentrant; no static non-constant
97 * variables may be used.
98 * \sa mp3dec_filter.c, oggdec_filter.c, wav_filter.c, compress_filter.c, filter_node
99 */
100 struct filter {
101 /** The name of the filter. */
102 const char *name;
103 /**
104 * Pointer to the filter init routine.
105 *
106 * This function is only called once at startup. It must initialize the
107 * other non-optional function pointers of this structure.
108 */
109 void (*init)(struct filter *f);
110 /**
111 * Open one instance of this filter.
112 *
113 * This should allocate the output buffer of the given filter node and do any
114 * other filter-specific preparations like initializing the private_data member
115 * of \a fn suitably. The open function is assumed to succeed.
116 */
117 void (*open)(struct filter_node *fn);
118 /**
119 * Close one instance of this filter.
120 *
121 * Free all resources of associated with \a fn that were previously allocated
122 * by the open() function.
123 */
124 void (*close)(struct filter_node *fn);
125 /**
126 * A pointer to the filter's command line parser.
127 *
128 * If this optional function pointer is not NULL, any filter options
129 * are passed from the main program to this command line parser once at
130 * application startup. The command line parser should check its
131 * command line options given by \a argc and \a argv and abort on
132 * errors. Success must be indicated by a non-negative return value. In
133 * this case the function should return a pointer to the
134 * filter-specific configuration data determined by \a argc and \a
135 * argv. On failure, a negative paraslash error code must be returned.
136 */
137 int (*parse_config)(int argc, char **argv, void **config);
138 void (*free_config)(void *conf);
139
140 /** The help texts for this filter. */
141 struct ggo_help help;
142 void (*pre_select)(struct sched *s, struct task *t);
143 /**
144 * Convert (filter) the given data.
145 *
146 * Pointer to the converting function of the filter. It should convert as
147 * input data as possible. On errors, the post_select function is supposed
148 * to set t->error to a (negative) error code.
149 */
150 void (*post_select)(struct sched *s, struct task *t);
151 btr_command_handler execute;
152 };
153
154 void filter_init(void);
155 int check_filter_arg(char *filter_arg, void **conf);
156 void print_filter_helps(int detailed);
157 void generic_filter_pre_select(struct sched *s, struct task *t);
158
159 static inline void write_int16_host_endian(char *buf, int val)
160 {
161 #ifdef WORDS_BIGENDIAN
162 *buf = val >> 8;
163 *(buf + 1) = val & 0xff;
164 #else
165 *buf = val & 0xff;
166 *(buf + 1) = val >> 8;
167 #endif
168 }
169
170 DECLARE_FILTER_INITS
171
172 #define FOR_EACH_SUPPORTED_FILTER(j) for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
173
174 /** The filter array, one structure for each supported filter. */
175 extern struct filter filters[NUM_SUPPORTED_FILTERS];