/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2021-2022, Ideas On Board * * AWB control algorithm */ #include "awb.h" #include #include #include #include #include #include "libipa/colours.h" /** * \file awb.h */ namespace libcamera { namespace ipa::rkisp1::algorithms { /** * \class Awb * \brief A Grey world white balance correction algorithm */ LOG_DEFINE_CATEGORY(RkISP1Awb) /* Minimum mean value below which AWB can't operate. */ constexpr double kMeanMinThreshold = 2.0; Awb::Awb() : rgbMode_(false) { } /** * \copydoc libcamera::ipa::Algorithm::configure */ int Awb::configure(IPAContext &context, const IPACameraSensorInfo &configInfo) { context.activeState.awb.gains.manual.red = 1.0; context.activeState.awb.gains.manual.blue = 1.0; context.activeState.awb.gains.manual.green = 1.0; context.activeState.awb.gains.automatic.red = 1.0; context.activeState.awb.gains.automatic.blue = 1.0; context.activeState.awb.gains.automatic.green = 1.0; context.activeState.awb.autoEnabled = true; /* * Define the measurement window for AWB as a centered rectangle * covering 3/4 of the image width and height. */ context.configuration.awb.measureWindow.h_offs = configInfo.outputSize.width / 8; context.configuration.awb.measureWindow.v_offs = configInfo.outputSize.height / 8; context.configuration.awb.measureWindow.h_size = 3 * configInfo.outputSize.width / 4; context.configuration.awb.measureWindow.v_size = 3 * configInfo.outputSize.height / 4; context.configuration.awb.enabled = true; return 0; } /** * \copydoc libcamera::ipa::Algorithm::queueRequest */ void Awb::queueRequest(IPAContext &context, [[maybe_unused]] const uint32_t frame, IPAFrameContext &frameContext, const ControlList &controls) { auto &awb = context.activeState.awb; const auto &awbEnable = controls.get(controls::AwbEnable); if (awbEnable && *awbEnable != awb.autoEnabled) { awb.autoEnabled = *awbEnable; LOG(RkISP1Awb, Debug) << (*awbEnable ? "Enabling" : "Disabling") << " AWB"; } const auto &colourGains = controls.get(controls::ColourGains); if (colourGains && !awb.autoEnabled) { awb.gains.manual.red = (*colourGains)[0]; awb.gains.manual.blue = (*colourGains)[1]; LOG(RkISP1Awb, Debug) << "Set colour gains to red: " << awb.gains.manual.red << ", blue: " << awb.gains.manual.blue; } frameContext.awb.autoEnabled = awb.autoEnabled; if (!awb.autoEnabled) { frameContext.awb.gains.red = awb.gains.manual.red; frameContext.awb.gains.green = 1.0; frameContext.awb.gains.blue = awb.gains.manual.blue; } } /** * \copydoc libcamera::ipa::Algorithm::prepare */ void Awb::prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, RkISP1Params *params) { /* * This is the latest time we can read the active state. This is the * most up-to-date automatic values we can read. */ if (frameContext.awb.autoEnabled) { frameContext.awb.gains.red = context.activeState.awb.gains.automatic.red; frameContext.awb.gains.green = context.activeState.awb.gains.automatic.green; frameContext.awb.gains.blue = context.activeState.awb.gains.automatic.blue; } auto gainConfig = params->block(); gainConfig.setEnabled(true); gainConfig->gain_green_b = std::clamp(256 * frameContext.awb.gains.green, 0, 0x3ff); gainConfig->gain_blue = std::clamp(256 * frameContext.awb.gains.blue, 0, 0x3ff); gainConfig->gain_red = std::clamp(256 * frameContext.awb.gains.red, 0, 0x3ff); gainConfig->gain_green_r = std::clamp(256 * frameContext.awb.gains.green, 0, 0x3ff); /* If we have already set the AWB measurement parameters, return. */ if (frame > 0) return; auto awbConfig = params->block(); awbConfig.setEnabled(true); /* Configure the measure window for AWB. */ awbConfig->awb_wnd = context.configuration.awb.measureWindow; /* Number of frames to use to estimate the means (0 means 1 frame). */ awbConfig->frames = 0; /* Select RGB or YCbCr means measurement. */ if (rgbMode_) { awbConfig->awb_mode = RKISP1_CIF_ISP_AWB_MODE_RGB; /* * For RGB-based measurements, pixels are selected with maximum * red, green and blue thresholds that are set in the * awb_ref_cr, awb_min_y and awb_ref_cb respectively. The other * values are not used, set them to 0. */ awbConfig->awb_ref_cr = 250; awbConfig->min_y = 250; awbConfig->awb_ref_cb = 250; awbConfig->max_y = 0; awbConfig->min_c = 0; awbConfig->max_csum = 0; } else { awbConfig->awb_mode = RKISP1_CIF_ISP_AWB_MODE_YCBCR; /* Set the reference Cr and Cb (AWB target) to white. */ awbConfig->awb_ref_cb = 128; awbConfig->awb_ref_cr = 128; /* * Filter out pixels based on luminance and chrominance values. * The acceptable luma values are specified as a [16, 250] * range, while the acceptable chroma values are specified with * a minimum of 16 and a maximum Cb+Cr sum of 250. */ awbConfig->min_y = 16; awbConfig->max_y = 250; awbConfig->min_c = 16; awbConfig->max_csum = 250; } } /** * \copydoc libcamera::ipa::Algorithm::process */ void Awb::process(IPAContext &context, [[maybe_unused]] const uint32_t frame, IPAFrameContext &frameContext, const rkisp1_stat_buffer *stats, ControlList &metadata) { const rkisp1_cif_isp_stat *params = &stats->params; const rkisp1_cif_isp_awb_stat *awb = ¶ms->awb; IPAActiveState &activeState = context.activeState; RGB rgbMeans; metadata.set(controls::AwbEnable, frameContext.awb.autoEnabled); metadata.set(controls::ColourGains, { static_cast(frameContext.awb.gains.red), static_cast(frameContext.awb.gains.blue) }); metadata.set(controls::ColourTemperature, activeState.awb.temperatureK); if (!stats || !(stats->meas_type & RKISP1_CIF_ISP_STAT_AWB)) { LOG(RkISP1Awb, Error) << "AWB data is missing in statistics"; return; } if (rgbMode_) { rgbMeans = {{ static_cast(awb->awb_mean[0].mean_y_or_g), static_cast(awb->awb_mean[0].mean_cr_or_r), static_cast(awb->awb_mean[0].mean_cb_or_b) }}; } else { /* Get the YCbCr mean values */ double yMean = awb->awb_mean[0].mean_y_or_g; double cbMean = awb->awb_mean[0].mean_cb_or_b; double crMean = awb->awb_mean[0].mean_cr_or_r; /* * Convert from YCbCr to RGB. * The hardware uses the following formulas: * Y = 16 + 0.2500 R + 0.5000 G + 0.1094 B * Cb = 128 - 0.1406 R - 0.2969 G + 0.4375 B * Cr = 128 + 0.4375 R - 0.3750 G - 0.0625 B * * The inverse matrix is thus: * [[1,1636, -0,0623, 1,6008] * [1,1636, -0,4045, -0,7949] * [1,1636, 1,9912, -0,0250]] */ yMean -= 16; cbMean -= 128; crMean -= 128; rgbMeans = {{ 1.1636 * yMean - 0.0623 * cbMean + 1.6008 * crMean, 1.1636 * yMean - 0.4045 * cbMean - 0.7949 * crMean, 1.1636 * yMean + 1.9912 * cbMean - 0.0250 * crMean }}; /* * Due to hardware rounding errors in the YCbCr means, the * calculated RGB means may be negative. This would lead to * negative gains, messing up calculation. Prevent this by * clamping the means to positive values. */ rgbMeans = rgbMeans.max(0.0); } /* * The ISP computes the AWB means after applying the colour gains, * divide by the gains that were used to get the raw means from the * sensor. */ RGB gains{{ frameContext.awb.gains.red, frameContext.awb.gains.green, frameContext.awb.gains.blue }}; rgbMeans /= gains; /* * If the means are too small we don't have enough information to * meaningfully calculate gains. Freeze the algorithm in that case. */ if (rgbMeans.r() < kMeanMinThreshold && rgbMeans.g() < kMeanMinThreshold && rgbMeans.b() < kMeanMinThreshold) return; activeState.awb.temperatureK = estimateCCT(rgbMeans); /* Metadata shall contain the up to date measurement */ metadata.set(controls::ColourTemperature, activeState.awb.temperatureK); /* * Estimate the red and blue gains to apply in a grey world. The green * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the * divisor to a minimum value of 1.0. */ double redGain = rgbMeans.g() / std::max(rgbMeans.r(), 1.0); double blueGain = rgbMeans.g() / std::max(rgbMeans.b(), 1.0); /* * Clamp the gain values to the hardware, which expresses gains as Q2.8 * unsigned integer values. Set the minimum just above zero to avoid * divisions by zero when computing the raw means in subsequent * iterations. */ redGain = std::clamp(redGain, 1.0 / 256, 1023.0 / 256); blueGain = std::clamp(blueGain, 1.0 / 256, 1023.0 / 256); /* Filter the values to avoid oscillations. */ double speed = 0.2; redGain = speed * redGain + (1 - speed) * activeState.awb.gains.automatic.red; blueGain = speed * blueGain + (1 - speed) * activeState.awb.gains.automatic.blue; activeState.awb.gains.automatic.red = redGain; activeState.awb.gains.automatic.blue = blueGain; activeState.awb.gains.automatic.green = 1.0; LOG(RkISP1Awb, Debug) << std::showpoint << "Means " << rgbMeans << ", gains [" << activeState.awb.gains.automatic.red << ", " << activeState.awb.gains.automatic.green << ", " << activeState.awb.gains.automatic.blue << "], temp " << activeState.awb.temperatureK << "K"; } REGISTER_IPA_ALGORITHM(Awb, "Awb") } /* namespace ipa::rkisp1::algorithms */ } /* namespace libcamera */