Ch. 14 · HDR Workflow in Practice

한국어日本語
8 min

HDR to SDR Tone Mapping: zscale and tonemap

Converting HDR down to SDR requires tone mapping, which folds the brightness range into a smaller one. What each stage of the ffmpeg filter chain does.

Sometimes you have to upload HDR video to a place that supports only SDR. If you simply treat the PQ code values as BT.709 video, the whole picture comes out hazy and dark. You need the tone mapping from Chapter 1: rearranging a wide brightness range to fit a narrow one.

A typical ffmpeg filter chain for converting PQ (HDR10) to BT.709 SDR
ffmpeg -i input_hdr.mp4 \
  -vf "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p" \
  -c:v libx264 -crf 18 -preset slow \
  -color_primaries bt709 -color_trc bt709 -colorspace bt709 \
  -c:a copy output_sdr.mp4
StepFilterWhat it does
1zscale=t=linear:npl=100Converts the PQ code values back to linear light. npl is the nominal peak brightness (nits) used as the reference
2format=gbrpf32leConverts to 32-bit floating-point RGB for the tone mapping (to preserve precision)
3zscale=p=bt709Converts the color gamut from BT.2020 to BT.709
4tonemap=tonemap=hableFolds the brightness range to fit SDR. You choose the curve type (hable, mobius, reinhard, etc.)
5zscale=t=bt709:m=bt709:r=tvReturns to the BT.709 transfer function and matrix, limited range
6format=yuv420pConverts to 8-bit 4:2:0 for ordinary SDR encoding
The order is what matters

Tone mapping has to be done in linear light. If you fold the brightness on top of nonlinear code values like PQ, the curve comes out wrong. So keep to the order: unwrap to linear, convert the gamut, tone map, then encode with the SDR curve again.

  • For an HLG source, you have to tell the first stage that the input transfer function is HLG (zscale's input option tin=arib-std-b67). PQ and HLG are unwrapped with different curves.
  • For a source with no tags, zscale doesn't know the input and may fail or interpret it wrongly, so you need to specify the input side explicitly with tin, pin, min, and so on.
  • The `zscale` filter is available only if your ffmpeg build includes libzimg. If it doesn't, you'll get an error saying the filter can't be found. Check with ffmpeg -filters.
In the field: tone mapping has no single right answer

hable, mobius, and reinhard differ in taste, in how to squeeze the highlights in. The same scene gets different highlight detail and a different overall brightness feel depending on the curve. Try the curves on a short clip (a few seconds), compare by eye, then run the whole video. If accurate color reproduction matters, then in principle it's better to grade a separate SDR version in your editor than to rely on automatic conversion.

zscalezscale filter

An ffmpeg filter based on libzimg. It handles color gamut, transfer function, matrix, and range conversion, plus high-quality scaling.

nplNominal peak luminance

A zscale option. The peak brightness (nits) used as the reference during the linear conversion.

Hable / Mobius / ReinhardTone mapping curve types

The highlight compression curves offered by ffmpeg's tonemap filter.

Limited / Full rangeLimited / Full range

Using only 16 to 235 in 8-bit versus using the whole 0 to 255. If they mismatch, blacks look lifted or crushed.

Check yourself

In an HDR-to-SDR tone mapping filter chain, why do we first convert the PQ code values to linear?