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