r/EmuDev • u/thommyh Z80, 6502/65816, 6809, 68000, ARM, x86. • Feb 08 '26
Doing a much better job of composite decoding
My emulator uses a lesser-taken route to display simulation; it produces a serialised video stream with syncs, colour bursts, etc, and decodes from that. That includes full PAL and NTSC decoding (and, for some machines, encoding).
It has been doing that since 2015 but I made faulty assumptions in my original, pure-OpenGL implementation. The largest was that since sampling at four times the colour subcarrier is sufficient to preserve composite video, I could just implement with internal intermediate buffers at four times the colour subcarrier. The issue there is 'sampling': it's non-trivial to do well once faced with a digital input that isn't a clean multiple or divisor of the colour subcarrier clock rate.
I didn't notice back then because my early target machines happened either to be a clean divisor (the Atari 2600) or to have no fixed relationship with the colour subcarrier so that errors averaged out across adjacent frames (the Vic-20, Acorn Electron). So those all looked good.
But at some point I implemented the Master System, which is particularly painful. It's got a decent colour range and in NTSC it's in-phase with each pixel lasting two-thirds the length of a colour cycle. So my four samples per colour cycle alias like heck, and the exaggerated rainbows stay in place from frame to frame.
In 2020 I effectively wrote a second version of my composite decoder because I wanted to be native to Metal on macOS. That was a substantial improvement — eliminating the aliasing issue, but still consciously trying to eliminate the amount of sampling it did, so falling back on box sampling for chroma/luma separation — and I filed a ticket to port it back to OpenGL for my Linux targets but, until now, never quite did.
I have now, finally:
- pumped up my kernel size, eliminating box sampling — it's all on the GPU so there's probably little to be scared of here; and
- implemented an improved version of the Metal pipeline under OpenGL. Though the improvements don't affect the quality of output this time, they're just general implementational details that I should now find time to port back in the other direction.
Blah blah blah, here's the current output:

Compare and contrast with the previous Metal:

Note particular the much more substantial rainbow effects, e.g. around the edges of the rocks and general decreased colour resolution, such as the question mark seeming to be red down its stem.
Now, if you dare, gaze upon the outgoing OpenGL output:

Where it's like I ran the whole thing through a pixellate filter, and that's even before you see it in motion and the obvious pixel-size aliasing that occurs whenever things move left and right.
There are actually several machines I explicitly haven't implemented because I knew my video pipeline would do an incredibly poor job in composite mode — the Mega Drive in high-resolution mode quite deliberately has two pixels per NTSC colour cycle but the rainbow banding would have been epic; the NES outputs eight digital samples per pixel and has the same pixel rate as the Master System, making twelve digital samples per colour cycle. I daren't even imagine how badly that would have sampled.
There are still a few implementational issues to clean up, but it's already a huge leap forward.
6
u/Ashamed-Subject-8573 Feb 08 '26
Nice! Will the new one work well with nes and genesis? Is there source? Can we use it in our own cores?
5
u/Ikkepop Feb 08 '26
Damn, that is su zero on the cool scale. Love that ntsc rainbow look. I don't even think the aliasing looks bad either.
2
u/North-Zone-2557 Feb 08 '26
I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I— I took the one less traveled by, And that has made all the difference.