2009-01-05 12:41:40 +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.
|
|
|
|
*
|
2013-10-23 17:05:47 +00:00
|
|
|
* Original author: Anders
|
|
|
|
*
|
2009-01-05 12:41:40 +00:00
|
|
|
* 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-10-01 06:45:08 +00:00
|
|
|
#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
|
|
|
|
2013-10-26 13:05:59 +00:00
|
|
|
#include "mpvcore/mp_common.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:
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] Unsupported number of bytes/sample: %i"
|
2002-12-28 13:59:53 +00:00
|
|
|
" 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)){
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[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;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
for(i=0;i<s->nr;i++){
|
|
|
|
if((s->route[i][FR] >= nin) || (s->route[i][TO] >= nout)){
|
2009-03-28 19:57:56 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[channels] Invalid routing in pair nr. %i.\n", i);
|
2002-12-28 13:59:53 +00:00
|
|
|
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
|
2012-11-01 11:23:48 +00:00
|
|
|
static int control(struct af_instance* af, int cmd, void* arg)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
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;
|
2009-07-06 23:26:13 +00:00
|
|
|
// Make sure this filter isn't redundant
|
2012-11-01 11:00:00 +00:00
|
|
|
if(af->data->nch == ((struct mp_audio*)arg)->nch)
|
2002-12-28 13:59:53 +00:00
|
|
|
return AF_DETACH;
|
|
|
|
|
|
|
|
// If mono: fake stereo
|
2012-11-01 11:00:00 +00:00
|
|
|
if(((struct mp_audio*)arg)->nch == 1){
|
2013-10-26 13:05:59 +00:00
|
|
|
s->nr = MPMIN(af->data->nch,2);
|
2002-12-28 13:59:53 +00:00
|
|
|
for(i=0;i<s->nr;i++){
|
|
|
|
s->route[i][FR] = 0;
|
|
|
|
s->route[i][TO] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2013-10-26 13:05:59 +00:00
|
|
|
s->nr = MPMIN(af->data->nch, ((struct mp_audio*)arg)->nch);
|
2002-12-28 13:59:53 +00:00
|
|
|
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
|
|
|
|
2012-11-01 11:00:00 +00:00
|
|
|
af->data->rate = ((struct mp_audio*)arg)->rate;
|
2013-04-05 17:47:51 +00:00
|
|
|
mp_audio_set_format(af->data, ((struct mp_audio*)arg)->format);
|
2013-11-10 22:11:40 +00:00
|
|
|
mp_audio_force_interleaved_format(af->data);
|
|
|
|
int r = af_test_output(af,(struct mp_audio*)arg);
|
|
|
|
if (r != AF_OK)
|
|
|
|
return r;
|
2012-11-01 11:00:00 +00:00
|
|
|
return check_routes(s,((struct mp_audio*)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)){
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_ERR, "[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);
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2002-12-28 13:59:53 +00:00
|
|
|
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);
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 18:39:52 +00:00
|
|
|
struct mp_chmap chmap;
|
|
|
|
mp_chmap_from_channels(&chmap, nch);
|
2013-11-18 13:16:08 +00:00
|
|
|
if (AF_OK != af->control(af, AF_CONTROL_SET_CHANNELS, &chmap))
|
2002-12-28 13:59:53 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
return AF_OK;
|
2009-07-06 23:26:13 +00:00
|
|
|
}
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_CHANNELS:
|
2002-10-01 06:45:08 +00:00
|
|
|
// Reinit must be called after this function has been called
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-04-05 18:39:52 +00:00
|
|
|
mp_audio_set_channels(af->data, (struct mp_chmap *)arg);
|
2002-12-28 13:59:53 +00:00
|
|
|
if(!s->router)
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_V, "[channels] Changing number of channels"
|
2002-12-28 13:59:53 +00:00
|
|
|
" to %i\n",af->data->nch);
|
|
|
|
return AF_OK;
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
// Deallocate memory
|
2012-11-01 11:23:48 +00:00
|
|
|
static void uninit(struct af_instance* af)
|
2002-10-01 06:45:08 +00:00
|
|
|
{
|
2007-02-10 18:12:03 +00:00
|
|
|
free(af->setup);
|
2002-10-01 06:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-01 06:45:08 +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
|
2002-12-28 13:59:53 +00:00
|
|
|
af_channels_t* s = af->setup;
|
|
|
|
int i;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2013-11-10 22:20:06 +00:00
|
|
|
mp_audio_realloc_min(af->data, data->samples);
|
2002-10-01 06:45:08 +00:00
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
// Reset unused channels
|
2013-11-10 22:11:40 +00:00
|
|
|
memset(l->planes[0],0,mp_audio_psize(c) / c->nch * l->nch);
|
2009-07-06 23:26:13 +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++)
|
2013-11-10 22:11:40 +00:00
|
|
|
copy(c->planes[0],l->planes[0],c->nch,s->route[i][FR],
|
|
|
|
l->nch,s->route[i][TO],mp_audio_psize(c),c->bps);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-10-01 06:45:08 +00:00
|
|
|
// Set output data
|
2013-11-10 22:11:40 +00:00
|
|
|
c->planes[0] = l->planes[0];
|
|
|
|
c->samples = c->samples / c->nch * l->nch;
|
2013-04-05 18:39:52 +00:00
|
|
|
mp_audio_set_channels(c, &l->channels);
|
2002-10-01 06:45:08 +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){
|
2002-10-01 06:45:08 +00:00
|
|
|
af->control=control;
|
|
|
|
af->uninit=uninit;
|
|
|
|
af->play=play;
|
2002-12-28 13:59:53 +00:00
|
|
|
af->setup=calloc(1,sizeof(af_channels_t));
|
2013-11-10 22:20:06 +00:00
|
|
|
if(af->setup == NULL)
|
2002-10-01 06:45:08 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description of this filter
|
2012-11-01 11:20:35 +00:00
|
|
|
struct af_info af_info_channels = {
|
2013-10-23 17:05:47 +00:00
|
|
|
.info = "Insert or remove channels",
|
|
|
|
.name = "channels",
|
|
|
|
.open = af_open,
|
2002-10-01 06:45:08 +00:00
|
|
|
};
|