ESPHome 2025.9.1
Loading...
Searching...
No Matches
esp32_camera_jpeg_encoder.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32_CAMERA_JPEG_ENCODER
2
4
6
7static const char *const TAG = "camera_encoder";
8
10 this->quality_ = quality;
11 this->output_ = output;
12}
13
15 this->bytes_written_ = 0;
16 this->out_of_output_memory_ = false;
17 bool success = fmt2jpg_cb(pixels->get_data_buffer(), pixels->get_data_length(), spec->width, spec->height,
18 to_internal_(spec->format), this->quality_, callback_, this);
19
20 if (!success)
22
23 if (this->out_of_output_memory_) {
24 if (this->buffer_expand_size_ <= 0)
26
27 size_t current_size = this->output_->get_max_size();
28 size_t new_size = this->output_->get_max_size() + this->buffer_expand_size_;
29 if (!this->output_->set_buffer_size(new_size)) {
30 ESP_LOGE(TAG, "Failed to expand output buffer.");
31 this->buffer_expand_size_ = 0;
33 }
34
35 ESP_LOGD(TAG, "Output buffer expanded (%u -> %u).", current_size, this->output_->get_max_size());
37 }
38
41}
42
44 ESP_LOGCONFIG(TAG,
45 "ESP32 Camera JPEG Encoder:\n"
46 " Size: %zu\n"
47 " Quality: %d\n"
48 " Expand: %d\n",
49 this->output_->get_max_size(), this->quality_, this->buffer_expand_size_);
50}
51
52size_t ESP32CameraJPEGEncoder::callback_(void *arg, size_t index, const void *data, size_t len) {
53 ESP32CameraJPEGEncoder *that = reinterpret_cast<ESP32CameraJPEGEncoder *>(arg);
54 uint8_t *buffer = that->output_->get_data();
55 size_t buffer_length = that->output_->get_max_size();
56 if (index + len > buffer_length) {
57 that->out_of_output_memory_ = true;
58 return 0;
59 }
60
61 std::memcpy(&buffer[index], data, len);
62 that->bytes_written_ += len;
63 return len;
64}
65
67 switch (format) {
69 return PIXFORMAT_GRAYSCALE;
71 return PIXFORMAT_RGB565;
72 // Internal representation for RGB is in byte order: B, G, R
74 return PIXFORMAT_RGB888;
75 }
76
77 return PIXFORMAT_GRAYSCALE;
78}
79
80} // namespace esphome::camera_encoder
81
82#endif
Interface for a generic buffer that stores image data.
Definition buffer.h:9
virtual size_t get_data_length()=0
Returns the length of the buffer in bytes.
virtual uint8_t * get_data_buffer()=0
Returns a pointer to the buffer's data.
Interface for an encoder buffer supporting resizing and variable-length data.
Definition encoder.h:32
virtual size_t get_max_size() const =0
Returns total allocated buffer size.
virtual uint8_t * get_data() const =0
Returns a pointer to the buffer data.
virtual bool set_buffer_size(size_t size)=0
Sets logical buffer size, reallocates if needed.
Encoder that uses the software-based JPEG implementation from Espressif's esp32-camera component.
camera::EncoderError encode_pixels(camera::CameraImageSpec *spec, camera::Buffer *pixels) override
ESP32CameraJPEGEncoder(uint8_t quality, camera::EncoderBuffer *output)
Constructs a ESP32CameraJPEGEncoder instance.
pixformat_t to_internal_(camera::PixelFormat format)
static size_t callback_(void *arg, size_t index, const void *data, size_t len)
EncoderError
Result codes from the encoder used to control camera pipeline flow.
Definition encoder.h:9
@ ENCODER_ERROR_RETRY_FRAME
Retry current frame, after buffer growth or for incremental encoding.
Definition encoder.h:12
@ ENCODER_ERROR_SUCCESS
Encoding succeeded, continue pipeline normally.
Definition encoder.h:10
@ ENCODER_ERROR_CONFIGURATION
Fatal config error, shut down pipeline.
Definition encoder.h:13
@ ENCODER_ERROR_SKIP_FRAME
Skip current frame, try again on next frame.
Definition encoder.h:11
PixelFormat
Enumeration of different pixel formats.
Definition camera.h:19
@ PIXEL_FORMAT_RGB565
16-bit RGB (5-6-5).
Definition camera.h:21
@ PIXEL_FORMAT_GRAYSCALE
8-bit grayscale.
Definition camera.h:20
@ PIXEL_FORMAT_BGR888
RGB pixel data in 8-bit format, stored as B, G, R (1 byte each).
Definition camera.h:22
std::string size_t len
Definition helpers.h:280
Specification of a caputured camera image.
Definition camera.h:69