Skip to content

Parse embedded screenmap.json from .rgb / MPEG1 / MP4 videos + WASM screenmap import binding #3063

Closed

Description

@zackees

Motivation

LED-data videos and their screenmap.json currently ship as two loose files. Users routinely lose the pairing. We're going to embed the screenmap directly into the video container at the generator side (zackees/ledmapper#122) and want the FastLED loader to pick it up automatically.

Companion issue (generator side): zackees/ledmapper#122.

Container spec (shared with ledmapper)

Identical to the generator-side spec. Reproduced here so both repos can implement to the same contract.

offset  size  field         notes
------  ----  ------------- ----------------------------------------
0       4     magic         ASCII "FLED"
4       1     version       u8 — start at 1
5       3     reserved      must be 0
8       4     json_length   u32 little-endian (bytes of JSON payload)
12      N     json_bytes    UTF-8 JSON (no NUL terminator, no BOM)
12+N    ...   payload       format-specific (raw RGB triplets, MPEG1 ES, MP4 boxes...)
  • Backwards compat: if the first 4 bytes are not FLED, treat as legacy headerless data (current behavior). Zero risk to existing files / sketches.
  • JSON payload is the existing ScreenMap format unchanged:
    { "map": { "strip1": { "x": [...], "y": [...], "diameter": 0.25 } } }
    i.e. exactly what ScreenMap::ParseJson(const char*, ...) (src/fl/math/screenmap.cpp.hpp:98) already eats.

Work in this repo

Phase 1 — Raw .rgb (PixelStream)

PixelStream::begin() / readRGB8() (src/fl/video/pixel_stream.cpp.hpp:53) currently treat the file as pure RGB triplets — frame count = total_bytes / (led_count * 3). Wrap the existing logic so it:

  1. Peeks the first 12 bytes via filebuf::read.
  2. If magic == \"FLED\": reads json_length, then json_length bytes of JSON, stashes it, and adjusts the stream's logical "start" so all subsequent readRGB8() calls are unaware of the header. Frame count math becomes (total_bytes − header_size) / (led_count * 3).
  3. If magic is anything else: rewinds to byte 0 and behaves exactly as today.

New API surface on PixelStream (or on the Video wrapper at FileSystem::openVideo()):

bool hasEmbeddedScreenMap() const;
const fl::string& embeddedScreenMapJson() const;  // empty if not present

Sketch hook (proposed UX): when a sketch calls setScreenMap(...) without arguments after opening a video with embedded JSON, auto-apply the embedded map. Bikeshed welcome.

Phase 2 — MPEG1 user_data

pl_mpeg (src/third_party/pl_mpeg/) decodes MPEG1 but does not surface user_data_start_code (0x000001B2) blocks. The FastLED-side Mpeg1Info struct (src/fl/codec/mpeg1.h:32) has no metadata fields today.

Two implementation choices:

  • (a) Patch pl_mpeg to expose a user_data callback. Touches a vendored third-party lib — small change but worth flagging.
  • (b) Pre-scan the file with our own minimal MPEG1 byte scanner that walks the bitstream looking for 00 00 01 B2 and stops once it finds (or fails to find) the FLED-magic'd block, then hands the full file to pl_mpeg unchanged.

(b) keeps the third-party patch surface zero. Recommended.

Wire the discovered JSON through the same embeddedScreenMapJson() accessor on the Video wrapper.

Phase 3 — MP4 udta (lower priority)

Current parseMp4() (src/fl/codec/mp4_parser.h:10) walks ftyp→moov→trak→mdia→…→avcC for H.264 SPS/PPS only — it doesn't touch moov.udta. Extend it to additionally walk moov.udta, look for our private UUID box, and recover the JSON.

Heads-up: the original brief mentioned "MP4 (parlio HW accel)". Parlio at src/platforms/esp/32/drivers/parlio/ is the parallel-I/O driver for LED output, not a video decoder. There's no MP4 HW decoder in FastLED core today. If there's a separate MP4 decode path I missed, please link it here so the metadata hook lands in the right place.

Phase 4 — WASM screenmap import binding

The current WASM bridge (src/platforms/wasm/js_bindings.cpp.hpp:91) is one-way: C++ exports screenmaps to JS via getScreenMapData(int*). There's no JS → C++ path to load a screenmap. For browser playback of embedded-screenmap videos to "just work", add:

// js_bindings.cpp.hpp
EMSCRIPTEN_KEEPALIVE
extern \"C\" void jsSetScreenMapsFromJson(const char* json, int len);

That hands the JSON to ScreenMap::ParseJson(...) and pushes the result into the same registry that _jsSetCanvasSize currently writes to.

Browser flow then becomes:

  1. JS fetches video.rgb.
  2. JS peeks the FLED header, extracts embedded JSON.
  3. JS calls _jsSetScreenMapsFromJson(jsonPtr, jsonLen).
  4. Sketch wakes up with a populated screenmap; user dropped one file, not two.

Acceptance

  • Phase 1: a .rgb with the FLED header parses cleanly via FileSystem::openVideo(), surfaces embeddedScreenMapJson(), and frame math is correct (off-by-12 + json_length errors caught by tests).
  • Phase 1: legacy headerless .rgb files still play unchanged (no perf or behavior regression).
  • Phase 4: WASM round-trip with a real ledmapper-recorded file works end-to-end in the browser demo.
  • Phases 2 / 3 land as follow-ups once Phase 1 is settled and we've validated the spec end-to-end.

Test plan

  • Unit test: synthesize a buffer of FLED \x01 \x00\x00\x00 + len + json + N frames of RGB, run through PixelStream, verify JSON and frames recover byte-identical.
  • Unit test: legacy headerless buffer parses as before, hasEmbeddedScreenMap() == false.
  • Unit test: corrupt headers (truncated, bad magic, length > buffer) degrade gracefully — never crash on user input.
  • Integration: pair with a ledmapper-recorded .rgb from the companion PR, play on an ESP32 / in WASM, confirm screenmap auto-applies.

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Locked the v1 spec on the generator side. Canonical reference: docs/fled-format.md in ledmapper (added in the Phase 1 PR linked from zackees/ledmapper#122). FastLED should consume this exact spec.

    Locked decisions:

    • File extension: .fled (replaces sidecar .rgb + .json pairing)
    • 12-byte fixed header: magic[4]="FLED" + version[1]=1 + pixel_format[1] + reserved[2] + json_length[4 u32 LE], then UTF-8 JSON, then payload.
    • Pixel format is in the binary header (not JSON) — consumers can slice frames without parsing JSON at all. bytesPerLed[format] is a 5-entry lookup. Unknown values reject cleanly.
    • Pixel format enum: 0x00 rgb8 (Phase 1), 0x01 gray8, 0x02 rgba8, 0x03 rgbw8, 0x04 rgb565le.
    • JSON keeps the standard ScreenMap map block and adds optional video.fps. No video.format (header is authoritative).
    • Versioning: adding new pixel-format enum values is NOT a version bump (since rejection of unknowns is defined). Only header/JSON schema changes bump version.
    • Container choice: ad-hoc rather than RIFF/MKV/MP4 — fits on a postcard, both ends are first-party.

    Reference vector (in the docs and in ledmapper's tests/unit/rgb-video.test.ts):

    • header: 46 4C 45 44 01 00 00 00 1F 00 00 00
    • JSON: {"map":{"a":{"x":[0],"y":[0]}}} (31 bytes)
    • payload: FF 00 00 (one frame, one LED, pure red)
    • total: 46 bytes

    Both repos should agree on this vector bit-exactly. Suggested FastLED test: synthesize the above 46 bytes in a unit test and verify PixelStream recovers JSON + frame correctly.

  2. zackees commented on Jun 20, 2026

    @zackees
    MemberAuthor

    Closing as resolved / superseded — per-phase status

    Phase 1 — PixelStream FLED header parsing: ✅ Already shipped independently for issue #3072. The current code at src/fl/video/pixel_stream.{h,cpp.hpp} parses the FLED v1 header on seekable handles, exposes PixelStream::hasEmbeddedScreenMap() and embeddedScreenMapJson(), and surfaces the same accessors on fl::Video (src/fl/fx/video.h:78-79). Legacy headerless .rgb files still work. Phase 1 was the load-bearing piece — done.

    Phase 4 — WASM JS→C++ screenmap binding: superseded by the fl::Fled bundle subsystem (#3311), specifically by fl::Fled::loadFromVector(fl::vector<fl::u8>&&) + fl::Fled::loadFromStatic(fl::span<const fl::u8>) and the new fl::FileSystem::wasm() virtual-FS factory. The browser flow becomes:

    • JS fetches the .fled bytes
    • Hands them to the C++ side as a buffer (or writes to the WASM virtual FS)
    • C++ calls fl::Fled::loadFromVector(...) / fl::Fled::load(fs, ...)
    • FastLED.load(fled) binds the screenmap to controllers, which broadcasts via EngineEvents::onCanvasUiSet — the JS canvas auto-receives it through the same path JS uses today

    No dedicated jsSetScreenMapsFromJson binding is needed; the buffer-handoff pattern covers it generically.

    Phases 2 & 3 — MPEG1 user_data / MP4 udta extraction: deferred indefinitely. These are container-specific format additions (embedding screenmap JSON inside an MPEG1 or MP4 file rather than alongside it). The .fled container (which #3311 generalizes into a full FastLED bundle format with hardware config, scripts, etc.) is the canonical path forward for "ship the screenmap in the same file as the pixel data." MPEG1/MP4 carrying a screenmap is a much smaller niche. If real demand emerges for either, file a focused issue at that point — but don't carry the open ticket here in the meantime.

    Cross-link

  3. moved this from Triage to Done in FastLED Trackeron Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions