fix(engine): hlg footage with a large header sei also tone-maps at hlg's white (#7349)

* fix(engine): hlg footage with a large header sei also tone-maps at hlg's white

* fix(engine): say in the read's own doc line why it streams

* test(engine): the header-SEI HLG fixture encodes without b-frames, so it muxes on windows
This commit is contained in:
Miguel Ángel
2026-11-07 21:46:28 +00:00
committed by GitHub
parent c27475b972
commit b559a98d37
2 changed files with 48 additions and 3 deletions
@@ -4046,6 +4046,42 @@ describe.skipIf(!HAS_ZSCALE)("forced-SDR HDR extraction", () => {
expect(colourProbesSince(before)).toEqual({ firstFrame: 2, keyframes: 1 });
}, 60_000);
it("tone-maps an HLG source at HLG's white when a large header SEI follows its SPS", async () => {
const source = join(fixtureDir, "hlg-x265-header.mp4");
// x265 stores its settings as an SEI after the SPS in the file header; traced, it runs past 64 KB.
// No B-frames: at 1 fps their reorder gap is a whole second, which Windows ffmpeg's mp4 muxer can refuse.
const synthesized = await runFfmpeg([
...["-y", "-hide_banner", "-loglevel", "error"],
...["-f", "lavfi", "-i", "color=c=0xe0b080:s=64x64:r=1:d=1"],
"-vf",
"zscale=pin=bt709:tin=bt709:min=bt709:p=bt2020:t=arib-std-b67:m=bt2020nc:r=tv,format=yuv420p10le",
...[
"-c:v",
"libx265",
"-x265-params",
"colorprim=bt2020:transfer=arib-std-b67:colormatrix=bt2020nc:log-level=error:bframes=0",
],
...["-color_primaries", "bt2020", "-color_trc", "arib-std-b67", "-colorspace", "bt2020nc"],
source,
]);
if (!synthesized.success) {
throw new Error(`x265 HLG fixture synthesis failed: ${synthesized.stderr.slice(-400)}`);
}
const before = vi.mocked(runFfmpeg).mock.calls.length;
await extractVideoFramesRange(source, "hlg-x265-header", 0, 1, {
fps: 1,
outputDir: join(fixtureDir, "x265-header"),
format: "png",
toneMapHdrToSdr: true,
});
const toneMaps = vi
.mocked(runFfmpeg)
.mock.calls.slice(before)
.flatMap(([args]) => args.filter((arg) => arg.includes("tonemap=")));
expect(toneMaps).toHaveLength(1);
expect(toneMaps[0]).toContain("npl=203");
}, 60_000);
it("reads an HLG source's keyframes again after that read failed", async () => {
const source = join(fixtureDir, "keyframe-retry.mp4");
await synthesizeHlgClip(source);
@@ -954,11 +954,20 @@ export function parseImageElements(html: string): ImageElement[] {
const firstFrameColourCache = new Map<string, MediaProbeCacheEntry<ToneMapFrameColour>>();
/** A source's SPS transfers, so a PQ or SDR keyframe keeps an HLG one at 100 nits; null when the read failed. */
/** A source's SPS transfers, read as the trace streams (a run keeps only stderr's tail); null when the read failed. */
function readKeyframeTransfers(videoPath: string, codec: string) {
let traced = "";
let rest = "";
const onStderr = (chunk: string) => {
const lines = (rest + chunk).split("\n");
rest = lines.pop() ?? "";
for (const line of lines) if (line.includes("transfer_characteristics")) traced += `${line}\n`;
};
return inExtractionSlot(() =>
runFfmpeg([...urlInputArgs(videoPath), ...keyframeTransferArgs(videoPath, codec)]),
).then((result) => (result.success ? parseKeyframeTransfers(result.stderr) : null));
runFfmpeg([...urlInputArgs(videoPath), ...keyframeTransferArgs(videoPath, codec)], {
onStderr,
}),
).then((result) => (result.success ? parseKeyframeTransfers(traced + rest) : null));
}
/** The first shown frame's colour tags (see {@link firstFrameColourArgs}), probed once per source file. */