Skip to content

Commit b559a98

Browse files
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
1 parent c27475b commit b559a98

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎packages/engine/src/services/videoFrameExtractor.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,6 +4046,42 @@ describe.skipIf(!HAS_ZSCALE)("forced-SDR HDR extraction", () => {
40464046
expect(colourProbesSince(before)).toEqual({ firstFrame: 2, keyframes: 1 });
40474047
}, 60_000);
40484048

4049+
it("tone-maps an HLG source at HLG's white when a large header SEI follows its SPS", async () => {
4050+
const source = join(fixtureDir, "hlg-x265-header.mp4");
4051+
// x265 stores its settings as an SEI after the SPS in the file header; traced, it runs past 64 KB.
4052+
// No B-frames: at 1 fps their reorder gap is a whole second, which Windows ffmpeg's mp4 muxer can refuse.
4053+
const synthesized = await runFfmpeg([
4054+
...["-y", "-hide_banner", "-loglevel", "error"],
4055+
...["-f", "lavfi", "-i", "color=c=0xe0b080:s=64x64:r=1:d=1"],
4056+
"-vf",
4057+
"zscale=pin=bt709:tin=bt709:min=bt709:p=bt2020:t=arib-std-b67:m=bt2020nc:r=tv,format=yuv420p10le",
4058+
...[
4059+
"-c:v",
4060+
"libx265",
4061+
"-x265-params",
4062+
"colorprim=bt2020:transfer=arib-std-b67:colormatrix=bt2020nc:log-level=error:bframes=0",
4063+
],
4064+
...["-color_primaries", "bt2020", "-color_trc", "arib-std-b67", "-colorspace", "bt2020nc"],
4065+
source,
4066+
]);
4067+
if (!synthesized.success) {
4068+
throw new Error(`x265 HLG fixture synthesis failed: ${synthesized.stderr.slice(-400)}`);
4069+
}
4070+
const before = vi.mocked(runFfmpeg).mock.calls.length;
4071+
await extractVideoFramesRange(source, "hlg-x265-header", 0, 1, {
4072+
fps: 1,
4073+
outputDir: join(fixtureDir, "x265-header"),
4074+
format: "png",
4075+
toneMapHdrToSdr: true,
4076+
});
4077+
const toneMaps = vi
4078+
.mocked(runFfmpeg)
4079+
.mock.calls.slice(before)
4080+
.flatMap(([args]) => args.filter((arg) => arg.includes("tonemap=")));
4081+
expect(toneMaps).toHaveLength(1);
4082+
expect(toneMaps[0]).toContain("npl=203");
4083+
}, 60_000);
4084+
40494085
it("reads an HLG source's keyframes again after that read failed", async () => {
40504086
const source = join(fixtureDir, "keyframe-retry.mp4");
40514087
await synthesizeHlgClip(source);

‎packages/engine/src/services/videoFrameExtractor.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,11 +954,20 @@ export function parseImageElements(html: string): ImageElement[] {
954954

955955
const firstFrameColourCache = new Map<string, MediaProbeCacheEntry<ToneMapFrameColour>>();
956956

957-
/** A source's SPS transfers, so a PQ or SDR keyframe keeps an HLG one at 100 nits; null when the read failed. */
957+
/** A source's SPS transfers, read as the trace streams (a run keeps only stderr's tail); null when the read failed. */
958958
function readKeyframeTransfers(videoPath: string, codec: string) {
959+
let traced = "";
960+
let rest = "";
961+
const onStderr = (chunk: string) => {
962+
const lines = (rest + chunk).split("\n");
963+
rest = lines.pop() ?? "";
964+
for (const line of lines) if (line.includes("transfer_characteristics")) traced += `${line}\n`;
965+
};
959966
return inExtractionSlot(() =>
960-
runFfmpeg([...urlInputArgs(videoPath), ...keyframeTransferArgs(videoPath, codec)]),
961-
).then((result) => (result.success ? parseKeyframeTransfers(result.stderr) : null));
967+
runFfmpeg([...urlInputArgs(videoPath), ...keyframeTransferArgs(videoPath, codec)], {
968+
onStderr,
969+
}),
970+
).then((result) => (result.success ? parseKeyframeTransfers(traced + rest) : null));
962971
}
963972

964973
/** The first shown frame's colour tags (see {@link firstFrameColourArgs}), probed once per source file. */

0 commit comments

Comments
 (0)