codecs.conf: remove yuv422 format from VDPAU section
[mplayer.git] / libaf / af_pan.c
blob9a3e520446279c52008f63fa61c016ef766c2823
1 /*
2 * Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
24 #include <inttypes.h>
25 #include <math.h>
26 #include <limits.h>
28 #include "af.h"
30 // Data for specific instances of this filter
31 typedef struct af_pan_s
33 int nch; // Number of output channels; zero means same as input
34 float level[AF_NCH][AF_NCH]; // Gain level for each channel
35 }af_pan_t;
37 // Initialization and runtime control
38 static int control(struct af_instance_s* af, int cmd, void* arg)
40 af_pan_t* s = af->setup;
42 switch(cmd){
43 case AF_CONTROL_REINIT:
44 // Sanity check
45 if(!arg) return AF_ERROR;
47 af->data->rate = ((af_data_t*)arg)->rate;
48 af->data->format = AF_FORMAT_FLOAT_NE;
49 af->data->bps = 4;
50 af->data->nch = s->nch ? s->nch: ((af_data_t*)arg)->nch;
51 af->mul = (double)af->data->nch / ((af_data_t*)arg)->nch;
53 if((af->data->format != ((af_data_t*)arg)->format) ||
54 (af->data->bps != ((af_data_t*)arg)->bps)){
55 ((af_data_t*)arg)->format = af->data->format;
56 ((af_data_t*)arg)->bps = af->data->bps;
57 return AF_FALSE;
59 return AF_OK;
60 case AF_CONTROL_COMMAND_LINE:{
61 int nch = 0;
62 int n = 0;
63 char* cp = NULL;
64 int j,k;
65 // Read number of outputs
66 sscanf((char*)arg,"%i%n", &nch,&n);
67 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch))
68 return AF_ERROR;
70 // Read pan values
71 cp = &((char*)arg)[n];
72 j = 0; k = 0;
73 while((*cp == ':') && (k < AF_NCH)){
74 sscanf(cp, ":%f%n" , &s->level[j][k], &n);
75 mp_msg(MSGT_AFILTER, MSGL_V, "[pan] Pan level from channel %i to"
76 " channel %i = %f\n",k,j,s->level[j][k]);
77 cp =&cp[n];
78 j++;
79 if(j>=nch){
80 j = 0;
81 k++;
84 return AF_OK;
86 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{
87 int i;
88 int ch = ((af_control_ext_t*)arg)->ch;
89 float* level = ((af_control_ext_t*)arg)->arg;
90 if (ch >= AF_NCH)
91 return AF_FALSE;
92 for(i=0;i<AF_NCH;i++)
93 s->level[ch][i] = level[i];
94 return AF_OK;
96 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{
97 int i;
98 int ch = ((af_control_ext_t*)arg)->ch;
99 float* level = ((af_control_ext_t*)arg)->arg;
100 if (ch >= AF_NCH)
101 return AF_FALSE;
102 for(i=0;i<AF_NCH;i++)
103 level[i] = s->level[ch][i];
104 return AF_OK;
106 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET:
107 // Reinit must be called after this function has been called
109 // Sanity check
110 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){
111 mp_msg(MSGT_AFILTER, MSGL_ERR, "[pan] The number of output channels must be"
112 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
113 return AF_ERROR;
115 s->nch=((int*)arg)[0];
116 return AF_OK;
117 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET:
118 *(int*)arg = af->data->nch;
119 return AF_OK;
120 case AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET:{
121 float val = *(float*)arg;
122 if (s->nch)
123 return AF_ERROR;
124 if (af->data->nch >= 2) {
125 s->level[0][0] = min(1.f, 1.f - val);
126 s->level[0][1] = max(0.f, val);
127 s->level[1][0] = max(0.f, -val);
128 s->level[1][1] = min(1.f, 1.f + val);
130 return AF_OK;
132 case AF_CONTROL_PAN_BALANCE | AF_CONTROL_GET:
133 if (s->nch)
134 return AF_ERROR;
135 *(float*)arg = s->level[0][1] - s->level[1][0];
136 return AF_OK;
138 return AF_UNKNOWN;
141 // Deallocate memory
142 static void uninit(struct af_instance_s* af)
144 if(af->data)
145 free(af->data->audio);
146 free(af->data);
147 if(af->setup)
148 free(af->setup);
151 // Filter data through filter
152 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
154 af_data_t* c = data; // Current working data
155 af_data_t* l = af->data; // Local data
156 af_pan_t* s = af->setup; // Setup for this instance
157 float* in = c->audio; // Input audio data
158 float* out = NULL; // Output audio data
159 float* end = in+c->len/4; // End of loop
160 int nchi = c->nch; // Number of input channels
161 int ncho = l->nch; // Number of output channels
162 register int j,k;
164 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
165 return NULL;
167 out = l->audio;
168 // Execute panning
169 // FIXME: Too slow
170 while(in < end){
171 for(j=0;j<ncho;j++){
172 register float x = 0.0;
173 register float* tin = in;
174 for(k=0;k<nchi;k++)
175 x += tin[k] * s->level[j][k];
176 out[j] = x;
178 out+= ncho;
179 in+= nchi;
182 // Set output data
183 c->audio = l->audio;
184 c->len = c->len / c->nch * l->nch;
185 c->nch = l->nch;
187 return c;
190 // Allocate memory and set function pointers
191 static int af_open(af_instance_t* af){
192 af->control=control;
193 af->uninit=uninit;
194 af->play=play;
195 af->mul=1;
196 af->data=calloc(1,sizeof(af_data_t));
197 af->setup=calloc(1,sizeof(af_pan_t));
198 if(af->data == NULL || af->setup == NULL)
199 return AF_ERROR;
200 return AF_OK;
203 // Description of this filter
204 af_info_t af_info_pan = {
205 "Panning audio filter",
206 "pan",
207 "Anders",
209 AF_FLAGS_REENTRANT,
210 af_open