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