[libcamera-devel] [PATCH v2 8/9] android: Set result metadata and EXIF fields based on request metadata

Paul Elder paul.elder at ideasonboard.com
Thu Jan 21 11:15:48 CET 2021


Set the following android result metadata:
- ANDROID_LENS_FOCAL_LENGTH
- ANDROID_LENS_APERTURE
- ANDROID_JPEG_GPS_TIMESTAMP
- ANDROID_JPEG_THUMBNAIL_SIZE
- ANDROID_JPEG_THUMBNAIL_QUALITY
- ANDROID_JPEG_GPS_COORDINATES
- ANDROID_JPEG_GPS_PROCESSING_METHOD
- ANDROID_JPEG_SIZE
- ANDROID_JPEG_QUALITY
- ANDROID_JPEG_ORIENTATION

And the following EXIF fields:
- GPSDatestamp
- GPSTimestamp
- GPSLocation
  - GPSLatitudeRef
  - GPSLatitude
  - GPSLongitudeRef
  - GPSLongitude
  - GPSAltitudeRef
  - GPSAltitude
- GPSProcessingMethod
- FocalLength
- ExposureTime
- FNumber
- ISO
- Flash
- WhiteBalance
- SubsecTime
- SubsecTimeOriginal
- SubsecTimeDigitized

Based on android request metadata.

This allows the following CTS tests to pass:
- android.hardware.camera2.cts.StillCaptureTest#testFocalLengths
- android.hardware.camera2.cts.StillCaptureTest#testJpegExif

Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>

---
Changes in v2:
- break out processControls and thumbnailer configuration into different
  patches
- fix android metadata entry number and size
- other small changes
---
 src/android/camera_device.cpp            | 30 ++++++--
 src/android/camera_device.h              |  5 +-
 src/android/camera_stream.cpp            |  7 +-
 src/android/camera_stream.h              |  4 +-
 src/android/jpeg/post_processor_jpeg.cpp | 90 ++++++++++++++++++++----
 src/android/jpeg/post_processor_jpeg.h   |  3 +-
 src/android/post_processor.h             |  3 +-
 7 files changed, 116 insertions(+), 26 deletions(-)

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index 49af221b..1b803c92 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -296,8 +296,9 @@ MappedCamera3Buffer::MappedCamera3Buffer(const buffer_handle_t camera3buffer,
  */
 
 CameraDevice::Camera3RequestDescriptor::Camera3RequestDescriptor(
-	Camera *camera, unsigned int frameNumber, unsigned int numBuffers)
-	: frameNumber_(frameNumber), numBuffers_(numBuffers)
+	Camera *camera, unsigned int frameNumber, unsigned int numBuffers,
+	CameraMetadata &settings)
+	: frameNumber_(frameNumber), numBuffers_(numBuffers), settings_(settings)
 {
 	buffers_ = new camera3_stream_buffer_t[numBuffers];
 
@@ -1691,9 +1692,12 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
 	 * The descriptor and the associated memory reserved here are freed
 	 * at request complete time.
 	 */
+	/* \todo Handle null request settings */
+	CameraMetadata settings(camera3Request->settings);
 	Camera3RequestDescriptor *descriptor =
 		new Camera3RequestDescriptor(camera_.get(), camera3Request->frame_number,
-					     camera3Request->num_output_buffers);
+					     camera3Request->num_output_buffers,
+					     settings);
 
 	LOG(HAL, Debug) << "Queueing Request to libcamera with "
 			<< descriptor->numBuffers_ << " HAL streams";
@@ -1828,6 +1832,7 @@ void CameraDevice::requestComplete(Request *request)
 		}
 
 		int ret = cameraStream->process(*buffer, &mapped,
+						descriptor->settings_,
 						resultMetadata.get());
 		if (ret) {
 			status = CAMERA3_BUFFER_STATUS_ERROR;
@@ -1924,14 +1929,21 @@ CameraDevice::getResultMetadata(Camera3RequestDescriptor *descriptor,
 
 	/*
 	 * \todo Keep this in sync with the actual number of entries.
-	 * Currently: 33 entries, 75 bytes
+	 * Currently: 40 entries, 163 bytes
 	 *
 	 * Reserve more space for the JPEG metadata set by the post-processor.
 	 * Currently: ANDROID_JPEG_SIZE (int32_t), ANDROID_JPEG_QUALITY (byte),
+	 * ANDROID_JPEG_GPS_COORDINATES (double x 3) = 24 bytes
+	 * ANDROID_JPEG_GPS_PROCESSING_METHOD (byte x 32) = 32 bytes
+	 * ANDROID_JPEG_GPS_TIMESTAMP (int64) = 8 bytes
 	 * ANDROID_JPEG_ORIENTATION (int32_t) = 3 entries, 9 bytes.
+	 * ANDROID_JPEG_THUMBNAIL_QUALITY (int32 x 2) = 8 bytes
+	 * ANDROID_JPEG_THUMBNAIL_SIZE (int32 x 2) = 8 bytes
+	 * ANDROID_LENS_APERTURE (float) = 4 bytes
+	 * ANDROID_LENS_FOCAL_LENGTH (float) = 4 bytes
 	 */
 	std::unique_ptr<CameraMetadata> resultMetadata =
-		std::make_unique<CameraMetadata>(33, 75);
+		std::make_unique<CameraMetadata>(40, 163);
 	if (!resultMetadata->isValid()) {
 		LOG(HAL, Error) << "Failed to allocate static metadata";
 		return nullptr;
@@ -1996,6 +2008,14 @@ CameraDevice::getResultMetadata(Camera3RequestDescriptor *descriptor,
 	value = ANDROID_FLASH_STATE_UNAVAILABLE;
 	resultMetadata->addEntry(ANDROID_FLASH_STATE, &value, 1);
 
+	camera_metadata_ro_entry_t entry;
+	int ret = descriptor->settings_.getEntry(ANDROID_LENS_APERTURE, &entry);
+	if (ret)
+		resultMetadata->addEntry(ANDROID_LENS_APERTURE, entry.data.f, 1);
+
+	float focal_length = 1.0;
+	resultMetadata->addEntry(ANDROID_LENS_FOCAL_LENGTH, &focal_length, 1);
+
 	value = ANDROID_LENS_STATE_STATIONARY;
 	resultMetadata->addEntry(ANDROID_LENS_STATE, &value, 1);
 
diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index a285d0a1..111a7d8f 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -24,6 +24,7 @@
 #include "libcamera/internal/log.h"
 #include "libcamera/internal/message.h"
 
+#include "camera_metadata.h"
 #include "camera_stream.h"
 #include "camera_worker.h"
 #include "jpeg/encoder.h"
@@ -78,7 +79,8 @@ private:
 	struct Camera3RequestDescriptor {
 		Camera3RequestDescriptor(libcamera::Camera *camera,
 					 unsigned int frameNumber,
-					 unsigned int numBuffers);
+					 unsigned int numBuffers,
+					 CameraMetadata &settings);
 		~Camera3RequestDescriptor();
 
 		uint32_t frameNumber_;
@@ -86,6 +88,7 @@ private:
 		camera3_stream_buffer_t *buffers_;
 		std::vector<std::unique_ptr<libcamera::FrameBuffer>> frameBuffers_;
 		std::unique_ptr<CaptureRequest> request_;
+		CameraMetadata settings_;
 	};
 
 	struct Camera3StreamConfiguration {
diff --git a/src/android/camera_stream.cpp b/src/android/camera_stream.cpp
index dba351a4..4c8020e5 100644
--- a/src/android/camera_stream.cpp
+++ b/src/android/camera_stream.cpp
@@ -96,12 +96,15 @@ int CameraStream::configure()
 }
 
 int CameraStream::process(const libcamera::FrameBuffer &source,
-			  MappedCamera3Buffer *dest, CameraMetadata *metadata)
+			  MappedCamera3Buffer *dest,
+			  const CameraMetadata &requestMetadata,
+			  CameraMetadata *resultMetadata)
 {
 	if (!postProcessor_)
 		return 0;
 
-	return postProcessor_->process(source, dest->maps()[0], metadata);
+	return postProcessor_->process(source, dest->maps()[0],
+				       requestMetadata, resultMetadata);
 }
 
 FrameBuffer *CameraStream::getBuffer()
diff --git a/src/android/camera_stream.h b/src/android/camera_stream.h
index cc9d5470..298ffbf6 100644
--- a/src/android/camera_stream.h
+++ b/src/android/camera_stream.h
@@ -119,7 +119,9 @@ public:
 
 	int configure();
 	int process(const libcamera::FrameBuffer &source,
-		    MappedCamera3Buffer *dest, CameraMetadata *metadata);
+		    MappedCamera3Buffer *dest,
+		    const CameraMetadata &requestMetadata,
+		    CameraMetadata *resultMetadata);
 	libcamera::FrameBuffer *getBuffer();
 	void putBuffer(libcamera::FrameBuffer *buffer);
 
diff --git a/src/android/jpeg/post_processor_jpeg.cpp b/src/android/jpeg/post_processor_jpeg.cpp
index 0c1226ad..ef5e011e 100644
--- a/src/android/jpeg/post_processor_jpeg.cpp
+++ b/src/android/jpeg/post_processor_jpeg.cpp
@@ -90,17 +90,26 @@ void PostProcessorJpeg::generateThumbnail(const FrameBuffer &source,
 
 int PostProcessorJpeg::process(const FrameBuffer &source,
 			       Span<uint8_t> destination,
-			       CameraMetadata *metadata)
+			       const CameraMetadata &requestMetadata,
+			       CameraMetadata *resultMetadata)
 {
 	if (!encoder_)
 		return 0;
 
+	camera_metadata_ro_entry_t entry;
+	int ret;
+
 	/* 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(cameraDevice_->orientation());
+	exif.setMake(cameraDevice_->cameraMake());
+	exif.setModel(cameraDevice_->cameraModel());
+
+	ret = requestMetadata.getEntry(ANDROID_JPEG_ORIENTATION, &entry);
+
+	const uint32_t jpeg_orientation = ret ? *entry.data.i32 : 0;
+	resultMetadata->addEntry(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
+	exif.setOrientation(ret ? *entry.data.i32 : jpeg_orientation);
+
 	exif.setSize(streamSize_);
 	/*
 	 * We set the frame's EXIF timestamp as the time of encode.
@@ -109,10 +118,62 @@ int PostProcessorJpeg::process(const FrameBuffer &source,
 	 */
 	exif.setTimestamp(std::time(nullptr));
 
-	std::vector<unsigned char> thumbnail;
-	generateThumbnail(source, &thumbnail);
-	if (!thumbnail.empty())
-		exif.setThumbnail(thumbnail, Exif::Compression::JPEG);
+	exif.setExposureTime(0);
+	ret = requestMetadata.getEntry(ANDROID_LENS_APERTURE, &entry);
+	if (ret)
+		exif.setAperture(*entry.data.f);
+	exif.setISO(100);
+	exif.setFlash(Exif::Flash::FlashNotPresent);
+	exif.setWhiteBalance(Exif::WhiteBalance::Auto);
+
+	exif.setSubsecTime(0);
+	exif.setSubsecTimeOriginal(0);
+	exif.setSubsecTimeDigitized(0);
+
+	exif.setFocalLength(1.0);
+
+	ret = requestMetadata.getEntry(ANDROID_JPEG_GPS_TIMESTAMP, &entry);
+	if (ret) {
+		exif.setGPSDateTimestamp(*entry.data.i64);
+		resultMetadata->addEntry(ANDROID_JPEG_GPS_TIMESTAMP,
+					 entry.data.i64, 1);
+	}
+
+	ret = requestMetadata.getEntry(ANDROID_JPEG_THUMBNAIL_SIZE, &entry);
+	if (ret) {
+		const int32_t *data = entry.data.i32;
+		Size thumbnailSize = { static_cast<uint32_t>(data[0]),
+				       static_cast<uint32_t>(data[1]) };
+
+		std::vector<unsigned char> thumbnail;
+		if (thumbnailSize != Size(0, 0)) {
+			configureThumbnailer(thumbnailSize, thumbnailer_.pixelFormat());
+			generateThumbnail(source, &thumbnail);
+			exif.setThumbnail(thumbnail, Exif::Compression::JPEG);
+		}
+
+		resultMetadata->addEntry(ANDROID_JPEG_THUMBNAIL_SIZE, data, 2);
+
+		/* \todo Use this quality as a parameter to the encoder */
+		ret = requestMetadata.getEntry(ANDROID_JPEG_THUMBNAIL_QUALITY, &entry);
+		if (ret)
+			resultMetadata->addEntry(ANDROID_JPEG_THUMBNAIL_QUALITY, entry.data.u8, 1);
+	}
+
+	ret = requestMetadata.getEntry(ANDROID_JPEG_GPS_COORDINATES, &entry);
+	if (ret) {
+		exif.setGPSLocation(entry.data.d);
+		resultMetadata->addEntry(ANDROID_JPEG_GPS_COORDINATES,
+					 entry.data.d, 3);
+	}
+
+	ret = requestMetadata.getEntry(ANDROID_JPEG_GPS_PROCESSING_METHOD, &entry);
+	if (ret) {
+		std::string method(entry.data.u8, entry.data.u8 + 32);
+		exif.setGPSMethod(method);
+		resultMetadata->addEntry(ANDROID_JPEG_GPS_PROCESSING_METHOD,
+					 entry.data.u8, 32);
+	}
 
 	if (exif.generate() != 0)
 		LOG(JPEG, Error) << "Failed to generate valid EXIF data";
@@ -143,13 +204,12 @@ int PostProcessorJpeg::process(const FrameBuffer &source,
 	blob->jpeg_size = jpeg_size;
 
 	/* Update the JPEG result Metadata. */
-	metadata->addEntry(ANDROID_JPEG_SIZE, &jpeg_size, 1);
-
-	const uint32_t jpeg_quality = 95;
-	metadata->addEntry(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
+	resultMetadata->addEntry(ANDROID_JPEG_SIZE, &jpeg_size, 1);
 
-	const uint32_t jpeg_orientation = 0;
-	metadata->addEntry(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
+	/* \todo Configure JPEG encoder with this */
+	ret = requestMetadata.getEntry(ANDROID_JPEG_QUALITY, &entry);
+	const uint32_t jpeg_quality = ret ? *entry.data.u8 : 95;
+	resultMetadata->addEntry(ANDROID_JPEG_QUALITY, &jpeg_quality, 1);
 
 	return 0;
 }
diff --git a/src/android/jpeg/post_processor_jpeg.h b/src/android/jpeg/post_processor_jpeg.h
index c545c29c..d07c9fe5 100644
--- a/src/android/jpeg/post_processor_jpeg.h
+++ b/src/android/jpeg/post_processor_jpeg.h
@@ -26,7 +26,8 @@ public:
 		      const libcamera::StreamConfiguration &outcfg) override;
 	int process(const libcamera::FrameBuffer &source,
 		    libcamera::Span<uint8_t> destination,
-		    CameraMetadata *metadata) override;
+		    const CameraMetadata &requestMetadata,
+		    CameraMetadata *resultMetadata) override;
 
 private:
 	void generateThumbnail(const libcamera::FrameBuffer &source,
diff --git a/src/android/post_processor.h b/src/android/post_processor.h
index e0f91880..bda93bb4 100644
--- a/src/android/post_processor.h
+++ b/src/android/post_processor.h
@@ -22,7 +22,8 @@ public:
 			      const libcamera::StreamConfiguration &outCfg) = 0;
 	virtual int process(const libcamera::FrameBuffer &source,
 			    libcamera::Span<uint8_t> destination,
-			    CameraMetadata *metadata) = 0;
+			    const CameraMetadata &requestMetadata,
+			    CameraMetadata *resultMetadata) = 0;
 };
 
 #endif /* __ANDROID_POST_PROCESSOR_H__ */
-- 
2.27.0



More information about the libcamera-devel mailing list