Merge branch 'feat/p4_rev3_isp_blc' into 'master'

isp: black level correction driver support on p4 eco5

Closes IDF-13931

See merge request espressif/esp-idf!41714
This commit is contained in:
Armando (Dou Yiwen)
2025-09-24 01:10:40 +00:00
15 changed files with 516 additions and 16 deletions

View File

@@ -22,6 +22,10 @@ if(CONFIG_SOC_ISP_BF_SUPPORTED)
list(APPEND srcs "src/isp_bf.c")
endif()
if(CONFIG_SOC_ISP_BLC_SUPPORTED)
list(APPEND srcs "src/isp_blc.c")
endif()
if(CONFIG_SOC_ISP_DEMOSAIC_SUPPORTED)
list(APPEND srcs "src/isp_demosaic.c")
endif()

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -16,10 +16,11 @@
#include "driver/isp_af.h"
#include "driver/isp_awb.h"
#include "driver/isp_bf.h"
#include "driver/isp_blc.h"
#include "driver/isp_ccm.h"
#include "driver/isp_color.h"
#include "driver/isp_demosaic.h"
#include "driver/isp_gamma.h"
#include "driver/isp_hist.h"
#include "driver/isp_sharpen.h"
#include "driver/isp_color.h"
#include "driver/isp_lsc.h"
#include "driver/isp_sharpen.h"

View File

@@ -0,0 +1,112 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_err.h"
#include "driver/isp_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------------
BLC (Black Level Correction)
---------------------------------------------------------------*/
/**
* @brief ISP BLC threshold configurations
*/
typedef struct {
uint8_t top_left_chan_thresh; ///< Black level threshold for top left channel of the raw Bayer image
uint8_t top_right_chan_thresh; ///< Black level threshold for top right channel of the raw Bayer image
uint8_t bottom_left_chan_thresh; ///< Black level threshold for bottom left channel of the raw Bayer image
uint8_t bottom_right_chan_thresh; ///< Black level threshold for bottom right channel of the raw Bayer image
} esp_isp_blc_thresh_t;
/**
* @brief ISP BLC stretch configurations
*
* Enable this can stretch the pixel value to 0~255 after black level correction
*/
typedef struct {
bool top_left_chan_stretch_en; ///< Enable stretch for top left channel of the raw Bayer image
bool top_right_chan_stretch_en; ///< Enable stretch for top right channel of the raw Bayer image
bool bottom_left_chan_stretch_en; ///< Enable stretch for bottom left channel of the raw Bayer image
bool bottom_right_chan_stretch_en; ///< Enable stretch for bottom right channel of the raw Bayer image
} esp_isp_blc_stretch_t;
/**
* @brief ISP BLC configurations
*/
typedef struct {
isp_window_t window; ///< The sampling windows of BLC, only pixels within the window will be sampled
esp_isp_blc_thresh_t filter_threshold; ///< Black level threshold for each channel of the raw Bayer image
bool filter_enable; ///< Enable filter for BLC, if enabled, only pixels within the threshold will be sampled
esp_isp_blc_stretch_t stretch; ///< Stretch configurations for each channel of the raw Bayer image
} esp_isp_blc_config_t;
/**
* @brief ISP BLC correction offset
*/
typedef struct {
uint32_t top_left_chan_offset; ///< Correction offset for top left channel of the raw Bayer image
uint32_t top_right_chan_offset; ///< Correction offset for top right channel of the raw Bayer image
uint32_t bottom_left_chan_offset; ///< Correction offset for bottom left channel of the raw Bayer image
uint32_t bottom_right_chan_offset; ///< Correction offset for bottom right channel of the raw Bayer image
} esp_isp_blc_offset_t;
/**
* @brief ISP BLC configuration
*
* @note After calling this API, BLC doesn't take into effect until `esp_isp_blc_enable` is called
*
* @param[in] isp_proc Processor handle
* @param[in] config BLC configurations
*
* @return
* - ESP_OK On success
* - ESP_ERR_INVALID_STATE Not allowed to be called under current state
* - ESP_ERR_INVALID_ARG If the combination of arguments is invalid
* - ESP_ERR_NOT_SUPPORTED Not supported
*/
esp_err_t esp_isp_blc_configure(isp_proc_handle_t isp_proc, const esp_isp_blc_config_t *config);
/**
* @brief Enable ISP BLC function
*
* @param[in] isp_proc Processor handle
*
* @return
* - ESP_OK On success
* - ESP_ERR_INVALID_ARG If the combination of arguments is invalid.
* - ESP_ERR_INVALID_STATE Driver state is invalid.
*/
esp_err_t esp_isp_blc_enable(isp_proc_handle_t isp_proc);
/**
* @brief Set the correction offset of ISP BLC function
*
* @param[in] isp_proc Processor handle
* @param[in] offset Correction offset
*/
esp_err_t esp_isp_blc_set_correction_offset(isp_proc_handle_t isp_proc, esp_isp_blc_offset_t *offset);
/**
* @brief Disable ISP BLC function
*
* @param[in] isp_proc Processor handle
*
* @return
* - ESP_OK On success
* - ESP_ERR_INVALID_ARG If the combination of arguments is invalid.
* - ESP_ERR_INVALID_STATE Driver state is invalid.
*/
esp_err_t esp_isp_blc_disable(isp_proc_handle_t isp_proc);
#ifdef __cplusplus
}
#endif

View File

@@ -14,6 +14,10 @@
extern "C" {
#endif
/*---------------------------------------------------------------
LSC (Lens Shading Correction)
---------------------------------------------------------------*/
/**
* @brief LSC Gain array
*/

View File

@@ -77,6 +77,7 @@ typedef struct isp_processor_t {
isp_fsm_t sharpen_fsm;
isp_fsm_t color_fsm;
isp_fsm_t lsc_fsm;
isp_fsm_t blc_fsm;
esp_isp_evt_cbs_t cbs;
void *user_data;

View File

@@ -0,0 +1,103 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdatomic.h>
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "driver/isp_core.h"
#include "driver/isp_blc.h"
#include "esp_private/isp_private.h"
#include "hal/efuse_hal.h"
#include "soc/chip_revision.h"
/*---------------------------------------------------------------
BLC
---------------------------------------------------------------*/
static const char *TAG = "ISP_BLC";
esp_err_t esp_isp_blc_configure(isp_proc_handle_t isp_proc, const esp_isp_blc_config_t *config)
{
#if CONFIG_IDF_TARGET_ESP32P4
unsigned chip_version = efuse_hal_chip_revision();
if (!ESP_CHIP_REV_ABOVE(chip_version, 300)) {
ESP_RETURN_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, TAG, "BLC is not supported on ESP32P4 chips prior than v3.0");
}
#endif
ESP_RETURN_ON_FALSE(isp_proc && config, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
ESP_RETURN_ON_FALSE(isp_proc->blc_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "blc is enabled already");
ESP_RETURN_ON_FALSE(config->window.top_left.x < config->window.btm_right.x, ESP_ERR_INVALID_ARG, TAG, "invalid window x coordinates");
ESP_RETURN_ON_FALSE(config->window.top_left.y < config->window.btm_right.y, ESP_ERR_INVALID_ARG, TAG, "invalid window y coordinates");
ESP_RETURN_ON_FALSE(config->window.btm_right.x <= isp_proc->h_res, ESP_ERR_INVALID_ARG, TAG, "window exceeds horizontal resolution");
ESP_RETURN_ON_FALSE(config->window.btm_right.y <= isp_proc->v_res, ESP_ERR_INVALID_ARG, TAG, "window exceeds vertical resolution");
// Configure clock control mode
isp_ll_blc_set_clk_ctrl_mode(isp_proc->hal.hw, ISP_LL_PIPELINE_CLK_CTRL_AUTO);
// Configure sampling window
isp_ll_blc_set_window(isp_proc->hal.hw,
config->window.top_left.x, config->window.top_left.y,
config->window.btm_right.x, config->window.btm_right.y);
if (config->filter_enable) {
// Configure threshold values for each channel
isp_ll_blc_set_filter_threshold(isp_proc->hal.hw, config->filter_threshold.top_left_chan_thresh, config->filter_threshold.top_right_chan_thresh, config->filter_threshold.bottom_left_chan_thresh, config->filter_threshold.bottom_right_chan_thresh);
isp_ll_blc_enable_filter(isp_proc->hal.hw, true);
} else {
isp_ll_blc_enable_filter(isp_proc->hal.hw, false);
}
// Configure stretch enable for each channel
isp_ll_blc_enable_stretch(isp_proc->hal.hw, config->stretch.top_left_chan_stretch_en, config->stretch.top_right_chan_stretch_en, config->stretch.bottom_left_chan_stretch_en, config->stretch.bottom_right_chan_stretch_en);
return ESP_OK;
}
esp_err_t esp_isp_blc_enable(isp_proc_handle_t isp_proc)
{
ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
ESP_RETURN_ON_FALSE(isp_proc->blc_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "blc is enabled already");
// Enable BLC module
isp_ll_blc_enable(isp_proc->hal.hw, true);
isp_proc->blc_fsm = ISP_FSM_ENABLE;
ESP_LOGD(TAG, "BLC enabled");
return ESP_OK;
}
esp_err_t esp_isp_blc_set_correction_offset(isp_proc_handle_t isp_proc, esp_isp_blc_offset_t *offset)
{
ESP_RETURN_ON_FALSE(isp_proc && offset, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
ESP_RETURN_ON_FALSE(isp_proc->blc_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "blc isn't enabled yet");
// Set correction offset for each channel
isp_ll_blc_set_correction_offset(isp_proc->hal.hw, offset->top_left_chan_offset, offset->top_right_chan_offset, offset->bottom_left_chan_offset, offset->bottom_right_chan_offset);
ESP_LOGD(TAG, "BLC correction offset set: TL=%"PRIu32", TR=%"PRIu32", BL=%"PRIu32", BR=%"PRIu32,
offset->top_left_chan_offset, offset->top_right_chan_offset,
offset->bottom_left_chan_offset, offset->bottom_right_chan_offset);
return ESP_OK;
}
esp_err_t esp_isp_blc_disable(isp_proc_handle_t isp_proc)
{
ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
ESP_RETURN_ON_FALSE(isp_proc->blc_fsm == ISP_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "blc isn't enabled yet");
// Disable BLC module
isp_ll_blc_enable(isp_proc->hal.hw, false);
isp_proc->blc_fsm = ISP_FSM_INIT;
ESP_LOGD(TAG, "BLC disabled");
return ESP_OK;
}

View File

@@ -27,7 +27,7 @@ static const char *TAG = "ISP_LSC";
esp_err_t esp_isp_lsc_allocate_gain_array(isp_proc_handle_t isp_proc, esp_isp_lsc_gain_array_t *gain_array, size_t *out_array_size_per_channel)
{
ESP_RETURN_ON_FALSE(isp_proc, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
ESP_RETURN_ON_FALSE(isp_proc && gain_array && out_array_size_per_channel, ESP_ERR_INVALID_ARG, TAG, "invalid argument: null pointer");
ESP_RETURN_ON_FALSE(isp_proc->lsc_fsm == ISP_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "lsc is enabled already");
int num_grids_x_max = ISP_LSC_GET_GRIDS(ISP_LL_HSIZE_MAX);