[v4,13/13] mali-c55: implement support for ScalerCrop
diff mbox series

Message ID 20240709143913.3276983-14-dan.scally@ideasonboard.com
State New
Headers show
Series
  • Miscellaneous Mali-C55 Pipeline Fixes
Related show

Commit Message

Dan Scally July 9, 2024, 2:39 p.m. UTC
From: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

Implement support for the ScalerCrop control that allows to apply a
digital zoom to the captured streams.

Initialize the camera controls at camera registration time and update
them at configure time as the sensor's analogue crop size might change
depending on the desired Camera configuration.

Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
---
 src/libcamera/pipeline/mali-c55/mali-c55.cpp | 133 +++++++++++++++++++
 1 file changed, 133 insertions(+)

Comments

Kieran Bingham Oct. 9, 2024, 3:14 p.m. UTC | #1
Quoting Daniel Scally (2024-07-09 15:39:13)
> From: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> 
> Implement support for the ScalerCrop control that allows to apply a
> digital zoom to the captured streams.
> 
> Initialize the camera controls at camera registration time and update
> them at configure time as the sensor's analogue crop size might change
> depending on the desired Camera configuration.
> 
> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>
> ---
>  src/libcamera/pipeline/mali-c55/mali-c55.cpp | 133 +++++++++++++++++++
>  1 file changed, 133 insertions(+)
> 
> diff --git a/src/libcamera/pipeline/mali-c55/mali-c55.cpp b/src/libcamera/pipeline/mali-c55/mali-c55.cpp
> index f5ca2ca4..916b1d30 100644
> --- a/src/libcamera/pipeline/mali-c55/mali-c55.cpp
> +++ b/src/libcamera/pipeline/mali-c55/mali-c55.cpp
> @@ -85,6 +85,8 @@ public:
>         int pixfmtToMbusCode(const PixelFormat &pixFmt) const;
>         const PixelFormat &bestRawFormat() const;
>  
> +       void updateControls();
> +
>         PixelFormat adjustRawFormat(const PixelFormat &pixFmt) const;
>         Size adjustRawSizes(const PixelFormat &pixFmt, const Size &rawSize) const;
>  
> @@ -264,6 +266,27 @@ const PixelFormat &MaliC55CameraData::bestRawFormat() const
>         return invalidPixFmt;
>  }
>  
> +void MaliC55CameraData::updateControls()
> +{
> +       if (!sensor_)
> +               return;
> +
> +       IPACameraSensorInfo sensorInfo;
> +       int ret = sensor_->sensorInfo(&sensorInfo);
> +       if (ret) {
> +               LOG(MaliC55, Error) << "Failed to retrieve sensor info";
> +               return;
> +       }
> +
> +       ControlInfoMap::Map controls;
> +       Rectangle ispMinCrop{ 0, 0, 640, 480 };

I'm weary that 0,0 might not be accurate.
I assume 640x480 is the minimum size from the ISP, but we might also
have a minimum x,y if the PixelArrayActiveAreas doesn't start at 0,0...

https://libcamera.org/api-html/namespacelibcamera_1_1controls.html#a854f9e84af827f57ada03fcc12090c56
```
◆ ScalerCrop
libcamera::controls::ScalerCrop

Sets the image portion that will be scaled to form the whole of the
final output image.

The (x,y) location of this rectangle is relative to the
PixelArrayActiveAreas that is being used. The units remain native sensor
pixels, even if the sensor is being used in a binning or skipping mode.

This control is only present when the pipeline supports scaling. Its
maximum valid value is given by the properties::ScalerCropMaximum
property, and the two can be used to implement digital zoom.
```


$ cam -c1 -p

Using camera /base/soc@0/bus@30800000/i2c@30a30000/sensor@1a as cam0
Property: SystemDevices = [ 20743, 20744 ]
Property: PixelArrayActiveAreas = [ (108, 40)/5472x3648 ]
Property: PixelArraySize = 5472x3648


So for this sensor (IMX283) - the minimum ScalerCrop surely must be
	{ 108, 40, 640, 480 }; ?


Ohhh - wait. No it's "relative" to the PixelArrayActiveAreas? So 0,0
scaler crop is correct!


I think we've got some work (in core and all pipeline handlers) to make
sure we clear up exactly what pixels are being delivered to
applications when modes and crops are adjusted...


> +       controls[&controls::ScalerCrop] =
> +               ControlInfo(ispMinCrop, sensorInfo.analogCrop,
> +                           sensorInfo.analogCrop);
> +
> +       controlInfo_ = ControlInfoMap(std::move(controls), controls::controls);
> +}
> +
>  /*
>   * Make sure the provided raw pixel format is supported and adjust it to
>   * one of the supported ones if it's not.
> @@ -544,6 +567,8 @@ private:
>                                      const StreamConfiguration &config,
>                                      V4L2SubdeviceFormat &subdevFormat);
>  
> +       void applyScalerCrop(Camera *camera, const ControlList &controls);
> +
>         void registerMaliCamera(std::unique_ptr<MaliC55CameraData> data,
>                                 const std::string &name);
>         bool registerTPGCamera(MediaLink *link);
> @@ -871,6 +896,8 @@ int PipelineHandlerMaliC55::configure(Camera *camera,
>                 pipe->stream = stream;
>         }
>  
> +       data->updateControls();
> +
>         return 0;
>  }
>  
> @@ -918,6 +945,102 @@ void PipelineHandlerMaliC55::stopDevice([[maybe_unused]] Camera *camera)
>         }
>  }
>  
> +void PipelineHandlerMaliC55::applyScalerCrop(Camera *camera,
> +                                            const ControlList &controls)
> +{
> +       MaliC55CameraData *data = cameraData(camera);
> +
> +       const auto &scalerCrop = controls.get<Rectangle>(controls::ScalerCrop);
> +       if (!scalerCrop)
> +               return;
> +
> +       if (!data->sensor_) {
> +               LOG(MaliC55, Error) << "ScalerCrop not supported for TPG";
> +               return;
> +       }
> +
> +       Rectangle nativeCrop = *scalerCrop;
> +
> +       IPACameraSensorInfo sensorInfo;
> +       int ret = data->sensor_->sensorInfo(&sensorInfo);
> +       if (ret) {
> +               LOG(MaliC55, Error) << "Failed to retrieve sensor info";
> +               return;
> +       }
> +
> +       /*
> +        * The ScalerCrop rectangle re-scaling in the ISP crop rectangle
> +        * comes straight from the RPi pipeline handler.
> +        *
> +        * Create a version of the crop rectangle aligned to the analogue crop
> +        * rectangle top-left coordinates and scaled in the [analogue crop to
> +        * output frame] ratio to take into account binning/skipping on the
> +        * sensor.
> +        */
> +       Rectangle ispCrop = nativeCrop.translatedBy(-sensorInfo.analogCrop
> +                                                              .topLeft());
> +       ispCrop.scaleBy(sensorInfo.outputSize, sensorInfo.analogCrop.size());
> +
> +       /*
> +        * The crop rectangle should be:
> +        * 1. At least as big as ispMinCropSize_, once that's been
> +        *    enlarged to the same aspect ratio.
> +        * 2. With the same mid-point, if possible.
> +        * 3. But it can't go outside the sensor area.
> +        */
> +       Rectangle ispMinCrop{ 0, 0, 640, 480 };
> +       Size minSize = ispMinCrop.size().expandedToAspectRatio(nativeCrop.size());
> +       Size size = ispCrop.size().expandedTo(minSize);
> +       ispCrop = size.centeredTo(ispCrop.center())
> +                     .enclosedIn(Rectangle(sensorInfo.outputSize));
> +
> +       /*
> +        * As the resizer can't upscale, the crop rectangle has to be larger
> +        * than the larger stream output size.
> +        */
> +       Size maxYuvSize;
> +       for (MaliC55Pipe &pipe : pipes_) {
> +               if (!pipe.stream)
> +                       continue;
> +
> +               const StreamConfiguration &config = pipe.stream->configuration();
> +               if (isFormatRaw(config.pixelFormat)) {
> +                       LOG(MaliC55, Debug) << "Cannot crop with a RAW stream";
> +                       return;
> +               }
> +
> +               Size streamSize = config.size;
> +               if (streamSize.width > maxYuvSize.width)
> +                       maxYuvSize.width = streamSize.width;
> +               if (streamSize.height > maxYuvSize.height)
> +                       maxYuvSize.height = streamSize.height;
> +       }
> +
> +       ispCrop.size().expandTo(maxYuvSize);
> +
> +       /*
> +        * Now apply the scaler crop to each enabled output. This overrides the
> +        * crop configuration performed at configure() time and can cause
> +        * square pixels if the crop rectangle and scaler output FOV ratio are
> +        * different.
> +        */
> +       for (MaliC55Pipe &pipe : pipes_) {
> +               if (!pipe.stream)
> +                       continue;
> +
> +               /* Create a copy to avoid setSelection() to modify ispCrop. */
> +               Rectangle pipeCrop = ispCrop;
> +               ret = pipe.resizer->setSelection(0, V4L2_SEL_TGT_CROP, &pipeCrop);
> +               if (ret) {
> +                       LOG(MaliC55, Error)
> +                               << "Failed to apply crop to "
> +                               << (pipe.stream == &data->frStream_ ?
> +                                   "FR" : "DS") << " pipe";
> +                       return;
> +               }

So we're lacking 'stream' metadata ... so we can't do this per stream -
but I think we should definitely be reporting whatever &pipeCrop gets
set to (as returned from the kernel) as metadata for what actually got
set.

With that if it's possible, or just handled on top later if it's not
possible right now:


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

--
Kieran


> +       }
> +}
> +
>  int PipelineHandlerMaliC55::queueRequestDevice(Camera *camera, Request *request)
>  {
>         int ret;
> @@ -930,6 +1053,15 @@ int PipelineHandlerMaliC55::queueRequestDevice(Camera *camera, Request *request)
>                         return ret;
>         }
>  
> +       /*
> +        * Some controls need to be applied immediately, as in example,
> +        * the ScalerCrop one.
> +        *
> +        * \todo Move it buffer queue time (likely after the IPA has filled in
> +        * the parameters buffer) once we have plumbed the IPA loop in.
> +        */
> +       applyScalerCrop(camera, request->controls());
> +
>         return 0;
>  }
>  
> @@ -1005,6 +1137,7 @@ bool PipelineHandlerMaliC55::registerSensorCamera(MediaLink *ispLink)
>                         return false;
>  
>                 data->properties_ = data->sensor_->properties();
> +               data->updateControls();
>  
>                 registerMaliCamera(std::move(data), sensor->name());
>         }
> -- 
> 2.34.1
>
Kieran Bingham Oct. 9, 2024, 3:16 p.m. UTC | #2
In $SUBJECT to be consistent with the other commits in the series,
please add "libcamera: " as a prefix.

--
Kieran

Patch
diff mbox series

diff --git a/src/libcamera/pipeline/mali-c55/mali-c55.cpp b/src/libcamera/pipeline/mali-c55/mali-c55.cpp
index f5ca2ca4..916b1d30 100644
--- a/src/libcamera/pipeline/mali-c55/mali-c55.cpp
+++ b/src/libcamera/pipeline/mali-c55/mali-c55.cpp
@@ -85,6 +85,8 @@  public:
 	int pixfmtToMbusCode(const PixelFormat &pixFmt) const;
 	const PixelFormat &bestRawFormat() const;
 
+	void updateControls();
+
 	PixelFormat adjustRawFormat(const PixelFormat &pixFmt) const;
 	Size adjustRawSizes(const PixelFormat &pixFmt, const Size &rawSize) const;
 
@@ -264,6 +266,27 @@  const PixelFormat &MaliC55CameraData::bestRawFormat() const
 	return invalidPixFmt;
 }
 
+void MaliC55CameraData::updateControls()
+{
+	if (!sensor_)
+		return;
+
+	IPACameraSensorInfo sensorInfo;
+	int ret = sensor_->sensorInfo(&sensorInfo);
+	if (ret) {
+		LOG(MaliC55, Error) << "Failed to retrieve sensor info";
+		return;
+	}
+
+	ControlInfoMap::Map controls;
+	Rectangle ispMinCrop{ 0, 0, 640, 480 };
+	controls[&controls::ScalerCrop] =
+		ControlInfo(ispMinCrop, sensorInfo.analogCrop,
+			    sensorInfo.analogCrop);
+
+	controlInfo_ = ControlInfoMap(std::move(controls), controls::controls);
+}
+
 /*
  * Make sure the provided raw pixel format is supported and adjust it to
  * one of the supported ones if it's not.
@@ -544,6 +567,8 @@  private:
 				     const StreamConfiguration &config,
 				     V4L2SubdeviceFormat &subdevFormat);
 
+	void applyScalerCrop(Camera *camera, const ControlList &controls);
+
 	void registerMaliCamera(std::unique_ptr<MaliC55CameraData> data,
 				const std::string &name);
 	bool registerTPGCamera(MediaLink *link);
@@ -871,6 +896,8 @@  int PipelineHandlerMaliC55::configure(Camera *camera,
 		pipe->stream = stream;
 	}
 
+	data->updateControls();
+
 	return 0;
 }
 
@@ -918,6 +945,102 @@  void PipelineHandlerMaliC55::stopDevice([[maybe_unused]] Camera *camera)
 	}
 }
 
+void PipelineHandlerMaliC55::applyScalerCrop(Camera *camera,
+					     const ControlList &controls)
+{
+	MaliC55CameraData *data = cameraData(camera);
+
+	const auto &scalerCrop = controls.get<Rectangle>(controls::ScalerCrop);
+	if (!scalerCrop)
+		return;
+
+	if (!data->sensor_) {
+		LOG(MaliC55, Error) << "ScalerCrop not supported for TPG";
+		return;
+	}
+
+	Rectangle nativeCrop = *scalerCrop;
+
+	IPACameraSensorInfo sensorInfo;
+	int ret = data->sensor_->sensorInfo(&sensorInfo);
+	if (ret) {
+		LOG(MaliC55, Error) << "Failed to retrieve sensor info";
+		return;
+	}
+
+	/*
+	 * The ScalerCrop rectangle re-scaling in the ISP crop rectangle
+	 * comes straight from the RPi pipeline handler.
+	 *
+	 * Create a version of the crop rectangle aligned to the analogue crop
+	 * rectangle top-left coordinates and scaled in the [analogue crop to
+	 * output frame] ratio to take into account binning/skipping on the
+	 * sensor.
+	 */
+	Rectangle ispCrop = nativeCrop.translatedBy(-sensorInfo.analogCrop
+							       .topLeft());
+	ispCrop.scaleBy(sensorInfo.outputSize, sensorInfo.analogCrop.size());
+
+	/*
+	 * The crop rectangle should be:
+	 * 1. At least as big as ispMinCropSize_, once that's been
+	 *    enlarged to the same aspect ratio.
+	 * 2. With the same mid-point, if possible.
+	 * 3. But it can't go outside the sensor area.
+	 */
+	Rectangle ispMinCrop{ 0, 0, 640, 480 };
+	Size minSize = ispMinCrop.size().expandedToAspectRatio(nativeCrop.size());
+	Size size = ispCrop.size().expandedTo(minSize);
+	ispCrop = size.centeredTo(ispCrop.center())
+		      .enclosedIn(Rectangle(sensorInfo.outputSize));
+
+	/*
+	 * As the resizer can't upscale, the crop rectangle has to be larger
+	 * than the larger stream output size.
+	 */
+	Size maxYuvSize;
+	for (MaliC55Pipe &pipe : pipes_) {
+		if (!pipe.stream)
+			continue;
+
+		const StreamConfiguration &config = pipe.stream->configuration();
+		if (isFormatRaw(config.pixelFormat)) {
+			LOG(MaliC55, Debug) << "Cannot crop with a RAW stream";
+			return;
+		}
+
+		Size streamSize = config.size;
+		if (streamSize.width > maxYuvSize.width)
+			maxYuvSize.width = streamSize.width;
+		if (streamSize.height > maxYuvSize.height)
+			maxYuvSize.height = streamSize.height;
+	}
+
+	ispCrop.size().expandTo(maxYuvSize);
+
+	/*
+	 * Now apply the scaler crop to each enabled output. This overrides the
+	 * crop configuration performed at configure() time and can cause
+	 * square pixels if the crop rectangle and scaler output FOV ratio are
+	 * different.
+	 */
+	for (MaliC55Pipe &pipe : pipes_) {
+		if (!pipe.stream)
+			continue;
+
+		/* Create a copy to avoid setSelection() to modify ispCrop. */
+		Rectangle pipeCrop = ispCrop;
+		ret = pipe.resizer->setSelection(0, V4L2_SEL_TGT_CROP, &pipeCrop);
+		if (ret) {
+			LOG(MaliC55, Error)
+				<< "Failed to apply crop to "
+				<< (pipe.stream == &data->frStream_ ?
+				    "FR" : "DS") << " pipe";
+			return;
+		}
+	}
+}
+
 int PipelineHandlerMaliC55::queueRequestDevice(Camera *camera, Request *request)
 {
 	int ret;
@@ -930,6 +1053,15 @@  int PipelineHandlerMaliC55::queueRequestDevice(Camera *camera, Request *request)
 			return ret;
 	}
 
+	/*
+	 * Some controls need to be applied immediately, as in example,
+	 * the ScalerCrop one.
+	 *
+	 * \todo Move it buffer queue time (likely after the IPA has filled in
+	 * the parameters buffer) once we have plumbed the IPA loop in.
+	 */
+	applyScalerCrop(camera, request->controls());
+
 	return 0;
 }
 
@@ -1005,6 +1137,7 @@  bool PipelineHandlerMaliC55::registerSensorCamera(MediaLink *ispLink)
 			return false;
 
 		data->properties_ = data->sensor_->properties();
+		data->updateControls();
 
 		registerMaliCamera(std::move(data), sensor->name());
 	}