Skip to content

Allow custom JPEG frame buffer sizes#850

Open
cccat6 wants to merge 2 commits into
espressif:masterfrom
cccat6:agent/custom-jpeg-frame-buffer-size
Open

Allow custom JPEG frame buffer sizes#850
cccat6 wants to merge 2 commits into
espressif:masterfrom
cccat6:agent/custom-jpeg-frame-buffer-size

Conversation

@cccat6

@cccat6 cccat6 commented Jul 7, 2026

Copy link
Copy Markdown

Summary

Add an optional camera_config_t.jpeg_buffer_size field for overriding the frame buffer allocation size in JPEG mode.

When jpeg_buffer_size is 0, the existing automatic or Kconfig-based buffer sizing behavior is preserved.

Motivation

JPEG output size depends on image content, sensor quality settings, and compression efficiency. The existing automatic allocation derives the buffer size from the selected resolution, which can create large jumps between adjacent frame size presets.

For example, with the automatic / 5 sizing rule:

  • SVGA (800x600) allocates approximately 96,000 bytes
  • XGA (1024x768) allocates approximately 157,286 bytes

On devices without PSRAM, the smaller allocation may not fit some JPEG frames, while the next allocation may require more contiguous internal memory than the application can provide.

A measured intermediate value such as 128 KiB can be sufficient for the expected JPEG output while leaving enough internal memory for networking, HTTP, MQTT, and the rest of the application.

What changed

  • Added size_t jpeg_buffer_size to camera_config_t.
  • A non-zero value overrides the JPEG receive and frame buffer size.
  • A value of 0 preserves the existing automatic or CONFIG_CAMERA_JPEG_MODE_FRAME_SIZE behavior.
  • frame_size remains a framesize_t and continues to represent image resolution.
  • Non-JPEG buffer allocation is unchanged.
  • Updated the README and camera example to document the new option.

Example

camera_config_t config = {
    .pixel_format = PIXFORMAT_JPEG,
    .frame_size = FRAMESIZE_UXGA,
    .jpeg_quality = 24,
    .fb_count = 1,
    .jpeg_buffer_size = 128 * 1024,
};

This captures UXGA images while allocating a 128 KiB JPEG frame buffer.

Compatibility

Existing behavior is unchanged when jpeg_buffer_size is 0.

Aggregate and designated initializers that omit the new trailing field initialize it to zero, preserving the previous allocation behavior.

Validation

  • Ran git diff --check.
  • Ran codespell on the changed files.
  • Built the complete downstream Lander ESP32-S3 Arduino project against the modified esp_camera.c and cam_hal.c.
  • Confirmed through the linker map that the patched camera sources were used by the downstream build.
  • Target configuration: ESP32S3 Dev Module, ESP32-S3 N8, 8 MB flash, internal DRAM only, no PSRAM.

@me-no-dev

Copy link
Copy Markdown
Member

frame size and buffer size are two very different things. If you want to be able to set a custom frame buffer size, then that should be a separate option in the camera config.

@cccat6

cccat6 commented Jul 14, 2026

Copy link
Copy Markdown
Author

frame size and buffer size are two very different things. If you want to be able to set a custom frame buffer size, then that should be a separate option in the camera config.

Hello,
Yes I know what you mean. However, the frame_size currently represent the frame buffer size. It is a enum. There is no other dedicated option or parameter to set the frame buffer size as I need.
If we want to be clear about the naming, here might should not be named as frame_size because it is not true in JPEG settings.
Thanks for your reply. Please let mw know what you think.

@cccat6

cccat6 commented Jul 14, 2026

Copy link
Copy Markdown
Author

frame size and buffer size are two very different things. If you want to be able to set a custom frame buffer size, then that should be a separate option in the camera config.

Would you mean I can add a new paraneter frame_buffer_size. If it is set, it will overwritre the auto frame_buffer option? Please let me know if I am wrong. I can do that for sure.

@me-no-dev

Copy link
Copy Markdown
Member

frame_size represents the resolution of the image, frame buffer size is the size that the image with that resolution will take. It depends on the frame_format what that size would be, and for JPEG is a middle ground number that is not too big, but would fit 99% of the images with the given resolution.

@me-no-dev

Copy link
Copy Markdown
Member

Would you mean I can add a new paraneter frame_buffer_size. If it is set, it will overwritre the auto frame_buffer

Yes. Maybe name it jpeg_buffer_size to be clear that it is only for JPEG

@cccat6

cccat6 commented Jul 14, 2026

Copy link
Copy Markdown
Author

Would you mean I can add a new paraneter frame_buffer_size. If it is set, it will overwritre the auto frame_buffer

Yes. Maybe name it jpeg_buffer_size to be clear that it is only for JPEG

Thank you. Will do shortly. I will renew this PR after that. Thanks again!

@cccat6

cccat6 commented Jul 14, 2026

Copy link
Copy Markdown
Author

Would you mean I can add a new paraneter frame_buffer_size. If it is set, it will overwritre the auto frame_buffer

Yes. Maybe name it jpeg_buffer_size to be clear that it is only for JPEG

Hi there,
I have updated the PR with jpeg_buffer_size and tested on my ESP32-S3-N8. Please let me know if there are any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants