2009-01-05 12:41:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "af.h"
|
|
|
|
|
|
|
|
// Data for specific instances of this filter
|
|
|
|
typedef struct af_pan_s
|
|
|
|
{
|
2007-06-13 09:28:49 +00:00
|
|
|
int nch; // Number of output channels; zero means same as input
|
2002-12-28 13:59:53 +00:00
|
|
|
float level[AF_NCH][AF_NCH]; // Gain level for each channel
|
|
|
|
}af_pan_t;
|
|
|
|
|
|
|
|
// Initialization and runtime control
|
|
|
|
static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
|
|
{
|
2009-07-06 23:26:13 +00:00
|
|
|
af_pan_t* s = af->setup;
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
switch(cmd){
|
|
|
|
case AF_CONTROL_REINIT:
|
|
|
|
// Sanity check
|
|
|
|
if(!arg) return AF_ERROR;
|
|
|
|
|
|
|
|
af->data->rate = ((af_data_t*)arg)->rate;
|
2004-12-27 17:30:15 +00:00
|
|
|
af->data->format = AF_FORMAT_FLOAT_NE;
|
2002-12-28 13:59:53 +00:00
|
|
|
af->data->bps = 4;
|
2007-06-13 09:28:49 +00:00
|
|
|
af->data->nch = s->nch ? s->nch: ((af_data_t*)arg)->nch;
|
2007-11-01 06:52:01 +00:00
|
|
|
af->mul = (double)af->data->nch / ((af_data_t*)arg)->nch;
|
2002-12-28 13:59:53 +00:00
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
if((af->data->format != ((af_data_t*)arg)->format) ||
|
2002-12-28 13:59:53 +00:00
|
|
|
(af->data->bps != ((af_data_t*)arg)->bps)){
|
|
|
|
((af_data_t*)arg)->format = af->data->format;
|
|
|
|
((af_data_t*)arg)->bps = af->data->bps;
|
|
|
|
return AF_FALSE;
|
|
|
|
}
|
2007-06-13 09:28:49 +00:00
|
|
|
return AF_OK;
|
2002-12-28 13:59:53 +00:00
|
|
|
case AF_CONTROL_COMMAND_LINE:{
|
|
|
|
int nch = 0;
|
|
|
|
int n = 0;
|
|
|
|
char* cp = NULL;
|
|
|
|
int j,k;
|
|
|
|
// Read number of outputs
|
|
|
|
sscanf((char*)arg,"%i%n", &nch,&n);
|
|
|
|
if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch))
|
|
|
|
return AF_ERROR;
|
|
|
|
|
|
|
|
// Read pan values
|
|
|
|
cp = &((char*)arg)[n];
|
|
|
|
j = 0; k = 0;
|
|
|
|
while((*cp == ':') && (k < AF_NCH)){
|
2005-09-15 11:41:23 +00:00
|
|
|
sscanf(cp, ":%f%n" , &s->level[j][k], &n);
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[pan] Pan level from channel %i to"
|
2005-09-15 11:41:23 +00:00
|
|
|
" channel %i = %f\n",k,j,s->level[j][k]);
|
2002-12-28 13:59:53 +00:00
|
|
|
cp =&cp[n];
|
|
|
|
j++;
|
|
|
|
if(j>=nch){
|
2009-07-06 23:26:13 +00:00
|
|
|
j = 0;
|
2002-12-28 13:59:53 +00:00
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{
|
|
|
|
int i;
|
|
|
|
int ch = ((af_control_ext_t*)arg)->ch;
|
|
|
|
float* level = ((af_control_ext_t*)arg)->arg;
|
2005-09-15 11:41:23 +00:00
|
|
|
if (ch >= AF_NCH)
|
|
|
|
return AF_FALSE;
|
2002-12-28 13:59:53 +00:00
|
|
|
for(i=0;i<AF_NCH;i++)
|
2005-09-15 11:41:23 +00:00
|
|
|
s->level[ch][i] = level[i];
|
2002-12-28 13:59:53 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{
|
|
|
|
int i;
|
|
|
|
int ch = ((af_control_ext_t*)arg)->ch;
|
|
|
|
float* level = ((af_control_ext_t*)arg)->arg;
|
2005-09-15 11:41:23 +00:00
|
|
|
if (ch >= AF_NCH)
|
|
|
|
return AF_FALSE;
|
2002-12-28 13:59:53 +00:00
|
|
|
for(i=0;i<AF_NCH;i++)
|
|
|
|
level[i] = s->level[ch][i];
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET:
|
|
|
|
// 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){
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[pan] The number of output channels must be"
|
2002-12-28 13:59:53 +00:00
|
|
|
" between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2007-06-13 09:28:49 +00:00
|
|
|
s->nch=((int*)arg)[0];
|
2002-12-28 13:59:53 +00:00
|
|
|
return AF_OK;
|
|
|
|
case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET:
|
|
|
|
*(int*)arg = af->data->nch;
|
|
|
|
return AF_OK;
|
2007-06-17 12:18:13 +00:00
|
|
|
case AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET:{
|
|
|
|
float val = *(float*)arg;
|
|
|
|
if (s->nch)
|
|
|
|
return AF_ERROR;
|
|
|
|
if (af->data->nch >= 2) {
|
|
|
|
s->level[0][0] = min(1.f, 1.f - val);
|
|
|
|
s->level[0][1] = max(0.f, val);
|
|
|
|
s->level[1][0] = max(0.f, -val);
|
|
|
|
s->level[1][1] = min(1.f, 1.f + val);
|
|
|
|
}
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_PAN_BALANCE | AF_CONTROL_GET:
|
|
|
|
if (s->nch)
|
|
|
|
return AF_ERROR;
|
|
|
|
*(float*)arg = s->level[0][1] - s->level[1][0];
|
|
|
|
return AF_OK;
|
2002-12-28 13:59:53 +00:00
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
// Deallocate memory
|
2002-12-28 13:59:53 +00:00
|
|
|
static void uninit(struct af_instance_s* af)
|
|
|
|
{
|
|
|
|
if(af->data)
|
2007-02-10 17:27:11 +00:00
|
|
|
free(af->data->audio);
|
|
|
|
free(af->data);
|
2010-11-14 09:12:34 +00:00
|
|
|
free(af->setup);
|
2002-12-28 13:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data through filter
|
|
|
|
static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
|
|
{
|
|
|
|
af_data_t* c = data; // Current working data
|
|
|
|
af_data_t* l = af->data; // Local data
|
|
|
|
af_pan_t* s = af->setup; // Setup for this instance
|
|
|
|
float* in = c->audio; // Input audio data
|
|
|
|
float* out = NULL; // Output audio data
|
|
|
|
float* end = in+c->len/4; // End of loop
|
|
|
|
int nchi = c->nch; // Number of input channels
|
|
|
|
int ncho = l->nch; // Number of output channels
|
|
|
|
register int j,k;
|
|
|
|
|
|
|
|
if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
out = l->audio;
|
2009-07-06 23:26:13 +00:00
|
|
|
// Execute panning
|
2002-12-28 13:59:53 +00:00
|
|
|
// FIXME: Too slow
|
|
|
|
while(in < end){
|
|
|
|
for(j=0;j<ncho;j++){
|
|
|
|
register float x = 0.0;
|
|
|
|
register float* tin = in;
|
|
|
|
for(k=0;k<nchi;k++)
|
|
|
|
x += tin[k] * s->level[j][k];
|
|
|
|
out[j] = x;
|
|
|
|
}
|
|
|
|
out+= ncho;
|
|
|
|
in+= nchi;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set output data
|
|
|
|
c->audio = l->audio;
|
2007-11-01 06:52:01 +00:00
|
|
|
c->len = c->len / c->nch * l->nch;
|
2002-12-28 13:59:53 +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-12-28 13:59:53 +00:00
|
|
|
af->control=control;
|
|
|
|
af->uninit=uninit;
|
|
|
|
af->play=play;
|
2007-11-01 06:52:01 +00:00
|
|
|
af->mul=1;
|
2002-12-28 13:59:53 +00:00
|
|
|
af->data=calloc(1,sizeof(af_data_t));
|
|
|
|
af->setup=calloc(1,sizeof(af_pan_t));
|
|
|
|
if(af->data == NULL || af->setup == NULL)
|
|
|
|
return AF_ERROR;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description of this filter
|
|
|
|
af_info_t af_info_pan = {
|
|
|
|
"Panning audio filter",
|
|
|
|
"pan",
|
|
|
|
"Anders",
|
|
|
|
"",
|
2007-06-13 09:20:36 +00:00
|
|
|
AF_FLAGS_REENTRANT,
|
2007-03-20 09:46:00 +00:00
|
|
|
af_open
|
2002-12-28 13:59:53 +00:00
|
|
|
};
|