2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 // Static list of filters
30 extern af_info_t af_info_dummy
;
31 extern af_info_t af_info_delay
;
32 extern af_info_t af_info_channels
;
33 extern af_info_t af_info_format
;
34 extern af_info_t af_info_resample
;
35 extern af_info_t af_info_volume
;
36 extern af_info_t af_info_equalizer
;
37 extern af_info_t af_info_gate
;
38 extern af_info_t af_info_comp
;
39 extern af_info_t af_info_pan
;
40 extern af_info_t af_info_surround
;
41 extern af_info_t af_info_sub
;
42 extern af_info_t af_info_export
;
43 extern af_info_t af_info_volnorm
;
44 extern af_info_t af_info_extrastereo
;
45 extern af_info_t af_info_lavcac3enc
;
46 extern af_info_t af_info_lavcresample
;
47 extern af_info_t af_info_sweep
;
48 extern af_info_t af_info_hrtf
;
49 extern af_info_t af_info_ladspa
;
50 extern af_info_t af_info_center
;
51 extern af_info_t af_info_sinesuppress
;
52 extern af_info_t af_info_karaoke
;
53 extern af_info_t af_info_scaletempo
;
54 extern af_info_t af_info_stats
;
56 static af_info_t
* filter_list
[]={
69 #ifdef HAVE_SYS_MMAN_H
74 #ifdef CONFIG_LIBAVCODEC_A
77 #ifdef CONFIG_LIBAVCODEC
78 &af_info_lavcresample
,
86 &af_info_sinesuppress
,
94 af_msg_cfg_t af_msg_cfg
={0,NULL
,NULL
};
97 int* af_cpu_speed
= NULL
;
99 /* Find a filter in the static list of filters using it's name. This
100 function is used internally */
101 static af_info_t
* af_find(char*name
)
104 while(filter_list
[i
]){
105 if(!strcmp(filter_list
[i
]->name
,name
))
106 return filter_list
[i
];
109 af_msg(AF_MSG_ERROR
,"Couldn't find audio filter '%s'\n",name
);
113 /* Find filter in the dynamic filter list using it's name This
114 function is used for finding already initialized filters */
115 af_instance_t
* af_get(af_stream_t
* s
, char* name
)
117 af_instance_t
* af
=s
->first
;
120 if(!strcmp(af
->info
->name
,name
))
127 /*/ Function for creating a new filter of type name. The name may
128 contain the commandline parameters for the filter */
129 static af_instance_t
* af_create(af_stream_t
* s
, const char* name_with_cmd
)
131 char* name
= strdup(name_with_cmd
);
132 char* cmdline
= name
;
134 // Allocate space for the new filter and reset all pointers
135 af_instance_t
* new=malloc(sizeof(af_instance_t
));
137 af_msg(AF_MSG_ERROR
,"[libaf] Could not allocate memory\n");
140 memset(new,0,sizeof(af_instance_t
));
142 // Check for commandline parameters
143 strsep(&cmdline
, "=");
145 // Find filter from name
146 if(NULL
== (new->info
=af_find(name
)))
149 /* Make sure that the filter is not already in the list if it is
151 if(new->info
->flags
& AF_FLAGS_NOT_REENTRANT
){
153 af_msg(AF_MSG_ERROR
,"[libaf] There can only be one instance of"
154 " the filter '%s' in each stream\n",name
);
159 af_msg(AF_MSG_VERBOSE
,"[libaf] Adding filter %s \n",name
);
161 // Initialize the new filter
162 if(AF_OK
== new->info
->open(new) &&
163 AF_ERROR
< new->control(new,AF_CONTROL_POST_CREATE
,&s
->cfg
)){
165 if(AF_ERROR
>=new->control(new,AF_CONTROL_COMMAND_LINE
,cmdline
))
174 af_msg(AF_MSG_ERROR
,"[libaf] Couldn't create or open audio filter '%s'\n",
180 /* Create and insert a new filter of type name before the filter in the
181 argument. This function can be called during runtime, the return
182 value is the new filter */
183 static af_instance_t
* af_prepend(af_stream_t
* s
, af_instance_t
* af
, char* name
)
185 // Create the new filter and make sure it is OK
186 af_instance_t
* new=af_create(s
,name
);
204 /* Create and insert a new filter of type name after the filter in the
205 argument. This function can be called during runtime, the return
206 value is the new filter */
207 static af_instance_t
* af_append(af_stream_t
* s
, af_instance_t
* af
, char* name
)
209 // Create the new filter and make sure it is OK
210 af_instance_t
* new=af_create(s
,name
);
228 // Uninit and remove the filter "af"
229 void af_remove(af_stream_t
* s
, af_instance_t
* af
)
233 // Print friendly message
234 af_msg(AF_MSG_VERBOSE
,"[libaf] Removing filter %s \n",af
->info
->name
);
236 // Notify filter before changing anything
237 af
->control(af
,AF_CONTROL_PRE_DESTROY
,0);
241 af
->prev
->next
=af
->next
;
245 af
->next
->prev
=af
->prev
;
249 // Uninitialize af and free memory
254 /* Reinitializes all filters downstream from the filter given in the
255 argument the return value is AF_OK if success and AF_ERROR if
257 static int af_reinit(af_stream_t
* s
, af_instance_t
* af
)
260 af_data_t in
; // Format of the input to current filter
261 int rv
=0; // Return value
263 // Check if there are any filters left in the list
265 if(!(af
=af_append(s
,s
->first
,"dummy")))
271 // Check if this is the first filter
273 memcpy(&in
,&(s
->input
),sizeof(af_data_t
));
275 memcpy(&in
,af
->prev
->data
,sizeof(af_data_t
));
276 // Reset just in case...
280 rv
= af
->control(af
,AF_CONTROL_REINIT
,&in
);
285 case AF_FALSE
:{ // Configuration filter is needed
286 // Do auto insertion only if force is not specified
287 if((AF_INIT_TYPE_MASK
& s
->cfg
.force
) != AF_INIT_FORCE
){
288 af_instance_t
* new = NULL
;
289 // Insert channels filter
290 if((af
->prev
?af
->prev
->data
->nch
:s
->input
.nch
) != in
.nch
){
291 // Create channels filter
292 if(NULL
== (new = af_prepend(s
,af
,"channels")))
294 // Set number of output channels
295 if(AF_OK
!= (rv
= new->control(new,AF_CONTROL_CHANNELS
,&in
.nch
)))
297 // Initialize channels filter
299 memcpy(&in
,&(s
->input
),sizeof(af_data_t
));
301 memcpy(&in
,new->prev
->data
,sizeof(af_data_t
));
302 if(AF_OK
!= (rv
= new->control(new,AF_CONTROL_REINIT
,&in
)))
305 // Insert format filter
306 if((af
->prev
?af
->prev
->data
->format
:s
->input
.format
) != in
.format
){
307 // Create format filter
308 if(NULL
== (new = af_prepend(s
,af
,"format")))
310 // Set output bits per sample
311 in
.format
|= af_bits2fmt(in
.bps
*8);
312 if(AF_OK
!= (rv
= new->control(new,AF_CONTROL_FORMAT_FMT
,&in
.format
)))
314 // Initialize format filter
316 memcpy(&in
,&(s
->input
),sizeof(af_data_t
));
318 memcpy(&in
,new->prev
->data
,sizeof(af_data_t
));
319 if(AF_OK
!= (rv
= new->control(new,AF_CONTROL_REINIT
,&in
)))
322 if(!new){ // Should _never_ happen
323 af_msg(AF_MSG_ERROR
,"[libaf] Unable to correct audio format. "
324 "This error should never uccur, please send bugreport.\n");
330 af_msg(AF_MSG_ERROR
, "[libaf] Automatic filter insertion disabled "
331 "but formats do not match. Giving up.\n");
336 case AF_DETACH
:{ // Filter is redundant and wants to be unloaded
337 // Do auto remove only if force is not specified
338 if((AF_INIT_TYPE_MASK
& s
->cfg
.force
) != AF_INIT_FORCE
){
339 af_instance_t
* aft
=af
->prev
;
344 af
=s
->first
; // Restart configuration
349 af_msg(AF_MSG_ERROR
,"[libaf] Reinitialization did not work, audio"
350 " filter '%s' returned error code %i\n",af
->info
->name
,rv
);
357 // Uninit and remove all filters
358 void af_uninit(af_stream_t
* s
)
361 af_remove(s
,s
->first
);
364 /* Initialize the stream "s". This function creates a new filter list
365 if necessary according to the values set in input and output. Input
366 and output should contain the format of the current movie and the
367 formate of the preferred output respectively. The function is
368 reentrant i.e. if called with an already initialized stream the
369 stream will be reinitialized.
370 If one of the prefered output parameters is 0 the one that needs
371 no conversion is used (i.e. the output format in the last filter).
372 The return value is 0 if success and -1 if failure */
373 int af_init(af_stream_t
* s
)
380 // Precaution in case caller is misbehaving
381 s
->input
.audio
= s
->output
.audio
= NULL
;
382 s
->input
.len
= s
->output
.len
= 0;
384 // Figure out how fast the machine is
385 if(AF_INIT_AUTO
== (AF_INIT_TYPE_MASK
& s
->cfg
.force
))
386 s
->cfg
.force
= (s
->cfg
.force
& ~AF_INIT_TYPE_MASK
) | AF_INIT_TYPE
;
388 // Check if this is the first call
390 // Add all filters in the list (if there are any)
391 if(!s
->cfg
.list
){ // To make automatic format conversion work
392 if(!af_append(s
,s
->first
,"dummy"))
396 while(s
->cfg
.list
[i
]){
397 if(!af_append(s
,s
->last
,s
->cfg
.list
[i
++]))
404 if(AF_OK
!= af_reinit(s
,s
->first
))
407 // make sure the chain is not empty and valid (e.g. because of AF_DETACH)
409 if (!af_append(s
,s
->first
,"dummy") || AF_OK
!= af_reinit(s
,s
->first
))
412 // Check output format
413 if((AF_INIT_TYPE_MASK
& s
->cfg
.force
) != AF_INIT_FORCE
){
414 af_instance_t
* af
= NULL
; // New filter
415 // Check output frequency if not OK fix with resample
416 if(s
->output
.rate
&& s
->last
->data
->rate
!=s
->output
.rate
){
417 // try to find a filter that can change samplrate
418 af
= af_control_any_rev(s
, AF_CONTROL_RESAMPLE_RATE
| AF_CONTROL_SET
,
421 char *resampler
= "resample";
422 #ifdef CONFIG_LIBAVCODEC
423 if ((AF_INIT_TYPE_MASK
& s
->cfg
.force
) == AF_INIT_SLOW
)
424 resampler
= "lavcresample";
426 if((AF_INIT_TYPE_MASK
& s
->cfg
.force
) == AF_INIT_SLOW
){
427 if(!strcmp(s
->first
->info
->name
,"format"))
428 af
= af_append(s
,s
->first
,resampler
);
430 af
= af_prepend(s
,s
->first
,resampler
);
433 if(!strcmp(s
->last
->info
->name
,"format"))
434 af
= af_prepend(s
,s
->last
,resampler
);
436 af
= af_append(s
,s
->last
,resampler
);
438 // Init the new filter
439 if(!af
|| (AF_OK
!= af
->control(af
,AF_CONTROL_RESAMPLE_RATE
| AF_CONTROL_SET
,
442 // Use lin int if the user wants fast
443 if ((AF_INIT_TYPE_MASK
& s
->cfg
.force
) == AF_INIT_FAST
) {
445 sprintf(args
, "%d", s
->output
.rate
);
446 #ifdef CONFIG_LIBAVCODEC
447 if (strcmp(resampler
, "lavcresample") == 0)
451 strcat(args
, ":0:0");
452 af
->control(af
, AF_CONTROL_COMMAND_LINE
, args
);
455 if(AF_OK
!= af_reinit(s
,af
))
459 // Check number of output channels fix if not OK
460 // If needed always inserted last -> easy to screw up other filters
461 if(s
->output
.nch
&& s
->last
->data
->nch
!=s
->output
.nch
){
462 if(!strcmp(s
->last
->info
->name
,"format"))
463 af
= af_prepend(s
,s
->last
,"channels");
465 af
= af_append(s
,s
->last
,"channels");
466 // Init the new filter
467 if(!af
|| (AF_OK
!= af
->control(af
,AF_CONTROL_CHANNELS
,&(s
->output
.nch
))))
469 if(AF_OK
!= af_reinit(s
,af
))
473 // Check output format fix if not OK
474 if(s
->output
.format
!= AF_FORMAT_UNKNOWN
&&
475 s
->last
->data
->format
!= s
->output
.format
){
476 if(strcmp(s
->last
->info
->name
,"format"))
477 af
= af_append(s
,s
->last
,"format");
480 // Init the new filter
481 s
->output
.format
|= af_bits2fmt(s
->output
.bps
*8);
482 if(!af
|| (AF_OK
!= af
->control(af
,AF_CONTROL_FORMAT_FMT
,&(s
->output
.format
))))
484 if(AF_OK
!= af_reinit(s
,af
))
488 // Re init again just in case
489 if(AF_OK
!= af_reinit(s
,s
->first
))
492 if (s
->output
.format
== AF_FORMAT_UNKNOWN
)
493 s
->output
.format
= s
->last
->data
->format
;
494 if (!s
->output
.nch
) s
->output
.nch
= s
->last
->data
->nch
;
495 if (!s
->output
.rate
) s
->output
.rate
= s
->last
->data
->rate
;
496 if((s
->last
->data
->format
!= s
->output
.format
) ||
497 (s
->last
->data
->nch
!= s
->output
.nch
) ||
498 (s
->last
->data
->rate
!= s
->output
.rate
)) {
499 // Something is stuffed audio out will not work
500 af_msg(AF_MSG_ERROR
,"[libaf] Unable to setup filter system can not"
501 " meet sound-card demands, please send bugreport. \n");
509 /* Add filter during execution. This function adds the filter "name"
510 to the stream s. The filter will be inserted somewhere nice in the
511 list of filters. The return value is a pointer to the new filter,
512 If the filter couldn't be added the return value is NULL. */
513 af_instance_t
* af_add(af_stream_t
* s
, char* name
){
516 if(!s
|| !s
->first
|| !name
)
518 // Insert the filter somwhere nice
519 if(!strcmp(s
->first
->info
->name
,"format"))
520 new = af_append(s
, s
->first
, name
);
522 new = af_prepend(s
, s
->first
, name
);
526 // Reinitalize the filter list
527 if(AF_OK
!= af_reinit(s
, s
->first
)){
534 // Filter data chunk through the filters in the list
535 af_data_t
* af_play(af_stream_t
* s
, af_data_t
* data
)
537 af_instance_t
* af
=s
->first
;
538 // Iterate through all filters
540 if (data
->len
<= 0) break;
541 data
=af
->play(af
,data
);
547 /* Calculate the minimum output buffer size for given input data d
548 * when using the RESIZE_LOCAL_BUFFER macro. The +t+1 part ensures the
549 * value is >= len*mul rounded upwards to whole samples even if the
550 * double 'mul' is inexact. */
551 int af_lencalc(double mul
, af_data_t
* d
)
553 int t
= d
->bps
* d
->nch
;
554 return d
->len
* mul
+ t
+ 1;
557 // Calculate average ratio of filter output size to input size
558 double af_calc_filter_multiplier(af_stream_t
* s
)
560 af_instance_t
* af
=s
->first
;
562 // Iterate through all filters and calculate total multiplication factor
571 /* Calculate the total delay [bytes output] caused by the filters */
572 double af_calc_delay(af_stream_t
* s
)
574 af_instance_t
* af
=s
->first
;
575 register double delay
= 0.0;
576 // Iterate through all filters
585 /* Helper function called by the macro with the same name this
586 function should not be called directly */
587 int af_resize_local_buffer(af_instance_t
* af
, af_data_t
* data
)
589 // Calculate new length
590 register int len
= af_lencalc(af
->mul
,data
);
591 af_msg(AF_MSG_VERBOSE
,"[libaf] Reallocating memory in module %s, "
592 "old len = %i, new len = %i\n",af
->info
->name
,af
->data
->len
,len
);
593 // If there is a buffer free it
595 free(af
->data
->audio
);
596 // Create new buffer and check that it is OK
597 af
->data
->audio
= malloc(len
);
598 if(!af
->data
->audio
){
599 af_msg(AF_MSG_FATAL
,"[libaf] Could not allocate memory \n");
606 // documentation in af.h
607 af_instance_t
*af_control_any_rev (af_stream_t
* s
, int cmd
, void* arg
) {
608 int res
= AF_UNKNOWN
;
609 af_instance_t
* filt
= s
->last
;
611 res
= filt
->control(filt
, cmd
, arg
);
619 void af_help (void) {
621 af_msg(AF_MSG_INFO
, "Available audio filters:\n");
622 while (filter_list
[i
]) {
623 if (filter_list
[i
]->comment
&& filter_list
[i
]->comment
[0])
624 af_msg(AF_MSG_INFO
, " %-15s: %s (%s)\n", filter_list
[i
]->name
, filter_list
[i
]->info
, filter_list
[i
]->comment
);
626 af_msg(AF_MSG_INFO
, " %-15s: %s\n", filter_list
[i
]->name
, filter_list
[i
]->info
);
631 void af_fix_parameters(af_data_t
*data
)
633 data
->bps
= af_fmt2bits(data
->format
)/8;