GIF Decoder Runs on Small Memory Devices

Larry Bank's AnimatedGIF library enables Cortex-M0, -M4 and -M7 Arduino boards to play back short GIFs.

CabeAtwell
about 5 years ago Displays

One of the greatest features of Arduino boards is the massive amount of resources available and the many great people contributing to the community. A more recent library that was introduced is the AnimatedGIF library from Larry Bank, which was specifically designed and optimized for low-memory, Arduino-compatible devices. The performance optimizations done to run GIFs smoothly on low-RAM MCUs included reading the image data, decoding the LZW codes into pixels, and displaying the pixels.

As Bank notes, reading GIF data from FLASH, RAM, or ROM did not present any challenges. However, when the data was read from an SD card, it produced a slow and unreliable interface. Thus, the first optimization included speeding up communication with external memory devices. This was accomplished by trying to limit the number of reads needed to collect the data. In addition, limiting the use of the seek function aided in speeding up the communication.

Decoding LZW codes into pixels present memory usage challenges. Basically, an array of symbols and pixels need to be kept to hold the decoded data. Bank used a combination of unsigned 16 bit and 8-bit arrays for the data. In addition, a couple of buffers are used, bringing the total memory needed to about 22.5K. To make the decoding as efficient as possible multiple chunks of data are read into a single variable to allow decoding of larger data sets without worrying about chunk boundaries.

Bank added a perf test example sketch that yielded the following results. (📷: Larry Bank)

As for displaying the pixels, a method was employed that enables each pixel to only require a single table lookup to be translated into the format needed by the LCD controller. Specifically, each pixel is 1 to 8 bits, and a 24-bit palette is used to translate them into RGB565 values. Since every pixel needs to be translated through this process, efficiency and optimization are key to making the animation work smoothly.

For more detailed information, Bank has written a blog post overviewing the low-memory GIF decoder and uploaded the code to GitHub.

Latest Articles