#include #include #include #include "AW_VideoInput_API.h" #include "rt_awaiisp_common.h" //#define RT_AWAIISP_COMMON_DUMP_TDM_DATA #ifdef RT_AWAIISP_COMMON_DUMP_TDM_DATA #define RT_AWAIISP_COMMON_DUMP_TDM_FRAME_CNT_MIN (50) #define RT_AWAIISP_COMMON_DUMP_TDM_FRAME_CNT_MAX (51) #endif /* print level */ #define rt_awaiisp_common_err_print(fmt, arg...) printf("[RT_AWAIISP_COMMON_ERR]%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ##arg) #define rt_awaiisp_common_warn_print(fmt, arg...) printf("[RT_AWAIISP_COMMON_WRN]%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ##arg) #define rt_awaiisp_common_dbg_print(fmt, arg...) printf("[RT_AWAIISP_COMMON_DBG]%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ##arg) #define rt_awaiisp_common_ver_print(fmt, arg...) //printf("[RT_AWAIISP_COMMON_VER]%s:%u: " fmt "\n", __FUNCTION__, __LINE__, ##arg) #define RT_AWAIISP_COMMON_NUM_MAX (4) #define RT_AWAIISP_COMMON_GAMMA_TYPE (3) typedef struct rt_awaiisp_common_context { int isp_id; unsigned int tdm_frm_cnt; unsigned int tdm_frm_head_len; unsigned int tdm_frm_fill_len; int npu_lut_enable; int aiisp_enable; } rt_awaiisp_common_context; static rt_awaiisp_common_context gRtAiIspCommonContext[RT_AWAIISP_COMMON_NUM_MAX]; static struct rt_awaiisp_common_context *rt_awaiisp_get_common_context(int isp) { if (isp >= RT_AWAIISP_COMMON_NUM_MAX) { rt_awaiisp_common_err_print("fatal error! invalid ch %d >= %d", isp, RT_AWAIISP_COMMON_NUM_MAX); return NULL; } return &gRtAiIspCommonContext[isp]; } #ifdef RT_AWAIISP_COMMON_DUMP_TDM_DATA void rt_awaiisp_dump_tdm_data_once_callback(int isp, int buf_id, unsigned int buf_size, char *flag) { struct rt_awaiisp_common_context *pContext = rt_awaiisp_get_common_context(isp); if (RT_AWAIISP_COMMON_DUMP_TDM_FRAME_CNT_MIN <= pContext->tdm_frm_cnt && pContext->tdm_frm_cnt <= RT_AWAIISP_COMMON_DUMP_TDM_FRAME_CNT_MAX) { struct vin_isp_tdm_data data; memset(&data, 0, sizeof(struct vin_isp_tdm_data)); data.buf = malloc(buf_size); if (data.buf == NULL) { rt_awaiisp_common_err_print("ch%d malloc size is %d error!", isp, buf_size); return; } data.buf_size = buf_size; data.req_buf_id = buf_id; AWVideoInput_GetTdmData(isp, &data); if (flag) { char fdstr[128]; FILE *fp = NULL; snprintf(fdstr, 128, "/mnt/extsd/tdm_test/%s_%d.bin", flag, pContext->tdm_frm_cnt); fp = fopen(fdstr, "w"); fwrite(data.buf + pContext->tdm_frm_fill_len + pContext->tdm_frm_head_len, data.buf_size - pContext->tdm_frm_fill_len - pContext->tdm_frm_head_len, 1, fp); rt_awaiisp_common_ver_print("ch%d save file %s success", isp, flag); fclose(fp); } if (data.buf) { free(data.buf); data.buf = NULL; data.buf_size = 0; } } } #endif void rt_awaiisp_return_tdm_buffer_callback(struct vin_isp_tdm_event_status *status) { int id = status->dev_id; struct rt_awaiisp_common_context *pContext = rt_awaiisp_get_common_context(id); pContext->isp_id = id; pContext->tdm_frm_fill_len = status->fill_len; pContext->tdm_frm_head_len = status->head_len; pContext->tdm_frm_cnt++; AWVideoInput_ReturnTdmBuf(id, status); } int rt_awaiisp_common_enable(int isp, rt_awaiisp_common_config_param *param) { int ret = 0; struct rt_awaiisp_common_context *pContext = rt_awaiisp_get_common_context(isp); if (1 == pContext->aiisp_enable) { rt_awaiisp_common_warn_print("ch%d aiisp repeatedly enable, please check it.", isp); return 0; } memset(pContext, 0, sizeof(rt_awaiisp_common_context)); pContext->aiisp_enable = 1; param->config.ion_mem_open = 1; ret |= awaiisp_open(isp, ¶m->config); ret |= awaiisp_start(isp); ret |= awaiisp_register_return_tdm_buffer_callback(isp, &rt_awaiisp_return_tdm_buffer_callback); #ifdef RT_AWAIISP_COMMON_DUMP_TDM_DATA ret |= awaiisp_register_dump_tdm_data_once_callback(isp, &rt_awaiisp_dump_tdm_data_once_callback); awaiisp_set_dump_g2d_data_once_path(isp, "/mnt/extsd/tdm_test"); #endif RTIspCtrlAttr isp_ctrl_attr; memset(&isp_ctrl_attr, 0, sizeof(RTIspCtrlAttr)); isp_ctrl_attr.isp_attr_cfg.cfg_id = RT_ISP_CTRL_AI_ISP; if (strlen(param->config.lut_model_file) != 0) { isp_ctrl_attr.isp_attr_cfg.ai_isp_en = RT_AWAIISP_COMMON_GAMMA_TYPE; pContext->npu_lut_enable = 1; } else { rt_awaiisp_common_dbg_print("ch%d user set lut nbg file path is NULL, set isp ai_isp_en=0", isp); isp_ctrl_attr.isp_attr_cfg.ai_isp_en = 0; pContext->npu_lut_enable = 0; } if (param->isp_cfg_bin_path && strlen(param->isp_cfg_bin_path) != 0) { isp_ctrl_attr.isp_attr_cfg.ai_isp_update = 0; } else { isp_ctrl_attr.isp_attr_cfg.ai_isp_update = 1; } ret |= AWVideoInput_SetIspAttrCfg(isp, &isp_ctrl_attr); if (param->isp_cfg_bin_path && strlen(param->isp_cfg_bin_path) != 0) { if (0 != access(param->isp_cfg_bin_path, F_OK)) { rt_awaiisp_common_err_print("fatal error! ch%d isp_cfg_bin_path %s is not exist!", isp, param->isp_cfg_bin_path); } rt_awaiisp_common_dbg_print("ch%d user set isp cfg bin file %s", isp, param->isp_cfg_bin_path); memset(&isp_ctrl_attr, 0, sizeof(RTIspCtrlAttr)); isp_ctrl_attr.isp_attr_cfg.cfg_id = RT_ISP_CTRL_READ_BIN_PARAM; strncpy(isp_ctrl_attr.isp_attr_cfg.path, param->isp_cfg_bin_path, 100); ret |= AWVideoInput_SetIspAttrCfg(isp, &isp_ctrl_attr); } ret |= AWVideoInput_RegisterTdmBufDoneCallback(isp, &awaiisp_tdm_buffer_process_callback); ret |= AWVideoInput_StartProcessTdmBuf(isp); return ret; } int rt_awaiisp_common_disable(int isp) { int ret = 0; struct rt_awaiisp_common_context *pContext = rt_awaiisp_get_common_context(isp); if (0 == pContext->aiisp_enable) { rt_awaiisp_common_warn_print("ch%d aiisp is not enable or repeatedly disable.", isp); return 0; } ret |= AWVideoInput_RegisterTdmBufDoneCallback(isp, NULL); ret |= awaiisp_stop(isp); ret |= awaiisp_close(isp); pContext->aiisp_enable = 0; return ret; } int rt_awaiisp_common_switch_mode(int isp, rt_awaiisp_common_switch_param *param) { int ret = 0; struct rt_awaiisp_common_context *pContext = rt_awaiisp_get_common_context(isp); if (0 == pContext->aiisp_enable) { rt_awaiisp_common_warn_print("ch%d aiisp is not enable, please check it.", isp); return -1; } if ((NULL == param->isp_cfg_bin_path) || (param->isp_cfg_bin_path && strlen(param->isp_cfg_bin_path) == 0)) { rt_awaiisp_common_err_print("fatal error! ch%d invalid isp cfg bin path %p", isp, param->isp_cfg_bin_path); return -1; } /** awaiisp_switch_mode is a block api that needs to be switched before configuring isp. */ ret |= awaiisp_switch_mode(isp, ¶m->config); RTIspCtrlAttr isp_ctrl_attr; memset(&isp_ctrl_attr, 0, sizeof(RTIspCtrlAttr)); isp_ctrl_attr.isp_attr_cfg.cfg_id = RT_ISP_CTRL_AI_ISP; if (0 == pContext->npu_lut_enable) { isp_ctrl_attr.isp_attr_cfg.ai_isp_en = 0; rt_awaiisp_common_dbg_print("ch%d user disable npu lut, so set ai_isp_en = 0", isp); } else { if (AWAIISP_MODE_NPU == param->config.mode || AWAIISP_MODE_NORMAL_GAMMA == param->config.mode) isp_ctrl_attr.isp_attr_cfg.ai_isp_en = RT_AWAIISP_COMMON_GAMMA_TYPE; else isp_ctrl_attr.isp_attr_cfg.ai_isp_en = 0; } isp_ctrl_attr.isp_attr_cfg.ai_isp_update = 0; ret |= AWVideoInput_SetIspAttrCfg(isp, &isp_ctrl_attr); memset(&isp_ctrl_attr, 0, sizeof(RTIspCtrlAttr)); isp_ctrl_attr.isp_attr_cfg.cfg_id = RT_ISP_CTRL_READ_BIN_PARAM; strncpy(isp_ctrl_attr.isp_attr_cfg.path, param->isp_cfg_bin_path, 100); ret |= AWVideoInput_SetIspAttrCfg(isp, &isp_ctrl_attr); return ret; }