Mercurial > mplayer.hg
annotate libaf/af.c @ 8711:906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
author | anders |
---|---|
date | Thu, 02 Jan 2003 03:00:44 +0000 |
parents | db0810dcab33 |
children | a1578b329cc0 |
rev | line source |
---|---|
7568 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #ifdef HAVE_MALLOC_H | |
6 #include <malloc.h> | |
7 #endif | |
8 | |
9 #include "af.h" | |
10 | |
11 // Static list of filters | |
12 extern af_info_t af_info_dummy; | |
13 extern af_info_t af_info_delay; | |
14 extern af_info_t af_info_channels; | |
15 extern af_info_t af_info_format; | |
16 extern af_info_t af_info_resample; | |
7974
db1f16543379
enable volume filter and fix nonsense default volume (still not usable
rfelker
parents:
7745
diff
changeset
|
17 extern af_info_t af_info_volume; |
8073 | 18 extern af_info_t af_info_equalizer; |
8607 | 19 extern af_info_t af_info_gate; |
20 extern af_info_t af_info_comp; | |
21 extern af_info_t af_info_pan; | |
8678 | 22 extern af_info_t af_info_surround; |
7568 | 23 |
24 static af_info_t* filter_list[]={ \ | |
25 &af_info_dummy,\ | |
26 &af_info_delay,\ | |
27 &af_info_channels,\ | |
28 &af_info_format,\ | |
29 &af_info_resample,\ | |
7974
db1f16543379
enable volume filter and fix nonsense default volume (still not usable
rfelker
parents:
7745
diff
changeset
|
30 &af_info_volume,\ |
8073 | 31 &af_info_equalizer,\ |
8607 | 32 &af_info_gate,\ |
33 &af_info_comp,\ | |
34 &af_info_pan,\ | |
8678 | 35 &af_info_surround,\ |
7568 | 36 NULL \ |
37 }; | |
38 | |
8167 | 39 // Message printing |
40 af_msg_cfg_t af_msg_cfg={0,NULL,NULL}; | |
41 | |
42 // CPU speed | |
43 int* af_cpu_speed = NULL; | |
44 | |
7568 | 45 /* Find a filter in the static list of filters using it's name. This |
46 function is used internally */ | |
47 af_info_t* af_find(char*name) | |
48 { | |
49 int i=0; | |
50 while(filter_list[i]){ | |
51 if(!strcmp(filter_list[i]->name,name)) | |
52 return filter_list[i]; | |
53 i++; | |
54 } | |
8167 | 55 af_msg(AF_MSG_ERROR,"Couldn't find audio filter '%s'\n",name); |
7568 | 56 return NULL; |
57 } | |
58 | |
7615 | 59 /* Find filter in the dynamic filter list using it's name This |
60 function is used for finding already initialized filters */ | |
61 af_instance_t* af_get(af_stream_t* s, char* name) | |
62 { | |
63 af_instance_t* af=s->first; | |
64 // Find the filter | |
65 while(af != NULL){ | |
66 if(!strcmp(af->info->name,name)) | |
67 return af; | |
68 af=af->next; | |
69 } | |
70 return NULL; | |
71 } | |
72 | |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
73 /*/ Function for creating a new filter of type name. The name may |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
74 contain the commandline parameters for the filter */ |
7615 | 75 af_instance_t* af_create(af_stream_t* s, char* name) |
7568 | 76 { |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
77 char* cmdline = name; |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
78 char* delim = "="; |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7993
diff
changeset
|
79 |
7568 | 80 // Allocate space for the new filter and reset all pointers |
81 af_instance_t* new=malloc(sizeof(af_instance_t)); | |
82 if(!new){ | |
8607 | 83 af_msg(AF_MSG_ERROR,"[libaf] Could not allocate memory\n"); |
7568 | 84 return NULL; |
85 } | |
86 memset(new,0,sizeof(af_instance_t)); | |
87 | |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
88 // Check for commandline parameters |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
89 strsep(&cmdline, delim); |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
90 |
7568 | 91 // Find filter from name |
7615 | 92 if(NULL == (new->info=af_find(name))) |
93 return NULL; | |
94 | |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
95 /* Make sure that the filter is not already in the list if it is |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
96 non-reentrant */ |
7615 | 97 if(new->info->flags & AF_FLAGS_NOT_REENTRANT){ |
98 if(af_get(s,name)){ | |
8607 | 99 af_msg(AF_MSG_ERROR,"[libaf] There can only be one instance of" |
100 " the filter '%s' in each stream\n",name); | |
7615 | 101 free(new); |
102 return NULL; | |
103 } | |
104 } | |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
105 |
8607 | 106 af_msg(AF_MSG_VERBOSE,"[libaf] Adding filter %s \n",name); |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
107 |
7568 | 108 // Initialize the new filter |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
109 if(AF_OK == new->info->open(new) && |
7993
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
110 AF_ERROR < new->control(new,AF_CONTROL_POST_CREATE,&s->cfg)){ |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
111 if(cmdline){ |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
112 if(AF_ERROR<new->control(new,AF_CONTROL_COMMAND_LINE,cmdline)) |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
113 return new; |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
114 } |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
115 else |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
116 return new; |
ea0680d87f3f
Changing the behavour of the commandline parameter -af to conform with -vop. Adding new commanline parameter -af-adv for advanced af options. Adding changes to volume control to support commandline parameters.
anders
parents:
7974
diff
changeset
|
117 } |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
118 |
7568 | 119 free(new); |
8607 | 120 af_msg(AF_MSG_ERROR,"[libaf] Couldn't create or open audio filter '%s'\n", |
121 name); | |
7568 | 122 return NULL; |
123 } | |
124 | |
125 /* Create and insert a new filter of type name before the filter in the | |
126 argument. This function can be called during runtime, the return | |
127 value is the new filter */ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
128 af_instance_t* af_prepend(af_stream_t* s, af_instance_t* af, char* name) |
7568 | 129 { |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
130 // Create the new filter and make sure it is OK |
7615 | 131 af_instance_t* new=af_create(s,name); |
7568 | 132 if(!new) |
133 return NULL; | |
134 // Update pointers | |
135 new->next=af; | |
136 if(af){ | |
137 new->prev=af->prev; | |
138 af->prev=new; | |
139 } | |
140 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
141 s->last=new; |
7568 | 142 if(new->prev) |
143 new->prev->next=new; | |
144 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
145 s->first=new; |
7568 | 146 return new; |
147 } | |
148 | |
149 /* Create and insert a new filter of type name after the filter in the | |
150 argument. This function can be called during runtime, the return | |
151 value is the new filter */ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
152 af_instance_t* af_append(af_stream_t* s, af_instance_t* af, char* name) |
7568 | 153 { |
154 // Create the new filter and make sure it is OK | |
7615 | 155 af_instance_t* new=af_create(s,name); |
7568 | 156 if(!new) |
157 return NULL; | |
158 // Update pointers | |
159 new->prev=af; | |
160 if(af){ | |
161 new->next=af->next; | |
162 af->next=new; | |
163 } | |
164 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
165 s->first=new; |
7568 | 166 if(new->next) |
167 new->next->prev=new; | |
168 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
169 s->last=new; |
7568 | 170 return new; |
171 } | |
172 | |
173 // Uninit and remove the filter "af" | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
174 void af_remove(af_stream_t* s, af_instance_t* af) |
7568 | 175 { |
176 if(!af) return; | |
177 | |
8607 | 178 // Print friendly message |
179 af_msg(AF_MSG_VERBOSE,"[libaf] Removing filter %s \n",af->info->name); | |
180 | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
181 // Notify filter before changing anything |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
182 af->control(af,AF_CONTROL_PRE_DESTROY,0); |
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
183 |
7568 | 184 // Detach pointers |
185 if(af->prev) | |
186 af->prev->next=af->next; | |
187 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
188 s->first=af->next; |
7568 | 189 if(af->next) |
190 af->next->prev=af->prev; | |
191 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
192 s->last=af->prev; |
7568 | 193 |
194 // Uninitialize af and free memory | |
195 af->uninit(af); | |
196 free(af); | |
197 } | |
198 | |
7649
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
199 /* Reinitializes all filters downstream from the filter given in the |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
200 argument the return value is AF_OK if success and AF_ERROR if |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
201 failure */ |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
202 int af_reinit(af_stream_t* s, af_instance_t* af) |
7568 | 203 { |
204 if(!af) | |
205 return AF_ERROR; | |
206 | |
207 do{ | |
208 af_data_t in; // Format of the input to current filter | |
209 int rv=0; // Return value | |
210 | |
211 // Check if this is the first filter | |
212 if(!af->prev) | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
213 memcpy(&in,&(s->input),sizeof(af_data_t)); |
7568 | 214 else |
215 memcpy(&in,af->prev->data,sizeof(af_data_t)); | |
216 // Reset just in case... | |
217 in.audio=NULL; | |
218 in.len=0; | |
219 | |
220 rv = af->control(af,AF_CONTROL_REINIT,&in); | |
221 switch(rv){ | |
222 case AF_OK: | |
223 break; | |
224 case AF_FALSE:{ // Configuration filter is needed | |
8167 | 225 // Do auto insertion only if force is not specified |
226 if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){ | |
227 af_instance_t* new = NULL; | |
228 // Insert channels filter | |
229 if((af->prev?af->prev->data->nch:s->input.nch) != in.nch){ | |
230 // Create channels filter | |
231 if(NULL == (new = af_prepend(s,af,"channels"))) | |
232 return AF_ERROR; | |
233 // Set number of output channels | |
234 if(AF_OK != (rv = new->control(new,AF_CONTROL_CHANNELS,&in.nch))) | |
235 return rv; | |
236 // Initialize channels filter | |
237 if(!new->prev) | |
238 memcpy(&in,&(s->input),sizeof(af_data_t)); | |
239 else | |
240 memcpy(&in,new->prev->data,sizeof(af_data_t)); | |
241 if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in))) | |
242 return rv; | |
243 } | |
244 // Insert format filter | |
245 if(((af->prev?af->prev->data->format:s->input.format) != in.format) || | |
246 ((af->prev?af->prev->data->bps:s->input.bps) != in.bps)){ | |
247 // Create format filter | |
248 if(NULL == (new = af_prepend(s,af,"format"))) | |
249 return AF_ERROR; | |
8607 | 250 // Set output bits per sample |
251 if(AF_OK != (rv = new->control(new,AF_CONTROL_FORMAT_BPS,&in.bps)) || | |
252 AF_OK != (rv = new->control(new,AF_CONTROL_FORMAT_FMT,&in.format))) | |
8167 | 253 return rv; |
254 // Initialize format filter | |
255 if(!new->prev) | |
256 memcpy(&in,&(s->input),sizeof(af_data_t)); | |
257 else | |
258 memcpy(&in,new->prev->data,sizeof(af_data_t)); | |
259 if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in))) | |
260 return rv; | |
261 } | |
8607 | 262 if(!new){ // Should _never_ happen |
263 af_msg(AF_MSG_ERROR,"[libaf] Unable to correct audio format. " | |
264 "This error should never uccur, please send bugreport.\n"); | |
7568 | 265 return AF_ERROR; |
8607 | 266 } |
8167 | 267 af=new; |
7568 | 268 } |
269 break; | |
270 } | |
271 case AF_DETACH:{ // Filter is redundant and wants to be unloaded | |
8167 | 272 // Do auto remove only if force is not specified |
273 if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){ | |
274 af_instance_t* aft=af->prev; | |
275 af_remove(s,af); | |
276 if(aft) | |
277 af=aft; | |
278 else | |
279 af=s->first; // Restart configuration | |
280 } | |
7568 | 281 break; |
282 } | |
283 default: | |
8607 | 284 af_msg(AF_MSG_ERROR,"[libaf] Reinitialization did not work, audio" |
285 " filter '%s' returned error code %i\n",af->info->name,rv); | |
7568 | 286 return AF_ERROR; |
287 } | |
8607 | 288 // Check if there are any filters left in the list |
289 if(NULL == af){ | |
8711
906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
anders
parents:
8678
diff
changeset
|
290 if(!(af=af_append(s,s->first,"dummy"))) |
8607 | 291 return -1; |
292 } | |
293 else | |
294 af=af->next; | |
7568 | 295 }while(af); |
296 return AF_OK; | |
297 } | |
298 | |
299 // Uninit and remove all filters | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
300 void af_uninit(af_stream_t* s) |
7568 | 301 { |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
302 while(s->first) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
303 af_remove(s,s->first); |
7568 | 304 } |
305 | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
306 /* Initialize the stream "s". This function creates a new filter list |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
307 if necessary according to the values set in input and output. Input |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
308 and output should contain the format of the current movie and the |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
309 formate of the preferred output respectively. The function is |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
310 reentrant i.e. if called with an already initialized stream the |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
311 stream will be reinitialized. The return value is 0 if success and |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
312 -1 if failure */ |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
313 int af_init(af_stream_t* s) |
7568 | 314 { |
315 int i=0; | |
316 | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
317 // Sanity check |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
318 if(!s) return -1; |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
319 |
7568 | 320 // Precaution in case caller is misbehaving |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
321 s->input.audio = s->output.audio = NULL; |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
322 s->input.len = s->output.len = 0; |
7568 | 323 |
324 // Figure out how fast the machine is | |
8167 | 325 if(AF_INIT_AUTO == (AF_INIT_TYPE_MASK & s->cfg.force)) |
326 s->cfg.force = (s->cfg.force & ~AF_INIT_TYPE_MASK) | AF_INIT_TYPE; | |
7568 | 327 |
328 // Check if this is the first call | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
329 if(!s->first){ |
7568 | 330 // Add all filters in the list (if there are any) |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
331 if(!s->cfg.list){ // To make automatic format conversion work |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
332 if(!af_append(s,s->first,"dummy")) |
7568 | 333 return -1; |
334 } | |
335 else{ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
336 while(s->cfg.list[i]){ |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
337 if(!af_append(s,s->last,s->cfg.list[i++])) |
7568 | 338 return -1; |
339 } | |
340 } | |
341 } | |
8607 | 342 |
7568 | 343 // Init filters |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
344 if(AF_OK != af_reinit(s,s->first)) |
7568 | 345 return -1; |
346 | |
347 // Check output format | |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
348 if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){ |
7568 | 349 af_instance_t* af = NULL; // New filter |
350 // Check output frequency if not OK fix with resample | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
351 if(s->last->data->rate!=s->output.rate){ |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
352 if(NULL==(af=af_get(s,"resample"))){ |
7745
1d3a3dc1f488
Adding volume control and moving control() call parameters to a seperate file
anders
parents:
7665
diff
changeset
|
353 if((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW){ |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
354 if(!strcmp(s->first->info->name,"format")) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
355 af = af_append(s,s->first,"resample"); |
7568 | 356 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
357 af = af_prepend(s,s->first,"resample"); |
7568 | 358 } |
359 else{ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
360 if(!strcmp(s->last->info->name,"format")) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
361 af = af_prepend(s,s->last,"resample"); |
7568 | 362 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
363 af = af_append(s,s->last,"resample"); |
7568 | 364 } |
365 } | |
366 // Init the new filter | |
8607 | 367 if(!af || (AF_OK != af->control(af,AF_CONTROL_RESAMPLE_RATE, |
368 &(s->output.rate)))) | |
7568 | 369 return -1; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
370 if(AF_OK != af_reinit(s,af)) |
7568 | 371 return -1; |
372 } | |
373 | |
374 // Check number of output channels fix if not OK | |
375 // If needed always inserted last -> easy to screw up other filters | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
376 if(s->last->data->nch!=s->output.nch){ |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
377 if(!strcmp(s->last->info->name,"format")) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
378 af = af_prepend(s,s->last,"channels"); |
7568 | 379 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
380 af = af_append(s,s->last,"channels"); |
7568 | 381 // Init the new filter |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
382 if(!af || (AF_OK != af->control(af,AF_CONTROL_CHANNELS,&(s->output.nch)))) |
7568 | 383 return -1; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
384 if(AF_OK != af_reinit(s,af)) |
7568 | 385 return -1; |
386 } | |
387 | |
388 // Check output format fix if not OK | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
389 if((s->last->data->format != s->output.format) || |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
390 (s->last->data->bps != s->output.bps)){ |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
391 if(strcmp(s->last->info->name,"format")) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
392 af = af_append(s,s->last,"format"); |
7568 | 393 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
394 af = s->last; |
7568 | 395 // Init the new filter |
8607 | 396 if(!af ||(AF_OK != af->control(af,AF_CONTROL_FORMAT_BPS,&(s->output.bps))) |
397 || (AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT,&(s->output.format)))) | |
7568 | 398 return -1; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
399 if(AF_OK != af_reinit(s,af)) |
7568 | 400 return -1; |
401 } | |
402 | |
403 // Re init again just in case | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
404 if(AF_OK != af_reinit(s,s->first)) |
7568 | 405 return -1; |
406 | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
407 if((s->last->data->format != s->output.format) || |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
408 (s->last->data->bps != s->output.bps) || |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
409 (s->last->data->nch != s->output.nch) || |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
410 (s->last->data->rate != s->output.rate)) { |
7568 | 411 // Something is stuffed audio out will not work |
8607 | 412 af_msg(AF_MSG_ERROR,"[libaf] Unable to setup filter system can not" |
413 " meet sound-card demands, please send bugreport. \n"); | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
414 af_uninit(s); |
7568 | 415 return -1; |
416 } | |
417 } | |
418 return 0; | |
419 } | |
420 | |
7649
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
421 /* Add filter during execution. This function adds the filter "name" |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
422 to the stream s. The filter will be inserted somewhere nice in the |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
423 list of filters. The return value is a pointer to the new filter, |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
424 If the filter couldn't be added the return value is NULL. */ |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
425 af_instance_t* af_add(af_stream_t* s, char* name){ |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
426 af_instance_t* new; |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
427 // Sanity check |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
428 if(!s || !s->first || !name) |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
429 return NULL; |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
430 // Insert the filter somwhere nice |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
431 if(!strcmp(s->first->info->name,"format")) |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
432 new = af_append(s, s->first, name); |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
433 else |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
434 new = af_prepend(s, s->first, name); |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
435 if(!new) |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
436 return NULL; |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
437 |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
438 // Reinitalize the filter list |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
439 if(AF_OK != af_reinit(s, s->first)){ |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
440 free(new); |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
441 return NULL; |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
442 } |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
443 return new; |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
444 } |
90e16aa8ae5f
Adding functionality for adding filters during execution
anders
parents:
7617
diff
changeset
|
445 |
7568 | 446 // Filter data chunk through the filters in the list |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
447 af_data_t* af_play(af_stream_t* s, af_data_t* data) |
7568 | 448 { |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
449 af_instance_t* af=s->first; |
7568 | 450 // Iterate through all filters |
451 do{ | |
452 data=af->play(af,data); | |
453 af=af->next; | |
454 }while(af); | |
455 return data; | |
456 } | |
457 | |
458 /* Helper function used to calculate the exact buffer length needed | |
7589 | 459 when buffers are resized. The returned length is >= than what is |
460 needed */ | |
461 inline int af_lencalc(frac_t mul, af_data_t* d){ | |
462 register int t = d->bps*d->nch; | |
7590 | 463 return t*(((d->len/t)*mul.n)/mul.d + 1); |
7568 | 464 } |
465 | |
466 /* Calculate how long the output from the filters will be given the | |
7589 | 467 input length "len". The calculated length is >= the actual |
468 length. */ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
469 int af_outputlen(af_stream_t* s, int len) |
7568 | 470 { |
7589 | 471 int t = s->input.bps*s->input.nch; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
472 af_instance_t* af=s->first; |
7589 | 473 register frac_t mul = {1,1}; |
7568 | 474 // Iterate through all filters |
475 do{ | |
476 mul.n *= af->mul.n; | |
477 mul.d *= af->mul.d; | |
478 af=af->next; | |
479 }while(af); | |
7589 | 480 return t * (((len/t)*mul.n + 1)/mul.d); |
7568 | 481 } |
482 | |
483 /* Calculate how long the input to the filters should be to produce a | |
484 certain output length, i.e. the return value of this function is | |
7589 | 485 the input length required to produce the output length "len". The |
486 calculated length is <= the actual length */ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
487 int af_inputlen(af_stream_t* s, int len) |
7568 | 488 { |
7589 | 489 int t = s->input.bps*s->input.nch; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
490 af_instance_t* af=s->first; |
7589 | 491 register frac_t mul = {1,1}; |
7568 | 492 // Iterate through all filters |
493 do{ | |
7589 | 494 mul.n *= af->mul.n; |
495 mul.d *= af->mul.d; | |
7568 | 496 af=af->next; |
497 }while(af); | |
7589 | 498 return t * (((len/t) * mul.d - 1)/mul.n); |
7568 | 499 } |
500 | |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
501 /* Calculate how long the input IN to the filters should be to produce |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
502 a certain output length OUT but with the following three constraints: |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
503 1. IN <= max_insize, where max_insize is the maximum possible input |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
504 block length |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
505 2. OUT <= max_outsize, where max_outsize is the maximum possible |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
506 output block length |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
507 3. If possible OUT >= len. |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
508 Return -1 in case of error */ |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
509 int af_calc_insize_constrained(af_stream_t* s, int len, |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
510 int max_outsize,int max_insize) |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
511 { |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
512 int t = s->input.bps*s->input.nch; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
513 int in = 0; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
514 int out = 0; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
515 af_instance_t* af=s->first; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
516 register frac_t mul = {1,1}; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
517 // Iterate through all filters and calculate total multiplication factor |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
518 do{ |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
519 mul.n *= af->mul.n; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
520 mul.d *= af->mul.d; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
521 af=af->next; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
522 }while(af); |
8607 | 523 // Sanity check |
524 if(!mul.n || !mul.d) | |
525 return -1; | |
526 | |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
527 in = t * (((len/t) * mul.d - 1)/mul.n); |
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
528 |
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
529 if(in>max_insize) in=t*(max_insize/t); |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
530 |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
531 // Try to meet constraint nr 3. |
7612 | 532 while((out=t * (((in/t+1)*mul.n - 1)/mul.d)) <= max_outsize && in<=max_insize){ |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
533 if( (t * (((in/t)*mul.n))/mul.d) >= len) return in; |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
534 in+=t; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
535 } |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
536 |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
537 // Could no meet constraint nr 3. |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
538 while(out > max_outsize || in > max_insize){ |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
539 in-=t; |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
540 if(in<t) return -1; // Input parameters are probably incorrect |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
541 out = t * (((in/t)*mul.n + 1)/mul.d); |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
542 } |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
543 return in; |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
544 } |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
545 |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
546 /* Calculate the total delay [ms] caused by the filters */ |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
547 double af_calc_delay(af_stream_t* s) |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
548 { |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
549 af_instance_t* af=s->first; |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
550 register double delay = 0.0; |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
551 // Iterate through all filters |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
552 while(af){ |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
553 delay += af->delay; |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
554 af=af->next; |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
555 } |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
556 return delay; |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
557 } |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7649
diff
changeset
|
558 |
7568 | 559 /* Helper function called by the macro with the same name this |
560 function should not be called directly */ | |
561 inline int af_resize_local_buffer(af_instance_t* af, af_data_t* data) | |
562 { | |
563 // Calculate new length | |
7589 | 564 register int len = af_lencalc(af->mul,data); |
8607 | 565 af_msg(AF_MSG_VERBOSE,"[libaf] Reallocating memory in module %s, " |
566 "old len = %i, new len = %i\n",af->info->name,af->data->len,len); | |
7568 | 567 // If there is a buffer free it |
568 if(af->data->audio) | |
569 free(af->data->audio); | |
570 // Create new buffer and check that it is OK | |
571 af->data->audio = malloc(len); | |
572 if(!af->data->audio){ | |
8607 | 573 af_msg(AF_MSG_FATAL,"[libaf] Could not allocate memory \n"); |
7568 | 574 return AF_ERROR; |
575 } | |
576 af->data->len=len; | |
577 return AF_OK; | |
578 } |