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>
|
|
|
|
|
2013-10-26 13:05:59 +00:00
|
|
|
#include "mpvcore/mp_common.h"
|
2002-12-28 13:59:53 +00:00
|
|
|
#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
|
2013-12-04 22:01:45 +00:00
|
|
|
char *matrixstr;
|
2002-12-28 13:59:53 +00:00
|
|
|
}af_pan_t;
|
|
|
|
|
2013-05-09 10:56:16 +00:00
|
|
|
static void set_channels(struct mp_audio *mpa, int num)
|
|
|
|
{
|
|
|
|
struct mp_chmap map;
|
|
|
|
// "unknown" channel layouts make it easier to pass through audio data,
|
|
|
|
// without triggering remixing.
|
|
|
|
mp_chmap_set_unknown(&map, num);
|
|
|
|
mp_audio_set_channels(mpa, &map);
|
|
|
|
}
|
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
// Initialization and runtime control
|
2012-11-01 11:23:48 +00:00
|
|
|
static int control(struct af_instance* af, int cmd, void* arg)
|
2002-12-28 13:59:53 +00:00
|
|
|
{
|
2013-12-04 22:01:45 +00:00
|
|
|
af_pan_t* s = af->priv;
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
switch(cmd){
|
|
|
|
case AF_CONTROL_REINIT:
|
|
|
|
// Sanity check
|
|
|
|
if(!arg) return AF_ERROR;
|
|
|
|
|
2012-11-01 11:00:00 +00:00
|
|
|
af->data->rate = ((struct mp_audio*)arg)->rate;
|
2013-11-15 20:25:05 +00:00
|
|
|
mp_audio_set_format(af->data, AF_FORMAT_FLOAT);
|
2013-05-09 10:56:16 +00:00
|
|
|
set_channels(af->data, s->nch ? s->nch: ((struct mp_audio*)arg)->nch);
|
2002-12-28 13:59:53 +00:00
|
|
|
|
2012-11-01 11:00:00 +00:00
|
|
|
if((af->data->format != ((struct mp_audio*)arg)->format) ||
|
|
|
|
(af->data->bps != ((struct mp_audio*)arg)->bps)){
|
2013-04-05 17:47:51 +00:00
|
|
|
mp_audio_set_format((struct mp_audio*)arg, af->data->format);
|
2002-12-28 13:59:53 +00:00
|
|
|
return AF_FALSE;
|
|
|
|
}
|
2007-06-13 09:28:49 +00:00
|
|
|
return AF_OK;
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_PAN_LEVEL:{
|
2002-12-28 13:59:53 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_PAN_NOUT:
|
2002-12-28 13:59:53 +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){
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[pan] The number of output channels must be"
|
2013-03-31 03:18:56 +00:00
|
|
|
" between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
|
2002-12-28 13:59:53 +00:00
|
|
|
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;
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_PAN_BALANCE:{
|
2007-06-17 12:18:13 +00:00
|
|
|
float val = *(float*)arg;
|
|
|
|
if (s->nch)
|
|
|
|
return AF_ERROR;
|
|
|
|
if (af->data->nch >= 2) {
|
2013-10-26 13:05:59 +00:00
|
|
|
s->level[0][0] = MPMIN(1.f, 1.f - val);
|
|
|
|
s->level[0][1] = MPMAX(0.f, val);
|
|
|
|
s->level[1][0] = MPMAX(0.f, -val);
|
|
|
|
s->level[1][1] = MPMIN(1.f, 1.f + val);
|
2007-06-17 12:18:13 +00:00
|
|
|
}
|
|
|
|
return AF_OK;
|
|
|
|
}
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_GET_PAN_BALANCE:
|
2007-06-17 12:18:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data through filter
|
2012-11-01 11:23:48 +00:00
|
|
|
static struct mp_audio* play(struct af_instance* af, struct mp_audio* data)
|
2002-12-28 13:59:53 +00:00
|
|
|
{
|
2012-11-01 11:00:00 +00:00
|
|
|
struct mp_audio* c = data; // Current working data
|
|
|
|
struct mp_audio* l = af->data; // Local data
|
2013-12-04 22:01:45 +00:00
|
|
|
af_pan_t* s = af->priv; // Setup for this instance
|
2013-11-10 22:11:40 +00:00
|
|
|
float* in = c->planes[0]; // Input audio data
|
2002-12-28 13:59:53 +00:00
|
|
|
float* out = NULL; // Output audio data
|
2013-11-10 22:11:40 +00:00
|
|
|
float* end = in+c->samples*c->nch; // End of loop
|
2002-12-28 13:59:53 +00:00
|
|
|
int nchi = c->nch; // Number of input channels
|
|
|
|
int ncho = l->nch; // Number of output channels
|
|
|
|
register int j,k;
|
|
|
|
|
2013-11-10 22:20:06 +00:00
|
|
|
mp_audio_realloc_min(af->data, data->samples);
|
2002-12-28 13:59:53 +00:00
|
|
|
|
2013-11-10 22:11:40 +00:00
|
|
|
out = l->planes[0];
|
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
|
2013-11-10 22:11:40 +00:00
|
|
|
c->planes[0] = l->planes[0];
|
2013-05-09 10:56:16 +00:00
|
|
|
set_channels(c, l->nch);
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate memory and set function pointers
|
2012-11-01 11:23:48 +00:00
|
|
|
static int af_open(struct af_instance* af){
|
2013-12-04 22:01:45 +00:00
|
|
|
af->control=control;
|
|
|
|
af->play=play;
|
|
|
|
af_pan_t *s = af->priv;
|
|
|
|
int n = 0;
|
|
|
|
int j,k;
|
|
|
|
|
|
|
|
int nch = s->nch;
|
|
|
|
if(AF_OK != control(af,AF_CONTROL_SET_PAN_NOUT, &nch))
|
|
|
|
return AF_ERROR;
|
|
|
|
|
|
|
|
// Read pan values
|
|
|
|
char *cp = s->matrixstr;
|
|
|
|
j = 0; k = 0;
|
|
|
|
while(k < AF_NCH){
|
|
|
|
sscanf(cp, "%f%n" , &s->level[j][k], &n);
|
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[pan] Pan level from channel %i to"
|
|
|
|
" channel %i = %f\n",k,j,s->level[j][k]);
|
|
|
|
cp =&cp[n];
|
|
|
|
j++;
|
|
|
|
if(j>=nch){
|
|
|
|
j = 0;
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
if (*cp != ',')
|
|
|
|
break;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
return AF_OK;
|
2002-12-28 13:59:53 +00:00
|
|
|
}
|
|
|
|
|
2013-12-04 22:01:45 +00:00
|
|
|
#define OPT_BASE_STRUCT af_pan_t
|
2012-11-01 11:20:35 +00:00
|
|
|
struct af_info af_info_pan = {
|
2013-10-23 17:05:47 +00:00
|
|
|
.info = "Panning audio filter",
|
|
|
|
.name = "pan",
|
|
|
|
.open = af_open,
|
2013-12-04 22:01:45 +00:00
|
|
|
.priv_size = sizeof(af_pan_t),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_INTRANGE("channels", nch, 0, 0, AF_NCH),
|
|
|
|
OPT_STRING("matrix", matrixstr, 0),
|
|
|
|
{0}
|
|
|
|
},
|
2002-12-28 13:59:53 +00:00
|
|
|
};
|