Mercurial > mplayer.hg
annotate libaf/af_pan.c @ 34233:7d06c9c40c2b
build: Remove dangling reference to removed DOCS/xml/configure.
author | diego |
---|---|
date | Mon, 07 Nov 2011 19:54:36 +0000 |
parents | a93891202051 |
children | 2b9bc3c2933d |
rev | line source |
---|---|
28229
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
1 /* |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
2 * Copyright (C) 2002 Anders Johansson ajh@atri.curtin.edu.au |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
3 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
4 * This file is part of MPlayer. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
5 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
6 * MPlayer is free software; you can redistribute it and/or modify |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
7 * it under the terms of the GNU General Public License as published by |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
8 * the Free Software Foundation; either version 2 of the License, or |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
9 * (at your option) any later version. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
10 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
11 * MPlayer is distributed in the hope that it will be useful, |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
14 * GNU General Public License for more details. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
15 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
16 * You should have received a copy of the GNU General Public License along |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
17 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24888
diff
changeset
|
19 */ |
8607 | 20 |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 | |
24 #include <inttypes.h> | |
25 #include <math.h> | |
26 #include <limits.h> | |
27 | |
34174
a93891202051
Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents:
32537
diff
changeset
|
28 #include "mp_msg.h" |
8607 | 29 #include "af.h" |
30 | |
31 // Data for specific instances of this filter | |
32 typedef struct af_pan_s | |
33 { | |
23532
a1eb547cf52e
Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents:
23531
diff
changeset
|
34 int nch; // Number of output channels; zero means same as input |
8607 | 35 float level[AF_NCH][AF_NCH]; // Gain level for each channel |
36 }af_pan_t; | |
37 | |
38 // Initialization and runtime control | |
39 static int control(struct af_instance_s* af, int cmd, void* arg) | |
40 { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
41 af_pan_t* s = af->setup; |
8607 | 42 |
43 switch(cmd){ | |
44 case AF_CONTROL_REINIT: | |
45 // Sanity check | |
46 if(!arg) return AF_ERROR; | |
47 | |
48 af->data->rate = ((af_data_t*)arg)->rate; | |
14245 | 49 af->data->format = AF_FORMAT_FLOAT_NE; |
8607 | 50 af->data->bps = 4; |
23532
a1eb547cf52e
Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents:
23531
diff
changeset
|
51 af->data->nch = s->nch ? s->nch: ((af_data_t*)arg)->nch; |
24888 | 52 af->mul = (double)af->data->nch / ((af_data_t*)arg)->nch; |
8607 | 53 |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
54 if((af->data->format != ((af_data_t*)arg)->format) || |
8607 | 55 (af->data->bps != ((af_data_t*)arg)->bps)){ |
56 ((af_data_t*)arg)->format = af->data->format; | |
57 ((af_data_t*)arg)->bps = af->data->bps; | |
58 return AF_FALSE; | |
59 } | |
23532
a1eb547cf52e
Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents:
23531
diff
changeset
|
60 return AF_OK; |
8607 | 61 case AF_CONTROL_COMMAND_LINE:{ |
62 int nch = 0; | |
63 int n = 0; | |
64 char* cp = NULL; | |
65 int j,k; | |
66 // Read number of outputs | |
67 sscanf((char*)arg,"%i%n", &nch,&n); | |
68 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch)) | |
69 return AF_ERROR; | |
70 | |
71 // Read pan values | |
72 cp = &((char*)arg)[n]; | |
73 j = 0; k = 0; | |
74 while((*cp == ':') && (k < AF_NCH)){ | |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
75 sscanf(cp, ":%f%n" , &s->level[j][k], &n); |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
76 mp_msg(MSGT_AFILTER, MSGL_V, "[pan] Pan level from channel %i to" |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
77 " channel %i = %f\n",k,j,s->level[j][k]); |
8607 | 78 cp =&cp[n]; |
79 j++; | |
80 if(j>=nch){ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
81 j = 0; |
8607 | 82 k++; |
83 } | |
84 } | |
85 return AF_OK; | |
86 } | |
87 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{ | |
88 int i; | |
89 int ch = ((af_control_ext_t*)arg)->ch; | |
90 float* level = ((af_control_ext_t*)arg)->arg; | |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
91 if (ch >= AF_NCH) |
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
92 return AF_FALSE; |
8607 | 93 for(i=0;i<AF_NCH;i++) |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
94 s->level[ch][i] = level[i]; |
8607 | 95 return AF_OK; |
96 } | |
97 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{ | |
98 int i; | |
99 int ch = ((af_control_ext_t*)arg)->ch; | |
100 float* level = ((af_control_ext_t*)arg)->arg; | |
16493
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
101 if (ch >= AF_NCH) |
851d10933f27
Fix af_pan commandline mess and (hopefully) improve description.
reimar
parents:
14433
diff
changeset
|
102 return AF_FALSE; |
8607 | 103 for(i=0;i<AF_NCH;i++) |
104 level[i] = s->level[ch][i]; | |
105 return AF_OK; | |
106 } | |
107 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET: | |
108 // Reinit must be called after this function has been called | |
109 | |
110 // Sanity check | |
12008 | 111 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > AF_NCH){ |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
112 mp_msg(MSGT_AFILTER, MSGL_ERR, "[pan] The number of output channels must be" |
8607 | 113 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]); |
114 return AF_ERROR; | |
115 } | |
23532
a1eb547cf52e
Avoid zero output for pan filter; zero output now means same # of channels
zuxy
parents:
23531
diff
changeset
|
116 s->nch=((int*)arg)[0]; |
8607 | 117 return AF_OK; |
118 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET: | |
119 *(int*)arg = af->data->nch; | |
120 return AF_OK; | |
23551 | 121 case AF_CONTROL_PAN_BALANCE | AF_CONTROL_SET:{ |
122 float val = *(float*)arg; | |
123 if (s->nch) | |
124 return AF_ERROR; | |
125 if (af->data->nch >= 2) { | |
126 s->level[0][0] = min(1.f, 1.f - val); | |
127 s->level[0][1] = max(0.f, val); | |
128 s->level[1][0] = max(0.f, -val); | |
129 s->level[1][1] = min(1.f, 1.f + val); | |
130 } | |
131 return AF_OK; | |
132 } | |
133 case AF_CONTROL_PAN_BALANCE | AF_CONTROL_GET: | |
134 if (s->nch) | |
135 return AF_ERROR; | |
136 *(float*)arg = s->level[0][1] - s->level[1][0]; | |
137 return AF_OK; | |
8607 | 138 } |
139 return AF_UNKNOWN; | |
140 } | |
141 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
142 // Deallocate memory |
8607 | 143 static void uninit(struct af_instance_s* af) |
144 { | |
22175
105f32787336
Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents:
16493
diff
changeset
|
145 if(af->data) |
8674
93212da0032e
10l memory leak + bug fixes in ms to sample time conversion
anders
parents:
8607
diff
changeset
|
146 free(af->data->audio); |
22175
105f32787336
Fix nonsense tests ("if (af->data->audio)" before "if (af->data)").
uau
parents:
16493
diff
changeset
|
147 free(af->data); |
32537
8fa2f43cb760
Remove most of the NULL pointer check before free all over the code
cboesch
parents:
29263
diff
changeset
|
148 free(af->setup); |
8607 | 149 } |
150 | |
151 // Filter data through filter | |
152 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
153 { | |
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; | |
163 | |
164 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
165 return NULL; | |
166 | |
167 out = l->audio; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
168 // Execute panning |
8607 | 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; | |
177 } | |
178 out+= ncho; | |
179 in+= nchi; | |
180 } | |
181 | |
182 // Set output data | |
183 c->audio = l->audio; | |
24888 | 184 c->len = c->len / c->nch * l->nch; |
8607 | 185 c->nch = l->nch; |
186 | |
187 return c; | |
188 } | |
189 | |
190 // Allocate memory and set function pointers | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22175
diff
changeset
|
191 static int af_open(af_instance_t* af){ |
8607 | 192 af->control=control; |
193 af->uninit=uninit; | |
194 af->play=play; | |
24888 | 195 af->mul=1; |
8607 | 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; | |
201 } | |
202 | |
203 // Description of this filter | |
204 af_info_t af_info_pan = { | |
205 "Panning audio filter", | |
206 "pan", | |
207 "Anders", | |
208 "", | |
23531
bd9e74cd4d3d
Make pan reentrant. Multiple pans in chain work fine.
zuxy
parents:
22748
diff
changeset
|
209 AF_FLAGS_REENTRANT, |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22175
diff
changeset
|
210 af_open |
8607 | 211 }; |