Repository navigation
Speed up JPEG decoding on ESP32-P4 and ESP32-S31 #11634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikeysklar
wants to merge
5
commits into
adafruit:main
Choose a base branch
from
mikeysklar:jpegio-hw-decode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+383
−5
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b065a0
jpegio: decode JPEGs with the JPEG engine on ESP32-P4 and ESP32-S31
mikeysklar 3994305
Update esp-idf for JPEG engine color range and S31 ROM fix
mikeysklar 5846a7c
espressif: align GC blocks to the cache line on P4 and S31
mikeysklar d69ef5f
jpegio: free the JPEG engine from a finaliser
mikeysklar e381071
jpegio: add JpegDecoder.used_hardware
mikeysklar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Submodule esp-idf
updated
9 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
|
@@ -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); | ||
|
|
@@ -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 | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.