1# Copyright 2022 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"""Verifies JPEG and YUV still capture images are pixel-wise matching."""
15
16
17import cv2
18import logging
19import os.path
20from mobly import test_runner
21
22import its_base_test
23import camera_properties_utils
24import capture_request_utils
25import image_processing_utils
26import its_session_utils
27
28_MAX_IMG_SIZE = (1920, 1080)
29_NAME = os.path.splitext(os.path.basename(__file__))[0]
30_TEST_REQUIRED_MPC = 33
31_THRESHOLD_MAX_RMS_DIFF_YUV_JPEG = 0.03  # YUV/JPEG bit exactness threshold
32_THRESHOLD_MAX_RMS_DIFF_USE_CASE = 0.1  # Catch swapped color channels
33_USE_CASE_PREVIEW = 1
34_USE_CASE_STILL_CAPTURE = 2
35_USE_CASE_VIDEO_RECORD = 3
36_USE_CASE_PREVIEW_VIDEO_STILL = 4
37_USE_CASE_VIDEO_CALL = 5
38_USE_CASE_NAME_MAP = {
39    _USE_CASE_PREVIEW: 'preview',
40    _USE_CASE_STILL_CAPTURE: 'still_capture',
41    _USE_CASE_VIDEO_RECORD: 'video_record',
42    _USE_CASE_PREVIEW_VIDEO_STILL: 'preview_video_still',
43    _USE_CASE_VIDEO_CALL: 'video_call'
44}
45
46
47class YuvJpegCaptureSamenessTest(its_base_test.ItsBaseTest):
48  """Test capturing a single frame as both YUV and JPEG outputs."""
49
50  def test_yuv_jpeg_capture_sameness(self):
51    logging.debug('Starting %s', _NAME)
52    with its_session_utils.ItsSession(
53        device_id=self.dut.serial,
54        camera_id=self.camera_id,
55        hidden_physical_id=self.hidden_physical_id) as cam:
56      props = cam.get_camera_properties()
57      props = cam.override_with_hidden_physical_camera_props(props)
58      log_path = self.log_path
59
60      # check media performance class
61      should_run = camera_properties_utils.stream_use_case(props)
62      media_performance_class = its_session_utils.get_media_performance_class(
63          self.dut.serial)
64      if media_performance_class >= _TEST_REQUIRED_MPC and not should_run:
65        its_session_utils.raise_mpc_assertion_error(
66            _TEST_REQUIRED_MPC, _NAME, media_performance_class)
67
68      # check SKIP conditions
69      camera_properties_utils.skip_unless(should_run)
70
71      # Load chart for scene
72      its_session_utils.load_scene(
73          cam, props, self.scene, self.tablet, self.chart_distance)
74
75      # Find the maximum mandatory size supported by all use cases
76      display_size = cam.get_display_size()
77      max_camcorder_profile_size = cam.get_max_camcorder_profile_size(
78          self.camera_id)
79      size_bound = min([_MAX_IMG_SIZE, display_size,
80                        max_camcorder_profile_size],
81                       key=lambda t: int(t[0])*int(t[1]))
82
83      logging.debug('display_size %s, max_camcorder_profile_size %s, '
84                    'size_bound %s', display_size, max_camcorder_profile_size,
85                    size_bound)
86      first_api_level = its_session_utils.get_first_api_level(self.dut.serial)
87      w, h = capture_request_utils.get_available_output_sizes(
88          'yuv', props, max_size=size_bound)[0]
89      jpeg_sizes = capture_request_utils.get_available_output_sizes(
90          'jpeg', props, match_ar_size=(w, h))
91
92      should_skip = not jpeg_sizes
93      # skip since no jpeg size with the same aspect ratio as YUV was found
94      skip_msg = 'same jpeg and yuv aspect ratio not found'
95      camera_properties_utils.skip_unless(not should_skip, skip_msg)
96
97      jpeg_w, jpeg_h = w, h
98      same_jpeg_and_yuv_available = (w, h) in jpeg_sizes
99      # no jpeg size found, which is the same as YUV
100      skip_msg = ('same jpeg + yuv sizes not available within threshold '
101                  f'first_api_level {first_api_level}')
102      should_skip = (
103          first_api_level < its_session_utils.ANDROID15_API_LEVEL and
104          not same_jpeg_and_yuv_available)
105      camera_properties_utils.skip_unless(not should_skip, skip_msg)
106      if not same_jpeg_and_yuv_available:
107        # Get the first size with the same AR as YUV
108        jpeg_w, jpeg_h = jpeg_sizes[0]
109
110      # Create requests
111      fmt_yuv = {'format': 'yuv', 'width': w, 'height': h,
112                 'useCase': _USE_CASE_STILL_CAPTURE}
113      fmt_jpg = {'format': 'jpeg', 'width': jpeg_w, 'height': jpeg_h,
114                 'useCase': _USE_CASE_STILL_CAPTURE}
115      logging.debug(
116          'YUV width: %d, height: %d, JPEG width %d height %d',
117          w, h, jpeg_w, jpeg_h)
118
119      cam.do_3a()
120      req = capture_request_utils.auto_capture_request()
121      req['android.jpeg.quality'] = 100
122
123      cap_yuv, cap_jpg = cam.do_capture(req, [fmt_yuv, fmt_jpg])
124      rgb_yuv = image_processing_utils.convert_capture_to_rgb_image(
125          cap_yuv, True)
126      file_stem = os.path.join(log_path, _NAME)
127      image_processing_utils.write_image(rgb_yuv, f'{file_stem}_yuv.jpg')
128      rgb_jpg = image_processing_utils.convert_capture_to_rgb_image(
129          cap_jpg, True)
130      image_processing_utils.write_image(rgb_jpg, f'{file_stem}_jpg.jpg')
131
132      if jpeg_w != w:
133        scale_factor = w / jpeg_w
134        rgb_jpg = cv2.resize(
135            rgb_jpg, None, fx=scale_factor, fy=scale_factor)
136        image_processing_utils.write_image(
137            rgb_jpg, f'{file_stem}_jpg_downscaled.jpg')
138
139      rms_diff = image_processing_utils.compute_image_rms_difference_3d(
140          rgb_yuv, rgb_jpg)
141      msg = f'RMS diff: {rms_diff:.4f}'
142      logging.debug('%s', msg)
143      if rms_diff >= _THRESHOLD_MAX_RMS_DIFF_YUV_JPEG:
144        raise AssertionError(f'{msg}, ATOL: {_THRESHOLD_MAX_RMS_DIFF_YUV_JPEG}')
145
146      # Create requests for all use cases, and make sure they are at least
147      # similar enough with the STILL_CAPTURE YUV. For example, the color
148      # channels must be valid.
149      num_tests = 0
150      num_fail = 0
151      for use_case in _USE_CASE_NAME_MAP:
152        num_tests += 1
153        cam.do_3a()
154        fmt_yuv_use_case = {'format': 'yuv', 'width': w, 'height': h,
155                            'useCase': use_case}
156        cap_yuv_use_case = cam.do_capture(req, [fmt_yuv_use_case])
157        rgb_yuv_use_case = image_processing_utils.convert_capture_to_rgb_image(
158            cap_yuv_use_case, True)
159        use_case_name = _USE_CASE_NAME_MAP[use_case]
160        image_processing_utils.write_image(
161            rgb_yuv_use_case, f'{file_stem}_yuv_{use_case_name}.jpg')
162        rms_diff = image_processing_utils.compute_image_rms_difference_3d(
163            rgb_yuv, rgb_yuv_use_case)
164        msg = (f'RMS diff for single {use_case_name} use case & still capture '
165               f'YUV: {rms_diff:.4f}')
166        logging.debug('%s', msg)
167        if rms_diff >= _THRESHOLD_MAX_RMS_DIFF_USE_CASE:
168          logging.error(msg + f', ATOL: {_THRESHOLD_MAX_RMS_DIFF_USE_CASE}')
169          num_fail += 1
170
171      if num_fail > 0:
172        raise AssertionError(f'Number of fails: {num_fail} / {num_tests}')
173
174if __name__ == '__main__':
175  test_runner.main()
176