blob: 4e98bf6c5914b82471451732e417625dcee8eead [file] [log] [blame]
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-ZoomRatioMapper"
18//#define LOG_NDEBUG 0
19
20#include <algorithm>
21
22#include "device3/ZoomRatioMapper.h"
23
24namespace android {
25
26namespace camera3 {
27
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -080028
29status_t ZoomRatioMapper::initZoomRatioInTemplate(CameraMetadata *request) {
30 camera_metadata_entry_t entry;
31 entry = request->find(ANDROID_CONTROL_ZOOM_RATIO);
32 float defaultZoomRatio = 1.0f;
33 if (entry.count == 0) {
34 return request->update(ANDROID_CONTROL_ZOOM_RATIO, &defaultZoomRatio, 1);
35 }
36 return OK;
37}
38
39status_t ZoomRatioMapper::overrideZoomRatioTags(
40 CameraMetadata* deviceInfo, bool* supportNativeZoomRatio) {
41 if (deviceInfo == nullptr || supportNativeZoomRatio == nullptr) {
42 return BAD_VALUE;
43 }
44
45 camera_metadata_entry_t entry;
46 entry = deviceInfo->find(ANDROID_CONTROL_ZOOM_RATIO_RANGE);
47 if (entry.count != 2 && entry.count != 0) return BAD_VALUE;
48
49 // Hal has zoom ratio support
50 if (entry.count == 2) {
51 *supportNativeZoomRatio = true;
52 return OK;
53 }
54
55 // Hal has no zoom ratio support
56 *supportNativeZoomRatio = false;
57
58 entry = deviceInfo->find(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
59 if (entry.count != 1) {
60 ALOGI("%s: Camera device doesn't support SCALER_AVAILABLE_MAX_DIGITAL_ZOOM key!",
61 __FUNCTION__);
62 return OK;
63 }
64
65 float zoomRange[] = {1.0f, entry.data.f[0]};
66 status_t res = deviceInfo->update(ANDROID_CONTROL_ZOOM_RATIO_RANGE, zoomRange, 2);
67 if (res != OK) {
68 ALOGE("%s: Failed to update CONTROL_ZOOM_RATIO_RANGE key: %s (%d)",
69 __FUNCTION__, strerror(-res), res);
70 return res;
71 }
72
73 std::vector<int32_t> requestKeys;
74 entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS);
75 if (entry.count > 0) {
76 requestKeys.insert(requestKeys.end(), entry.data.i32, entry.data.i32 + entry.count);
77 }
78 requestKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO);
79 res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS,
80 requestKeys.data(), requestKeys.size());
81 if (res != OK) {
82 ALOGE("%s: Failed to update REQUEST_AVAILABLE_REQUEST_KEYS: %s (%d)",
83 __FUNCTION__, strerror(-res), res);
84 return res;
85 }
86
87 std::vector<int32_t> resultKeys;
88 entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS);
89 if (entry.count > 0) {
90 resultKeys.insert(resultKeys.end(), entry.data.i32, entry.data.i32 + entry.count);
91 }
92 resultKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO);
93 res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS,
94 resultKeys.data(), resultKeys.size());
95 if (res != OK) {
96 ALOGE("%s: Failed to update REQUEST_AVAILABLE_RESULT_KEYS: %s (%d)",
97 __FUNCTION__, strerror(-res), res);
98 return res;
99 }
100
101 std::vector<int32_t> charKeys;
102 entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
103 if (entry.count > 0) {
104 charKeys.insert(charKeys.end(), entry.data.i32, entry.data.i32 + entry.count);
105 }
106 charKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO_RANGE);
107 res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
108 charKeys.data(), charKeys.size());
109 if (res != OK) {
110 ALOGE("%s: Failed to update REQUEST_AVAILABLE_CHARACTERISTICS_KEYS: %s (%d)",
111 __FUNCTION__, strerror(-res), res);
112 return res;
113 }
114
115 return OK;
116}
117
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800118ZoomRatioMapper::ZoomRatioMapper(const CameraMetadata* deviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800119 bool supportNativeZoomRatio, bool usePrecorrectArray) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800120 camera_metadata_ro_entry_t entry;
121
122 entry = deviceInfo->find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800123 if (entry.count != 4) return;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800124 int32_t arrayW = entry.data.i32[2];
125 int32_t arrayH = entry.data.i32[3];
126
127 entry = deviceInfo->find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800128 if (entry.count != 4) return;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800129 int32_t activeW = entry.data.i32[2];
130 int32_t activeH = entry.data.i32[3];
131
132 if (usePrecorrectArray) {
133 mArrayWidth = arrayW;
134 mArrayHeight = arrayH;
135 } else {
136 mArrayWidth = activeW;
137 mArrayHeight = activeH;
138 }
139 mHalSupportsZoomRatio = supportNativeZoomRatio;
140
141 ALOGV("%s: array size: %d x %d, mHalSupportsZoomRatio %d",
142 __FUNCTION__, mArrayWidth, mArrayHeight, mHalSupportsZoomRatio);
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800143 mIsValid = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800144}
145
146status_t ZoomRatioMapper::updateCaptureRequest(CameraMetadata* request) {
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800147 if (!mIsValid) return INVALID_OPERATION;
148
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800149 status_t res = OK;
150 bool zoomRatioIs1 = true;
151 camera_metadata_entry_t entry;
152
153 entry = request->find(ANDROID_CONTROL_ZOOM_RATIO);
154 if (entry.count == 1 && entry.data.f[0] != 1.0f) {
155 zoomRatioIs1 = false;
Shuzhen Wangc2dae592020-11-30 20:16:06 -0800156
157 // If cropRegion is windowboxing, override it with activeArray
158 camera_metadata_entry_t cropRegionEntry = request->find(ANDROID_SCALER_CROP_REGION);
159 if (cropRegionEntry.count == 4) {
160 int cropWidth = cropRegionEntry.data.i32[2];
161 int cropHeight = cropRegionEntry.data.i32[3];
162 if (cropWidth < mArrayWidth && cropHeight < mArrayHeight) {
163 cropRegionEntry.data.i32[0] = 0;
164 cropRegionEntry.data.i32[1] = 0;
165 cropRegionEntry.data.i32[2] = mArrayWidth;
166 cropRegionEntry.data.i32[3] = mArrayHeight;
167 }
168 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800169 }
170
171 if (mHalSupportsZoomRatio && zoomRatioIs1) {
172 res = separateZoomFromCropLocked(request, false/*isResult*/);
173 } else if (!mHalSupportsZoomRatio && !zoomRatioIs1) {
174 res = combineZoomAndCropLocked(request, false/*isResult*/);
175 }
176
177 // If CONTROL_ZOOM_RATIO is in request, but HAL doesn't support
178 // CONTROL_ZOOM_RATIO, remove it from the request.
179 if (!mHalSupportsZoomRatio && entry.count == 1) {
180 request->erase(ANDROID_CONTROL_ZOOM_RATIO);
181 }
182
183 return res;
184}
185
186status_t ZoomRatioMapper::updateCaptureResult(CameraMetadata* result, bool requestedZoomRatioIs1) {
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800187 if (!mIsValid) return INVALID_OPERATION;
188
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800189 status_t res = OK;
190
191 if (mHalSupportsZoomRatio && requestedZoomRatioIs1) {
192 res = combineZoomAndCropLocked(result, true/*isResult*/);
193 } else if (!mHalSupportsZoomRatio && !requestedZoomRatioIs1) {
194 res = separateZoomFromCropLocked(result, true/*isResult*/);
195 } else {
196 camera_metadata_entry_t entry = result->find(ANDROID_CONTROL_ZOOM_RATIO);
197 if (entry.count == 0) {
198 float zoomRatio1x = 1.0f;
199 result->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio1x, 1);
200 }
201 }
202
203 return res;
204}
205
206float ZoomRatioMapper::deriveZoomRatio(const CameraMetadata* metadata) {
207 float zoomRatio = 1.0;
208
209 camera_metadata_ro_entry_t entry;
210 entry = metadata->find(ANDROID_SCALER_CROP_REGION);
211 if (entry.count != 4) return zoomRatio;
212
213 // Center of the preCorrection/active size
214 float arrayCenterX = mArrayWidth / 2.0;
215 float arrayCenterY = mArrayHeight / 2.0;
216
217 // Re-map crop region to coordinate system centered to (arrayCenterX,
218 // arrayCenterY).
219 float cropRegionLeft = arrayCenterX - entry.data.i32[0] ;
220 float cropRegionTop = arrayCenterY - entry.data.i32[1];
221 float cropRegionRight = entry.data.i32[0] + entry.data.i32[2] - arrayCenterX;
222 float cropRegionBottom = entry.data.i32[1] + entry.data.i32[3] - arrayCenterY;
223
224 // Calculate the scaling factor for left, top, bottom, right
225 float zoomRatioLeft = std::max(mArrayWidth / (2 * cropRegionLeft), 1.0f);
226 float zoomRatioTop = std::max(mArrayHeight / (2 * cropRegionTop), 1.0f);
227 float zoomRatioRight = std::max(mArrayWidth / (2 * cropRegionRight), 1.0f);
228 float zoomRatioBottom = std::max(mArrayHeight / (2 * cropRegionBottom), 1.0f);
229
230 // Use minimum scaling factor to handle letterboxing or pillarboxing
231 zoomRatio = std::min(std::min(zoomRatioLeft, zoomRatioRight),
232 std::min(zoomRatioTop, zoomRatioBottom));
233
234 ALOGV("%s: derived zoomRatio is %f", __FUNCTION__, zoomRatio);
235 return zoomRatio;
236}
237
238status_t ZoomRatioMapper::separateZoomFromCropLocked(CameraMetadata* metadata, bool isResult) {
239 status_t res;
240 float zoomRatio = deriveZoomRatio(metadata);
241
242 // Update zoomRatio metadata tag
243 res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
244 if (res != OK) {
245 ALOGE("%s: Failed to update ANDROID_CONTROL_ZOOM_RATIO: %s(%d)",
246 __FUNCTION__, strerror(-res), res);
247 return res;
248 }
249
250 // Scale regions using zoomRatio
251 camera_metadata_entry_t entry;
252 for (auto region : kMeteringRegionsToCorrect) {
253 entry = metadata->find(region);
254 for (size_t j = 0; j < entry.count; j += 5) {
255 int32_t weight = entry.data.i32[j + 4];
256 if (weight == 0) {
257 continue;
258 }
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700259 // Top left (inclusive)
260 scaleCoordinates(entry.data.i32 + j, 1, zoomRatio, true /*clamp*/);
261 // Bottom right (exclusive): Use adjacent inclusive pixel to
262 // calculate.
263 entry.data.i32[j+2] -= 1;
264 entry.data.i32[j+3] -= 1;
265 scaleCoordinates(entry.data.i32 + j + 2, 1, zoomRatio, true /*clamp*/);
266 entry.data.i32[j+2] += 1;
267 entry.data.i32[j+3] += 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800268 }
269 }
270
271 for (auto rect : kRectsToCorrect) {
272 entry = metadata->find(rect);
273 scaleRects(entry.data.i32, entry.count / 4, zoomRatio);
274 }
275
276 if (isResult) {
277 for (auto pts : kResultPointsToCorrectNoClamp) {
278 entry = metadata->find(pts);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700279 scaleCoordinates(entry.data.i32, entry.count / 2, zoomRatio, false /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800280 }
281 }
282
283 return OK;
284}
285
286status_t ZoomRatioMapper::combineZoomAndCropLocked(CameraMetadata* metadata, bool isResult) {
287 float zoomRatio = 1.0f;
288 camera_metadata_entry_t entry;
289 entry = metadata->find(ANDROID_CONTROL_ZOOM_RATIO);
290 if (entry.count == 1) {
291 zoomRatio = entry.data.f[0];
292 }
293
294 // Unscale regions with zoomRatio
295 status_t res;
296 for (auto region : kMeteringRegionsToCorrect) {
297 entry = metadata->find(region);
298 for (size_t j = 0; j < entry.count; j += 5) {
299 int32_t weight = entry.data.i32[j + 4];
300 if (weight == 0) {
301 continue;
302 }
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700303 // Top-left (inclusive)
304 scaleCoordinates(entry.data.i32 + j, 1, 1.0 / zoomRatio, true /*clamp*/);
305 // Bottom-right (exclusive): Use adjacent inclusive pixel to
306 // calculate.
307 entry.data.i32[j+2] -= 1;
308 entry.data.i32[j+3] -= 1;
309 scaleCoordinates(entry.data.i32 + j + 2, 1, 1.0 / zoomRatio, true /*clamp*/);
310 entry.data.i32[j+2] += 1;
311 entry.data.i32[j+3] += 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800312 }
313 }
314 for (auto rect : kRectsToCorrect) {
315 entry = metadata->find(rect);
316 scaleRects(entry.data.i32, entry.count / 4, 1.0 / zoomRatio);
317 }
318 if (isResult) {
319 for (auto pts : kResultPointsToCorrectNoClamp) {
320 entry = metadata->find(pts);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700321 scaleCoordinates(entry.data.i32, entry.count / 2, 1.0 / zoomRatio, false /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800322 }
323 }
324
325 zoomRatio = 1.0;
326 res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
327 if (res != OK) {
328 return res;
329 }
330
331 return OK;
332}
333
334void ZoomRatioMapper::scaleCoordinates(int32_t* coordPairs, int coordCount,
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700335 float scaleRatio, bool clamp) {
336 // A pixel's coordinate is represented by the position of its top-left corner.
337 // To avoid the rounding error, we use the coordinate for the center of the
338 // pixel instead:
339 // 1. First shift the coordinate system half pixel both horizontally and
340 // vertically, so that [x, y] is the center of the pixel, not the top-left corner.
341 // 2. Do zoom operation to scale the coordinate relative to the center of
342 // the active array (shifted by 0.5 pixel as well).
343 // 3. Shift the coordinate system back by directly using the pixel center
344 // coordinate.
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800345 for (int i = 0; i < coordCount * 2; i += 2) {
346 float x = coordPairs[i];
347 float y = coordPairs[i + 1];
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700348 float xCentered = x - (mArrayWidth - 2) / 2;
349 float yCentered = y - (mArrayHeight - 2) / 2;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800350 float scaledX = xCentered * scaleRatio;
351 float scaledY = yCentered * scaleRatio;
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700352 scaledX += (mArrayWidth - 2) / 2;
353 scaledY += (mArrayHeight - 2) / 2;
354 coordPairs[i] = static_cast<int32_t>(std::round(scaledX));
355 coordPairs[i+1] = static_cast<int32_t>(std::round(scaledY));
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800356 // Clamp to within activeArray/preCorrectionActiveArray
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700357 if (clamp) {
358 int32_t right = mArrayWidth - 1;
359 int32_t bottom = mArrayHeight - 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800360 coordPairs[i] =
361 std::min(right, std::max(0, coordPairs[i]));
362 coordPairs[i+1] =
363 std::min(bottom, std::max(0, coordPairs[i+1]));
364 }
365 ALOGV("%s: coordinates: %d, %d", __FUNCTION__, coordPairs[i], coordPairs[i+1]);
366 }
367}
368
369void ZoomRatioMapper::scaleRects(int32_t* rects, int rectCount,
370 float scaleRatio) {
371 for (int i = 0; i < rectCount * 4; i += 4) {
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700372 // Map from (l, t, width, height) to (l, t, l+width-1, t+height-1),
373 // where both top-left and bottom-right are inclusive.
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800374 int32_t coords[4] = {
375 rects[i],
376 rects[i + 1],
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700377 rects[i] + rects[i + 2] - 1,
378 rects[i + 1] + rects[i + 3] - 1
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800379 };
380
381 // top-left
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700382 scaleCoordinates(coords, 1, scaleRatio, true /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800383 // bottom-right
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700384 scaleCoordinates(coords+2, 1, scaleRatio, true /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800385
386 // Map back to (l, t, width, height)
387 rects[i] = coords[0];
388 rects[i + 1] = coords[1];
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700389 rects[i + 2] = coords[2] - coords[0] + 1;
390 rects[i + 3] = coords[3] - coords[1] + 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800391 }
392}
393
394} // namespace camera3
395
396} // namespace android