1 /*=============================================================================
3 // This software has been released under the terms of the GNU General Public
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
8 //=============================================================================
11 /* This audio filter changes the sample rate. */
16 #include "libavutil/common.h"
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 filterlength is choosen to 8 if the machine is
25 slow and to 16 if the machine is fast and has MMX.
28 #if !defined(HAVE_MMX) // This machine is slow
34 #include "af_resample.h"
37 #define RSMP_LIN (0<<0) // Linear interpolation
38 #define RSMP_INT (1<<0) // 16 bit integer
39 #define RSMP_FLOAT (2<<0) // 32 bit floating point
40 #define RSMP_MASK (3<<0)
42 // Defines for sloppy or exact resampling
43 #define FREQ_SLOPPY (0<<2)
44 #define FREQ_EXACT (1<<2)
45 #define FREQ_MASK (1<<2)
47 // Accuracy for linear interpolation
48 #define STEPACCURACY 32
51 typedef struct af_resample_s
53 void* w
; // Current filter weights
54 void** xq
; // Circular buffers
55 uint32_t xi
; // Index for circular buffers
56 uint32_t wi
; // Index for w
57 uint32_t i
; // Number of new samples to put in x queue
58 uint32_t dn
; // Down sampling factor
59 uint32_t up
; // Up sampling factor
60 uint64_t step
; // Step size for linear interpolation
61 uint64_t pt
; // Pointer remainder for linear interpolation
62 int setup
; // Setup parameters cmdline or through postcreate
65 // Fast linear interpolation resample with modest audio quality
66 static int linint(af_data_t
* c
,af_data_t
* l
, af_resample_t
* s
)
68 uint32_t len
= 0; // Number of input samples
69 uint32_t nch
= l
->nch
; // Words pre transfer
70 uint64_t step
= s
->step
;
71 int16_t* in16
= ((int16_t*)c
->audio
);
72 int16_t* out16
= ((int16_t*)l
->audio
);
73 int32_t* in32
= ((int32_t*)c
->audio
);
74 int32_t* out32
= ((int32_t*)l
->audio
);
75 uint64_t end
= ((((uint64_t)c
->len
)/2LL)<<STEPACCURACY
);
82 out16
[len
++]=in16
[pt
>>STEPACCURACY
];
85 s
->pt
=pt
& ((1LL<<STEPACCURACY
)-1);
90 out32
[len
++]=in32
[pt
>>STEPACCURACY
];
94 s
->pt
=pt
& ((1LL<<STEPACCURACY
)-1);
102 out16
[len
+tmp
]=in16
[tmp
+(pt
>>STEPACCURACY
)*nch
];
107 s
->pt
=pt
& ((1LL<<STEPACCURACY
)-1);
112 /* Determine resampling type and format */
113 static int set_types(struct af_instance_s
* af
, af_data_t
* data
)
115 af_resample_t
* s
= af
->setup
;
119 // Make sure this filter isn't redundant
120 if((af
->data
->rate
== data
->rate
) || (af
->data
->rate
== 0))
122 /* If sloppy and small resampling difference (2%) */
123 rd
= abs((float)af
->data
->rate
- (float)data
->rate
)/(float)data
->rate
;
124 if((((s
->setup
& FREQ_MASK
) == FREQ_SLOPPY
) && (rd
< 0.02) &&
125 (data
->format
!= (AF_FORMAT_FLOAT_NE
))) ||
126 ((s
->setup
& RSMP_MASK
) == RSMP_LIN
)){
127 s
->setup
= (s
->setup
& ~RSMP_MASK
) | RSMP_LIN
;
128 af
->data
->format
= AF_FORMAT_S16_NE
;
130 af_msg(AF_MSG_VERBOSE
,"[resample] Using linear interpolation. \n");
133 /* If the input format is float or if float is explicitly selected
134 use float, otherwise use int */
135 if((data
->format
== (AF_FORMAT_FLOAT_NE
)) ||
136 ((s
->setup
& RSMP_MASK
) == RSMP_FLOAT
)){
137 s
->setup
= (s
->setup
& ~RSMP_MASK
) | RSMP_FLOAT
;
138 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
142 s
->setup
= (s
->setup
& ~RSMP_MASK
) | RSMP_INT
;
143 af
->data
->format
= AF_FORMAT_S16_NE
;
146 af_msg(AF_MSG_VERBOSE
,"[resample] Using %s processing and %s frequecy"
148 ((s
->setup
& RSMP_MASK
) == RSMP_FLOAT
)?"floating point":"integer",
149 ((s
->setup
& FREQ_MASK
) == FREQ_SLOPPY
)?"inexact":"exact");
152 if(af
->data
->format
!= data
->format
|| af
->data
->bps
!= data
->bps
)
154 data
->format
= af
->data
->format
;
155 data
->bps
= af
->data
->bps
;
156 af
->data
->nch
= data
->nch
;
160 // Initialization and runtime control
161 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
164 case AF_CONTROL_REINIT
:{
165 af_resample_t
* s
= (af_resample_t
*)af
->setup
;
166 af_data_t
* n
= (af_data_t
*)arg
; // New configureation
170 // Free space for circular bufers
172 for(i
=1;i
<af
->data
->nch
;i
++)
179 if(AF_DETACH
== (rv
= set_types(af
,n
)))
182 // If linear interpolation
183 if((s
->setup
& RSMP_MASK
) == RSMP_LIN
){
185 s
->step
=((uint64_t)n
->rate
<<STEPACCURACY
)/(uint64_t)af
->data
->rate
+1LL;
186 af_msg(AF_MSG_DEBUG0
,"[resample] Linear interpolation step: 0x%016"PRIX64
".\n",
188 af
->mul
= (double)af
->data
->rate
/ n
->rate
;
192 // Calculate up and down sampling factors
193 d
=ff_gcd(af
->data
->rate
,n
->rate
);
195 // If sloppy resampling is enabled limit the upsampling factor
196 if(((s
->setup
& FREQ_MASK
) == FREQ_SLOPPY
) && (af
->data
->rate
/d
> 5000)){
197 int up
=af
->data
->rate
/2;
200 while(af
->data
->rate
/(d
*m
) > 5000){
207 // Create space for circular bufers
208 s
->xq
= malloc(n
->nch
*sizeof(void*));
209 for(i
=0;i
<n
->nch
;i
++)
210 s
->xq
[i
] = malloc(2*L
*af
->data
->bps
);
213 // Check if the the design needs to be redone
214 if(s
->up
!= af
->data
->rate
/d
|| s
->dn
!= n
->rate
/d
){
219 s
->up
= af
->data
->rate
/d
;
224 // Calculate cuttof frequency for filter
225 fc
= 1/(float)(max(s
->up
,s
->dn
));
226 // Allocate space for polyphase filter bank and protptype filter
227 w
= malloc(sizeof(float) * s
->up
*L
);
230 s
->w
= malloc(L
*s
->up
*af
->data
->bps
);
232 // Design prototype filter type using Kaiser window with beta = 10
233 if(NULL
== w
|| NULL
== s
->w
||
234 -1 == af_filter_design_fir(s
->up
*L
, w
, &fc
, LP
|KAISER
, 10.0)){
235 af_msg(AF_MSG_ERROR
,"[resample] Unable to design prototype filter.\n");
238 // Copy data from prototype to polyphase filter
240 for(j
=0;j
<L
;j
++){//Columns
241 for(i
=0;i
<s
->up
;i
++){//Rows
242 if((s
->setup
& RSMP_MASK
) == RSMP_INT
){
243 float t
=(float)s
->up
*32767.0*(*wt
);
244 ((int16_t*)s
->w
)[i
*L
+j
] = (int16_t)((t
>=0.0)?(t
+0.5):(t
-0.5));
247 ((float*)s
->w
)[i
*L
+j
] = (float)s
->up
*(*wt
);
252 af_msg(AF_MSG_VERBOSE
,"[resample] New filter designed up: %i "
253 "down: %i\n", s
->up
, s
->dn
);
256 // Set multiplier and delay
257 af
->delay
= 0; // not set correctly, but shouldn't be too large anyway
258 af
->mul
= (double)s
->up
/ s
->dn
;
261 case AF_CONTROL_COMMAND_LINE
:{
262 af_resample_t
* s
= (af_resample_t
*)af
->setup
;
266 sscanf((char*)arg
,"%i:%i:%i", &rate
, &sloppy
, &type
);
267 s
->setup
= (sloppy
?FREQ_SLOPPY
:FREQ_EXACT
) |
268 (clamp(type
,RSMP_LIN
,RSMP_FLOAT
));
269 return af
->control(af
,AF_CONTROL_RESAMPLE_RATE
| AF_CONTROL_SET
, &rate
);
271 case AF_CONTROL_POST_CREATE
:
272 if((((af_cfg_t
*)arg
)->force
& AF_INIT_FORMAT_MASK
) == AF_INIT_FLOAT
)
273 ((af_resample_t
*)af
->setup
)->setup
= RSMP_FLOAT
;
275 case AF_CONTROL_RESAMPLE_RATE
| AF_CONTROL_SET
:
276 // Reinit must be called after this function has been called
279 if(((int*)arg
)[0] < 8000 || ((int*)arg
)[0] > 192000){
280 af_msg(AF_MSG_ERROR
,"[resample] The output sample frequency "
281 "must be between 8kHz and 192kHz. Current value is %i \n",
286 af
->data
->rate
=((int*)arg
)[0];
287 af_msg(AF_MSG_VERBOSE
,"[resample] Changing sample rate "
288 "to %iHz\n",af
->data
->rate
);
295 static void uninit(struct af_instance_s
* af
)
298 free(af
->data
->audio
);
302 // Filter data through filter
303 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
305 int len
= 0; // Length of output data
306 af_data_t
* c
= data
; // Current working data
307 af_data_t
* l
= af
->data
; // Local data
308 af_resample_t
* s
= (af_resample_t
*)af
->setup
;
310 if(AF_OK
!= RESIZE_LOCAL_BUFFER(af
,data
))
314 switch(s
->setup
& RSMP_MASK
){
319 # include "af_resample.h"
324 # include "af_resample.h"
333 # include "af_resample.h"
338 # include "af_resample.h"
343 len
= linint(c
, l
, s
);
355 // Allocate memory and set function pointers
356 static int af_open(af_instance_t
* af
){
361 af
->data
=calloc(1,sizeof(af_data_t
));
362 af
->setup
=calloc(1,sizeof(af_resample_t
));
363 if(af
->data
== NULL
|| af
->setup
== NULL
)
365 ((af_resample_t
*)af
->setup
)->setup
= RSMP_INT
| FREQ_SLOPPY
;
369 // Description of this plugin
370 af_info_t af_info_resample
= {
371 "Sample frequency conversion",