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