Anti-flicker

Some Spectrum programs flicker on purpose, and a modern screen shows that flicker in a way a television never did. Anti-flicker mixes the frames back into the picture they were meant to make. This page explains what the flicker is, how the mixing decides what to mix, and when it is better to leave it off. The settings are in the Anti-flicker group of the Video page.

Why Spectrum pictures flicker

The Spectrum has 15 colors, and only two of them in each 8 by 8 cell. Programs get past that by showing a different picture in every frame:

  • GigaScreen - two pictures shown in turn. A pixel that is black in one and red in the other looks dark red, so a cell can show far more than two colors.

  • 3-color pictures - three pictures shown in turn: one red, one green and one blue. Every pixel gets its own color, and each part of that color is shown every third frame.

  • Games - if there are more sprites than there is time to draw, they are drawn every other frame. A sprite can also blink to look see-through.

A television at 50 Hz blended the frames: its picture faded slowly, and the eye mixed the rest. A modern screen shows each frame sharp and whole, so you see the pictures one after another, flickering. On a 60 Hz screen they do not even take turns evenly.

Mixing frames

Anti-flicker shows each pixel as a mix of its color in this frame and in the frames before. Intensity sets how much of the earlier frames goes into the mix. At 100%, two frames are mixed half and half.

The colors are mixed as light, not as numbers. A screen does not give light in proportion to its numbers: a pixel at half the number is much darker than half as bright. So each color is first turned into light, then mixed, and then turned back, the way a television’s tube mixed light. Gamma sets how numbers turn into light. 2.2 is right for most screens. If flickering colors are mixed as plain numbers, they come out too dark.

Below is the GigaScreen palette of the ZX EMU Flickering Test, one frame as it is and with anti-flicker at 100%. In the gamma test on the right, the inner rectangle matches its frame if Gamma suits the screen:

One frame

Anti-flicker

GigaScreen palette, one frame

GigaScreen palette, mixed

The two “fullscreen” modes mix every pixel that changed, all over the picture. A still picture comes out right. But anything that moves leaves a half-bright trail, because each frame is mixed with where the thing was a frame ago. Below, a block moves 8 pixels a frame: 2-frames (fullscreen) turns it into a gray smear twice as wide, while the adaptive modes leave it as it is.

2-frames (fullscreen)

2-/3-frames (adaptive)

A moving block, fullscreen mode

A moving block, adaptive mode

The adaptive modes

The adaptive modes mix only what flickers on purpose. A pixel that goes A, B, A, B… flickers on purpose. A pixel of a moving picture can also go A, B, A by chance, but not for long. So a pixel is mixed only after its two colors have taken turns for about six frames. Plain motion stays sharp, even a 1-pixel scroll at 50 frames a second.

When a GigaScreen picture scrolls, both of its pictures move, and no pixel takes turns in its place any more. So Xpeccy+ cuts the picture into bands of 16 rows. For each band it finds how far the band moved in two frames - from one picture to the same picture again. It then mixes the frame with the frame before, moved half that far, which puts the other picture where this one now stands. Some bands are left alone:

  • a band whose half move would be an odd number of rows. A picture made of alternate lines would then be mixed with its own other lines.

  • a band that stood still between two of the three frames. It scrolls slower than the frame rate, and pairing it along its move would mix every pixel with its neighbor.

2-/3-frames (adaptive) also looks for 3-color pictures: pixels that are pure red, green or blue and have repeated every third frame for a few frames. It mixes those three frames at a time. Anything else is treated as two-frame flicker or left alone.

One frame

Anti-flicker

A 3-color picture, one frame

A 3-color picture, mixed

Look ahead shortens the start. A flicker that has just begun takes six frames to be recognized. With look ahead, the next two frames are run in the background, the way Run ahead runs them, so the pattern is seen on both sides of the frame shown. The picture is not held back for this, so look ahead adds no input lag. It is not used on the ZX Spectrum Next, where running frames twice costs too much.

The history starts again after a gap - Fast Mode, a rewind, anti-flicker turned off - or after a change of the picture’s size. The first frame after that is shown as it is.

When to leave it off

Anti-flicker is a guess about what a picture was meant to be, and some programs look better without it:

  • fast scrollers and single-pixel motion, in the fullscreen modes. They turn soft and leave trails. The adaptive modes leave most of them sharp.

  • games that flicker to show more sprites. When mixed, the sprites become half-bright and see-through, which is not how they looked on a television either.

  • scrolling GigaScreen pictures. Sharp color changes can still leave a comb pattern at their edges, and a moving object drawn on both pictures can show twice.

The Anti-flicker action switches the intensity between 50%, 100% and off with one key, so you can turn it off just for the part of a program that needs it.

Where it comes from

Work on anti-flicker began with the author’s own GigaScreen game, Project AZX, as pull requests to the original Xpeccy before Xpeccy+ began. It was then refined here, and also released as a render plugin for Spectaculator. It aims at colors close to a CRT’s, and at as few wrong mixes as possible in the special cases. It is not perfect. Similar work goes on in other projects.