mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(hdr): fail loudly on native asset extraction errors
This commit is contained in:
@@ -1,6 +1,55 @@
|
||||
import { readFileSync } from "fs";
|
||||
import { resolve } from "path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { extractVideoMetadata } from "./ffprobe.js";
|
||||
import { extractPngMetadataFromBuffer, extractVideoMetadata } from "./ffprobe.js";
|
||||
|
||||
function crc32(buf: Buffer): number {
|
||||
let crc = 0xffffffff;
|
||||
for (let i = 0; i < buf.length; i++) {
|
||||
crc ^= buf[i] ?? 0;
|
||||
for (let bit = 0; bit < 8; bit++) {
|
||||
const mask = -(crc & 1);
|
||||
crc = (crc >>> 1) ^ (0xedb88320 & mask);
|
||||
}
|
||||
}
|
||||
return (crc ^ 0xffffffff) >>> 0;
|
||||
}
|
||||
|
||||
function pngChunk(type: string, data: number[]): Buffer {
|
||||
const chunkData = Buffer.from(data);
|
||||
const header = Buffer.alloc(8);
|
||||
header.writeUInt32BE(chunkData.length, 0);
|
||||
header.write(type, 4, 4, "ascii");
|
||||
const crc = Buffer.alloc(4);
|
||||
crc.writeUInt32BE(crc32(Buffer.concat([Buffer.from(type, "ascii"), chunkData])), 0);
|
||||
return Buffer.concat([header, chunkData, crc]);
|
||||
}
|
||||
|
||||
function buildPngWithChunks(chunks: Buffer[]): Buffer {
|
||||
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), ...chunks]);
|
||||
}
|
||||
|
||||
function buildMinimalPng(options?: {
|
||||
cIcpAfterIdat?: boolean;
|
||||
invalidCrc?: boolean;
|
||||
longCicp?: boolean;
|
||||
}) {
|
||||
const ihdr = pngChunk("IHDR", [0, 0, 0, 1, 0, 0, 0, 1, 16, 2, 0, 0, 0]);
|
||||
const cicpData = options?.longCicp ? [9, 16, 0, 1, 255] : [9, 16, 0, 1];
|
||||
let cicp = pngChunk("cICP", cicpData);
|
||||
if (options?.invalidCrc) {
|
||||
cicp = Buffer.from(cicp);
|
||||
cicp[cicp.length - 1] ^= 0xff;
|
||||
}
|
||||
const idat = pngChunk(
|
||||
"IDAT",
|
||||
[0x78, 0x9c, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01],
|
||||
);
|
||||
const iend = pngChunk("IEND", []);
|
||||
return options?.cIcpAfterIdat
|
||||
? buildPngWithChunks([ihdr, idat, cicp, iend])
|
||||
: buildPngWithChunks([ihdr, cicp, idat, iend]);
|
||||
}
|
||||
|
||||
describe("extractVideoMetadata", () => {
|
||||
it("reads HDR PNG cICP metadata when ffprobe color fields are absent", async () => {
|
||||
@@ -18,3 +67,43 @@ describe("extractVideoMetadata", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractPngMetadataFromBuffer", () => {
|
||||
it("accepts a valid cICP chunk before IDAT", () => {
|
||||
const metadata = extractPngMetadataFromBuffer(buildMinimalPng());
|
||||
expect(metadata?.colorSpace).toEqual({
|
||||
colorPrimaries: "bt2020",
|
||||
colorTransfer: "smpte2084",
|
||||
colorSpace: "gbr",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects cICP chunks after IDAT", () => {
|
||||
const metadata = extractPngMetadataFromBuffer(buildMinimalPng({ cIcpAfterIdat: true }));
|
||||
expect(metadata).toEqual({
|
||||
width: 1,
|
||||
height: 1,
|
||||
colorSpace: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects cICP chunks with invalid CRC", () => {
|
||||
expect(extractPngMetadataFromBuffer(buildMinimalPng({ invalidCrc: true }))).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects cICP chunks whose payload is not exactly four bytes", () => {
|
||||
const metadata = extractPngMetadataFromBuffer(buildMinimalPng({ longCicp: true }));
|
||||
expect(metadata).toEqual({
|
||||
width: 1,
|
||||
height: 1,
|
||||
colorSpace: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("continues to parse the checked-in HDR PNG fixture", () => {
|
||||
const fixture = readFileSync(
|
||||
resolve(__dirname, "../../../producer/tests/hdr-image-only/src/hdr-photo.png"),
|
||||
);
|
||||
expect(extractPngMetadataFromBuffer(fixture)?.colorSpace?.colorTransfer).toBe("smpte2084");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -104,7 +104,19 @@ interface StillImageMetadata {
|
||||
colorSpace: VideoColorSpace | null;
|
||||
}
|
||||
|
||||
function extractPngMetadataFromBuffer(buf: Buffer): StillImageMetadata | null {
|
||||
function crc32(buf: Buffer): number {
|
||||
let crc = 0xffffffff;
|
||||
for (let i = 0; i < buf.length; i++) {
|
||||
crc ^= buf[i] ?? 0;
|
||||
for (let bit = 0; bit < 8; bit++) {
|
||||
const mask = -(crc & 1);
|
||||
crc = (crc >>> 1) ^ (0xedb88320 & mask);
|
||||
}
|
||||
}
|
||||
return (crc ^ 0xffffffff) >>> 0;
|
||||
}
|
||||
|
||||
export function extractPngMetadataFromBuffer(buf: Buffer): StillImageMetadata | null {
|
||||
if (
|
||||
buf.length < 8 ||
|
||||
buf[0] !== 137 ||
|
||||
@@ -121,19 +133,27 @@ function extractPngMetadataFromBuffer(buf: Buffer): StillImageMetadata | null {
|
||||
|
||||
let width = 0;
|
||||
let height = 0;
|
||||
let seenIdat = false;
|
||||
let pos = 8;
|
||||
while (pos + 12 <= buf.length) {
|
||||
const chunkLen = buf.readUInt32BE(pos);
|
||||
const chunkType = buf.toString("ascii", pos + 4, pos + 8);
|
||||
if (pos + 12 + chunkLen > buf.length) return null;
|
||||
const chunkData = buf.subarray(pos + 8, pos + 8 + chunkLen);
|
||||
const chunkCrc = buf.readUInt32BE(pos + 8 + chunkLen);
|
||||
const chunkBytes = Buffer.concat([Buffer.from(chunkType, "ascii"), chunkData]);
|
||||
if (crc32(chunkBytes) !== chunkCrc) return null;
|
||||
|
||||
if (chunkType === "IHDR" && chunkLen >= 8) {
|
||||
width = buf.readUInt32BE(pos + 8);
|
||||
height = buf.readUInt32BE(pos + 12);
|
||||
}
|
||||
|
||||
if (chunkType === "cICP" && chunkLen >= 4) {
|
||||
const chunkData = buf.subarray(pos + 8, pos + 12);
|
||||
if (chunkType === "IDAT") {
|
||||
seenIdat = true;
|
||||
}
|
||||
|
||||
if (chunkType === "cICP" && chunkLen === 4 && !seenIdat) {
|
||||
const primariesCode = chunkData[0] ?? 0;
|
||||
const transferCode = chunkData[1] ?? 0;
|
||||
const matrixCode = chunkData[2] ?? 0;
|
||||
|
||||
@@ -210,6 +210,12 @@ export interface RenderPerfSummary {
|
||||
stages: Record<string, number>;
|
||||
captureAvgMs?: number;
|
||||
capturePeakMs?: number;
|
||||
hdrDiagnostics?: HdrDiagnostics;
|
||||
}
|
||||
|
||||
export interface HdrDiagnostics {
|
||||
videoExtractionFailures: number;
|
||||
imageDecodeFailures: number;
|
||||
}
|
||||
|
||||
export interface RenderJob {
|
||||
@@ -235,6 +241,7 @@ export interface RenderJob {
|
||||
freeMemoryMB: number;
|
||||
browserConsoleTail?: string[];
|
||||
perfStages?: Record<string, number>;
|
||||
hdrDiagnostics?: HdrDiagnostics;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -646,6 +653,10 @@ export async function executeRenderJob(
|
||||
let lastBrowserConsole: string[] = [];
|
||||
let restoreLogger: (() => void) | null = null;
|
||||
const perfStages: Record<string, number> = {};
|
||||
const hdrDiagnostics: HdrDiagnostics = {
|
||||
videoExtractionFailures: 0,
|
||||
imageDecodeFailures: 0,
|
||||
};
|
||||
const perfOutputPath = join(workDir, "perf-summary.json");
|
||||
const cfg = { ...(job.config.producerConfig ?? resolveConfig()) };
|
||||
const outputFormat = (job.config.format ?? "mp4") as "mp4" | "webm" | "mov";
|
||||
@@ -1391,11 +1402,16 @@ export async function executeRenderJob(
|
||||
];
|
||||
const result = await runFfmpeg(ffmpegArgs, { signal: abortSignal });
|
||||
if (!result.success) {
|
||||
log.warn("HDR frame pre-extraction failed; loop will fill with black", {
|
||||
hdrDiagnostics.videoExtractionFailures += 1;
|
||||
log.error("HDR frame pre-extraction failed; aborting render", {
|
||||
videoId,
|
||||
srcPath,
|
||||
stderr: result.stderr.slice(-400),
|
||||
});
|
||||
throw new Error(
|
||||
`HDR frame extraction failed for video "${videoId}". ` +
|
||||
`Aborting render to avoid shipping black HDR layers.`,
|
||||
);
|
||||
}
|
||||
hdrFrameDirs.set(videoId, frameDir);
|
||||
}
|
||||
@@ -1443,11 +1459,16 @@ export async function executeRenderJob(
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn("HDR image decode failed; layer will be empty", {
|
||||
hdrDiagnostics.imageDecodeFailures += 1;
|
||||
log.error("HDR image decode failed; aborting render", {
|
||||
imageId,
|
||||
srcPath,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
throw new Error(
|
||||
`HDR image decode failed for image "${imageId}". ` +
|
||||
`Aborting render to avoid shipping missing HDR image layers.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2253,6 +2274,10 @@ export async function executeRenderJob(
|
||||
videoCount: composition.videos.length,
|
||||
audioCount: composition.audios.length,
|
||||
stages: perfStages,
|
||||
hdrDiagnostics:
|
||||
hdrDiagnostics.videoExtractionFailures > 0 || hdrDiagnostics.imageDecodeFailures > 0
|
||||
? { ...hdrDiagnostics }
|
||||
: undefined,
|
||||
captureAvgMs:
|
||||
totalFrames > 0 ? Math.round((perfStages.captureMs ?? 0) / totalFrames) : undefined,
|
||||
};
|
||||
@@ -2354,6 +2379,10 @@ export async function executeRenderJob(
|
||||
freeMemoryMB: freeMemMB,
|
||||
browserConsoleTail: lastBrowserConsole.length > 0 ? lastBrowserConsole.slice(-30) : undefined,
|
||||
perfStages: Object.keys(perfStages).length > 0 ? { ...perfStages } : undefined,
|
||||
hdrDiagnostics:
|
||||
hdrDiagnostics.videoExtractionFailures > 0 || hdrDiagnostics.imageDecodeFailures > 0
|
||||
? { ...hdrDiagnostics }
|
||||
: undefined,
|
||||
};
|
||||
|
||||
// Cleanup
|
||||
|
||||
Reference in New Issue
Block a user