/* SPDX-License-Identifier: ((GPL-2.0+ WITH Linux-syscall-note) OR MIT) */ /* * Rockchip ISP1 userspace API * Copyright (C) 2017 Rockchip Electronics Co., Ltd. */ #ifndef _RKISP1_CONFIG_H #define _RKISP1_CONFIG_H #include /* Defect Pixel Cluster Detection */ #define RKISP1_CIF_ISP_MODULE_DPCC (1U << 0) /* Black Level Subtraction */ #define RKISP1_CIF_ISP_MODULE_BLS (1U << 1) /* Sensor De-gamma */ #define RKISP1_CIF_ISP_MODULE_SDG (1U << 2) /* Histogram */ #define RKISP1_CIF_ISP_MODULE_HST (1U << 3) /* Lens Shade Control */ #define RKISP1_CIF_ISP_MODULE_LSC (1U << 4) /* Auto White Balance Gain */ #define RKISP1_CIF_ISP_MODULE_AWB_GAIN (1U << 5) /* Filter */ #define RKISP1_CIF_ISP_MODULE_FLT (1U << 6) /* Bayer Demosaic */ #define RKISP1_CIF_ISP_MODULE_BDM (1U << 7) /* Cross Talk */ #define RKISP1_CIF_ISP_MODULE_CTK (1U << 8) /* Gamma Out Curve */ #define RKISP1_CIF_ISP_MODULE_GOC (1U << 9) /* Color Processing */ #define RKISP1_CIF_ISP_MODULE_CPROC (1U << 10) /* Auto Focus Control */ #define RKISP1_CIF_ISP_MODULE_AFC (1U << 11) /* Auto White Balancing */ #define RKISP1_CIF_ISP_MODULE_AWB (1U << 12) /* Image Effect */ #define RKISP1_CIF_ISP_MODULE_IE (1U << 13) /* Auto Exposure Control */ #define RKISP1_CIF_ISP_MODULE_AEC (1U << 14) /* Wide Dynamic Range */ #define RKISP1_CIF_ISP_MODULE_WDR (1U << 15) /* Denoise Pre-Filter */ #define RKISP1_CIF_ISP_MODULE_DPF (1U << 16) /* Denoise Pre-Filter Strength */ #define RKISP1_CIF_ISP_MODULE_DPF_STRENGTH (1U << 17) #define RKISP1_CIF_ISP_CTK_COEFF_MAX 0x100 #define RKISP1_CIF_ISP_CTK_OFFSET_MAX 0x800 #define RKISP1_CIF_ISP_AE_MEAN_MAX_V10 25 #define RKISP1_CIF_ISP_AE_MEAN_MAX_V12 81 #define RKISP1_CIF_ISP_AE_MEAN_MAX RKISP1_CIF_ISP_AE_MEAN_MAX_V12 #define RKISP1_CIF_ISP_HIST_BIN_N_MAX_V10 16 #define RKISP1_CIF_ISP_HIST_BIN_N_MAX_V12 32 #define RKISP1_CIF_ISP_HIST_BIN_N_MAX RKISP1_CIF_ISP_HIST_BIN_N_MAX_V12 #define RKISP1_CIF_ISP_AFM_MAX_WINDOWS 3 #define RKISP1_CIF_ISP_DEGAMMA_CURVE_SIZE 17 #define RKISP1_CIF_ISP_BDM_MAX_TH 0xff /* * Black level compensation */ /* maximum value for horizontal start address */ #define RKISP1_CIF_ISP_BLS_START_H_MAX 0x00000fff /* maximum value for horizontal stop address */ #define RKISP1_CIF_ISP_BLS_STOP_H_MAX 0x00000fff /* maximum value for vertical start address */ #define RKISP1_CIF_ISP_BLS_START_V_MAX 0x00000fff /* maximum value for vertical stop address */ #define RKISP1_CIF_ISP_BLS_STOP_V_MAX 0x00000fff /* maximum is 2^18 = 262144*/ #define RKISP1_CIF_ISP_BLS_SAMPLES_MAX 0x00000012 /* maximum value for fixed black level */ #define RKISP1_CIF_ISP_BLS_FIX_SUB_MAX 0x00000fff /* minimum value for fixed black level */ #define RKISP1_CIF_ISP_BLS_FIX_SUB_MIN 0xfffff000 /* 13 bit range (signed)*/ #define RKISP1_CIF_ISP_BLS_FIX_MASK 0x00001fff /* * Automatic white balance measurements */ #define RKISP1_CIF_ISP_AWB_MAX_GRID 1 #define RKISP1_CIF_ISP_AWB_MAX_FRAMES 7 /* * Gamma out */ /* Maximum number of color samples supported */ #define RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V10 17 #define RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V12 34 #define RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V12 /* * Lens shade correction */ #define RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE 8 /* * The following matches the tuning process, * not the max capabilities of the chip. */ #define RKISP1_CIF_ISP_LSC_SAMPLES_MAX 17 /* * Histogram calculation */ #define RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V10 25 #define RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V12 81 #define RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V12 /* * Defect Pixel Cluster Correction */ #define RKISP1_CIF_ISP_DPCC_METHODS_MAX 3 /* * Denoising pre filter */ #define RKISP1_CIF_ISP_DPF_MAX_NLF_COEFFS 17 #define RKISP1_CIF_ISP_DPF_MAX_SPATIAL_COEFFS 6 /* * Measurement types */ #define RKISP1_CIF_ISP_STAT_AWB (1U << 0) #define RKISP1_CIF_ISP_STAT_AUTOEXP (1U << 1) #define RKISP1_CIF_ISP_STAT_AFM (1U << 2) #define RKISP1_CIF_ISP_STAT_HIST (1U << 3) /** * enum rkisp1_cif_isp_version - ISP variants * * @RKISP1_V10: used at least in rk3288 and rk3399 * @RKISP1_V11: declared in the original vendor code, but not used * @RKISP1_V12: used at least in rk3326 and px30 * @RKISP1_V13: used at least in rk1808 */ enum rkisp1_cif_isp_version { RKISP1_V10 = 10, RKISP1_V11, RKISP1_V12, RKISP1_V13, }; enum rkisp1_cif_isp_histogram_mode { RKISP1_CIF_ISP_HISTOGRAM_MODE_DISABLE, RKISP1_CIF_ISP_HISTOGRAM_MODE_RGB_COMBINED, RKISP1_CIF_ISP_HISTOGRAM_MODE_R_HISTOGRAM, RKISP1_CIF_ISP_HISTOGRAM_MODE_G_HISTOGRAM, RKISP1_CIF_ISP_HISTOGRAM_MODE_B_HISTOGRAM, RKISP1_CIF_ISP_HISTOGRAM_MODE_Y_HISTOGRAM }; enum rkisp1_cif_isp_awb_mode_type { RKISP1_CIF_ISP_AWB_MODE_MANUAL, RKISP1_CIF_ISP_AWB_MODE_RGB, RKISP1_CIF_ISP_AWB_MODE_YCBCR }; enum rkisp1_cif_isp_flt_mode { RKISP1_CIF_ISP_FLT_STATIC_MODE, RKISP1_CIF_ISP_FLT_DYNAMIC_MODE }; /** * enum rkisp1_cif_isp_exp_ctrl_autostop - stop modes * @RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_0: continuous measurement * @RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_1: stop measuring after a complete frame */ enum rkisp1_cif_isp_exp_ctrl_autostop { RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_0 = 0, RKISP1_CIF_ISP_EXP_CTRL_AUTOSTOP_1 = 1, }; /** * enum rkisp1_cif_isp_exp_meas_mode - Exposure measure mode * @RKISP1_CIF_ISP_EXP_MEASURING_MODE_0: Y = 16 + 0.25R + 0.5G + 0.1094B * @RKISP1_CIF_ISP_EXP_MEASURING_MODE_1: Y = (R + G + B) x (85/256) */ enum rkisp1_cif_isp_exp_meas_mode { RKISP1_CIF_ISP_EXP_MEASURING_MODE_0, RKISP1_CIF_ISP_EXP_MEASURING_MODE_1, }; /*---------- PART1: Input Parameters ------------*/ /** * struct rkisp1_cif_isp_window - measurement window. * * Measurements are calculated per window inside the frame. * This struct represents a window for a measurement. * * @h_offs: the horizontal offset of the window from the left of the frame in pixels. * @v_offs: the vertical offset of the window from the top of the frame in pixels. * @h_size: the horizontal size of the window in pixels * @v_size: the vertical size of the window in pixels. */ struct rkisp1_cif_isp_window { __u16 h_offs; __u16 v_offs; __u16 h_size; __u16 v_size; }; /** * struct rkisp1_cif_isp_bls_fixed_val - BLS fixed subtraction values * * The values will be subtracted from the sensor * values. Therefore a negative value means addition instead of subtraction! * * @r: Fixed (signed!) subtraction value for Bayer pattern R * @gr: Fixed (signed!) subtraction value for Bayer pattern Gr * @gb: Fixed (signed!) subtraction value for Bayer pattern Gb * @b: Fixed (signed!) subtraction value for Bayer pattern B */ struct rkisp1_cif_isp_bls_fixed_val { __s16 r; __s16 gr; __s16 gb; __s16 b; }; /** * struct rkisp1_cif_isp_bls_config - Configuration used by black level subtraction * * @enable_auto: Automatic mode activated means that the measured values * are subtracted. Otherwise the fixed subtraction * values will be subtracted. * @en_windows: enabled window * @bls_window1: Measurement window 1 size * @bls_window2: Measurement window 2 size * @bls_samples: Set amount of measured pixels for each Bayer position * (A, B,C and D) to 2^bls_samples. * @fixed_val: Fixed subtraction values */ struct rkisp1_cif_isp_bls_config { __u8 enable_auto; __u8 en_windows; struct rkisp1_cif_isp_window bls_window1; struct rkisp1_cif_isp_window bls_window2; __u8 bls_samples; struct rkisp1_cif_isp_bls_fixed_val fixed_val; }; /** * struct rkisp1_cif_isp_dpcc_methods_config - Methods Configuration used by DPCC * * Methods Configuration used by Defect Pixel Cluster Correction * * @method: Method enable bits * @line_thresh: Line threshold * @line_mad_fac: Line MAD factor * @pg_fac: Peak gradient factor * @rnd_thresh: Rank Neighbor Difference threshold * @rg_fac: Rank gradient factor */ struct rkisp1_cif_isp_dpcc_methods_config { __u32 method; __u32 line_thresh; __u32 line_mad_fac; __u32 pg_fac; __u32 rnd_thresh; __u32 rg_fac; }; /** * struct rkisp1_cif_isp_dpcc_config - Configuration used by DPCC * * Configuration used by Defect Pixel Cluster Correction * * @mode: dpcc output mode * @output_mode: whether use hard coded methods * @set_use: stage1 methods set * @methods: methods config * @ro_limits: rank order limits * @rnd_offs: differential rank offsets for rank neighbor difference */ struct rkisp1_cif_isp_dpcc_config { __u32 mode; __u32 output_mode; __u32 set_use; struct rkisp1_cif_isp_dpcc_methods_config methods[RKISP1_CIF_ISP_DPCC_METHODS_MAX]; __u32 ro_limits; __u32 rnd_offs; }; /** * struct rkisp1_cif_isp_gamma_corr_curve - gamma curve point definition y-axis (output). * * The reset values define a linear curve which has the same effect as bypass. Reset values are: * gamma_y[0] = 0x0000, gamma_y[1] = 0x0100, ... gamma_y[15] = 0x0f00, gamma_y[16] = 0xfff * * @gamma_y: the values for the y-axis of gamma curve points. Each value is 12 bit. */ struct rkisp1_cif_isp_gamma_corr_curve { __u16 gamma_y[RKISP1_CIF_ISP_DEGAMMA_CURVE_SIZE]; }; /** * struct rkisp1_cif_isp_gamma_curve_x_axis_pnts - De-Gamma Curve definition x increments * (sampling points). gamma_dx0 is for the lower samples (1-8), gamma_dx1 is for the * higher samples (9-16). The reset values for both fields is 0x44444444. This means * that each sample is 4 units away from the previous one on the x-axis. * * @gamma_dx0: gamma curve sample points definitions. Bits 0:2 for sample 1. Bit 3 unused. * Bits 4:6 for sample 2. bit 7 unused ... Bits 28:30 for sample 8. Bit 31 unused * @gamma_dx1: gamma curve sample points definitions. Bits 0:2 for sample 9. Bit 3 unused. * Bits 4:6 for sample 10. bit 7 unused ... Bits 28:30 for sample 16. Bit 31 unused */ struct rkisp1_cif_isp_gamma_curve_x_axis_pnts { __u32 gamma_dx0; __u32 gamma_dx1; }; /** * struct rkisp1_cif_isp_sdg_config - Configuration used by sensor degamma * * @curve_r: gamma curve point definition axis for red * @curve_g: gamma curve point definition axis for green * @curve_b: gamma curve point definition axis for blue * @xa_pnts: x axis increments */ struct rkisp1_cif_isp_sdg_config { struct rkisp1_cif_isp_gamma_corr_curve curve_r; struct rkisp1_cif_isp_gamma_corr_curve curve_g; struct rkisp1_cif_isp_gamma_corr_curve curve_b; struct rkisp1_cif_isp_gamma_curve_x_axis_pnts xa_pnts; }; /** * struct rkisp1_cif_isp_lsc_config - Configuration used by Lens shading correction * * @r_data_tbl: sample table red * @gr_data_tbl: sample table green (red) * @gb_data_tbl: sample table green (blue) * @b_data_tbl: sample table blue * @x_grad_tbl: gradient table x * @y_grad_tbl: gradient table y * @x_size_tbl: size table x * @y_size_tbl: size table y * @config_width: not used at the moment * @config_height: not used at the moment */ struct rkisp1_cif_isp_lsc_config { __u16 r_data_tbl[RKISP1_CIF_ISP_LSC_SAMPLES_MAX][RKISP1_CIF_ISP_LSC_SAMPLES_MAX]; __u16 gr_data_tbl[RKISP1_CIF_ISP_LSC_SAMPLES_MAX][RKISP1_CIF_ISP_LSC_SAMPLES_MAX]; __u16 gb_data_tbl[RKISP1_CIF_ISP_LSC_SAMPLES_MAX][RKISP1_CIF_ISP_LSC_SAMPLES_MAX]; __u16 b_data_tbl[RKISP1_CIF_ISP_LSC_SAMPLES_MAX][RKISP1_CIF_ISP_LSC_SAMPLES_MAX]; __u16 x_grad_tbl[RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE]; __u16 y_grad_tbl[RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE]; __u16 x_size_tbl[RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE]; __u16 y_size_tbl[RKISP1_CIF_ISP_LSC_SECTORS_TBL_SIZE]; __u16 config_width; __u16 config_height; }; /** * struct rkisp1_cif_isp_ie_config - Configuration used by image effects * * @effect: values from 'enum v4l2_colorfx'. Possible values are: V4L2_COLORFX_SEPIA, * V4L2_COLORFX_SET_CBCR, V4L2_COLORFX_AQUA, V4L2_COLORFX_EMBOSS, * V4L2_COLORFX_SKETCH, V4L2_COLORFX_BW, V4L2_COLORFX_NEGATIVE * @color_sel: bits 0:2 - colors bitmask (001 - blue, 010 - green, 100 - red). * bits 8:15 - Threshold value of the RGB colors for the color selection effect. * @eff_mat_1: 3x3 Matrix Coefficients for Emboss Effect 1 * @eff_mat_2: 3x3 Matrix Coefficients for Emboss Effect 2 * @eff_mat_3: 3x3 Matrix Coefficients for Emboss 3/Sketch 1 * @eff_mat_4: 3x3 Matrix Coefficients for Sketch Effect 2 * @eff_mat_5: 3x3 Matrix Coefficients for Sketch Effect 3 * @eff_tint: Chrominance increment values of tint (used for sepia effect) */ struct rkisp1_cif_isp_ie_config { __u16 effect; __u16 color_sel; __u16 eff_mat_1; __u16 eff_mat_2; __u16 eff_mat_3; __u16 eff_mat_4; __u16 eff_mat_5; __u16 eff_tint; }; /** * struct rkisp1_cif_isp_cproc_config - Configuration used by Color Processing * * @c_out_range: Chrominance pixel clipping range at output. * (0 for limit, 1 for full) * @y_in_range: Luminance pixel clipping range at output. * @y_out_range: Luminance pixel clipping range at output. * @contrast: 00~ff, 0.0~1.992 * @brightness: 80~7F, -128~+127 * @sat: saturation, 00~FF, 0.0~1.992 * @hue: 80~7F, -90~+87.188 */ struct rkisp1_cif_isp_cproc_config { __u8 c_out_range; __u8 y_in_range; __u8 y_out_range; __u8 contrast; __u8 brightness; __u8 sat; __u8 hue; }; /** * struct rkisp1_cif_isp_awb_meas_config - Configuration used by auto white balance * * @awb_mode: the awb meas mode. From enum rkisp1_cif_isp_awb_mode_type. * @awb_wnd: white balance measurement window (in pixels) * @max_y: only pixels values < max_y contribute to awb measurement, set to 0 * to disable this feature * @min_y: only pixels values > min_y contribute to awb measurement * @max_csum: Chrominance sum maximum value, only consider pixels with Cb+Cr, * smaller than threshold for awb measurements * @min_c: Chrominance minimum value, only consider pixels with Cb/Cr * each greater than threshold value for awb measurements * @frames: number of frames - 1 used for mean value calculation * (ucFrames=0 means 1 Frame) * @awb_ref_cr: reference Cr value for AWB regulation, target for AWB * @awb_ref_cb: reference Cb value for AWB regulation, target for AWB * @enable_ymax_cmp: enable Y_MAX compare (Not valid in RGB measurement mode.) */ struct rkisp1_cif_isp_awb_meas_config { /* * Note: currently the h and v offsets are mapped to grid offsets */ struct rkisp1_cif_isp_window awb_wnd; __u32 awb_mode; __u8 max_y; __u8 min_y; __u8 max_csum; __u8 min_c; __u8 frames; __u8 awb_ref_cr; __u8 awb_ref_cb; __u8 enable_ymax_cmp; }; /** * struct rkisp1_cif_isp_awb_gain_config - Configuration used by auto white balance gain * * All fields in this struct are 10 bit, where: * 0x100h = 1, unsigned integer value, range 0 to 4 with 8 bit fractional part. * * out_data_x = ( AWB_GAIN_X * in_data + 128) >> 8 * * @gain_red: gain value for red component. * @gain_green_r: gain value for green component in red line. * @gain_blue: gain value for blue component. * @gain_green_b: gain value for green component in blue line. */ struct rkisp1_cif_isp_awb_gain_config { __u16 gain_red; __u16 gain_green_r; __u16 gain_blue; __u16 gain_green_b; }; /** * struct rkisp1_cif_isp_flt_config - Configuration used by ISP filtering * * All 4 threshold fields (thresh_*) are 10 bits. * All 6 factor fields (fac_*) are 6 bits. * * @mode: ISP_FILT_MODE register fields (from enum rkisp1_cif_isp_flt_mode) * @grn_stage1: Green filter stage 1 select (range 0x0...0x8) * @chr_h_mode: Chroma filter horizontal mode * @chr_v_mode: Chroma filter vertical mode * @thresh_bl0: If thresh_bl1 < sum_grad < thresh_bl0 then fac_bl0 is selected (blurring th) * @thresh_bl1: If sum_grad < thresh_bl1 then fac_bl1 is selected (blurring th) * @thresh_sh0: If thresh_sh0 < sum_grad < thresh_sh1 then thresh_sh0 is selected (sharpening th) * @thresh_sh1: If thresh_sh1 < sum_grad then thresh_sh1 is selected (sharpening th) * @lum_weight: Parameters for luminance weight function. * @fac_sh1: filter factor for sharp1 level * @fac_sh0: filter factor for sharp0 level * @fac_mid: filter factor for mid level and for static filter mode * @fac_bl0: filter factor for blur 0 level * @fac_bl1: filter factor for blur 1 level (max blur) */ struct rkisp1_cif_isp_flt_config { __u32 mode; __u8 grn_stage1; __u8 chr_h_mode; __u8 chr_v_mode; __u32 thresh_bl0; __u32 thresh_bl1; __u32 thresh_sh0; __u32 thresh_sh1; __u32 lum_weight; __u32 fac_sh1; __u32 fac_sh0; __u32 fac_mid; __u32 fac_bl0; __u32 fac_bl1; }; /** * struct rkisp1_cif_isp_bdm_config - Configuration used by Bayer DeMosaic * * @demosaic_th: threshold for bayer demosaicing texture detection */ struct rkisp1_cif_isp_bdm_config { __u8 demosaic_th; }; /** * struct rkisp1_cif_isp_ctk_config - Configuration used by Cross Talk correction * * @coeff: color correction matrix. Values are 11-bit signed fixed-point numbers with 4 bit integer * and 7 bit fractional part, ranging from -8 (0x400) to +7.992 (0x3FF). 0 is * represented by 0x000 and a coefficient value of 1 as 0x080. * @ct_offset: Red, Green, Blue offsets for the crosstalk correction matrix */ struct rkisp1_cif_isp_ctk_config { __u16 coeff[3][3]; __u16 ct_offset[3]; }; enum rkisp1_cif_isp_goc_mode { RKISP1_CIF_ISP_GOC_MODE_LOGARITHMIC, RKISP1_CIF_ISP_GOC_MODE_EQUIDISTANT }; /** * struct rkisp1_cif_isp_goc_config - Configuration used by Gamma Out correction * * @mode: goc mode (from enum rkisp1_cif_isp_goc_mode) * @gamma_y: gamma out curve y-axis for all color components * * The number of entries of @gamma_y depends on the hardware revision * as is reported by the hw_revision field of the struct media_device_info * that is returned by ioctl MEDIA_IOC_DEVICE_INFO. * * Versions <= V11 have RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V10 * entries, versions >= V12 have RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES_V12 * entries. RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES is equal to the maximum * of the two. */ struct rkisp1_cif_isp_goc_config { __u32 mode; __u16 gamma_y[RKISP1_CIF_ISP_GAMMA_OUT_MAX_SAMPLES]; }; /** * struct rkisp1_cif_isp_hst_config - Configuration used by Histogram * * @mode: histogram mode (from enum rkisp1_cif_isp_histogram_mode) * @histogram_predivider: process every stepsize pixel, all other pixels are * skipped * @meas_window: coordinates of the measure window * @hist_weight: weighting factor for sub-windows * * The number of entries of @hist_weight depends on the hardware revision * as is reported by the hw_revision field of the struct media_device_info * that is returned by ioctl MEDIA_IOC_DEVICE_INFO. * * Versions <= V11 have RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V10 * entries, versions >= V12 have RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE_V12 * entries. RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE is equal to the maximum * of the two. */ struct rkisp1_cif_isp_hst_config { __u32 mode; __u8 histogram_predivider; struct rkisp1_cif_isp_window meas_window; __u8 hist_weight[RKISP1_CIF_ISP_HISTOGRAM_WEIGHT_GRIDS_SIZE]; }; /** * struct rkisp1_cif_isp_aec_config - Configuration used by Auto Exposure Control * * @mode: Exposure measure mode (from enum rkisp1_cif_isp_exp_meas_mode) * @autostop: stop mode (from enum rkisp1_cif_isp_exp_ctrl_autostop) * @meas_window: coordinates of the measure window */ struct rkisp1_cif_isp_aec_config { __u32 mode; __u32 autostop; struct rkisp1_cif_isp_window meas_window; }; /** * struct rkisp1_cif_isp_afc_config - Configuration used by Auto Focus Control * * @num_afm_win: max RKISP1_CIF_ISP_AFM_MAX_WINDOWS * @afm_win: coordinates of the meas window * @thres: threshold used for minimizing the influence of noise * @var_shift: the number of bits for the shift operation at the end of the * calculation chain. */ struct rkisp1_cif_isp_afc_config { __u8 num_afm_win; struct rkisp1_cif_isp_window afm_win[RKISP1_CIF_ISP_AFM_MAX_WINDOWS]; __u32 thres; __u32 var_shift; }; /** * enum rkisp1_cif_isp_dpf_gain_usage - dpf gain usage * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_DISABLED: don't use any gains in preprocessing stage * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_GAINS: use only the noise function gains from * registers DPF_NF_GAIN_R, ... * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_LSC_GAINS: use only the gains from LSC module * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_LSC_GAINS: use the noise function gains and the * gains from LSC module * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_GAINS: use only the gains from AWB module * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_LSC_GAINS: use the gains from AWB and LSC module * @RKISP1_CIF_ISP_DPF_GAIN_USAGE_MAX: upper border (only for an internal evaluation) */ enum rkisp1_cif_isp_dpf_gain_usage { RKISP1_CIF_ISP_DPF_GAIN_USAGE_DISABLED, RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_GAINS, RKISP1_CIF_ISP_DPF_GAIN_USAGE_LSC_GAINS, RKISP1_CIF_ISP_DPF_GAIN_USAGE_NF_LSC_GAINS, RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_GAINS, RKISP1_CIF_ISP_DPF_GAIN_USAGE_AWB_LSC_GAINS, RKISP1_CIF_ISP_DPF_GAIN_USAGE_MAX }; /** * enum rkisp1_cif_isp_dpf_rb_filtersize - Red and blue filter sizes * @RKISP1_CIF_ISP_DPF_RB_FILTERSIZE_13x9: red and blue filter kernel size 13x9 * (means 7x5 active pixel) * @RKISP1_CIF_ISP_DPF_RB_FILTERSIZE_9x9: red and blue filter kernel size 9x9 * (means 5x5 active pixel) */ enum rkisp1_cif_isp_dpf_rb_filtersize { RKISP1_CIF_ISP_DPF_RB_FILTERSIZE_13x9, RKISP1_CIF_ISP_DPF_RB_FILTERSIZE_9x9, }; /** * enum rkisp1_cif_isp_dpf_nll_scale_mode - dpf noise level scale mode * @RKISP1_CIF_ISP_NLL_SCALE_LINEAR: use a linear scaling * @RKISP1_CIF_ISP_NLL_SCALE_LOGARITHMIC: use a logarithmic scaling */ enum rkisp1_cif_isp_dpf_nll_scale_mode { RKISP1_CIF_ISP_NLL_SCALE_LINEAR, RKISP1_CIF_ISP_NLL_SCALE_LOGARITHMIC, }; /** * struct rkisp1_cif_isp_dpf_nll - Noise level lookup * * @coeff: Noise level Lookup coefficient * @scale_mode: dpf noise level scale mode (from enum rkisp1_cif_isp_dpf_nll_scale_mode) */ struct rkisp1_cif_isp_dpf_nll { __u16 coeff[RKISP1_CIF_ISP_DPF_MAX_NLF_COEFFS]; __u32 scale_mode; }; /** * struct rkisp1_cif_isp_dpf_rb_flt - Red blue filter config * * @fltsize: The filter size for the red and blue pixels * (from enum rkisp1_cif_isp_dpf_rb_filtersize) * @spatial_coeff: Spatial weights * @r_enable: enable filter processing for red pixels * @b_enable: enable filter processing for blue pixels */ struct rkisp1_cif_isp_dpf_rb_flt { __u32 fltsize; __u8 spatial_coeff[RKISP1_CIF_ISP_DPF_MAX_SPATIAL_COEFFS]; __u8 r_enable; __u8 b_enable; }; /** * struct rkisp1_cif_isp_dpf_g_flt - Green filter Configuration * * @spatial_coeff: Spatial weights * @gr_enable: enable filter processing for green pixels in green/red lines * @gb_enable: enable filter processing for green pixels in green/blue lines */ struct rkisp1_cif_isp_dpf_g_flt { __u8 spatial_coeff[RKISP1_CIF_ISP_DPF_MAX_SPATIAL_COEFFS]; __u8 gr_enable; __u8 gb_enable; }; /** * struct rkisp1_cif_isp_dpf_gain - Noise function Configuration * * @mode: dpf gain usage (from enum rkisp1_cif_isp_dpf_gain_usage) * @nf_r_gain: Noise function Gain that replaces the AWB gain for red pixels * @nf_b_gain: Noise function Gain that replaces the AWB gain for blue pixels * @nf_gr_gain: Noise function Gain that replaces the AWB gain * for green pixels in a red line * @nf_gb_gain: Noise function Gain that replaces the AWB gain * for green pixels in a blue line */ struct rkisp1_cif_isp_dpf_gain { __u32 mode; __u16 nf_r_gain; __u16 nf_b_gain; __u16 nf_gr_gain; __u16 nf_gb_gain; }; /** * struct rkisp1_cif_isp_dpf_config - Configuration used by De-noising pre-filter * * @gain: noise function gain * @g_flt: green filter config * @rb_flt: red blue filter config * @nll: noise level lookup */ struct rkisp1_cif_isp_dpf_config { struct rkisp1_cif_isp_dpf_gain gain; struct rkisp1_cif_isp_dpf_g_flt g_flt; struct rkisp1_cif_isp_dpf_rb_flt rb_flt; struct rkisp1_cif_isp_dpf_nll nll; }; /** * struct rkisp1_cif_isp_dpf_strength_config - strength of the filter * * @r: filter strength of the RED filter * @g: filter strength of the GREEN filter * @b: filter strength of the BLUE filter */ struct rkisp1_cif_isp_dpf_strength_config { __u8 r; __u8 g; __u8 b; }; /** * struct rkisp1_cif_isp_isp_other_cfg - Parameters for some blocks in rockchip isp1 * * @dpcc_config: Defect Pixel Cluster Correction config * @bls_config: Black Level Subtraction config * @sdg_config: sensor degamma config * @lsc_config: Lens Shade config * @awb_gain_config: Auto White balance gain config * @flt_config: filter config * @bdm_config: demosaic config * @ctk_config: cross talk config * @goc_config: gamma out config * @bls_config: black level subtraction config * @dpf_config: De-noising pre-filter config * @dpf_strength_config: dpf strength config * @cproc_config: color process config * @ie_config: image effects config */ struct rkisp1_cif_isp_isp_other_cfg { struct rkisp1_cif_isp_dpcc_config dpcc_config; struct rkisp1_cif_isp_bls_config bls_config; struct rkisp1_cif_isp_sdg_config sdg_config; struct rkisp1_cif_isp_lsc_config lsc_config; struct rkisp1_cif_isp_awb_gain_config awb_gain_config; struct rkisp1_cif_isp_flt_config flt_config; struct rkisp1_cif_isp_bdm_config bdm_config; struct rkisp1_cif_isp_ctk_config ctk_config; struct rkisp1_cif_isp_goc_config goc_config; struct rkisp1_cif_isp_dpf_config dpf_config; struct rkisp1_cif_isp_dpf_strength_config dpf_strength_config; struct rkisp1_cif_isp_cproc_config cproc_config; struct rkisp1_cif_isp_ie_config ie_config; }; /** * struct rkisp1_cif_isp_isp_meas_cfg - Rockchip ISP1 Measure Parameters * * @awb_meas_config: auto white balance config * @hst_config: histogram config * @aec_config: auto exposure config * @afc_config: auto focus config */ struct rkisp1_cif_isp_isp_meas_cfg { struct rkisp1_cif_isp_awb_meas_config awb_meas_config; struct rkisp1_cif_isp_hst_config hst_config; struct rkisp1_cif_isp_aec_config aec_config; struct rkisp1_cif_isp_afc_config afc_config; }; /** * struct rkisp1_params_cfg - Rockchip ISP1 Input Parameters Meta Data * * @module_en_update: mask the enable bits of which module should be updated * @module_ens: mask the enable value of each module, only update the module * which correspond bit was set in module_en_update * @module_cfg_update: mask the config bits of which module should be updated * @meas: measurement config * @others: other config */ struct rkisp1_params_cfg { __u32 module_en_update; __u32 module_ens; __u32 module_cfg_update; struct rkisp1_cif_isp_isp_meas_cfg meas; struct rkisp1_cif_isp_isp_other_cfg others; }; /*---------- PART2: Measurement Statistics ------------*/ /** * struct rkisp1_cif_isp_awb_meas - AWB measured values * * @cnt: White pixel count, number of "white pixels" found during last * measurement * @mean_y_or_g: Mean value of Y within window and frames, * Green if RGB is selected. * @mean_cb_or_b: Mean value of Cb within window and frames, * Blue if RGB is selected. * @mean_cr_or_r: Mean value of Cr within window and frames, * Red if RGB is selected. */ struct rkisp1_cif_isp_awb_meas { __u32 cnt; __u8 mean_y_or_g; __u8 mean_cb_or_b; __u8 mean_cr_or_r; }; /** * struct rkisp1_cif_isp_awb_stat - statistics automatic white balance data * * @awb_mean: Mean measured data */ struct rkisp1_cif_isp_awb_stat { struct rkisp1_cif_isp_awb_meas awb_mean[RKISP1_CIF_ISP_AWB_MAX_GRID]; }; /** * struct rkisp1_cif_isp_bls_meas_val - BLS measured values * * @meas_r: Mean measured value for Bayer pattern R * @meas_gr: Mean measured value for Bayer pattern Gr * @meas_gb: Mean measured value for Bayer pattern Gb * @meas_b: Mean measured value for Bayer pattern B */ struct rkisp1_cif_isp_bls_meas_val { __u16 meas_r; __u16 meas_gr; __u16 meas_gb; __u16 meas_b; }; /** * struct rkisp1_cif_isp_ae_stat - statistics auto exposure data * * @exp_mean: Mean luminance value of block xx * @bls_val: BLS measured values * * The number of entries of @exp_mean depends on the hardware revision * as is reported by the hw_revision field of the struct media_device_info * that is returned by ioctl MEDIA_IOC_DEVICE_INFO. * * Versions <= V11 have RKISP1_CIF_ISP_AE_MEAN_MAX_V10 entries, * versions >= V12 have RKISP1_CIF_ISP_AE_MEAN_MAX_V12 entries. * RKISP1_CIF_ISP_AE_MEAN_MAX is equal to the maximum of the two. * * Image is divided into 5x5 blocks on V10 and 9x9 blocks on V12. */ struct rkisp1_cif_isp_ae_stat { __u8 exp_mean[RKISP1_CIF_ISP_AE_MEAN_MAX]; struct rkisp1_cif_isp_bls_meas_val bls_val; }; /** * struct rkisp1_cif_isp_af_meas_val - AF measured values * * @sum: sharpness value * @lum: luminance value */ struct rkisp1_cif_isp_af_meas_val { __u32 sum; __u32 lum; }; /** * struct rkisp1_cif_isp_af_stat - statistics auto focus data * * @window: AF measured value of window x * * The module measures the sharpness in 3 windows of selectable size via * register settings(ISP_AFM_*_A/B/C) */ struct rkisp1_cif_isp_af_stat { struct rkisp1_cif_isp_af_meas_val window[RKISP1_CIF_ISP_AFM_MAX_WINDOWS]; }; /** * struct rkisp1_cif_isp_hist_stat - statistics histogram data * * @hist_bins: measured bin counters. Each bin is a 20 bits unsigned fixed point * type. Bits 0-4 are the fractional part and bits 5-19 are the * integer part. * * The window of the measurements area is divided to 5x5 sub-windows for * V10/V11 and to 9x9 sub-windows for V12. The histogram is then computed for * each sub-window independently and the final result is a weighted average of * the histogram measurements on all sub-windows. The window of the * measurements area and the weight of each sub-window are configurable using * struct @rkisp1_cif_isp_hst_config. * * The histogram contains 16 bins in V10/V11 and 32 bins in V12/V13. * * The number of entries of @hist_bins depends on the hardware revision * as is reported by the hw_revision field of the struct media_device_info * that is returned by ioctl MEDIA_IOC_DEVICE_INFO. * * Versions <= V11 have RKISP1_CIF_ISP_HIST_BIN_N_MAX_V10 entries, * versions >= V12 have RKISP1_CIF_ISP_HIST_BIN_N_MAX_V12 entries. * RKISP1_CIF_ISP_HIST_BIN_N_MAX is equal to the maximum of the two. */ struct rkisp1_cif_isp_hist_stat { __u32 hist_bins[RKISP1_CIF_ISP_HIST_BIN_N_MAX]; }; /** * struct rkisp1_cif_isp_stat - Rockchip ISP1 Statistics Data * * @awb: statistics data for automatic white balance * @ae: statistics data for auto exposure * @af: statistics data for auto focus * @hist: statistics histogram data */ struct rkisp1_cif_isp_stat { struct rkisp1_cif_isp_awb_stat awb; struct rkisp1_cif_isp_ae_stat ae; struct rkisp1_cif_isp_af_stat af; struct rkisp1_cif_isp_hist_stat hist; }; /** * struct rkisp1_stat_buffer - Rockchip ISP1 Statistics Meta Data * * @meas_type: measurement types (RKISP1_CIF_ISP_STAT_* definitions) * @frame_id: frame ID for sync * @params: statistics data */ struct rkisp1_stat_buffer { __u32 meas_type; __u32 frame_id; struct rkisp1_cif_isp_stat params; }; #endif /* _RKISP1_CONFIG_H */ 5'>825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019-2023, Raspberry Pi Ltd
 *
 * ipa_base.cpp - Raspberry Pi IPA base class
 */

#include "ipa_base.h"

#include <cmath>

#include <libcamera/base/log.h>
#include <libcamera/base/span.h>
#include <libcamera/control_ids.h>
#include <libcamera/property_ids.h>

#include "controller/af_algorithm.h"
#include "controller/af_status.h"
#include "controller/agc_algorithm.h"
#include "controller/awb_algorithm.h"
#include "controller/awb_status.h"
#include "controller/black_level_status.h"
#include "controller/ccm_algorithm.h"
#include "controller/ccm_status.h"
#include "controller/contrast_algorithm.h"
#include "controller/denoise_algorithm.h"
#include "controller/lux_status.h"
#include "controller/sharpen_algorithm.h"
#include "controller/statistics.h"

namespace libcamera {

using namespace std::literals::chrono_literals;
using utils::Duration;

namespace {

/* Number of frame length times to hold in the queue. */
constexpr unsigned int FrameLengthsQueueSize = 10;

/* Configure the sensor with these values initially. */
constexpr double defaultAnalogueGain = 1.0;
constexpr Duration defaultExposureTime = 20.0ms;
constexpr Duration defaultMinFrameDuration = 1.0s / 30.0;
constexpr Duration defaultMaxFrameDuration = 250.0s;

/*
 * Determine the minimum allowable inter-frame duration to run the controller
 * algorithms. If the pipeline handler provider frames at a rate higher than this,
 * we rate-limit the controller Prepare() and Process() calls to lower than or
 * equal to this rate.
 */
constexpr Duration controllerMinFrameDuration = 1.0s / 30.0;

/* List of controls handled by the Raspberry Pi IPA */
const ControlInfoMap::Map ipaControls{
	{ &controls::AeEnable, ControlInfo(false, true) },
	{ &controls::ExposureTime, ControlInfo(0, 66666) },
	{ &controls::AnalogueGain, ControlInfo(1.0f, 16.0f) },
	{ &controls::AeMeteringMode, ControlInfo(controls::AeMeteringModeValues) },
	{ &controls::AeConstraintMode, ControlInfo(controls::AeConstraintModeValues) },
	{ &controls::AeExposureMode, ControlInfo(controls::AeExposureModeValues) },
	{ &controls::ExposureValue, ControlInfo(-8.0f, 8.0f, 0.0f) },
	{ &controls::AeFlickerMode, ControlInfo(static_cast<int>(controls::FlickerOff),
						static_cast<int>(controls::FlickerManual),
						static_cast<int>(controls::FlickerOff)) },
	{ &controls::AeFlickerPeriod, ControlInfo(100, 1000000) },
	{ &controls::Brightness, ControlInfo(-1.0f, 1.0f, 0.0f) },
	{ &controls::Contrast, ControlInfo(0.0f, 32.0f, 1.0f) },
	{ &controls::Sharpness, ControlInfo(0.0f, 16.0f, 1.0f) },
	{ &controls::ScalerCrop, ControlInfo(Rectangle{}, Rectangle(65535, 65535, 65535, 65535), Rectangle{}) },
	{ &controls::FrameDurationLimits, ControlInfo(INT64_C(33333), INT64_C(120000)) },
	{ &controls::draft::NoiseReductionMode, ControlInfo(controls::draft::NoiseReductionModeValues) }
};

/* IPA controls handled conditionally, if the sensor is not mono */
const ControlInfoMap::Map ipaColourControls{
	{ &controls::AwbEnable, ControlInfo(false, true) },
	{ &controls::AwbMode, ControlInfo(controls::AwbModeValues) },
	{ &controls::ColourGains, ControlInfo(0.0f, 32.0f) },
	{ &controls::Saturation, ControlInfo(0.0f, 32.0f, 1.0f) },
};

/* IPA controls handled conditionally, if the lens has a focus control */
const ControlInfoMap::Map ipaAfControls{
	{ &controls::AfMode, ControlInfo(controls::AfModeValues) },
	{ &controls::AfRange, ControlInfo(controls::AfRangeValues) },
	{ &controls::AfSpeed, ControlInfo(controls::AfSpeedValues) },
	{ &controls::AfMetering, ControlInfo(controls::AfMeteringValues) },
	{ &controls::AfWindows, ControlInfo(Rectangle{}, Rectangle(65535, 65535, 65535, 65535), Rectangle{}) },
	{ &controls::AfTrigger, ControlInfo(controls::AfTriggerValues) },
	{ &controls::AfPause, ControlInfo(controls::AfPauseValues) },
	{ &controls::LensPosition, ControlInfo(0.0f, 32.0f, 1.0f) }
};

} /* namespace */

LOG_DEFINE_CATEGORY(IPARPI)

namespace ipa::RPi {

IpaBase::IpaBase()
	: controller_(), frameLengths_(FrameLengthsQueueSize, 0s), frameCount_(0),
	  mistrustCount_(0), lastRunTimestamp_(0), firstStart_(true), flickerState_({ 0, 0s })
{
}

IpaBase::~IpaBase()
{
}

int32_t IpaBase::init(const IPASettings &settings, const InitParams &params, InitResult *result)
{
	/*
	 * Load the "helper" for this sensor. This tells us all the device specific stuff
	 * that the kernel driver doesn't. We only do this the first time; we don't need
	 * to re-parse the metadata after a simple mode-switch for no reason.
	 */
	helper_ = std::unique_ptr<RPiController::CamHelper>(RPiController::CamHelper::create(settings.sensorModel));
	if (!helper_) {
		LOG(IPARPI, Error) << "Could not create camera helper for "
				   << settings.sensorModel;
		return -EINVAL;
	}

	/*
	 * Pass out the sensor config to the pipeline handler in order
	 * to setup the staggered writer class.
	 */
	int gainDelay, exposureDelay, vblankDelay, hblankDelay, sensorMetadata;
	helper_->getDelays(exposureDelay, gainDelay, vblankDelay, hblankDelay);
	sensorMetadata = helper_->sensorEmbeddedDataPresent();

	result->sensorConfig.gainDelay = gainDelay;
	result->sensorConfig.exposureDelay = exposureDelay;
	result->sensorConfig.vblankDelay = vblankDelay;
	result->sensorConfig.hblankDelay = hblankDelay;
	result->sensorConfig.sensorMetadata = sensorMetadata;

	/* Load the tuning file for this sensor. */
	int ret = controller_.read(settings.configurationFile.c_str());
	if (ret) {
		LOG(IPARPI, Error)
			<< "Failed to load tuning data file "
			<< settings.configurationFile;
		return ret;
	}

	lensPresent_ = params.lensPresent;

	controller_.initialise();

	/* Return the controls handled by the IPA */
	ControlInfoMap::Map ctrlMap = ipaControls;
	if (lensPresent_)
		ctrlMap.merge(ControlInfoMap::Map(ipaAfControls));

	monoSensor_ = params.sensorInfo.cfaPattern == properties::draft::ColorFilterArrangementEnum::MONO;
	if (!monoSensor_)
		ctrlMap.merge(ControlInfoMap::Map(ipaColourControls));

	result->controlInfo = ControlInfoMap(std::move(ctrlMap), controls::controls);

	return platformInit(params, result);
}

int32_t IpaBase::configure(const IPACameraSensorInfo &sensorInfo, const ConfigParams &params,
			   ConfigResult *result)
{
	sensorCtrls_ = params.sensorControls;

	if (!validateSensorControls()) {
		LOG(IPARPI, Error) << "Sensor control validation failed.";
		return -1;
	}

	if (lensPresent_) {
		lensCtrls_ = params.lensControls;
		if (!validateLensControls()) {
			LOG(IPARPI, Warning) << "Lens validation failed, "
					     << "no lens control will be available.";
			lensPresent_ = false;
		}
	}

	/* Setup a metadata ControlList to output metadata. */
	libcameraMetadata_ = ControlList(controls::controls);

	/* Re-assemble camera mode using the sensor info. */
	setMode(sensorInfo);

	mode_.transform = static_cast<libcamera::Transform>(params.transform);

	/* Pass the camera mode to the CamHelper to setup algorithms. */
	helper_->setCameraMode(mode_);

	/*
	 * Initialise this ControlList correctly, even if empty, in case the IPA is
	 * running is isolation mode (passing the ControlList through the IPC layer).
	 */
	ControlList ctrls(sensorCtrls_);

	/* The pipeline handler passes out the mode's sensitivity. */
	result->modeSensitivity = mode_.sensitivity;

	if (firstStart_) {
		/* Supply initial values for frame durations. */
		applyFrameDurations(defaultMinFrameDuration, defaultMaxFrameDuration);

		/* Supply initial values for gain and exposure. */
		AgcStatus agcStatus;
		agcStatus.shutterTime = defaultExposureTime;
		agcStatus.analogueGain = defaultAnalogueGain;
		applyAGC(&agcStatus, ctrls);

		/*
		 * Set the lens to the default (typically hyperfocal) position
		 * on first start.
		 */
		if (lensPresent_) {
			RPiController::AfAlgorithm *af =
				dynamic_cast<RPiController::AfAlgorithm *>(controller_.getAlgorithm("af"));

			if (af) {
				float defaultPos =
					ipaAfControls.at(&controls::LensPosition).def().get<float>();
				ControlList lensCtrl(lensCtrls_);
				int32_t hwpos;

				af->setLensPosition(defaultPos, &hwpos);
				lensCtrl.set(V4L2_CID_FOCUS_ABSOLUTE, hwpos);
				result->lensControls = std::move(lensCtrl);
			}
		}
	}

	result->sensorControls = std::move(ctrls);

	/*
	 * Apply the correct limits to the exposure, gain and frame duration controls
	 * based on the current sensor mode.
	 */
	ControlInfoMap::Map ctrlMap = ipaControls;
	ctrlMap[&controls::FrameDurationLimits] =
		ControlInfo(static_cast<int64_t>(mode_.minFrameDuration.get<std::micro>()),
			    static_cast<int64_t>(mode_.maxFrameDuration.get<std::micro>()));

	ctrlMap[&controls::AnalogueGain] =
		ControlInfo(static_cast<float>(mode_.minAnalogueGain),
			    static_cast<float>(mode_.maxAnalogueGain));

	ctrlMap[&controls::ExposureTime] =
		ControlInfo(static_cast<int32_t>(mode_.minShutter.get<std::micro>()),
			    static_cast<int32_t>(mode_.maxShutter.get<std::micro>()));

	/* Declare colour processing related controls for non-mono sensors. */
	if (!monoSensor_)
		ctrlMap.merge(ControlInfoMap::Map(ipaColourControls));

	/* Declare Autofocus controls, only if we have a controllable lens */
	if (lensPresent_)
		ctrlMap.merge(ControlInfoMap::Map(ipaAfControls));

	result->controlInfo = ControlInfoMap(std::move(ctrlMap), controls::controls);

	return platformConfigure(params, result);
}

void IpaBase::start(const ControlList &controls, StartResult *result)
{
	RPiController::Metadata metadata;

	if (!controls.empty()) {
		/* We have been given some controls to action before start. */
		applyControls(controls);
	}

	controller_.switchMode(mode_, &metadata);

	/* Reset the frame lengths queue state. */
	lastTimeout_ = 0s;
	frameLengths_.clear();
	frameLengths_.resize(FrameLengthsQueueSize, 0s);

	/* SwitchMode may supply updated exposure/gain values to use. */
	AgcStatus agcStatus;
	agcStatus.shutterTime = 0.0s;
	agcStatus.analogueGain = 0.0;

	metadata.get("agc.status", agcStatus);
	if (agcStatus.shutterTime && agcStatus.analogueGain) {
		ControlList ctrls(sensorCtrls_);
		applyAGC(&agcStatus, ctrls);
		result->controls = std::move(ctrls);
		setCameraTimeoutValue();
	}

	/*
	 * Initialise frame counts, and decide how many frames must be hidden or
	 * "mistrusted", which depends on whether this is a startup from cold,
	 * or merely a mode switch in a running system.
	 */
	frameCount_ = 0;
	if (firstStart_) {
		dropFrameCount_ = helper_->hideFramesStartup();
		mistrustCount_ = helper_->mistrustFramesStartup();

		/*
		 * Query the AGC/AWB for how many frames they may take to
		 * converge sufficiently. Where these numbers are non-zero
		 * we must allow for the frames with bad statistics
		 * (mistrustCount_) that they won't see. But if zero (i.e.
		 * no convergence necessary), no frames need to be dropped.
		 */
		unsigned int agcConvergenceFrames = 0;
		RPiController::AgcAlgorithm *agc = dynamic_cast<RPiController::AgcAlgorithm *>(
			controller_.getAlgorithm("agc"));
		if (agc) {
			agcConvergenceFrames = agc->getConvergenceFrames();
			if (agcConvergenceFrames)
				agcConvergenceFrames += mistrustCount_;
		}

		unsigned int awbConvergenceFrames = 0;
		RPiController::AwbAlgorithm *awb = dynamic_cast<RPiController::AwbAlgorithm *>(
			controller_.getAlgorithm("awb"));
		if (awb) {
			awbConvergenceFrames = awb->getConvergenceFrames();
			if (awbConvergenceFrames)
				awbConvergenceFrames += mistrustCount_;
		}

		dropFrameCount_ = std::max({ dropFrameCount_, agcConvergenceFrames, awbConvergenceFrames });
		LOG(IPARPI, Debug) << "Drop " << dropFrameCount_ << " frames on startup";
	} else {
		dropFrameCount_ = helper_->hideFramesModeSwitch();
		mistrustCount_ = helper_->mistrustFramesModeSwitch();
	}

	result->dropFrameCount = dropFrameCount_;

	firstStart_ = false;
	lastRunTimestamp_ = 0;

	platformStart(controls, result);
}

void IpaBase::mapBuffers(const std::vector<IPABuffer> &buffers)
{
	for (const IPABuffer &buffer : buffers) {
		const FrameBuffer fb(buffer.planes);
		buffers_.emplace(buffer.id,
				 MappedFrameBuffer(&fb, MappedFrameBuffer::MapFlag::ReadWrite));
	}
}

void IpaBase::unmapBuffers(const std::vector<unsigned int> &ids)
{
	for (unsigned int id : ids) {
		auto it = buffers_.find(id);
		if (it == buffers_.end())
			continue;

		buffers_.erase(id);
	}
}

void IpaBase::prepareIsp(const PrepareParams &params)
{
	applyControls(params.requestControls);

	/*
	 * At start-up, or after a mode-switch, we may want to
	 * avoid running the control algos for a few frames in case
	 * they are "unreliable".
	 */
	int64_t frameTimestamp = params.sensorControls.get(controls::SensorTimestamp).value_or(0);
	unsigned int ipaContext = params.ipaContext % rpiMetadata_.size();
	RPiController::Metadata &rpiMetadata = rpiMetadata_[ipaContext];
	Span<uint8_t> embeddedBuffer;

	rpiMetadata.clear();
	fillDeviceStatus(params.sensorControls, ipaContext);

	if (params.buffers.embedded) {
		/*
		 * Pipeline handler has supplied us with an embedded data buffer,
		 * we must pass it to the CamHelper for parsing.
		 */
		auto it = buffers_.find(params.buffers.embedded);
		ASSERT(it != buffers_.end());
		embeddedBuffer = it->second.planes()[0];
	}

	/*
	 * AGC wants to know the algorithm status from the time it actioned the
	 * sensor exposure/gain changes. So fetch it from the metadata list
	 * indexed by the IPA cookie returned, and put it in the current frame
	 * metadata.
	 */
	AgcStatus agcStatus;
	RPiController::Metadata &delayedMetadata = rpiMetadata_[params.delayContext];
	if (!delayedMetadata.get<AgcStatus>("agc.status", agcStatus))
		rpiMetadata.set("agc.delayed_status", agcStatus);

	/*
	 * This may overwrite the DeviceStatus using values from the sensor
	 * metadata, and may also do additional custom processing.
	 */
	helper_->prepare(embeddedBuffer, rpiMetadata);

	/* Allow a 10% margin on the comparison below. */
	Duration delta = (frameTimestamp - lastRunTimestamp_) * 1.0ns;
	if (lastRunTimestamp_ && frameCount_ > dropFrameCount_ &&
	    delta < controllerMinFrameDuration * 0.9) {
		/*
		 * Ensure we merge the previous frame's metadata with the current
		 * frame. This will not overwrite exposure/gain values for the
		 * current frame, or any other bits of metadata that were added
		 * in helper_->Prepare().
		 */
		RPiController::Metadata &lastMetadata =
			rpiMetadata_[(ipaContext ? ipaContext : rpiMetadata_.size()) - 1];
		rpiMetadata.mergeCopy(lastMetadata);
		processPending_ = false;
	} else {
		processPending_ = true;
		lastRunTimestamp_ = frameTimestamp;
	}

	/*
	 * If the statistics are inline (i.e. already available with the Bayer
	 * frame), call processStats() now before prepare().
	 */
	if (controller_.getHardwareConfig().statsInline)
		processStats({ params.buffers, params.ipaContext });

	/* Do we need/want to call prepare? */
	if (processPending_) {
		controller_.prepare(&rpiMetadata);
		/* Actually prepare the ISP parameters for the frame. */
		platformPrepareIsp(params, rpiMetadata);
	}

	frameCount_++;

	/* If the statistics are inline the metadata can be returned early. */
	if (controller_.getHardwareConfig().statsInline)
		reportMetadata(ipaContext);

	/* Ready to push the input buffer into the ISP. */
	prepareIspComplete.emit(params.buffers, false);
}

void IpaBase::processStats(const ProcessParams &params)
{
	unsigned int ipaContext = params.ipaContext % rpiMetadata_.size();

	if (processPending_ && frameCount_ >= mistrustCount_) {
		RPiController::Metadata &rpiMetadata = rpiMetadata_[ipaContext];

		auto it = buffers_.find(params.buffers.stats);
		if (it == buffers_.end()) {
			LOG(IPARPI, Error) << "Could not find stats buffer!";
			return;
		}

		RPiController::StatisticsPtr statistics = platformProcessStats(it->second.planes()[0]);

		/* reportMetadata() will pick this up and set the FocusFoM metadata */