Files
feuerwerk/tests/core/avc-colour.test.ts
adminandClaude Opus 5.5 30b618d9a9 Show game view recordings in their true colours in other players
A game view recording looked darker and its colours off in VLC, and
each round of recording a recording made it worse. Chrome's encoder,
fed RGB frames, wrote full-range H.264 and left the colour out of the
stream. Only our Matroska head said "full range", and VLC reads only
the stream. Chrome itself plays the converted MP4, which was right.

- The GPU now turns the game view into I420, limited range BT.709,
  before it is read back (PictureReader). The encoder gets the planes
  with that colour, as it does for a recording of the whole editor.
- Where the planes lie in the read-back picture is a plain function,
  i420Layout in recording.ts.
- Only 1.5 instead of 4 bytes a pixel come back from the GPU.
- avcColour.ts writes the colour into the SPS of the encoder's avcC as
  well, so a player that reads only the stream knows it. An SPS with a
  VUI only gets its colour block replaced. One without gets a VUI that
  holds the colour. An SPS it cannot read stays as the encoder made it.
- A first try sent the frames through a 2D canvas. That gave limited
  range too, but only 18 to 20 of 33 frames at 1080p30.

Found on the way:

- With the colour in the stream alone, VLC 3.0.8 still showed the
  full-range picture too dark. It ignores the range.
- The one old full-range original on the server got the same SPS fix
  without loss: the clusters are byte-equal, the decoded frames are
  equal (framemd5), backup in /root/config-backup-20261013/videos/.
- Older game view recordings stay full range. Fixing that in VLC would
  mean encoding them again.

Tests:
- tsc, eslint, Prettier clean.
- Gate on melektaus-atx: CHECK EXIT 0, 308 files, 4772 tests.
- avc-colour.test.ts new (9), expectations read back with ffmpeg
  trace_headers. Counter-check: the old bug in the no-VUI path turns
  it red.
- e2e 67: ALL PASSED, 12 checks. 67d-ii reads tv/bt709 from the file
  and from the bare H.264 stream, red with the old code.
- 1080p30 34 frames, 640x360@60 68 frames, none dropped (was 33/47).
- Checked by the user: a new recording looks right in VLC 3.0.8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 19:48:44 +02:00

87 lines
4.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import {
avcConfigSps,
avcConfigWithColour,
spsColour,
spsWithColour,
} from '../../src/core/recording/avcColour';
import type { MatroskaColour } from '../../src/core/recording/matroska';
const hex = (text: string) => Uint8Array.from(text.match(/../g)!.map((pair) => parseInt(pair, 16)));
const toHex = (bytes: Uint8Array) => [...bytes].map((value) => value.toString(16).padStart(2, '0')).join('');
/** Chrome 136 on Windows, a game view recording (RGBX frames): a VUI, but no `video_signal_type`. */
const GAME_VIEW = hex(
'0164002affe100196764002aac2b403c0113f2e022000007d00003a9801e38554001000468ee3cb0fdf8f800',
);
/** The same file after the rewrite, as ffmpeg's `trace_headers` read it: full range, BT.709, sRGB. */
const GAME_VIEW_FIXED = hex(
'0164002affe1001c6764002aac2b403c0113f2e02dc043405000003e80001d4c00f1c2aa01000468ee3cb0fdf8f800',
);
/** Chrome 136 on Windows, a recording of the editor (canvas frames): the colour already in the SPS. */
const EDITOR = hex('01640c33ffe1001367640c33ac18d0050016b4d40434041e1108d401000468ce3c80fdf8f800');
/** A baseline SPS without a VUI: 320×240, written by hand. */
const BASELINE = hex('6742c01eda0507e4');
const FULL_SRGB: MatroskaColour = { matrix: 1, transfer: 13, primaries: 1, fullRange: true };
const TV_SRGB: MatroskaColour = { matrix: 1, transfer: 13, primaries: 1, fullRange: false };
describe('avcConfigWithColour', () => {
it('writes the colour into an SPS that lacks it, as ffmpeg reads it back', () => {
expect(spsColour(avcConfigSps(GAME_VIEW)[0]!)).toBeNull();
const fixed = avcConfigWithColour(GAME_VIEW, FULL_SRGB);
expect(toHex(fixed)).toBe(toHex(GAME_VIEW_FIXED));
expect(spsColour(avcConfigSps(fixed)[0]!)).toEqual(FULL_SRGB);
});
it('keeps everything around the SPS byte for byte', () => {
const fixed = avcConfigWithColour(GAME_VIEW, FULL_SRGB);
expect(toHex(fixed.subarray(0, 6))).toBe(toHex(GAME_VIEW.subarray(0, 6)));
const pps = (config: Uint8Array) => config.subarray(6 + 2 + avcConfigSps(config)[0]!.length);
expect(toHex(pps(fixed))).toBe(toHex(pps(GAME_VIEW)));
});
it('changes nothing where the SPS already says the same colour', () => {
expect(spsColour(avcConfigSps(EDITOR)[0]!)).toEqual(TV_SRGB);
expect(toHex(avcConfigWithColour(EDITOR, TV_SRGB))).toBe(toHex(EDITOR));
});
it('replaces a colour the SPS says with the one asked for', () => {
const changed = avcConfigWithColour(EDITOR, FULL_SRGB);
expect(spsColour(avcConfigSps(changed)[0]!)).toEqual(FULL_SRGB);
expect(toHex(avcConfigWithColour(changed, TV_SRGB))).toBe(toHex(EDITOR));
});
it('is the same once written twice', () => {
const once = avcConfigWithColour(GAME_VIEW, FULL_SRGB);
expect(toHex(avcConfigWithColour(once, FULL_SRGB))).toBe(toHex(once));
});
it('refuses what is no avcC', () => {
expect(() => avcConfigWithColour(hex('00000001'), FULL_SRGB)).toThrow();
});
});
describe('spsWithColour', () => {
it('gives an SPS without a VUI one that says the colour', () => {
expect(spsColour(BASELINE)).toBeNull();
const fixed = spsWithColour(BASELINE, FULL_SRGB);
// As ffmpeg's `trace_headers` reads it: 320×240 still, then the VUI with the colour alone.
expect(toHex(fixed)).toBe('6742c01eda0507e9b808680810');
expect(spsColour(fixed)).toEqual(FULL_SRGB);
});
it('escapes what would read as a start code, and reads it back', () => {
// Zeros in all three numbers put 00 00 00 into this SPS, which has to go out as 00 00 03 00.
const zero: MatroskaColour = { matrix: 0, transfer: 0, primaries: 0, fullRange: false };
const escaped = avcConfigWithColour(EDITOR, zero);
expect(toHex(avcConfigSps(escaped)[0]!)).toBe('67640c33ac18d0050016b4d4000003001e1108d4');
expect(spsColour(avcConfigSps(escaped)[0]!)).toEqual(zero);
expect(toHex(avcConfigWithColour(escaped, TV_SRGB))).toBe(toHex(EDITOR));
});
it('refuses a NAL unit that is no SPS', () => {
expect(() => spsWithColour(hex('68ee3cb0'), FULL_SRGB)).toThrow();
});
});