Configure FFmpeg afir Filter Frequency Domain Convolution
This article explains how to configure the frequency-domain
convolution settings in the FFmpeg afir (Finite Impulse
Response) filter. You will learn how to adjust FFT partition sizes using
the maxp and minp parameters to optimize the
balance between CPU performance and audio latency, complete with
practical command-line examples.
How the
afir Filter Uses Frequency-Domain Convolution
The afir filter applies an impulse response (IR) to an
audio stream using partitioned frequency-domain convolution
(specifically, the overlap-save method). Instead of calculating
convolution in the time domain—which is computationally expensive for
long impulse responses—FFmpeg converts the audio into the frequency
domain using Fast Fourier Transforms (FFTs).
By partitioning the impulse response into smaller blocks, FFmpeg can process audio with significantly lower latency while maintaining high computational efficiency.
Key Configuration Parameters
To control how the frequency-domain convolution is partitioned, you
must configure two main parameters in the afir filter:
maxp(Maximum Partition Size): Sets the maximum block size (in samples) for the FFT partition. The value must be a power of two. Larger block sizes are more CPU-efficient but introduce higher latency. The default is typically8192.minp(Minimum Partition Size): Sets the minimum block size (in samples) for the partition. Reducing this value allows the filter to process the beginning of the impulse response with lower latency. The default is typically8192.
Choosing the Right Settings
1. Low-Latency Configuration
If you are using the filter for real-time audio processing (such as live streaming or monitoring), you should reduce the partition sizes. This forces FFmpeg to use smaller FFT blocks, reducing the time delay before audio is output.
- Recommended settings:
minp=1024:maxp=2048(or lower, down to128or256depending on your hardware limits).
2. High-Efficiency Configuration
If you are batch-processing files offline and want to minimize CPU usage, you should increase the partition sizes. Larger blocks allow the FFT algorithm to run much faster at the expense of latency.
- Recommended settings:
minp=16384:maxp=16384(or up to65536for very long impulse responses).
Example Commands
To apply these settings, pass the parameters to the afir
filter inside your FFmpeg command. The filter requires two inputs: the
main audio stream (index 0:a) and the impulse response file
(index 1:a).
Example: Low-Latency Setup
This command configures the convolution engine to use a minimum partition size of 1024 samples and a maximum partition size of 2048 samples:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=minp=1024:maxp=2048[out]" -map "[out]" output.wavExample: High-Efficiency Setup
This command configures the engine to use a uniform, large partition size of 16384 samples, maximizing CPU throughput during offline rendering:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=minp=16384:maxp=16384[out]" -map "[out]" output.wav