summaryrefslogtreecommitdiff
path: root/src/ipa/libipa/histogram.h
AgeCommit message (Expand)Author
2024-09-02libcamera: libipa: Remove unused includesMilan Zamazal
2024-05-09ipa: libipa: histogram: Add transform parameter to constructorPaul Elder
2024-05-08libcamera: Drop file name from header comment blocksLaurent Pinchart
2024-05-08ipa: libipa: Allow creation of empty HistogramDaniel Scally
2022-07-27raspberrypi: Update Copyright statement in all Raspberry Pi source filesNaushir Patuck
2022-03-28ipa: libipa: Histogram: Constify the constructor spanJean-Michel Hautbois
2021-11-24ipa: libipa: Convert to pragma onceKieran Bingham
2021-06-25libcamera/base: Move span to base libraryKieran Bingham
2021-04-22ipa: ipu3: Add a histogram classJean-Michel Hautbois
14'>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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * v4l2_device.h - V4L2 Device
 */
#ifndef __LIBCAMERA_V4L2_DEVICE_H__
#define __LIBCAMERA_V4L2_DEVICE_H__

#include <atomic>
#include <string>
#include <vector>

#include <linux/videodev2.h>

#include <libcamera/signal.h>

#include "log.h"

namespace libcamera {

class Buffer;
class BufferPool;
class EventNotifier;
class MediaDevice;
class MediaEntity;

struct V4L2Capability final : v4l2_capability {
	const char *driver() const
	{
		return reinterpret_cast<const char *>(v4l2_capability::driver);
	}
	const char *card() const
	{
		return reinterpret_cast<const char *>(v4l2_capability::card);
	}
	const char *bus_info() const
	{
		return reinterpret_cast<const char *>(v4l2_capability::bus_info);
	}
	unsigned int device_caps() const
	{
		return capabilities & V4L2_CAP_DEVICE_CAPS
				    ? v4l2_capability::device_caps
				    : v4l2_capability::capabilities;
	}
	bool isMultiplanar() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
					V4L2_CAP_VIDEO_OUTPUT_MPLANE);
	}
	bool isCapture() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
					V4L2_CAP_VIDEO_CAPTURE_MPLANE |
					V4L2_CAP_META_CAPTURE);
	}
	bool isOutput() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_OUTPUT |
					V4L2_CAP_VIDEO_OUTPUT_MPLANE);
	}
	bool isVideo() const
	{
		return device_caps() & (V4L2_CAP_VIDEO_CAPTURE |
					V4L2_CAP_VIDEO_CAPTURE_MPLANE |
					V4L2_CAP_VIDEO_OUTPUT |
					V4L2_CAP_VIDEO_OUTPUT_MPLANE);
	}
	bool isMeta() const
	{
		return device_caps() & V4L2_CAP_META_CAPTURE;
	}
	bool isVideoCapture() const
	{
		return isVideo() && isCapture();
	}
	bool isVideoOutput() const
	{
		return isVideo() && isOutput();
	}
	bool isMetaCapture() const
	{
		return isMeta() && isCapture();
	}
	bool hasStreaming() const
	{
		return device_caps() & V4L2_CAP_STREAMING;
	}
};

class V4L2DeviceFormat
{
public:
	uint32_t width;
	uint32_t height;
	uint32_t fourcc;

	struct {
		uint32_t size;
		uint32_t bpl;
	} planes[3];
	unsigned int planesCount;

	const std::string toString() const;
};

class V4L2Device : protected Loggable
{
public:
	explicit V4L2Device(const std::string &deviceNode);
	explicit V4L2Device(const MediaEntity *entity);
	V4L2Device(const V4L2Device &) = delete;
	~V4L2Device();

	V4L2Device &operator=(const V4L2Device &) = delete;

	int open();
	bool isOpen() const;
	void close();

	const char *driverName() const { return caps_.driver(); }
	const char *deviceName() const { return caps_.card(); }
	const char *busName() const { return caps_.bus_info(); }
	const std::string &deviceNode() const { return deviceNode_; }

	int getFormat(V4L2DeviceFormat *format);
	int setFormat(V4L2DeviceFormat *format);

	int exportBuffers(BufferPool *pool);
	int importBuffers(BufferPool *pool);
	int releaseBuffers();

	int queueBuffer(Buffer *buffer);
	Signal<Buffer *> bufferReady;

	int streamOn();
	int streamOff();

	static V4L2Device *fromEntityName(const MediaDevice *media,
					  const std::string &entity);

protected:
	std::string logPrefix() const;

private:
	int getFormatSingleplane(V4L2DeviceFormat *format);
	int setFormatSingleplane(V4L2DeviceFormat *format);

	int getFormatMultiplane(V4L2DeviceFormat *format);
	int setFormatMultiplane(V4L2DeviceFormat *format);

	int requestBuffers(unsigned int count);
	int createPlane(Buffer *buffer, unsigned int plane,
			unsigned int length);

	Buffer *dequeueBuffer();
	void bufferAvailable(EventNotifier *notifier);

	std::string deviceNode_;
	int fd_;
	V4L2Capability caps_;

	enum v4l2_buf_type bufferType_;
	enum v4l2_memory memoryType_;

	BufferPool *bufferPool_;
	std::atomic<unsigned int> queuedBuffersCount_;

	EventNotifier *fdEvent_;
};

} /* namespace libcamera */

#endif /* __LIBCAMERA_V4L2_DEVICE_H__ */