AVI recording

I’ve now done the bulk of the work needed for SimCoupe’s AVI recording feature, using 8-bit MS RLE encoding as planned. It gives lossless video and (optionally) audio recording at up to full framerate, and should be no problem for most systems to handle in parallel to the emulation. The encoder was written from scratch, meaning it’s available on all platforms without additional library dependencies, as well as being tuned for emulator use.

Video is recorded in either half size or full size modes, with the latter needed to preserve mode 3 hi-res pixels. Half size mode defaults to storing the odd pixels for the best quality in SAM BASIC, but even pixels can optionally be used instead. Full size mode also respects the scanline option and intensity, so it should look the same as when using the emulator. Only the 5:4 mode is ignored, as AVI doesn’t support aspect ratios, and proper smooth scaling of the image can’t be done due to the limited 8-bit video format.

Audio is stored in the same 44.1KHz 16-bit PCM format as the separate sound recording feature. Unfortunately there aren’t any widely supported lossless codecs I can rely on, so the sound data remains uncompressed. This requires 3528 bytes per frame, adding a steady 172K/s to the recording. Of course, post-processing the audio stream into MP3 format reduced the file size without impacting the video quality.

The recording framerate uses a simple 1:n method to decide which frames to store, giving framerates of 50fps, 25fps, 16.67fps, … down to 1fps. Key frames are stored every 5 seconds to give faster seeking during video playback, and allow catch-up if the decoder is struggling to keep up (possible with full-size 50fps videos on slower systems).

As with GIF, only the differences are stored between most frames. MS RLE includes a “00 02 xx yy” GOTO sequence to skip up to 255 pixels on both horizontal and vertical directions, to position at the next change point. Identical frames are stored as a 2-byte 00 01 sequence, adding 10 bytes to the recording including chunk headers. Not as efficient as GIF but much better than most traditional video codecs.

Here’s a 2.4MB sample video of the first level of Manic Miner. It was recorded in full resolution (576×480) at 25fps, with sound post-processed into 192Kbps MP3 using VirtualDub: