new file mode 100644
@@ -0,0 +1,176 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2020, Google Inc.
+ *
+ * exif.cpp - EXIF tag creation and parser using libexif
+ */
+
+#include "exif.h"
+
+#include "libcamera/internal/log.h"
+
+using namespace libcamera;
+
+LOG_DEFINE_CATEGORY(EXIF)
+
+Exif::Exif()
+ : valid_(false), exif_data_(0), size_(0)
+{
+ /* Create an ExifMem allocator to construct entries. */
+ mem_ = exif_mem_new_default();
+ if (!mem_) {
+ LOG(EXIF, Fatal) << "Failed to allocate ExifMem Allocator";
+ return;
+ }
+
+ data_ = exif_data_new_mem(mem_);
+ if (!data_) {
+ LOG(EXIF, Fatal) << "Failed to allocate an ExifData structure";
+ return;
+ }
+
+ valid_ = true;
+
+ exif_data_set_option(data_, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
+ exif_data_set_data_type(data_, EXIF_DATA_TYPE_COMPRESSED);
+
+ /*
+ * Big-Endian: EXIF_BYTE_ORDER_MOTOROLA
+ * Little Endian: EXIF_BYTE_ORDER_INTEL
+ */
+ exif_data_set_byte_order(data_, EXIF_BYTE_ORDER_INTEL);
+
+ /* Create the mandatory EXIF fields with default data */
+ exif_data_fix(data_);
+}
+
+Exif::~Exif()
+{
+ if (exif_data_)
+ free(exif_data_);
+
+ if (data_)
+ exif_data_unref(data_);
+
+ if (mem_)
+ exif_mem_unref(mem_);
+}
+
+ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag)
+{
+ ExifContent *content = data_->ifd[ifd];
+ ExifEntry *entry = exif_content_get_entry(content, tag);
+
+ if (entry) {
+ exif_entry_ref(entry);
+ return entry;
+ }
+
+ entry = exif_entry_new_mem(mem_);
+ if (!entry) {
+ LOG(EXIF, Fatal) << "Failed to allocated new entry";
+ return nullptr;
+ }
+
+ entry->tag = tag;
+
+ exif_content_add_entry(content, entry);
+ exif_entry_initialize(entry, tag);
+
+ return entry;
+}
+
+ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
+ uint64_t components, unsigned int size)
+{
+ ExifContent *content = data_->ifd[ifd];
+
+ /* Replace any existing entry with the same tag. */
+ ExifEntry *existing = exif_content_get_entry(content, tag);
+ exif_content_remove_entry(content, existing);
+
+ ExifEntry *entry = exif_entry_new_mem(mem_);
+ if (!entry) {
+ LOG(EXIF, Fatal) << "Failed to allocated new entry";
+ return nullptr;
+ }
+
+ void *buffer = exif_mem_alloc(mem_, size);
+ if (!buffer) {
+ LOG(EXIF, Fatal) << "Failed to allocate buffer for variable entry";
+ exif_mem_unref(mem_);
+ return nullptr;
+ }
+
+ entry->data = static_cast<unsigned char *>(buffer);
+ entry->components = components;
+ entry->format = format;
+ entry->size = size;
+ entry->tag = tag;
+
+ exif_content_add_entry(content, entry);
+
+ return entry;
+}
+
+int Exif::setShort(ExifIfd ifd, ExifTag tag, uint16_t item)
+{
+ ExifEntry *entry = createEntry(ifd, tag);
+
+ exif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, item);
+ exif_entry_unref(entry);
+
+ return 0;
+}
+
+int Exif::setLong(ExifIfd ifd, ExifTag tag, uint32_t item)
+{
+ ExifEntry *entry = createEntry(ifd, tag);
+
+ exif_set_long(entry->data, EXIF_BYTE_ORDER_INTEL, item);
+ exif_entry_unref(entry);
+
+ return 0;
+}
+
+int Exif::setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, uint32_t denominator)
+{
+ ExifEntry *entry = createEntry(ifd, tag);
+ ExifRational item{ numerator, denominator };
+
+ exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, item);
+ exif_entry_unref(entry);
+
+ return 0;
+}
+
+int Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item)
+{
+ size_t length = item.length();
+
+ ExifEntry *entry = createEntry(ifd, tag, format, length, length);
+ if (!entry) {
+ LOG(EXIF, Error) << "Failed to add tag: " << tag;
+ return -ENOMEM;
+ }
+
+ memcpy(entry->data, item.c_str(), length);
+ exif_entry_unref(entry);
+
+ return 0;
+}
+
+Span<uint8_t> Exif::generate()
+{
+ if (exif_data_) {
+ free(exif_data_);
+ exif_data_ = nullptr;
+ }
+
+ exif_data_save_data(data_, &exif_data_, &size_);
+
+ LOG(EXIF, Debug) << "Created EXIF instance (" << size_ << " bytes)";
+
+ return { exif_data_, size_ };
+}
+
new file mode 100644
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2020, Google Inc.
+ *
+ * exif.h - EXIF tag creator and parser using libexif
+ */
+#ifndef __LIBCAMERA_EXIF_H__
+#define __LIBCAMERA_EXIF_H__
+
+#include <libexif/exif-data.h>
+
+#include <libcamera/span.h>
+
+#include <string>
+
+class Exif
+{
+public:
+ Exif();
+ ~Exif();
+
+ int setShort(ExifIfd ifd, ExifTag tag, uint16_t item);
+ int setLong(ExifIfd ifd, ExifTag tag, uint32_t item);
+ int setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item);
+ int setRational(ExifIfd ifd, ExifTag tag, uint32_t numerator, uint32_t denominator);
+
+ libcamera::Span<uint8_t> generate();
+ unsigned char *data() const { return exif_data_; }
+ unsigned int size() const { return size_; }
+
+private:
+ ExifEntry *createEntry(ExifIfd ifd, ExifTag tag);
+ ExifEntry *createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format,
+ uint64_t components, unsigned int size);
+
+ bool valid_;
+
+ ExifData *data_;
+ ExifMem *mem_;
+
+ unsigned char *exif_data_;
+ unsigned int size_;
+};
+
+#endif /* __LIBCAMERA_EXIF_H__ */
@@ -7,6 +7,7 @@ android_hal_sources = files([
'camera_metadata.cpp',
'camera_ops.cpp',
'jpeg/encoder_libjpeg.cpp',
+ 'jpeg/exif.cpp',
])
android_camera_metadata_sources = files([
@@ -15,6 +16,7 @@ android_camera_metadata_sources = files([
android_deps = [
dependency('libjpeg'),
+ dependency('libexif'),
]
android_camera_metadata = static_library('camera_metadata',