-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.cpp
180 lines (154 loc) · 5.34 KB
/
run.cpp
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
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
#include <chrono>
#include <mutex>
#include <libcamera/controls.h>
#include <libcamera/transform.h>
#include "libcamera_app.hpp"
#include "video_options.hpp"
#include "options.hpp"
#include "kcamera.h"
using namespace std::placeholders;
using namespace libcamera;
class LibcameraRaw: public LibcameraApp<VideoOptions>
{
public:
FrameType frameType_;
};
static bool run;
static LibcameraRaw *app = nullptr;
static ControlList _controls;
static std::mutex controls_mutex;
// The main even loop for the application.
static void event_loop(void)
{
void *mem;
int64_t timestamp_ns;
app->frameType_ = FRAME_BGR;
app->options.width = g_params->m_width;
app->options.height = g_params->m_height;
// Copy g_params into options and controls
if (g_params->m_hflip)
app->options.transform = Transform::HFlip * app->options.transform;
if (g_params->m_vflip)
app->options.transform = Transform::VFlip * app->options.transform;
kcSetBrightness();
kcSetAWB();
kcSetFramerate();
kcSetAutoShutter();
//using namespace libcamera::controls::draft;
//_controls.set(NoiseReductionMode, NoiseReductionModeOff);
//_controls.set(NoiseReductionMode, NoiseReductionModeMinimal);
//_controls.set(NoiseReductionMode, NoiseReductionModeHighQuality);
app->OpenCamera();
app->ConfigureVideo(LibcameraRaw::FLAG_VIDEO_RAW);
app->StartCamera();
for (unsigned int count = 0; run; count++)
{
{
std::lock_guard<std::mutex> lock(controls_mutex);
if (!_controls.empty())
app->SetControls(_controls);
}
LibcameraRaw::Msg msg = app->Wait();
if (msg.type != LibcameraRaw::MsgType::RequestComplete)
throw std::runtime_error("unrecognised message!");
CompletedRequest &completed_request = std::get<CompletedRequest>(msg.payload);
libcamera::Stream *stream = app->VideoStream();
libcamera::FrameBuffer *buffer = completed_request.buffers[stream];
mem = app->Mmap(buffer)[0];
if (!buffer || !mem)
throw std::runtime_error("no buffer to encode");
timestamp_ns = buffer->metadata().timestamp;
kcFrameData(app->options.width, app->options.height, app->frameType_, timestamp_ns/1000, (uint8_t *)mem, buffer->planes()[0].length);
app->QueueRequest(completed_request);
}
app->StopCamera();
kcStopped(); // indicate that we've stopped
}
extern "C" int kcStartCameraLoop(void)
{
run = true;
try
{
LibcameraRaw _app;
app = &_app;
event_loop();
app = nullptr;
}
catch (std::exception const &e)
{
std::cerr << "ERROR: *** " << e.what() << " ***" << std::endl;
return -1;
}
return 0;
}
extern "C" int kcStopCameraLoop(void)
{
run = false;
return 0;
}
#define BRIGHTNESS_KNEE 75
#define BRIGHTNESS_GAIN 1.0 // smaller is more gain
#define BRIGHTNESS_CONTRAST_RATIO 0.5
extern "C" void kcSetBrightness(void)
{
std::lock_guard<std::mutex> lock(controls_mutex);
_controls.set(controls::ExposureValue, (float)((int)g_params->m_brightness-50)/5.0);
// For the last part of the brightness range, we add some digital gain by combining
// brightness and contrast together -- a bit more contrast for the same brightness.
if (g_params->m_brightness<=BRIGHTNESS_KNEE)
{
_controls.set(controls::Brightness, 0.0);
_controls.set(controls::Contrast, 1.0);
}
else
{
_controls.set(controls::Brightness, (float)((int)g_params->m_brightness-BRIGHTNESS_KNEE)/((100-BRIGHTNESS_KNEE)*BRIGHTNESS_GAIN));
_controls.set(controls::Contrast, (float)((int)g_params->m_brightness-BRIGHTNESS_KNEE)/((100-BRIGHTNESS_KNEE)*BRIGHTNESS_GAIN*BRIGHTNESS_CONTRAST_RATIO)+1.0);
}
}
extern "C" void kcSetAWBGains(void)
{
std::lock_guard<std::mutex> lock(controls_mutex);
float r=g_params->m_awbRed*2.0, b=g_params->m_awbBlue*2.0;
_controls.set(controls::ColourGains, {r, b});
}
extern "C" void kcSetAWB(void)
{
std::lock_guard<std::mutex> lock(controls_mutex);
if (g_params->m_awb)
{
float r=0.0, b=0.0;
_controls.set(controls::ColourGains, {r, b});
}
else
{
float r=g_params->m_awbRed*2.0, b=g_params->m_awbBlue*2.0;
_controls.set(controls::ColourGains, {r, b});
}
}
extern "C" void kcSetFramerate(void)
{
std::lock_guard<std::mutex> lock(controls_mutex);
int64_t shutterSpeed = (uint64_t)(g_params->m_shutterSpeed*1000000.0);
int64_t frame_time = 1000000/g_params->m_framerate; // in us
_controls.set(controls::FrameDurations, {frame_time, frame_time});
// If shutter speed exceeds frame period, we need to adjust the shutter speed
if (shutterSpeed>frame_time)
{
g_params->m_shutterSpeed = frame_time/1000000.0;
_controls.set(controls::ExposureTime, (uint32_t)frame_time);
}
}
extern "C" void kcSetShutterSpeed(void)
{
std::lock_guard<std::mutex> lock(controls_mutex);
_controls.set(controls::ExposureTime, (uint32_t)(g_params->m_shutterSpeed*1000000.0));
}
extern "C" void kcSetAutoShutter(void)
{
std::lock_guard<std::mutex> lock(controls_mutex);
if (g_params->m_autoShutter)
_controls.set(controls::ExposureTime, 0);
else
_controls.set(controls::ExposureTime, (uint32_t)(g_params->m_shutterSpeed*1000000.0));
}