Mercurial > mplayer.hg
annotate libaf/af_resample.c @ 8839:c932c17042bf
For solaris, sys/smedia.h header is not needed any more. Compilation was
failing on Solaris 7, where sys/smedia.h is not available.
author | jkeil |
---|---|
date | Wed, 08 Jan 2003 14:11:33 +0000 |
parents | 906f7a2dc085 |
children | 558f0b1f45ee |
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 | |
8607 | 28 #if !defined(HAVE_MMX) // This machine is slow |
29 #define L8 | |
30 #else | |
31 #define L16 | |
32 #endif | |
7568 | 33 |
8607 | 34 #include "af_resample.h" |
7568 | 35 |
8607 | 36 // Filtering types |
37 #define TYPE_LIN 0 // Linear interpolation | |
38 #define TYPE_INT 1 // 16 bit integer | |
39 #define TYPE_FLOAT 2 // 32 bit floating point | |
7568 | 40 |
8607 | 41 // Accuracy for linear interpolation |
42 #define STEPACCURACY 32 | |
7568 | 43 |
44 // local data | |
45 typedef struct af_resample_s | |
46 { | |
8607 | 47 void* w; // Current filter weights |
48 void** xq; // Circular buffers | |
7580 | 49 uint32_t xi; // Index for circular buffers |
50 uint32_t wi; // Index for w | |
8607 | 51 uint32_t i; // Number of new samples to put in x queue |
7580 | 52 uint32_t dn; // Down sampling factor |
53 uint32_t up; // Up sampling factor | |
8607 | 54 uint64_t step; // Step size for linear interpolation |
55 uint64_t pt; // Pointer remainder for linear interpolation | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
56 int sloppy; // Enable sloppy resampling to reduce memory usage |
8607 | 57 int type; // Filter type |
7568 | 58 } af_resample_t; |
59 | |
60 // Euclids algorithm for calculating Greatest Common Divisor GCD(a,b) | |
7894 | 61 static inline int gcd(register int a, register int b) |
7568 | 62 { |
63 register int r = min(a,b); | |
64 a=max(a,b); | |
65 b=r; | |
66 | |
67 r=a%b; | |
68 while(r!=0){ | |
69 a=b; | |
70 b=r; | |
71 r=a%b; | |
72 } | |
73 return b; | |
74 } | |
75 | |
8607 | 76 // Fast linear interpolation resample with modest audio quality |
77 static int linint(af_data_t* c,af_data_t* l, af_resample_t* s) | |
7568 | 78 { |
8607 | 79 uint32_t len = 0; // Number of input samples |
80 uint32_t nch = l->nch; // Words pre transfer | |
81 uint64_t step = s->step; | |
82 int16_t* in16 = ((int16_t*)c->audio); | |
83 int16_t* out16 = ((int16_t*)l->audio); | |
84 int32_t* in32 = ((int32_t*)c->audio); | |
85 int32_t* out32 = ((int32_t*)l->audio); | |
86 uint64_t end = ((((uint64_t)c->len)/2LL)<<STEPACCURACY); | |
87 uint64_t pt = s->pt; | |
88 uint16_t tmp; | |
89 | |
90 switch (nch){ | |
91 case 1: | |
92 while(pt < end){ | |
93 out16[len++]=in16[pt>>STEPACCURACY]; | |
94 pt+=step; | |
7568 | 95 } |
8607 | 96 s->pt=pt & ((1LL<<STEPACCURACY)-1); |
97 break; | |
98 case 2: | |
99 end/=2; | |
100 while(pt < end){ | |
101 out32[len++]=in32[pt>>STEPACCURACY]; | |
102 pt+=step; | |
103 } | |
104 len=(len<<1); | |
105 s->pt=pt & ((1LL<<STEPACCURACY)-1); | |
106 break; | |
107 default: | |
108 end /=nch; | |
109 while(pt < end){ | |
110 tmp=nch; | |
111 do { | |
112 tmp--; | |
113 out16[len+tmp]=in16[tmp+(pt>>STEPACCURACY)*nch]; | |
114 } while (tmp); | |
115 len+=nch; | |
116 pt+=step; | |
117 } | |
118 s->pt=pt & ((1LL<<STEPACCURACY)-1); | |
7568 | 119 } |
120 return len; | |
121 } | |
122 | |
123 // Initialization and runtime control | |
124 static int control(struct af_instance_s* af, int cmd, void* arg) | |
125 { | |
126 switch(cmd){ | |
127 case AF_CONTROL_REINIT:{ | |
128 af_resample_t* s = (af_resample_t*)af->setup; | |
129 af_data_t* n = (af_data_t*)arg; // New configureation | |
130 int i,d = 0; | |
131 int rv = AF_OK; | |
8607 | 132 size_t tsz = (s->type==TYPE_INT) ? sizeof(int16_t) : sizeof(float); |
7568 | 133 |
134 // Make sure this filter isn't redundant | |
8711
906f7a2dc085
sig 11 fix in reinit and resample + spelling error fixes
anders
parents:
8607
diff
changeset
|
135 if((af->data->rate == n->rate) || (af->data->rate == 0)) |
7568 | 136 return AF_DETACH; |
137 | |
8607 | 138 // If linear interpolation |
139 if(s->type == TYPE_LIN){ | |
140 s->pt=0LL; | |
141 s->step=((uint64_t)n->rate<<STEPACCURACY)/(uint64_t)af->data->rate+1LL; | |
142 af_msg(AF_MSG_VERBOSE,"[resample] Linear interpolation step: 0x%016X.\n", | |
143 s->step); | |
144 af->mul.n = af->data->rate; | |
145 af->mul.d = n->rate; | |
146 } | |
147 | |
7568 | 148 // Create space for circular bufers (if nesessary) |
8607 | 149 if((af->data->nch != n->nch) && (s->type != TYPE_LIN)){ |
7568 | 150 // First free the old ones |
151 if(s->xq){ | |
152 for(i=1;i<af->data->nch;i++) | |
153 if(s->xq[i]) | |
154 free(s->xq[i]); | |
155 free(s->xq); | |
156 } | |
157 // ... then create new | |
8607 | 158 s->xq = malloc(n->nch*sizeof(void*)); |
7568 | 159 for(i=0;i<n->nch;i++) |
8607 | 160 s->xq[i] = malloc(2*L*tsz); |
7568 | 161 s->xi = 0; |
162 } | |
163 | |
164 // Set parameters | |
165 af->data->nch = n->nch; | |
8607 | 166 if(s->type == TYPE_INT || s->type == TYPE_LIN){ |
167 af->data->format = AF_FORMAT_NE | AF_FORMAT_SI; | |
168 af->data->bps = 2; | |
169 } | |
170 else{ | |
171 af->data->format = AF_FORMAT_NE | AF_FORMAT_F; | |
172 af->data->bps = 4; | |
173 } | |
7568 | 174 if(af->data->format != n->format || af->data->bps != n->bps) |
175 rv = AF_FALSE; | |
8607 | 176 n->format = af->data->format; |
177 n->bps = af->data->bps; | |
178 | |
179 // If linear interpolation is used the setup is done. | |
180 if(s->type == TYPE_LIN) | |
181 return rv; | |
7568 | 182 |
183 // Calculate up and down sampling factors | |
184 d=gcd(af->data->rate,n->rate); | |
185 | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
186 // 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
|
187 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
|
188 int up=af->data->rate/2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
189 int dn=n->rate/2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
190 int m=2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
191 while(af->data->rate/(d*m) > 5000){ |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
192 d=gcd(up,dn); |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
193 up/=2; dn/=2; m*=2; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
194 } |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
195 d*=m; |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
196 } |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
197 |
7568 | 198 // Check if the the design needs to be redone |
199 if(s->up != af->data->rate/d || s->dn != n->rate/d){ | |
200 float* w; | |
201 float* wt; | |
202 float fc; | |
203 int j; | |
204 s->up = af->data->rate/d; | |
205 s->dn = n->rate/d; | |
206 | |
207 // Calculate cuttof frequency for filter | |
208 fc = 1/(float)(max(s->up,s->dn)); | |
209 // Allocate space for polyphase filter bank and protptype filter | |
210 w = malloc(sizeof(float) * s->up *L); | |
211 if(NULL != s->w) | |
212 free(s->w); | |
8607 | 213 s->w = malloc(L*s->up*tsz); |
7568 | 214 |
215 // Design prototype filter type using Kaiser window with beta = 10 | |
216 if(NULL == w || NULL == s->w || | |
217 -1 == design_fir(s->up*L, w, &fc, LP|KAISER , 10.0)){ | |
8167 | 218 af_msg(AF_MSG_ERROR,"[resample] Unable to design prototype filter.\n"); |
7568 | 219 return AF_ERROR; |
220 } | |
221 // Copy data from prototype to polyphase filter | |
222 wt=w; | |
223 for(j=0;j<L;j++){//Columns | |
224 for(i=0;i<s->up;i++){//Rows | |
8607 | 225 if(s->type == TYPE_INT){ |
226 float t=(float)s->up*32767.0*(*wt); | |
227 ((int16_t*)s->w)[i*L+j] = (int16_t)((t>=0.0)?(t+0.5):(t-0.5)); | |
228 } | |
229 else | |
230 ((float*)s->w)[i*L+j] = (float)s->up*(*wt); | |
7568 | 231 wt++; |
232 } | |
233 } | |
234 free(w); | |
8607 | 235 af_msg(AF_MSG_VERBOSE,"[resample] New filter designed up: %i " |
236 "down: %i\n", s->up, s->dn); | |
7568 | 237 } |
238 | |
7665
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
239 // Set multiplier and delay |
fbd5445cc853
Adding function for calculating the delay caused by the filters
anders
parents:
7616
diff
changeset
|
240 af->delay = (double)(1000*L/2)/((double)n->rate); |
7568 | 241 af->mul.n = s->up; |
242 af->mul.d = s->dn; | |
243 return rv; | |
244 } | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
245 case AF_CONTROL_COMMAND_LINE:{ |
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
246 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
|
247 int rate=0; |
8607 | 248 int lin=0; |
249 sscanf((char*)arg,"%i:%i:%i", &rate, &(s->sloppy), &lin); | |
250 if(lin) | |
251 s->type = TYPE_LIN; | |
252 return af->control(af,AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET, &rate); | |
7998
d48a06d07afb
Adding commandline options for filters and fixing stupid bug in cfg
anders
parents:
7894
diff
changeset
|
253 } |
8607 | 254 case AF_CONTROL_POST_CREATE: |
255 ((af_resample_t*)af->setup)->type = | |
256 ((af_cfg_t*)arg)->force == AF_INIT_SLOW ? TYPE_INT : TYPE_FLOAT; | |
257 return AF_OK; | |
258 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
7568 | 259 // Reinit must be called after this function has been called |
260 | |
261 // Sanity check | |
7616 | 262 if(((int*)arg)[0] < 8000 || ((int*)arg)[0] > 192000){ |
8607 | 263 af_msg(AF_MSG_ERROR,"[resample] The output sample frequency " |
264 "must be between 8kHz and 192kHz. Current value is %i \n", | |
265 ((int*)arg)[0]); | |
7568 | 266 return AF_ERROR; |
267 } | |
268 | |
269 af->data->rate=((int*)arg)[0]; | |
8607 | 270 af_msg(AF_MSG_VERBOSE,"[resample] Changing sample rate " |
271 "to %iHz\n",af->data->rate); | |
7568 | 272 return AF_OK; |
273 } | |
274 return AF_UNKNOWN; | |
275 } | |
276 | |
277 // Deallocate memory | |
278 static void uninit(struct af_instance_s* af) | |
279 { | |
280 if(af->data) | |
281 free(af->data); | |
282 } | |
283 | |
284 // Filter data through filter | |
285 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
286 { | |
287 int len = 0; // Length of output data | |
288 af_data_t* c = data; // Current working data | |
289 af_data_t* l = af->data; // Local data | |
290 af_resample_t* s = (af_resample_t*)af->setup; | |
291 | |
292 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
293 return NULL; | |
294 | |
295 // Run resampling | |
8607 | 296 switch(s->type){ |
297 case(TYPE_INT): | |
298 # define FORMAT_I 1 | |
299 if(s->up>s->dn){ | |
300 # define UP | |
301 # include "af_resample.h" | |
302 # undef UP | |
303 } | |
304 else{ | |
305 # define DN | |
306 # include "af_resample.h" | |
307 # undef DN | |
308 } | |
309 break; | |
310 case(TYPE_FLOAT): | |
311 # undef FORMAT_I | |
312 # define FORMAT_F 1 | |
313 if(s->up>s->dn){ | |
314 # define UP | |
315 # include "af_resample.h" | |
316 # undef UP | |
317 } | |
318 else{ | |
319 # define DN | |
320 # include "af_resample.h" | |
321 # undef DN | |
322 } | |
323 break; | |
324 case(TYPE_LIN): | |
325 len = linint(c, l, s); | |
326 break; | |
327 } | |
7568 | 328 |
329 // Set output data | |
330 c->audio = l->audio; | |
8607 | 331 c->len = len*l->bps; |
7568 | 332 c->rate = l->rate; |
333 | |
334 return c; | |
335 } | |
336 | |
337 // Allocate memory and set function pointers | |
338 static int open(af_instance_t* af){ | |
339 af->control=control; | |
340 af->uninit=uninit; | |
341 af->play=play; | |
342 af->mul.n=1; | |
343 af->mul.d=1; | |
344 af->data=calloc(1,sizeof(af_data_t)); | |
345 af->setup=calloc(1,sizeof(af_resample_t)); | |
346 if(af->data == NULL || af->setup == NULL) | |
347 return AF_ERROR; | |
348 return AF_OK; | |
349 } | |
350 | |
351 // Description of this plugin | |
352 af_info_t af_info_resample = { | |
353 "Sample frequency conversion", | |
354 "resample", | |
355 "Anders", | |
356 "", | |
7615 | 357 AF_FLAGS_REENTRANT, |
7568 | 358 open |
359 }; | |
360 |