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