瀏覽代碼

Merge pull request #4181 from alfeg/perf/alignment-integer-ssd

perf(alignment): integer SSD in CFindTemplate::FindTemplate
SybexXx 1 月之前
父節點
當前提交
e353fa7ff9
共有 1 個文件被更改,包括 20 次插入 和 9 次删除
  1. 20 9
      code/components/jomjol_image_proc/CFindTemplate.cpp

+ 20 - 9
code/components/jomjol_image_proc/CFindTemplate.cpp

@@ -8,6 +8,7 @@
 #include <esp_timer.h>
 #include <esp_timer.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "freertos/task.h"
+#include <cstdint>
 
 
 static const char* TAG = "C FIND TEMPL";
 static const char* TAG = "C FIND TEMPL";
 
 
@@ -97,9 +98,11 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
 //    ESP_LOGD(TAG, "FindTemplate 04");
 //    ESP_LOGD(TAG, "FindTemplate 04");
 
 
 
 
-    double aktSAD;
-    double sadSpan = (double)tpl_width * tpl_height * 255;
-    double minSAD = sadSpan * sadSpan;
+    // Sum of squared differences in integer arithmetic: the ESP32 FPU is single precision only,
+    // so the former double/pow() version ran in software emulation and took several seconds per reference.
+    // The result is identical: the sum is exact either way and the search order / tie-breaking is unchanged.
+    uint64_t aktSAD;
+    uint64_t minSAD = UINT64_MAX;
 
 
     RGBImageLock();
     RGBImageLock();
 
 
@@ -112,22 +115,30 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
     // PSRAM-Zugriffe auf Core 0 verdrängen den WiFi-Stack. Spätestens alle 50 ms
     // PSRAM-Zugriffe auf Core 0 verdrängen den WiFi-Stack. Spätestens alle 50 ms
     // den Core abgeben, damit Beacons weiter verarbeitet werden (Tick = 10 ms).
     // den Core abgeben, damit Beacons weiter verarbeitet werden (Tick = 10 ms).
     int64_t nextYieldUs = esp_timer_get_time() + 50000;
     int64_t nextYieldUs = esp_timer_get_time() + 50000;
+    const int tpl_row_len = tpl_width * channels;
 
 
     for (xouter = ow_start; xouter <= ow_stop; xouter++)
     for (xouter = ow_start; xouter <= ow_stop; xouter++)
         for (youter = oh_start; youter <= oh_stop; ++youter)
         for (youter = oh_start; youter <= oh_stop; ++youter)
         {
         {
             aktSAD = 0;
             aktSAD = 0;
-            for (tpl_x = 0; tpl_x < tpl_width; tpl_x++)
-                for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
+            // Walk the template row by row so that image accesses stay sequential in (PSRAM) memory
+            for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
+            {
+                const stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + xouter));
+                const stbi_uc* p_tpl = rgb_template + (tpl_y * tpl_row_len);
+                uint32_t rowSAD = 0;
+                for (tpl_x = 0; tpl_x < tpl_row_len; tpl_x += channels)
                 {
                 {
-                    stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + (xouter + tpl_x)));
-                    stbi_uc* p_tpl = rgb_template + (channels * (tpl_y * tpl_width + tpl_x));
                     for (_ch = 0; _ch < _anzchannels; ++_ch)
                     for (_ch = 0; _ch < _anzchannels; ++_ch)
                     {
                     {
-                        int diff = (int)p_tpl[_ch] - (int)p_org[_ch];
-                        aktSAD += diff * diff;
+                        int dif = p_tpl[tpl_x + _ch] - p_org[tpl_x + _ch];
+                        rowSAD += dif * dif;
                     }
                     }
                 }
                 }
+                aktSAD += rowSAD;
+                if (aktSAD >= minSAD)   // can no longer become the new minimum
+                    break;
+            }
             if (aktSAD < minSAD)
             if (aktSAD < minSAD)
             {
             {
                 minSAD = aktSAD;
                 minSAD = aktSAD;