From patchwork Fri Sep 4 06:31:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9478 X-Patchwork-Delegate: umang.jain@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 1CA24BF019 for ; Fri, 4 Sep 2020 06:31:21 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DCD36629B6; Fri, 4 Sep 2020 08:31:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="Gf2YeNNW"; dkim-atps=neutral Received: from mail.uajain.com (static.126.159.217.95.clients.your-server.de [95.217.159.126]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 33BD66036F for ; Fri, 4 Sep 2020 08:31:19 +0200 (CEST) From: Umang Jain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=uajain.com; s=mail; t=1599201078; bh=wItggernxYqx/0qdbms0u/2TVGnoK5ZXYfbxbGHOggw=; h=From:To:Cc:Subject:In-Reply-To:References; b=Gf2YeNNW0Qj1mXKvePJVOCbkXIdWnYrhDQCLjYBPDkiT5Wir/B/dkiZb6P1tKGPg0 tCe5IE8imSHj4eUj2t6aMraWTsEqj7NDuKPi1Pp9kq+JPF03g5X2ZFfBEkH+agRA+e Qr6FSlcgRikmek/aSZ0G6Pz68gEy1f3DvRnbcRhb0XWJGmG96lvbdMxRW2qVrp84ix ZgYPQV53iVYGbn2M3eXx+xqZmA/01HSOBf7gtZPrhzLJgZHzlgTxXqpSjZlIJnUmiw zviARwrQ8p34j2DbVOex5M2QDw+Eyo1n52A3Rx4JlkdndSxCgfIDl1TGOkb5bYZzqV Zd+oJF6Vdbw+A== To: libcamera-devel@lists.libcamera.org Date: Fri, 4 Sep 2020 12:01:00 +0530 Message-Id: <20200904063101.29133-2-email@uajain.com> In-Reply-To: <20200904063101.29133-1-email@uajain.com> References: <20200904063101.29133-1-email@uajain.com> Mime-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 1/2] libcamera: android: Add EXIF infrastructure X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Kieran Bingham Provide helper classes to utilise the libexif interfaces and link against libexif to support tag additions when creating JPEG images. Signed-off-by: Kieran Bingham Signed-off-by: Umang Jain Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/jpeg/exif.cpp | 188 ++++++++++++++++++++++++++++++++++++++ src/android/jpeg/exif.h | 46 ++++++++++ src/android/meson.build | 2 + 3 files changed, 236 insertions(+) create mode 100644 src/android/jpeg/exif.cpp create mode 100644 src/android/jpeg/exif.h diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp new file mode 100644 index 0000000..41ddf48 --- /dev/null +++ b/src/android/jpeg/exif.cpp @@ -0,0 +1,188 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * exif.cpp - EXIF tag creation using libexif + */ + +#include "exif.h" + +#include "libcamera/internal/log.h" + +using namespace libcamera; + +LOG_DEFINE_CATEGORY(EXIF) + +/* + * The Exif class should be instantiated and specific properties set + * through the exposed public API. + * + * Once all desired properties have been set, the user shall call + * generate() to process the entries and generate the Exif data. + * + * Calls to generate() must check the return code to determine if any error + * occurred during the construction of the Exif data, and if successful the + * data can be obtained using the data() method. + */ +Exif::Exif() + : valid_(false), data_(nullptr), exifData_(0), size_(0) +{ + /* Create an ExifMem allocator to construct entries. */ + mem_ = exif_mem_new_default(); + if (!mem_) { + LOG(EXIF, Error) << "Failed to allocate ExifMem Allocator"; + return; + } + + data_ = exif_data_new_mem(mem_); + if (!data_) { + LOG(EXIF, Error) << "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 (exifData_) + free(exifData_); + + 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, Error) << "Failed to allocated new entry"; + valid_ = false; + return nullptr; + } + + exif_content_add_entry(content, entry); + exif_entry_initialize(entry, tag); + + return entry; +} + +ExifEntry *Exif::createEntry(ExifIfd ifd, ExifTag tag, ExifFormat format, + unsigned long 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, Error) << "Failed to allocated new entry"; + valid_ = false; + return nullptr; + } + + void *buffer = exif_mem_alloc(mem_, size); + if (!buffer) { + LOG(EXIF, Error) << "Failed to allocate buffer for variable entry"; + exif_mem_unref(mem_); + valid_ = false; + return nullptr; + } + + entry->data = static_cast(buffer); + entry->components = components; + entry->format = format; + entry->size = size; + entry->tag = tag; + + exif_content_add_entry(content, entry); + + return entry; +} + +void Exif::setShort(ExifIfd ifd, ExifTag tag, uint16_t item) +{ + ExifEntry *entry = createEntry(ifd, tag); + if (!entry) + return; + + exif_set_short(entry->data, EXIF_BYTE_ORDER_INTEL, item); + exif_entry_unref(entry); +} + +void Exif::setLong(ExifIfd ifd, ExifTag tag, uint32_t item) +{ + ExifEntry *entry = createEntry(ifd, tag); + if (!entry) + return; + + exif_set_long(entry->data, EXIF_BYTE_ORDER_INTEL, item); + exif_entry_unref(entry); +} + +void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item) +{ + ExifEntry *entry = createEntry(ifd, tag); + if (!entry) + return; + + exif_set_rational(entry->data, EXIF_BYTE_ORDER_INTEL, item); + exif_entry_unref(entry); +} + +void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item) +{ + /* Pad 1 extra byte for null-terminated string. */ + size_t length = item.length() + 1; + + ExifEntry *entry = createEntry(ifd, tag, format, length, length); + if (!entry) + return; + + memcpy(entry->data, item.c_str(), length); + exif_entry_unref(entry); +} + +[[nodiscard]] int Exif::generate() +{ + if (exifData_) { + free(exifData_); + exifData_ = nullptr; + } + + if (!valid_) { + LOG(EXIF, Error) << "Generated EXIF data is invalid"; + return -1; + } + + exif_data_save_data(data_, &exifData_, &size_); + + LOG(EXIF, Debug) << "Created EXIF instance (" << size_ << " bytes)"; + + return 0; +} diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h new file mode 100644 index 0000000..8dfc324 --- /dev/null +++ b/src/android/jpeg/exif.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * exif.h - EXIF tag creator using libexif + */ +#ifndef __ANDROID_JPEG_EXIF_H__ +#define __ANDROID_JPEG_EXIF_H__ + +#include +#include + +#include + +#include + +class Exif +{ +public: + Exif(); + ~Exif(); + + libcamera::Span 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 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); + void setRational(ExifIfd ifd, ExifTag tag, ExifRational item); + + bool valid_; + + ExifData *data_; + ExifMem *mem_; + + unsigned char *exifData_; + unsigned int size_; +}; + +#endif /* __ANDROID_JPEG_EXIF_H__ */ diff --git a/src/android/meson.build b/src/android/meson.build index f7b81a4..ecb92f6 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -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([ @@ -14,6 +15,7 @@ android_camera_metadata_sources = files([ ]) android_deps = [ + dependency('libexif'), dependency('libjpeg'), ] From patchwork Fri Sep 4 06:40:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9479 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 64C49BE174 for ; Fri, 4 Sep 2020 06:40:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EB346629B6; Fri, 4 Sep 2020 08:40:34 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="S6mzDBTA"; dkim-atps=neutral Received: from mail.uajain.com (static.126.159.217.95.clients.your-server.de [95.217.159.126]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B94566036F for ; Fri, 4 Sep 2020 08:40:33 +0200 (CEST) From: Umang Jain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=uajain.com; s=mail; t=1599201633; bh=8JtHc3uH+e5QKUbcRrSPZKeN1OXWcVlYqmD/QC1t/Is=; h=From:To:Cc:Subject:In-Reply-To:References; b=S6mzDBTAoqWviXBNW4LGCti+Q2/iVIRWzlpy5VKlsFOvlznlG/znLSB9PWIS1JDEX gKz0IYWizUZPfVJ4v4fqyB3Yy0G5+Qd6Y0Loj2CkfJBCbkbLQjXvGv4i1hOmzvfx6r bN98U5Oi8U8CNdP9vEecdw87Ain33821zvdrtsXKxzP2Eq/GwFyf7UGWx3eOa8k8/P Y4A4jBbE18p3b+klaZSQ/9cgBHCXnGisnrT4cKVo5Wto1jfqo+xrdizhw+KMpULxLp Cbel6OGlfyMjUr5M3h7lv+JCr5dMHfbx9UpjXZFzg37NIThwsHCr11cJKDisODXnog WZDT2nw0BWe1A== To: libcamera-devel@lists.libcamera.org Date: Fri, 4 Sep 2020 12:10:19 +0530 Message-Id: <20200904064019.29869-1-email@uajain.com> In-Reply-To: <[libcamera-devel] [PATCH v6 0/2] Initial EXIF metadata support> References: <[libcamera-devel] [PATCH v6 0/2] Initial EXIF metadata support> Mime-Version: 1.0 Subject: [libcamera-devel] [PATCH v6 2/2] android: jpeg: Support an initial set of EXIF metadata tags X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Create a Exif object with various metadata tags set, just before the encoder starts to encode the frame. The object is passed directly as libcamera::Span<> to make sure EXIF tags can be set in a single place i.e. in CameraDevice and the encoder only has the job to write the data in the final output. Signed-off-by: Kieran Bingham Signed-off-by: Umang Jain Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 19 ++++++++++- src/android/jpeg/encoder.h | 3 +- src/android/jpeg/encoder_libjpeg.cpp | 9 ++++- src/android/jpeg/encoder_libjpeg.h | 3 +- src/android/jpeg/exif.cpp | 50 ++++++++++++++++++++++++++++ src/android/jpeg/exif.h | 8 +++++ 6 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index de6f86f..0328ac6 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -24,6 +24,7 @@ #include "system/graphics.h" #include "jpeg/encoder_libjpeg.h" +#include "jpeg/exif.h" using namespace libcamera; @@ -1434,7 +1435,23 @@ void CameraDevice::requestComplete(Request *request) continue; } - int jpeg_size = encoder->encode(buffer, mapped.maps()[0]); + /* Set EXIF metadata for various tags. */ + Exif exif; + /* \todo Set Make and Model from external vendor tags. */ + exif.setMake("libcamera"); + exif.setModel("cameraModel"); + exif.setOrientation(orientation_); + exif.setSize(cameraStream->size); + /* + * We set the frame's EXIF timestamp as the time of encode. + * Since the precision we need for EXIF timestamp is only one + * second, it is good enough. + */ + exif.setTimestamp(std::time(nullptr)); + if (exif.generate() != 0) + LOG(HAL, Error) << "Failed to get valid EXIF data"; + + int jpeg_size = encoder->encode(buffer, mapped.maps()[0], exif.data()); if (jpeg_size < 0) { LOG(HAL, Error) << "Failed to encode stream image"; status = CAMERA3_BUFFER_STATUS_ERROR; diff --git a/src/android/jpeg/encoder.h b/src/android/jpeg/encoder.h index f9eb88e..cf26d67 100644 --- a/src/android/jpeg/encoder.h +++ b/src/android/jpeg/encoder.h @@ -18,7 +18,8 @@ public: virtual int configure(const libcamera::StreamConfiguration &cfg) = 0; virtual int encode(const libcamera::FrameBuffer *source, - const libcamera::Span &destination) = 0; + const libcamera::Span &destination, + const libcamera::Span &exifData) = 0; }; #endif /* __ANDROID_JPEG_ENCODER_H__ */ diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp index 977538c..510613c 100644 --- a/src/android/jpeg/encoder_libjpeg.cpp +++ b/src/android/jpeg/encoder_libjpeg.cpp @@ -180,7 +180,8 @@ void EncoderLibJpeg::compressNV(const libcamera::MappedBuffer *frame) } int EncoderLibJpeg::encode(const FrameBuffer *source, - const libcamera::Span &dest) + const libcamera::Span &dest, + const libcamera::Span &exifData) { MappedFrameBuffer frame(source, PROT_READ); if (!frame.isValid()) { @@ -204,6 +205,12 @@ int EncoderLibJpeg::encode(const FrameBuffer *source, jpeg_start_compress(&compress_, TRUE); + if (exifData.size()) + /* Store Exif data in the JPEG_APP1 data block. */ + jpeg_write_marker(&compress_, JPEG_APP0 + 1, + static_cast(exifData.data()), + exifData.size()); + LOG(JPEG, Debug) << "JPEG Encode Starting:" << compress_.image_width << "x" << compress_.image_height; diff --git a/src/android/jpeg/encoder_libjpeg.h b/src/android/jpeg/encoder_libjpeg.h index aed081a..1e8df05 100644 --- a/src/android/jpeg/encoder_libjpeg.h +++ b/src/android/jpeg/encoder_libjpeg.h @@ -22,7 +22,8 @@ public: int configure(const libcamera::StreamConfiguration &cfg) override; int encode(const libcamera::FrameBuffer *source, - const libcamera::Span &destination) override; + const libcamera::Span &destination, + const libcamera::Span &exifData) override; private: void compressRGB(const libcamera::MappedBuffer *frame); diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp index 41ddf48..36922ef 100644 --- a/src/android/jpeg/exif.cpp +++ b/src/android/jpeg/exif.cpp @@ -168,6 +168,56 @@ void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::str exif_entry_unref(entry); } +void Exif::setMake(const std::string &make) +{ + setString(EXIF_IFD_0, EXIF_TAG_MAKE, EXIF_FORMAT_ASCII, make); +} + +void Exif::setModel(const std::string &model) +{ + setString(EXIF_IFD_0, EXIF_TAG_MODEL, EXIF_FORMAT_ASCII, model); +} + +void Exif::setSize(const Size &size) +{ + setLong(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, size.height); + setLong(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, size.width); +} + +void Exif::setTimestamp(time_t timestamp) +{ + char str[20]; + std::strftime(str, sizeof(str), "%Y:%m:%d %H:%M:%S", + std::localtime(×tamp)); + std::string ts(str); + + setString(EXIF_IFD_0, EXIF_TAG_DATE_TIME, EXIF_FORMAT_ASCII, ts); + setString(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_FORMAT_ASCII, ts); + setString(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_DIGITIZED, EXIF_FORMAT_ASCII, ts); +} + +void Exif::setOrientation(int orientation) +{ + int value; + switch (orientation) { + case 0: + default: + value = 1; + break; + case 90: + value = 6; + break; + case 180: + value = 3; + break; + case 270: + value = 8; + break; + } + + setShort(EXIF_IFD_0, EXIF_TAG_ORIENTATION, value); +} + [[nodiscard]] int Exif::generate() { if (exifData_) { diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h index 8dfc324..622de4c 100644 --- a/src/android/jpeg/exif.h +++ b/src/android/jpeg/exif.h @@ -12,6 +12,7 @@ #include +#include #include class Exif @@ -20,6 +21,13 @@ public: Exif(); ~Exif(); + void setMake(const std::string &make); + void setModel(const std::string &model); + + void setOrientation(int orientation); + void setSize(const libcamera::Size &size); + void setTimestamp(time_t timestamp); + libcamera::Span data() const { return { exifData_, size_ }; } [[nodiscard]] int generate();