From a489012f52ae586b33c0fd714cb56ddb1c5eec5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Mon, 3 Jun 2024 12:24:28 -0400 Subject: [PATCH] Fixing stuttering and standardizing the HAL functions on Ingenic T31 --- src/hal/inge/t31_hal.c | 114 +++++++++++++++++++++++++---------------- src/hal/inge/t31_hal.h | 3 ++ src/hal/inge/t31_isp.h | 22 +++++++- src/video.c | 7 ++- 4 files changed, 99 insertions(+), 47 deletions(-) diff --git a/src/hal/inge/t31_hal.c b/src/hal/inge/t31_hal.c index a1900b8..c0aac37 100644 --- a/src/hal/inge/t31_hal.c +++ b/src/hal/inge/t31_hal.c @@ -116,7 +116,7 @@ int t31_channel_create(char index, short width, short height, char framerate) .dest = { .width = width, .height = height }, .pixFmt = T31_PIXFMT_NV12, .scale = { .enable = (_t31_snr_dim.width != width || _t31_snr_dim.height != height) ? 1 : 0, .width = width, .height = height }, - .fpsNum = framerate, .fpsDen = 1, .bufCount = 1, .phyOrExtChn = 0, + .fpsNum = framerate, .fpsDen = 1, .bufCount = width > 1920 ? 2 : 1, .phyOrExtChn = 0, }; if (ret = t31_fs.fnCreateChannel(index, &channel)) @@ -162,6 +162,59 @@ int t31_config_load(char *path) return t31_isp.fnLoadConfig(path); } +int t31_pipeline_create(char mirror, char flip) +{ + int ret; + + if (ret = t31_isp.fnInit()) + return ret; + { + const char *sensor = getenv("SENSOR"); + for (char i = 0; i < sizeof(t31_sensors) / sizeof(*t31_sensors); i++) { + if (strcmp(t31_sensors[i].name, sensor)) continue; + memcpy(&_t31_isp_snr, &t31_sensors[i], sizeof(t31_isp_snr)); + if (t31_sensors[i].mode == T31_ISP_COMM_I2C) + memcpy(&_t31_isp_snr.i2c.type, &t31_sensors[i].name, strlen(t31_sensors[i].name)); + else if (t31_sensors[i].mode == T31_ISP_COMM_SPI) + memcpy(&_t31_isp_snr.spi.alias, &t31_sensors[i].name, strlen(t31_sensors[i].name)); + _t31_snr_dim = t31_dims[i]; + ret = 0; + break; + } + if (ret) + return EXIT_FAILURE; + } + if (ret = t31_isp.fnAddSensor(&_t31_isp_snr)) + return ret; + if (ret = t31_isp.fnEnableSensor()) + return ret; + if (ret = t31_isp.fnEnableTuning()) + return ret; + if (ret = t31_isp.fnSetRunningMode(0)) + return ret; + if (ret = t31_isp.fnSetAntiFlicker(T31_ISP_FLICK_50HZ)) + return ret; + if (ret = t31_isp.fnSetFlip((mirror ? 1 : 0) | (flip ? 2 : 0))) + return ret; + + if (ret = t31_osd.fnCreateGroup(_t31_osd_grp)) + return ret; + if (ret = t31_osd.fnStartGroup(_t31_osd_grp)) + return ret; + + return EXIT_SUCCESS; +} + +void t31_pipeline_destroy(void) +{ + t31_osd.fnStopGroup(_t31_osd_grp); + t31_osd.fnDestroyGroup(_t31_osd_grp); + + t31_isp.fnDisableSensor(); + t31_isp.fnDeleteSensor(&_t31_isp_snr); + t31_isp.fnExit(); +} + int t31_region_create(int *handle, hal_rect rect) { int ret; @@ -380,9 +433,23 @@ void *t31_video_thread(void) if (t31_venc_cb) { hal_vidstream outStrm; - hal_vidpack outPack[1]; - outStrm.count = 1; + hal_vidpack outPack[stream.count]; + memset(outPack, 0, sizeof(outPack)); + outStrm.count = stream.count; outStrm.seq = stream.sequence; + for (int j = 0; j < stream.count; j++) { + t31_venc_pack *pack = &stream.packet[j]; + if (!pack->length) continue; + outPack[j].offset = 0; + unsigned int remain = stream.length - pack->offset; + if (remain < pack->length) { + outPack[j].data = (unsigned char*)(stream.addr); + outPack[j].length = pack->length - remain; + } else { + outPack[j].data = (unsigned char*)(stream.addr + pack->offset); + outPack[j].length = pack->length; + } + } outPack[0].data = (unsigned char*)(stream.addr); outPack[0].offset = 0; outPack[0].length = 0; @@ -405,14 +472,7 @@ void *t31_video_thread(void) void t31_system_deinit(void) { - t31_osd.fnStopGroup(_t31_osd_grp); - t31_osd.fnDestroyGroup(_t31_osd_grp); - t31_sys.fnExit(); - - t31_isp.fnDisableSensor(); - t31_isp.fnDeleteSensor(&_t31_isp_snr); - t31_isp.fnExit(); } int t31_system_init(void) @@ -427,42 +487,8 @@ int t31_system_init(void) puts(version.version); } - { - const char *sensor = getenv("SENSOR"); - for (char i = 0; i < sizeof(t31_sensors) / sizeof(*t31_sensors); i++) { - if (strcmp(t31_sensors[i].name, sensor)) continue; - memcpy(&_t31_isp_snr, &t31_sensors[i], sizeof(t31_isp_snr)); - if (t31_sensors[i].mode == T31_ISP_COMM_I2C) - memcpy(&_t31_isp_snr.i2c.type, &t31_sensors[i].name, strlen(t31_sensors[i].name)); - else if (t31_sensors[i].mode == T31_ISP_COMM_SPI) - memcpy(&_t31_isp_snr.spi.alias, &t31_sensors[i].name, strlen(t31_sensors[i].name)); - _t31_snr_dim = t31_dims[i]; - ret = 0; - break; - } - if (ret) - return EXIT_FAILURE; - } - - if (ret = t31_isp.fnInit()) - return ret; - if (ret = t31_isp.fnAddSensor(&_t31_isp_snr)) - return ret; - if (ret = t31_isp.fnEnableSensor()) - return ret; - if (ret = t31_sys.fnInit()) return ret; - if (ret = t31_isp.fnEnableTuning()) - return ret; - if (ret = t31_isp.fnSetRunningMode(0)) - return ret; - - if (ret = t31_osd.fnCreateGroup(_t31_osd_grp)) - return ret; - if (ret = t31_osd.fnStartGroup(_t31_osd_grp)) - return ret; - return EXIT_SUCCESS; } diff --git a/src/hal/inge/t31_hal.h b/src/hal/inge/t31_hal.h index f261ccf..98d9f9c 100644 --- a/src/hal/inge/t31_hal.h +++ b/src/hal/inge/t31_hal.h @@ -29,6 +29,9 @@ int t31_channel_unbind(char index); int t31_config_load(char *path); +int t31_pipeline_create(char mirror, char flip); +void t31_pipeline_destroy(void); + int t31_region_create(int *handle, hal_rect rect); void t31_region_destroy(int *handle); int t31_region_setbitmap(int *handle, hal_bitmap *bitmap); diff --git a/src/hal/inge/t31_isp.h b/src/hal/inge/t31_isp.h index b58a278..f8bbe0b 100644 --- a/src/hal/inge/t31_isp.h +++ b/src/hal/inge/t31_isp.h @@ -7,6 +7,12 @@ typedef enum { T31_ISP_COMM_SPI, } t31_isp_comm; +typedef enum { + T31_ISP_FLICK_OFF, + T31_ISP_FLICK_50HZ, + T31_ISP_FLICK_60HZ +} t31_isp_flick; + typedef struct { char type[20]; int addr; @@ -99,6 +105,8 @@ typedef struct { int (*fnExit)(void); int (*fnInit)(void); int (*fnLoadConfig)(char *path); + int (*fnSetAntiFlicker)(t31_isp_flick mode); + int (*fnSetFlip)(int mode); int (*fnSetRunningMode)(int nightOn); int (*fnAddSensor)(t31_isp_snr *sensor); @@ -137,11 +145,23 @@ static int t31_isp_load(t31_isp_impl *isp_lib) { return EXIT_FAILURE; } + if (!(isp_lib->fnSetAntiFlicker = (int(*)(t31_isp_flick mode)) + dlsym(isp_lib->handle, "IMP_ISP_Tuning_SetAntiFlickerAttr"))) { + fprintf(stderr, "[t31_isp] Failed to acquire symbol IMP_ISP_Tuning_SetAntiFlickerAttr!\n"); + return EXIT_FAILURE; + } + + if (!(isp_lib->fnSetFlip = (int(*)(int mode)) + dlsym(isp_lib->handle, "IMP_ISP_Tuning_SetHVFLIP"))) { + fprintf(stderr, "[t31_isp] Failed to acquire symbol IMP_ISP_Tuning_SetHVFLIP!\n"); + return EXIT_FAILURE; + } + if (!(isp_lib->fnSetRunningMode = (int(*)(int nightOn)) dlsym(isp_lib->handle, "IMP_ISP_Tuning_SetISPRunningMode"))) { fprintf(stderr, "[t31_isp] Failed to acquire symbol IMP_ISP_Tuning_SetISPRunningMode!\n"); return EXIT_FAILURE; - } + } if (!(isp_lib->fnAddSensor = (int(*)(t31_isp_snr *sensor)) dlsym(isp_lib->handle, "IMP_ISP_AddSensor"))) { diff --git a/src/video.c b/src/video.c index d0bbeb4..0e9f72b 100644 --- a/src/video.c +++ b/src/video.c @@ -205,6 +205,8 @@ int start_sdk() { height, framerate); break; case HAL_PLATFORM_I6F: ret = i6f_pipeline_create(0, width, height, framerate); break; + case HAL_PLATFORM_T31: ret = t31_pipeline_create( + app_config.mirror, app_config.flip); break; case HAL_PLATFORM_V3: ret = v3_pipeline_create(); break; case HAL_PLATFORM_V4: ret = v4_pipeline_create(); break; } @@ -287,8 +289,8 @@ int start_sdk() { if (app_config.mjpeg_enable) { int index = take_next_free_channel(true); - if (ret = create_vpss_chn(index, app_config.mjpeg_width, app_config.mjpeg_height, - app_config.mjpeg_fps, 1)) { + if (ret = create_vpss_chn(index, app_config.mjpeg_width, + app_config.mjpeg_height, app_config.mjpeg_fps, 1)) { fprintf(stderr, "Creating channel %d failed with %#x!\n%s\n", index, ret, errstr(ret)); @@ -389,6 +391,7 @@ int stop_sdk() { case HAL_PLATFORM_I6: i6_pipeline_destroy(); break; case HAL_PLATFORM_I6C: i6c_pipeline_destroy(); break; case HAL_PLATFORM_I6F: i6f_pipeline_destroy(); break; + case HAL_PLATFORM_T31: t31_pipeline_destroy(); break; case HAL_PLATFORM_V3: v3_pipeline_destroy(); break; case HAL_PLATFORM_V4: v4_pipeline_destroy(); break; }