summaryrefslogtreecommitdiff
path: root/src/ipa/simple/black_level.h
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-04-22 15:37:10 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-06-03 11:53:12 +0300
commitf7d7a5e294237a50dbdc3aa3e0fa7c3200a08fa7 (patch)
tree48538524a2db89ed8a492549701c6bf544b4a547 /src/ipa/simple/black_level.h
parent73f18f32c958d681126394fa7556828805221f69 (diff)
pipeline: vimc: Don't hardcode scaling factor with recent kernels
Starting in kernel v5.16, the vimc driver stopped hardcoding the scaler factor. Use this to lift constraints on the camera configuration, and in particular on the exotic output size alignment to a multiple of 6. As a result, vimc-based cameras can more easily match common display resolutions. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
Diffstat (limited to 'src/ipa/simple/black_level.h')
0 files changed, 0 insertions, 0 deletions
n136' href='#n136'>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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * main_window.cpp - qcam - Main application window
 */

#include <iomanip>
#include <iostream>
#include <string>

#include <QCoreApplication>
#include <QInputDialog>
#include <QTimer>

#include <libcamera/camera_manager.h>
#include <libcamera/version.h>

#include "main_window.h"
#include "viewfinder.h"

using namespace libcamera;

MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)
	: options_(options), isCapturing_(false)
{
	int ret;

	title_ = "QCam " + QString::fromStdString(CameraManager::version());
	setWindowTitle(title_);
	connect(&titleTimer_, SIGNAL(timeout()), this, SLOT(updateTitle()));

	viewfinder_ = new ViewFinder(this);
	setCentralWidget(viewfinder_);
	viewfinder_->setFixedSize(500, 500);
	adjustSize();

	ret = openCamera(cm);
	if (!ret)
		ret = startCapture();

	if (ret < 0)
		QTimer::singleShot(0, QCoreApplication::instance(),
				   &QCoreApplication::quit);
}

MainWindow::~MainWindow()
{
	if (camera_) {
		stopCapture();
		camera_->release();
		camera_.reset();
	}
}

void MainWindow::updateTitle()
{
	unsigned int duration = frameRateInterval_.elapsed();
	unsigned int frames = framesCaptured_ - previousFrames_;
	double fps = frames * 1000.0 / duration;

	/* Restart counters. */
	frameRateInterval_.start();
	previousFrames_ = framesCaptured_;

	setWindowTitle(title_ + " : " + QString::number(fps, 'f', 2) + " fps");
}

int MainWindow::openCamera(CameraManager *cm)
{
	std::string cameraName;

	if (!options_.isSet(OptCamera)) {
		QStringList cameras;
		bool result;

		for (const std::shared_ptr<Camera> &cam : cm->cameras())
			cameras.append(QString::fromStdString(cam->name()));

		QString name = QInputDialog::getItem(this, "Select Camera",
						     "Camera:", cameras, 0,
						     false, &result);
		if (!result)
			return -EINVAL;

		cameraName = name.toStdString();
	} else {
		cameraName = static_cast<std::string>(options_[OptCamera]);
	}

	camera_ = cm->get(cameraName);
	if (!camera_) {
		std::cout << "Camera " << cameraName << " not found"
			  << std::endl;
		return -ENODEV;
	}

	if (camera_->acquire()) {
		std::cout << "Failed to acquire camera" << std::endl;
		camera_.reset();
		return -EBUSY;
	}

	std::cout << "Using camera " << camera_->name() << std::endl;

	camera_->requestCompleted.connect(this, &MainWindow::requestComplete);

	return 0;
}

int MainWindow::startCapture()
{
	int ret;

	config_ = camera_->generateConfiguration({ StreamRole::VideoRecording });

	StreamConfiguration &cfg = config_->at(0);
	if (options_.isSet(OptSize)) {
		const std::vector<OptionValue> &sizeOptions =
			options_[OptSize].toArray();

		/* Set desired stream size if requested. */
		for (const auto &value : sizeOptions) {
			KeyValueParser::Options opt = value.toKeyValues();

			if (opt.isSet("width"))
				cfg.size.width = opt["width"];

			if (opt.isSet("height"))
				cfg.size.height = opt["height"];
		}
	}

	CameraConfiguration::Status validation = config_->validate();
	if (validation == CameraConfiguration::Invalid) {
		std::cerr << "Failed to create valid camera configuration";
		return -EINVAL;
	}

	if (validation == CameraConfiguration::Adjusted) {
		std::cout << "Stream size adjusted to "
			  << cfg.size.toString() << std::endl;
	}

	ret = camera_->configure(config_.get());
	if (ret < 0) {
		std::cout << "Failed to configure camera" << std::endl;
		return ret;
	}

	Stream *stream = cfg.stream();
	ret = viewfinder_->setFormat(cfg.pixelFormat, cfg.size.width,
				     cfg.size.height);
	if (ret < 0) {
		std::cout << "Failed to set viewfinder format" << std::endl;
		return ret;
	}

	adjustSize();

	ret = camera_->allocateBuffers();
	if (ret) {
		std::cerr << "Failed to allocate buffers"
			  << std::endl;
		return ret;
	}

	std::vector<Request *> requests;
	for (unsigned int i = 0; i < cfg.bufferCount; ++i) {
		Request *request = camera_->createRequest();
		if (!request) {
			std::cerr << "Can't create request" << std::endl;
			ret = -ENOMEM;
			goto error;
		}

		std::unique_ptr<Buffer> buffer = stream->createBuffer(i);
		if (!buffer) {
			std::cerr << "Can't create buffer " << i << std::endl;
			goto error;
		}

		ret = request->addBuffer(std::move(buffer));
		if (ret < 0) {
			std::cerr << "Can't set buffer for request" << std::endl;
			goto error;
		}

		requests.push_back(request);
	}

	titleTimer_.start(2000);
	frameRateInterval_.start();
	previousFrames_ = 0;
	framesCaptured_ = 0;
	lastBufferTime_ = 0;

	ret = camera_->start();
	if (ret) {
		std::cout << "Failed to start capture" << std::endl;
		goto error;
	}

	for (Request *request : requests) {
		ret = camera_->queueRequest(request);
		if (ret < 0) {
			std::cerr << "Can't queue request" << std::endl;
			goto error;
		}
	}

	isCapturing_ = true;
	return 0;

error:
	for (Request *request : requests)
		delete request;

	camera_->freeBuffers();
	return ret;
}

void MainWindow::stopCapture()
{
	if (!isCapturing_)
		return;

	int ret = camera_->stop();
	if (ret)
		std::cout << "Failed to stop capture" << std::endl;

	camera_->freeBuffers();
	isCapturing_ = false;

	config_.reset();

	titleTimer_.stop();
	setWindowTitle(title_);
}

void MainWindow::requestComplete(Request *request,
				 const std::map<Stream *, Buffer *> &buffers)
{
	if (request->status() == Request::RequestCancelled)
		return;

	framesCaptured_++;

	Buffer *buffer = buffers.begin()->second;

	double fps = buffer->timestamp() - lastBufferTime_;
	fps = lastBufferTime_ && fps ? 1000000000.0 / fps : 0.0;
	lastBufferTime_ = buffer->timestamp();

	std::cout << "seq: " << std::setw(6) << std::setfill('0') << buffer->sequence()
		  << " buf: " << buffer->index()
		  << " bytesused: " << buffer->bytesused()
		  << " timestamp: " << buffer->timestamp()
		  << " fps: " << std::fixed << std::setprecision(2) << fps
		  << std::endl;

	display(buffer);

	request = camera_->createRequest();
	if (!request) {
		std::cerr << "Can't create request" << std::endl;
		return;
	}

	for (auto it = buffers.begin(); it != buffers.end(); ++it) {
		Stream *stream = it->first;
		Buffer *buffer = it->second;
		unsigned int index = buffer->index();

		std::unique_ptr<Buffer> newBuffer = stream->createBuffer(index);
		if (!newBuffer) {
			std::cerr << "Can't create buffer " << index << std::endl;
			return;
		}

		request->addBuffer(std::move(newBuffer));
	}

	camera_->queueRequest(request);
}

int MainWindow::display(Buffer *buffer)
{
	BufferMemory *mem = buffer->mem();
	if (mem->planes().size() != 1)
		return -EINVAL;

	Plane &plane = mem->planes().front();
	unsigned char *raw = static_cast<unsigned char *>(plane.mem());
	viewfinder_->display(raw, buffer->bytesused());

	return 0;
}