synced with r24788
[mplayer/glamo.git] / libaf / af.h
blob09a64a3392c80de186fdf28b053f78a7d28912cf
1 #ifndef __af_h__
2 #define __af_h__
4 #include <stdio.h>
6 #include "af_mp.h"
7 #include "config.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;
29 // Fraction, used to calculate buffer lengths
30 typedef struct frac_s
32 int n; // Numerator
33 int d; // Denominator
34 } frac_t;
36 int af_gcd(register int a, register int b);
37 void af_frac_cancel(frac_t *f);
38 void af_frac_mul(frac_t *out, const frac_t *in);
40 // Flags used for defining the behavior of an audio filter
41 #define AF_FLAGS_REENTRANT 0x00000000
42 #define AF_FLAGS_NOT_REENTRANT 0x00000001
44 /* Audio filter information not specific for current instance, but for
45 a specific filter */
46 typedef struct af_info_s
48 const char *info;
49 const char *name;
50 const char *author;
51 const char *comment;
52 const int flags;
53 int (*open)(struct af_instance_s* vf);
54 } af_info_t;
56 // Linked list of audio filters
57 typedef struct af_instance_s
59 af_info_t* info;
60 int (*control)(struct af_instance_s* af, int cmd, void* arg);
61 void (*uninit)(struct af_instance_s* af);
62 af_data_t* (*play)(struct af_instance_s* af, af_data_t* data);
63 void* setup; // setup data for this specific instance and filter
64 af_data_t* data; // configuration for outgoing data stream
65 struct af_instance_s* next;
66 struct af_instance_s* prev;
67 double delay; // Delay caused by the filter [ms]
68 frac_t mul; /* length multiplier: how much does this instance change
69 the length of the buffer. */
70 }af_instance_t;
72 // Initialization flags
73 extern int* af_cpu_speed;
75 #define AF_INIT_AUTO 0x00000000
76 #define AF_INIT_SLOW 0x00000001
77 #define AF_INIT_FAST 0x00000002
78 #define AF_INIT_FORCE 0x00000003
79 #define AF_INIT_TYPE_MASK 0x00000003
81 #define AF_INIT_INT 0x00000000
82 #define AF_INIT_FLOAT 0x00000004
83 #define AF_INIT_FORMAT_MASK 0x00000004
85 // Default init type
86 #ifndef AF_INIT_TYPE
87 #if defined(HAVE_SSE) || defined(HAVE_3DNOW)
88 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_FAST)
89 #else
90 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_SLOW)
91 #endif
92 #endif
94 // Configuration switches
95 typedef struct af_cfg_s{
96 int force; // Initialization type
97 char** list; /* list of names of filters that are added to filter
98 list during first initialization of stream */
99 }af_cfg_t;
101 // Current audio stream
102 typedef struct af_stream_s
104 // The first and last filter in the list
105 af_instance_t* first;
106 af_instance_t* last;
107 // Storage for input and output data formats
108 af_data_t input;
109 af_data_t output;
110 // Configuration for this stream
111 af_cfg_t cfg;
112 }af_stream_t;
114 /*********************************************
115 // Return values
118 #define AF_DETACH 2
119 #define AF_OK 1
120 #define AF_TRUE 1
121 #define AF_FALSE 0
122 #define AF_UNKNOWN -1
123 #define AF_ERROR -2
124 #define AF_FATAL -3
128 /*********************************************
129 // Export functions
133 * \defgroup af_chain Audio filter chain functions
134 * \{
135 * \param s filter chain
139 * \brief Initialize the stream "s".
140 * \return 0 on success, -1 on failure
142 * This function creates a new filter list if necessary, according
143 * to the values set in input and output. Input and output should contain
144 * the format of the current movie and the format of the preferred output
145 * respectively.
146 * Filters to convert to the preferred output format are inserted
147 * automatically, except when they are set to 0.
148 * The function is reentrant i.e. if called with an already initialized
149 * stream the stream will be reinitialized.
151 int af_init(af_stream_t* s);
154 * \brief Uninit and remove all filters from audio filter chain
156 void af_uninit(af_stream_t* s);
159 * \brief This function adds the filter "name" to the stream s.
160 * \param name name of filter to add
161 * \return pointer to the new filter, NULL if insert failed
163 * The filter will be inserted somewhere nice in the
164 * list of filters (i.e. at the beginning unless the
165 * first filter is the format filter (why??).
167 af_instance_t* af_add(af_stream_t* s, char* name);
170 * \brief Uninit and remove the filter "af"
171 * \param af filter to remove
173 void af_remove(af_stream_t* s, af_instance_t* af);
176 * \brief find filter in chain by name
177 * \param name name of the filter to find
178 * \return first filter with right name or NULL if not found
180 * This function is used for finding already initialized filters
182 af_instance_t* af_get(af_stream_t* s, char* name);
185 * \brief filter data chunk through the filters in the list
186 * \param data data to play
187 * \return resulting data
188 * \ingroup af_chain
190 af_data_t* af_play(af_stream_t* s, af_data_t* data);
193 * \brief send control to all filters, starting with the last until
194 * one accepts the command with AF_OK.
195 * \param cmd filter control command
196 * \param arg argument for filter command
197 * \return the accepting filter or NULL if none was found
199 af_instance_t *af_control_any_rev (af_stream_t* s, int cmd, void* arg);
202 * \brief Calculate how long the output from the filters will be for a given
203 * input length.
204 * \param len input lenght for which to calculate output length
205 * \return calculated output length, will always be >= the resulting
206 * length when actually calling af_play.
208 int af_outputlen(af_stream_t* s, int len);
211 * \brief Calculate how long the input to the filters should be to produce a
212 * certain output length
213 * \param len wanted output length
214 * \return input length required to produce the output length "len". Possibly
215 * smaller to avoid overflow of output buffer
217 int af_inputlen(af_stream_t* s, int len);
220 * \brief calculate required input length for desired output size
221 * \param len desired minimum output length
222 * \param max_outsize maximum output length
223 * \param max_insize maximum input length
224 * \return input length or -1 on error
226 Calculate how long the input IN to the filters should be to produce
227 a certain output length OUT but with the following three constraints:
228 1. IN <= max_insize, where max_insize is the maximum possible input
229 block length
230 2. OUT <= max_outsize, where max_outsize is the maximum possible
231 output block length
232 3. If possible OUT >= len.
234 int af_calc_insize_constrained(af_stream_t* s, int len,
235 int max_outsize,int max_insize);
238 * \brief Calculate the total delay caused by the filters
239 * \return delay in seconds
241 double af_calc_delay(af_stream_t* s);
243 /** \} */ // end of af_chain group
245 // Helper functions and macros used inside the audio filters
248 * \defgroup af_filter Audio filter helper functions
249 * \{
252 /* Helper function called by the macro with the same name only to be
253 called from inside filters */
254 int af_resize_local_buffer(af_instance_t* af, af_data_t* data);
256 /* Helper function used to calculate the exact buffer length needed
257 when buffers are resized. The returned length is >= than what is
258 needed */
259 int af_lencalc(frac_t mul, af_data_t* data);
262 * \brief convert dB to gain value
263 * \param n number of values to convert
264 * \param in [in] values in dB, <= -200 will become 0 gain
265 * \param out [out] gain values
266 * \param k input values are divided by this
267 * \param mi minimum dB value, input will be clamped to this
268 * \param ma maximum dB value, input will be clamped to this
269 * \return AF_ERROR on error, AF_OK otherwise
271 int af_from_dB(int n, float* in, float* out, float k, float mi, float ma);
274 * \brief convert gain value to dB
275 * \param n number of values to convert
276 * \param in [in] gain values, 0 wil become -200 dB
277 * \param out [out] values in dB
278 * \param k output values will be multiplied by this
279 * \return AF_ERROR on error, AF_OK otherwise
281 int af_to_dB(int n, float* in, float* out, float k);
284 * \brief convert milliseconds to sample time
285 * \param n number of values to convert
286 * \param in [in] values in milliseconds
287 * \param out [out] sample time values
288 * \param rate sample rate
289 * \param mi minimum ms value, input will be clamped to this
290 * \param ma maximum ms value, input will be clamped to this
291 * \return AF_ERROR on error, AF_OK otherwise
293 int af_from_ms(int n, float* in, int* out, int rate, float mi, float ma);
296 * \brief convert sample time to milliseconds
297 * \param n number of values to convert
298 * \param in [in] sample time values
299 * \param out [out] values in milliseconds
300 * \param rate sample rate
301 * \return AF_ERROR on error, AF_OK otherwise
303 int af_to_ms(int n, int* in, float* out, int rate);
306 * \brief test if output format matches
307 * \param af audio filter
308 * \param out needed format, will be overwritten by available
309 * format if they do not match
310 * \return AF_FALSE if formats do not match, AF_OK if they match
312 * compares the format, bps, rate and nch values of af->data with out
314 int af_test_output(struct af_instance_s* af, af_data_t* out);
317 * \brief soft clipping function using sin()
318 * \param a input value
319 * \return clipped value
321 float af_softclip(float a);
323 /** \} */ // end of af_filter group, but more functions of this group below
325 /** Print a list of all available audio filters */
326 void af_help(void);
329 * \brief fill the missing parameters in the af_data_t structure
330 * \param data structure to fill
331 * \ingroup af_filter
333 * Currently only sets bps based on format
335 void af_fix_parameters(af_data_t *data);
337 /** Memory reallocation macro: if a local buffer is used (i.e. if the
338 filter doesn't operate on the incoming buffer this macro must be
339 called to ensure the buffer is big enough.
340 * \ingroup af_filter
342 #define RESIZE_LOCAL_BUFFER(a,d)\
343 ((a->data->len < af_lencalc(a->mul,d))?af_resize_local_buffer(a,d):AF_OK)
345 /* Some other useful macro definitions*/
346 #ifndef min
347 #define min(a,b)(((a)>(b))?(b):(a))
348 #endif
350 #ifndef max
351 #define max(a,b)(((a)>(b))?(a):(b))
352 #endif
354 #ifndef clamp
355 #define clamp(a,min,max) (((a)>(max))?(max):(((a)<(min))?(min):(a)))
356 #endif
358 #ifndef sign
359 #define sign(a) (((a)>0)?(1):(-1))
360 #endif
362 #ifndef lrnd
363 #define lrnd(a,b) ((b)((a)>=0.0?(a)+0.5:(a)-0.5))
364 #endif
366 /* Error messages */
368 typedef struct af_msg_cfg_s
370 int level; /* Message level for debug and error messages max = 2
371 min = -2 default = 0 */
372 FILE* err; // Stream to print error messages to
373 FILE* msg; // Stream to print information messages to
374 }af_msg_cfg_t;
376 extern af_msg_cfg_t af_msg_cfg; // Message
378 //! \addtogroup af_filter
379 //! \{
380 #define AF_MSG_FATAL -3 ///< Fatal error exit immediately
381 #define AF_MSG_ERROR -2 ///< Error return gracefully
382 #define AF_MSG_WARN -1 ///< Print warning but do not exit (can be suppressed)
383 #define AF_MSG_INFO 0 ///< Important information
384 #define AF_MSG_VERBOSE 1 ///< Print this if verbose is enabled
385 #define AF_MSG_DEBUG0 2 ///< Print if very verbose
386 #define AF_MSG_DEBUG1 3 ///< Print if very very verbose
388 //! Macro for printing error messages
389 #ifndef af_msg
390 #define af_msg(lev, args... ) \
391 (((lev)<AF_MSG_WARN)?(fprintf(af_msg_cfg.err?af_msg_cfg.err:stderr, ## args )): \
392 (((lev)<=af_msg_cfg.level)?(fprintf(af_msg_cfg.msg?af_msg_cfg.msg:stdout, ## args )):0))
393 #endif
394 //! \}
396 #endif /* __af_h__ */