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