2002-10-01 06:45:08 +00:00
|
|
|
/* Audio filter that adds and removes channels, according to the
|
|
|
|
command line parameter channels. It is stupid and can only add
|
|
|
|
silence or copy channels not mix or filter.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-10-30 09:05:02 +00:00
|
|
|
#include <inttypes.h>
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
#include "af.h"
|
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
#define FR 0
|
|
|
|
#define TO 1
|
|
|
|
|
|
|
|
typedef struct af_channels_s{
|
|
|
|
int route[AF_NCH][2];
|
|
|
|
int nr;
|
|
|
|
int router;
|
|
|
|
}af_channels_t;
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Local function for copying data
|
2003-01-02 03:00:44 +00:00
|
|
|
static void copy(void* in, void* out, int ins, int inos,int outs, int outos, int len, int bps)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
|
|
|
switch(bps){
|
|
|
|
case 1:{
|
|
|
|
int8_t* tin = (int8_t*)in;
|
|
|
|
int8_t* tout = (int8_t*)out;
|
|
|
|
tin += inos;
|
|
|
|
tout += outos;
|
|
|
|
len = len/ins;
|
|
|
|
while(len--){
|
|
|
|
*tout=*tin;
|
|
|
|
tin +=ins;
|
|
|
|
tout+=outs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:{
|
|
|
|
int16_t* tin = (int16_t*)in;
|
|
|
|
int16_t* tout = (int16_t*)out;
|
|
|
|
tin += inos;
|
|
|
|
tout += outos;
|
|
|
|
len = len/(2*ins);
|
|
|
|
while(len--){
|
|
|
|
*tout=*tin;
|
|
|
|
tin +=ins;
|
|
|
|
tout+=outs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-07-29 16:23:16 +00:00
|
|
|
case 3:{
|
|
|
|
int8_t* tin = (int8_t*)in;
|
|
|
|
int8_t* tout = (int8_t*)out;
|
|
|
|
tin += 3 * inos;
|
|
|
|
tout += 3 * outos;
|
|
|
|
len = len / ( 3 * ins);
|
|
|
|
while (len--) {
|
|
|
|
tout[0] = tin[0];
|
|
|
|
tout[1] = tin[1];
|
|
|
|
tout[2] = tin[2];
|
|
|
|
tin += 3 * ins;
|
|
|
|
tout += 3 * outs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
case 4:{
|
|
|
|
int32_t* tin = (int32_t*)in;
|
|
|
|
int32_t* tout = (int32_t*)out;
|
|
|
|
tin += inos;
|
|
|
|
tout += outos;
|
|
|
|
len = len/(4*ins);
|
|
|
|
while(len--){
|
|
|
|
*tout=*tin;
|
|
|
|
tin +=ins;
|
|
|
|
tout+=outs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 8:{
|
|
|
|
int64_t* tin = (int64_t*)in;
|
|
|
|
int64_t* tout = (int64_t*)out;
|
|
|
|
tin += inos;
|
|
|
|
tout += outos;
|
|
|
|
len = len/(8*ins);
|
|
|
|
while(len--){
|
|
|
|
*tout=*tin;
|
|
|
|
tin +=ins;
|
|
|
|
tout+=outs;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[channels] Unsupported number of bytes/sample: %i"
|
|
|
|
" please report this error on the MPlayer mailing list. \n",bps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the routes are sane
|
|
|
|
static int check_routes(af_channels_t* s, int nin, int nout)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if((s->nr < 1) || (s->nr > AF_NCH)){
|
2003-01-02 03:00:44 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[channels] The number of routing pairs must be"
|
2002-12-28 13:59:53 +00:00
|
|
|
" between 1 and %i. Current value is %i\n",AF_NCH,s->nr);
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0;i<s->nr;i++){
|
|
|
|
if((s->route[i][FR] >= nin) || (s->route[i][TO] >= nout)){
|
|
|
|
af_msg(AF_MSG_ERROR,"[channels] Invalid routing in pair nr. %i.\n", i);
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
2002-12-28 13:59:53 +00:00
|
|
|
return AF_OK;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialization and runtime control
|
|
|
|
static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
|
|
{
|
2002-12-28 13:59:53 +00:00
|
|
|
af_channels_t* s = af->setup;
|
2002-10-01 06:45:08 +00:00
|
|
|
switch(cmd){
|
|
|
|
case AF_CONTROL_REINIT:
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
// Set default channel assignment
|
|
|
|
if(!s->router){
|
|
|
|
int i;
|
|
|
|
// Make sure this filter isn't redundant
|
|
|
|
if(af->data->nch == ((af_data_t*)arg)->nch)
|
|
|
|
return AF_DETACH;
|
|
|
|
|
|
|
|
// If mono: fake stereo
|
|
|
|
if(((af_data_t*)arg)->nch == 1){
|
|
|
|
s->nr = min(af->data->nch,2);
|
|
|
|
for(i=0;i<s->nr;i++){
|
|
|
|
s->route[i][FR] = 0;
|
|
|
|
s->route[i][TO] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
s->nr = min(af->data->nch, ((af_data_t*)arg)->nch);
|
|
|
|
for(i=0;i<s->nr;i++){
|
|
|
|
s->route[i][FR] = i;
|
|
|
|
s->route[i][TO] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
af->data->rate = ((af_data_t*)arg)->rate;
|
|
|
|
af->data->format = ((af_data_t*)arg)->format;
|
|
|
|
af->data->bps = ((af_data_t*)arg)->bps;
|
|
|
|
af->mul.n = af->data->nch;
|
|
|
|
af->mul.d = ((af_data_t*)arg)->nch;
|
2005-01-08 21:34:06 +00:00
|
|
|
af_frac_cancel(&af->mul);
|
2002-12-28 13:59:53 +00:00
|
|
|
return check_routes(s,((af_data_t*)arg)->nch,af->data->nch);
|
2002-10-31 11:06:19 +00:00
|
|
|
case AF_CONTROL_COMMAND_LINE:{
|
|
|
|
int nch = 0;
|
2002-12-28 13:59:53 +00:00
|
|
|
int n = 0;
|
|
|
|
// Check number of channels and number of routing pairs
|
|
|
|
sscanf(arg, "%i:%i%n", &nch, &s->nr, &n);
|
|
|
|
|
|
|
|
// If router scan commandline for routing pairs
|
|
|
|
if(s->nr){
|
|
|
|
char* cp = &((char*)arg)[n];
|
|
|
|
int ch = 0;
|
|
|
|
// Sanity check
|
|
|
|
if((s->nr < 1) || (s->nr > AF_NCH)){
|
2003-01-02 03:00:44 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[channels] The number of routing pairs must be"
|
2002-12-28 13:59:53 +00:00
|
|
|
" between 1 and %i. Current value is %i\n",AF_NCH,s->nr);
|
|
|
|
}
|
|
|
|
s->router = 1;
|
|
|
|
// Scan for pairs on commandline
|
|
|
|
while((*cp == ':') && (ch < s->nr)){
|
|
|
|
sscanf(cp, ":%i:%i%n" ,&s->route[ch][FR], &s->route[ch][TO], &n);
|
2003-01-02 03:00:44 +00:00
|
|
|
af_msg(AF_MSG_VERBOSE,"[channels] Routing from channel %i to"
|
2002-12-28 13:59:53 +00:00
|
|
|
" channel %i\n",s->route[ch][FR],s->route[ch][TO]);
|
|
|
|
cp = &cp[n];
|
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(AF_OK != af->control(af,AF_CONTROL_CHANNELS | AF_CONTROL_SET ,&nch))
|
|
|
|
return AF_ERROR;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_CHANNELS | AF_CONTROL_SET:
|
2002-10-01 06:45:08 +00:00
|
|
|
// Reinit must be called after this function has been called
|
|
|
|
|
|
|
|
// Sanity check
|
2004-03-04 19:39:16 +00:00
|
|
|
if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){
|
2002-12-28 13:59:53 +00:00
|
|
|
af_msg(AF_MSG_ERROR,"[channels] The number of output channels must be"
|
|
|
|
" between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
af->data->nch=((int*)arg)[0];
|
2002-12-28 13:59:53 +00:00
|
|
|
if(!s->router)
|
|
|
|
af_msg(AF_MSG_VERBOSE,"[channels] Changing number of channels"
|
|
|
|
" to %i\n",af->data->nch);
|
|
|
|
return AF_OK;
|
|
|
|
case AF_CONTROL_CHANNELS | AF_CONTROL_GET:
|
|
|
|
*(int*)arg = af->data->nch;
|
|
|
|
return AF_OK;
|
|
|
|
case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_SET:{
|
|
|
|
int ch = ((af_control_ext_t*)arg)->ch;
|
|
|
|
int* route = ((af_control_ext_t*)arg)->arg;
|
|
|
|
s->route[ch][FR] = route[FR];
|
|
|
|
s->route[ch][TO] = route[TO];
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_CHANNELS_ROUTING | AF_CONTROL_GET:{
|
|
|
|
int ch = ((af_control_ext_t*)arg)->ch;
|
|
|
|
int* route = ((af_control_ext_t*)arg)->arg;
|
|
|
|
route[FR] = s->route[ch][FR];
|
|
|
|
route[TO] = s->route[ch][TO];
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_CHANNELS_NR | AF_CONTROL_SET:
|
|
|
|
s->nr = *(int*)arg;
|
|
|
|
return AF_OK;
|
|
|
|
case AF_CONTROL_CHANNELS_NR | AF_CONTROL_GET:
|
|
|
|
*(int*)arg = s->nr;
|
|
|
|
return AF_OK;
|
|
|
|
case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_SET:
|
|
|
|
s->router = *(int*)arg;
|
|
|
|
return AF_OK;
|
|
|
|
case AF_CONTROL_CHANNELS_ROUTER | AF_CONTROL_GET:
|
|
|
|
*(int*)arg = s->router;
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deallocate memory
|
|
|
|
static void uninit(struct af_instance_s* af)
|
|
|
|
{
|
2007-02-10 18:12:03 +00:00
|
|
|
free(af->setup);
|
|
|
|
if (af->data)
|
|
|
|
free(af->data->audio);
|
|
|
|
free(af->data);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data through filter
|
|
|
|
static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
|
|
{
|
2002-12-28 13:59:53 +00:00
|
|
|
af_data_t* c = data; // Current working data
|
|
|
|
af_data_t* l = af->data; // Local data
|
|
|
|
af_channels_t* s = af->setup;
|
|
|
|
int i;
|
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
|
|
|
|
return NULL;
|
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
// Reset unused channels
|
|
|
|
memset(l->audio,0,(c->len*af->mul.n)/af->mul.d);
|
2002-10-01 06:45:08 +00:00
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
if(AF_OK == check_routes(s,c->nch,l->nch))
|
|
|
|
for(i=0;i<s->nr;i++)
|
|
|
|
copy(c->audio,l->audio,c->nch,s->route[i][FR],
|
|
|
|
l->nch,s->route[i][TO],c->len,c->bps);
|
2002-10-01 06:45:08 +00:00
|
|
|
|
|
|
|
// Set output data
|
|
|
|
c->audio = l->audio;
|
2002-10-03 12:43:39 +00:00
|
|
|
c->len = (c->len*af->mul.n)/af->mul.d;
|
2002-10-01 06:45:08 +00:00
|
|
|
c->nch = l->nch;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate memory and set function pointers
|
2007-03-20 09:46:00 +00:00
|
|
|
static int af_open(af_instance_t* af){
|
2002-10-01 06:45:08 +00:00
|
|
|
af->control=control;
|
|
|
|
af->uninit=uninit;
|
|
|
|
af->play=play;
|
|
|
|
af->mul.n=1;
|
|
|
|
af->mul.d=1;
|
|
|
|
af->data=calloc(1,sizeof(af_data_t));
|
2002-12-28 13:59:53 +00:00
|
|
|
af->setup=calloc(1,sizeof(af_channels_t));
|
|
|
|
if((af->data == NULL) || (af->setup == NULL))
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description of this filter
|
|
|
|
af_info_t af_info_channels = {
|
|
|
|
"Insert or remove channels",
|
|
|
|
"channels",
|
|
|
|
"Anders",
|
|
|
|
"",
|
2002-10-06 11:26:14 +00:00
|
|
|
AF_FLAGS_REENTRANT,
|
2007-03-20 09:46:00 +00:00
|
|
|
af_open
|
2002-10-01 06:45:08 +00:00
|
|
|
};
|