/* 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; } >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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * camera3_hal.cpp - Android Camera HALv3 module
 */

#include <hardware/camera_common.h>

#include <libcamera/base/log.h>

#include "camera_device.h"
#include "camera_hal_manager.h"

using namespace libcamera;

LOG_DEFINE_CATEGORY(HAL)

/*------------------------------------------------------------------------------
 * Android Camera HAL callbacks
 */

static int hal_get_number_of_cameras()
{
	return CameraHalManager::instance()->numCameras();
}

static int hal_get_camera_info(int id, struct camera_info *info)
{
	return CameraHalManager::instance()->getCameraInfo(id, info);
}

static int hal_set_callbacks(const camera_module_callbacks_t *callbacks)
{
	CameraHalManager::instance()->setCallbacks(callbacks);

	return 0;
}

static int hal_open_legacy([[maybe_unused]] const struct hw_module_t *module,
			   [[maybe_unused]] const char *id,
			   [[maybe_unused]] uint32_t halVersion,
			   [[maybe_unused]] struct hw_device_t **device)
{
	return -ENOSYS;
}

static int hal_set_torch_mode([[maybe_unused]] const char *camera_id,
			      [[maybe_unused]] bool enabled)
{
	return -ENOSYS;
}

/*
 * First entry point of the camera HAL module.
 *
 * Initialize the HAL but does not open any camera device yet (see hal_dev_open)
 */
static int hal_init()
{
	LOG(HAL, Info) << "Initialising Android camera HAL";

	CameraHalManager::instance()->init();

	return 0;
}

/*------------------------------------------------------------------------------
 * Android Camera Device
 */

static int hal_dev_open(const hw_module_t *module, const char *name,
			hw_device_t **device)
{
	LOG(HAL, Debug) << "Open camera " << name;

	int id = atoi(name);

	auto [camera, ret] = CameraHalManager::instance()->open(id, module);
	if (!camera) {
		LOG(HAL, Error)
			<< "Failed to open camera module '" << id << "'";
		return ret == -EBUSY ? -EUSERS : ret;
	}

	*device = &camera->camera3Device()->common;

	return 0;
}

static struct hw_module_methods_t hal_module_methods = {
	.open = hal_dev_open,
};

camera_module_t HAL_MODULE_INFO_SYM = {
	.common = {
		.tag = HARDWARE_MODULE_TAG,
		.module_api_version = CAMERA_MODULE_API_VERSION_2_4,
		.hal_api_version = HARDWARE_HAL_API_VERSION,
		.id = CAMERA_HARDWARE_MODULE_ID,
		.name = "libcamera camera HALv3 module",
		.author = "libcamera",
		.methods = &hal_module_methods,
		.dso = nullptr,
		.reserved = {},
	},

	.get_number_of_cameras = hal_get_number_of_cameras,
	.get_camera_info = hal_get_camera_info,
	.set_callbacks = hal_set_callbacks,
	.get_vendor_tag_ops = nullptr,
	.open_legacy = hal_open_legacy,
	.set_torch_mode = hal_set_torch_mode,
	.init = hal_init,
	.reserved = {},
};