Skip to content

Commit f073281

Browse files
fix(producer): stop JPEG frame capture adding colour noise to graded footage (#5514)
* fix(producer): stop JPEG frame capture adding colour noise to graded footage Standard and high renders captured page frames as JPEG at quality 95 (the probe session and distributed chunks at 80). Chrome's encoder turns graded colour and grain into 8x8 chroma blotches at those settings, and they survive the final encode. Capture now uses quality 100 for standard and high, from one owner that the in-process render, the probe session and distributed chunks all read. Draft keeps 80. The disk estimate for JPEG frames doubles to match the larger files. * fix(producer): key resume and disk estimate on the capture quality Segment resume now fingerprints the capture jpeg quality, so a resume across a quality change re-renders instead of splicing old-quality segments. The disk estimate allows for larger frames at any jpeg quality above draft's 80 instead of only at exactly 100. * fix(producer): keep segment manifest comment share at base * fix(producer): fit capture quality comment in four lines
1 parent 1cd086f commit f073281

10 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎packages/producer/src/services/distributed/renderChunk.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { applyRenderWarningPolicy } from "../renderOrchestrator.js";
6969
import { runEncodeStage } from "../render/stages/encodeStage.js";
7070
import { runCaptureStage } from "../render/stages/captureStage.js";
7171
import { resolveVideoCaptureBeyondViewport } from "../render/captureBeyondViewport.js";
72+
import { resolveCaptureJpegQuality } from "../render/captureImageFormat.js";
7273
import { createCapturePlan } from "../render/capturePlan.js";
7374
import {
7475
type ChunkSliceJson,
@@ -819,7 +820,10 @@ export async function renderChunk(
819820
height: plan.dimensions.height,
820821
fps: { num: plan.dimensions.fpsNum, den: plan.dimensions.fpsDen },
821822
format: plan.dimensions.format === "mp4" ? "jpeg" : "png",
822-
quality: plan.dimensions.format === "mp4" ? 80 : undefined,
823+
quality:
824+
plan.dimensions.format === "mp4"
825+
? resolveCaptureJpegQuality(job.config.quality)
826+
: undefined,
823827
deviceScaleFactor: encoder.deviceScaleFactor,
824828
// Re-inject the controller's snapshotted variables so the chunk's
825829
// first capture sees the same `window.__hfVariables` the in-process

‎packages/producer/src/services/render/captureImageFormat.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "bun:test";
22
import { frameFileExtension } from "@hyperframes/engine";
3-
import { resolveCaptureImageFormat } from "./captureImageFormat.js";
3+
import { resolveCaptureImageFormat, resolveCaptureJpegQuality } from "./captureImageFormat.js";
44

55
describe("resolveCaptureImageFormat", () => {
66
it("captures JPEG for an ordinary opaque render", () => {
@@ -27,3 +27,14 @@ describe("resolveCaptureImageFormat", () => {
2727
}
2828
});
2929
});
30+
31+
describe("resolveCaptureJpegQuality", () => {
32+
it("captures standard and high renders at quality 100 so graded colour survives", () => {
33+
expect(resolveCaptureJpegQuality("standard")).toBe(100);
34+
expect(resolveCaptureJpegQuality("high")).toBe(100);
35+
});
36+
37+
it("keeps draft renders on the smaller, faster frames", () => {
38+
expect(resolveCaptureJpegQuality("draft")).toBe(80);
39+
});
40+
});

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MotionBlurOptions } from "@hyperframes/engine";
1+
import type { EngineConfig, MotionBlurOptions } from "@hyperframes/engine";
22

33
/**
44
* The format a render captures its frames in.
@@ -19,3 +19,11 @@ export function resolveCaptureImageFormat(input: {
1919
}): "jpeg" | "png" {
2020
return input.needsAlpha || input.motionBlur ? "png" : "jpeg";
2121
}
22+
23+
/**
24+
* Below 100, Chrome's JPEG turns graded colour and grain into 8x8 chroma blotches that survive
25+
* the final encode. Draft keeps the smaller, faster frames.
26+
*/
27+
export function resolveCaptureJpegQuality(quality: EngineConfig["quality"]): number {
28+
return quality === "draft" ? 80 : 100;
29+
}

‎packages/producer/src/services/render/segmentManifest.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const hashInput: SegmentPlanHashInput = {
3131
outputWidth: 1920,
3232
outputHeight: 1080,
3333
motionBlur: "",
34+
captureQuality: 80,
3435
};
3536

3637
describe("computeSegmentPlanHash", () => {
@@ -68,6 +69,7 @@ describe("computeSegmentPlanHash", () => {
6869
expect(computeSegmentPlanHash({ ...hashInput, motionBlur: '{"samplesPerFrame":32}' })).not.toBe(
6970
a,
7071
);
72+
expect(computeSegmentPlanHash({ ...hashInput, captureQuality: 100 })).not.toBe(a);
7173
});
7274

7375
it("does not collide across adjacent field boundaries", () => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface SegmentPlanHashInput {
4848
outputHeight: number;
4949
/** Serialised motion-blur options, or "" when off; sample count and shutter change every pixel. */
5050
motionBlur: string;
51+
captureQuality: number | undefined;
5152
}
5253

5354
const MANIFEST_FILENAME = "segments.json";
@@ -70,6 +71,7 @@ export function computeSegmentPlanHash(input: SegmentPlanHashInput): string {
7071
input.useGpu ? "gpu" : "cpu",
7172
`${input.outputWidth}x${input.outputHeight}`,
7273
input.motionBlur,
74+
input.captureQuality === undefined ? "" : String(input.captureQuality),
7375
].join(" ");
7476
return createHash("sha256").update(ordered).digest("hex").slice(0, 16);
7577
}

‎packages/producer/src/services/render/stages/captureStage.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ describe("disk capture capacity", () => {
8080
expect(estimateDiskCaptureBytes(10, captureOptions)).toBe(100_000);
8181
});
8282

83+
it("allows for the larger frames of jpeg above draft quality", () => {
84+
expect(estimateDiskCaptureBytes(10, { ...captureOptions, quality: 80 })).toBe(100_000);
85+
expect(estimateDiskCaptureBytes(10, { ...captureOptions, quality: 95 })).toBe(200_000);
86+
expect(estimateDiskCaptureBytes(10, { ...captureOptions, quality: 100 })).toBe(200_000);
87+
});
88+
8389
it("fails before capture when estimated frames exceed available headroom", () => {
8490
expect(() =>
8591
assertDiskCaptureHeadroom("/render/captured-frames", 10, pngOptions, () => 800_000),

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ export function shouldAllowAdaptiveCaptureRetry(
142142
return workerCount > 1;
143143
}
144144

145-
/** Jpeg frames are far smaller than raw pixels; this divisor is a conservative bound. */
145+
/** Jpeg frames are far smaller than raw pixels; these divisors are conservative bounds. */
146146
const JPEG_RAW_RGBA_DIVISOR = 8;
147+
/** Above draft's quality 80; quality-100 jpeg of grainy 4K footage measures about 1/6 of raw RGBA. */
148+
const HIGH_QUALITY_JPEG_RAW_RGBA_DIVISOR = 4;
147149
/** Frames captured before the measured projection replaces the static estimate. */
148150
const DISK_PROJECTION_SAMPLE_FRAMES = 10;
149151

@@ -156,7 +158,12 @@ export function estimateDiskCaptureBytes(
156158
const outputHeight = Math.ceil(captureOptions.height * scale);
157159
const rawBytes = Math.ceil(totalFrames) * outputWidth * outputHeight * 4;
158160
return frameFileExtension(captureOptions.format) === "jpg"
159-
? Math.ceil(rawBytes / JPEG_RAW_RGBA_DIVISOR)
161+
? Math.ceil(
162+
rawBytes /
163+
((captureOptions.quality ?? 80) > 80
164+
? HIGH_QUALITY_JPEG_RAW_RGBA_DIVISOR
165+
: JPEG_RAW_RGBA_DIVISOR),
166+
)
160167
: rawBytes;
161168
}
162169

‎packages/producer/src/services/render/stages/probeStage.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,26 @@ describe("runProbeStage — render variable threading", () => {
927927
});
928928
});
929929

930+
describe("runProbeStage — capture jpeg quality", () => {
931+
// A sequential disk render reuses this session for every frame, so its quality is the render's.
932+
for (const [preset, expected] of [
933+
["draft", 80],
934+
["standard", 100],
935+
["high", 100],
936+
] as const) {
937+
it(`captures ${preset} renders at jpeg quality ${expected}`, async () => {
938+
capturedOptions.length = 0;
939+
const { runProbeStage } = await import("./probeStage.js");
940+
const input = makeProbeInput({ stageForceScreenshot: false });
941+
input.job.config.quality = preset;
942+
943+
await runProbeStage(input);
944+
945+
expect(capturedOptions[0]).toMatchObject({ format: "jpeg", quality: expected });
946+
});
947+
}
948+
});
949+
930950
describe("runProbeStage — decimal duration frame count", () => {
931951
it("reproduces all 14 cumulative final-frame indices from the field report", async () => {
932952
const { runProbeStage } = await import("./probeStage.js");

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import {
7171
import type { RenderJob } from "../../renderOrchestrator.js";
7272
import { isActionableProbeFailure } from "./probeFailures.js";
7373
import { preflightCompositionAssetMediaTypes } from "../../assetMediaType.js";
74-
import { resolveCaptureImageFormat } from "../captureImageFormat.js";
74+
import { resolveCaptureImageFormat, resolveCaptureJpegQuality } from "../captureImageFormat.js";
7575

7676
export interface ProbeStageInput {
7777
projectDir: string;
@@ -379,7 +379,8 @@ export async function runProbeStage(input: ProbeStageInput): Promise<ProbeStageR
379379
height,
380380
fps: job.config.fps,
381381
format: captureFormat,
382-
quality: captureFormat === "png" ? undefined : 80,
382+
quality:
383+
captureFormat === "png" ? undefined : resolveCaptureJpegQuality(job.config.quality),
383384
variables: job.config.variables,
384385
deviceScaleFactor,
385386
motionBlur,

‎packages/producer/src/services/renderOrchestrator.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ import { runCaptureHdrStage } from "./render/stages/captureHdrStage.js";
211211
import { runEncodeStage } from "./render/stages/encodeStage.js";
212212
import { runAssembleStage } from "./render/stages/assembleStage.js";
213213
import { shouldUseLayeredComposite } from "./hdrCompositor.js";
214-
import { resolveCaptureImageFormat } from "./render/captureImageFormat.js";
214+
import {
215+
resolveCaptureImageFormat,
216+
resolveCaptureJpegQuality,
217+
} from "./render/captureImageFormat.js";
215218
import { assertMotionBlurSupported } from "./render/motionBlurRoute.js";
216219

217220
export function sampleDirectoryBytes(dir: string): number {
@@ -3471,7 +3474,8 @@ async function executeRenderPipeline(input: {
34713474
height,
34723475
fps: job.config.fps,
34733476
format: captureImageFormat,
3474-
quality: captureImageFormat === "png" ? undefined : job.config.quality === "draft" ? 80 : 95,
3477+
quality:
3478+
captureImageFormat === "png" ? undefined : resolveCaptureJpegQuality(job.config.quality),
34753479
variables: job.config.variables,
34763480
deviceScaleFactor,
34773481
motionBlur: job.config.motionBlur,
@@ -4438,6 +4442,7 @@ async function executeRenderPipeline(input: {
44384442
outputWidth: captureCompositionWidth ?? width,
44394443
outputHeight: captureCompositionHeight ?? height,
44404444
motionBlur: job.config.motionBlur ? JSON.stringify(job.config.motionBlur) : "",
4445+
captureQuality: captureOptions.quality,
44414446
});
44424447
const segmentDir = segmentDirFor(join(projectDir, "renders"), segmentPlanHash);
44434448
let completedSegments: ReadonlySet<number> = new Set<number>();

0 commit comments

Comments
 (0)