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