/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * libcamera V4L2 API tests * * Validate the function of exporting buffers from a V4L2VideoDevice and * the ability to import them to another V4L2VideoDevice instance. * Ensure that the Buffers can successfully be queued and dequeued * between both devices. */ #include #include #include #include #include "thread.h" #include "v4l2_videodevice_test.h" class BufferSharingTest : public V4L2VideoDeviceTest { public: BufferSharingTest() : V4L2VideoDeviceTest("vivid", "vivid-000-vid-cap"), output_(nullptr), framesCaptured_(0), framesOutput_(0) {} protected: int init() { int ret = V4L2VideoDeviceTest::init(); if (ret) return ret; /* media_ already represents VIVID */ MediaEntity *entity = media_->getEntityByName("vivid-000-vid-out"); if (!entity) return TestSkip; output_ = new V4L2VideoDevice(entity); if (!output_) { std::cout << "Failed to create output device" << std::endl; return TestFail; } ret = output_->open(); if (ret) { std::cout << "Failed to open output device" << std::endl; return TestFail; } V4L2DeviceFormat format = {}; ret = capture_->getFormat(&format); if (ret) { std::cout << "Failed to get capture format" << std::endl; return TestFail; } format.size.width = 320; format.size.height = 180; ret = capture_->setFormat(&format); if (ret) { std::cout << "Failed to set capture format" << std::endl; return TestFail; } ret = output_->setFormat(&format); if (ret) { std::cout << "Failed to set output format" << std::endl; return TestFail; } ret = capture_->allocateBuffers(bufferCount, &buffers_); if (ret < 0) { std::cout << "Failed to allocate buffers" << std::endl; return TestFail; } ret = output_->importBuffers(bufferCount); if (ret < 0) { std::cout << "Failed to import buffers" << std::endl; return TestFail; } return 0; } void captureBufferReady(FrameBuffer *buffer) { const FrameMetadata &metadata = buffer->metadata(); std::cout << "Received capture buffer" << std::endl; if (metadata.status != FrameMetadata::FrameSuccess) return; output_->queueBuffer(buffer); framesCaptured_++; } void outputBufferReady(FrameBuffer *buffer) { const FrameMetadata &metadata = buffer->metadata(); std::cout << "Received output buffer" << std::endl; if (metadata.status != FrameMetadata::FrameSuccess) return; capture_->queueBuffer(buffer); framesOutput_++; } int run() { EventDispatcher *dispatcher = Thread::current()->eventDispatcher(); Timer timeout; int ret; capture_->bufferReady.connect(this, &BufferSharingTest::captureBufferReady); output_->bufferReady.connect(this, &BufferSharingTest::outputBufferReady); for (const std::unique_ptr &buffer : buffers_) { if (capture_->queueBuffer(buffer.get())) { std::cout << "Failed to queue buffer" << std::endl; return TestFail; } } ret = capture_->streamOn(); if (ret) { std::cout << "Failed to start streaming on the capture device" << std::endl; return TestFail; } ret = output_->streamOn(); if (ret) { std::cout << "Failed to start streaming on the output device" << std::endl; return TestFail; } timeout.start(10000); while (timeout.isRunning()) { dispatcher->processEvents(); if (framesCaptured_ > 30 && framesOutput_ > 30) break; } if ((framesCaptured_ < 1) || (framesOutput_ < 1)) { std::cout << "Failed to process any frames within timeout." << std::endl; return TestFail; } if ((framesCaptured_ < 30) || (framesOutput_ < 30)) { std::cout << "Failed to process 30 frames within timeout." << std::endl; return TestFail; } ret = capture_->streamOff(); if (ret) { std::cout << "Failed to stop streaming on the capture device" << std::endl; return TestFail; } ret = output_->streamOff(); if (ret) { std::cout << "Failed to stop streaming on the output device" << std::endl; return TestFail; } return TestPass; } void cleanup() { std::cout << "Captured " << framesCaptured_ << " frames and " << "output " << framesOutput_ << " frames" << std::endl; output_->streamOff(); output_->releaseBuffers(); output_->close(); delete output_; V4L2VideoDeviceTest::cleanup(); } private: const unsigned int bufferCount = 4; V4L2VideoDevice *output_; unsigned int framesCaptured_; unsigned int framesOutput_; }; TEST_REGISTER(BufferSharingTest); f='#n29'>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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (C) 2019, Raspberry Pi (Trading) Limited
 *
 * contrast.cpp - contrast (gamma) control algorithm
 */
#include <stdint.h>

#include "../contrast_status.h"
#include "../histogram.hpp"

#include "contrast.hpp"

using namespace RPi;

// This is a very simple control algorithm which simply retrieves the results of
// AGC and AWB via their "status" metadata, and applies digital gain to the
// colour channels in accordance with those instructions. We take care never to
// apply less than unity gains, as that would cause fully saturated pixels to go
// off-white.

#define NAME "rpi.contrast"

Contrast::Contrast(Controller *controller)
	: ContrastAlgorithm(controller), brightness_(0.0), contrast_(1.0)
{
}

char const *Contrast::Name() const
{
	return NAME;
}

void Contrast::Read(boost::property_tree::ptree const &params)
{
	// enable adaptive enhancement by default
	config_.ce_enable = params.get<int>("ce_enable", 1);
	// the point near the bottom of the histogram to move
	config_.lo_histogram = params.get<double>("lo_histogram", 0.01);
	// where in the range to try and move it to
	config_.lo_level = params.get<double>("lo_level", 0.015);
	// but don't move by more than this
	config_.lo_max = params.get<double>("lo_max", 500);
	// equivalent values for the top of the histogram...
	config_.hi_histogram = params.get<double>("hi_histogram", 0.95);
	config_.hi_level = params.get<double>("hi_level", 0.95);
	config_.hi_max = params.get<double>("hi_max", 2000);
	config_.gamma_curve.Read(params.get_child("gamma_curve"));
}

void Contrast::SetBrightness(double brightness)
{
	brightness_ = brightness;
}

void Contrast::SetContrast(double contrast)
{
	contrast_ = contrast;
}

static void fill_in_status(ContrastStatus &status, double brightness,
			   double contrast, Pwl &gamma_curve)
{
	status.brightness = brightness;
	status.contrast = contrast;
	for (int i = 0; i < CONTRAST_NUM_POINTS - 1; i++) {
		int x = i < 16 ? i * 1024
			       : (i < 24 ? (i - 16) * 2048 + 16384
					 : (i - 24) * 4096 + 32768);
		status.points[i].x = x;
		status.points[i].y = std::min(65535.0, gamma_curve.Eval(x));
	}
	status.points[CONTRAST_NUM_POINTS - 1].x = 65535;
	status.points[CONTRAST_NUM_POINTS - 1].y = 65535;
}

void Contrast::Initialise()
{
	// Fill in some default values as Prepare will run before Process gets
	// called.
	fill_in_status(status_, brightness_, contrast_, config_.gamma_curve);
}

void Contrast::Prepare(Metadata *image_metadata)
{
	std::unique_lock<std::mutex> lock(mutex_);
	image_metadata->Set("contrast.status", status_);
}

Pwl compute_stretch_curve(Histogram const &histogram,
			  ContrastConfig const &config)
{
	Pwl enhance;
	enhance.Append(0, 0);
	// If the start of the histogram is rather empty, try to pull it down a
	// bit.
	double hist_lo = histogram.Quantile(config.lo_histogram) *
			 (65536 / NUM_HISTOGRAM_BINS);
	double level_lo = config.lo_level * 65536;
	RPI_LOG("Move histogram point " << hist_lo << " to " << level_lo);
	hist_lo = std::max(
		level_lo,
		std::min(65535.0, std::min(hist_lo, level_lo + config.lo_max)));
	RPI_LOG("Final values " << hist_lo << " -> " << level_lo);
	enhance.Append(hist_lo, level_lo);
	// Keep the mid-point (median) in the same place, though, to limit the
	// apparent amount of global brightness shift.
	double mid = histogram.Quantile(0.5) * (65536 / NUM_HISTOGRAM_BINS);
	enhance.Append(mid, mid);

	// If the top to the histogram is empty, try to pull the pixel values
	// there up.
	double hist_hi = histogram.Quantile(config.hi_histogram) *
			 (65536 / NUM_HISTOGRAM_BINS);
	double level_hi = config.hi_level * 65536;
	RPI_LOG("Move histogram point " << hist_hi << " to " << level_hi);
	hist_hi = std::min(
		level_hi,
		std::max(0.0, std::max(hist_hi, level_hi - config.hi_max)));
	RPI_LOG("Final values " << hist_hi << " -> " << level_hi);
	enhance.Append(hist_hi, level_hi);
	enhance.Append(65535, 65535);
	return enhance;
}

Pwl apply_manual_contrast(Pwl const &gamma_curve, double brightness,
			  double contrast)
{