Merge "Camera: Prune depth samples with low confidence" into qt-dev am: eb99a27ffa
am: 1b10af809c

Change-Id: I35469c6903d2bbc8a6a49eff723d4f44fbbfd779
diff --git a/services/camera/libcameraservice/common/DepthPhotoProcessor.cpp b/services/camera/libcameraservice/common/DepthPhotoProcessor.cpp
index 6d96163..fc79150 100644
--- a/services/camera/libcameraservice/common/DepthPhotoProcessor.cpp
+++ b/services/camera/libcameraservice/common/DepthPhotoProcessor.cpp
@@ -64,6 +64,10 @@
 namespace android {
 namespace camera3 {
 
+// Depth samples with low confidence can skew the
+// near/far values and impact the range inverse coding.
+static const float CONFIDENCE_THRESHOLD = .15f;
+
 ExifOrientation getExifOrientation(const unsigned char *jpegBuffer, size_t jpegBufferSize) {
     if ((jpegBuffer == nullptr) || (jpegBufferSize == 0)) {
         return ExifOrientation::ORIENTATION_UNDEFINED;
@@ -238,6 +242,9 @@
     auto conf = (value >> 13) & 0x7;
     float normConfidence = (conf == 0) ? 1.f : (static_cast<float>(conf) - 1) / 7.f;
     confidence->push_back(normConfidence);
+    if (normConfidence < CONFIDENCE_THRESHOLD) {
+        return;
+    }
 
     if (*near > point) {
         *near = point;
@@ -358,8 +365,12 @@
     auto pointIt = points.begin();
     auto confidenceIt = confidence.begin();
     while ((pointIt != points.end()) && (confidenceIt != confidence.end())) {
-        pointsQuantized.push_back(floorf(((far * (*pointIt - near)) /
-                (*pointIt * (far - near))) * 255.0f));
+        auto point = *pointIt;
+        if ((*confidenceIt) < CONFIDENCE_THRESHOLD) {
+            point = std::clamp(point, near, far);
+        }
+        pointsQuantized.push_back(floorf(((far * (point - near)) /
+                (point * (far - near))) * 255.0f));
         confidenceQuantized.push_back(floorf(*confidenceIt * 255.0f));
         confidenceIt++; pointIt++;
     }