src.nth.io/

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Hoersten <[email protected]>2026-06-15 07:23:47 -0500
committerLuke Hoersten <[email protected]>2026-06-15 07:23:47 -0500
commit51b816025183801f5953d849fe4ea83cd9385582 (patch)
treef719f6a72e4ee76a64fb2067523ce457de0faf63
parent78a1a327b23bf216c1021a06d22a67795176e288 (diff)
firmware: zero-copy JPEG → BGR888 → DSI hot path; Scrypted pre-rotates
Architectural rework of the /frame hot path. Scrypted now ships every JPEG already scaled + rotated to the panel's native dimensions (read from the firmware's /state response so nothing is hardcoded on the Scrypted side); the firmware decodes the JPEG straight into a BGR888 buffer that's directly draw_bitmap'able by the DSI driver, with zero CPU pixel work and zero rotation work in between. Firmware - jpeg_decoder now uses JPEG_DECODE_OUT_FORMAT_RGB888 + JPEG_DEC_RGB_ELEMENT_ORDER_BGR. Output buffer sized for 800*480*3. - New display_present_bgr888() is a one-liner that hands the decoder's output straight to esp_lcd_panel_draw_bitmap. - /frame handler validates dimensions against the panel-native VIEWPORT_PANEL_WIDTH x VIEWPORT_PANEL_HEIGHT (was effective_dims branching on orientation). Returns 400 if it's anything else. - /state JSON adds panel_width + panel_height so Scrypted can read them without hardcoding board-specific knowledge. - display_present_rgb565 + s_rot_buf stay for the local-screens cold path (info screen, loading) which still does its own CPU conversion + rotation — infrequent enough that it's not worth the rewrite. Scrypted - startStream() GETs /state at stream-start time, caches panel_width and panel_height in viewport storage, and uses them as the ffmpeg scale target. Falls back to cached or 800x480 if /state is mid-reboot. - For portrait viewports the ffmpeg pipeline now does scale=H:W:flags=lanczos,transpose=1 so the JPEG arrives pre-rotated 90° CW into panel-native dimensions. Landscape is just scale=W:H. - No more in-firmware rotation; Scrypted is the single source of truth for "how do I get this camera frame into a panel-shaped JPEG". Expected ceiling lift: ~5 fps → ~10 fps, gated by the JPEG decoder hardware throughput instead of the CPU rgb565→bgr888 + rotation loop.
-rw-r--r--main/display.c8
-rw-r--r--main/display.h11
-rw-r--r--main/http_api.c19
-rw-r--r--main/jpeg_decoder.c16
-rw-r--r--main/jpeg_decoder.h13
-rw-r--r--scrypted/scrypted-viewport.ts34
6 files changed, 79 insertions, 22 deletions
diff --git a/main/display.c b/main/display.c
index a704891..1acddba 100644
--- a/main/display.c
+++ b/main/display.c
@@ -429,6 +429,14 @@ static inline void rgb565_to_rgb888(uint16_t px, uint8_t *out)
out[2] = (uint8_t)(((px >> 11) & 0x1F) * 255 / 31); // R
}
+esp_err_t display_present_bgr888(const void *bgr888)
+{
+ if (!s_up) return ESP_ERR_INVALID_STATE;
+ return esp_lcd_panel_draw_bitmap(s_panel, 0, 0,
+ PANEL_H_ACTIVE, PANEL_V_ACTIVE,
+ bgr888);
+}
+
esp_err_t display_present_rgb565(const uint16_t *src,
uint16_t src_w,
uint16_t src_h)
diff --git a/main/display.h b/main/display.h
index 9092efb..5f939b9 100644
--- a/main/display.h
+++ b/main/display.h
@@ -31,7 +31,16 @@ esp_err_t display_wake(void);
// orientation. Source dimensions must match the effective resolution:
// portrait -> src is 480x800 (rotated 90° CW into the 800x480 panel)
// landscape -> src is 800x480 (copied 1:1)
-// Used by /frame after JPEG decode.
+// CPU-rotation + format-conversion path used by local_screens for the
+// info / loading screens (cold path). /frame uses the zero-copy
+// BGR888 path below.
esp_err_t display_present_rgb565(const uint16_t *src,
uint16_t src_w,
uint16_t src_h);
+
+// Zero-copy hot path. Source is already 800x480 with bytes in [B, G, R]
+// memory order (i.e. the format the panel pipeline natively wants);
+// hand it straight to esp_lcd_panel_draw_bitmap. No CPU pixel work, no
+// format conversion, no rotation — Scrypted is responsible for sending
+// the buffer pre-rotated and pre-scaled to panel-native dimensions.
+esp_err_t display_present_bgr888(const void *bgr888);
diff --git a/main/http_api.c b/main/http_api.c
index 2c5ce87..db317e4 100644
--- a/main/http_api.c
+++ b/main/http_api.c
@@ -53,6 +53,12 @@ static esp_err_t state_get_handler(httpd_req_t *req)
cJSON_AddNumberToObject(root, "decode_errors", (double)st->decode_errors);
cJSON_AddNumberToObject(root, "state_post_failures", (double)st->state_post_failures);
cJSON_AddStringToObject(root, "resolution", viewport_state_resolution_str());
+ // Panel-native dimensions are stable per-board (Scrypted uses them as
+ // the ffmpeg scale target; orientation drives whether to transpose
+ // before scaling). Separate from "resolution" which is the effective
+ // dimensions after the orientation rotation.
+ cJSON_AddNumberToObject(root, "panel_width", (double)VIEWPORT_PANEL_WIDTH);
+ cJSON_AddNumberToObject(root, "panel_height", (double)VIEWPORT_PANEL_HEIGHT);
cJSON_AddStringToObject(root, "ip", net_eth_get_ip_str());
cJSON_AddNumberToObject(root, "free_heap",
(double)heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
@@ -362,19 +368,22 @@ static esp_err_t frame_post_handler(httpd_req_t *req)
return respond_status(req, "400 Bad Request", "JPEG decode failed");
}
- uint16_t want_w, want_h;
- viewport_state_effective_dims(&want_w, &want_h);
- if (w != want_w || h != want_h) {
+ // /frame always expects the panel-native 800x480 BGR888 layout —
+ // Scrypted does the rotation + scale, the firmware just decodes and
+ // paints. Orientation lives in viewport_state for /state reporting
+ // and the Scrypted-side ffmpeg pipeline only.
+ if (w != VIEWPORT_PANEL_WIDTH || h != VIEWPORT_PANEL_HEIGHT) {
viewport_state_lock();
viewport_state_get()->decode_errors++;
viewport_state_unlock();
jpeg_decoder_unlock();
char msg[80];
- snprintf(msg, sizeof(msg), "expected %ux%u, got %ux%u", want_w, want_h, w, h);
+ snprintf(msg, sizeof(msg), "expected %ux%u, got %ux%u",
+ VIEWPORT_PANEL_WIDTH, VIEWPORT_PANEL_HEIGHT, w, h);
return respond_status(req, "400 Bad Request", msg);
}
- esp_err_t paint_err = display_present_rgb565((const uint16_t *)rgb, w, h);
+ esp_err_t paint_err = display_present_bgr888(rgb);
if (paint_err != ESP_OK) {
jpeg_decoder_unlock();
return respond_status(req, "500 Internal Server Error", "display paint failed");
diff --git a/main/jpeg_decoder.c b/main/jpeg_decoder.c
index f2a7d92..2538f69 100644
--- a/main/jpeg_decoder.c
+++ b/main/jpeg_decoder.c
@@ -62,18 +62,20 @@ void jpeg_decoder_unlock(void)
void *jpeg_decoder_input_buffer(void) { return s_in_buf; }
esp_err_t jpeg_decoder_decode(size_t jpeg_len,
- void **out_rgb565,
+ void **out_bgr888,
uint16_t *out_width,
uint16_t *out_height)
{
if (jpeg_len == 0 || jpeg_len > s_in_cap) return ESP_ERR_INVALID_SIZE;
+ // Hardware decode directly into a panel-native BGR888 buffer.
+ // _BGR rgb_order swaps the channel layout so memory ends up as
+ // [B, G, R] per pixel — exactly what the ESP32-P4 DSI engine +
+ // TC358762 + Pi panel pipeline wants. Total firmware-side cost in
+ // the hot /frame path: this hardware decode + one DMA hand-off to
+ // the panel. No CPU pixel work.
jpeg_decode_cfg_t dec_cfg = {
- .output_format = JPEG_DECODE_OUT_FORMAT_RGB565,
- // Empirically: _RGB emits the RGB565 word in big-endian byte order
- // (red 0xF800 → bytes f8 00) which our LE uint16 painter reads
- // swapped. _BGR emits little-endian to match (despite the
- // misleading "BGR" name) — verified by the decode-byte dump.
+ .output_format = JPEG_DECODE_OUT_FORMAT_RGB888,
.rgb_order = JPEG_DEC_RGB_ELEMENT_ORDER_BGR,
.conv_std = JPEG_YUV_RGB_CONV_STD_BT601,
};
@@ -94,7 +96,7 @@ esp_err_t jpeg_decoder_decode(size_t jpeg_len,
return err;
}
- *out_rgb565 = s_out_buf;
+ *out_bgr888 = s_out_buf;
*out_width = info.width;
*out_height = info.height;
return ESP_OK;
diff --git a/main/jpeg_decoder.h b/main/jpeg_decoder.h
index 9c5bf89..2ac436d 100644
--- a/main/jpeg_decoder.h
+++ b/main/jpeg_decoder.h
@@ -7,7 +7,7 @@
#include "esp_err.h"
#define JPEG_DECODER_MAX_INPUT_BYTES (1024 * 1024) // 1 MB
-#define JPEG_DECODER_MAX_OUTPUT_BYTES (800 * 480 * 2) // RGB565 panel native
+#define JPEG_DECODER_MAX_OUTPUT_BYTES (800 * 480 * 3) // BGR888 panel native
// One-time setup of the ESP32-P4 hardware JPEG decoder. Allocates reusable
// DMA-aligned input + output buffers in PSRAM.
@@ -23,11 +23,12 @@ void jpeg_decoder_unlock(void);
// JPEG_DECODER_MAX_INPUT_BYTES. Caller fills before calling decode.
void *jpeg_decoder_input_buffer(void);
-// Decode the JPEG sitting in the input buffer. Fills out_rgb565 with a
-// pointer to the decoded RGB565 image and reports its width/height in
-// pixels. The output buffer is owned by the decoder — valid only until
-// the next jpeg_decoder_unlock().
+// Decode the JPEG sitting in the input buffer. Fills out_bgr888 with a
+// pointer to the decoded 24-bit image (3 bytes/pixel, BGR memory order so
+// the DSI engine + TC358762 + Pi panel render channels correctly) and
+// reports the image's width/height in pixels. The output buffer is owned
+// by the decoder — valid only until the next jpeg_decoder_unlock().
esp_err_t jpeg_decoder_decode(size_t jpeg_len,
- void **out_rgb565,
+ void **out_bgr888,
uint16_t *out_width,
uint16_t *out_height);
diff --git a/scrypted/scrypted-viewport.ts b/scrypted/scrypted-viewport.ts
index 966d2fb..d4bf8a5 100644
--- a/scrypted/scrypted-viewport.ts
+++ b/scrypted/scrypted-viewport.ts
@@ -509,8 +509,36 @@ class ScryptedViewportProvider extends ScryptedDeviceBase
const cam: any = systemManager.getDeviceById(v.cameraId);
if (!cam) return;
- const w = v.orientation === "portrait" ? 480 : 800;
- const h = v.orientation === "portrait" ? 800 : 480;
+
+ // Fetch the panel's native dimensions from the firmware and
+ // cache them on the viewport's storage. Falls back to 800x480
+ // if /state is unreachable (e.g. mid-reboot). Panel dims never
+ // change for a given device so this only really needs to run
+ // once per discovery; refreshing on every wake costs ~5 ms and
+ // self-heals if the firmware is replaced.
+ const pw = parseInt(v.storage.getItem("panel_w") || "0", 10);
+ const ph = parseInt(v.storage.getItem("panel_h") || "0", 10);
+ let panelW = pw || 800;
+ let panelH = ph || 480;
+ try {
+ const st = await fetch(`http://${v.host}/state`, { signal: AbortSignal.timeout(1500) }).then(r => r.json());
+ if (st?.panel_width && st?.panel_height) {
+ panelW = Number(st.panel_width);
+ panelH = Number(st.panel_height);
+ v.storage.setItem("panel_w", String(panelW));
+ v.storage.setItem("panel_h", String(panelH));
+ }
+ } catch { /* keep cached values */ }
+
+ // Always send panel-native dimensions (panelW x panelH). For a
+ // portrait viewport we scale to the logical (panelH x panelW)
+ // target then transpose 90° CW so the buffer that arrives at the
+ // panel is already in the right rotation. The firmware never
+ // touches pixels — the hardware JPEG decoder writes BGR888
+ // straight into a DMA buffer that gets handed to the DSI engine.
+ const vf = v.orientation === "portrait"
+ ? `scale=${panelH}:${panelW}:flags=lanczos,transpose=1` // 90° CW → panelW x panelH
+ : `scale=${panelW}:${panelH}:flags=lanczos`;
const fps = Math.max(1, Math.round(1000 / v.frameIntervalMs));
// Pull the camera's video stream, convert to ffmpeg input args, and
@@ -548,7 +576,7 @@ class ScryptedViewportProvider extends ScryptedDeviceBase
"-fflags", "+genpts+nobuffer", "-flags", "low_delay",
...(ffmpegInput.inputArguments || []),
"-an", "-sn",
- "-vf", `scale=${w}:${h}:flags=lanczos,fps=${fps}`,
+ "-vf", `${vf},fps=${fps}`,
"-c:v", "mjpeg", "-q:v", "2",
"-f", "image2pipe", "-flush_packets", "1",
"pipe:1",