ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
jpeg_encoder.cpp
Go to the documentation of this file.
1#include "jpeg_encoder.h"
2
3#ifdef USE_ESP32_VARIANT_ESP32P4
4
5#include "esphome/core/log.h"
6
8
9ESPHOME_LOG_TAG(TAG, "mipi_csi.jpeg");
10
12static constexpr int ENCODE_TIMEOUT_MS = 5000;
13
14bool JpegEncoder::init(uint16_t width, uint16_t height, size_t frame_size) {
15 jpeg_encode_engine_cfg_t engine_config{};
16 engine_config.timeout_ms = ENCODE_TIMEOUT_MS;
17 esp_err_t err = jpeg_new_encoder_engine(&engine_config, &this->engine_);
18 if (err != ESP_OK) {
19 ESP_LOGE(TAG, "Encoder engine setup failed: %s", esp_err_to_name(err));
20 return false;
21 }
22
23 jpeg_encode_memory_alloc_cfg_t memory_config{};
24 memory_config.buffer_direction = JPEG_ENC_ALLOC_OUTPUT_BUFFER;
25 this->output_ = static_cast<uint8_t *>(jpeg_alloc_encoder_mem(frame_size, &memory_config, &this->output_size_));
26 if (this->output_ == nullptr) {
27 ESP_LOGE(TAG, "Failed to allocate %zu byte JPEG output buffer", frame_size);
28 return false;
29 }
30
31 this->config_.width = width;
32 this->config_.height = height;
33 return true;
34}
35
36size_t JpegEncoder::encode(const uint8_t *frame, size_t frame_length) {
37 uint32_t encoded_length = 0;
38 esp_err_t err = jpeg_encoder_process(this->engine_, &this->config_, frame, frame_length, this->output_,
39 this->output_size_, &encoded_length);
40 if (err != ESP_OK) {
41 ESP_LOGW(TAG, "Encoding frame failed: %s", esp_err_to_name(err));
42 return 0;
43 }
44 return encoded_length;
45}
46
47} // namespace esphome::mipi_csi
48
49#endif
jpeg_encoder_handle_t engine_
bool init(uint16_t width, uint16_t height, size_t frame_size)
Allocates the encoder engine and its output buffer.
size_t encode(const uint8_t *frame, size_t frame_length)
Encodes one frame into the internal output buffer.
ESPHOME_LOG_TAG(TAG, "mipi_csi.jpeg")
static void uint32_t