feat: MGC raw hot-pixel detection and suppression pipeline

This commit is contained in:
zhoubinjia committed 2026-11-11 13:48:00 +08:00
1 parent bf72729c27
commit f7a6415a88
2 files changed
+387 -44

No files matched your search

@@ -13,6 +13,7 @@ import android.util.Half
import com.hinnka.mycamera.camera.MultiFrameConfig
import com.hinnka.mycamera.model.SafeImage
import com.hinnka.mycamera.raw.MgcSpatialStrengthMap
import com.hinnka.mycamera.raw.RawHotPixelShaders
import com.hinnka.mycamera.raw.RawSceneAERawStats
import com.hinnka.mycamera.utils.DngCaptureDiagnostics
import com.hinnka.mycamera.utils.DirectBufferPixelPacker
@@ -231,6 +232,16 @@ internal class GlesMgcRawSpatialStacker(
}
}
/**
* MGC ProcessCaptureBaseFrame detects sensor defects once on the base frame; the burst then
* suppresses that list in every frame before alignment and merge.
*/
private class HotPixelReference(
val image: SafeImage,
val blackLevel: FloatArray,
val noiseModel: RawNoiseModel,
)
private data class FrameCalibration(
val blackLevels: FloatArray,
val inputGain: Float,
@@ -484,6 +495,19 @@ internal class GlesMgcRawSpatialStacker(
private var strengthFloatPackProgram = 0
private var strengthUnorm8PackProgram = 0
private var strengthSint16PackProgram = 0
private var hotPixelDetectProgram = 0
private var hotPixelResolveProgram = 0
private var hotPixelSuppressProgram = 0
private var hotPixelReference: HotPixelReference? = null
/** Resolved sensor-coordinate defect mask of [hotPixelReference], applied to every frame. */
private var hotPixelMask = 0
/**
* Two upload slots, alternated per frame like the temporal RAW textures: re-uploading into
* the slot the previous frame's suppression pass still samples would stall the CPU until
* the GPU drains every pass queued ahead of that read.
*/
private val hotPixelStagingRaw = IntArray(2)
private var hotPixelStagingSlot = 0
private var supportsComputePrograms = false
private var supportsComputeReadback = false
private var maxShaderStorageBlockBytes = 0L
@@ -583,6 +607,7 @@ internal class GlesMgcRawSpatialStacker(
includeReferenceHighlightMask = hasBentoCandidate || hasShadowLongFrame,
includeTemporalProcessing = hasAuxiliaryFrames,
)
beginHotPixelSuppression(frames.first(), resolvedNoiseModels.first().model)
val programInitMs = (System.nanoTime() - programInitStartNs) / 1_000_000L
renderFbo = createFramebuffer()
applyRawRenderState()
@@ -2135,6 +2160,7 @@ internal class GlesMgcRawSpatialStacker(
if (useCurrentGlContext) attachCurrentEgl() else initEgl()
ensureGles3()
initSabrePrograms()
beginHotPixelSuppression(frames.first(), resolvedNoiseModels.first().model)
renderFbo = createFramebuffer()
applyRawRenderState()
@@ -7242,7 +7268,154 @@ internal class GlesMgcRawSpatialStacker(
return output
}
/**
* Uploads one RAW frame with sensor defects suppressed. The first upload of a run must be the
* reference frame: it produces the defect mask that every later upload, including the
* reference itself, is repaired with before any alignment, rejection or merge reads it.
*/
private fun uploadRaw(image: SafeImage, texture: Int, label: String) {
// Both slots are allocated with the reference upload, ahead of every per-frame transient
// texture range, so index-based transient releases can never reclaim a staging slot.
if (hotPixelStagingRaw[0] == 0) {
for (index in hotPixelStagingRaw.indices) {
hotPixelStagingRaw[index] = allocateTexture(
TextureSpec(
width = width,
height = height,
internalFormat = GLES30.GL_R16UI,
filter = GLES30.GL_NEAREST,
),
)
}
}
val slot = hotPixelStagingSlot
hotPixelStagingSlot = 1 - slot
val staging = hotPixelStagingRaw[slot]
uploadRawPlane(image, staging, label)
if (hotPixelMask == 0) {
detectHotPixels(image, staging, label)
}
GLES30.glUseProgram(hotPixelSuppressProgram)
bindTexture(hotPixelSuppressProgram, "uRaw", 0, staging)
bindTexture(hotPixelSuppressProgram, "uHotPixelMask", 1, hotPixelMask)
uniform2i(hotPixelSuppressProgram, "uImageSize", width, height)
draw(hotPixelSuppressProgram, width, height, intArrayOf(texture))
}
private fun beginHotPixelSuppression(reference: RawStackFrame, noiseModel: RawNoiseModel) {
check(supportsComputePrograms) {
"MGC RAW hot-pixel detection requires the GLES 3.1 compute baseline"
}
if (hotPixelDetectProgram == 0) {
hotPixelDetectProgram = linkComputeProgram(
RawHotPixelShaders.DETECT,
"mgc_raw_hot_pixel_detect",
)
hotPixelResolveProgram = linkComputeProgram(
RawHotPixelShaders.RESOLVE,
"mgc_raw_hot_pixel_resolve",
)
hotPixelSuppressProgram = linkProgram(
RawHotPixelShaders.SUPPRESS,
"mgc_raw_hot_pixel_suppress",
)
}
hotPixelReference = HotPixelReference(
image = reference.image,
blackLevel = canonicalBlackLevelForFrame(reference),
noiseModel = noiseModel,
)
hotPixelMask = 0
}
/** Run-scoped textures that phase arenas must keep while RAW frames can still be uploaded. */
private fun hotPixelSuppressionTextures(): IntArray =
(hotPixelStagingRaw + hotPixelMask).filter { it != 0 }.toIntArray()
/**
* Called by the final arena handoffs, which delete every texture but their output: no RAW
* upload follows, so the mask and staging ids die with the arena and a late upload fails the
* reference check instead of touching a deleted texture.
*/
private fun endHotPixelSuppression() {
check(hotPixelSuppressionTextures().none(textures::contains)) {
"Hot-pixel suppression textures must be released with the final arena"
}
hotPixelReference = null
hotPixelMask = 0
hotPixelStagingRaw.fill(0)
hotPixelStagingSlot = 0
}
private fun detectHotPixels(image: SafeImage, rawTexture: Int, label: String) {
val reference = checkNotNull(hotPixelReference) {
"$label: hot-pixel suppression was not started for this run"
}
check(image === reference.image) {
"$label: the hot-pixel mask must be detected on the reference frame"
}
val packedWidth = ceilDiv(width, 4)
val maskSpec = TextureSpec(
width = packedWidth,
height = height,
internalFormat = GLES30.GL_RGBA8UI,
filter = GLES30.GL_NEAREST,
)
val isolatedMask = allocateTexture(maskSpec)
val resolvedMask = allocateTexture(maskSpec)
val groupsX = GlesComputeWorkGroup.imageGroupCount(packedWidth)
val groupsY = GlesComputeWorkGroup.imageGroupCount(height)
val shot = reference.noiseModel.normalizedShotNoiseForShader(cfaPattern)
val read = reference.noiseModel.normalizedReadNoiseForShader(cfaPattern)
fun bindDetectionInputs(program: Int) {
GLES30.glUseProgram(program)
bindTexture(program, "uRawTexture", 0, rawTexture)
uniform2i(program, "uImageSize", width, height)
uniform2i(program, "uGlobalOrigin", 0, 0)
uniform1i(program, "uCfaPattern", cfaPattern.mod(4))
uniform4fv(program, "uBlackLevel", reference.blackLevel)
uniform1f(program, "uWhiteLevel", sensorWhiteLevel)
// Canonical R, Gr, Gb, B -> the shader's per-color R, G, B model.
uniform3f(program, "uNoiseSlope", shot[0], 0.5f * (shot[1] + shot[2]), shot[3])
uniform3f(program, "uNoiseOffset", read[0], 0.5f * (read[1] + read[2]), read[3])
}
bindDetectionInputs(hotPixelDetectProgram)
GLES31.glBindImageTexture(
0, isolatedMask, 0, false, 0, GLES31.GL_WRITE_ONLY, GLES30.GL_RGBA8UI,
)
GLES31.glDispatchCompute(groupsX, groupsY, 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)
bindDetectionInputs(hotPixelResolveProgram)
GLES31.glBindImageTexture(
0, isolatedMask, 0, false, 0, GLES31.GL_READ_ONLY, GLES30.GL_RGBA8UI,
)
GLES31.glBindImageTexture(
1, resolvedMask, 0, false, 0, GLES31.GL_WRITE_ONLY, GLES30.GL_RGBA8UI,
)
GLES31.glDispatchCompute(groupsX, groupsY, 1)
// The suppression pass consumes the resolved mask through a sampler.
GLES31.glMemoryBarrier(GLES31.GL_TEXTURE_FETCH_BARRIER_BIT)
for (unit in 0..1) {
GLES31.glBindImageTexture(
unit, 0, 0, false, 0, GLES31.GL_READ_ONLY, GLES30.GL_RGBA8UI,
)
}
checkGlError("$label hot-pixel detection")
releaseOwnedTexture(isolatedMask, "hot-pixel isolated mask")
hotPixelMask = resolvedMask
PLog.i(
TAG,
"MGC RAW hot-pixel mask detected on $label size=${width}x$height " +
"cfa=${cfaPattern.mod(4)} black=${reference.blackLevel.contentToString()} " +
"white=$sensorWhiteLevelCode shot=${shot.contentToString()} " +
"read=${read.contentToString()}",
)
}
private fun uploadRawPlane(image: SafeImage, texture: Int, label: String) {
val plane = image.planes.firstOrNull()
?: throw IllegalArgumentException("$label has no RAW plane")
require(plane.pixelStride == RAW_BYTES_PER_PIXEL) {
@@ -7502,6 +7675,7 @@ internal class GlesMgcRawSpatialStacker(
textureSpecs.remove(texture)
}
}
endHotPixelSuppression()
checkGlError("release MGC Sabre merge texture arena")
PLog.i(
SABRE_TAG,
@@ -7571,6 +7745,7 @@ internal class GlesMgcRawSpatialStacker(
textureSpecs.remove(texture)
}
}
endHotPixelSuppression()
checkGlError("release MGC Spatial RGB $label fusion texture arena")
PLog.i(
TAG,
@@ -7601,6 +7776,9 @@ internal class GlesMgcRawSpatialStacker(
retained += capture.alignmentAtlas
retained += capture.rejectionAtlas
}
// The RGB merge re-uploads every frame after this boundary, so the run's defect mask and
// its upload staging stay alive until the fusion output handoff.
hotPixelSuppressionTextures().forEach(retained::add)
retained.remove(0)
check(retained.all(textures::contains)) {
"RGB persistent temporal resource is not owned by the stacker"
@@ -7667,6 +7845,7 @@ internal class GlesMgcRawSpatialStacker(
GLES30.GL_R16UI -> 2
GLES30.GL_R32F,
GLES30.GL_RG16F,
GLES30.GL_RGBA8UI,
GLES30.GL_RGB10_A2 -> 4
GLES30.GL_RGB16UI -> 6
GLES30.GL_RGBA16F,
@@ -8096,6 +8275,13 @@ internal class GlesMgcRawSpatialStacker(
ownsEglContext = false
supportsComputePrograms = false
supportsComputeReadback = false
hotPixelDetectProgram = 0
hotPixelResolveProgram = 0
hotPixelSuppressProgram = 0
hotPixelReference = null
hotPixelMask = 0
hotPixelStagingRaw.fill(0)
hotPixelStagingSlot = 0
}
private fun canonicalChannelAtPhase(phase: Int): Int {
@@ -210,6 +210,64 @@ internal object RawHotPixelShaders {
""".trimIndent()
/**
* Second-stage classification against the first-pass isolated-impulse mask. Requires
* `uHotPixelMask` as a readonly rgba8ui image packed four sensor pixels per texel.
*/
private val MASK_RESOLUTION = """
const int RESOLVE_CLEAN_NEIGHBORHOOD = 0;
const int RESOLVE_NOT_HOT = 1;
const int RESOLVE_HOT = 2;
bool maskedAt(ivec2 coord) {
ivec2 safe = safeCoord(coord);
uvec4 packedMask = imageLoad(
uHotPixelMask,
ivec2(safe.x / 4, safe.y)
);
return packedMask[safe.x & 3] != 0u;
}
// RESOLVE_CLEAN_NEIGHBORHOOD leaves the out arrays unwritten: no impulse is near, so the
// pixel is ordinary and its RAW neighborhood is neither read nor sorted.
int resolveHotPixel(
ivec2 coord,
float blackLevel,
float sensorRange,
float center,
out float neighbors[NEIGHBOR_COUNT],
out bool maskedNeighbors[NEIGHBOR_COUNT]
) {
bool hot = maskedAt(coord);
int maskedNeighborCount = 0;
for (int i = 0; i < NEIGHBOR_COUNT; ++i) {
bool neighborMasked = maskedAt(coord + SAME_CFA_OFFSETS[i]);
maskedNeighbors[i] = neighborMasked;
if (neighborMasked) {
maskedNeighborCount += 1;
}
}
if (!hot && maskedNeighborCount == 0) {
return RESOLVE_CLEAN_NEIGHBORHOOD;
}
collectNeighborSignals(coord, blackLevel, sensorRange, neighbors);
float baseMedian = median12(neighbors);
for (int i = 0; i < NEIGHBOR_COUNT; ++i) {
if (maskedNeighbors[i]) {
neighbors[i] = baseMedian;
}
}
// This is the Phocus hotPixelPlus role: once an isolated impulse is known, remove it
// from the statistics and test the remaining members of a small same-CFA cluster.
if (!hot) {
hot = isHotFromNeighbors(center, neighbors, sensorRange, colorAt(coord));
}
return hot ? RESOLVE_HOT : RESOLVE_NOT_HOT;
}
""".trimIndent()
val DETECT = """
#version 310 es
precision highp float;
@@ -259,14 +317,7 @@ internal object RawHotPixelShaders {
$COMMON
bool maskedAt(ivec2 coord) {
ivec2 safe = safeCoord(coord);
uvec4 packedMask = imageLoad(
uHotPixelMask,
ivec2(safe.x / 4, safe.y)
);
return packedMask[safe.x & 3] != 0u;
}
$MASK_RESOLUTION
uint repairAt(ivec2 coord) {
uint original = rawAt(coord);
@@ -274,43 +325,17 @@ internal object RawHotPixelShaders {
float blackLevel = uBlackLevel[blackChannel];
float sensorRange = max(uWhiteLevel - blackLevel, 1.0);
float center = clamp(float(original) - blackLevel, 0.0, sensorRange);
bool repairCenter = maskedAt(coord);
int maskedNeighborCount = 0;
bool maskedNeighbors[NEIGHBOR_COUNT];
for (int i = 0; i < NEIGHBOR_COUNT; ++i) {
bool neighborMasked = maskedAt(coord + SAME_CFA_OFFSETS[i]);
maskedNeighbors[i] = neighborMasked;
if (neighborMasked) {
maskedNeighborCount += 1;
}
}
// The mask is sparse, so ordinary pixels avoid RAW neighborhood reads and sorting.
if (!repairCenter && maskedNeighborCount == 0) {
return original;
}
float neighbors[NEIGHBOR_COUNT];
collectNeighborSignals(coord, blackLevel, sensorRange, neighbors);
float baseMedian = median12(neighbors);
for (int i = 0; i < NEIGHBOR_COUNT; ++i) {
if (maskedNeighbors[i]) {
neighbors[i] = baseMedian;
}
}
// This is the Phocus hotPixelPlus role: once an isolated impulse is known, remove it
// from the statistics and test the remaining members of a small same-CFA cluster.
if (!repairCenter && maskedNeighborCount > 0) {
repairCenter = isHotFromNeighbors(
center,
neighbors,
sensorRange,
colorAt(coord)
);
}
if (!repairCenter) {
bool maskedNeighbors[NEIGHBOR_COUNT];
int resolution = resolveHotPixel(
coord,
blackLevel,
sensorRange,
center,
neighbors,
maskedNeighbors
);
if (resolution != RESOLVE_HOT) {
return original;
}
@@ -343,6 +368,138 @@ internal object RawHotPixelShaders {
imageStore(uRepairedRawTexture, packedCoord, repaired);
}
""".trimIndent()
/**
* Folds second-stage cluster members into the isolated-impulse mask, producing the final
* sensor-coordinate defect mask that a burst applies to every frame.
*/
val RESOLVE = """
#version 310 es
precision highp float;
precision highp int;
precision highp uimage2D;
layout(local_size_x = 8, local_size_y = 8) in;
uniform highp usampler2D uRawTexture;
layout(rgba8ui, binding = 0) readonly uniform highp uimage2D uHotPixelMask;
layout(rgba8ui, binding = 1) writeonly uniform highp uimage2D uResolvedHotPixelMask;
$COMMON
$MASK_RESOLUTION
void main() {
ivec2 packedCoord = ivec2(gl_GlobalInvocationID.xy);
int packedWidth = (uImageSize.x + 3) / 4;
if (packedCoord.x >= packedWidth || packedCoord.y >= uImageSize.y) return;
uvec4 mask = uvec4(0u);
for (int lane = 0; lane < 4; ++lane) {
ivec2 coord = ivec2(packedCoord.x * 4 + lane, packedCoord.y);
if (coord.x >= uImageSize.x) continue;
int blackChannel = blackChannelAt(coord);
float blackLevel = uBlackLevel[blackChannel];
float sensorRange = max(uWhiteLevel - blackLevel, 1.0);
float center = signalAt(coord, blackLevel, sensorRange);
float neighbors[NEIGHBOR_COUNT];
bool maskedNeighbors[NEIGHBOR_COUNT];
int resolution = resolveHotPixel(
coord,
blackLevel,
sensorRange,
center,
neighbors,
maskedNeighbors
);
mask[lane] = resolution == RESOLVE_HOT ? 255u : 0u;
}
imageStore(uResolvedHotPixelMask, packedCoord, mask);
}
""".trimIndent()
/**
* Applies a resolved sensor-coordinate defect mask to one RAW frame (MGC SuppressHotPixels).
* Masked samples take the median of their unmasked same-CFA cardinal neighbors, falling back
* to the outer same-CFA ring when all four cardinals are defective. The pass is integer-only:
* the replacement is a same-channel median of codes, independent of black level and gain, and
* no 16-bit sampler value is converted to float.
*/
val SUPPRESS = """
#version 300 es
precision highp float;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
uniform highp usampler2D uHotPixelMask;
uniform ivec2 uImageSize;
layout(location = 0) out highp uint oRaw;
const int NEIGHBOR_COUNT = 12;
const int CARDINAL_COUNT = 4;
const ivec2 SAME_CFA_OFFSETS[NEIGHBOR_COUNT] = ivec2[NEIGHBOR_COUNT](
ivec2(-2, 0), ivec2( 2, 0),
ivec2( 0, -2), ivec2( 0, 2),
ivec2(-2, -2), ivec2( 2, -2),
ivec2(-2, 2), ivec2( 2, 2),
ivec2(-4, 0), ivec2( 4, 0),
ivec2( 0, -4), ivec2( 0, 4)
);
bool inside(ivec2 coord) {
return all(greaterThanEqual(coord, ivec2(0))) && all(lessThan(coord, uImageSize));
}
bool maskedAt(ivec2 coord) {
uvec4 packedMask = texelFetch(uHotPixelMask, ivec2(coord.x >> 2, coord.y), 0);
return packedMask[coord.x & 3] != 0u;
}
uint medianOf(inout uint values[NEIGHBOR_COUNT], int count) {
for (int i = 1; i < count; ++i) {
uint value = values[i];
int j = i - 1;
while (j >= 0 && values[j] > value) {
values[j + 1] = values[j];
j -= 1;
}
values[j + 1] = value;
}
int middle = count / 2;
if ((count & 1) != 0) return values[middle];
return (values[middle - 1] + values[middle] + 1u) >> 1;
}
void main() {
ivec2 coord = ivec2(gl_FragCoord.xy);
uint original = texelFetch(uRaw, coord, 0).r;
if (!maskedAt(coord)) {
oRaw = original;
return;
}
uint values[NEIGHBOR_COUNT];
int count = 0;
for (int i = 0; i < CARDINAL_COUNT; ++i) {
ivec2 neighbor = coord + SAME_CFA_OFFSETS[i];
if (inside(neighbor) && !maskedAt(neighbor)) {
values[count] = texelFetch(uRaw, neighbor, 0).r;
count += 1;
}
}
if (count == 0) {
for (int i = CARDINAL_COUNT; i < NEIGHBOR_COUNT; ++i) {
ivec2 neighbor = coord + SAME_CFA_OFFSETS[i];
if (inside(neighbor) && !maskedAt(neighbor)) {
values[count] = texelFetch(uRaw, neighbor, 0).r;
count += 1;
}
}
}
oRaw = count == 0 ? original : medianOf(values, count);
}
""".trimIndent()
}
/** Two-pass RAW-domain hot-pixel detection and repair algorithm. */