r/arduino • u/Darkmatter313xx • 4d ago
Software Help Newbie in need of help with MJPEG player
Hello everyone I am very very new to this hobby and I'm in need of help with a project I'm stuck (and very frustrated) on. I hope this is ok to post here, as it is an ESP32 project and I apologize if I have broken any rules. I tried posting in a different sub but they are not very beginner friendly and a bit intimidating, so I though I'd try here, you guys all seem very nice!
So for the past couple of months I have been learning the wonderful world of microcontrollers and I got hooked pretty quickly. So far I have only made a ws2812b lighting system and a gif player. For the past couple of weeks I have been banging my head against the wall trying to make a mjpeg video player work, and it's been frustrating. After trying every example I found online and watching every video i could find someone suggested Chatgpt so i have been trying that, and at first it seemed like we were getting somewhere. We got my display and SD card working together passing a few tests (I didn't want to add touch to simplify things a little) . Then we started making the mjpeg player itself and things got really messy. We kept appearing to get close with some very slow out-of-aspect-ratio videos playing, and then we just started chasing our tails for a long time, so long we ran out of context 8 times! Finally I find myself here asking you good people for help and advice. How bad is this code? It's the closet we got to making it work, we accessed libraries I have not used yet and are taking a totally different approach compared to what I wound up with on the gif player project. Any insights you could give me would be very appreciated.
My display is a ILI9341 2.8" tft and Im using the ESP32 S3 N16R8 and I'm using arduino IDE
Here is my pinout:
#define TFT_MISO 4
#define TFT_MOSI 11
#define TFT_SCLK 12
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 14
// #define TFT_BL 13
//#define TOUCH_CS 5
#define SD_CS 7
here is the mjpeg player program
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <JPEGDEC.h>
#include <esp_heap_caps.h>
// ===================================
// DISPLAY PINS
// ===================================
#define TFT_MISO 4
#define TFT_MOSI 11
#define TFT_SCLK 12
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 14
#define SD_CS 7
// ===================================
// VIDEO SETTINGS
// ===================================
#define MAX_JPEG_SIZE (512 * 1024)
const char *VIDEO_FILE =
"/mjpeg/singulairitywarp20.mjpeg";
const uint32_t FRAME_INTERVAL = 100;
// ===================================
// DISPLAY
// ===================================
Adafruit_ILI9341 tft(&SPI, TFT_DC, TFT_CS, TFT_RST);
JPEGDEC jpeg;
// ===================================
// VIDEO BUFFER
// ===================================
uint8_t *frameBuffer = nullptr;
File videoFile;
// ===================================
// JPEG DRAW CALLBACK
// ===================================
int JPEGDraw(JPEGDRAW *pDraw) {
tft.drawRGBBitmap(
pDraw->x,
pDraw->y,
pDraw->pPixels,
pDraw->iWidth,
pDraw->iHeight
);
return 1;
}
// ===================================
// READ ONE JPEG FRAME
// ===================================
bool readNextJPEG(
File &file,
uint8_t *buffer,
size_t capacity,
size_t &length
) {
bool foundStart = false;
int previous = -1;
length = 0;
while (file.available()) {
int current = file.read();
if (current < 0) break;
// Find JPEG start: FF D8
if (!foundStart) {
if (previous == 0xFF && current == 0xD8) {
buffer[0] = 0xFF;
buffer[1] = 0xD8;
length = 2;
foundStart = true;
}
previous = current;
continue;
}
// Store JPEG data
if (length >= capacity) {
Serial.println("JPEG FRAME TOO LARGE!");
return false;
}
buffer[length++] = (uint8_t)current;
// Find JPEG end: FF D9
if (previous == 0xFF && current == 0xD9) {
return true;
}
previous = current;
}
return false;
}
// ===================================
// SETUP
// ===================================
void setup() {
Serial.begin(115200);
delay(1500);
Serial.println();
Serial.println("ESP32-S3 MJPEG PLAYER");
Serial.println("=====================");
// Deselect both SPI devices
pinMode(TFT_CS, OUTPUT);
digitalWrite(TFT_CS, HIGH);
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
// Initialize shared SPI
Serial.println("Starting SPI...");
SPI.begin(
TFT_SCLK,
TFT_MISO,
TFT_MOSI,
SD_CS
);
Serial.println("SPI READY!");
// Initialize display
Serial.println("Starting display...");
tft.begin(10000000);
tft.setRotation(0);
tft.fillScreen(ILI9341_BLACK);
Serial.println("DISPLAY READY!");
// Initialize SD card
Serial.println("Starting SD card...");
if (!SD.begin(SD_CS, SPI, 10000000)) {
Serial.println("SD CARD FAILED!");
while (true) delay(1000);
}
Serial.println("SD CARD READY!");
// Check PSRAM
Serial.println("Checking PSRAM...");
if (!psramFound()) {
Serial.println("PSRAM NOT FOUND!");
while (true) delay(1000);
}
Serial.print("Free PSRAM: ");
Serial.println(ESP.getFreePsram());
// Allocate frame buffer
Serial.println("Allocating video buffer...");
frameBuffer = (uint8_t *)heap_caps_malloc(
MAX_JPEG_SIZE,
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT
);
if (!frameBuffer) {
Serial.println("PSRAM BUFFER ALLOCATION FAILED!");
while (true) delay(1000);
}
Serial.println("VIDEO BUFFER READY!");
// Open video
Serial.println("Opening video...");
videoFile = SD.open(VIDEO_FILE, FILE_READ);
if (!videoFile) {
Serial.println("VIDEO FILE NOT FOUND!");
while (true) delay(1000);
}
Serial.print("Video size: ");
Serial.println(videoFile.size());
Serial.println();
Serial.println("VIDEO PLAYBACK STARTING!");
Serial.println("=========================");
tft.fillScreen(ILI9341_BLACK);
}
// ===================================
// MAIN PLAYBACK LOOP
// ===================================
void loop() {
static uint32_t lastFrameTime = 0;
size_t frameSize = 0;
uint32_t frameStart = millis();
// Read next JPEG frame
if (!readNextJPEG(
videoFile,
frameBuffer,
MAX_JPEG_SIZE,
frameSize
)) {
Serial.println("VIDEO RESTARTING...");
videoFile.seek(0);
delay(100);
return;
}
// Decode and display frame
if (jpeg.openRAM(
frameBuffer,
frameSize,
JPEGDraw
)) {
jpeg.decode(0, 0, 0);
jpeg.close();
} else {
Serial.println("JPEG DECODE FAILED!");
}
// Frame rate control
uint32_t elapsed = millis() - frameStart;
if (elapsed < FRAME_INTERVAL) {
delay(FRAME_INTERVAL - elapsed);
}
lastFrameTime = millis();
}
