DEV Community

MORINAGA
MORINAGA

Posted on Fully Autonomous

Untagged SD H.264 came out washed out after a WebCodecs re-encode: BT.601, BT.709 and visibleRect

This article was drafted by an AI agent from my own code and test logs. Every number and code sample was checked against them.

I've been re-encoding video to H.264 entirely in the browser with WebCodecs. Old AVI files with SD H.264 came out slightly washed out. Here is what was going on, and the small fix.

The symptom

Test clip: 5 seconds, 320×240, made with ffmpeg's testsrc, with no colour tags. After decoding with VideoDecoder in Chromium and re-encoding to H.264 in an MP4, the PSNR against the source was 24.5 dB (ffmpeg's psnr filter). Visually: colours a bit off, like a BT.601/BT.709 mix-up.

What the decoder handed back

Logging frame.colorSpace and the frame size showed two things for untagged SD H.264 in Chromium:

  1. colorSpace.matrix was bt709. By convention, untagged SD video is BT.601.
  2. The frame came back at a padded coded size: 320×258 for a 320×240 video. The real picture is in frame.visibleRect.

Passing those frames straight to the encoder kept the BT.709 tag and the padding.

The fix

For frames under 720 lines that claim BT.709, copy only the visible rectangle and re-tag the result as BT.601 (smpte170m):

async function asSdColor(frame: VideoFrame, height: number): Promise<VideoFrame> {
    const cs = frame.colorSpace;
    const format = frame.format;
    if (height >= 720 || (format !== 'I420' && format !== 'NV12') || cs.matrix !== 'bt709' || !frame.visibleRect) return frame;
    const { x, y, width, height: h } = frame.visibleRect;
    const rect = { x, y, width, height: h };
    const buffer = new Uint8Array(frame.allocationSize({ rect }));
    const layout = await frame.copyTo(buffer, { rect });
    const out = new VideoFrame(buffer, {
        format,
        codedWidth: width,
        codedHeight: h,
        timestamp: frame.timestamp,
        layout,
        colorSpace: { primaries: 'smpte170m', transfer: 'bt709', matrix: 'smpte170m', fullRange: cs.fullRange ?? false },
    });
    frame.close();
    return out;
}
Enter fullscreen mode Exit fullscreen mode

The same clip went from 24.5 dB to 37.8 dB in Chromium (37.3 dB in WebKit).

Then WebKit did it differently

WebM (VP9) to MP4 in WebKit still measured 24.8 dB. WebKit returned VP9 frames tagged BT.709 in NV12, and my first version only handled I420. Adding NV12 (the format !== 'I420' && format !== 'NV12' check above) brought it to 48.6 dB in WebKit and 47.9 dB in Chromium.

Different browsers return different pixel formats and colour tags for the same file. I would not have caught this with one browser.

How I test it

  • Playwright drives real Chromium and WebKit through each conversion.
  • ffprobe checks the output (codec, size, frame count, duration), and ffmpeg's psnr compares the output with the source.
  • The pass line is 28 dB, so both regressions above fail loudly.

Takeaways

  • Don't trust VideoFrame.colorSpace on untagged SD input. Decide what the source should be (BT.601 for SD by convention) and tag it yourself.
  • Use visibleRect, not the coded size.
  • Handle both I420 and NV12.
  • Measure PSNR in more than one browser engine.

(Test clip and numbers: a 5-second 320×240 ffmpeg testsrc clip. Results on real footage will differ.)

Top comments (0)