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