summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ipa/raspberrypi/raspberrypi.cpp57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp
index e76688a7..ef8b19ba 100644
--- a/src/ipa/raspberrypi/raspberrypi.cpp
+++ b/src/ipa/raspberrypi/raspberrypi.cpp
@@ -43,6 +43,7 @@
#include "contrast_algorithm.hpp"
#include "contrast_status.h"
#include "controller.hpp"
+#include "denoise_algorithm.hpp"
#include "denoise_status.h"
#include "dpc_status.h"
#include "focus_status.h"
@@ -573,6 +574,7 @@ bool IPARPi::validateIspControls()
V4L2_CID_USER_BCM2835_ISP_SHARPEN,
V4L2_CID_USER_BCM2835_ISP_DPC,
V4L2_CID_USER_BCM2835_ISP_LENS_SHADING,
+ V4L2_CID_USER_BCM2835_ISP_CDN,
};
for (auto c : ctrls) {
@@ -622,6 +624,14 @@ static const std::map<int32_t, std::string> AwbModeTable = {
{ controls::AwbCustom, "custom" },
};
+static const std::map<int32_t, RPiController::DenoiseMode> DenoiseModeTable = {
+ { controls::draft::NoiseReductionModeOff, RPiController::DenoiseMode::Off },
+ { controls::draft::NoiseReductionModeFast, RPiController::DenoiseMode::ColourFast },
+ { controls::draft::NoiseReductionModeHighQuality, RPiController::DenoiseMode::ColourHighQuality },
+ { controls::draft::NoiseReductionModeMinimal, RPiController::DenoiseMode::ColourOff },
+ { controls::draft::NoiseReductionModeZSL, RPiController::DenoiseMode::ColourHighQuality },
+};
+
void IPARPi::queueRequest(const ControlList &controls)
{
/* Clear the return metadata buffer. */
@@ -889,6 +899,29 @@ void IPARPi::queueRequest(const ControlList &controls)
break;
}
+ case controls::NOISE_REDUCTION_MODE: {
+ RPiController::DenoiseAlgorithm *sdn = dynamic_cast<RPiController::DenoiseAlgorithm *>(
+ controller_.GetAlgorithm("SDN"));
+ ASSERT(sdn);
+
+ int32_t idx = ctrl.second.get<int32_t>();
+ auto mode = DenoiseModeTable.find(idx);
+ if (mode != DenoiseModeTable.end()) {
+ sdn->SetMode(mode->second);
+
+ /*
+ * \todo If the colour denoise is not going to run due to an
+ * analysis image resolution or format mismatch, we should
+ * report the status correctly in the metadata.
+ */
+ libcameraMetadata_.set(controls::draft::NoiseReductionMode, idx);
+ } else {
+ LOG(IPARPI, Error) << "Noise reduction mode " << idx
+ << " not recognised";
+ }
+ break;
+ }
+
default:
LOG(IPARPI, Warning)
<< "Ctrl " << controls::controls.at(ctrl.first)->name()
@@ -1173,18 +1206,40 @@ void IPARPi::applyGEQ(const struct GeqStatus *geqStatus, ControlList &ctrls)
void IPARPi::applyDenoise(const struct DenoiseStatus *denoiseStatus, ControlList &ctrls)
{
+ using RPiController::DenoiseMode;
+
bcm2835_isp_denoise denoise;
+ DenoiseMode mode = static_cast<DenoiseMode>(denoiseStatus->mode);
- denoise.enabled = 1;
+ denoise.enabled = mode != DenoiseMode::Off;
denoise.constant = denoiseStatus->noise_constant;
denoise.slope.num = 1000 * denoiseStatus->noise_slope;
denoise.slope.den = 1000;
denoise.strength.num = 1000 * denoiseStatus->strength;
denoise.strength.den = 1000;
+ /* Set the CDN mode to match the SDN operating mode. */
+ bcm2835_isp_cdn cdn;
+ switch (mode) {
+ case DenoiseMode::ColourFast:
+ cdn.enabled = 1;
+ cdn.mode = CDN_MODE_FAST;
+ break;
+ case DenoiseMode::ColourHighQuality:
+ cdn.enabled = 1;
+ cdn.mode = CDN_MODE_HIGH_QUALITY;
+ break;
+ default:
+ cdn.enabled = 0;
+ }
+
ControlValue c(Span<const uint8_t>{ reinterpret_cast<uint8_t *>(&denoise),
sizeof(denoise) });
ctrls.set(V4L2_CID_USER_BCM2835_ISP_DENOISE, c);
+
+ c = ControlValue(Span<const uint8_t>{ reinterpret_cast<uint8_t *>(&cdn),
+ sizeof(cdn) });
+ ctrls.set(V4L2_CID_USER_BCM2835_ISP_CDN, c);
}
void IPARPi::applySharpen(const struct SharpenStatus *sharpenStatus, ControlList &ctrls)
264' href='#n264'>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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * exif.cpp - EXIF tag creation using libexif
 */

#include "exif.h"

#include <cmath>
#include <iomanip>
#include <map>
#include <sstream>
#include <tuple>
#include <uchar.h>

#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>

using namespace libcamera;

LOG_DEFINE_CATEGORY(EXIF)

/*
 * List of EXIF tags that we set directly because they are not supported
 * by libexif version 0.6.21.
 */
enum class _ExifTag {
	OFFSET_TIME              = 0x9010,
	OFFSET_TIME_ORIGINAL     = 0x9011,
	OFFSET_TIME_DIGITIZED    = 0x9012,
};

/*
 * The Exif class should be instantiated and specific properties set
 * through the exposed public API.
 *
 * Once all desired properties have been set, the user shall call
 * generate() to process the entries and generate the Exif data.
 *
 * Calls to generate() must check the return code to determine if any error
 * occurred during the construction of the Exif data, and if successful the
 * data can be obtained using the data() function.
 */
Exif::Exif()
	: valid_(false), data_(nullptr), order_(EXIF_BYTE_ORDER_INTEL),
	  exifData_(0), size_(0)
{
	/* Create an ExifMem allocator to construct entries. */
	mem_ = exif_mem_new_default();
	if (!mem_) {
		LOG(EXIF, Error) << "Failed to allocate ExifMem Allocator";
		return;
	}

	data_ = exif_data_new_mem(mem_);
	if (!data_) {
		LOG(EXIF, Error) << "Failed to allocate an ExifData structure";
		return;
	}

	valid_ = true;

	exif_data_set_option(data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
	exif_data_set_data_type(data_, EXIF_DATA_TYPE_COMPRESSED);

	/*
	 * Big-Endian: EXIF_BYTE_ORDER_MOTOROLA
	 * Little Endian: EXIF_BYTE_ORDER_INTEL
	 */
	exif_data_set_byte_order(data_, order_);

	setString(EXIF_IFD_EXIF, EXIF_TAG_EXIF_VERSION,
		  EXIF_FORMAT_UNDEFINED, "0231");

	/* Create the mandatory EXIF fields with default data. */
	exif_data_fix(data_);
}

Exif::~Exif()
{
	if (exifData_)
		free(exifData_);

	if (data_) {
		/*
		 * Reset thumbnail data to avoid getting double-freed by
		 * libexif. It is owned by the caller (i.e. PostProcessorJpeg).
		 */
		data_->data = nullptr;
		data_->size = 0;

		exif_data_unref(data_);
	}

	if (mem_)
		exif_mem_unref(mem_);
}

ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag)
{
	ExifContent *content = data_->ifd[ifd];
	ExifEntry *entry = exif_content_get_entry(content, tag);

	if (entry) {
		exif_entry_ref(entry);
		return entry;
	}

	entry = exif_entry_new_mem(mem_);
	if (!entry) {
		LOG(EXIF, Error) << "Failed to allocated new entry";
		valid_ = false;
		return nullptr;
	}

	exif_content_add_entry(content, entry);
	exif_entry_initialize(entry, tag);

	return entry;
}

ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
			     unsigned long components, unsigned int size)
{
	ExifContent *content = data_->ifd[ifd];

	/* Replace any existing entry with the same tag. */
	ExifEntry *existing = exif_content_get_entry(content, tag);
	exif_content_remove_entry(content, existing);

	ExifEntry *entry = exif_entry_new_mem(mem_);
	if (!entry) {
		LOG(EXIF, Error) << "Failed to allocated new entry";
		valid_ = false;
		return nullptr;
	}

	void *buffer = exif_mem_alloc(mem_, size);
	if (!buffer) {
		LOG(EXIF, Error) << "Failed to allocate buffer for variable entry";
		exif_mem_unref(mem_);
		valid_ = false;
		return nullptr;
	}

	entry->data = static_cast<unsigned char *>(buffer);
	entry->components = components;
	entry->format = format;
	entry->size = size;
	entry->tag = tag;

	exif_content_add_entry(content, entry);

	return entry;
}

void Exif::setByte(ExifIfd ifd, ExifTag tag, uint8_t item)
{
	ExifEntry *entry = createEntry(ifd, tag, EXIF_FORMAT_BYTE, 1, 1);
	if (!entry)
		return;

	entry->data[0] = item;
	exif_entry_unref(entry);
}

void Exif::setShort(ExifIfd ifd, ExifTag tag, uint16_t item)
{
	ExifEntry *entry = createEntry(ifd, tag);
	if (!entry)
		return;

	exif_set_short(entry->data, order_, item);
	exif_entry_unref(entry);
}

void Exif::setLong(ExifIfd ifd, ExifTag tag, uint32_t item)
{
	ExifEntry *entry = createEntry(ifd, tag);
	if (!entry)
		return;

	exif_set_long(entry->data, order_, item);
	exif_entry_unref(entry);
}

void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item)
{
	setRational(ifd, tag, { &item, 1 });
}

void Exif::setRational(ExifIfd ifd, ExifTag tag, Span<const ExifRational> items)
{
	ExifEntry *entry = createEntry(ifd, tag, EXIF_FORMAT_RATIONAL,
				       items.size(),
				       items.size() * sizeof(ExifRational));
	if (!entry)
		return;

	for (size_t i = 0; i < items.size(); i++)
		exif_set_rational(entry->data + i * sizeof(ExifRational),
				  order_, items[i]);
	exif_entry_unref(entry);
}

static const std::map<Exif::StringEncoding, std::array<uint8_t, 8>> stringEncodingCodes = {
	{ Exif::ASCII,     { 0x41, 0x53, 0x43, 0x49, 0x49, 0x00, 0x00, 0x00 } },
	{ Exif::Unicode,   { 0x55, 0x4e, 0x49, 0x43, 0x4f, 0x44, 0x45, 0x00 } },
};

void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
		     const std::string &item, StringEncoding encoding)
{
	std::string ascii;
	size_t length;
	const char *str;
	std::vector<uint8_t> buf;

	if (format == EXIF_FORMAT_ASCII) {
		ascii = utils::toAscii(item);
		str = ascii.c_str();

		/* Pad 1 extra byte to null-terminate the ASCII string. */
		length = ascii.length() + 1;
	} else {
		std::u16string u16str;

		auto encodingString = stringEncodingCodes.find(encoding);
		if (encodingString != stringEncodingCodes.end()) {
			buf = {
				encodingString->second.begin(),
				encodingString->second.end()
			};
		}

		switch (encoding) {
		case Unicode:
			u16str = utf8ToUtf16(item);

			buf.resize(8 + u16str.size() * 2);
			for (size_t i = 0; i < u16str.size(); i++) {
				if (order_ == EXIF_BYTE_ORDER_INTEL) {
					buf[8 + 2 * i] = u16str[i] & 0xff;
					buf[8 + 2 * i + 1] = (u16str[i] >> 8) & 0xff;
				} else {
					buf[8 + 2 * i] = (u16str[i] >> 8) & 0xff;
					buf[8 + 2 * i + 1] = u16str[i] & 0xff;
				}
			}

			break;

		case ASCII:
		case NoEncoding:
			buf.insert(buf.end(), item.begin(), item.end());
			break;
		}

		str = reinterpret_cast<const char *>(buf.data());

		/*
		 * Strings stored in different formats (EXIF_FORMAT_UNDEFINED)
		 * are not null-terminated.
		 */
		length = buf.size();
	}

	ExifEntry *entry = createEntry(ifd, tag, format, length, length);
	if (!entry)
		return;

	memcpy(entry->data, str, length);
	exif_entry_unref(entry);
}

void Exif::setMake(const std::string &make)
{
	setString(EXIF_IFD_0, EXIF_TAG_MAKE, EXIF_FORMAT_ASCII, make);
}