feat: enhance image fusion robustness and add performance logging
This commit is contained in:
1 parent
69939db239
commit
a6f8b10944
3 files changed
+165
-29
No files matched your search
@@ -87,6 +87,15 @@ vec3 rgb2ycbcr(vec3 rgb) {
|
||||
return vec3(Y, Cb, Cr);
|
||||
}
|
||||
|
||||
float sampleLuma(sampler2D s, vec2 uv, vec2 size) {
|
||||
return rgb2ycbcr(sampleBicubic(s, uv, size).rgb).x;
|
||||
}
|
||||
|
||||
float yccDiffSq(vec3 a, vec3 b) {
|
||||
vec3 d = (a - b) * vec3(1.0, 0.5, 0.5);
|
||||
return dot(d, d);
|
||||
}
|
||||
|
||||
float sampleLocalTileWeight(vec2 uv) {
|
||||
vec2 gridPos = uv * vec2(float(pc.gridW), float(pc.gridH));
|
||||
ivec2 g = ivec2(floor(gridPos));
|
||||
@@ -120,6 +129,7 @@ void main() {
|
||||
vec2 finalUV;
|
||||
finalUV.x = normalizedUV.x * pc.t0 + normalizedUV.y * pc.t1 + pc.t2;
|
||||
finalUV.y = normalizedUV.x * pc.t3 + normalizedUV.y * pc.t4 + pc.t5;
|
||||
vec2 referenceUV = finalUV;
|
||||
|
||||
// 2. Alignment
|
||||
vec2 nativeSize = vec2(float(pc.width) / pc.scale, float(pc.height) / pc.scale);
|
||||
@@ -148,11 +158,12 @@ void main() {
|
||||
float e11 = motionPrior.errors[g1.y * pc.gridW + g1.x];
|
||||
motionError = mix(mix(e00, e10, gf.x), mix(e01, e11, gf.x), gf.y);
|
||||
|
||||
finalUV += alignment / nativeSize;
|
||||
finalUV = referenceUV + alignment / nativeSize;
|
||||
}
|
||||
|
||||
vec2 posInSource = finalUV * nativeSize;
|
||||
vec2 centerSource = floor(posInSource - 0.5) + 0.5;
|
||||
vec2 centerReference = floor(referenceUV * nativeSize - 0.5) + 0.5;
|
||||
|
||||
vec4 accumValue = accumulator.data[outIndex];
|
||||
float currentW = accumValue.a;
|
||||
@@ -166,26 +177,86 @@ void main() {
|
||||
centerColor = sampleBicubic(rgbSampler, finalUV, nativeSize).rgb;
|
||||
centerYCC = rgb2ycbcr(centerColor);
|
||||
|
||||
vec3 refYCC = rgb2ycbcr(sampleBicubic(referenceSampler, finalUV, nativeSize).rgb);
|
||||
float diff = length((centerYCC - refYCC) * vec3(1.0, 0.5, 0.5));
|
||||
|
||||
float expectedVar = pc.noiseAlpha * refYCC.x + pc.noiseBeta;
|
||||
expectedSigma = sqrt(max(expectedVar, 1e-7));
|
||||
expectedSigma = max(expectedSigma, 0.025);
|
||||
vec3 refYCC = rgb2ycbcr(sampleBicubic(referenceSampler, referenceUV, nativeSize).rgb);
|
||||
float centerLuma = refYCC.x;
|
||||
|
||||
float ratio = diff / expectedSigma;
|
||||
float T1 = 2.5; float T2 = 8.0;
|
||||
if (ratio <= T1) centerRobustness = 1.0;
|
||||
else if (ratio >= T2) centerRobustness = 0.0;
|
||||
else centerRobustness = (T2 - ratio) / (T2 - T1);
|
||||
float sumY = 0.0;
|
||||
float sumY2 = 0.0;
|
||||
for (int sy = -1; sy <= 1; sy++) {
|
||||
for (int sx = -1; sx <= 1; sx++) {
|
||||
vec2 refSampleUV = (centerReference + vec2(float(sx), float(sy))) / nativeSize;
|
||||
float yRef = sampleLuma(referenceSampler, refSampleUV, nativeSize);
|
||||
sumY += yRef;
|
||||
sumY2 += yRef * yRef;
|
||||
}
|
||||
}
|
||||
float meanY = sumY / 9.0;
|
||||
float sigma2Spatial = max(sumY2 / 9.0 - meanY * meanY, 0.0);
|
||||
float sigma2Noise = max(pc.noiseAlpha * centerLuma + pc.noiseBeta, 1e-10);
|
||||
float sigma2 = max(sigma2Spatial, sigma2Noise);
|
||||
expectedSigma = sqrt(max(sigma2, 1e-10));
|
||||
|
||||
float gradX = 0.5 * (
|
||||
sampleLuma(referenceSampler, (centerReference + vec2(1.0, 0.0)) / nativeSize, nativeSize) -
|
||||
sampleLuma(referenceSampler, (centerReference + vec2(-1.0, 0.0)) / nativeSize, nativeSize));
|
||||
float gradY = 0.5 * (
|
||||
sampleLuma(referenceSampler, (centerReference + vec2(0.0, 1.0)) / nativeSize, nativeSize) -
|
||||
sampleLuma(referenceSampler, (centerReference + vec2(0.0, -1.0)) / nativeSize, nativeSize));
|
||||
float edgeStrength = sqrt(max((gradX * gradX + gradY * gradY) / max(sigma2, 1e-8), 0.0));
|
||||
float edgeRelax = smoothstep(1.2, 5.0, edgeStrength);
|
||||
|
||||
float flowMag = length(alignment);
|
||||
float flowPenalty = 1.0;
|
||||
if (flowMag > 15.0) {
|
||||
flowPenalty = exp(-0.1 * (flowMag - 15.0));
|
||||
}
|
||||
float errorPenalty = exp(-0.015 * max(motionError, 0.0));
|
||||
float globalPenalty = flowPenalty * errorPenalty;
|
||||
|
||||
float minRobustness = 1.0;
|
||||
float sumRobustness = 0.0;
|
||||
float weightSum = 0.0;
|
||||
float centerR = 1.0;
|
||||
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
vec2 sampleOffset = vec2(float(dx), float(dy));
|
||||
vec2 refSampleUV = (centerReference + sampleOffset) / nativeSize;
|
||||
vec2 trgSampleUV = (centerReference + sampleOffset + alignment) / nativeSize;
|
||||
|
||||
vec3 localRefYCC = rgb2ycbcr(sampleBicubic(referenceSampler, refSampleUV, nativeSize).rgb);
|
||||
vec3 localTrgYCC = rgb2ycbcr(sampleBicubic(rgbSampler, trgSampleUV, nativeSize).rgb);
|
||||
float dSq = yccDiffSq(localRefYCC, localTrgYCC);
|
||||
|
||||
float noiseFloor = mix(4.5 * sigma2, sigma2Noise, edgeRelax);
|
||||
float den = mix(sigma2, sigma2Noise, edgeRelax);
|
||||
float residual = max(0.0, dSq - noiseFloor) / max(den, 1e-10);
|
||||
float tau = 1.0 + 1.2 * edgeRelax;
|
||||
float r = exp(-0.5 * pow(residual / tau, 8.0)) * globalPenalty;
|
||||
|
||||
float spatialWeight = (dx == 0 && dy == 0) ? 2.0 : 1.0;
|
||||
sumRobustness += r * spatialWeight;
|
||||
weightSum += spatialWeight;
|
||||
minRobustness = min(minRobustness, r);
|
||||
if (dx == 0 && dy == 0) {
|
||||
centerR = r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float avgRobustness = (weightSum > 0.0) ? (sumRobustness / weightSum) : minRobustness;
|
||||
float minMix = mix(0.35, 0.15, edgeRelax);
|
||||
float centerMix = mix(0.35, 0.55, edgeRelax);
|
||||
float avgMix = 1.0 - minMix - centerMix;
|
||||
centerRobustness = clamp(minMix * minRobustness +
|
||||
centerMix * centerR +
|
||||
avgMix * avgRobustness,
|
||||
0.0, 1.0);
|
||||
|
||||
if (isnan(centerRobustness) || isinf(centerRobustness)) centerRobustness = 0.0;
|
||||
|
||||
float motionPenalty = 0.015;
|
||||
centerRobustness *= (1.0 / (1.0 + motionError * motionPenalty));
|
||||
}
|
||||
|
||||
float localTileWeight = sampleLocalTileWeight(finalUV);
|
||||
float localTileWeight = sampleLocalTileWeight(referenceUV);
|
||||
float fusionMask = localTileWeight;
|
||||
int supportRadius = 0;
|
||||
|
||||
@@ -245,10 +316,12 @@ void main() {
|
||||
|
||||
float robustness = centerRobustness;
|
||||
if (pc.isFirstFrame == 0) {
|
||||
vec3 sampleRefYCC = rgb2ycbcr(texture(referenceSampler, sampleUV).rgb);
|
||||
float sampleDiff = length((sampleYCC - sampleRefYCC) * vec3(1.0, 0.5, 0.5));
|
||||
float sampleRatio = sampleDiff / expectedSigma;
|
||||
float sampleRobustness = 1.0 - smoothstep(3.0, 9.0, sampleRatio);
|
||||
vec2 refSampleUV = (samplePos - alignment) / nativeSize;
|
||||
vec3 sampleRefYCC = rgb2ycbcr(texture(referenceSampler, refSampleUV).rgb);
|
||||
float sampleDiffSq = yccDiffSq(sampleYCC, sampleRefYCC);
|
||||
float sampleResidual = max(0.0, sampleDiffSq - 4.5 * expectedSigma * expectedSigma) /
|
||||
max(expectedSigma * expectedSigma, 1e-10);
|
||||
float sampleRobustness = exp(-0.5 * pow(sampleResidual, 8.0));
|
||||
robustness *= sampleRobustness;
|
||||
robustness = max(robustness, robustnessFloor);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,24 @@ struct LocalReliabilityMap {
|
||||
float detailAreaFraction = 0.0f;
|
||||
};
|
||||
|
||||
constexpr bool kEnableYuvStackPerfLogs = false;
|
||||
|
||||
inline void logYuvStackStage(const char *stage, double elapsedMs) {
|
||||
if (!kEnableYuvStackPerfLogs)
|
||||
return;
|
||||
LOGI("YuvStackPerf[%s]: %.3f ms", stage, elapsedMs);
|
||||
}
|
||||
|
||||
#undef TIME_START
|
||||
#undef TIME_END
|
||||
#define TIME_START(name) const auto start_##name = std::chrono::steady_clock::now()
|
||||
#define TIME_END(name) \
|
||||
const double elapsed_##name = std::chrono::duration<double, std::milli>( \
|
||||
std::chrono::steady_clock::now() - \
|
||||
start_##name) \
|
||||
.count(); \
|
||||
logYuvStackStage(#name, elapsed_##name)
|
||||
|
||||
inline float clamp01(float v) { return std::clamp(v, 0.0f, 1.0f); }
|
||||
|
||||
inline float smoothstepf(float edge0, float edge1, float x) {
|
||||
@@ -175,6 +193,19 @@ float computeFrameFusionWeight(const std::vector<float> &errorMap,
|
||||
|
||||
} // namespace
|
||||
|
||||
void YuvStackPerfStats::logSummary(uint32_t outputW, uint32_t outputH,
|
||||
float scale) const {
|
||||
if (!kEnableYuvStackPerfLogs)
|
||||
return;
|
||||
LOGI("YuvStackPerf summary: total=%.3f ms output=%ux%u scale=%.2f frames=%zu "
|
||||
"kept=%zu skipped=%zu effFrames=%.2f",
|
||||
totalMs, outputW, outputH, scale, totalFrames, keptFrames, skippedFrames,
|
||||
totalEffectiveFrames);
|
||||
LOGI("YuvStackPerf phases: score=%.3f process=%.3f norm=%.3f copy=%.3f",
|
||||
scoreCalculationMs, allFramesProcessingMs, normalizationDispatchMs,
|
||||
outputCopyMs);
|
||||
}
|
||||
|
||||
VulkanImageStacker::VulkanImageStacker(uint32_t w, uint32_t h, bool sr)
|
||||
: width(w), height(h), enableSuperRes(sr) {
|
||||
|
||||
@@ -822,13 +853,15 @@ bool VulkanImageStacker::addFrame(AHardwareBuffer *buffer) {
|
||||
}
|
||||
|
||||
bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
|
||||
GrayImage &cachedGray) {
|
||||
using PerfClock = std::chrono::high_resolution_clock;
|
||||
GrayImage &cachedGray, YuvStackPerfStats &perf) {
|
||||
using PerfClock = std::chrono::steady_clock;
|
||||
auto elapsedMs = [](const PerfClock::time_point &start,
|
||||
const PerfClock::time_point &end) {
|
||||
return std::chrono::duration<double, std::milli>(end - start).count();
|
||||
};
|
||||
VulkanImage input;
|
||||
perf.totalFrames++;
|
||||
|
||||
// Use existing conversion if available (from first frame) to compatible with
|
||||
// immutable sampler
|
||||
TIME_START(importBuffer);
|
||||
@@ -840,7 +873,7 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
|
||||
TIME_END(importBuffer);
|
||||
|
||||
bool currentIsFirstFrame = isFirstFrame;
|
||||
LOGI("processFrame: Start. isFirstFrame=%d", currentIsFirstFrame);
|
||||
// LOGI("processFrame: Start. isFirstFrame=%d", currentIsFirstFrame);
|
||||
|
||||
VulkanManager &vm = VulkanManager::getInstance();
|
||||
VkDevice device = vm.getDevice();
|
||||
@@ -1040,8 +1073,8 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
|
||||
frameWeight = computeFrameFusionWeight(alignmentErrorMap, sharpnessRatio,
|
||||
localMap);
|
||||
float p90Err = computeScalarStats(alignmentErrorMap).p90;
|
||||
LOGI("YUV fusion: sharpnessRatio=%.3f frameWeight=%.3f flatGood=%.3f flatArea=%.3f detailArea=%.3f p90Err=%.3f p90ErrRms=%.3f",
|
||||
sharpnessRatio, frameWeight, localMap.flatGoodFraction,
|
||||
LOGI("YUV fusion: frame=%zu sharpnessRatio=%.3f frameWeight=%.3f flatGood=%.3f flatArea=%.3f detailArea=%.3f p90Err=%.3f p90ErrRms=%.3f",
|
||||
perf.totalFrames - 1, sharpnessRatio, frameWeight, localMap.flatGoodFraction,
|
||||
localMap.flatAreaFraction, localMap.detailAreaFraction,
|
||||
p90Err, std::sqrt(std::max(p90Err, 0.0f)));
|
||||
}
|
||||
@@ -1074,8 +1107,11 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
|
||||
if (!currentIsFirstFrame && pc.frameWeight < 0.12f) {
|
||||
input.release(device);
|
||||
isFirstFrame = false;
|
||||
return true;
|
||||
perf.skippedFrames++;
|
||||
return false;
|
||||
}
|
||||
perf.keptFrames++;
|
||||
perf.totalEffectiveFrames += pc.frameWeight;
|
||||
|
||||
double yuvToRgbaGpuMs = -1.0;
|
||||
double tensorGpuMs = -1.0;
|
||||
@@ -1527,6 +1563,9 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
|
||||
// Lower CPU priority for this thread during heavy processing
|
||||
setpriority(PRIO_PROCESS, 0, 10);
|
||||
|
||||
YuvStackPerfStats perf;
|
||||
const auto startTotal = std::chrono::steady_clock::now();
|
||||
|
||||
// Process all queued frames first
|
||||
isFirstFrame = true;
|
||||
if (pendingFrames.empty()) {
|
||||
@@ -1578,6 +1617,7 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
|
||||
}
|
||||
}
|
||||
TIME_END(scoreCalculation);
|
||||
perf.scoreCalculationMs = elapsed_scoreCalculation;
|
||||
|
||||
// 2. Sort pendingFrames by score descending
|
||||
std::sort(
|
||||
@@ -1599,8 +1639,8 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
|
||||
for (auto &frame : pendingFrames) {
|
||||
// LOGI("processStack: Processing frame %d, score %f", frameIdx,
|
||||
// frame.score);
|
||||
if (!processFrame(frame.buffer, frame.score, frame.grayY)) {
|
||||
LOGE("processStack: Failed to process frame %d", frameIdx);
|
||||
if (!processFrame(frame.buffer, frame.score, frame.grayY, perf)) {
|
||||
// LOGW("processStack: Skipped or failed frame %d", frameIdx);
|
||||
}
|
||||
// Release grayscale cache to free memory as soon as it's consumed
|
||||
frame.grayY.data.clear();
|
||||
@@ -1614,6 +1654,7 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
|
||||
resetDescriptorHandles();
|
||||
|
||||
TIME_END(allFramesProcessing);
|
||||
perf.allFramesProcessingMs = elapsed_allFramesProcessing;
|
||||
|
||||
if (!outBitmap || isFirstFrame) // Check again after processing
|
||||
return false;
|
||||
@@ -1832,6 +1873,7 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
|
||||
vm.endSingleTimeCommands(cb);
|
||||
vkQueueWaitIdle(vm.getComputeQueue());
|
||||
TIME_END(normalizationDispatch);
|
||||
perf.normalizationDispatchMs = elapsed_normalizationDispatch;
|
||||
|
||||
// 2. Map and copy to bitmap
|
||||
TIME_START(outputCopy);
|
||||
@@ -1848,11 +1890,17 @@ bool VulkanImageStacker::processStack(uint32_t *outBitmap, uint32_t outWidth,
|
||||
}
|
||||
vkUnmapMemory(device, stagingMemory);
|
||||
TIME_END(outputCopy);
|
||||
perf.outputCopyMs = elapsed_outputCopy;
|
||||
|
||||
vkFreeDescriptorSets(device, descriptorPool, numTiles, normalizeSets.data());
|
||||
std::fill(normalizeSets.begin(), normalizeSets.end(),
|
||||
(VkDescriptorSet)VK_NULL_HANDLE);
|
||||
|
||||
perf.totalMs = std::chrono::duration<double, std::milli>(
|
||||
std::chrono::steady_clock::now() - startTotal)
|
||||
.count();
|
||||
perf.logSummary(outWidth, outHeight, scale);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,21 @@
|
||||
#include "vulkan_utils.h"
|
||||
#include <vector>
|
||||
|
||||
struct YuvStackPerfStats {
|
||||
double totalMs = 0.0;
|
||||
double scoreCalculationMs = 0.0;
|
||||
double allFramesProcessingMs = 0.0;
|
||||
double normalizationDispatchMs = 0.0;
|
||||
double outputCopyMs = 0.0;
|
||||
|
||||
size_t totalFrames = 0;
|
||||
size_t keptFrames = 0;
|
||||
size_t skippedFrames = 0;
|
||||
double totalEffectiveFrames = 0.0;
|
||||
|
||||
void logSummary(uint32_t outputW, uint32_t outputH, float scale) const;
|
||||
};
|
||||
|
||||
class VulkanImageStacker {
|
||||
public:
|
||||
VulkanImageStacker(uint32_t width, uint32_t height, bool enableSuperRes);
|
||||
@@ -123,7 +138,7 @@ private:
|
||||
void releaseVulkanResources();
|
||||
void createPipelines(VkSampler immutableSampler);
|
||||
bool processFrame(AHardwareBuffer *buffer, float frameScore,
|
||||
GrayImage &cachedGray);
|
||||
GrayImage &cachedGray, YuvStackPerfStats &perf);
|
||||
void resetDescriptorHandles();
|
||||
void releasePendingFrames();
|
||||
};
|
||||
Reference in new issue
Block a user