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