summaryrefslogtreecommitdiff
path: root/.clang-format
AgeCommit message (Expand)Author
2019-09-23clang-format: Don't indent namespacesLaurent Pinchart
2019-02-13clang-format: Remove space after templateLaurent Pinchart
2019-02-04clang-format: Enable BreakBeforeTernaryOperatorsKieran Bingham
2019-01-22libcamera: Add clang-format styleLaurent Pinchart
href='#n18'>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 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 299 300 301 302 303 304 305 306 307 308
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * ipa_data_serializer.h - Image Processing Algorithm data serializer
 */
#ifndef __LIBCAMERA_INTERNAL_IPA_DATA_SERIALIZER_H__
#define __LIBCAMERA_INTERNAL_IPA_DATA_SERIALIZER_H__

#include <deque>
#include <iostream>
#include <string.h>
#include <tuple>
#include <type_traits>
#include <vector>

#include <libcamera/base/log.h>

#include <libcamera/control_ids.h>
#include <libcamera/framebuffer.h>
#include <libcamera/geometry.h>
#include <libcamera/ipa/ipa_interface.h>

#include "libcamera/internal/byte_stream_buffer.h"
#include "libcamera/internal/camera_sensor.h"
#include "libcamera/internal/control_serializer.h"

namespace libcamera {

LOG_DECLARE_CATEGORY(IPADataSerializer)

namespace {

template<typename T,
	 typename std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
void appendPOD(std::vector<uint8_t> &vec, T val)
{
	constexpr size_t byteWidth = sizeof(val);
	vec.resize(vec.size() + byteWidth);
	memcpy(&*(vec.end() - byteWidth), &val, byteWidth);
}

template<typename T,
	 std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
T readPOD(std::vector<uint8_t>::const_iterator it, size_t pos,
	  std::vector<uint8_t>::const_iterator end)
{
	ASSERT(pos + it < end);

	T ret = 0;
	memcpy(&ret, &(*(it + pos)), sizeof(ret));

	return ret;
}

template<typename T,
	 std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
T readPOD(std::vector<uint8_t> &vec, size_t pos)
{
	return readPOD<T>(vec.cbegin(), pos, vec.end());
}

} /* namespace */

template<typename T>
class IPADataSerializer
{
public:
	static std::tuple<std::vector<uint8_t>, std::vector<int32_t>>
	serialize(const T &data, ControlSerializer *cs = nullptr);

	static T deserialize(const std::vector<uint8_t> &data,
			     ControlSerializer *cs = nullptr);
	static T deserialize(std::vector<uint8_t>::const_iterator dataBegin,
			     std::vector<uint8_t>::const_iterator dataEnd,
			     ControlSerializer *cs = nullptr);

	static T deserialize(const std::vector<uint8_t> &data,
			     const std::vector<int32_t> &fds,
			     ControlSerializer *cs = nullptr);
	static T deserialize(std::vector<uint8_t>::const_iterator dataBegin,
			     std::vector<uint8_t>::const_iterator dataEnd,
			     std::vector<int32_t>::const_iterator fdsBegin,
			     std::vector<int32_t>::const_iterator fdsEnd,
			     ControlSerializer *cs = nullptr);
};

#ifndef __DOXYGEN__

/*
 * Serialization format for vector of type V:
 *
 * 4 bytes - uint32_t Length of vector, in number of elements
 *
 * For every element in the vector:
 *
 * 4 bytes - uint32_t Size of element, in bytes
 * 4 bytes - uint32_t Number of fds for the element
 * X bytes - Serialized element
 *
 * \todo Support elements that are references
 */
template<typename V>
class IPADataSerializer<std::vector<V>>
{
public:
	static std::tuple<std::vector<uint8_t>, std::vector<int32_t>>
	serialize(const std::vector<V> &data, ControlSerializer *cs = nullptr)
	{
		std::vector<uint8_t> dataVec;
		std::vector<int32_t> fdsVec;

		/* Serialize the length. */
		uint32_t vecLen = data.size();
		appendPOD<uint32_t>(dataVec, vecLen);

		/* Serialize the members. */
		for (auto const &it : data) {
			std::vector<uint8_t> dvec;
			std::vector<int32_t> fvec;

			std::tie(dvec, fvec) =
				IPADataSerializer<V>::serialize(it, cs);

			appendPOD<uint32_t>(dataVec, dvec.size());
			appendPOD<uint32_t>(dataVec, fvec.size());

			dataVec.insert(dataVec.end(), dvec.begin(), dvec.end());
			fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end());
		}

		return { dataVec, fdsVec };
	}

	static std::vector<V> deserialize(std::vector<uint8_t> &data, ControlSerializer *cs = nullptr)
	{
		return deserialize(data.cbegin(), data.end(), cs);
	}

	static std::vector<V> deserialize(std::vector<uint8_t>::const_iterator dataBegin,
					  std::vector<uint8_t>::const_iterator dataEnd,
					  ControlSerializer *cs = nullptr)
	{
		std::vector<int32_t> fds;
		return deserialize(dataBegin, dataEnd, fds.cbegin(), fds.end(), cs);
	}

	static std::vector<V> deserialize(std::vector<uint8_t> &data, std::vector<int32_t> &fds,
					  ControlSerializer *cs = nullptr)
	{
		return deserialize(data.cbegin(), data.end(), fds.cbegin(), fds.end(), cs);
	}

	static std::vector<V> deserialize(std::vector<uint8_t>::const_iterator dataBegin,
					  std::vector<uint8_t>::const_iterator dataEnd,
					  std::vector<int32_t>::const_iterator fdsBegin,
					  [[maybe_unused]] std::vector<int32_t>::const_iterator fdsEnd,
					  ControlSerializer *cs = nullptr)
	{
		uint32_t vecLen = readPOD<uint32_t>(dataBegin, 0, dataEnd);
		std::vector<V> ret(vecLen);

		std::vector<uint8_t>::const_iterator dataIter = dataBegin + 4;
		std::vector<int32_t>::const_iterator fdIter = fdsBegin;
		for (uint32_t i = 0; i < vecLen; i++) {
			uint32_t sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd);
			uint32_t sizeofFds  = readPOD<uint32_t>(dataIter, 4, dataEnd);
			dataIter += 8;

			ret[i] = IPADataSerializer<V>::deserialize(dataIter,
								   dataIter + sizeofData,
								   fdIter,
								   fdIter + sizeofFds,
								   cs);

			dataIter += sizeofData;
			fdIter += sizeofFds;
		}

		return ret;
	}
};

/*
 * Serialization format for map of key type K and value type V:
 *
 * 4 bytes - uint32_t Length of map, in number of pairs
 *
 * For every pair in the map:
 *
 * 4 bytes - uint32_t Size of key, in bytes
 * 4 bytes - uint32_t Number of fds for the key
 * X bytes - Serialized key
 * 4 bytes - uint32_t Size of value, in bytes
 * 4 bytes - uint32_t Number of fds for the value
 * X bytes - Serialized value
 *
 * \todo Support keys or values that are references
 */
template<typename K, typename V>
class IPADataSerializer<std::map<K, V>>
{
public:
	static std::tuple<std::vector<uint8_t>, std::vector<int32_t>>
	serialize(const std::map<K, V> &data, ControlSerializer *cs = nullptr)
	{
		std::vector<uint8_t> dataVec;
		std::vector<int32_t> fdsVec;

		/* Serialize the length. */
		uint32_t mapLen = data.size();
		appendPOD<uint32_t>(dataVec, mapLen);

		/* Serialize the members. */
		for (auto const &it : data) {
			std::vector<uint8_t> dvec;
			std::vector<int32_t> fvec;

			std::tie(dvec, fvec) =
				IPADataSerializer<K>::serialize(it.first, cs);

			appendPOD<uint32_t>(dataVec, dvec.size());
			appendPOD<uint32_t>(dataVec, fvec.size());

			dataVec.insert(dataVec.end(), dvec.begin(), dvec.end());
			fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end());

			std::tie(dvec, fvec) =
				IPADataSerializer<V>::serialize(it.second, cs);

			appendPOD<uint32_t>(dataVec, dvec.size());
			appendPOD<uint32_t>(dataVec, fvec.size());

			dataVec.insert(dataVec.end(), dvec.begin(), dvec.end());
			fdsVec.insert(fdsVec.end(), fvec.begin(), fvec.end());
		}

		return { dataVec, fdsVec };
	}

	static std::map<K, V> deserialize(std::vector<uint8_t> &data, ControlSerializer *cs = nullptr)
	{
		return deserialize(data.cbegin(), data.end(), cs);
	}

	static std::map<K, V> deserialize(std::vector<uint8_t>::const_iterator dataBegin,
					  std::vector<uint8_t>::const_iterator dataEnd,
					  ControlSerializer *cs = nullptr)
	{
		std::vector<int32_t> fds;
		return deserialize(dataBegin, dataEnd, fds.cbegin(), fds.end(), cs);
	}

	static std::map<K, V> deserialize(std::vector<uint8_t> &data, std::vector<int32_t> &fds,
					  ControlSerializer *cs = nullptr)
	{
		return deserialize(data.cbegin(), data.end(), fds.cbegin(), fds.end(), cs);
	}

	static std::map<K, V> deserialize(std::vector<uint8_t>::const_iterator dataBegin,
					  std::vector<uint8_t>::const_iterator dataEnd,
					  std::vector<int32_t>::const_iterator fdsBegin,
					  [[maybe_unused]] std::vector<int32_t>::const_iterator fdsEnd,
					  ControlSerializer *cs = nullptr)
	{
		std::map<K, V> ret;

		uint32_t mapLen = readPOD<uint32_t>(dataBegin, 0, dataEnd);

		std::vector<uint8_t>::const_iterator dataIter = dataBegin + 4;
		std::vector<int32_t>::const_iterator fdIter = fdsBegin;
		for (uint32_t i = 0; i < mapLen; i++) {
			uint32_t sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd);
			uint32_t sizeofFds  = readPOD<uint32_t>(dataIter, 4, dataEnd);
			dataIter += 8;

			K key = IPADataSerializer<K>::deserialize(dataIter,
								  dataIter + sizeofData,
								  fdIter,
								  fdIter + sizeofFds,
								  cs);

			dataIter += sizeofData;
			fdIter += sizeofFds;
			sizeofData = readPOD<uint32_t>(dataIter, 0, dataEnd);
			sizeofFds  = readPOD<uint32_t>(dataIter, 4, dataEnd);
			dataIter += 8;

			const V value = IPADataSerializer<V>::deserialize(dataIter,
									  dataIter + sizeofData,
									  fdIter,
									  fdIter + sizeofFds,
									  cs);
			ret.insert({ key, value });

			dataIter += sizeofData;
			fdIter += sizeofFds;
		}

		return ret;
	}
};

#endif /* __DOXYGEN__ */

} /* namespace libcamera */

#endif /* __LIBCAMERA_INTERNAL_IPA_DATA_SERIALIZER_H__ */