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