Emulating NTSC video

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Emulating NTSC video

Post by zeroone »

For the C++ on the Wiki, what is that 9-bit pixel color? Is there a way to convert the 6-bit PPU palette index into that?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Emulating NTSC video

Post by lidnariq »

It's the 6-bit raw color number (i.e. the output of the palette) appended to the preemphasis bits.
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Emulating NTSC video

Post by zeroone »

Thanks. I plugged in that code without any optimizations to see what it looks like. He's a screenshot (3x magnification):

Image

That looks pretty horrible to me. And, I'm pretty sure that effect can be simulated by applying a filter that emphasizes R, then G, then B cyclically over every 3 pixels.

Anyway, the NTSC filters in other emulators don't look that bad. What is missing here?
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Emulating NTSC video

Post by rainwarrior »

zeroone wrote:Anyway, the NTSC filters in other emulators don't look that bad. What is missing here?
I think that looks more or less correct, but you've rendered to low resolution and then upscaled.

If you want better looking quality, you should render the NTSC output directly to a higher resolution output. Vertical can just double or triple lines (darkening some if you like "scanline" simulation), but horizontal should be accounting for the NTSC signal across the whole line (possibly while stretching to a different aspect ratio).

e.g. Blargg's NTSC filter demo upscales 256 x 240 to 602 x 480.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Emulating NTSC video

Post by rainwarrior »

For a quick comparison, Blargg's filter demo output compared to decimating it to half the resolution:
smb3_blargg.png
smb3_blargg_decimated.png
Note the better quality in the first image because it is able to subsample the analog NTSC signal during horizontal upscaling.
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Emulating NTSC video

Post by zeroone »

It looks like every other line is slightly darker. I'll try to figure out how that code works.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Emulating NTSC video

Post by tepples »

You're seeing a combination of three differences:
  • As rainwarrior mentioned, TVs and emulators run the NTSC decoder at full output resolution, as opposed to one sample per NES pixel.
  • The PPU alternates the color subcarrier's phase by one-third of a cycle between one frame and the next. When played at 60 fps, frames with different phases blend together in a way that a single-field still screenshot doesn't capture.
  • The last thing you noticed is "scanlines", an interpolation technique that makes every other line darker to simulate the beam shape on a CRT that's focused for 480i but displaying 240p. But this is independent of the NTSC encoding and decoding; PlayChoice-style emulation with scanlines but without an NTSC filter is still valid.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Emulating NTSC video

Post by rainwarrior »

zeroone wrote:It looks like every other line is slightly darker. I'll try to figure out how that code works.
That part isn't important, simulating scanlines is merely a stylistic choice / personal preference. It's a vague approximation of the gaps between lines in 240p on a CRT.

The vertical stretching of the image is simply done by doubling every line (no interpolation or oversampling, etc.). The scanline effect is literally just darkening every second line. I would have removed it for clarity if the filter demo had an option to do so, since I was only trying to demonstrate how the horizontal scaling is different.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Emulating NTSC video

Post by rainwarrior »

For a better comparison, here's the Blarrg filter example with the scanlines removed. (Compare this against the second "decimated" example above.)
smb3_blargg_scanlines_removed.png
Comparing the two you can see the decimated version has the same problem as your example, though maybe slightly milder (the problem is a little bit obscured by the aspect ratio change). The point is that quality improves with better sampling. You should see that the decimated version has more spurious colour fringes, etc. than the normal version, similar to your example.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Emulating NTSC video

Post by lidnariq »

Does following along with my manual demodulation explain things at all?
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Emulating NTSC video

Post by zeroone »

One of the functions on the wiki takes a Width parameter. Here's the result with a Width of 512:

Image

@rainwarrior It's a lot closer to the last Blarrg image that you posted, but Blarrg's still doesn't not contain that much color fringing.
lidnariq wrote:Does following along with my manual demodulation explain things at all?
That looks like a really interesting post. I'll study it. I'm clueless about how NTSC does it's thing. I just copied-and-pasted wiki code :)
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Emulating NTSC video

Post by zeroone »

Below is another with Width set to 768. It also smoothly stretches it to the TV pixel aspect ratio.

Image

I don't recall any CRT looking quite like these images.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Emulating NTSC video

Post by tokumaru »

The red-green-blue artifacts are way too pronounced, but the image looks fairly accurate otherwise, IMO.
User avatar
zeroone
Posts: 939
Joined: Mon Dec 29, 2014 1:46 pm
Location: New York, NY
Contact:

Re: Emulating NTSC video

Post by zeroone »

tokumaru wrote:The red-green-blue artifacts are way too pronounced, but the image looks fairly accurate otherwise, IMO.
I agree. But, this was generated with the code straight off the wiki. Any tuning suggestions?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Emulating NTSC video

Post by lidnariq »

The C++ on the wiki "should" generate a 2048x240 image.

Oh, I see, the "width" parameter imposes a simple decimation of the output image. At a width of 768, it ought to be ... moderately representative to the actual bandwidth of the system, I'd think...?
Post Reply