xref: /aosp_15_r20/cts/apps/CameraITS/tests/scene0/test_metadata.py (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1# Copyright 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""CameraITS test to verify metadata entries."""
15
16import logging
17import math
18
19from mobly import test_runner
20
21import its_base_test
22import camera_properties_utils
23import capture_request_utils
24import its_session_utils
25
26_HYPERFOCAL_MIN = 0.02
27_M_TO_CM = 100
28
29
30class MetadataTest(its_base_test.ItsBaseTest):
31  """Test the validity of some metadata entries.
32
33  Looks at the capture results and at the camera characteristics objects.
34  """
35
36  def test_metadata(self):
37    with its_session_utils.ItsSession(
38        device_id=self.dut.serial,
39        camera_id=self.camera_id,
40        hidden_physical_id=self.hidden_physical_id) as cam:
41      # Arbitrary capture request exposure values; image content is not
42      # important for this test, only the metadata.
43      props = cam.get_camera_properties()
44      props = cam.override_with_hidden_physical_camera_props(props)
45      camera_properties_utils.skip_unless(
46          camera_properties_utils.backward_compatible(props))
47      auto_req = capture_request_utils.auto_capture_request()
48      cap = cam.do_capture(auto_req)
49      md = cap['metadata']
50      self.failed = False
51      logging.debug('Hardware level')
52      logging.debug('Legacy: %s', camera_properties_utils.legacy(props))
53
54      logging.debug('Limited: %s', camera_properties_utils.limited(props))
55      logging.debug('Full or better: %s',
56                    camera_properties_utils.full_or_better(props))
57      logging.debug('Level 3: %s', camera_properties_utils.level3(props))
58      logging.debug('Capabilities')
59      logging.debug('Manual sensor: %s',
60                    camera_properties_utils.manual_sensor(props))
61      logging.debug('Manual post-proc: %s',
62                    camera_properties_utils.manual_post_proc(props))
63      logging.debug('Raw: %s', camera_properties_utils.raw(props))
64      logging.debug('Sensor fusion: %s',
65                    camera_properties_utils.sensor_fusion(props))
66
67      check(self, 'android.info.supportedHardwareLevel' in props,
68            'android.info.supportedHardwareLevel in props')
69      check(self, props['android.info.supportedHardwareLevel'] is not None,
70            'props[android.info.supportedHardwareLevel] is not None')
71      check(self, props['android.info.supportedHardwareLevel'] in [0, 1, 2, 3],
72            'props[android.info.supportedHardwareLevel] in [0, 1, 2, 3]')
73      manual_sensor = camera_properties_utils.manual_sensor(props)
74      # Test: rollingShutterSkew, and frameDuration tags must all be present,
75      # and rollingShutterSkew must be greater than zero and smaller than all
76      # of the possible frame durations.
77      if manual_sensor:
78        check(self, 'android.sensor.frameDuration' in md,
79              'md.has_key("android.sensor.frameDuration")')
80        check(self, md['android.sensor.frameDuration'] is not None,
81              'md["android.sensor.frameDuration"] is not None')
82        check(self, md['android.sensor.rollingShutterSkew'] > 0,
83              'md["android.sensor.rollingShutterSkew"] > 0')
84        check(self, md['android.sensor.frameDuration'] > 0,
85              'md["android.sensor.frameDuration"] > 0')
86        check(
87            self, md['android.sensor.rollingShutterSkew'] <=
88            md['android.sensor.frameDuration'],
89            ('md["android.sensor.rollingShutterSkew"] <= '
90             'md["android.sensor.frameDuration"]'))
91        logging.debug('frameDuration: %d ns',
92                      md['android.sensor.frameDuration'])
93
94      check(self, 'android.sensor.rollingShutterSkew' in md,
95            'md.has_key("android.sensor.rollingShutterSkew")')
96      check(self, md['android.sensor.rollingShutterSkew'] is not None,
97            'md["android.sensor.rollingShutterSkew"] is not None')
98      logging.debug('rollingShutterSkew: %d ns',
99                    md['android.sensor.rollingShutterSkew'])
100
101      # Test: timestampSource must be a valid value.
102      check(self, 'android.sensor.info.timestampSource' in props,
103            'props.has_key("android.sensor.info.timestampSource")')
104      check(self, props['android.sensor.info.timestampSource'] is not None,
105            'props["android.sensor.info.timestampSource"] is not None')
106      check(self, props['android.sensor.info.timestampSource'] in [0, 1],
107            'props["android.sensor.info.timestampSource"] in [0,1]')
108
109      # Test: croppingType must be a valid value, and for full devices, it
110      # must be FREEFORM=1.
111      check(self, 'android.scaler.croppingType' in props,
112            'props.has_key("android.scaler.croppingType")')
113      check(self, props['android.scaler.croppingType'] is not None,
114            'props["android.scaler.croppingType"] is not None')
115      check(self, props['android.scaler.croppingType'] in [0, 1],
116            'props["android.scaler.croppingType"] in [0,1]')
117
118      # Test: android.sensor.blackLevelPattern exists for RAW and is not None
119      if camera_properties_utils.raw(props):
120        check(self, 'android.sensor.blackLevelPattern' in props,
121              'props.has_key("android.sensor.blackLevelPattern")')
122        check(self, props['android.sensor.blackLevelPattern'] is not None,
123              'props["android.sensor.blackLevelPattern"] is not None')
124
125      if self.failed:
126        raise AssertionError('props failure. Check test_log.DEBUG.')
127
128      if not camera_properties_utils.legacy(props):
129        # Test: pixel_pitch, FOV, and hyperfocal distance are reasonable
130        fmts = props['android.scaler.streamConfigurationMap'][
131            'availableStreamConfigurations']
132        fmts = sorted(
133            fmts, key=lambda k: k['width'] * k['height'], reverse=True)
134        sensor_size = props['android.sensor.info.physicalSize']
135        pixel_pitch_h = (sensor_size['height'] / fmts[0]['height'] * 1E3)
136        pixel_pitch_w = (sensor_size['width'] / fmts[0]['width'] * 1E3)
137        logging.debug('Assert pixel_pitch WxH: %.2f um, %.2f um', pixel_pitch_w,
138                      pixel_pitch_h)
139        if (not 0.5 <= pixel_pitch_w <= 10 or
140            not 0.5 <= pixel_pitch_h <= 10 or
141            not 0.333 <= pixel_pitch_w/pixel_pitch_h <= 3.0):
142          raise AssertionError(
143              f'Pixel pitch error! w: {pixel_pitch_w}, h: {pixel_pitch_h}')
144
145        diag = math.sqrt(sensor_size['height']**2 + sensor_size['width']**2)
146        fl = md['android.lens.focalLength']
147        logging.debug('Focal length: %.3f', fl)
148        fov = 2 * math.degrees(math.atan(diag / (2 * fl)))
149        logging.debug('Assert field of view: %.1f degrees', fov)
150        if not 10 <= fov <= 130:
151          raise AssertionError(f'FoV error: {fov:.1f}')
152
153        if camera_properties_utils.lens_approx_calibrated(props):
154          diopter_hyperfocal = props['android.lens.info.hyperfocalDistance']
155          if diopter_hyperfocal != 0.0:
156            hyperfocal = 1.0 / diopter_hyperfocal
157            if _HYPERFOCAL_MIN > hyperfocal:
158              raise AssertionError('hyperfocal distance error: '
159                                   f'{hyperfocal:.2f}, MIN: {_HYPERFOCAL_MIN}')
160
161        min_focus_distance = props['android.lens.info.minimumFocusDistance']
162        if min_focus_distance:
163          min_focus_distance_cm = _M_TO_CM / min_focus_distance
164        else:
165          min_focus_distance_cm = 0
166        logging.debug('Minimum focus distance: %.3f diopters (%.2f cm)',
167                      min_focus_distance, min_focus_distance_cm
168                     )
169
170
171def check(self, expr, msg):
172  if expr:
173    logging.debug('Passed>>%s', msg)
174  else:
175    logging.debug('Failed>>%s', msg)
176    self.failed = True
177
178
179if __name__ == '__main__':
180  test_runner.main()
181