← Back to Pull requests

docs(blog): add article on preparing video for ESP32 displays with FFmpeg, tested in QEMU #864

Merged
markksantos merged 6 files (812 lines) on Nov 18, 2026 09:04 UTC
View on GitHub
markksantos markksantos commented on Nov 02, 2026 14:02 UTC
Author

📝 Description

This adds a community article: preparing video for ESP32 displays with FFmpeg, packing the MJPEG frames into a data partition, and playing them with ESP_NEW_JPEG. Every step was tested in Espressif's QEMU, with no board.

What was tested:

  • ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and esp_lcd_qemu_rgb 1.0.2, in the QEMU 9.2.2 from the espressif/idf:v6.1 image. FFmpeg was 9.0.2, and the FFmpeg behaviors were also checked on 7.1.5.

  • All 150 frames of the test clip decoded and drew on a 320x240 and a 135x240 virtual panel.

  • For the frames dumped over serial, every color channel of every pixel is within one RGB565 step of FFmpeg's decode of the same JPEG. The QEMU screen captures match the decoded pixels exactly.
  • It also covers four things that broke along the way and how they were handled:

  • FFmpeg silently rounding odd panel sizes down with 4:2:0.

  • ESP_NEW_JPEG rejecting FFmpeg's 4:4:4 MJPEG.

  • esp_lcd_qemu_rgb's draw_bitmap hanging when QEMU runs headless (fixed with monitor screendump).

  • An odd-width virtual panel never refreshing in QEMU.
  • The full code is in collapsible listings at the end of the article, so nothing depends on an outside repository.

    I drafted the article with help from an AI assistant. Every command in it was run and every number was measured before this PR. The author entry uses the default Espressif image.

    Publish date

    Expected publish date: 2026-11-20 (tentative). Happy to adjust to your publication schedule.

    Review process

  • Initiate technical review

  • - Subject matter experts: whoever covers ESP_NEW_JPEG, esp_lcd_qemu_rgb or Espressif's QEMU
  • Once tech review mostly done, initiate editorial review

  • - Technical editors (f-hollow, FBEZ, and/or pedrominatel)

    Checks

  • Article folder and file names:

  • - Folder path is content/blog/2026/11/preparing-video-for-esp32-displays
    - Folder and file names have no underscores, spaces, or uppercase letters
  • New article's YAML frontmatter:

  • - Title updated
    - Date matches the format date: 20YY-MM-DD
    - Summary updated
    - Authors added (mark-santos, with data/authors/mark-santos.json)
    - Tags added
  • Updated article's YAML frontmatter:<br>

  • - [x] This item is N/A
  • Article media files:

  • - All images are in .WebP format
    - Images are 5-96 KB each, under the 500 kB limit
  • Links in articles

  • - All links are valid (checked today)
    - No links to Google docs present
    - No ESP-IDF documentation links, so no stable or latest
  • Git history

  • - One commit
    - Commit message follows Conventional Commits
    - The branch is rebased on main

    🔗 Related

  • ESP_NEW_JPEG: https://components.espressif.com/components/espressif/esp_new_jpeg

  • esp_lcd_qemu_rgb: https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb
  • 🧪 Testing (Hugo)

  • bash tools/ci/check_article_details.sh passes locally on the rebased branch ("All required checks passed"), including its Hugo build.

  • The built page renders the article, the code listings and the author box.
  • Diff: 812 lines (+812 / -0) across 6 files Reviewer: pedrominatel Review duration: 22742m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.50) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 812 lines diff across 6 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.80 · 5 substantive comment(s)
    0.80
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    85%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Real PR Conversation Timeline
    17 items
    f-hollow f-hollow commented on code on Nov 10, 2026 02:58 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."
    +authors:
    +  - "mark-santos"
    +tags:
    +  - Multimedia
    +  - JPEG
    +  - ESP32-S3
    +  - QEMU
    +  - community contribution
    +---
    +
    +A boot animation, a product demo loop or a short clip on a status screen all
    +come down to the same job: get video into a form a microcontroller can decode
    +fast and store cheaply. On an ESP32, a sequence of JPEG frames (MJPEG) is the
    +usual answer, because
    +[ESP_NEW_JPEG](https://components.espressif.com/components/espressif/esp_new_jpeg)
    +decodes baseline JPEG in software on every chip, and each frame decodes on its
    +own.
    +
    +I edit video for a living, so the FFmpeg side of this is familiar ground. This
    +article covers the whole path: preparing the frames with FFmpeg, packing them
    +into a data partition, and a small player that memory-maps the partition and
    +decodes with ESP_NEW_JPEG. Since timing on real hardware varies by chip and
    +panel, I tested correctness rather than speed, and I did it without a board:
    +Espressif's QEMU emulates an RGB framebuffer, so the decoded frames can be
    +captured and compared with FFmpeg's decode of the same files.
    +
    +The example project is ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    +[esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    +1.0.2, run in the QEMU 9.2.2 that ships in the `espressif/idf:v6.1` Docker
    +image. FFmpeg is 9.0.2. The FFmpeg behaviors below were also checked on FFmpeg

    Can you please provide a link to the Docker image?

    f-hollow f-hollow commented on code on Nov 10, 2026 03:01 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."
    +authors:
    +  - "mark-santos"
    +tags:
    +  - Multimedia
    +  - JPEG
    +  - ESP32-S3
    +  - QEMU
    +  - community contribution
    +---
    +
    +A boot animation, a product demo loop or a short clip on a status screen all
    +come down to the same job: get video into a form a microcontroller can decode
    +fast and store cheaply. On an ESP32, a sequence of JPEG frames (MJPEG) is the
    +usual answer, because
    +[ESP_NEW_JPEG](https://components.espressif.com/components/espressif/esp_new_jpeg)
    +decodes baseline JPEG in software on every chip, and each frame decodes on its
    +own.
    +
    +I edit video for a living, so the FFmpeg side of this is familiar ground. This
    +article covers the whole path: preparing the frames with FFmpeg, packing them
    +into a data partition, and a small player that memory-maps the partition and
    +decodes with ESP_NEW_JPEG. Since timing on real hardware varies by chip and
    +panel, I tested correctness rather than speed, and I did it without a board:
    +Espressif's QEMU emulates an RGB framebuffer, so the decoded frames can be
    +captured and compared with FFmpeg's decode of the same files.
    +
    +The example project is ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    +[esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    +1.0.2, run in the QEMU 9.2.2 that ships in the `espressif/idf:v6.1` Docker

    The example project uses [ESP-IDF v6.1](https://github.com/espressif/esp-idf) on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    [esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    1.0.2, run in [QEMU 9.2.2](https://github.com/espressif/qemu) that ships in the `espressif/idf:v6.1` Docker
    

    f-hollow f-hollow commented on code on Nov 10, 2026 03:12 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."
    +authors:
    +  - "mark-santos"
    +tags:
    +  - Multimedia
    +  - JPEG
    +  - ESP32-S3
    +  - QEMU
    +  - community contribution
    +---
    +
    +A boot animation, a product demo loop or a short clip on a status screen all
    +come down to the same job: get video into a form a microcontroller can decode
    +fast and store cheaply. On an ESP32, a sequence of JPEG frames (MJPEG) is the
    +usual answer, because
    +[ESP_NEW_JPEG](https://components.espressif.com/components/espressif/esp_new_jpeg)
    +decodes baseline JPEG in software on every chip, and each frame decodes on its
    +own.
    +
    +I edit video for a living, so the FFmpeg side of this is familiar ground. This
    +article covers the whole path: preparing the frames with FFmpeg, packing them
    +into a data partition, and a small player that memory-maps the partition and
    +decodes with ESP_NEW_JPEG. Since timing on real hardware varies by chip and
    +panel, I tested correctness rather than speed, and I did it without a board:
    +Espressif's QEMU emulates an RGB framebuffer, so the decoded frames can be
    +captured and compared with FFmpeg's decode of the same files.
    +
    +The example project is ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    +[esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    +1.0.2, run in the QEMU 9.2.2 that ships in the `espressif/idf:v6.1` Docker
    +image. FFmpeg is 9.0.2. The FFmpeg behaviors below were also checked on FFmpeg
    +7.1.5.
    +
    +## Preparing the frames
    +
    +The test footage comes from FFmpeg itself, so anyone can regenerate it: six
    +seconds of a Mandelbrot zoom (fine detail, the worst case for JPEG) and four of
    +`testsrc2` (flat colors and sharp edges, like UI graphics), at 1280x720 and 30
    +fps.
    +
    +For a 320x240 panel at 15 fps, one command scales the video until it covers the
    +panel, crops the middle, drops the frame rate and encodes each frame as a JPEG:
    +
    +```bash
    +ffmpeg -i source.mp4 \
    +  -vf "scale=320:240:force_original_aspect_ratio=increase,crop=320:240,fps=15" \
    +  -an -c:v mjpeg -q:v 5 -pix_fmt yuv420p -f mjpeg frames.mjpeg
    +```
    +
    +`-f mjpeg` writes the JPEGs back to back with no container. `-pix_fmt yuv420p`
    +gives 4:2:0 chroma; the older `yuvj420p` produces a byte-identical file but
    +prints a deprecation warning. FFmpeg's MJPEG encoder stores full-range color
    +either way.
    +
    +`-q:v` sets the quality, from 2 (best) to 31. For the 10-second test clip at
    +320x240 and 15 fps, here is what each setting costs in flash, and the luma PSNR
    +of the decoded frames against the same frames before compression:
    +
    +| `-q:v` | 150 frames  | Per frame (avg) | PSNR, average | PSNR, worst frame |
    +| ------ | ----------- | --------------- | ------------- | ----------------- |
    +| 2      | 2,679,760 B | 17,865 B        | 45.1 dB       | 41.3 dB           |
    +| 3      | 2,177,344 B | 14,516 B        | 42.2 dB       | 38.1 dB           |
    +| 5      | 1,648,984 B | 10,993 B        | 38.7 dB       | 34.4 dB           |
    +| 8      | 1,208,992 B | 8,060 B         | 35.7 dB       | 31.2 dB           |
    +| 12     | 914,140 B   | 6,094 B         | 33.5 dB       | 29.0 dB           |
    +| 20     | 630,520 B   | 4,203 B         | 31.1 dB       | 26.6 dB           |
    +| 31     | 467,872 B   | 3,119 B         | 29.3 dB       | 25.1 dB           |
    +
    +The Mandelbrot frames are 13% (at `-q:v 31`) to 66% (at `-q:v 2`) larger than the
    +`testsrc2` frames, so judge quality on the kind of footage you will actually show. For a
    +3 MB partition, `-q:v 5` fits this clip with room to spare.
    +
    +## Packing frames into a partition
    +
    +The player needs to know where each frame starts. Searching the stream for the
    +JPEG start marker (`FF D8`) is tempting, but those bytes can also appear inside
    +a header segment, so `pack_video.py` walks the markers properly: it follows
    +segment lengths up to the start of scan, then skips the entropy-coded data to
    +the end marker. Inside entropy-coded data, a real `FF` byte is always followed
    +by `00` or a restart marker, so the end marker can't be mistaken.
    +
    +It writes a small header, a frame index and the frames, each frame on a 4-byte
    +boundary:
    +
    +```text
    +offset 0   char[4]  "MJV1"
    +       4   u16      panel width
    +       6   u16      panel height
    +       8   u16      fps numerator
    +      10   u16      fps denominator
    +      12   u32      frame count (N)
    +      16   N x {u32 offset, u32 size}
    +           JPEG frames
    +```
    +
    +The partition table gives the video its own data partition, so the video can be
    +replaced without rebuilding the app:
    +
    +```csv
    +# Name,   Type, SubType, Offset,   Size
    +nvs,      data, nvs,     0x9000,   0x6000
    +factory,  app,  factory, 0x10000,  0xF0000
    +video,    data, 0x40,    0x100000, 0x300000
    +```
    +
    +## The player
    +
    +The player maps the whole partition into the address space and hands each
    +frame to the decoder straight from flash, so no frame is copied into RAM before
    +decoding:
    +
    +```c
    +const esp_partition_t *part =
    +    esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "video");
    +const void *map;
    +esp_partition_mmap_handle_t map_handle;
    +ESP_ERROR_CHECK(esp_partition_mmap(part, 0, part->size, ESP_PARTITION_MMAP_DATA,
    +                                   &map, &map_handle));
    +const video_t *v = map;
    +```
    +
    +The decoder writes RGB565 into one buffer, which must be 16-byte aligned:
    +
    +```c
    +jpeg_dec_config_t config = DEFAULT_JPEG_DEC_CONFIG();
    +config.output_type = JPEG_PIXEL_FORMAT_RGB565_LE;
    +jpeg_dec_handle_t dec;
    +jpeg_dec_open(&config, &dec);
    +uint8_t *pixels = jpeg_calloc_align(frame_bytes, 16);
    +
    +for (uint32_t i = 0; i < v->count; i++) {
    +  jpeg_dec_io_t io = {
    +      .inbuf = (uint8_t *)map + v->frames[i].offset,
    +      .inbuf_len = v->frames[i].size,
    +      .outbuf = pixels,
    +  };
    +  jpeg_dec_header_info_t info;
    +  jpeg_error_t err = jpeg_dec_parse_header(dec, &io, &info);
    +  if (err == JPEG_ERR_OK) {
    +    err = jpeg_dec_process(dec, &io);
    +  }
    +  // ... draw, then wait for the next frame time
    +}
    +```
    +
    +Each frame is shown for one frame period, timed with `esp_timer`. If decoding

    Consider adding a link to esp_timer:

    Each frame is shown for one frame period, timed with [esp_timer](https://docs.espressif.com/projects/esp-idf/en/v6.1/esp32s3/api-reference/system/esp_timer.html). If decoding
    

    f-hollow f-hollow commented on code on Nov 10, 2026 03:16 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."
    +authors:
    +  - "mark-santos"
    +tags:
    +  - Multimedia
    +  - JPEG
    +  - ESP32-S3
    +  - QEMU
    +  - community contribution
    +---
    +
    +A boot animation, a product demo loop or a short clip on a status screen all
    +come down to the same job: get video into a form a microcontroller can decode
    +fast and store cheaply. On an ESP32, a sequence of JPEG frames (MJPEG) is the
    +usual answer, because
    +[ESP_NEW_JPEG](https://components.espressif.com/components/espressif/esp_new_jpeg)
    +decodes baseline JPEG in software on every chip, and each frame decodes on its
    +own.
    +
    +I edit video for a living, so the FFmpeg side of this is familiar ground. This
    +article covers the whole path: preparing the frames with FFmpeg, packing them
    +into a data partition, and a small player that memory-maps the partition and
    +decodes with ESP_NEW_JPEG. Since timing on real hardware varies by chip and
    +panel, I tested correctness rather than speed, and I did it without a board:
    +Espressif's QEMU emulates an RGB framebuffer, so the decoded frames can be
    +captured and compared with FFmpeg's decode of the same files.
    +
    +The example project is ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    +[esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    +1.0.2, run in the QEMU 9.2.2 that ships in the `espressif/idf:v6.1` Docker
    +image. FFmpeg is 9.0.2. The FFmpeg behaviors below were also checked on FFmpeg
    +7.1.5.
    +
    +## Preparing the frames
    +
    +The test footage comes from FFmpeg itself, so anyone can regenerate it: six
    +seconds of a Mandelbrot zoom (fine detail, the worst case for JPEG) and four of
    +`testsrc2` (flat colors and sharp edges, like UI graphics), at 1280x720 and 30
    +fps.
    +
    +For a 320x240 panel at 15 fps, one command scales the video until it covers the
    +panel, crops the middle, drops the frame rate and encodes each frame as a JPEG:
    +
    +```bash
    +ffmpeg -i source.mp4 \
    +  -vf "scale=320:240:force_original_aspect_ratio=increase,crop=320:240,fps=15" \
    +  -an -c:v mjpeg -q:v 5 -pix_fmt yuv420p -f mjpeg frames.mjpeg
    +```
    +
    +`-f mjpeg` writes the JPEGs back to back with no container. `-pix_fmt yuv420p`
    +gives 4:2:0 chroma; the older `yuvj420p` produces a byte-identical file but
    +prints a deprecation warning. FFmpeg's MJPEG encoder stores full-range color
    +either way.
    +
    +`-q:v` sets the quality, from 2 (best) to 31. For the 10-second test clip at
    +320x240 and 15 fps, here is what each setting costs in flash, and the luma PSNR
    +of the decoded frames against the same frames before compression:
    +
    +| `-q:v` | 150 frames  | Per frame (avg) | PSNR, average | PSNR, worst frame |
    +| ------ | ----------- | --------------- | ------------- | ----------------- |
    +| 2      | 2,679,760 B | 17,865 B        | 45.1 dB       | 41.3 dB           |
    +| 3      | 2,177,344 B | 14,516 B        | 42.2 dB       | 38.1 dB           |
    +| 5      | 1,648,984 B | 10,993 B        | 38.7 dB       | 34.4 dB           |
    +| 8      | 1,208,992 B | 8,060 B         | 35.7 dB       | 31.2 dB           |
    +| 12     | 914,140 B   | 6,094 B         | 33.5 dB       | 29.0 dB           |
    +| 20     | 630,520 B   | 4,203 B         | 31.1 dB       | 26.6 dB           |
    +| 31     | 467,872 B   | 3,119 B         | 29.3 dB       | 25.1 dB           |
    +
    +The Mandelbrot frames are 13% (at `-q:v 31`) to 66% (at `-q:v 2`) larger than the
    +`testsrc2` frames, so judge quality on the kind of footage you will actually show. For a
    +3 MB partition, `-q:v 5` fits this clip with room to spare.
    +
    +## Packing frames into a partition
    +
    +The player needs to know where each frame starts. Searching the stream for the
    +JPEG start marker (`FF D8`) is tempting, but those bytes can also appear inside
    +a header segment, so `pack_video.py` walks the markers properly: it follows
    +segment lengths up to the start of scan, then skips the entropy-coded data to
    +the end marker. Inside entropy-coded data, a real `FF` byte is always followed
    +by `00` or a restart marker, so the end marker can't be mistaken.
    +
    +It writes a small header, a frame index and the frames, each frame on a 4-byte
    +boundary:
    +
    +```text
    +offset 0   char[4]  "MJV1"
    +       4   u16      panel width
    +       6   u16      panel height
    +       8   u16      fps numerator
    +      10   u16      fps denominator
    +      12   u32      frame count (N)
    +      16   N x {u32 offset, u32 size}
    +           JPEG frames
    +```
    +
    +The partition table gives the video its own data partition, so the video can be
    +replaced without rebuilding the app:
    +
    +```csv
    +# Name,   Type, SubType, Offset,   Size
    +nvs,      data, nvs,     0x9000,   0x6000
    +factory,  app,  factory, 0x10000,  0xF0000
    +video,    data, 0x40,    0x100000, 0x300000
    +```
    +
    +## The player
    +
    +The player maps the whole partition into the address space and hands each
    +frame to the decoder straight from flash, so no frame is copied into RAM before
    +decoding:
    +
    +```c
    +const esp_partition_t *part =
    +    esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "video");
    +const void *map;
    +esp_partition_mmap_handle_t map_handle;
    +ESP_ERROR_CHECK(esp_partition_mmap(part, 0, part->size, ESP_PARTITION_MMAP_DATA,
    +                                   &map, &map_handle));
    +const video_t *v = map;
    +```
    +
    +The decoder writes RGB565 into one buffer, which must be 16-byte aligned:
    +
    +```c
    +jpeg_dec_config_t config = DEFAULT_JPEG_DEC_CONFIG();
    +config.output_type = JPEG_PIXEL_FORMAT_RGB565_LE;
    +jpeg_dec_handle_t dec;
    +jpeg_dec_open(&config, &dec);
    +uint8_t *pixels = jpeg_calloc_align(frame_bytes, 16);
    +
    +for (uint32_t i = 0; i < v->count; i++) {
    +  jpeg_dec_io_t io = {
    +      .inbuf = (uint8_t *)map + v->frames[i].offset,
    +      .inbuf_len = v->frames[i].size,
    +      .outbuf = pixels,
    +  };
    +  jpeg_dec_header_info_t info;
    +  jpeg_error_t err = jpeg_dec_parse_header(dec, &io, &info);
    +  if (err == JPEG_ERR_OK) {
    +    err = jpeg_dec_process(dec, &io);
    +  }
    +  // ... draw, then wait for the next frame time
    +}
    +```
    +
    +Each frame is shown for one frame period, timed with `esp_timer`. If decoding
    +falls behind, the player shows the next frame right away and counts it as late,
    +so the count tells you whether a quality or frame-rate setting is too much for
    +your chip.
    +
    +### RGB565 byte order
    +
    +QEMU's virtual panel takes RGB565 little-endian, hence
    +`JPEG_PIXEL_FORMAT_RGB565_LE`. Controllers such as the ST7789 and ILI9341 take

    `JPEG_PIXEL_FORMAT_RGB565_LE`. Common display controllers, such as the ST7789 and ILI9341, take
    

    f-hollow f-hollow commented on code on Nov 10, 2026 03:28 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."
    +authors:
    +  - "mark-santos"
    +tags:
    +  - Multimedia
    +  - JPEG
    +  - ESP32-S3
    +  - QEMU
    +  - community contribution
    +---
    +
    +A boot animation, a product demo loop or a short clip on a status screen all
    +come down to the same job: get video into a form a microcontroller can decode
    +fast and store cheaply. On an ESP32, a sequence of JPEG frames (MJPEG) is the
    +usual answer, because
    +[ESP_NEW_JPEG](https://components.espressif.com/components/espressif/esp_new_jpeg)
    +decodes baseline JPEG in software on every chip, and each frame decodes on its
    +own.
    +
    +I edit video for a living, so the FFmpeg side of this is familiar ground. This
    +article covers the whole path: preparing the frames with FFmpeg, packing them
    +into a data partition, and a small player that memory-maps the partition and
    +decodes with ESP_NEW_JPEG. Since timing on real hardware varies by chip and
    +panel, I tested correctness rather than speed, and I did it without a board:
    +Espressif's QEMU emulates an RGB framebuffer, so the decoded frames can be
    +captured and compared with FFmpeg's decode of the same files.
    +
    +The example project is ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    +[esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    +1.0.2, run in the QEMU 9.2.2 that ships in the `espressif/idf:v6.1` Docker
    +image. FFmpeg is 9.0.2. The FFmpeg behaviors below were also checked on FFmpeg
    +7.1.5.
    +
    +## Preparing the frames
    +
    +The test footage comes from FFmpeg itself, so anyone can regenerate it: six
    +seconds of a Mandelbrot zoom (fine detail, the worst case for JPEG) and four of
    +`testsrc2` (flat colors and sharp edges, like UI graphics), at 1280x720 and 30
    +fps.
    +
    +For a 320x240 panel at 15 fps, one command scales the video until it covers the
    +panel, crops the middle, drops the frame rate and encodes each frame as a JPEG:
    +
    +```bash
    +ffmpeg -i source.mp4 \
    +  -vf "scale=320:240:force_original_aspect_ratio=increase,crop=320:240,fps=15" \
    +  -an -c:v mjpeg -q:v 5 -pix_fmt yuv420p -f mjpeg frames.mjpeg
    +```
    +
    +`-f mjpeg` writes the JPEGs back to back with no container. `-pix_fmt yuv420p`
    +gives 4:2:0 chroma; the older `yuvj420p` produces a byte-identical file but
    +prints a deprecation warning. FFmpeg's MJPEG encoder stores full-range color
    +either way.
    +
    +`-q:v` sets the quality, from 2 (best) to 31. For the 10-second test clip at
    +320x240 and 15 fps, here is what each setting costs in flash, and the luma PSNR
    +of the decoded frames against the same frames before compression:
    +
    +| `-q:v` | 150 frames  | Per frame (avg) | PSNR, average | PSNR, worst frame |
    +| ------ | ----------- | --------------- | ------------- | ----------------- |
    +| 2      | 2,679,760 B | 17,865 B        | 45.1 dB       | 41.3 dB           |
    +| 3      | 2,177,344 B | 14,516 B        | 42.2 dB       | 38.1 dB           |
    +| 5      | 1,648,984 B | 10,993 B        | 38.7 dB       | 34.4 dB           |
    +| 8      | 1,208,992 B | 8,060 B         | 35.7 dB       | 31.2 dB           |
    +| 12     | 914,140 B   | 6,094 B         | 33.5 dB       | 29.0 dB           |
    +| 20     | 630,520 B   | 4,203 B         | 31.1 dB       | 26.6 dB           |
    +| 31     | 467,872 B   | 3,119 B         | 29.3 dB       | 25.1 dB           |
    +
    +The Mandelbrot frames are 13% (at `-q:v 31`) to 66% (at `-q:v 2`) larger than the
    +`testsrc2` frames, so judge quality on the kind of footage you will actually show. For a
    +3 MB partition, `-q:v 5` fits this clip with room to spare.
    +
    +## Packing frames into a partition
    +
    +The player needs to know where each frame starts. Searching the stream for the
    +JPEG start marker (`FF D8`) is tempting, but those bytes can also appear inside
    +a header segment, so `pack_video.py` walks the markers properly: it follows

    It is not clear where to find this and other scripts until you reach the end of the article.

    Maybe provide links?

    f-hollow f-hollow commented on code on Nov 10, 2026 03:31 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."
    +authors:
    +  - "mark-santos"
    +tags:
    +  - Multimedia
    +  - JPEG
    +  - ESP32-S3
    +  - QEMU
    +  - community contribution
    +---
    +
    +A boot animation, a product demo loop or a short clip on a status screen all
    +come down to the same job: get video into a form a microcontroller can decode
    +fast and store cheaply. On an ESP32, a sequence of JPEG frames (MJPEG) is the
    +usual answer, because
    +[ESP_NEW_JPEG](https://components.espressif.com/components/espressif/esp_new_jpeg)
    +decodes baseline JPEG in software on every chip, and each frame decodes on its
    +own.
    +
    +I edit video for a living, so the FFmpeg side of this is familiar ground. This
    +article covers the whole path: preparing the frames with FFmpeg, packing them
    +into a data partition, and a small player that memory-maps the partition and
    +decodes with ESP_NEW_JPEG. Since timing on real hardware varies by chip and
    +panel, I tested correctness rather than speed, and I did it without a board:
    +Espressif's QEMU emulates an RGB framebuffer, so the decoded frames can be
    +captured and compared with FFmpeg's decode of the same files.
    +
    +The example project is ESP-IDF v6.1 on an ESP32-S3, with ESP_NEW_JPEG 1.0.2 and
    +[esp_lcd_qemu_rgb](https://components.espressif.com/components/espressif/esp_lcd_qemu_rgb)
    +1.0.2, run in the QEMU 9.2.2 that ships in the `espressif/idf:v6.1` Docker
    +image. FFmpeg is 9.0.2. The FFmpeg behaviors below were also checked on FFmpeg
    +7.1.5.
    +
    +## Preparing the frames
    +
    +The test footage comes from FFmpeg itself, so anyone can regenerate it: six
    +seconds of a Mandelbrot zoom (fine detail, the worst case for JPEG) and four of
    +`testsrc2` (flat colors and sharp edges, like UI graphics), at 1280x720 and 30
    +fps.
    +
    +For a 320x240 panel at 15 fps, one command scales the video until it covers the
    +panel, crops the middle, drops the frame rate and encodes each frame as a JPEG:
    +
    +```bash
    +ffmpeg -i source.mp4 \
    +  -vf "scale=320:240:force_original_aspect_ratio=increase,crop=320:240,fps=15" \
    +  -an -c:v mjpeg -q:v 5 -pix_fmt yuv420p -f mjpeg frames.mjpeg
    +```
    +
    +`-f mjpeg` writes the JPEGs back to back with no container. `-pix_fmt yuv420p`
    +gives 4:2:0 chroma; the older `yuvj420p` produces a byte-identical file but
    +prints a deprecation warning. FFmpeg's MJPEG encoder stores full-range color
    +either way.
    +
    +`-q:v` sets the quality, from 2 (best) to 31. For the 10-second test clip at
    +320x240 and 15 fps, here is what each setting costs in flash, and the luma PSNR
    +of the decoded frames against the same frames before compression:
    +
    +| `-q:v` | 150 frames  | Per frame (avg) | PSNR, average | PSNR, worst frame |
    +| ------ | ----------- | --------------- | ------------- | ----------------- |
    +| 2      | 2,679,760 B | 17,865 B        | 45.1 dB       | 41.3 dB           |
    +| 3      | 2,177,344 B | 14,516 B        | 42.2 dB       | 38.1 dB           |
    +| 5      | 1,648,984 B | 10,993 B        | 38.7 dB       | 34.4 dB           |
    +| 8      | 1,208,992 B | 8,060 B         | 35.7 dB       | 31.2 dB           |
    +| 12     | 914,140 B   | 6,094 B         | 33.5 dB       | 29.0 dB           |
    +| 20     | 630,520 B   | 4,203 B         | 31.1 dB       | 26.6 dB           |
    +| 31     | 467,872 B   | 3,119 B         | 29.3 dB       | 25.1 dB           |
    +
    +The Mandelbrot frames are 13% (at `-q:v 31`) to 66% (at `-q:v 2`) larger than the
    +`testsrc2` frames, so judge quality on the kind of footage you will actually show. For a
    +3 MB partition, `-q:v 5` fits this clip with room to spare.
    +
    +## Packing frames into a partition
    +
    +The player needs to know where each frame starts. Searching the stream for the
    +JPEG start marker (`FF D8`) is tempting, but those bytes can also appear inside
    +a header segment, so `pack_video.py` walks the markers properly: it follows
    +segment lengths up to the start of scan, then skips the entropy-coded data to
    +the end marker. Inside entropy-coded data, a real `FF` byte is always followed
    +by `00` or a restart marker, so the end marker can't be mistaken.
    +
    +It writes a small header, a frame index and the frames, each frame on a 4-byte
    +boundary:
    +
    +```text
    +offset 0   char[4]  "MJV1"
    +       4   u16      panel width
    +       6   u16      panel height
    +       8   u16      fps numerator
    +      10   u16      fps denominator
    +      12   u32      frame count (N)
    +      16   N x {u32 offset, u32 size}
    +           JPEG frames
    +```
    +
    +The partition table gives the video its own data partition, so the video can be
    +replaced without rebuilding the app:
    +
    +```csv
    +# Name,   Type, SubType, Offset,   Size
    +nvs,      data, nvs,     0x9000,   0x6000
    +factory,  app,  factory, 0x10000,  0xF0000
    +video,    data, 0x40,    0x100000, 0x300000
    +```
    +
    +## The player
    +
    +The player maps the whole partition into the address space and hands each
    +frame to the decoder straight from flash, so no frame is copied into RAM before
    +decoding:
    +
    +```c
    +const esp_partition_t *part =
    +    esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 0x40, "video");
    +const void *map;
    +esp_partition_mmap_handle_t map_handle;
    +ESP_ERROR_CHECK(esp_partition_mmap(part, 0, part->size, ESP_PARTITION_MMAP_DATA,
    +                                   &map, &map_handle));
    +const video_t *v = map;
    +```
    +
    +The decoder writes RGB565 into one buffer, which must be 16-byte aligned:
    +
    +```c
    +jpeg_dec_config_t config = DEFAULT_JPEG_DEC_CONFIG();
    +config.output_type = JPEG_PIXEL_FORMAT_RGB565_LE;
    +jpeg_dec_handle_t dec;
    +jpeg_dec_open(&config, &dec);
    +uint8_t *pixels = jpeg_calloc_align(frame_bytes, 16);
    +
    +for (uint32_t i = 0; i < v->count; i++) {
    +  jpeg_dec_io_t io = {
    +      .inbuf = (uint8_t *)map + v->frames[i].offset,
    +      .inbuf_len = v->frames[i].size,
    +      .outbuf = pixels,
    +  };
    +  jpeg_dec_header_info_t info;
    +  jpeg_error_t err = jpeg_dec_parse_header(dec, &io, &info);
    +  if (err == JPEG_ERR_OK) {
    +    err = jpeg_dec_process(dec, &io);
    +  }
    +  // ... draw, then wait for the next frame time
    +}
    +```
    +
    +Each frame is shown for one frame period, timed with `esp_timer`. If decoding
    +falls behind, the player shows the next frame right away and counts it as late,
    +so the count tells you whether a quality or frame-rate setting is too much for
    +your chip.
    +
    +### RGB565 byte order
    +
    +QEMU's virtual panel takes RGB565 little-endian, hence
    +`JPEG_PIXEL_FORMAT_RGB565_LE`. Controllers such as the ST7789 and ILI9341 take
    +each 16-bit pixel high byte first over SPI (see their datasheets), which is
    +what `JPEG_PIXEL_FORMAT_RGB565_BE` produces. If colors look wrong on a real
    +panel, check this first.
    +
    +## Odd panel sizes
    +
    +Some popular panels have an odd side: the 1.14" ST7789 modules are 135x240. Ask
    +FFmpeg for a 135x240 crop with 4:2:0 chroma and it silently produces 134x240
    +frames, because 4:2:0 halves the chroma resolution both ways, so it can only
    +represent even sizes (4:2:2 has the same limit across). The first version of
    +the packer recorded 135 in the header while every frame said 134, and the
    +player rejected all of them.
    +
    +`pack_video.py` now rounds the frame size up to the next even number, so a
    +135x240 panel gets 136x240 frames, and the player drops the extra column after
    +decoding by packing the rows tightly in place:
    +
    +```c
    +if (info.width > v->width) {
    +  for (int row = 1; row < v->height; row++) {
    +    memmove(pixels + row * v->width * 2, pixels + row * info.width * 2, v->width * 2);
    +  }
    +}
    +```
    +
    +{{< figure src="img/qemu-frame-135x240.webp" alt="Fig. 1 - The last frame on a 135x240 virtual panel, decoded from a 136x240 JPEG with the extra column dropped" >}}
    +
    +Avoiding chroma subsampling would avoid the rounding, but that path is closed
    +too: FFmpeg's 4:4:4 MJPEG uses 1x2 sampling factors on every component, and
    +ESP_NEW_JPEG 1.0.2 rejects those frames with `JPEG_ERR_UNSUPPORT_FMT`. 4:2:0
    +decoded at every size I tried: 130, 132, 136, 138, 140 and 144 pixels wide, and
    +236, 238 and 240 tall.
    +
    +## Testing without a board
    +
    +Espressif's QEMU emulates an RGB framebuffer (a virtual device, not a real
    +peripheral) on the ESP32, ESP32-S3 and ESP32-C3, and `esp_lcd_qemu_rgb` drives
    +it like any other `esp_lcd` panel. Build, then merge the bootloader, partition
    +table, app and video into one flash image:
    +
    +```bash
    +idf.py build
    +(cd build && python -m esptool --chip esp32s3 merge-bin --fill-flash-size 4MB \
    +  -o ../flash.bin @flash_args 0x100000 ../video.bin)
    +qemu-system-xtensa -nographic -machine esp32s3 \
    +  -drive file=flash.bin,if=mtd,format=raw
    +```
    +
    +Two details kept it from working on the first try.
    +
    +**Headless, `draw_bitmap` never returns.** The driver hands the frame to the
    +virtual device and waits until QEMU's display picks it up. With `-nographic`
    +there is no display, so nothing picks it up, and the player hung after the
    +first decode. The QEMU monitor's `screendump` command forces that update and
    +saves the screen to a file, so the example runs QEMU with a monitor socket and
    +a small script that keeps sending `screendump`:
    +
    +```bash
    +qemu-system-xtensa -nographic -machine esp32s3 \
    +  -drive file=flash.bin,if=mtd,format=raw \
    +  -monitor unix:/tmp/qemu-mon.sock,server,nowait -serial file:qemu.log
    +```
    +
    +This also yields screenshots of exactly the frames you want. The player prints
    +`SHOT n` after drawing frame `n` and pauses, and the script saves a
    +`screendump` at that moment. Note that `-monitor` takes the serial console off
    +standard output, hence `-serial file:`.
    +
    +**An odd-width virtual panel never refreshes.** QEMU builds its 16-bit screen
    +with a row stride of width x 2 bytes. At 135 pixels (270 bytes per row) the
    +virtual panel never refreshed, and `draw_bitmap` hung again; at 136 it worked.
    +For the 135x240 test, the player gives the virtual panel one spare column and
    +still draws 135-pixel rows. A real 135-pixel panel has no such
    +limit; this is purely an emulator detail.
    +
    +### Checking the frames
    +
    +For three frames (the first, a Mandelbrot frame and the last, a `testsrc2`
    +frame), the player prints its decoded pixels over the serial port, and
    +`compare_frames.py` compares them with FFmpeg's decode of the same JPEG,
    +reduced to RGB565 the same way:
    +
    +| Panel   | Frame 0 | Frame 75 | Frame 149 |
    +| ------- | ------- | -------- | --------- |
    +| 320x240 | 39.2 dB | 38.2 dB  | 46.0 dB   |
    +| 135x240 | 38.7 dB | 37.6 dB  | 47.0 dB   |
    +
    +These are PSNR values between two decoders of the same file. They differ in
    +rounding, so they don't match exactly, but in all six frames every color
    +channel of every pixel is within one RGB565 step of FFmpeg's decode. In every case the QEMU screen capture was
    +identical to the decoded pixels, byte for byte, and all 150 frames played on
    +both panels.
    +
    +{{< figure src="img/qemu-frames-320x240.webp" alt="Fig. 2 - Frames 75 and 149 of the test clip on QEMU's virtual 320x240 panel, saved with the monitor's screendump command" >}}
    +
    +What QEMU can't tell you is speed: the emulator runs at its own pace, so every
    +frame counted as late there. Measure decode time on your board with the same
    +player, and pick the quality and frame rate from that.
    +
    +## Conclusion
    +
    +FFmpeg does most of the work here, and a few flags decide whether the result
    +fits the panel and the flash: scale and crop to the exact panel size, drop the
    +frame rate early, pick `-q:v` from a size table rather than by eye, and keep to
    +4:2:0. Odd panel sizes need one extra pixel and a few lines of C. Running the
    +player in QEMU, with the screen captured and compared against FFmpeg, catches
    +mistakes in all of those steps before any hardware is involved.
    +
    +The complete code is below: the packer, the player, the QEMU runner and the
    +comparison script, as they ran for this article.
    +
    +## Full listings

    It is not very easy to navigate to every script and recreate local files.

    I highly recommend that you place all the scripts in a GitHub repo. Then obtaining all scripts will be as easy as cloning the repo.

    Also, I will have a chance to easily update the scripts if needed in the future.

    f-hollow f-hollow commented on code on Nov 10, 2026 03:45 UTC
    Code Review
    content/blog/2026/11/preparing-video-for-esp32-displays/index.md:1
    @@ -0,0 +1,760 @@
    +---
    +title: "Preparing Video for ESP32 Displays with FFmpeg, Tested in QEMU"
    +date: "2026-11-20"
    +summary: "How to turn a video into MJPEG frames that fit an ESP32 display and its flash, pack them into a data partition, and play them with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU, with no board: the decoded frames are compared with FFmpeg's decode of the same JPEGs, and the article covers the FFmpeg, decoder and QEMU details that broke along the way."

    that fit an ESP32 display

    To my knowledge, there is no such thing as an ESP32 display. Also, the wording in a few other places can be more optimized.

    Maybe rephrase it to something like this:

    summary: "How to turn a video into MJPEG frames sized for an ESP32-driven display, pack them into a flash data partition, and play them back with ESP_NEW_JPEG. Every step is checked in Espressif's QEMU without hardware by comparing decoded frames against FFmpeg's output. The article covers the FFmpeg, decoder, and QEMU issues encountered along the way."
    

    f-hollow f-hollow commented on Nov 10, 2026 03:52 UTC
    Reviewer

    @markksantos Thank you very much for your contribution!

    The article looks great. Please add some links, improve the wording in a couple of places, and consider moving the scripts into a GitHub repo.

    f-hollow f-hollow commented on code on Nov 10, 2026 03:58 UTC
    Code Review
    data/authors/mark-santos.json:1
    @@ -0,0 +1,14 @@
    +{
    +    "name": "Mark Santos",
    +    "type": "community",
    +    "image": "img/authors/espressif.webp",

    If you want to use a default image, please use a community author default.

        "image": "img/authors/community-light.webp",
    

    markksantos markksantos commented on Nov 10, 2026 04:27 UTC
    Author

    Thanks for the review! Pushed 01a8bed:

  • Linked ESP-IDF v6.1, Espressif's QEMU, the espressif/idf:v6.1 Docker image and esp_timer.

  • Applied your summary and display-controller wording suggestions.

  • Added a line near the top saying where the scripts are (Full listings at the end).

  • Switched the author image to img/authors/community-light.webp.
  • On a repo for the scripts: agreed. Would espressif/developer-portal-codebase work, under content/blog/2026/11/preparing-video-for-esp32-displays/ with a README covering the build and QEMU steps? Then you can update them in one place. If that suits you, I'll open a PR there and replace the Full listings section with a link to that folder.

    f-hollow f-hollow commented on Nov 10, 2026 08:30 UTC
    Contributor

    @markksantos Regarding where to possibly upload the scripts, it would be better if you could store them in your repository. This is the code that you created and own. You might want to further improve or maintain it in the future.

    If you don't have a chance to create a repo, let's keep the code as part of the article.

    markksantos markksantos commented on Nov 10, 2026 08:33 UTC
    Author

    Thanks, let's keep the code with the article then, so everything stays on one page.

    To make the files easier to recreate, 6e732d3 opens Full listings with the folder layout and the commands that run it, and each listing's title now shows its path (for example mjpeg_player/main/main.c). The pointer near the top mentions the layout too.

    f-hollow f-hollow commented on Nov 10, 2026 09:20 UTC
    Contributor

    @pedrominatel PTAL

    pedrominatel pedrominatel approved these changes on Nov 10, 2026 15:15 UTC
    Approved Reviewer

    @f-hollow, LGTM. Merging on Nov 20th is ok.
    Thank you, @markksantos, for your contribution.

    markksantos markksantos commented on Nov 10, 2026 16:06 UTC
    Author

    Thanks, @pedrominatel and @f-hollow. The article is already dated 2026-11-20, so it lines up with the merge.

    f-hollow f-hollow approved these changes on Nov 12, 2026 05:35 UTC
    Approved Reviewer

    Approved these changes.

    Reviewers
    Assignees
    markksantos markksantos
    Labels
    risk: low confidence: 85.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 85.9% · Lower = rushed or shallow review
    85.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.1% · Safe to merge
    7.1%