Skip to content

Commit 401354c

Browse files
jrusso1020claude
andauthored
fix(engine): keep the rightmost 8 columns of SDR MP4 at widths like 1080 (#5559)
#4564 routes SDR colour through `format=gbrp`. Without accurate_rnd, swscale's yuv-to-gbrp conversion leaves the last 8 columns black when the width is 16n + 8 (1000, 1080, 1112...), so every 1080x1920 portrait MP4 since 0.8.81 has had an 8 px black strip on the right. The same step in SDR jpg frame extraction blanked the right edge of footage at those widths. Set accurate_rnd on the yuv-to-gbrp scale in both chains. It is also the more accurate rounding: on a testsrc2 frame, worst luma error against a float reference drops from 1.85 to 0.90 codes. Fixes #5551 Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent bf4cf86 commit 401354c

4 files changed

Lines changed: 100 additions & 2 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,50 @@ describe.skipIf(!HAS_FFMPEG)("buildEncoderArgs SDR colour", () => {
18971897
}
18981898
}, 30_000);
18991899

1900+
it("keeps the rightmost 8 columns of a 1080-wide (16n + 8) mp4", () => {
1901+
const dir = mkdtempSync(join(tmpdir(), "hf-sdr-right-edge-"));
1902+
try {
1903+
const jpg = join(dir, "frame.jpg");
1904+
const out = join(dir, "out.mp4");
1905+
const synth = spawnSync(ffmpeg, [
1906+
"-v",
1907+
"error",
1908+
"-y",
1909+
"-f",
1910+
"lavfi",
1911+
"-i",
1912+
"color=c=0xC83C28:s=1080x16,format=rgb24",
1913+
"-frames:v",
1914+
"1",
1915+
"-pix_fmt",
1916+
"yuvj420p",
1917+
jpg,
1918+
]);
1919+
expect(synth.status).toBe(0);
1920+
const args = buildEncoderArgs(
1921+
{
1922+
fps: { num: 30, den: 1 },
1923+
width: 1080,
1924+
height: 16,
1925+
codec: "h264",
1926+
preset: "ultrafast",
1927+
quality: 0,
1928+
},
1929+
["-i", jpg],
1930+
out,
1931+
);
1932+
expect(spawnSync(ffmpeg, args).status).toBe(0);
1933+
for (const x of [1071, 1075, 1079]) {
1934+
const captured = pixelAt(jpg, "", x);
1935+
const delivered = pixelAt(out, "scale=in_color_matrix=bt709:in_range=tv,", x);
1936+
const worst = Math.max(...delivered.map((v, i) => Math.abs(v - captured[i]!)));
1937+
expect(worst, `x=${x}: capture ${captured} delivered ${delivered}`).toBeLessThanOrEqual(2);
1938+
}
1939+
} finally {
1940+
rmSync(dir, { recursive: true, force: true });
1941+
}
1942+
}, 30_000);
1943+
19001944
// Untagged HD reads as BT.709 in QuickTime, so ProRes has to be BT.709 too. MOV captures are
19011945
// RGBA PNGs, and the conversion must leave their alpha plane alone. The tags come from
19021946
// setparams because prores_ks ignores -color_* (ffmpeg 5.1 writes none, 8.1 only the matrix).

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,60 @@ describe.skipIf(!HAS_FFMPEG)("video frame extraction format", () => {
13821382
};
13831383
}
13841384

1385+
it("keeps the rightmost 8 columns of a 1080-wide (16n + 8) jpg extraction", async () => {
1386+
const source = join(FIXTURE_DIR, "white-1080.mp4");
1387+
const synth = await runFfmpeg([
1388+
"-y",
1389+
"-hide_banner",
1390+
"-loglevel",
1391+
"error",
1392+
"-f",
1393+
"lavfi",
1394+
"-i",
1395+
"color=c=white:s=1080x16:d=1:r=1",
1396+
"-c:v",
1397+
"libx264",
1398+
"-preset",
1399+
"ultrafast",
1400+
"-crf",
1401+
"0",
1402+
"-pix_fmt",
1403+
"yuv420p",
1404+
source,
1405+
]);
1406+
expect(synth.success).toBe(true);
1407+
const outDir = join(FIXTURE_DIR, "out-white-1080");
1408+
mkdirSync(outDir, { recursive: true });
1409+
const result = await extractAllVideoFrames(
1410+
[{ ...fixtureVideo(), id: "white", src: source }],
1411+
FIXTURE_DIR,
1412+
{ fps: 1, outputDir: outDir },
1413+
);
1414+
expect(result.errors).toEqual([]);
1415+
const frame = result.extracted[0]!.framePaths.get(0)!;
1416+
expect(frame.endsWith(".jpg")).toBe(true);
1417+
const lastColumns = (file: string) => [
1418+
...spawnSync("ffmpeg", [
1419+
"-v",
1420+
"error",
1421+
"-i",
1422+
file,
1423+
"-frames:v",
1424+
"1",
1425+
"-vf",
1426+
"format=gray,crop=8:1:iw-8:8",
1427+
"-f",
1428+
"rawvideo",
1429+
"-",
1430+
]).stdout,
1431+
];
1432+
const delivered = lastColumns(frame);
1433+
const expected = lastColumns(source);
1434+
expect(delivered).toHaveLength(8);
1435+
for (const [i, v] of delivered.entries())
1436+
expect(Math.abs(v - expected[i]!)).toBeLessThanOrEqual(5);
1437+
}, 60_000);
1438+
13851439
it("keeps color-sensitive UI reds closer to source when extraction is forced to png", async () => {
13861440
const defaultOut = join(FIXTURE_DIR, "out-default");
13871441
const pngOut = join(FIXTURE_DIR, "out-png");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ const HDR_TO_SDR_ZSCALE_TRANSFORM_KEY = "hdr2sdr-hable-srgb";
258258
const HDR_TO_SDR_VIDEOTOOLBOX_TRANSFORM_KEY = "hdr2sdr-videotoolbox-hdr";
259259
const SDR_CANVAS_PASSTHROUGH_FILTER = "setparams=color_primaries=bt709:color_trc=iec61966-2-1";
260260
const SDR_JPEG_AS_BT601_FULL_RANGE_FILTER =
261-
"scale=flags=neighbor,format=gbrp,scale=out_color_matrix=bt601:out_range=pc:flags=neighbor,format=yuv420p";
261+
"scale=flags=neighbor+accurate_rnd,format=gbrp,scale=out_color_matrix=bt601:out_range=pc:flags=neighbor,format=yuv420p";
262262

263263
let warnedMissingZscale = false;
264264

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const SDR_RGB_TO_BT709_FILTER = "scale=out_color_matrix=bt709:out_range=tv:flags=neighbor";
22

3-
export const SDR_CAPTURE_TO_BT709_FILTER = `scale=in_color_matrix=bt601:in_range=pc:flags=neighbor,format=gbrp,${SDR_RGB_TO_BT709_FILTER}`;
3+
export const SDR_CAPTURE_TO_BT709_FILTER = `scale=in_color_matrix=bt601:in_range=pc:flags=neighbor+accurate_rnd,format=gbrp,${SDR_RGB_TO_BT709_FILTER}`;
44

55
export const SDR_RGB_TO_TAGGED_BT709_FILTER = `${SDR_RGB_TO_BT709_FILTER},setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=tv`;

0 commit comments

Comments
 (0)