summaryrefslogtreecommitdiff
path: root/src/android/jpeg/exif.h
blob: 446d53f3800538cce23dcf58de4abaa9ea47aa74 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * EXIF tag creator using libexif
 */

#pragma once

#include <chrono>
#include <string>
#include <time.h>
#include <vector>

#include <libexif/exif-data.h>

#include <libcamera/base/span.h>

#include <libcamera/geometry.h>

class Exif
{
public:
	Exif();
	~Exif();

	enum Compression {
		None = 1,
		JPEG = 6,
	};

	enum Flash {
		/* bit 0 */
		Fired = 0x01,
		/* bits 1 and 2 */
		StrobeDetected = 0x04,
		StrobeNotDetected = 0x06,
		/* bits 3 and 4 */
		ModeCompulsoryFiring = 0x08,
		ModeCompulsorySuppression = 0x10,
		ModeAuto = 0x18,
		/* bit 5 */
		FlashNotPresent = 0x20,
		/* bit 6 */
		RedEye = 0x40,
	};

	enum WhiteBalance {
		Auto = 0,
		Manual = 1,
	};

	enum StringEncoding {
		NoEncoding = 0,
		ASCII = 1,
		Unicode = 2,
	};

	void setMake(const std::string &make);
	void setModel(const std::string &model);

	void setOrientation(int orientation);
	void setSize(const libcamera::Size &size);
	void setThumbnail(std::vector<unsigned char> &&thumbnail,
			  Compression compression);
	void setTimestamp(time_t timestamp, std::chrono::milliseconds msec);

	void setGPSDateTimestamp(time_t timestamp);
	void setGPSLocation(const double *coords);
	void setGPSMethod(const std::string &method);

	void setFocalLength(float length);
	void setExposureTime(uint64_t nsec);
	void setAperture(float size);
	void setISO(uint16_t iso);
	void setFlash(Flash flash);
	void setWhiteBalance(WhiteBalance wb);

	libcamera::Span<const uint8_t> data() const { return { exifData_, size_ }; }
	[[nodiscard]] int generate();

private:
	ExifEntry *createEntry(ExifIfd ifd, ExifTag tag);
	ExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
			       unsigned long components, unsigned int size);

	void setByte(ExifIfd ifd, ExifTag tag, uint8_t item);
	void setShort(ExifIfd ifd, ExifTag tag, uint16_t item);
	void setLong(ExifIfd ifd, ExifTag tag, uint32_t item);
	void setString(ExifIfd ifd, ExifTag tag, ExifFormat format,
		       const std::string &item,
		       StringEncoding encoding = NoEncoding);
	void setRational(ExifIfd ifd, ExifTag tag, ExifRational item);
	void setRational(ExifIfd ifd, ExifTag tag,
			 libcamera::Span<const ExifRational> items);

	std::tuple<int, int, int> degreesToDMS(double decimalDegrees);
	void setGPSDMS(ExifIfd ifd, ExifTag tag, int deg, int min, int sec);

	std::u16string utf8ToUtf16(const std::string &str);

	bool valid_;

	ExifData *data_;
	ExifMem *mem_;
	ExifByteOrder order_;

	unsigned char *exifData_;
	unsigned int size_;

	std::vector<unsigned char> thumbnailData_;
};
/span> 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 | V4L2_CAP_META_OUTPUT); } 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 isM2M() const { return device_caps() & (V4L2_CAP_VIDEO_M2M | V4L2_CAP_VIDEO_M2M_MPLANE); } bool isMeta() const { return device_caps() & (V4L2_CAP_META_CAPTURE | V4L2_CAP_META_OUTPUT); } bool isVideoCapture() const { return isVideo() && isCapture(); } bool isVideoOutput() const { return isVideo() && isOutput(); } bool isMetaCapture() const { return isMeta() && isCapture(); } bool isMetaOutput() const { return isMeta() && isOutput(); } bool hasStreaming() const { return device_caps() & V4L2_CAP_STREAMING; } bool hasMediaController() const { return device_caps() & V4L2_CAP_IO_MC; } }; class V4L2BufferCache { public: V4L2BufferCache(unsigned int numEntries); V4L2BufferCache(const std::vector<std::unique_ptr<FrameBuffer>> &buffers); ~V4L2BufferCache(); bool isEmpty() const; int get(const FrameBuffer &buffer); void put(unsigned int index); private: class Entry { public: Entry(); Entry(bool free, uint64_t lastUsed, const FrameBuffer &buffer); bool operator==(const FrameBuffer &buffer) const; bool free_; uint64_t lastUsed_; private: struct Plane { Plane(const FrameBuffer::Plane &plane) : fd(plane.fd.get()), length(plane.length) { } int fd; unsigned int length; }; std::vector<Plane> planes_; }; std::atomic<uint64_t> lastUsedCounter_; std::vector<Entry> cache_; /* \todo Expose the miss counter through an instrumentation API. */ unsigned int missCounter_; }; class V4L2DeviceFormat { public: struct Plane { uint32_t size = 0; uint32_t bpl = 0; }; V4L2PixelFormat fourcc; Size size; std::optional<ColorSpace> colorSpace; std::array<Plane, 3> planes; unsigned int planesCount = 0; const std::string toString() const; }; std::ostream &operator<<(std::ostream &out, const V4L2DeviceFormat &f); class V4L2VideoDevice : public V4L2Device { public: using Formats = std::map<V4L2PixelFormat, std::vector<SizeRange>>; explicit V4L2VideoDevice(const std::string &deviceNode); explicit V4L2VideoDevice(const MediaEntity *entity); ~V4L2VideoDevice(); int open(); int open(SharedFD handle, enum v4l2_buf_type type); 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 V4L2Capability &caps() const { return caps_; } int getFormat(V4L2DeviceFormat *format); int tryFormat(V4L2DeviceFormat *format); int setFormat(V4L2DeviceFormat *format); Formats formats(uint32_t code = 0); int setSelection(unsigned int target, Rectangle *rect); int allocateBuffers(unsigned int count, std::vector<std::unique_ptr<FrameBuffer>> *buffers); int exportBuffers(unsigned int count, std::vector<std::unique_ptr<FrameBuffer>> *buffers); int importBuffers(unsigned int count); int releaseBuffers(); int queueBuffer(FrameBuffer *buffer); Signal<FrameBuffer *> bufferReady; int streamOn(); int streamOff(); void setDequeueTimeout(utils::Duration timeout); Signal<> dequeueTimeout; static std::unique_ptr<V4L2VideoDevice> fromEntityName(const MediaDevice *media, const std::string &entity); V4L2PixelFormat toV4L2PixelFormat(const PixelFormat &pixelFormat) const; protected: std::string logPrefix() const override; private: LIBCAMERA_DISABLE_COPY(V4L2VideoDevice) enum class State { Streaming, Stopping, Stopped, }; int initFormats(); int getFormatMeta(V4L2DeviceFormat *format); int trySetFormatMeta(V4L2DeviceFormat *format, bool set); int getFormatMultiplane(V4L2DeviceFormat *format); int trySetFormatMultiplane(V4L2DeviceFormat *format, bool set); int getFormatSingleplane(V4L2DeviceFormat *format); int trySetFormatSingleplane(V4L2DeviceFormat *format, bool set); std::vector<V4L2PixelFormat> enumPixelformats(uint32_t code); std::vector<SizeRange> enumSizes(V4L2PixelFormat pixelFormat); int requestBuffers(unsigned int count, enum v4l2_memory memoryType); int createBuffers(unsigned int count, std::vector<std::unique_ptr<FrameBuffer>> *buffers); std::unique_ptr<FrameBuffer> createBuffer(unsigned int index); UniqueFD exportDmabufFd(unsigned int index, unsigned int plane); void bufferAvailable(); FrameBuffer *dequeueBuffer(); void watchdogExpired(); template<typename T> static std::optional<ColorSpace> toColorSpace(const T &v4l2Format); V4L2Capability caps_; V4L2DeviceFormat format_; const PixelFormatInfo *formatInfo_; std::unordered_set<V4L2PixelFormat> pixelFormats_; enum v4l2_buf_type bufferType_; enum v4l2_memory memoryType_; V4L2BufferCache *cache_; std::map<unsigned int, FrameBuffer *> queuedBuffers_; EventNotifier *fdBufferNotifier_; State state_; std::optional<unsigned int> firstFrame_; Timer watchdog_; utils::Duration watchdogDuration_; }; class V4L2M2MDevice { public: V4L2M2MDevice(const std::string &deviceNode); ~V4L2M2MDevice(); int open(); void close(); V4L2VideoDevice *output() { return output_; } V4L2VideoDevice *capture() { return capture_; } private: std::string deviceNode_; V4L2VideoDevice *output_; V4L2VideoDevice *capture_; }; } /* namespace libcamera */