FFmpeg tonemap_opencl Hardware Accelerated Tone Mapping

This article provides a practical guide on how to use the tonemap_opencl filter in FFmpeg to perform hardware-accelerated High Dynamic Range (HDR) to Standard Dynamic Range (SDR) tone mapping. You will learn the required prerequisites, the core command syntax, and real-world examples to leverage your GPU for fast and efficient video transcoding.

Prerequisites

To use tonemap_opencl, your system and FFmpeg build must meet the following requirements:

Core Command Structure

Hardware-accelerated filtering in FFmpeg requires initializing the hardware device, uploading the video frames to the GPU, processing them with the OpenCL filter, and downloading them back to system memory (unless using a full hardware encoder pipeline).

Here is the template for a basic pipeline:

ffmpeg -init_hw_device opencl=gpu -filter_hw_device gpu -i input_hdr.mp4 -vf "hwupload,tonemap_opencl=t=bt709:tonemap=mobius:format=nv12,hwdownload,format=nv12" -c:v libx264 -c:a copy output_sdr.mp4

Parameter Breakdown

Advanced Usage and Customization

Adjusting Brightness and Contrast

You can fine-tune the output by specifying the peak luminance of the source and target.

ffmpeg -init_hw_device opencl=gpu -filter_hw_device gpu -i input_hdr.mp4 -vf "hwupload,tonemap_opencl=t=bt709:tonemap=reinhard:peak=1000:target_peak=100:format=nv12,hwdownload,format=nv12" -c:v libx264 output_sdr.mp4

Full Hardware Pipeline (NVENC Example)

If you want to decode, tone-map, and encode entirely on an NVIDIA GPU without transferring frames back and forth to system memory, use the following pipeline:

ffmpeg -init_hw_device opencl=ocl -init_hw_device cuda=cuda -filter_hw_device ocl -c:v hevc_cuvid -i input_hdr.mp4 -vf "hwmap=derive_device=opencl,tonemap_opencl=t=bt709:tonemap=mobius:format=nv12,hwmap=derive_device=cuda" -c:v h264_nvenc output_sdr.mp4