Mercurial > mplayer.hg
annotate libaf/af_resample.c @ 7995:04f386daac34
dummy -npp
author | michael |
---|---|
date | Thu, 31 Oct 2002 10:42:20 +0000 |
parents | 3aba91eb5c1f |
children | d48a06d07afb |
rev | line source |
---|---|
7568 | 1 /*============================================================================= |
2 // | |
3 // This software has been released under the terms of the GNU Public | |
4 // license. See http://www.gnu.org/copyleft/gpl.html for details. | |
5 // | |
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au | |
7 // | |
8 //============================================================================= | |
9 */ | |
10 | |
11 /* This audio filter changes the sample rate. */ | |
12 | |
13 #define PLUGIN | |
14 | |
15 #include <stdio.h> | |
16 #include <stdlib.h> | |
17 #include <unistd.h> | |
18 #include <inttypes.h> | |
19 | |
20 #include "../config.h" | |
21 #include "../mp_msg.h" | |
22 #include "../libao2/afmt.h" | |
23 | |
24 #include "af.h" | |
25 #include "dsp.h" | |
26 | |
27 /* Below definition selects the length of each poly phase component. | |
28 Valid definitions are L8 and L16, where the number denotes the | |
29 length of the filter. This definition affects the computational | |
30 complexity (see play()), the performance (see filter.h) and the | |
31 memory usage. The filterlenght is choosen to 8 if the machine is | |
32 slow and to 16 if the machine is fast and has MMX. | |
33 */ | |
34 | |
7588 | 35 #if !defined(HAVE_SSE) && !defined(HAVE_3DNOW) // This machine is slow |
7568 | 36 |
37 #define L 8 // Filter length | |
38 // Unrolled loop to speed up execution | |
7602
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
39 #define FIR(x,w,y) \ |
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
40 (y[0]) = ( w[0]*x[0]+w[1]*x[1]+w[2]*x[2]+w[3]*x[3] \ |
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
41 + w[4]*x[4]+w[5]*x[5]+w[6]*x[6]+w[7]*x[7] ) >> 16 |
7568 | 42 |
43 #else /* Fast machine */ | |
44 | |
45 #define L 16 | |
46 // Unrolled loop to speed up execution | |
7602
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
47 #define FIR(x,w,y) \ |
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
48 y[0] = ( w[0] *x[0] +w[1] *x[1] +w[2] *x[2] +w[3] *x[3] \ |
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
49 + w[4] *x[4] +w[5] *x[5] +w[6] *x[6] +w[7] *x[7] \ |
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
50 + w[8] *x[8] +w[9] *x[9] +w[10]*x[10]+w[11]*x[11] \ |
2c8dcba3d8bb
optimization of the FIR macros, extending outfreq range to 2..192khz
arpi
parents:
7588
diff
changeset
|
51 + w[12]*x[12]+w[13]*x[13]+w[14]*x[14]+w[15]*x[15] ) >> 16 |
7568 | 52 |
53 #endif /* Fast machine */ | |
54 | |
55 // Macro to add data to circular que | |
56 #define ADDQUE(xi,xq,in)\ | |
57 xq[xi]=xq[xi+L]=(*in);\ | |
58 xi=(--xi)&(L-1); | |
59 | |
60 | |
61 | |
62 // local data | |
63 typedef struct af_resample_s | |
64 { | |
65 int16_t* w; // Current filter weights | |
66 int16_t** xq; // Circular buffers | |
7580 | 67 uint32_t xi; // Index for circular buffers |
68 uint32_t wi; // Index for w | |
69 uint32_t i; // Number of new samples to put in x queue | |
70 uint32_t dn; // Down sampling factor | |
71 uint32_t up; // Up sampling factor | |
7568 | 72 } af_resample_t; |
73 | |
74 // Euclids algorithm for calculating Greatest Common Divisor GCD(a,b) | |
7894 | 75 static inline int gcd(register int a, register int b) |
7568 | 76 { |
77 register int r = min(a,b); | |
78 a=max(a,b); | |
79 b=r; | |
80 | |
81 r=a%b; | |
82 while(r!=0){ | |
83 a=b; | |
84 b=r; | |
85 r=a%b; | |
86 } | |
87 return b; | |
88 } | |
89 | |
90 static int upsample(af_data_t* c,af_data_t* l, af_resample_t* s) | |
91 { | |
7580 | 92 uint32_t ci = l->nch; // Index for channels |
93 uint32_t len = 0; // Number of input samples | |
94 uint32_t nch = l->nch; // Number of channels | |
95 uint32_t inc = s->up/s->dn; | |
96 uint32_t level = s->up%s->dn; | |
97 uint32_t up = s->up; | |
98 uint32_t dn = s->dn; | |
7568 | 99 |
100 register int16_t* w = s->w; | |
7580 | 101 register uint32_t wi = 0; |
102 register uint32_t xi = 0; | |
7568 | 103 |
104 // Index current channel | |
105 while(ci--){ | |
106 // Temporary pointers | |
107 register int16_t* x = s->xq[ci]; | |
108 register int16_t* in = ((int16_t*)c->audio)+ci; | |
109 register int16_t* out = ((int16_t*)l->audio)+ci; | |
110 int16_t* end = in+c->len/2; // Block loop end | |
111 wi = s->wi; xi = s->xi; | |
112 | |
113 while(in < end){ | |
7580 | 114 register uint32_t i = inc; |
7568 | 115 if(wi<level) i++; |
116 | |
117 ADDQUE(xi,x,in); | |
118 in+=nch; | |
119 while(i--){ | |
120 // Run the FIR filter | |
121 FIR((&x[xi]),(&w[wi*L]),out); | |
122 len++; out+=nch; | |
123 // Update wi to point at the correct polyphase component | |
124 wi=(wi+dn)%up; | |
125 } | |
126 } | |
127 } | |
128 // Save values that needs to be kept for next time | |
129 s->wi = wi; | |
130 s->xi = xi; | |
131 return len; | |
132 } | |
133 | |
134 static int downsample(af_data_t* c,af_data_t* l, af_resample_t* s) | |
135 { | |
7580 | 136 uint32_t ci = l->nch; // Index for channels |
137 uint32_t len = 0; // Number of output samples | |
138 uint32_t nch = l->nch; // Number of channels | |
139 uint32_t inc = s->dn/s->up; | |
140 uint32_t level = s->dn%s->up; | |
141 uint32_t up = s->up; | |
142 uint32_t dn = s->dn; | |
7568 | 143 |
7587 | 144 register int32_t i = 0; |
7580 | 145 register uint32_t wi = 0; |
146 register uint32_t xi = 0; | |
7568 | 147 |
148 // Index current channel | |
149 while(ci--){ | |
150 // Temporary pointers | |
151 register int16_t* x = s->xq[ci]; | |
152 register int16_t* in = ((int16_t*)c->audio)+ci; | |
153 register int16_t* out = ((int16_t*)l->audio)+ci; | |
154 register int16_t* end = in+c->len/2; // Block loop end | |
155 i = s->i; wi = s->wi; xi = s->xi; | |
156 | |
157 while(in < end){ | |
158 | |
159 ADDQUE(xi,x,in); | |
160 in+=nch; | |
7587 | 161 if((--i)<=0){ |
7568 | 162 // Run the FIR filter |
163 FIR((&x[xi]),(&s->w[wi*L]),out); | |
164 len++; out+=nch; | |
165 | |
166 // Update wi to point at the correct polyphase component | |
167 wi=(wi+dn)%up; | |
168 | |
169 // Insert i number of new samples in queue | |
170 i = inc; | |
171 if(wi<level) i++; | |
172 } | |
173 } | |
174 } | |
175 // Save values that needs to be kept for next time | |
176 s->wi = wi; | |
177 s->xi = xi; | |
178 s->i = i; | |
179 | |
180 return len; | |
181 } | |
182 | |
183 // Initialization and runtime control | |
184 static int control(struct af_instance_s* af, int cmd, void* arg) | |
185 { | |
186 switch(cmd){ | |
187 case AF_CONTROL_REINIT:{ | |
188 af_resample_t* s = (af_resample_t*)af->setup; | |
189 af_data_t* n = (af_data_t*)arg; // New configureation | |
190 int i,d = 0; | |
191 int rv = AF_OK; | |
192 | |
193 // Make sure this filter isn't redundant | |
194 if(af->data->rate == n->rate) | |
195 return AF_DETACH; | |
196 | |
197 // Create space for circular bufers (if nesessary) | |
198 if(af->data->nch != n->nch){ | |
199 // First free the old ones | |
200 if(s->xq){ | |
201 for(i=1;i<af->data->nch;i++) | |
202 if(s->xq[i]) | |
203 free(s->xq[i]); | |
204 free(s->xq); | |
205 } | |
206 // ... then create new | |
207 s->xq = malloc(n->nch*sizeof(int16_t*)); | |
208 for(i=0;i<n->nch;i++) | |
209 s->xq[i] = malloc(2*L*sizeof(int16_t)); | |
210 s->xi = 0; | |
211 } | |
212 | |
213 // Set parameters | |
214 af->data->nch = n->nch; | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7665
diff
changeset
|
215 af->data->format = AFMT_S16_NE; |
7568 | 216 af->data->bps = 2; |
217 if(af->data->format != n->format || af->data->bps != n->bps) | |
218 rv = AF_FALSE; | |
7719
41e8d0916c60
Fix for audio filters on big endian cpus. It's working now on Solaris SPARC &
jkeil
parents:
7665
diff
changeset
|
219 n->format = AFMT_S16_NE; |
7568 | 220 n->bps = 2; |
221 | |
222 // Calculate up and down sampling factors | |
223 d=gcd(af->data->rate,n->rate); | |
224 | |
225 // Check if the the design needs to be redone | |
226 if(s->up != af->data->rate/d || s->dn != n->rate/d){ | |
227 float* w; | |
228 float* wt; | |
229 float fc; | |
230 int j; | |
231 s->up = af->data->rate/d; | |
232 s->dn = n->rate/d; | |
233 | |
234 // Calculate cuttof frequency for filter | |
235 fc = 1/(float)(max(s->up,s->dn)); | |
236 // Allocate space for polyphase filter bank and protptype filter | |
237 w = malloc(sizeof(float) * s->up *L); | |
238 if(NULL != s->w) | |
239 free(s->w); | |
240 s->w = malloc(L*s->up*sizeof(int16_t)); | |
241 | |
242 // Design prototype filter type using Kaiser window with beta = 10 | |
243 if(NULL == w || NULL == s->w || | |
244 -1 == design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){ | |
245 mp_msg(MSGT_AFILTER,MSGL_ERR,"[resample] Unable to design prototype filter.\n"); | |
246 return AF_ERROR; | |
247 } | |
248 // Copy data from prototype to polyphase filter | |
249 wt=w; | |
250 for(j=0;j<L;j++){//Columns | |
251 for(i=0;i<s->up;i++){//Rows | |
252 float t=(float)s->up*32767.0*(*wt); | |
253 s->w[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5)); | |
254 wt++; | |
255 } | |
256 } | |
257 free(w); | |
258 mp_msg(MSGT_AFILTER,MSGL_V,"[resample] New filter designed up: %i down: %i\n", s->up, s->dn); | |
259 } | |
260 | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
261 // Set multiplier and delay |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
262 af->delay = (double)(1000*L/2)/((double)n->rate); |
7568 | 263 af->mul.n = s->up; |
264 af->mul.d = s->dn; | |
265 return rv; | |
266 } | |
267 case AF_CONTROL_RESAMPLE: | |
268 // Reinit must be called after this function has been called | |
269 | |
270 // Sanity check | |
7616 | 271 if(((int*)arg)[0] < 8000 || ((int*)arg)[0] > 192000){ |
272 mp_msg(MSGT_AFILTER,MSGL_ERR,"[resample] The output sample frequency must be between 8kHz and 192kHz. Current value is %i \n",((int*)arg)[0]); | |
7568 | 273 return AF_ERROR; |
274 } | |
275 | |
276 af->data->rate=((int*)arg)[0]; | |
7571
8819fdf88b5d
Adding support for multiple audio streams and removing annoying message from resample and format
anders
parents:
7568
diff
changeset
|
277 mp_msg(MSGT_AFILTER,MSGL_V,"[resample] Changing sample rate to %iHz\n",af->data->rate); |
7568 | 278 return AF_OK; |
279 } | |
280 return AF_UNKNOWN; | |
281 } | |
282 | |
283 // Deallocate memory | |
284 static void uninit(struct af_instance_s* af) | |
285 { | |
286 if(af->data) | |
287 free(af->data); | |
288 } | |
289 | |
290 // Filter data through filter | |
291 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
292 { | |
293 int len = 0; // Length of output data | |
294 af_data_t* c = data; // Current working data | |
295 af_data_t* l = af->data; // Local data | |
296 af_resample_t* s = (af_resample_t*)af->setup; | |
297 | |
298 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
299 return NULL; | |
300 | |
301 // Run resampling | |
302 if(s->up>s->dn) | |
303 len = upsample(c,l,s); | |
304 else | |
305 len = downsample(c,l,s); | |
306 | |
307 // Set output data | |
308 c->audio = l->audio; | |
309 c->len = len*2; | |
310 c->rate = l->rate; | |
311 | |
312 return c; | |
313 } | |
314 | |
315 // Allocate memory and set function pointers | |
316 static int open(af_instance_t* af){ | |
317 af->control=control; | |
318 af->uninit=uninit; | |
319 af->play=play; | |
320 af->mul.n=1; | |
321 af->mul.d=1; | |
322 af->data=calloc(1,sizeof(af_data_t)); | |
323 af->setup=calloc(1,sizeof(af_resample_t)); | |
324 if(af->data == NULL || af->setup == NULL) | |
325 return AF_ERROR; | |
326 return AF_OK; | |
327 } | |
328 | |
329 // Description of this plugin | |
330 af_info_t af_info_resample = { | |
331 "Sample frequency conversion", | |
332 "resample", | |
333 "Anders", | |
334 "", | |
7615 | 335 AF_FLAGS_REENTRANT, |
7568 | 336 open |
337 }; | |
338 |