ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
esp32_camera_jpeg_encoder.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32_CAMERA_JPEG_ENCODER
4
6
8
9static const char *const TAG = "camera_encoder";
10
12 this->quality_ = quality;
13 this->output_ = output;
14}
15
17 this->bytes_written_ = 0;
18 this->out_of_output_memory_ = false;
19 bool success = fmt2jpg_cb(pixels->get_data_buffer(), pixels->get_data_length(), spec->width, spec->height,
20 to_internal_(spec->format), this->quality_, callback, this);
21
22 if (!success)
24
25 if (this->out_of_output_memory_) {
26 if (this->buffer_expand_size_ <= 0)
28
29 size_t current_size = this->output_->get_max_size();
30 size_t new_size = this->output_->get_max_size() + this->buffer_expand_size_;
31 if (!this->output_->set_buffer_size(new_size)) {
32 ESP_LOGE(TAG, "Failed to expand output buffer.");
33 this->buffer_expand_size_ = 0;
35 }
36
37 ESP_LOGD(TAG, "Output buffer expanded (%u -> %u).", current_size, this->output_->get_max_size());
39 }
40
43}
44
46 ESP_LOGCONFIG(TAG,
47 "ESP32 Camera JPEG Encoder:\n"
48 " Size: %zu\n"
49 " Quality: %d\n"
50 " Expand: %d\n",
51 this->output_->get_max_size(), this->quality_, this->buffer_expand_size_);
52}
53
54size_t ESP32CameraJPEGEncoder::callback(void *arg, size_t index, const void *data, size_t len) {
55 ESP32CameraJPEGEncoder *that = reinterpret_cast<ESP32CameraJPEGEncoder *>(arg);
56 uint8_t *buffer = that->output_->get_data();
57 size_t buffer_length = that->output_->get_max_size();
58 if (index + len > buffer_length) {
59 that->out_of_output_memory_ = true;
60 return 0;
61 }
62
63 std::memcpy(&buffer[index], data, len);
64 that->bytes_written_ += len;
65 return len;
66}
67
69 switch (format) {
71 return PIXFORMAT_GRAYSCALE;
73 return PIXFORMAT_RGB565;
74 // Internal representation for RGB is in byte order: B, G, R
76 return PIXFORMAT_RGB888;
77 }
78
79 return PIXFORMAT_GRAYSCALE;
80}
81
82} // namespace esphome::camera_encoder
83
84#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.
static size_t callback(void *arg, size_t index, const void *data, size_t len)
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)
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:291
Specification of a caputured camera image.
Definition camera.h:69