fix(hdr): bound HDR image decode and resample sizes (#7917)

* fix(hdr): bound HDR image decode and resample sizes

The native HDR image path sized its host buffers from the PNG header and
the element's page-measured layout box, with no ceiling.

- PNG decode checks IHDR dimensions and bit depth before allocating,
  inflates with maxOutputLength = height * (stride + 1), and rejects image
  data whose inflated length does not match the header.
- HDR images are limited to 8192x8192 pixels; resampleRgb48leObjectFit
  refuses targets above that.
- A layout box larger than the output frame is shrunk uniformly to fit it.
- Decoded and resampled HDR image buffers count against the HDR extraction
  budget (HDR_EXTRACTION_MAX_BYTES / cgroup limit) for the render's lifetime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* build(engine): raise the packed size budget to 510000 bytes

The PNG header checks and bounded inflate add about 500 bytes to the
packed engine, which was already within a few hundred bytes of its
1% tolerance over the 500000-byte budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
andybpan
2026-11-16 03:15:55 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 846cce4891
commit 758ad7f585
8 changed files with 281 additions and 37 deletions
+1
View File
@@ -64,6 +64,7 @@ This is why a lower third or animated interface can remain visually consistent o
- HDR output is MP4 only. MOV, WebM, GIF, PNG sequence, and alpha output fall back to SDR.
- HDR and `--resolution` supersampling cannot be combined in one render. Author at the target dimensions or resize in a separate step.
- Native HDR still images can be up to 8192×8192 pixels. An HDR image whose box is larger than the video frame is drawn shrunk to fit inside it, keeping its aspect ratio.
- Distributed AWS Lambda and Cloud Run renders are currently SDR.
- Hardware H.265 encoding carries the color tags but does not include the same static mastering metadata as the software `libx265` path. Use software encoding for HDR10 delivery.
- Browser playback depends on the host browser, display, and operating system; `<hyperframes-player>` does not add its own HDR playback pipeline.
@@ -10,6 +10,7 @@ import {
roundedRectAlpha,
resampleRgb48leObjectFit,
normalizeObjectFit,
MAX_RGB48LE_PIXELS,
} from "./alphaBlit.js";
// ── PNG construction helpers ─────────────────────────────────────────────────
@@ -396,6 +397,46 @@ function makePng16(width: number, height: number, pixels: number[]): Buffer {
]);
}
function makePngWithIdat(width: number, height: number, bitDepth: number, idat: Buffer): Buffer {
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(width, 0);
ihdr.writeUInt32BE(height, 4);
ihdr[8] = bitDepth;
ihdr[9] = 2;
return Buffer.concat([
PNG_SIG,
makeChunk("IHDR", ihdr),
makeChunk("IDAT", idat),
makeChunk("IEND", Buffer.alloc(0)),
]);
}
describe("PNG decode size limits", () => {
it("rejects a 16000x16000 header over a 2 MB zero IDAT before inflating", () => {
const png = makePngWithIdat(16000, 16000, 16, Buffer.alloc(2 * 1024 * 1024));
expect(() => decodePngToRgb48le(png)).toThrow("outside the supported size");
});
it("rejects zero dimensions and sub-byte bit depths", () => {
const idat = deflateSync(Buffer.alloc(13));
expect(() => decodePngToRgb48le(makePngWithIdat(0, 1, 16, idat))).toThrow(
"outside the supported size",
);
expect(() => decodePng(makePngWithIdat(2, 2, 4, idat))).toThrow("unsupported bit depth 4");
});
it("rejects image data that inflates past the size its header declares", () => {
// 2x2 rgb48: 2 rows of (1 filter byte + 12 sample bytes) = 26 bytes.
const png = makePngWithIdat(2, 2, 16, deflateSync(Buffer.alloc(1024)));
expect(() => decodePngToRgb48le(png)).toThrow("does not inflate to 26 bytes");
});
it("rejects image data shorter than its header declares", () => {
const png = makePngWithIdat(2, 2, 16, deflateSync(Buffer.alloc(13)));
expect(() => decodePngToRgb48le(png)).toThrow("inflates to 13 bytes, expected 26");
});
});
describe("decodePngToRgb48le", () => {
it("swaps PNG big-endian samples to little-endian rgb48le", () => {
// Pick a value where high and low bytes differ so a missed swap is observable
@@ -1249,6 +1290,14 @@ function readRgb16(buf: Buffer, width: number, x: number, y: number): [number, n
}
describe("resampleRgb48leObjectFit", () => {
it("refuses a 30000x30000 target instead of allocating it", () => {
const src = makeHdrFrame(2, 2, 40000, 30000, 20000);
expect(30000 * 30000).toBeGreaterThan(MAX_RGB48LE_PIXELS);
expect(() => resampleRgb48leObjectFit(src, 2, 2, 30000, 30000)).toThrow(
"exceeds the 67108864-pixel limit",
);
});
it.each(["-50% 50%", "150% 50%", "50% -50%", "50% 150%"])(
"allows %s to position none-fit content outside the box",
(position) => {
+67 -9
View File
@@ -10,6 +10,11 @@ import { chunkCrc32 } from "./crc32.js";
// ── PNG decoder ───────────────────────────────────────────────────────────────
/** Ceiling for user-supplied rgb48le images and their resampled layout buffers. */
export const MAX_RGB48LE_PIXELS = 8192 * 8192;
/** Chrome caps screenshots at 16384 px per side, so `decodePng` never needs more. */
const MAX_SCREENSHOT_PIXELS = 16384 * 16384;
function paeth(a: number, b: number, c: number): number {
const p = a + b - c;
const pa = Math.abs(p - a);
@@ -20,6 +25,46 @@ function paeth(a: number, b: number, c: number): number {
return c;
}
/** Checks the IHDR size before any allocation and inflates exactly the bytes it declares. */
function inflateScanlines(
idatChunks: Buffer[],
header: { width: number; height: number; bitDepth: number; colorType: number },
caller: string,
maxPixels: number,
): { decompressed: Buffer; bpp: number; stride: number } {
const { width, height, bitDepth, colorType } = header;
if (bitDepth !== 8 && bitDepth !== 16) {
throw new Error(`${caller}: unsupported bit depth ${bitDepth} (expected 8 or 16)`);
}
if (width === 0 || height === 0 || width * height > maxPixels) {
throw new Error(
`${caller}: ${width}x${height} PNG is outside the supported size (max ${maxPixels} pixels)`,
);
}
// Bytes per pixel: channels x bytes-per-channel
const channels = colorType === 6 ? 4 : 3;
const bpp = channels * (bitDepth / 8);
const stride = width * bpp;
const expectedLength = height * (stride + 1);
let decompressed: Buffer;
try {
decompressed = inflateSync(Buffer.concat(idatChunks), { maxOutputLength: expectedLength });
} catch (err) {
throw new Error(
`${caller}: image data does not inflate to ${expectedLength} bytes for ${width}x${height}`,
{ cause: err },
);
}
if (decompressed.length !== expectedLength) {
throw new Error(
`${caller}: image data inflates to ${decompressed.length} bytes, expected ${expectedLength}`,
);
}
return { decompressed, bpp, stride };
}
/**
* Shared PNG chunk parsing + filter reconstruction.
*
@@ -32,6 +77,7 @@ function paeth(a: number, b: number, c: number): number {
function decodePngRaw(
buf: Buffer,
caller: string,
maxPixels: number,
): { width: number; height: number; bitDepth: number; colorType: number; rawPixels: Buffer } {
// Verify PNG signature
if (
@@ -89,13 +135,12 @@ function decodePngRaw(
);
}
// Bytes per pixel: channels x bytes-per-channel
const channels = colorType === 6 ? 4 : 3;
const bpp = channels * (bitDepth / 8);
const stride = width * bpp;
const compressed = Buffer.concat(idatChunks);
const decompressed = inflateSync(compressed);
const { decompressed, bpp, stride } = inflateScanlines(
idatChunks,
{ width, height, bitDepth, colorType },
caller,
maxPixels,
);
// Reconstruct filtered rows into a flat pixel buffer (no filter bytes)
const rawPixels = Buffer.allocUnsafe(height * stride);
@@ -158,7 +203,11 @@ function decodePngRaw(
* Returns a Uint8Array of width*height*4 bytes in RGBA order.
*/
export function decodePng(buf: Buffer): { width: number; height: number; data: Uint8Array } {
const { width, height, bitDepth, colorType, rawPixels } = decodePngRaw(buf, "decodePng");
const { width, height, bitDepth, colorType, rawPixels } = decodePngRaw(
buf,
"decodePng",
MAX_SCREENSHOT_PIXELS,
);
if (bitDepth !== 8) {
throw new Error(`decodePng: unsupported bit depth ${bitDepth} (expected 8)`);
@@ -244,7 +293,11 @@ export function encodePng(width: number, height: number, rgba: Uint8Array): Buff
* Supports colorType 2 (RGB) and 6 (RGBA) at 16-bit depth, non-interlaced.
*/
export function decodePngToRgb48le(buf: Buffer): { width: number; height: number; data: Buffer } {
const { width, height, bitDepth, colorType, rawPixels } = decodePngRaw(buf, "decodePngToRgb48le");
const { width, height, bitDepth, colorType, rawPixels } = decodePngRaw(
buf,
"decodePngToRgb48le",
MAX_RGB48LE_PIXELS,
);
if (bitDepth !== 16) {
throw new Error(`decodePngToRgb48le: unsupported bit depth ${bitDepth} (expected 16)`);
@@ -840,6 +893,11 @@ export function resampleRgb48leObjectFit(
if (fit === "fill" && srcW === dstW && srcH === dstH) {
return source;
}
if (dstW * dstH > MAX_RGB48LE_PIXELS) {
throw new Error(
`resampleRgb48leObjectFit: ${dstW}x${dstH} target exceeds the ${MAX_RGB48LE_PIXELS}-pixel limit`,
);
}
const pos = parseObjectPosition(objectPosition);
const rect = computeObjectFitRect(srcW, srcH, dstW, dstH, fit, pos);
@@ -2,6 +2,7 @@ import { existsSync, mkdtempSync, readSync, readdirSync, rmSync, writeFileSync }
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { spawnSync } from "node:child_process";
import { deflateSync } from "node:zlib";
import {
extractMediaMetadata,
resolveFinalFrameExtractionWindow,
@@ -15,7 +16,9 @@ import { createRenderJob } from "../../renderOrchestrator.js";
import { resolveHdrVideoFrameIndex } from "../../hdrCompositor.js";
import {
cleanupHdrVideoFrameSource,
decodeHdrImageBuffers,
extractHdrVideoFrames,
fitHdrImageLayoutToFrame,
getHdrExtractionReservedBytes,
planHdrResources,
reserveHdrExtractionBytes,
@@ -918,3 +921,94 @@ describe.skipIf(!HAS_FFMPEG)("raw HDR windows past the first (#6556)", () => {
}
}, 60_000);
});
/** A 2x2 16-bit RGB PNG; the decoder does not check chunk CRCs, so they are left zero. */
function tiny16BitPng(): Buffer {
const chunk = (type: string, data: Buffer) => {
const len = Buffer.alloc(4);
len.writeUInt32BE(data.length);
return Buffer.concat([len, Buffer.from(type, "ascii"), data, Buffer.alloc(4)]);
};
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(2, 0);
ihdr.writeUInt32BE(2, 4);
ihdr[8] = 16;
ihdr[9] = 2;
const rows = Buffer.alloc(2 * (1 + 12), 0x40);
rows[0] = 0;
rows[13] = 0;
return Buffer.concat([
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
chunk("IHDR", ihdr),
chunk("IDAT", deflateSync(rows)),
chunk("IEND", Buffer.alloc(0)),
]);
}
describe("fitHdrImageLayoutToFrame", () => {
it("keeps a box that fits and shrinks a larger one uniformly into the frame", () => {
const fits = { width: 1920, height: 1080 };
expect(fitHdrImageLayoutToFrame(fits, 1920, 1080)).toBe(fits);
expect(fitHdrImageLayoutToFrame({ width: 30000, height: 30000 }, 1920, 1080)).toEqual({
width: 1080,
height: 1080,
});
expect(fitHdrImageLayoutToFrame({ width: 3840, height: 100 }, 1920, 1080)).toEqual({
width: 1920,
height: 50,
});
});
});
describe("decodeHdrImageBuffers", () => {
let dir: string;
let srcPath: string;
beforeAll(() => {
dir = mkdtempSync(join(tmpdir(), "hf-hdr-image-"));
srcPath = join(dir, "tiny.png");
writeFileSync(srcPath, tiny16BitPng());
});
afterAll(() => rmSync(dir, { recursive: true, force: true }));
function decodeArgs(layout: { width: number; height: number }) {
const prep = planHdrResources({
composition: { duration: 1, width: 1920, height: 1080, videos: [], audios: [], images: [] },
nativeHdrVideoIds: new Set(),
nativeHdrImageIds: new Set(),
projectDir: dir,
compiledDir: dir,
});
prep.hdrExtractionDims.set("img", layout);
return {
log: { error: vi.fn(), warn: vi.fn(), info: vi.fn(), debug: vi.fn() },
hdrImageSrcPaths: new Map([["img", srcPath]]),
prep,
hdrDiagnostics: { videoExtractionFailures: 0, imageDecodeFailures: 0 },
width: 1920,
height: 1080,
};
}
it("resamples a 30000x30000 layout box no larger than the output frame", () => {
const args = decodeArgs({ width: 30000, height: 30000 });
const { buffers, releaseReservation } = decodeHdrImageBuffers(args);
try {
const buffer = buffers.get("img");
expect(buffer).toMatchObject({ width: 1080, height: 1080 });
expect(buffer?.data.length).toBeLessThanOrEqual(1920 * 1080 * 6);
expect(getHdrExtractionReservedBytes()).toBe(1080 * 1080 * 6);
expect(args.log.warn).toHaveBeenCalledOnce();
} finally {
releaseReservation();
}
});
it("fails the render when the image buffers exceed the HDR budget", () => {
vi.stubEnv("HDR_EXTRACTION_MAX_BYTES", String(1000));
const args = decodeArgs({ width: 100, height: 100 });
expect(() => decodeHdrImageBuffers(args)).toThrow(
/HDR image decode failed for image "img": Concurrent HDR pre-extractions/,
);
expect(args.hdrDiagnostics.imageDecodeFailures).toBe(1);
});
});
@@ -609,60 +609,95 @@ function holdFirstFrameUntilStreamStarts(
};
}
/** Shrinks the box uniformly so page CSS alone cannot size a buffer larger than the frame. */
export function fitHdrImageLayoutToFrame(
layout: { width: number; height: number },
frameWidth: number,
frameHeight: number,
): { width: number; height: number } {
const scale = Math.min(1, frameWidth / layout.width, frameHeight / layout.height);
if (scale === 1) return layout;
return {
width: Math.max(1, Math.floor(layout.width * scale)),
height: Math.max(1, Math.floor(layout.height * scale)),
};
}
export interface HdrImageDecodeResult {
buffers: Map<string, HdrImageBuffer>;
releaseReservation: () => void;
}
/**
* Decode each HDR image into an rgb48le buffer, resampling to the element's
* layout box if known. Failures abort the render to avoid shipping missing
* layers (the hot loop has no fallback for a missing HDR layer that the
* composition expects to see).
* layout box (fitted inside the output frame) if known. Every buffer is counted
* against the HDR extraction budget until the caller releases the reservation.
* Failures abort the render to avoid shipping missing layers (the hot loop has
* no fallback for a missing HDR layer that the composition expects to see).
*/
export function decodeHdrImageBuffers(args: {
log: ProducerLogger;
hdrImageSrcPaths: Map<string, string>;
prep: HdrResourcePrep;
hdrDiagnostics: HdrDiagnostics;
}): Map<string, HdrImageBuffer> {
const { log, hdrImageSrcPaths, prep, hdrDiagnostics } = args;
width: number;
height: number;
}): HdrImageDecodeResult {
const { log, hdrImageSrcPaths, prep, hdrDiagnostics, width, height } = args;
const budgetBytes = resolveHdrExtractionBudgetBytes(process.env[HDR_EXTRACTION_MAX_BYTES_ENV]);
const releases: Array<() => void> = [];
const releaseReservation = () => {
for (const release of releases.splice(0)) release();
};
const out = new Map<string, HdrImageBuffer>();
for (const [imageId, srcPath] of hdrImageSrcPaths) {
try {
const decoded = decodePngToRgb48le(readFileSync(srcPath));
const releaseDecoded = reserveHdrExtractionBytes(decoded.data.length, budgetBytes);
const layout = prep.hdrExtractionDims.get(imageId);
const fitInfo = prep.hdrImageFitInfo.get(imageId);
if (layout && (layout.width !== decoded.width || layout.height !== decoded.height)) {
const fit = normalizeObjectFit(fitInfo?.fit);
if (!layout || (layout.width === decoded.width && layout.height === decoded.height)) {
releases.push(releaseDecoded);
out.set(imageId, decoded);
continue;
}
const target = fitHdrImageLayoutToFrame(layout, width, height);
if (target !== layout) {
log.warn("HDR image layout box is larger than the output frame; drawing it smaller", {
imageId,
layout: `${layout.width}x${layout.height}`,
drawn: `${target.width}x${target.height}`,
});
}
try {
releases.push(reserveHdrExtractionBytes(target.width * target.height * 6, budgetBytes));
const fitInfo = prep.hdrImageFitInfo.get(imageId);
const resampled = resampleRgb48leObjectFit(
decoded.data,
decoded.width,
decoded.height,
layout.width,
layout.height,
fit,
target.width,
target.height,
normalizeObjectFit(fitInfo?.fit),
fitInfo?.position,
);
out.set(imageId, {
data: resampled,
width: layout.width,
height: layout.height,
});
} else {
out.set(imageId, {
data: Buffer.from(decoded.data),
width: decoded.width,
height: decoded.height,
});
out.set(imageId, { data: resampled, width: target.width, height: target.height });
} finally {
releaseDecoded();
}
} catch (err) {
releaseReservation();
hdrDiagnostics.imageDecodeFailures += 1;
const message = err instanceof Error ? err.message : String(err);
log.error("HDR image decode failed; aborting render", {
imageId,
srcPath,
error: err instanceof Error ? err.message : String(err),
error: message,
});
throw new Error(
`HDR image decode failed for image "${imageId}". ` +
`HDR image decode failed for image "${imageId}": ${message}. ` +
`Aborting render to avoid shipping missing HDR image layers.`,
);
}
}
return out;
return { buffers: out, releaseReservation };
}
@@ -222,6 +222,7 @@ export async function runCaptureHdrStage(
let hdrEncoderClosed = false;
let domSessionClosed = false;
let releaseHdrExtractionReservation: (() => void) | null = null;
let releaseHdrImageReservation: (() => void) | null = null;
const hdrVideoFrameSources = new Map<string, HdrVideoFrameSource>();
try {
await initializeSession(domSession);
@@ -308,12 +309,16 @@ export async function runCaptureHdrStage(
});
releaseHdrExtractionReservation = extracted.releaseReservation;
for (const [id, source] of extracted.sources) hdrVideoFrameSources.set(id, source);
const hdrImageBuffers = decodeHdrImageBuffers({
const decodedImages = decodeHdrImageBuffers({
log,
hdrImageSrcPaths,
prep,
hdrDiagnostics,
width,
height,
});
releaseHdrImageReservation = decodedImages.releaseReservation;
const hdrImageBuffers = decodedImages.buffers;
assertNotAborted();
@@ -475,6 +480,8 @@ export async function runCaptureHdrStage(
hdrVideoFrameSources.clear();
releaseHdrExtractionReservation?.();
releaseHdrExtractionReservation = null;
releaseHdrImageReservation?.();
releaseHdrImageReservation = null;
}
return {
@@ -179,7 +179,7 @@ mock.module("../../hdrCompositor.js", () => ({
mock.module("./captureHdrResources.js", () => ({
cleanupHdrVideoFrameSource: () => {},
decodeHdrImageBuffers: () => new Map(),
decodeHdrImageBuffers: () => ({ buffers: new Map(), releaseReservation: () => {} }),
extractHdrVideoFrames: async () => ({
sources: new Map(),
estimatedBytes: 0,
@@ -393,7 +393,7 @@ mock.module("../../hdrCompositor.js", () => ({
mock.module("./captureHdrResources.js", () => ({
cleanupHdrVideoFrameSource: () => {},
decodeHdrImageBuffers: () => new Map(),
decodeHdrImageBuffers: () => ({ buffers: new Map(), releaseReservation: () => {} }),
extractHdrVideoFrames: async () => ({
sources: new Map(),
estimatedBytes: 0,