summaryrefslogtreecommitdiff
path: root/src/ipa/raspberrypi/cam_helper.cpp
blob: c8ac32329b15f4277b588a0f58cf9894b8ea53a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi (Trading) Limited
 *
 * 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.hpp"
#include "md_parser.hpp"

using namespace RPiController;

static std::map<std::string, CamHelperCreateFunc> 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(MdParser *parser)
	: parser_(parser), initialized_(false)
{
}

CamHelper::~CamHelper()
{
	delete parser_;
}

uint32_t CamHelper::ExposureLines(double exposure_us) const
{
	assert(initialized_);
	return exposure_us * 1000.0 / mode_.line_length;
}

double CamHelper::Exposure(uint32_t exposure_lines) const
{
	assert(initialized_);
	return exposure_lines * mode_.line_length / 1000.0;
}

void CamHelper::SetCameraMode(const CameraMode &mode)
{
	mode_ = mode;
	parser_->SetBitsPerPixel(mode.bitdepth);
	parser_->SetLineLengthBytes(0); /* We use SetBufferSize. */
	initialized_ = true;
}

void CamHelper::GetDelays(int &exposure_delay, int &gain_delay) const
{
	/*
	 * These values are correct for many sensors. Other sensors will
	 * need to over-ride this method.
	 */
	exposure_delay = 2;
	gain_delay = 1;
}

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

unsigned int CamHelper::HideFramesStartup() const
{
	/*
	 * By default, hide 6 frames completely at start-up while AGC etc. sort
	 * themselves out (converge).
	 */
	return 6;
}

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;
}

RegisterCamHelper::RegisterCamHelper(char const *cam_name,
				     CamHelperCreateFunc create_func)
{
	cam_helpers[std::string(cam_name)] = create_func;
}
an>::move(dupFd)); if (desc1_->get() != dupFdCopy) { std::cout << "Failed fd numerical check (rvalue ref constructor)" << std::endl; return TestFail; } if (dupFd != -1 || !isValidFd(fd_) || !isValidFd(desc1_->get())) { std::cout << "Failed fd validity after construction (rvalue ref constructor)" << std::endl; return TestFail; } fd = desc1_->get(); delete desc1_; desc1_ = nullptr; if (!isValidFd(fd_) || isValidFd(fd)) { std::cout << "Failed fd validity after destruction (rvalue ref constructor)" << std::endl; return TestFail; } /* Test creating SharedFD from other SharedFD. */ desc1_ = new SharedFD(fd_); desc2_ = new SharedFD(*desc1_); if (desc1_->get() == fd_ || desc2_->get() == fd_ || desc1_->get() != desc2_->get()) { std::cout << "Failed fd numerical check (copy constructor)" << std::endl; return TestFail; } if (!isValidFd(desc1_->get()) || !isValidFd(desc2_->get())) { std::cout << "Failed fd validity after construction (copy constructor)" << std::endl; return TestFail; } delete desc1_; desc1_ = nullptr; if (!isValidFd(desc2_->get())) { std::cout << "Failed fd validity after destruction (copy constructor)" << std::endl; return TestFail; } delete desc2_; desc2_ = nullptr; /* Test creating SharedFD by taking over other SharedFD. */ desc1_ = new SharedFD(fd_); fd = desc1_->get(); desc2_ = new SharedFD(std::move(*desc1_)); if (desc1_->get() != -1 || desc2_->get() != fd) { std::cout << "Failed fd numerical check (move constructor)" << std::endl; return TestFail; } if (!isValidFd(desc2_->get())) { std::cout << "Failed fd validity after construction (move constructor)" << std::endl; return TestFail; } delete desc1_; desc1_ = nullptr; delete desc2_; desc2_ = nullptr; /* Test creating SharedFD by copy assignment. */ desc1_ = new SharedFD(); desc2_ = new SharedFD(fd_); fd = desc2_->get(); *desc1_ = *desc2_; if (desc1_->get() != fd || desc2_->get() != fd) { std::cout << "Failed fd numerical check (copy assignment)" << std::endl; return TestFail; } if (!isValidFd(desc1_->get()) || !isValidFd(desc2_->get())) { std::cout << "Failed fd validity after construction (copy assignment)" << std::endl; return TestFail; } delete desc1_; desc1_ = nullptr; delete desc2_; desc2_ = nullptr; /* Test creating SharedFD by move assignment. */ desc1_ = new SharedFD(); desc2_ = new SharedFD(fd_); fd = desc2_->get(); *desc1_ = std::move(*desc2_); if (desc1_->get() != fd || desc2_->get() != -1) { std::cout << "Failed fd numerical check (move assignment)" << std::endl; return TestFail; } if (!isValidFd(desc1_->get())) { std::cout << "Failed fd validity after construction (move assignment)" << std::endl; return TestFail; } delete desc1_; desc1_ = nullptr; delete desc2_; desc2_ = nullptr; return TestPass; } void cleanup() { delete desc2_; delete desc1_; if (fd_ > 0) close(fd_); } private: bool isValidFd(int fd) { struct stat s; if (fstat(fd, &s)) return false; /* Check that inode number matches cached temp file. */ return s.st_ino == inodeNr_; } int fd_; ino_t inodeNr_; SharedFD *desc1_, *desc2_; }; TEST_REGISTER(SharedFDTest)