fix(hdr): fail loudly on native asset extraction errors

This commit is contained in:
vanceingalls
2026-04-20 18:38:19 +00:00
parent 60ae12a7e6
commit dbbbbb186f
3 changed files with 144 additions and 6 deletions
+90 -1
View File
@@ -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");
});
});
+23 -3
View File
@@ -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;