render.py: HDR clips with missing or SEI-signalled transfer tags skip the tone map or crash
is_hdr_source() decides whether to tone-map by reading only the stream-level color_transfer and checking it against {"smpte2084", "arib-std-b67"} (helpers/render.py lines 123 and 144). The tone-map chain then starts with zscale=t=linear:npl=100, which takes its input primaries and matrix from the frame (line 126). HDR clips whose tags are missing or signalled another way fail in three ways.
Results (commit b877063, ffmpeg 8.0.1, macOS arm64; synthetic 10-bit HEVC HLG clips; render.py <edl> -o out.mp4 --draft --no-subtitles --no-loudnorm):
| Source tags (transfer / primaries / matrix) | What render.py does |
Output vs the correctly tagged render |
|---|---|---|
| arib-std-b67 / bt2020 / bt2020nc (reference) | Tone-maps; output tagged bt709 | — |
| none | Treats the clip as SDR; no warning, exit 0 | PSNR 23.8 dB; output untagged |
| arib-std-b67 / none / none | Crashes: CalledProcessError. ffmpeg's hidden error is zscale code 3074: no path between colorspaces |
no output |
| bt2020-10 in the stream header, HLG in the alternative-transfer SEI (as some cameras write it) | Treats the clip as SDR; exit 0 | PSNR 23.8 dB; output is 8-bit but still tagged arib-std-b67 / bt2020, the case the HDR comment in render.py warns about |
For the SEI case, the decoded frames report color_transfer=arib-std-b67; only the stream header says bt2020-10:
ffprobe -v error -select_streams v:0 -read_intervals 0%+#1 -show_entries frame=color_transfer -of csv=p=0 sei.mp4A wrapper that used the same stream-level check rendered a real iPhone HLG clip, with its colour tags stripped, as SDR at 29.0 dB against the correct render: a visible yellow cast and flat skin tones. Tags can go missing when a clip passes through a messaging app or an editor; I did not measure how often this happens.
Reproduce:
ffmpeg -f lavfi -i "testsrc2=s=1920x1080:r=30:d=3" -f lavfi -i "anoisesrc=d=3" \
-vf format=yuv420p10le -c:v libx265 -x265-params "<TAGS>" -tag:v hvc1 -c:a aac -shortest clip.mp4with <TAGS> set to log-level=error (none), log-level=error:transfer=arib-std-b67 (transfer only), or log-level=error:colorprim=bt2020:colormatrix=bt2020nc:transfer=bt2020-10:atc-sei=18 (SEI). Render a 2 s range of each with render.py --draft.
Suggested fix (what worked in a wrapper; its tests fail on the old behaviour and pass on the new):
- Read the colour fields from the first decoded frame (
-read_intervals 0%+#1 -show_entries frame=color_transfer,color_primaries,color_space), falling back to the stream. The0%seek matters: without it, a stream-copied trim with an edit list returned no frame. - Before the chain, fill only the missing fields with
setparams(default primariesbt2020, matrixbt2020nc) and keep the probed transfer. On correctly tagged HLG and PQ clips the output was identical to the current chain (PSNR inf). - If a clip has no HDR or Rec.709-family transfer but is 10-bit or has a BT.2020 tag, stop and ask rather than guess. It could be HLG, PQ, SDR, or log footage that needs a LUT.
Open PR #150 moves the is_hdr_source() call but keeps the same check, so it does not change this. PR #6 added the tone map.
Source: browser-use/video-use