Skip to content

Commit c9b3d9c

Browse files
fix(engine): SDR renders keep footage and page colours as in the source (#4564)
* fix(engine): declare SDR extracted frames as sRGB so Chrome does not darken footage * fix(engine): encode SDR mp4 frames with the BT.709 matrix they are tagged with Chrome hands the SDR encoder BT.601 full-range JPEG captures (or RGB PNGs). The encoders only converted the range, so swscale kept the BT.601 matrix under the BT.709 tag (ffmpeg 5.1 to 7.0: CSS #C83C28 delivered as #D44824) or converted YUV to YUV through a low-precision step (ffmpeg 8+: #101010 delivered as #0B0D0D). Decode to RGB with the capture matrix, then encode with BT.709. Co-authored-by: Kenneth Shortrede <[email protected]> * fix(engine): tag SDR frames plain sRGB so every ffmpeg writes the sRGB chunk ffmpeg writes a PNG sRGB chunk only for BT.709 primaries and writes cICP only from 6.1. On ffmpeg 5.1 (Debian bookworm) a BT.601-tagged source therefore got cHRM plus gAMA 1/2.2, which Chrome decodes as a pure 2.2 power: 0x10 shadows rendered as 0x07. Declaring BT.709 primaries too gives the sRGB chunk on every version, so Chrome shows the frame code values for every SDR source. The test now reads the PNG chunks instead of ffprobe, which reports no transfer for an sRGB chunk on ffmpeg 5.1. Co-authored-by: Kenneth Shortrede <[email protected]> * fix(engine): extract SDR jpg frames as the BT.601 full range JPEG readers assume The default frame format for opaque SDR sources is JPEG. ffmpeg converted the BT.709 source to full-range YUV without changing the matrix, and every JPEG reader (Chrome included) decodes BT.601, so footage shifted: 0xC83C28 showed as 0xBB2D29. The old mp4 encode made the inverse mistake and hid it; with the encode fixed, it would reach the output. Decode through RGB with the source matrix, then encode BT.601 full range, keeping 4:2:0. Co-authored-by: Kenneth Shortrede <[email protected]> * fix(engine): round the SDR mp4 colour conversion accurately swscale's default rounding moved saturated colours up to 3 codes (pure red 254 delivered as 251 on ffmpeg 5.1 to 7.0); accurate_rnd brings that to 1. The chunk encoder now decides the HDR-or-SDR filter once, and the 10-bit GPU pad test sets hdr, which its name always claimed. * style(engine): keep the colour filter reasons in names and tests, not source comments The comment ratchet caps a file's comment share: the new filter module and the extractor carry their intent in constant names, and the encode test states why the conversion goes through RGB. * fix(engine): keep colour edges sharp through the SDR colour conversions Going through RGB upsampled chroma with swscale's default filter, so ffmpeg 4.4 to 7.x smeared colour across edges by about 2 px (red 254 next to blue came out 8 inside the blue). Nearest-neighbour resampling maps the half-resolution chroma back one to one, keeps the flat-colour results, and makes accurate_rnd redundant. The pixel tests now also check a red to blue edge. * test(producer): regenerate goldens whose colours the old mp4 matrix bent These four fixtures compare at 40 to 45 dB, and their goldens were rendered with the BT.601-under-BT.709 encode, so the corrected colours score 34 to 37 dB. Regenerated in the Dockerfile.test image. Each new golden is closer to the colour its page declares, e.g. #00d4aa was 00bcab and is now 00d3aa, and #087443 was 006643 and is now 087241. * fix(engine): keep VFR frame timing when extraction applies colour filters ffmpeg before 6.1 ignores frame durations in CFR resampling once any -vf is set, so a VFR window ending on a still lost frames. Filtered VFR extraction now resamples in one process exactly as before and filters in a second. Co-authored-by: Kenneth Shortrede <[email protected]> * fix(engine): encode raw SDR layered frames in BT.709 Co-authored-by: Kenneth Shortrede <[email protected]> * test(engine): keep the new colour tests inside the format, comment and argv checks * fix(engine): keep HDR and unusual pixel formats out of the two-process VFR path The raw handoff dropped HDR10 light levels and misread semi-planar formats, and decoder options rejected some ffprobe colour names. Only SDR sources in formats nut carries unchanged now take it, their colour tags come back through setparams, and a process that cannot start releases the other. Co-authored-by: Kenneth Shortrede <[email protected]> * fix(engine): read untagged footage with the matrix Chrome plays it with Chrome decodes an untagged matrix as BT.709 from 720 coded lines up and as BT.601 below; extraction assumed BT.601 at every size, so HD footage without colour tags shifted hue against Studio's own preview. Co-authored-by: Kenneth Shortrede <[email protected]> * fix(engine): untagged VP9 and AV1 footage keeps BT.601, as Chrome plays it Chrome applies the 720-line rule only to codecs it decodes through ffmpeg (H.264, VP8); untagged VP9, VP9 alpha and AV1 play as BT.601 at every size. Also widens the raw-pipe pixel formats to all verified ones, and reports the pipeline process that failed first by settle order, not by per-process clocks. Co-authored-by: Kenneth Shortrede <[email protected]> * test(producer): regenerate goldens for corrected page colours and untagged SD footage * docs(engine): say which untagged codecs the matrix rule was measured on --------- Co-authored-by: Kenneth Shortrede <[email protected]>
1 parent fcfcafa commit c9b3d9c

20 files changed

Lines changed: 873 additions & 103 deletions

File tree

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

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { spawnSync } from "node:child_process";
12
import { EventEmitter } from "node:events";
23
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
34
import { tmpdir } from "node:os";
@@ -13,6 +14,10 @@ import {
1314
resolveLockedGopSize,
1415
} from "./chunkEncoder.js";
1516
import { renderProvenanceArgs } from "../utils/renderProvenance.js";
17+
import { SDR_CAPTURE_TO_BT709_FILTER } from "../utils/sdrCaptureColor.js";
18+
import { getFfmpegBinary } from "../utils/ffmpegBinaries.js";
19+
20+
const HAS_FFMPEG = spawnSync(getFfmpegBinary(), ["-version"]).status === 0;
1621

1722
const TINY_PNG = Buffer.from(
1823
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAACXBIWXMAAAABAAAAAQBPJcTWAAAAEElEQVR4nGP8wwACLGCSAQANBAECv1AVswAAAABJRU5ErkJggg==",
@@ -1071,7 +1076,7 @@ describe("buildEncoderArgs color space", () => {
10711076
);
10721077
const vfIdx = args.indexOf("-vf");
10731078
expect(vfIdx).toBeGreaterThan(-1);
1074-
expect(args[vfIdx + 1]).toBe("scale=in_range=pc:out_range=tv");
1079+
expect(args[vfIdx + 1]).toBe(SDR_CAPTURE_TO_BT709_FILTER);
10751080
});
10761081

10771082
it("adds the pad after range conversion for odd CPU output dimensions", () => {
@@ -1081,7 +1086,7 @@ describe("buildEncoderArgs color space", () => {
10811086
"out.mp4",
10821087
);
10831088
const vfIdx = args.indexOf("-vf");
1084-
expect(args[vfIdx + 1]).toBe("scale=in_range=pc:out_range=tv,pad=ceil(iw/2)*2:ceil(ih/2)*2");
1089+
expect(args[vfIdx + 1]).toBe(`${SDR_CAPTURE_TO_BT709_FILTER},pad=ceil(iw/2)*2:ceil(ih/2)*2`);
10851090
});
10861091

10871092
it("prepends range conversion to VAAPI filter chain", () => {
@@ -1093,10 +1098,10 @@ describe("buildEncoderArgs color space", () => {
10931098
);
10941099
const vfIdx = args.indexOf("-vf");
10951100
expect(vfIdx).toBeGreaterThan(-1);
1096-
expect(args[vfIdx + 1]).toBe("scale=in_range=pc:out_range=tv,format=nv12,hwupload");
1101+
expect(args[vfIdx + 1]).toBe(`${SDR_CAPTURE_TO_BT709_FILTER},format=nv12,hwupload`);
10971102
});
10981103

1099-
it("pads odd dimensions (no range scale) for non-VAAPI GPU encoding", () => {
1104+
it("converts to BT.709 and pads odd dimensions for non-VAAPI GPU encoding", () => {
11001105
for (const gpu of ["nvenc", "videotoolbox", "qsv", "amf"] as const) {
11011106
const args = buildEncoderArgs(
11021107
{
@@ -1112,10 +1117,7 @@ describe("buildEncoderArgs color space", () => {
11121117
gpu,
11131118
);
11141119
const vfIdx = args.indexOf("-vf");
1115-
// 4:2:0 HW encode still aborts on odd dims, so the pad must be present —
1116-
// but the range scale belongs to the SW path only.
1117-
expect(args[vfIdx + 1]).toBe("pad=ceil(iw/2)*2:ceil(ih/2)*2");
1118-
expect(args[vfIdx + 1]).not.toContain("scale=in_range");
1120+
expect(args[vfIdx + 1]).toBe(`${SDR_CAPTURE_TO_BT709_FILTER},pad=ceil(iw/2)*2:ceil(ih/2)*2`);
11191121
// but still has color metadata
11201122
expect(args).toContain("-colorspace:v");
11211123
}
@@ -1128,7 +1130,7 @@ describe("buildEncoderArgs color space", () => {
11281130
"out.mp4",
11291131
"videotoolbox",
11301132
);
1131-
expect(args).not.toContain("-vf");
1133+
expect(args[args.indexOf("-vf") + 1]).toBe(SDR_CAPTURE_TO_BT709_FILTER);
11321134
});
11331135

11341136
it("pads odd dimensions for 10-bit (yuv420p10le) GPU HDR encoding", () => {
@@ -1141,6 +1143,7 @@ describe("buildEncoderArgs color space", () => {
11411143
quality: 23,
11421144
useGpu: true,
11431145
pixelFormat: "yuv420p10le",
1146+
hdr: { transfer: "pq" },
11441147
},
11451148
inputArgs,
11461149
"out.mp4",
@@ -1684,14 +1687,14 @@ describe("buildEncoderArgs HDR color space", () => {
16841687
expect(args[vfIdx + 1]).toContain("scale=in_range=pc:out_range=tv");
16851688
});
16861689

1687-
it("uses same range conversion for SDR CPU encoding", () => {
1690+
it("converts SDR CPU captures to the BT.709 matrix", () => {
16881691
const args = buildEncoderArgs(
16891692
{ ...baseOptions, codec: "h264", preset: "medium", quality: 23 },
16901693
inputArgs,
16911694
"out.mp4",
16921695
);
16931696
const vfIdx = args.indexOf("-vf");
1694-
expect(args[vfIdx + 1]).toContain("scale=in_range=pc:out_range=tv");
1697+
expect(args[vfIdx + 1]).toBe(SDR_CAPTURE_TO_BT709_FILTER);
16951698
});
16961699

16971700
it("tags BT.2020 + transfer for HDR GPU H.265 (no mastering metadata via -x265-params)", () => {
@@ -1741,3 +1744,78 @@ describe("buildConcatArgs", () => {
17411744
expect(args.at(-1)).toBe("/w/video-only.mp4");
17421745
});
17431746
});
1747+
1748+
describe.skipIf(!HAS_FFMPEG)("buildEncoderArgs SDR colour", () => {
1749+
// Chrome captures are BT.601 JPEGs. A direct YUV-to-YUV scale keeps that matrix under the BT.709
1750+
// tag (ffmpeg 7 and older) or tints greys (8 and newer), so the encode goes through RGB.
1751+
it("delivers Chrome's JPEG colours in the BT.709 the mp4 is tagged with", () => {
1752+
const ffmpeg = getFfmpegBinary();
1753+
const dir = mkdtempSync(join(tmpdir(), "hf-sdr-colour-"));
1754+
const rgbAt = (file: string, decode: string, x: number): number[] => [
1755+
...spawnSync(ffmpeg, [
1756+
"-v",
1757+
"error",
1758+
"-i",
1759+
file,
1760+
"-vf",
1761+
`${decode}format=rgb24,crop=1:1:${x}:8`,
1762+
"-frames:v",
1763+
"1",
1764+
"-f",
1765+
"rawvideo",
1766+
"-",
1767+
]).stdout,
1768+
];
1769+
try {
1770+
for (const color of ["0xC83C28", "0xFE0000", "0x101010", "0x2050E0"]) {
1771+
const jpg = join(dir, "frame.jpg");
1772+
const out = join(dir, "out.mp4");
1773+
const synth = spawnSync(ffmpeg, [
1774+
"-v",
1775+
"error",
1776+
"-y",
1777+
"-f",
1778+
"lavfi",
1779+
"-i",
1780+
`color=c=${color}:s=64x16,format=rgb24,drawbox=x=31:y=0:w=33:h=16:c=0x0000FE:t=fill`,
1781+
"-frames:v",
1782+
"1",
1783+
"-pix_fmt",
1784+
"yuvj420p",
1785+
jpg,
1786+
]);
1787+
expect(synth.status).toBe(0);
1788+
const args = buildEncoderArgs(
1789+
{
1790+
fps: { num: 30, den: 1 },
1791+
width: 64,
1792+
height: 16,
1793+
codec: "h264",
1794+
preset: "ultrafast",
1795+
quality: 0,
1796+
},
1797+
["-i", jpg],
1798+
out,
1799+
);
1800+
expect(spawnSync(ffmpeg, args).status).toBe(0);
1801+
1802+
// x=8 is flat colour; x=32 sits one pixel inside the blue edge, where a smoothing
1803+
// chroma resample bleeds the colour across.
1804+
for (const [x, limit] of [
1805+
[8, 2],
1806+
[32, 2],
1807+
] as const) {
1808+
const captured = rgbAt(jpg, "", x);
1809+
const delivered = rgbAt(out, "scale=in_color_matrix=bt709:in_range=tv,", x);
1810+
const worst = Math.max(...delivered.map((v, i) => Math.abs(v - captured[i]!)));
1811+
expect(
1812+
worst,
1813+
`${color} x=${x}: capture ${captured} delivered ${delivered}`,
1814+
).toBeLessThanOrEqual(limit);
1815+
}
1816+
}
1817+
} finally {
1818+
rmSync(dir, { recursive: true, force: true });
1819+
}
1820+
}, 30_000);
1821+
});

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from "../utils/gpuEncoder.js";
3232
import { type HdrTransfer, getHdrEncoderColorParams } from "../utils/hdr.js";
3333
import { withEvenDimensionPad } from "../utils/evenDimensions.js";
34+
import { SDR_CAPTURE_TO_BT709_FILTER } from "../utils/sdrCaptureColor.js";
3435
import { formatFfmpegError, isExternalFfmpegInterruption, runFfmpeg } from "../utils/runFfmpeg.js";
3536
import { extractAudioMetadata } from "../utils/ffprobe.js";
3637
import { type Fps, fpsToFfmpegArg, fpsToNumber } from "@hyperframes/core";
@@ -446,34 +447,31 @@ export function buildEncoderArgs(
446447
);
447448
}
448449

449-
// Range conversion: Chrome's full-range RGB → limited/TV range.
450+
// Range conversion: Chrome's full-range capture → limited/TV range; SDR also
451+
// converts to the BT.709 matrix it is tagged with.
452+
const sdrFilter = options.hdr ? undefined : SDR_CAPTURE_TO_BT709_FILTER;
453+
const captureFilter = sdrFilter ?? "scale=in_range=pc:out_range=tv";
450454
if (gpuEncoder === "vaapi") {
451455
// vaapi already runs `format=nv12,hwupload`; the nv12 conversion aligns
452-
// odd dimensions before upload, so only prepend the range conversion.
456+
// odd dimensions before upload, so only prepend the colour conversion.
453457
const vfIdx = args.indexOf("-vf");
454458
if (vfIdx !== -1) {
455-
args[vfIdx + 1] = `scale=in_range=pc:out_range=tv,${args[vfIdx + 1]}`;
459+
args[vfIdx + 1] = `${captureFilter},${args[vfIdx + 1]}`;
456460
}
457461
} else if (shouldUseGpu) {
458462
// nvenc/videotoolbox/qsv/amf feed software frames straight to the HW
459-
// encoder with no `-vf`. They hit the same "height not divisible by 2"
460-
// abort as libx264 on an odd-sized 4:2:0 canvas, so pad odd dimensions
461-
// up to even on the software side before the encode.
462-
const vf = withEvenDimensionPad("", pixelFormat, options.width, options.height);
463+
// encoder. They hit the same "height not divisible by 2" abort as
464+
// libx264 on an odd-sized 4:2:0 canvas, so pad odd dimensions up to even
465+
// on the software side before the encode.
466+
const vf = withEvenDimensionPad(sdrFilter ?? "", pixelFormat, options.width, options.height);
463467
if (vf) args.push("-vf", vf);
464468
} else {
465-
// Range conversion: Chrome screenshots are full-range RGB.
466469
// The scale filter handles both 8-bit and 10-bit correctly. Pad odd
467470
// dimensions up to even so libx264/libx265 (4:2:0) don't abort with
468471
// "height not divisible by 2" on an odd-sized composition canvas.
469472
args.push(
470473
"-vf",
471-
withEvenDimensionPad(
472-
"scale=in_range=pc:out_range=tv",
473-
pixelFormat,
474-
options.width,
475-
options.height,
476-
),
474+
withEvenDimensionPad(captureFilter, pixelFormat, options.width, options.height),
477475
);
478476
}
479477

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ function seedPartialDir(entry: { dir: string; keyHash: string }, frameContent: s
6565
}
6666

6767
describe("extractionCache constants", () => {
68-
it("exposes the v4 schema prefix", () => {
69-
expect(SCHEMA_PREFIX).toBe("hfcache-v4-");
68+
it("exposes the v5 schema prefix", () => {
69+
expect(SCHEMA_PREFIX).toBe("hfcache-v5-");
7070
});
7171

7272
it("exposes the frame filename prefix shared with the extractor", () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const GC_MARKER = ".hf-last-gc";
6767
* a JavaScript number. This invalidates entries created after rational NTSC
6868
* rates had already been rounded to a decimal.
6969
*/
70-
export const SCHEMA_PREFIX = "hfcache-v4-";
70+
export const SCHEMA_PREFIX = "hfcache-v5-";
7171

7272
/** Truncated hex chars of SHA-256 used for the entry directory name. */
7373
const KEY_HEX_CHARS = 16;

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

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
import { validJpeg } from "./__fixtures__/jpeg.js";
1414

15+
import { spawnSync } from "child_process";
1516
import { EventEmitter } from "events";
16-
import { mkdtempSync } from "fs";
17+
import { mkdtempSync, rmSync } from "fs";
1718
import { tmpdir } from "os";
1819
import { basename, join } from "path";
1920
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -24,6 +25,7 @@ import {
2425
type StreamingEncoderOptions,
2526
} from "./streamingEncoder.js";
2627
import { DEFAULT_HDR10_MASTERING } from "../utils/hdr.js";
28+
import { SDR_CAPTURE_TO_BT709_FILTER } from "../utils/sdrCaptureColor.js";
2729

2830
const baseHdrPq: StreamingEncoderOptions = {
2931
fps: { num: 30, den: 1 },
@@ -167,13 +169,13 @@ describe("buildStreamingArgs", () => {
167169
expect(args[args.indexOf("-color_primaries:v") + 1]).toBe("bt709");
168170
expect(args[args.indexOf("-colorspace:v") + 1]).toBe("bt709");
169171
expect(args[args.indexOf("-color_range") + 1]).toBe("tv");
170-
expect(args[args.indexOf("-vf") + 1]).toBe("scale=in_range=pc:out_range=tv");
172+
expect(args[args.indexOf("-vf") + 1]).toBe(SDR_CAPTURE_TO_BT709_FILTER);
171173
});
172174

173175
it("adds the pad after range conversion for odd SDR output dimensions", () => {
174176
const args = buildStreamingArgs({ ...baseSdr, height: 1081 }, "/tmp/out.mp4");
175177
expect(args[args.indexOf("-vf") + 1]).toBe(
176-
"scale=in_range=pc:out_range=tv,pad=ceil(iw/2)*2:ceil(ih/2)*2",
178+
`${SDR_CAPTURE_TO_BT709_FILTER},pad=ceil(iw/2)*2:ceil(ih/2)*2`,
177179
);
178180
});
179181
});
@@ -340,27 +342,26 @@ describe("buildStreamingArgs", () => {
340342
expect(h265Args[h265Args.indexOf("-qp_i") + 1]).toBe("23");
341343
});
342344

343-
// 4:2:0 HW encode aborts on odd dims just like libx264, and these paths
344-
// feed software frames straight to the encoder with no `-vf`, so the
345-
// even-dim pad (and only the pad, not the SW range scale) must be added.
346-
it("pads odd dimensions (no range scale) for non-VAAPI GPU encoding", () => {
345+
// 4:2:0 HW encode aborts on odd dims just like libx264, so the pad follows the colour conversion.
346+
it("converts to BT.709 and pads odd dimensions for non-VAAPI GPU encoding", () => {
347347
for (const gpu of ["nvenc", "videotoolbox", "qsv", "amf"] as const) {
348348
const args = buildStreamingArgs({ ...baseGpu, height: 1081 }, "/tmp/out.mp4", gpu);
349349
const vfIdx = args.indexOf("-vf");
350-
expect(args[vfIdx + 1]).toBe("pad=ceil(iw/2)*2:ceil(ih/2)*2");
351-
expect(args[vfIdx + 1]).not.toContain("scale=in_range");
350+
expect(args[vfIdx + 1]).toBe(
351+
`${SDR_CAPTURE_TO_BT709_FILTER},pad=ceil(iw/2)*2:ceil(ih/2)*2`,
352+
);
352353
}
353354
});
354355

355356
it("does not require the pad filter for even GPU output dimensions", () => {
356357
const args = buildStreamingArgs(baseGpu, "/tmp/out.mp4", "videotoolbox");
357-
expect(args).not.toContain("-vf");
358+
expect(args[args.indexOf("-vf") + 1]).toBe(SDR_CAPTURE_TO_BT709_FILTER);
358359
});
359360

360361
it("prepends range conversion to VAAPI chain (nv12 covers even-dim)", () => {
361362
const args = buildStreamingArgs(baseGpu, "/tmp/out.mp4", "vaapi");
362363
const vfIdx = args.indexOf("-vf");
363-
expect(args[vfIdx + 1]).toBe("scale=in_range=pc:out_range=tv,format=nv12,hwupload");
364+
expect(args[vfIdx + 1]).toBe(`${SDR_CAPTURE_TO_BT709_FILTER},format=nv12,hwupload`);
364365
});
365366
});
366367

@@ -1130,3 +1131,81 @@ describe("createFrameReorderBuffer abort (interleaved parallel drain)", () => {
11301131
await expect(buf.waitForFrame(1)).resolves.toBeUndefined();
11311132
});
11321133
});
1134+
1135+
describe.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)(
1136+
"buildStreamingArgs raw SDR colour",
1137+
() => {
1138+
// ffmpeg 7 and older convert raw RGB with the BT.601 matrix unless told otherwise.
1139+
it("delivers raw sRGB frames in the BT.709 the mp4 is tagged with", () => {
1140+
const dir = mkdtempSync(join(tmpdir(), "se-raw-sdr-"));
1141+
const rgbAt = (
1142+
inputFormat: string[],
1143+
source: string,
1144+
decode: string,
1145+
x: number,
1146+
stdin?: Buffer,
1147+
): number[] => [
1148+
...spawnSync(
1149+
"ffmpeg",
1150+
[
1151+
"-v",
1152+
"error",
1153+
...inputFormat,
1154+
"-i",
1155+
source,
1156+
"-vf",
1157+
`${decode}format=rgb24,crop=1:1:${x}:8`,
1158+
"-frames:v",
1159+
"1",
1160+
"-f",
1161+
"rawvideo",
1162+
"-",
1163+
],
1164+
{ input: stdin },
1165+
).stdout,
1166+
];
1167+
try {
1168+
const frame = spawnSync("ffmpeg", [
1169+
"-v",
1170+
"error",
1171+
"-f",
1172+
"lavfi",
1173+
"-i",
1174+
"color=c=0xC83C28:s=64x16,format=rgb24,drawbox=x=31:y=0:w=33:h=16:c=0x0000FE:t=fill",
1175+
"-frames:v",
1176+
"1",
1177+
"-pix_fmt",
1178+
"rgb48le",
1179+
"-f",
1180+
"rawvideo",
1181+
"-",
1182+
]).stdout;
1183+
const out = join(dir, "out.mp4");
1184+
const args = buildStreamingArgs(
1185+
{
1186+
...baseSdr,
1187+
width: 64,
1188+
height: 16,
1189+
preset: "ultrafast",
1190+
quality: 0,
1191+
rawInputFormat: "rgb48le",
1192+
},
1193+
out,
1194+
);
1195+
expect(spawnSync("ffmpeg", args, { input: frame }).status).toBe(0);
1196+
1197+
const rawInput = ["-f", "rawvideo", "-pix_fmt", "rgb48le", "-s", "64x16"];
1198+
// x=8 is flat colour; x=32 sits one pixel inside the blue edge.
1199+
for (const x of [8, 32]) {
1200+
const source = rgbAt(rawInput, "-", "", x, frame);
1201+
const delivered = rgbAt([], out, "scale=in_color_matrix=bt709:in_range=tv,", x);
1202+
expect([source.length, delivered.length]).toEqual([3, 3]);
1203+
const worst = Math.max(...delivered.map((v, i) => Math.abs(v - source[i]!)));
1204+
expect(worst, `x=${x}: source ${source} delivered ${delivered}`).toBeLessThanOrEqual(2);
1205+
}
1206+
} finally {
1207+
rmSync(dir, { recursive: true, force: true });
1208+
}
1209+
}, 30_000);
1210+
},
1211+
);

0 commit comments

Comments
 (0)