How to Use tonemap_vaapi Filter in FFmpeg

This article explains how to use the tonemap_vaapi filter in FFmpeg to perform hardware-accelerated High Dynamic Range (HDR) to Standard Dynamic Range (SDR) tone mapping. You will learn the required command-line syntax, key parameters, and a practical example to convert HDR videos using Intel VAAPI hardware acceleration.

Understanding tonemap_vaapi

The tonemap_vaapi filter is a hardware-accelerated filter in FFmpeg designed for Intel graphics processors. It converts HDR video (typically BT.2020 color space) to SDR video (typically BT.709 color space) directly on the GPU. This process significantly reduces CPU usage and increases conversion speed compared to software-based tone mapping filters like zscale or tonemap.

Prerequisites

To use this filter, your system must meet the following requirements: * An Intel processor with integrated graphics or a dedicated Intel GPU supporting VAAPI. * FFmpeg compiled with VAAPI support (--enable-vaapi). * Appropriate graphics drivers (such as intel-media-driver or libva-intel-driver on Linux).

Basic Command Syntax

To use tonemap_vaapi, you must decode the input video in hardware, apply the filter, and then encode the output using a VAAPI encoder (such as h264_vaapi or hevc_vaapi).

Here is a standard command to perform the conversion:

ffmpeg -init_hw_device vaapi=gpu:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input_hdr.mp4 -filter_hw_device gpu -vf "tonemap_vaapi=format=nv12:matrix=bt709:transfer=bt709:primaries=bt709" -c:v h264_vaapi -c:a copy output_sdr.mp4

Command Breakdown

Customizing Tone Mapping Parameters

The tonemap_vaapi filter allows you to fine-tune the output image quality using additional parameters inside the filter chain:

Example with adjustments:

ffmpeg -init_hw_device vaapi=gpu:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -i input_hdr.mp4 -filter_hw_device gpu -vf "tonemap_vaapi=format=nv12:matrix=bt709:transfer=bt709:primaries=bt709:contrast=10:saturation=5" -c:v h264_vaapi output_sdr.mp4