/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2019, Google Inc. * * camera_metadata.cpp - libcamera Android Camera Metadata Helper */ #include "camera_metadata.h" #include using namespace libcamera; LOG_DEFINE_CATEGORY(CameraMetadata) CameraMetadata::CameraMetadata() : metadata_(nullptr), valid_(false), resized_(false) { } CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) : resized_(false) { metadata_ = allocate_camera_metadata(entryCapacity, dataCapacity); valid_ = metadata_ != nullptr; } CameraMetadata::CameraMetadata(const camera_metadata_t *metadata) : resized_(false) { metadata_ = clone_camera_metadata(metadata); valid_ = metadata_ != nullptr; } CameraMetadata::CameraMetadata(const CameraMetadata &other) : CameraMetadata(other.getMetadata()) { } CameraMetadata::~CameraMetadata() { if (metadata_) free_camera_metadata(metadata_); } CameraMetadata &CameraMetadata::operator=(const CameraMetadata &other) { if (this == &other) return *this; if (metadata_) free_camera_metadata(metadata_); metadata_ = clone_camera_metadata(other.getMetadata()); valid_ = metadata_ != nullptr; return *this; } std::tuple CameraMetadata::usage() const { size_t currentEntryCount = get_camera_metadata_entry_count(metadata_); size_t currentDataCount = get_camera_metadata_data_count(metadata_); return { currentEntryCount, currentDataCount }; } bool CameraMetadata::getEntry(uint32_t tag, camera_metadata_ro_entry_t *entry) const { if (find_camera_metadata_ro_entry(metadata_, tag, entry)) return false; return true; } /* * \brief Resize the metadata container, if necessary * \param[in] count Number of entries to add to the container * \param[in] size Total size of entries to add, in bytes * \return True if resize was successful or unnecessary, false otherwise */ bool CameraMetadata::resize(size_t count, size_t size) { if (!valid_) return false; if (!count && !size) return true; size_t currentEntryCount = get_camera_metadata_entry_count(metadata_); size_t currentEntryCapacity = get_camera_metadata_entry_capacity(metadata_); size_t newEntryCapacity = currentEntryCapacity < currentEntryCount + count ? currentEntryCapacity * 2 : currentEntryCapacity; size_t currentDataCount = get_camera_metadata_data_count(metadata_); size_t currentDataCapacity = get_camera_metadata_data_capacity(metadata_); size_t newDataCapacity = currentDataCapacity < currentDataCount + size ? currentDataCapacity * 2 : currentDataCapacity; if (newEntryCapacity > currentEntryCapacity || newDataCapacity > currentDataCapacity) { camera_metadata_t *oldMetadata = metadata_; metadata_ = allocate_camera_metadata(newEntryCapacity, newDataCapacity); if (!metadata_) { metadata_ = oldMetadata; return false; } LOG(CameraMetadata, Info) << "Resized: old entry capacity " << currentEntryCapacity << ", old data capacity " << currentDataCapacity << ", new entry capacity " << newEntryCapacity << ", new data capacity " << newDataCapacity; append_camera_metadata(metadata_, oldMetadata); free_camera_metadata(oldMetadata); resized_ = true; } return true; } template<> bool CameraMetadata::entryContains(uint32_t tag, uint8_t value) const { camera_metadata_ro_entry_t entry; if (!getEntry(tag, &entry)) return false; for (unsigned int i = 0; i < entry.count; i++) { if (entry.data.u8[i] == value) return true; } return false; } bool CameraMetadata::hasEntry(uint32_t tag) const { camera_metadata_ro_entry_t entry; return getEntry(tag, &entry); } bool CameraMetadata::addEntry(uint32_t tag, const void *data, size_t count, size_t elementSize) { if (!valid_) return false; if (!resize(1, count * elementSize)) { LOG(CameraMetadata, Error) << "Failed to resize"; valid_ = false; return false; } if (!add_camera_metadata_entry(metadata_, tag, data, count)) return true; const char *name = get_camera_metadata_tag_name(tag); if (name) LOG(CameraMetadata, Error) << "Failed to add tag " << name; else LOG(CameraMetadata, Error) << "Failed to add unknown tag " << tag; valid_ = false; return false; } bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t count, size_t elementSize) { if (!valid_) return false; camera_metadata_entry_t entry; int ret = find_camera_metadata_entry(metadata_, tag, &entry); if (ret) { const char *name = get_camera_metadata_tag_name(tag); LOG(CameraMetadata, Error) << "Failed to update tag " << (name ? name : "") << ": not present"; return false; } if (camera_metadata_type_size[entry.type] != elementSize) { const char *name = get_camera_metadata_tag_name(tag); LOG(CameraMetadata, Fatal) << "Invalid element size for tag " << (name ? name : ""); return false; } size_t oldSize = calculate_camera_metadata_entry_data_size(entry.type, entry.count); size_t newSize = calculate_camera_metadata_entry_data_size(entry.type, count); size_t sizeIncrement = newSize - oldSize > 0 ? newSize - oldSize : 0; if (!resize(0, sizeIncrement)) { LOG(CameraMetadata, Error) << "Failed to resize"; valid_ = false; return false; } ret = update_camera_metadata_entry(metadata_, entry.index, data, count, nullptr); if (!ret) return true; const char *name = get_camera_metadata_tag_name(tag); LOG(CameraMetadata, Error) << "Failed to update tag " << (name ? name : ""); valid_ = false; return false; } camera_metadata_t *CameraMetadata::getMetadata() { return valid_ ? metadata_ : nullptr; } const camera_metadata_t *CameraMetadata::getMetadata() const { return valid_ ? metadata_ : nullptr; } id='n22' href='#n22'>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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * ipu3-pack - Convert unpacked RAW10 Bayer data to the IPU3 packed Bayer formats
 *
 * Copyright 2022 Umang Jain <umang.jain@ideasonboard.com>
 */
#define _GNU_SOURCE

#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

static void usage(const char *argv0)
{

	printf("Usage: %s input-file output-file\n", basename(argv0));
	printf("Convert unpacked RAW10 Bayer data to the IPU3 packed Bayer formats\n");
	printf("If the output-file '-', output data will be written to standard output\n");
}

int main(int argc, char *argv[])
{
	int in_fd;
	int out_fd;
	int ret;

	if (argc != 3) {
		usage(argv[0]);
		return 1;
	}

	in_fd = open(argv[1], O_RDONLY);
	if (in_fd == -1) {
		fprintf(stderr, "Failed to open input file '%s': %s\n",
			argv[1], strerror(errno));
		return 1;
	}

	if (strcmp(argv[2], "-") == 0) {
		out_fd = STDOUT_FILENO;
	} else {
		out_fd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0644);
		if (out_fd == -1) {
			fprintf(stderr, "Failed to open output file '%s': %s\n",
				argv[2], strerror(errno));
			close(in_fd);
			return 1;
		}
	}

	while (1) {
		uint16_t in_data[25];
		uint8_t out_data[32];
		unsigned int i;

		ret = read(in_fd, in_data, sizeof(in_data));
		if (ret < 0) {
			fprintf(stderr, "Failed to read input data: %s\n",
				strerror(errno));
			goto done;
		}

		if ((unsigned)ret < sizeof(in_data)) {
			if (ret != 0)
				fprintf(stderr, "%u bytes of stray data at end of input\n",
					ret);
			goto done;
		}

		for (i = 0; i < 30; ++i) {
			unsigned int index = (i * 8) / 10;
			unsigned int msb_shift = (i * 8) % 10;
			unsigned int lsb_shift = 10 - msb_shift;

			out_data[i] = ((in_data[index] >> msb_shift) & 0xff)
				    | ((in_data[index+1] << lsb_shift) & 0xff);
		}