Close

Video Playback

A project log for AVR-Laptop

Pushing an 8-bit MCU far beyond what it was designed for

vladimir-tsibrovVladimir Tsibrov • 09/29/2026 at 20:27•0 Comments

Initially, I didn't set out to play video. The interest came when it became clear that the display could be redrawn fast enough. At first, outputting a 128×64 XBM image from flash took about 40 ms; after optimizing the data transfer and reducing delays, it came down to around 32 ms per frame. That was already enough to try video.

I wrote a converter in Python: it extracted frames from a video file, converted them to monochrome 128×64 using simple brightness thresholding, and saved the result to a file. On the device, all that remained was to open this file from the microSD, read it in 1024-byte blocks into the video buffer, and send them to the display. A separate challenge was hitting the source video's fps exactly. The difficulty wasn't that the AVR couldn't keep up, but that frames had to be swapped at the right moment.

The first playback attempts weren't perfect (brightness thresholding is just terrible), but they showed that the microcontroller and display handle 24 fps easily — a typical frame rate for most video. Then came improvements to the converter: besides threshold binarization, I added other methods — Otsu, Sauvola — which gave better results. The next step was an attempt to convey halftones by blinking pixels between frames: a pixel isn't lit in every frame, and over several frames the brightness averages out to the desired shade. I later learned that this method has existed for a long time and is called temporal dithering. With it, I managed to squeeze the most out of the monochrome 128×64: the picture got much better, especially in close-ups.

Once the video pipeline was done, I started looking into audio playback. The VS1053 audio decoder became part of the device at this stage. I placed the speakers in the display module's enclosure without changing the device's dimensions. I was already familiar with the VS1053, but this decoder has a quirk: it needs to be constantly fed audio data, or the sound simply cuts out. Meanwhile, my repaint function that output a frame to the display was a loop over the video buffer and blocked the CPU for about 32 ms. Most of that time, the processor was just waiting for the display to become ready before sending the next byte.

According to the ST7920 datasheet, almost all controller instructions take 72 µs to execute — though in reality it's noticeably less, otherwise sending a single frame would take 1024 × 72 = over 73 ms, not counting address setup commands.

I ran a small test: I added code to the display send function that measured how long the display stays busy, and got values from 8 to 40 µs. Setting the control signals and outputting a data byte to the port takes just a few hundred nanoseconds, meaning the waiting eats up more than 99% of CPU time.

The VS1053 has a data request pin that could be used to feed the decoder from an interrupt. But reading the SD card is an operation orders of magnitude longer than servicing audio, and inside an ISR it would break the timing and hit fps.

The obvious move was to shift display data transfer into a timer interrupt. Display readiness time fluctuates from 8 to 40 µs, so I set the timer interval to the lower bound, and moved the readiness check inside the handler, before sending the next byte.

Along the way, I dropped the explicit repaint call in favor of a dirty-line mechanism: graphics functions mark changed lines, the send routine clears those marks, and service() picks the next dirty line and starts its transfer.

The solution has two parts:

After the line is sent, the ISR stops the timer itself. So the ISR is a pump that moves the bytes of a given line into the display, while service() decides which line to send and when.

Separately, let me explain why all the code operates on lines. Before sending a data byte, you need to set the address of the row and column where it will be written. After that, the display increments the column address automatically, so the rest of the line's bytes can be sent in sequence without resetting the address for each byte.

And since we're on the topic of the controller's internals, one more detail is worth mentioning. A single byte encodes 8 horizontal pixels, so a 128×64 frame is 16×64 bytes. But the ST7920's video memory is organized as 32×32, that is 256 dots across 32 rows. Hence the peculiarity: the first 16 bytes of a video memory row correspond to physical row X, and the next 16 — to row X+32. One video memory row covers two display rows at once: 0 and 32, 1 and 33, and so on.

So I got rid of the blocking display send loop, freeing up a noticeable amount of CPU time. Frame transfer now ran asynchronously, and the microcontroller could regularly check the VS1053's DREQ flag. When needed, it read audio data from the card, filled the audio buffer, and sent a chunk of data (32 bytes) to the decoder. That's how video and audio started working at the same time: video went to the display via the ISR, and the microcontroller fed the VS1053 in between. The most interesting part — I didn't have to synchronize video and audio. Frame output had been timed precisely from the start, so the video never ran ahead or fell behind, and the audio played at its own pace. Later I added seeking: knowing the audio data format and the frame size, you can skip the right amount of data in the file and land at the right moment in time.

That's how my device learned to play video with sound.

Discussions