Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ports/espressif/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cmake_minimum_required(VERSION 3.16)
set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)

# The component list here determines what options we get in menuconfig and what the ninja file can build.
set(COMPONENTS bt driver esp_driver_dac esp_driver_gpio esp_driver_gptimer esp_driver_i2c esp_driver_i2s esp_driver_ledc esp_driver_pcnt esp_driver_rmt esp_driver_sdmmc esp_driver_spi esp_driver_touch_sens esp_driver_tsens esp_driver_uart esp_eth esp-tls esp_adc esp_adc_cal esp_event esp_netif esp_psram esp_security esp_wifi esptool_py freertos log lwip main mbedtls mdns pthread soc ulp wpa_supplicant esp-camera esp_lcd vfs esp_vfs_console esp_stdio sdmmc usb)
set(COMPONENTS bt driver esp_driver_dac esp_driver_gpio esp_driver_gptimer esp_driver_i2c esp_driver_i2s esp_driver_jpeg esp_driver_ledc esp_driver_pcnt esp_driver_rmt esp_driver_sdmmc esp_driver_spi esp_driver_touch_sens esp_driver_tsens esp_driver_uart esp_eth esp-tls esp_adc esp_adc_cal esp_event esp_netif esp_psram esp_security esp_wifi esptool_py freertos log lwip main mbedtls mdns pthread soc ulp wpa_supplicant esp-camera esp_lcd vfs esp_vfs_console esp_stdio sdmmc usb)
set(EXTRA_COMPONENT_DIRS "esp-protocols/components/mdns" "esp-camera")

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
Expand Down
15 changes: 15 additions & 0 deletions ports/espressif/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ INC += \
-isystem esp-idf/components/esp_driver_gptimer/include \
-isystem esp-idf/components/esp_driver_i2c/include \
-isystem esp-idf/components/esp_driver_i2s/include \
-isystem esp-idf/components/esp_driver_jpeg/include \
-isystem esp-idf/components/esp_driver_ledc/include \
-isystem esp-idf/components/esp_driver_parlio/include \
-isystem esp-idf/components/esp_driver_pcnt/include \
Expand Down Expand Up @@ -119,6 +120,8 @@ INC += \
-isystem esp-idf/components/esp_hal_i2c/include \
-isystem esp-idf/components/esp_hal_i2s/include \
-isystem esp-idf/components/esp_hal_i2s/$(IDF_TARGET)/include \
-isystem esp-idf/components/esp_hal_jpeg/include \
-isystem esp-idf/components/esp_hal_jpeg/$(IDF_TARGET)/include \
-isystem esp-idf/components/esp_hal_ledc/include \
-isystem esp-idf/components/esp_hal_pcnt/include \
-isystem esp-idf/components/esp_hal_rmt/include \
Expand Down Expand Up @@ -226,6 +229,13 @@ REGISTRATION_FUNCTIONS = \
-u esp_security_init_include_impl \
-u mbedtls_psa_crypto_init_include_impl

# JPEG engine DMA destinations come from the Python heap (Bitmaps), and
# the cache line is 64 bytes on the P4 and S31, so make GC blocks 64 bytes as well.
# supervisor/port.c aligns the PSRAM heap areas to the same value.
ifneq ($(filter esp32p4 esp32s31,$(IDF_TARGET)),)
CFLAGS += -DMICROPY_BYTES_PER_GC_BLOCK=64
endif
Comment on lines +232 to +237

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this below twice for each platform. That's the style we have.


# RISC-V picolibc ships optimized memcpy/memset/etc. that need forcing
# (gated by CONFIG_LIBC_OPTIMIZED_MISALIGNED_ACCESS in IDF's esp_libc CMake).
ifeq ($(IDF_TARGET_ARCH),riscv)
Expand Down Expand Up @@ -815,6 +825,11 @@ SRC_ULP := \
SRC_C += $(SRC_ULP)
endif

ifneq ($(CIRCUITPY_JPEGIO_HW),0)
SRC_C += common-hal/jpegio/JpegDecoder.c
CHIP_COMPONENTS += esp_driver_jpeg esp_hal_jpeg
endif

ifneq ($(CIRCUITPY_NEOPIXEL_WRITE),0)
CHIP_COMPONENTS += esp_driver_rmt
endif
Expand Down
194 changes: 194 additions & 0 deletions ports/espressif/common-hal/jpegio/JpegDecoder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2026 Mikey Sklar
//
// SPDX-License-Identifier: MIT

// Hardware JPEG decode for jpegio, on chips with SOC_JPEG_DECODE_SUPPORTED.
// The engine decodes a whole baseline JPEG to RGB565 with no scaling, so only
// decode() calls with scale=0 into a 16-bit Bitmap take this path. Anything the
// hardware does not handle returns false and jpegio falls back to TJpgDec.

#include "py/runtime.h"

#include "shared-bindings/bitmaptools/__init__.h"
#include "shared-module/displayio/Bitmap.h"
#include "shared-module/jpegio/JpegDecoder.h"

#include "driver/jpeg_decode.h"
#include "esp_cache.h"
#include "esp_heap_caps.h"
#include "esp_memory_utils.h"

// A bad image blocks the engine for at most this long.
#define JPEG_DECODE_TIMEOUT_MS (1000)

void jpegio_jpegdecoder_deinit_hw(jpegio_jpegdecoder_obj_t *self) {
if (self->hw_decoder != NULL) {
jpeg_del_decoder_engine(self->hw_decoder);
self->hw_decoder = NULL;
}
}

// GC blocks start on a cache line, so the engine can write Bitmap pixels directly.
#ifdef CONFIG_CACHE_L2_CACHE_LINE_SIZE
_Static_assert(MICROPY_BYTES_PER_GC_BLOCK % CONFIG_CACHE_L2_CACHE_LINE_SIZE == 0, "GC block is not a cache line");
#endif
#ifdef CONFIG_CACHE_L1_DCACHE_LINE_SIZE
_Static_assert(MICROPY_BYTES_PER_GC_BLOCK % CONFIG_CACHE_L1_DCACHE_LINE_SIZE == 0, "GC block is not a cache line");
#endif

// True if the hardware may write `size` bytes at `buf` directly: the start and
// length must both be whole cache lines.
static bool output_aligned(const void *buf, size_t size) {
size_t align = esp_cache_get_line_size_by_addr(buf);
if (align < 4) {
align = 4;
}
return ((uintptr_t)buf & (align - 1)) == 0 && (size & (align - 1)) == 0;
}

bool jpegio_jpegdecoder_decode_hw(jpegio_jpegdecoder_obj_t *self, const mp_buffer_info_t *jpeg, int scale) {
displayio_bitmap_t *bitmap = self->dest;
if (scale != 0 || bitmap->bits_per_value != 16 || bitmap->read_only) {
return false;
}
// Skip indices are slower on this path than in TJpgDec.
if (!self->skip_source_index_none || !self->skip_dest_index_none) {
return false;
}
// The engine reads the input by DMA, so it must be in DMA-capable memory.
if (!esp_ptr_dma_capable(jpeg->buf) && !esp_ptr_dma_ext_capable(jpeg->buf)) {
return false;
}

jpeg_decode_picture_info_t info;
if (jpeg_decoder_get_info(jpeg->buf, jpeg->len, &info) != ESP_OK) {
return false;
}

// The engine pads its output to whole MCUs: 8x8 for 4:4:4, 16x8 for 4:2:2
// and 16x16 for 4:2:0. Grayscale is left to TJpgDec.
uint32_t mcu_width, mcu_height;
switch (info.sample_method) {
case JPEG_DOWN_SAMPLING_YUV444:
mcu_width = 8;
mcu_height = 8;
break;
case JPEG_DOWN_SAMPLING_YUV422:
mcu_width = 16;
mcu_height = 8;
break;
case JPEG_DOWN_SAMPLING_YUV420:
mcu_width = 16;
mcu_height = 16;
break;
default:
return false;
}
uint32_t width = info.width;
uint32_t height = info.height;
// The driver rejects images whose pixel count is not a multiple of 8.
if ((width * height) % 8 != 0) {
return false;
}
uint32_t output_width = (width + mcu_width - 1) / mcu_width * mcu_width;
uint32_t output_height = (height + mcu_height - 1) / mcu_height * mcu_height;
size_t output_size = output_width * output_height * sizeof(uint16_t);

// Decode straight into the Bitmap when it is exactly the image, unpadded and
// cache-line aligned, and the whole image lands at 0,0.
bool direct = width == output_width && height == output_height &&
bitmap->width == width && bitmap->height == height &&
self->x == 0 && self->y == 0 &&
self->lim.x1 <= 0 && self->lim.y1 <= 0 &&
self->lim.x2 >= (int)width && self->lim.y2 >= (int)height &&
output_aligned(bitmap->data, output_size);

if (self->hw_decoder == NULL) {
jpeg_decode_engine_cfg_t engine_config = {
.timeout_ms = JPEG_DECODE_TIMEOUT_MS,
};
jpeg_decoder_handle_t engine;
if (jpeg_new_decoder_engine(&engine_config, &engine) != ESP_OK) {
return false;
}
self->hw_decoder = engine;
}

// Otherwise decode into a temporary buffer and copy the visible part out of it.
uint8_t *output;
size_t output_buffer_size;
uint8_t *scratch = NULL;
if (direct) {
output = (uint8_t *)bitmap->data;
output_buffer_size = output_size;
} else {
jpeg_decode_memory_alloc_cfg_t mem_config = {
.buffer_direction = JPEG_DEC_ALLOC_OUTPUT_BUFFER,
};
scratch = jpeg_alloc_decoder_mem(output_size, &mem_config, &output_buffer_size);
if (scratch == NULL) {
return false;
}
output = scratch;
}

// RGB order with RGB565 makes the engine emit the high byte first, the same
// byte-swapped RGB565 that TJpgDec produces here. JFIF files use full range
// BT601 YCbCr.
jpeg_decode_cfg_t decode_config = {
.output_format = JPEG_DECODE_OUT_FORMAT_RGB565,
.rgb_order = JPEG_DEC_RGB_ELEMENT_ORDER_RGB,
.conv_std = JPEG_YUV_RGB_CONV_STD_BT601,
.yuv_range = COLOR_RANGE_FULL,
};
uint32_t decoded_size;
if (jpeg_decoder_process(self->hw_decoder, &decode_config, jpeg->buf, jpeg->len,
output, output_buffer_size, &decoded_size) != ESP_OK) {
// A failed direct decode may have written part of the Bitmap. TJpgDec
// then decodes the whole image over it.
heap_caps_free(scratch);
return false;
}

if (direct) {
displayio_area_t area = { 0, 0, width, height, NULL };
displayio_bitmap_set_dirty_area(bitmap, &area);
return true;
}

// Copy out the visible part with the same clipping TJpgDec's output callback
// applies, treating the whole image as a single block at 0,0.
displayio_bitmap_t src = {
.width = width,
.height = height,
.data = (uint32_t *)(void *)output,
.stride = output_width / 2, // in units of uint32_t
.bits_per_value = 16,
.x_shift = 1,
.x_mask = 1,
.bitmask = 0xffff,
};
int x = self->x;
int y = self->y;
int x1 = self->lim.x1;
int y1 = self->lim.y1;
int x2 = MIN(self->lim.x2, (int)width);
int y2 = MIN(self->lim.y2, (int)height);
if (x1 < 0) {
x += -x1;
x1 = 0;
}
if (y1 < 0) {
y += -y1;
y1 = 0;
}
if (x2 > 0 && y2 > 0) {
common_hal_bitmaptools_blit(bitmap, &src, x, y, x1, y1, x2, y2,
self->skip_source_index, self->skip_source_index_none,
self->skip_dest_index, self->skip_dest_index_none);
}
heap_caps_free(scratch);
return true;
}
6 changes: 6 additions & 0 deletions ports/espressif/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ CIRCUITPY_ESPCAMERA = 0
# P4 has MIPI-DSI
CIRCUITPY_MIPIDSI = 1

# Hardware JPEG decoder
CIRCUITPY_JPEGIO_HW ?= $(CIRCUITPY_JPEGIO)

#### esp32s31 #########################################################
else ifeq ($(IDF_TARGET),esp32s31)

Expand Down Expand Up @@ -438,6 +441,9 @@ CIRCUITPY_ESPULP = 0
# The S31 has five eFuse key blocks, not six.
CIRCUITPY_HARDWAREKEY = 0

# Hardware JPEG decoder
CIRCUITPY_JPEGIO_HW ?= $(CIRCUITPY_JPEGIO)

# No I80 support from the IDF
CIRCUITPY_PARALLELDISPLAYBUS = 0

Expand Down
6 changes: 4 additions & 2 deletions ports/espressif/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ void *port_malloc(size_t size, bool dma_capable) {
}

void *ptr = NULL;
// Try SPIRAM first if available.
// Try SPIRAM first if available. Aligned to the GC block size so heap
// areas (and with 64-byte GC blocks, every GC allocation in them) are
// cache-line aligned, which video DMA destinations require.
#ifdef CONFIG_SPIRAM
ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
ptr = heap_caps_aligned_alloc(MICROPY_BYTES_PER_GC_BLOCK, size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
#endif
if (ptr == NULL) {
ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT);
Expand Down
4 changes: 4 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ CFLAGS += -DCIRCUITPY_IS31FL3741=$(CIRCUITPY_IS31FL3741)
CIRCUITPY_JPEGIO ?= $(CIRCUITPY_BITMAPTOOLS)
CFLAGS += -DCIRCUITPY_JPEGIO=$(CIRCUITPY_JPEGIO)

# Port provides a hardware JPEG decode path for jpegio.
CIRCUITPY_JPEGIO_HW ?= 0
CFLAGS += -DCIRCUITPY_JPEGIO_HW=$(CIRCUITPY_JPEGIO_HW)

CIRCUITPY_JSON ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_JSON=$(CIRCUITPY_JSON)

Expand Down
34 changes: 33 additions & 1 deletion shared-bindings/jpegio/JpegDecoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "py/obj.h"
#include "py/builtin.h"
#include "py/objproperty.h"
#include "py/runtime.h"

#include "shared-bindings/bitmaptools/__init__.h"
Expand Down Expand Up @@ -47,7 +48,7 @@ static mp_obj_t jpegio_jpegdecoder_make_new(const mp_obj_type_t *type, size_t n_
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

jpegio_jpegdecoder_obj_t *self = mp_obj_malloc(jpegio_jpegdecoder_obj_t, &jpegio_jpegdecoder_type);
jpegio_jpegdecoder_obj_t *self = mp_obj_malloc_with_finaliser(jpegio_jpegdecoder_obj_t, &jpegio_jpegdecoder_type);
common_hal_jpegio_jpegdecoder_construct(self);

return MP_OBJ_FROM_PTR(self);
Expand Down Expand Up @@ -124,6 +125,14 @@ MP_DEFINE_CONST_FUN_OBJ_2(jpegio_jpegdecoder_open_obj, jpegio_jpegdecoder_open);
//| possible to repeatedly ``decode`` the same jpeg data, even if it is to
//| select different scales or crop regions from it.
//|
//| On chips with a JPEG decoder peripheral (ESP32-P4 and ESP32-S31), ``decode``
//| uses the peripheral for baseline color JPEGs when the data was opened from a
//| buffer in DMA-capable memory, ``scale`` is 0, ``bitmap`` holds 16-bit values,
//| and no skip index is given. It is fastest when ``bitmap`` is exactly the image
//| size and the whole image is decoded at 0,0. Colors may differ slightly from the
//| software decoder's. Other images, and data opened from a file or stream, use
//| the software decoder.
//|
//| :param Bitmap bitmap: Optional output buffer
//| :param int scale: Scale factor from 0 to 3, inclusive.
//| :param int x: Horizontal pixel location in bitmap where source_bitmap upper-left
Expand Down Expand Up @@ -195,9 +204,32 @@ static mp_obj_t jpegio_jpegdecoder_decode(size_t n_args, const mp_obj_t *pos_arg
}
static MP_DEFINE_CONST_FUN_OBJ_KW(jpegio_jpegdecoder_decode_obj, 1, jpegio_jpegdecoder_decode);

//| used_hardware: bool
//| """True if the last `decode` used the chip's JPEG decoder peripheral, False if it
//| used the software decoder. (read only)"""
//|
static mp_obj_t jpegio_jpegdecoder_obj_get_used_hardware(mp_obj_t self_in) {
jpegio_jpegdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_jpegio_jpegdecoder_get_used_hardware(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(jpegio_jpegdecoder_get_used_hardware_obj, jpegio_jpegdecoder_obj_get_used_hardware);

MP_PROPERTY_GETTER(jpegio_jpegdecoder_used_hardware_obj,
(mp_obj_t)&jpegio_jpegdecoder_get_used_hardware_obj);

// Releases any hardware decoder the object holds when it is collected.
static mp_obj_t jpegio_jpegdecoder_del(mp_obj_t self_in) {
jpegio_jpegdecoder_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_jpegio_jpegdecoder_deinit(self);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(jpegio_jpegdecoder_del_obj, jpegio_jpegdecoder_del);

Comment on lines +207 to +227

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't what I was thinking. Just remove it.

static const mp_rom_map_elem_t jpegio_jpegdecoder_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&jpegio_jpegdecoder_open_obj) },
{ MP_ROM_QSTR(MP_QSTR_decode), MP_ROM_PTR(&jpegio_jpegdecoder_decode_obj) },
{ MP_ROM_QSTR(MP_QSTR_used_hardware), MP_ROM_PTR(&jpegio_jpegdecoder_used_hardware_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&jpegio_jpegdecoder_del_obj) },
};
static MP_DEFINE_CONST_DICT(jpegio_jpegdecoder_locals_dict, jpegio_jpegdecoder_locals_dict_table);

Expand Down
2 changes: 2 additions & 0 deletions shared-bindings/jpegio/JpegDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ extern const mp_obj_type_t jpegio_jpegdecoder_type;
typedef struct jpegio_jpegdecoder_obj jpegio_jpegdecoder_obj_t;

void common_hal_jpegio_jpegdecoder_construct(jpegio_jpegdecoder_obj_t *self);
void common_hal_jpegio_jpegdecoder_deinit(jpegio_jpegdecoder_obj_t *self);
void common_hal_jpegio_jpegdecoder_close(jpegio_jpegdecoder_obj_t *self);
bool common_hal_jpegio_jpegdecoder_get_used_hardware(jpegio_jpegdecoder_obj_t *self);
mp_obj_t common_hal_jpegio_jpegdecoder_set_source_buffer(jpegio_jpegdecoder_obj_t *self, mp_obj_t jpeg_data);
mp_obj_t common_hal_jpegio_jpegdecoder_set_source_file(jpegio_jpegdecoder_obj_t *self, mp_obj_t file_obj);
void common_hal_jpegio_jpegdecoder_decode_into(
Expand Down
Loading
Loading