Mercurial > mplayer.hg
annotate libaf/af_resample.c @ 8090:9fecd5be36ab
Suddenly I needed vidix.h for vidix_video_eq_t
author | mswitch |
---|---|
date | Sun, 03 Nov 2002 20:26:47 +0000 |
parents | a7fa2d14ee91 |
children | e8832e66babd |
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 | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
72 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
|
73 int fast; // Enable linear interpolation instead of filtering |
7568 | 74 } af_resample_t; |
75 | |
76 // Euclids algorithm for calculating Greatest Common Divisor GCD(a,b) | |
7894 | 77 static inline int gcd(register int a, register int b) |
7568 | 78 { |
79 register int r = min(a,b); | |
80 a=max(a,b); | |
81 b=r; | |
82 | |
83 r=a%b; | |
84 while(r!=0){ | |
85 a=b; | |
86 b=r; | |
87 r=a%b; | |
88 } | |
89 return b; | |
90 } | |
91 | |
92 static int upsample(af_data_t* c,af_data_t* l, af_resample_t* s) | |
93 { | |
7580 | 94 uint32_t ci = l->nch; // Index for channels |
95 uint32_t len = 0; // Number of input samples | |
96 uint32_t nch = l->nch; // Number of channels | |
97 uint32_t inc = s->up/s->dn; | |
98 uint32_t level = s->up%s->dn; | |
99 uint32_t up = s->up; | |
100 uint32_t dn = s->dn; | |
7568 | 101 |
102 register int16_t* w = s->w; | |
7580 | 103 register uint32_t wi = 0; |
104 register uint32_t xi = 0; | |
7568 | 105 |
106 // Index current channel | |
107 while(ci--){ | |
108 // Temporary pointers | |
109 register int16_t* x = s->xq[ci]; | |
110 register int16_t* in = ((int16_t*)c->audio)+ci; | |
111 register int16_t* out = ((int16_t*)l->audio)+ci; | |
112 int16_t* end = in+c->len/2; // Block loop end | |
113 wi = s->wi; xi = s->xi; | |
114 | |
115 while(in < end){ | |
7580 | 116 register uint32_t i = inc; |
7568 | 117 if(wi<level) i++; |
118 | |
119 ADDQUE(xi,x,in); | |
120 in+=nch; | |
121 while(i--){ | |
122 // Run the FIR filter | |
123 FIR((&x[xi]),(&w[wi*L]),out); | |
124 len++; out+=nch; | |
125 // Update wi to point at the correct polyphase component | |
126 wi=(wi+dn)%up; | |
127 } | |
128 } | |
129 } | |
130 // Save values that needs to be kept for next time | |
131 s->wi = wi; | |
132 s->xi = xi; | |
133 return len; | |
134 } | |
135 | |
136 static int downsample(af_data_t* c,af_data_t* l, af_resample_t* s) | |
137 { | |
7580 | 138 uint32_t ci = l->nch; // Index for channels |
139 uint32_t len = 0; // Number of output samples | |
140 uint32_t nch = l->nch; // Number of channels | |
141 uint32_t inc = s->dn/s->up; | |
142 uint32_t level = s->dn%s->up; | |
143 uint32_t up = s->up; | |
144 uint32_t dn = s->dn; | |
7568 | 145 |
7587 | 146 register int32_t i = 0; |
7580 | 147 register uint32_t wi = 0; |
148 register uint32_t xi = 0; | |
7568 | 149 |
150 // Index current channel | |
151 while(ci--){ | |
152 // Temporary pointers | |
153 register int16_t* x = s->xq[ci]; | |
154 register int16_t* in = ((int16_t*)c->audio)+ci; | |
155 register int16_t* out = ((int16_t*)l->audio)+ci; | |
156 register int16_t* end = in+c->len/2; // Block loop end | |
157 i = s->i; wi = s->wi; xi = s->xi; | |
158 | |
159 while(in < end){ | |
160 | |
161 ADDQUE(xi,x,in); | |
162 in+=nch; | |
7587 | 163 if((--i)<=0){ |
7568 | 164 // Run the FIR filter |
165 FIR((&x[xi]),(&s->w[wi*L]),out); | |
166 len++; out+=nch; | |
167 | |
168 // Update wi to point at the correct polyphase component | |
169 wi=(wi+dn)%up; | |
170 | |
171 // Insert i number of new samples in queue | |
172 i = inc; | |
173 if(wi<level) i++; | |
174 } | |
175 } | |
176 } | |
177 // Save values that needs to be kept for next time | |
178 s->wi = wi; | |
179 s->xi = xi; | |
180 s->i = i; | |
181 | |
182 return len; | |
183 } | |
184 | |
185 // Initialization and runtime control | |
186 static int control(struct af_instance_s* af, int cmd, void* arg) | |
187 { | |
188 switch(cmd){ | |
189 case AF_CONTROL_REINIT:{ | |
190 af_resample_t* s = (af_resample_t*)af->setup; | |
191 af_data_t* n = (af_data_t*)arg; // New configureation | |
192 int i,d = 0; | |
193 int rv = AF_OK; | |
194 | |
195 // Make sure this filter isn't redundant | |
196 if(af->data->rate == n->rate) | |
197 return AF_DETACH; | |
198 | |
199 // Create space for circular bufers (if nesessary) | |
200 if(af->data->nch != n->nch){ | |
201 // First free the old ones | |
202 if(s->xq){ | |
203 for(i=1;i<af->data->nch;i++) | |
204 if(s->xq[i]) | |
205 free(s->xq[i]); | |
206 free(s->xq); | |
207 } | |
208 // ... then create new | |
209 s->xq = malloc(n->nch*sizeof(int16_t*)); | |
210 for(i=0;i<n->nch;i++) | |
211 s->xq[i] = malloc(2*L*sizeof(int16_t)); | |
212 s->xi = 0; | |
213 } | |
214 | |
215 // Set parameters | |
216 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
|
217 af->data->format = AFMT_S16_NE; |
7568 | 218 af->data->bps = 2; |
219 if(af->data->format != n->format || af->data->bps != n->bps) | |
220 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
|
221 n->format = AFMT_S16_NE; |
7568 | 222 n->bps = 2; |
223 | |
224 // Calculate up and down sampling factors | |
225 d=gcd(af->data->rate,n->rate); | |
226 | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
227 // 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
|
228 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
|
229 int up=af->data->rate/2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
230 int dn=n->rate/2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
231 int m=2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
232 while(af->data->rate/(d*m) > 5000){ |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
233 d=gcd(up,dn); |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
234 up/=2; dn/=2; m*=2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
235 } |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
236 d*=m; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
237 } |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
238 |
7568 | 239 // Check if the the design needs to be redone |
240 if(s->up != af->data->rate/d || s->dn != n->rate/d){ | |
241 float* w; | |
242 float* wt; | |
243 float fc; | |
244 int j; | |
245 s->up = af->data->rate/d; | |
246 s->dn = n->rate/d; | |
247 | |
248 // Calculate cuttof frequency for filter | |
249 fc = 1/(float)(max(s->up,s->dn)); | |
250 // Allocate space for polyphase filter bank and protptype filter | |
251 w = malloc(sizeof(float) * s->up *L); | |
252 if(NULL != s->w) | |
253 free(s->w); | |
254 s->w = malloc(L*s->up*sizeof(int16_t)); | |
255 | |
256 // Design prototype filter type using Kaiser window with beta = 10 | |
257 if(NULL == w || NULL == s->w || | |
258 -1 == design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){ | |
259 mp_msg(MSGT_AFILTER,MSGL_ERR,"[resample] Unable to design prototype filter.\n"); | |
260 return AF_ERROR; | |
261 } | |
262 // Copy data from prototype to polyphase filter | |
263 wt=w; | |
264 for(j=0;j<L;j++){//Columns | |
265 for(i=0;i<s->up;i++){//Rows | |
266 float t=(float)s->up*32767.0*(*wt); | |
267 s->w[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5)); | |
268 wt++; | |
269 } | |
270 } | |
271 free(w); | |
272 mp_msg(MSGT_AFILTER,MSGL_V,"[resample] New filter designed up: %i down: %i\n", s->up, s->dn); | |
273 } | |
274 | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
275 // Set multiplier and delay |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
276 af->delay = (double)(1000*L/2)/((double)n->rate); |
7568 | 277 af->mul.n = s->up; |
278 af->mul.d = s->dn; | |
279 return rv; | |
280 } | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
281 case AF_CONTROL_COMMAND_LINE:{ |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
282 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
|
283 int rate=0; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
284 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
|
285 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
|
286 } |
7568 | 287 case AF_CONTROL_RESAMPLE: |
288 // Reinit must be called after this function has been called | |
289 | |
290 // Sanity check | |
7616 | 291 if(((int*)arg)[0] < 8000 || ((int*)arg)[0] > 192000){ |
292 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 | 293 return AF_ERROR; |
294 } | |
295 | |
296 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
|
297 mp_msg(MSGT_AFILTER,MSGL_V,"[resample] Changing sample rate to %iHz\n",af->data->rate); |
7568 | 298 return AF_OK; |
299 } | |
300 return AF_UNKNOWN; | |
301 } | |
302 | |
303 // Deallocate memory | |
304 static void uninit(struct af_instance_s* af) | |
305 { | |
306 if(af->data) | |
307 free(af->data); | |
308 } | |
309 | |
310 // Filter data through filter | |
311 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
312 { | |
313 int len = 0; // Length of output data | |
314 af_data_t* c = data; // Current working data | |
315 af_data_t* l = af->data; // Local data | |
316 af_resample_t* s = (af_resample_t*)af->setup; | |
317 | |
318 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
319 return NULL; | |
320 | |
321 // Run resampling | |
322 if(s->up>s->dn) | |
323 len = upsample(c,l,s); | |
324 else | |
325 len = downsample(c,l,s); | |
326 | |
327 // Set output data | |
328 c->audio = l->audio; | |
329 c->len = len*2; | |
330 c->rate = l->rate; | |
331 | |
332 return c; | |
333 } | |
334 | |
335 // Allocate memory and set function pointers | |
336 static int open(af_instance_t* af){ | |
337 af->control=control; | |
338 af->uninit=uninit; | |
339 af->play=play; | |
340 af->mul.n=1; | |
341 af->mul.d=1; | |
342 af->data=calloc(1,sizeof(af_data_t)); | |
343 af->setup=calloc(1,sizeof(af_resample_t)); | |
344 if(af->data == NULL || af->setup == NULL) | |
345 return AF_ERROR; | |
346 return AF_OK; | |
347 } | |
348 | |
349 // Description of this plugin | |
350 af_info_t af_info_resample = { | |
351 "Sample frequency conversion", | |
352 "resample", | |
353 "Anders", | |
354 "", | |
7615 | 355 AF_FLAGS_REENTRANT, |
7568 | 356 open |
357 }; | |
358 |