fix: long exposure metering

This commit is contained in:
Hinnka committed 2026-09-19 10:21:03 +08:00
1 parent 55448c6083
commit bb629b841c
4 files changed
+52 -19

No files matched your search

@@ -2916,24 +2916,26 @@ class Camera2Controller(private val context: Context) {
// 保护:如果画面全黑,避免除以0或Log错误
if (rawAvgLuminance < 1.0) return
// --- 2. 关键修复:预览流亮度补偿 ---
// 预览流的曝光时间被帧率限制了(比如最长只能 33ms)
// 但实际拍摄参数可能是 100ms。我们需要推算“如果预览流能曝光 100ms,亮度会是多少”
// --- 2. 按自动调整的参数选择测光域 ---
val currentShutter = currentState.shutterSpeed
val clampedPreviewTime = currentShutter.coerceAtMost(getMaxPreviewExposureTime(currentState))
// 补偿系数:如果当前设定快门是 66ms,预览限制是 33ms,那么真实亮度应该是预览亮度的 2 倍
val exposureRatio = currentShutter.toDouble() / clampedPreviewTime.toDouble()
// 【修正】使用补偿后的亮度来与目标值对比
val estimatedRealLuminance = rawAvgLuminance * exposureRatio
// 手动快门、自动 ISO:以实际预览亮度为目标。超过预览快门上限后,
// 不能用拍摄快门放大测光亮度,否则会持续降低 ISO,反而压暗取景器。
// 手动 ISO、自动快门:仍需换算拍摄亮度,才能在预览曝光不再变化时
// 求出正确的长曝光快门,而不是一直增加到传感器上限。
val exposureRatio = if (currentState.isShutterSpeedAuto) {
currentShutter.toDouble() / clampedPreviewTime.toDouble()
} else {
1.0
}
val meteredLuminance = rawAvgLuminance * exposureRatio
val targetLuminance = 128.0 // Target (Gamma Corrected 18% Gray)
// --- 3. 计算 EV 误差 ---
// 使用 Log2 计算差了多少档光圈 (Stops)
// 这是一个更符合人眼和相机光学的度量方式
val evErrorStops = ln(targetLuminance / estimatedRealLuminance) / ln(2.0)
val evErrorStops = ln(targetLuminance / meteredLuminance) / ln(2.0)
// --- 4. 稳定性控制 (Deadband) ---
// 如果误差在 +/- 0.3 EV (约 1/3 档) 以内,认为曝光准确,不调整
@@ -2955,7 +2957,7 @@ class Camera2Controller(private val context: Context) {
var needsUpdate = false
if (currentState.isIsoAuto) {
// ISO 优先模式:快门固定,调 ISO
// 快门优先模式:快门固定,调 ISO
val calculatedIso = (currentState.iso * correctionFactor).toInt()
val range = currentState.getIsoRange()
val clampedIso = calculatedIso.coerceIn(range.lower, range.upper)
@@ -2966,7 +2968,7 @@ class Camera2Controller(private val context: Context) {
needsUpdate = true
}
} else {
// 快门优先模式:ISO 固定,调快门
// ISO 优先模式:ISO 固定,调快门
val calculatedShutter = (currentState.shutterSpeed * correctionFactor).toLong()
val range = currentState.getManualShutterSpeedRange()
val clampedShutter = calculatedShutter.coerceIn(range.lower, range.upper)
@@ -2979,6 +2981,15 @@ class Camera2Controller(private val context: Context) {
// --- 7. 下发指令 ---
if (needsUpdate) {
PLog.d(
TAG,
"SEMI_AUTO_METERING isoAuto=${currentState.isIsoAuto} " +
"previewLuma=$rawAvgLuminance meteredLuma=$meteredLuminance " +
"exposureRatio=$exposureRatio evError=$evErrorStops " +
"previewShutterNs=$clampedPreviewTime " +
"captureShutterNs=$currentShutter->$newShutter " +
"iso=${currentState.iso}->$newIso",
)
// 更新状态
_state.value = currentState.copy(iso = newIso, shutterSpeed = newShutter)
@@ -1370,7 +1370,7 @@ internal class DngPhotonProfileGainTableAlgorithm {
val statsBounds: Rect?,
/** Total BaselineExposure used by DNG ProfileGainTableMap lookup. */
val rendererBaselineExposureEv: Float,
/** Complete capture-time viewfinder target; null when regenerating without capture state. */
/** Complete viewfinder target; null for captures beyond the preview shutter limit or regeneration. */
val viewfinderReference: RawLegacyExposurePreviewFrame?,
/** Rotation that maps the sensor/model grid into [viewfinderReference] orientation. */
val outputRotation: Int,
@@ -3,8 +3,9 @@ package com.hinnka.mycamera.raw
/**
* The mutually exclusive capture/development modes for adaptive RAW exposure handling.
*
* All modes preserve capture-time viewfinder brightness through BaselineExposure. [PHOTON_HDR]
* runs ML AE plus HDRNet and spatially matches the HDRNet result to the complete viewfinder.
* [PHOTON_HDR] runs ML AE plus HDRNet and spatially matches the result to the complete viewfinder
* only when the capture shutter does not exceed the preview limit. Longer exposures retain the
* ML AE result without viewfinder matching. Other modes match through BaselineExposure.
* [LEGACY_AUTO_EXPOSURE] also generates the historical Local Laplacian profile gain table. [OFF]
* applies no HDR mapping and constrains its scalar viewfinder match with a RAW highlight guard.
*/
@@ -18,6 +18,7 @@ import android.opengl.GLES31
import android.util.Half
import androidx.core.graphics.createBitmap
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.CameraState
import com.hinnka.mycamera.camera.RawBlackBorderCrop
import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.lut.ChromaDenoiseAlgorithm
@@ -3295,6 +3296,18 @@ class RawDemosaicProcessor {
if (prepareCaptureProfile) {
val captureUsesHdrNet = sceneExposureRequest != null
// The preview cannot represent the captured brightness beyond its shutter cap.
// Keep ML AE's exposure when HDRNet develops a longer physical exposure.
val skipHdrNetViewfinderMatch = captureUsesHdrNet &&
actualMetadata.shutterSpeed > CameraState.MAX_PHOTO_PREVIEW_SHUTTER_SPEED_NS
if (skipHdrNetViewfinderMatch) {
PLog.i(
TAG,
"HDRNET_MATCH stage=SKIPPED reason=CAPTURE_SHUTTER_EXCEEDS_PREVIEW_LIMIT " +
"captureShutterNs=${actualMetadata.shutterSpeed} " +
"previewMaxShutterNs=${CameraState.MAX_PHOTO_PREVIEW_SHUTTER_SPEED_NS}",
)
}
val captureUsesViewfinderBrightnessMatch =
sceneExposureRequest == null && legacyAutoExposureRequest != null
val captureUsesLocalLaplacian =
@@ -3371,7 +3384,8 @@ class RawDemosaicProcessor {
?.takeIf { it.isFinite() && it > 0f }
val initialCaptureProfileOutput = when {
capturePhotonPgtmRequested && captureUsesHdrNet &&
legacyAutoExposureRequest != null && captureHdrRatio != null &&
(skipHdrNetViewfinderMatch || legacyAutoExposureRequest != null) &&
captureHdrRatio != null &&
captureSourceToShortGain != null -> {
generateProfileGainTableMapOnGpu(
mode = DngPhotonProfileGainTableAlgorithm.Mode.HDR_NET,
@@ -3389,7 +3403,9 @@ class RawDemosaicProcessor {
statsBounds = processingBounds,
rendererBaselineExposureEv = initialBaselineExposureEv +
dcpBaselineExposureOffsetEv,
viewfinderReference = legacyAutoExposureRequest.referenceFrame,
viewfinderReference = legacyAutoExposureRequest
?.takeUnless { skipHdrNetViewfinderMatch }
?.referenceFrame,
outputRotation = actualRotation,
// HDRNet's ratio describes capture exposure, not the DCP profile's
// independent BaselineExposureOffset rendering adjustment.
@@ -3528,8 +3544,13 @@ class RawDemosaicProcessor {
buildString {
append(summary.trimEnd())
appendLine()
appendLine("hdrNetExposureTarget=FULL_CAPTURE_VIEWFINDER_THUMBNAIL")
appendLine("hdrNetExposureGrid=8x6_WEIGHTED_DIRECT_LOG2_EV")
if (skipHdrNetViewfinderMatch) {
appendLine("hdrNetExposureTarget=ML_AE")
appendLine("hdrNetViewfinderMatchSkipped=CAPTURE_SHUTTER_EXCEEDS_PREVIEW_LIMIT")
} else {
appendLine("hdrNetExposureTarget=FULL_CAPTURE_VIEWFINDER_THUMBNAIL")
appendLine("hdrNetExposureGrid=8x6_WEIGHTED_DIRECT_LOG2_EV")
}
appendLine("hdrNetFinalShortGain=$outputShortGain")
appendLine("hdrNetFinalLongGain=${outputShortGain * outputHdrRatio}")
appendLine("hdrNetFinalHdrRatio=$outputHdrRatio")