summaryrefslogtreecommitdiff
path: root/src/ipa/rpi/common/ipa_base.h
blob: 39d00760d0120010fdfe0940f49a05c23dbdf476 (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
114
115
116
117
118
119
120
121
122
123
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2023, Raspberry Pi Ltd
 *
 * ipa_base.h - Raspberry Pi IPA base class
 */
#pragma once

#include <array>
#include <deque>
#include <map>
#include <stdint.h>

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

#include <libcamera/ipa/raspberrypi_ipa_interface.h>

#include "libcamera/internal/mapped_framebuffer.h"

#include "cam_helper/cam_helper.h"
#include "controller/agc_status.h"
#include "controller/camera_mode.h"
#include "controller/controller.h"
#include "controller/metadata.h"

namespace libcamera {

namespace ipa::RPi {

class IpaBase : public IPARPiInterface
{
public:
	IpaBase();
	~IpaBase();

	int32_t init(const IPASettings &settings, const InitParams &params, InitResult *result) override;
	int32_t configure(const IPACameraSensorInfo &sensorInfo, const ConfigParams &params,
			  ConfigResult *result) override;

	void start(const ControlList &controls, StartResult *result) override;
	void stop() override {}

	void mapBuffers(const std::vector<IPABuffer> &buffers) override;
	void unmapBuffers(const std::vector<unsigned int> &ids) override;

	void prepareIsp(const PrepareParams &params) override;
	void processStats(const ProcessParams &params) override;

protected:
	/* Raspberry Pi controller specific defines. */
	std::unique_ptr<RPiController::CamHelper> helper_;
	RPiController::Controller controller_;

	ControlInfoMap sensorCtrls_;
	ControlInfoMap lensCtrls_;

	/* Camera sensor params. */
	CameraMode mode_;

	/* Track the frame length times over FrameLengthsQueueSize frames. */
	std::deque<utils::Duration> frameLengths_;
	utils::Duration lastTimeout_;

private:
	/* Number of metadata objects available in the context list. */
	static constexpr unsigned int numMetadataContexts = 16;

	virtual int32_t platformInit(const InitParams &params, InitResult *result) = 0;
	virtual int32_t platformConfigure(const ConfigParams &params, ConfigResult *result) = 0;

	virtual void platformPrepareIsp(const PrepareParams &params,
					RPiController::Metadata &rpiMetadata) = 0;
	virtual RPiController::StatisticsPtr platformProcessStats(Span<uint8_t> mem) = 0;

	void setMode(const IPACameraSensorInfo &sensorInfo);
	void setCameraTimeoutValue();
	bool validateSensorControls();
	bool validateLensControls();
	void applyControls(const ControlList &controls);
	virtual void handleControls(const ControlList &controls) = 0;
	void fillDeviceStatus(const ControlList &sensorControls, unsigned int ipaContext);
	void reportMetadata(unsigned int ipaContext);
	void applyFrameDurations(utils::Duration minFrameDuration, utils::Duration maxFrameDuration);
	void applyAGC(const struct AgcStatus *agcStatus, ControlList &ctrls);

	std::map<unsigned int, MappedFrameBuffer> buffers_;

	bool lensPresent_;
	bool monoSensor_;
	ControlList libcameraMetadata_;

	std::array<RPiController::Metadata, numMetadataContexts> rpiMetadata_;

	/*
	 * We count frames to decide if the frame must be hidden (e.g. from
	 * display) or mistrusted (i.e. not given to the control algos).
	 */
	uint64_t frameCount_;

	/* How many frames we should avoid running control algos on. */
	unsigned int mistrustCount_;

	/* Number of frames that need to be dropped on startup. */
	unsigned int dropFrameCount_;

	/* Frame timestamp for the last run of the controller. */
	uint64_t lastRunTimestamp_;

	/* Do we run a Controller::process() for this frame? */
	bool processPending_;

	/* Distinguish the first camera start from others. */
	bool firstStart_;

	/* Frame duration (1/fps) limits. */
	utils::Duration minFrameDuration_;
	utils::Duration maxFrameDuration_;
};

} /* namespace ipa::RPi */

} /* namespace libcamera */