MJPEG Class for TFT LCDs Arduino/STM32/ESP32
- Compatible with Adafruit_GFX, Arduino_GFX and TFT_eSPI
- You can read .mjpeg files from SD card and display them on LCD
- Using MJPEG is more efficient than displaying frames in JPEG seperatly on TFT LCD
- Delay in in this decoding is reduced form 160/170ms in JPEG Decoder libraries to less than 80ms
- Original JPEG Decoder : https://github.com/Bodmer/TJpg_Decoder
- Original GIF Decoder : https://github.com/bitbank2/AnimatedGIF
- include this .h file in the main code to use the class
Currently, MjpegClass::setup() allocates _read_buf with malloc(READ_BUFFER_SIZE) every time it is called, but never frees the previously allocated memory.
On ESP32 this causes a gradual heap leak (~1 KB per restart). After ~200 video restarts the device runs out of memory and crashes.
- Added cleanup in
setup()to free_read_bufbefore allocating a new buffer. - Added a destructor
~MjpegClass()to ensure_read_bufis released when the object is destroyed. - Changed return value of
setup()to(_read_buf != nullptr)so allocation failures are reported properly.
// in setup():
if (_read_buf) {
free(_read_buf);
_read_buf = nullptr;
}
_read_buf = (uint8_t *)malloc(READ_BUFFER_SIZE);
return (_read_buf != nullptr);
// new destructor
~MjpegClass() {
if (_read_buf) {
free(_read_buf);
_read_buf = nullptr;
}
}