feat: yuv hdr capture timestamp
This commit is contained in:
1 parent
8ad536a75f
commit
e63ec50be4
2 files changed
+106
-37
No files matched your search
@@ -256,6 +256,49 @@ class Camera2Controller(private val context: Context) {
|
||||
return result.get(CaptureResult.SENSOR_TIMESTAMP) ?: startedTimestamp
|
||||
}
|
||||
|
||||
private fun shouldPairImageWithCaptureResult(image: SafeImage): Boolean {
|
||||
return image.format == ImageFormat.RAW_SENSOR || _state.value.hdrBracketCapturing
|
||||
}
|
||||
|
||||
private fun processOrBufferImageForCaptureResult(image: SafeImage) {
|
||||
val timestamp = image.timestamp
|
||||
val pendingResult = pendingResults.remove(timestamp)
|
||||
if (pendingResult != null) {
|
||||
processAndTriggerCapture(image, pendingResult)
|
||||
} else {
|
||||
pendingImages.put(timestamp, image)?.close()
|
||||
trimPendingImages()
|
||||
}
|
||||
}
|
||||
|
||||
private fun processOrBufferCaptureResult(result: TotalCaptureResult) {
|
||||
val timestamp = getCaptureTimestamp(result)
|
||||
if (timestamp == null) {
|
||||
PLog.w(TAG, "Capture result missing timestamp, frame=${result.frameNumber}")
|
||||
return
|
||||
}
|
||||
|
||||
val pendingImage = pendingImages.remove(timestamp)
|
||||
if (pendingImage != null) {
|
||||
processAndTriggerCapture(pendingImage, result)
|
||||
} else {
|
||||
pendingResults[timestamp] = result
|
||||
trimPendingResults()
|
||||
}
|
||||
}
|
||||
|
||||
private fun trimPendingImages(maxSize: Int = 20) {
|
||||
if (pendingImages.size <= maxSize) return
|
||||
val oldestKey = pendingImages.keys.minOrNull() ?: return
|
||||
pendingImages.remove(oldestKey)?.close()
|
||||
}
|
||||
|
||||
private fun trimPendingResults(maxSize: Int = 20) {
|
||||
if (pendingResults.size <= maxSize) return
|
||||
val oldestKey = pendingResults.keys.minOrNull() ?: return
|
||||
pendingResults.remove(oldestKey)
|
||||
}
|
||||
|
||||
// 快门音效播放回调
|
||||
var onPlayShutterSound: (() -> Unit)? = null
|
||||
|
||||
@@ -951,20 +994,8 @@ class Camera2Controller(private val context: Context) {
|
||||
}
|
||||
val image = trackImage(rawImage)
|
||||
if (image != null) {
|
||||
if (image.image.format == ImageFormat.RAW_SENSOR) {
|
||||
val timestamp = image.timestamp
|
||||
val pendingResult = pendingResults.remove(timestamp)
|
||||
if (pendingResult != null) {
|
||||
processAndTriggerCapture(image, pendingResult)
|
||||
} else {
|
||||
pendingImages[timestamp] = image
|
||||
if (pendingImages.size > 20) {
|
||||
val oldestKey = pendingImages.keys.minOrNull()
|
||||
if (oldestKey != null) {
|
||||
pendingImages.remove(oldestKey)?.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (shouldPairImageWithCaptureResult(image)) {
|
||||
processOrBufferImageForCaptureResult(image)
|
||||
} else {
|
||||
processAndTriggerCapture(image, null)
|
||||
}
|
||||
@@ -1526,8 +1557,9 @@ class Camera2Controller(private val context: Context) {
|
||||
|
||||
private fun setZslDisabledIfSupported(builder: CaptureRequest.Builder) {
|
||||
if (!isZslControlAvailable()) return
|
||||
|
||||
builder.set(CaptureRequest.CONTROL_ENABLE_ZSL, false)
|
||||
if (_state.value.useHdrComposition && !_state.value.useRaw) {
|
||||
builder.set(CaptureRequest.CONTROL_ENABLE_ZSL, false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isZslControlAvailable(): Boolean {
|
||||
@@ -3612,11 +3644,11 @@ class Camera2Controller(private val context: Context) {
|
||||
val currentState = _state.value
|
||||
val manualBaseExposure = resolveHdrBracketManualBaseExposure(currentState)
|
||||
val evOffsets = buildList {
|
||||
add(-2f)
|
||||
add(-2.5f)
|
||||
repeat(normalizedZeroEvFrameCount) {
|
||||
add(0f)
|
||||
}
|
||||
add(2f)
|
||||
add(2.5f)
|
||||
}
|
||||
val requests = evOffsets.mapIndexed { index, evOffset ->
|
||||
device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply {
|
||||
@@ -3640,9 +3672,7 @@ class Camera2Controller(private val context: Context) {
|
||||
timestamp: Long,
|
||||
frameNumber: Long
|
||||
) {
|
||||
if (isRawCapture) {
|
||||
pendingCaptureStartedTimestamps[frameNumber] = timestamp
|
||||
}
|
||||
pendingCaptureStartedTimestamps[frameNumber] = timestamp
|
||||
PLog.d(TAG, "HDR bracket capture started: frame=$frameNumber")
|
||||
}
|
||||
|
||||
@@ -3651,15 +3681,7 @@ class Camera2Controller(private val context: Context) {
|
||||
request: CaptureRequest,
|
||||
result: TotalCaptureResult
|
||||
) {
|
||||
val timestamp = getCaptureTimestamp(result)
|
||||
if (timestamp != null && isRawCapture) {
|
||||
val pendingImage = pendingImages.remove(timestamp)
|
||||
if (pendingImage != null) {
|
||||
processAndTriggerCapture(pendingImage, result)
|
||||
} else {
|
||||
pendingResults[timestamp] = result
|
||||
}
|
||||
}
|
||||
processOrBufferCaptureResult(result)
|
||||
lastCaptureResult = result
|
||||
}
|
||||
|
||||
|
||||
@@ -203,6 +203,18 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
private const val HDR_BRACKET_FRAME_COUNT = 3
|
||||
}
|
||||
|
||||
private data class HdrBracketFrame(
|
||||
val image: SafeImage,
|
||||
val captureResult: CaptureResult?,
|
||||
val originalIndex: Int,
|
||||
val timestamp: Long
|
||||
)
|
||||
|
||||
private data class HdrBracketFrameOrder(
|
||||
val images: List<SafeImage>,
|
||||
val captureResults: List<CaptureResult?>
|
||||
)
|
||||
|
||||
private val cameraController = Camera2Controller(application)
|
||||
|
||||
|
||||
@@ -4206,10 +4218,13 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
characteristics: CameraCharacteristics?,
|
||||
captureResult: CaptureResult?
|
||||
) {
|
||||
if (images.firstOrNull()?.format?.let(::isRawCaptureFormat) == true) {
|
||||
val orderedFrames = orderHdrBracketFramesByTimestamp(images, captureResults)
|
||||
val orderedImages = orderedFrames.images
|
||||
val orderedCaptureResults = orderedFrames.captureResults
|
||||
if (orderedImages.firstOrNull()?.format?.let(::isRawCaptureFormat) == true) {
|
||||
processRawHdrBracket(
|
||||
images = images,
|
||||
captureResults = captureResults,
|
||||
images = orderedImages,
|
||||
captureResults = orderedCaptureResults,
|
||||
zeroEvFrameCount = zeroEvFrameCount,
|
||||
expectedFrameCount = expectedFrameCount,
|
||||
captureInfo = captureInfo,
|
||||
@@ -4220,14 +4235,14 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
var imagesHandedToGallery = false
|
||||
try {
|
||||
if (images.size != expectedFrameCount) return
|
||||
if (orderedImages.size != expectedFrameCount) return
|
||||
val context = getApplication<Application>()
|
||||
val shouldAutoSave = autoSaveAfterCapture.firstOrNull() ?: false
|
||||
val sharpeningValue = sharpening.firstOrNull() ?: 0f
|
||||
val noiseReductionValue = noiseReduction.firstOrNull() ?: 0f
|
||||
val chromaNoiseReductionValue = chromaNoiseReduction.firstOrNull() ?: 0f
|
||||
val photoQualityValue = photoQuality.firstOrNull() ?: 95
|
||||
val baseImage = images[1]
|
||||
val baseImage = orderedImages[1]
|
||||
val useSuperRes = state.value.useMFSR
|
||||
val superResScale = if (useSuperRes) 2f else 1.0f
|
||||
val captureMode = if (zeroEvFrameCount > 1) "hdr_mfnr" else "hdr_bracket"
|
||||
@@ -4261,7 +4276,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
var fusedBitmap: Bitmap? = null
|
||||
try {
|
||||
fusedBitmap = GalleryManager.composeHdrBracketPhoto(
|
||||
images = images,
|
||||
images = orderedImages,
|
||||
rotation = metadata.rotation,
|
||||
aspectRatio = aspectRatio,
|
||||
shouldMirror = metadata.isMirrored,
|
||||
@@ -4295,11 +4310,43 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
PLog.e(TAG, "Failed to process HDR bracket", e)
|
||||
} finally {
|
||||
if (!imagesHandedToGallery) {
|
||||
images.forEach { it.close() }
|
||||
orderedImages.forEach { it.close() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun orderHdrBracketFramesByTimestamp(
|
||||
images: List<SafeImage>,
|
||||
captureResults: List<CaptureResult?>
|
||||
): HdrBracketFrameOrder {
|
||||
val frames = images.mapIndexed { index, image ->
|
||||
val result = captureResults.getOrNull(index)
|
||||
HdrBracketFrame(
|
||||
image = image,
|
||||
captureResult = result,
|
||||
originalIndex = index,
|
||||
timestamp = result?.get(CaptureResult.SENSOR_TIMESTAMP) ?: image.timestamp
|
||||
)
|
||||
}
|
||||
val sortedFrames = frames.sortedWith(
|
||||
compareBy<HdrBracketFrame> { it.timestamp }
|
||||
.thenBy { it.originalIndex }
|
||||
)
|
||||
val sortedOrder = sortedFrames.map { it.originalIndex }
|
||||
val originalOrder = frames.map { it.originalIndex }
|
||||
if (sortedOrder != originalOrder) {
|
||||
PLog.d(
|
||||
TAG,
|
||||
"HDR bracket frames reordered by timestamp: " +
|
||||
sortedFrames.joinToString { "${it.originalIndex}:${it.timestamp}" }
|
||||
)
|
||||
}
|
||||
return HdrBracketFrameOrder(
|
||||
images = sortedFrames.map { it.image },
|
||||
captureResults = sortedFrames.map { it.captureResult }
|
||||
)
|
||||
}
|
||||
|
||||
private fun resetHdrBracketCapture(closeImages: Boolean) {
|
||||
if (closeImages) {
|
||||
hdrBracketImages.forEach { it.close() }
|
||||
|
||||
Reference in new issue
Block a user