Mercurial > mplayer.hg
annotate libaf/af.c @ 7615:c67328dd459a
Adding Support for non-reentrant audio filters
author | anders |
---|---|
date | Sun, 06 Oct 2002 11:26:14 +0000 |
parents | bf7f2bd2eb24 |
children | faa7dad7b4b1 |
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 printf("%s\n",af->info->name); | |
52 if(!strcmp(af->info->name,name)) | |
53 return af; | |
54 af=af->next; | |
55 } | |
56 return NULL; | |
57 } | |
58 | |
7568 | 59 // Function for creating a new filter of type name |
7615 | 60 af_instance_t* af_create(af_stream_t* s, char* name) |
7568 | 61 { |
62 // Allocate space for the new filter and reset all pointers | |
63 af_instance_t* new=malloc(sizeof(af_instance_t)); | |
64 if(!new){ | |
65 mp_msg(MSGT_AFILTER,MSGL_ERR,"Could not allocate memory\n"); | |
66 return NULL; | |
67 } | |
68 memset(new,0,sizeof(af_instance_t)); | |
69 | |
70 // Find filter from name | |
7615 | 71 if(NULL == (new->info=af_find(name))) |
72 return NULL; | |
73 | |
74 // Make sure that the filter is not already in the list if it is non-reentrant | |
75 if(new->info->flags & AF_FLAGS_NOT_REENTRANT){ | |
76 if(af_get(s,name)){ | |
77 mp_msg(MSGT_AFILTER,MSGL_ERR,"There can only be one instance of the filter '%s' in each stream\n",name); | |
78 free(new); | |
79 return NULL; | |
80 } | |
81 } | |
82 | |
7568 | 83 // Initialize the new filter |
7615 | 84 if(AF_OK==new->info->open(new)) |
7568 | 85 return new; |
86 | |
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 | |
145 // Detach pointers | |
146 if(af->prev) | |
147 af->prev->next=af->next; | |
148 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
149 s->first=af->next; |
7568 | 150 if(af->next) |
151 af->next->prev=af->prev; | |
152 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
153 s->last=af->prev; |
7568 | 154 |
155 // Uninitialize af and free memory | |
156 af->uninit(af); | |
157 free(af); | |
158 } | |
159 | |
160 /* Reinitializes all filters downstream from the filter given in the argument */ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
161 int af_reinit(af_stream_t* s, af_instance_t* af) |
7568 | 162 { |
163 if(!af) | |
164 return AF_ERROR; | |
165 | |
166 do{ | |
167 af_data_t in; // Format of the input to current filter | |
168 int rv=0; // Return value | |
169 | |
170 // Check if this is the first filter | |
171 if(!af->prev) | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
172 memcpy(&in,&(s->input),sizeof(af_data_t)); |
7568 | 173 else |
174 memcpy(&in,af->prev->data,sizeof(af_data_t)); | |
175 // Reset just in case... | |
176 in.audio=NULL; | |
177 in.len=0; | |
178 | |
179 rv = af->control(af,AF_CONTROL_REINIT,&in); | |
180 switch(rv){ | |
181 case AF_OK: | |
182 break; | |
183 case AF_FALSE:{ // Configuration filter is needed | |
184 af_instance_t* new = NULL; | |
185 // Insert channels filter | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
186 if((af->prev?af->prev->data->nch:s->input.nch) != in.nch){ |
7568 | 187 // Create channels filter |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
188 if(NULL == (new = af_prepend(s,af,"channels"))) |
7568 | 189 return AF_ERROR; |
190 // Set number of output channels | |
191 if(AF_OK != (rv = new->control(new,AF_CONTROL_CHANNELS,&in.nch))) | |
192 return rv; | |
193 // Initialize channels filter | |
194 if(!new->prev) | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
195 memcpy(&in,&(s->input),sizeof(af_data_t)); |
7568 | 196 else |
197 memcpy(&in,new->prev->data,sizeof(af_data_t)); | |
198 if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in))) | |
199 return rv; | |
200 } | |
201 // Insert format filter | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
202 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
|
203 ((af->prev?af->prev->data->bps:s->input.bps) != in.bps)){ |
7568 | 204 // Create format filter |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
205 if(NULL == (new = af_prepend(s,af,"format"))) |
7568 | 206 return AF_ERROR; |
207 // Set output format | |
208 if(AF_OK != (rv = new->control(new,AF_CONTROL_FORMAT,&in))) | |
209 return rv; | |
210 // Initialize format filter | |
211 if(!new->prev) | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
212 memcpy(&in,&(s->input),sizeof(af_data_t)); |
7568 | 213 else |
214 memcpy(&in,new->prev->data,sizeof(af_data_t)); | |
215 if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in))) | |
216 return rv; | |
217 } | |
218 if(!new) // Should _never_ happen | |
219 return AF_ERROR; | |
220 af=new; | |
221 break; | |
222 } | |
223 case AF_DETACH:{ // Filter is redundant and wants to be unloaded | |
224 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
|
225 af_remove(s,af); |
7568 | 226 if(aft) |
227 af=aft; | |
228 else | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
229 af=s->first; // Restart configuration |
7568 | 230 break; |
231 } | |
232 default: | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
233 mp_msg(MSGT_AFILTER,MSGL_ERR,"Reinitialization did not work, audio filter '%s' returned error code %i\n",af->info->name,rv); |
7568 | 234 return AF_ERROR; |
235 } | |
236 af=af->next; | |
237 }while(af); | |
238 return AF_OK; | |
239 } | |
240 | |
241 // 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
|
242 void af_uninit(af_stream_t* s) |
7568 | 243 { |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
244 while(s->first) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
245 af_remove(s,s->first); |
7568 | 246 } |
247 | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
248 /* 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 -1 if failure */ |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
255 int af_init(af_stream_t* s) |
7568 | 256 { |
257 int cfg=SLOW; // configuration type | |
258 int i=0; | |
259 | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
260 // Sanity check |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
261 if(!s) return -1; |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
262 |
7568 | 263 // 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
|
264 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
|
265 s->input.len = s->output.len = 0; |
7568 | 266 |
267 // 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
|
268 if(s->cfg.force) |
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
269 cfg=s->cfg.force; |
7568 | 270 else{ |
271 # if defined(HAVE_SSE) || defined(HAVE_3DNOWEX) | |
272 cfg=FAST; | |
273 # else | |
274 cfg=SLOW; | |
275 # endif | |
276 } | |
277 | |
278 // 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
|
279 if(!s->first){ |
7568 | 280 // 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
|
281 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
|
282 if(!af_append(s,s->first,"dummy")) |
7568 | 283 return -1; |
284 } | |
285 else{ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
286 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
|
287 if(!af_append(s,s->last,s->cfg.list[i++])) |
7568 | 288 return -1; |
289 } | |
290 } | |
291 } | |
292 | |
293 // Init filters | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
294 if(AF_OK != af_reinit(s,s->first)) |
7568 | 295 return -1; |
296 | |
297 // Check output format | |
298 if(cfg!=FORCE){ | |
299 af_instance_t* af = NULL; // New filter | |
300 // 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
|
301 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
|
302 if(NULL==(af=af_get(s,"resample"))){ |
7568 | 303 if(cfg==SLOW){ |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
304 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
|
305 af = af_append(s,s->first,"resample"); |
7568 | 306 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
307 af = af_prepend(s,s->first,"resample"); |
7568 | 308 } |
309 else{ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
310 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
|
311 af = af_prepend(s,s->last,"resample"); |
7568 | 312 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
313 af = af_append(s,s->last,"resample"); |
7568 | 314 } |
315 } | |
316 // 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
|
317 if(!af || (AF_OK != af->control(af,AF_CONTROL_RESAMPLE,&(s->output.rate)))) |
7568 | 318 return -1; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
319 if(AF_OK != af_reinit(s,af)) |
7568 | 320 return -1; |
321 } | |
322 | |
323 // Check number of output channels fix if not OK | |
324 // 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
|
325 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
|
326 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
|
327 af = af_prepend(s,s->last,"channels"); |
7568 | 328 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
329 af = af_append(s,s->last,"channels"); |
7568 | 330 // 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
|
331 if(!af || (AF_OK != af->control(af,AF_CONTROL_CHANNELS,&(s->output.nch)))) |
7568 | 332 return -1; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
333 if(AF_OK != af_reinit(s,af)) |
7568 | 334 return -1; |
335 } | |
336 | |
337 // 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
|
338 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
|
339 (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
|
340 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
|
341 af = af_append(s,s->last,"format"); |
7568 | 342 else |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
343 af = s->last; |
7568 | 344 // 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
|
345 if(!af ||(AF_OK != af->control(af,AF_CONTROL_FORMAT,&(s->output)))) |
7568 | 346 return -1; |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
347 if(AF_OK != af_reinit(s,af)) |
7568 | 348 return -1; |
349 } | |
350 | |
351 // 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
|
352 if(AF_OK != af_reinit(s,s->first)) |
7568 | 353 return -1; |
354 | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
355 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
|
356 (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
|
357 (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
|
358 (s->last->data->rate != s->output.rate)) { |
7568 | 359 // Something is stuffed audio out will not work |
360 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
|
361 af_uninit(s); |
7568 | 362 return -1; |
363 } | |
364 } | |
365 return 0; | |
366 } | |
367 | |
368 // 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
|
369 af_data_t* af_play(af_stream_t* s, af_data_t* data) |
7568 | 370 { |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
371 af_instance_t* af=s->first; |
7568 | 372 // Iterate through all filters |
373 do{ | |
374 data=af->play(af,data); | |
375 af=af->next; | |
376 }while(af); | |
377 return data; | |
378 } | |
379 | |
380 /* Helper function used to calculate the exact buffer length needed | |
7589 | 381 when buffers are resized. The returned length is >= than what is |
382 needed */ | |
383 inline int af_lencalc(frac_t mul, af_data_t* d){ | |
384 register int t = d->bps*d->nch; | |
7590 | 385 return t*(((d->len/t)*mul.n)/mul.d + 1); |
7568 | 386 } |
387 | |
388 /* Calculate how long the output from the filters will be given the | |
7589 | 389 input length "len". The calculated length is >= the actual |
390 length. */ | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
391 int af_outputlen(af_stream_t* s, int len) |
7568 | 392 { |
7589 | 393 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
|
394 af_instance_t* af=s->first; |
7589 | 395 register frac_t mul = {1,1}; |
7568 | 396 // Iterate through all filters |
397 do{ | |
398 mul.n *= af->mul.n; | |
399 mul.d *= af->mul.d; | |
400 af=af->next; | |
401 }while(af); | |
7589 | 402 return t * (((len/t)*mul.n + 1)/mul.d); |
7568 | 403 } |
404 | |
405 /* Calculate how long the input to the filters should be to produce a | |
406 certain output length, i.e. the return value of this function is | |
7589 | 407 the input length required to produce the output length "len". The |
408 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
|
409 int af_inputlen(af_stream_t* s, int len) |
7568 | 410 { |
7589 | 411 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
|
412 af_instance_t* af=s->first; |
7589 | 413 register frac_t mul = {1,1}; |
7568 | 414 // Iterate through all filters |
415 do{ | |
7589 | 416 mul.n *= af->mul.n; |
417 mul.d *= af->mul.d; | |
7568 | 418 af=af->next; |
419 }while(af); | |
7589 | 420 return t * (((len/t) * mul.d - 1)/mul.n); |
7568 | 421 } |
422 | |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
423 /* 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
|
424 a certain output length OUT but with the following three constraints: |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
425 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
|
426 block length |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
427 2. OUT <= max_outsize, where max_outsize is the maximum possible |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
428 output block length |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
429 3. If possible OUT >= len. |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
430 Return -1 in case of error */ |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
431 int af_calc_insize_constrained(af_stream_t* s, int len, |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
432 int max_outsize,int max_insize) |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
433 { |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
434 int t = s->input.bps*s->input.nch; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
435 int in = 0; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
436 int out = 0; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
437 af_instance_t* af=s->first; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
438 register frac_t mul = {1,1}; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
439 // Iterate through all filters and calculate total multiplication factor |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
440 do{ |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
441 mul.n *= af->mul.n; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
442 mul.d *= af->mul.d; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
443 af=af->next; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
444 }while(af); |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
445 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
|
446 |
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
447 if(in>max_insize) in=t*(max_insize/t); |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
448 |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
449 // Try to meet constraint nr 3. |
7612 | 450 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
|
451 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
|
452 in+=t; |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
453 } |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
454 |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
455 // printf("Could no meet constraint nr 3. in=%d out=%d len=%d max_in=%d max_out=%d", |
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
456 // in,out,len,max_insize,max_outsize); |
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
457 |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
458 // 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
|
459 while(out > max_outsize || in > max_insize){ |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
460 in-=t; |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
461 if(in<t) return -1; // Input parameters are probably incorrect |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
462 out = t * (((in/t)*mul.n + 1)/mul.d); |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
463 } |
7603
c89106306f5a
af_calc_insize_constrained() rounding changes, works better for me this way
arpi
parents:
7599
diff
changeset
|
464 return in; |
7598
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
465 } |
48f8c731efb5
Adding function for estimating required buffer length
anders
parents:
7590
diff
changeset
|
466 |
7568 | 467 /* Helper function called by the macro with the same name this |
468 function should not be called directly */ | |
469 inline int af_resize_local_buffer(af_instance_t* af, af_data_t* data) | |
470 { | |
471 // Calculate new length | |
7589 | 472 register int len = af_lencalc(af->mul,data); |
7568 | 473 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); |
474 // If there is a buffer free it | |
475 if(af->data->audio) | |
476 free(af->data->audio); | |
477 // Create new buffer and check that it is OK | |
478 af->data->audio = malloc(len); | |
479 if(!af->data->audio){ | |
480 mp_msg(MSGT_AFILTER,MSGL_ERR,"Could not allocate memory \n"); | |
481 return AF_ERROR; | |
482 } | |
483 af->data->len=len; | |
484 return AF_OK; | |
485 } |