initial commit

This commit is contained in:
lsdlsd88
2024-04-25 17:22:13 +02:00
parent 683b846d60
commit b5204b2a68
2737 changed files with 1534650 additions and 0 deletions
@@ -0,0 +1,246 @@
/*******************************************************************************
* Motion JPEG Image Viewer
* This is a simple Motion JPEG image viewer example
* Image Source: https://www.pexels.com/video/earth-rotating-video-856356/
* cropped: x: 598 y: 178 width: 720 height: 720 resized: 240x240
* ffmpeg -i "Pexels Videos 3931.mp4" -ss 0 -t 20.4s -vf "reverse,setpts=0.5*PTS,fps=10,vflip,hflip,rotate=90,crop=720:720:178:598,scale=240:240:flags=lanczos" -pix_fmt yuv420p -q:v 9 earth.mjpeg
*
* Dependent libraries:
* JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
*
* Setup steps:
* 1. Change your LCD parameters in Arduino_GFX setting
* 2. Upload Motion JPEG file
* FFat (ESP32):
* upload FFat (FatFS) data with ESP32 Sketch Data Upload:
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
* LittleFS (ESP32 / ESP8266 / Pico):
* upload LittleFS data with ESP8266 LittleFS Data Upload:
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
* ESP8266: https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
* Pico: https://github.com/earlephilhower/arduino-pico-littlefs-plugin.git
* SPIFFS (ESP32):
* upload SPIFFS data with ESP32 Sketch Data Upload:
* ESP32: https://github.com/lorol/arduino-esp32fs-plugin
* SD:
* Most Arduino system built-in support SD file system.
* Wio Terminal require extra dependant Libraries:
* - Seeed_Arduino_FS: https://github.com/Seeed-Studio/Seeed_Arduino_FS.git
* - Seeed_Arduino_SFUD: https://github.com/Seeed-Studio/Seeed_Arduino_SFUD.git
******************************************************************************/
#define MJPEG_FILENAME "/earth.mjpeg"
#define MJPEG_BUFFER_SIZE (240 * 240 * 2 / 10) // memory for a single JPEG frame
// #define MJPEG_FILENAME "/earth128.mjpeg"
// #define MJPEG_BUFFER_SIZE (128 * 128 * 2 / 10) // memory for a single JPEG frame
/*******************************************************************************
* Start of Arduino_GFX setting
*
* Arduino_GFX try to find the settings depends on selected board in Arduino IDE
* Or you can define the display dev kit not in the board list
* Defalult pin list for non display dev kit:
* Arduino Nano, Micro and more: CS: 9, DC: 8, RST: 7, BL: 6, SCK: 13, MOSI: 11, MISO: 12
* ESP32 various dev board : CS: 5, DC: 27, RST: 33, BL: 22, SCK: 18, MOSI: 23, MISO: nil
* ESP32-C3 various dev board : CS: 7, DC: 2, RST: 1, BL: 3, SCK: 4, MOSI: 6, MISO: nil
* ESP32-S2 various dev board : CS: 34, DC: 38, RST: 33, BL: 21, SCK: 36, MOSI: 35, MISO: nil
* ESP32-S3 various dev board : CS: 40, DC: 41, RST: 42, BL: 48, SCK: 36, MOSI: 35, MISO: nil
* ESP8266 various dev board : CS: 15, DC: 4, RST: 2, BL: 5, SCK: 14, MOSI: 13, MISO: 12
* Raspberry Pi Pico dev board : CS: 17, DC: 27, RST: 26, BL: 28, SCK: 18, MOSI: 19, MISO: 16
* RTL8720 BW16 old patch core : CS: 18, DC: 17, RST: 2, BL: 23, SCK: 19, MOSI: 21, MISO: 20
* RTL8720_BW16 Official core : CS: 9, DC: 8, RST: 6, BL: 3, SCK: 10, MOSI: 12, MISO: 11
* RTL8722 dev board : CS: 18, DC: 17, RST: 22, BL: 23, SCK: 13, MOSI: 11, MISO: 12
* RTL8722_mini dev board : CS: 12, DC: 14, RST: 15, BL: 13, SCK: 11, MOSI: 9, MISO: 10
* Seeeduino XIAO dev board : CS: 3, DC: 2, RST: 1, BL: 0, SCK: 8, MOSI: 10, MISO: 9
* Teensy 4.1 dev board : CS: 39, DC: 41, RST: 40, BL: 22, SCK: 13, MOSI: 11, MISO: 12
******************************************************************************/
#include <Arduino_GFX_Library.h>
#define GFX_BL DF_GFX_BL // default backlight pin, you may replace DF_GFX_BL to actual backlight pin
/* More dev device declaration: https://github.com/moononournation/Arduino_GFX/wiki/Dev-Device-Declaration */
#if defined(DISPLAY_DEV_KIT)
Arduino_GFX *gfx = create_default_Arduino_GFX();
#else /* !defined(DISPLAY_DEV_KIT) */
/* More data bus class: https://github.com/moononournation/Arduino_GFX/wiki/Data-Bus-Class */
Arduino_DataBus *bus = create_default_Arduino_DataBus();
/* More display class: https://github.com/moononournation/Arduino_GFX/wiki/Display-Class */
Arduino_GFX *gfx = new Arduino_ILI9341(bus, DF_GFX_RST, 3 /* rotation */, false /* IPS */);
#endif /* !defined(DISPLAY_DEV_KIT) */
/*******************************************************************************
* End of Arduino_GFX setting
******************************************************************************/
/* Wio Terminal */
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
#include <Seeed_FS.h>
#include <SD/Seeed_SD.h>
#elif defined(TARGET_RP2040)
#include <LittleFS.h>
#include <SD.h>
#elif defined(ESP32)
#include <FFat.h>
#include <LittleFS.h>
#include <SPIFFS.h>
#include <SD.h>
#include <SD_MMC.h>
#elif defined(ESP8266)
#include <LittleFS.h>
#include <SD.h>
#else
#include <SD.h>
#endif
#include "MjpegClass.h"
static MjpegClass mjpeg;
/* variables */
static int total_frames = 0;
static unsigned long total_read_video = 0;
static unsigned long total_decode_video = 0;
static unsigned long total_show_video = 0;
static unsigned long start_ms, curr_ms;
// pixel drawing callback
static int jpegDrawCallback(JPEGDRAW *pDraw)
{
// Serial.printf("Draw pos = %d,%d. size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
unsigned long start = millis();
gfx->draw16bitBeRGBBitmap(pDraw->x, pDraw->y, pDraw->pPixels, pDraw->iWidth, pDraw->iHeight);
total_show_video += millis() - start;
return 1;
}
void setup()
{
Serial.begin(115200);
// Serial.setDebugOutput(true);
// while(!Serial);
Serial.println("Arduino_GFX Motion JPEG Image Viewer example");
#ifdef GFX_EXTRA_PRE_INIT
GFX_EXTRA_PRE_INIT();
#endif
// Init Display
if (!gfx->begin())
{
Serial.println("gfx->begin() failed!");
}
gfx->fillScreen(BLACK);
#ifdef GFX_BL
pinMode(GFX_BL, OUTPUT);
digitalWrite(GFX_BL, HIGH);
#endif
/* Wio Terminal */
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
// Init SPIFLASH
if (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
#elif defined(TARGET_RP2040)
if (!LittleFS.begin())
// if (!SD.begin(SS))
#elif defined(ESP32)
// if (!FFat.begin())
if (!LittleFS.begin())
// if (!SPIFFS.begin())
// if (!SD.begin(SS))
// pinMode(10 /* CS */, OUTPUT);
// digitalWrite(10 /* CS */, HIGH);
// SD_MMC.setPins(12 /* CLK */, 11 /* CMD/MOSI */, 13 /* D0/MISO */);
// if (!SD_MMC.begin("/root", true))
#elif defined(ESP8266)
if (!LittleFS.begin())
// if (!SD.begin(SS))
#else
if (!SD.begin())
#endif
{
Serial.println(F("ERROR: File System Mount Failed!"));
gfx->println(F("ERROR: File System Mount Failed!"));
}
else
{
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
File mjpegFile = SD.open(MJPEG_FILENAME, "r");
#elif defined(TARGET_RP2040)
File mjpegFile = LittleFS.open(MJPEG_FILENAME, "r");
// File mjpegFile = SD.open(MJPEG_FILENAME, "r");
#elif defined(ESP32)
// File mjpegFile = FFat.open(MJPEG_FILENAME, "r");
File mjpegFile = LittleFS.open(MJPEG_FILENAME, "r");
// File mjpegFile = SPIFFS.open(MJPEG_FILENAME, "r");
// File mjpegFile = SD.open(MJPEG_FILENAME, "r");
// File mjpegFile = SD_MMC.open(MJPEG_FILENAME, "r");
#elif defined(ESP8266)
File mjpegFile = LittleFS.open(MJPEG_FILENAME, "r");
// File mjpegFile = SD.open(MJPEG_FILENAME, "r");
#else
File mjpegFile = SD.open(MJPEG_FILENAME, FILE_READ);
#endif
if (!mjpegFile || mjpegFile.isDirectory())
{
Serial.println(F("ERROR: Failed to open " MJPEG_FILENAME " file for reading"));
gfx->println(F("ERROR: Failed to open " MJPEG_FILENAME " file for reading"));
}
else
{
uint8_t *mjpeg_buf = (uint8_t *)malloc(MJPEG_BUFFER_SIZE);
if (!mjpeg_buf)
{
Serial.println(F("mjpeg_buf malloc failed!"));
}
else
{
Serial.println(F("MJPEG start"));
start_ms = millis();
curr_ms = millis();
mjpeg.setup(
&mjpegFile, mjpeg_buf, jpegDrawCallback, true /* useBigEndian */,
0 /* x */, 0 /* y */, gfx->width() /* widthLimit */, gfx->height() /* heightLimit */);
while (mjpegFile.available() && mjpeg.readMjpegBuf())
{
// Read video
total_read_video += millis() - curr_ms;
curr_ms = millis();
// Play video
mjpeg.drawJpg();
total_decode_video += millis() - curr_ms;
curr_ms = millis();
total_frames++;
}
int time_used = millis() - start_ms;
Serial.println(F("MJPEG end"));
mjpegFile.close();
float fps = 1000.0 * total_frames / time_used;
total_decode_video -= total_show_video;
Serial.printf("Total frames: %d\n", total_frames);
Serial.printf("Time used: %d ms\n", time_used);
Serial.printf("Average FPS: %0.1f\n", fps);
Serial.printf("Read MJPEG: %lu ms (%0.1f %%)\n", total_read_video, 100.0 * total_read_video / time_used);
Serial.printf("Decode video: %lu ms (%0.1f %%)\n", total_decode_video, 100.0 * total_decode_video / time_used);
Serial.printf("Show video: %lu ms (%0.1f %%)\n", total_show_video, 100.0 * total_show_video / time_used);
gfx->setCursor(0, 0);
gfx->printf("Total frames: %d\n", total_frames);
gfx->printf("Time used: %d ms\n", time_used);
gfx->printf("Average FPS: %0.1f\n", fps);
gfx->printf("Read MJPEG: %lu ms (%0.1f %%)\n", total_read_video, 100.0 * total_read_video / time_used);
gfx->printf("Decode video: %lu ms (%0.1f %%)\n", total_decode_video, 100.0 * total_decode_video / time_used);
gfx->printf("Show video: %lu ms (%0.1f %%)\n", total_show_video, 100.0 * total_show_video / time_used);
}
}
}
}
void loop()
{
}
@@ -0,0 +1,209 @@
/*******************************************************************************
* JPEGDEC Wrapper Class
*
* Dependent libraries:
* JPEGDEC: https://github.com/bitbank2/JPEGDEC.git
******************************************************************************/
#ifndef _MJPEGCLASS_H_
#define _MJPEGCLASS_H_
#define READ_BUFFER_SIZE 1024
#define MAXOUTPUTSIZE (MAX_BUFFERED_PIXELS / 16 / 16)
/* Wio Terminal */
#if defined(ARDUINO_ARCH_SAMD) && defined(SEEED_GROVE_UI_WIRELESS)
#include <Seeed_FS.h>
#elif defined(ESP32) || defined(ESP8266)
#include <FS.h>
#else
#include <SD.h>
#endif
#include <JPEGDEC.h>
class MjpegClass
{
public:
bool setup(
Stream *input, uint8_t *mjpeg_buf, JPEG_DRAW_CALLBACK *pfnDraw, bool useBigEndian,
int x, int y, int widthLimit, int heightLimit)
{
_input = input;
_mjpeg_buf = mjpeg_buf;
_pfnDraw = pfnDraw;
_useBigEndian = useBigEndian;
_x = x;
_y = y;
_widthLimit = widthLimit;
_heightLimit = heightLimit;
_inputindex = 0;
if (!_read_buf)
{
_read_buf = (uint8_t *)malloc(READ_BUFFER_SIZE);
}
return true;
}
bool readMjpegBuf()
{
if (_inputindex == 0)
{
_buf_read = _input->readBytes(_read_buf, READ_BUFFER_SIZE);
_inputindex += _buf_read;
}
_mjpeg_buf_offset = 0;
int i = 0;
bool found_FFD8 = false;
while ((_buf_read > 0) && (!found_FFD8))
{
i = 0;
while ((i < _buf_read) && (!found_FFD8))
{
if ((_read_buf[i] == 0xFF) && (_read_buf[i + 1] == 0xD8)) // JPEG header
{
// Serial.printf("Found FFD8 at: %d.\n", i);
found_FFD8 = true;
}
++i;
}
if (found_FFD8)
{
--i;
}
else
{
_buf_read = _input->readBytes(_read_buf, READ_BUFFER_SIZE);
}
}
uint8_t *_p = _read_buf + i;
_buf_read -= i;
bool found_FFD9 = false;
if (_buf_read > 0)
{
i = 3;
while ((_buf_read > 0) && (!found_FFD9))
{
if ((_mjpeg_buf_offset > 0) && (_mjpeg_buf[_mjpeg_buf_offset - 1] == 0xFF) && (_p[0] == 0xD9)) // JPEG trailer
{
// Serial.printf("Found FFD9 at: %d.\n", i);
found_FFD9 = true;
}
else
{
while ((i < _buf_read) && (!found_FFD9))
{
if ((_p[i] == 0xFF) && (_p[i + 1] == 0xD9)) // JPEG trailer
{
found_FFD9 = true;
++i;
}
++i;
}
}
// Serial.printf("i: %d\n", i);
memcpy(_mjpeg_buf + _mjpeg_buf_offset, _p, i);
_mjpeg_buf_offset += i;
size_t o = _buf_read - i;
if (o > 0)
{
// Serial.printf("o: %d\n", o);
memcpy(_read_buf, _p + i, o);
_buf_read = _input->readBytes(_read_buf + o, READ_BUFFER_SIZE - o);
_p = _read_buf;
_inputindex += _buf_read;
_buf_read += o;
// Serial.printf("_buf_read: %d\n", _buf_read);
}
else
{
_buf_read = _input->readBytes(_read_buf, READ_BUFFER_SIZE);
_p = _read_buf;
_inputindex += _buf_read;
}
i = 0;
}
if (found_FFD9)
{
return true;
}
}
return false;
}
bool drawJpg()
{
_remain = _mjpeg_buf_offset;
_jpeg.openRAM(_mjpeg_buf, _remain, _pfnDraw);
if (_scale == -1)
{
// scale to fit height
int iMaxMCUs;
int w = _jpeg.getWidth();
int h = _jpeg.getHeight();
float ratio = (float)h / _heightLimit;
if (ratio <= 1)
{
_scale = 0;
iMaxMCUs = _widthLimit / 16;
}
else if (ratio <= 2)
{
_scale = JPEG_SCALE_HALF;
iMaxMCUs = _widthLimit / 8;
w /= 2;
h /= 2;
}
else if (ratio <= 4)
{
_scale = JPEG_SCALE_QUARTER;
iMaxMCUs = _widthLimit / 4;
w /= 4;
h /= 4;
}
else
{
_scale = JPEG_SCALE_EIGHTH;
iMaxMCUs = _widthLimit / 2;
w /= 8;
h /= 8;
}
_jpeg.setMaxOutputSize(iMaxMCUs);
_x = (w > _widthLimit) ? 0 : ((_widthLimit - w) / 2);
_y = (_heightLimit - h) / 2;
}
if (_useBigEndian)
{
_jpeg.setPixelType(RGB565_BIG_ENDIAN);
}
_jpeg.decode(_x, _y, _scale);
_jpeg.close();
return true;
}
private:
Stream *_input;
uint8_t *_mjpeg_buf;
JPEG_DRAW_CALLBACK *_pfnDraw;
bool _useBigEndian;
int _x;
int _y;
int _widthLimit;
int _heightLimit;
uint8_t *_read_buf;
int32_t _mjpeg_buf_offset = 0;
JPEGDEC _jpeg;
int _scale = -1;
int32_t _inputindex = 0;
int32_t _buf_read;
int32_t _remain = 0;
};
#endif // _MJPEGCLASS_H_
Binary file not shown.

After

Width:  |  Height:  |  Size: 630 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 KiB