14 typedef struct af_data_s
16 void* audio
; // data buffer
17 int len
; // buffer length
18 int rate
; // sample rate
19 int nch
; // number of channels
21 int bps
; // bytes per sample
24 // Fraction, used to calculate buffer lengths
31 int af_gcd(register int a
, register int b
);
32 void af_frac_cancel(frac_t
*f
);
33 void af_frac_mul(frac_t
*out
, const frac_t
*in
);
35 // Flags used for defining the behavior of an audio filter
36 #define AF_FLAGS_REENTRANT 0x00000000
37 #define AF_FLAGS_NOT_REENTRANT 0x00000001
39 /* Audio filter information not specific for current instance, but for
41 typedef struct af_info_s
48 int (*open
)(struct af_instance_s
* vf
);
51 // Linked list of audio filters
52 typedef struct af_instance_s
55 int (*control
)(struct af_instance_s
* af
, int cmd
, void* arg
);
56 void (*uninit
)(struct af_instance_s
* af
);
57 af_data_t
* (*play
)(struct af_instance_s
* af
, af_data_t
* data
);
58 void* setup
; // setup data for this specific instance and filter
59 af_data_t
* data
; // configuration for outgoing data stream
60 struct af_instance_s
* next
;
61 struct af_instance_s
* prev
;
62 double delay
; // Delay caused by the filter [ms]
63 frac_t mul
; /* length multiplier: how much does this instance change
64 the length of the buffer. */
67 // Initialization flags
68 extern int* af_cpu_speed
;
70 #define AF_INIT_AUTO 0x00000000
71 #define AF_INIT_SLOW 0x00000001
72 #define AF_INIT_FAST 0x00000002
73 #define AF_INIT_FORCE 0x00000003
74 #define AF_INIT_TYPE_MASK 0x00000003
76 #define AF_INIT_INT 0x00000000
77 #define AF_INIT_FLOAT 0x00000004
78 #define AF_INIT_FORMAT_MASK 0x00000004
82 #if defined(HAVE_SSE) || defined(HAVE_3DNOW)
83 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_FAST)
85 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_SLOW)
89 // Configuration switches
90 typedef struct af_cfg_s
{
91 int force
; // Initialization type
92 char** list
; /* list of names of filters that are added to filter
93 list during first initialization of stream */
96 // Current audio stream
97 typedef struct af_stream_s
99 // The first and last filter in the list
100 af_instance_t
* first
;
102 // Storage for input and output data formats
105 // Configuration for this stream
109 /*********************************************
117 #define AF_UNKNOWN -1
123 /*********************************************
127 /* Initialize the stream "s". This function creates a new filter list
128 if necessary according to the values set in input and output. Input
129 and output should contain the format of the current movie and the
130 formate of the preferred output respectively. The function is
131 reentrant i.e. if called with an already initialized stream the
132 stream will be reinitialized. If the binary parameter
133 "force_output" is set, the output format will be converted to the
134 format given in "s", otherwise the output fromat in the last filter
135 will be copied "s". The return value is 0 if success and -1 if
137 int af_init(af_stream_t
* s
);
139 // Uninit and remove all filters
140 void af_uninit(af_stream_t
* s
);
142 /* Add filter during execution. This function adds the filter "name"
143 to the stream s. The filter will be inserted somewhere nice in the
144 list of filters. The return value is a pointer to the new filter,
145 If the filter couldn't be added the return value is NULL. */
146 af_instance_t
* af_add(af_stream_t
* s
, char* name
);
148 // Uninit and remove the filter "af"
149 void af_remove(af_stream_t
* s
, af_instance_t
* af
);
151 /* Find filter in the dynamic filter list using it's name This
152 function is used for finding already initialized filters */
153 af_instance_t
* af_get(af_stream_t
* s
, char* name
);
155 // Filter data chunk through the filters in the list
156 af_data_t
* af_play(af_stream_t
* s
, af_data_t
* data
);
158 // send control to all filters, starting with the last until
159 // one accepts the command with AF_OK.
160 // Returns the accepting filter or NULL if none was found.
161 af_instance_t
*af_control_any_rev (af_stream_t
* s
, int cmd
, void* arg
);
163 /* Calculate how long the output from the filters will be given the
164 input length "len". The calculated length is >= the actual
166 int af_outputlen(af_stream_t
* s
, int len
);
168 /* Calculate how long the input to the filters should be to produce a
169 certain output length, i.e. the return value of this function is
170 the input length required to produce the output length "len". The
171 calculated length is <= the actual length */
172 int af_inputlen(af_stream_t
* s
, int len
);
174 /* Calculate how long the input IN to the filters should be to produce
175 a certain output length OUT but with the following three constraints:
176 1. IN <= max_insize, where max_insize is the maximum possible input
178 2. OUT <= max_outsize, where max_outsize is the maximum possible
180 3. If possible OUT >= len.
181 Return -1 in case of error */
182 int af_calc_insize_constrained(af_stream_t
* s
, int len
,
183 int max_outsize
,int max_insize
);
185 /* Calculate the total delay caused by the filters */
186 double af_calc_delay(af_stream_t
* s
);
188 // Helper functions and macros used inside the audio filters
190 /* Helper function called by the macro with the same name only to be
191 called from inside filters */
192 int af_resize_local_buffer(af_instance_t
* af
, af_data_t
* data
);
194 /* Helper function used to calculate the exact buffer length needed
195 when buffers are resized. The returned length is >= than what is
197 int af_lencalc(frac_t mul
, af_data_t
* data
);
199 /* Helper function used to convert to gain value from dB. Returns
200 AF_OK if of and AF_ERROR if fail */
201 int af_from_dB(int n
, float* in
, float* out
, float k
, float mi
, float ma
);
202 /* Helper function used to convert from gain value to dB. Returns
203 AF_OK if of and AF_ERROR if fail */
204 int af_to_dB(int n
, float* in
, float* out
, float k
);
205 /* Helper function used to convert from ms to sample time*/
206 int af_from_ms(int n
, float* in
, int* out
, int rate
, float mi
, float ma
);
207 /* Helper function used to convert from sample time to ms */
208 int af_to_ms(int n
, int* in
, float* out
, int rate
);
209 /* Helper function for testing the output format */
210 int af_test_output(struct af_instance_s
* af
, af_data_t
* out
);
212 float af_softclip(float a
);
214 /** Print a list of all available audio filters */
217 /* Fill the missing parameters in the af_data_t structure.
218 Used for stuffing bps with a value based on format. */
219 void af_fix_parameters(af_data_t
*data
);
221 /* Memory reallocation macro: if a local buffer is used (i.e. if the
222 filter doesn't operate on the incoming buffer this macro must be
223 called to ensure the buffer is big enough. */
224 #define RESIZE_LOCAL_BUFFER(a,d)\
225 ((a->data->len < af_lencalc(a->mul,d))?af_resize_local_buffer(a,d):AF_OK)
227 /* Some other useful macro definitions*/
229 #define min(a,b)(((a)>(b))?(b):(a))
233 #define max(a,b)(((a)>(b))?(a):(b))
237 #define clamp(a,min,max) (((a)>(max))?(max):(((a)<(min))?(min):(a)))
241 #define sign(a) (((a)>0)?(1):(-1))
245 #define lrnd(a,b) ((b)((a)>=0.0?(a)+0.5:(a)-0.5))
250 typedef struct af_msg_cfg_s
252 int level
; /* Message level for debug and error messages max = 2
253 min = -2 default = 0 */
254 FILE* err
; // Stream to print error messages to
255 FILE* msg
; // Stream to print information messages to
258 extern af_msg_cfg_t af_msg_cfg
; // Message
260 #define AF_MSG_FATAL -3 // Fatal error exit immediately
261 #define AF_MSG_ERROR -2 // Error return gracefully
262 #define AF_MSG_WARN -1 // Print warning but do not exit (can be suppressed)
263 #define AF_MSG_INFO 0 // Important information
264 #define AF_MSG_VERBOSE 1 // Print this if verbose is enabled
265 #define AF_MSG_DEBUG0 2 // Print if very verbose
266 #define AF_MSG_DEBUG1 3 // Print if very very verbose
268 /* Macro for printing error messages */
270 #define af_msg(lev, args... ) \
271 (((lev)<AF_MSG_WARN)?(fprintf(af_msg_cfg.err?af_msg_cfg.err:stderr, ## args )): \
272 (((lev)<=af_msg_cfg.level)?(fprintf(af_msg_cfg.msg?af_msg_cfg.msg:stdout, ## args )):0))
275 #endif /* __aop_h__ */