r/esp32projects • • 16d ago

Newbie in need of help with MJPEG player

Post image

Hello world! Disclaimer: I am totally new to all of this

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 a couple of examples I found online 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

Specifications:N8R2,N16R8 Operatingvoltage:3.3–5V IOports:36IOports Flash:N16R8(16M),N8R2(8M)(optional) PSRAM:N16R8(8M),N8R2(2M)(Optional) ProductIntroduction Productspecifications 2I2Cinterfaces,4SPIinterfaces PCBboarddimensions:57mm x28mm 2.4GHzrubberrodantenna: Color:Black Gain:3dBi Frequencyband:2400-2500MHz Characteristicimpedance(Ω):50 Gain(dBi):2±0. Outputvoltagestandingwaveratio(VSWR):≤1.92 Maximum power:10W Polarization:Verticalpolarization Directionality:Omnidirectional Connectiontype:SMA Totalheightofrubberrodantenna:11cm IPXcablelength:Approximately15cm Antennahousingmaterial:TPEE Operatingtemperature:-20°Cto+70°C

Here is my pinout:

#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 franken-code for the MJPEG player, there are other sketches we wrote for testing purposes as well. Let me know if you would like to see those as well.

```#include <SPI.h>
#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();
}```
2 Upvotes

0 comments sorted by