Configure FFmpeg afir Frequency-Domain Convolution
The FFmpeg afir filter applies Finite Impulse Response
(FIR) filtering to an audio stream using non-uniform frequency-domain
partitioned Fast Fourier Transform (FFT) convolution. By breaking
impulse responses into frequency partitions rather than processing
samples directly in the time domain, afir drastically
reduces CPU consumption for long convolution reverbs and equalization
tasks. This guide details how to configure the critical parameters that
govern frequency-domain processing, partition sizes, latency, and
calculation accuracy in FFmpeg.
Understanding Key Frequency-Domain Parameters
The afir filter performs overlap-save or overlap-add
block convolution using FFT algorithms. You can control the partition
boundaries and internal processing through the following options:
minp(Minimum Partition Size): Defines the smallest FFT block length in samples. Smaller sizes reduce initial latency at the cost of slightly higher CPU overhead. The value must be a power of two between 4 and 65536 (default is typically 16 or 32).maxp(Maximum Partition Size): Defines the largest FFT block length in samples used for the tail of the impulse response. Larger sizes maximize computational efficiency for longer IR files. The value must be a power of two between 4 and 65536 (default is 8192).precision: Sets the internal numerical precision for frequency-domain operations. Valid options include:auto: Selects the best precision matching the input audio format.f32: 32-bit single-precision floating point (faster, ideal for standard multi-track audio).f64: 64-bit double-precision floating point (highest dynamic range and lowest quantization noise).
Basic Command Syntax
The afir filter requires two inputs: the primary audio
stream ([0:a]) and the impulse response file
([1:a]). Apply frequency-domain settings via
filter_complex:
ffmpeg -i input.wav -i impulse_response.wav -filter_complex "[0:a][1:a]afir=minp=32:maxp=4096:precision=f32" output.wavOptimizing for Low-Latency Applications
When processing live audio or streaming feeds where latency must be minimized, constrain the partition sizes so the initial block delay is reduced:
ffmpeg -i input.wav -i ir.wav -filter_complex "[0:a][1:a]afir=minp=16:maxp=1024" output.wav- Setting
minp=16causes the filter to transform and output the initial onset of the impulse response after only 16 samples. - Setting
maxp=1024prevents later partitions from introducing excessive buffer memory accumulation.
Optimizing for High-Efficiency Batch Processing
For file-based processing where latency is irrelevant and minimum processing time is the priority, raise both partition thresholds:
ffmpeg -i input.wav -i ir.wav -filter_complex "[0:a][1:a]afir=minp=512:maxp=16384:precision=f64" output.wav- Higher
minpvalues bypass small FFT transforms, shifting the workload toward larger, highly optimized FFT kernels. - Setting
maxp=16384optimizes CPU cache utilization when convolving long impulse responses, such as large hall reverbs lasting several seconds.
Additional Frequency Convolution Options
irgain: Adjusts the gain applied to the impulse response in the frequency domain before convolution (e.g.,irgain=0.5).irnorm: Normalizes the frequency-domain coefficients to prevent clipping caused by energy accumulation across frequency bins. Set to1(enabled by default) to automatically normalize the IR.dryandwet: Controls the mix ratio between the unconvolved dry signal and the convolved frequency-domain output (e.g.,afir=dry=1:wet=0.7).