summaryrefslogtreecommitdiff
path: root/test/v4l2_videodevice/buffer_sharing.cpp
diff options
context:
space:
mode:
authorFlorian Sylvestre <fsylvestre@baylibre.com>2022-07-27 10:40:52 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2022-07-28 14:54:57 +0300
commit564537a9066141f01871b06bae090e6ea5b7a37f (patch)
tree29a12afc98219bf2222e3c4d37fc5e3b201dab7d /test/v4l2_videodevice/buffer_sharing.cpp
parentb3e96411d8a95d3bb4533dc2da605e681701b8b5 (diff)
ipa: rkisp1: Add support of Defect Pixel Cluster Correction control
The Defect Pixel Cluster Correction algorithm is responsible to minimize the impact of defective pixels. The on-the-fly method is actually used, based on coefficient provided by the tuning file. Signed-off-by: Florian Sylvestre <fsylvestre@baylibre.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'test/v4l2_videodevice/buffer_sharing.cpp')
0 files changed, 0 insertions, 0 deletions
f='#n111'>111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi Ltd
 *
 * agc.cpp - AGC/AEC control algorithm
 */

#include <algorithm>
#include <map>
#include <tuple>

#include <linux/bcm2835-isp.h>

#include <libcamera/base/log.h>

#include "../awb_status.h"
#include "../device_status.h"
#include "../histogram.h"
#include "../lux_status.h"
#include "../metadata.h"

#include "agc.h"

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

LOG_DEFINE_CATEGORY(RPiAgc)

#define NAME "rpi.agc"

static constexpr unsigned int PipelineBits = 13; /* seems to be a 13-bit pipeline */

int AgcMeteringMode::read(const libcamera::YamlObject &params)
{
	const YamlObject &yamlWeights = params["weights"];
	if (yamlWeights.size() != AgcStatsSize) {
		LOG(RPiAgc, Error) << "AgcMeteringMode: Incorrect number of weights";
		return -EINVAL;
	}

	unsigned int num = 0;
	for (const auto &p : yamlWeights.asList()) {
		auto value = p.get<double>();
		if (!value)
			return -EINVAL;
		weights[num++] = *value;
	}

	return 0;
}

static std::tuple<int, std::string>
readMeteringModes(std::map<std::string, AgcMeteringMode> &metering_modes,
		  const libcamera::YamlObject &params)
{
	std::string first;
	int ret;

	for (const auto &[key, value] : params.asDict()) {
		AgcMeteringMode meteringMode;
		ret = meteringMode.read(value);
		if (ret)
			return { ret, {} };

		metering_modes[key] = std::move(meteringMode);
		if (first.empty())
			first = key;
	}

	return { 0, first };
}

int AgcExposureMode::read(const libcamera::YamlObject &params)
{
	auto value = params["shutter"].getList<double>();
	if (!value)
		return -EINVAL;
	std::transform(value->begin(), value->end(), std::back_inserter(shutter),
		       [](double v) { return v * 1us; });

	value = params["gain"].getList<double>();
	if (!value)
		return -EINVAL;
	gain = std::move(*value);

	if (shutter.size() < 2 || gain.size() < 2) {
		LOG(RPiAgc, Error)
			<< "AgcExposureMode: must have at least two entries in exposure profile";
		return -EINVAL;
	}

	if (shutter.size() != gain.size()) {
		LOG(RPiAgc, Error)
			<< "AgcExposureMode: expect same number of exposure and gain entries in exposure profile";
		return -EINVAL;
	}

	return 0;
}

static std::tuple<int, std::string>
readExposureModes(std::map<std::string, AgcExposureMode> &exposureModes,
		  const libcamera::YamlObject &params)
{
	std::string first;
	int ret;

	for (const auto &[key, value] : params.asDict()) {
		AgcExposureMode exposureMode;
		ret = exposureMode.read(value);
		if (ret)
			return { ret, {} };

		exposureModes[key] = std::move(exposureMode);
		if (first.empty())
			first = key;
	}

	return { 0, first };
}

int AgcConstraint::read(const libcamera::YamlObject &params)
{
	std::string boundString = params["bound"].get<std::string>("");
	transform(boundString.begin(), boundString.end(),
		  boundString.begin(), ::toupper);
	if (boundString != "UPPER" && boundString != "LOWER") {
		LOG(RPiAgc, Error) << "AGC constraint type should be UPPER or LOWER";
		return -EINVAL;
	}
	bound = boundString == "UPPER" ? Bound::UPPER : Bound::LOWER;

	auto value = params["q_lo"].get<double>();
	if (!value)
		return -EINVAL;
	qLo = *value;

	value = params["q_hi"].get<double>();
	if (!value)
		return -EINVAL;
	qHi = *value;

	return yTarget.read(params["y_target"]);
}

static std::tuple<int, AgcConstraintMode>
readConstraintMode(const libcamera::YamlObject &params)
{
	AgcConstraintMode mode;
	int ret;

	for (const auto &p : params.asList()) {
		AgcConstraint constraint;
		ret = constraint.read(p);
		if (ret)
			return { ret, {} };

		mode.push_back(std::move(constraint));
	}

	return { 0, mode };
}

static std::tuple<int, std::string>
readConstraintModes(std::map<std::string, AgcConstraintMode> &constraintModes,
		    const libcamera::YamlObject &params)
{
	std::string first;
	int ret;

	for (const auto &[key, value] : params.asDict()) {
		std::tie(ret, constraintModes[key]) = readConstraintMode(value);
		if (ret)
			return { ret, {} };

		if (first.empty())
			first = key;
	}

	return { 0, first };
}

int AgcConfig::read(const libcamera::YamlObject &params)
{
	LOG(RPiAgc, Debug) << "AgcConfig";
	int ret;

	std::tie(ret, defaultMeteringMode) =
		readMeteringModes(meteringModes, params["metering_modes"]);
	if (ret)
		return ret;
	std::tie(ret, defaultExposureMode) =
		readExposureModes(exposureModes, params["exposure_modes"]);
	if (ret)
		return ret;
	std::tie(ret, defaultConstraintMode) =
		readConstraintModes(constraintModes, params["constraint_modes"]);
	if (ret)
		return ret;

	ret = yTarget.read(params["y_target"]);
	if (ret)
		return ret;

	speed = params["speed"].get<double>(0.2);
	startupFrames = params["startup_frames"].get<uint16_t>(10);
	convergenceFrames = params["convergence_frames"].get<unsigned int>(6);
	fastReduceThreshold = params["fast_reduce_threshold"].get<double>(0.4);
	baseEv = params["base_ev"].get<double>(1.0);

	/* Start with quite a low value as ramping up is easier than ramping down. */
	defaultExposureTime = params["default_exposure_time"].get<double>(1000) * 1us;
	defaultAnalogueGain = params["default_analogue_gain"].get<double>(1.0);

	return 0;
}

Agc::ExposureValues::ExposureValues()
	: shutter(0s), analogueGain(0),
	  totalExposure(0s), totalExposureNoDG(0s)
{
}

Agc::Agc(Controller *controller)
	: AgcAlgorithm(controller), meteringMode_(nullptr),
	  exposureMode_(nullptr), constraintMode_(nullptr),
	  frameCount_(0), lockCount_(0),
	  lastTargetExposure_(0s), lastSensitivity_(0.0),
	  ev_(1.0), flickerPeriod_(0s),
	  maxShutter_(0s), fixedShutter_(0s), fixedAnalogueGain_(0.0)
{
	memset(&awb_, 0, sizeof(awb_));
	/*
	 * Setting status_.totalExposureValue_ to zero initially tells us
	 * it's not been calculated yet (i.e. Process hasn't yet run).
	 */
	memset(&status_, 0, sizeof(status_));
	status_.ev = ev_;
}

char const *Agc::name() const
{
	return NAME;
}

int Agc::read(const libcamera::YamlObject &params)
{
	LOG(RPiAgc, Debug) << "Agc";

	int ret = config_.read(params);
	if (ret)
		return ret;

	/*
	 * Set the config's defaults (which are the first ones it read) as our
	 * current modes, until someone changes them.  (they're all known to
	 * exist at this point)
	 */
	meteringModeName_ = config_.defaultMeteringMode;
	meteringMode_ = &config_.meteringModes[meteringModeName_];
	exposureModeName_ = config_.defaultExposureMode;
	exposureMode_ = &config_.exposureModes[exposureModeName_];
	constraintModeName_ = config_.defaultConstraintMode;
	constraintMode_ = &config_.constraintModes[constraintModeName_];
	/* Set up the "last shutter/gain" values, in case AGC starts "disabled". */
	status_.shutterTime = config_.defaultExposureTime;
	status_.analogueGain = config_.defaultAnalogueGain;
	return 0;
}

void Agc::disableAuto()
{
	fixedShutter_ = status_.shutterTime;
	fixedAnalogueGain_ = status_.analogueGain;
}

void Agc::enableAuto()
{
	fixedShutter_ = 0s;
	fixedAnalogueGain_ = 0;
}

unsigned int Agc::getConvergenceFrames() const
{
	/*
	 * If shutter and gain have been explicitly set, there is no
	 * convergence to happen, so no need to drop any frames - return zero.
	 */
	if (fixedShutter_ && fixedAnalogueGain_)
		return 0;
	else
		return config_.convergenceFrames;
}

void Agc::setEv(double ev)
{
	ev_ = ev;
}

void Agc::setFlickerPeriod(Duration flickerPeriod)
{
	flickerPeriod_ = flickerPeriod;
}

void Agc::setMaxShutter(Duration maxShutter)
{
	maxShutter_ = maxShutter;
}

void Agc::setFixedShutter(Duration fixedShutter)
{
	fixedShutter_ = fixedShutter;
	/* Set this in case someone calls disableAuto() straight after. */
	status_.shutterTime = clipShutter(fixedShutter_);
}

void Agc::setFixedAnalogueGain(double fixedAnalogueGain)
{
	fixedAnalogueGain_ = fixedAnalogueGain;
	/* Set this in case someone calls disableAuto() straight after. */
	status_.analogueGain = fixedAnalogueGain;
}

void Agc::setMeteringMode(std::string const &meteringModeName)
{
	meteringModeName_ = meteringModeName;
}

void Agc::setExposureMode(std::string const &exposureModeName)
{
	exposureModeName_ = exposureModeName;
}

void Agc::setConstraintMode(std::string const &constraintModeName)
{
	constraintModeName_ = constraintModeName;
}

void Agc::switchMode(CameraMode const &cameraMode,
		     Metadata *metadata)
{
	/* AGC expects the mode sensitivity always to be non-zero. */
	ASSERT(cameraMode.sensitivity);

	housekeepConfig();

	Duration fixedShutter = clipShutter(fixedShutter_);
	if (fixedShutter && fixedAnalogueGain_) {
		/* We're going to reset the algorithm here with these fixed values. */

		fetchAwbStatus(metadata);
		double minColourGain = std::min({ awb_.gainR, awb_.gainG, awb_.gainB, 1.0 });
		ASSERT(minColourGain != 0.0);

		/* This is the equivalent of computeTargetExposure and applyDigitalGain. */
		target_.totalExposureNoDG = fixedShutter_ * fixedAnalogueGain_;
		target_.totalExposure = target_.totalExposureNoDG / minColourGain;

		/* Equivalent of filterExposure. This resets any "history". */
		filtered_ = target_;

		/* Equivalent of divideUpExposure. */
		filtered_.shutter = fixedShutter;
		filtered_.analogueGain = fixedAnalogueGain_;
	} else if (status_.totalExposureValue) {
		/*
		 * On a mode switch, various things could happen:
		 * - the exposure profile might change
		 * - a fixed exposure or gain might be set
		 * - the new mode's sensitivity might be different
		 * We cope with the last of these by scaling the target values. After
		 * that we just need to re-divide the exposure/gain according to the
		 * current exposure profile, which takes care of everything else.
		 */

		double ratio = lastSensitivity_ / cameraMode.sensitivity;
		target_.totalExposureNoDG *= ratio;
		target_.totalExposure *= ratio;
		filtered_.totalExposureNoDG *= ratio;
		filtered_.totalExposure *= ratio;

		divideUpExposure();
	} else {
		/*
		 * We come through here on startup, when at least one of the shutter
		 * or gain has not been fixed. We must still write those values out so
		 * that they will be applied immediately. We supply some arbitrary defaults
		 * for any that weren't set.
		 */

		/* Equivalent of divideUpExposure. */
		filtered_.shutter = fixedShutter ? fixedShutter : config_.defaultExposureTime;
		filtered_.analogueGain = fixedAnalogueGain_ ? fixedAnalogueGain_ : config_.defaultAnalogueGain;
	}

	writeAndFinish(metadata, false);

	/* We must remember the sensitivity of this mode for the next SwitchMode. */
	lastSensitivity_ = cameraMode.sensitivity;
}

void Agc::prepare(Metadata *imageMetadata)
{
	status_.digitalGain = 1.0;
	fetchAwbStatus(imageMetadata); /* always fetch it so that Process knows it's been done */

	if (status_.totalExposureValue) {
		/* Process has run, so we have meaningful values. */
		DeviceStatus deviceStatus;
		if (imageMetadata->get("device.status", deviceStatus) == 0) {
			Duration actualExposure = deviceStatus.shutterSpeed *
						  deviceStatus.analogueGain;
			if (actualExposure) {
				status_.digitalGain = status_.totalExposureValue / actualExposure;
				LOG(RPiAgc, Debug) << "Want total exposure " << status_.totalExposureValue;
				/*
				 * Never ask for a gain < 1.0, and also impose
				 * some upper limit. Make it customisable?
				 */
				status_.digitalGain = std::max(1.0, std::min(status_.digitalGain, 4.0));
				LOG(RPiAgc, Debug) << "Actual exposure " << actualExposure;
				LOG(RPiAgc, Debug) << "Use digitalGain " << status_.digitalGain;
				LOG(RPiAgc, Debug) << "Effective exposure "
						   << actualExposure * status_.digitalGain;
				/* Decide whether AEC/AGC has converged. */
				updateLockStatus(deviceStatus);
			}
		} else
			LOG(RPiAgc, Warning) << name() << ": no device metadata";
		imageMetadata->set("agc.status", status_);
	}
}

void Agc::process(StatisticsPtr &stats, Metadata *imageMetadata)
{
	frameCount_++;
	/*
	 * First a little bit of housekeeping, fetching up-to-date settings and
	 * configuration, that kind of thing.
	 */
	housekeepConfig();
	/* Get the current exposure values for the frame that's just arrived. */
	fetchCurrentExposure(imageMetadata);
	/* Compute the total gain we require relative to the current exposure. */
	double gain, targetY;
	computeGain(stats.get(), imageMetadata, gain, targetY);
	/* Now compute the target (final) exposure which we think we want. */
	computeTargetExposure(gain);
	/*
	 * Some of the exposure has to be applied as digital gain, so work out
	 * what that is. This function also tells us whether it's decided to
	 * "desaturate" the image more quickly.
	 */
	bool desaturate = applyDigitalGain(gain, targetY);
	/* The results have to be filtered so as not to change too rapidly. */
	filterExposure(desaturate);
	/*
	 * The last thing is to divide up the exposure value into a shutter time
	 * and analogue gain, according to the current exposure mode.
	 */
	divideUpExposure();
	/* Finally advertise what we've done. */
	writeAndFinish(imageMetadata, desaturate);
}

void Agc::updateLockStatus(DeviceStatus const &deviceStatus)
{
	const double errorFactor = 0.10; /* make these customisable? */
	const int maxLockCount = 5;
	/* Reset "lock count" when we exceed this multiple of errorFactor */
	const double resetMargin = 1.5;

	/* Add 200us to the exposure time error to allow for line quantisation. */
	Duration exposureError = lastDeviceStatus_.shutterSpeed * errorFactor + 200us;
	double gainError = lastDeviceStatus_.analogueGain * errorFactor;
	Duration targetError = lastTargetExposure_ * errorFactor;

	/*
	 * Note that we don't know the exposure/gain limits of the sensor, so
	 * the values we keep requesting may be unachievable. For this reason
	 * we only insist that we're close to values in the past few frames.
	 */
	if (deviceStatus.shutterSpeed > lastDeviceStatus_.shutterSpeed - exposureError &&
	    deviceStatus.shutterSpeed < lastDeviceStatus_.shutterSpeed + exposureError &&
	    deviceStatus.analogueGain > lastDeviceStatus_.analogueGain - gainError &&
	    deviceStatus.analogueGain < lastDeviceStatus_.analogueGain + gainError &&
	    status_.targetExposureValue > lastTargetExposure_ - targetError &&
	    status_.targetExposureValue < lastTargetExposure_ + targetError)
		lockCount_ = std::min(lockCount_ + 1, maxLockCount);
	else if (deviceStatus.shutterSpeed < lastDeviceStatus_.shutterSpeed - resetMargin * exposureError ||
		 deviceStatus.shutterSpeed > lastDeviceStatus_.shutterSpeed + resetMargin * exposureError ||
		 deviceStatus.analogueGain < lastDeviceStatus_.analogueGain - resetMargin * gainError ||
		 deviceStatus.analogueGain > lastDeviceStatus_.analogueGain + resetMargin * gainError ||
		 status_.targetExposureValue < lastTargetExposure_ - resetMargin * targetError ||
		 status_.targetExposureValue > lastTargetExposure_ + resetMargin * targetError)
		lockCount_ = 0;

	lastDeviceStatus_ = deviceStatus;
	lastTargetExposure_ = status_.targetExposureValue;

	LOG(RPiAgc, Debug) << "Lock count updated to " << lockCount_;
	status_.locked = lockCount_ == maxLockCount;
}

static void copyString(std::string const &s, char *d, size_t size)
{
	size_t length = s.copy(d, size - 1);
	d[length] = '\0';
}

void Agc::housekeepConfig()
{
	/* First fetch all the up-to-date settings, so no one else has to do it. */
	status_.ev = ev_;
	status_.fixedShutter = clipShutter(fixedShutter_);
	status_.fixedAnalogueGain = fixedAnalogueGain_;
	status_.flickerPeriod = flickerPeriod_;
	LOG(RPiAgc, Debug) << "ev " << status_.ev << " fixedShutter "
			   << status_.fixedShutter << " fixedAnalogueGain "
			   << status_.fixedAnalogueGain;
	/*
	 * Make sure the "mode" pointers point to the up-to-date things, if
	 * they've changed.
	 */
	if (strcmp(meteringModeName_.c_str(), status_.meteringMode)) {
		auto it = config_.meteringModes.find(meteringModeName_);
		if (it == config_.meteringModes.end())
			LOG(RPiAgc, Fatal) << "No metering mode " << meteringModeName_;
		meteringMode_ = &it->second;
		copyString(meteringModeName_, status_.meteringMode,
			   sizeof(status_.meteringMode));
	}
	if (strcmp(exposureModeName_.c_str(), status_.exposureMode)) {
		auto it = config_.exposureModes.find(exposureModeName_);
		if (it == config_.exposureModes.end())
			LOG(RPiAgc, Fatal) << "No exposure profile " << exposureModeName_;
		exposureMode_ = &it->second;
		copyString(exposureModeName_, status_.exposureMode,
			   sizeof(status_.exposureMode));
	}
	if (strcmp(constraintModeName_.c_str(), status_.constraintMode)) {
		auto it =
			config_.constraintModes.find(constraintModeName_);
		if (it == config_.constraintModes.end())
			LOG(RPiAgc, Fatal) << "No constraint list " << constraintModeName_;
		constraintMode_ = &it->second;
		copyString(constraintModeName_, status_.constraintMode,
			   sizeof(status_.constraintMode));
	}
	LOG(RPiAgc, Debug) << "exposureMode "
			   << exposureModeName_ << " constraintMode "
			   << constraintModeName_ << " meteringMode "
			   << meteringModeName_;
}

void Agc::fetchCurrentExposure(Metadata *imageMetadata)
{
	std::unique_lock<Metadata> lock(*imageMetadata);
	DeviceStatus *deviceStatus =
		imageMetadata->getLocked<DeviceStatus>("device.status");
	if (!deviceStatus)
		LOG(RPiAgc, Fatal) << "No device metadata";
	current_.shutter = deviceStatus->shutterSpeed;
	current_.analogueGain = deviceStatus->analogueGain;
	AgcStatus *agcStatus =
		imageMetadata->getLocked<AgcStatus>("agc.status");
	current_.totalExposure = agcStatus ? agcStatus->totalExposureValue : 0s;
	current_.totalExposureNoDG = current_.shutter * current_.analogueGain;
}

void Agc::fetchAwbStatus(Metadata *imageMetadata)
{
	awb_.gainR = 1.0; /* in case not found in metadata */
	awb_.gainG = 1.0;
	awb_.gainB = 1.0;
	if (imageMetadata->get("awb.status", awb_) != 0)
		LOG(RPiAgc, Debug) << "No AWB status found";
}

static double computeInitialY(bcm2835_isp_stats *stats, AwbStatus const &awb,
			      double weights[], double gain)
{
	bcm2835_isp_stats_region *regions = stats->agc_stats;
	/*
	 * Note how the calculation below means that equal weights give you
	 * "average" metering (i.e. all pixels equally important).
	 */
	double rSum = 0, gSum = 0, bSum = 0, pixelSum = 0;
	for (unsigned int i = 0; i < AgcStatsSize; i++) {
		double counted = regions[i].counted;
		double rAcc = std::min(regions[i].r_sum * gain, ((1 << PipelineBits) - 1) * counted);
		double gAcc = std::min(regions[i].g_sum * gain, ((1 << PipelineBits) - 1) * counted);
		double bAcc = std::min(regions[i].b_sum * gain, ((1 << PipelineBits) - 1) * counted);
		rSum += rAcc * weights[i];
		gSum += gAcc * weights[i];
		bSum += bAcc * weights[i];
		pixelSum += counted * weights[i];
	}
	if (pixelSum == 0.0) {
		LOG(RPiAgc, Warning) << "computeInitialY: pixelSum is zero";
		return 0;
	}
	double ySum = rSum * awb.gainR * .299 +
		      gSum * awb.gainG * .587 +
		      bSum * awb.gainB * .114;
	return ySum / pixelSum / (1 << PipelineBits);
}

/*
 * We handle extra gain through EV by adjusting our Y targets. However, you
 * simply can't monitor histograms once they get very close to (or beyond!)
 * saturation, so we clamp the Y targets to this value. It does mean that EV
 * increases don't necessarily do quite what you might expect in certain
 * (contrived) cases.
 */

static constexpr double EvGainYTargetLimit = 0.9;

static double constraintComputeGain(AgcConstraint &c, Histogram &h, double lux,
				    double evGain, double &targetY)
{
	targetY = c.yTarget.eval(c.yTarget.domain().clip(lux));
	targetY = std::min(EvGainYTargetLimit, targetY * evGain);
	double iqm = h.interQuantileMean(c.qLo, c.qHi);
	return (targetY * NUM_HISTOGRAM_BINS) / iqm;
}

void Agc::computeGain(bcm2835_isp_stats *statistics, Metadata *imageMetadata,
		      double &gain, double &targetY)
{
	struct LuxStatus lux = {};
	lux.lux = 400; /* default lux level to 400 in case no metadata found */
	if (imageMetadata->get("lux.status", lux) != 0)
		LOG(RPiAgc, Warning) << "No lux level found";
	Histogram h(statistics->hist[0].g_hist, NUM_HISTOGRAM_BINS);
	double evGain = status_.ev * config_.baseEv;
	/*
	 * The initial gain and target_Y come from some of the regions. After
	 * that we consider the histogram constraints.
	 */
	targetY = config_.yTarget.eval(config_.yTarget.domain().clip(lux.lux));
	targetY = std::min(EvGainYTargetLimit, targetY * evGain);

	/*
	 * Do this calculation a few times as brightness increase can be
	 * non-linear when there are saturated regions.
	 */
	gain = 1.0;
	for (int i = 0; i < 8; i++) {
		double initialY = computeInitialY(statistics, awb_, meteringMode_->weights, gain);
		double extraGain = std::min(10.0, targetY / (initialY + .001));
		gain *= extraGain;
		LOG(RPiAgc, Debug) << "Initial Y " << initialY << " target " << targetY
				   << " gives gain " << gain;
		if (extraGain < 1.01) /* close enough */
			break;
	}

	for (auto &c : *constraintMode_) {
		double newTargetY;
		double newGain = constraintComputeGain(c, h, lux.lux, evGain, newTargetY);
		LOG(RPiAgc, Debug) << "Constraint has target_Y "
				   << newTargetY << " giving gain " << newGain;
		if (c.bound == AgcConstraint::Bound::LOWER && newGain > gain) {
			LOG(RPiAgc, Debug) << "Lower bound constraint adopted";
			gain = newGain;
			targetY = newTargetY;
		} else if (c.bound == AgcConstraint::Bound::UPPER && newGain < gain) {
			LOG(RPiAgc, Debug) << "Upper bound constraint adopted";
			gain = newGain;
			targetY = newTargetY;
		}
	}
	LOG(RPiAgc, Debug) << "Final gain " << gain << " (target_Y " << targetY << " ev "
			   << status_.ev << " base_ev " << config_.baseEv
			   << ")";
}

void Agc::computeTargetExposure(double gain)
{
	if (status_.fixedShutter && status_.fixedAnalogueGain) {
		/*
		 * When ag and shutter are both fixed, we need to drive the
		 * total exposure so that we end up with a digital gain of at least
		 * 1/minColourGain. Otherwise we'd desaturate channels causing
		 * white to go cyan or magenta.
		 */
		double minColourGain = std::min({ awb_.gainR, awb_.gainG, awb_.gainB, 1.0 });
		ASSERT(minColourGain != 0.0);
		target_.totalExposure =
			status_.fixedShutter * status_.fixedAnalogueGain / minColourGain;
	} else {
		/*
		 * The statistics reflect the image without digital gain, so the final
		 * total exposure we're aiming for is:
		 */
		target_.totalExposure = current_.totalExposureNoDG * gain;
		/* The final target exposure is also limited to what the exposure mode allows. */
		Duration maxShutter = status_.fixedShutter
					      ? status_.fixedShutter
					      : exposureMode_->shutter.back();
		maxShutter = clipShutter(maxShutter);
		Duration maxTotalExposure =
			maxShutter *
			(status_.fixedAnalogueGain != 0.0
				 ? status_.fixedAnalogueGain
				 : exposureMode_->gain.back());
		target_.totalExposure = std::min(target_.totalExposure, maxTotalExposure);
	}
	LOG(RPiAgc, Debug) << "Target totalExposure " << target_.totalExposure;
}

bool Agc::applyDigitalGain(double gain, double targetY)
{
	double minColourGain = std::min({ awb_.gainR, awb_.gainG, awb_.gainB, 1.0 });
	ASSERT(minColourGain != 0.0);
	double dg = 1.0 / minColourGain;
	/*
	 * I think this pipeline subtracts black level and rescales before we
	 * get the stats, so no need to worry about it.
	 */
	LOG(RPiAgc, Debug) << "after AWB, target dg " << dg << " gain " << gain
			   << " target_Y " << targetY;
	/*
	 * Finally, if we're trying to reduce exposure but the target_Y is
	 * "close" to 1.0, then the gain computed for that constraint will be
	 * only slightly less than one, because the measured Y can never be
	 * larger than 1.0. When this happens, demand a large digital gain so
	 * that the exposure can be reduced, de-saturating the image much more
	 * quickly (and we then approach the correct value more quickly from
	 * below).
	 */
	bool desaturate = targetY > config_.fastReduceThreshold &&
			  gain < sqrt(targetY);
	if (desaturate)
		dg /= config_.fastReduceThreshold;
	LOG(RPiAgc, Debug) << "Digital gain " << dg << " desaturate? " << desaturate;
	target_.totalExposureNoDG = target_.totalExposure / dg;
	LOG(RPiAgc, Debug) << "Target totalExposureNoDG " << target_.totalExposureNoDG;
	return desaturate;
}

void Agc::filterExposure(bool desaturate)
{
	double speed = config_.speed;
	/*
	 * AGC adapts instantly if both shutter and gain are directly specified
	 * or we're in the startup phase.
	 */
	if ((status_.fixedShutter && status_.fixedAnalogueGain) ||
	    frameCount_ <= config_.startupFrames)
		speed = 1.0;
	if (!filtered_.totalExposure) {
		filtered_.totalExposure = target_.totalExposure;
		filtered_.totalExposureNoDG = target_.totalExposureNoDG;
	} else {
		/*
		 * If close to the result go faster, to save making so many
		 * micro-adjustments on the way. (Make this customisable?)
		 */
		if (filtered_.totalExposure < 1.2 * target_.totalExposure &&
		    filtered_.totalExposure > 0.8 * target_.totalExposure)
			speed = sqrt(speed);
		filtered_.totalExposure = speed * target_.totalExposure +
					  filtered_.totalExposure * (1.0 - speed);
		/*
		 * When desaturing, take a big jump down in totalExposureNoDG,
		 * which we'll hide with digital gain.
		 */
		if (desaturate)
			filtered_.totalExposureNoDG =
				target_.totalExposureNoDG;
		else
			filtered_.totalExposureNoDG =
				speed * target_.totalExposureNoDG +
				filtered_.totalExposureNoDG * (1.0 - speed);
	}
	/*
	 * We can't let the totalExposureNoDG exposure deviate too far below the
	 * total exposure, as there might not be enough digital gain available
	 * in the ISP to hide it (which will cause nasty oscillation).
	 */
	if (filtered_.totalExposureNoDG <
	    filtered_.totalExposure * config_.fastReduceThreshold)
		filtered_.totalExposureNoDG = filtered_.totalExposure * config_.fastReduceThreshold;
	LOG(RPiAgc, Debug) << "After filtering, totalExposure " << filtered_.totalExposure
			   << " no dg " << filtered_.totalExposureNoDG;
}

void Agc::divideUpExposure()
{
	/*
	 * Sending the fixed shutter/gain cases through the same code may seem
	 * unnecessary, but it will make more sense when extend this to cover
	 * variable aperture.
	 */
	Duration exposureValue = filtered_.totalExposureNoDG;
	Duration shutterTime;
	double analogueGain;
	shutterTime = status_.fixedShutter ? status_.fixedShutter
					   : exposureMode_->shutter[0];
	shutterTime = clipShutter(shutterTime);
	analogueGain = status_.fixedAnalogueGain != 0.0 ? status_.fixedAnalogueGain
							: exposureMode_->gain[0];
	if (shutterTime * analogueGain < exposureValue) {
		for (unsigned int stage = 1;
		     stage < exposureMode_->gain.size(); stage++) {
			if (!status_.fixedShutter) {
				Duration stageShutter =
					clipShutter(exposureMode_->shutter[stage]);
				if (stageShutter * analogueGain >= exposureValue) {
					shutterTime = exposureValue / analogueGain;
					break;
				}
				shutterTime = stageShutter;
			}
			if (status_.fixedAnalogueGain == 0.0) {
				if (exposureMode_->gain[stage] * shutterTime >= exposureValue) {
					analogueGain = exposureValue / shutterTime;
					break;
				}
				analogueGain = exposureMode_->gain[stage];
			}
		}
	}
	LOG(RPiAgc, Debug) << "Divided up shutter and gain are " << shutterTime << " and "
			   << analogueGain;
	/*
	 * Finally adjust shutter time for flicker avoidance (require both
	 * shutter and gain not to be fixed).
	 */
	if (!status_.fixedShutter && !status_.fixedAnalogueGain &&
	    status_.flickerPeriod) {
		int flickerPeriods = shutterTime / status_.flickerPeriod;
		if (flickerPeriods) {
			Duration newShutterTime = flickerPeriods * status_.flickerPeriod;
			analogueGain *= shutterTime / newShutterTime;
			/*
			 * We should still not allow the ag to go over the
			 * largest value in the exposure mode. Note that this
			 * may force more of the total exposure into the digital
			 * gain as a side-effect.
			 */
			analogueGain = std::min(analogueGain, exposureMode_->gain.back());
			shutterTime = newShutterTime;
		}
		LOG(RPiAgc, Debug) << "After flicker avoidance, shutter "
				   << shutterTime << " gain " << analogueGain;
	}
	filtered_.shutter = shutterTime;
	filtered_.analogueGain = analogueGain;
}

void Agc::writeAndFinish(Metadata *imageMetadata, bool desaturate)
{
	status_.totalExposureValue = filtered_.totalExposure;
	status_.targetExposureValue = desaturate ? 0s : target_.totalExposureNoDG;
	status_.shutterTime = filtered_.shutter;
	status_.analogueGain = filtered_.analogueGain;
	/*
	 * Write to metadata as well, in case anyone wants to update the camera
	 * immediately.
	 */
	imageMetadata->set("agc.status", status_);
	LOG(RPiAgc, Debug) << "Output written, total exposure requested is "
			   << filtered_.totalExposure;
	LOG(RPiAgc, Debug) << "Camera exposure update: shutter time " << filtered_.shutter
			   << " analogue gain " << filtered_.analogueGain;
}

Duration Agc::clipShutter(Duration shutter)
{
	if (maxShutter_)
		shutter = std::min(shutter, maxShutter_);
	return shutter;
}

/* Register algorithm with the system. */
static Algorithm *create(Controller *controller)
{
	return (Algorithm *)new Agc(controller);
}
static RegisterAlgorithm reg(NAME, &create);
in &ipu3_uapi_ff_status */ struct ipu3_uapi_stats_3a { struct ipu3_uapi_awb_raw_buffer awb_raw_buffer; struct ipu3_uapi_ae_raw_buffer_aligned ae_raw_buffer[IPU3_UAPI_MAX_STRIPES]; struct ipu3_uapi_af_raw_buffer af_raw_buffer; struct ipu3_uapi_awb_fr_raw_buffer awb_fr_raw_buffer; struct ipu3_uapi_4a_config stats_4a_config; __u32 ae_join_buffers; __u8 padding[28]; struct ipu3_uapi_stats_3a_bubble_info_per_stripe stats_3a_bubble_per_stripe; struct ipu3_uapi_ff_status stats_3a_status; } __attribute__((packed)); /******************* ipu3_uapi_acc_param *******************/ #define IPU3_UAPI_ISP_VEC_ELEMS 64 #define IPU3_UAPI_ISP_TNR3_VMEM_LEN 9 #define IPU3_UAPI_BNR_LUT_SIZE 32 /* number of elements in gamma correction LUT */ #define IPU3_UAPI_GAMMA_CORR_LUT_ENTRIES 256 /* largest grid is 73x56, for grid_height_per_slice of 2, 73x2 = 146 */ #define IPU3_UAPI_SHD_MAX_CELLS_PER_SET 146 #define IPU3_UAPI_SHD_MAX_CFG_SETS 28 /* Normalization shift aka nf */ #define IPU3_UAPI_SHD_BLGR_NF_SHIFT 13 #define IPU3_UAPI_SHD_BLGR_NF_MASK 7 #define IPU3_UAPI_YUVP2_TCC_MACC_TABLE_ELEMENTS 16 #define IPU3_UAPI_YUVP2_TCC_INV_Y_LUT_ELEMENTS 14 #define IPU3_UAPI_YUVP2_TCC_GAIN_PCWL_LUT_ELEMENTS 258 #define IPU3_UAPI_YUVP2_TCC_R_SQR_LUT_ELEMENTS 24 #define IPU3_UAPI_ANR_LUT_SIZE 26 #define IPU3_UAPI_ANR_PYRAMID_SIZE 22 #define IPU3_UAPI_LIN_LUT_SIZE 64 /* Bayer Noise Reduction related structs */ /** * struct ipu3_uapi_bnr_static_config_wb_gains_config - White balance gains * * @gr: white balance gain for Gr channel. * @r: white balance gain for R channel. * @b: white balance gain for B channel. * @gb: white balance gain for Gb channel. * * For BNR parameters WB gain factor for the three channels [Ggr, Ggb, Gb, Gr]. * Their precision is U3.13 and the range is (0, 8) and the actual gain is * Gx + 1, it is typically Gx = 1. * * Pout = {Pin * (1 + Gx)}. */ struct ipu3_uapi_bnr_static_config_wb_gains_config { __u16 gr; __u16 r; __u16 b; __u16 gb; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_wb_gains_thr_config - Threshold config * * @gr: white balance threshold gain for Gr channel. * @r: white balance threshold gain for R channel. * @b: white balance threshold gain for B channel. * @gb: white balance threshold gain for Gb channel. * * Defines the threshold that specifies how different a defect pixel can be from * its neighbors.(used by dynamic defect pixel correction sub block) * Precision u4.4 range [0, 8]. */ struct ipu3_uapi_bnr_static_config_wb_gains_thr_config { __u8 gr; __u8 r; __u8 b; __u8 gb; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_thr_coeffs_config - Noise model * coefficients that controls noise threshold * * @cf: Free coefficient for threshold calculation, range [0, 8191], default 0. * @reserved0: reserved * @cg: Gain coefficient for threshold calculation, [0, 31], default 8. * @ci: Intensity coefficient for threshold calculation. range [0, 0x1f] * default 6. * format: u3.2 (3 most significant bits represent whole number, * 2 least significant bits represent the fractional part * with each count representing 0.25) * e.g. 6 in binary format is 00110, that translates to 1.5 * @reserved1: reserved * @r_nf: Normalization shift value for r^2 calculation, range [12, 20] * where r is a radius of pixel [row, col] from centor of sensor. * default 14. * * Threshold used to distinguish between noise and details. */ struct ipu3_uapi_bnr_static_config_thr_coeffs_config { __u32 cf:13; __u32 reserved0:3; __u32 cg:5; __u32 ci:5; __u32 reserved1:1; __u32 r_nf:5; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_thr_ctrl_shd_config - Shading config * * @gr: Coefficient defines lens shading gain approximation for gr channel * @r: Coefficient defines lens shading gain approximation for r channel * @b: Coefficient defines lens shading gain approximation for b channel * @gb: Coefficient defines lens shading gain approximation for gb channel * * Parameters for noise model (NM) adaptation of BNR due to shading correction. * All above have precision of u3.3, default to 0. */ struct ipu3_uapi_bnr_static_config_thr_ctrl_shd_config { __u8 gr; __u8 r; __u8 b; __u8 gb; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_opt_center_config - Optical center config * * @x_reset: Reset value of X (col start - X center). Precision s12.0. * @reserved0: reserved * @y_reset: Reset value of Y (row start - Y center). Precision s12.0. * @reserved2: reserved * * Distance from corner to optical center for NM adaptation due to shading * correction (should be calculated based on shading tables) */ struct ipu3_uapi_bnr_static_config_opt_center_config { __s32 x_reset:13; __u32 reserved0:3; __s32 y_reset:13; __u32 reserved2:3; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_lut_config - BNR square root lookup table * * @values: pre-calculated values of square root function. * * LUT implementation of square root operation. */ struct ipu3_uapi_bnr_static_config_lut_config { __u8 values[IPU3_UAPI_BNR_LUT_SIZE]; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_bp_ctrl_config - Detect bad pixels (bp) * * @bp_thr_gain: Defines the threshold that specifies how different a * defect pixel can be from its neighbors. Threshold is * dependent on de-noise threshold calculated by algorithm. * Range [4, 31], default 4. * @reserved0: reserved * @defect_mode: Mode of addressed defect pixels, * 0 - single defect pixel is expected, * 1 - 2 adjacent defect pixels are expected, default 1. * @bp_gain: Defines how 2nd derivation that passes through a defect pixel * is different from 2nd derivations that pass through * neighbor pixels. u4.2, range [0, 256], default 8. * @reserved1: reserved * @w0_coeff: Blending coefficient of defect pixel correction. * Precision u4, range [0, 8], default 8. * @reserved2: reserved * @w1_coeff: Enable influence of incorrect defect pixel correction to be * avoided. Precision u4, range [1, 8], default 8. * @reserved3: reserved */ struct ipu3_uapi_bnr_static_config_bp_ctrl_config { __u32 bp_thr_gain:5; __u32 reserved0:2; __u32 defect_mode:1; __u32 bp_gain:6; __u32 reserved1:18; __u32 w0_coeff:4; __u32 reserved2:4; __u32 w1_coeff:4; __u32 reserved3:20; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_dn_detect_ctrl_config - Denoising config * * @alpha: Weight of central element of smoothing filter. * @beta: Weight of peripheral elements of smoothing filter, default 4. * @gamma: Weight of diagonal elements of smoothing filter, default 4. * * beta and gamma parameter define the strength of the noise removal filter. * All above has precision u0.4, range [0, 0xf] * format: u0.4 (no / zero bits represent whole number, * 4 bits represent the fractional part * with each count representing 0.0625) * e.g. 0xf translates to 0.0625x15 = 0.9375 * * @reserved0: reserved * @max_inf: Maximum increase of peripheral or diagonal element influence * relative to the pre-defined value range: [0x5, 0xa] * @reserved1: reserved * @gd_enable: Green disparity enable control, 0 - disable, 1 - enable. * @bpc_enable: Bad pixel correction enable control, 0 - disable, 1 - enable. * @bnr_enable: Bayer noise removal enable control, 0 - disable, 1 - enable. * @ff_enable: Fixed function enable, 0 - disable, 1 - enable. * @reserved2: reserved */ struct ipu3_uapi_bnr_static_config_dn_detect_ctrl_config { __u32 alpha:4; __u32 beta:4; __u32 gamma:4; __u32 reserved0:4; __u32 max_inf:4; __u32 reserved1:7; __u32 gd_enable:1; __u32 bpc_enable:1; __u32 bnr_enable:1; __u32 ff_enable:1; __u32 reserved2:1; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_opt_center_sqr_config - BNR optical square * * @x_sqr_reset: Reset value of X^2. * @y_sqr_reset: Reset value of Y^2. * * Please note: * * #. X and Y ref to * &ipu3_uapi_bnr_static_config_opt_center_config * #. Both structs are used in threshold formula to calculate r^2, where r * is a radius of pixel [row, col] from centor of sensor. */ struct ipu3_uapi_bnr_static_config_opt_center_sqr_config { __u32 x_sqr_reset; __u32 y_sqr_reset; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config - BNR static config * * @wb_gains: white balance gains &ipu3_uapi_bnr_static_config_wb_gains_config * @wb_gains_thr: white balance gains threshold as defined by * &ipu3_uapi_bnr_static_config_wb_gains_thr_config * @thr_coeffs: coefficients of threshold * &ipu3_uapi_bnr_static_config_thr_coeffs_config * @thr_ctrl_shd: control of shading threshold * &ipu3_uapi_bnr_static_config_thr_ctrl_shd_config * @opt_center: optical center &ipu3_uapi_bnr_static_config_opt_center_config * * Above parameters and opt_center_sqr are used for white balance and shading. * * @lut: lookup table &ipu3_uapi_bnr_static_config_lut_config * @bp_ctrl: detect and remove bad pixels as defined in struct * &ipu3_uapi_bnr_static_config_bp_ctrl_config * @dn_detect_ctrl: detect and remove noise. * &ipu3_uapi_bnr_static_config_dn_detect_ctrl_config * @column_size: The number of pixels in column. * @opt_center_sqr: Reset value of r^2 to optical center, see * &ipu3_uapi_bnr_static_config_opt_center_sqr_config. */ struct ipu3_uapi_bnr_static_config { struct ipu3_uapi_bnr_static_config_wb_gains_config wb_gains; struct ipu3_uapi_bnr_static_config_wb_gains_thr_config wb_gains_thr; struct ipu3_uapi_bnr_static_config_thr_coeffs_config thr_coeffs; struct ipu3_uapi_bnr_static_config_thr_ctrl_shd_config thr_ctrl_shd; struct ipu3_uapi_bnr_static_config_opt_center_config opt_center; struct ipu3_uapi_bnr_static_config_lut_config lut; struct ipu3_uapi_bnr_static_config_bp_ctrl_config bp_ctrl; struct ipu3_uapi_bnr_static_config_dn_detect_ctrl_config dn_detect_ctrl; __u32 column_size; struct ipu3_uapi_bnr_static_config_opt_center_sqr_config opt_center_sqr; } __attribute__((packed)); /** * struct ipu3_uapi_bnr_static_config_green_disparity - Correct green disparity * * @gd_red: Shading gain coeff for gr disparity level in bright red region. * Precision u0.6, default 4(0.0625). * @reserved0: reserved * @gd_green: Shading gain coeff for gr disparity level in bright green * region. Precision u0.6, default 4(0.0625). * @reserved1: reserved * @gd_blue: Shading gain coeff for gr disparity level in bright blue region. * Precision u0.6, default 4(0.0625). * @reserved2: reserved * @gd_black: Maximal green disparity level in dark region (stronger disparity * assumed to be image detail). Precision u14, default 80. * @reserved3: reserved * @gd_shading: Change maximal green disparity level according to square * distance from image center. * @reserved4: reserved * @gd_support: Lower bound for the number of second green color pixels in * current pixel neighborhood with less than threshold difference * from it. * * The shading gain coeff of red, green, blue and black are used to calculate * threshold given a pixel's color value and its coordinates in the image. * * @reserved5: reserved * @gd_clip: Turn green disparity clip on/off, [0, 1], default 1. * @gd_central_weight: Central pixel weight in 9 pixels weighted sum. */ struct ipu3_uapi_bnr_static_config_green_disparity { __u32 gd_red:6; __u32 reserved0:2; __u32 gd_green:6; __u32 reserved1:2; __u32 gd_blue:6; __u32 reserved2:10; __u32 gd_black:14; __u32 reserved3:2; __u32 gd_shading:7; __u32 reserved4:1; __u32 gd_support:2; __u32 reserved5:1; __u32 gd_clip:1; __u32 gd_central_weight:4; } __attribute__((packed)); /** * struct ipu3_uapi_dm_config - De-mosaic parameters * * @dm_en: de-mosaic enable. * @ch_ar_en: Checker artifacts removal enable flag. Default 0. * @fcc_en: False color correction (FCC) enable flag. Default 0. * @reserved0: reserved * @frame_width: do not care * @gamma_sc: Sharpening coefficient (coefficient of 2-d derivation of * complementary color in Hamilton-Adams interpolation). * u5, range [0, 31], default 8. * @reserved1: reserved * @lc_ctrl: Parameter that controls weights of Chroma Homogeneity metric * in calculation of final homogeneity metric. * u5, range [0, 31], default 7. * @reserved2: reserved * @cr_param1: First parameter that defines Checker artifact removal * feature gain. Precision u5, range [0, 31], default 8. * @reserved3: reserved * @cr_param2: Second parameter that defines Checker artifact removal * feature gain. Precision u5, range [0, 31], default 8. * @reserved4: reserved * @coring_param: Defines power of false color correction operation. * low for preserving edge colors, high for preserving gray * edge artifacts. * Precision u1.4, range [0, 1.9375], default 4 (0.25). * @reserved5: reserved * * The demosaic fixed function block is responsible to covert Bayer(mosaiced) * images into color images based on demosaicing algorithm. */ struct ipu3_uapi_dm_config { __u32 dm_en:1; __u32 ch_ar_en:1; __u32 fcc_en:1; __u32 reserved0:13; __u32 frame_width:16; __u32 gamma_sc:5; __u32 reserved1:3; __u32 lc_ctrl:5; __u32 reserved2:3; __u32 cr_param1:5; __u32 reserved3:3; __u32 cr_param2:5; __u32 reserved4:3; __u32 coring_param:5; __u32 reserved5:27; } __attribute__((packed)); /** * struct ipu3_uapi_ccm_mat_config - Color correction matrix * * @coeff_m11: CCM 3x3 coefficient, range [-65536, 65535] * @coeff_m12: CCM 3x3 coefficient, range [-8192, 8191] * @coeff_m13: CCM 3x3 coefficient, range [-32768, 32767] * @coeff_o_r: Bias 3x1 coefficient, range [-8191, 8181] * @coeff_m21: CCM 3x3 coefficient, range [-32767, 32767] * @coeff_m22: CCM 3x3 coefficient, range [-8192, 8191] * @coeff_m23: CCM 3x3 coefficient, range [-32768, 32767] * @coeff_o_g: Bias 3x1 coefficient, range [-8191, 8181] * @coeff_m31: CCM 3x3 coefficient, range [-32768, 32767] * @coeff_m32: CCM 3x3 coefficient, range [-8192, 8191] * @coeff_m33: CCM 3x3 coefficient, range [-32768, 32767] * @coeff_o_b: Bias 3x1 coefficient, range [-8191, 8181] * * Transform sensor specific color space to standard sRGB by applying 3x3 matrix * and adding a bias vector O. The transformation is basically a rotation and * translation in the 3-dimensional color spaces. Here are the defaults: * * 9775, -2671, 1087, 0 * -1071, 8303, 815, 0 * -23, -7887, 16103, 0 */ struct ipu3_uapi_ccm_mat_config { __s16 coeff_m11; __s16 coeff_m12; __s16 coeff_m13; __s16 coeff_o_r; __s16 coeff_m21; __s16 coeff_m22; __s16 coeff_m23; __s16 coeff_o_g; __s16 coeff_m31; __s16 coeff_m32; __s16 coeff_m33; __s16 coeff_o_b; } __attribute__((packed)); /** * struct ipu3_uapi_gamma_corr_ctrl - Gamma correction * * @enable: gamma correction enable. * @reserved: reserved */ struct ipu3_uapi_gamma_corr_ctrl { __u32 enable:1; __u32 reserved:31; } __attribute__((packed)); /** * struct ipu3_uapi_gamma_corr_lut - Per-pixel tone mapping implemented as LUT. * * @lut: 256 tabulated values of the gamma function. LUT[1].. LUT[256] * format u13.0, range [0, 8191]. * * The tone mapping operation is done by a Piece wise linear graph * that is implemented as a lookup table(LUT). The pixel component input * intensity is the X-axis of the graph which is the table entry. */ struct ipu3_uapi_gamma_corr_lut { __u16 lut[IPU3_UAPI_GAMMA_CORR_LUT_ENTRIES]; } __attribute__((packed)); /** * struct ipu3_uapi_gamma_config - Gamma config * * @gc_ctrl: control of gamma correction &ipu3_uapi_gamma_corr_ctrl * @gc_lut: lookup table of gamma correction &ipu3_uapi_gamma_corr_lut */ struct ipu3_uapi_gamma_config { struct ipu3_uapi_gamma_corr_ctrl gc_ctrl __attribute__((aligned(32))); struct ipu3_uapi_gamma_corr_lut gc_lut __attribute__((aligned(32))); } __attribute__((packed)); /** * struct ipu3_uapi_csc_mat_config - Color space conversion matrix config * * @coeff_c11: Conversion matrix value, format s0.14, range [-16384, 16383]. * @coeff_c12: Conversion matrix value, format s0.14, range [-8192, 8191]. * @coeff_c13: Conversion matrix value, format s0.14, range [-16384, 16383]. * @coeff_b1: Bias 3x1 coefficient, s13.0 range [-8192, 8191]. * @coeff_c21: Conversion matrix value, format s0.14, range [-16384, 16383]. * @coeff_c22: Conversion matrix value, format s0.14, range [-8192, 8191]. * @coeff_c23: Conversion matrix value, format s0.14, range [-16384, 16383]. * @coeff_b2: Bias 3x1 coefficient, s13.0 range [-8192, 8191]. * @coeff_c31: Conversion matrix value, format s0.14, range [-16384, 16383]. * @coeff_c32: Conversion matrix value, format s0.14, range [-8192, 8191]. * @coeff_c33: Conversion matrix value, format s0.14, range [-16384, 16383]. * @coeff_b3: Bias 3x1 coefficient, s13.0 range [-8192, 8191]. * * To transform each pixel from RGB to YUV (Y - brightness/luminance, * UV -chroma) by applying the pixel's values by a 3x3 matrix and adding an * optional bias 3x1 vector. Here are the default values for the matrix: * * 4898, 9617, 1867, 0, * -2410, -4732, 7143, 0, * 10076, -8437, -1638, 0, * * (i.e. for real number 0.299, 0.299 * 2^14 becomes 4898.) */ struct ipu3_uapi_csc_mat_config { __s16 coeff_c11; __s16 coeff_c12; __s16 coeff_c13; __s16 coeff_b1; __s16 coeff_c21; __s16 coeff_c22; __s16 coeff_c23; __s16 coeff_b2; __s16 coeff_c31; __s16 coeff_c32; __s16 coeff_c33; __s16 coeff_b3; } __attribute__((packed)); /** * struct ipu3_uapi_cds_params - Chroma down-scaling * * @ds_c00: range [0, 3] * @ds_c01: range [0, 3] * @ds_c02: range [0, 3] * @ds_c03: range [0, 3] * @ds_c10: range [0, 3] * @ds_c11: range [0, 3] * @ds_c12: range [0, 3] * @ds_c13: range [0, 3] * * In case user does not provide, above 4x2 filter will use following defaults: * 1, 3, 3, 1, * 1, 3, 3, 1, * * @ds_nf: Normalization factor for Chroma output downscaling filter, * range 0,4, default 2. * @reserved0: reserved * @csc_en: Color space conversion enable * @uv_bin_output: 0: output YUV 4.2.0, 1: output YUV 4.2.2(default). * @reserved1: reserved */ struct ipu3_uapi_cds_params { __u32 ds_c00:2; __u32 ds_c01:2; __u32 ds_c02:2; __u32 ds_c03:2; __u32 ds_c10:2; __u32 ds_c11:2; __u32 ds_c12:2; __u32 ds_c13:2; __u32 ds_nf:5; __u32 reserved0:3; __u32 csc_en:1; __u32 uv_bin_output:1; __u32 reserved1:6; } __attribute__((packed)); /** * struct ipu3_uapi_shd_grid_config - Bayer shading(darkening) correction * * @width: Grid horizontal dimensions, u8, [8, 128], default 73 * @height: Grid vertical dimensions, u8, [8, 128], default 56 * @block_width_log2: Log2 of the width of the grid cell in pixel count * u4, [0, 15], default value 5. * @reserved0: reserved * @block_height_log2: Log2 of the height of the grid cell in pixel count * u4, [0, 15], default value 6. * @reserved1: reserved * @grid_height_per_slice: SHD_MAX_CELLS_PER_SET/width. * (with SHD_MAX_CELLS_PER_SET = 146). * @x_start: X value of top left corner of sensor relative to ROI * s13, [-4096, 0], default 0, only negative values. * @y_start: Y value of top left corner of sensor relative to ROI * s13, [-4096, 0], default 0, only negative values. */ struct ipu3_uapi_shd_grid_config { /* reg 0 */ __u8 width; __u8 height; __u8 block_width_log2:3; __u8 reserved0:1; __u8 block_height_log2:3; __u8 reserved1:1; __u8 grid_height_per_slice; /* reg 1 */ __s16 x_start; __s16 y_start; } __attribute__((packed)); /** * struct ipu3_uapi_shd_general_config - Shading general config * * @init_set_vrt_offst_ul: set vertical offset, * y_start >> block_height_log2 % grid_height_per_slice. * @shd_enable: shading enable. * @gain_factor: Gain factor. Shift calculated anti shading value. Precision u2. * 0x0 - gain factor [1, 5], means no shift interpolated value. * 0x1 - gain factor [1, 9], means shift interpolated by 1. * 0x2 - gain factor [1, 17], means shift interpolated by 2. * @reserved: reserved * * Correction is performed by multiplying a gain factor for each of the 4 Bayer * channels as a function of the pixel location in the sensor. */ struct ipu3_uapi_shd_general_config { __u32 init_set_vrt_offst_ul:8; __u32 shd_enable:1; __u32 gain_factor:2; __u32 reserved:21; } __attribute__((packed)); /** * struct ipu3_uapi_shd_black_level_config - Black level correction * * @bl_r: Bios values for green red. s11 range [-2048, 2047]. * @bl_gr: Bios values for green blue. s11 range [-2048, 2047]. * @bl_gb: Bios values for red. s11 range [-2048, 2047]. * @bl_b: Bios values for blue. s11 range [-2048, 2047]. */ struct ipu3_uapi_shd_black_level_config { __s16 bl_r; __s16 bl_gr; __s16 bl_gb; __s16 bl_b; } __attribute__((packed)); /** * struct ipu3_uapi_shd_config_static - Shading config static * * @grid: shading grid config &ipu3_uapi_shd_grid_config * @general: shading general config &ipu3_uapi_shd_general_config * @black_level: black level config for shading correction as defined by * &ipu3_uapi_shd_black_level_config */ struct ipu3_uapi_shd_config_static { struct ipu3_uapi_shd_grid_config grid; struct ipu3_uapi_shd_general_config general; struct ipu3_uapi_shd_black_level_config black_level; } __attribute__((packed)); /** * struct ipu3_uapi_shd_lut - Shading gain factor lookup table. * * @sets: array * @sets.r_and_gr: Red and GreenR Lookup table. * @sets.r_and_gr.r: Red shading factor. * @sets.r_and_gr.gr: GreenR shading factor. * @sets.reserved1: reserved * @sets.gb_and_b: GreenB and Blue Lookup table. * @sets.gb_and_b.gb: GreenB shading factor. * @sets.gb_and_b.b: Blue shading factor. * @sets.reserved2: reserved * * Map to shading correction LUT register set. */ struct ipu3_uapi_shd_lut { struct { struct { __u16 r; __u16 gr; } r_and_gr[IPU3_UAPI_SHD_MAX_CELLS_PER_SET]; __u8 reserved1[24]; struct { __u16 gb; __u16 b; } gb_and_b[IPU3_UAPI_SHD_MAX_CELLS_PER_SET]; __u8 reserved2[24]; } sets[IPU3_UAPI_SHD_MAX_CFG_SETS]; } __attribute__((packed)); /** * struct ipu3_uapi_shd_config - Shading config * * @shd: shading static config, see &ipu3_uapi_shd_config_static * @shd_lut: shading lookup table &ipu3_uapi_shd_lut */ struct ipu3_uapi_shd_config { struct ipu3_uapi_shd_config_static shd __attribute__((aligned(32))); struct ipu3_uapi_shd_lut shd_lut __attribute__((aligned(32))); } __attribute__((packed)); /* Image Enhancement Filter directed */ /** * struct ipu3_uapi_iefd_cux2 - IEFd Config Unit 2 parameters * * @x0: X0 point of Config Unit, u9.0, default 0. * @x1: X1 point of Config Unit, u9.0, default 0. * @a01: Slope A of Config Unit, s4.4, default 0. * @b01: Slope B, always 0. * * Calculate weight for blending directed and non-directed denoise elements * * Note: * Each instance of Config Unit needs X coordinate of n points and * slope A factor between points calculated by driver based on calibration * parameters. * * All CU inputs are unsigned, they will be converted to signed when written * to register, i.e. a01 will be written to 9 bit register in s4.4 format. * The data precision s4.4 means 4 bits for integer parts and 4 bits for the * fractional part, the first bit indicates positive or negative value. * For userspace software (commonly the imaging library), the computation for * the CU slope values should be based on the slope resolution 1/16 (binary * 0.0001 - the minimal interval value), the slope value range is [-256, +255]. * This applies to &ipu3_uapi_iefd_cux6_ed, &ipu3_uapi_iefd_cux2_1, * &ipu3_uapi_iefd_cux2_1, &ipu3_uapi_iefd_cux4 and &ipu3_uapi_iefd_cux6_rad. */ struct ipu3_uapi_iefd_cux2 { __u32 x0:9; __u32 x1:9; __u32 a01:9; __u32 b01:5; } __attribute__((packed)); /** * struct ipu3_uapi_iefd_cux6_ed - Calculate power of non-directed sharpening * element, Config Unit 6 for edge detail (ED). * * @x0: X coordinate of point 0, u9.0, default 0. * @x1: X coordinate of point 1, u9.0, default 0. * @x2: X coordinate of point 2, u9.0, default 0. * @reserved0: reserved * @x3: X coordinate of point 3, u9.0, default 0. * @x4: X coordinate of point 4, u9.0, default 0. * @x5: X coordinate of point 5, u9.0, default 0. * @reserved1: reserved * @a01: slope A points 01, s4.4, default 0. * @a12: slope A points 12, s4.4, default 0. * @a23: slope A points 23, s4.4, default 0. * @reserved2: reserved * @a34: slope A points 34, s4.4, default 0. * @a45: slope A points 45, s4.4, default 0. * @reserved3: reserved * @b01: slope B points 01, s4.4, default 0. * @b12: slope B points 12, s4.4, default 0. * @b23: slope B points 23, s4.4, default 0. * @reserved4: reserved * @b34: slope B points 34, s4.4, default 0. * @b45: slope B points 45, s4.4, default 0. * @reserved5: reserved. */ struct ipu3_uapi_iefd_cux6_ed { __u32 x0:9; __u32 x1:9; __u32 x2:9; __u32 reserved0:5; __u32 x3:9; __u32 x4:9; __u32 x5:9; __u32 reserved1:5; __u32 a01:9; __u32 a12:9; __u32 a23:9; __u32 reserved2:5; __u32 a34:9; __u32 a45:9; __u32 reserved3:14; __u32 b01:9; __u32 b12:9; __u32 b23:9; __u32 reserved4:5; __u32 b34:9; __u32 b45:9; __u32 reserved5:14; } __attribute__((packed)); /** * struct ipu3_uapi_iefd_cux2_1 - Calculate power of non-directed denoise * element apply. * @x0: X0 point of Config Unit, u9.0, default 0. * @x1: X1 point of Config Unit, u9.0, default 0. * @a01: Slope A of Config Unit, s4.4, default 0. * @reserved1: reserved * @b01: offset B0 of Config Unit, u7.0, default 0. * @reserved2: reserved */ struct ipu3_uapi_iefd_cux2_1 { __u32 x0:9; __u32 x1:9; __u32 a01:9; __u32 reserved1:5; __u32 b01:8; __u32 reserved2:24; } __attribute__((packed)); /** * struct ipu3_uapi_iefd_cux4 - Calculate power of non-directed sharpening * element. * * @x0: X0 point of Config Unit, u9.0, default 0. * @x1: X1 point of Config Unit, u9.0, default 0. * @x2: X2 point of Config Unit, u9.0, default 0. * @reserved0: reserved * @x3: X3 point of Config Unit, u9.0, default 0. * @a01: Slope A0 of Config Unit, s4.4, default 0. * @a12: Slope A1 of Config Unit, s4.4, default 0. * @reserved1: reserved * @a23: Slope A2 of Config Unit, s4.4, default 0. * @b01: Offset B0 of Config Unit, s7.0, default 0. * @b12: Offset B1 of Config Unit, s7.0, default 0. * @reserved2: reserved * @b23: Offset B2 of Config Unit, s7.0, default 0. * @reserved3: reserved */ struct ipu3_uapi_iefd_cux4 { __u32 x0:9; __u32 x1:9; __u32 x2:9; __u32 reserved0:5; __u32 x3:9; __u32 a01:9; __u32 a12:9; __u32 reserved1:5; __u32 a23:9; __u32 b01:8; __u32 b12:8; __u32 reserved2:7; __u32 b23:8; __u32 reserved3:24; } __attribute__((packed)); /** * struct ipu3_uapi_iefd_cux6_rad - Radial Config Unit (CU) * * @x0: x0 points of Config Unit radial, u8.0 * @x1: x1 points of Config Unit radial, u8.0 * @x2: x2 points of Config Unit radial, u8.0 * @x3: x3 points of Config Unit radial, u8.0 * @x4: x4 points of Config Unit radial, u8.0 * @x5: x5 points of Config Unit radial, u8.0 * @reserved1: reserved * @a01: Slope A of Config Unit radial, s7.8 * @a12: Slope A of Config Unit radial, s7.8 * @a23: Slope A of Config Unit radial, s7.8 * @a34: Slope A of Config Unit radial, s7.8 * @a45: Slope A of Config Unit radial, s7.8 * @reserved2: reserved * @b01: Slope B of Config Unit radial, s9.0 * @b12: Slope B of Config Unit radial, s9.0 * @b23: Slope B of Config Unit radial, s9.0 * @reserved4: reserved * @b34: Slope B of Config Unit radial, s9.0 * @b45: Slope B of Config Unit radial, s9.0 * @reserved5: reserved */ struct ipu3_uapi_iefd_cux6_rad { __u32 x0:8; __u32 x1:8; __u32 x2:8; __u32 x3:8; __u32 x4:8; __u32 x5:8; __u32 reserved1:16; __u32 a01:16; __u32 a12:16; __u32 a23:16; __u32 a34:16; __u32 a45:16; __u32 reserved2:16; __u32 b01:10; __u32 b12:10; __u32 b23:10; __u32 reserved4:2; __u32 b34:10; __u32 b45:10; __u32 reserved5:12; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_cfg_units - IEFd Config Units parameters * * @cu_1: calculate weight for blending directed and * non-directed denoise elements. See &ipu3_uapi_iefd_cux2 * @cu_ed: calculate power of non-directed sharpening element, see * &ipu3_uapi_iefd_cux6_ed * @cu_3: calculate weight for blending directed and * non-directed denoise elements. A &ipu3_uapi_iefd_cux2 * @cu_5: calculate power of non-directed denoise element apply, use * &ipu3_uapi_iefd_cux2_1 * @cu_6: calculate power of non-directed sharpening element. See * &ipu3_uapi_iefd_cux4 * @cu_7: calculate weight for blending directed and * non-directed denoise elements. Use &ipu3_uapi_iefd_cux2 * @cu_unsharp: Config Unit of unsharp &ipu3_uapi_iefd_cux4 * @cu_radial: Config Unit of radial &ipu3_uapi_iefd_cux6_rad * @cu_vssnlm: Config Unit of vssnlm &ipu3_uapi_iefd_cux2 */ struct ipu3_uapi_yuvp1_iefd_cfg_units { struct ipu3_uapi_iefd_cux2 cu_1; struct ipu3_uapi_iefd_cux6_ed cu_ed; struct ipu3_uapi_iefd_cux2 cu_3; struct ipu3_uapi_iefd_cux2_1 cu_5; struct ipu3_uapi_iefd_cux4 cu_6; struct ipu3_uapi_iefd_cux2 cu_7; struct ipu3_uapi_iefd_cux4 cu_unsharp; struct ipu3_uapi_iefd_cux6_rad cu_radial; struct ipu3_uapi_iefd_cux2 cu_vssnlm; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_config_s - IEFd config * * @horver_diag_coeff: Gradient compensation. Compared with vertical / * horizontal (0 / 90 degree), coefficient of diagonal (45 / * 135 degree) direction should be corrected by approx. * 1/sqrt(2). * @reserved0: reserved * @clamp_stitch: Slope to stitch between clamped and unclamped edge values * @reserved1: reserved * @direct_metric_update: Update coeff for direction metric * @reserved2: reserved * @ed_horver_diag_coeff: Radial Coefficient that compensates for * different distance for vertical/horizontal and * diagonal gradient calculation (approx. 1/sqrt(2)) * @reserved3: reserved */ struct ipu3_uapi_yuvp1_iefd_config_s { __u32 horver_diag_coeff:7; __u32 reserved0:1; __u32 clamp_stitch:6; __u32 reserved1:2; __u32 direct_metric_update:5; __u32 reserved2:3; __u32 ed_horver_diag_coeff:7; __u32 reserved3:1; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_control - IEFd control * * @iefd_en: Enable IEFd * @denoise_en: Enable denoise * @direct_smooth_en: Enable directional smooth * @rad_en: Enable radial update * @vssnlm_en: Enable VSSNLM output filter * @reserved: reserved */ struct ipu3_uapi_yuvp1_iefd_control { __u32 iefd_en:1; __u32 denoise_en:1; __u32 direct_smooth_en:1; __u32 rad_en:1; __u32 vssnlm_en:1; __u32 reserved:27; } __attribute__((packed)); /** * struct ipu3_uapi_sharp_cfg - Sharpening config * * @nega_lmt_txt: Sharpening limit for negative overshoots for texture. * @reserved0: reserved * @posi_lmt_txt: Sharpening limit for positive overshoots for texture. * @reserved1: reserved * @nega_lmt_dir: Sharpening limit for negative overshoots for direction (edge). * @reserved2: reserved * @posi_lmt_dir: Sharpening limit for positive overshoots for direction (edge). * @reserved3: reserved * * Fixed point type u13.0, range [0, 8191]. */ struct ipu3_uapi_sharp_cfg { __u32 nega_lmt_txt:13; __u32 reserved0:19; __u32 posi_lmt_txt:13; __u32 reserved1:19; __u32 nega_lmt_dir:13; __u32 reserved2:19; __u32 posi_lmt_dir:13; __u32 reserved3:19; } __attribute__((packed)); /** * struct ipu3_uapi_far_w - Sharpening config for far sub-group * * @dir_shrp: Weight of wide direct sharpening, u1.6, range [0, 64], default 64. * @reserved0: reserved * @dir_dns: Weight of wide direct denoising, u1.6, range [0, 64], default 0. * @reserved1: reserved * @ndir_dns_powr: Power of non-direct denoising, * Precision u1.6, range [0, 64], default 64. * @reserved2: reserved */ struct ipu3_uapi_far_w { __u32 dir_shrp:7; __u32 reserved0:1; __u32 dir_dns:7; __u32 reserved1:1; __u32 ndir_dns_powr:7; __u32 reserved2:9; } __attribute__((packed)); /** * struct ipu3_uapi_unsharp_cfg - Unsharp config * * @unsharp_weight: Unsharp mask blending weight. * u1.6, range [0, 64], default 16. * 0 - disabled, 64 - use only unsharp. * @reserved0: reserved * @unsharp_amount: Unsharp mask amount, u4.5, range [0, 511], default 0. * @reserved1: reserved */ struct ipu3_uapi_unsharp_cfg { __u32 unsharp_weight:7; __u32 reserved0:1; __u32 unsharp_amount:9; __u32 reserved1:15; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_shrp_cfg - IEFd sharpness config * * @cfg: sharpness config &ipu3_uapi_sharp_cfg * @far_w: wide range config, value as specified by &ipu3_uapi_far_w: * The 5x5 environment is separated into 2 sub-groups, the 3x3 nearest * neighbors (8 pixels called Near), and the second order neighborhood * around them (16 pixels called Far). * @unshrp_cfg: unsharpness config. &ipu3_uapi_unsharp_cfg */ struct ipu3_uapi_yuvp1_iefd_shrp_cfg { struct ipu3_uapi_sharp_cfg cfg; struct ipu3_uapi_far_w far_w; struct ipu3_uapi_unsharp_cfg unshrp_cfg; } __attribute__((packed)); /** * struct ipu3_uapi_unsharp_coef0 - Unsharp mask coefficients * * @c00: Coeff11, s0.8, range [-255, 255], default 1. * @c01: Coeff12, s0.8, range [-255, 255], default 5. * @c02: Coeff13, s0.8, range [-255, 255], default 9. * @reserved: reserved * * Configurable registers for common sharpening support. */ struct ipu3_uapi_unsharp_coef0 { __u32 c00:9; __u32 c01:9; __u32 c02:9; __u32 reserved:5; } __attribute__((packed)); /** * struct ipu3_uapi_unsharp_coef1 - Unsharp mask coefficients * * @c11: Coeff22, s0.8, range [-255, 255], default 29. * @c12: Coeff23, s0.8, range [-255, 255], default 55. * @c22: Coeff33, s0.8, range [-255, 255], default 96. * @reserved: reserved */ struct ipu3_uapi_unsharp_coef1 { __u32 c11:9; __u32 c12:9; __u32 c22:9; __u32 reserved:5; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_unshrp_cfg - Unsharp mask config * * @unsharp_coef0: unsharp coefficient 0 config. See &ipu3_uapi_unsharp_coef0 * @unsharp_coef1: unsharp coefficient 1 config. See &ipu3_uapi_unsharp_coef1 */ struct ipu3_uapi_yuvp1_iefd_unshrp_cfg { struct ipu3_uapi_unsharp_coef0 unsharp_coef0; struct ipu3_uapi_unsharp_coef1 unsharp_coef1; } __attribute__((packed)); /** * struct ipu3_uapi_radial_reset_xy - Radial coordinate reset * * @x: Radial reset of x coordinate. Precision s12, [-4095, 4095], default 0. * @reserved0: reserved * @y: Radial center y coordinate. Precision s12, [-4095, 4095], default 0. * @reserved1: reserved */ struct ipu3_uapi_radial_reset_xy { __s32 x:13; __u32 reserved0:3; __s32 y:13; __u32 reserved1:3; } __attribute__((packed)); /** * struct ipu3_uapi_radial_reset_x2 - Radial X^2 reset * * @x2: Radial reset of x^2 coordinate. Precision u24, default 0. * @reserved: reserved */ struct ipu3_uapi_radial_reset_x2 { __u32 x2:24; __u32 reserved:8; } __attribute__((packed)); /** * struct ipu3_uapi_radial_reset_y2 - Radial Y^2 reset * * @y2: Radial reset of y^2 coordinate. Precision u24, default 0. * @reserved: reserved */ struct ipu3_uapi_radial_reset_y2 { __u32 y2:24; __u32 reserved:8; } __attribute__((packed)); /** * struct ipu3_uapi_radial_cfg - Radial config * * @rad_nf: Radial. R^2 normalization factor is scale down by 2^ - (15 + scale) * @reserved0: reserved * @rad_inv_r2: Radial R^-2 normelized to (0.5..1). * Precision u7, range [0, 127]. * @reserved1: reserved */ struct ipu3_uapi_radial_cfg { __u32 rad_nf:4; __u32 reserved0:4; __u32 rad_inv_r2:7; __u32 reserved1:17; } __attribute__((packed)); /** * struct ipu3_uapi_rad_far_w - Radial FAR sub-group * * @rad_dir_far_sharp_w: Weight of wide direct sharpening, u1.6, range [0, 64], * default 64. * @rad_dir_far_dns_w: Weight of wide direct denoising, u1.6, range [0, 64], * default 0. * @rad_ndir_far_dns_power: power of non-direct sharpening, u1.6, range [0, 64], * default 0. * @reserved: reserved */ struct ipu3_uapi_rad_far_w { __u32 rad_dir_far_sharp_w:8; __u32 rad_dir_far_dns_w:8; __u32 rad_ndir_far_dns_power:8; __u32 reserved:8; } __attribute__((packed)); /** * struct ipu3_uapi_cu_cfg0 - Radius Config Unit cfg0 register * * @cu6_pow: Power of CU6. Power of non-direct sharpening, u3.4. * @reserved0: reserved * @cu_unsharp_pow: Power of unsharp mask, u2.4. * @reserved1: reserved * @rad_cu6_pow: Radial/corner CU6. Directed sharpening power, u3.4. * @reserved2: reserved * @rad_cu_unsharp_pow: Radial power of unsharp mask, u2.4. * @reserved3: reserved */ struct ipu3_uapi_cu_cfg0 { __u32 cu6_pow:7; __u32 reserved0:1; __u32 cu_unsharp_pow:7; __u32 reserved1:1; __u32 rad_cu6_pow:7; __u32 reserved2:1; __u32 rad_cu_unsharp_pow:6; __u32 reserved3:2; } __attribute__((packed)); /** * struct ipu3_uapi_cu_cfg1 - Radius Config Unit cfg1 register * * @rad_cu6_x1: X1 point of Config Unit 6, precision u9.0. * @reserved0: reserved * @rad_cu_unsharp_x1: X1 point for Config Unit unsharp for radial/corner point * precision u9.0. * @reserved1: reserved */ struct ipu3_uapi_cu_cfg1 { __u32 rad_cu6_x1:9; __u32 reserved0:1; __u32 rad_cu_unsharp_x1:9; __u32 reserved1:13; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_rad_cfg - IEFd parameters changed radially over * the picture plane. * * @reset_xy: reset xy value in radial calculation. &ipu3_uapi_radial_reset_xy * @reset_x2: reset x square value in radial calculation. See struct * &ipu3_uapi_radial_reset_x2 * @reset_y2: reset y square value in radial calculation. See struct * &ipu3_uapi_radial_reset_y2 * @cfg: radial config defined in &ipu3_uapi_radial_cfg * @rad_far_w: weight for wide range radial. &ipu3_uapi_rad_far_w * @cu_cfg0: configuration unit 0. See &ipu3_uapi_cu_cfg0 * @cu_cfg1: configuration unit 1. See &ipu3_uapi_cu_cfg1 */ struct ipu3_uapi_yuvp1_iefd_rad_cfg { struct ipu3_uapi_radial_reset_xy reset_xy; struct ipu3_uapi_radial_reset_x2 reset_x2; struct ipu3_uapi_radial_reset_y2 reset_y2; struct ipu3_uapi_radial_cfg cfg; struct ipu3_uapi_rad_far_w rad_far_w; struct ipu3_uapi_cu_cfg0 cu_cfg0; struct ipu3_uapi_cu_cfg1 cu_cfg1; } __attribute__((packed)); /* Vssnlm - Very small scale non-local mean algorithm */ /** * struct ipu3_uapi_vss_lut_x - Vssnlm LUT x0/x1/x2 * * @vs_x0: Vssnlm LUT x0, precision u8, range [0, 255], default 16. * @vs_x1: Vssnlm LUT x1, precision u8, range [0, 255], default 32. * @vs_x2: Vssnlm LUT x2, precision u8, range [0, 255], default 64. * @reserved2: reserved */ struct ipu3_uapi_vss_lut_x { __u32 vs_x0:8; __u32 vs_x1:8; __u32 vs_x2:8; __u32 reserved2:8; } __attribute__((packed)); /** * struct ipu3_uapi_vss_lut_y - Vssnlm LUT y0/y1/y2 * * @vs_y1: Vssnlm LUT y1, precision u4, range [0, 8], default 1. * @reserved0: reserved * @vs_y2: Vssnlm LUT y2, precision u4, range [0, 8], default 3. * @reserved1: reserved * @vs_y3: Vssnlm LUT y3, precision u4, range [0, 8], default 8. * @reserved2: reserved */ struct ipu3_uapi_vss_lut_y { __u32 vs_y1:4; __u32 reserved0:4; __u32 vs_y2:4; __u32 reserved1:4; __u32 vs_y3:4; __u32 reserved2:12; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_vssnlm_cfg - IEFd Vssnlm Lookup table * * @vss_lut_x: vss lookup table. See &ipu3_uapi_vss_lut_x description * @vss_lut_y: vss lookup table. See &ipu3_uapi_vss_lut_y description */ struct ipu3_uapi_yuvp1_iefd_vssnlm_cfg { struct ipu3_uapi_vss_lut_x vss_lut_x; struct ipu3_uapi_vss_lut_y vss_lut_y; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_iefd_config - IEFd config * * @units: configuration unit setting, &ipu3_uapi_yuvp1_iefd_cfg_units * @config: configuration, as defined by &ipu3_uapi_yuvp1_iefd_config_s * @control: control setting, as defined by &ipu3_uapi_yuvp1_iefd_control * @sharp: sharpness setting, as defined by &ipu3_uapi_yuvp1_iefd_shrp_cfg * @unsharp: unsharpness setting, as defined by &ipu3_uapi_yuvp1_iefd_unshrp_cfg * @rad: radial setting, as defined by &ipu3_uapi_yuvp1_iefd_rad_cfg * @vsslnm: vsslnm setting, as defined by &ipu3_uapi_yuvp1_iefd_vssnlm_cfg */ struct ipu3_uapi_yuvp1_iefd_config { struct ipu3_uapi_yuvp1_iefd_cfg_units units; struct ipu3_uapi_yuvp1_iefd_config_s config; struct ipu3_uapi_yuvp1_iefd_control control; struct ipu3_uapi_yuvp1_iefd_shrp_cfg sharp; struct ipu3_uapi_yuvp1_iefd_unshrp_cfg unsharp; struct ipu3_uapi_yuvp1_iefd_rad_cfg rad; struct ipu3_uapi_yuvp1_iefd_vssnlm_cfg vsslnm; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_yds_config - Y Down-Sampling config * * @c00: range [0, 3], default 0x0 * @c01: range [0, 3], default 0x1 * @c02: range [0, 3], default 0x1 * @c03: range [0, 3], default 0x0 * @c10: range [0, 3], default 0x0 * @c11: range [0, 3], default 0x1 * @c12: range [0, 3], default 0x1 * @c13: range [0, 3], default 0x0 * * Above are 4x2 filter coefficients for chroma output downscaling. * * @norm_factor: Normalization factor, range [0, 4], default 2 * 0 - divide by 1 * 1 - divide by 2 * 2 - divide by 4 * 3 - divide by 8 * 4 - divide by 16 * @reserved0: reserved * @bin_output: Down sampling on Luma channel in two optional modes * 0 - Bin output 4.2.0 (default), 1 output 4.2.2. * @reserved1: reserved */ struct ipu3_uapi_yuvp1_yds_config { __u32 c00:2; __u32 c01:2; __u32 c02:2; __u32 c03:2; __u32 c10:2; __u32 c11:2; __u32 c12:2; __u32 c13:2; __u32 norm_factor:5; __u32 reserved0:4; __u32 bin_output:1; __u32 reserved1:6; } __attribute__((packed)); /* Chroma Noise Reduction */ /** * struct ipu3_uapi_yuvp1_chnr_enable_config - Chroma noise reduction enable * * @enable: enable/disable chroma noise reduction * @yuv_mode: 0 - YUV420, 1 - YUV422 * @reserved0: reserved * @col_size: number of columns in the frame, max width is 2560 * @reserved1: reserved */ struct ipu3_uapi_yuvp1_chnr_enable_config { __u32 enable:1; __u32 yuv_mode:1; __u32 reserved0:14; __u32 col_size:12; __u32 reserved1:4; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_chnr_coring_config - Coring thresholds for UV * * @u: U coring level, u0.13, range [0.0, 1.0], default 0.0 * @reserved0: reserved * @v: V coring level, u0.13, range [0.0, 1.0], default 0.0 * @reserved1: reserved */ struct ipu3_uapi_yuvp1_chnr_coring_config { __u32 u:13; __u32 reserved0:3; __u32 v:13; __u32 reserved1:3; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_chnr_sense_gain_config - Chroma noise reduction gains * * All sensitivity gain parameters have precision u13.0, range [0, 8191]. * * @vy: Sensitivity of horizontal edge of Y, default 100 * @vu: Sensitivity of horizontal edge of U, default 100 * @vv: Sensitivity of horizontal edge of V, default 100 * @reserved0: reserved * @hy: Sensitivity of vertical edge of Y, default 50 * @hu: Sensitivity of vertical edge of U, default 50 * @hv: Sensitivity of vertical edge of V, default 50 * @reserved1: reserved */ struct ipu3_uapi_yuvp1_chnr_sense_gain_config { __u32 vy:8; __u32 vu:8; __u32 vv:8; __u32 reserved0:8; __u32 hy:8; __u32 hu:8; __u32 hv:8; __u32 reserved1:8; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_chnr_iir_fir_config - Chroma IIR/FIR filter config * * @fir_0h: Value of center tap in horizontal FIR, range [0, 32], default 8. * @reserved0: reserved * @fir_1h: Value of distance 1 in horizontal FIR, range [0, 32], default 12. * @reserved1: reserved * @fir_2h: Value of distance 2 tap in horizontal FIR, range [0, 32], default 0. * @dalpha_clip_val: weight for previous row in IIR, range [1, 256], default 0. * @reserved2: reserved */ struct ipu3_uapi_yuvp1_chnr_iir_fir_config { __u32 fir_0h:6; __u32 reserved0:2; __u32 fir_1h:6; __u32 reserved1:2; __u32 fir_2h:6; __u32 dalpha_clip_val:9; __u32 reserved2:1; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_chnr_config - Chroma noise reduction config * * @enable: chroma noise reduction enable, see * &ipu3_uapi_yuvp1_chnr_enable_config * @coring: coring config for chroma noise reduction, see * &ipu3_uapi_yuvp1_chnr_coring_config * @sense_gain: sensitivity config for chroma noise reduction, see * ipu3_uapi_yuvp1_chnr_sense_gain_config * @iir_fir: iir and fir config for chroma noise reduction, see * ipu3_uapi_yuvp1_chnr_iir_fir_config */ struct ipu3_uapi_yuvp1_chnr_config { struct ipu3_uapi_yuvp1_chnr_enable_config enable; struct ipu3_uapi_yuvp1_chnr_coring_config coring; struct ipu3_uapi_yuvp1_chnr_sense_gain_config sense_gain; struct ipu3_uapi_yuvp1_chnr_iir_fir_config iir_fir; } __attribute__((packed)); /* Edge Enhancement and Noise Reduction */ /** * struct ipu3_uapi_yuvp1_y_ee_nr_lpf_config - Luma(Y) edge enhancement low-pass * filter coefficients * * @a_diag: Smoothing diagonal coefficient, u5.0. * @reserved0: reserved * @a_periph: Image smoothing perpherial, u5.0. * @reserved1: reserved * @a_cent: Image Smoothing center coefficient, u5.0. * @reserved2: reserved * @enable: 0: Y_EE_NR disabled, output = input; 1: Y_EE_NR enabled. */ struct ipu3_uapi_yuvp1_y_ee_nr_lpf_config { __u32 a_diag:5; __u32 reserved0:3; __u32 a_periph:5; __u32 reserved1:3; __u32 a_cent:5; __u32 reserved2:9; __u32 enable:1; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_y_ee_nr_sense_config - Luma(Y) edge enhancement * noise reduction sensitivity gains * * @edge_sense_0: Sensitivity of edge in dark area. u13.0, default 8191. * @reserved0: reserved * @delta_edge_sense: Difference in the sensitivity of edges between * the bright and dark areas. u13.0, default 0. * @reserved1: reserved * @corner_sense_0: Sensitivity of corner in dark area. u13.0, default 0. * @reserved2: reserved * @delta_corner_sense: Difference in the sensitivity of corners between * the bright and dark areas. u13.0, default 8191. * @reserved3: reserved */ struct ipu3_uapi_yuvp1_y_ee_nr_sense_config { __u32 edge_sense_0:13; __u32 reserved0:3; __u32 delta_edge_sense:13; __u32 reserved1:3; __u32 corner_sense_0:13; __u32 reserved2:3; __u32 delta_corner_sense:13; __u32 reserved3:3; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_y_ee_nr_gain_config - Luma(Y) edge enhancement * noise reduction gain config * * @gain_pos_0: Gain for positive edge in dark area. u5.0, [0, 16], default 2. * @reserved0: reserved * @delta_gain_posi: Difference in the gain of edges between the bright and * dark areas for positive edges. u5.0, [0, 16], default 0. * @reserved1: reserved * @gain_neg_0: Gain for negative edge in dark area. u5.0, [0, 16], default 8. * @reserved2: reserved * @delta_gain_neg: Difference in the gain of edges between the bright and * dark areas for negative edges. u5.0, [0, 16], default 0. * @reserved3: reserved */ struct ipu3_uapi_yuvp1_y_ee_nr_gain_config { __u32 gain_pos_0:5; __u32 reserved0:3; __u32 delta_gain_posi:5; __u32 reserved1:3; __u32 gain_neg_0:5; __u32 reserved2:3; __u32 delta_gain_neg:5; __u32 reserved3:3; } __attribute__((packed)); /** * struct ipu3_uapi_yuvp1_y_ee_nr_clip_config - Luma(Y) edge enhancement * noise reduction clipping config