/* SPDX-License-Identifier: BSD-2-Clause */ /* * Copyright (C) 2019, Raspberry Pi (Trading) Limited * * cam_helper.cpp - helper information for different sensors */ #include #include #include #include #include "libcamera/internal/v4l2_videodevice.h" #include "cam_helper.hpp" #include "md_parser.hpp" using namespace RPiController; using namespace libcamera; using libcamera::utils::Duration; namespace libcamera { LOG_DECLARE_CATEGORY(IPARPI) } static std::map cam_helpers; CamHelper *CamHelper::Create(std::string const &cam_name) { /* * CamHelpers get registered by static RegisterCamHelper * initialisers. */ for (auto &p : cam_helpers) { if (cam_name.find(p.first) != std::string::npos) return p.second(); } return nullptr; } CamHelper::CamHelper(std::unique_ptr parser, unsigned int frameIntegrationDiff) : parser_(std::move(parser)), initialized_(false), frameIntegrationDiff_(frameIntegrationDiff) { } CamHelper::~CamHelper() { } void CamHelper::Prepare(Span 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_.line_length; } Duration CamHelper::Exposure(uint32_t exposure_lines) const { assert(initialized_); return exposure_lines * mode_.line_length; } uint32_t CamHelper::GetVBlanking(Duration &exposure, Duration minFrameDuration, Duration maxFrameDuration) const { uint32_t frameLengthMin, frameLengthMax, vblank; uint32_t exposureLines = ExposureLines(exposure); assert(initialized_); /* * minFrameDuration and maxFrameDuration are clamped by the caller * based on the limits for the active sensor mode. */ frameLengthMin = minFrameDuration / mode_.line_length; frameLengthMax = maxFrameDuration / mode_.line_length; /* * Limit the exposure to the maximum frame duration requested, and * re-calculate if it has been clipped. */ exposureLines = std::min(frameLengthMax - frameIntegrationDiff_, exposureLines); exposure = 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_->SetBitsPerPixel(mode.bitdepth); parser_->SetLineLengthBytes(0); /* We use SetBufferSize. */ } initialized_ = true; } void CamHelper::GetDelays(int &exposure_delay, int &gain_delay, int &vblank_delay) const { /* * These values are correct for many sensors. Other sensors will * need to over-ride this function. */ exposure_delay = 2; gain_delay = 1; vblank_delay = 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 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 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.shutter_speed = parsedDeviceStatus.shutter_speed; deviceStatus.analogue_gain = parsedDeviceStatus.analogue_gain; deviceStatus.frame_length = parsedDeviceStatus.frame_length; LOG(IPARPI, Debug) << "Metadata updated - " << deviceStatus; metadata.Set("device.status", deviceStatus); } void CamHelper::PopulateMetadata([[maybe_unused]] const MdParser::RegisterMap ®isters, [[maybe_unused]] Metadata &metadata) const { } RegisterCamHelper::RegisterCamHelper(char const *cam_name, CamHelperCreateFunc create_func) { cam_helpers[std::string(cam_name)] = create_func; } 52'>52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.
 *
 * v4l2_pixelformat.cpp - V4L2 Pixel Format
 */

#include "libcamera/internal/v4l2_pixelformat.h"

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

#include <libcamera/formats.h>
#include <libcamera/pixel_format.h>

#include "libcamera/internal/formats.h"
#include "libcamera/internal/log.h"

/**
 * \file v4l2_pixelformat.h
 * \brief V4L2 Pixel Format
 */
namespace libcamera {

LOG_DECLARE_CATEGORY(V4L2)

/**
 * \class V4L2PixelFormat
 * \brief V4L2 pixel format FourCC wrapper
 *
 * The V4L2PixelFormat class describes the pixel format of a V4L2 buffer. It
 * wraps the V4L2 numerical FourCC, and shall be used in all APIs that deal with
 * V4L2 pixel formats. Its purpose is to prevent unintentional confusion of
 * V4L2 and DRM FourCCs in code by catching implicit conversion attempts at
 * compile time.
 *
 * To achieve this goal, construction of a V4L2PixelFormat from an integer value
 * is explicit. To retrieve the integer value of a V4L2PixelFormat, both the
 * explicit value() and implicit uint32_t conversion operators may be used.
 */

namespace {

const std::map<V4L2PixelFormat, PixelFormat> vpf2pf{
	/* RGB formats. */
	{ V4L2PixelFormat(V4L2_PIX_FMT_RGB565), formats::RGB565 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_RGB24), formats::BGR888 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_BGR24), formats::RGB888 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_RGBA32), formats::ABGR8888 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_ABGR32), formats::ARGB8888 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_ARGB32), formats::BGRA8888 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_BGRA32), formats::RGBA8888 },

	/* YUV packed formats. */
	{ V4L2PixelFormat(V4L2_PIX_FMT_YUYV), formats::YUYV },
	{ V4L2PixelFormat(V4L2_PIX_FMT_YVYU), formats::YVYU },
	{ V4L2PixelFormat(V4L2_PIX_FMT_UYVY), formats::UYVY },
	{ V4L2PixelFormat(V4L2_PIX_FMT_VYUY), formats::VYUY },

	/* YUV planar formats. */
	{ V4L2PixelFormat(V4L2_PIX_FMT_NV16), formats::NV16 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_NV61), formats::NV61 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_NV12), formats::NV12 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_NV21), formats::NV21 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_YUV420), formats::YUV420 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_YVU420), formats::YVU420 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_YUV422P), formats::YUV422 },

	/* Greyscale formats. */
	{ V4L2PixelFormat(V4L2_PIX_FMT_GREY), formats::R8 },

	/* Bayer formats. */
	{ V4L2PixelFormat(V4L2_PIX_FMT_SBGGR8), formats::SBGGR8 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGBRG8), formats::SGBRG8 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGRBG8), formats::SGRBG8 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SRGGB8), formats::SRGGB8 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SBGGR10), formats::SBGGR10 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGBRG10), formats::SGBRG10 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGRBG10), formats::SGRBG10 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SRGGB10), formats::SRGGB10 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SBGGR10P), formats::SBGGR10_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGBRG10P), formats::SGBRG10_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGRBG10P), formats::SGRBG10_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SRGGB10P), formats::SRGGB10_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SBGGR12), formats::SBGGR12 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGBRG12), formats::SGBRG12 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGRBG12), formats::SGRBG12 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SRGGB12), formats::SRGGB12 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SBGGR12P), formats::SBGGR12_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGBRG12P), formats::SGBRG12_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGRBG12P), formats::SGRBG12_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SRGGB12P), formats::SRGGB12_CSI2P },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SBGGR16), formats::SBGGR16 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGBRG16), formats::SGBRG16 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SGRBG16), formats::SGRBG16 },
	{ V4L2PixelFormat(V4L2_PIX_FMT_SRGGB16), formats::SRGGB16 },

	/* Compressed formats. */
	{ V4L2PixelFormat(V4L2_PIX_FMT_MJPEG), formats::MJPEG },
};

} /* namespace */

/**
 * \fn V4L2PixelFormat::V4L2PixelFormat()
 * \brief Construct a V4L2PixelFormat with an invalid format
 *
 * V4L2PixelFormat instances constructed with the default constructor are
 * invalid, calling the isValid() function returns false.
 */

/**
 * \fn V4L2PixelFormat::V4L2PixelFormat(uint32_t fourcc)
 * \brief Construct a V4L2PixelFormat from a FourCC value
 * \param[in] fourcc The pixel format FourCC numerical value
 */

/**
 * \fn bool V4L2PixelFormat::isValid() const
 * \brief Check if the pixel format is valid
 *
 * V4L2PixelFormat instances constructed with the default constructor are
 * invalid. Instances constructed with a FourCC defined in the V4L2 API are
 * valid. The behaviour is undefined otherwise.
 *
 * \return True if the pixel format is valid, false otherwise
 */

/**
 * \fn uint32_t V4L2PixelFormat::fourcc() const
 * \brief Retrieve the pixel format FourCC numerical value
 * \return The pixel format FourCC numerical value
 */

/**
 * \fn V4L2PixelFormat::operator uint32_t() const
 * \brief Convert to the pixel format FourCC numerical value
 * \return The pixel format FourCC numerical value
 */

/**
 * \brief Assemble and return a string describing the pixel format
 * \return A string describing the pixel format
 */
std::string V4L2PixelFormat::toString() const
{
	if (fourcc_ == 0)
		return "<INVALID>";

	char ss[8] = { static_cast<char>(fourcc_ & 0x7f),
		       static_cast<char>((fourcc_ >> 8) & 0x7f),
		       static_cast<char>((fourcc_ >> 16) & 0x7f),
		       static_cast<char>((fourcc_ >> 24) & 0x7f) };

	for (unsigned int i = 0; i < 4; i++) {
		if (!isprint(ss[i]))
			ss[i] = '.';
	}

	if (fourcc_ & (1 << 31))
		strcat(ss, "-BE");

	return ss;
}

/**
 * \brief Convert the V4L2 pixel format to the corresponding PixelFormat
 * \return The PixelFormat corresponding to the V4L2 pixel format
 */
PixelFormat V4L2PixelFormat::toPixelFormat() const
{
	const auto iter = vpf2pf.find(*this);
	if (iter == vpf2pf.end()) {
		LOG(V4L2, Warning)
			<< "Unsupported V4L2 pixel format "
			<< toString();
		return PixelFormat();
	}

	return iter->second;
}

/**
 * \brief Convert \a pixelFormat to its corresponding V4L2PixelFormat
 * \param[in] pixelFormat The PixelFormat to convert
 * \param[in] multiplanar V4L2 Multiplanar API support flag
 *
 * Multiple V4L2 formats may exist for one PixelFormat when the format uses
 * multiple planes, as V4L2 defines separate 4CCs for contiguous and separate
 * planes formats. Set the \a multiplanar parameter to false to select a format