fix: EIS+ orientation
This commit is contained in:
1 parent
086469604d
commit
caf1474356
5 files changed
+134
-4
No files matched your search
@@ -6487,6 +6487,8 @@ class Camera2Controller(private val context: Context) {
|
||||
cameraTimestampSource = getActiveOpenCameraCharacteristics()
|
||||
?.get(CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE)
|
||||
?: CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN,
|
||||
sensorOrientationDegrees = _state.value.getCurrentCameraInfo()?.sensorOrientation
|
||||
?: cachedSensorOrientation,
|
||||
orientationHintDegrees = resolveDedicatedVideoOrientationHintDegrees(orientationOffsetDegrees),
|
||||
flipEncodedFrame = shouldFlipEncodedFrame,
|
||||
enhancedStabilization = useEnhancedStabilization,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
import android.graphics.SurfaceTexture
|
||||
import android.opengl.Matrix
|
||||
import kotlin.math.abs
|
||||
|
||||
@@ -8,6 +9,23 @@ internal class MgcEisPresentationTransform {
|
||||
val matrix = FloatArray(16).also { Matrix.setIdentityM(it, 0) }
|
||||
private var captured = false
|
||||
|
||||
/**
|
||||
* Initializes the ImageReader presentation transform without waiting for an OES frame.
|
||||
*
|
||||
* Video EIS+ intentionally does not attach the recorder's OES surface to Camera2: frames are
|
||||
* delivered through the timestamp-matched YUV ImageReader instead. Consequently the recorder
|
||||
* cannot discover the producer transform through [SurfaceTexture.getTransformMatrix]. Camera2
|
||||
* guarantees that SENSOR_ORIENTATION is cardinal, so it is sufficient to rotate the raw YUV
|
||||
* image into the same natural-device orientation used by the normal OES recording path.
|
||||
*/
|
||||
fun captureFromSensorOrientation(sensorOrientationDegrees: Int): Boolean {
|
||||
if (captured) return true
|
||||
val resolved = resolveMgcEisPresentationMatrix(sensorOrientationDegrees) ?: return false
|
||||
System.arraycopy(resolved, 0, matrix, 0, matrix.size)
|
||||
captured = true
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures the configured stream orientation once. Camera2 may update the
|
||||
* OES crop every frame, whereas its cardinal orientation is a stream
|
||||
@@ -54,3 +72,37 @@ internal class MgcEisPresentationTransform {
|
||||
captured = false
|
||||
}
|
||||
}
|
||||
|
||||
/** Maps natural-display coordinates to the unrotated Camera2 YUV texture. */
|
||||
internal fun resolveMgcEisPresentationMatrix(sensorOrientationDegrees: Int): FloatArray? {
|
||||
if (sensorOrientationDegrees % 90 != 0) return null
|
||||
val matrix = FloatArray(16).also {
|
||||
it[10] = 1f
|
||||
it[15] = 1f
|
||||
}
|
||||
when (Math.floorMod(sensorOrientationDegrees, 360)) {
|
||||
0 -> {
|
||||
matrix[0] = 1f
|
||||
matrix[5] = 1f
|
||||
}
|
||||
90 -> {
|
||||
// Clockwise display rotation: source = (1 - outputV, outputU).
|
||||
matrix[4] = -1f
|
||||
matrix[1] = 1f
|
||||
matrix[12] = 1f
|
||||
}
|
||||
180 -> {
|
||||
matrix[0] = -1f
|
||||
matrix[5] = -1f
|
||||
matrix[12] = 1f
|
||||
matrix[13] = 1f
|
||||
}
|
||||
270 -> {
|
||||
// Counter-clockwise display rotation: source = (outputV, 1 - outputU).
|
||||
matrix[4] = 1f
|
||||
matrix[1] = -1f
|
||||
matrix[13] = 1f
|
||||
}
|
||||
}
|
||||
return matrix
|
||||
}
|
||||
@@ -41,6 +41,7 @@ class RealtimeVideoRenderer(
|
||||
context: Context,
|
||||
private val cameraInputSize: Size,
|
||||
private val encoderOutputSize: Size,
|
||||
private val sensorOrientationDegrees: Int,
|
||||
colorLayers: List<VideoColorEffectLayer>,
|
||||
private val videoLogProfile: VideoLogProfile,
|
||||
private val mirrorHorizontally: Boolean,
|
||||
@@ -170,6 +171,14 @@ class RealtimeVideoRenderer(
|
||||
}
|
||||
cameraSurface = Surface(cameraSurfaceTexture)
|
||||
if (enhancedStabilizationEnabled) {
|
||||
check(
|
||||
mgcEisPresentationTransform.captureFromSensorOrientation(
|
||||
sensorOrientationDegrees,
|
||||
)
|
||||
) {
|
||||
"Camera reported non-cardinal sensor orientation $sensorOrientationDegrees"
|
||||
}
|
||||
updateCameraTransformGeometry(mgcEisPresentationTransform.matrix)
|
||||
stabilizationSession = stabilizationCoordinator?.createSession(
|
||||
useCase = StabilizationUseCase.VIDEO,
|
||||
strength = enhancedStabilizationStrength,
|
||||
@@ -184,6 +193,7 @@ class RealtimeVideoRenderer(
|
||||
TAG,
|
||||
"Initialized: camera=${cameraInputSize.width}x${cameraInputSize.height}, " +
|
||||
"encoder=${encoderOutputSize.width}x${encoderOutputSize.height}, " +
|
||||
"sensorOrientation=$sensorOrientationDegrees, " +
|
||||
"layers=${layers.size}, log=${videoLogProfile.name}, " +
|
||||
"enhancedStabilization=$enhancedStabilizationEnabled, " +
|
||||
"stabilizationStrength=$enhancedStabilizationStrength, " +
|
||||
@@ -312,7 +322,13 @@ class RealtimeVideoRenderer(
|
||||
) {
|
||||
PLog.w(TAG, "MGC cannot derive a cardinal presentation orientation yet")
|
||||
}
|
||||
updateCameraTransformGeometry()
|
||||
updateCameraTransformGeometry(
|
||||
if (enhancedStabilizationEnabled) {
|
||||
mgcEisPresentationTransform.matrix
|
||||
} else {
|
||||
stMatrix
|
||||
},
|
||||
)
|
||||
true
|
||||
} catch (error: RuntimeException) {
|
||||
PLog.e(TAG, "Failed to acquire realtime video frame", error)
|
||||
@@ -333,8 +349,8 @@ class RealtimeVideoRenderer(
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateCameraTransformGeometry() {
|
||||
val axesSwapped = isTextureTransformAxesSwapped(stMatrix)
|
||||
private fun updateCameraTransformGeometry(presentationMatrix: FloatArray) {
|
||||
val axesSwapped = isTextureTransformAxesSwapped(presentationMatrix)
|
||||
if (cameraTransformAxesSwapped == axesSwapped) return
|
||||
|
||||
cameraTransformAxesSwapped = axesSwapped
|
||||
@@ -349,7 +365,7 @@ class RealtimeVideoRenderer(
|
||||
"Camera texture transform: axesSwapped=$axesSwapped, " +
|
||||
"effective=${orientedInputSize.width}x${orientedInputSize.height}, " +
|
||||
"crop=${cameraCropRect.joinToString(prefix = "[", postfix = "]")}, " +
|
||||
"matrix=${stMatrix.joinToString(prefix = "[", postfix = "]")}"
|
||||
"matrix=${presentationMatrix.joinToString(prefix = "[", postfix = "]")}"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -130,6 +130,7 @@ class VideoRecorder(
|
||||
private var requestedEnhancedStabilizationLookahead = DEFAULT_VIDEO_STABILIZATION_LOOKAHEAD
|
||||
private var cameraInputStarted = false
|
||||
private var requestedCameraTimestampSource = CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN
|
||||
private var requestedSensorOrientationDegrees = 0
|
||||
private var cameraTimestampToBoottimeOffsetNs = UNSET_TIMESTAMP
|
||||
private var timelineOriginUs = UNSET_TIMESTAMP
|
||||
private var pauseIntervals = mutableListOf<PauseInterval>()
|
||||
@@ -189,6 +190,7 @@ class VideoRecorder(
|
||||
colorConfig: VideoEncoderColorRequest = VideoEncoderColorRequest(),
|
||||
colorLayers: List<VideoColorEffectLayer> = emptyList(),
|
||||
cameraTimestampSource: Int = CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN,
|
||||
sensorOrientationDegrees: Int = 0,
|
||||
orientationHintDegrees: Int = 0,
|
||||
flipEncodedFrame: Boolean = false,
|
||||
enhancedStabilization: Boolean = false,
|
||||
@@ -213,6 +215,7 @@ class VideoRecorder(
|
||||
requestedColorConfig = colorConfig
|
||||
requestedColorLayers = colorLayers.toList()
|
||||
requestedCameraTimestampSource = cameraTimestampSource
|
||||
requestedSensorOrientationDegrees = sensorOrientationDegrees
|
||||
requestedFlipEncodedFrame = flipEncodedFrame
|
||||
requestedEnhancedStabilization = enhancedStabilization
|
||||
requestedEnhancedStabilizationStrength =
|
||||
@@ -255,6 +258,7 @@ class VideoRecorder(
|
||||
"Video recording prepared: encoder=${requestedSize.width}x${requestedSize.height}, " +
|
||||
"output=${requestedOutputSize.width}x${requestedOutputSize.height} @ " +
|
||||
"${requestedFps}fps, orientationHint=$requestedOrientationHintDegrees, " +
|
||||
"sensorOrientation=$requestedSensorOrientationDegrees, " +
|
||||
"enhancedStabilization=$requestedEnhancedStabilization, " +
|
||||
"stabilizationStrength=$requestedEnhancedStabilizationStrength, " +
|
||||
"input=${if (requestedEnhancedStabilization) "timestamped-yuv" else "dedicated-surface-texture"}, " +
|
||||
@@ -354,6 +358,7 @@ class VideoRecorder(
|
||||
context = context,
|
||||
cameraInputSize = requestedCameraInputSize,
|
||||
encoderOutputSize = requestedSize,
|
||||
sensorOrientationDegrees = requestedSensorOrientationDegrees,
|
||||
colorLayers = requestedColorLayers,
|
||||
videoLogProfile = requestedColorConfig.logProfile,
|
||||
mirrorHorizontally =
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
@@ -32,4 +34,57 @@ class RealtimeVideoRendererTransformTest {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sensorOrientationBuildsRawYuvPresentationTransform() {
|
||||
assertMappedCorners(
|
||||
orientationDegrees = 0,
|
||||
bottomLeft = floatArrayOf(0f, 0f),
|
||||
bottomRight = floatArrayOf(1f, 0f),
|
||||
topLeft = floatArrayOf(0f, 1f),
|
||||
)
|
||||
assertMappedCorners(
|
||||
orientationDegrees = 90,
|
||||
bottomLeft = floatArrayOf(1f, 0f),
|
||||
bottomRight = floatArrayOf(1f, 1f),
|
||||
topLeft = floatArrayOf(0f, 0f),
|
||||
)
|
||||
assertMappedCorners(
|
||||
orientationDegrees = 180,
|
||||
bottomLeft = floatArrayOf(1f, 1f),
|
||||
bottomRight = floatArrayOf(0f, 1f),
|
||||
topLeft = floatArrayOf(1f, 0f),
|
||||
)
|
||||
assertMappedCorners(
|
||||
orientationDegrees = 270,
|
||||
bottomLeft = floatArrayOf(0f, 1f),
|
||||
bottomRight = floatArrayOf(0f, 0f),
|
||||
topLeft = floatArrayOf(1f, 1f),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sensorOrientationMustBeCardinal() {
|
||||
assertNull(resolveMgcEisPresentationMatrix(45))
|
||||
}
|
||||
|
||||
private fun assertMappedCorners(
|
||||
orientationDegrees: Int,
|
||||
bottomLeft: FloatArray,
|
||||
bottomRight: FloatArray,
|
||||
topLeft: FloatArray,
|
||||
) {
|
||||
val matrix = requireNotNull(resolveMgcEisPresentationMatrix(orientationDegrees))
|
||||
assertArrayEquals(bottomLeft, map(matrix, 0f, 0f), 0f)
|
||||
assertArrayEquals(bottomRight, map(matrix, 1f, 0f), 0f)
|
||||
assertArrayEquals(topLeft, map(matrix, 0f, 1f), 0f)
|
||||
assertTrue(isTextureTransformAxesSwapped(matrix) == (orientationDegrees % 180 != 0))
|
||||
}
|
||||
|
||||
private fun map(matrix: FloatArray, x: Float, y: Float): FloatArray {
|
||||
return floatArrayOf(
|
||||
matrix[0] * x + matrix[4] * y + matrix[12],
|
||||
matrix[1] * x + matrix[5] * y + matrix[13],
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user