Skip to content

Commit 3137924

Browse files
fix(producer): HDR sources are tone-mapped for formats that cannot carry HDR (#6597)
* fix(producer): HDR sources are tone-mapped for formats that cannot carry HDR * refactor(producer): one owner for which output formats can carry HDR * refactor(producer): drop comments the helper names already carry * fix(engine): SDR clips are not lifted to HDR when the render tone-maps to SDR
1 parent 3b2058a commit 3137924

5 files changed

Lines changed: 52 additions & 5 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,22 @@ describe.skipIf(!HAS_FFMPEG)("extractAllVideoFrames on a VFR source", () => {
32833283
).toBe(false);
32843284
}, 60_000);
32853285

3286+
it("leaves SDR clips alone in a mixed timeline that tone-maps to SDR", async () => {
3287+
const SDR = await synthCfrClip("sdr-tonemap-peer.mp4", 1);
3288+
const HDR = await synthHdrTaggedClip("hdr-tonemap-peer.mp4", 1);
3289+
const outputDir = join(FIXTURE_DIR, "out-hdr-tonemap-mixed");
3290+
mkdirSync(outputDir, { recursive: true });
3291+
3292+
const result = await extractAllVideoFrames(
3293+
[cfrClipElement("sdr", SDR, 1), { ...cfrClipElement("hdr", HDR, 1), start: 1, end: 2 }],
3294+
FIXTURE_DIR,
3295+
{ fps: 30, outputDir, toneMapHdrToSdr: true },
3296+
);
3297+
3298+
expect(result.errors).toEqual([]);
3299+
expect(result.phaseBreakdown.hdrPreflightCount).toBe(0);
3300+
}, 60_000);
3301+
32863302
it("keeps a finite SDR past-EOF slot in a mixed HDR timeline", async () => {
32873303
const SDR_SHORT = await synthCfrClip("sdr-past-eof.mp4", 1);
32883304
const HDR_SHORT = await synthHdrTaggedClip("hdr-past-eof-peer.mp4", 1);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ export async function extractAllVideoFrames(
21412141
// Phase 3 still calls `extractVideoFramesRange` on the same past-EOF
21422142
// mediaStart, surfacing a second raw FFmpeg error for the same clip.
21432143
const hdrSkippedIndices = new Set<number>();
2144-
if (hdrInfo.hasHdr && hdrInfo.dominantTransfer) {
2144+
if (!options.toneMapHdrToSdr && hdrInfo.hasHdr && hdrInfo.dominantTransfer) {
21452145
// dominantTransfer is "majority wins" — if a composition mixes PQ and HLG
21462146
// sources (rare but legal), the minority transfer's videos get converted
21472147
// with the wrong curve. We treat this as caller-error: a single composition

‎packages/producer/src/services/render/hdrMode.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ function logHdrResolution(
107107
);
108108
}
109109

110+
function formatCarriesHdr(format: NonNullable<RenderConfig["format"]>): boolean {
111+
return format === "mp4";
112+
}
113+
114+
export function rendersSdrOnly(config: Pick<RenderConfig, "hdrMode" | "format">): boolean {
115+
return config.hdrMode === "force-sdr" || !formatCarriesHdr(config.format ?? "mp4");
116+
}
117+
110118
export function resolveEffectiveHdrMode(input: HdrModeInput): EffectiveHdr {
111119
const hdrMode = input.hdrMode ?? "auto";
112120
const videoColorSpaces = (input.extractionResult?.extracted ?? []).map(
@@ -131,7 +139,7 @@ export function resolveEffectiveHdrMode(input: HdrModeInput): EffectiveHdr {
131139
effectiveHdr = { transfer: info.dominantTransfer };
132140
}
133141

134-
if (effectiveHdr && input.outputFormat !== "mp4") {
142+
if (effectiveHdr && !formatCarriesHdr(input.outputFormat)) {
135143
input.log.warn(formatDowngradeWarning(input.outputFormat, forcedHdrWithoutSources));
136144
effectiveHdr = undefined;
137145
}

‎packages/producer/src/services/render/stages/extractVideosStage.timelineBound.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ import { runExtractVideosStage } from "./extractVideosStage.js";
8282
async function runStage(
8383
compositionDuration: number,
8484
materializeSymlinks: boolean,
85-
options: { source?: string; log?: ProducerLogger; extraVideos?: VideoElement[] } = {},
85+
options: {
86+
source?: string;
87+
log?: ProducerLogger;
88+
extraVideos?: VideoElement[];
89+
hdrMode?: "auto" | "force-hdr" | "force-sdr";
90+
format?: "mp4" | "webm" | "mov";
91+
} = {},
8692
): Promise<VideoElement[]> {
8793
const composition = {
8894
duration: compositionDuration,
@@ -109,7 +115,8 @@ async function runStage(
109115
job: createRenderJob({
110116
fps: { num: 30, den: 1 },
111117
quality: "standard",
112-
hdrMode: "force-sdr",
118+
hdrMode: options.hdrMode ?? "force-sdr",
119+
format: options.format,
113120
}),
114121
cfg: resolveConfig(),
115122
log: options.log,
@@ -154,6 +161,21 @@ describe.each([
154161
expect(toneMapHdrToSdrCalls).toEqual([true]);
155162
});
156163

164+
it.each([
165+
["auto", "mp4", false],
166+
["auto", "webm", true],
167+
["force-hdr", "mov", true],
168+
] as const)(
169+
"tone maps HDR sources in %s mode to %s output: %s",
170+
async (hdrMode, format, want) => {
171+
toneMapHdrToSdrCalls.splice(0);
172+
173+
await runStage(2, materializeSymlinks, { hdrMode, format });
174+
175+
expect(toneMapHdrToSdrCalls).toEqual([want]);
176+
},
177+
);
178+
157179
it("drops clips starting at or after the end before extraction, keeping natural-length ones", async () => {
158180
requestedVideoIds.splice(0);
159181
const outside = (id: string, start: number, end: number): VideoElement => ({

‎packages/producer/src/services/render/stages/extractVideosStage.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import { REMOTE_MEDIA_SUBDIR } from "../../htmlCompiler.js";
6565
import { resolveRenderFpsConfig } from "../../fileServer.js";
6666
import type { ProducerLogger } from "../../../logger.js";
6767
import { encoderFailureError } from "../encoderInterruption.js";
68+
import { rendersSdrOnly } from "../hdrMode.js";
6869
import {
6970
compareExtractionFailureGroups,
7071
extractionFailureGroupIdentityKey,
@@ -513,7 +514,7 @@ export async function runExtractVideosStage(
513514
fps: job.config.fps,
514515
outputDir: join(compiledDir, "__hyperframes_video_frames"),
515516
format: job.config.videoFrameFormat ?? "auto",
516-
toneMapHdrToSdr: job.config.hdrMode === "force-sdr",
517+
toneMapHdrToSdr: rendersSdrOnly(job.config),
517518
timelineEnd: composition.duration,
518519
maxTransientRetries: extractionPolicy.maxTransientRetries,
519520
collectProbeFailures: extractionPolicy.failureMode === "enforce",

0 commit comments

Comments
 (0)