diff --git a/multimedia/libcamera/Makefile b/multimedia/libcamera/Makefile index 04d99f5a99..2510765676 100644 --- a/multimedia/libcamera/Makefile +++ b/multimedia/libcamera/Makefile @@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=libcamera PKG_VERSION:=0.7.2 -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_SOURCE_PROTO:=git PKG_SOURCE_URL:=https://git.libcamera.org/libcamera/libcamera.git @@ -78,7 +78,7 @@ endef define Package/libcamera-cam $(call Package/libcamera/Default) TITLE:=libcamera command line capture tool - DEPENDS:=+libcamera +libevent2 +libevent2-pthreads + DEPENDS:=+libcamera +libevent2 +libevent2-pthreads +libtiff endef define Package/libcamera-cam/description @@ -127,7 +127,7 @@ MESON_ARGS += \ -Dlc-compliance=disabled \ -Dlibdw=disabled \ -Dlibunwind=disabled \ - -Dapps-output-dng=disabled \ + -Dapps-output-dng=enabled \ -Dpipelines=imx8-isi,ipu3,mali-c55,rkisp1,rkisp2,rpi/vc4,simple,uvcvideo \ -Dpycamera=disabled \ -Dqcam=disabled \ diff --git a/multimedia/libcamera/patches/212-pipeline-rkisp2-support-the-raw-readback-from-the.patch b/multimedia/libcamera/patches/212-pipeline-rkisp2-support-the-raw-readback-from-the.patch index 4b3be65ea5..c9f70a5882 100644 --- a/multimedia/libcamera/patches/212-pipeline-rkisp2-support-the-raw-readback-from-the.patch +++ b/multimedia/libcamera/patches/212-pipeline-rkisp2-support-the-raw-readback-from-the.patch @@ -1,4 +1,4 @@ -From af1faca420e32d380a21b87508df3b55845919d1 Mon Sep 17 00:00:00 2001 +From f7ac6188a0bf7e938e197eac80b586c88b7ea35a Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Fri, 16 Oct 2026 23:59:28 +0100 Subject: [PATCH] pipeline: rkisp2: Support the raw readback from the RV1126 @@ -18,9 +18,8 @@ VICAP is the frame source of the ISP through an in-kernel binding: - start the main path first and the readback node last, as the ISP selects the readback as its input when the main path starts, and stop them in the opposite order; - - size the buffers that the VICAP capture node allocates for the - readback, which has more lines than the frame, while the capture - node stays the allocator because the VICAP has no IOMMU; + - configure the VICAP capture node, which allocates the buffers shared + with the readback because the VICAP has no IOMMU; - apply the delayed sensor controls from the VICAP frame timing, as the ISP frame start comes a full frame after the sensor started the frame; - keep six requests in flight, so that the sensor controls of a request @@ -30,8 +29,8 @@ VICAP is the frame source of the ISP through an in-kernel binding: Assisted-by: LLM Signed-off-by: Daniel Golle --- - src/libcamera/pipeline/rkisp2/rkisp2.cpp | 241 ++++++++++++++++------- - 1 file changed, 173 insertions(+), 68 deletions(-) + src/libcamera/pipeline/rkisp2/rkisp2.cpp | 226 ++++++++++++++++------- + 1 file changed, 158 insertions(+), 68 deletions(-) --- a/src/libcamera/pipeline/rkisp2/rkisp2.cpp +++ b/src/libcamera/pipeline/rkisp2/rkisp2.cpp @@ -182,34 +181,19 @@ Signed-off-by: Daniel Golle * \todo Figure out if these should go in the pipeline handler * or camera data */ -@@ -809,6 +840,26 @@ int PipelineHandlerRkISP2::configure(Cam +@@ -809,6 +840,11 @@ int PipelineHandlerRkISP2::configure(Cam if (ret) return ret; -+ /* -+ * The VICAP capture node allocates the buffers shared with the raw -+ * readback node, which may read more lines than the frame has. Ask -+ * the capture node for buffers of the readback size. -+ */ -+ unsigned int rawSize = vicapOutputFormat.planes[0].size; -+ + LOG(RkISP2, Debug) << "Configuring VICAP capture node with : " << vicapOutputFormat; + ret = data->video_->setFormat(&vicapOutputFormat); + if (ret) + return ret; -+ -+ if (vicapOutputFormat.planes[0].size < rawSize) { -+ LOG(RkISP2, Error) -+ << "VICAP buffers of " << vicapOutputFormat.planes[0].size -+ << " bytes are too small for the raw readback (" -+ << rawSize << " bytes)"; -+ return -EINVAL; -+ } + LOG(RkISP2, Debug) << "Configuring ISP input with: " << format; ret = isp_->setFormat(0, &format); if (ret) -@@ -929,6 +980,7 @@ int PipelineHandlerRkISP2::start(Camera +@@ -929,6 +965,7 @@ int PipelineHandlerRkISP2::start(Camera return ret; } actions += [&]() { data->ipa_->stop(); }; @@ -217,7 +201,7 @@ Signed-off-by: Daniel Golle ret = data->param_->streamOn(); if (ret) { -@@ -987,13 +1039,12 @@ int PipelineHandlerRkISP2::start(Camera +@@ -987,13 +1024,12 @@ int PipelineHandlerRkISP2::start(Camera actions += [&]() { data->video_->streamOff(); }; @@ -236,7 +220,7 @@ Signed-off-by: Daniel Golle ret = data->stat_->streamOn(); if (ret) { LOG(RkISP2, Error) -@@ -1007,6 +1058,12 @@ int PipelineHandlerRkISP2::start(Camera +@@ -1007,6 +1043,12 @@ int PipelineHandlerRkISP2::start(Camera return ret; actions += [&]() { data->mainPath_->streamOff(); }; @@ -249,7 +233,7 @@ Signed-off-by: Daniel Golle } if (controls) { -@@ -1015,9 +1072,6 @@ int PipelineHandlerRkISP2::start(Camera +@@ -1015,9 +1057,6 @@ int PipelineHandlerRkISP2::start(Camera return ret; } @@ -259,7 +243,7 @@ Signed-off-by: Daniel Golle actions.release(); return 0; } -@@ -1027,21 +1081,28 @@ void PipelineHandlerRkISP2::stopDevice(C +@@ -1027,21 +1066,28 @@ void PipelineHandlerRkISP2::stopDevice(C RkISP2CameraData *data = cameraData(camera); bool useMP = data->usingIsp_ && !data->isRaw_; @@ -293,7 +277,7 @@ Signed-off-by: Daniel Golle data->mainPath_->releaseBuffers(); data->stat_->streamOff(); -@@ -1126,8 +1187,6 @@ bool PipelineHandlerRkISP2::createCamera +@@ -1126,8 +1172,6 @@ bool PipelineHandlerRkISP2::createCamera data->delayedCtrls_ = std::make_unique(data->sensor_->device(), params); @@ -302,7 +286,7 @@ Signed-off-by: Daniel Golle int ret = data->loadIPA(); if (ret) { -@@ -1155,47 +1214,48 @@ bool PipelineHandlerRkISP2::createCamera +@@ -1155,47 +1199,48 @@ bool PipelineHandlerRkISP2::createCamera return true; } @@ -381,7 +365,7 @@ Signed-off-by: Daniel Golle if (sensor_) break; } -@@ -1205,6 +1265,45 @@ bool PipelineHandlerRkISP2::match(Device +@@ -1205,6 +1250,45 @@ bool PipelineHandlerRkISP2::match(Device return false; } @@ -427,7 +411,7 @@ Signed-off-by: Daniel Golle const GlobalConfiguration &configuration = cameraManager()->_d()->configuration(); bool usingIsp = configuration.configuration()["pipelines"]["rkisp2"]["isp_enable"].get(true); if (!usingIsp) { -@@ -1241,6 +1340,12 @@ bool PipelineHandlerRkISP2::match(Device +@@ -1241,6 +1325,12 @@ bool PipelineHandlerRkISP2::match(Device return false; } diff --git a/multimedia/libcamera/patches/216-pipeline-rkisp2-honour-requested-stream-strides.patch b/multimedia/libcamera/patches/216-pipeline-rkisp2-honour-requested-stream-strides.patch index d93629d057..2fa4acc5c4 100644 --- a/multimedia/libcamera/patches/216-pipeline-rkisp2-honour-requested-stream-strides.patch +++ b/multimedia/libcamera/patches/216-pipeline-rkisp2-honour-requested-stream-strides.patch @@ -34,7 +34,7 @@ Signed-off-by: Daniel Golle cfg.bufferCount = kRkISP2StreamBufferCount; std::transform(rawFormats.begin(), rawFormats.end(), -@@ -880,6 +888,7 @@ int PipelineHandlerRkISP2::configure(Cam +@@ -865,6 +873,7 @@ int PipelineHandlerRkISP2::configure(Cam outputFormat.fourcc = mainPathFormat(mainPath_.get(), cfg.pixelFormat); outputFormat.size = cfg.size; outputFormat.colorSpace = cfg.colorSpace; diff --git a/multimedia/libcamera/patches/218-include-linux-rkisp2-config-h-add-the-ispp-definitions.patch b/multimedia/libcamera/patches/218-include-linux-rkisp2-config-h-add-the-ispp-definitions.patch index 2bffb65b24..fbc0b38bde 100644 --- a/multimedia/libcamera/patches/218-include-linux-rkisp2-config-h-add-the-ispp-definitions.patch +++ b/multimedia/libcamera/patches/218-include-linux-rkisp2-config-h-add-the-ispp-definitions.patch @@ -42,10 +42,11 @@ Signed-off-by: Daniel Golle RKISP3_V0 = 30, }; -@@ -668,4 +671,616 @@ struct rkisp2_params_awb_meas { +@@ -668,4 +671,624 @@ struct rkisp2_params_awb_meas { sizeof(struct rkisp2_params_hist_big) * 3 +\ sizeof(struct rkisp2_params_awb_meas)) +-#endif /* _RKISP2_CONFIG_H */ +/*---------- ISPP (image post-processing) ------------*/ + +/* @@ -67,14 +68,6 @@ Signed-off-by: Daniel Golle +#define RKISPP_ORB_BRIEF_NUM 15 +#define RKISPP_ORB_DATA_NUM 10000 + -+/* -+ * Maximum number of FEC mesh points the driver accepts per frame. This -+ * covers the densest mesh grid (16 pixel vertex spacing) at the maximum -+ * supported input resolution and matches the 131072 point mesh buffers of -+ * the ISPP FEC engine. -+ */ -+#define RKISPP_FEC_MESH_POINTS_MAX 131072 -+ +/* ISPP TNR curve and table sizes */ +#define RKISPP_TNR_SIGMA_CURVE_SIZE 17 +#define RKISPP_TNR_LUMA_CURVE_SIZE 6 @@ -130,7 +123,7 @@ Signed-off-by: Daniel Golle + * @RKISPP_PARAMS_BLOCK_TNR: Temporal noise reduction + * @RKISPP_PARAMS_BLOCK_NR: Noise reduction (luma and chroma) + * @RKISPP_PARAMS_BLOCK_SHP: Sharpening -+ * @RKISPP_PARAMS_BLOCK_FEC: FEC frame estimation (motion mesh) ++ * @RKISPP_PARAMS_BLOCK_FEC: FEC fisheye and lens distortion correction + * @RKISPP_PARAMS_BLOCK_ORB: ORB feature detection + */ +enum rkispp_params_block_type { @@ -145,11 +138,9 @@ Signed-off-by: Daniel Golle + * enum rkispp_stats_block_type - Rockchip ISPP extensible stats block types + * + * @RKISPP_STATS_BLOCK_ORB: ORB feature points measured in the frame -+ * @RKISPP_STATS_BLOCK_FEC_MESH: FEC mesh measured in the frame + */ +enum rkispp_stats_block_type { + RKISPP_STATS_BLOCK_ORB, -+ RKISPP_STATS_BLOCK_FEC_MESH, +}; + +/** @@ -484,34 +475,25 @@ Signed-off-by: Daniel Golle +/** + * struct rkispp_params_fec - Rockchip ISPP params FEC config + * -+ * Rockchip ISPP parameters FEC (frame estimation) motion mesh configuration -+ * block. Identified by :c:type:`RKISPP_PARAMS_BLOCK_FEC`. ++ * Rockchip ISPP parameters FEC (fisheye and lens distortion correction) ++ * configuration block. Identified by :c:type:`RKISPP_PARAMS_BLOCK_FEC`. + * -+ * The FEC engine measures a mesh of motion vectors between consecutive -+ * frames across the pipeline; the mesh of the measured frame is reported -+ * in the RKISPP_STATS_BLOCK_FEC_MESH stats block. ++ * The FEC engine remaps the picture through the correction mesh queued on ++ * the rkispp_fec_mesh video device (struct rkispp_fec_mesh). The module ++ * can only be enabled once a mesh has been queued. + * + * @header: The ISPP parameters block header -+ * @mesh_density: 0 for the dense mesh grid (16 pixel vertex spacing, -+ * inputs up to 1920 pixels wide), 1 for the coarse mesh grid -+ * (32 pixel vertex spacing, wider inputs) -+ * @crop_en: Enable the measurement crop window below ++ * @crop_en: Enable the output crop window below + * @reserved: padding -+ * @crop_width: Measurement crop window width in pixels (16 bits) -+ * @crop_height: Measurement crop window height in pixels (16 bits) -+ * @mesh_size: Number of mesh points to measure for this frame, must not -+ * exceed the grid size of the input geometry -+ * @reserved1: padding ++ * @crop_width: Output crop window width in pixels (16 bits) ++ * @crop_height: Output crop window height in pixels (16 bits) + */ +struct rkispp_params_fec { + struct v4l2_isp_block_header header; -+ __u8 mesh_density; + __u8 crop_en; -+ __u8 reserved[2]; ++ __u8 reserved[3]; + __u16 crop_width; + __u16 crop_height; -+ __u32 mesh_size; -+ __u8 reserved1[4]; +} __attribute__((aligned(8))); + +/** @@ -579,52 +561,79 @@ Signed-off-by: Daniel Golle + struct rkispp_stats_orb_point points[RKISPP_ORB_DATA_NUM]; +} __attribute__((aligned(8))); + -+/** -+ * struct rkispp_stats_fec_mesh - Rockchip ISPP FEC mesh stats -+ * -+ * Rockchip ISPP statistics FEC mesh block. -+ * Identified by :c:type:`RKISPP_STATS_BLOCK_FEC_MESH`. -+ * -+ * The frame the mesh belongs to is given by the buffer sequence of the -+ * stats buffer carrying this block. -+ * -+ * @header: The ISPP stats block header -+ * @mesh_width: Mesh grid width in vertices of this frame -+ * @mesh_height: Mesh grid height in vertices of this frame -+ * @points: Number of valid mesh points -+ * @xint: Mesh point x coordinates, integer part in pixels (16 bits) -+ * @xfra: Mesh point x coordinates, fractional part (8 bits) -+ * @yint: Mesh point y coordinates, integer part in pixels (16 bits) -+ * @yfra: Mesh point y coordinates, fractional part (8 bits) -+ * -+ * The arrays hold one entry per mesh point, only the first @points -+ * entries are valid. -+ */ -+struct rkispp_stats_fec_mesh { -+ struct v4l2_isp_block_header header; -+ __u16 mesh_width; -+ __u16 mesh_height; -+ __u32 points; -+ __u16 xint[RKISPP_FEC_MESH_POINTS_MAX]; -+ __u8 xfra[RKISPP_FEC_MESH_POINTS_MAX]; -+ __u16 yint[RKISPP_FEC_MESH_POINTS_MAX]; -+ __u8 yfra[RKISPP_FEC_MESH_POINTS_MAX]; -+} __attribute__((aligned(8))); ++#define RKISPP_STATS_MAX_SIZE sizeof(struct rkispp_stats_orb) + -+#define RKISPP_STATS_MAX_SIZE \ -+ (sizeof(struct rkispp_stats_orb) +\ -+ sizeof(struct rkispp_stats_fec_mesh)) ++/** ++ * enum rkispp_fec_mesh_density - Rockchip ISPP FEC mesh grid density ++ * ++ * @RKISPP_FEC_MESH_DENSE: one vertex every 16 pixels and 8 lines, for ++ * pictures up to 1920 pixels wide ++ * @RKISPP_FEC_MESH_COARSE: one vertex every 32 pixels and 16 lines, the ++ * only density for pictures wider than 1920 pixels ++ */ ++enum rkispp_fec_mesh_density { ++ RKISPP_FEC_MESH_DENSE, ++ RKISPP_FEC_MESH_COARSE, ++}; ++ ++/** ++ * struct rkispp_fec_mesh - Rockchip ISPP FEC correction mesh ++ * ++ * Layout of a buffer of the rkispp_fec_mesh metadata output video device ++ * (V4L2_META_FMT_RKISPP_FEC_MESH). The FEC engine corrects the picture by ++ * backward mapping: for every vertex of a grid over the output picture ++ * the mesh holds the source position the engine samples, as an integer ++ * table (pixels) and a fraction table (1/256 pixel) for x and for y. The ++ * positions must stay between 1 and width - 3 and between 1 and ++ * height - 3; a vertex outside that range stalls the engine. A mesh ++ * queued on the device replaces the one in use from the next frame on, ++ * and the buffer is returned once the engine holds its own copy. ++ * ++ * The grid has one vertex every 16 pixels and 8 lines (dense) or every ++ * 32 pixels and 16 lines (coarse) over the picture size rounded up to a ++ * multiple of 32, walked in strips of 128 lines: W / 16 + 1 (dense) or ++ * W / 32 + 1 (coarse) vertices per row, 17 (dense) or 9 (coarse) rows per ++ * strip with the boundary row shared by two strips present in both, and ++ * L / 8 + 1 or L / 16 + 1 rows for the L lines of the last strip. Within a ++ * strip the tables hold the vertices column by column, the rows of one ++ * column consecutively, and the strips follow each other. @mesh_size is ++ * the resulting vertex count. ++ * ++ * @width: Width of the picture the mesh was generated for, in pixels; ++ * must match the ISPP output format ++ * @height: Height of that picture in pixels ++ * @density: Grid density, from enum rkispp_fec_mesh_density ++ * @reserved: padding ++ * @mesh_size: Number of vertices in each table ++ * @xint_offset: Byte offset of the x integer table (2 bytes per vertex) ++ * from the start of the buffer, a multiple of 16 ++ * @xfra_offset: Byte offset of the x fraction table (1 byte per vertex) ++ * @yint_offset: Byte offset of the y integer table (2 bytes per vertex) ++ * @yfra_offset: Byte offset of the y fraction table (1 byte per vertex) ++ */ ++struct rkispp_fec_mesh { ++ __u32 width; ++ __u32 height; ++ __u8 density; ++ __u8 reserved[3]; ++ __u32 mesh_size; ++ __u32 xint_offset; ++ __u32 xfra_offset; ++ __u32 yint_offset; ++ __u32 yfra_offset; ++}; + +/* -+ * Rockchip ISPP stand-alone FEC (frame estimation) mem2mem node. ++ * Rockchip ISPP stand-alone FEC mem2mem node. + */ + +/** + * struct rkispp_fec_in_out - Rockchip ISPP FEC m2m request + * -+ * One request measures the motion mesh between two consecutive frames -+ * held in the in picture and reports the result in the out picture and -+ * the four mesh arrays. ++ * One request remaps the in picture through the mesh held in the four ++ * mesh arrays into the out picture. The arrays follow the layout given ++ * for struct rkispp_fec_mesh, with the density chosen by the picture ++ * width. + * + * @width: Picture width in pixels + * @height: Picture height in pixels @@ -634,13 +643,13 @@ Signed-off-by: Daniel Golle + * chroma plane right after the full luma plane) + * @out_pic_fd: dma-buf fd of the out picture (same layout) + * @mesh_xint_fd: dma-buf fd of the mesh x integer array (2 bytes per -+ * mesh point, at least cal_fec_mesh() * 2 bytes) ++ * vertex) + * @mesh_xfra_fd: dma-buf fd of the mesh x fraction array (1 byte per -+ * mesh point) ++ * vertex) + * @mesh_yint_fd: dma-buf fd of the mesh y integer array (2 bytes per -+ * mesh point, at least cal_fec_mesh() * 2 bytes) ++ * vertex) + * @mesh_yfra_fd: dma-buf fd of the mesh y fraction array (1 byte per -+ * mesh point) ++ * vertex) + */ +struct rkispp_fec_in_out { + __u32 width; @@ -658,4 +667,4 @@ Signed-off-by: Daniel Golle +#define RKISPP_CMD_FEC_IN_OUT \ + _IOW('V', BASE_VIDIOC_PRIVATE + 10, struct rkispp_fec_in_out) + - #endif /* _RKISP2_CONFIG_H */ ++#endif /* _UAPI_RKISP2_CONFIG_H */ diff --git a/multimedia/libcamera/patches/219-ipa-rkisp2-add-the-ispp-parameters-path.patch b/multimedia/libcamera/patches/219-ipa-rkisp2-add-the-ispp-parameters-path.patch index 1b32f3b65d..b8de706b64 100644 --- a/multimedia/libcamera/patches/219-ipa-rkisp2-add-the-ispp-parameters-path.patch +++ b/multimedia/libcamera/patches/219-ipa-rkisp2-add-the-ispp-parameters-path.patch @@ -73,7 +73,7 @@ Signed-off-by: Daniel Golle - --- /dev/null +++ b/src/ipa/rkisp2/algorithms/ispp.cpp -@@ -0,0 +1,408 @@ +@@ -0,0 +1,407 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2026, Daniel Golle @@ -109,7 +109,8 @@ Signed-off-by: Daniel Golle + * \brief RkISPP module parameters from the tuning file + * + * The ISPP post-processor (temporal noise reduction, noise reduction, -+ * sharpening, FEC motion mesh and ORB feature detection) takes its ++ * sharpening, FEC lens distortion correction and ORB feature detection) ++ * takes its + * parameters through its own params node. This algorithm sends the modules + * configured in the tuning file with every frame, each tuning dictionary + * filling the fields of the corresponding UAPI block by name: @@ -307,11 +308,9 @@ Signed-off-by: Daniel Golle +}; + +constexpr Field fecFields[] = { -+ ISPP_FIELD(rkispp_params_fec, mesh_density), + ISPP_FIELD(rkispp_params_fec, crop_en), + ISPP_FIELD(rkispp_params_fec, crop_width), + ISPP_FIELD(rkispp_params_fec, crop_height), -+ ISPP_FIELD(rkispp_params_fec, mesh_size), +}; + +constexpr Field orbFields[] = { diff --git a/multimedia/libcamera/patches/220-pipeline-rkisp2-drive-the-ispp-on-the-rv1126.patch b/multimedia/libcamera/patches/220-pipeline-rkisp2-drive-the-ispp-on-the-rv1126.patch index e9288e5ecb..5dc8ef2dac 100644 --- a/multimedia/libcamera/patches/220-pipeline-rkisp2-drive-the-ispp-on-the-rv1126.patch +++ b/multimedia/libcamera/patches/220-pipeline-rkisp2-drive-the-ispp-on-the-rv1126.patch @@ -1,4 +1,4 @@ -From 317d9f303b63f9e7d86cdd575139392738c0250f Mon Sep 17 00:00:00 2001 +From fb9f7fbe360bc1bf559753a379aeb6dc4bedcb61 Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Wed, 28 Oct 2026 07:27:22 +0000 Subject: [PATCH] pipeline: rkisp2: Drive the ISPP on the RV1126 @@ -36,7 +36,7 @@ Without an ISPP media device the ISP main path remains the output. Signed-off-by: Daniel Golle --- - src/libcamera/pipeline/rkisp2/rkisp2.cpp | 505 +++++++++++++++++++++-- + src/libcamera/pipeline/rkisp2/rkisp2.cpp | 505 +++++++++++++++++++++-- 1 file changed, 481 insertions(+), 24 deletions(-) --- a/src/libcamera/pipeline/rkisp2/rkisp2.cpp @@ -66,7 +66,7 @@ Signed-off-by: Daniel Golle RkISP2CameraData(PipelineHandler *pipe, MediaDevice *media) - : Camera::Private(pipe), media_(media), frame_(0) + : Camera::Private(pipe), media_(media), frame_(0), ispSequence_(0), -+ ispBase_(0), ispBaseSet_(false), isppPushSequence_(1) ++ isppPushSequence_(1), ispBase_(0), ispBaseSet_(false) { } @@ -433,7 +433,7 @@ Signed-off-by: Daniel Golle /* * The ISP takes its input from the raw readback node only while the * link is enabled; the in-kernel VICAP binding feeds it otherwise. -@@ -911,6 +1096,81 @@ int PipelineHandlerRkISP2::configure(Cam +@@ -896,6 +1081,81 @@ int PipelineHandlerRkISP2::configure(Cam cfg.setStream(&data->stream_); cfg.stride = outputFormat.planes[0].bpl; @@ -515,7 +515,7 @@ Signed-off-by: Daniel Golle V4L2DeviceFormat paramFormat; paramFormat.fourcc = V4L2PixelFormat(V4L2_META_FMT_RKISP2_PARAMS); ret = param_->setFormat(¶mFormat); -@@ -959,9 +1219,133 @@ int PipelineHandlerRkISP2::exportFrameBu +@@ -944,9 +1204,133 @@ int PipelineHandlerRkISP2::exportFrameBu if (!data->usingIsp_ || data->isRaw_) return data->video_->exportBuffers(count, buffers); @@ -649,7 +649,7 @@ Signed-off-by: Daniel Golle int PipelineHandlerRkISP2::start(Camera *camera, const ControlList *controls) { -@@ -971,7 +1355,11 @@ int PipelineHandlerRkISP2::start(Camera +@@ -956,7 +1340,11 @@ int PipelineHandlerRkISP2::start(Camera utils::ScopeExitActions actions; int ret; @@ -661,7 +661,7 @@ Signed-off-by: Daniel Golle LOG(RkISP2, Debug) << (useMP ? "Using" : "Not using") << " main path"; -@@ -1093,6 +1481,11 @@ void PipelineHandlerRkISP2::stopDevice(C +@@ -1078,6 +1466,11 @@ void PipelineHandlerRkISP2::stopDevice(C RkISP2CameraData *data = cameraData(camera); bool useMP = data->usingIsp_ && !data->isRaw_; @@ -673,7 +673,7 @@ Signed-off-by: Daniel Golle /* * Stop the main path while the readback still feeds the ISP, so that * the ISP stops at a frame boundary. The capture node is stopped -@@ -1148,12 +1541,27 @@ int PipelineHandlerRkISP2::queueRequestD +@@ -1133,12 +1526,27 @@ int PipelineHandlerRkISP2::queueRequestD data->frame_ += correction; data->syncHelper_.pushCorrection(correction); @@ -703,7 +703,7 @@ Signed-off-by: Daniel Golle return data->mainPath_->queueBuffer(buffer); } -@@ -1180,7 +1588,8 @@ bool PipelineHandlerRkISP2::createCamera +@@ -1165,7 +1573,8 @@ bool PipelineHandlerRkISP2::createCamera std::unique_ptr data = std::make_unique(this, media_.get()); if (data->init(usingIsp, sensor_.get(), video_.get(), rawrd_.get(), @@ -713,7 +713,7 @@ Signed-off-by: Daniel Golle LOG(RkISP2, Error) << "Failed to initialize data"; return false; } -@@ -1377,6 +1786,36 @@ bool PipelineHandlerRkISP2::match(Device +@@ -1362,6 +1771,36 @@ bool PipelineHandlerRkISP2::match(Device return false; } @@ -750,7 +750,7 @@ Signed-off-by: Daniel Golle return createCamera(true); } -@@ -1395,12 +1834,15 @@ int PipelineHandlerRkISP2::allocateBuffe +@@ -1380,12 +1819,15 @@ int PipelineHandlerRkISP2::allocateBuffe RkISP2CameraData *data = cameraData(camera); unsigned int ipaBufferId = 1; utils::ScopeExitActions actions; @@ -770,7 +770,7 @@ Signed-off-by: Daniel Golle ret = data->stat_->allocateBuffers(kRkISP2MinBufferCount, &data->statBuffers_); if (ret < 0) -@@ -1414,6 +1856,15 @@ int PipelineHandlerRkISP2::allocateBuffe +@@ -1399,6 +1841,15 @@ int PipelineHandlerRkISP2::allocateBuffe actions += [&]() { data->paramQueue_->releaseBuffers(); }; @@ -786,7 +786,7 @@ Signed-off-by: Daniel Golle auto pushBuffers = [&](const std::vector> &buffers) { for (const std::unique_ptr &buffer : buffers) { Span planes = buffer->planes(); -@@ -1427,6 +1878,7 @@ int PipelineHandlerRkISP2::allocateBuffe +@@ -1412,6 +1863,7 @@ int PipelineHandlerRkISP2::allocateBuffe pushBuffers(data->paramQueue_->buffers()); pushBuffers(data->statBuffers_); @@ -794,7 +794,7 @@ Signed-off-by: Daniel Golle data->ipa_->mapBuffers(data->ipaBuffers_); -@@ -1447,6 +1899,11 @@ int PipelineHandlerRkISP2::freeBuffers(C +@@ -1432,6 +1884,11 @@ int PipelineHandlerRkISP2::freeBuffers(C data->paramQueue_->releaseBuffers(); diff --git a/multimedia/libcamera/patches/221-pipeline-rkisp2-add-the-ispp-scaler-stream.patch b/multimedia/libcamera/patches/221-pipeline-rkisp2-add-the-ispp-scaler-stream.patch index a44dfe6f33..043a11c1be 100644 --- a/multimedia/libcamera/patches/221-pipeline-rkisp2-add-the-ispp-scaler-stream.patch +++ b/multimedia/libcamera/patches/221-pipeline-rkisp2-add-the-ispp-scaler-stream.patch @@ -296,7 +296,7 @@ Signed-off-by: Daniel Golle /* * The ISP takes its input from the raw readback node only while the -@@ -1166,6 +1277,43 @@ int PipelineHandlerRkISP2::configureIspp +@@ -1151,6 +1262,43 @@ int PipelineHandlerRkISP2::configureIspp return configureIpa(data, cfg); } @@ -340,7 +340,7 @@ Signed-off-by: Daniel Golle int PipelineHandlerRkISP2::configureIpa(RkISP2CameraData *data, const StreamConfiguration &cfg) { -@@ -1219,6 +1367,9 @@ int PipelineHandlerRkISP2::exportFrameBu +@@ -1204,6 +1352,9 @@ int PipelineHandlerRkISP2::exportFrameBu if (!data->usingIsp_ || data->isRaw_) return data->video_->exportBuffers(count, buffers); @@ -350,7 +350,7 @@ Signed-off-by: Daniel Golle if (data->usingIspp_) return data->isppMain_->exportBuffers(count, buffers); -@@ -1289,6 +1440,17 @@ int PipelineHandlerRkISP2::startIspp(Cam +@@ -1274,6 +1425,17 @@ int PipelineHandlerRkISP2::startIspp(Cam } actions += [&]() { data->isppMain_->releaseBuffers(); }; @@ -368,7 +368,7 @@ Signed-off-by: Daniel Golle for (const std::unique_ptr &buffer : data->statBuffers_) { ret = data->stat_->queueBuffer(buffer.get()); if (ret < 0) -@@ -1316,6 +1478,13 @@ int PipelineHandlerRkISP2::startIspp(Cam +@@ -1301,6 +1463,13 @@ int PipelineHandlerRkISP2::startIspp(Cam return ret; actions += [&]() { data->isppMain_->streamOff(); }; @@ -382,7 +382,7 @@ Signed-off-by: Daniel Golle if (controls) { ret = processControls(data, *controls); if (ret < 0) -@@ -1330,10 +1499,14 @@ void PipelineHandlerRkISP2::stopIspp(Cam +@@ -1315,10 +1484,14 @@ void PipelineHandlerRkISP2::stopIspp(Cam { RkISP2CameraData *data = cameraData(camera); @@ -397,7 +397,7 @@ Signed-off-by: Daniel Golle data->isppMain_->releaseBuffers(); data->stat_->streamOff(); -@@ -1559,10 +1732,20 @@ int PipelineHandlerRkISP2::queueRequestD +@@ -1544,10 +1717,20 @@ int PipelineHandlerRkISP2::queueRequestD data->computeParamBuffers(data->frame_ - data->ispBase_); data->frame_++; @@ -421,7 +421,7 @@ Signed-off-by: Daniel Golle } int PipelineHandlerRkISP2::updateControls(RkISP2CameraData *data) -@@ -1589,7 +1772,8 @@ bool PipelineHandlerRkISP2::createCamera +@@ -1574,7 +1757,8 @@ bool PipelineHandlerRkISP2::createCamera std::make_unique(this, media_.get()); if (data->init(usingIsp, sensor_.get(), video_.get(), rawrd_.get(), isp_.get(), mainPath_.get(), param_.get(), stat_.get(), @@ -431,7 +431,7 @@ Signed-off-by: Daniel Golle LOG(RkISP2, Error) << "Failed to initialize data"; return false; } -@@ -1624,6 +1808,8 @@ bool PipelineHandlerRkISP2::createCamera +@@ -1609,6 +1793,8 @@ bool PipelineHandlerRkISP2::createCamera const std::string &id = data->sensor_->id(); std::set streams{ &data->stream_ }; @@ -440,7 +440,7 @@ Signed-off-by: Daniel Golle std::shared_ptr camera = Camera::create(std::move(data), id, streams); registerCamera(std::move(camera)); -@@ -1816,6 +2002,13 @@ bool PipelineHandlerRkISP2::match(Device +@@ -1801,6 +1987,13 @@ bool PipelineHandlerRkISP2::match(Device return false; } diff --git a/multimedia/libcamera/patches/222-include-linux-rkisp2-config-h-add-the-dpcc-block.patch b/multimedia/libcamera/patches/222-include-linux-rkisp2-config-h-add-the-dpcc-block.patch new file mode 100644 index 0000000000..09dfe98b82 --- /dev/null +++ b/multimedia/libcamera/patches/222-include-linux-rkisp2-config-h-add-the-dpcc-block.patch @@ -0,0 +1,175 @@ +From 46394c3e3ae5cb615a2684b066f597be749e2f2b Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 09:49:12 +0000 +Subject: [PATCH] include: linux: rkisp2-config.h: Add the DPCC block + +Sync the header copy with the kernel's DPCC parameters block of the +ISP 2.0: the block type, the method set bits including the Rockchip +distance method, and the configuration structures. + +Assisted-by: LLM +--- + include/linux/media/rockchip/rkisp2-config.h | 136 +++++++++++++++++-- + 1 file changed, 124 insertions(+), 12 deletions(-) + +--- a/include/linux/media/rockchip/rkisp2-config.h ++++ b/include/linux/media/rockchip/rkisp2-config.h +@@ -225,6 +225,7 @@ struct rkisp2_stats_awb_mean { + * @RKISP2_PARAMS_BLOCK_HIST_BIG1: Histogram measurement config (first big block) + * @RKISP2_PARAMS_BLOCK_HIST_BIG2: Histogram measurement config (second big block) + * @RKISP2_PARAMS_BLOCK_AWB_MEAS: AWB measurements config ++ * @RKISP2_PARAMS_BLOCK_DPCC: Defect pixel cluster correction config + */ + enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_BLS, +@@ -240,8 +241,59 @@ enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_HIST_BIG1, + RKISP2_PARAMS_BLOCK_HIST_BIG2, + RKISP2_PARAMS_BLOCK_AWB_MEAS, ++ RKISP2_PARAMS_BLOCK_DPCC, + }; + ++/* DPCC: three method sets, each with a green and a red/blue variant */ ++#define RKISP2_ISP_DPCC_METHODS_MAX 3 ++ ++#define RKISP2_ISP_DPCC_MODE_GRAYSCALE_MODE (1U << 1) ++#define RKISP2_ISP_DPCC_MODE_STAGE1_ENABLE (1U << 2) ++ ++#define RKISP2_ISP_DPCC_OUTPUT_MODE_STAGE1_INCL_G_CENTER (1U << 0) ++#define RKISP2_ISP_DPCC_OUTPUT_MODE_STAGE1_INCL_RB_CENTER (1U << 1) ++#define RKISP2_ISP_DPCC_OUTPUT_MODE_STAGE1_G_3X3 (1U << 2) ++#define RKISP2_ISP_DPCC_OUTPUT_MODE_STAGE1_RB_3X3 (1U << 3) ++#define RKISP2_ISP_DPCC_OUTPUT_MODE_DPCC_OUTPUT_SEL (1U << 4) ++#define RKISP2_ISP_DPCC_OUTPUT_MODE_RK_OUT_SEL(v) ((v) << 5) ++ ++#define RKISP2_ISP_DPCC_SET_USE_STAGE1_USE_SET(n) (1U << (n)) ++#define RKISP2_ISP_DPCC_SET_USE_STAGE1_USE_FIX_SET (1U << 3) ++ ++#define RKISP2_ISP_DPCC_METHODS_SET_PG_GREEN_ENABLE (1U << 0) ++#define RKISP2_ISP_DPCC_METHODS_SET_LC_GREEN_ENABLE (1U << 1) ++#define RKISP2_ISP_DPCC_METHODS_SET_RO_GREEN_ENABLE (1U << 2) ++#define RKISP2_ISP_DPCC_METHODS_SET_RND_GREEN_ENABLE (1U << 3) ++#define RKISP2_ISP_DPCC_METHODS_SET_RG_GREEN_ENABLE (1U << 4) ++#define RKISP2_ISP_DPCC_METHODS_SET_RK_GREEN_ENABLE (1U << 5) ++#define RKISP2_ISP_DPCC_METHODS_SET_PG_RED_BLUE_ENABLE (1U << 8) ++#define RKISP2_ISP_DPCC_METHODS_SET_LC_RED_BLUE_ENABLE (1U << 9) ++#define RKISP2_ISP_DPCC_METHODS_SET_RO_RED_BLUE_ENABLE (1U << 10) ++#define RKISP2_ISP_DPCC_METHODS_SET_RND_RED_BLUE_ENABLE (1U << 11) ++#define RKISP2_ISP_DPCC_METHODS_SET_RG_RED_BLUE_ENABLE (1U << 12) ++#define RKISP2_ISP_DPCC_METHODS_SET_RK_RED_BLUE_ENABLE (1U << 13) ++ ++#define RKISP2_ISP_DPCC_LINE_THRESH_G(v) ((v) << 0) ++#define RKISP2_ISP_DPCC_LINE_THRESH_RB(v) ((v) << 8) ++#define RKISP2_ISP_DPCC_LINE_THRESH_MINDIS_G(v) ((v) << 16) ++#define RKISP2_ISP_DPCC_LINE_THRESH_MINDIS_RB(v) ((v) << 24) ++#define RKISP2_ISP_DPCC_LINE_MAD_FAC_G(v) ((v) << 0) ++#define RKISP2_ISP_DPCC_LINE_MAD_FAC_RB(v) ((v) << 8) ++#define RKISP2_ISP_DPCC_LINE_MAD_FAC_DIS_SCALE_MAX(v) ((v) << 16) ++#define RKISP2_ISP_DPCC_LINE_MAD_FAC_DIS_SCALE_MIN(v) ((v) << 24) ++#define RKISP2_ISP_DPCC_PG_FAC_G(v) ((v) << 0) ++#define RKISP2_ISP_DPCC_PG_FAC_RB(v) ((v) << 8) ++#define RKISP2_ISP_DPCC_RND_THRESH_G(v) ((v) << 0) ++#define RKISP2_ISP_DPCC_RND_THRESH_RB(v) ((v) << 8) ++#define RKISP2_ISP_DPCC_RG_FAC_G(v) ((v) << 0) ++#define RKISP2_ISP_DPCC_RG_FAC_RB(v) ((v) << 8) ++ ++#define RKISP2_ISP_DPCC_RO_LIMITS_n_G(n, v) ((v) << ((n) * 4)) ++#define RKISP2_ISP_DPCC_RO_LIMITS_n_RB(n, v) ((v) << ((n) * 4 + 2)) ++ ++#define RKISP2_ISP_DPCC_RND_OFFS_n_G(n, v) ((v) << ((n) * 4)) ++#define RKISP2_ISP_DPCC_RND_OFFS_n_RB(n, v) ((v) << ((n) * 4 + 2)) ++ + /** + * struct rkisp2_isp_window - measurement window. + * +@@ -658,18 +710,78 @@ struct rkisp2_params_awb_meas { + __u8 reserved[7]; + } __attribute__((aligned(8))); + +-#define RKISP2_PARAMS_MAX_SIZE \ +- (sizeof(struct rkisp2_params_bls) +\ +- sizeof(struct rkisp2_params_awb_gains) +\ +- sizeof(struct rkisp2_params_csm) +\ +- sizeof(struct rkisp2_params_ccm) +\ +- sizeof(struct rkisp2_params_goc) +\ +- sizeof(struct rkisp2_params_lsc) +\ +- sizeof(struct rkisp2_params_crop) +\ +- sizeof(struct rkisp2_params_ae_lite) +\ +- sizeof(struct rkisp2_params_hist_lite) +\ +- sizeof(struct rkisp2_params_hist_big) * 3 +\ +- sizeof(struct rkisp2_params_awb_meas)) ++/** ++ * struct rkisp2_isp_dpcc_methods_config - DPCC methods set configuration ++ * ++ * This structure stores the configuration of one set of methods for the DPCC ++ * algorithm. Multiple methods can be selected in each set (independently for ++ * the Green and Red/Blue components) through the @method field, the result is ++ * the logical AND of all enabled methods. The remaining fields set thresholds ++ * and factors for each method. ++ * ++ * @method: Method enable bits (RKISP2_ISP_DPCC_METHODS_SET_*) ++ * @line_thresh: Line threshold (RKISP2_ISP_DPCC_LINE_THRESH_*), the upper ++ * half holds the minimum distance of the Rockchip method ++ * @line_mad_fac: Line Mean Absolute Difference factor ++ * (RKISP2_ISP_DPCC_LINE_MAD_FAC_*), the upper half holds the ++ * distance scale of the Rockchip method ++ * @pg_fac: Peak gradient factor (RKISP2_ISP_DPCC_PG_FAC_*) ++ * @rnd_thresh: Rank Neighbor Difference threshold (RKISP2_ISP_DPCC_RND_THRESH_*) ++ * @rg_fac: Rank gradient factor (RKISP2_ISP_DPCC_RG_FAC_*) ++ */ ++struct rkisp2_isp_dpcc_methods_config { ++ __u32 method; ++ __u32 line_thresh; ++ __u32 line_mad_fac; ++ __u32 pg_fac; ++ __u32 rnd_thresh; ++ __u32 rg_fac; ++}; ++ ++/** ++ * struct rkisp2_params_dpcc - RkISP2 params DPCC config ++ * ++ * RkISP2 parameters Defect Pixel Cluster Correction configuration block. ++ * Identified by :c:type:`RKISP2_PARAMS_BLOCK_DPCC`. ++ * ++ * The ISP 2.0 carries three DPCC instances, one per HDR frame path, and the ++ * block configures all of them alike. The layout follows the ISP1 DPCC ++ * configuration, extended with the Rockchip method and its minimum distance ++ * and distance scale fields. ++ * ++ * @header: The RkISP2 parameters block header ++ * @mode: DPCC mode (RKISP2_ISP_DPCC_MODE_*) ++ * @output_mode: Interpolation output mode (RKISP2_ISP_DPCC_OUTPUT_MODE_*) ++ * @set_use: Methods sets selection (RKISP2_ISP_DPCC_SET_USE_*) ++ * @methods: Methods sets configuration ++ * @ro_limits: Rank order limits (RKISP2_ISP_DPCC_RO_LIMITS_*) ++ * @rnd_offs: Differential rank offsets for the rank neighbor difference ++ * (RKISP2_ISP_DPCC_RND_OFFS_*) ++ * @reserved: padding ++ */ ++struct rkisp2_params_dpcc { ++ struct v4l2_isp_params_block_header header; ++ __u32 mode; ++ __u32 output_mode; ++ __u32 set_use; ++ struct rkisp2_isp_dpcc_methods_config ++ methods[RKISP2_ISP_DPCC_METHODS_MAX]; ++ __u32 ro_limits; ++ __u32 rnd_offs; ++ __u32 reserved; ++} __attribute__((aligned(8))); ++ ++#define RKISP2_PARAMS_MAX_SIZE \ ++ (sizeof(struct rkisp2_params_bls) + \ ++ sizeof(struct rkisp2_params_awb_gains) + \ ++ sizeof(struct rkisp2_params_csm) + sizeof(struct rkisp2_params_ccm) + \ ++ sizeof(struct rkisp2_params_goc) + sizeof(struct rkisp2_params_lsc) + \ ++ sizeof(struct rkisp2_params_crop) + \ ++ sizeof(struct rkisp2_params_ae_lite) + \ ++ sizeof(struct rkisp2_params_hist_lite) + \ ++ sizeof(struct rkisp2_params_hist_big) * 3 + \ ++ sizeof(struct rkisp2_params_awb_meas) + \ ++ sizeof(struct rkisp2_params_dpcc)) + + /*---------- ISPP (image post-processing) ------------*/ + diff --git a/multimedia/libcamera/patches/223-ipa-rkisp2-add-the-defect-pixel-cluster-correction.patch b/multimedia/libcamera/patches/223-ipa-rkisp2-add-the-defect-pixel-cluster-correction.patch new file mode 100644 index 0000000000..b41e0ab305 --- /dev/null +++ b/multimedia/libcamera/patches/223-ipa-rkisp2-add-the-defect-pixel-cluster-correction.patch @@ -0,0 +1,388 @@ +From df2b080201a42f0fc2a54a2ae7ec2c7b384ea492 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 09:49:12 +0000 +Subject: [PATCH] ipa: rkisp2: Add the defect pixel cluster correction + +The ISP 2.0 passes every hot pixel of the sensor through to the picture +while its DPCC instances stay unprogrammed, which shows as a sky of +single bright pixels on a covered IMX415 at full gain. + +Port the RkISP1 defect pixel cluster correction algorithm to the RkISP2 +IPA. The tuning sets keep the RkISP1 keys and gain the distance method +of the ISP 2.0 (rk-min-distance, rk-distance-scale), the output +selection bits and a min-gain per set: hot pixels rise out of the noise +floor with the analogue gain, so each set and the fixed set of the +hardware come into use once the gain of the frame reaches their +threshold, and the block is sent again whenever that selection changes. + +Seed the imx415 tuning with the method sets of the vendor tuning for +this sensor module, with the sets the vendor enables from ISO 400 tied +to a gain of 8. + +Assisted-by: LLM +--- + src/ipa/rkisp2/algorithms/dpcc.cpp | 211 ++++++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/dpcc.h | 38 +++++ + src/ipa/rkisp2/algorithms/meson.build | 1 + + src/ipa/rkisp2/data/imx415.yaml | 64 ++++++++ + src/ipa/rkisp2/params.h | 2 + + 5 files changed, 316 insertions(+) + create mode 100644 src/ipa/rkisp2/algorithms/dpcc.cpp + create mode 100644 src/ipa/rkisp2/algorithms/dpcc.h + +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/dpcc.cpp +@@ -0,0 +1,211 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2021-2022, Ideas On Board ++ * ++ * RkISP2 Defect Pixel Cluster Correction control ++ */ ++ ++#include "dpcc.h" ++ ++#include ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++/** ++ * \file dpcc.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class DefectPixelClusterCorrection ++ * \brief RkISP2 Defect Pixel Cluster Correction control ++ * ++ * Depending on the sensor quality, some pixels can be defective and then ++ * appear significantly brighter or darker than the other pixels. ++ * ++ * The Defect Pixel Cluster Correction detects such pixels on the fly with up ++ * to three sets of methods and replaces them by an interpolation of their ++ * neighbours. The ISP 2.0 adds a distance based method to the sets of the ++ * ISP1. Hot pixels rise out of the noise floor with the analogue gain, so ++ * each set, and the fixed set of the hardware, can name a minimum gain and ++ * is used once the gain of the frame reaches it. ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2Dpcc) ++ ++namespace { ++ ++/* ++ * Read the green and red/blue values of a method parameter, enable the ++ * method for the components present in the tuning data and return the ++ * values packed with the red/blue one at \a redBlueShift. ++ */ ++uint32_t parseMethod(const ValueNode &object, uint32_t &method, ++ uint32_t greenEnable, uint32_t redBlueEnable, ++ unsigned int redBlueShift = 8) ++{ ++ uint32_t value = 0; ++ ++ if (object.contains("green")) { ++ method |= greenEnable; ++ value |= object["green"].get(0); ++ } ++ ++ if (object.contains("red-blue")) { ++ method |= redBlueEnable; ++ value |= object["red-blue"].get(0) << redBlueShift; ++ } ++ ++ return value; ++} ++ ++} /* namespace */ ++ ++DefectPixelClusterCorrection::DefectPixelClusterCorrection() ++ : config_({}), setMinGain_{}, numSets_(0), setUse_(0) ++{ ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int DefectPixelClusterCorrection::init([[maybe_unused]] IPAContext &context, ++ const ValueNode &tuningData) ++{ ++ config_.mode = RKISP2_ISP_DPCC_MODE_STAGE1_ENABLE; ++ config_.output_mode = RKISP2_ISP_DPCC_OUTPUT_MODE_STAGE1_INCL_G_CENTER ++ | RKISP2_ISP_DPCC_OUTPUT_MODE_STAGE1_INCL_RB_CENTER; ++ ++ if (tuningData["output-select"].get(false)) ++ config_.output_mode |= RKISP2_ISP_DPCC_OUTPUT_MODE_DPCC_OUTPUT_SEL; ++ ++ uint16_t rkOutputSelect = tuningData["rk-output-select"].get(0); ++ config_.output_mode |= RKISP2_ISP_DPCC_OUTPUT_MODE_RK_OUT_SEL(rkOutputSelect & 3); ++ ++ if (tuningData["fixed-set"].get(false)) ++ fixedSetMinGain_ = 0.0; ++ else ++ fixedSetMinGain_ = tuningData["fixed-set-min-gain"].get(); ++ ++ /* Get all defined sets to apply (up to 3). */ ++ const ValueNode &setsObject = tuningData["sets"]; ++ if (!setsObject.isList()) { ++ LOG(RkISP2Dpcc, Error) ++ << "'sets' parameter not found in tuning file"; ++ return -EINVAL; ++ } ++ ++ if (setsObject.size() > RKISP2_ISP_DPCC_METHODS_MAX) { ++ LOG(RkISP2Dpcc, Error) ++ << "'sets' size in tuning file (" << setsObject.size() ++ << ") exceeds the maximum hardware capacity (3)"; ++ return -EINVAL; ++ } ++ ++ numSets_ = setsObject.size(); ++ ++ for (unsigned int i = 0; i < numSets_; ++i) { ++ struct rkisp2_isp_dpcc_methods_config &method = config_.methods[i]; ++ const ValueNode &set = setsObject[i]; ++ ++ setMinGain_[i] = set["min-gain"].get(0.0); ++ ++ method.pg_fac = parseMethod(set["pg-factor"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_PG_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_PG_RED_BLUE_ENABLE); ++ ++ uint32_t roLimits = parseMethod(set["ro-limits"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_RO_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_RO_RED_BLUE_ENABLE, ++ 2); ++ config_.ro_limits |= roLimits << (i * 4); ++ ++ method.rg_fac = parseMethod(set["rg-factor"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_RG_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_RG_RED_BLUE_ENABLE); ++ ++ uint32_t rndOffsets = parseMethod(set["rnd-offsets"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_RND_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_RND_RED_BLUE_ENABLE, ++ 2); ++ config_.rnd_offs |= rndOffsets << (i * 4); ++ ++ method.rnd_thresh = parseMethod(set["rnd-threshold"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_RND_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_RND_RED_BLUE_ENABLE); ++ ++ method.line_thresh = parseMethod(set["line-threshold"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_LC_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_LC_RED_BLUE_ENABLE); ++ ++ method.line_mad_fac = parseMethod(set["line-mad-factor"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_LC_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_LC_RED_BLUE_ENABLE); ++ ++ /* The distance method of the ISP 2.0 lives in the upper half of the LC registers. */ ++ uint32_t minDistance = parseMethod(set["rk-min-distance"], method.method, ++ RKISP2_ISP_DPCC_METHODS_SET_RK_GREEN_ENABLE, ++ RKISP2_ISP_DPCC_METHODS_SET_RK_RED_BLUE_ENABLE); ++ method.line_thresh |= minDistance << 16; ++ ++ const ValueNode &scaleObject = set["rk-distance-scale"]; ++ method.line_mad_fac |= ++ RKISP2_ISP_DPCC_LINE_MAD_FAC_DIS_SCALE_MAX(scaleObject["max"].get(0)) | ++ RKISP2_ISP_DPCC_LINE_MAD_FAC_DIS_SCALE_MIN(scaleObject["min"].get(0)); ++ } ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::prepare ++ */ ++void DefectPixelClusterCorrection::prepare(IPAContext &context, ++ const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) ++{ ++ if (context.configuration.raw) ++ return; ++ ++ double gain = frameContext.agc.gain; ++ uint32_t setUse = 0; ++ ++ for (unsigned int i = 0; i < numSets_; ++i) { ++ if (gain >= setMinGain_[i]) ++ setUse |= RKISP2_ISP_DPCC_SET_USE_STAGE1_USE_SET(i); ++ } ++ ++ if (fixedSetMinGain_ && gain >= *fixedSetMinGain_) ++ setUse |= RKISP2_ISP_DPCC_SET_USE_STAGE1_USE_FIX_SET; ++ ++ if (frame > 1 && setUse == setUse_) ++ return; ++ ++ setUse_ = setUse; ++ ++ LOG(RkISP2Dpcc, Debug) ++ << "Frame " << frame << ": gain " << gain ++ << ", method sets " << utils::hex(setUse); ++ ++ auto config = params->block(); ++ config.setEnabled(true); ++ ++ config->mode = config_.mode; ++ config->output_mode = config_.output_mode; ++ config->set_use = setUse; ++ for (unsigned int i = 0; i < RKISP2_ISP_DPCC_METHODS_MAX; ++i) ++ config->methods[i] = config_.methods[i]; ++ config->ro_limits = config_.ro_limits; ++ config->rnd_offs = config_.rnd_offs; ++} ++ ++REGISTER_IPA_ALGORITHM(DefectPixelClusterCorrection, "DefectPixelClusterCorrection") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/dpcc.h +@@ -0,0 +1,38 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2021-2022, Ideas On Board ++ * ++ * RkISP2 Defect Pixel Cluster Correction control ++ */ ++ ++#pragma once ++ ++#include ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class DefectPixelClusterCorrection : public Algorithm ++{ ++public: ++ DefectPixelClusterCorrection(); ++ ~DefectPixelClusterCorrection() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ void prepare(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) override; ++ ++private: ++ struct rkisp2_params_dpcc config_; ++ double setMinGain_[RKISP2_ISP_DPCC_METHODS_MAX]; ++ std::optional fixedSetMinGain_; ++ unsigned int numSets_; ++ uint32_t setUse_; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -7,6 +7,7 @@ rkisp2_ipa_algorithms = files([ + 'ccm.cpp', + 'crop.cpp', + 'csm.cpp', ++ 'dpcc.cpp', + 'goc.cpp', + 'ispp.cpp', + 'lsc.cpp', +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -53,6 +53,70 @@ algorithms: + - ct: 5658 + gains: [ 2.22321, 2.18627 ] + - BlackLevelSubtraction: ++ - DefectPixelClusterCorrection: ++ fixed-set-min-gain: 8.0 ++ output-select: true ++ rk-output-select: 1 ++ sets: ++ # PG, LC, RO, RND, RG, RK ++ - min-gain: 8.0 ++ rk-min-distance: ++ green: 0 ++ red-blue: 0 ++ line-threshold: ++ green: 8 ++ red-blue: 8 ++ line-mad-factor: ++ green: 4 ++ red-blue: 4 ++ pg-factor: ++ green: 3 ++ red-blue: 4 ++ rnd-threshold: ++ green: 10 ++ red-blue: 10 ++ rnd-offsets: ++ green: 3 ++ red-blue: 3 ++ rg-factor: ++ green: 32 ++ red-blue: 32 ++ ro-limits: ++ green: 2 ++ red-blue: 2 ++ # PG, LC, RO, RK ++ - rk-min-distance: ++ green: 8 ++ red-blue: 8 ++ rk-distance-scale: ++ min: 9 ++ max: 9 ++ line-threshold: ++ green: 8 ++ red-blue: 8 ++ line-mad-factor: ++ green: 6 ++ red-blue: 7 ++ pg-factor: ++ green: 4 ++ red-blue: 4 ++ ro-limits: ++ green: 2 ++ red-blue: 2 ++ # PG, LC, RO ++ - min-gain: 8.0 ++ line-threshold: ++ green: 32 ++ red-blue: 32 ++ line-mad-factor: ++ green: 4 ++ red-blue: 4 ++ pg-factor: ++ green: 3 ++ red-blue: 4 ++ ro-limits: ++ green: 2 ++ red-blue: 2 + - Ccm: + ccms: + - ct: 2698 +--- a/src/ipa/rkisp2/params.h ++++ b/src/ipa/rkisp2/params.h +@@ -32,6 +32,7 @@ enum class RkISP2ParamsBlocks : uint16_t + HistBig1, + HistBig2, + AwbMeas, ++ Dpcc, + }; + + namespace details { +@@ -61,6 +62,7 @@ RkISP2_DEFINE_PARAMS_BLOCK_TYPE(HistBig0 + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(HistBig1, hist_big, HIST_BIG1); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(HistBig2, hist_big, HIST_BIG2); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(AwbMeas, awb_meas, AWB_MEAS); ++RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Dpcc, dpcc, DPCC); + + struct param_traits { + using id_type = RkISP2ParamsBlocks; diff --git a/multimedia/libcamera/patches/224-ipa-rkisp2-goc-place-the-samples-at-the-segment-ends.patch b/multimedia/libcamera/patches/224-ipa-rkisp2-goc-place-the-samples-at-the-segment-ends.patch new file mode 100644 index 0000000000..aeaba08bff --- /dev/null +++ b/multimedia/libcamera/patches/224-ipa-rkisp2-goc-place-the-samples-at-the-segment-ends.patch @@ -0,0 +1,76 @@ +From 652a8265928beeb69b93e5332104415e4b772e56 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 11:58:18 +0000 +Subject: [PATCH] ipa: rkisp2: goc: Place the samples at the segment ends + +The gamma out block of the ISP 2.0 takes each of the 44 samples as the +output at the end of its logarithmic segment and the offset register as +the output at zero; the 45th sample is never read. The algorithm +evaluates the curve at the segment starts, so the whole curve sits one +segment too low: an identity curve returns 0.85 of the input, and the +output tops out at the value computed for x = 3584, 241 of 255 for the +2.2 power curve, so whites come out grey. Measured on the RV1126 against +the bypassed block in the same capture session. + +Evaluate the curve at the segment ends and clamp the samples to the +12-bit range, which the last one, y(4096), needs. + +Assisted-by: LLM +--- + src/ipa/rkisp2/algorithms/goc.cpp | 18 +++++++++++++----- + 1 file changed, 13 insertions(+), 5 deletions(-) + +--- a/src/ipa/rkisp2/algorithms/goc.cpp ++++ b/src/ipa/rkisp2/algorithms/goc.cpp +@@ -7,6 +7,7 @@ + + #include "goc.h" + ++#include + #include + + #include +@@ -44,6 +45,9 @@ LOG_DEFINE_CATEGORY(RkISP2Gamma) + + const float kDefaultGamma = 2.2f; + ++/* The gamma out samples are 12-bit values */ ++constexpr unsigned int kGammaOutMax = 4095; ++ + /** + * \copydoc libcamera::ipa::Algorithm::init + */ +@@ -95,14 +99,15 @@ void GammaOutCorrection::prepare([[maybe + /* \todo Optimize so we don't have to write it every frame */ + + /* +- * The logarithmic segments as specified in the reference, plus an +- * additional 0 to make the loop easier ++ * The logarithmic segments as specified in the reference. The hardware ++ * takes each sample as the output at the end of its segment and the ++ * offset register as the output at zero; the 45th sample is not read. + * + * Just use 44 mode for now. 48 mode is when the last four 512 are + * split into eight pieces of 256. + */ +- static constexpr std::array segments = { +- 0, 1, 1, 1, 1, 1, 1, 1, 1, ++ static constexpr std::array segments = { ++ 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 4, 4, 4, 4, + 8, 8, 8, 8, 16, 16, 16, 16, + 32, 32, 32, 32, 64, 64, 64, 64, +@@ -115,9 +120,12 @@ void GammaOutCorrection::prepare([[maybe + + unsigned x = 0; + for (const auto [i, size] : utils::enumerate(segments)) { +- config->gamma_y[i] = static_cast(std::pow(x / 4096.0, 1.0 / frameContext.goc.gamma) * 4096.0); + x += size; ++ unsigned int y = std::lround(std::pow(x / 4096.0, 1.0 / frameContext.goc.gamma) * 4096.0); ++ ++ config->gamma_y[i] = std::min(y, kGammaOutMax); + } ++ config->gamma_y[segments.size()] = kGammaOutMax; + + config->mode = RKISP2_ISP_GOC_MODE_LOGARITHMIC; + config->segments = RKISP2_ISP_GOC_SEGMENTS_44; diff --git a/multimedia/libcamera/patches/225-ipa-rkisp2-goc-take-the-tone-curve-from-the-tuning-file.patch b/multimedia/libcamera/patches/225-ipa-rkisp2-goc-take-the-tone-curve-from-the-tuning-file.patch new file mode 100644 index 0000000000..28f7780eea --- /dev/null +++ b/multimedia/libcamera/patches/225-ipa-rkisp2-goc-take-the-tone-curve-from-the-tuning-file.patch @@ -0,0 +1,91 @@ +From 60c504b98f27ac9c26c077705ba07e03953db320 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 11:58:18 +0000 +Subject: [PATCH] ipa: rkisp2: goc: Take the tone curve from the tuning file + +A pure power curve has no toe and lifts the shadows while flattening +the mid-tones. Let the tuning file provide a tone curve as a piecewise +linear 16-bit function (the `curve` key, in the Raspberry Pi +gamma_curve format), used while the Gamma control stays at its default; +a different Gamma value falls back to the power curve. + +Assisted-by: LLM +--- + src/ipa/rkisp2/algorithms/goc.cpp | 25 +++++++++++++++++++++++-- + src/ipa/rkisp2/algorithms/goc.h | 3 +++ + 2 files changed, 26 insertions(+), 2 deletions(-) + +--- a/src/ipa/rkisp2/algorithms/goc.cpp ++++ b/src/ipa/rkisp2/algorithms/goc.cpp +@@ -32,6 +32,12 @@ namespace ipa::rkisp2::algorithms { + * This algorithm implements the gamma out curve for the RkISP2. It defaults to + * a gamma value of 2.2. + * ++ * A tuning file may instead provide a tone curve as a piecewise linear ++ * function of 16-bit input and output values (the \c curve key, in the format ++ * of the Raspberry Pi \c gamma_curve). The curve is used as long as the Gamma ++ * control stays at its default value; setting a different gamma switches to ++ * the power function. ++ * + * As gamma is internally represented as a piecewise linear function with only + * 17 knots, the difference between gamma=2.2 and sRGB gamma is minimal. + * Therefore sRGB gamma was not implemented as special case. +@@ -56,6 +62,13 @@ int GammaOutCorrection::init(IPAContext + defaultGamma_ = tuningData["gamma"].get(kDefaultGamma); + context.ctrlMap[&controls::Gamma] = ControlInfo(0.1f, 10.0f, defaultGamma_); + ++ curve_ = tuningData["curve"].get(ipa::Pwl{}); ++ if (!curve_.empty() && curve_.domain().end < 65535.0) { ++ LOG(RkISP2Gamma, Error) ++ << "'curve' does not span the 16-bit input range"; ++ return -EINVAL; ++ } ++ + return 0; + } + +@@ -115,15 +128,23 @@ void GammaOutCorrection::prepare([[maybe + 512, 512, 512, 512 + }; + ++ bool useCurve = !curve_.empty() && ++ frameContext.goc.gamma == defaultGamma_; ++ + auto config = params->block(); + config.setEnabled(true); + + unsigned x = 0; + for (const auto [i, size] : utils::enumerate(segments)) { ++ double y; ++ + x += size; +- unsigned int y = std::lround(std::pow(x / 4096.0, 1.0 / frameContext.goc.gamma) * 4096.0); ++ if (useCurve) ++ y = curve_.eval(std::min(x * 16.0, 65535.0)) / 16.0; ++ else ++ y = std::pow(x / 4096.0, 1.0 / frameContext.goc.gamma) * 4096.0; + +- config->gamma_y[i] = std::min(y, kGammaOutMax); ++ config->gamma_y[i] = std::min(std::lround(y), kGammaOutMax); + } + config->gamma_y[segments.size()] = kGammaOutMax; + +--- a/src/ipa/rkisp2/algorithms/goc.h ++++ b/src/ipa/rkisp2/algorithms/goc.h +@@ -7,6 +7,8 @@ + + #pragma once + ++#include "libipa/pwl.h" ++ + #include "algorithm.h" + + namespace libcamera { +@@ -36,6 +38,7 @@ public: + + private: + float defaultGamma_; ++ ipa::Pwl curve_; + }; + + } /* namespace ipa::rkisp2::algorithms */ diff --git a/multimedia/libcamera/patches/226-ipa-rkisp2-imx415-use-the-raspberry-pi-tone-curve.patch b/multimedia/libcamera/patches/226-ipa-rkisp2-imx415-use-the-raspberry-pi-tone-curve.patch new file mode 100644 index 0000000000..90c018493f --- /dev/null +++ b/multimedia/libcamera/patches/226-ipa-rkisp2-imx415-use-the-raspberry-pi-tone-curve.patch @@ -0,0 +1,27 @@ +From 173c390f6ca7344a97a140d5a1a5bb06a504c7de Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 11:58:18 +0000 +Subject: [PATCH] ipa: rkisp2: imx415: Use the Raspberry Pi tone curve + +The imx415 tuning already carries the colour matrices of the Raspberry +Pi tuning for this sensor; take its gamma_curve too, so the shadows get +a toe and the mid-tones the contrast that the power curve lacks. + +Assisted-by: LLM +--- + src/ipa/rkisp2/data/imx415.yaml | 5 +++++ + 1 file changed, 5 insertions(+) + +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -145,5 +145,10 @@ algorithms: + 1.73041, + ] + - GammaOutCorrection: ++ curve: [ 0, 0, 1024, 5040, 2048, 9338, 3072, 12356, 4096, 15312, 5120, 18051, 6144, 20790, 7168, 23193, ++ 8192, 25744, 9216, 27942, 10240, 30035, 11264, 32005, 12288, 33975, 13312, 35815, 14336, 37600, ++ 15360, 39168, 16384, 40642, 18432, 43379, 20480, 45749, 22528, 47753, 24576, 49621, 26624, 51253, ++ 28672, 52698, 30720, 53796, 32768, 54876, 36864, 57012, 40960, 58656, 45056, 59954, 49152, 61183, ++ 53248, 62355, 57344, 63419, 61440, 64476, 65535, 65535 ] + - ColorSpaceConversion: + ... diff --git a/multimedia/libcamera/patches/227-include-linux-rkisp2-config-h-add-the-cproc-block.patch b/multimedia/libcamera/patches/227-include-linux-rkisp2-config-h-add-the-cproc-block.patch new file mode 100644 index 0000000000..3df9dd5f0e --- /dev/null +++ b/multimedia/libcamera/patches/227-include-linux-rkisp2-config-h-add-the-cproc-block.patch @@ -0,0 +1,80 @@ +From 111206169bdcb88a0a44d4d6ba32895779fd8938 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 12:22:07 +0000 +Subject: [PATCH] include: linux: rkisp2-config.h: Add the CPROC block + +Sync the header copy with the kernel's colour processing block and the +corrected description of the gamma out samples. + +Assisted-by: LLM +--- + include/linux/media/rockchip/rkisp2-config.h | 37 +++++++++++++++++++- + 1 file changed, 36 insertions(+), 1 deletion(-) + +--- a/include/linux/media/rockchip/rkisp2-config.h ++++ b/include/linux/media/rockchip/rkisp2-config.h +@@ -226,6 +226,7 @@ struct rkisp2_stats_awb_mean { + * @RKISP2_PARAMS_BLOCK_HIST_BIG2: Histogram measurement config (second big block) + * @RKISP2_PARAMS_BLOCK_AWB_MEAS: AWB measurements config + * @RKISP2_PARAMS_BLOCK_DPCC: Defect pixel cluster correction config ++ * @RKISP2_PARAMS_BLOCK_CPROC: Color processing config + */ + enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_BLS, +@@ -242,6 +243,7 @@ enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_HIST_BIG2, + RKISP2_PARAMS_BLOCK_AWB_MEAS, + RKISP2_PARAMS_BLOCK_DPCC, ++ RKISP2_PARAMS_BLOCK_CPROC, + }; + + /* DPCC: three method sets, each with a green and a red/blue variant */ +@@ -771,6 +773,38 @@ struct rkisp2_params_dpcc { + __u32 reserved; + } __attribute__((aligned(8))); + ++/** ++ * struct rkisp2_params_cproc - RkISP2 params color processing config ++ * ++ * RkISP2 parameters color processing configuration block, the brightness, ++ * contrast, saturation and hue adjustment of the YUV output. ++ * Identified by :c:type:`RKISP2_PARAMS_BLOCK_CPROC`. ++ * ++ * @header: The RkISP2 parameters block header ++ * @c_out_range: Chrominance pixel clipping range at output (0 for limited, ++ * 1 for full) ++ * @y_in_range: Luminance pixel clipping range at input (0 for limited, 1 for ++ * full) ++ * @y_out_range: Luminance pixel clipping range at output (0 for limited, 1 ++ * for full) ++ * @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 ++ * @reserved: padding ++ */ ++struct rkisp2_params_cproc { ++ struct v4l2_isp_params_block_header header; ++ __u8 c_out_range; ++ __u8 y_in_range; ++ __u8 y_out_range; ++ __u8 contrast; ++ __u8 brightness; ++ __u8 sat; ++ __u8 hue; ++ __u8 reserved; ++} __attribute__((aligned(8))); ++ + #define RKISP2_PARAMS_MAX_SIZE \ + (sizeof(struct rkisp2_params_bls) + \ + sizeof(struct rkisp2_params_awb_gains) + \ +@@ -781,7 +815,8 @@ struct rkisp2_params_dpcc { + sizeof(struct rkisp2_params_hist_lite) + \ + sizeof(struct rkisp2_params_hist_big) * 3 + \ + sizeof(struct rkisp2_params_awb_meas) + \ +- sizeof(struct rkisp2_params_dpcc)) ++ sizeof(struct rkisp2_params_dpcc) + \ ++ sizeof(struct rkisp2_params_cproc)) + + /*---------- ISPP (image post-processing) ------------*/ + diff --git a/multimedia/libcamera/patches/228-ipa-rkisp2-add-the-colour-processing-algorithm.patch b/multimedia/libcamera/patches/228-ipa-rkisp2-add-the-colour-processing-algorithm.patch new file mode 100644 index 0000000000..de83262b28 --- /dev/null +++ b/multimedia/libcamera/patches/228-ipa-rkisp2-add-the-colour-processing-algorithm.patch @@ -0,0 +1,393 @@ +From 1b64e6333fa47f88b3213bee7696655f6188b007 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 12:22:07 +0000 +Subject: [PATCH] ipa: rkisp2: Add the colour processing algorithm + +Port the RkISP1 colour processing algorithm to the RkISP2 IPA: the +Brightness, Contrast, Saturation and Hue controls program the CPROC +block, with the brightness kept neutral at mid grey when the contrast +changes as the RkISP1 algorithm does, and the block's range flags follow +the colour space range of the stream. List the algorithm in the imx415 +tuning. + +Assisted-by: LLM +--- + src/ipa/rkisp2/algorithms/cproc.cpp | 241 ++++++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/cproc.h | 38 ++++ + src/ipa/rkisp2/algorithms/meson.build | 1 + + src/ipa/rkisp2/data/imx415.yaml | 1 + + src/ipa/rkisp2/ipa_context.h | 24 +++ + src/ipa/rkisp2/params.h | 2 + + 6 files changed, 307 insertions(+) + create mode 100644 src/ipa/rkisp2/algorithms/cproc.cpp + create mode 100644 src/ipa/rkisp2/algorithms/cproc.h + +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/cproc.cpp +@@ -0,0 +1,241 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2021-2022, Ideas On Board ++ * ++ * RkISP2 Color Processing control ++ */ ++ ++#include "cproc.h" ++ ++#include ++#include ++ ++#include ++ ++#include ++#include ++ ++/** ++ * \file cproc.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class ColorProcessing ++ * \brief RkISP2 Color Processing control ++ * ++ * The ColorProcessing algorithm is responsible for applying brightness, ++ * contrast and saturation corrections. The values are directly provided ++ * through requests by the corresponding controls. ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2CProc) ++ ++namespace { ++ ++constexpr float kDefaultBrightness = 0.0f; ++constexpr float kDefaultContrast = 1.0f; ++constexpr float kDefaultHue = 0.0f; ++constexpr float kDefaultSaturation = 1.0f; ++ ++/* ++ * The Hue scale is negated as the hardware performs the opposite phase shift ++ * to what is expected and defined from the libcamera Hue control value. ++ */ ++constexpr float kHueScale = -90.0f; ++ ++void applyRequestedBrightness(IPAActiveState &activeState) ++{ ++ auto &cproc = activeState.cproc; ++ ++ /* ++ * The CPROC module applies the transfer function ++ * ++ * Yout = Yin * contrast + brightness/2 ++ * ++ * The calculations are done one bit wider than the input data to ++ * account for the maximum contrast of 2x without clamping. Brightness ++ * is applied after and therefore affects the output only with half the ++ * value. ++ * ++ * Most users expect that changing contrast doesn't change the middle ++ * gray and that the brightness value is normalized to one. So they ++ * expect a transfer function of ++ * ++ * Yout = (Yin - 0.5) * contrast + 0.5 + user brightness ++ * ++ * This can be achieved by computing the hardware brightness value from ++ * the user brightness and contrast: ++ * ++ * Yout = Yin * contrast + 0.5 - 0.5 * contrast + user brightness ++ * = Yin * contrast + (1 - contrast + 2 * user brightness) / 2 ++ */ ++ cproc.brightness = 1 - cproc.contrast.value() + 2 * cproc.requestedBrightness; ++ ++ /* ++ * The cproc.brightness value is clamped to the hardware limits by the ++ * Quantized class. This effectively clamps the user brightness to ++ * limits that now depend on the contrast. Calculate the actual user ++ * brightness to report in metadata by inverting the formula. ++ */ ++ cproc.actualBrightness = (cproc.brightness.value() - 1 + cproc.contrast.value()) / 2; ++} ++ ++} /* namespace */ ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int ColorProcessing::init(IPAContext &context, ++ [[maybe_unused]] const ValueNode &tuningData) ++{ ++ auto &cmap = context.ctrlMap; ++ ++ cmap[&controls::Brightness] = ControlInfo(-1.0f, 0.993f, kDefaultBrightness); ++ cmap[&controls::Contrast] = ControlInfo(0.0f, 1.993f, kDefaultContrast); ++ cmap[&controls::Saturation] = ControlInfo(0.0f, 1.993f, kDefaultSaturation); ++ ++ /* Hue adjustment is negated by kHueScale, min/max are swapped */ ++ cmap[&controls::Hue] = ControlInfo(HueQ::TraitsType::max * kHueScale, ++ HueQ::TraitsType::min * kHueScale, ++ kDefaultHue); ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::configure ++ */ ++int ColorProcessing::configure(IPAContext &context, ++ [[maybe_unused]] const IPACameraSensorInfo &configInfo) ++{ ++ auto &cproc = context.activeState.cproc; ++ ++ cproc.requestedBrightness = kDefaultBrightness; ++ cproc.contrast = ContrastQ(kDefaultContrast); ++ applyRequestedBrightness(context.activeState); ++ ++ cproc.hue = HueQ(kDefaultHue); ++ cproc.saturation = SaturationQ(kDefaultSaturation); ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::queueRequest ++ */ ++void ColorProcessing::queueRequest(IPAContext &context, ++ const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const ControlList &controls) ++{ ++ auto &cproc = context.activeState.cproc; ++ bool update = false; ++ ++ if (frame == 0) ++ update = true; ++ ++ const auto &contrast = controls.get(controls::Contrast); ++ if (contrast) { ++ ContrastQ value = *contrast; ++ if (cproc.contrast != value) { ++ cproc.contrast = value; ++ update = true; ++ } ++ ++ LOG(RkISP2CProc, Debug) << "Set contrast to " << value; ++ } ++ ++ const auto &brightness = controls.get(controls::Brightness); ++ if (brightness) ++ cproc.requestedBrightness = *brightness; ++ ++ if (update || brightness) { ++ BrightnessQ old = cproc.brightness; ++ applyRequestedBrightness(context.activeState); ++ if (cproc.brightness != old) ++ update = true; ++ ++ LOG(RkISP2CProc, Debug) << "Set brightness to " << cproc.actualBrightness; ++ } ++ ++ const auto &hue = controls.get(controls::Hue); ++ if (hue) { ++ /* Scale the Hue from ]-90, +90] */ ++ HueQ value = *hue / kHueScale; ++ if (cproc.hue != value) { ++ cproc.hue = value; ++ update = true; ++ } ++ ++ LOG(RkISP2CProc, Debug) << "Set hue to " << value; ++ } ++ ++ const auto saturation = controls.get(controls::Saturation); ++ if (saturation) { ++ SaturationQ value = *saturation; ++ if (cproc.saturation != value) { ++ cproc.saturation = value; ++ update = true; ++ } ++ ++ LOG(RkISP2CProc, Debug) << "Set saturation to " << value; ++ } ++ ++ frameContext.cproc.brightness = cproc.brightness; ++ frameContext.cproc.actualBrightness = cproc.actualBrightness; ++ frameContext.cproc.contrast = cproc.contrast; ++ frameContext.cproc.hue = cproc.hue; ++ frameContext.cproc.saturation = cproc.saturation; ++ frameContext.cproc.update = update; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::prepare ++ */ ++void ColorProcessing::prepare(IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) ++{ ++ /* Check if the algorithm configuration has been updated. */ ++ if (!frameContext.cproc.update) ++ return; ++ ++ bool fullRange = context.configuration.csm.colorSpaceRange != ++ static_cast(ColorSpace::Range::Limited); ++ ++ auto config = params->block(); ++ config.setEnabled(true); ++ config->c_out_range = fullRange; ++ config->y_in_range = fullRange; ++ config->y_out_range = fullRange; ++ config->brightness = frameContext.cproc.brightness.quantized(); ++ config->contrast = frameContext.cproc.contrast.quantized(); ++ config->hue = frameContext.cproc.hue.quantized(); ++ config->sat = frameContext.cproc.saturation.quantized(); ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::process ++ */ ++void ColorProcessing::process([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ IPAFrameContext &frameContext, ++ [[maybe_unused]] const RkISP2Stats *stats, ++ ControlList &metadata) ++{ ++ metadata.set(controls::Brightness, frameContext.cproc.actualBrightness); ++ metadata.set(controls::Contrast, frameContext.cproc.contrast.value()); ++ metadata.set(controls::Hue, frameContext.cproc.hue.value() * kHueScale); ++ metadata.set(controls::Saturation, frameContext.cproc.saturation.value()); ++} ++ ++REGISTER_IPA_ALGORITHM(ColorProcessing, "ColorProcessing") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/cproc.h +@@ -0,0 +1,38 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2021-2022, Ideas On Board ++ * ++ * RkISP2 Color Processing control ++ */ ++ ++#pragma once ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class ColorProcessing : public Algorithm ++{ ++public: ++ ColorProcessing() = default; ++ ~ColorProcessing() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ int configure(IPAContext &context, ++ const IPACameraSensorInfo &configInfo) override; ++ void queueRequest(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const ControlList &controls) override; ++ void prepare(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) override; ++ void process(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const RkISP2Stats *stats, ++ ControlList &metadata) override; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -5,6 +5,7 @@ rkisp2_ipa_algorithms = files([ + 'awb.cpp', + 'bls.cpp', + 'ccm.cpp', ++ 'cproc.cpp', + 'crop.cpp', + 'csm.cpp', + 'dpcc.cpp', +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -150,5 +150,6 @@ algorithms: + 15360, 39168, 16384, 40642, 18432, 43379, 20480, 45749, 22528, 47753, 24576, 49621, 26624, 51253, + 28672, 52698, 30720, 53796, 32768, 54876, 36864, 57012, 40960, 58656, 45056, 59954, 49152, 61183, + 53248, 62355, 57344, 63419, 61440, 64476, 65535, 65535 ] ++ - ColorProcessing: + - ColorSpaceConversion: + ... +--- a/src/ipa/rkisp2/ipa_context.h ++++ b/src/ipa/rkisp2/ipa_context.h +@@ -36,6 +36,11 @@ namespace libcamera { + + namespace ipa::rkisp2 { + ++using BrightnessQ = Q<1, 7>; ++using ContrastQ = UQ<1, 7>; ++using HueQ = Q<1, 7>; ++using SaturationQ = UQ<1, 7>; ++ + struct IPAHwSettings { + unsigned int numAeCells; + unsigned int numHistogramBins; +@@ -109,6 +114,15 @@ struct IPAActiveState { + ipa::ccm::ActiveState ccm; + + struct { ++ float requestedBrightness; ++ float actualBrightness; ++ BrightnessQ brightness; ++ ContrastQ contrast; ++ HueQ hue; ++ SaturationQ saturation; ++ } cproc; ++ ++ struct { + double lux; + } lux; + +@@ -168,6 +182,16 @@ struct IPAFrameContext : public FrameCon + ipa::ccm::FrameContext ccm; + + struct { ++ float actualBrightness; ++ BrightnessQ brightness; ++ ContrastQ contrast; ++ HueQ hue; ++ SaturationQ saturation; ++ ++ bool update; ++ } cproc; ++ ++ struct { + double lux; + } lux; + +--- a/src/ipa/rkisp2/params.h ++++ b/src/ipa/rkisp2/params.h +@@ -33,6 +33,7 @@ enum class RkISP2ParamsBlocks : uint16_t + HistBig2, + AwbMeas, + Dpcc, ++ Cproc, + }; + + namespace details { +@@ -63,6 +64,7 @@ RkISP2_DEFINE_PARAMS_BLOCK_TYPE(HistBig1 + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(HistBig2, hist_big, HIST_BIG2); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(AwbMeas, awb_meas, AWB_MEAS); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Dpcc, dpcc, DPCC); ++RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Cproc, cproc, CPROC); + + struct param_traits { + using id_type = RkISP2ParamsBlocks; diff --git a/multimedia/libcamera/patches/229-pipeline-rkisp2-apply-the-stream-range-to-the-ispp-pad.patch b/multimedia/libcamera/patches/229-pipeline-rkisp2-apply-the-stream-range-to-the-ispp-pad.patch new file mode 100644 index 0000000000..e6c74e23e8 --- /dev/null +++ b/multimedia/libcamera/patches/229-pipeline-rkisp2-apply-the-stream-range-to-the-ispp-pad.patch @@ -0,0 +1,53 @@ +From 9a5c09086a4acdd30ea44dc46d4c003290eeebad Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 13:53:35 +0000 +Subject: [PATCH] pipeline: rkisp2: Apply the stream range to the ISPP pad + +The ISPP pipe configures the ISP sink only and leaves the ISP output +pad towards the ISPP at its full-range default, while the IPA programs +the colour space conversion for the range of the stream. A limited +range stream then comes out unoffset, black at 0 and white near 225, +and the colour processing block, told its input is limited, wraps the +pixels below 16 to near white. + +Set the stream colour space on that pad, which selects the conversion +range, and hand the IPA the range the pad takes. + +Assisted-by: LLM +--- + src/libcamera/pipeline/rkisp2/rkisp2.cpp | 24 ++++++++++++++++++++++++ + 1 file changed, 24 insertions(+) + +--- a/src/libcamera/pipeline/rkisp2/rkisp2.cpp ++++ b/src/libcamera/pipeline/rkisp2/rkisp2.cpp +@@ -1216,6 +1216,30 @@ int PipelineHandlerRkISP2::configureIspp + if (ret) + return ret; + ++ /* ++ * The ISP output pad towards the ISPP selects the quantization range ++ * of the colour space conversion; the IPA programs the matrix and the ++ * colour processing for the range the pad takes. ++ */ ++ V4L2SubdeviceFormat ispFormat; ++ ret = isp_->getFormat(kRkISP2IspPadSourceIspp, &ispFormat); ++ if (ret) ++ return ret; ++ ++ ispFormat.colorSpace = cfg.colorSpace; ++ ret = isp_->setFormat(kRkISP2IspPadSourceIspp, &ispFormat); ++ if (ret) ++ return ret; ++ ++ if (cfg.colorSpace && ispFormat.colorSpace && ++ ispFormat.colorSpace->range != cfg.colorSpace->range) { ++ LOG(RkISP2, Warning) ++ << "ISP output takes the " ++ << ColorSpace::toString(ispFormat.colorSpace) ++ << " range for " << ColorSpace::toString(cfg.colorSpace); ++ cfg.colorSpace->range = ispFormat.colorSpace->range; ++ } ++ + /* The ISPP input follows the ISP output pad, the query syncs them. */ + V4L2SubdeviceFormat isppFormat; + ret = isppSubdev_->getFormat(0, &isppFormat); diff --git a/multimedia/libcamera/patches/230-include-linux-rkisp2-config-h-add-the-wdr-block.patch b/multimedia/libcamera/patches/230-include-linux-rkisp2-config-h-add-the-wdr-block.patch new file mode 100644 index 0000000000..5df7193dd9 --- /dev/null +++ b/multimedia/libcamera/patches/230-include-linux-rkisp2-config-h-add-the-wdr-block.patch @@ -0,0 +1,113 @@ +From 05b6ad22c9aba4dd132172a112550c10de3a0f6b Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 14:17:44 +0000 +Subject: [PATCH] include: linux: rkisp2-config.h: Add the WDR block + +Sync the header copy with the kernel's wide dynamic range block. + +Assisted-by: LLM +--- + include/linux/media/rockchip/rkisp2-config.h | 71 +++++++++++++++++++- + 1 file changed, 70 insertions(+), 1 deletion(-) + +--- a/include/linux/media/rockchip/rkisp2-config.h ++++ b/include/linux/media/rockchip/rkisp2-config.h +@@ -227,6 +227,7 @@ struct rkisp2_stats_awb_mean { + * @RKISP2_PARAMS_BLOCK_AWB_MEAS: AWB measurements config + * @RKISP2_PARAMS_BLOCK_DPCC: Defect pixel cluster correction config + * @RKISP2_PARAMS_BLOCK_CPROC: Color processing config ++ * @RKISP2_PARAMS_BLOCK_WDR: Wide dynamic range config + */ + enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_BLS, +@@ -244,6 +245,7 @@ enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_AWB_MEAS, + RKISP2_PARAMS_BLOCK_DPCC, + RKISP2_PARAMS_BLOCK_CPROC, ++ RKISP2_PARAMS_BLOCK_WDR, + }; + + /* DPCC: three method sets, each with a green and a red/blue variant */ +@@ -805,6 +807,72 @@ struct rkisp2_params_cproc { + __u8 reserved; + } __attribute__((aligned(8))); + ++/* WDR: 32 curve intervals, 33 curve points */ ++#define RKISP2_ISP_WDR_CURVE_NUM_INTERV 32 ++#define RKISP2_ISP_WDR_CURVE_NUM_COEFF (RKISP2_ISP_WDR_CURVE_NUM_INTERV + 1) ++#define RKISP2_ISP_WDR_CURVE_NUM_DY_REGS 4 ++ ++#define RKISP2_ISP_WDR_CTRL0_LVL_EN(v) ((v) << 4) ++#define RKISP2_ISP_WDR_CTRL0_EPSILON(v) ((v) << 8) ++#define RKISP2_ISP_WDR_CTRL0_PYM_CC(v) ((v) << 16) ++ ++#define RKISP2_ISP_WDR_CTRL1_MODE_BLOCK (1U << 0) ++#define RKISP2_ISP_WDR_CTRL1_FLT_SEL (1U << 1) ++#define RKISP2_ISP_WDR_CTRL1_NONL_MODE1 (1U << 3) ++#define RKISP2_ISP_WDR_CTRL1_NONL_OPEN (1U << 4) ++#define RKISP2_ISP_WDR_CTRL1_NONL_SEGM (1U << 5) ++#define RKISP2_ISP_WDR_CTRL1_BAVG_CLIP(v) ((v) << 6) ++#define RKISP2_ISP_WDR_CTRL1_GAIN_MAX(v) ((v) << 8) ++#define RKISP2_ISP_WDR_CTRL1_GAIN_MAX_CLIP_EN (1U << 16) ++ ++#define RKISP2_ISP_WDR_AVGCLIP_NOISERATIO(v) ((v) << 4) ++#define RKISP2_ISP_WDR_AVGCLIP_BESTLIGHT(v) ((v) << 20) ++ ++/** ++ * struct rkisp2_params_wdr - RkISP2 params WDR config ++ * ++ * RkISP2 parameters wide dynamic range configuration block, a tone mapping ++ * curve applied to the luminance either globally or, in block mode, scaled by ++ * the local brightness. Identified by :c:type:`RKISP2_PARAMS_BLOCK_WDR`. ++ * ++ * The curve is defined at 33 points whose input positions follow from the ++ * 32 interval widths in @dy: each 4-bit field n is an interval of 2^(n+3), the ++ * intervals adding up to 4096. The output values are on the same scale, ++ * 0x1000 being full scale. ++ * ++ * @header: The RkISP2 parameters block header ++ * @ctrl: Colour space and illumination reference control, as the ISP1 WDR_CTRL ++ * register without its enable bit ++ * @dy: Interval widths of the curve, eight 4-bit fields per word ++ * @global_y: Curve output values in global mode ++ * @block_y: Curve output values in block mode ++ * @offset: RGB and luminance offsets, as the ISP1 WDR_OFFSET register ++ * @deltamin: DeltaMin threshold and strength, as the ISP1 WDR_DELTAMIN register ++ * @ctrl0: Pyramid coefficients, epsilon and levels (RKISP2_ISP_WDR_CTRL0_*); ++ * the enable bit is set from the block flags ++ * @ctrl1: Mode, filter and gain limit (RKISP2_ISP_WDR_CTRL1_*) ++ * @blkoff0: Block mode gain offset ++ * @avgclip: Block mode noise ratio and best light level ++ * (RKISP2_ISP_WDR_AVGCLIP_*) ++ * @coe: Block mode luminance coefficients for R, G and B ++ * @coe_off: Block mode luminance offset ++ */ ++struct rkisp2_params_wdr { ++ struct v4l2_isp_params_block_header header; ++ __u32 ctrl; ++ __u32 dy[RKISP2_ISP_WDR_CURVE_NUM_DY_REGS]; ++ __u16 global_y[RKISP2_ISP_WDR_CURVE_NUM_COEFF]; ++ __u16 block_y[RKISP2_ISP_WDR_CURVE_NUM_COEFF]; ++ __u32 offset; ++ __u32 deltamin; ++ __u32 ctrl0; ++ __u32 ctrl1; ++ __u32 blkoff0; ++ __u32 avgclip; ++ __u32 coe[3]; ++ __u32 coe_off; ++} __attribute__((aligned(8))); ++ + #define RKISP2_PARAMS_MAX_SIZE \ + (sizeof(struct rkisp2_params_bls) + \ + sizeof(struct rkisp2_params_awb_gains) + \ +@@ -816,7 +884,8 @@ struct rkisp2_params_cproc { + sizeof(struct rkisp2_params_hist_big) * 3 + \ + sizeof(struct rkisp2_params_awb_meas) + \ + sizeof(struct rkisp2_params_dpcc) + \ +- sizeof(struct rkisp2_params_cproc)) ++ sizeof(struct rkisp2_params_cproc) + \ ++ sizeof(struct rkisp2_params_wdr)) + + /*---------- ISPP (image post-processing) ------------*/ + diff --git a/multimedia/libcamera/patches/231-ipa-rkisp2-agc-use-the-constraint-and-exposure-modes.patch b/multimedia/libcamera/patches/231-ipa-rkisp2-agc-use-the-constraint-and-exposure-modes.patch new file mode 100644 index 0000000000..db123a4449 --- /dev/null +++ b/multimedia/libcamera/patches/231-ipa-rkisp2-agc-use-the-constraint-and-exposure-modes.patch @@ -0,0 +1,31 @@ +From a9b5d2f0a8cce864c0543d779874c8e93ed5b30b Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 14:19:46 +0000 +Subject: [PATCH] ipa: rkisp2: agc: Use the constraint and exposure modes + +The AGC takes the AeConstraintMode and AeExposureMode controls and the +modes of the tuning file into the frame context but always computes the +exposure with the first mode of each. Pass the modes of the frame. + +Assisted-by: LLM +--- + src/ipa/rkisp2/algorithms/agc.cpp | 5 +++-- + 1 file changed, 3 insertions(+), 2 deletions(-) + +--- a/src/ipa/rkisp2/algorithms/agc.cpp ++++ b/src/ipa/rkisp2/algorithms/agc.cpp +@@ -448,11 +448,12 @@ void Agc::process([[maybe_unused]] IPACo + + /* \todo Support lux estimation */ + +- /* \todo Support setting constraint and exposure modes */ + utils::Duration newExposureTime; + double aGain, qGain, dGain; + std::tie(newExposureTime, aGain, qGain, dGain) = +- calculateNewEv(0, 0, hist, effectiveExposureValue); ++ calculateNewEv(frameContext.agc.constraintMode, ++ frameContext.agc.exposureMode, ++ hist, effectiveExposureValue); + + LOG(RkISP2Agc, Debug) + << "Divided up exposure time, analogue gain, quantization gain" diff --git a/multimedia/libcamera/patches/232-ipa-rkisp2-add-the-wide-dynamic-range-algorithm.patch b/multimedia/libcamera/patches/232-ipa-rkisp2-add-the-wide-dynamic-range-algorithm.patch new file mode 100644 index 0000000000..bf061a087e --- /dev/null +++ b/multimedia/libcamera/patches/232-ipa-rkisp2-add-the-wide-dynamic-range-algorithm.patch @@ -0,0 +1,658 @@ +From 7c118ab0ba127c56845a7a9e5ed0ee230436e9ee Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 14:19:46 +0000 +Subject: [PATCH] ipa: rkisp2: Add the wide dynamic range algorithm + +Port the RkISP1 wide dynamic range algorithm to the RkISP2 IPA. It runs +the WDR block of the ISP 2.0 in global mode, with the tone curve the +WdrMode, WdrStrength and WdrMaxBrightPixels controls select, takes its +histogram from the HIST_BIG0 statistics the AGC uses, and adds its +bright pixel constraint to the AGC while WDR is on. The block mode +settings are written with their hardware reset values. + +List the algorithm in the imx415 tuning, off by default, with the +exposure constraint of the RkISP1 default. + +Assisted-by: LLM +--- + src/ipa/rkisp2/algorithms/agc.cpp | 7 +- + src/ipa/rkisp2/algorithms/meson.build | 1 + + src/ipa/rkisp2/algorithms/wdr.cpp | 509 ++++++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/wdr.h | 61 +++ + src/ipa/rkisp2/data/imx415.yaml | 4 + + src/ipa/rkisp2/params.h | 2 + + 6 files changed, 583 insertions(+), 1 deletion(-) + create mode 100644 src/ipa/rkisp2/algorithms/wdr.cpp + create mode 100644 src/ipa/rkisp2/algorithms/wdr.h + +--- a/src/ipa/rkisp2/algorithms/agc.cpp ++++ b/src/ipa/rkisp2/algorithms/agc.cpp +@@ -431,7 +431,12 @@ void Agc::process([[maybe_unused]] IPACo + maxAnalogueGain = frameContext.agc.gain; + } + +- setLimits(minExposureTime, maxExposureTime, minAnalogueGain, maxAnalogueGain, {}); ++ std::vector additionalConstraints; ++ if (context.activeState.wdr.mode != controls::WdrOff) ++ additionalConstraints.push_back(context.activeState.wdr.constraint); ++ ++ setLimits(minExposureTime, maxExposureTime, minAnalogueGain, maxAnalogueGain, ++ std::move(additionalConstraints)); + + /* + * The Agc algorithm needs to know the effective exposure value that was +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -12,5 +12,6 @@ rkisp2_ipa_algorithms = files([ + 'goc.cpp', + 'ispp.cpp', + 'lsc.cpp', ++ 'wdr.cpp', + ]) + +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/wdr.cpp +@@ -0,0 +1,509 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2025, Ideas On Board ++ * ++ * RkISP2 Wide Dynamic Range control ++ */ ++ ++#include "wdr.h" ++ ++#include ++ ++#include ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++#include ++#include ++#include ++ ++/** ++ * \file wdr.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class WideDynamicRange ++ * \brief RkISP2 Wide Dynamic Range algorithm ++ * ++ * This algorithm implements automatic global tone mapping for the RkISP2. ++ * Global tone mapping is done by the WDR hardware block in global mode and applies ++ * a global tone mapping curve to the image to increase the perceived dynamic ++ * range. Imagine an indoor scene with bright outside visible through the ++ * windows. With normal exposure settings, the windows will be completely ++ * saturated and no structure (sky/clouds) will be visible because the AEGC has ++ * to increase overall exposure to reach a certain level of mean brightness. In ++ * WDR mode, the algorithm will artifically reduce the exposure time so that the ++ * texture and colours become visible in the formerly saturated areas. Then the ++ * global tone mapping curve is applied to mitigate the loss of brightness. ++ * ++ * Calculating that tone mapping curve is the most difficult part. This ++ * algorithm implements four tone mapping strategies: ++ * - Linear: The tone mapping curve is a combination of two linear functions ++ * with one kneepoint ++ * - Power: The tone mapping curve follows a power function ++ * - Exponential: The tone mapping curve follows an exponential function ++ * - HistogramEqualization: The tone mapping curve tries to equalize the ++ * histogram ++ * ++ * The overall strategy is the same in all cases: Add a constraint to the AEGC ++ * regulation so that the number of nearly saturated pixels goes below a given ++ * threshold (default 2%). This threshold can either be specified in the tuning ++ * file or set via the WdrMaxBrightPixels control. ++ * ++ * The global tone mapping curve is then calculated so that it accounts for the ++ * reduction of brightness due to the exposure constraint. We'll call this the ++ * WDR-gain. As the result of tone mapping is very difficult to quantize and is ++ * by definition a lossy process there is not a single "correct" solution on how ++ * this curve should look like. ++ * ++ * The approach taken here is based on a simple linear model. Consider a pixel ++ * that was originally 50% grey. It will have its exposure pushed down by the ++ * WDR's initial exposure compensation. This value then needs to be pushed back ++ * up by the tone mapping curve so that it is 50% grey again. This point serves ++ * as our kneepoint. To get to this kneepoint, this pixel and all darker pixels ++ * (to the left of the kneepoint on the tone mapping curve) will simply have the ++ * exposure compensation undone by WDR-gain. This cancels out the ++ * original exposure compensation, which was 1/WDR-gain. The remaining ++ * brigher pixels (to the right of the kneepoint on the tone mapping curve) will ++ * be compressed. The WdrStrength control adjusts the gain of the left part of ++ * the tone mapping curve. ++ * ++ * In the Power and Exponential modes, the curves are calculated so that they ++ * pass through that kneepoint. ++ * ++ * The histogram equalization mode tries to equalize the histogram of the ++ * image and acts independently of the calculated exposure value. ++ * ++ * \code{.unparsed} ++ * algorithms: ++ * - WideDynamicRange: ++ * ExposureConstraint: ++ * MaxBrightPixels: 0.02 ++ * yTarget: 0.95 ++ * \endcode ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2Wdr) ++ ++static constexpr unsigned int kTonecurveXIntervals = RKISP2_ISP_WDR_CURVE_NUM_INTERV; ++ ++/* ++ * Increasing interval sizes. The intervals are crafted so that they sum ++ * up to 4096. This results in better fitting curves than the constant intervals ++ * (all entries are 4) ++ */ ++static constexpr std::array kLoglikeIntervals = { ++ { 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, ++ 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6 } ++}; ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int WideDynamicRange::init([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const ValueNode &tuningData) ++{ ++ toneCurveIntervalValues_ = kLoglikeIntervals; ++ ++ /* Calculate a list of normed x values */ ++ toneCurveX_[0] = 0.0; ++ int lastValue = 0; ++ for (unsigned int i = 1; i < toneCurveX_.size(); i++) { ++ lastValue += std::pow(2, toneCurveIntervalValues_[i - 1] + 3); ++ lastValue = std::min(lastValue, 4096); ++ toneCurveX_[i] = lastValue / 4096.0; ++ } ++ ++ exposureConstraintMaxBrightPixels_ = 0.02; ++ exposureConstraintY_ = 0.95; ++ ++ const auto &constraint = tuningData["ExposureConstraint"]; ++ if (!constraint.isDictionary()) { ++ LOG(RkISP2Wdr, Warning) ++ << "ExposureConstraint not found in tuning data." ++ "Using default values MaxBrightPixels: " ++ << exposureConstraintMaxBrightPixels_ ++ << " yTarget: " << exposureConstraintY_; ++ } else { ++ exposureConstraintMaxBrightPixels_ = ++ constraint["MaxBrightPixels"] ++ .get() ++ .value_or(exposureConstraintMaxBrightPixels_); ++ exposureConstraintY_ = ++ constraint["yTarget"] ++ .get() ++ .value_or(exposureConstraintY_); ++ } ++ ++ context.ctrlMap[&controls::WdrMode] = ++ ControlInfo(controls::WdrModeValues, controls::WdrOff); ++ context.ctrlMap[&controls::WdrStrength] = ++ ControlInfo(0.0f, 2.0f, 1.0f); ++ context.ctrlMap[&controls::WdrMaxBrightPixels] = ++ ControlInfo(0.0f, 1.0f, static_cast(exposureConstraintMaxBrightPixels_)); ++ ++ applyCompensationLinear(1.0, 0.0); ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::configure ++ */ ++int WideDynamicRange::configure(IPAContext &context, ++ [[maybe_unused]] const IPACameraSensorInfo &configInfo) ++{ ++ context.activeState.wdr.mode = controls::WdrOff; ++ context.activeState.wdr.gain = 1.0; ++ context.activeState.wdr.strength = 1.0; ++ auto &constraint = context.activeState.wdr.constraint; ++ constraint.bound = AgcMeanLuminance::AgcConstraint::Bound::Upper; ++ constraint.qHi = 1.0; ++ constraint.qLo = 1.0 - exposureConstraintMaxBrightPixels_; ++ constraint.yTarget.clear(); ++ constraint.yTarget.append(0, exposureConstraintY_); ++ return 0; ++} ++ ++void WideDynamicRange::applyHistogramEqualization(double strength) ++{ ++ if (hist_.empty()) ++ return; ++ ++ /* ++ * Apply a factor on strength, so that it roughly matches the optical ++ * impression that is produced by the other algorithms. The goal is that ++ * the user can switch algorithms for different looks but similar ++ * "strength". ++ */ ++ strength *= 0.65; ++ ++ /* ++ * In a fully equalized histogram, all bins have the same value. Try ++ * to equalize the histogram by applying a gain or damping depending on ++ * the distance of the actual bin value from that norm. ++ */ ++ std::vector gains; ++ gains.resize(hist_.size()); ++ double sum = 0; ++ double norm = 1.0 / (gains.size()); ++ for (unsigned i = 0; i < hist_.size(); i++) { ++ double diff = 1.0 + strength * (hist_[i] - norm) / norm; ++ gains[i] = diff; ++ sum += diff; ++ } ++ ++ /* Never amplify the last entry. */ ++ gains.back() = std::max(gains.back(), 1.0); ++ ++ double scale = gains.size() / sum; ++ for (auto &v : gains) ++ v *= scale; ++ ++ Pwl pwl; ++ double step = 1.0 / gains.size(); ++ double lastX = 0; ++ double lastY = 0; ++ ++ pwl.append(lastX, lastY); ++ for (unsigned int i = 0; i < gains.size() - 1; i++) { ++ lastY += gains[i] * step; ++ lastX += step; ++ pwl.append(lastX, lastY); ++ } ++ pwl.append(1.0, 1.0); ++ ++ for (unsigned int i = 0; i < toneCurveX_.size(); i++) ++ toneCurveY_[i] = pwl.eval(toneCurveX_[i]); ++} ++ ++Vector WideDynamicRange::kneePoint(double gain, double strength) ++{ ++ gain = std::pow(gain, strength); ++ double y = 0.5; ++ double x = y / gain; ++ ++ return { { x, y } }; ++} ++ ++void WideDynamicRange::applyCompensationLinear(double gain, double strength) ++{ ++ auto kp = kneePoint(gain, strength); ++ double g1 = kp.y() / kp.x(); ++ double g2 = (kp.y() - 1) / (kp.x() - 1); ++ ++ for (unsigned int i = 0; i < toneCurveX_.size(); i++) { ++ double x = toneCurveX_[i]; ++ double y; ++ if (x <= kp.x()) { ++ y = g1 * x; ++ } else { ++ y = g2 * x + 1 - g2; ++ } ++ toneCurveY_[i] = y; ++ } ++} ++ ++void WideDynamicRange::applyCompensationPower(double gain, double strength) ++{ ++ double e = 1.0; ++ if (strength > 1e-6) { ++ auto kp = kneePoint(gain, strength); ++ /* Calculate an exponent to go through the knee point. */ ++ e = log(kp.y()) / log(kp.x()); ++ } ++ ++ /* ++ * The power function tends to be extremely steep at the beginning. This ++ * leads to noise and image artifacts in the dark areas. To mitigate ++ * that, we add a short linear section at the beginning of the curve. ++ * The connection between linear and power is the point where the linear ++ * section reaches the y level yLin. The power curve is then scaled so ++ * that it starts at the connection point with the steepness it would ++ * have at y=yLin but still goes through 1,1 ++ */ ++ double yLin = 0.1; ++ /* x position of the connection point */ ++ double xb = yLin / gain; ++ /* x offset for the scaled power function */ ++ double q = xb - std::exp(std::log(yLin) / e); ++ ++ for (unsigned int i = 0; i < toneCurveX_.size(); i++) { ++ double x = toneCurveX_[i]; ++ if (x < xb) { ++ toneCurveY_[i] = x * gain; ++ } else { ++ x = (x - q) / (1 - q); ++ toneCurveY_[i] = std::pow(x, e); ++ } ++ } ++} ++ ++void WideDynamicRange::applyCompensationExponential(double gain, double strength) ++{ ++ double k = 0.1; ++ auto kp = kneePoint(gain, strength); ++ double kx = kp.x(); ++ double ky = kp.y(); ++ ++ if (kx > ky) { ++ LOG(RkISP2Wdr, Warning) << "Invalid knee point: " << kp; ++ kx = ky; ++ } ++ ++ /* ++ * The exponential curve is based on the function proposed by Glozman ++ * et al. in ++ * S. Glozman, T. Kats, and O. Yadid-Pecht, "Exponent Operator Based ++ * Tone Mapping Algorithm for Color Wide Dynamic Range Images," 2011. ++ * ++ * That function uses a k factor as parameter for the WDR compression ++ * curve: ++ * k=0: maximum compression ++ * k=infinity: linear curve ++ * ++ * To calculate a k factor that results in a curve that passes through ++ * the kneepoint, the equation needs to be solved for k after inserting ++ * the kneepoint. This can be formulated as search for a zero point. ++ * Unfortunately there is no closed solution for that transformation. ++ * Using newton's method to approximate the value is numerically ++ * unstable. ++ * ++ * Luckily the function only crosses the x axis once and for the set of ++ * possible kneepoints, a negative and a positive point can be guessed. ++ * The approximation is then implemented using bisection. ++ */ ++ if (std::abs(kx - ky) < 0.001) { ++ k = 1e8; ++ } else { ++ double kl = 0.0001; ++ double kh = 1000; ++ ++ auto fk = [=](double v) { ++ return std::exp(-kx / v) - ++ ky * std::exp(-1.0 / v) + ky - 1.0; ++ }; ++ ++ ASSERT(fk(kl) < 0); ++ ASSERT(fk(kh) > 0); ++ ++ k = kh / 10.0; ++ while (fk(k) > 0) { ++ kh = k; ++ k /= 10.0; ++ } ++ ++ do { ++ k = (kl + kh) / 2; ++ if (fk(k) < 0) ++ kl = k; ++ else ++ kh = k; ++ } while (std::abs(kh - kl) > 1e-3); ++ } ++ ++ double a = 1.0 / (1.0 - std::exp(-1.0 / k)); ++ for (unsigned int i = 0; i < toneCurveX_.size(); i++) ++ toneCurveY_[i] = a * (1.0 - std::exp(-toneCurveX_[i] / k)); ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::queueRequest ++ */ ++void WideDynamicRange::queueRequest([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const ControlList &controls) ++{ ++ auto &activeState = context.activeState; ++ ++ const auto &mode = controls.get(controls::WdrMode); ++ if (mode) ++ activeState.wdr.mode = static_cast(*mode); ++ ++ const auto &brightPixels = controls.get(controls::WdrMaxBrightPixels); ++ if (brightPixels) ++ activeState.wdr.constraint.qLo = 1.0 - *brightPixels; ++ ++ const auto &strength = controls.get(controls::WdrStrength); ++ if (strength) ++ activeState.wdr.strength = *strength; ++ ++ frameContext.wdr.mode = activeState.wdr.mode; ++ frameContext.wdr.strength = activeState.wdr.strength; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::prepare ++ */ ++void WideDynamicRange::prepare(IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) ++{ ++ if (!params) { ++ LOG(RkISP2Wdr, Warning) << "Params is null"; ++ return; ++ } ++ ++ auto mode = frameContext.wdr.mode; ++ ++ auto config = params->block(); ++ config.setEnabled(mode != controls::WdrOff); ++ ++ /* Calculate how much EV we need to compensate with the WDR curve. */ ++ double gain = context.activeState.wdr.gain; ++ frameContext.wdr.gain = gain; ++ ++ if (mode == controls::WdrOff) { ++ applyCompensationLinear(1.0, 0.0); ++ } else if (mode == controls::WdrLinear) { ++ applyCompensationLinear(gain, frameContext.wdr.strength); ++ } else if (mode == controls::WdrPower) { ++ applyCompensationPower(gain, frameContext.wdr.strength); ++ } else if (mode == controls::WdrExponential) { ++ applyCompensationExponential(gain, frameContext.wdr.strength); ++ } else if (mode == controls::WdrHistogramEqualization) { ++ applyHistogramEqualization(frameContext.wdr.strength); ++ } ++ ++ /* ++ * Global mode with the block mode settings at their hardware defaults; ++ * the curve output is used in global mode only. ++ */ ++ config->ctrl = 0; ++ config->offset = 0; ++ config->deltamin = 0; ++ config->ctrl0 = RKISP2_ISP_WDR_CTRL0_PYM_CC(3) | ++ RKISP2_ISP_WDR_CTRL0_EPSILON(0xc) | ++ RKISP2_ISP_WDR_CTRL0_LVL_EN(0xf); ++ config->ctrl1 = RKISP2_ISP_WDR_CTRL1_GAIN_MAX_CLIP_EN | ++ RKISP2_ISP_WDR_CTRL1_GAIN_MAX(0x40) | ++ RKISP2_ISP_WDR_CTRL1_BAVG_CLIP(3) | ++ RKISP2_ISP_WDR_CTRL1_NONL_OPEN | ++ RKISP2_ISP_WDR_CTRL1_FLT_SEL; ++ config->blkoff0 = 0xc0; ++ config->avgclip = RKISP2_ISP_WDR_AVGCLIP_BESTLIGHT(0xcc) | ++ RKISP2_ISP_WDR_AVGCLIP_NOISERATIO(0xe); ++ config->coe[0] = 0x36; ++ config->coe[1] = 0xb7; ++ config->coe[2] = 0x12; ++ config->coe_off = 0; ++ ++ for (unsigned int i = 0; i < RKISP2_ISP_WDR_CURVE_NUM_DY_REGS; i++) ++ config->dy[i] = 0; ++ for (unsigned int i = 0; i < kTonecurveXIntervals; i++) { ++ int v = toneCurveIntervalValues_[i]; ++ config->dy[i / 8] |= (v & 0x0f) << ((i % 8) * 4); ++ } ++ ++ /* ++ * Fix the curve to adhere to the hardware constraints. Don't apply a ++ * constraint on the first element, which is most likely zero anyways. ++ */ ++ int lastY = toneCurveY_[0] * 4096.0; ++ for (unsigned int i = 0; i < toneCurveX_.size(); i++) { ++ int diff = static_cast(toneCurveY_[i] * 4096.0) - lastY; ++ diff = std::clamp(diff, -2048, 2048); ++ lastY = std::clamp(lastY + diff, 0, 4096); ++ config->global_y[i] = lastY; ++ config->block_y[i] = 0; ++ } ++} ++ ++void WideDynamicRange::process(IPAContext &context, [[maybe_unused]] const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const RkISP2Stats *stats, ++ ControlList &metadata) ++{ ++ auto mode = frameContext.wdr.mode; ++ metadata.set(controls::WdrMode, mode); ++ ++ auto histBig = stats ? stats->block() : nullptr; ++ if (!histBig) { ++ LOG(RkISP2Wdr, Warning) << "No histogram data in statistics"; ++ return; ++ } ++ ++ /* The lower 5 bits are fractional and meant to be discarded. */ ++ Histogram cumHist({ histBig->hist_bins, RKISP2_ISP_HIST_BIN_N_MAX }, ++ [](uint32_t x) { return x >> 5; }); ++ ++ /* Calculate the gain needed to reach the requested yTarget. */ ++ double value = cumHist.interQuantileMean(0, 1.0) / cumHist.bins(); ++ double gain = context.activeState.agc.automatic.yTarget / value; ++ gain = std::max(gain, 1.0); ++ ++ double speed = 0.2; ++ gain = gain * speed + context.activeState.wdr.gain * (1.0 - speed); ++ ++ context.activeState.wdr.gain = gain; ++ ++ std::vector hist; ++ double sum = 0; ++ for (unsigned i = 0; i < RKISP2_ISP_HIST_BIN_N_MAX; i++) { ++ double v = histBig->hist_bins[i] >> 5; ++ hist.push_back(v); ++ sum += v; ++ } ++ ++ if (sum == 0) ++ return; ++ ++ /* Scale so that the entries sum up to 1. */ ++ double scale = 1.0 / sum; ++ for (auto &v : hist) ++ v *= scale; ++ hist_.swap(hist); ++} ++ ++REGISTER_IPA_ALGORITHM(WideDynamicRange, "WideDynamicRange") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/wdr.h +@@ -0,0 +1,61 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2025, Ideas On Board ++ * ++ * RkISP2 Wide Dynamic Range control ++ */ ++ ++#pragma once ++ ++#include ++#include ++ ++#include ++ ++#include "libcamera/internal/vector.h" ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class WideDynamicRange : public Algorithm ++{ ++public: ++ WideDynamicRange() = default; ++ ~WideDynamicRange() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ int configure(IPAContext &context, const IPACameraSensorInfo &configInfo) override; ++ ++ void queueRequest(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const ControlList &controls) override; ++ void prepare(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) override; ++ void process(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const RkISP2Stats *stats, ++ ControlList &metadata) override; ++ ++private: ++ Vector kneePoint(double gain, double strength); ++ void applyCompensationLinear(double gain, double strength); ++ void applyCompensationPower(double gain, double strength); ++ void applyCompensationExponential(double gain, double strength); ++ void applyHistogramEqualization(double strength); ++ ++ double exposureConstraintMaxBrightPixels_; ++ double exposureConstraintY_; ++ ++ std::vector hist_; ++ ++ std::array toneCurveIntervalValues_; ++ std::array toneCurveX_; ++ std::array toneCurveY_; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -151,5 +151,9 @@ algorithms: + 28672, 52698, 30720, 53796, 32768, 54876, 36864, 57012, 40960, 58656, 45056, 59954, 49152, 61183, + 53248, 62355, 57344, 63419, 61440, 64476, 65535, 65535 ] + - ColorProcessing: ++ - WideDynamicRange: ++ ExposureConstraint: ++ MaxBrightPixels: 0.02 ++ yTarget: 0.95 + - ColorSpaceConversion: + ... +--- a/src/ipa/rkisp2/params.h ++++ b/src/ipa/rkisp2/params.h +@@ -34,6 +34,7 @@ enum class RkISP2ParamsBlocks : uint16_t + AwbMeas, + Dpcc, + Cproc, ++ Wdr, + }; + + namespace details { +@@ -65,6 +66,7 @@ RkISP2_DEFINE_PARAMS_BLOCK_TYPE(HistBig2 + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(AwbMeas, awb_meas, AWB_MEAS); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Dpcc, dpcc, DPCC); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Cproc, cproc, CPROC); ++RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Wdr, wdr, WDR); + + struct param_traits { + using id_type = RkISP2ParamsBlocks; diff --git a/multimedia/libcamera/patches/233-gstreamer-expose-the-wide-dynamic-range-controls.patch b/multimedia/libcamera/patches/233-gstreamer-expose-the-wide-dynamic-range-controls.patch new file mode 100644 index 0000000000..3c70c85be9 --- /dev/null +++ b/multimedia/libcamera/patches/233-gstreamer-expose-the-wide-dynamic-range-controls.patch @@ -0,0 +1,25 @@ +From 1193e1157e114f9b6481abd784770d0fd87e0367 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 14:19:56 +0000 +Subject: [PATCH] gstreamer: Expose the wide dynamic range controls + +Add the WdrMode, WdrStrength and WdrMaxBrightPixels controls to the +controls libcamerasrc offers as properties, so pipelines can switch the +tone mapping of IPAs that implement them. + +Assisted-by: LLM +--- + utils/codegen/gen-gst-controls.py | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +--- a/utils/codegen/gen-gst-controls.py ++++ b/utils/codegen/gen-gst-controls.py +@@ -25,7 +25,7 @@ exposed_controls = [ + 'Brightness', 'Contrast', 'AwbEnable', 'AwbMode', 'ColourGains', + 'Saturation', 'Sharpness', 'ColourCorrectionMatrix', 'ScalerCrop', + 'DigitalGain', 'AfMode', 'AfRange', 'AfSpeed', 'AfMetering', 'AfWindows', +- 'LensPosition', 'Gamma', ++ 'LensPosition', 'Gamma', 'WdrMode', 'WdrStrength', 'WdrMaxBrightPixels', + ] + + diff --git a/multimedia/libcamera/patches/234-include-linux-rkisp2-config-h-add-the-gain-map-block.patch b/multimedia/libcamera/patches/234-include-linux-rkisp2-config-h-add-the-gain-map-block.patch new file mode 100644 index 0000000000..40958a1f33 --- /dev/null +++ b/multimedia/libcamera/patches/234-include-linux-rkisp2-config-h-add-the-gain-map-block.patch @@ -0,0 +1,73 @@ +From bf63642f8d98f7cb6b67009a560c72efe3b81a1b Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 21:31:44 +0000 +Subject: [PATCH] include: linux: rkisp2-config.h: Add the gain map block + +Sync the header copy with the kernel's gain map block. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + include/linux/media/rockchip/rkisp2-config.h | 30 +++++++++++++++++++- + 1 file changed, 29 insertions(+), 1 deletion(-) + +--- a/include/linux/media/rockchip/rkisp2-config.h ++++ b/include/linux/media/rockchip/rkisp2-config.h +@@ -228,6 +228,7 @@ struct rkisp2_stats_awb_mean { + * @RKISP2_PARAMS_BLOCK_DPCC: Defect pixel cluster correction config + * @RKISP2_PARAMS_BLOCK_CPROC: Color processing config + * @RKISP2_PARAMS_BLOCK_WDR: Wide dynamic range config ++ * @RKISP2_PARAMS_BLOCK_GAIN: Gain map config + */ + enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_BLS, +@@ -246,6 +247,7 @@ enum rkisp2_params_block_type { + RKISP2_PARAMS_BLOCK_DPCC, + RKISP2_PARAMS_BLOCK_CPROC, + RKISP2_PARAMS_BLOCK_WDR, ++ RKISP2_PARAMS_BLOCK_GAIN, + }; + + /* DPCC: three method sets, each with a green and a red/blue variant */ +@@ -873,6 +875,32 @@ struct rkisp2_params_wdr { + __u32 coe_off; + } __attribute__((aligned(8))); + ++/* gain map: three exposure gains, as in the HDR merge */ ++#define RKISP2_ISP_GAIN_NUM 3 ++ ++/** ++ * struct rkisp2_params_gain - RkISP2 params gain map config ++ * ++ * RkISP2 parameters gain map configuration block. With each frame the ISP ++ * hands to the ISPP it writes a map of the gain applied to every 4x8 pixel ++ * block, which the ISPP temporal and spatial noise reduction scale their noise ++ * model with. A map entry is 16 times the square root of the block gain: the ++ * exposure gain below, times the lens shading gain and, with the WDR block ++ * enabled, its local tone gain. Identified by ++ * :c:type:`RKISP2_PARAMS_BLOCK_GAIN`. ++ * ++ * Without the block enabled the ISP writes a map of zeros. ++ * ++ * @header: The RkISP2 parameters block header ++ * @gain: Exposure gain in 12.6 fixed point, for the long, middle and short ++ * exposures of an HDR merge; a linear exposure sets all three. The ++ * first holds 18 bits, the other two 14 bits. ++ */ ++struct rkisp2_params_gain { ++ struct v4l2_isp_params_block_header header; ++ __u32 gain[RKISP2_ISP_GAIN_NUM]; ++} __attribute__((aligned(8))); ++ + #define RKISP2_PARAMS_MAX_SIZE \ + (sizeof(struct rkisp2_params_bls) + \ + sizeof(struct rkisp2_params_awb_gains) + \ +@@ -885,7 +913,7 @@ struct rkisp2_params_wdr { + sizeof(struct rkisp2_params_awb_meas) + \ + sizeof(struct rkisp2_params_dpcc) + \ + sizeof(struct rkisp2_params_cproc) + \ +- sizeof(struct rkisp2_params_wdr)) ++ sizeof(struct rkisp2_params_wdr) + sizeof(struct rkisp2_params_gain)) + + /*---------- ISPP (image post-processing) ------------*/ + diff --git a/multimedia/libcamera/patches/235-ipa-rkisp2-add-the-temporal-noise-reduction-algorithm.patch b/multimedia/libcamera/patches/235-ipa-rkisp2-add-the-temporal-noise-reduction-algorithm.patch new file mode 100644 index 0000000000..253696a481 --- /dev/null +++ b/multimedia/libcamera/patches/235-ipa-rkisp2-add-the-temporal-noise-reduction-algorithm.patch @@ -0,0 +1,941 @@ +From 3b15a0296420c9f815b75e747df38974f7262c05 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 21:35:53 +0000 +Subject: [PATCH] ipa: rkisp2: Add the temporal noise reduction algorithm + +The ISPP TNR filters each frame against its filtered predecessor with a +noise model calibrated per ISO step, and scales the model block by block +with the gain map the ISP writes along with the frame. + +Add an algorithm that programs the ISP gain map with the analogue gain +and fills the ISPP TNR block with the calibration interpolated at that +gain. The conversion follows the Rockchip AIQ MFNR algorithm, including +its gain state ratio, so that the IQ calibration of a sensor applies as +is. The TNR block moves out of the static Ispp parameters. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/algorithms/ispp.cpp | 65 +-- + src/ipa/rkisp2/algorithms/ispp.h | 1 - + src/ipa/rkisp2/algorithms/meson.build | 1 + + src/ipa/rkisp2/algorithms/tnr.cpp | 662 ++++++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/tnr.h | 112 +++++ + src/ipa/rkisp2/params.h | 2 + + 6 files changed, 783 insertions(+), 60 deletions(-) + create mode 100644 src/ipa/rkisp2/algorithms/tnr.cpp + create mode 100644 src/ipa/rkisp2/algorithms/tnr.h + +--- a/src/ipa/rkisp2/algorithms/ispp.cpp ++++ b/src/ipa/rkisp2/algorithms/ispp.cpp +@@ -32,12 +32,12 @@ namespace ipa::rkisp2::algorithms { + * \class Ispp + * \brief RkISPP module parameters from the tuning file + * +- * The ISPP post-processor (temporal noise reduction, noise reduction, +- * sharpening, FEC lens distortion correction and ORB feature detection) +- * takes its +- * parameters through its own params node. This algorithm sends the modules +- * configured in the tuning file with every frame, each tuning dictionary +- * filling the fields of the corresponding UAPI block by name: ++ * The ISPP post-processor (noise reduction, sharpening, FEC lens distortion ++ * correction and ORB feature detection) takes its parameters through its own ++ * params node; the temporal noise reduction has an algorithm of its own. This ++ * algorithm sends the modules configured in the tuning file with every frame, ++ * each tuning dictionary filling the fields of the corresponding UAPI block by ++ * name: + * + * \code{.unparsed} + * algorithms: +@@ -90,53 +90,6 @@ constexpr bool fieldSigned() + fieldSize(), \ + fieldSigned() } + +-constexpr Field tnrFields[] = { +- ISPP_FIELD(rkispp_params_tnr, mode), +- ISPP_FIELD(rkispp_params_tnr, opty_en), +- ISPP_FIELD(rkispp_params_tnr, optc_en), +- ISPP_FIELD(rkispp_params_tnr, gain_en), +- ISPP_FIELD(rkispp_params_tnr, pk0_y), +- ISPP_FIELD(rkispp_params_tnr, pk1_y), +- ISPP_FIELD(rkispp_params_tnr, pk0_c), +- ISPP_FIELD(rkispp_params_tnr, pk1_c), +- ISPP_FIELD(rkispp_params_tnr, glb_gain_cur_sqrt), +- ISPP_FIELD(rkispp_params_tnr, sigma_x), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_y0), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_y1), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_y2), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_y3), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yg0), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yg1), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yg2), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yg3), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yl0), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yl1), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_yl2), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_cg0), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_cg1), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_cg2), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_cl0), +- ISPP_FIELD(rkispp_params_tnr, gfcoef_cl1), +- ISPP_FIELD(rkispp_params_tnr, weight_y), +- ISPP_FIELD(rkispp_params_tnr, glb_gain_cur), +- ISPP_FIELD(rkispp_params_tnr, glb_gain_nxt), +- ISPP_FIELD(rkispp_params_tnr, glb_gain_cur_div), +- ISPP_FIELD(rkispp_params_tnr, txt_th1_y), +- ISPP_FIELD(rkispp_params_tnr, txt_th0_c), +- ISPP_FIELD(rkispp_params_tnr, txt_th1_c), +- ISPP_FIELD(rkispp_params_tnr, txt_thy_dlt), +- ISPP_FIELD(rkispp_params_tnr, txt_thc_dlt), +- ISPP_FIELD(rkispp_params_tnr, txt_th0_y), +- ISPP_FIELD(rkispp_params_tnr, sigma_y), +- ISPP_FIELD(rkispp_params_tnr, luma_curve), +- ISPP_FIELD(rkispp_params_tnr, scale_yg), +- ISPP_FIELD(rkispp_params_tnr, scale_yl), +- ISPP_FIELD(rkispp_params_tnr, scale_cg), +- ISPP_FIELD(rkispp_params_tnr, scale_y2cg), +- ISPP_FIELD(rkispp_params_tnr, scale_cl), +- ISPP_FIELD(rkispp_params_tnr, scale_y2cl), +-}; +- + constexpr Field nrFields[] = { + ISPP_FIELD(rkispp_params_nr, uvnr_step1_en), + ISPP_FIELD(rkispp_params_nr, uvnr_step2_en), +@@ -359,10 +312,6 @@ int Ispp::init([[maybe_unused]] IPAConte + { + int ret; + +- ret = loadBlock(tuningData, "Tnr", tnrFields, tnr_); +- if (ret) +- return ret; +- + ret = loadBlock(tuningData, "Nr", nrFields, nr_); + if (ret) + return ret; +@@ -388,8 +337,6 @@ void Ispp::prepareIspp([[maybe_unused]] + [[maybe_unused]] const uint32_t frame, + RkISPPParams *params) + { +- if (tnr_) +- sendBlock(params->block(), *tnr_); + if (nr_) + sendBlock(params->block(), *nr_); + if (shp_) +--- a/src/ipa/rkisp2/algorithms/ispp.h ++++ b/src/ipa/rkisp2/algorithms/ispp.h +@@ -28,7 +28,6 @@ public: + RkISPPParams *params) override; + + private: +- std::optional tnr_; + std::optional nr_; + std::optional shp_; + std::optional fec_; +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -12,6 +12,7 @@ rkisp2_ipa_algorithms = files([ + 'goc.cpp', + 'ispp.cpp', + 'lsc.cpp', ++ 'tnr.cpp', + 'wdr.cpp', + ]) + +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/tnr.cpp +@@ -0,0 +1,662 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 temporal noise reduction ++ */ ++ ++#include "tnr.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++#include ++ ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++/** ++ * \file tnr.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class TemporalNoiseReduction ++ * \brief RkISP2 temporal noise reduction of the ISPP ++ * ++ * The ISPP TNR blends each frame with its filtered predecessor, weighting the ++ * blend by a multi-level noise model: a luminance dependent noise sigma curve, ++ * guided filter kernels and scales per pyramid level and direction. The ISP ++ * supplies a gain map with every frame, which the TNR and the NR scale the ++ * model with block by block. ++ * ++ * The model is calibrated per ISO step, ISO 50 being unity gain. For each ++ * frame the algorithm programs the ISP gain map with the analogue gain and ++ * interpolates the calibration at that gain into the ISPP TNR block, following ++ * the Rockchip AIQ MFNR algorithm (select_mfnr_params_by_ISO() and ++ * mfnr_fix_transfer() of camera_engine_rkaiq) so that its IQ calibration can ++ * be used as is. The calibration is converted from an IQ file with ++ * mfnr-iq2yaml.py. ++ * ++ * \code{.unparsed} ++ * algorithms: ++ * - TemporalNoiseReduction: ++ * local-gain: true ++ * sigma-scale: 1.0 ++ * settings: ++ * - iso: 50 ++ * weight-limit-y: [ 0, 0, 0, 0 ] ++ * ... ++ * \endcode ++ * ++ * With local-gain the TNR takes the gain of each block from the ISP gain map, ++ * otherwise from the global gain. The sigma-scale multiplies the noise sigma ++ * curve; the AIQ raises it together with its motion detection only. ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2Tnr) ++ ++namespace { ++ ++constexpr unsigned int kIsoUnity = 50; ++constexpr unsigned int kPolyOrder = 4; ++constexpr unsigned int kCalBits = 12; ++constexpr unsigned int kRange = 1 << kCalBits; ++constexpr unsigned int kProcBits = 8; ++constexpr unsigned int kStep = 1 << (kCalBits - kProcBits); ++constexpr double kSigmaLimit = 1 << 6; ++ ++/* fractional bits of the TNR register fields, from the AIQ */ ++constexpr int kFracGain = 8; ++constexpr int kFracGainSqrt = 4; ++constexpr int kFracGainSqrtInv = 13; ++constexpr int kFracSigma = 6; ++constexpr int kFracLumaScale = 6; ++constexpr int kFracTxtRatio = 6; ++constexpr int kFracConvWeight = 8; ++constexpr int kFracScaleL = 8; ++constexpr int kFracScaleLUv = 6; ++constexpr int kFracLumaWInChroma = 5; ++ ++/* fractional bits of the ISP gain map exposure gains */ ++constexpr int kFracMapGain = 6; ++constexpr uint32_t kMapGain0Max = (1u << 18) - 1; ++constexpr uint32_t kMapGain12Max = (1u << 14) - 1; ++ ++/* ++ * The gain state of the AIQ ANRGainRatioProcess(): the map and global gains ++ * are scaled by 16 at low gain and by 1/16 at high gain, with hysteresis ++ * between the two thresholds of each switch. ++ */ ++constexpr std::array kGainTh0 = { 2.0, 32.0 }; ++constexpr std::array kGainTh1 = { 4.0, 64.0 }; ++constexpr std::array kGainRatio = { 16.0, 1.0, 1.0 / 16.0 }; ++ ++long roundD(double x) ++{ ++ return static_cast(x + (x > 0 ? 0.5 : -0.5)); ++} ++ ++long fix(double x, int bits) ++{ ++ return roundD(x * (1 << bits)); ++} ++ ++double lerp(double r, double lo, double hi) ++{ ++ return r * (hi - lo) + lo; ++} ++ ++double inverse(double v) ++{ ++ return v ? 1.0 / v : std::numeric_limits::infinity(); ++} ++ ++template ++bool readArray(const ValueNode &node, const char *key, ++ std::array &out) ++{ ++ std::optional> v = ++ node[key].get>(); ++ ++ if (!v || v->size() != N) { ++ LOG(RkISP2Tnr, Error) << key << " needs " << N << " values"; ++ return false; ++ } ++ ++ std::copy(v->begin(), v->end(), out.begin()); ++ return true; ++} ++ ++bool readScalar(const ValueNode &node, const char *key, double &out) ++{ ++ std::optional v = node[key].get(); ++ ++ if (!v) { ++ LOG(RkISP2Tnr, Error) << "Invalid " << key; ++ return false; ++ } ++ ++ out = *v; ++ return true; ++} ++ ++/* The index of the kernel tap (i, j) among the unique taps of a symmetric kernel */ ++unsigned int kernelTap(int i, int j, int rad) ++{ ++ int ia = i > rad ? 2 * rad - i : i; ++ int ja = j > rad ? 2 * rad - j : j; ++ unsigned int n = 0; ++ ++ if (ja < ia) ++ std::swap(ia, ja); ++ ++ for (int ii = rad; ii >= 0; ii--) { ++ for (int jj = rad; jj >= ii; jj--) { ++ if (ia == ii && ja == jj) ++ return n; ++ n++; ++ } ++ } ++ ++ return 0; ++} ++ ++/* Quantise a kernel; the centre tap takes up the rounding so that it sums to one */ ++template ++void kernelFix(int rad, const std::array &coef, uint8_t *out) ++{ ++ double sum = 0.0; ++ long tap; ++ ++ for (int i = 0; i < 2 * rad + 1; i++) { ++ for (int j = 0; j < 2 * rad + 1; j++) { ++ unsigned int k = kernelTap(i, j, rad); ++ ++ tap = fix(coef[k], kFracConvWeight); ++ out[k] = tap; ++ sum += static_cast(tap) / (1 << kFracConvWeight); ++ } ++ } ++ ++ tap = fix(static_cast(out[0]) / (1 << kFracConvWeight) + ++ (1.0 - sum), ++ kFracConvWeight); ++ out[0] = tap; ++} ++ ++/* The tail of the curve repeats its last position, a 0 field as in the AIQ */ ++uint8_t sigmaInterval(unsigned int from, unsigned int to) ++{ ++ if (to <= from) ++ return 0; ++ return static_cast(std::max(std::log2(to - from) - 2, 0.0)); ++} ++ ++} /* namespace */ ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int TemporalNoiseReduction::init([[maybe_unused]] IPAContext &context, ++ const ValueNode &tuningData) ++{ ++ localGain_ = tuningData["local-gain"].get().value_or(true); ++ sigmaScale_ = tuningData["sigma-scale"].get().value_or(1.0); ++ ++ if (!tuningData.contains("settings")) { ++ LOG(RkISP2Tnr, Error) << "No settings"; ++ return -EINVAL; ++ } ++ ++ for (const auto &node : tuningData["settings"].asList()) { ++ std::array curve; ++ bool ok = true; ++ Setting s; ++ double x0; ++ ++ ok &= readScalar(node, "iso", s.iso); ++ ok &= readArray(node, "weight-limit-y", s.weightLimitY); ++ ok &= readArray(node, "weight-limit-uv", s.weightLimitUv); ++ ok &= readArray(node, "ratio-frq", s.ratioFrq); ++ ok &= readArray(node, "luma-w-in-chroma", s.lumaWInChroma); ++ ok &= readArray(node, "noise-curve", curve); ++ ok &= readScalar(node, "noise-curve-x00", x0); ++ ok &= readArray(node, "y-lo-noiseprofile", s.noiseProfile[0]); ++ ok &= readArray(node, "y-hi-noiseprofile", s.noiseProfile[1]); ++ ok &= readArray(node, "y-lo-bfscale", s.bfScale[0]); ++ ok &= readArray(node, "y-hi-bfscale", s.bfScale[1]); ++ ok &= readArray(node, "y-lumanrcurve", s.lumaNrCurve); ++ ok &= readScalar(node, "y-denoisestrength", s.denoiseStrength); ++ ok &= readArray(node, "y-lo-lvl0-gfdelta", s.gfDelta0[0]); ++ ok &= readArray(node, "y-hi-lvl0-gfdelta", s.gfDelta0[1]); ++ ok &= readArray(node, "y-lo-lvl1-gfdelta", s.gfDelta[0][0]); ++ ok &= readArray(node, "y-lo-lvl2-gfdelta", s.gfDelta[0][1]); ++ ok &= readArray(node, "y-lo-lvl3-gfdelta", s.gfDelta[0][2]); ++ ok &= readArray(node, "y-hi-lvl1-gfdelta", s.gfDelta[1][0]); ++ ok &= readArray(node, "y-hi-lvl2-gfdelta", s.gfDelta[1][1]); ++ ok &= readArray(node, "uv-lo-noiseprofile", s.noiseProfileUv[0]); ++ ok &= readArray(node, "uv-hi-noiseprofile", s.noiseProfileUv[1]); ++ ok &= readArray(node, "uv-lo-bfscale", s.bfScaleUv[0]); ++ ok &= readArray(node, "uv-hi-bfscale", s.bfScaleUv[1]); ++ ok &= readScalar(node, "uv-denoisestrength", s.denoiseStrengthUv); ++ ok &= readArray(node, "uv-lo-lvl0-gfdelta", s.gfDeltaUv0[0]); ++ ok &= readArray(node, "uv-hi-lvl0-gfdelta", s.gfDeltaUv0[1]); ++ ok &= readArray(node, "uv-lo-lvl1-gfdelta", s.gfDeltaUv[0][0]); ++ ok &= readArray(node, "uv-lo-lvl2-gfdelta", s.gfDeltaUv[0][1]); ++ ok &= readArray(node, "uv-hi-lvl1-gfdelta", s.gfDeltaUv[1][0]); ++ ok &= readArray(node, "lvl0-gfsigma", s.gfSigma0); ++ ok &= readArray(node, "lvl1-gfsigma", s.gfSigma[0]); ++ ok &= readArray(node, "lvl2-gfsigma", s.gfSigma[1]); ++ ok &= readArray(node, "lvl3-gfsigma", s.gfSigma[2]); ++ if (!ok) ++ return -EINVAL; ++ ++ /* the high direction of the chroma has two levels */ ++ s.gfDeltaUv[1][1] = {}; ++ ++ /* the noise sigma polynomial, flat beyond its last point */ ++ s.noiseCurveX0 = std::min(x0, kRange - 1); ++ s.noiseSigma.resize(kRange); ++ for (unsigned int j = 0; j < kRange; j++) { ++ double y = curve[kPolyOrder]; ++ double xp = 1.0; ++ ++ for (unsigned int o = 1; o <= kPolyOrder; o++) { ++ xp *= j; ++ y += xp * curve[kPolyOrder - o]; ++ } ++ s.noiseSigma[j] = y; ++ } ++ for (unsigned int j = s.noiseCurveX0 + 1; j < kRange; j++) ++ s.noiseSigma[j] = s.noiseSigma[s.noiseCurveX0]; ++ ++ if (!settings_.empty() && s.iso <= settings_.back().iso) { ++ LOG(RkISP2Tnr, Error) << "Settings not sorted by iso"; ++ return -EINVAL; ++ } ++ settings_.push_back(std::move(s)); ++ } ++ ++ if (settings_.size() < 2) { ++ LOG(RkISP2Tnr, Error) << "Need two settings at least"; ++ return -EINVAL; ++ } ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::configure ++ */ ++int TemporalNoiseReduction::configure([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const IPACameraSensorInfo &configInfo) ++{ ++ gainStateFull_ = -1; ++ gainState_ = -1; ++ gain_ = 1.0; ++ ratio_ = gainRatio(gain_); ++ tnr_.reset(); ++ ++ return 0; ++} ++ ++double TemporalNoiseReduction::gainRatio(double gain) ++{ ++ std::array th; ++ int full, cur = -1; ++ ++ for (unsigned int i = 0; i < th.size(); i++) ++ th[i] = std::exp2((std::log2(kGainTh0[i]) + ++ std::log2(kGainTh1[i])) / 2); ++ ++ if (gain <= kGainTh0[0]) { ++ full = 0; ++ cur = 0; ++ } else if (gain <= kGainTh1[0]) { ++ full = 1; ++ } else if (gain <= kGainTh0[1]) { ++ full = 2; ++ cur = 1; ++ } else if (gain <= kGainTh1[1]) { ++ full = 3; ++ } else { ++ full = 4; ++ cur = 2; ++ } ++ ++ if (gainState_ < 0 || ++ (std::abs(full - gainStateFull_) >= 2 && cur < 0)) ++ cur = gain <= th[0] ? 0 : gain <= th[1] ? 1 : 2; ++ ++ if (cur >= 0) { ++ gainState_ = cur; ++ gainStateFull_ = full; ++ } ++ ++ return kGainRatio[gainState_]; ++} ++ ++/** ++ * \brief Interpolate the calibration at an ISO ++ * \param[in] iso The ISO, 50 times the gain ++ * ++ * \return The calibration of the AIQ select_mfnr_params_by_ISO() ++ */ ++TemporalNoiseReduction::Selection ++TemporalNoiseReduction::select(double iso) const ++{ ++ size_t lo = 0, hi = 1; ++ double r = 0.0; ++ Selection s; ++ ++ for (size_t i = 0; i + 1 < settings_.size(); i++) { ++ if (iso >= settings_[i].iso && iso <= settings_[i + 1].iso) { ++ lo = i; ++ hi = i + 1; ++ r = (iso - settings_[i].iso) / ++ (settings_[i + 1].iso - settings_[i].iso); ++ break; ++ } ++ if (iso > settings_.back().iso) { ++ lo = settings_.size() - 2; ++ hi = settings_.size() - 1; ++ r = 1.0; ++ } ++ } ++ ++ const Setting &L = settings_[lo]; ++ const Setting &H = settings_[hi]; ++ ++ auto li = [r](const auto &a, const auto &b, auto &out) { ++ for (size_t k = 0; k < out.size(); k++) ++ out[k] = lerp(r, a[k], b[k]); ++ }; ++ auto liInv = [r](const auto &a, const auto &b, auto &out) { ++ for (size_t k = 0; k < out.size(); k++) ++ out[k] = lerp(r, inverse(a[k]), inverse(b[k])); ++ }; ++ ++ li(L.weightLimitY, H.weightLimitY, s.weightLimitY); ++ li(L.weightLimitUv, H.weightLimitUv, s.weightLimitUv); ++ li(L.ratioFrq, H.ratioFrq, s.ratioFrq); ++ li(L.lumaWInChroma, H.lumaWInChroma, s.lumaWInChroma); ++ li(L.lumaNrCurve, H.lumaNrCurve, s.lumaNrCurve); ++ li(L.gfSigma0, H.gfSigma0, s.gfSigma0); ++ s.dnStr = lerp(r, L.denoiseStrength, H.denoiseStrength); ++ s.dnStrUv = lerp(r, L.denoiseStrengthUv, H.denoiseStrengthUv); ++ for (unsigned int d = 0; d < 2; d++) { ++ liInv(L.noiseProfile[d], H.noiseProfile[d], s.ci[d]); ++ li(L.bfScale[d], H.bfScale[d], s.scale[d]); ++ li(L.gfDelta0[d], H.gfDelta0[d], s.gfDelta0[d]); ++ liInv(L.noiseProfileUv[d], H.noiseProfileUv[d], s.ciUv[d]); ++ li(L.bfScaleUv[d], H.bfScaleUv[d], s.scaleUv[d]); ++ li(L.gfDeltaUv0[d], H.gfDeltaUv0[d], s.gfDeltaUv0[d]); ++ for (unsigned int l = 0; l < kLevels - 1; l++) ++ li(L.gfDelta[d][l], H.gfDelta[d][l], s.gfDelta[d][l]); ++ for (unsigned int l = 0; l < kLevelsUv - 1; l++) ++ li(L.gfDeltaUv[d][l], H.gfDeltaUv[d][l], s.gfDeltaUv[d][l]); ++ } ++ for (unsigned int l = 0; l < kLevels - 1; l++) ++ li(L.gfSigma[l], H.gfSigma[l], s.gfSigma[l]); ++ ++ std::vector sigma(kRange + 1); ++ for (unsigned int j = 0; j < kRange; j++) ++ sigma[j] = lerp(r, L.noiseSigma[j], H.noiseSigma[j]); ++ sigma[kRange] = sigma[kRange - 1]; ++ ++ /* ++ * The curve is sampled at 17 points: dense at the start, at the curve ++ * end and at its extrema, each step a power of two. ++ */ ++ unsigned int x0 = std::max(L.noiseCurveX0, H.noiseCurveX0); ++ std::vector maxima, minima, pos; ++ for (unsigned int i = 1; i < x0; i++) { ++ if (sigma[i] > sigma[i - 1] && sigma[i] > sigma[i + 1]) ++ maxima.push_back(i); ++ if (sigma[i] < sigma[i - 1] && sigma[i] < sigma[i + 1]) ++ minima.push_back(i); ++ } ++ pos = { 0, x0 }; ++ for (size_t i = 0; i < std::min(maxima.size(), 2); i++) ++ pos.push_back(maxima[i]); ++ if (!minima.empty()) ++ pos.push_back(minima[0]); ++ std::sort(pos.begin(), pos.end()); ++ pos.push_back(kRange); ++ ++ for (size_t i = 1; i < pos.size(); i++) { ++ unsigned int q = i == 1 ? kStep * 4 : kStep * 16; ++ ++ pos[i] = pos[i] / q * q; ++ } ++ pos.erase(std::unique(pos.begin(), pos.end()), pos.end()); ++ ++ const unsigned int o4 = 4 * kStep, o8 = 8 * kStep; ++ const unsigned int o16 = 16 * kStep, o32 = 32 * kStep; ++ std::array fx; ++ unsigned int off = 0; ++ size_t idx = 1; ++ ++ for (unsigned int n = 0; n < kCurvePoints - 1; n++) { ++ unsigned int st, d; ++ ++ if (idx >= pos.size() && off >= kRange) { ++ fx[n] = kRange; ++ continue; ++ } ++ fx[n] = off; ++ if (off & o4) ++ st = o4; ++ else if (off & o8) ++ st = o8; ++ else if (off < 64 * kStep || off & o16) ++ st = o16; ++ else ++ st = o32; ++ if (off + st > pos[idx] && off != pos[idx]) { ++ d = pos[idx] - off; ++ st = d & o4 ? o4 : d & o8 ? o8 : d & o16 ? o16 : o32; ++ } ++ if (off + st == pos[idx]) ++ idx++; ++ off += st; ++ } ++ fx[kCurvePoints - 1] = kRange; ++ ++ double sMax = 0.0; ++ for (unsigned int i = 0; i < kCurvePoints; i++) { ++ s.noiseSigma[i] = std::abs(sigma[fx[i]] / kStep); ++ s.fixX[i] = fx[i] / kStep; ++ sMax = std::max(sMax, s.noiseSigma[i]); ++ } ++ ++ double scaleScale = 1.0, sigScale = sigmaScale_; ++ if (sMax * sigmaScale_ > kSigmaLimit) { ++ scaleScale = kSigmaLimit / (sMax * sigmaScale_); ++ sigScale = sigmaScale_ * scaleScale; ++ } ++ for (double &v : s.noiseSigma) ++ v *= sigScale; ++ for (unsigned int d = 0; d < 2; d++) { ++ for (double &v : s.scale[d]) ++ v *= scaleScale; ++ for (double &v : s.scaleUv[d]) ++ v *= scaleScale; ++ } ++ ++ return s; ++} ++ ++/** ++ * \brief Convert a calibration to the TNR block ++ * \param[in] s The calibration at the frame ISO ++ * \param[in] gain The global gain, scaled by the gain state ratio ++ * \param[out] tnr The TNR block, without its header ++ * ++ * Port of the AIQ mfnr_fix_transfer() at unity strength. ++ */ ++void TemporalNoiseReduction::fixTransfer(const Selection &s, double gain, ++ struct rkispp_params_tnr *tnr) ++{ ++ double gs = std::sqrt(gain); ++ double gsi, clp; ++ ++ auto u16 = [](long v) { return static_cast(v); }; ++ auto pk = [](double v) { ++ return static_cast(std::min(static_cast(v), 0xff)); ++ }; ++ auto sly = [&s](unsigned int d, unsigned int l) { ++ return s.ci[d][l] * s.scale[d][l] / s.dnStr; ++ }; ++ auto slyUv = [&s](unsigned int d, unsigned int l) { ++ return s.ci[d][l] * s.scaleUv[d][l] / s.dnStrUv; ++ }; ++ auto slUv = [&s](unsigned int d, unsigned int l) { ++ return s.ciUv[d][l] * s.scaleUv[d][l] / s.dnStrUv; ++ }; ++ ++ tnr->mode = RKISPP_TNR_MODE_2_TO_1; ++ tnr->opty_en = 1; ++ tnr->optc_en = 1; ++ tnr->pk0_y = pk(s.weightLimitY[0]); ++ tnr->pk1_y = pk(s.weightLimitY[kLevels - 1]); ++ tnr->pk0_c = pk(s.weightLimitUv[0]); ++ tnr->pk1_c = pk(s.weightLimitUv[kLevelsUv - 1]); ++ ++ tnr->glb_gain_cur = std::min(fix(gain, kFracGain), 0xffff); ++ tnr->glb_gain_nxt = tnr->glb_gain_cur; ++ gsi = 1.0 / gs; ++ clp = std::clamp(static_cast(gsi * (1 << kFracGainSqrtInv)), ++ -((1 << kFracGainSqrtInv) - 1), ++ (1 << kFracGainSqrtInv) - 1); ++ clp /= 1 << kFracGainSqrtInv; ++ tnr->glb_gain_cur_div = ++ std::min(fix(clp, kFracGainSqrtInv), 0x1fff); ++ tnr->glb_gain_cur_sqrt = std::min(fix(gs, kFracGainSqrt), 0xff); ++ ++ for (unsigned int i = 0; i < kCurvePoints - 1; i++) ++ tnr->sigma_x[i] = sigmaInterval(s.fixX[i], s.fixX[i + 1]); ++ for (unsigned int i = 0; i < kCurvePoints; i++) ++ tnr->sigma_y[i] = u16(fix(s.noiseSigma[i], kFracSigma)); ++ for (unsigned int i = 0; i < RKISPP_TNR_LUMA_CURVE_SIZE; i++) ++ tnr->luma_curve[i] = u16(fix(s.lumaNrCurve[i], kFracLumaScale)); ++ ++ tnr->txt_th0_y = u16(fix(s.ratioFrq[0], kFracTxtRatio)); ++ tnr->txt_th1_y = u16(fix(s.ratioFrq[1], kFracTxtRatio)); ++ tnr->txt_th0_c = u16(fix(s.ratioFrq[2], kFracTxtRatio)); ++ tnr->txt_th1_c = u16(fix(s.ratioFrq[3], kFracTxtRatio)); ++ tnr->txt_thy_dlt = u16(std::floor((1 << kFracTxtRatio) / ++ (s.ratioFrq[1] - s.ratioFrq[0]))); ++ tnr->txt_thc_dlt = u16(std::floor((1 << kFracTxtRatio) / ++ (s.ratioFrq[3] - s.ratioFrq[2]))); ++ ++ kernelFix(2, s.gfSigma0, tnr->gfcoef_y0); ++ kernelFix(1, s.gfSigma[0], tnr->gfcoef_y1); ++ kernelFix(1, s.gfSigma[1], tnr->gfcoef_y2); ++ kernelFix(1, s.gfSigma[2], tnr->gfcoef_y3); ++ kernelFix(2, s.gfDelta0[0], tnr->gfcoef_yg0); ++ kernelFix(1, s.gfDelta[0][0], tnr->gfcoef_yg1); ++ kernelFix(1, s.gfDelta[0][1], tnr->gfcoef_yg2); ++ kernelFix(1, s.gfDelta[0][2], tnr->gfcoef_yg3); ++ kernelFix(2, s.gfDelta0[1], tnr->gfcoef_yl0); ++ kernelFix(1, s.gfDelta[1][0], tnr->gfcoef_yl1); ++ kernelFix(1, s.gfDelta[1][1], tnr->gfcoef_yl2); ++ kernelFix(2, s.gfDeltaUv0[0], tnr->gfcoef_cg0); ++ kernelFix(1, s.gfDeltaUv[0][0], tnr->gfcoef_cg1); ++ kernelFix(1, s.gfDeltaUv[0][1], tnr->gfcoef_cg2); ++ kernelFix(2, s.gfDeltaUv0[1], tnr->gfcoef_cl0); ++ kernelFix(1, s.gfDeltaUv[1][0], tnr->gfcoef_cl1); ++ ++ for (unsigned int l = 0; l < RKISPP_TNR_SCALE_YG_SIZE; l++) ++ tnr->scale_yg[l] = u16(fix(sly(0, l), kFracScaleL)); ++ for (unsigned int l = 0; l < RKISPP_TNR_SCALE_YL_SIZE; l++) ++ tnr->scale_yl[l] = u16(fix(sly(1, l), kFracScaleL)); ++ for (unsigned int l = 0; l < RKISPP_TNR_SCALE_CG_SIZE; l++) { ++ tnr->scale_cg[l] = u16(fix(slUv(0, l), kFracScaleLUv)); ++ tnr->scale_y2cg[l] = u16(fix(slyUv(0, l), kFracScaleLUv)); ++ } ++ for (unsigned int l = 0; l < RKISPP_TNR_SCALE_CL_SIZE; l++) ++ tnr->scale_cl[l] = u16(fix(slUv(1, l), kFracScaleLUv)); ++ for (unsigned int l = 0; l < RKISPP_TNR_SCALE_Y2CL_SIZE; l++) ++ tnr->scale_y2cl[l] = u16(fix(slyUv(1, l), kFracScaleLUv)); ++ for (unsigned int l = 0; l < RKISPP_TNR_WEIGHT_Y_SIZE; l++) ++ tnr->weight_y[l] = static_cast( ++ fix(s.lumaWInChroma[l], kFracLumaWInChroma)); ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::prepare ++ */ ++void TemporalNoiseReduction::prepare([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) ++{ ++ double gain = frameContext.agc.gain; ++ uint32_t map; ++ ++ if (gain <= 0.0) ++ gain = 1.0; ++ ++ gain_ = gain; ++ ratio_ = gainRatio(gain); ++ ++ auto config = params->block(); ++ config.setEnabled(true); ++ ++ map = std::max(roundD(gain * ratio_ * (1 << kFracMapGain)), 0); ++ config->gain[0] = std::min(map, kMapGain0Max); ++ config->gain[1] = std::min(map, kMapGain12Max); ++ config->gain[2] = std::min(map, kMapGain12Max); ++} ++ ++/** ++ * \brief Send the TNR block for the gain of the latest ISP frame ++ * \param[in] context The shared IPA context ++ * \param[in] frame The ISPP frame number ++ * \param[out] params The ISPP parameters buffer ++ */ ++void TemporalNoiseReduction::prepareIspp([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ RkISPPParams *params) ++{ ++ constexpr size_t header = sizeof(struct v4l2_isp_params_block_header); ++ double scaled = gain_ * ratio_; ++ ++ if (!tnr_ || std::abs(scaled - tnrGain_) > 0.005 * tnrGain_) { ++ struct rkispp_params_tnr tnr = {}; ++ ++ fixTransfer(select(gain_ * kIsoUnity), scaled, &tnr); ++ tnr.gain_en = !localGain_; ++ tnr_ = tnr; ++ tnrGain_ = scaled; ++ ++ LOG(RkISP2Tnr, Debug) ++ << "gain " << gain_ << " ratio " << ratio_ ++ << " sigma " << tnr.sigma_y[4] << " pk " ++ << static_cast(tnr.pk0_y); ++ } ++ ++ auto block = params->block(); ++ memcpy(reinterpret_cast(&*block) + header, ++ reinterpret_cast(&*tnr_) + header, ++ sizeof(*tnr_) - header); ++ block.setEnabled(true); ++} ++ ++REGISTER_IPA_ALGORITHM(TemporalNoiseReduction, "TemporalNoiseReduction") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/tnr.h +@@ -0,0 +1,112 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 temporal noise reduction ++ */ ++ ++#pragma once ++ ++#include ++#include ++#include ++ ++#include ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class TemporalNoiseReduction : public Algorithm ++{ ++public: ++ TemporalNoiseReduction() = default; ++ ~TemporalNoiseReduction() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ int configure(IPAContext &context, ++ const IPACameraSensorInfo &configInfo) override; ++ void prepare(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ RkISP2Params *params) override; ++ void prepareIspp(IPAContext &context, const uint32_t frame, ++ RkISPPParams *params) override; ++ ++ static constexpr unsigned int kLevels = 4; ++ static constexpr unsigned int kLevelsUv = 3; ++ static constexpr unsigned int kCurvePoints = 17; ++ ++ using Lvl = std::array; ++ using LvlUv = std::array; ++ using Coef6 = std::array; ++ using Coef3 = std::array; ++ ++ struct Setting { ++ double iso; ++ Lvl weightLimitY; ++ LvlUv weightLimitUv; ++ std::array ratioFrq; ++ LvlUv lumaWInChroma; ++ unsigned int noiseCurveX0; ++ std::vector noiseSigma; ++ std::array noiseProfile; ++ std::array bfScale; ++ Coef6 lumaNrCurve; ++ double denoiseStrength; ++ std::array gfDelta0; ++ std::array, 2> gfDelta; ++ std::array noiseProfileUv; ++ std::array bfScaleUv; ++ double denoiseStrengthUv; ++ std::array gfDeltaUv0; ++ std::array, 2> gfDeltaUv; ++ Coef6 gfSigma0; ++ std::array gfSigma; ++ }; ++ ++ struct Selection { ++ Lvl weightLimitY; ++ LvlUv weightLimitUv; ++ std::array ratioFrq; ++ LvlUv lumaWInChroma; ++ std::array ci; ++ std::array scale; ++ Coef6 lumaNrCurve; ++ double dnStr; ++ std::array gfDelta0; ++ std::array, 2> gfDelta; ++ std::array ciUv; ++ std::array scaleUv; ++ double dnStrUv; ++ std::array gfDeltaUv0; ++ std::array, 2> gfDeltaUv; ++ Coef6 gfSigma0; ++ std::array gfSigma; ++ std::array noiseSigma; ++ std::array fixX; ++ }; ++ ++ Selection select(double iso) const; ++ static void fixTransfer(const Selection &s, double gain, ++ struct rkispp_params_tnr *tnr); ++ ++private: ++ double gainRatio(double gain); ++ ++ std::vector settings_; ++ double sigmaScale_; ++ bool localGain_; ++ ++ int gainStateFull_; ++ int gainState_; ++ double gain_; ++ double ratio_; ++ ++ std::optional tnr_; ++ double tnrGain_; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ +--- a/src/ipa/rkisp2/params.h ++++ b/src/ipa/rkisp2/params.h +@@ -35,6 +35,7 @@ enum class RkISP2ParamsBlocks : uint16_t + Dpcc, + Cproc, + Wdr, ++ Gain, + }; + + namespace details { +@@ -67,6 +68,7 @@ RkISP2_DEFINE_PARAMS_BLOCK_TYPE(AwbMeas, + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Dpcc, dpcc, DPCC); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Cproc, cproc, CPROC); + RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Wdr, wdr, WDR); ++RkISP2_DEFINE_PARAMS_BLOCK_TYPE(Gain, gain, GAIN); + + struct param_traits { + using id_type = RkISP2ParamsBlocks; diff --git a/multimedia/libcamera/patches/236-ipa-rkisp2-imx415-add-the-temporal-noise-reduction-calibration.patch b/multimedia/libcamera/patches/236-ipa-rkisp2-imx415-add-the-temporal-noise-reduction-calibration.patch new file mode 100644 index 0000000000..7aff80a23c --- /dev/null +++ b/multimedia/libcamera/patches/236-ipa-rkisp2-imx415-add-the-temporal-noise-reduction-calibration.patch @@ -0,0 +1,478 @@ +From 9a7a5865d6ebdeac1ac63c6957cf3b4efdc0edbf Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Wed, 4 Nov 2026 21:35:53 +0000 +Subject: [PATCH] ipa: rkisp2: imx415: Add the temporal noise reduction + calibration + +Enable the temporal noise reduction with the MFNR calibration of the +Rockchip IQ file for this sensor (normal mode, LSNR setting), converted +with mfnr-iq2yaml.py. + +The IQ weight limits are close to zero, as the vendor pairs them with +its closed motion detection. Without it, low-contrast motion fades over +about ten frames. The limits are set to 64 up to ISO 200, 96 at ISO 400 +and 128 from ISO 800, which keeps a moving object's trail within about +three frames in the dark at the cost of weaker averaging there. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/data/imx415.yaml | 447 ++++++++++++++++++++++++++++++++ + 1 file changed, 447 insertions(+) + +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -17,6 +17,453 @@ algorithms: + ExposureShort: + exposureTime: [ 100, 5000, 10000, 20000, 120000 ] + gain: [ 1.0, 2.0, 4.0, 6.0, 6.0 ] ++ - TemporalNoiseReduction: ++ # converted from the vendor IQ MFNR normal/LSNR setting by ++ # mfnr-iq2yaml.py; one entry per ISO step, gain = iso / 50; the ++ # weight limits are raised over the IQ, which relies on motion detection ++ settings: ++ - iso: 50 ++ weight-limit-y: [ 64, 64, 64, 64 ] ++ weight-limit-uv: [ 64, 64, 64 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.5, 0.5, 0.5 ] ++ noise-curve: [ -1.08097064118e-12, 1.01241568527e-08, -3.39654575524e-05, 0.0430931083487, 11.4384162926 ] ++ noise-curve-x00: 3647 ++ y-lo-noiseprofile: [ 1, 0.74769, 0.46958, 0.26844 ] ++ y-hi-noiseprofile: [ 0.76118, 0.7238, 0.54969, 0 ] ++ y-lo-bfscale: [ 0.4, 0.4, 0.4, 0.35 ] ++ y-hi-bfscale: [ 0.4, 0.4, 0.4, 0.35 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 2 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.74769, 0.46958 ] ++ uv-hi-noiseprofile: [ 0.76118, 0.7238, 0.3574 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 2 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 100 ++ weight-limit-y: [ 64, 64, 64, 64 ] ++ weight-limit-uv: [ 64, 64, 64 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.5, 0.5, 0.5 ] ++ noise-curve: [ -1.42643494505e-12, 1.34890885238e-08, -4.57067447826e-05, 0.0584140906109, 15.3080175136 ] ++ noise-curve-x00: 3647 ++ y-lo-noiseprofile: [ 1, 0.74366, 0.45724, 0.25642 ] ++ y-hi-noiseprofile: [ 0.74715, 0.68663, 0.49695, 0 ] ++ y-lo-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-hi-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 2 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.74366, 0.45724 ] ++ uv-hi-noiseprofile: [ 0.74715, 0.68663, 0.38596 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 2 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 200 ++ weight-limit-y: [ 64, 64, 64, 64 ] ++ weight-limit-uv: [ 64, 64, 64 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.5, 0.5, 0.5 ] ++ noise-curve: [ -1.92489788527e-12, 1.83133699063e-08, -6.28272056364e-05, 0.0816138655195, 20.494600962 ] ++ noise-curve-x00: 3790 ++ y-lo-noiseprofile: [ 1, 0.74803, 0.45862, 0.26022 ] ++ y-hi-noiseprofile: [ 0.72292, 0.6571, 0.45054, 0 ] ++ y-lo-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-hi-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 3 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.74803, 0.45862 ] ++ uv-hi-noiseprofile: [ 0.72292, 0.6571, 0.3574 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 3 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 400 ++ weight-limit-y: [ 96, 96, 96, 96 ] ++ weight-limit-uv: [ 96, 96, 96 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.5, 0.5, 0.5 ] ++ noise-curve: [ -2.92316241432e-12, 2.75250206713e-08, -9.32202602113e-05, 0.119250987692, 29.1180509503 ] ++ noise-curve-x00: 3714 ++ y-lo-noiseprofile: [ 1, 0.76421, 0.48242, 0.28359 ] ++ y-hi-noiseprofile: [ 0.69204, 0.64101, 0.43381, 0 ] ++ y-lo-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-hi-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 3 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.76421, 0.48242 ] ++ uv-hi-noiseprofile: [ 0.69204, 0.64101, 0.39704 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 3 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 800 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.6, 0.6, 0.6 ] ++ noise-curve: [ -3.66919679355e-12, 3.40534546077e-08, -0.000114675923701, 0.14507808339, 53.0572121429 ] ++ noise-curve-x00: 3331 ++ y-lo-noiseprofile: [ 1, 0.76633, 0.48565, 0.28995 ] ++ y-hi-noiseprofile: [ 0.68362, 0.62949, 0.42035, 0 ] ++ y-lo-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-hi-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 3 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.76633, 0.48565 ] ++ uv-hi-noiseprofile: [ 0.68362, 0.62949, 0.39342 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 3 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 1600 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.65, 0.65, 0.65 ] ++ noise-curve: [ -6.88500038533e-12, 6.28952567479e-08, -0.000206166706955, 0.256322540496, 58.7093336455 ] ++ noise-curve-x00: 3244 ++ y-lo-noiseprofile: [ 1, 0.75127, 0.46507, 0.26956 ] ++ y-hi-noiseprofile: [ 0.69931, 0.62307, 0.40394, 0 ] ++ y-lo-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-hi-bfscale: [ 0.45, 0.45, 0.45, 0.35 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 3.5 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.75127, 0.46507 ] ++ uv-hi-noiseprofile: [ 0.69931, 0.62307, 0.39427 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 3.5 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 3200 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.7, 0.7, 0.7 ] ++ noise-curve: [ -9.464965e-12, 8.72810970505e-08, -0.000290638086962, 0.366968609966, 82.1710507886 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.75531, 0.46713, 0.26197 ] ++ y-hi-noiseprofile: [ 0.6933, 0.62411, 0.4078, 0 ] ++ y-lo-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ y-hi-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 1 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.75531, 0.46713 ] ++ uv-hi-noiseprofile: [ 0.6933, 0.62411, 0.39626 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.4 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.4 ] ++ uv-denoisestrength: 1 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 6400 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.75, 0.75, 0.75 ] ++ noise-curve: [ -1.30178255978e-11, 1.23199356009e-07, -0.000429366291333, 0.578668286916, 75.3975737482 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.76594, 0.47372, 0.26397 ] ++ y-hi-noiseprofile: [ 0.68205, 0.62966, 0.41234, 0 ] ++ y-lo-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ y-hi-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 1 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.76594, 0.47372 ] ++ uv-hi-noiseprofile: [ 0.68205, 0.62966, 0.39626 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.4 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.4 ] ++ uv-denoisestrength: 1 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 12800 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.8, 0.8, 0.8 ] ++ noise-curve: [ -1.06902659849e-11, 1.01044089872e-07, -0.000390330176439, 0.618080545783, 124.273449663 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.75532, 0.4663, 0.25807 ] ++ y-hi-noiseprofile: [ 0.69174, 0.62165, 0.40573, 0 ] ++ y-lo-bfscale: [ 0.6, 0.6, 0.6, 0.6 ] ++ y-hi-bfscale: [ 0.6, 0.6, 0.6, 0.6 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 1.1 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.75532, 0.4663 ] ++ uv-hi-noiseprofile: [ 0.69174, 0.62165, 0.39626 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 1.1 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 25600 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.8, 0.8, 0.6 ] ++ noise-curve: [ -1.06902659849e-11, 1.01044089872e-07, -0.000390330176439, 0.618080545783, 124.273449663 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.75532, 0.4663, 0.25807 ] ++ y-hi-noiseprofile: [ 0.69174, 0.62165, 0.40573, 0 ] ++ y-lo-bfscale: [ 0.6, 0.6, 0.6, 0.6 ] ++ y-hi-bfscale: [ 0.6, 0.6, 0.6, 0.6 ] ++ y-lumanrcurve: [ 1, 1, 1, 1, 1, 1 ] ++ y-denoisestrength: 1.2 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.75532, 0.4663 ] ++ uv-hi-noiseprofile: [ 0.69174, 0.62165, 0.39626 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.3 ] ++ uv-denoisestrength: 1.2 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 51200 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 7, 7.140625, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.8, 0.8, 0.6 ] ++ noise-curve: [ -2.35581884731e-11, 1.55685371261e-07, -0.000452079932134, 0.788144215984, 204.57332096 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.74142, 0.45606, 0.25969 ] ++ y-hi-noiseprofile: [ 0.70202, 0.61152, 0.39843, 0 ] ++ y-lo-bfscale: [ 0.55, 0.5, 0.5, 0.55 ] ++ y-hi-bfscale: [ 0.5, 0.5, 0.5, 0.48 ] ++ y-lumanrcurve: [ 1.5, 1.5, 1.5, 1.5, 1.5, 1.3 ] ++ y-denoisestrength: 1.8 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.74142, 0.45606 ] ++ uv-hi-noiseprofile: [ 0.70202, 0.61152, 0.39626 ] ++ uv-lo-bfscale: [ 0.4, 0.4, 0.5 ] ++ uv-hi-bfscale: [ 0.4, 0.4, 0.4 ] ++ uv-denoisestrength: 2 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 102400 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.8, 0.8, 0.8 ] ++ noise-curve: [ -5.24186459287e-12, 4.83761295342e-08, -0.000158009865774, 0.205697200837, -24.6859972726 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.72924, 0.45749, 0.28517 ] ++ y-hi-noiseprofile: [ 0.7296, 0.61071, 0.39626, 0 ] ++ y-lo-bfscale: [ 0.5, 0.5, 0.5, 0.7 ] ++ y-hi-bfscale: [ 0.5, 0.5, 0.5, 0.7 ] ++ y-lumanrcurve: [ 1, 1, 1, 1.4, 1.4, 1.4 ] ++ y-denoisestrength: 1.8 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.72924, 0.45749 ] ++ uv-hi-noiseprofile: [ 0.7296, 0.61071, 0.39626 ] ++ uv-lo-bfscale: [ 0.5, 0.7, 0.7 ] ++ uv-hi-bfscale: [ 0.3, 0.3, 0.4 ] ++ uv-denoisestrength: 1.8 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - iso: 204800 ++ weight-limit-y: [ 128, 128, 128, 128 ] ++ weight-limit-uv: [ 128, 128, 128 ] ++ ratio-frq: [ 0.5, 2, 0.5, 2 ] ++ luma-w-in-chroma: [ 0.8, 0.8, 0.8 ] ++ noise-curve: [ -5.24186459287e-12, 4.83761295342e-08, -0.000158009865774, 0.205697200837, -24.6859972726 ] ++ noise-curve-x00: 3170 ++ y-lo-noiseprofile: [ 1, 0.72924, 0.45749, 0.28517 ] ++ y-hi-noiseprofile: [ 0.7296, 0.61071, 0.39626, 0 ] ++ y-lo-bfscale: [ 0.5, 0.5, 0.5, 0.7 ] ++ y-hi-bfscale: [ 0.5, 0.5, 0.5, 0.7 ] ++ y-lumanrcurve: [ 1, 1, 1, 1.4, 1.4, 1.4 ] ++ y-denoisestrength: 1.8 ++ y-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-lo-lvl3-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ y-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ y-hi-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-noiseprofile: [ 1, 0.72924, 0.45749 ] ++ uv-hi-noiseprofile: [ 0.7296, 0.61071, 0.39626 ] ++ uv-lo-bfscale: [ 0.5, 0.7, 0.7 ] ++ uv-hi-bfscale: [ 0.3, 0.3, 0.4 ] ++ uv-denoisestrength: 1.8 ++ uv-lo-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-lo-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-lo-lvl2-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ uv-hi-lvl0-gfdelta: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ uv-hi-lvl1-gfdelta: [ 0.125, 0.10938, 0.10938 ] ++ lvl0-gfsigma: [ 0.0625, 0.03906, 0.03906, 0.03906, 0.03906, 0.03906 ] ++ lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] + - Awb: + algorithm: "grey" + AwbMode: diff --git a/multimedia/libcamera/patches/237-ipa-rkisp2-share-the-iso-step-calibration-helpers.patch b/multimedia/libcamera/patches/237-ipa-rkisp2-share-the-iso-step-calibration-helpers.patch new file mode 100644 index 0000000000..74798f21cd --- /dev/null +++ b/multimedia/libcamera/patches/237-ipa-rkisp2-share-the-iso-step-calibration-helpers.patch @@ -0,0 +1,440 @@ +From a908745a9e725808d1a07500b5104ecc966176f8 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 10:40:56 +0000 +Subject: [PATCH 237/239] ipa: rkisp2: Share the ISO step calibration helpers + +The Rockchip AIQ calibrates its noise reduction and sharpening per ISO +step and runs them on one gain state, the ratio by which the gain map +is scaled at low and high gain. The temporal noise reduction kept both +the calibration helpers and that state to itself. + +Move the helpers to a calibration module and the gain, its ratio and +the use of the gain map to the active state, where the spatial noise +reduction can use them too. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/algorithms/calibration.cpp | 90 +++++++++++++++++++ + src/ipa/rkisp2/algorithms/calibration.h | 72 +++++++++++++++ + src/ipa/rkisp2/algorithms/meson.build | 1 + + src/ipa/rkisp2/algorithms/tnr.cpp | 104 +++++++--------------- + src/ipa/rkisp2/algorithms/tnr.h | 3 +- + src/ipa/rkisp2/ipa_context.h | 6 ++ + 6 files changed, 201 insertions(+), 75 deletions(-) + create mode 100644 src/ipa/rkisp2/algorithms/calibration.cpp + create mode 100644 src/ipa/rkisp2/algorithms/calibration.h + +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/calibration.cpp +@@ -0,0 +1,90 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 ISO step calibrations of the Rockchip AIQ ++ */ ++ ++#include "calibration.h" ++ ++/** ++ * \file calibration.h ++ * \brief Helpers for the ISO step calibrations of the Rockchip AIQ ++ * ++ * The noise reduction and sharpening calibrations of a Rockchip IQ file hold ++ * one setting per ISO step, ISO 50 being unity gain. The AIQ interpolates ++ * them linearly between the two steps around the ISO of a frame and clamps to ++ * the first and last step. ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::calibration { ++ ++LOG_DEFINE_CATEGORY(RkISP2Calibration) ++ ++/** ++ * \brief Round half away from zero, as ROUND_F() of the AIQ ++ * \param[in] x The value to round ++ * \return The rounded value ++ */ ++long roundF(double x) ++{ ++ return static_cast(x + (x > 0 ? 0.5 : -0.5)); ++} ++ ++/** ++ * \brief Interpolate linearly ++ * \param[in] r The position between \a lo (0) and \a hi (1) ++ * \param[in] lo The value at 0 ++ * \param[in] hi The value at 1 ++ * \return The interpolated value ++ */ ++double lerp(double r, double lo, double hi) ++{ ++ return r * (hi - lo) + lo; ++} ++ ++/** ++ * \brief Read a scalar of a calibration entry ++ * \param[in] node The calibration entry ++ * \param[in] key The key of the scalar ++ * \param[out] out The value ++ * \return True on success ++ */ ++bool readScalar(const ValueNode &node, const char *key, double &out) ++{ ++ std::optional v = node[key].get(); ++ ++ if (!v) { ++ LOG(RkISP2Calibration, Error) << "Invalid " << key; ++ return false; ++ } ++ ++ out = *v; ++ return true; ++} ++ ++/** ++ * \brief Find the ISO steps around an ISO ++ * \param[in] isos The ISO of each step, ascending, two at least ++ * \param[in] iso The ISO to look up ++ * \return The steps below and above \a iso and its position between them, ++ * clamped to the first and last step ++ */ ++IsoInterval isoInterval(const std::vector &isos, double iso) ++{ ++ for (size_t i = 0; i + 1 < isos.size(); i++) { ++ if (iso >= isos[i] && iso <= isos[i + 1]) ++ return { i, i + 1, (iso - isos[i]) / (isos[i + 1] - isos[i]) }; ++ } ++ ++ if (iso < isos.front()) ++ return { 0, 1, 0.0 }; ++ ++ return { isos.size() - 2, isos.size() - 1, 1.0 }; ++} ++ ++} /* namespace ipa::rkisp2::calibration */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/calibration.h +@@ -0,0 +1,72 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 ISO step calibrations of the Rockchip AIQ ++ */ ++ ++#pragma once ++ ++#include ++#include ++#include ++#include ++#include ++ ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::calibration { ++ ++LOG_DECLARE_CATEGORY(RkISP2Calibration) ++ ++constexpr double kIsoUnity = 50.0; ++ ++/* Relative gain change below which a converted block is reused */ ++constexpr double kGainTolerance = 0.005; ++ ++struct IsoInterval { ++ size_t lo; ++ size_t hi; ++ double r; ++}; ++ ++long roundF(double x); ++double lerp(double r, double lo, double hi); ++bool readScalar(const ValueNode &node, const char *key, double &out); ++IsoInterval isoInterval(const std::vector &isos, double iso); ++ ++template ++bool readArray(const ValueNode &node, const char *key, ++ std::array &out) ++{ ++ std::optional> v = ++ node[key].get>(); ++ ++ if (!v || v->size() != N) { ++ LOG(RkISP2Calibration, Error) << key << " needs " << N << " values"; ++ return false; ++ } ++ ++ std::copy(v->begin(), v->end(), out.begin()); ++ return true; ++} ++ ++template ++std::array lerp(double r, const std::array &lo, ++ const std::array &hi) ++{ ++ std::array out; ++ ++ for (size_t k = 0; k < N; k++) ++ out[k] = lerp(r, lo[k], hi[k]); ++ ++ return out; ++} ++ ++} /* namespace ipa::rkisp2::calibration */ ++ ++} /* namespace libcamera */ +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -4,6 +4,7 @@ rkisp2_ipa_algorithms = files([ + 'agc.cpp', + 'awb.cpp', + 'bls.cpp', ++ 'calibration.cpp', + 'ccm.cpp', + 'cproc.cpp', + 'crop.cpp', +--- a/src/ipa/rkisp2/algorithms/tnr.cpp ++++ b/src/ipa/rkisp2/algorithms/tnr.cpp +@@ -22,6 +22,8 @@ + + #include "libcamera/internal/value_node.h" + ++#include "calibration.h" ++ + /** + * \file tnr.h + */ +@@ -68,7 +70,10 @@ LOG_DEFINE_CATEGORY(RkISP2Tnr) + + namespace { + +-constexpr unsigned int kIsoUnity = 50; ++using calibration::lerp; ++using calibration::readArray; ++using calibration::readScalar; ++ + constexpr unsigned int kPolyOrder = 4; + constexpr unsigned int kCalBits = 12; + constexpr unsigned int kRange = 1 << kCalBits; +@@ -102,19 +107,9 @@ constexpr std::array kGainTh0 + constexpr std::array kGainTh1 = { 4.0, 64.0 }; + constexpr std::array kGainRatio = { 16.0, 1.0, 1.0 / 16.0 }; + +-long roundD(double x) +-{ +- return static_cast(x + (x > 0 ? 0.5 : -0.5)); +-} +- + long fix(double x, int bits) + { +- return roundD(x * (1 << bits)); +-} +- +-double lerp(double r, double lo, double hi) +-{ +- return r * (hi - lo) + lo; ++ return calibration::roundF(x * (1 << bits)); + } + + double inverse(double v) +@@ -122,35 +117,6 @@ double inverse(double v) + return v ? 1.0 / v : std::numeric_limits::infinity(); + } + +-template +-bool readArray(const ValueNode &node, const char *key, +- std::array &out) +-{ +- std::optional> v = +- node[key].get>(); +- +- if (!v || v->size() != N) { +- LOG(RkISP2Tnr, Error) << key << " needs " << N << " values"; +- return false; +- } +- +- std::copy(v->begin(), v->end(), out.begin()); +- return true; +-} +- +-bool readScalar(const ValueNode &node, const char *key, double &out) +-{ +- std::optional v = node[key].get(); +- +- if (!v) { +- LOG(RkISP2Tnr, Error) << "Invalid " << key; +- return false; +- } +- +- out = *v; +- return true; +-} +- + /* The index of the kernel tap (i, j) among the unique taps of a symmetric kernel */ + unsigned int kernelTap(int i, int j, int rad) + { +@@ -285,6 +251,7 @@ int TemporalNoiseReduction::init([[maybe + LOG(RkISP2Tnr, Error) << "Settings not sorted by iso"; + return -EINVAL; + } ++ isos_.push_back(s.iso); + settings_.push_back(std::move(s)); + } + +@@ -299,13 +266,14 @@ int TemporalNoiseReduction::init([[maybe + /** + * \copydoc libcamera::ipa::Algorithm::configure + */ +-int TemporalNoiseReduction::configure([[maybe_unused]] IPAContext &context, ++int TemporalNoiseReduction::configure(IPAContext &context, + [[maybe_unused]] const IPACameraSensorInfo &configInfo) + { + gainStateFull_ = -1; + gainState_ = -1; +- gain_ = 1.0; +- ratio_ = gainRatio(gain_); ++ context.activeState.noiseGain.gain = 1.0; ++ context.activeState.noiseGain.ratio = gainRatio(1.0); ++ context.activeState.noiseGain.localGain = localGain_; + tnr_.reset(); + + return 0; +@@ -356,28 +324,12 @@ double TemporalNoiseReduction::gainRatio + TemporalNoiseReduction::Selection + TemporalNoiseReduction::select(double iso) const + { +- size_t lo = 0, hi = 1; +- double r = 0.0; ++ calibration::IsoInterval in = calibration::isoInterval(isos_, iso); ++ const Setting &L = settings_[in.lo]; ++ const Setting &H = settings_[in.hi]; ++ double r = in.r; + Selection s; + +- for (size_t i = 0; i + 1 < settings_.size(); i++) { +- if (iso >= settings_[i].iso && iso <= settings_[i + 1].iso) { +- lo = i; +- hi = i + 1; +- r = (iso - settings_[i].iso) / +- (settings_[i + 1].iso - settings_[i].iso); +- break; +- } +- if (iso > settings_.back().iso) { +- lo = settings_.size() - 2; +- hi = settings_.size() - 1; +- r = 1.0; +- } +- } +- +- const Setting &L = settings_[lo]; +- const Setting &H = settings_[hi]; +- + auto li = [r](const auto &a, const auto &b, auto &out) { + for (size_t k = 0; k < out.size(); k++) + out[k] = lerp(r, a[k], b[k]); +@@ -598,24 +550,27 @@ void TemporalNoiseReduction::fixTransfer + /** + * \copydoc libcamera::ipa::Algorithm::prepare + */ +-void TemporalNoiseReduction::prepare([[maybe_unused]] IPAContext &context, ++void TemporalNoiseReduction::prepare(IPAContext &context, + [[maybe_unused]] const uint32_t frame, + IPAFrameContext &frameContext, + RkISP2Params *params) + { ++ auto &noiseGain = context.activeState.noiseGain; + double gain = frameContext.agc.gain; + uint32_t map; + + if (gain <= 0.0) + gain = 1.0; + +- gain_ = gain; +- ratio_ = gainRatio(gain); ++ noiseGain.gain = gain; ++ noiseGain.ratio = gainRatio(gain); + + auto config = params->block(); + config.setEnabled(true); + +- map = std::max(roundD(gain * ratio_ * (1 << kFracMapGain)), 0); ++ map = std::max(calibration::roundF(gain * noiseGain.ratio * ++ (1 << kFracMapGain)), ++ 0); + config->gain[0] = std::min(map, kMapGain0Max); + config->gain[1] = std::min(map, kMapGain12Max); + config->gain[2] = std::min(map, kMapGain12Max); +@@ -627,23 +582,26 @@ void TemporalNoiseReduction::prepare([[m + * \param[in] frame The ISPP frame number + * \param[out] params The ISPP parameters buffer + */ +-void TemporalNoiseReduction::prepareIspp([[maybe_unused]] IPAContext &context, ++void TemporalNoiseReduction::prepareIspp(IPAContext &context, + [[maybe_unused]] const uint32_t frame, + RkISPPParams *params) + { + constexpr size_t header = sizeof(struct v4l2_isp_params_block_header); +- double scaled = gain_ * ratio_; ++ const auto &noiseGain = context.activeState.noiseGain; ++ double scaled = noiseGain.gain * noiseGain.ratio; + +- if (!tnr_ || std::abs(scaled - tnrGain_) > 0.005 * tnrGain_) { ++ if (!tnr_ || ++ std::abs(scaled - tnrGain_) > calibration::kGainTolerance * tnrGain_) { + struct rkispp_params_tnr tnr = {}; + +- fixTransfer(select(gain_ * kIsoUnity), scaled, &tnr); ++ fixTransfer(select(noiseGain.gain * calibration::kIsoUnity), ++ scaled, &tnr); + tnr.gain_en = !localGain_; + tnr_ = tnr; + tnrGain_ = scaled; + + LOG(RkISP2Tnr, Debug) +- << "gain " << gain_ << " ratio " << ratio_ ++ << "gain " << noiseGain.gain << " ratio " << noiseGain.ratio + << " sigma " << tnr.sigma_y[4] << " pk " + << static_cast(tnr.pk0_y); + } +--- a/src/ipa/rkisp2/algorithms/tnr.h ++++ b/src/ipa/rkisp2/algorithms/tnr.h +@@ -96,13 +96,12 @@ private: + double gainRatio(double gain); + + std::vector settings_; ++ std::vector isos_; + double sigmaScale_; + bool localGain_; + + int gainStateFull_; + int gainState_; +- double gain_; +- double ratio_; + + std::optional tnr_; + double tnrGain_; +--- a/src/ipa/rkisp2/ipa_context.h ++++ b/src/ipa/rkisp2/ipa_context.h +@@ -144,6 +144,12 @@ struct IPAActiveState { + Rectangle crop; + bool set; + } crop; ++ ++ struct { ++ double gain; ++ double ratio; ++ bool localGain; ++ } noiseGain; + }; + + struct IPAFrameContext : public FrameContext { diff --git a/multimedia/libcamera/patches/238-ipa-rkisp2-add-the-spatial-noise-reduction-and-sharpening.patch b/multimedia/libcamera/patches/238-ipa-rkisp2-add-the-spatial-noise-reduction-and-sharpening.patch new file mode 100644 index 0000000000..c6e25c4116 --- /dev/null +++ b/multimedia/libcamera/patches/238-ipa-rkisp2-add-the-spatial-noise-reduction-and-sharpening.patch @@ -0,0 +1,1473 @@ +From d52cf1c86a6d3834cb9001a43663cc6ace03986d Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 10:41:06 +0000 +Subject: [PATCH 238/239] ipa: rkisp2: Add the spatial noise reduction and + sharpening + +The ISPP NR and SHP stages run on whatever the static Ispp parameters +hold, which for every tuning so far is nothing: both stages process +each frame with all-zero parameters. + +Add a NoiseReduction algorithm for the luma wavelet (YNR) and chroma +(UVNR) filters of the NR block and a Sharpen algorithm for the +sharpening and edge filters of the SHP block. Both interpolate a per +ISO step calibration at the gain of the latest ISP frame and convert +it the way the Rockchip AIQ does, at unity strength, so that the IQ +calibration of a sensor applies as is. NoiseReduction follows the +gain ratio and gain map use of TemporalNoiseReduction. The NR and SHP +blocks move out of the static Ispp parameters. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/algorithms/ispp.cpp | 126 +----- + src/ipa/rkisp2/algorithms/ispp.h | 2 - + src/ipa/rkisp2/algorithms/meson.build | 2 + + src/ipa/rkisp2/algorithms/nr.cpp | 552 ++++++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/nr.h | 104 +++++ + src/ipa/rkisp2/algorithms/sharpen.cpp | 484 ++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/sharpen.h | 104 +++++ + 7 files changed, 1254 insertions(+), 120 deletions(-) + create mode 100644 src/ipa/rkisp2/algorithms/nr.cpp + create mode 100644 src/ipa/rkisp2/algorithms/nr.h + create mode 100644 src/ipa/rkisp2/algorithms/sharpen.cpp + create mode 100644 src/ipa/rkisp2/algorithms/sharpen.h + +--- a/src/ipa/rkisp2/algorithms/ispp.cpp ++++ b/src/ipa/rkisp2/algorithms/ispp.cpp +@@ -32,22 +32,18 @@ namespace ipa::rkisp2::algorithms { + * \class Ispp + * \brief RkISPP module parameters from the tuning file + * +- * The ISPP post-processor (noise reduction, sharpening, FEC lens distortion +- * correction and ORB feature detection) takes its parameters through its own +- * params node; the temporal noise reduction has an algorithm of its own. This +- * algorithm sends the modules configured in the tuning file with every frame, +- * each tuning dictionary filling the fields of the corresponding UAPI block by +- * name: ++ * The ISPP post-processor (FEC lens distortion correction and ORB feature ++ * detection) takes its parameters through its own params node; the temporal ++ * and spatial noise reduction and the sharpening have algorithms of their ++ * own. This algorithm sends the modules configured in the tuning file with ++ * every frame, each tuning dictionary filling the fields of the corresponding ++ * UAPI block by name: + * + * \code{.unparsed} + * algorithms: + * - Ispp: +- * Shp: +- * tile_xcnt: 18 +- * tile_ycnt: 121 +- * pbf_k: [ 2, 1, 0 ] +- * Nr: +- * uvnr_step1_en: 1 ++ * Fec: ++ * crop_en: 0 + * \endcode + * + * A module absent from the tuning file is not sent and keeps the driver's +@@ -90,100 +86,6 @@ constexpr bool fieldSigned() + fieldSize(), \ + fieldSigned() } + +-constexpr Field nrFields[] = { +- ISPP_FIELD(rkispp_params_nr, uvnr_step1_en), +- ISPP_FIELD(rkispp_params_nr, uvnr_step2_en), +- ISPP_FIELD(rkispp_params_nr, nr_gain_en), +- ISPP_FIELD(rkispp_params_nr, uvnr_sd32_self_en), +- ISPP_FIELD(rkispp_params_nr, uvnr_nobig_en), +- ISPP_FIELD(rkispp_params_nr, uvnr_big_en), +- ISPP_FIELD(rkispp_params_nr, uvnr_gain_1sigma), +- ISPP_FIELD(rkispp_params_nr, uvnr_gain_offset), +- ISPP_FIELD(rkispp_params_nr, uvnr_gain_t2gen), +- ISPP_FIELD(rkispp_params_nr, uvnr_gain_iso), +- ISPP_FIELD(rkispp_params_nr, uvnr_t1gen_m3alpha), +- ISPP_FIELD(rkispp_params_nr, uvnr_t1flt_mode), +- ISPP_FIELD(rkispp_params_nr, uvnr_t1flt_wtp), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2gen_m3alpha), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2gen_wtp), +- ISPP_FIELD(rkispp_params_nr, uvnr_gain_uvgain), +- ISPP_FIELD(rkispp_params_nr, uvnr_t1flt_wtq), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2gen_wtq), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2flt_wtp), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2flt_wt), +- ISPP_FIELD(rkispp_params_nr, ynr_sgm_dx), +- ISPP_FIELD(rkispp_params_nr, ynr_lci), +- ISPP_FIELD(rkispp_params_nr, ynr_lgain_min), +- ISPP_FIELD(rkispp_params_nr, ynr_lgain_max), +- ISPP_FIELD(rkispp_params_nr, ynr_lmerge_bound), +- ISPP_FIELD(rkispp_params_nr, ynr_lmerge_ratio), +- ISPP_FIELD(rkispp_params_nr, ynr_lweit_flt), +- ISPP_FIELD(rkispp_params_nr, ynr_hlci), +- ISPP_FIELD(rkispp_params_nr, ynr_lhci), +- ISPP_FIELD(rkispp_params_nr, ynr_hhci), +- ISPP_FIELD(rkispp_params_nr, ynr_hgain_sgm), +- ISPP_FIELD(rkispp_params_nr, ynr_hweit_d), +- ISPP_FIELD(rkispp_params_nr, ynr_hgrad_y), +- ISPP_FIELD(rkispp_params_nr, ynr_hmax_adjust), +- ISPP_FIELD(rkispp_params_nr, ynr_hstrength), +- ISPP_FIELD(rkispp_params_nr, ynr_lweit_cmp), +- ISPP_FIELD(rkispp_params_nr, ynr_lmaxgain_lv4), +- ISPP_FIELD(rkispp_params_nr, uvnr_t1flt_msigma), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2gen_msigma), +- ISPP_FIELD(rkispp_params_nr, uvnr_t2flt_msigma), +- ISPP_FIELD(rkispp_params_nr, ynr_lsgm_y), +- ISPP_FIELD(rkispp_params_nr, ynr_hsgm_y), +- ISPP_FIELD(rkispp_params_nr, ynr_hweit), +- ISPP_FIELD(rkispp_params_nr, ynr_hstv_y), +- ISPP_FIELD(rkispp_params_nr, ynr_st_scale), +-}; +- +-constexpr Field shpFields[] = { +- ISPP_FIELD(rkispp_params_shp, rotation), +- ISPP_FIELD(rkispp_params_shp, scl_down_v), +- ISPP_FIELD(rkispp_params_shp, scl_down_h), +- ISPP_FIELD(rkispp_params_shp, tile_ycnt), +- ISPP_FIELD(rkispp_params_shp, tile_xcnt), +- ISPP_FIELD(rkispp_params_shp, alpha_adp_en), +- ISPP_FIELD(rkispp_params_shp, yin_flt_en), +- ISPP_FIELD(rkispp_params_shp, edge_avg_en), +- ISPP_FIELD(rkispp_params_shp, ehf_th), +- ISPP_FIELD(rkispp_params_shp, pbf_ratio), +- ISPP_FIELD(rkispp_params_shp, edge_thed), +- ISPP_FIELD(rkispp_params_shp, dir_min), +- ISPP_FIELD(rkispp_params_shp, pbf_shf_bits), +- ISPP_FIELD(rkispp_params_shp, mbf_shf_bits), +- ISPP_FIELD(rkispp_params_shp, hbf_shf_bits), +- ISPP_FIELD(rkispp_params_shp, m_ratio), +- ISPP_FIELD(rkispp_params_shp, h_ratio), +- ISPP_FIELD(rkispp_params_shp, pbf_k), +- ISPP_FIELD(rkispp_params_shp, mrf_k), +- ISPP_FIELD(rkispp_params_shp, mbf_k), +- ISPP_FIELD(rkispp_params_shp, hrf_k), +- ISPP_FIELD(rkispp_params_shp, hbf_k), +- ISPP_FIELD(rkispp_params_shp, eg_coef), +- ISPP_FIELD(rkispp_params_shp, eg_smoth), +- ISPP_FIELD(rkispp_params_shp, eg_gaus), +- ISPP_FIELD(rkispp_params_shp, dog_k), +- ISPP_FIELD(rkispp_params_shp, lum_point), +- ISPP_FIELD(rkispp_params_shp, pbf_sigma), +- ISPP_FIELD(rkispp_params_shp, lum_clp_m), +- ISPP_FIELD(rkispp_params_shp, lum_min_m), +- ISPP_FIELD(rkispp_params_shp, mbf_sigma), +- ISPP_FIELD(rkispp_params_shp, lum_clp_h), +- ISPP_FIELD(rkispp_params_shp, hbf_sigma), +- ISPP_FIELD(rkispp_params_shp, edge_lum_thed), +- ISPP_FIELD(rkispp_params_shp, clamp_pos), +- ISPP_FIELD(rkispp_params_shp, clamp_neg), +- ISPP_FIELD(rkispp_params_shp, detail_alpha), +- ISPP_FIELD(rkispp_params_shp, hbf_ratio), +- ISPP_FIELD(rkispp_params_shp, smoth_th4), +- ISPP_FIELD(rkispp_params_shp, l_alpha), +- ISPP_FIELD(rkispp_params_shp, g_alpha), +- ISPP_FIELD(rkispp_params_shp, rfl_ratio), +- ISPP_FIELD(rkispp_params_shp, rfh_ratio), +-}; +- + constexpr Field fecFields[] = { + ISPP_FIELD(rkispp_params_fec, crop_en), + ISPP_FIELD(rkispp_params_fec, crop_width), +@@ -312,14 +214,6 @@ int Ispp::init([[maybe_unused]] IPAConte + { + int ret; + +- ret = loadBlock(tuningData, "Nr", nrFields, nr_); +- if (ret) +- return ret; +- +- ret = loadBlock(tuningData, "Shp", shpFields, shp_); +- if (ret) +- return ret; +- + ret = loadBlock(tuningData, "Fec", fecFields, fec_); + if (ret) + return ret; +@@ -337,10 +231,6 @@ void Ispp::prepareIspp([[maybe_unused]] + [[maybe_unused]] const uint32_t frame, + RkISPPParams *params) + { +- if (nr_) +- sendBlock(params->block(), *nr_); +- if (shp_) +- sendBlock(params->block(), *shp_); + if (fec_) + sendBlock(params->block(), *fec_); + if (orb_) +--- a/src/ipa/rkisp2/algorithms/ispp.h ++++ b/src/ipa/rkisp2/algorithms/ispp.h +@@ -28,8 +28,6 @@ public: + RkISPPParams *params) override; + + private: +- std::optional nr_; +- std::optional shp_; + std::optional fec_; + std::optional orb_; + }; +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -13,6 +13,8 @@ rkisp2_ipa_algorithms = files([ + 'goc.cpp', + 'ispp.cpp', + 'lsc.cpp', ++ 'nr.cpp', ++ 'sharpen.cpp', + 'tnr.cpp', + 'wdr.cpp', + ]) +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/nr.cpp +@@ -0,0 +1,552 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 spatial noise reduction ++ */ ++ ++#include "nr.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++#include ++ ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++#include "calibration.h" ++ ++/** ++ * \file nr.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class NoiseReduction ++ * \brief RkISP2 spatial noise reduction of the ISPP ++ * ++ * The ISPP NR stage denoises the luma with a four level wavelet filter (YNR) ++ * and the chroma with median and bilateral filters on two downscaled copies ++ * (UVNR), both steered by a luminance dependent noise sigma curve and by the ++ * ISP gain map of the frame. ++ * ++ * The calibration is per ISO step, ISO 50 being unity gain. For each frame ++ * the algorithm interpolates it at the gain of the latest ISP frame and ++ * converts it to the NR block following the Rockchip AIQ ++ * (select_ynr_params_by_ISO(), ynr_fix_transfer(), ++ * select_uvnr_params_by_ISO() and uvnr_fix_transfer() of ++ * camera_engine_rkaiq) at unity strength, so that its IQ calibration can be ++ * used as is. The calibration is converted from an IQ file with ++ * nr-iq2yaml.py. ++ * ++ * \code{.unparsed} ++ * algorithms: ++ * - NoiseReduction: ++ * luma: ++ * - iso: 50 ++ * sigma-curve: [ ... ] ++ * ... ++ * chroma: ++ * kernel-3x3: [ ... ] ++ * ... ++ * settings: ++ * - iso: 50 ++ * step0-uvgrad-ratio: 40 ++ * ... ++ * \endcode ++ * ++ * The gain ratio and the use of the gain map come from the ++ * TemporalNoiseReduction algorithm, which programs the gain map; without it ++ * the NR runs on the global gain. ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2Nr) ++ ++namespace { ++ ++using calibration::lerp; ++using calibration::readArray; ++using calibration::readScalar; ++using calibration::roundF; ++ ++/* calibration and processing bit depths of the YNR sigma curve */ ++constexpr unsigned int kCalBits = 12; ++constexpr unsigned int kSigmaBits = 10; ++constexpr unsigned int kCurveStepBits = 4; ++constexpr double kSigmaDiv = 1 << (kCalBits - kSigmaBits); ++constexpr double kLumaPointScale = 1 << (kSigmaBits - 8); ++ ++/* fractional bits of the YNR register fields, from the AIQ */ ++constexpr int kFracCi = 5; ++constexpr int kFracSigma = 5; ++constexpr int kFracWeight = 7; ++constexpr int kFracBfScale = 4; ++constexpr int kFracSoftness = 7; ++constexpr int kFracGrad = 4; ++constexpr int kFracStrength = 4; ++constexpr int kFracSoftThreshold = 8; ++constexpr int kFracExpLut = 7; ++constexpr int kFracLv4Gain = 4; ++constexpr long kSigmaMax = (1 << (kFracSigma + 9)) - 1; ++ ++/* ynr_fix_transfer(): fixed low frequency weight comparison */ ++constexpr double kWeightCompare = 0.1; ++ ++/* squared distances of the five YNR high frequency edge weight taps */ ++constexpr std::array kEdgeDist2 = { 1, 2, 4, 5, 8 }; ++ ++/* fractional bits of the UVNR register fields, from the AIQ */ ++constexpr int kFracUvOffset = 4; ++constexpr int kFracUvGain = 4; ++constexpr int kFracUvMedian = 4; ++constexpr int kFracUvKernel = 7; ++constexpr int kFracUvBfRatio = 7; ++constexpr int kFracUvGainIso = 7; ++constexpr long kUvGainMax = 0x7f; ++constexpr long kUvGainIsoMin = 0x8; ++constexpr long kUvGainIsoMax = 0x80; ++constexpr long kUvSigmaMax = 8191; ++ ++/* uvnr_fix_transfer(): log2(e) scaling of the inverse bilateral sigma */ ++constexpr double kUvLog2e = 0.8493; ++constexpr int kUvLog2eBits = 6; ++constexpr int kUvImgBits = 8; ++ ++long clampMax(long v, long max) ++{ ++ return std::min(v, max); ++} ++ ++double lumaPoint(unsigned int i) ++{ ++ return i == NoiseReduction::kCurvePoints - 1 ++ ? 1 << kSigmaBits ++ : i << (kSigmaBits - kCurveStepBits); ++} ++ ++/* The luma curve ratio at \a x, linear between the curve points */ ++double curveRate(double x, const NoiseReduction::Curve &pts, ++ const NoiseReduction::Curve &ratios) ++{ ++ unsigned int j = 0; ++ double t; ++ ++ while (j < pts.size() && x > pts[j]) ++ j++; ++ ++ if (j == 0) ++ return ratios[0]; ++ if (j >= pts.size()) ++ return ratios.back(); ++ ++ t = (x - pts[j - 1]) / (pts[j] - pts[j - 1]); ++ return ratios[j - 1] + t * (ratios[j] - ratios[j - 1]); ++} ++ ++unsigned int topOnePos(unsigned int v) ++{ ++ unsigned int pos = 0; ++ ++ while (v) { ++ pos++; ++ v >>= 1; ++ } ++ ++ return pos; ++} ++ ++} /* namespace */ ++ ++int NoiseReduction::parseLuma(const ValueNode &tuningData) ++{ ++ if (!tuningData["luma"].isList()) { ++ LOG(RkISP2Nr, Error) << "No luma settings"; ++ return -EINVAL; ++ } ++ ++ for (const auto &node : tuningData["luma"].asList()) { ++ std::array curve; ++ bool ok = true; ++ Luma s; ++ ++ ok &= readScalar(node, "iso", s.iso); ++ ok &= readArray(node, "sigma-curve", curve); ++ ok &= readArray(node, "ynr-lci", s.lci); ++ ok &= readArray(node, "ynr-lhci", s.lhci); ++ ok &= readArray(node, "ynr-hlci", s.hlci); ++ ok &= readArray(node, "ynr-hhci", s.hhci); ++ ok &= readArray(node, "lo-lumapoint", s.loPoint); ++ ok &= readArray(node, "lo-lumaratio", s.loRatio); ++ ok &= readScalar(node, "lo-directionstrength", s.loDirection); ++ ok &= readArray(node, "lo-bfscale", s.loBfScale); ++ ok &= readScalar(node, "imerge-ratio", s.mergeRatio); ++ ok &= readArray(node, "denoise-weight", s.loWeight); ++ ok &= readArray(node, "hi-lumapoint", s.hiPoint); ++ ok &= readArray(node, "hi-lumaratio", s.hiRatio); ++ ok &= readArray(node, "hi-bfscale", s.hiBfScale); ++ ok &= readArray(node, "hwith-d", s.hiSoftness); ++ ok &= readScalar(node, "hi-denoisestrength", s.hiStrength); ++ ok &= readScalar(node, "hi-detailminadjdnw", s.minAdjust); ++ ok &= readArray(node, "hi-denoiseweight", s.hiWeight); ++ ok &= readArray(node, "hgrad-y-level1", s.grad[0]); ++ ok &= readArray(node, "hgrad-y-level2", s.grad[1]); ++ ok &= readArray(node, "hgrad-y-level3", s.grad[2]); ++ ok &= readArray(node, "hgrad-y-level4", s.grad[3]); ++ ok &= readArray(node, "hi-soft-thresh-scale", s.softThreshold); ++ if (!ok) ++ return -EINVAL; ++ ++ /* the sigma polynomial is calibrated over 12-bit luma */ ++ for (unsigned int i = 0; i < kCurvePoints; i++) { ++ double x = lumaPoint(i) * (1 << (kCalBits - kSigmaBits)); ++ double y = curve[4]; ++ double xp = 1.0; ++ ++ for (unsigned int o = 1; o <= 4; o++) { ++ xp *= x; ++ y += xp * curve[4 - o]; ++ } ++ s.sigma[i] = std::max(y, 0.0); ++ } ++ ++ if (!luma_.empty() && s.iso <= luma_.back().iso) { ++ LOG(RkISP2Nr, Error) << "Luma settings not sorted by iso"; ++ return -EINVAL; ++ } ++ lumaIsos_.push_back(s.iso); ++ luma_.push_back(s); ++ } ++ ++ if (luma_.size() < 2) { ++ LOG(RkISP2Nr, Error) << "Need two luma settings at least"; ++ return -EINVAL; ++ } ++ ++ return 0; ++} ++ ++int NoiseReduction::parseChroma(const ValueNode &tuningData) ++{ ++ const ValueNode &chroma = tuningData["chroma"]; ++ bool ok = true; ++ ++ ok &= readArray(chroma, "kernel-3x3", kernel3_); ++ ok &= readArray(chroma, "kernel-5x5", kernel5_); ++ ok &= readArray(chroma, "kernel-9x9", kernel9_); ++ ok &= readScalar(chroma, "kernel-9x9-num", kernel9Mode_); ++ if (!ok) ++ return -EINVAL; ++ ++ if (!chroma["settings"].isList()) { ++ LOG(RkISP2Nr, Error) << "No chroma settings"; ++ return -EINVAL; ++ } ++ ++ for (const auto &node : chroma["settings"].asList()) { ++ Chroma s; ++ ++ ok &= readScalar(node, "iso", s.iso); ++ ok &= readScalar(node, "step0-uvgrad-ratio", s.gradRatio); ++ ok &= readScalar(node, "step0-uvgrad-offset", s.gradOffset); ++ ok &= readScalar(node, "step1-median-ratio", s.median1); ++ ok &= readScalar(node, "step2-median-ratio", s.median2); ++ ok &= readScalar(node, "step1-bf-sigmar", s.sigmaR1); ++ ok &= readScalar(node, "step2-bf-sigmar", s.sigmaR2); ++ ok &= readScalar(node, "step3-bf-sigmar", s.sigmaR3); ++ ok &= readScalar(node, "step1-bf-uvgain", s.uvgain1); ++ ok &= readScalar(node, "step2-bf-uvgain", s.uvgain2); ++ ok &= readScalar(node, "step3-bf-uvgain", s.uvgain3); ++ ok &= readScalar(node, "step1-bf-ratio", s.bfRatio1); ++ ok &= readScalar(node, "step3-bf-ratio", s.bfRatio3); ++ if (!ok) ++ return -EINVAL; ++ ++ if (s.sigmaR1 <= 0 || s.sigmaR2 <= 0 || s.sigmaR3 <= 0) { ++ LOG(RkISP2Nr, Error) << "Chroma sigmas must be positive"; ++ return -EINVAL; ++ } ++ ++ if (!chroma_.empty() && s.iso <= chroma_.back().iso) { ++ LOG(RkISP2Nr, Error) << "Chroma settings not sorted by iso"; ++ return -EINVAL; ++ } ++ chromaIsos_.push_back(s.iso); ++ chroma_.push_back(s); ++ } ++ ++ if (chroma_.size() < 2) { ++ LOG(RkISP2Nr, Error) << "Need two chroma settings at least"; ++ return -EINVAL; ++ } ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int NoiseReduction::init([[maybe_unused]] IPAContext &context, ++ const ValueNode &tuningData) ++{ ++ int ret; ++ ++ ret = parseLuma(tuningData); ++ if (ret) ++ return ret; ++ ++ return parseChroma(tuningData); ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::configure ++ */ ++int NoiseReduction::configure([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const IPACameraSensorInfo &configInfo) ++{ ++ nr_.reset(); ++ ++ return 0; ++} ++ ++/** ++ * \brief Convert the luma calibration at an ISO to the YNR fields ++ * \param[in] iso The ISO, 50 times the gain ++ * \param[in] ratio The gain ratio of the gain map ++ * \param[out] nr The NR block ++ * ++ * Port of the AIQ select_ynr_params_by_ISO() and ynr_fix_transfer(). ++ */ ++void NoiseReduction::fixLuma(double iso, double ratio, ++ struct rkispp_params_nr *nr) const ++{ ++ calibration::IsoInterval in = calibration::isoInterval(lumaIsos_, iso); ++ const Luma &L = luma_[in.lo]; ++ const Luma &H = luma_[in.hi]; ++ double div = kSigmaDiv * std::sqrt(ratio); ++ std::array sigma; ++ double r = in.r; ++ double gf; ++ ++ Lvl lci = calibration::lerp(r, L.lci, H.lci); ++ Lvl lhci = calibration::lerp(r, L.lhci, H.lhci); ++ Lvl hlci = calibration::lerp(r, L.hlci, H.hlci); ++ Lvl hhci = calibration::lerp(r, L.hhci, H.hhci); ++ Lvl loBf = calibration::lerp(r, L.loBfScale, H.loBfScale); ++ Lvl loW = calibration::lerp(r, L.loWeight, H.loWeight); ++ Lvl hiBf = calibration::lerp(r, L.hiBfScale, H.hiBfScale); ++ Lvl hiSoft = calibration::lerp(r, L.hiSoftness, H.hiSoftness); ++ Lvl hiW = calibration::lerp(r, L.hiWeight, H.hiWeight); ++ Lvl sts = calibration::lerp(r, L.softThreshold, H.softThreshold); ++ Curve loPt = calibration::lerp(r, L.loPoint, H.loPoint); ++ Curve loRatio = calibration::lerp(r, L.loRatio, H.loRatio); ++ Curve hiPt = calibration::lerp(r, L.hiPoint, H.hiPoint); ++ Curve hiRatio = calibration::lerp(r, L.hiRatio, H.hiRatio); ++ double mergeRatio = lerp(r, L.mergeRatio, H.mergeRatio); ++ double mergeBound = 1.0 / mergeRatio / 2; ++ double direction = lerp(r, L.loDirection, H.loDirection); ++ double hiStrength = lerp(r, L.hiStrength, H.hiStrength); ++ double minAdjust = lerp(r, L.minAdjust, H.minAdjust); ++ ++ for (double &v : loPt) ++ v *= kLumaPointScale; ++ for (double &v : hiPt) ++ v *= kLumaPointScale; ++ ++ /* the AIQ halves the noise ci where the sigma gain drops below 1/2 */ ++ gf = std::sqrt(calibration::kIsoUnity / iso); ++ if (gf < 0.5) { ++ for (Lvl *ci : { &lci, &lhci, &hlci, &hhci }) ++ for (double &v : *ci) ++ v /= 2; ++ gf *= 2; ++ } ++ for (unsigned int i = 0; i < kCurvePoints; i++) ++ sigma[i] = lerp(r, L.sigma[i], H.sigma[i]) * gf; ++ ++ for (unsigned int i = 0; i < kCurvePoints - 1; i++) ++ nr->ynr_sgm_dx[i] = topOnePos(lumaPoint(i + 1) - lumaPoint(i)) - 2; ++ ++ for (unsigned int i = 0; i < kCurvePoints; i++) { ++ long base = static_cast(sigma[i] * (1 << kFracSigma)); ++ long lo = static_cast(base * curveRate(lumaPoint(i), loPt, loRatio)); ++ long hi = static_cast(base * curveRate(lumaPoint(i), hiPt, hiRatio)); ++ ++ nr->ynr_lsgm_y[i] = std::min(lo / div, kSigmaMax); ++ nr->ynr_hsgm_y[i] = std::min(hi / div, kSigmaMax); ++ nr->ynr_hstv_y[i] = std::min(base / div, UINT16_MAX); ++ } ++ ++ for (unsigned int i = 0; i < kLevels; i++) { ++ nr->ynr_lci[i] = clampMax(lci[i] * (1 << kFracCi), 0xff); ++ nr->ynr_lhci[i] = clampMax(lhci[i] * (1 << kFracCi), 0xff); ++ nr->ynr_hlci[i] = clampMax(hlci[i] * (1 << kFracCi), 0xff); ++ nr->ynr_hhci[i] = clampMax(hhci[i] * (1 << kFracCi), 0xff); ++ nr->ynr_lgain_min[i] = clampMax(loBf[i] * (1 << kFracBfScale), 0xff); ++ nr->ynr_hgain_sgm[i] = clampMax(hiBf[i] * (1 << kFracBfScale), 0xff); ++ nr->ynr_lweit_flt[i] = std::min( ++ static_cast(loW[i] * (1 << kFracWeight)), 0x80); ++ nr->ynr_hweit[i] = std::min( ++ static_cast(hiW[i] * (1 << kFracWeight)), 0x1ff); ++ } ++ ++ nr->ynr_lgain_max = static_cast(direction * (1 << kFracBfScale)); ++ nr->ynr_lmerge_bound = static_cast(mergeBound * (1 << kFracStrength)); ++ nr->ynr_lmerge_ratio = static_cast(mergeRatio * (1 << kFracStrength)); ++ ++ for (unsigned int i = 0; i < kLevels; i++) { ++ long e = static_cast(hiSoft[i] * (1 << kFracSoftness)); ++ ++ for (unsigned int k = 0; k < kEdgeDist2.size(); k++) { ++ double d = kEdgeDist2[k] * (1 << (2 * kFracSoftness)); ++ ++ nr->ynr_hweit_d[k * kLevels + i] = static_cast( ++ std::exp(-d / (2.0 * e * e)) * (1 << kFracExpLut)); ++ } ++ } ++ ++ for (unsigned int i = 0; i < kLumaPoints; i++) ++ for (unsigned int l = 0; l < kLevels; l++) ++ nr->ynr_hgrad_y[i * kLevels + l] = static_cast( ++ lerp(r, L.grad[l][i], H.grad[l][i]) * (1 << kFracGrad)); ++ ++ nr->ynr_hmax_adjust = static_cast(minAdjust * (1 << kFracGrad)); ++ nr->ynr_hstrength = clampMax(hiStrength * (1 << kFracStrength), 0xff); ++ nr->ynr_lweit_cmp[0] = roundF(kWeightCompare * (1 << kFracExpLut)); ++ nr->ynr_lweit_cmp[1] = nr->ynr_lweit_cmp[0]; ++ nr->ynr_lmaxgain_lv4 = clampMax( ++ roundF(std::sqrt(iso / calibration::kIsoUnity) * (1 << kFracLv4Gain)), ++ 0xff); ++ ++ for (unsigned int i = 0; i < RKISPP_NR_YNR_ST_SCALE_SIZE; i++) ++ nr->ynr_st_scale[i] = ++ static_cast(sts[i] * (1 << kFracSoftThreshold)); ++} ++ ++/** ++ * \brief Convert the chroma calibration at an ISO to the UVNR fields ++ * \param[in] iso The ISO, 50 times the gain ++ * \param[in] ratio The gain ratio of the gain map ++ * \param[in] localGain True if the NR takes the gain from the gain map ++ * \param[out] nr The NR block ++ * ++ * Port of the AIQ select_uvnr_params_by_ISO() and uvnr_fix_transfer(). ++ */ ++void NoiseReduction::fixChroma(double iso, double ratio, bool localGain, ++ struct rkispp_params_nr *nr) const ++{ ++ calibration::IsoInterval in = calibration::isoInterval(chromaIsos_, iso); ++ const Chroma &L = chroma_[in.lo]; ++ const Chroma &H = chroma_[in.hi]; ++ double log2e = static_cast(kUvLog2e * (1 << kUvLog2eBits)) * ++ (1 << kUvImgBits); ++ double r = in.r; ++ ++ auto li = [&](double Chroma::*m) { return lerp(r, L.*m, H.*m); }; ++ auto msigma = [&](double sigma) { ++ return clampMax(static_cast(log2e / sigma), kUvSigmaMax); ++ }; ++ auto kernel = [](double v) { ++ return static_cast(v * (1 << kFracUvKernel)); ++ }; ++ ++ nr->uvnr_step1_en = 1; ++ nr->uvnr_step2_en = 1; ++ nr->nr_gain_en = !localGain; ++ nr->uvnr_nobig_en = 0; ++ nr->uvnr_big_en = 0; ++ nr->uvnr_sd32_self_en = 1; ++ ++ nr->uvnr_gain_1sigma = static_cast(li(&Chroma::gradRatio)); ++ nr->uvnr_gain_offset = ++ static_cast(li(&Chroma::gradOffset) * (1 << kFracUvOffset)); ++ nr->uvnr_gain_uvgain[0] = clampMax( ++ static_cast(li(&Chroma::uvgain1) * (1 << kFracUvGain)), ++ kUvGainMax); ++ nr->uvnr_gain_uvgain[1] = clampMax( ++ static_cast(li(&Chroma::uvgain3) * (1 << kFracUvGain)), ++ kUvGainMax); ++ nr->uvnr_gain_t2gen = clampMax( ++ static_cast(li(&Chroma::uvgain2) * (1 << kFracUvGain)), ++ kUvGainMax); ++ nr->uvnr_gain_iso = std::clamp( ++ std::sqrt(calibration::kIsoUnity / (iso * ratio)) * ++ (1 << kFracUvGainIso), ++ kUvGainIsoMin, kUvGainIsoMax); ++ ++ nr->uvnr_t1gen_m3alpha = ++ static_cast(li(&Chroma::median1) * (1 << kFracUvMedian)); ++ nr->uvnr_t1flt_mode = static_cast(kernel9Mode_); ++ nr->uvnr_t1flt_msigma = msigma(li(&Chroma::sigmaR1)); ++ nr->uvnr_t1flt_wtp = ++ static_cast(li(&Chroma::bfRatio1) * (1 << kFracUvBfRatio)); ++ for (unsigned int i = 0; i < kernel9_.size(); i++) ++ nr->uvnr_t1flt_wtq[i] = kernel(kernel9_[i]); ++ ++ nr->uvnr_t2gen_m3alpha = ++ static_cast(li(&Chroma::median2) * (1 << kFracUvMedian)); ++ nr->uvnr_t2gen_msigma = msigma(li(&Chroma::sigmaR2)); ++ nr->uvnr_t2gen_wtp = kernel(kernel5_[0]); ++ for (unsigned int i = 0; i < RKISPP_NR_UVNR_T2GEN_WTQ_SIZE; i++) ++ nr->uvnr_t2gen_wtq[i] = kernel(kernel5_[i + 1]); ++ ++ nr->uvnr_t2flt_msigma = msigma(li(&Chroma::sigmaR3)); ++ nr->uvnr_t2flt_wtp = ++ static_cast(li(&Chroma::bfRatio3) * (1 << kFracUvBfRatio)); ++ for (unsigned int i = 0; i < kernel3_.size(); i++) ++ nr->uvnr_t2flt_wt[i] = kernel(kernel3_[i]); ++} ++ ++/** ++ * \brief Send the NR block for the gain of the latest ISP frame ++ * \param[in] context The shared IPA context ++ * \param[in] frame The ISPP frame number ++ * \param[out] params The ISPP parameters buffer ++ */ ++void NoiseReduction::prepareIspp(IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ RkISPPParams *params) ++{ ++ constexpr size_t header = sizeof(struct v4l2_isp_params_block_header); ++ const auto &noiseGain = context.activeState.noiseGain; ++ double gain = noiseGain.gain > 0.0 ? noiseGain.gain : 1.0; ++ double ratio = noiseGain.ratio > 0.0 ? noiseGain.ratio : 1.0; ++ ++ if (!nr_ || ratio != nrRatio_ || ++ std::abs(gain - nrGain_) > calibration::kGainTolerance * nrGain_) { ++ struct rkispp_params_nr nr = {}; ++ double iso = gain * calibration::kIsoUnity; ++ ++ fixChroma(iso, ratio, noiseGain.localGain, &nr); ++ fixLuma(iso, ratio, &nr); ++ nr_ = nr; ++ nrGain_ = gain; ++ nrRatio_ = ratio; ++ ++ LOG(RkISP2Nr, Debug) ++ << "gain " << gain << " ratio " << ratio << " lsgm " ++ << nr.ynr_lsgm_y[8] << " uv gain iso " ++ << static_cast(nr.uvnr_gain_iso); ++ } ++ ++ auto block = params->block(); ++ memcpy(reinterpret_cast(&*block) + header, ++ reinterpret_cast(&*nr_) + header, ++ sizeof(*nr_) - header); ++ block.setEnabled(true); ++} ++ ++REGISTER_IPA_ALGORITHM(NoiseReduction, "NoiseReduction") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/nr.h +@@ -0,0 +1,104 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 spatial noise reduction ++ */ ++ ++#pragma once ++ ++#include ++#include ++#include ++ ++#include ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class NoiseReduction : public Algorithm ++{ ++public: ++ NoiseReduction() = default; ++ ~NoiseReduction() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ int configure(IPAContext &context, ++ const IPACameraSensorInfo &configInfo) override; ++ void prepareIspp(IPAContext &context, const uint32_t frame, ++ RkISPPParams *params) override; ++ ++ static constexpr unsigned int kLevels = 4; ++ static constexpr unsigned int kCurvePoints = 17; ++ static constexpr unsigned int kLumaPoints = 6; ++ ++ using Lvl = std::array; ++ using Curve = std::array; ++ ++ struct Luma { ++ double iso; ++ std::array sigma; ++ Lvl lci; ++ Lvl lhci; ++ Lvl hlci; ++ Lvl hhci; ++ Curve loPoint; ++ Curve loRatio; ++ double loDirection; ++ Lvl loBfScale; ++ double mergeRatio; ++ Lvl loWeight; ++ Curve hiPoint; ++ Curve hiRatio; ++ Lvl hiBfScale; ++ Lvl hiSoftness; ++ double hiStrength; ++ double minAdjust; ++ Lvl hiWeight; ++ std::array grad; ++ Lvl softThreshold; ++ }; ++ ++ struct Chroma { ++ double iso; ++ double gradRatio; ++ double gradOffset; ++ double median1; ++ double median2; ++ double sigmaR1; ++ double sigmaR2; ++ double sigmaR3; ++ double uvgain1; ++ double uvgain2; ++ double uvgain3; ++ double bfRatio1; ++ double bfRatio3; ++ }; ++ ++ void fixLuma(double iso, double ratio, struct rkispp_params_nr *nr) const; ++ void fixChroma(double iso, double ratio, bool localGain, ++ struct rkispp_params_nr *nr) const; ++ ++private: ++ int parseLuma(const ValueNode &tuningData); ++ int parseChroma(const ValueNode &tuningData); ++ ++ std::vector luma_; ++ std::vector lumaIsos_; ++ std::vector chroma_; ++ std::vector chromaIsos_; ++ std::array kernel3_; ++ std::array kernel5_; ++ std::array kernel9_; ++ double kernel9Mode_; ++ ++ std::optional nr_; ++ double nrGain_; ++ double nrRatio_; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/sharpen.cpp +@@ -0,0 +1,484 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 sharpening ++ */ ++ ++#include "sharpen.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++#include ++ ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++#include "calibration.h" ++ ++/** ++ * \file sharpen.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class Sharpen ++ * \brief RkISP2 sharpening of the ISPP ++ * ++ * The ISPP SHP stage sharpens the luma in a medium and a high frequency band, ++ * each through a bilateral filter with a luminance dependent sigma and ++ * clipping, and enhances edges with a difference of gaussians along the local ++ * edge direction. ++ * ++ * The calibration is per ISO step, ISO 50 being unity gain. For each frame ++ * the algorithm interpolates it at the gain of the latest ISP frame and ++ * converts it to the SHP block following the Rockchip AIQ ++ * (select_rk_sharpen_hw_params_by_ISO(), rk_Sharp_V1_fix_transfer(), ++ * select_edgefilter_params_by_ISO() and edgefilter_fix_transfer() of ++ * camera_engine_rkaiq) at unity strength, so that its IQ calibration can be ++ * used as is. The calibration is converted from an IQ file with ++ * nr-iq2yaml.py. ++ * ++ * \code{.unparsed} ++ * algorithms: ++ * - Sharpen: ++ * luma-point: [ 0, 16, 32, 64, 96, 160, 224, 256 ] ++ * pbf-coeff: [ ... ] ++ * ... ++ * sharp: ++ * - iso: 50 ++ * lratio: 0.2 ++ * ... ++ * edge: ++ * - iso: 50 ++ * edge-thed: 8 ++ * ... ++ * \endcode ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2Sharpen) ++ ++namespace { ++ ++using calibration::lerp; ++using calibration::readArray; ++using calibration::readScalar; ++using calibration::roundF; ++ ++/* fractional bits of the SHP register fields, from the AIQ */ ++constexpr int kFracLratio = 8; ++constexpr int kFracHratio = 8; ++constexpr int kFracMHRatio = 2; ++constexpr int kFracPbfRatio = 7; ++constexpr int kFracLumMinM = 4; ++constexpr int kFracHbfRatio = 8; ++constexpr int kFracPbfCoeff = 6; ++constexpr int kFracMrfCoeff = 7; ++constexpr int kFracMbfCoeff = 6; ++constexpr int kFracHrfCoeff = 7; ++constexpr int kFracHbfCoeff = 6; ++constexpr int kFracDirMin = 6; ++constexpr int kFracAlpha = 8; ++constexpr int kFracDetailAlpha = 6; ++constexpr int kFracSmoothCoeff = 6; ++constexpr int kFracGausCoeff = 6; ++constexpr int kFracGuideCoeff = 4; ++constexpr int kFracDogCoeff = 6; ++constexpr int kFracSmooth4 = 8; ++ ++/* rk_Sharp_V1_fix_transfer(): inverse sigma tables and their shift */ ++constexpr int kSigmaDeciBits = 7; ++constexpr int kSigmaShiftOffset = 5; ++constexpr double kMaxLumaClip = 8.0; ++constexpr double kMaxFilterGain = 2.0; ++constexpr double kMaxFilterAdd = 255.0; ++constexpr long kLumMinMMin = -32; ++ ++/* coefficient taps of each kernel register, in the AIQ order */ ++constexpr std::array kTaps3 = { 4, 1, 0 }; ++constexpr std::array kTaps5 = { 12, 7, 6, 2, 1, 0 }; ++constexpr std::array kTapsMbf = { ++ 110, 93, 92, 76, 58, 56, 25, 23, 38, 4, 69, 102 ++}; ++constexpr std::array kTapsSmooth = { 12, 11, 10 }; ++ ++/* row 2 of the 5x5 directional smoothing kernel */ ++constexpr unsigned int kSmoothRow = 2 * 5; ++ ++/* ++ * Quantise the register taps of a kernel; the first takes up the rounding of ++ * the whole kernel so that the fixed point sum matches the real one. ++ */ ++template ++std::array kernelFix(const std::array &coef, ++ const std::array &taps, int bits) ++{ ++ std::array out; ++ double sum = 0.0; ++ long fixSum = 0; ++ ++ for (unsigned int t = 0; t < T; t++) ++ out[t] = roundF(coef[taps[t]] * (1 << bits)); ++ ++ for (double c : coef) { ++ sum += c; ++ fixSum += roundF(c * (1 << bits)); ++ } ++ ++ out[0] += static_cast(sum * (1 << bits)) - fixSum; ++ ++ return out; ++} ++ ++/* The inverse sigma table of a bilateral filter and its shift */ ++uint8_t sigmaTable(const Sharpen::Luma &lumaSigma, double gain, double add, ++ uint8_t *table) ++{ ++ long minVal = LONG_MAX; ++ int bits; ++ ++ for (double ls : lumaSigma) ++ minVal = std::min(minVal, static_cast(std::floor(ls * gain + add))); ++ ++ bits = kSigmaDeciBits + ++ static_cast(std::floor(std::log2(std::max(minVal, 1L)))); ++ ++ for (unsigned int i = 0; i < lumaSigma.size(); i++) ++ table[i] = static_cast( ++ roundF(1.0 / (lumaSigma[i] * gain + add) * (1 << bits))); ++ ++ return bits - kSigmaShiftOffset; ++} ++ ++} /* namespace */ ++ ++int Sharpen::parseSharp(const ValueNode &tuningData) ++{ ++ if (!tuningData["sharp"].isList()) { ++ LOG(RkISP2Sharpen, Error) << "No sharp settings"; ++ return -EINVAL; ++ } ++ ++ for (const auto &node : tuningData["sharp"].asList()) { ++ bool ok = true; ++ SharpSetting s; ++ ++ ok &= readScalar(node, "iso", s.iso); ++ ok &= readScalar(node, "lratio", s.lratio); ++ ok &= readScalar(node, "hratio", s.hratio); ++ ok &= readScalar(node, "mf-sharp-ratio", s.mRatio); ++ ok &= readScalar(node, "hf-sharp-ratio", s.hRatio); ++ ok &= readArray(node, "luma-sigma", s.lumaSigma); ++ ok &= readScalar(node, "pbf-gain", s.pbfGain); ++ ok &= readScalar(node, "pbf-ratio", s.pbfRatio); ++ ok &= readScalar(node, "pbf-add", s.pbfAdd); ++ ok &= readArray(node, "mf-clip-pos", s.clipPosM); ++ ok &= readArray(node, "mf-clip-neg", s.clipNegM); ++ ok &= readArray(node, "hf-clip", s.clipH); ++ ok &= readScalar(node, "mbf-gain", s.mbfGain); ++ ok &= readScalar(node, "hbf-gain", s.hbfGain); ++ ok &= readScalar(node, "hbf-ratio", s.hbfRatio); ++ ok &= readScalar(node, "mbf-add", s.mbfAdd); ++ ok &= readScalar(node, "hbf-add", s.hbfAdd); ++ ok &= readScalar(node, "local-sharp-strength", s.localStrength); ++ if (!ok) ++ return -EINVAL; ++ ++ if (!sharp_.empty() && s.iso <= sharp_.back().iso) { ++ LOG(RkISP2Sharpen, Error) << "Sharp settings not sorted by iso"; ++ return -EINVAL; ++ } ++ sharpIsos_.push_back(s.iso); ++ sharp_.push_back(s); ++ } ++ ++ if (sharp_.size() < 2) { ++ LOG(RkISP2Sharpen, Error) << "Need two sharp settings at least"; ++ return -EINVAL; ++ } ++ ++ return 0; ++} ++ ++int Sharpen::parseEdge(const ValueNode &tuningData) ++{ ++ if (!tuningData["edge"].isList()) { ++ LOG(RkISP2Sharpen, Error) << "No edge settings"; ++ return -EINVAL; ++ } ++ ++ for (const auto &node : tuningData["edge"].asList()) { ++ bool ok = true; ++ EdgeSetting s; ++ ++ ok &= readScalar(node, "iso", s.iso); ++ ok &= readScalar(node, "edge-thed", s.edgeThed); ++ ok &= readScalar(node, "src-wgt", s.srcWeight); ++ ok &= readScalar(node, "alpha-adp-en", s.alphaAdaptive); ++ ok &= readScalar(node, "local-alpha", s.localAlpha); ++ ok &= readScalar(node, "global-alpha", s.globalAlpha); ++ ok &= readArray(node, "noise-clip", s.noiseClip); ++ ok &= readArray(node, "dog-clip-pos", s.dogClipPos); ++ ok &= readArray(node, "dog-clip-neg", s.dogClipNeg); ++ ok &= readArray(node, "dog-alpha", s.dogAlpha); ++ ok &= readArray(node, "direct-filter-coeff", s.directFilter); ++ ok &= readArray(node, "dog-kernel", s.dogKernel); ++ if (!ok) ++ return -EINVAL; ++ ++ if (!edge_.empty() && s.iso <= edge_.back().iso) { ++ LOG(RkISP2Sharpen, Error) << "Edge settings not sorted by iso"; ++ return -EINVAL; ++ } ++ edgeIsos_.push_back(s.iso); ++ edge_.push_back(s); ++ } ++ ++ if (edge_.size() < 2) { ++ LOG(RkISP2Sharpen, Error) << "Need two edge settings at least"; ++ return -EINVAL; ++ } ++ ++ return 0; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int Sharpen::init([[maybe_unused]] IPAContext &context, ++ const ValueNode &tuningData) ++{ ++ bool ok = true; ++ int ret; ++ ++ ok &= readArray(tuningData, "luma-point", lumaPoint_); ++ ok &= readArray(tuningData, "pbf-coeff", pbfCoeff_); ++ ok &= readArray(tuningData, "rf-m-coeff", rfMCoeff_); ++ ok &= readArray(tuningData, "mbf-coeff", mbfCoeff_); ++ ok &= readArray(tuningData, "rf-h-coeff", rfHCoeff_); ++ ok &= readArray(tuningData, "hbf-coeff", hbfCoeff_); ++ ok &= readArray(tuningData, "edge-hf-coeff", edgeHfCoeff_); ++ ok &= readArray(tuningData, "edge-guide-coeff", edgeGuideCoeff_); ++ ok &= readScalar(tuningData, "edge-dir-min", edgeDirMin_); ++ if (!ok) ++ return -EINVAL; ++ ++ ret = parseSharp(tuningData); ++ if (ret) ++ return ret; ++ ++ return parseEdge(tuningData); ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::configure ++ */ ++int Sharpen::configure([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const IPACameraSensorInfo &configInfo) ++{ ++ shp_.reset(); ++ ++ return 0; ++} ++ ++/** ++ * \brief Convert the sharpening calibration at an ISO to the SHP fields ++ * \param[in] iso The ISO, 50 times the gain ++ * \param[out] shp The SHP block ++ * ++ * Port of the AIQ select_rk_sharpen_hw_params_by_ISO() and ++ * rk_Sharp_V1_fix_transfer(). ++ */ ++void Sharpen::fixSharp(double iso, struct rkispp_params_shp *shp) const ++{ ++ calibration::IsoInterval in = calibration::isoInterval(sharpIsos_, iso); ++ const SharpSetting &L = sharp_[in.lo]; ++ const SharpSetting &H = sharp_[in.hi]; ++ double r = in.r; ++ ++ auto li = [&](double SharpSetting::*m) { return lerp(r, L.*m, H.*m); }; ++ ++ Luma lumaSigma = calibration::lerp(r, L.lumaSigma, H.lumaSigma); ++ Luma clipPosM = calibration::lerp(r, L.clipPosM, H.clipPosM); ++ Luma clipNegM = calibration::lerp(r, L.clipNegM, H.clipNegM); ++ Luma clipH = calibration::lerp(r, L.clipH, H.clipH); ++ double mRatio = li(&SharpSetting::mRatio); ++ double hRatio = li(&SharpSetting::hRatio); ++ double mClip = mRatio > kMaxLumaClip ? mRatio / kMaxLumaClip : 1.0; ++ double hClip = hRatio > kMaxLumaClip ? hRatio / kMaxLumaClip : 1.0; ++ double pbfGain = std::min(li(&SharpSetting::pbfGain), kMaxFilterGain); ++ double pbfAdd = std::min(li(&SharpSetting::pbfAdd), kMaxFilterAdd); ++ double mbfGain = std::min(li(&SharpSetting::mbfGain), kMaxFilterGain); ++ double mbfAdd = std::min(li(&SharpSetting::mbfAdd), kMaxFilterAdd); ++ double hbfGain = std::min(li(&SharpSetting::hbfGain), kMaxFilterGain); ++ double hbfAdd = std::min(li(&SharpSetting::hbfAdd), kMaxFilterAdd); ++ long ehf = roundF(li(&SharpSetting::localStrength)); ++ ++ shp->yin_flt_en = 1; ++ shp->edge_avg_en = 1; ++ shp->hbf_ratio = std::min(roundF(li(&SharpSetting::hbfRatio) * ++ (1 << kFracHbfRatio)), ++ 0x100L); ++ shp->ehf_th = std::min(ehf, 0xffL); ++ shp->pbf_ratio = std::min(roundF(li(&SharpSetting::pbfRatio) * ++ (1 << kFracPbfRatio)), ++ 0x80L); ++ ++ std::array pbf = kernelFix(pbfCoeff_, kTaps3, kFracPbfCoeff); ++ std::array mrf = kernelFix(rfMCoeff_, kTaps5, kFracMrfCoeff); ++ std::array mbf = kernelFix(mbfCoeff_, kTapsMbf, kFracMbfCoeff); ++ std::array hrf = kernelFix(rfHCoeff_, kTaps5, kFracHrfCoeff); ++ std::array hbf = kernelFix(hbfCoeff_, kTaps3, kFracHbfCoeff); ++ std::copy(pbf.begin(), pbf.end(), shp->pbf_k); ++ std::copy(mrf.begin(), mrf.end(), shp->mrf_k); ++ std::copy(mbf.begin(), mbf.end(), shp->mbf_k); ++ std::copy(hrf.begin(), hrf.end(), shp->hrf_k); ++ std::copy(hbf.begin(), hbf.end(), shp->hbf_k); ++ ++ for (unsigned int i = 0; i < RKISPP_SHP_LUM_POINT_SIZE; i++) ++ shp->lum_point[i] = roundF(lumaPoint_[i + 1]); ++ ++ shp->pbf_shf_bits = sigmaTable(lumaSigma, pbfGain, pbfAdd, shp->pbf_sigma); ++ shp->mbf_shf_bits = sigmaTable(lumaSigma, mbfGain, mbfAdd, shp->mbf_sigma); ++ shp->hbf_shf_bits = sigmaTable(lumaSigma, hbfGain, hbfAdd, shp->hbf_sigma); ++ ++ for (unsigned int i = 0; i < kLumaPoints; i++) { ++ shp->lum_clp_m[i] = std::min( ++ static_cast(roundF(clipPosM[i]) * mClip), 0xffL); ++ shp->lum_min_m[i] = std::max( ++ roundF(clipNegM[i] * mClip * (1 << kFracLumMinM)), ++ kLumMinMMin); ++ shp->lum_clp_h[i] = std::min( ++ static_cast(roundF(clipH[i]) * hClip), 0xffL); ++ } ++ ++ shp->rfl_ratio = std::min(roundF(li(&SharpSetting::lratio) * ++ (1 << kFracLratio)), ++ 0x100L); ++ shp->rfh_ratio = std::clamp(roundF(li(&SharpSetting::hratio) * ++ (1 << kFracHratio)), ++ 0x100L, 0x3ffL); ++ shp->m_ratio = std::min(roundF(mRatio * (1 << kFracMHRatio)), 0x1f); ++ shp->h_ratio = std::min(roundF(hRatio * (1 << kFracMHRatio)), 0x1f); ++} ++ ++/** ++ * \brief Convert the edge filter calibration at an ISO to the SHP fields ++ * \param[in] iso The ISO, 50 times the gain ++ * \param[out] shp The SHP block ++ * ++ * Port of the AIQ select_edgefilter_params_by_ISO() and ++ * edgefilter_fix_transfer(). Like the AIQ, the difference of gaussians kernel ++ * is the one of the ISO step below, not interpolated. ++ */ ++void Sharpen::fixEdge(double iso, struct rkispp_params_shp *shp) const ++{ ++ calibration::IsoInterval in = calibration::isoInterval(edgeIsos_, iso); ++ const EdgeSetting &L = edge_[in.lo]; ++ const EdgeSetting &H = edge_[in.hi]; ++ double r = in.r; ++ Kernel5 smooth = {}; ++ double dogSum = 0.0; ++ ++ auto li = [&](double EdgeSetting::*m) { return lerp(r, L.*m, H.*m); }; ++ ++ Luma noiseClip = calibration::lerp(r, L.noiseClip, H.noiseClip); ++ Luma clipPos = calibration::lerp(r, L.dogClipPos, H.dogClipPos); ++ Luma clipNeg = calibration::lerp(r, L.dogClipNeg, H.dogClipNeg); ++ Luma dogAlpha = calibration::lerp(r, L.dogAlpha, H.dogAlpha); ++ std::array direct = ++ calibration::lerp(r, L.directFilter, H.directFilter); ++ ++ shp->alpha_adp_en = static_cast(li(&EdgeSetting::alphaAdaptive)); ++ shp->edge_thed = std::min(roundF(li(&EdgeSetting::edgeThed)), 0xffL); ++ shp->dir_min = roundF(edgeDirMin_ * (1 << kFracDirMin)); ++ shp->smoth_th4 = std::min(roundF(li(&EdgeSetting::srcWeight) * ++ (1 << kFracSmooth4)), ++ 0xffL); ++ shp->l_alpha = std::min(roundF(li(&EdgeSetting::localAlpha) * ++ (1 << kFracAlpha)), ++ 0x1ffL); ++ shp->g_alpha = std::min(roundF(li(&EdgeSetting::globalAlpha) * ++ (1 << kFracAlpha)), ++ 0x1ffL); ++ ++ std::array guide = ++ kernelFix(edgeGuideCoeff_, kTaps3, kFracGuideCoeff); ++ std::copy(guide.begin(), guide.end(), shp->eg_coef); ++ ++ std::copy(direct.begin(), direct.end(), smooth.begin() + kSmoothRow); ++ std::array sm = kernelFix(smooth, kTapsSmooth, kFracSmoothCoeff); ++ std::copy(sm.begin(), sm.end(), shp->eg_smoth); ++ ++ std::array gaus = kernelFix(edgeHfCoeff_, kTaps5, kFracGausCoeff); ++ std::copy(gaus.begin(), gaus.end(), shp->eg_gaus); ++ ++ /* at unity strength the AIQ adds the whole kernel sum to the centre tap */ ++ for (unsigned int t = 0; t < kTaps5.size(); t++) ++ shp->dog_k[t] = roundF(L.dogKernel[kTaps5[t]] * (1 << kFracDogCoeff)); ++ for (double c : L.dogKernel) ++ dogSum += c; ++ shp->dog_k[0] += static_cast(dogSum * (1 << kFracDogCoeff)); ++ ++ for (unsigned int i = 0; i < kLumaPoints; i++) { ++ shp->edge_lum_thed[i] = std::min(roundF(noiseClip[i]), 0xffL); ++ shp->clamp_pos[i] = std::min(roundF(clipPos[i]), 0xffL); ++ shp->clamp_neg[i] = std::min(roundF(clipNeg[i]), 0xffL); ++ shp->detail_alpha[i] = ++ std::min(roundF(dogAlpha[i] * (1 << kFracDetailAlpha)), 0xffL); ++ } ++} ++ ++/** ++ * \brief Send the SHP block for the gain of the latest ISP frame ++ * \param[in] context The shared IPA context ++ * \param[in] frame The ISPP frame number ++ * \param[out] params The ISPP parameters buffer ++ */ ++void Sharpen::prepareIspp(IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ RkISPPParams *params) ++{ ++ constexpr size_t header = sizeof(struct v4l2_isp_params_block_header); ++ const auto &noiseGain = context.activeState.noiseGain; ++ double gain = noiseGain.gain > 0.0 ? noiseGain.gain : 1.0; ++ ++ if (!shp_ || ++ std::abs(gain - shpGain_) > calibration::kGainTolerance * shpGain_) { ++ struct rkispp_params_shp shp = {}; ++ double iso = gain * calibration::kIsoUnity; ++ ++ fixSharp(iso, &shp); ++ fixEdge(iso, &shp); ++ shp_ = shp; ++ shpGain_ = gain; ++ ++ LOG(RkISP2Sharpen, Debug) ++ << "gain " << gain << " m_ratio " ++ << static_cast(shp.m_ratio) << " h_ratio " ++ << static_cast(shp.h_ratio); ++ } ++ ++ auto block = params->block(); ++ memcpy(reinterpret_cast(&*block) + header, ++ reinterpret_cast(&*shp_) + header, ++ sizeof(*shp_) - header); ++ block.setEnabled(true); ++} ++ ++REGISTER_IPA_ALGORITHM(Sharpen, "Sharpen") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/sharpen.h +@@ -0,0 +1,104 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 sharpening ++ */ ++ ++#pragma once ++ ++#include ++#include ++#include ++ ++#include ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class Sharpen : public Algorithm ++{ ++public: ++ Sharpen() = default; ++ ~Sharpen() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ int configure(IPAContext &context, ++ const IPACameraSensorInfo &configInfo) override; ++ void prepareIspp(IPAContext &context, const uint32_t frame, ++ RkISPPParams *params) override; ++ ++ static constexpr unsigned int kLumaPoints = 8; ++ ++ using Luma = std::array; ++ using Kernel3 = std::array; ++ using Kernel5 = std::array; ++ using KernelMbf = std::array; ++ ++ struct SharpSetting { ++ double iso; ++ double lratio; ++ double hratio; ++ double mRatio; ++ double hRatio; ++ Luma lumaSigma; ++ double pbfGain; ++ double pbfRatio; ++ double pbfAdd; ++ Luma clipPosM; ++ Luma clipNegM; ++ Luma clipH; ++ double mbfGain; ++ double hbfGain; ++ double hbfRatio; ++ double mbfAdd; ++ double hbfAdd; ++ double localStrength; ++ }; ++ ++ struct EdgeSetting { ++ double iso; ++ double edgeThed; ++ double srcWeight; ++ double alphaAdaptive; ++ double localAlpha; ++ double globalAlpha; ++ Luma noiseClip; ++ Luma dogClipPos; ++ Luma dogClipNeg; ++ Luma dogAlpha; ++ std::array directFilter; ++ Kernel5 dogKernel; ++ }; ++ ++ void fixSharp(double iso, struct rkispp_params_shp *shp) const; ++ void fixEdge(double iso, struct rkispp_params_shp *shp) const; ++ ++private: ++ int parseSharp(const ValueNode &tuningData); ++ int parseEdge(const ValueNode &tuningData); ++ ++ Luma lumaPoint_; ++ Kernel3 pbfCoeff_; ++ Kernel5 rfMCoeff_; ++ KernelMbf mbfCoeff_; ++ Kernel5 rfHCoeff_; ++ Kernel3 hbfCoeff_; ++ Kernel5 edgeHfCoeff_; ++ Kernel3 edgeGuideCoeff_; ++ double edgeDirMin_; ++ ++ std::vector sharp_; ++ std::vector sharpIsos_; ++ std::vector edge_; ++ std::vector edgeIsos_; ++ ++ std::optional shp_; ++ double shpGain_; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ diff --git a/multimedia/libcamera/patches/239-ipa-rkisp2-imx415-add-the-noise-reduction-and-sharpening-calibration.patch b/multimedia/libcamera/patches/239-ipa-rkisp2-imx415-add-the-noise-reduction-and-sharpening-calibration.patch new file mode 100644 index 0000000000..b2189b2b35 --- /dev/null +++ b/multimedia/libcamera/patches/239-ipa-rkisp2-imx415-add-the-noise-reduction-and-sharpening-calibration.patch @@ -0,0 +1,922 @@ +From 45c06ae7dff71c03c04cbf9df26d56ef53f60ee4 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 10:41:21 +0000 +Subject: [PATCH 239/239] ipa: rkisp2: imx415: Add the noise reduction and + sharpening calibration + +Enable the spatial noise reduction and the sharpening with the YNR, +UVNR, SHARP and EDGEFILTER calibrations of the Rockchip IQ file for +this sensor (normal mode, LSNR setting), converted with nr-iq2yaml.py. +The edge filter kernels and direction minimum, which the AIQ sets in +code, are carried over as tuning values. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/data/imx415.yaml | 895 ++++++++++++++++++++++++++++++++ + 1 file changed, 895 insertions(+) + +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -464,6 +464,901 @@ algorithms: + lvl1-gfsigma: [ 0.125, 0.10938, 0.10938 ] + lvl2-gfsigma: [ 0.125, 0.10938, 0.10938 ] + lvl3-gfsigma: [ 0.125, 0.10938, 0.10938 ] ++ - NoiseReduction: ++ # converted from the vendor IQ YNR and UVNR normal/LSNR ++ # settings by nr-iq2yaml.py; gain = iso / 50 ++ luma: ++ - iso: 50 ++ sigma-curve: [ -1.08097064118e-12, 1.01241568527e-08, -3.39654575524e-05, 0.0430931083487, 11.4384162926 ] ++ ynr-lci: [ 1.0288, 0.767, 0.4966, 0.2957 ] ++ ynr-lhci: [ 0.7416, 1.2922, 1.1543, 0.7838 ] ++ ynr-hlci: [ 0.7052, 1.1315, 0.9849, 0.6998 ] ++ ynr-hhci: [ 0.7901, 1.7467, 1.6339, 1.2194 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1.1, 1.2, 1.2, 1, 1, 1 ] ++ lo-directionstrength: 0.5 ++ lo-bfscale: [ 1, 1, 1.6, 1.6 ] ++ imerge-ratio: 0.4 ++ denoise-weight: [ 0.6, 0.6, 0.7, 0.7 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1.1, 1.2, 1.2, 1, 1, 1 ] ++ hi-bfscale: [ 2, 2, 2, 1.5 ] ++ hwith-d: [ 2, 2, 1.2, 1.2 ] ++ hi-denoisestrength: 1 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.4, 0.4, 0.4, 0.4 ] ++ hgrad-y-level1: [ 1, 0.67, 0.67, 0.67, 0.67, 0.67 ] ++ hgrad-y-level2: [ 1, 0.67, 0.67, 0.67, 0.67, 0.67 ] ++ hgrad-y-level3: [ 1, 0.57, 0.57, 0.57, 0.57, 0.57 ] ++ hgrad-y-level4: [ 1, 0.5, 0.5, 0.4, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.11, 0.11, 0.1, 0.1 ] ++ - iso: 100 ++ sigma-curve: [ -1.42643494505e-12, 1.34890885238e-08, -4.57067447826e-05, 0.0584140906109, 15.3080175136 ] ++ ynr-lci: [ 1.0311, 0.7468, 0.4726, 0.2662 ] ++ ynr-lhci: [ 0.7363, 1.2222, 1.0467, 0.6595 ] ++ ynr-hlci: [ 0.7176, 1.1074, 0.9059, 0.5959 ] ++ ynr-hhci: [ 0.7821, 1.7362, 1.5249, 1.0927 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1.1, 1.2, 1.2, 1, 1, 1 ] ++ lo-directionstrength: 0.5 ++ lo-bfscale: [ 1, 1, 1.5, 1.5 ] ++ imerge-ratio: 0.4 ++ denoise-weight: [ 0.3, 0.3, 0.3, 0.3 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1.1, 1.2, 1.2, 1, 1, 1 ] ++ hi-bfscale: [ 2, 2, 1.5, 1.5 ] ++ hwith-d: [ 2, 2, 1.2, 1.2 ] ++ hi-denoisestrength: 1 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.6, 0.6, 0.6, 0.5 ] ++ hgrad-y-level1: [ 1, 0.5, 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level2: [ 1, 0.5, 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level3: [ 1, 0.5, 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level4: [ 1, 0.5, 0.5, 0.4, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.11, 0.11, 0.1, 0.1 ] ++ - iso: 200 ++ sigma-curve: [ -1.92489788527e-12, 1.83133699063e-08, -6.28272056364e-05, 0.0816138655195, 20.494600962 ] ++ ynr-lci: [ 1.0366, 0.7486, 0.4676, 0.2666 ] ++ ynr-lhci: [ 0.7094, 1.1528, 0.9302, 0.5924 ] ++ ynr-hlci: [ 0.7099, 1.0778, 0.8637, 0.5622 ] ++ ynr-hhci: [ 0.7677, 1.7011, 1.4501, 1.0186 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1.2, 1.2, 1.2, 1.2, 1, 1 ] ++ lo-directionstrength: 0.5 ++ lo-bfscale: [ 0.7, 0.7, 1.5, 1.5 ] ++ imerge-ratio: 0.3 ++ denoise-weight: [ 0.3, 0.3, 0.3, 0.3 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1.1, 1.1, 1.2, 1, 1, 1 ] ++ hi-bfscale: [ 2, 2, 2, 1.5 ] ++ hwith-d: [ 4, 4, 2, 1.2 ] ++ hi-denoisestrength: 1 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level1: [ 1, 0.5, 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level2: [ 1, 0.5, 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level3: [ 1, 0.5, 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level4: [ 1, 0.5, 0.5, 0.4, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.12, 0.12, 0.1, 0.1 ] ++ - iso: 400 ++ sigma-curve: [ -2.92316241432e-12, 2.75250206713e-08, -9.32202602113e-05, 0.119250987692, 29.1180509503 ] ++ ynr-lci: [ 1.0479, 0.7713, 0.4942, 0.2871 ] ++ ynr-lhci: [ 0.6709, 1.1068, 0.8738, 0.5623 ] ++ ynr-hlci: [ 0.6824, 1.0662, 0.8464, 0.5398 ] ++ ynr-hhci: [ 0.7468, 1.6695, 1.4201, 0.989 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1.2, 1.2, 1.2, 1, 1, 1 ] ++ lo-directionstrength: 0.5 ++ lo-bfscale: [ 0.6, 0.7, 1.5, 1.5 ] ++ imerge-ratio: 0.35 ++ denoise-weight: [ 0.35, 0.35, 0.35, 0.35 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1.1, 1.2, 1.2, 1.2, 1, 1 ] ++ hi-bfscale: [ 2.2, 2.2, 2.2, 1.7 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 0.9 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.6, 0.6, 0.6, 0.6 ] ++ hgrad-y-level1: [ 1, 0.5, 0.5, 0.5, 0.5, 0.3 ] ++ hgrad-y-level2: [ 1, 0.5, 0.5, 0.5, 0.5, 0.3 ] ++ hgrad-y-level3: [ 1, 0.51, 0.51, 0.51, 0.51, 0.3 ] ++ hgrad-y-level4: [ 1, 0.5, 0.5, 0.4, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.13, 0.13, 0.1, 0.1 ] ++ - iso: 800 ++ sigma-curve: [ -3.66919679355e-12, 3.40534546077e-08, -0.000114675923701, 0.14507808339, 53.0572121429 ] ++ ynr-lci: [ 1.0484, 0.7722, 0.4959, 0.2933 ] ++ ynr-lhci: [ 0.6707, 1.0796, 0.8458, 0.5469 ] ++ ynr-hlci: [ 0.684, 1.0556, 0.8258, 0.5209 ] ++ ynr-hhci: [ 0.7418, 1.6515, 1.3885, 0.9759 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ lo-directionstrength: 0.3 ++ lo-bfscale: [ 0.3, 0.4, 0.8, 0.8 ] ++ imerge-ratio: 0.35 ++ denoise-weight: [ 0.3, 0.4, 0.4, 0.4 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ hi-bfscale: [ 2.2, 2.2, 2.2, 1.7 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 0.7 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.45, 0.45, 0.45, 0.45 ] ++ hgrad-y-level1: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.1, 0.1, 0.1, 0.1 ] ++ - iso: 1600 ++ sigma-curve: [ -6.88500038533e-12, 6.28952567479e-08, -0.000206166706955, 0.256322540496, 58.7093336455 ] ++ ynr-lci: [ 1.037, 0.7514, 0.471, 0.2726 ] ++ ynr-lhci: [ 0.703, 1.0702, 0.8065, 0.5151 ] ++ ynr-hlci: [ 0.7147, 1.0547, 0.7988, 0.4938 ] ++ ynr-hhci: [ 0.752, 1.6516, 1.367, 0.9291 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 0.3, 0.7, 0.7, 0.3 ] ++ imerge-ratio: 0.3 ++ denoise-weight: [ 0.3, 0.6, 0.6, 0.8 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ hi-bfscale: [ 2.2, 2.2, 2.2, 1.7 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 0.7 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.5, 0.5, 0.5, 0.5 ] ++ hgrad-y-level1: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.5, 0.4, 0.35, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.1, 0.2, 0.2, 0.1 ] ++ - iso: 3200 ++ sigma-curve: [ -9.464965e-12, 8.72810970505e-08, -0.000290638086962, 0.366968609966, 82.1710507886 ] ++ ynr-lci: [ 1.0388, 0.7594, 0.4671, 0.2638 ] ++ ynr-lhci: [ 0.7067, 1.0717, 0.8083, 0.5003 ] ++ ynr-hlci: [ 0.7152, 1.0464, 0.8022, 0.502 ] ++ ynr-hhci: [ 0.756, 1.6484, 1.3763, 0.9423 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ imerge-ratio: 0.3 ++ denoise-weight: [ 0.5, 0.6, 0.6, 0.6 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ hi-bfscale: [ 2.2, 2.2, 2.2, 1.7 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 0.7 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.6, 0.7, 0.7, 0.6 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.2, 0.2, 0.2, 0 ] ++ - iso: 6400 ++ sigma-curve: [ -1.30178255978e-11, 1.23199356009e-07, -0.000429366291333, 0.578668286916, 75.3975737482 ] ++ ynr-lci: [ 1.0477, 0.7698, 0.4802, 0.2679 ] ++ ynr-lhci: [ 0.6902, 1.0727, 0.8224, 0.5202 ] ++ ynr-hlci: [ 0.7086, 1.06, 0.8163, 0.5094 ] ++ ynr-hhci: [ 0.7582, 1.6318, 1.3924, 0.9582 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ imerge-ratio: 0.15 ++ denoise-weight: [ 0.5, 0.5, 0.5, 0.5 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ hi-bfscale: [ 1.5, 2.2, 2.2, 1.5 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 0.7 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.7, 0.7, 0.7, 0.7 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.2, 0.2, 0.2, 0.1 ] ++ - iso: 12800 ++ sigma-curve: [ -1.06902659849e-11, 1.01044089872e-07, -0.000390330176439, 0.618080545783, 124.273449663 ] ++ ynr-lci: [ 1.0298, 0.7548, 0.4694, 0.267 ] ++ ynr-lhci: [ 0.7207, 1.0508, 0.8038, 0.5216 ] ++ ynr-hlci: [ 0.733, 1.0444, 0.7992, 0.5114 ] ++ ynr-hhci: [ 0.763, 1.5805, 1.3473, 0.9288 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ imerge-ratio: 0.15 ++ denoise-weight: [ 0.5, 0.5, 0.6, 0.6 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1, 1, 1, 1, 1, 1 ] ++ hi-bfscale: [ 1.5, 2.2, 2.2, 1.5 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 0.7 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.7, 0.7, 0.7, 0.7 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.26, 0.3, 0.3, 0.1 ] ++ - iso: 25600 ++ sigma-curve: [ -1.06902659849e-11, 1.01044089872e-07, -0.000390330176439, 0.618080545783, 124.273449663 ] ++ ynr-lci: [ 1.0298, 0.7548, 0.4694, 0.267 ] ++ ynr-lhci: [ 0.7207, 1.0508, 0.8038, 0.5216 ] ++ ynr-hlci: [ 0.733, 1.0444, 0.7992, 0.5114 ] ++ ynr-hhci: [ 0.763, 1.5805, 1.3473, 0.9288 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1.5, 1.5, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 0.5, 0.5, 0.5, 0.5 ] ++ imerge-ratio: 0.15 ++ denoise-weight: [ 0.8, 0.85, 0.7, 0.7 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 1.5, 1.5, 1.5, 1.5, 1.3, 1.2 ] ++ hi-bfscale: [ 1.5, 2.2, 2.2, 1.5 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 1.2 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.85, 0.85, 0.76, 0.76 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.15, 0.6, 0.6, 0.12 ] ++ - iso: 51200 ++ sigma-curve: [ -2.35581884731e-11, 1.55685371261e-07, -0.000452079932134, 0.788144215984, 204.57332096 ] ++ ynr-lci: [ 0.9896, 0.757, 0.4563, 0.182 ] ++ ynr-lhci: [ 0.7285, 1.0224, 0.7836, 0.4887 ] ++ ynr-hlci: [ 0.7305, 1.0156, 0.7603, 0.5563 ] ++ ynr-hhci: [ 0.6922, 1.4114, 1.3231, 0.6804 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1.5, 1.5, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 1, 1, 3, 1 ] ++ imerge-ratio: 0.15 ++ denoise-weight: [ 0.3, 0.85, 0.3, 0.3 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 2, 2.5, 1.5, 1.5, 1, 1 ] ++ hi-bfscale: [ 1.2, 1.1, 0.86, 0.86 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 1.2 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.75, 0.7, 0.66, 0.36 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.13, 0.3, 0.3, 0.1 ] ++ - iso: 102400 ++ sigma-curve: [ -5.24186459287e-12, 4.83761295342e-08, -0.000158009865774, 0.205697200837, -24.6859972726 ] ++ ynr-lci: [ 0.936182, 0.654274, 0.45586, 0.403902 ] ++ ynr-lhci: [ 0.855909, 0.979829, 0.691382, 0.538871 ] ++ ynr-hlci: [ 0.885451, 0.981653, 0.701294, 0.551322 ] ++ ynr-hhci: [ 0.706894, 1.463291, 1.102797, 0.741516 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1.5, 1.5, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 1, 1, 3, 1 ] ++ imerge-ratio: 0.15 ++ denoise-weight: [ 0.3, 0.85, 0.3, 0.3 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 2, 2.5, 1.5, 1.5, 1, 1 ] ++ hi-bfscale: [ 1.2, 1.1, 0.86, 0.86 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 1.2 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.75, 0.7, 0.66, 0.36 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.13, 0.3, 0.3, 0.1 ] ++ - iso: 204800 ++ sigma-curve: [ -5.24186459287e-12, 4.83761295342e-08, -0.000158009865774, 0.205697200837, -24.6859972726 ] ++ ynr-lci: [ 0.936182, 0.654274, 0.45586, 0.403902 ] ++ ynr-lhci: [ 0.855909, 0.979829, 0.691382, 0.538871 ] ++ ynr-hlci: [ 0.885451, 0.981653, 0.701294, 0.551322 ] ++ ynr-hhci: [ 0.706894, 1.463291, 1.102797, 0.741516 ] ++ lo-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ lo-lumaratio: [ 1, 1, 1.5, 1.5, 1, 1 ] ++ lo-directionstrength: 0.2 ++ lo-bfscale: [ 1, 1, 3, 1 ] ++ imerge-ratio: 0.15 ++ denoise-weight: [ 0.3, 0.85, 0.3, 0.3 ] ++ hi-lumapoint: [ 0, 32, 64, 128, 192, 256 ] ++ hi-lumaratio: [ 2, 2.5, 1.5, 1.5, 1, 1 ] ++ hi-bfscale: [ 1.2, 1.1, 0.86, 0.86 ] ++ hwith-d: [ 6, 6, 4, 2 ] ++ hi-denoisestrength: 1.2 ++ hi-detailminadjdnw: 0.2 ++ hi-denoiseweight: [ 0.75, 0.7, 0.66, 0.36 ] ++ hgrad-y-level1: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level2: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level3: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hgrad-y-level4: [ 1, 0.65, 0.55, 0.45, 0.3, 0.2 ] ++ hi-soft-thresh-scale: [ 0.13, 0.3, 0.3, 0.1 ] ++ chroma: ++ kernel-3x3: [ 1, 0.8825, 0.7788 ] ++ kernel-5x5: [ 1, 0.8825, 0.7788, 0.6065, 0.3679 ] ++ kernel-9x9: [ 1, 0.8825, 0.7788, 0.6065, 0.3679, 0.1969, 0.1353, 0.0439 ] ++ kernel-9x9-num: 0 ++ settings: ++ - iso: 50 ++ step0-uvgrad-ratio: 40 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.3 ++ step2-median-ratio: 0.2 ++ step1-bf-sigmar: 8.24 ++ step2-bf-sigmar: 5.12 ++ step3-bf-sigmar: 3.413 ++ step1-bf-uvgain: 1.5 ++ step2-bf-uvgain: 2 ++ step3-bf-uvgain: 4 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 100 ++ step0-uvgrad-ratio: 40 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.4 ++ step2-median-ratio: 0.2 ++ step1-bf-sigmar: 8.24 ++ step2-bf-sigmar: 6.4 ++ step3-bf-sigmar: 4.267 ++ step1-bf-uvgain: 1.5 ++ step2-bf-uvgain: 2 ++ step3-bf-uvgain: 4 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 200 ++ step0-uvgrad-ratio: 40 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.5 ++ step2-median-ratio: 0.3 ++ step1-bf-sigmar: 8.24 ++ step2-bf-sigmar: 8.53 ++ step3-bf-sigmar: 5.689 ++ step1-bf-uvgain: 1.5 ++ step2-bf-uvgain: 2 ++ step3-bf-uvgain: 4 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 400 ++ step0-uvgrad-ratio: 40 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.6 ++ step2-median-ratio: 0.4 ++ step1-bf-sigmar: 8.24 ++ step2-bf-sigmar: 10.24 ++ step3-bf-sigmar: 7.11 ++ step1-bf-uvgain: 1.5 ++ step2-bf-uvgain: 2 ++ step3-bf-uvgain: 3 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 800 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.8 ++ step2-median-ratio: 0.5 ++ step1-bf-sigmar: 12.8 ++ step2-bf-sigmar: 12.24 ++ step3-bf-sigmar: 7 ++ step1-bf-uvgain: 1.2 ++ step2-bf-uvgain: 2 ++ step3-bf-uvgain: 3 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 1600 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.8 ++ step2-median-ratio: 0.6 ++ step1-bf-sigmar: 28.8 ++ step2-bf-sigmar: 12.8 ++ step3-bf-sigmar: 8.4 ++ step1-bf-uvgain: 1 ++ step2-bf-uvgain: 2 ++ step3-bf-uvgain: 2.5 ++ step1-bf-ratio: 1 ++ step3-bf-ratio: 1 ++ - iso: 3200 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.8 ++ step2-median-ratio: 0.7 ++ step1-bf-sigmar: 42.3 ++ step2-bf-sigmar: 18 ++ step3-bf-sigmar: 9.8 ++ step1-bf-uvgain: 0.4 ++ step2-bf-uvgain: 1.5 ++ step3-bf-uvgain: 1.8 ++ step1-bf-ratio: 1 ++ step3-bf-ratio: 1 ++ - iso: 6400 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.8 ++ step2-median-ratio: 0.8 ++ step1-bf-sigmar: 58.6 ++ step2-bf-sigmar: 28 ++ step3-bf-sigmar: 11 ++ step1-bf-uvgain: 0.4 ++ step2-bf-uvgain: 1.2 ++ step3-bf-uvgain: 1 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 12800 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.8 ++ step2-median-ratio: 0.8 ++ step1-bf-sigmar: 64 ++ step2-bf-sigmar: 32 ++ step3-bf-sigmar: 11 ++ step1-bf-uvgain: 0.3 ++ step2-bf-uvgain: 0.8 ++ step3-bf-uvgain: 0.8 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 25600 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 0.8 ++ step2-median-ratio: 0.8 ++ step1-bf-sigmar: 64 ++ step2-bf-sigmar: 32 ++ step3-bf-sigmar: 11 ++ step1-bf-uvgain: 0.3 ++ step2-bf-uvgain: 0.6 ++ step3-bf-uvgain: 0.6 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 51200 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 1 ++ step2-median-ratio: 0.8 ++ step1-bf-sigmar: 64 ++ step2-bf-sigmar: 32 ++ step3-bf-sigmar: 11 ++ step1-bf-uvgain: 0.3 ++ step2-bf-uvgain: 0.6 ++ step3-bf-uvgain: 0.6 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 102400 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 1 ++ step2-median-ratio: 0.8 ++ step1-bf-sigmar: 64 ++ step2-bf-sigmar: 32 ++ step3-bf-sigmar: 11 ++ step1-bf-uvgain: 0.3 ++ step2-bf-uvgain: 0.6 ++ step3-bf-uvgain: 0.6 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - iso: 204800 ++ step0-uvgrad-ratio: 60 ++ step0-uvgrad-offset: 0.8 ++ step1-median-ratio: 1 ++ step2-median-ratio: 1 ++ step1-bf-sigmar: 64 ++ step2-bf-sigmar: 32 ++ step3-bf-sigmar: 11 ++ step1-bf-uvgain: 0.3 ++ step2-bf-uvgain: 0.6 ++ step3-bf-uvgain: 0.6 ++ step1-bf-ratio: 0 ++ step3-bf-ratio: 1 ++ - Sharpen: ++ # converted from the vendor IQ SHARP and EDGEFILTER ++ # normal/LSNR settings by nr-iq2yaml.py; gain = iso / 50 ++ luma-point: [ 0, 16, 32, 64, 96, 160, 224, 256 ] ++ pbf-coeff: [ 0.15625, 0.25, 0.15625, 0.25, 0.375, 0.25, 0.15625, 0.25, 0.15625 ] ++ rf-m-coeff: [ 0.023438, 0.03125, 0.039063, 0.03125, 0.023438, 0.03125, 0.046875, 0.054688, 0.046875, 0.03125, 0.039063, 0.054688, 0.09375, 0.054688, 0.039063, 0.03125, 0.046875, 0.054688, 0.046875, 0.03125, 0.023438, 0.03125, 0.039063, 0.03125, 0.023438 ] ++ mbf-coeff: [ 0, 0, 0, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.34375, 0, 0.328125, 0, 0.34375, 0, 0, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0.359375, 0, 0, 0, 0, 0, 0, 0, 0.359375, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0, 0.296875, 0, 0.234375, 0, 0.234375, 0, 0.296875, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0.34375, 0, 0, 0, 0, 0.171875, 0, 0, 0, 0, 0.34375, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0.234375, 0, 0.140625, 0.109375, 0.140625, 0, 0.234375, 0, 0, 0, 0, 0, 0.4375, 0, 0, 0.328125, 0, 0, 0.171875, 0.109375, 0, 0.109375, 0.171875, 0, 0, 0.328125, 0, 0, 0.4375, 0, 0, 0, 0, 0, 0.234375, 0, 0.140625, 0.109375, 0.140625, 0, 0.234375, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0.34375, 0, 0, 0, 0, 0.171875, 0, 0, 0, 0, 0.34375, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0.296875, 0, 0.234375, 0, 0.234375, 0, 0.296875, 0, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0.359375, 0, 0, 0, 0, 0, 0, 0, 0.359375, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0, 0, 0.34375, 0, 0.328125, 0, 0.34375, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0, 0, 0, 0, 0, 0, 0.40625, 0, 0, 0, 0 ] ++ rf-h-coeff: [ 0, 0.015625, 0.023438, 0.015625, 0, 0.015625, 0.0625, 0.101563, 0.0625, 0.015625, 0.023438, 0.101563, 0.125, 0.101563, 0.023438, 0.015625, 0.0625, 0.101563, 0.0625, 0.015625, 0, 0.015625, 0.023438, 0.015625, 0 ] ++ hbf-coeff: [ 0.15625, 0.25, 0.15625, 0.25, 0.375, 0.25, 0.15625, 0.25, 0.15625 ] ++ edge-hf-coeff: [ 0, 0, 0, 0, 0, 0, 0.0625, 0.125, 0.0625, 0, 0, 0.125, 0.25, 0.125, 0, 0, 0.0625, 0.125, 0.0625, 0, 0, 0, 0, 0, 0 ] ++ edge-guide-coeff: [ -0.0625, -0.125, -0.0625, -0.125, 1.75, -0.125, -0.0625, -0.125, -0.0625 ] ++ edge-dir-min: 0.94 ++ sharp: ++ - iso: 50 ++ lratio: 0.3 ++ hratio: 1.8 ++ mf-sharp-ratio: 2.5 ++ hf-sharp-ratio: 7.5 ++ pbf-gain: 0.7 ++ pbf-ratio: 0.7 ++ pbf-add: 2 ++ mbf-gain: 0.7 ++ hbf-gain: 0.9 ++ hbf-ratio: 0.8 ++ mbf-add: 3 ++ hbf-add: 1.5 ++ luma-sigma: [ 2, 3, 4, 4, 6, 5, 4, 4 ] ++ mf-clip-pos: [ 8, 16, 20, 32, 40, 40, 40, 30 ] ++ mf-clip-neg: [ -0.3, -0.4, -0.5, -0.6, -0.8, -0.8, -0.6, -0.4 ] ++ hf-clip: [ 14, 30, 32, 42, 52, 52, 46, 42 ] ++ local-sharp-strength: 64 ++ - iso: 100 ++ lratio: 0.3 ++ hratio: 1.8 ++ mf-sharp-ratio: 2.5 ++ hf-sharp-ratio: 6.5 ++ pbf-gain: 0.7 ++ pbf-ratio: 0.7 ++ pbf-add: 2 ++ mbf-gain: 0.7 ++ hbf-gain: 0.9 ++ hbf-ratio: 0.8 ++ mbf-add: 3 ++ hbf-add: 1.5 ++ luma-sigma: [ 3, 4, 5, 6, 6, 5, 4, 3 ] ++ mf-clip-pos: [ 8, 16, 20, 32, 40, 40, 40, 30 ] ++ mf-clip-neg: [ -0.3, -0.4, -0.5, -0.6, -0.8, -0.8, -0.6, -0.4 ] ++ hf-clip: [ 14, 30, 32, 42, 52, 52, 46, 42 ] ++ local-sharp-strength: 64 ++ - iso: 200 ++ lratio: 0.35 ++ hratio: 1.8 ++ mf-sharp-ratio: 2.5 ++ hf-sharp-ratio: 6.5 ++ pbf-gain: 0.7 ++ pbf-ratio: 1 ++ pbf-add: 3 ++ mbf-gain: 0.7 ++ hbf-gain: 0.9 ++ hbf-ratio: 0.9 ++ mbf-add: 4 ++ hbf-add: 3.5 ++ luma-sigma: [ 3, 4, 5, 6, 6, 5, 4, 3 ] ++ mf-clip-pos: [ 8, 16, 20, 24, 32, 32, 32, 26 ] ++ mf-clip-neg: [ -0.3, -0.4, -0.5, -0.6, -0.8, -0.8, -0.6, -0.4 ] ++ hf-clip: [ 14, 30, 32, 42, 50, 50, 46, 42 ] ++ local-sharp-strength: 64 ++ - iso: 400 ++ lratio: 0.35 ++ hratio: 1.8 ++ mf-sharp-ratio: 4 ++ hf-sharp-ratio: 5 ++ pbf-gain: 0.7 ++ pbf-ratio: 1 ++ pbf-add: 5 ++ mbf-gain: 0.7 ++ hbf-gain: 0.9 ++ hbf-ratio: 1 ++ mbf-add: 7 ++ hbf-add: 5 ++ luma-sigma: [ 3, 4, 5, 6, 6, 5, 4, 3 ] ++ mf-clip-pos: [ 2, 4, 10, 20, 36, 36, 28, 24 ] ++ mf-clip-neg: [ -0.2, -0.3, -0.4, -0.4, -0.4, -0.4, -0.3, -0.2 ] ++ hf-clip: [ 8, 16, 20, 30, 36, 36, 30, 24 ] ++ local-sharp-strength: 42 ++ - iso: 800 ++ lratio: 0.2 ++ hratio: 1.8 ++ mf-sharp-ratio: 5 ++ hf-sharp-ratio: 4 ++ pbf-gain: 0.7 ++ pbf-ratio: 0.8 ++ pbf-add: 5 ++ mbf-gain: 0.8 ++ hbf-gain: 1 ++ hbf-ratio: 1 ++ mbf-add: 8 ++ hbf-add: 6 ++ luma-sigma: [ 4, 5, 6, 8, 8, 8, 7, 6 ] ++ mf-clip-pos: [ 2, 4, 10, 20, 36, 36, 28, 24 ] ++ mf-clip-neg: [ -0.1, -0.2, -0.3, -0.4, -0.4, -0.4, -0.35, -0.2 ] ++ hf-clip: [ 8, 16, 24, 36, 40, 40, 36, 32 ] ++ local-sharp-strength: 32 ++ - iso: 1600 ++ lratio: 0.2 ++ hratio: 2 ++ mf-sharp-ratio: 6 ++ hf-sharp-ratio: 4 ++ pbf-gain: 0.8 ++ pbf-ratio: 0.8 ++ pbf-add: 6 ++ mbf-gain: 0.8 ++ hbf-gain: 1 ++ hbf-ratio: 1 ++ mbf-add: 10 ++ hbf-add: 7 ++ luma-sigma: [ 5, 5, 6, 8, 9, 8, 8, 7 ] ++ mf-clip-pos: [ 2, 4, 10, 20, 36, 36, 28, 24 ] ++ mf-clip-neg: [ -0.1, -0.2, -0.3, -0.4, -0.4, -0.3, -0.2, -0.1 ] ++ hf-clip: [ 8, 16, 24, 36, 40, 40, 36, 32 ] ++ local-sharp-strength: 24 ++ - iso: 3200 ++ lratio: 0.1 ++ hratio: 2 ++ mf-sharp-ratio: 6 ++ hf-sharp-ratio: 3 ++ pbf-gain: 0.8 ++ pbf-ratio: 0.8 ++ pbf-add: 6 ++ mbf-gain: 0.8 ++ hbf-gain: 1 ++ hbf-ratio: 1 ++ mbf-add: 10 ++ hbf-add: 7 ++ luma-sigma: [ 8, 10, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 2, 6, 16, 24, 34, 36, 34, 30 ] ++ mf-clip-neg: [ -0, -0.15, -0.3, -0.4, -0.5, -0.4, -0.2, -0.1 ] ++ hf-clip: [ 2, 6, 12, 18, 32, 32, 24, 16 ] ++ local-sharp-strength: 24 ++ - iso: 6400 ++ lratio: 0 ++ hratio: 2 ++ mf-sharp-ratio: 6 ++ hf-sharp-ratio: 2 ++ pbf-gain: 0.8 ++ pbf-ratio: 0.8 ++ pbf-add: 6 ++ mbf-gain: 0.8 ++ hbf-gain: 1.2 ++ hbf-ratio: 1 ++ mbf-add: 10 ++ hbf-add: 7 ++ luma-sigma: [ 8, 10, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 4, 6, 12, 24, 34, 34, 32, 30 ] ++ mf-clip-neg: [ -0, -0.1, -0.2, -0.2, -0.22, -0.2, -0.12, -0.05 ] ++ hf-clip: [ 3, 5, 7, 10, 10, 12, 10, 10 ] ++ local-sharp-strength: 24 ++ - iso: 12800 ++ lratio: 0 ++ hratio: 2 ++ mf-sharp-ratio: 6 ++ hf-sharp-ratio: 2 ++ pbf-gain: 0.8 ++ pbf-ratio: 0.8 ++ pbf-add: 7 ++ mbf-gain: 0.8 ++ hbf-gain: 1.2 ++ hbf-ratio: 1 ++ mbf-add: 12 ++ hbf-add: 8 ++ luma-sigma: [ 8, 10, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 4, 6, 12, 24, 34, 34, 32, 30 ] ++ mf-clip-neg: [ -0.5, -0.5, -0.5, -0.4, -0.4, -0.4, -0.35, -0.2 ] ++ hf-clip: [ 2, 5, 7, 10, 10, 12, 10, 10 ] ++ local-sharp-strength: 24 ++ - iso: 25600 ++ lratio: 0 ++ hratio: 2 ++ mf-sharp-ratio: 6 ++ hf-sharp-ratio: 2 ++ pbf-gain: 0.8 ++ pbf-ratio: 0.8 ++ pbf-add: 7 ++ mbf-gain: 0.8 ++ hbf-gain: 1.2 ++ hbf-ratio: 1 ++ mbf-add: 12 ++ hbf-add: 8 ++ luma-sigma: [ 8, 10, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 1, 2, 6, 8, 12, 16, 16, 10 ] ++ mf-clip-neg: [ -0.5, -0.5, -0.5, -0.4, -0.4, -0.4, -0.35, -0.2 ] ++ hf-clip: [ 2, 5, 7, 10, 10, 12, 10, 10 ] ++ local-sharp-strength: 16 ++ - iso: 51200 ++ lratio: 0.67 ++ hratio: 1.7 ++ mf-sharp-ratio: 1 ++ hf-sharp-ratio: 1 ++ pbf-gain: 1 ++ pbf-ratio: 0.9 ++ pbf-add: 16 ++ mbf-gain: 1 ++ hbf-gain: 1 ++ hbf-ratio: 0.9 ++ mbf-add: 24 ++ hbf-add: 24 ++ luma-sigma: [ 8, 10, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 1, 2, 4, 6, 10, 16, 16, 10 ] ++ mf-clip-neg: [ -0.5, -0.5, -0.5, -0.4, -0.4, -0.4, -0.35, -0.2 ] ++ hf-clip: [ 1, 4, 6, 10, 24, 32, 32, 24 ] ++ local-sharp-strength: 12 ++ - iso: 102400 ++ lratio: 0.67 ++ hratio: 1.5 ++ mf-sharp-ratio: 3 ++ hf-sharp-ratio: 2.5 ++ pbf-gain: 1 ++ pbf-ratio: 1 ++ pbf-add: 0 ++ mbf-gain: 1 ++ hbf-gain: 1 ++ hbf-ratio: 0.9 ++ mbf-add: 2 ++ hbf-add: 1 ++ luma-sigma: [ 6, 8, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 4, 6, 8, 8, 10, 8, 4, 0 ] ++ mf-clip-neg: [ -1, -0.5, -0.25, -0.25, -0.2, -0.2, -0.1, -0 ] ++ hf-clip: [ 6, 10, 12, 16, 20, 20, 8, 0 ] ++ local-sharp-strength: 10 ++ - iso: 204800 ++ lratio: 0.67 ++ hratio: 1.5 ++ mf-sharp-ratio: 3 ++ hf-sharp-ratio: 2.5 ++ pbf-gain: 1 ++ pbf-ratio: 1 ++ pbf-add: 0 ++ mbf-gain: 1 ++ hbf-gain: 1 ++ hbf-ratio: 0.9 ++ mbf-add: 2 ++ hbf-add: 1 ++ luma-sigma: [ 6, 8, 10, 12, 14, 12, 12, 10 ] ++ mf-clip-pos: [ 4, 6, 8, 8, 10, 8, 4, 0 ] ++ mf-clip-neg: [ -1, -0.5, -0.25, -0.25, -0.2, -0.2, -0.1, -0 ] ++ hf-clip: [ 6, 10, 12, 16, 20, 20, 8, 0 ] ++ local-sharp-strength: 10 ++ edge: ++ - iso: 50 ++ edge-thed: 8 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.1 ++ noise-clip: [ 2, 6, 8, 10, 16, 16, 14, 10 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - iso: 100 ++ edge-thed: 8 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.15 ++ noise-clip: [ 2, 6, 8, 10, 16, 16, 14, 10 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - iso: 200 ++ edge-thed: 10 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.2 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - iso: 400 ++ edge-thed: 14 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.3 ++ noise-clip: [ 4, 8, 16, 32, 32, 24, 16, 16 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - iso: 800 ++ edge-thed: 20 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.5 ++ noise-clip: [ 4, 8, 16, 32, 32, 24, 16, 16 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - iso: 1600 ++ edge-thed: 20 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.5 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 2, 3, 4, 6, 5, 4, 4, 3 ] ++ dog-clip-neg: [ 2, 3, 4, 6, 5, 4, 4, 3 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0232, -0.0334, -0.0366, -0.0334, -0.0232, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0366, 0.0541, 0.3775, 0.0541, -0.0366, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0232, -0.0334, -0.0366, -0.0334, -0.0232 ] ++ - iso: 3200 ++ edge-thed: 26 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.5 ++ noise-clip: [ 8, 10, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 2, 3, 4, 6, 5, 4, 4, 3 ] ++ dog-clip-neg: [ 2, 3, 4, 6, 5, 4, 4, 3 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0232, -0.0334, -0.0366, -0.0334, -0.0232, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0366, 0.0541, 0.3775, 0.0541, -0.0366, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0232, -0.0334, -0.0366, -0.0334, -0.0232 ] ++ - iso: 6400 ++ edge-thed: 26 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.5 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 2, 3, 4, 6, 5, 4, 4, 3 ] ++ dog-clip-neg: [ 2, 3, 4, 6, 5, 4, 4, 3 ] ++ dog-alpha: [ 0, 0, 0, 0.3, 0.3, 0.3, 0.3, 0.3 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0232, -0.0334, -0.0366, -0.0334, -0.0232, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0366, 0.0541, 0.3775, 0.0541, -0.0366, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0232, -0.0334, -0.0366, -0.0334, -0.0232 ] ++ - iso: 12800 ++ edge-thed: 32 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 0 ++ global-alpha: 0.5 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0, 0.2, 0.2, 0.2, 0.2, 0.2 ] ++ direct-filter-coeff: [ 0.1, 0.2, 0.4, 0.2, 0.1 ] ++ dog-kernel: [ -0.0232, -0.0334, -0.0366, -0.0334, -0.0232, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0366, 0.0541, 0.3775, 0.0541, -0.0366, -0.0334, -0.0218, 0.0541, -0.0218, -0.0334, -0.0232, -0.0334, -0.0366, -0.0334, -0.0232 ] ++ - iso: 25600 ++ edge-thed: 48 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 1 ++ global-alpha: 0.5 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0, 0.2, 0.2, 0.2, 0.2, 0.2 ] ++ direct-filter-coeff: [ 0.125, 0.3125, 0.125, 0.3125, 0.125 ] ++ dog-kernel: [ 0, -0.25, -0.375, -0.25, 0, -0.25, 0.25, 0.5625, 0.25, -0.25, -0.375, 0.5625, 0.25, 0.5625, -0.375, -0.25, 0.25, 0.5625, 0.25, -0.25, 0, -0.25, -0.375, -0.25, 0 ] ++ - iso: 51200 ++ edge-thed: 32 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 1 ++ global-alpha: 1 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-clip-neg: [ 3, 4, 4, 5, 6, 5, 5, 4 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0.125, 0.3125, 0.125, 0.3125, 0.125 ] ++ dog-kernel: [ 0, -0.25, -0.375, -0.25, 0, -0.25, 0.25, 0.5625, 0.25, -0.25, -0.375, 0.5625, 0.25, 0.5625, -0.375, -0.25, 0.25, 0.5625, 0.25, -0.25, 0, -0.25, -0.375, -0.25, 0 ] ++ - iso: 102400 ++ edge-thed: 30 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 1 ++ global-alpha: 1 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 0, 2, 2, 3, 3, 3, 2, 2 ] ++ dog-clip-neg: [ 0, 2, 2, 3, 3, 3, 2, 2 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0, 0.4375, 0.125, 0.4375, 0 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - iso: 204800 ++ edge-thed: 30 ++ src-wgt: 0 ++ alpha-adp-en: 0 ++ local-alpha: 1 ++ global-alpha: 1 ++ noise-clip: [ 4, 8, 16, 24, 24, 16, 16, 16 ] ++ dog-clip-pos: [ 0, 2, 2, 3, 3, 3, 2, 2 ] ++ dog-clip-neg: [ 0, 2, 2, 3, 3, 3, 2, 2 ] ++ dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] ++ direct-filter-coeff: [ 0, 0.4375, 0.125, 0.4375, 0 ] ++ dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] + - Awb: + algorithm: "grey" + AwbMode: diff --git a/multimedia/libcamera/patches/240-libcamera-controls-add-the-rkisp2-lens-controls.patch b/multimedia/libcamera/patches/240-libcamera-controls-add-the-rkisp2-lens-controls.patch new file mode 100644 index 0000000000..c5d360aca1 --- /dev/null +++ b/multimedia/libcamera/patches/240-libcamera-controls-add-the-rkisp2-lens-controls.patch @@ -0,0 +1,129 @@ +From 93a0352d855aa2f5578c83c1265021f9e7ca60ef Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 12:59:52 +0000 +Subject: [PATCH 240/243] libcamera: controls: Add the rkisp2 lens controls + +The RkISP2 ISPP corrects lens distortion through a mesh generated from a +lens model, and the model differs from one board to the next. Add rkisp2 +vendor controls for the lens model (OCamCalib or OpenCV fisheye), its +optical centre, distortion coefficients and focal length, and the +strength of the correction, in a new vendor range. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + include/libcamera/meson.build | 1 + + src/libcamera/control_ids_rkisp2.yaml | 84 +++++++++++++++++++++++++++ + src/libcamera/control_ranges.yaml | 4 +- + 3 files changed, 88 insertions(+), 1 deletion(-) + create mode 100644 src/libcamera/control_ids_rkisp2.yaml + +--- a/include/libcamera/meson.build ++++ b/include/libcamera/meson.build +@@ -37,6 +37,7 @@ controls_map = { + 'core': 'control_ids_core.yaml', + 'debug': 'control_ids_debug.yaml', + 'draft': 'control_ids_draft.yaml', ++ 'rkisp2': 'control_ids_rkisp2.yaml', + 'rpi/pisp': 'control_ids_rpi.yaml', + 'rpi/vc4': 'control_ids_rpi.yaml', + }, +--- /dev/null ++++ b/src/libcamera/control_ids_rkisp2.yaml +@@ -0,0 +1,84 @@ ++# SPDX-License-Identifier: LGPL-2.1-or-later ++# ++# Copyright (C) 2026, Daniel Golle ++# ++%YAML 1.1 ++--- ++# Rockchip RkISP2 (RV1126 ISP and ISPP) specific vendor controls ++vendor: rkisp2 ++controls: ++ - LensModel: ++ type: int32_t ++ direction: inout ++ description: | ++ Lens distortion model corrected by the ISPP FEC. ++ ++ The model parameters are given by LensCentre, LensCoefficients and ++ LensFocalLength, the strength of the correction by ++ LensCorrectionLevel. ++ ++ The FEC is the slowest stage of the ISPP: it samples the picture at ++ about 134 megapixels per second. With a model other than ++ LensModelNone a 3856x2192 stream runs at about 15 frames per second; ++ a 1920x1080 picture takes about 16 ms and a 2688x1520 picture about ++ 30 ms in the FEC. ++ ++ \sa LensCentre LensCoefficients LensFocalLength LensCorrectionLevel ++ enum: ++ - name: LensModelNone ++ value: 0 ++ description: No lens correction. ++ - name: LensModelOcamCalib ++ value: 1 ++ description: | ++ Omnidirectional camera model of the OCamCalib toolbox ++ (Scaramuzza): the ray of a pixel at distance rho from the centre ++ has the axial component a0 + a2 rho^2 + a3 rho^3 + a4 rho^4. ++ LensCoefficients holds a0, a2, a3 and a4. ++ - name: LensModelFisheye ++ value: 2 ++ description: | ++ Fisheye model of OpenCV: a ray at angle theta from the axis lands ++ at theta (1 + k1 theta^2 + k2 theta^4 + k3 theta^6 + k4 theta^8) ++ times the focal length from the centre. LensCoefficients holds ++ k1 to k4, LensFocalLength the focal length. ++ ++ - LensCentre: ++ type: float ++ direction: inout ++ size: [2] ++ description: | ++ Optical centre of the lens in pixels of the full sensor picture, as ++ x and y. ++ ++ \sa LensModel ++ ++ - LensCoefficients: ++ type: float ++ direction: inout ++ size: [4] ++ description: | ++ Distortion coefficients of the lens model selected by LensModel. ++ ++ \sa LensModel ++ ++ - LensFocalLength: ++ type: float ++ direction: inout ++ size: [2] ++ description: | ++ Focal length of the lens in pixels, horizontal and vertical, for the ++ fisheye model. ++ ++ \sa LensModel ++ ++ - LensCorrectionLevel: ++ type: int32_t ++ direction: inout ++ description: | ++ Strength of the lens correction from 0 (none) to 255 (full). The ++ sampled source position moves linearly from the uncorrected to the ++ fully corrected one. ++ ++ \sa LensModel ++... +--- a/src/libcamera/control_ranges.yaml ++++ b/src/libcamera/control_ranges.yaml +@@ -15,6 +15,8 @@ ranges: + rpi: 20000 + # Controls for debug metadata + debug: 30000 +- # Next range starts at 40000 ++ # Rockchip RkISP2 vendor controls ++ rkisp2: 40000 ++ # Next range starts at 50000 + + ... diff --git a/multimedia/libcamera/patches/241-ipa-rkisp2-add-the-lens-distortion-correction.patch b/multimedia/libcamera/patches/241-ipa-rkisp2-add-the-lens-distortion-correction.patch new file mode 100644 index 0000000000..cf31218db2 --- /dev/null +++ b/multimedia/libcamera/patches/241-ipa-rkisp2-add-the-lens-distortion-correction.patch @@ -0,0 +1,818 @@ +From 894032dfd6d345948bc6e5f1c1cd490c7aa0bfed Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 12:59:52 +0000 +Subject: [PATCH 241/243] ipa: rkisp2: Add the lens distortion correction + +The ISPP FEC stage resamples the picture through a mesh of source +positions loaded on its mesh device, which no algorithm generated. + +Add a DistortionCorrection algorithm that generates the mesh from the +OCamCalib or OpenCV fisheye model given by the tuning file or the +rkisp2 lens controls, with an output focal length that keeps the image +edge nearest to the optical centre so that no blank area appears, and +enables the FEC stage while a model is set. The level blends linearly +between the uncorrected and the fully corrected source position. The +IPA fills a mesh buffer on request from the pipeline handler, and +reports lens control changes so that the pipeline handler can have a +new mesh computed. The FEC block moves out of the static Ispp +parameters. + +For the OCamCalib lens of the vendor IQ files the generated mesh is +within 1.2 pixels of the one of the Rockchip mesh generator. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + include/libcamera/ipa/rkisp2.mojom | 5 + + src/ipa/rkisp2/algorithms/algorithm.h | 19 ++ + src/ipa/rkisp2/algorithms/fec.cpp | 273 +++++++++++++++++++++++++ + src/ipa/rkisp2/algorithms/fec.h | 44 ++++ + src/ipa/rkisp2/algorithms/fec_mesh.cpp | 193 +++++++++++++++++ + src/ipa/rkisp2/algorithms/fec_mesh.h | 52 +++++ + src/ipa/rkisp2/algorithms/ispp.cpp | 24 +-- + src/ipa/rkisp2/algorithms/ispp.h | 1 - + src/ipa/rkisp2/algorithms/meson.build | 2 + + src/ipa/rkisp2/ipa_context.h | 4 + + src/ipa/rkisp2/rkisp2.cpp | 34 +++ + 11 files changed, 631 insertions(+), 20 deletions(-) + create mode 100644 src/ipa/rkisp2/algorithms/fec.cpp + create mode 100644 src/ipa/rkisp2/algorithms/fec.h + create mode 100644 src/ipa/rkisp2/algorithms/fec_mesh.cpp + create mode 100644 src/ipa/rkisp2/algorithms/fec_mesh.h + +--- a/include/libcamera/ipa/rkisp2.mojom ++++ b/include/libcamera/ipa/rkisp2.mojom +@@ -37,6 +37,9 @@ interface IPARkISP2Interface { + [async] computeParams(uint32 frame, uint32 bufferId); + [async] computeIsppParams(uint32 frame, uint32 bufferId); + primeIsppParams(uint32 frame, uint32 bufferId) => (uint32 bytesused); ++ primeFecMesh(uint32 bufferId, uint32 width, uint32 height, ++ libcamera.ControlList controls) => (uint32 bytesused); ++ [async] computeFecMesh(uint32 bufferId, uint32 width, uint32 height); + [async] processStats(uint32 frame, uint32 bufferId, + libcamera.ControlList sensorControls); + }; +@@ -46,5 +49,7 @@ interface IPARkISP2EventInterface { + isppParamsComputed(uint32 frame, uint32 bufferId, uint32 bytesused); + setSensorControls(uint32 frame, libcamera.ControlList sensorControls); + metadataReady(libcamera.ControlList metadata); ++ fecMeshChanged(); ++ fecMeshComputed(uint32 bufferId, uint32 bytesused); + }; + +--- a/src/ipa/rkisp2/algorithms/algorithm.h ++++ b/src/ipa/rkisp2/algorithms/algorithm.h +@@ -7,8 +7,15 @@ + + #pragma once + ++#include ++#include ++ + #include + ++#include ++ ++#include ++ + #include + + #include "module.h" +@@ -29,6 +36,18 @@ public: + [[maybe_unused]] RkISPPParams *params) + { + } ++ ++ /* ++ * Fill a FEC mesh buffer after applying the controls; return the bytes ++ * used, 0 without a mesh. ++ */ ++ virtual size_t computeFecMesh([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] Span buffer, ++ [[maybe_unused]] const Size &size, ++ [[maybe_unused]] const ControlList &controls) ++ { ++ return 0; ++ } + }; + + } /* namespace ipa::rkisp2 */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/fec.cpp +@@ -0,0 +1,273 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 lens distortion correction ++ */ ++ ++#include "fec.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++ ++#include ++ ++#include ++ ++#include "libcamera/internal/value_node.h" ++ ++#include "fec_mesh.h" ++ ++/** ++ * \file fec.h ++ */ ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++/** ++ * \class DistortionCorrection ++ * \brief RkISP2 lens distortion correction by the ISPP FEC ++ * ++ * The ISPP FEC stage resamples the picture through a mesh that holds, for a ++ * grid over the output, the source position to sample. The algorithm ++ * generates the mesh from a lens model, the OCamCalib omnidirectional model ++ * or the OpenCV fisheye model, and the pipeline handler loads it on the mesh ++ * device before streaming starts and whenever the lens controls change it. ++ * The output is a pinhole view whose focal length keeps the image edge ++ * nearest to the optical centre, so that the corrected picture has no blank ++ * area. ++ * ++ * \code{.unparsed} ++ * algorithms: ++ * - DistortionCorrection: ++ * model: ocamcalib ++ * centre: [ 1956.39, 1140.64 ] ++ * coefficients: [ -2819.41, 3.161e-5, 6.884e-8, -1.307e-11 ] ++ * level: 255 ++ * \endcode ++ * ++ * The fisheye model takes k1 to k4 as coefficients and a focal-length list ++ * of the horizontal and vertical focal length in pixels. The rkisp2 ++ * LensModel, LensCentre, LensCoefficients, LensFocalLength and ++ * LensCorrectionLevel controls override the tuning file. ++ */ ++ ++LOG_DEFINE_CATEGORY(RkISP2Fec) ++ ++namespace { ++ ++constexpr int32_t kLevelMax = 255; ++constexpr size_t kTableAlign = 16; ++ ++size_t alignTable(size_t offset) ++{ ++ return (offset + kTableAlign - 1) / kTableAlign * kTableAlign; ++} ++ ++template ++bool readList(const ValueNode &node, const char *key, std::array &out) ++{ ++ std::optional> v = node[key].get>(); ++ ++ if (!v || v->size() != N) { ++ LOG(RkISP2Fec, Error) << key << " needs " << N << " values"; ++ return false; ++ } ++ ++ std::copy(v->begin(), v->end(), out.begin()); ++ return true; ++} ++ ++template ++bool update(std::array &dst, const std::optional> &src) ++{ ++ if (!src || std::equal(dst.begin(), dst.end(), src->begin())) ++ return false; ++ ++ std::copy(src->begin(), src->end(), dst.begin()); ++ return true; ++} ++ ++template ++bool update(T &dst, const std::optional &src) ++{ ++ if (!src || dst == *src) ++ return false; ++ ++ dst = *src; ++ return true; ++} ++ ++std::array toDouble(const std::array &v) ++{ ++ return { v[0], v[1], v[2], v[3] }; ++} ++ ++} /* namespace */ ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::init ++ */ ++int DistortionCorrection::init(IPAContext &context, const ValueNode &tuningData) ++{ ++ std::string model = tuningData["model"].get("none"); ++ constexpr float kMax = std::numeric_limits::max(); ++ constexpr float kMin = std::numeric_limits::lowest(); ++ ++ centre_ = {}; ++ coefficients_ = {}; ++ focal_ = {}; ++ level_ = std::clamp(tuningData["level"].get(kLevelMax), 0, kLevelMax); ++ ++ if (model == "ocamcalib") { ++ model_ = controls::rkisp2::LensModelOcamCalib; ++ } else if (model == "fisheye") { ++ model_ = controls::rkisp2::LensModelFisheye; ++ if (!readList(tuningData, "focal-length", focal_)) ++ return -EINVAL; ++ } else if (model == "none") { ++ model_ = controls::rkisp2::LensModelNone; ++ } else { ++ LOG(RkISP2Fec, Error) << "Unknown lens model " << model; ++ return -EINVAL; ++ } ++ ++ if (model_ != controls::rkisp2::LensModelNone && ++ (!readList(tuningData, "centre", centre_) || ++ !readList(tuningData, "coefficients", coefficients_))) ++ return -EINVAL; ++ ++ context.ctrlMap[&controls::rkisp2::LensModel] = ++ ControlInfo(controls::rkisp2::LensModelValues, ControlValue(model_)); ++ context.ctrlMap[&controls::rkisp2::LensCentre] = ControlInfo(kMin, kMax); ++ context.ctrlMap[&controls::rkisp2::LensCoefficients] = ControlInfo(kMin, kMax); ++ context.ctrlMap[&controls::rkisp2::LensFocalLength] = ControlInfo(kMin, kMax); ++ context.ctrlMap[&controls::rkisp2::LensCorrectionLevel] = ++ ControlInfo(0, kLevelMax, level_); ++ ++ return 0; ++} ++ ++bool DistortionCorrection::applyControls(const ControlList &controls) ++{ ++ const auto &level = controls.get(controls::rkisp2::LensCorrectionLevel); ++ bool changed = false; ++ ++ changed |= update(model_, controls.get(controls::rkisp2::LensModel)); ++ changed |= update(centre_, controls.get(controls::rkisp2::LensCentre)); ++ changed |= update(coefficients_, ++ controls.get(controls::rkisp2::LensCoefficients)); ++ changed |= update(focal_, controls.get(controls::rkisp2::LensFocalLength)); ++ if (level) ++ changed |= update(level_, std::optional( ++ std::clamp(*level, 0, kLevelMax))); ++ ++ return changed; ++} ++ ++/** ++ * \copydoc libcamera::ipa::Algorithm::queueRequest ++ */ ++void DistortionCorrection::queueRequest(IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ [[maybe_unused]] IPAFrameContext &frameContext, ++ const ControlList &controls) ++{ ++ if (applyControls(controls)) ++ context.activeState.fec.meshChanged = true; ++} ++ ++/** ++ * \brief Enable the FEC stage while a lens model is set ++ * \param[in] context The shared IPA context ++ * \param[in] frame The ISPP frame number ++ * \param[out] params The ISPP parameters buffer ++ */ ++void DistortionCorrection::prepareIspp([[maybe_unused]] IPAContext &context, ++ [[maybe_unused]] const uint32_t frame, ++ RkISPPParams *params) ++{ ++ auto block = params->block(); ++ ++ block.setEnabled(model_ != controls::rkisp2::LensModelNone); ++} ++ ++/** ++ * \brief Generate the FEC mesh for a picture size ++ * \param[in] context The shared IPA context ++ * \param[out] buffer The mesh buffer of the mesh device ++ * \param[in] size The ISPP output size ++ * \param[in] controls The lens controls to apply first ++ * ++ * \return The number of bytes used, or 0 without a lens model ++ */ ++size_t DistortionCorrection::computeFecMesh([[maybe_unused]] IPAContext &context, ++ Span buffer, const Size &size, ++ const ControlList &controls) ++{ ++ struct rkispp_fec_mesh header = {}; ++ std::array coefficients; ++ fecmesh::Mesh mesh; ++ size_t n, end; ++ ++ applyControls(controls); ++ coefficients = toDouble(coefficients_); ++ ++ if (model_ == controls::rkisp2::LensModelOcamCalib) { ++ fecmesh::OcamLens lens{ centre_[0], centre_[1], coefficients }; ++ ++ mesh = fecmesh::generate(lens, size.width, size.height, level_); ++ } else if (model_ == controls::rkisp2::LensModelFisheye) { ++ fecmesh::FisheyeLens lens{ focal_[0], focal_[1], centre_[0], ++ centre_[1], coefficients }; ++ ++ mesh = fecmesh::generate(lens, size.width, size.height, level_); ++ } else { ++ return 0; ++ } ++ ++ n = mesh.xi.size(); ++ header.width = size.width; ++ header.height = size.height; ++ header.density = RKISPP_FEC_MESH_COARSE; ++ header.mesh_size = n; ++ header.xint_offset = alignTable(sizeof(header)); ++ header.xfra_offset = alignTable(header.xint_offset + 2 * n); ++ header.yint_offset = alignTable(header.xfra_offset + n); ++ header.yfra_offset = alignTable(header.yint_offset + 2 * n); ++ end = header.yfra_offset + n; ++ ++ if (end > buffer.size()) { ++ LOG(RkISP2Fec, Error) ++ << "Mesh of " << end << " bytes exceeds the buffer of " ++ << buffer.size(); ++ return 0; ++ } ++ ++ memcpy(buffer.data(), &header, sizeof(header)); ++ memcpy(buffer.data() + header.xint_offset, mesh.xi.data(), 2 * n); ++ memcpy(buffer.data() + header.xfra_offset, mesh.xf.data(), n); ++ memcpy(buffer.data() + header.yint_offset, mesh.yi.data(), 2 * n); ++ memcpy(buffer.data() + header.yfra_offset, mesh.yf.data(), n); ++ ++ LOG(RkISP2Fec, Debug) ++ << "Mesh for " << size << ", model " << model_ << ", level " ++ << level_ << ", " << n << " vertices"; ++ ++ return end; ++} ++ ++REGISTER_IPA_ALGORITHM(DistortionCorrection, "DistortionCorrection") ++ ++} /* namespace ipa::rkisp2::algorithms */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/fec.h +@@ -0,0 +1,44 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISP2 lens distortion correction ++ */ ++ ++#pragma once ++ ++#include ++ ++#include "algorithm.h" ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::algorithms { ++ ++class DistortionCorrection : public Algorithm ++{ ++public: ++ DistortionCorrection() = default; ++ ~DistortionCorrection() = default; ++ ++ int init(IPAContext &context, const ValueNode &tuningData) override; ++ void queueRequest(IPAContext &context, const uint32_t frame, ++ IPAFrameContext &frameContext, ++ const ControlList &controls) override; ++ void prepareIspp(IPAContext &context, const uint32_t frame, ++ RkISPPParams *params) override; ++ size_t computeFecMesh(IPAContext &context, Span buffer, ++ const Size &size, const ControlList &controls) override; ++ ++private: ++ bool applyControls(const ControlList &controls); ++ ++ int32_t model_; ++ std::array centre_; ++ std::array coefficients_; ++ std::array focal_; ++ int32_t level_; ++}; ++ ++} /* namespace ipa::rkisp2::algorithms */ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/fec_mesh.cpp +@@ -0,0 +1,193 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISPP FEC correction mesh generator ++ */ ++ ++#include "fec_mesh.h" ++ ++#include ++#include ++#include ++#include ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::fecmesh { ++ ++namespace { ++ ++/* coarse mesh geometry of cal_fec_mesh() in the ISPP driver */ ++constexpr unsigned int kAlign = 32; ++constexpr unsigned int kStepX = 32; ++constexpr unsigned int kStepY = 16; ++constexpr unsigned int kStrip = 128; ++ ++constexpr unsigned int kLevelMax = 255; ++constexpr int kFracBits = 8; ++ ++/* the engine stalls on source positions 0 and w - 1 or beyond */ ++constexpr double kClampLow = 1.0; ++constexpr double kClampHighMargin = 3.0; ++ ++/* halves the bracket each step: 64 steps reach double precision */ ++constexpr unsigned int kBisectSteps = 64; ++ ++/* output focal search bracket, natural log around the edge distance */ ++constexpr double kFocalLogRange = 10.0; ++ ++using SourceMap = std::function(double, double)>; ++ ++unsigned int alignUp(unsigned int v) ++{ ++ return (v + kAlign - 1) / kAlign * kAlign; ++} ++ ++/* Output positions of the vertices, in the order the engine reads them */ ++std::vector> ++vertices(unsigned int width, unsigned int height) ++{ ++ unsigned int w = alignUp(width), h = alignUp(height); ++ unsigned int cols = w / kStepX + 1; ++ unsigned int strips = (h + kStrip - 1) / kStrip; ++ unsigned int lastRows = ((h % kStrip) ? h % kStrip : kStrip) / kStepY + 1; ++ std::vector> out; ++ ++ for (unsigned int s = 0; s < strips; s++) { ++ unsigned int rows = s + 1 < strips ? kStrip / kStepY + 1 : lastRows; ++ ++ for (unsigned int c = 0; c < cols; c++) ++ for (unsigned int r = 0; r < rows; r++) ++ out.emplace_back(c * kStepX, s * kStrip + r * kStepY); ++ } ++ ++ return out; ++} ++ ++/* Find x in [lo, hi] with f(x) = target for f increasing */ ++double bisect(const std::function &f, double target, ++ double lo, double hi) ++{ ++ for (unsigned int i = 0; i < kBisectSteps; i++) { ++ double mid = (lo + hi) / 2; ++ ++ if (f(mid) < target) ++ lo = mid; ++ else ++ hi = mid; ++ } ++ ++ return (lo + hi) / 2; ++} ++ ++double nearestEdge(double cx, double cy, unsigned int width, unsigned int height) ++{ ++ return std::min({ cx, width - 1 - cx, cy, height - 1 - cy }); ++} ++ ++void encode(double p, double max, uint16_t &integer, uint8_t &fraction) ++{ ++ long fixed; ++ ++ p = std::clamp(p, kClampLow, max); ++ fixed = std::lround(p * (1 << kFracBits)); ++ integer = fixed >> kFracBits; ++ fraction = fixed & ((1 << kFracBits) - 1); ++} ++ ++Mesh build(const SourceMap &map, unsigned int width, unsigned int height, ++ unsigned int level) ++{ ++ double k = static_cast(std::min(level, kLevelMax)) / kLevelMax; ++ auto order = vertices(width, height); ++ Mesh mesh; ++ ++ mesh.width = width; ++ mesh.height = height; ++ mesh.xi.resize(order.size()); ++ mesh.xf.resize(order.size()); ++ mesh.yi.resize(order.size()); ++ mesh.yf.resize(order.size()); ++ ++ for (size_t n = 0; n < order.size(); n++) { ++ double u = order[n].first, v = order[n].second; ++ auto [x, y] = map(u, v); ++ ++ encode(u + (x - u) * k, width - kClampHighMargin, ++ mesh.xi[n], mesh.xf[n]); ++ encode(v + (y - v) * k, height - kClampHighMargin, ++ mesh.yi[n], mesh.yf[n]); ++ } ++ ++ return mesh; ++} ++ ++} /* namespace */ ++ ++unsigned int meshSize(unsigned int width, unsigned int height) ++{ ++ return vertices(width, height).size(); ++} ++ ++/* ++ * The output is a pinhole view whose focal length maps the image edge nearest ++ * to the centre onto itself, so that the corrected picture has no blank area. ++ */ ++Mesh generate(const OcamLens &lens, unsigned int width, unsigned int height, ++ unsigned int level) ++{ ++ const auto &a = lens.a; ++ auto z = [&a](double r) { ++ return a[0] + a[1] * r * r + a[2] * r * r * r + a[3] * r * r * r * r; ++ }; ++ double focal = -z(nearestEdge(lens.cx, lens.cy, width, height)); ++ double diag = std::hypot(width, height); ++ ++ SourceMap map = [&](double u, double v) { ++ double ro = std::hypot(u - lens.cx, v - lens.cy); ++ double rs, s; ++ ++ if (ro == 0.0) ++ return std::make_pair(u, v); ++ ++ rs = bisect([&](double r) { return focal * r / -z(r); }, ro, 0.0, diag); ++ s = rs / ro; ++ return std::make_pair(lens.cx + (u - lens.cx) * s, ++ lens.cy + (v - lens.cy) * s); ++ }; ++ ++ return build(map, width, height, level); ++} ++ ++Mesh generate(const FisheyeLens &lens, unsigned int width, unsigned int height, ++ unsigned int level) ++{ ++ const auto &k = lens.k; ++ auto distort = [&k](double theta) { ++ double t2 = theta * theta; ++ ++ return theta * (1 + t2 * (k[0] + t2 * (k[1] + t2 * (k[2] + t2 * k[3])))); ++ }; ++ double edge = nearestEdge(lens.cx, lens.cy, width, height); ++ /* source radius of the edge point decreases as the output focal grows */ ++ double focal = std::exp(bisect( ++ [&](double lf) { return -lens.fx * distort(std::atan(edge / std::exp(lf))); }, ++ -edge, std::log(edge) - kFocalLogRange, ++ std::log(edge) + kFocalLogRange)); ++ ++ SourceMap map = [&](double u, double v) { ++ double a = (u - lens.cx) / focal, b = (v - lens.cy) / focal; ++ double r = std::hypot(a, b); ++ double s = r > 0.0 ? distort(std::atan(r)) / r : 1.0; ++ ++ return std::make_pair(lens.cx + lens.fx * a * s, ++ lens.cy + lens.fy * b * s); ++ }; ++ ++ return build(map, width, height, level); ++} ++ ++} /* namespace ipa::rkisp2::fecmesh */ ++ ++} /* namespace libcamera */ +--- /dev/null ++++ b/src/ipa/rkisp2/algorithms/fec_mesh.h +@@ -0,0 +1,52 @@ ++/* SPDX-License-Identifier: LGPL-2.1-or-later */ ++/* ++ * Copyright (C) 2026, Daniel Golle ++ * ++ * RkISPP FEC correction mesh generator ++ */ ++ ++#pragma once ++ ++#include ++#include ++#include ++ ++namespace libcamera { ++ ++namespace ipa::rkisp2::fecmesh { ++ ++/* OCamCalib (Scaramuzza) model: z(rho) = a0 + a2 rho^2 + a3 rho^3 + a4 rho^4 */ ++struct OcamLens { ++ double cx; ++ double cy; ++ std::array a; ++}; ++ ++/* OpenCV fisheye model: theta_d = theta (1 + k1 t^2 + k2 t^4 + k3 t^6 + k4 t^8) */ ++struct FisheyeLens { ++ double fx; ++ double fy; ++ double cx; ++ double cy; ++ std::array k; ++}; ++ ++struct Mesh { ++ unsigned int width; ++ unsigned int height; ++ std::vector xi; ++ std::vector xf; ++ std::vector yi; ++ std::vector yf; ++}; ++ ++unsigned int meshSize(unsigned int width, unsigned int height); ++ ++Mesh generate(const OcamLens &lens, unsigned int width, unsigned int height, ++ unsigned int level); ++Mesh generate(const FisheyeLens &lens, unsigned int width, unsigned int height, ++ unsigned int level); ++ ++} /* namespace ipa::rkisp2::fecmesh */ ++ ++} /* namespace libcamera */ +--- a/src/ipa/rkisp2/algorithms/ispp.cpp ++++ b/src/ipa/rkisp2/algorithms/ispp.cpp +@@ -32,9 +32,9 @@ namespace ipa::rkisp2::algorithms { + * \class Ispp + * \brief RkISPP module parameters from the tuning file + * +- * The ISPP post-processor (FEC lens distortion correction and ORB feature +- * detection) takes its parameters through its own params node; the temporal +- * and spatial noise reduction and the sharpening have algorithms of their ++ * The ISPP post-processor (ORB feature detection) takes its parameters ++ * through its own params node; the temporal and spatial noise reduction, the ++ * sharpening and the lens distortion correction have algorithms of their + * own. This algorithm sends the modules configured in the tuning file with + * every frame, each tuning dictionary filling the fields of the corresponding + * UAPI block by name: +@@ -42,8 +42,8 @@ namespace ipa::rkisp2::algorithms { + * \code{.unparsed} + * algorithms: + * - Ispp: +- * Fec: +- * crop_en: 0 ++ * Orb: ++ * limit_value: 0 + * \endcode + * + * A module absent from the tuning file is not sent and keeps the driver's +@@ -86,12 +86,6 @@ constexpr bool fieldSigned() + fieldSize(), \ + fieldSigned() } + +-constexpr Field fecFields[] = { +- ISPP_FIELD(rkispp_params_fec, crop_en), +- ISPP_FIELD(rkispp_params_fec, crop_width), +- ISPP_FIELD(rkispp_params_fec, crop_height), +-}; +- + constexpr Field orbFields[] = { + ISPP_FIELD(rkispp_params_orb, limit_value), + ISPP_FIELD(rkispp_params_orb, max_feature), +@@ -212,12 +206,6 @@ void sendBlock(V4L2ParamsBlock block, + */ + int Ispp::init([[maybe_unused]] IPAContext &context, const ValueNode &tuningData) + { +- int ret; +- +- ret = loadBlock(tuningData, "Fec", fecFields, fec_); +- if (ret) +- return ret; +- + return loadBlock(tuningData, "Orb", orbFields, orb_); + } + +@@ -231,8 +219,6 @@ void Ispp::prepareIspp([[maybe_unused]] + [[maybe_unused]] const uint32_t frame, + RkISPPParams *params) + { +- if (fec_) +- sendBlock(params->block(), *fec_); + if (orb_) + sendBlock(params->block(), *orb_); + } +--- a/src/ipa/rkisp2/algorithms/ispp.h ++++ b/src/ipa/rkisp2/algorithms/ispp.h +@@ -28,7 +28,6 @@ public: + RkISPPParams *params) override; + + private: +- std::optional fec_; + std::optional orb_; + }; + +--- a/src/ipa/rkisp2/algorithms/meson.build ++++ b/src/ipa/rkisp2/algorithms/meson.build +@@ -10,6 +10,8 @@ rkisp2_ipa_algorithms = files([ + 'crop.cpp', + 'csm.cpp', + 'dpcc.cpp', ++ 'fec.cpp', ++ 'fec_mesh.cpp', + 'goc.cpp', + 'ispp.cpp', + 'lsc.cpp', +--- a/src/ipa/rkisp2/ipa_context.h ++++ b/src/ipa/rkisp2/ipa_context.h +@@ -150,6 +150,10 @@ struct IPAActiveState { + double ratio; + bool localGain; + } noiseGain; ++ ++ struct { ++ bool meshChanged; ++ } fec; + }; + + struct IPAFrameContext : public FrameContext { +--- a/src/ipa/rkisp2/rkisp2.cpp ++++ b/src/ipa/rkisp2/rkisp2.cpp +@@ -68,6 +68,11 @@ public: + void computeParams(const uint32_t frame, const uint32_t bufferId) override; + void computeIsppParams(const uint32_t frame, const uint32_t bufferId) override; + uint32_t primeIsppParams(const uint32_t frame, const uint32_t bufferId) override; ++ uint32_t primeFecMesh(const uint32_t bufferId, const uint32_t width, ++ const uint32_t height, ++ const ControlList &controls) override; ++ void computeFecMesh(const uint32_t bufferId, const uint32_t width, ++ const uint32_t height) override; + void initializeFrameContext(IPAFrameContext &frameContext, + const ControlList &controls); + void processStats(const uint32_t frame, const uint32_t bufferId, +@@ -277,6 +282,35 @@ void IPARkISP2::queueRequest(const uint3 + context_.debugMetadata.enableByControl(controls); + + context_.frameContexts.getOrInitContext(frame, controls); ++ ++ if (context_.activeState.fec.meshChanged) { ++ context_.activeState.fec.meshChanged = false; ++ fecMeshChanged.emit(); ++ } ++} ++ ++void IPARkISP2::computeFecMesh(const uint32_t bufferId, const uint32_t width, ++ const uint32_t height) ++{ ++ fecMeshComputed.emit(bufferId, ++ primeFecMesh(bufferId, width, height, ControlList())); ++} ++ ++uint32_t IPARkISP2::primeFecMesh(const uint32_t bufferId, const uint32_t width, ++ const uint32_t height, const ControlList &controls) ++{ ++ Span buffer = mappedBuffers_.at(bufferId).planes()[0]; ++ Size size(width, height); ++ ++ for (const auto &a : algorithms()) { ++ Algorithm *algo = static_cast(a.get()); ++ size_t bytes = algo->computeFecMesh(context_, buffer, size, controls); ++ ++ if (bytes) ++ return bytes; ++ } ++ ++ return 0; + } + + void IPARkISP2::initializeFrameContext(IPAFrameContext &frameContext, diff --git a/multimedia/libcamera/patches/242-pipeline-rkisp2-load-the-fec-mesh.patch b/multimedia/libcamera/patches/242-pipeline-rkisp2-load-the-fec-mesh.patch new file mode 100644 index 0000000000..fce9aa13fc --- /dev/null +++ b/multimedia/libcamera/patches/242-pipeline-rkisp2-load-the-fec-mesh.patch @@ -0,0 +1,347 @@ +From 921b9d3b3a76c5daa6d67f0d9c86f036089addf8 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 12:59:52 +0000 +Subject: [PATCH 242/243] pipeline: rkisp2: Load the FEC mesh + +The ISPP driver enables its FEC stage only with a correction mesh +loaded on the rkispp_fec_mesh device when streaming starts. + +Open the mesh device where the ISPP has one, share two mesh buffers +with the IPA, have the IPA fill one with the start controls applied +and queue it before the ISPP starts, and queue a regenerated mesh +whenever the IPA reports a lens control change. A lens model set only +after the start takes effect at the next start. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/libcamera/pipeline/rkisp2/rkisp2.cpp | 173 ++++++++++++++++++++++- + 1 file changed, 170 insertions(+), 3 deletions(-) + +--- a/src/libcamera/pipeline/rkisp2/rkisp2.cpp ++++ b/src/libcamera/pipeline/rkisp2/rkisp2.cpp +@@ -6,6 +6,7 @@ + */ + + #include ++#include + #include + #include + #include +@@ -149,7 +150,7 @@ public: + V4L2VideoDevice *mainPath, V4L2VideoDevice *param, + V4L2VideoDevice *stat, V4L2Subdevice *isppSubdev, + V4L2VideoDevice *isppMain, V4L2VideoDevice *isppScale, +- V4L2VideoDevice *isppParam); ++ V4L2VideoDevice *isppParam, V4L2VideoDevice *isppMesh); + PixelFormat getSensorFormat(unsigned int mbusCode, const Size &size, + const Size &maxSize); + +@@ -160,6 +161,7 @@ public: + void computeParamBuffers(unsigned int frame); + void computeIsppParamBuffer(FrameBuffer *buffer); + int primeIsppParamBuffer(FrameBuffer *buffer); ++ int primeFecMesh(const ControlList *controls); + + void tryCompleteRequest(Request *request, FrameBuffer *buffer); + void tryCompleteRequest(const ControlList &metadata); +@@ -171,11 +173,14 @@ public: + void isppScaleBufferReady(FrameBuffer *buffer); + void statBufferReady(FrameBuffer *buffer); + void isppParamBufferReady(FrameBuffer *buffer); ++ void isppMeshBufferReady(FrameBuffer *buffer); + + void paramsComputed(unsigned int frame, unsigned int bufferId, unsigned int bytesused); + void isppParamsComputed(unsigned int frame, unsigned int bufferId, unsigned int bytesused); + void setSensorControls(unsigned int frame, const ControlList &sensorControls); + void metadataReady(const ControlList &metadata); ++ void fecMeshChanged(); ++ void fecMeshComputed(unsigned int bufferId, unsigned int bytesused); + + /* These are the buffers that go between the vicap and the isp */ + std::vector> internalBuffers_; +@@ -190,6 +195,15 @@ public: + std::vector> isppParamBuffers_; + unsigned int isppFrame_; + ++ /* ++ * The FEC mesh is loaded before streaming starts and replaced when the ++ * lens controls change; the driver returns a buffer once it holds a copy. ++ */ ++ std::vector> isppMeshBuffers_; ++ std::vector isppMeshFree_; ++ Size isppMeshSize_; ++ bool isppMeshActive_; ++ + std::unique_ptr delayedCtrls_; + + /* +@@ -227,6 +241,7 @@ public: + V4L2VideoDevice *isppMain_; + V4L2VideoDevice *isppScale_; + V4L2VideoDevice *isppParam_; ++ V4L2VideoDevice *isppMesh_; + Stream stream_; + Stream scaleStream_; + bool scaleActive_; +@@ -278,6 +293,8 @@ namespace { + * pairs) ensures that the pipeline runs smoothly, without frame drops. + */ + static constexpr unsigned int kRkISP2MinBufferCount = 4; ++/* One mesh loaded in the driver and one being regenerated */ ++static constexpr unsigned int kRkISP2FecMeshBufferCount = 2; + + /* + * A request completes a frame after the VICAP capture, the readback and the +@@ -366,6 +383,7 @@ private: + std::unique_ptr isppMain_; + std::unique_ptr isppScale_; + std::unique_ptr isppParam_; ++ std::unique_ptr isppMesh_; + }; + + int RkISP2CameraData::init(bool usingIsp, CameraSensor *sensor, +@@ -374,7 +392,7 @@ int RkISP2CameraData::init(bool usingIsp + V4L2VideoDevice *mainPath, V4L2VideoDevice *param, + V4L2VideoDevice *stat, V4L2Subdevice *isppSubdev, + V4L2VideoDevice *isppMain, V4L2VideoDevice *isppScale, +- V4L2VideoDevice *isppParam) ++ V4L2VideoDevice *isppParam, V4L2VideoDevice *isppMesh) + { + usingIsp_ = usingIsp; + usingIspp_ = usingIsp && isppMain; +@@ -389,6 +407,8 @@ int RkISP2CameraData::init(bool usingIsp + isppMain_ = isppMain; + isppScale_ = isppScale; + isppParam_ = isppParam; ++ isppMesh_ = isppMesh; ++ isppMeshActive_ = false; + isppFrame_ = 0; + scaleActive_ = false; + +@@ -416,6 +436,8 @@ int RkISP2CameraData::init(bool usingIsp + if (isppScale_) + isppScale_->bufferReady.connect(this, &RkISP2CameraData::isppScaleBufferReady); + isppParam_->bufferReady.connect(this, &RkISP2CameraData::isppParamBufferReady); ++ if (isppMesh_) ++ isppMesh_->bufferReady.connect(this, &RkISP2CameraData::isppMeshBufferReady); + } + + return 0; +@@ -456,6 +478,8 @@ int RkISP2CameraData::loadIPA() + ipa_->isppParamsComputed.connect(this, &RkISP2CameraData::isppParamsComputed); + ipa_->setSensorControls.connect(this, &RkISP2CameraData::setSensorControls); + ipa_->metadataReady.connect(this, &RkISP2CameraData::metadataReady); ++ ipa_->fecMeshChanged.connect(this, &RkISP2CameraData::fecMeshChanged); ++ ipa_->fecMeshComputed.connect(this, &RkISP2CameraData::fecMeshComputed); + + /* The IPA tuning file is made from the sensor name. */ + std::string ipaTuningFile = +@@ -548,6 +572,113 @@ void RkISP2CameraData::isppParamBufferRe + computeIsppParamBuffer(buffer); + } + ++/* ++ * The driver enables the FEC stage only with a mesh loaded at stream start, ++ * so the start controls apply to it and a lens model set later takes effect ++ * at the next start. ++ */ ++int RkISP2CameraData::primeFecMesh(const ControlList *controls) ++{ ++ static const std::array lensControls = { ++ &controls::rkisp2::LensModel, ++ &controls::rkisp2::LensCentre, ++ &controls::rkisp2::LensCoefficients, ++ &controls::rkisp2::LensFocalLength, ++ &controls::rkisp2::LensCorrectionLevel, ++ }; ++ ControlList lens(controls::controls); ++ FrameBuffer *buffer; ++ unsigned int bytesused; ++ int ret; ++ ++ for (const ControlId *id : lensControls) { ++ if (controls && controls->contains(id->id())) ++ lens.set(id->id(), controls->get(id->id())); ++ } ++ ++ isppMeshActive_ = false; ++ isppMeshFree_.clear(); ++ for (const std::unique_ptr &b : isppMeshBuffers_) ++ isppMeshFree_.push_back(b.get()); ++ ++ if (!isppMesh_ || isppMeshFree_.empty()) ++ return 0; ++ ++ buffer = isppMeshFree_.back(); ++ bytesused = ipa_->primeFecMesh(buffer->cookie(), isppMeshSize_.width, ++ isppMeshSize_.height, lens); ++ if (!bytesused) ++ return 0; ++ ++ ret = isppMesh_->streamOn(); ++ if (ret) ++ return ret; ++ ++ buffer->_d()->metadata().planes()[0].bytesused = bytesused; ++ ret = isppMesh_->queueBuffer(buffer); ++ if (ret) { ++ isppMesh_->streamOff(); ++ return ret; ++ } ++ ++ isppMeshFree_.pop_back(); ++ isppMeshActive_ = true; ++ ++ return 0; ++} ++ ++void RkISP2CameraData::isppMeshBufferReady(FrameBuffer *buffer) ++{ ++ isppMeshFree_.push_back(buffer); ++} ++ ++void RkISP2CameraData::fecMeshChanged() ++{ ++ FrameBuffer *buffer; ++ ++ if (!isppMeshActive_) { ++ LOG(RkISP2, Warning) << "Lens correction changes apply at the next start"; ++ return; ++ } ++ ++ if (isppMeshFree_.empty()) { ++ LOG(RkISP2, Warning) << "No free FEC mesh buffer"; ++ return; ++ } ++ ++ buffer = isppMeshFree_.back(); ++ isppMeshFree_.pop_back(); ++ ipa_->computeFecMesh(buffer->cookie(), isppMeshSize_.width, ++ isppMeshSize_.height); ++} ++ ++void RkISP2CameraData::fecMeshComputed(unsigned int bufferId, ++ unsigned int bytesused) ++{ ++ auto it = std::find_if(isppMeshBuffers_.begin(), isppMeshBuffers_.end(), ++ [bufferId](const std::unique_ptr &b) { ++ return b->cookie() == bufferId; ++ }); ++ FrameBuffer *buffer; ++ int ret; ++ ++ if (it == isppMeshBuffers_.end()) ++ return; ++ ++ buffer = it->get(); ++ if (!bytesused) { ++ isppMeshFree_.push_back(buffer); ++ return; ++ } ++ ++ buffer->_d()->metadata().planes()[0].bytesused = bytesused; ++ ret = isppMesh_->queueBuffer(buffer); ++ if (ret < 0) { ++ LOG(RkISP2, Error) << "Failed to queue the FEC mesh: " << strerror(-ret); ++ isppMeshFree_.push_back(buffer); ++ } ++} ++ + void RkISP2CameraData::vicapBufferReady(FrameBuffer *buffer) + { + Request *request = buffer->request(); +@@ -1247,6 +1378,7 @@ int PipelineHandlerRkISP2::configureIspp + return ret; + + LOG(RkISP2, Debug) << "ISPP input: " << isppFormat; ++ data->isppMeshSize_ = isppFormat.size; + + V4L2DeviceFormat outputFormat; + outputFormat.fourcc = mainPathFormat(isppMain_.get(), cfg.pixelFormat); +@@ -1427,6 +1559,17 @@ int PipelineHandlerRkISP2::startIspp(Cam + } + actions += [&]() { data->param_->streamOff(); }; + ++ /* The FEC block of the ISPP parameters takes effect only with a mesh */ ++ ret = data->primeFecMesh(controls); ++ if (ret) { ++ LOG(RkISP2, Error) << "Failed to load the FEC mesh " << camera->id(); ++ return ret; ++ } ++ actions += [&]() { ++ if (data->isppMeshActive_) ++ data->isppMesh_->streamOff(); ++ }; ++ + for (const std::unique_ptr &buffer : data->isppParamBuffers_) { + ret = data->primeIsppParamBuffer(buffer.get()); + if (ret < 0) { +@@ -1522,6 +1665,10 @@ void PipelineHandlerRkISP2::stopIspp(Cam + data->stat_->releaseBuffers(); + + data->isppParam_->streamOff(); ++ if (data->isppMeshActive_) { ++ data->isppMesh_->streamOff(); ++ data->isppMeshActive_ = false; ++ } + data->param_->streamOff(); + + data->statBuffers_.clear(); +@@ -1782,7 +1929,7 @@ bool PipelineHandlerRkISP2::createCamera + if (data->init(usingIsp, sensor_.get(), video_.get(), rawrd_.get(), + isp_.get(), mainPath_.get(), param_.get(), stat_.get(), + isppSubdev_.get(), isppMain_.get(), isppScale_.get(), +- isppParam_.get())) { ++ isppParam_.get(), isppMesh_.get())) { + LOG(RkISP2, Error) << "Failed to initialize data"; + return false; + } +@@ -2011,6 +2158,11 @@ bool PipelineHandlerRkISP2::match(Device + return false; + } + ++ /* Without the mesh device the FEC stage stays off. */ ++ isppMesh_ = V4L2VideoDevice::fromEntityName(isppMedia_.get(), "rkispp_fec_mesh"); ++ if (isppMesh_ && isppMesh_->open() < 0) ++ isppMesh_.reset(); ++ + /* The first scaler output carries a second, scaled stream. */ + isppScale_ = V4L2VideoDevice::fromEntityName(isppMedia_.get(), "rkispp_scale0"); + if (isppScale_ && isppScale_->open() < 0) { +@@ -2065,6 +2217,15 @@ int PipelineHandlerRkISP2::allocateBuffe + return ret; + + actions += [&]() { data->isppParam_->releaseBuffers(); }; ++ ++ if (data->isppMesh_) { ++ ret = data->isppMesh_->allocateBuffers(kRkISP2FecMeshBufferCount, ++ &data->isppMeshBuffers_); ++ if (ret < 0) ++ return ret; ++ ++ actions += [&]() { data->isppMesh_->releaseBuffers(); }; ++ } + } + + auto pushBuffers = [&](const std::vector> &buffers) { +@@ -2081,6 +2242,7 @@ int PipelineHandlerRkISP2::allocateBuffe + pushBuffers(data->paramQueue_->buffers()); + pushBuffers(data->statBuffers_); + pushBuffers(data->isppParamBuffers_); ++ pushBuffers(data->isppMeshBuffers_); + + data->ipa_->mapBuffers(data->ipaBuffers_); + +@@ -2104,6 +2266,11 @@ int PipelineHandlerRkISP2::freeBuffers(C + if (data->usingIspp_) { + data->isppParamBuffers_.clear(); + data->isppParam_->releaseBuffers(); ++ if (data->isppMesh_) { ++ data->isppMeshFree_.clear(); ++ data->isppMeshBuffers_.clear(); ++ data->isppMesh_->releaseBuffers(); ++ } + } + + return 0; diff --git a/multimedia/libcamera/patches/243-ipa-rkisp2-imx415-add-the-lens-distortion-correction.patch b/multimedia/libcamera/patches/243-ipa-rkisp2-imx415-add-the-lens-distortion-correction.patch new file mode 100644 index 0000000000..565cd7dd31 --- /dev/null +++ b/multimedia/libcamera/patches/243-ipa-rkisp2-imx415-add-the-lens-distortion-correction.patch @@ -0,0 +1,28 @@ +From 9d5497cc9e8027aa8825dd184f2a7775f4f69180 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 12:59:52 +0000 +Subject: [PATCH 243/243] ipa: rkisp2: imx415: Add the lens distortion + correction + +The lens of an IMX415 module depends on the board, so enable the +DistortionCorrection algorithm without a lens model: the rkisp2 lens +controls set the model at start. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/data/imx415.yaml | 3 +++ + 1 file changed, 3 insertions(+) + +--- a/src/ipa/rkisp2/data/imx415.yaml ++++ b/src/ipa/rkisp2/data/imx415.yaml +@@ -1359,6 +1359,9 @@ algorithms: + dog-alpha: [ 0, 0, 0, 0, 0, 0, 0, 0 ] + direct-filter-coeff: [ 0, 0.4375, 0.125, 0.4375, 0 ] + dog-kernel: [ -0.0625, -0.109375, -0.140625, -0.109375, -0.0625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.140625, 0.234375, 0.4375, 0.234375, -0.140625, -0.109375, 0.078125, 0.234375, 0.078125, -0.109375, -0.0625, -0.109375, -0.140625, -0.109375, -0.0625 ] ++ - DistortionCorrection: ++ # the lens depends on the board; it comes with the rkisp2 Lens controls ++ model: none + - Awb: + algorithm: "grey" + AwbMode: diff --git a/multimedia/libcamera/patches/244-pipeline-rkisp2-take-the-vicap-raw-frames-as-16-bit-bayer.patch b/multimedia/libcamera/patches/244-pipeline-rkisp2-take-the-vicap-raw-frames-as-16-bit-bayer.patch new file mode 100644 index 0000000000..b4d34b192b --- /dev/null +++ b/multimedia/libcamera/patches/244-pipeline-rkisp2-take-the-vicap-raw-frames-as-16-bit-bayer.patch @@ -0,0 +1,146 @@ +From ff46b61f2014a691d0086639e53ecaea3fbe97bc Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Thu, 5 Nov 2026 15:57:02 +0000 +Subject: [PATCH] pipeline: rkisp2: Take the VICAP raw frames as 16-bit Bayer + +The VICAP writes unpacked RAW10 and RAW12 samples MSB-justified in +16-bit words, the layout the ISP readback takes, and its video node +advertises that layout as the 16-bit Bayer formats. + +Offer the raw stream in the 16-bit format of the sensor's Bayer order, +pick the deepest sensor code behind it unless a sensor configuration +names the depth, and configure the VICAP node with it. The readback +node keeps the format of the sensor's sample depth. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/libcamera/pipeline/rkisp2/rkisp2.cpp | 77 +++++++++++++++++++++--- + 1 file changed, 67 insertions(+), 10 deletions(-) + +--- a/src/libcamera/pipeline/rkisp2/rkisp2.cpp ++++ b/src/libcamera/pipeline/rkisp2/rkisp2.cpp +@@ -25,6 +25,7 @@ + #include + #include + ++#include "libcamera/internal/bayer_format.h" + #include "libcamera/internal/buffer_queue.h" + #include "libcamera/internal/camera.h" + #include "libcamera/internal/camera_manager.h" +@@ -76,6 +77,48 @@ const std::map ra + + LOG_DEFINE_CATEGORY(RkISP2) + ++/* ++ * The VICAP writes unpacked samples MSB-justified in 16-bit words, the layout ++ * the ISP readback takes. ++ */ ++static PixelFormat vicapRawFormat(unsigned int mbusCode) ++{ ++ BayerFormat bayer = BayerFormat::fromMbusCode(mbusCode); ++ ++ if (bayer.bitDepth > 8) ++ bayer.bitDepth = 16; ++ ++ return bayer.toPixelFormat(); ++} ++ ++static unsigned int rawMbusCode(const CameraSensor *sensor, ++ const PixelFormat &pixelFormat, ++ unsigned int bitDepth) ++{ ++ const std::vector codes = sensor->mbusCodes(); ++ unsigned int mbusCode = 0; ++ unsigned int depth = 0; ++ ++ for (const auto &value : rawFormats) { ++ unsigned int code = value.second; ++ const BayerFormat &bayer = BayerFormat::fromMbusCode(code); ++ ++ if (std::find(codes.begin(), codes.end(), code) == codes.end()) ++ continue; ++ if (pixelFormat.isValid() && vicapRawFormat(code) != pixelFormat) ++ continue; ++ if (bitDepth && bayer.bitDepth != bitDepth) ++ continue; ++ if (bayer.bitDepth <= depth) ++ continue; ++ ++ mbusCode = code; ++ depth = bayer.bitDepth; ++ } ++ ++ return mbusCode; ++} ++ + static V4L2PixelFormat mainPathFormat(V4L2VideoDevice *video, + const PixelFormat &pixelFormat) + { +@@ -452,10 +495,10 @@ PixelFormat RkISP2CameraData::getSensorF + const auto &ret = std::find_if(rawFormats.begin(), rawFormats.end(), + [format](const auto &value) { return value.second == format.code; }); + if (ret != rawFormats.end()) +- return (*ret).first; ++ return vicapRawFormat(format.code); + + LOG(RkISP2, Error) << "No raw format supported by sensor"; +- return formats::SRGGB10; ++ return formats::SRGGB16; + } + + PipelineHandlerRkISP2 *RkISP2CameraData::pipe() +@@ -928,12 +971,21 @@ CameraConfiguration::Status RkISP2Camera + const Size &maxSize = usingIsp ? ispMaxSize : vicapMaxSize; + + if (!usingIsp) { +- if (!rawFormats.count(cfg.pixelFormat)) { +- cfg.pixelFormat = formats::SRGGB10; ++ unsigned int bitDepth = sensorConfig ? sensorConfig->bitDepth : 0; ++ unsigned int mbusCode = rawMbusCode(sensor, cfg.pixelFormat, ++ bitDepth); ++ ++ if (!mbusCode) { ++ mbusCode = rawMbusCode(sensor, PixelFormat(), bitDepth); ++ if (!mbusCode) { ++ LOG(RkISP2, Error) << "No raw format supported by sensor"; ++ return Invalid; ++ } ++ ++ cfg.pixelFormat = vicapRawFormat(mbusCode); + status = Adjusted; + } + +- unsigned int mbusCode = rawFormats.at(cfg.pixelFormat); + auto sizes = sensor->sizes(mbusCode); + + Size bestSize; +@@ -1154,9 +1206,8 @@ PipelineHandlerRkISP2::generateConfigura + std::transform(formatToMediaBus.begin(), formatToMediaBus.end(), + std::inserter(streamFormats, streamFormats.end()), + makeStream); +- std::transform(rawFormats.begin(), rawFormats.end(), +- std::inserter(streamFormats, streamFormats.end()), +- makeStream); ++ for (const auto &value : rawFormats) ++ streamFormats[vicapRawFormat(value.second)] = sizes; + + StreamFormats formats(streamFormats); + StreamConfiguration cfg(formats); +@@ -1273,8 +1324,14 @@ int PipelineHandlerRkISP2::configure(Cam + * \todo Figure out if these should go in the pipeline handler + * or camera data + */ +- LOG(RkISP2, Debug) << "Configuring rawrd0 with: " << vicapOutputFormat; +- ret = rawrd_->setFormat(&vicapOutputFormat); ++ V4L2DeviceFormat rawrdFormat; ++ PixelFormat rawrdPixelFormat = ++ BayerFormat::fromMbusCode(format.code).toPixelFormat(); ++ rawrdFormat.fourcc = rawrd_->toV4L2PixelFormat(rawrdPixelFormat); ++ rawrdFormat.size = format.size; ++ ++ LOG(RkISP2, Debug) << "Configuring rawrd0 with: " << rawrdFormat; ++ ret = rawrd_->setFormat(&rawrdFormat); + if (ret) + return ret; + diff --git a/multimedia/libcamera/patches/245-ipa-rkisp2-ccm-program-the-matrix-relative-to-the-identity.patch b/multimedia/libcamera/patches/245-ipa-rkisp2-ccm-program-the-matrix-relative-to-the-identity.patch new file mode 100644 index 0000000000..037f1732ea --- /dev/null +++ b/multimedia/libcamera/patches/245-ipa-rkisp2-ccm-program-the-matrix-relative-to-the-identity.patch @@ -0,0 +1,39 @@ +From 71832a3f09cfe3152f8e8c80b1f893239f50ebe0 Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Fri, 6 Nov 2026 00:19:06 +0000 +Subject: [PATCH] ipa: rkisp2: ccm: Program the matrix relative to the identity + +The RkISP2 colour correction adds the identity matrix to the programmed +coefficients, so the diagonal registers hold the coefficient minus one, +as the Rockchip AIQ isp20 CCM configuration writes them. Commit eec470d +("ipa: rkisp2: algo: ccm: Implement color correction matrix") writes the +full matrix, which applies M + I: grey doubles in brightness and colours +reach half the calibrated saturation. Subtract the identity before +quantising the coefficients. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/algorithms/ccm.cpp | 9 ++++++--- + 1 file changed, 6 insertions(+), 3 deletions(-) + +--- a/src/ipa/rkisp2/algorithms/ccm.cpp ++++ b/src/ipa/rkisp2/algorithms/ccm.cpp +@@ -70,11 +70,14 @@ void Ccm::setParameters(RkISP2Params *pa + + /* + * 4 bit integer and 7 bit fractional, ranging from -8 (0x400) to +- * +7.9921875 (0x3ff) ++ * +7.9921875 (0x3ff); the hardware adds the identity matrix + */ + for (unsigned int i = 0; i < 3; i++) { +- for (unsigned int j = 0; j < 3; j++) +- config->coeff[i][j] = Q<4, 7>(matrix[i][j]).quantized(); ++ for (unsigned int j = 0; j < 3; j++) { ++ float coeff = matrix[i][j] - (i == j ? 1.0f : 0.0f); ++ ++ config->coeff[i][j] = Q<4, 7>(coeff).quantized(); ++ } + } + + for (unsigned int i = 0; i < 3; i++) diff --git a/multimedia/libcamera/patches/246-ipa-rkisp2-agc-measure-the-histogram-over-the-whole-frame.patch b/multimedia/libcamera/patches/246-ipa-rkisp2-agc-measure-the-histogram-over-the-whole-frame.patch new file mode 100644 index 0000000000..9563a65901 --- /dev/null +++ b/multimedia/libcamera/patches/246-ipa-rkisp2-agc-measure-the-histogram-over-the-whole-frame.patch @@ -0,0 +1,52 @@ +From fc938914c104ee551daf39cea027f760d8b6113d Mon Sep 17 00:00:00 2001 +From: Daniel Golle +Date: Fri, 6 Nov 2026 15:09:43 +0000 +Subject: [PATCH] ipa: rkisp2: agc: Measure the histogram over the whole frame + +The big histogram is configured for a 5x5 grid (window_num 0), but it +is given the block size of a 15x15 grid. The hardware takes the window +size as the size of one block, as the Rockchip ISP2x driver programs it +in isp_rawhstbig_config(), so the histogram covers only the top-left +ninth of the frame. The histogram constraint then misses highlights +elsewhere in the picture and holds the exposure up in scenes that +clip. Use the 5x5 measurement window that the AE-lite grid already +uses, and drop the 15x15 window that nothing else reads. + +Assisted-by: LLM +Signed-off-by: Daniel Golle +--- + src/ipa/rkisp2/algorithms/agc.cpp | 5 +---- + src/ipa/rkisp2/ipa_context.h | 1 - + 2 files changed, 1 insertion(+), 5 deletions(-) + +--- a/src/ipa/rkisp2/algorithms/agc.cpp ++++ b/src/ipa/rkisp2/algorithms/agc.cpp +@@ -84,9 +84,6 @@ int Agc::configure(IPAContext &context, + */ + context.configuration.agc.measureWindow.v_size = (configInfo.outputSize.height / 5) - 2; + +- context.configuration.agc.measureWindow15.h_size = (configInfo.outputSize.width / 15); +- context.configuration.agc.measureWindow15.v_size = (configInfo.outputSize.height / 15) - 2; +- + /* Configure the default exposure and gain. */ + context.activeState.agc.automatic.gain = context.configuration.sensor.minAnalogueGain; + context.activeState.agc.automatic.exposure = +@@ -283,7 +280,7 @@ void Agc::prepare(IPAContext &context, [ + hstConfig->coeffs.g = 0x86; + hstConfig->coeffs.b = 0x0d; + +- hstConfig->meas_window = context.configuration.agc.measureWindow15; ++ hstConfig->meas_window = context.configuration.agc.measureWindow; + + /* \todo Support configuring the weights */ + for (size_t i = 0; i < RKISP2_ISP_HIST_WEIGHT_GRIDS_SIZE_BIG; i++) +--- a/src/ipa/rkisp2/ipa_context.h ++++ b/src/ipa/rkisp2/ipa_context.h +@@ -58,7 +58,6 @@ struct RKISP2AwbSession { + struct IPASessionConfiguration { + struct { + struct rkisp2_isp_window measureWindow; +- struct rkisp2_isp_window measureWindow15; + } agc; + + struct RKISP2AwbSession awb;