Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalerCrops handling #1087

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion picamera2/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def align(self, optimal=True):


class StreamConfiguration(Configuration):
_ALLOWED_FIELDS = ("size", "format", "stride", "framesize")
_ALLOWED_FIELDS = ("size", "format", "stride", "framesize", "preserve_ar")
_FIELD_CLASS_MAP = {}
_FORWARD_FIELDS = {}

Expand Down
23 changes: 16 additions & 7 deletions picamera2/picamera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def _make_initial_stream_config(stream_config: dict, updates: dict, ignore_list=
"""
if updates is None:
return None
valid = ("format", "size", "stride")
valid = ("format", "size", "stride", "preserve_ar")
for key, value in updates.items():
if isinstance(value, SensorFormat):
value = str(value)
Expand Down Expand Up @@ -687,9 +687,9 @@ def create_preview_configuration(self, main={}, lores=None, raw={}, transform=li
if not self._is_rpi_camera():
raw = None
sensor = None
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (640, 480)}, main)
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (640, 480), "preserve_ar": True}, main)
self.align_stream(main, optimal=False)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"]}, lores)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"], "preserve_ar": False}, lores)
if lores is not None:
self.align_stream(lores, optimal=False)
raw = self._make_initial_stream_config({"format": self.sensor_format, "size": main["size"]},
Expand Down Expand Up @@ -721,9 +721,9 @@ def create_still_configuration(self, main={}, lores=None, raw={}, transform=libc
if not self._is_rpi_camera():
raw = None
sensor = None
main = self._make_initial_stream_config({"format": "BGR888", "size": self.sensor_resolution}, main)
main = self._make_initial_stream_config({"format": "BGR888", "size": self.sensor_resolution, "preserve_ar": True}, main)
self.align_stream(main, optimal=False)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"]}, lores)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"], "preserve_ar": False}, lores)
if lores is not None:
self.align_stream(lores, optimal=False)
raw = self._make_initial_stream_config({"format": self.sensor_format, "size": main["size"]},
Expand Down Expand Up @@ -755,9 +755,9 @@ def create_video_configuration(self, main={}, lores=None, raw={}, transform=libc
if not self._is_rpi_camera():
raw = None
sensor = None
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (1280, 720)}, main)
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (1280, 720), "preserve_ar": True}, main)
self.align_stream(main, optimal=False)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"]}, lores)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"], "preserve_ar": False}, lores)
if lores is not None:
self.align_stream(lores, optimal=False)
raw = self._make_initial_stream_config({"format": self.sensor_format, "size": main["size"]},
Expand Down Expand Up @@ -1116,6 +1116,15 @@ def configure_(self, camera_config="preview") -> None:
self.controls = Controls(self, controls=self.camera_config['controls'])
self.configure_count += 1

if "ScalerCrops" in self.camera_controls:
par_crop = self.camera_controls["ScalerCrops"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'm guessing par_crop means preserve_ar_crop? Actually, for my curiosity, how does this work? Presumably libcamera is calculating the AR-preserving crop for each of the cameras - except I thought asking for camera controls was supposed to give us min/max/default triple. So I think I'm a bit confused!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably libcamera is calculating the AR-preserving crop for each of the cameras - except I thought asking for camera controls was supposed to give us min/max/default triple. So I think I'm a bit confused!

This is a bit of a hack where the controlinfo for rpi::ScalerCrops actually gives us the default (i.e. preserved ar) crop for output 0 and output 1 in the min/max fields. Unfortunately there is no other mechanism that can be used to get this out for span controls.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yes, that's a bit sneaky! Maybe worth a comment as it was a bit head-scratchy?

full_fov = self.camera_controls["ScalerCrop"][1]
scaler_crops = [par_crop[0] if camera_config["main"]["preserve_ar"] else full_fov]
if self.lores_index >= 0:
scaler_crops.append(par_crop[1] if camera_config["lores"]["preserve_ar"] else scaler_crops[0])
self.set_controls({"ScalerCrops": scaler_crops})


def configure(self, camera_config="preview") -> None:
"""Configure the camera system with the given configuration."""
self.configure_(camera_config)
Expand Down