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

Laurent Pinchart laurent.pinchart at ideasonboard.com
Thu Jan 21 22:23:57 CET 2021


Hi Paul,

Thank you for the patch.

On Thu, Jan 21, 2021 at 07:15:48PM +0900, Paul Elder wrote:
> 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)

It would be nice to split this change out, as proposed by Jacopo in the
review of 7/9.

>  {
>  	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

Does this consume 8 bytes ? I thought it only consumed 1.

> +	 * 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;

s/jpeg_orientation/jpegOrientation/

> +	resultMetadata->addEntry(ANDROID_JPEG_ORIENTATION, &jpeg_orientation, 1);
> +	exif.setOrientation(ret ? *entry.data.i32 : jpeg_orientation);

If ret != 0, this will be identical to jpeg_orientation, so I think you
can pass jpeg_orientation unconditionally to this function.

> +
>  	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;

This can be moved within the if () { ... }.

> +		if (thumbnailSize != Size(0, 0)) {
> +			configureThumbnailer(thumbnailSize, thumbnailer_.pixelFormat());

This isn't quite right, configure() function takes the size and pixel
format of the source, not of the target. I think you could simply pass
the target size to PostProcessorJpeg::generateThumbnail() and
Thumbnailer::createThumbnail(), and drop
Thumbnailer::computeThumbnailSize().

Would it also make sense to split the thumbnail-related part of this
patch to a separate patch ?

> +			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);

method.size() will return 32, the string won't be truncated to the first
\0. Is that desired ?

Is this guaranteed to be 32 bytes long, or should you use entry.count as
the size ?

> +		exif.setGPSMethod(method);
> +		resultMetadata->addEntry(ANDROID_JPEG_GPS_PROCESSING_METHOD,
> +					 entry.data.u8, 32);

Same here.

> +	}
>  
>  	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);

Unless I'm mistaken, it should be just a matter of passing the quality
to Encoder::encode() (with a default of 95 maybe), dropping the quality_
member of EncoderLibJpeg and moving the jpeg_set_quality() call to
EncoderLibJpeg::encode(). This can be done on top of this series, but it
would be nice to address it soon before we forget about it.

>  
>  	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__ */

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list