libcamera/src/ipa/rkisp1/algorithms/awb.cpp
Laurent Pinchart 128f22bce5 ipa: rkisp1: awb: Store per-frame information in frame context
Rework the algorithm's usage of the active state and frame context to
store data in the right place.

The active state stores two distinct categories of information:

- The consolidated value of all algorithm controls. Requests passed to
  the queueRequest() function store values for controls that the
  application wants to modify for that particular frame, and the
  queueRequest() function updates the active state with those values.
  The active state thus contains a consolidated view of the value of all
  controls handled by the algorithm.

- The value of parameters computed by the algorithm when running in auto
  mode. Algorithms running in auto mode compute new parameters every
  time statistics buffers are received (either synchronously, or
  possibly in a background thread). The latest computed value of those
  parameters is stored in the active state in the process() function.

The frame context also stores two categories of information:

- The value of the controls to be applied to the frame. These values are
  typically set in the queueRequest() function, from the consolidated
  control values stored in the active state. The frame context thus
  stores values for all controls related to the algorithm, not limited
  to the controls specified in the corresponding request, but
  consolidated from all requests that have been queued so far.

  For controls that can be specified manually or computed by the
  algorithm depending on the operation mode (such as the colour gains),
  the control value will be stored in the frame context in
  queueRequest() only when operating in manual mode. When operating in
  auto mode, the values are computed by the algorithm and stored in the
  frame context in prepare(), just before being stored in the ISP
  parameters buffer.

  The queueRequest() function can also store ancillary data in the frame
  context, such as flags to indicate if (and what) control values have
  changed compared to the previous request.

- Status information computed by the algorithm for a frame. For
  instance, the colour temperature estimated by the algorithm from ISP
  statistics calculated on a frame is stored in the frame context for
  that frame in the process() function.

The active state and frame context thus both contain identical members
for most control values, but store values that have a different meaning.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
2022-09-28 05:41:14 +03:00

237 lines
7.7 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2021-2022, Ideas On Board
*
* awb.cpp - AWB control algorithm
*/
#include "awb.h"
#include <algorithm>
#include <cmath>
#include <libcamera/base/log.h>
#include <libcamera/control_ids.h>
#include <libcamera/ipa/core_ipa_interface.h>
/**
* \file awb.h
*/
namespace libcamera {
namespace ipa::rkisp1::algorithms {
/**
* \class Awb
* \brief A Grey world white balance correction algorithm
*/
LOG_DEFINE_CATEGORY(RkISP1Awb)
/**
* \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;
}
uint32_t Awb::estimateCCT(double red, double green, double blue)
{
/* Convert the RGB values to CIE tristimulus values (XYZ) */
double X = (-0.14282) * (red) + (1.54924) * (green) + (-0.95641) * (blue);
double Y = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);
double Z = (-0.68202) * (red) + (0.77073) * (green) + (0.56332) * (blue);
/* Calculate the normalized chromaticity values */
double x = X / (X + Y + Z);
double y = Y / (X + Y + Z);
/* Calculate CCT */
double n = (x - 0.3320) / (0.1858 - y);
return 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;
}
/**
* \copydoc libcamera::ipa::Algorithm::prepare
*/
void Awb::prepare(IPAContext &context, const uint32_t frame,
IPAFrameContext &frameContext, rkisp1_params_cfg *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;
}
params->others.awb_gain_config.gain_green_b = 256 * frameContext.awb.gains.green;
params->others.awb_gain_config.gain_blue = 256 * frameContext.awb.gains.blue;
params->others.awb_gain_config.gain_red = 256 * frameContext.awb.gains.red;
params->others.awb_gain_config.gain_green_r = 256 * frameContext.awb.gains.green;
/* Update the gains. */
params->module_cfg_update |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
/* If we already have configured the gains and window, return. */
if (frame > 0)
return;
/* Configure the gains to apply. */
params->module_en_update |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
/* Update the ISP to apply the gains configured. */
params->module_ens |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
/* Configure the measure window for AWB. */
params->meas.awb_meas_config.awb_wnd = context.configuration.awb.measureWindow;
/*
* Measure Y, Cr and Cb means.
* \todo RGB is not working, the kernel seems to not configure it ?
*/
params->meas.awb_meas_config.awb_mode = RKISP1_CIF_ISP_AWB_MODE_YCBCR;
/* Reference Cr and Cb. */
params->meas.awb_meas_config.awb_ref_cb = 128;
params->meas.awb_meas_config.awb_ref_cr = 128;
/* Y values to include are between min_y and max_y only. */
params->meas.awb_meas_config.min_y = 16;
params->meas.awb_meas_config.max_y = 250;
/* Maximum Cr+Cb value to take into account for awb. */
params->meas.awb_meas_config.max_csum = 250;
/* Minimum Cr and Cb values to take into account. */
params->meas.awb_meas_config.min_c = 16;
/* Number of frames to use to estimate the mean (0 means 1 frame). */
params->meas.awb_meas_config.frames = 0;
/* Update AWB measurement unit configuration. */
params->module_cfg_update |= RKISP1_CIF_ISP_MODULE_AWB;
/* Make sure the ISP is measuring the means for the next frame. */
params->module_en_update |= RKISP1_CIF_ISP_MODULE_AWB;
params->module_ens |= RKISP1_CIF_ISP_MODULE_AWB;
}
/**
* \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::process
*/
void Awb::process(IPAContext &context,
[[maybe_unused]] const uint32_t frame,
IPAFrameContext &frameContext,
const rkisp1_stat_buffer *stats)
{
const rkisp1_cif_isp_stat *params = &stats->params;
const rkisp1_cif_isp_awb_stat *awb = &params->awb;
IPAActiveState &activeState = context.activeState;
/* Get the YCbCr mean values */
double yMean = awb->awb_mean[0].mean_y_or_g;
double crMean = awb->awb_mean[0].mean_cr_or_r;
double cbMean = awb->awb_mean[0].mean_cb_or_b;
/*
* 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;
double redMean = 1.1636 * yMean - 0.0623 * cbMean + 1.6008 * crMean;
double greenMean = 1.1636 * yMean - 0.4045 * cbMean - 0.7949 * crMean;
double blueMean = 1.1636 * yMean + 1.9912 * cbMean - 0.0250 * crMean;
frameContext.awb.temperatureK = estimateCCT(redMean, greenMean, blueMean);
/* Estimate the red and blue gains to apply in a grey world. */
double redGain = greenMean / (redMean + 1);
double blueGain = greenMean / (blueMean + 1);
/* 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;
/*
* Gain values are unsigned integer value, range 0 to 4 with 8 bit
* fractional part. Hardcode the green gain to 1.0.
*/
activeState.awb.gains.automatic.red = std::clamp(redGain, 0.0, 1023.0 / 256);
activeState.awb.gains.automatic.blue = std::clamp(blueGain, 0.0, 1023.0 / 256);
activeState.awb.gains.automatic.green = 1.0;
LOG(RkISP1Awb, Debug) << "Gain found for red: " << activeState.awb.gains.automatic.red
<< " and for blue: " << activeState.awb.gains.automatic.blue;
}
REGISTER_IPA_ALGORITHM(Awb, "Awb")
} /* namespace ipa::rkisp1::algorithms */
} /* namespace libcamera */