How to Use FFmpeg VAAPI for Hardware Deinterlacing
Deinterlacing video using CPU-bound filters can be resource-intensive and slow, often limiting real-time transcoding capabilities. By offloading this task to your GPU via VAAPI (Video Acceleration API), FFmpeg can perform hardware-accelerated deinterlacing on supported Intel and AMD hardware with minimal CPU utilization. This article explains the requirements, filter options, and command-line implementations necessary to deinterlace video streams using FFmpeg and VAAPI.
Prerequisites
To use VAAPI hardware deinterlacing in FFmpeg, ensure you have:
- A supported GPU (Intel integrated graphics or compatible AMD GPUs).
- The appropriate VAAPI drivers installed on your Linux system
(
intel-media-driver,libva-intel-driver, ormesa-va-drivers). - An FFmpeg binary compiled with
--enable-vaapi. - Verification that your hardware device is functional, which can be
checked by running
vainfoin your terminal.
Understanding the
deinterlace_vaapi Filter
The hardware deinterlacing filter in FFmpeg is
deinterlace_vaapi. For this filter to work, the video
frames must reside in GPU memory (hardware surfaces) rather than system
RAM.
Filter Options
mode: The deinterlacing algorithm to apply.default: Uses the driver-chosen default method (often motion-adaptive if supported).bob: Basic bob deinterlacing.weave: Merges fields together without interpolation (rarely desired).motion_adaptive: Applies motion-adaptive interpolation if supported by the hardware.motion_compensated: Applies motion-compensated interpolation (demands higher GPU capability).
rate: Output frame rate control.field: Generates one output frame per pair of fields (retains original nominal frame rate, e.g., 29.97i to 29.97p).frame: Generates one output frame per field, doubling the frame rate (e.g., 29.97i to 59.94p), resulting in smoother motion.
auto: Set to1to only deinterlace frames marked as interlaced.
Example Commands
1. Full Hardware Pipeline (Hardware Decode, Deinterlace, and Encode)
The most efficient approach processes the entire pipeline inside GPU memory to avoid PCIe transfer overhead between RAM and VRAM:
ffmpeg -hwaccel vaapi \
-hwaccel_output_format vaapi \
-vaapi_device /dev/dri/renderD128 \
-i input_interlaced.ts \
-vf "deinterlace_vaapi=rate=frame:mode=default" \
-c:v h264_vaapi \
-b:v 5M \
-c:a copy \
output.mp4-hwaccel vaapi: Enables hardware-accelerated decoding.-hwaccel_output_format vaapi: Keeps decoded frames in GPU memory.-vaapi_device /dev/dri/renderD128: Specifies the direct rendering device.-vf "deinterlace_vaapi=rate=frame:mode=default": Deinterlaces at double the frame rate for smooth playback.-c:v h264_vaapi: Encodes the output using the VAAPI H.264 hardware encoder.
2. Hardware Deinterlace with Software Encoding (CPU Output)
If you prefer higher-quality CPU encoding (such as
libx264) but still want to offload decoding and
deinterlacing to the GPU, download the frames back to system memory:
ffmpeg -hwaccel vaapi \
-hwaccel_output_format vaapi \
-vaapi_device /dev/dri/renderD128 \
-i input_interlaced.ts \
-vf "deinterlace_vaapi=rate=field,hwdownload,format=nv12" \
-c:v libx264 \
-crf 20 \
-preset fast \
-c:a copy \
output.mp4hwdownload: Transmits the processed frames from GPU memory back to system memory.format=nv12: Converts the downloaded hardware format to a pixel format supported by standard software encoders.
3. Software Decode with Hardware Deinterlace
If the input codec is not supported for VAAPI decoding, frames can be decoded by the CPU, uploaded to the GPU for deinterlacing, and encoded via hardware:
ffmpeg -vaapi_device /dev/dri/renderD128 \
-i input_interlaced.ts \
-vf "format=nv12,hwupload,deinterlace_vaapi=rate=frame" \
-c:v h264_vaapi \
-b:v 5M \
-c:a copy \
output.mp4format=nv12,hwupload: Prepares CPU memory frames and uploads them into VAAPI surfaces sodeinterlace_vaapican accept the input.