summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacopo Mondi <jacopo@jmondi.org>2021-08-12 19:36:55 +0200
committerJacopo Mondi <jacopo@jmondi.org>2021-08-23 12:00:57 +0200
commit2dab5d2a46118c17cd2d31b8f83c63c93839dcb6 (patch)
tree384bfdbe3eb8ff1d0898f1e5ad6e52b360331db1
parent359ade5027d1856066e74f597db930012e92fb6c (diff)
libcamera: controls: Initialize ControlInfoMap::idmap_
The compiler generated constructor does not initialize the ControlInfoMap::idmap_ field. Fix this by explicitly initializing the field in the class declaration. Reported-by: Coverity CID=354657 Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
-rw-r--r--include/libcamera/controls.h2
1 files changed, 1 insertions, 1 deletions
diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h
index 9b0d5a54..6668e4bb 100644
--- a/include/libcamera/controls.h
+++ b/include/libcamera/controls.h
@@ -343,7 +343,7 @@ public:
private:
bool validate();
- const ControlIdMap *idmap_;
+ const ControlIdMap *idmap_ = nullptr;
};
class ControlList
f='#n132'>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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi Ltd
 *
 * cam_helper.cpp - helper information for different sensors
 */

#include <linux/videodev2.h>

#include <assert.h>
#include <map>
#include <string.h>

#include "libcamera/internal/v4l2_videodevice.h"

#include "cam_helper.h"
#include "md_parser.h"

using namespace RPiController;
using namespace libcamera;
using libcamera::utils::Duration;

namespace libcamera {
LOG_DECLARE_CATEGORY(IPARPI)
}

static std::map<std::string, CamHelperCreateFunc> camHelpers;

CamHelper *CamHelper::create(std::string const &camName)
{
	/*
	 * CamHelpers get registered by static RegisterCamHelper
	 * initialisers.
	 */
	for (auto &p : camHelpers) {
		if (camName.find(p.first) != std::string::npos)
			return p.second();
	}

	return nullptr;
}

CamHelper::CamHelper(std::unique_ptr<MdParser> parser, unsigned int frameIntegrationDiff)
	: parser_(std::move(parser)), initialized_(false),
	  frameIntegrationDiff_(frameIntegrationDiff)
{
}

CamHelper::~CamHelper()
{
}

void CamHelper::prepare(Span<const uint8_t> buffer,
			Metadata &metadata)
{
	parseEmbeddedData(buffer, metadata);
}

void CamHelper::process([[maybe_unused]] StatisticsPtr &stats,
			[[maybe_unused]] Metadata &metadata)
{
}

uint32_t CamHelper::exposureLines(const Duration exposure) const
{
	assert(initialized_);
	return exposure / mode_.lineLength;
}

Duration CamHelper::exposure(uint32_t exposureLines) const
{
	assert(initialized_);
	return exposureLines * mode_.lineLength;
}

uint32_t CamHelper::getVBlanking(Duration &exposure,
				 Duration minFrameDuration,
				 Duration maxFrameDuration) const
{
	uint32_t frameLengthMin, frameLengthMax, vblank;
	uint32_t exposureLines = CamHelper::exposureLines(exposure);

	assert(initialized_);

	/*
	 * minFrameDuration and maxFrameDuration are clamped by the caller
	 * based on the limits for the active sensor mode.
	 */
	frameLengthMin = minFrameDuration / mode_.lineLength;
	frameLengthMax = maxFrameDuration / mode_.lineLength;

	/*
	 * Limit the exposure to the maximum frame duration requested, and
	 * re-calculate if it has been clipped.
	 */
	exposureLines = std::min(frameLengthMax - frameIntegrationDiff_, exposureLines);
	exposure = CamHelper::exposure(exposureLines);

	/* Limit the vblank to the range allowed by the frame length limits. */
	vblank = std::clamp(exposureLines + frameIntegrationDiff_,
			    frameLengthMin, frameLengthMax) - mode_.height;
	return vblank;
}

void CamHelper::setCameraMode(const CameraMode &mode)
{
	mode_ = mode;
	if (parser_) {
		parser_->reset();
		parser_->setBitsPerPixel(mode.bitdepth);
		parser_->setLineLengthBytes(0); /* We use SetBufferSize. */
	}
	initialized_ = true;
}

void CamHelper::getDelays(int &exposureDelay, int &gainDelay,
			  int &vblankDelay) const
{
	/*
	 * These values are correct for many sensors. Other sensors will
	 * need to over-ride this function.
	 */
	exposureDelay = 2;
	gainDelay = 1;
	vblankDelay = 2;
}

bool CamHelper::sensorEmbeddedDataPresent() const
{
	return false;
}

double CamHelper::getModeSensitivity([[maybe_unused]] const CameraMode &mode) const
{
	/*
	 * Most sensors have the same sensitivity in every mode, but this
	 * function can be overridden for those that do not. Note that it is
	 * called before mode_ is set, so it must return the sensitivity
	 * of the mode that is passed in.
	 */
	return 1.0;
}

unsigned int CamHelper::hideFramesStartup() const
{
	/*
	 * The number of frames when a camera first starts that shouldn't be
	 * displayed as they are invalid in some way.
	 */
	return 0;
}

unsigned int CamHelper::hideFramesModeSwitch() const
{
	/* After a mode switch, many sensors return valid frames immediately. */
	return 0;
}

unsigned int CamHelper::mistrustFramesStartup() const
{
	/* Many sensors return a single bad frame on start-up. */
	return 1;
}

unsigned int CamHelper::mistrustFramesModeSwitch() const
{
	/* Many sensors return valid metadata immediately. */
	return 0;
}

void CamHelper::parseEmbeddedData(Span<const uint8_t> buffer,
				  Metadata &metadata)
{
	MdParser::RegisterMap registers;
	Metadata parsedMetadata;

	if (buffer.empty())
		return;

	if (parser_->parse(buffer, registers) != MdParser::Status::OK) {
		LOG(IPARPI, Error) << "Embedded data buffer parsing failed";
		return;
	}

	populateMetadata(registers, parsedMetadata);
	metadata.merge(parsedMetadata);

	/*
	 * Overwrite the exposure/gain, frame length and sensor temperature values
	 * in the existing DeviceStatus with values from the parsed embedded buffer.
	 * Fetch it first in case any other fields were set meaningfully.
	 */
	DeviceStatus deviceStatus, parsedDeviceStatus;
	if (metadata.get("device.status", deviceStatus) ||
	    parsedMetadata.get("device.status", parsedDeviceStatus)) {
		LOG(IPARPI, Error) << "DeviceStatus not found";
		return;
	}

	deviceStatus.shutterSpeed = parsedDeviceStatus.shutterSpeed;
	deviceStatus.analogueGain = parsedDeviceStatus.analogueGain;
	deviceStatus.frameLength = parsedDeviceStatus.frameLength;
	if (parsedDeviceStatus.sensorTemperature)
		deviceStatus.sensorTemperature = parsedDeviceStatus.sensorTemperature;

	LOG(IPARPI, Debug) << "Metadata updated - " << deviceStatus;

	metadata.set("device.status", deviceStatus);
}

void CamHelper::populateMetadata([[maybe_unused]] const MdParser::RegisterMap &registers,
				 [[maybe_unused]] Metadata &metadata) const
{
}

RegisterCamHelper::RegisterCamHelper(char const *camName,
				     CamHelperCreateFunc createFunc)
{
	camHelpers[std::string(camName)] = createFunc;
}