feat: noise model fallback

This commit is contained in:
Hinnka committed 2026-09-01 20:52:00 +08:00
1 parent a0bcae59e6
commit 1e922d2d2f
9 files changed
+92 -103

No files matched your search

@@ -158,11 +158,6 @@ internal class GlesMgcRawBaseFrameSelector(
}
val rawTextureFences = LongArray(rawTextures.size)
val baseCamera2Model = normalIndices.firstNotNullOfOrNull { index ->
frames[index].channelNoiseProfile
?.let(RawNoiseModel::fromCamera2NoiseProfile)
?.takeIf { it.hasValidCamera2Profile }
} ?: RawNoiseModel.EMPTY
try {
normalIndices.forEachIndexed { measurementIndex, frameIndex ->
val ringIndex = measurementIndex % rawTextures.size
@@ -181,7 +176,6 @@ internal class GlesMgcRawBaseFrameSelector(
minimumSensitivityIso = frame.minimumSensitivityIso,
maximumAnalogSensitivityIso = frame.maximumAnalogSensitivityIso,
perFrameCamera2Profile = frame.channelNoiseProfile,
baseFrameCamera2Model = baseCamera2Model,
)
check(noiseModel.source != RawNoiseModelSource.UNAVAILABLE) {
"Noise model is unavailable for base candidate ${frame.frameNumber}"
@@ -536,7 +536,6 @@ internal class GlesMgcRawSpatialStacker(
private var maxShaderStorageBlockBytes = 0L
private var maxComputePackGroupsX = 0
private var maxComputePackGroupsY = 0
private var baseFrameCamera2Model: RawNoiseModel = RawNoiseModel.EMPTY
private val pixelDifferenceKernel = gaussianKernel(
size = PIXEL_DIFFERENCE_KERNEL_SIZE,
sigma = PIXEL_DIFFERENCE_SMOOTH_SIGMA,
@@ -588,11 +587,6 @@ internal class GlesMgcRawSpatialStacker(
images.forEach { it.close() }
return null
}
baseFrameCamera2Model = frames.firstOrNull()
?.channelNoiseProfile
?.let(RawNoiseModel::fromCamera2NoiseProfile)
?.takeIf { it.hasValidCamera2Profile }
?: RawNoiseModel.EMPTY
val resolvedNoiseModels = frames.map(::resolveNoiseModelForFrame)
if (resolvedNoiseModels.any { it.source == RawNoiseModelSource.UNAVAILABLE }) {
PLog.e(
@@ -736,24 +730,28 @@ internal class GlesMgcRawSpatialStacker(
val perFrameCamera2Profiles = resolvedNoiseModels.count {
it.source == RawNoiseModelSource.CAMERA2_PER_FRAME
}
val baseFrameCamera2Profiles = resolvedNoiseModels.count {
it.source == RawNoiseModelSource.CAMERA2_BASE_FRAME
}
val calibratedProfiles = resolvedNoiseModels.count {
it.source == RawNoiseModelSource.GCAM_CALIBRATED
}
val systemFallbackProfiles = resolvedNoiseModels.count {
it.source == RawNoiseModelSource.GCAM_SYSTEM_FALLBACK
}
val calibratedProfile = (noiseProfileSelection as? RawNoiseProfileSelection.Calibrated)
?.profile
val profileSource = calibratedProfile?.let { profile ->
profile.maxAnalogSensitivity?.let { maxAnalog ->
"gcam-c:${profile.id} profileMaxAnalog=$maxAnalog"
} ?: "mgc-override:${profile.id} cameraGainSplit=enabled"
} ?: "Camera2 SENSOR_NOISE_PROFILE"
val profileSource = when (val selection = noiseProfileSelection) {
is RawNoiseProfileSelection.Calibrated ->
selection.profile.maxAnalogSensitivity?.let { maxAnalog ->
"gcam-c:${selection.profile.id} profileMaxAnalog=$maxAnalog"
} ?: "mgc-override:${selection.profile.id} cameraGainSplit=enabled"
is RawNoiseProfileSelection.Camera2 ->
"Camera2 SENSOR_NOISE_PROFILE fallback=gcam-c:" +
selection.fallbackProfile.id
}
PLog.i(
TAG,
"MGC Spatial noise profile source=$profileSource " +
"perFrame=$perFrameCamera2Profiles/${frames.size} " +
"baseFallback=$baseFrameCamera2Profiles/${frames.size} " +
"systemFallback=$systemFallbackProfiles/${frames.size} " +
"calibrated=$calibratedProfiles/${frames.size}",
)
calibratedProfile?.let { profile ->
@@ -2031,11 +2029,6 @@ internal class GlesMgcRawSpatialStacker(
images.forEach { it.close() }
return null
}
baseFrameCamera2Model = frames.firstOrNull()
?.channelNoiseProfile
?.let(RawNoiseModel::fromCamera2NoiseProfile)
?.takeIf { it.hasValidCamera2Profile }
?: RawNoiseModel.EMPTY
val resolvedNoiseModels = frames.map(::resolveNoiseModelForFrame)
if (resolvedNoiseModels.any { it.source == RawNoiseModelSource.UNAVAILABLE }) {
PLog.e(
@@ -3782,7 +3775,6 @@ internal class GlesMgcRawSpatialStacker(
minimumSensitivityIso = frame.minimumSensitivityIso,
maximumAnalogSensitivityIso = frame.maximumAnalogSensitivityIso,
perFrameCamera2Profile = frame.channelNoiseProfile,
baseFrameCamera2Model = baseFrameCamera2Model,
)
private fun noiseModelForFrame(frame: RawStackFrame): RawNoiseModel =
@@ -292,7 +292,7 @@ object MultiFrameStacker {
masterBlackLevel: FloatArray = floatArrayOf(0f, 0f, 0f, 0f),
whiteLevel: Int = 1023,
whiteBalanceGains: FloatArray = floatArrayOf(1f, 1f, 1f, 1f),
noiseProfileSelection: RawNoiseProfileSelection = RawNoiseProfileSelection.Camera2,
noiseProfileSelection: RawNoiseProfileSelection,
lensShading: FloatArray? = null,
lensShadingWidth: Int = 0,
lensShadingHeight: Int = 0,
@@ -184,8 +184,8 @@ class RawNoiseModel private constructor(
internal enum class RawNoiseModelSource {
GCAM_CALIBRATED,
GCAM_SYSTEM_FALLBACK,
CAMERA2_PER_FRAME,
CAMERA2_BASE_FRAME,
UNAVAILABLE,
}
@@ -194,7 +194,7 @@ internal data class ResolvedRawNoiseModel(
val source: RawNoiseModelSource,
)
/** Resolves the selected calibrated or Camera2 noise profile without substituting another model. */
/** Resolves the selected calibrated or Camera2 noise profile and its configured system fallback. */
internal object RawNoiseModelResolver {
fun resolve(
selection: RawNoiseProfileSelection,
@@ -202,7 +202,6 @@ internal object RawNoiseModelResolver {
minimumSensitivityIso: Int = 0,
maximumAnalogSensitivityIso: Int = 0,
perFrameCamera2Profile: FloatArray?,
baseFrameCamera2Model: RawNoiseModel,
): ResolvedRawNoiseModel {
when (selection) {
is RawNoiseProfileSelection.Calibrated -> {
@@ -217,7 +216,7 @@ internal object RawNoiseModelResolver {
)
return ResolvedRawNoiseModel(model, RawNoiseModelSource.GCAM_CALIBRATED)
}
RawNoiseProfileSelection.Camera2 -> Unit
is RawNoiseProfileSelection.Camera2 -> Unit
}
perFrameCamera2Profile
?.let(RawNoiseModel::fromCamera2NoiseProfile)
@@ -225,10 +224,15 @@ internal object RawNoiseModelResolver {
?.let { model ->
return ResolvedRawNoiseModel(model, RawNoiseModelSource.CAMERA2_PER_FRAME)
}
if (baseFrameCamera2Model.hasValidCamera2Profile) {
val fallbackModel = selection.fallbackProfile.evaluate(
sensitivity = sensitivity,
minimumSensitivityIso = minimumSensitivityIso,
maximumAnalogSensitivityIso = maximumAnalogSensitivityIso,
)
if (fallbackModel != null) {
return ResolvedRawNoiseModel(
baseFrameCamera2Model,
RawNoiseModelSource.CAMERA2_BASE_FRAME,
fallbackModel,
RawNoiseModelSource.GCAM_SYSTEM_FALLBACK,
)
}
return ResolvedRawNoiseModel(
@@ -8,7 +8,10 @@ sealed interface RawNoiseProfileSelection {
override val id: String = profile.id,
) : RawNoiseProfileSelection
data object Camera2 : RawNoiseProfileSelection {
data class Camera2(
/** Calibrated model used only when no usable Camera2 sensor model is available. */
val fallbackProfile: CalibratedRawNoiseProfile,
) : RawNoiseProfileSelection {
override val id: String = SYSTEM_CAMERA2_ID
}
@@ -191,7 +191,7 @@ data class RawMetadata(
fun withNoiseProfileSelection(selection: RawNoiseProfileSelection): RawMetadata {
val profile = when (selection) {
is RawNoiseProfileSelection.Calibrated -> selection.profile
RawNoiseProfileSelection.Camera2 -> {
is RawNoiseProfileSelection.Camera2 -> {
val hasUsableSourceProfile = when (noiseProfileLayout) {
RawNoiseProfileLayout.CAMERA2_CFA,
RawNoiseProfileLayout.CANONICAL_BAYER ->
@@ -205,10 +205,7 @@ data class RawMetadata(
RawNoiseProfileLayout.NONE -> false
}
if (hasUsableSourceProfile) return this
return copy(
channelNoiseProfile = FloatArray(0),
noiseProfileLayout = RawNoiseProfileLayout.NONE,
)
selection.fallbackProfile
}
}
val model = profile.evaluate(
@@ -26,6 +26,17 @@ class RawNoiseProfileManager(context: Context) {
private val appContext = context.applicationContext
private val customImportManager = CustomImportManager(appContext)
private val calibratedCache = ConcurrentHashMap<String, CalibratedRawNoiseProfile>()
private val systemCamera2Selection: RawNoiseProfileSelection.Camera2 by lazy(
LazyThreadSafetyMode.SYNCHRONIZED,
) {
val fallbackInfo = checkNotNull(
BUILT_IN_PROFILES.firstOrNull { it.id == PIXEL8_PRO_PROFILE_ID },
) { "Bundled Pixel 8 Pro RAW noise fallback is not registered" }
val fallbackProfile = checkNotNull(loadCalibratedProfile(fallbackInfo)) {
"Bundled Pixel 8 Pro RAW noise fallback could not be loaded"
}
RawNoiseProfileSelection.Camera2(fallbackProfile)
}
fun getAvailableProfiles(): List<RawNoiseProfileInfo> =
(BUILT_IN_PROFILES + customImportManager.getCustomRawNoiseProfiles())
@@ -33,13 +44,17 @@ class RawNoiseProfileManager(context: Context) {
fun resolveSelection(requestedId: String?): RawNoiseProfileSelection {
val id = requestedId?.takeIf { it.isNotBlank() } ?: DEFAULT_PROFILE_ID
if (id == SYSTEM_PROFILE_ID) return RawNoiseProfileSelection.Camera2
if (id == SYSTEM_PROFILE_ID) return systemCamera2Selection
val info = getAvailableProfiles().firstOrNull { it.id == id }
val calibrated = info?.let(::loadCalibratedProfile)
if (calibrated != null) return RawNoiseProfileSelection.Calibrated(calibrated, id)
PLog.w(TAG, "RAW noise profile unavailable: $id; using Camera2 system profile")
return RawNoiseProfileSelection.Camera2
PLog.w(
TAG,
"RAW noise profile unavailable: $id; using Camera2 system profile with " +
"$PIXEL8_PRO_PROFILE_ID fallback",
)
return systemCamera2Selection
}
private fun loadCalibratedProfile(info: RawNoiseProfileInfo): CalibratedRawNoiseProfile? {
@@ -7,6 +7,10 @@ import org.junit.Assert.assertTrue
import org.junit.Test
class RawNoiseModelTest {
private val systemSelection = RawNoiseProfileSelection.Camera2(
fallbackProfile = CalibratedRawNoiseProfile.MGC_GOOGLE_BLUELINE_REAR,
)
@Test
fun legacyNoiseModelReplicatesScalarCoefficientsAcrossBayerChannels() {
val model = RawNoiseModel.fromLegacyNoiseModel(floatArrayOf(1024f, 256f))
@@ -154,20 +158,11 @@ class RawNoiseModelTest {
3f, 30f,
4f, 40f,
)
val base = RawNoiseModel.fromCamera2NoiseProfile(
floatArrayOf(
11f, 110f,
12f, 120f,
13f, 130f,
14f, 140f,
),
)
val resolved = RawNoiseModelResolver.resolve(
selection = RawNoiseProfileSelection.Camera2,
selection = systemSelection,
sensitivity = 8000,
perFrameCamera2Profile = perFrame,
baseFrameCamera2Model = base,
)
assertEquals(RawNoiseModelSource.CAMERA2_PER_FRAME, resolved.source)
@@ -176,44 +171,21 @@ class RawNoiseModelTest {
}
@Test
fun defaultResolverFallsBackOnlyToExactBaseCamera2Model() {
val base = RawNoiseModel.fromCamera2NoiseProfile(
floatArrayOf(
1f, 10f,
2f, 20f,
3f, 30f,
4f, 40f,
),
)
fun defaultResolverUsesConfiguredFallbackWhenPerFrameProfileIsMissing() {
val resolved = RawNoiseModelResolver.resolve(
selection = RawNoiseProfileSelection.Camera2,
selection = systemSelection,
sensitivity = 8000,
perFrameCamera2Profile = null,
baseFrameCamera2Model = base,
)
val expected = checkNotNull(systemSelection.fallbackProfile.evaluate(8000))
assertEquals(RawNoiseModelSource.CAMERA2_BASE_FRAME, resolved.source)
assertArrayEquals(base.shotNoise, resolved.model.shotNoise, 0f)
assertArrayEquals(base.readNoise, resolved.model.readNoise, 0f)
assertEquals(RawNoiseModelSource.GCAM_SYSTEM_FALLBACK, resolved.source)
assertArrayEquals(expected.shotNoise, resolved.model.shotNoise, 0f)
assertArrayEquals(expected.readNoise, resolved.model.readNoise, 0f)
}
@Test
fun defaultResolverReportsUnavailableWhenCamera2ProfilesAreUnavailable() {
val resolved = RawNoiseModelResolver.resolve(
selection = RawNoiseProfileSelection.Camera2,
sensitivity = 8000,
perFrameCamera2Profile = null,
baseFrameCamera2Model = RawNoiseModel.EMPTY,
)
assertEquals(RawNoiseModelSource.UNAVAILABLE, resolved.source)
assertArrayEquals(FloatArray(4), resolved.model.shotNoise, 0f)
assertArrayEquals(FloatArray(4), resolved.model.readNoise, 0f)
}
@Test
fun defaultResolverReportsUnavailableWhenCamera2ReadTermsAreZero() {
fun defaultResolverUsesConfiguredFallbackWhenCamera2ReadTermsAreZero() {
val shotOnlyProfile = floatArrayOf(
1f, 0f,
2f, 0f,
@@ -222,15 +194,15 @@ class RawNoiseModelTest {
)
val resolved = RawNoiseModelResolver.resolve(
selection = RawNoiseProfileSelection.Camera2,
selection = systemSelection,
sensitivity = 800,
perFrameCamera2Profile = shotOnlyProfile,
baseFrameCamera2Model = RawNoiseModel.fromCamera2NoiseProfile(shotOnlyProfile),
)
assertEquals(RawNoiseModelSource.UNAVAILABLE, resolved.source)
assertArrayEquals(FloatArray(4), resolved.model.shotNoise, 0f)
assertArrayEquals(FloatArray(4), resolved.model.readNoise, 0f)
val expected = checkNotNull(systemSelection.fallbackProfile.evaluate(800))
assertEquals(RawNoiseModelSource.GCAM_SYSTEM_FALLBACK, resolved.source)
assertArrayEquals(expected.shotNoise, resolved.model.shotNoise, 0f)
assertArrayEquals(expected.readNoise, resolved.model.readNoise, 0f)
}
@Test
@@ -247,7 +219,6 @@ class RawNoiseModelTest {
),
sensitivity = 0,
perFrameCamera2Profile = camera2,
baseFrameCamera2Model = RawNoiseModel.fromCamera2NoiseProfile(camera2),
)
assertEquals(RawNoiseModelSource.UNAVAILABLE, resolved.source)
@@ -8,6 +8,10 @@ import org.junit.Assert.assertSame
import org.junit.Test
class RawMetadataNoiseProfileTest {
private val systemSelection = RawNoiseProfileSelection.Camera2(
fallbackProfile = CalibratedRawNoiseProfile.MGC_GOOGLE_BLUELINE_REAR,
)
@Test
fun camera2SelectionKeepsCompleteSensorNoiseProfile() {
val profile = floatArrayOf(
@@ -18,13 +22,13 @@ class RawMetadataNoiseProfileTest {
)
val metadata = metadata(profile)
val resolved = metadata.withNoiseProfileSelection(RawNoiseProfileSelection.Camera2)
val resolved = metadata.withNoiseProfileSelection(systemSelection)
assertSame(metadata, resolved)
}
@Test
fun camera2SelectionClearsNoiseProfileWhenReadTermsAreZero() {
fun camera2SelectionUsesConfiguredFallbackWhenReadTermsAreZero() {
val metadata = metadata(
floatArrayOf(
1f, 0f,
@@ -33,26 +37,24 @@ class RawMetadataNoiseProfileTest {
4f, 0f,
),
)
val resolved = metadata.withNoiseProfileSelection(RawNoiseProfileSelection.Camera2)
val resolved = metadata.withNoiseProfileSelection(systemSelection)
assertEquals(RawNoiseProfileLayout.NONE, resolved.noiseProfileLayout)
assertEquals(0, resolved.channelNoiseProfile.size)
assertSystemFallback(resolved)
}
@Test
fun camera2SelectionKeepsNoiseProfileUnavailableWhenSensorProfileIsMissing() {
fun camera2SelectionUsesConfiguredFallbackWhenSensorProfileIsMissing() {
val metadata = metadata(FloatArray(0)).copy(
noiseProfileLayout = RawNoiseProfileLayout.NONE,
)
val resolved = metadata.withNoiseProfileSelection(RawNoiseProfileSelection.Camera2)
val resolved = metadata.withNoiseProfileSelection(systemSelection)
assertEquals(RawNoiseProfileLayout.NONE, resolved.noiseProfileLayout)
assertEquals(0, resolved.channelNoiseProfile.size)
assertSystemFallback(resolved)
}
@Test
fun camera2SelectionClearsPersistedDngProfileWhenReadTermsAreZero() {
fun camera2SelectionUsesConfiguredFallbackForInvalidPersistedDngProfile() {
val metadata = metadata(
floatArrayOf(
1f, 0f,
@@ -60,10 +62,9 @@ class RawMetadataNoiseProfileTest {
3f, 0f,
),
).copy(noiseProfileLayout = RawNoiseProfileLayout.DNG_RGB)
val resolved = metadata.withNoiseProfileSelection(RawNoiseProfileSelection.Camera2)
val resolved = metadata.withNoiseProfileSelection(systemSelection)
assertEquals(RawNoiseProfileLayout.NONE, resolved.noiseProfileLayout)
assertEquals(0, resolved.channelNoiseProfile.size)
assertSystemFallback(resolved)
}
@Test
@@ -102,6 +103,18 @@ class RawMetadataNoiseProfileTest {
iso = TEST_ISO,
)
private fun assertSystemFallback(resolved: RawMetadata) {
val expected = checkNotNull(
systemSelection.fallbackProfile.evaluate(
sensitivity = resolved.iso,
minimumSensitivityIso = resolved.minimumSensitivityIso,
maximumAnalogSensitivityIso = resolved.maxAnalogSensitivity,
),
)
assertEquals(RawNoiseProfileLayout.CANONICAL_BAYER, resolved.noiseProfileLayout)
assertArrayEquals(expected.canonicalChannelPairs(), resolved.channelNoiseProfile, 0f)
}
companion object {
private const val TEST_ISO = 800
}