1 /*
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 
24 namespace android {
25 
26 namespace camera3 {
27 
28 
initZoomRatioInTemplate(CameraMetadata * request)29 status_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 
overrideZoomRatioTags(CameraMetadata * deviceInfo,bool * supportNativeZoomRatio)39 status_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 
ZoomRatioMapper(const CameraMetadata * deviceInfo,bool supportNativeZoomRatio,bool usePrecorrectArray)118 ZoomRatioMapper::ZoomRatioMapper(const CameraMetadata* deviceInfo,
119         bool supportNativeZoomRatio, bool usePrecorrectArray) {
120     camera_metadata_ro_entry_t entry;
121 
122     entry = deviceInfo->find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
123     if (entry.count != 4) return;
124     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);
128     if (entry.count != 4) return;
129     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);
143     mIsValid = true;
144 }
145 
updateCaptureRequest(CameraMetadata * request)146 status_t ZoomRatioMapper::updateCaptureRequest(CameraMetadata* request) {
147     if (!mIsValid) return INVALID_OPERATION;
148 
149     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;
156     }
157 
158     if (mHalSupportsZoomRatio && zoomRatioIs1) {
159         res = separateZoomFromCropLocked(request, false/*isResult*/);
160     } else if (!mHalSupportsZoomRatio && !zoomRatioIs1) {
161         res = combineZoomAndCropLocked(request, false/*isResult*/);
162     }
163 
164     // If CONTROL_ZOOM_RATIO is in request, but HAL doesn't support
165     // CONTROL_ZOOM_RATIO, remove it from the request.
166     if (!mHalSupportsZoomRatio && entry.count == 1) {
167         request->erase(ANDROID_CONTROL_ZOOM_RATIO);
168     }
169 
170     return res;
171 }
172 
updateCaptureResult(CameraMetadata * result,bool requestedZoomRatioIs1)173 status_t ZoomRatioMapper::updateCaptureResult(CameraMetadata* result, bool requestedZoomRatioIs1) {
174     if (!mIsValid) return INVALID_OPERATION;
175 
176     status_t res = OK;
177 
178     if (mHalSupportsZoomRatio && requestedZoomRatioIs1) {
179         res = combineZoomAndCropLocked(result, true/*isResult*/);
180     } else if (!mHalSupportsZoomRatio && !requestedZoomRatioIs1) {
181         res = separateZoomFromCropLocked(result, true/*isResult*/);
182     } else {
183         camera_metadata_entry_t entry = result->find(ANDROID_CONTROL_ZOOM_RATIO);
184         if (entry.count == 0) {
185             float zoomRatio1x = 1.0f;
186             result->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio1x, 1);
187         }
188     }
189 
190     return res;
191 }
192 
deriveZoomRatio(const CameraMetadata * metadata)193 float ZoomRatioMapper::deriveZoomRatio(const CameraMetadata* metadata) {
194     float zoomRatio = 1.0;
195 
196     camera_metadata_ro_entry_t entry;
197     entry = metadata->find(ANDROID_SCALER_CROP_REGION);
198     if (entry.count != 4) return zoomRatio;
199 
200     // Center of the preCorrection/active size
201     float arrayCenterX = mArrayWidth / 2.0;
202     float arrayCenterY = mArrayHeight / 2.0;
203 
204     // Re-map crop region to coordinate system centered to (arrayCenterX,
205     // arrayCenterY).
206     float cropRegionLeft = arrayCenterX - entry.data.i32[0] ;
207     float cropRegionTop = arrayCenterY - entry.data.i32[1];
208     float cropRegionRight = entry.data.i32[0] + entry.data.i32[2] - arrayCenterX;
209     float cropRegionBottom = entry.data.i32[1] + entry.data.i32[3] - arrayCenterY;
210 
211     // Calculate the scaling factor for left, top, bottom, right
212     float zoomRatioLeft = std::max(mArrayWidth / (2 * cropRegionLeft), 1.0f);
213     float zoomRatioTop = std::max(mArrayHeight / (2 * cropRegionTop), 1.0f);
214     float zoomRatioRight = std::max(mArrayWidth / (2 * cropRegionRight), 1.0f);
215     float zoomRatioBottom = std::max(mArrayHeight / (2 * cropRegionBottom), 1.0f);
216 
217     // Use minimum scaling factor to handle letterboxing or pillarboxing
218     zoomRatio = std::min(std::min(zoomRatioLeft, zoomRatioRight),
219             std::min(zoomRatioTop, zoomRatioBottom));
220 
221     ALOGV("%s: derived zoomRatio is %f", __FUNCTION__, zoomRatio);
222     return zoomRatio;
223 }
224 
separateZoomFromCropLocked(CameraMetadata * metadata,bool isResult)225 status_t ZoomRatioMapper::separateZoomFromCropLocked(CameraMetadata* metadata, bool isResult) {
226     status_t res;
227     float zoomRatio = deriveZoomRatio(metadata);
228 
229     // Update zoomRatio metadata tag
230     res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
231     if (res != OK) {
232         ALOGE("%s: Failed to update ANDROID_CONTROL_ZOOM_RATIO: %s(%d)",
233                 __FUNCTION__, strerror(-res), res);
234         return res;
235     }
236 
237     // Scale regions using zoomRatio
238     camera_metadata_entry_t entry;
239     for (auto region : kMeteringRegionsToCorrect) {
240         entry = metadata->find(region);
241         for (size_t j = 0; j < entry.count; j += 5) {
242             int32_t weight = entry.data.i32[j + 4];
243             if (weight == 0) {
244                 continue;
245             }
246             // Top left (inclusive)
247             scaleCoordinates(entry.data.i32 + j, 1, zoomRatio, true /*clamp*/);
248             // Bottom right (exclusive): Use adjacent inclusive pixel to
249             // calculate.
250             entry.data.i32[j+2] -= 1;
251             entry.data.i32[j+3] -= 1;
252             scaleCoordinates(entry.data.i32 + j + 2, 1, zoomRatio, true /*clamp*/);
253             entry.data.i32[j+2] += 1;
254             entry.data.i32[j+3] += 1;
255         }
256     }
257 
258     for (auto rect : kRectsToCorrect) {
259         entry = metadata->find(rect);
260         scaleRects(entry.data.i32, entry.count / 4, zoomRatio);
261     }
262 
263     if (isResult) {
264         for (auto pts : kResultPointsToCorrectNoClamp) {
265             entry = metadata->find(pts);
266             scaleCoordinates(entry.data.i32, entry.count / 2, zoomRatio, false /*clamp*/);
267         }
268     }
269 
270     return OK;
271 }
272 
combineZoomAndCropLocked(CameraMetadata * metadata,bool isResult)273 status_t ZoomRatioMapper::combineZoomAndCropLocked(CameraMetadata* metadata, bool isResult) {
274     float zoomRatio = 1.0f;
275     camera_metadata_entry_t entry;
276     entry = metadata->find(ANDROID_CONTROL_ZOOM_RATIO);
277     if (entry.count == 1) {
278         zoomRatio = entry.data.f[0];
279     }
280 
281     // Unscale regions with zoomRatio
282     status_t res;
283     for (auto region : kMeteringRegionsToCorrect) {
284         entry = metadata->find(region);
285         for (size_t j = 0; j < entry.count; j += 5) {
286             int32_t weight = entry.data.i32[j + 4];
287             if (weight == 0) {
288                 continue;
289             }
290             // Top-left (inclusive)
291             scaleCoordinates(entry.data.i32 + j, 1, 1.0 / zoomRatio, true /*clamp*/);
292             // Bottom-right (exclusive): Use adjacent inclusive pixel to
293             // calculate.
294             entry.data.i32[j+2] -= 1;
295             entry.data.i32[j+3] -= 1;
296             scaleCoordinates(entry.data.i32 + j + 2, 1, 1.0 / zoomRatio, true /*clamp*/);
297             entry.data.i32[j+2] += 1;
298             entry.data.i32[j+3] += 1;
299         }
300     }
301     for (auto rect : kRectsToCorrect) {
302         entry = metadata->find(rect);
303         scaleRects(entry.data.i32, entry.count / 4, 1.0 / zoomRatio);
304     }
305     if (isResult) {
306         for (auto pts : kResultPointsToCorrectNoClamp) {
307             entry = metadata->find(pts);
308             scaleCoordinates(entry.data.i32, entry.count / 2, 1.0 / zoomRatio, false /*clamp*/);
309         }
310     }
311 
312     zoomRatio = 1.0;
313     res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
314     if (res != OK) {
315         return res;
316     }
317 
318     return OK;
319 }
320 
scaleCoordinates(int32_t * coordPairs,int coordCount,float scaleRatio,bool clamp)321 void ZoomRatioMapper::scaleCoordinates(int32_t* coordPairs, int coordCount,
322         float scaleRatio, bool clamp) {
323     // A pixel's coordinate is represented by the position of its top-left corner.
324     // To avoid the rounding error, we use the coordinate for the center of the
325     // pixel instead:
326     // 1. First shift the coordinate system half pixel both horizontally and
327     // vertically, so that [x, y] is the center of the pixel, not the top-left corner.
328     // 2. Do zoom operation to scale the coordinate relative to the center of
329     // the active array (shifted by 0.5 pixel as well).
330     // 3. Shift the coordinate system back by directly using the pixel center
331     // coordinate.
332     for (int i = 0; i < coordCount * 2; i += 2) {
333         float x = coordPairs[i];
334         float y = coordPairs[i + 1];
335         float xCentered = x - (mArrayWidth - 2) / 2;
336         float yCentered = y - (mArrayHeight - 2) / 2;
337         float scaledX = xCentered * scaleRatio;
338         float scaledY = yCentered * scaleRatio;
339         scaledX += (mArrayWidth - 2) / 2;
340         scaledY += (mArrayHeight - 2) / 2;
341         coordPairs[i] = static_cast<int32_t>(std::round(scaledX));
342         coordPairs[i+1] = static_cast<int32_t>(std::round(scaledY));
343         // Clamp to within activeArray/preCorrectionActiveArray
344         if (clamp) {
345             int32_t right = mArrayWidth - 1;
346             int32_t bottom = mArrayHeight - 1;
347             coordPairs[i] =
348                     std::min(right, std::max(0, coordPairs[i]));
349             coordPairs[i+1] =
350                     std::min(bottom, std::max(0, coordPairs[i+1]));
351         }
352         ALOGV("%s: coordinates: %d, %d", __FUNCTION__, coordPairs[i], coordPairs[i+1]);
353     }
354 }
355 
scaleRects(int32_t * rects,int rectCount,float scaleRatio)356 void ZoomRatioMapper::scaleRects(int32_t* rects, int rectCount,
357         float scaleRatio) {
358     for (int i = 0; i < rectCount * 4; i += 4) {
359         // Map from (l, t, width, height) to (l, t, l+width-1, t+height-1),
360         // where both top-left and bottom-right are inclusive.
361         int32_t coords[4] = {
362             rects[i],
363             rects[i + 1],
364             rects[i] + rects[i + 2] - 1,
365             rects[i + 1] + rects[i + 3] - 1
366         };
367 
368         // top-left
369         scaleCoordinates(coords, 1, scaleRatio, true /*clamp*/);
370         // bottom-right
371         scaleCoordinates(coords+2, 1, scaleRatio, true /*clamp*/);
372 
373         // Map back to (l, t, width, height)
374         rects[i] = coords[0];
375         rects[i + 1] = coords[1];
376         rects[i + 2] = coords[2] - coords[0] + 1;
377         rects[i + 3] = coords[3] - coords[1] + 1;
378     }
379 }
380 
381 } // namespace camera3
382 
383 } // namespace android
384