From ceb5d888c1ef068a3703305c4b10740b30f3d67f Mon Sep 17 00:00:00 2001 From: xiaenling Date: Wed, 24 Jul 2024 19:50:53 +0800 Subject: [PATCH] Add set vo wbc compress quality in python --- micropython/port/builtin_py/media/display.py | 11 ++++++++--- .../port/builtin_py/mpp_binding/connector_api.c | 2 +- micropython/port/omv/ide_dbg.c | 16 +++++++++------- micropython/port/omv/imlib/jpeg.c | 13 ++++++++++--- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/micropython/port/builtin_py/media/display.py b/micropython/port/builtin_py/media/display.py index 48ca8bc..c307d13 100755 --- a/micropython/port/builtin_py/media/display.py +++ b/micropython/port/builtin_py/media/display.py @@ -175,7 +175,7 @@ def unbind_layer(cls, layer): # to_ide # osd_num @classmethod - def init(cls, type = None, width = None, height = None, osd_num = 1, to_ide = False, flag = None, fps = None): + def init(cls, type = None, width = None, height = None, osd_num = 1, to_ide = False, flag = None, fps = None, quality = 90): if cls._is_inited: print("Already run Display.init()") return @@ -281,9 +281,14 @@ def init(cls, type = None, width = None, height = None, osd_num = 1, to_ide = Fa raise RuntimeError(f"Display configure buffer for ide failed.") config = None - ide_dbg_set_vo_wbc(True, cls._width, cls._height) + if not isinstance(quality, int): + quality = 90 + if quality < 10: + quality = 90 + + ide_dbg_set_vo_wbc(quality, cls._width, cls._height) else: - ide_dbg_set_vo_wbc(False, 0, 0) + ide_dbg_set_vo_wbc(0, 0, 0) # must have one for disp if cls._osd_layer_num < 1: diff --git a/micropython/port/builtin_py/mpp_binding/connector_api.c b/micropython/port/builtin_py/mpp_binding/connector_api.c index 964edc5..7d1df6b 100644 --- a/micropython/port/builtin_py/mpp_binding/connector_api.c +++ b/micropython/port/builtin_py/mpp_binding/connector_api.c @@ -35,7 +35,7 @@ int ide_dbg_vo_wbc_deinit(void); int ide_dbg_vo_wbc_init(void); -int ide_dbg_set_vo_wbc(int enable, int width, int height); +int ide_dbg_set_vo_wbc(int, int, int); int kd_mpi_vo_osd_rotation(int flag, k_video_frame_info *in, k_video_frame_info *out); diff --git a/micropython/port/omv/ide_dbg.c b/micropython/port/omv/ide_dbg.c index df5061b..2944944 100644 --- a/micropython/port/omv/ide_dbg.c +++ b/micropython/port/omv/ide_dbg.c @@ -275,6 +275,7 @@ static bool _dma_dev_init_flag = false; static void* wbc_jpeg_buffer = NULL; static size_t wbc_jpeg_buffer_size = 0; static uint32_t wbc_jpeg_size = 0; +static int wbc_jpeg_quality = 90; static uint16_t wbc_width, wbc_height; static k_video_frame_info frame_info; #if ENABLE_BUFFER_ROTATION @@ -384,13 +385,14 @@ int kd_mpi_vo_osd_rotation(int flag, k_video_frame_info *in, k_video_frame_info return 0; } -int ide_dbg_set_vo_wbc(int enable, int width, int height) +int ide_dbg_set_vo_wbc(int quality, int width, int height) { - fb_from = enable ? FB_FROM_VO_WRITEBACK : FB_FROM_NONE; + fb_from = (0x00 != quality) ? FB_FROM_VO_WRITEBACK : FB_FROM_NONE; #if ENABLE_VO_WRITEBACK wbc_width = width; wbc_height = height; + wbc_jpeg_quality = (0x00 != quality) ? quality : 10; #endif // ENABLE_VO_WRITEBACK #if ENABLE_BUFFER_ROTATION @@ -556,7 +558,7 @@ static void rotation270_u16(uint16_t* __restrict dst, uint16_t* __restrict src, } #endif -int hd_jpeg_encode(k_video_frame_info* frame, void** buffer, size_t size, int timeout, void*(*realloc)(void*, unsigned long)); +extern int hd_jpeg_encode(k_video_frame_info* frame, void** buffer, size_t size, int timeout, int quality, void*(*realloc)(void*, unsigned long)); static ide_dbg_status_t ide_dbg_update(ide_dbg_state_t* state, const uint8_t* data, size_t length) { for (size_t i = 0; i < length;) { @@ -866,7 +868,7 @@ static ide_dbg_status_t ide_dbg_update(ide_dbg_state_t* state, const uint8_t* da kd_mpi_sys_mmz_flush_cache(frame_info.v_frame.phys_addr[1], uv, uvsize); kd_mpi_sys_munmap(uv, uvsize); - ssize = hd_jpeg_encode(&rotation_buffer, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, realloc); + ssize = hd_jpeg_encode(&rotation_buffer, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, wbc_jpeg_quality, realloc); } else if (K_ROTATION_270 == (vo_wbc_flag & K_ROTATION_270)) { // y uint8_t* y = kd_mpi_sys_mmap_cached(frame_info.v_frame.phys_addr[0], ysize); @@ -882,12 +884,12 @@ static ide_dbg_status_t ide_dbg_update(ide_dbg_state_t* state, const uint8_t* da kd_mpi_sys_mmz_flush_cache(frame_info.v_frame.phys_addr[1], uv, uvsize); kd_mpi_sys_munmap(uv, uvsize); - ssize = hd_jpeg_encode(&rotation_buffer, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, realloc); + ssize = hd_jpeg_encode(&rotation_buffer, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, wbc_jpeg_quality, realloc); } else { - ssize = hd_jpeg_encode(&frame_info, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, realloc); + ssize = hd_jpeg_encode(&frame_info, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, wbc_jpeg_quality, realloc); } #else - ssize = hd_jpeg_encode(&frame_info, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, realloc); + ssize = hd_jpeg_encode(&frame_info, &wbc_jpeg_buffer, wbc_jpeg_buffer_size, 1000, wbc_jpeg_quality, realloc); #endif if (0) { // dump raw file diff --git a/micropython/port/omv/imlib/jpeg.c b/micropython/port/omv/imlib/jpeg.c index 8422bd7..ed18358 100644 --- a/micropython/port/omv/imlib/jpeg.c +++ b/micropython/port/omv/imlib/jpeg.c @@ -1971,7 +1971,7 @@ void hd_jpeg_encoder_destory(void) * Hardware JPEG compressing * @retval -1: error, 0: overflow, >0: JPEG size */ -int hd_jpeg_encode(k_video_frame_info* frame, void** buffer, size_t size, int timeout, void*(*realloc)(void*, unsigned long)) { +int hd_jpeg_encode(k_video_frame_info* frame, void** buffer, size_t size, int timeout, int quality, void*(*realloc)(void*, unsigned long)) { int ret = -1; int error = 0; @@ -1982,6 +1982,13 @@ int hd_jpeg_encode(k_video_frame_info* frame, void** buffer, size_t size, int ti static bool first_frame = true; static k_venc_chn_attr attr; init: + + if(10 > quality) { + quality = 10; + } else if(100 < quality) { + quality = 100; + } + if (jpeg_encoder_created == 0) { // create channel memset(&attr, 0, sizeof(attr)); @@ -1993,7 +2000,7 @@ int hd_jpeg_encode(k_video_frame_info* frame, void** buffer, size_t size, int ti attr.rc_attr.rc_mode = K_VENC_RC_MODE_MJPEG_FIXQP; attr.rc_attr.mjpeg_fixqp.src_frame_rate = 30; attr.rc_attr.mjpeg_fixqp.dst_frame_rate = 30; - attr.rc_attr.mjpeg_fixqp.q_factor = 45; + attr.rc_attr.mjpeg_fixqp.q_factor = quality; error = kd_mpi_venc_create_chn(VENC_MAX_CHN_NUMS - 1, &attr); if (error) { fprintf(stderr, "[omv] kd_mpi_venc_create_chn error %u\n", error); @@ -2146,7 +2153,7 @@ bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc) { // unsupported goto skip; } - int ssize = hd_jpeg_encode(&frame, (void**)&dst->data, dst->size, 1000, NULL); + int ssize = hd_jpeg_encode(&frame, (void**)&dst->data, dst->size, 1000, quality, NULL); if (ssize > 0) { dst->size = ssize; } else if (ssize == 0) {