From patchwork Thu Aug 27 09:49:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9409 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 A1FB2BDC71 for ; Thu, 27 Aug 2020 09:50:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 6EFD0628F3; Thu, 27 Aug 2020 11:50:00 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="mqpmjfck"; dkim-atps=neutral Received: from o1.b.az.sendgrid.net (o1.b.az.sendgrid.net [208.117.55.133]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A49FF628EE for ; Thu, 27 Aug 2020 11:49:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=CL8M3ZR4RQZWTlKSzmcm0YXa7L4iD7mVLRwY/N4znJ0=; b=mqpmjfck9axoH+Cegh0itPuhi8Co11YVd9NGZD0ZSt2/Gza8CRNXu/YpvMKmhQwykuxJ JLle0MoM4v6TUhipa1H/B7DC1rGbCp5k6+M0jVw+V/YRg+kIMtFuMGqsgm6/b/LiSKIdET +LGbdvkwEJKGj7LeCswOHmG1e6tEeyipA= Received: by filterdrecv-p3iad2-56cd58c948-klxrx with SMTP id filterdrecv-p3iad2-56cd58c948-klxrx-19-5F4781C5-1F 2020-08-27 09:49:57.457193922 +0000 UTC m=+41470.438757443 Received: from mail.uajain.com (unknown) by ismtpd0001p1maa1.sendgrid.net (SG) with ESMTP id 8Hn06XtCS6agBN5D2UumrQ Thu, 27 Aug 2020 09:49:56.971 +0000 (UTC) From: Umang Jain Date: Thu, 27 Aug 2020 09:49:57 +0000 (UTC) Message-Id: <20200827094930.21662-3-email@uajain.com> In-Reply-To: <20200827094930.21662-1-email@uajain.com> References: <20200827094930.21662-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcZj5z9XHfwy1Ge6f/0y1LY4dIL4p7iA32g13uWqIA7L6xaE+fSo7Pg77wK9esz5zfIoUdJv/J3ceCl4YzUM+JjybTITjGsTJ58YoPRLkP33oSLVxCuMwNW2P9lRTt5qDFFSYKmxCzDep7d1MEl9zqx59AOApAuYBssEEHMQyjKA7tYZH8UzZuFv6fwTOm3WB0FPF9cAWQ45SfkdLPC1/l+g== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v3 2/2] android: jpeg: Support a 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" From: Kieran Bingham 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 at 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 --- src/android/camera_device.cpp | 18 +++++++- 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 | 62 ++++++++++++++++++++++++++++ src/android/jpeg/exif.h | 9 ++++ 6 files changed, 100 insertions(+), 4 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index de6f86f..54ca9c6 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,22 @@ 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)); + exif.generate(); + + 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..58bc1ca 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()) { @@ -214,5 +215,11 @@ int EncoderLibJpeg::encode(const FrameBuffer *source, jpeg_finish_compress(&compress_); + 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()); + return size; } 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 ce9c69b..56733fd 100644 --- a/src/android/jpeg/exif.cpp +++ b/src/android/jpeg/exif.cpp @@ -157,6 +157,68 @@ int Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::stri return 0; } +int Exif::setMake(const std::string &make) +{ + return setString(EXIF_IFD_0, EXIF_TAG_MAKE, EXIF_FORMAT_ASCII, make); +} + +int Exif::setModel(const std::string &model) +{ + return setString(EXIF_IFD_0, EXIF_TAG_MODEL, EXIF_FORMAT_ASCII, model); +} + +int Exif::setSize(Size size) +{ + setShort(EXIF_IFD_0, EXIF_TAG_IMAGE_LENGTH, size.height); + setLong(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION, size.height); + + setShort(EXIF_IFD_0, EXIF_TAG_IMAGE_WIDTH, size.width); + setLong(EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION, size.width); + + return 0; +} + +int Exif::setTimestamp(const time_t timestamp) +{ + char str[20]; + std::strftime(str, sizeof(str), "%Y:%m:%d %H:%M:%S", + std::localtime(×tamp)); + std::string ts(str); + int ret = -1; + + ret = setString(EXIF_IFD_0, EXIF_TAG_DATE_TIME, EXIF_FORMAT_ASCII, ts); + if (ret < 0) + return ret; + + ret = setString(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_FORMAT_ASCII, ts); + if (ret < 0) + return ret; + + ret = setString(EXIF_IFD_EXIF, EXIF_TAG_DATE_TIME_DIGITIZED, EXIF_FORMAT_ASCII, ts); + if (ret < 0) + return ret; + + return ret; +} + +int Exif::setOrientation(int orientation) +{ + int value = 1; + switch (orientation) { + case 90: + value = 6; + break; + case 180: + value = 3; + break; + case 270: + value = 8; + break; + } + + return setShort(EXIF_IFD_0, EXIF_TAG_ORIENTATION, value); +} + int Exif::generate() { if (exif_data_) { diff --git a/src/android/jpeg/exif.h b/src/android/jpeg/exif.h index 3a132ba..102b07c 100644 --- a/src/android/jpeg/exif.h +++ b/src/android/jpeg/exif.h @@ -9,8 +9,10 @@ #include +#include #include +#include #include class Exif @@ -19,6 +21,13 @@ public: Exif(); ~Exif(); + int setMake(const std::string &make); + int setModel(const std::string &model); + + int setOrientation(int orientation); + int setSize(libcamera::Size size); + int setTimestamp(const time_t timestamp); + libcamera::Span data() const { return { exif_data_, size_ }; } int generate();