zstd_backend

Example of the component espressif/esp_efc_codec v1.1.1
# EFC Custom Zstd Compression Backend Example

- [中文版本](./README_CN.md)
- Regular Example: ⭐⭐

## Example Brief

This example demonstrates how to use **Zstandard (Zstd)** as a custom compression backend for `esp_efc_codec` via the streaming decoder API (`esp_efc_dec_open_stream()`), without modifying `libesp_efc_codec.a`.

```text
Uncompressed PRED EFC (z=OFF) --> Zstd Compress (PC) --> giphy_zstd.efc --> efc_zstd_read_cb() --> esp_efc_dec_open_stream()
```

### How It Works

1. **Build-Time Pre-Generation (`main/CMakeLists.txt` & `scripts/gen_zstd_efc.py`):**
   - During CMake configuration, `scripts/gen_zstd_efc.py` automatically takes `test_app/main/assets/giphy_z.efc` (`347,494 B`), copies it to `${CMAKE_CURRENT_BINARY_DIR}/giphy_z.efc`, and generates `${CMAKE_CURRENT_BINARY_DIR}/giphy_zstd.efc` (`331,963 B`) by inflating the inner `PRED` planes (`struct`, `modes`, `lits`) and wrapping the header and planes in 4 independent RFC 8878 Zstd frames (level 19, prefixed with a 24-byte RFC 8878 skippable frame `0x184D2A50`).
   - If built standalone outside the full repository without `test_app/main/assets/giphy_z.efc`, `scripts/gen_zstd_efc.py` automatically synthesizes a valid fallback `PRED` test clip.
   - Both `giphy_z.efc` and `giphy_zstd.efc` are embedded into the firmware via `EMBED_FILES`.
2. **On-Device Zstd Stream Decoder (`main/zstd_dec.c` & `main/zstd_backend_main.c`):**
   - Uses the single-file BSD-3-Clause RFC 8878 Zstd decoder (`zstd_dec.c`).
   - Implements `efc_zstd_read_cb()` (`esp_efc_dec_read_cb_t`):
     - At `esp_efc_dec_open_stream()`, only the ~564-byte EFC header/palette/directory Zstd frame is decompressed (`~385 µs`).
     - On Frame 0 (`I-frame`), only the `struct` plane is decompressed directly into `d->struct_buf`, cutting first-frame latency in half.
     - On Frame 1 (`1st P-frame`), `modes` and `lits` are decompressed **directly into `d->modes_buf` and `d->lits_buf`** (zero extra intermediate full-file PSRAM buffer), and `ZSTD_DCtx` is freed immediately after decompression.
3. **Side-by-Side Benchmark (`app_main`):**
   - Benchmarks standard in-memory `zlib` (`esp_efc_dec_open` on `giphy_z.efc`) against custom `Zstd` stream mode (`esp_efc_dec_open_stream` on `giphy_zstd.efc`) and verifies that the decoded RGB565 frame checksums match 100%.

### Folder Contents

```text
├── CMakeLists.txt
├── partitions.csv              8 MB flash table with 6 MB factory partition
├── sdkconfig.defaults          Compiler perf optimization, 8 MB flash, custom partition
├── sdkconfig.defaults.esp32s3  240 MHz CPU clock and octal PSRAM
├── sdkconfig.defaults.esp32p4  ESP32-P4 PSRAM defaults
├── scripts
│   └── gen_zstd_efc.py         Pre-build helper generating giphy_z.efc & giphy_zstd.efc
└── main
    ├── CMakeLists.txt          Runs gen_zstd_efc.py and embeds both .efc assets
    ├── idf_component.yml       Depends only on esp_efc_codec (no board manager needed)
    ├── zstd_dec.c              Single-file Zstd frame decoder (RFC 8878)
    ├── zstd_dec.h              Zstd decoder header
    └── zstd_backend_main.c     Zstd stream callback + zlib vs Zstd benchmark
```

## Environment Setup

### Hardware Required

- An ESP32-S3 or ESP32-P4 development board with **PSRAM** (`>= 2 MB` for holding the 480×480 RGB565 framebuffer and embedded benchmark buffers) and **8 MB flash** (configured by `sdkconfig.defaults` and `partitions.csv`).
- No LCD display, SD card, or `esp_board_manager` configuration is required.

### Host Prerequisites

- A working [ESP-IDF v5.5](https://docs.espressif.com/projects/esp-idf/en/v5.5/esp32s3/get-started/index.html) or later environment with `IDF_PATH` set.
- Host Python 3 with at least one Zstd compressor available for level-19 pre-build compression:
  - `pip install zstandard`, system `libzstd` (`libzstd.so.1` / `libzstd.dylib`), or the `zstd` CLI on `PATH`.
  - If none of these are installed on the host, `scripts/gen_zstd_efc.py` automatically falls back to emitting a valid uncompressed RFC 8878 Zstd raw-block stream so the build still succeeds.

## Build and Flash

```bash
idf.py set-target esp32s3
idf.py build flash monitor
```

To target ESP32-P4 instead:

```bash
idf.py set-target esp32p4
idf.py build flash monitor
```

## Example Output

```text
I (325) efc_zstd_example: === EFC Custom Zstd Backend Benchmark ===
I (325) efc_zstd_example: Assets: giphy_z.efc (zlib)=347494 B | giphy_zstd.efc (zstd)=331963 B (-4.47%)
I (1310) efc_zstd_example: Clip: 480x480, 35 frames
I (1310) efc_zstd_example:   [1] Standard API (zlib) : size= 347494 B | open=109012 us | 1st_frame=118310 us | decode=15506.9 us/f ( 64.5 fps) | csum=0xfca1c019
I (1315) efc_zstd_example:   [2] Stream Mode  (zstd) : size= 331963 B | open=   385 us | 1st_frame= 60234 us | decode=14987.5 us/f ( 66.7 fps) | csum=0xfca1c019
I (1320) efc_zstd_example: PASS: Zstd stream mode pixels match standard zlib EFC 100% (csum=0xfca1c019)
```

## Technical Support and Feedback

Please use the following feedback channels:

- For technical queries, go to the [esp32.com](https://esp32.com/viewforum.php?f=20) forum
- For a feature request or bug report, open an issue on the `esp_efc_codec` project

To create a project from this example, run:

idf.py create-project-from-example "espressif/esp_efc_codec=1.1.1:zstd_backend"

or download archive (~30.31 KB)