2002-10-01 06:45:08 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "af.h"
|
|
|
|
|
|
|
|
// Static list of filters
|
|
|
|
extern af_info_t af_info_dummy;
|
|
|
|
extern af_info_t af_info_delay;
|
|
|
|
extern af_info_t af_info_channels;
|
|
|
|
extern af_info_t af_info_format;
|
|
|
|
extern af_info_t af_info_resample;
|
2002-10-30 04:11:26 +00:00
|
|
|
extern af_info_t af_info_volume;
|
2002-11-03 09:51:02 +00:00
|
|
|
extern af_info_t af_info_equalizer;
|
2002-12-28 13:59:53 +00:00
|
|
|
extern af_info_t af_info_gate;
|
|
|
|
extern af_info_t af_info_comp;
|
|
|
|
extern af_info_t af_info_pan;
|
2002-12-31 15:14:13 +00:00
|
|
|
extern af_info_t af_info_surround;
|
2003-01-07 10:33:30 +00:00
|
|
|
extern af_info_t af_info_sub;
|
2003-09-20 13:42:26 +00:00
|
|
|
extern af_info_t af_info_export;
|
2004-10-04 19:11:05 +00:00
|
|
|
extern af_info_t af_info_volnorm;
|
|
|
|
extern af_info_t af_info_extrastereo;
|
2004-10-21 03:32:31 +00:00
|
|
|
extern af_info_t af_info_lavcresample;
|
2004-10-21 12:10:55 +00:00
|
|
|
extern af_info_t af_info_sweep;
|
2004-11-20 14:41:51 +00:00
|
|
|
extern af_info_t af_info_hrtf;
|
2004-12-23 02:09:52 +00:00
|
|
|
extern af_info_t af_info_ladspa;
|
2005-02-21 17:14:00 +00:00
|
|
|
extern af_info_t af_info_center;
|
2006-06-07 00:02:51 +00:00
|
|
|
extern af_info_t af_info_sinesuppress;
|
2006-05-13 08:46:31 +00:00
|
|
|
extern af_info_t af_info_karaoke;
|
2002-10-01 06:45:08 +00:00
|
|
|
|
2003-09-21 12:16:16 +00:00
|
|
|
static af_info_t* filter_list[]={
|
|
|
|
&af_info_dummy,
|
|
|
|
&af_info_delay,
|
|
|
|
&af_info_channels,
|
|
|
|
&af_info_format,
|
|
|
|
&af_info_resample,
|
|
|
|
&af_info_volume,
|
|
|
|
&af_info_equalizer,
|
|
|
|
&af_info_gate,
|
|
|
|
&af_info_comp,
|
|
|
|
&af_info_pan,
|
|
|
|
&af_info_surround,
|
|
|
|
&af_info_sub,
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
&af_info_export,
|
|
|
|
#endif
|
2004-10-04 19:11:05 +00:00
|
|
|
&af_info_volnorm,
|
|
|
|
&af_info_extrastereo,
|
2004-10-21 03:32:31 +00:00
|
|
|
#ifdef USE_LIBAVCODEC
|
|
|
|
&af_info_lavcresample,
|
|
|
|
#endif
|
2004-10-21 12:10:55 +00:00
|
|
|
&af_info_sweep,
|
2004-11-20 14:41:51 +00:00
|
|
|
&af_info_hrtf,
|
2004-12-23 02:09:52 +00:00
|
|
|
#ifdef HAVE_LADSPA
|
|
|
|
&af_info_ladspa,
|
|
|
|
#endif
|
2005-02-21 17:14:00 +00:00
|
|
|
&af_info_center,
|
2006-06-07 00:02:51 +00:00
|
|
|
&af_info_sinesuppress,
|
2006-05-13 08:46:31 +00:00
|
|
|
&af_info_karaoke,
|
2003-09-21 12:16:16 +00:00
|
|
|
NULL
|
2002-10-01 06:45:08 +00:00
|
|
|
};
|
|
|
|
|
2002-11-12 12:33:56 +00:00
|
|
|
// Message printing
|
|
|
|
af_msg_cfg_t af_msg_cfg={0,NULL,NULL};
|
|
|
|
|
|
|
|
// CPU speed
|
|
|
|
int* af_cpu_speed = NULL;
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
/* Find a filter in the static list of filters using it's name. This
|
|
|
|
function is used internally */
|
|
|
|
af_info_t* af_find(char*name)
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
while(filter_list[i]){
|
|
|
|
if(!strcmp(filter_list[i]->name,name))
|
|
|
|
return filter_list[i];
|
|
|
|
i++;
|
|
|
|
}
|
2002-11-12 12:33:56 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"Couldn't find audio filter '%s'\n",name);
|
2002-10-01 06:45:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-06 11:26:14 +00:00
|
|
|
/* Find filter in the dynamic filter list using it's name This
|
|
|
|
function is used for finding already initialized filters */
|
|
|
|
af_instance_t* af_get(af_stream_t* s, char* name)
|
|
|
|
{
|
|
|
|
af_instance_t* af=s->first;
|
|
|
|
// Find the filter
|
|
|
|
while(af != NULL){
|
|
|
|
if(!strcmp(af->info->name,name))
|
|
|
|
return af;
|
|
|
|
af=af->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-31 08:03:51 +00:00
|
|
|
/*/ Function for creating a new filter of type name. The name may
|
|
|
|
contain the commandline parameters for the filter */
|
2002-10-06 11:26:14 +00:00
|
|
|
af_instance_t* af_create(af_stream_t* s, char* name)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2002-10-31 08:03:51 +00:00
|
|
|
char* cmdline = name;
|
2002-10-31 11:06:19 +00:00
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Allocate space for the new filter and reset all pointers
|
|
|
|
af_instance_t* new=malloc(sizeof(af_instance_t));
|
|
|
|
if(!new){
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[libaf] Could not allocate memory\n");
|
2006-03-08 15:39:53 +00:00
|
|
|
goto err_out;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
memset(new,0,sizeof(af_instance_t));
|
|
|
|
|
2002-10-31 08:03:51 +00:00
|
|
|
// Check for commandline parameters
|
2004-06-25 19:02:53 +00:00
|
|
|
strsep(&cmdline, "=");
|
2002-10-31 08:03:51 +00:00
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Find filter from name
|
2002-10-06 11:26:14 +00:00
|
|
|
if(NULL == (new->info=af_find(name)))
|
2006-03-08 15:39:53 +00:00
|
|
|
goto err_out;
|
2002-10-06 11:26:14 +00:00
|
|
|
|
2002-10-31 08:03:51 +00:00
|
|
|
/* Make sure that the filter is not already in the list if it is
|
|
|
|
non-reentrant */
|
2002-10-06 11:26:14 +00:00
|
|
|
if(new->info->flags & AF_FLAGS_NOT_REENTRANT){
|
|
|
|
if(af_get(s,name)){
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[libaf] There can only be one instance of"
|
|
|
|
" the filter '%s' in each stream\n",name);
|
2006-03-08 15:39:53 +00:00
|
|
|
goto err_out;
|
2002-10-06 11:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-31 08:03:51 +00:00
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_VERBOSE,"[libaf] Adding filter %s \n",name);
|
2002-10-31 08:03:51 +00:00
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Initialize the new filter
|
2002-10-16 01:49:40 +00:00
|
|
|
if(AF_OK == new->info->open(new) &&
|
2002-10-31 08:03:51 +00:00
|
|
|
AF_ERROR < new->control(new,AF_CONTROL_POST_CREATE,&s->cfg)){
|
|
|
|
if(cmdline){
|
|
|
|
if(AF_ERROR<new->control(new,AF_CONTROL_COMMAND_LINE,cmdline))
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new;
|
|
|
|
}
|
2002-10-16 01:49:40 +00:00
|
|
|
|
2006-03-08 15:39:53 +00:00
|
|
|
err_out:
|
2002-10-01 06:45:08 +00:00
|
|
|
free(new);
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[libaf] Couldn't create or open audio filter '%s'\n",
|
|
|
|
name);
|
2002-10-01 06:45:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and insert a new filter of type name before the filter in the
|
|
|
|
argument. This function can be called during runtime, the return
|
|
|
|
value is the new filter */
|
2002-10-01 12:53:30 +00:00
|
|
|
af_instance_t* af_prepend(af_stream_t* s, af_instance_t* af, char* name)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2002-10-01 12:53:30 +00:00
|
|
|
// Create the new filter and make sure it is OK
|
2002-10-06 11:26:14 +00:00
|
|
|
af_instance_t* new=af_create(s,name);
|
2002-10-01 06:45:08 +00:00
|
|
|
if(!new)
|
|
|
|
return NULL;
|
|
|
|
// Update pointers
|
|
|
|
new->next=af;
|
|
|
|
if(af){
|
|
|
|
new->prev=af->prev;
|
|
|
|
af->prev=new;
|
|
|
|
}
|
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
s->last=new;
|
2002-10-01 06:45:08 +00:00
|
|
|
if(new->prev)
|
|
|
|
new->prev->next=new;
|
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
s->first=new;
|
2002-10-01 06:45:08 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create and insert a new filter of type name after the filter in the
|
|
|
|
argument. This function can be called during runtime, the return
|
|
|
|
value is the new filter */
|
2002-10-01 12:53:30 +00:00
|
|
|
af_instance_t* af_append(af_stream_t* s, af_instance_t* af, char* name)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
|
|
|
// Create the new filter and make sure it is OK
|
2002-10-06 11:26:14 +00:00
|
|
|
af_instance_t* new=af_create(s,name);
|
2002-10-01 06:45:08 +00:00
|
|
|
if(!new)
|
|
|
|
return NULL;
|
|
|
|
// Update pointers
|
|
|
|
new->prev=af;
|
|
|
|
if(af){
|
|
|
|
new->next=af->next;
|
|
|
|
af->next=new;
|
|
|
|
}
|
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
s->first=new;
|
2002-10-01 06:45:08 +00:00
|
|
|
if(new->next)
|
|
|
|
new->next->prev=new;
|
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
s->last=new;
|
2002-10-01 06:45:08 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninit and remove the filter "af"
|
2002-10-01 12:53:30 +00:00
|
|
|
void af_remove(af_stream_t* s, af_instance_t* af)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
|
|
|
if(!af) return;
|
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
// Print friendly message
|
|
|
|
af_msg(AF_MSG_VERBOSE,"[libaf] Removing filter %s \n",af->info->name);
|
|
|
|
|
2002-10-16 01:49:40 +00:00
|
|
|
// Notify filter before changing anything
|
|
|
|
af->control(af,AF_CONTROL_PRE_DESTROY,0);
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Detach pointers
|
|
|
|
if(af->prev)
|
|
|
|
af->prev->next=af->next;
|
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
s->first=af->next;
|
2002-10-01 06:45:08 +00:00
|
|
|
if(af->next)
|
|
|
|
af->next->prev=af->prev;
|
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
s->last=af->prev;
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
// Uninitialize af and free memory
|
|
|
|
af->uninit(af);
|
|
|
|
free(af);
|
|
|
|
}
|
|
|
|
|
2002-10-07 10:46:01 +00:00
|
|
|
/* Reinitializes all filters downstream from the filter given in the
|
|
|
|
argument the return value is AF_OK if success and AF_ERROR if
|
|
|
|
failure */
|
2002-10-01 12:53:30 +00:00
|
|
|
int af_reinit(af_stream_t* s, af_instance_t* af)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
|
|
|
do{
|
|
|
|
af_data_t in; // Format of the input to current filter
|
|
|
|
int rv=0; // Return value
|
|
|
|
|
2005-04-16 16:48:15 +00:00
|
|
|
// Check if there are any filters left in the list
|
|
|
|
if(NULL == af){
|
|
|
|
if(!(af=af_append(s,s->first,"dummy")))
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
else
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Check if this is the first filter
|
|
|
|
if(!af->prev)
|
2002-10-01 12:53:30 +00:00
|
|
|
memcpy(&in,&(s->input),sizeof(af_data_t));
|
2002-10-01 06:45:08 +00:00
|
|
|
else
|
|
|
|
memcpy(&in,af->prev->data,sizeof(af_data_t));
|
|
|
|
// Reset just in case...
|
|
|
|
in.audio=NULL;
|
|
|
|
in.len=0;
|
|
|
|
|
|
|
|
rv = af->control(af,AF_CONTROL_REINIT,&in);
|
|
|
|
switch(rv){
|
|
|
|
case AF_OK:
|
2005-04-16 16:48:15 +00:00
|
|
|
af = af->next;
|
2002-10-01 06:45:08 +00:00
|
|
|
break;
|
|
|
|
case AF_FALSE:{ // Configuration filter is needed
|
2002-11-12 12:33:56 +00:00
|
|
|
// Do auto insertion only if force is not specified
|
|
|
|
if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){
|
|
|
|
af_instance_t* new = NULL;
|
|
|
|
// Insert channels filter
|
|
|
|
if((af->prev?af->prev->data->nch:s->input.nch) != in.nch){
|
|
|
|
// Create channels filter
|
|
|
|
if(NULL == (new = af_prepend(s,af,"channels")))
|
|
|
|
return AF_ERROR;
|
|
|
|
// Set number of output channels
|
|
|
|
if(AF_OK != (rv = new->control(new,AF_CONTROL_CHANNELS,&in.nch)))
|
|
|
|
return rv;
|
|
|
|
// Initialize channels filter
|
|
|
|
if(!new->prev)
|
|
|
|
memcpy(&in,&(s->input),sizeof(af_data_t));
|
|
|
|
else
|
|
|
|
memcpy(&in,new->prev->data,sizeof(af_data_t));
|
|
|
|
if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in)))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// Insert format filter
|
2005-02-25 11:11:07 +00:00
|
|
|
if((af->prev?af->prev->data->format:s->input.format) != in.format){
|
2002-11-12 12:33:56 +00:00
|
|
|
// Create format filter
|
|
|
|
if(NULL == (new = af_prepend(s,af,"format")))
|
|
|
|
return AF_ERROR;
|
2002-12-28 13:59:53 +00:00
|
|
|
// Set output bits per sample
|
2005-01-03 18:59:16 +00:00
|
|
|
in.format |= af_bits2fmt(in.bps*8);
|
|
|
|
if(AF_OK != (rv = new->control(new,AF_CONTROL_FORMAT_FMT,&in.format)))
|
2002-11-12 12:33:56 +00:00
|
|
|
return rv;
|
|
|
|
// Initialize format filter
|
|
|
|
if(!new->prev)
|
|
|
|
memcpy(&in,&(s->input),sizeof(af_data_t));
|
|
|
|
else
|
|
|
|
memcpy(&in,new->prev->data,sizeof(af_data_t));
|
|
|
|
if(AF_OK != (rv = new->control(new,AF_CONTROL_REINIT,&in)))
|
|
|
|
return rv;
|
|
|
|
}
|
2002-12-28 13:59:53 +00:00
|
|
|
if(!new){ // Should _never_ happen
|
|
|
|
af_msg(AF_MSG_ERROR,"[libaf] Unable to correct audio format. "
|
|
|
|
"This error should never uccur, please send bugreport.\n");
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_ERROR;
|
2002-12-28 13:59:53 +00:00
|
|
|
}
|
2005-04-16 16:48:15 +00:00
|
|
|
af=new->next;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
2005-07-23 16:58:25 +00:00
|
|
|
else {
|
|
|
|
af_msg(AF_MSG_ERROR, "[libaf] Automatic filter insertion disabled "
|
|
|
|
"but formats do not match. Giving up.\n");
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_DETACH:{ // Filter is redundant and wants to be unloaded
|
2002-11-12 12:33:56 +00:00
|
|
|
// Do auto remove only if force is not specified
|
|
|
|
if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){
|
|
|
|
af_instance_t* aft=af->prev;
|
|
|
|
af_remove(s,af);
|
|
|
|
if(aft)
|
2005-04-16 16:48:15 +00:00
|
|
|
af=aft->next;
|
2002-11-12 12:33:56 +00:00
|
|
|
else
|
|
|
|
af=s->first; // Restart configuration
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[libaf] Reinitialization did not work, audio"
|
|
|
|
" filter '%s' returned error code %i\n",af->info->name,rv);
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
}while(af);
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninit and remove all filters
|
2002-10-01 12:53:30 +00:00
|
|
|
void af_uninit(af_stream_t* s)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2002-10-01 12:53:30 +00:00
|
|
|
while(s->first)
|
|
|
|
af_remove(s,s->first);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2002-10-01 12:53:30 +00:00
|
|
|
/* Initialize the stream "s". This function creates a new filter list
|
|
|
|
if necessary according to the values set in input and output. Input
|
|
|
|
and output should contain the format of the current movie and the
|
|
|
|
formate of the preferred output respectively. The function is
|
|
|
|
reentrant i.e. if called with an already initialized stream the
|
2005-06-24 10:50:53 +00:00
|
|
|
stream will be reinitialized.
|
|
|
|
If one of the prefered output parameters is 0 the one that needs
|
|
|
|
no conversion is used (i.e. the output format in the last filter).
|
|
|
|
The return value is 0 if success and -1 if failure */
|
|
|
|
int af_init(af_stream_t* s)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
|
2002-10-01 12:53:30 +00:00
|
|
|
// Sanity check
|
|
|
|
if(!s) return -1;
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Precaution in case caller is misbehaving
|
2002-10-01 12:53:30 +00:00
|
|
|
s->input.audio = s->output.audio = NULL;
|
|
|
|
s->input.len = s->output.len = 0;
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
// Figure out how fast the machine is
|
2002-11-12 12:33:56 +00:00
|
|
|
if(AF_INIT_AUTO == (AF_INIT_TYPE_MASK & s->cfg.force))
|
|
|
|
s->cfg.force = (s->cfg.force & ~AF_INIT_TYPE_MASK) | AF_INIT_TYPE;
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
// Check if this is the first call
|
2002-10-01 12:53:30 +00:00
|
|
|
if(!s->first){
|
2002-10-01 06:45:08 +00:00
|
|
|
// Add all filters in the list (if there are any)
|
2002-10-01 12:53:30 +00:00
|
|
|
if(!s->cfg.list){ // To make automatic format conversion work
|
|
|
|
if(!af_append(s,s->first,"dummy"))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else{
|
2002-10-01 12:53:30 +00:00
|
|
|
while(s->cfg.list[i]){
|
|
|
|
if(!af_append(s,s->last,s->cfg.list[i++]))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-28 13:59:53 +00:00
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Init filters
|
2002-10-01 12:53:30 +00:00
|
|
|
if(AF_OK != af_reinit(s,s->first))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
|
2005-05-01 09:23:31 +00:00
|
|
|
// make sure the chain is not empty and valid (e.g. because of AF_DETACH)
|
|
|
|
if (!s->first)
|
|
|
|
if (!af_append(s,s->first,"dummy") || AF_OK != af_reinit(s,s->first))
|
|
|
|
return -1;
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Check output format
|
2002-10-16 01:49:40 +00:00
|
|
|
if((AF_INIT_TYPE_MASK & s->cfg.force) != AF_INIT_FORCE){
|
2002-10-01 06:45:08 +00:00
|
|
|
af_instance_t* af = NULL; // New filter
|
|
|
|
// Check output frequency if not OK fix with resample
|
2005-06-24 10:50:53 +00:00
|
|
|
if(s->output.rate && s->last->data->rate!=s->output.rate){
|
2005-01-03 14:05:38 +00:00
|
|
|
// try to find a filter that can change samplrate
|
|
|
|
af = af_control_any_rev(s, AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET,
|
|
|
|
&(s->output.rate));
|
|
|
|
if (!af) {
|
|
|
|
char *resampler = "resample";
|
|
|
|
#ifdef USE_LIBAVCODEC
|
|
|
|
if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW)
|
|
|
|
resampler = "lavcresample";
|
|
|
|
#endif
|
2002-10-16 01:49:40 +00:00
|
|
|
if((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_SLOW){
|
2002-10-01 12:53:30 +00:00
|
|
|
if(!strcmp(s->first->info->name,"format"))
|
2005-01-03 14:05:38 +00:00
|
|
|
af = af_append(s,s->first,resampler);
|
2002-10-01 06:45:08 +00:00
|
|
|
else
|
2005-01-03 14:05:38 +00:00
|
|
|
af = af_prepend(s,s->first,resampler);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
else{
|
2002-10-01 12:53:30 +00:00
|
|
|
if(!strcmp(s->last->info->name,"format"))
|
2005-01-03 14:05:38 +00:00
|
|
|
af = af_prepend(s,s->last,resampler);
|
2002-10-01 06:45:08 +00:00
|
|
|
else
|
2005-01-03 14:05:38 +00:00
|
|
|
af = af_append(s,s->last,resampler);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
// Init the new filter
|
2005-04-16 16:48:15 +00:00
|
|
|
if(!af || (AF_OK != af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET,
|
2002-12-28 13:59:53 +00:00
|
|
|
&(s->output.rate))))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
2004-01-27 06:45:43 +00:00
|
|
|
// Use lin int if the user wants fast
|
|
|
|
if ((AF_INIT_TYPE_MASK & s->cfg.force) == AF_INIT_FAST) {
|
|
|
|
char args[32];
|
2005-01-03 14:05:38 +00:00
|
|
|
sprintf(args, "%d", s->output.rate);
|
|
|
|
#ifdef USE_LIBAVCODEC
|
|
|
|
if (strcmp(resampler, "lavcresample") == 0)
|
|
|
|
strcat(args, ":1");
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
strcat(args, ":0:0");
|
2004-01-27 06:45:43 +00:00
|
|
|
af->control(af, AF_CONTROL_COMMAND_LINE, args);
|
|
|
|
}
|
2005-01-03 14:05:38 +00:00
|
|
|
}
|
2002-10-01 12:53:30 +00:00
|
|
|
if(AF_OK != af_reinit(s,af))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check number of output channels fix if not OK
|
|
|
|
// If needed always inserted last -> easy to screw up other filters
|
2005-06-24 10:50:53 +00:00
|
|
|
if(s->output.nch && s->last->data->nch!=s->output.nch){
|
2002-10-01 12:53:30 +00:00
|
|
|
if(!strcmp(s->last->info->name,"format"))
|
|
|
|
af = af_prepend(s,s->last,"channels");
|
2002-10-01 06:45:08 +00:00
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
af = af_append(s,s->last,"channels");
|
2002-10-01 06:45:08 +00:00
|
|
|
// Init the new filter
|
2002-10-01 12:53:30 +00:00
|
|
|
if(!af || (AF_OK != af->control(af,AF_CONTROL_CHANNELS,&(s->output.nch))))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
2002-10-01 12:53:30 +00:00
|
|
|
if(AF_OK != af_reinit(s,af))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check output format fix if not OK
|
2005-06-24 10:50:53 +00:00
|
|
|
if(s->output.format && s->last->data->format != s->output.format){
|
2002-10-01 12:53:30 +00:00
|
|
|
if(strcmp(s->last->info->name,"format"))
|
|
|
|
af = af_append(s,s->last,"format");
|
2002-10-01 06:45:08 +00:00
|
|
|
else
|
2002-10-01 12:53:30 +00:00
|
|
|
af = s->last;
|
2002-10-01 06:45:08 +00:00
|
|
|
// Init the new filter
|
2005-01-03 18:59:16 +00:00
|
|
|
s->output.format |= af_bits2fmt(s->output.bps*8);
|
|
|
|
if(!af || (AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT,&(s->output.format))))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
2002-10-01 12:53:30 +00:00
|
|
|
if(AF_OK != af_reinit(s,af))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re init again just in case
|
2002-10-01 12:53:30 +00:00
|
|
|
if(AF_OK != af_reinit(s,s->first))
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
|
2005-06-24 10:50:53 +00:00
|
|
|
if (!s->output.format) s->output.format = s->last->data->format;
|
|
|
|
if (!s->output.nch) s->output.nch = s->last->data->nch;
|
|
|
|
if (!s->output.rate) s->output.rate = s->last->data->rate;
|
2002-10-01 12:53:30 +00:00
|
|
|
if((s->last->data->format != s->output.format) ||
|
|
|
|
(s->last->data->nch != s->output.nch) ||
|
|
|
|
(s->last->data->rate != s->output.rate)) {
|
2002-10-01 06:45:08 +00:00
|
|
|
// Something is stuffed audio out will not work
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[libaf] Unable to setup filter system can not"
|
|
|
|
" meet sound-card demands, please send bugreport. \n");
|
2002-10-01 12:53:30 +00:00
|
|
|
af_uninit(s);
|
2002-10-01 06:45:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-07 10:46:01 +00:00
|
|
|
/* Add filter during execution. This function adds the filter "name"
|
|
|
|
to the stream s. The filter will be inserted somewhere nice in the
|
|
|
|
list of filters. The return value is a pointer to the new filter,
|
|
|
|
If the filter couldn't be added the return value is NULL. */
|
|
|
|
af_instance_t* af_add(af_stream_t* s, char* name){
|
|
|
|
af_instance_t* new;
|
|
|
|
// Sanity check
|
|
|
|
if(!s || !s->first || !name)
|
|
|
|
return NULL;
|
|
|
|
// Insert the filter somwhere nice
|
|
|
|
if(!strcmp(s->first->info->name,"format"))
|
|
|
|
new = af_append(s, s->first, name);
|
|
|
|
else
|
|
|
|
new = af_prepend(s, s->first, name);
|
|
|
|
if(!new)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Reinitalize the filter list
|
|
|
|
if(AF_OK != af_reinit(s, s->first)){
|
|
|
|
free(new);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Filter data chunk through the filters in the list
|
2002-10-01 12:53:30 +00:00
|
|
|
af_data_t* af_play(af_stream_t* s, af_data_t* data)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2002-10-01 12:53:30 +00:00
|
|
|
af_instance_t* af=s->first;
|
2002-10-01 06:45:08 +00:00
|
|
|
// Iterate through all filters
|
|
|
|
do{
|
2005-10-20 09:12:28 +00:00
|
|
|
if (data->len <= 0) break;
|
2002-10-01 06:45:08 +00:00
|
|
|
data=af->play(af,data);
|
|
|
|
af=af->next;
|
|
|
|
}while(af);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function used to calculate the exact buffer length needed
|
2002-10-03 12:02:46 +00:00
|
|
|
when buffers are resized. The returned length is >= than what is
|
|
|
|
needed */
|
|
|
|
inline int af_lencalc(frac_t mul, af_data_t* d){
|
|
|
|
register int t = d->bps*d->nch;
|
2002-10-03 12:43:39 +00:00
|
|
|
return t*(((d->len/t)*mul.n)/mul.d + 1);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate how long the output from the filters will be given the
|
2002-10-03 12:02:46 +00:00
|
|
|
input length "len". The calculated length is >= the actual
|
|
|
|
length. */
|
2002-10-01 12:53:30 +00:00
|
|
|
int af_outputlen(af_stream_t* s, int len)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2002-10-03 12:02:46 +00:00
|
|
|
int t = s->input.bps*s->input.nch;
|
2002-10-01 12:53:30 +00:00
|
|
|
af_instance_t* af=s->first;
|
2005-01-22 10:47:58 +00:00
|
|
|
frac_t mul = {1,1};
|
2002-10-01 06:45:08 +00:00
|
|
|
// Iterate through all filters
|
|
|
|
do{
|
2005-01-08 21:34:06 +00:00
|
|
|
af_frac_mul(&mul, &af->mul);
|
2002-10-01 06:45:08 +00:00
|
|
|
af=af->next;
|
|
|
|
}while(af);
|
2002-10-03 12:02:46 +00:00
|
|
|
return t * (((len/t)*mul.n + 1)/mul.d);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate how long the input to the filters should be to produce a
|
|
|
|
certain output length, i.e. the return value of this function is
|
2002-10-03 12:02:46 +00:00
|
|
|
the input length required to produce the output length "len". The
|
|
|
|
calculated length is <= the actual length */
|
2002-10-01 12:53:30 +00:00
|
|
|
int af_inputlen(af_stream_t* s, int len)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2002-10-03 12:02:46 +00:00
|
|
|
int t = s->input.bps*s->input.nch;
|
2002-10-01 12:53:30 +00:00
|
|
|
af_instance_t* af=s->first;
|
2005-01-22 10:47:58 +00:00
|
|
|
frac_t mul = {1,1};
|
2002-10-01 06:45:08 +00:00
|
|
|
// Iterate through all filters
|
|
|
|
do{
|
2005-01-08 21:34:06 +00:00
|
|
|
af_frac_mul(&mul, &af->mul);
|
2002-10-01 06:45:08 +00:00
|
|
|
af=af->next;
|
|
|
|
}while(af);
|
2002-10-03 12:02:46 +00:00
|
|
|
return t * (((len/t) * mul.d - 1)/mul.n);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
2002-10-05 11:02:39 +00:00
|
|
|
/* Calculate how long the input IN to the filters should be to produce
|
|
|
|
a certain output length OUT but with the following three constraints:
|
|
|
|
1. IN <= max_insize, where max_insize is the maximum possible input
|
|
|
|
block length
|
|
|
|
2. OUT <= max_outsize, where max_outsize is the maximum possible
|
|
|
|
output block length
|
|
|
|
3. If possible OUT >= len.
|
|
|
|
Return -1 in case of error */
|
|
|
|
int af_calc_insize_constrained(af_stream_t* s, int len,
|
|
|
|
int max_outsize,int max_insize)
|
|
|
|
{
|
|
|
|
int t = s->input.bps*s->input.nch;
|
|
|
|
int in = 0;
|
|
|
|
int out = 0;
|
|
|
|
af_instance_t* af=s->first;
|
2005-01-22 10:47:58 +00:00
|
|
|
frac_t mul = {1,1};
|
2002-10-05 11:02:39 +00:00
|
|
|
// Iterate through all filters and calculate total multiplication factor
|
|
|
|
do{
|
2005-01-08 21:34:06 +00:00
|
|
|
af_frac_mul(&mul, &af->mul);
|
2002-10-05 11:02:39 +00:00
|
|
|
af=af->next;
|
|
|
|
}while(af);
|
2002-12-28 13:59:53 +00:00
|
|
|
// Sanity check
|
|
|
|
if(!mul.n || !mul.d)
|
|
|
|
return -1;
|
|
|
|
|
2002-10-05 22:54:11 +00:00
|
|
|
in = t * (((len/t) * mul.d - 1)/mul.n);
|
|
|
|
|
|
|
|
if(in>max_insize) in=t*(max_insize/t);
|
2002-10-05 11:02:39 +00:00
|
|
|
|
|
|
|
// Try to meet constraint nr 3.
|
2002-10-06 10:42:24 +00:00
|
|
|
while((out=t * (((in/t+1)*mul.n - 1)/mul.d)) <= max_outsize && in<=max_insize){
|
2002-10-05 22:54:11 +00:00
|
|
|
if( (t * (((in/t)*mul.n))/mul.d) >= len) return in;
|
2002-10-05 11:02:39 +00:00
|
|
|
in+=t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Could no meet constraint nr 3.
|
2002-10-05 22:54:11 +00:00
|
|
|
while(out > max_outsize || in > max_insize){
|
2002-10-05 11:02:39 +00:00
|
|
|
in-=t;
|
2002-10-05 22:54:11 +00:00
|
|
|
if(in<t) return -1; // Input parameters are probably incorrect
|
2002-10-05 11:02:39 +00:00
|
|
|
out = t * (((in/t)*mul.n + 1)/mul.d);
|
|
|
|
}
|
2002-10-05 22:54:11 +00:00
|
|
|
return in;
|
2002-10-05 11:02:39 +00:00
|
|
|
}
|
|
|
|
|
2002-10-08 10:20:36 +00:00
|
|
|
/* Calculate the total delay [ms] caused by the filters */
|
|
|
|
double af_calc_delay(af_stream_t* s)
|
|
|
|
{
|
|
|
|
af_instance_t* af=s->first;
|
|
|
|
register double delay = 0.0;
|
|
|
|
// Iterate through all filters
|
|
|
|
while(af){
|
|
|
|
delay += af->delay;
|
|
|
|
af=af->next;
|
|
|
|
}
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
/* Helper function called by the macro with the same name this
|
|
|
|
function should not be called directly */
|
|
|
|
inline int af_resize_local_buffer(af_instance_t* af, af_data_t* data)
|
|
|
|
{
|
|
|
|
// Calculate new length
|
2002-10-03 12:02:46 +00:00
|
|
|
register int len = af_lencalc(af->mul,data);
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_VERBOSE,"[libaf] Reallocating memory in module %s, "
|
|
|
|
"old len = %i, new len = %i\n",af->info->name,af->data->len,len);
|
2002-10-01 06:45:08 +00:00
|
|
|
// If there is a buffer free it
|
|
|
|
if(af->data->audio)
|
|
|
|
free(af->data->audio);
|
|
|
|
// Create new buffer and check that it is OK
|
|
|
|
af->data->audio = malloc(len);
|
|
|
|
if(!af->data->audio){
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_FATAL,"[libaf] Could not allocate memory \n");
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
af->data->len=len;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
2004-06-25 19:02:53 +00:00
|
|
|
|
2005-10-01 12:55:34 +00:00
|
|
|
// documentation in af.h
|
2005-01-01 18:46:56 +00:00
|
|
|
af_instance_t *af_control_any_rev (af_stream_t* s, int cmd, void* arg) {
|
2004-06-25 19:02:53 +00:00
|
|
|
int res = AF_UNKNOWN;
|
|
|
|
af_instance_t* filt = s->last;
|
2005-01-01 18:46:56 +00:00
|
|
|
while (filt) {
|
2004-06-25 19:02:53 +00:00
|
|
|
res = filt->control(filt, cmd, arg);
|
2005-01-01 18:46:56 +00:00
|
|
|
if (res == AF_OK)
|
|
|
|
return filt;
|
2004-06-25 19:02:53 +00:00
|
|
|
filt = filt->prev;
|
|
|
|
}
|
2005-01-01 18:46:56 +00:00
|
|
|
return NULL;
|
2004-06-25 19:02:53 +00:00
|
|
|
}
|
|
|
|
|
2005-01-08 21:34:06 +00:00
|
|
|
/**
|
|
|
|
* \brief calculate greatest common divisior of a and b.
|
2005-10-01 12:55:34 +00:00
|
|
|
* \ingroup af_filter
|
|
|
|
*
|
2005-01-08 21:34:06 +00:00
|
|
|
* Extended for negative and 0 values. If both are 0 the result is 1.
|
|
|
|
* The sign of the result will be so that it has the same sign as b.
|
|
|
|
*/
|
|
|
|
int af_gcd(register int a, register int b) {
|
|
|
|
int b_org = b;
|
|
|
|
while (b != 0) {
|
|
|
|
a %= b;
|
|
|
|
if (a == 0)
|
|
|
|
break;
|
|
|
|
b %= a;
|
|
|
|
}
|
|
|
|
// the result is either in a or b. As the other one is 0 just add them.
|
|
|
|
a += b;
|
|
|
|
if (!a)
|
|
|
|
return 1;
|
|
|
|
if (a * b_org < 0)
|
|
|
|
return -a;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief cancel down a fraction f
|
2005-10-01 12:55:34 +00:00
|
|
|
* \param f fraction to cancel down
|
|
|
|
* \ingroup af_filter
|
2005-01-08 21:34:06 +00:00
|
|
|
*/
|
|
|
|
void af_frac_cancel(frac_t *f) {
|
|
|
|
int gcd = af_gcd(f->n, f->d);
|
|
|
|
f->n /= gcd;
|
|
|
|
f->d /= gcd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief multiply out by in and store result in out.
|
2005-10-01 12:55:34 +00:00
|
|
|
* \param out [inout] fraction to multiply by in
|
|
|
|
* \param in [in] fraction to multiply out by
|
|
|
|
* \ingroup af_filter
|
|
|
|
*
|
|
|
|
* the resulting fraction will be cancelled down
|
2005-01-08 21:34:06 +00:00
|
|
|
* if in and out were.
|
|
|
|
*/
|
|
|
|
void af_frac_mul(frac_t *out, const frac_t *in) {
|
|
|
|
int gcd1 = af_gcd(out->n, in->d);
|
|
|
|
int gcd2 = af_gcd(in->n, out->d);
|
|
|
|
out->n = (out->n / gcd1) * (in->n / gcd2);
|
|
|
|
out->d = (out->d / gcd2) * (in->d / gcd1);
|
|
|
|
}
|
|
|
|
|
2004-09-06 22:27:08 +00:00
|
|
|
void af_help (void) {
|
|
|
|
int i = 0;
|
2004-10-05 19:36:47 +00:00
|
|
|
af_msg(AF_MSG_INFO, "Available audio filters:\n");
|
2004-09-06 22:27:08 +00:00
|
|
|
while (filter_list[i]) {
|
2004-10-05 19:36:47 +00:00
|
|
|
if (filter_list[i]->comment && filter_list[i]->comment[0])
|
|
|
|
af_msg(AF_MSG_INFO, " %-15s: %s (%s)\n", filter_list[i]->name, filter_list[i]->info, filter_list[i]->comment);
|
|
|
|
else
|
|
|
|
af_msg(AF_MSG_INFO, " %-15s: %s\n", filter_list[i]->name, filter_list[i]->info);
|
2004-09-06 22:27:08 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 11:11:07 +00:00
|
|
|
void af_fix_parameters(af_data_t *data)
|
|
|
|
{
|
|
|
|
data->bps = af_fmt2bits(data->format)/8;
|
|
|
|
}
|