synced with r26729
[mplayer/greg.git] / libaf / af.h
blobdfa9cfdea534b111c0e50fb938d7cee2a4b6fc6b
1 #ifndef MPLAYER_AF_H
2 #define MPLAYER_AF_H
4 #include <stdio.h>
6 #include "config.h"
7 #include "af_mp.h"
8 #include "control.h"
9 #include "af_format.h"
11 struct af_instance_s;
13 // Number of channels
14 #ifndef AF_NCH
15 #define AF_NCH 6
16 #endif
18 // Audio data chunk
19 typedef struct af_data_s
21 void* audio; // data buffer
22 int len; // buffer length
23 int rate; // sample rate
24 int nch; // number of channels
25 int format; // format
26 int bps; // bytes per sample
27 } af_data_t;
30 // Flags used for defining the behavior of an audio filter
31 #define AF_FLAGS_REENTRANT 0x00000000
32 #define AF_FLAGS_NOT_REENTRANT 0x00000001
34 /* Audio filter information not specific for current instance, but for
35 a specific filter */
36 typedef struct af_info_s
38 const char *info;
39 const char *name;
40 const char *author;
41 const char *comment;
42 const int flags;
43 int (*open)(struct af_instance_s* vf);
44 } af_info_t;
46 // Linked list of audio filters
47 typedef struct af_instance_s
49 af_info_t* info;
50 int (*control)(struct af_instance_s* af, int cmd, void* arg);
51 void (*uninit)(struct af_instance_s* af);
52 af_data_t* (*play)(struct af_instance_s* af, af_data_t* data);
53 void* setup; // setup data for this specific instance and filter
54 af_data_t* data; // configuration for outgoing data stream
55 struct af_instance_s* next;
56 struct af_instance_s* prev;
57 double delay; /* Delay caused by the filter, in units of bytes read without
58 * corresponding output */
59 double mul; /* length multiplier: how much does this instance change
60 the length of the buffer. */
61 }af_instance_t;
63 // Initialization flags
64 extern int* af_cpu_speed;
66 #define AF_INIT_AUTO 0x00000000
67 #define AF_INIT_SLOW 0x00000001
68 #define AF_INIT_FAST 0x00000002
69 #define AF_INIT_FORCE 0x00000003
70 #define AF_INIT_TYPE_MASK 0x00000003
72 #define AF_INIT_INT 0x00000000
73 #define AF_INIT_FLOAT 0x00000004
74 #define AF_INIT_FORMAT_MASK 0x00000004
76 // Default init type
77 #ifndef AF_INIT_TYPE
78 #if defined(HAVE_SSE) || defined(HAVE_3DNOW)
79 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_FAST)
80 #else
81 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_SLOW)
82 #endif
83 #endif
85 // Configuration switches
86 typedef struct af_cfg_s{
87 int force; // Initialization type
88 char** list; /* list of names of filters that are added to filter
89 list during first initialization of stream */
90 }af_cfg_t;
92 // Current audio stream
93 typedef struct af_stream_s
95 // The first and last filter in the list
96 af_instance_t* first;
97 af_instance_t* last;
98 // Storage for input and output data formats
99 af_data_t input;
100 af_data_t output;
101 // Configuration for this stream
102 af_cfg_t cfg;
103 }af_stream_t;
105 /*********************************************
106 // Return values
109 #define AF_DETACH 2
110 #define AF_OK 1
111 #define AF_TRUE 1
112 #define AF_FALSE 0
113 #define AF_UNKNOWN -1
114 #define AF_ERROR -2
115 #define AF_FATAL -3
119 /*********************************************
120 // Export functions
124 * \defgroup af_chain Audio filter chain functions
125 * \{
126 * \param s filter chain
130 * \brief Initialize the stream "s".
131 * \return 0 on success, -1 on failure
133 * This function creates a new filter list if necessary, according
134 * to the values set in input and output. Input and output should contain
135 * the format of the current movie and the format of the preferred output
136 * respectively.
137 * Filters to convert to the preferred output format are inserted
138 * automatically, except when they are set to 0.
139 * The function is reentrant i.e. if called with an already initialized
140 * stream the stream will be reinitialized.
142 int af_init(af_stream_t* s);
145 * \brief Uninit and remove all filters from audio filter chain
147 void af_uninit(af_stream_t* s);
150 * \brief This function adds the filter "name" to the stream s.
151 * \param name name of filter to add
152 * \return pointer to the new filter, NULL if insert failed
154 * The filter will be inserted somewhere nice in the
155 * list of filters (i.e. at the beginning unless the
156 * first filter is the format filter (why??).
158 af_instance_t* af_add(af_stream_t* s, char* name);
161 * \brief Uninit and remove the filter "af"
162 * \param af filter to remove
164 void af_remove(af_stream_t* s, af_instance_t* af);
167 * \brief find filter in chain by name
168 * \param name name of the filter to find
169 * \return first filter with right name or NULL if not found
171 * This function is used for finding already initialized filters
173 af_instance_t* af_get(af_stream_t* s, char* name);
176 * \brief filter data chunk through the filters in the list
177 * \param data data to play
178 * \return resulting data
179 * \ingroup af_chain
181 af_data_t* af_play(af_stream_t* s, af_data_t* data);
184 * \brief send control to all filters, starting with the last until
185 * one accepts the command with AF_OK.
186 * \param cmd filter control command
187 * \param arg argument for filter command
188 * \return the accepting filter or NULL if none was found
190 af_instance_t *af_control_any_rev (af_stream_t* s, int cmd, void* arg);
193 * \brief calculate average ratio of filter output lenth to input length
194 * \return the ratio
196 double af_calc_filter_multiplier(af_stream_t* s);
199 * \brief Calculate the total delay caused by the filters
200 * \return delay in bytes of "missing" output
202 double af_calc_delay(af_stream_t* s);
204 /** \} */ // end of af_chain group
206 // Helper functions and macros used inside the audio filters
209 * \defgroup af_filter Audio filter helper functions
210 * \{
213 /* Helper function called by the macro with the same name only to be
214 called from inside filters */
215 int af_resize_local_buffer(af_instance_t* af, af_data_t* data);
217 /* Helper function used to calculate the exact buffer length needed
218 when buffers are resized. The returned length is >= than what is
219 needed */
220 int af_lencalc(double mul, af_data_t* data);
223 * \brief convert dB to gain value
224 * \param n number of values to convert
225 * \param in [in] values in dB, <= -200 will become 0 gain
226 * \param out [out] gain values
227 * \param k input values are divided by this
228 * \param mi minimum dB value, input will be clamped to this
229 * \param ma maximum dB value, input will be clamped to this
230 * \return AF_ERROR on error, AF_OK otherwise
232 int af_from_dB(int n, float* in, float* out, float k, float mi, float ma);
235 * \brief convert gain value to dB
236 * \param n number of values to convert
237 * \param in [in] gain values, 0 wil become -200 dB
238 * \param out [out] values in dB
239 * \param k output values will be multiplied by this
240 * \return AF_ERROR on error, AF_OK otherwise
242 int af_to_dB(int n, float* in, float* out, float k);
245 * \brief convert milliseconds to sample time
246 * \param n number of values to convert
247 * \param in [in] values in milliseconds
248 * \param out [out] sample time values
249 * \param rate sample rate
250 * \param mi minimum ms value, input will be clamped to this
251 * \param ma maximum ms value, input will be clamped to this
252 * \return AF_ERROR on error, AF_OK otherwise
254 int af_from_ms(int n, float* in, int* out, int rate, float mi, float ma);
257 * \brief convert sample time to milliseconds
258 * \param n number of values to convert
259 * \param in [in] sample time values
260 * \param out [out] values in milliseconds
261 * \param rate sample rate
262 * \return AF_ERROR on error, AF_OK otherwise
264 int af_to_ms(int n, int* in, float* out, int rate);
267 * \brief test if output format matches
268 * \param af audio filter
269 * \param out needed format, will be overwritten by available
270 * format if they do not match
271 * \return AF_FALSE if formats do not match, AF_OK if they match
273 * compares the format, bps, rate and nch values of af->data with out
275 int af_test_output(struct af_instance_s* af, af_data_t* out);
278 * \brief soft clipping function using sin()
279 * \param a input value
280 * \return clipped value
282 float af_softclip(float a);
284 /** \} */ // end of af_filter group, but more functions of this group below
286 /** Print a list of all available audio filters */
287 void af_help(void);
290 * \brief fill the missing parameters in the af_data_t structure
291 * \param data structure to fill
292 * \ingroup af_filter
294 * Currently only sets bps based on format
296 void af_fix_parameters(af_data_t *data);
298 /** Memory reallocation macro: if a local buffer is used (i.e. if the
299 filter doesn't operate on the incoming buffer this macro must be
300 called to ensure the buffer is big enough.
301 * \ingroup af_filter
303 #define RESIZE_LOCAL_BUFFER(a,d)\
304 ((a->data->len < af_lencalc(a->mul,d))?af_resize_local_buffer(a,d):AF_OK)
306 /* Some other useful macro definitions*/
307 #ifndef min
308 #define min(a,b)(((a)>(b))?(b):(a))
309 #endif
311 #ifndef max
312 #define max(a,b)(((a)>(b))?(a):(b))
313 #endif
315 #ifndef clamp
316 #define clamp(a,min,max) (((a)>(max))?(max):(((a)<(min))?(min):(a)))
317 #endif
319 #ifndef sign
320 #define sign(a) (((a)>0)?(1):(-1))
321 #endif
323 #ifndef lrnd
324 #define lrnd(a,b) ((b)((a)>=0.0?(a)+0.5:(a)-0.5))
325 #endif
327 /* Error messages */
329 typedef struct af_msg_cfg_s
331 int level; /* Message level for debug and error messages max = 2
332 min = -2 default = 0 */
333 FILE* err; // Stream to print error messages to
334 FILE* msg; // Stream to print information messages to
335 }af_msg_cfg_t;
337 extern af_msg_cfg_t af_msg_cfg; // Message
339 //! \addtogroup af_filter
340 //! \{
341 #define AF_MSG_FATAL -3 ///< Fatal error exit immediately
342 #define AF_MSG_ERROR -2 ///< Error return gracefully
343 #define AF_MSG_WARN -1 ///< Print warning but do not exit (can be suppressed)
344 #define AF_MSG_INFO 0 ///< Important information
345 #define AF_MSG_VERBOSE 1 ///< Print this if verbose is enabled
346 #define AF_MSG_DEBUG0 2 ///< Print if very verbose
347 #define AF_MSG_DEBUG1 3 ///< Print if very very verbose
349 //! Macro for printing error messages
350 #ifndef af_msg
351 #define af_msg(lev, args... ) \
352 (((lev)<AF_MSG_WARN)?(fprintf(af_msg_cfg.err?af_msg_cfg.err:stderr, ## args )): \
353 (((lev)<=af_msg_cfg.level)?(fprintf(af_msg_cfg.msg?af_msg_cfg.msg:stdout, ## args )):0))
354 #endif
355 //! \}
357 #endif /* MPLAYER_AF_H */