From 2bdda357318304135e811630f238cf73f57fb2c8 Mon Sep 17 00:00:00 2001 From: huangdarwin Date: Fri, 1 Sep 2023 11:25:58 -0700 Subject: [PATCH] Overlay: Rename videoFrameAnchor to backgroundFrameAnchor. Overlays may be overlaid over: * In VideoFrameProcessor, videos or images (or texture input). * In Compositor, other videos. In Compositor, Overlays may consist of video, so it could be confusing for videoFrameAnchor to contrast with overlayAnchor. Also, rename overlayAnchor to overlayFrameAnchor, since it's modifying the anchor in the overlay's frame, so this name seems slightly more precise. PiperOrigin-RevId: 562004292 --- .../media3/demo/transformer/TimerOverlay.java | 4 +- .../demo/transformer/TransformerActivity.java | 4 +- .../effect/OverlayShaderProgramPixelTest.java | 15 ++--- .../media3/effect/OverlayMatrixProvider.java | 38 ++++++------- .../media3/effect/OverlaySettings.java | 56 +++++++++---------- .../effect/SamplerOverlayMatrixProvider.java | 6 +- .../DefaultVideoCompositorPixelTest.java | 10 ++-- 7 files changed, 67 insertions(+), 66 deletions(-) diff --git a/demos/transformer/src/main/java/androidx/media3/demo/transformer/TimerOverlay.java b/demos/transformer/src/main/java/androidx/media3/demo/transformer/TimerOverlay.java index f12aa024e0..17cdbf4eac 100644 --- a/demos/transformer/src/main/java/androidx/media3/demo/transformer/TimerOverlay.java +++ b/demos/transformer/src/main/java/androidx/media3/demo/transformer/TimerOverlay.java @@ -38,8 +38,8 @@ import java.util.Locale; new OverlaySettings.Builder() // Place the timer in the bottom left corner of the screen with some padding from the // edges. - .setOverlayAnchor(/* x= */ 1f, /* y= */ 1f) - .setVideoFrameAnchor(/* x= */ -0.7f, /* y= */ -0.95f) + .setOverlayFrameAnchor(/* x= */ 1f, /* y= */ 1f) + .setBackgroundFrameAnchor(/* x= */ -0.7f, /* y= */ -0.95f) .build(); } diff --git a/demos/transformer/src/main/java/androidx/media3/demo/transformer/TransformerActivity.java b/demos/transformer/src/main/java/androidx/media3/demo/transformer/TransformerActivity.java index 61a23601dc..fa682920f9 100644 --- a/demos/transformer/src/main/java/androidx/media3/demo/transformer/TransformerActivity.java +++ b/demos/transformer/src/main/java/androidx/media3/demo/transformer/TransformerActivity.java @@ -599,8 +599,8 @@ public final class TransformerActivity extends AppCompatActivity { new OverlaySettings.Builder() // Place the logo in the bottom left corner of the screen with some padding from the // edges. - .setOverlayAnchor(/* x= */ 1f, /* y= */ 1f) - .setVideoFrameAnchor(/* x= */ -0.95f, /* y= */ -0.95f) + .setOverlayFrameAnchor(/* x= */ 1f, /* y= */ 1f) + .setBackgroundFrameAnchor(/* x= */ -0.95f, /* y= */ -0.95f) .build(); Drawable logo; try { diff --git a/libraries/effect/src/androidTest/java/androidx/media3/effect/OverlayShaderProgramPixelTest.java b/libraries/effect/src/androidTest/java/androidx/media3/effect/OverlayShaderProgramPixelTest.java index a9a46867ee..baf5d32334 100644 --- a/libraries/effect/src/androidTest/java/androidx/media3/effect/OverlayShaderProgramPixelTest.java +++ b/libraries/effect/src/androidTest/java/androidx/media3/effect/OverlayShaderProgramPixelTest.java @@ -177,8 +177,8 @@ public class OverlayShaderProgramPixelTest { Bitmap overlayBitmap = readBitmap(OVERLAY_PNG_ASSET_PATH); OverlaySettings overlaySettings = new OverlaySettings.Builder() - .setOverlayAnchor(/* x= */ 1f, /* y= */ -1f) - .setVideoFrameAnchor(/* x= */ -1f, /* y= */ 1f) + .setOverlayFrameAnchor(/* x= */ 1f, /* y= */ -1f) + .setBackgroundFrameAnchor(/* x= */ -1f, /* y= */ 1f) .build(); BitmapOverlay staticBitmapOverlay = BitmapOverlay.createStaticBitmapOverlay(overlayBitmap, overlaySettings); @@ -201,11 +201,12 @@ public class OverlayShaderProgramPixelTest { @Test @RequiresNonNull("testId") - public void drawFrame_overlayAnchoredOnlyBitmapOverlay_anchorsOverlayFromTopLeftCornerOfFrame() - throws Exception { + public void + drawFrame_overlayFrameAnchoredOnlyBitmapOverlay_anchorsOverlayFromTopLeftCornerOfFrame() + throws Exception { Bitmap overlayBitmap = readBitmap(OVERLAY_PNG_ASSET_PATH); OverlaySettings overlaySettings = - new OverlaySettings.Builder().setOverlayAnchor(/* x= */ 1f, /* y= */ -1f).build(); + new OverlaySettings.Builder().setOverlayFrameAnchor(/* x= */ 1f, /* y= */ -1f).build(); BitmapOverlay staticBitmapOverlay = BitmapOverlay.createStaticBitmapOverlay(overlayBitmap, overlaySettings); overlayShaderProgram = @@ -367,7 +368,7 @@ public class OverlayShaderProgramPixelTest { /* end= */ 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); OverlaySettings overlaySettings = - new OverlaySettings.Builder().setVideoFrameAnchor(0.5f, 0.5f).build(); + new OverlaySettings.Builder().setBackgroundFrameAnchor(0.5f, 0.5f).build(); TextOverlay staticTextOverlay = TextOverlay.createStaticTextOverlay(overlayText, overlaySettings); overlayShaderProgram = @@ -397,7 +398,7 @@ public class OverlayShaderProgramPixelTest { /* end= */ 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); OverlaySettings overlaySettings1 = - new OverlaySettings.Builder().setVideoFrameAnchor(0.5f, 0.5f).build(); + new OverlaySettings.Builder().setBackgroundFrameAnchor(0.5f, 0.5f).build(); TextOverlay textOverlay = TextOverlay.createStaticTextOverlay(overlayText, overlaySettings1); Bitmap bitmap = readBitmap(OVERLAY_PNG_ASSET_PATH); OverlaySettings overlaySettings2 = new OverlaySettings.Builder().setAlphaScale(0.5f).build(); diff --git a/libraries/effect/src/main/java/androidx/media3/effect/OverlayMatrixProvider.java b/libraries/effect/src/main/java/androidx/media3/effect/OverlayMatrixProvider.java index 91df8d8205..366517405c 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/OverlayMatrixProvider.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/OverlayMatrixProvider.java @@ -26,11 +26,11 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** Provides a matrix for {@link OverlaySettings}, to be applied on a vertex. */ /* package */ class OverlayMatrixProvider { protected static final int MATRIX_OFFSET = 0; - private final float[] videoFrameAnchorMatrix; + private final float[] backgroundFrameAnchorMatrix; private final float[] aspectRatioMatrix; private final float[] scaleMatrix; private final float[] scaleMatrixInv; - private final float[] overlayAnchorMatrix; + private final float[] overlayFrameAnchorMatrix; private final float[] rotateMatrix; private final float[] overlayAspectRatioMatrix; private final float[] overlayAspectRatioMatrixInv; @@ -39,8 +39,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; public OverlayMatrixProvider() { aspectRatioMatrix = GlUtil.create4x4IdentityMatrix(); - videoFrameAnchorMatrix = GlUtil.create4x4IdentityMatrix(); - overlayAnchorMatrix = GlUtil.create4x4IdentityMatrix(); + backgroundFrameAnchorMatrix = GlUtil.create4x4IdentityMatrix(); + overlayFrameAnchorMatrix = GlUtil.create4x4IdentityMatrix(); rotateMatrix = GlUtil.create4x4IdentityMatrix(); scaleMatrix = GlUtil.create4x4IdentityMatrix(); scaleMatrixInv = GlUtil.create4x4IdentityMatrix(); @@ -62,12 +62,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; reset(); // Anchor point of overlay within output frame. - Pair videoFrameAnchor = overlaySettings.videoFrameAnchor; + Pair backgroundFrameAnchor = overlaySettings.backgroundFrameAnchor; Matrix.translateM( - videoFrameAnchorMatrix, + backgroundFrameAnchorMatrix, MATRIX_OFFSET, - videoFrameAnchor.first, - videoFrameAnchor.second, + backgroundFrameAnchor.first, + backgroundFrameAnchor.second, /* z= */ 0f); checkStateNotNull(backgroundSize); @@ -83,14 +83,14 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; Matrix.scaleM(scaleMatrix, MATRIX_OFFSET, scale.first, scale.second, /* z= */ 1f); Matrix.invertM(scaleMatrixInv, MATRIX_OFFSET, scaleMatrix, MATRIX_OFFSET); - // Translate the overlay within its frame. To position the overlay's anchor at the correct + // Translate the overlay within its frame. To position the overlay frame's anchor at the correct // position, it must be translated the opposite direction by the same magnitude. - Pair overlayAnchor = overlaySettings.overlayAnchor; + Pair overlayFrameAnchor = overlaySettings.overlayFrameAnchor; Matrix.translateM( - overlayAnchorMatrix, + overlayFrameAnchorMatrix, MATRIX_OFFSET, - -1 * overlayAnchor.first, - -1 * overlayAnchor.second, + -1 * overlayFrameAnchor.first, + -1 * overlayFrameAnchor.second, /* z= */ 0f); // Rotate the image. @@ -112,8 +112,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; Matrix.invertM( overlayAspectRatioMatrixInv, MATRIX_OFFSET, overlayAspectRatioMatrix, MATRIX_OFFSET); - // transformationMatrix = videoFrameAnchorMatrix * aspectRatioMatrix - // * scaleMatrix * overlayAnchorMatrix * scaleMatrixInv + // transformationMatrix = backgroundFrameAnchorMatrix * aspectRatioMatrix + // * scaleMatrix * overlayFrameAnchorMatrix * scaleMatrixInv // * overlayAspectRatioMatrix * rotateMatrix * overlayAspectRatioMatrixInv // * scaleMatrix. @@ -123,7 +123,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; MATRIX_OFFSET, transformationMatrix, MATRIX_OFFSET, - videoFrameAnchorMatrix, + backgroundFrameAnchorMatrix, MATRIX_OFFSET); // Correct for aspect ratio of image in output frame. @@ -147,7 +147,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; MATRIX_OFFSET, transformationMatrix, MATRIX_OFFSET, - overlayAnchorMatrix, + overlayFrameAnchorMatrix, MATRIX_OFFSET); Matrix.multiplyMM( transformationMatrix, @@ -194,8 +194,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; private void reset() { GlUtil.setToIdentity(aspectRatioMatrix); - GlUtil.setToIdentity(videoFrameAnchorMatrix); - GlUtil.setToIdentity(overlayAnchorMatrix); + GlUtil.setToIdentity(backgroundFrameAnchorMatrix); + GlUtil.setToIdentity(overlayFrameAnchorMatrix); GlUtil.setToIdentity(scaleMatrix); GlUtil.setToIdentity(scaleMatrixInv); GlUtil.setToIdentity(rotateMatrix); diff --git a/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java b/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java index 97d9d17186..266b079454 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/OverlaySettings.java @@ -30,22 +30,22 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; public final class OverlaySettings { public final boolean useHdr; public final float alphaScale; - public final Pair videoFrameAnchor; - public final Pair overlayAnchor; + public final Pair backgroundFrameAnchor; + public final Pair overlayFrameAnchor; public final Pair scale; public final float rotationDegrees; private OverlaySettings( boolean useHdr, float alphaScale, - Pair videoFrameAnchor, - Pair overlayAnchor, + Pair backgroundFrameAnchor, + Pair overlayFrameAnchor, Pair scale, float rotationDegrees) { this.useHdr = useHdr; this.alphaScale = alphaScale; - this.videoFrameAnchor = videoFrameAnchor; - this.overlayAnchor = overlayAnchor; + this.backgroundFrameAnchor = backgroundFrameAnchor; + this.overlayFrameAnchor = overlayFrameAnchor; this.scale = scale; this.rotationDegrees = rotationDegrees; } @@ -59,16 +59,16 @@ public final class OverlaySettings { public static final class Builder { private boolean useHdr; private float alphaScale; - private Pair videoFrameAnchor; - private Pair overlayAnchor; + private Pair backgroundFrameAnchor; + private Pair overlayFrameAnchor; private Pair scale; private float rotationDegrees; /** Creates a new {@link Builder}. */ public Builder() { alphaScale = 1f; - videoFrameAnchor = Pair.create(0f, 0f); - overlayAnchor = Pair.create(0f, 0f); + backgroundFrameAnchor = Pair.create(0f, 0f); + overlayFrameAnchor = Pair.create(0f, 0f); scale = Pair.create(1f, 1f); rotationDegrees = 0f; } @@ -76,8 +76,8 @@ public final class OverlaySettings { private Builder(OverlaySettings overlaySettings) { this.useHdr = overlaySettings.useHdr; this.alphaScale = overlaySettings.alphaScale; - this.videoFrameAnchor = overlaySettings.videoFrameAnchor; - this.overlayAnchor = overlaySettings.overlayAnchor; + this.backgroundFrameAnchor = overlaySettings.backgroundFrameAnchor; + this.overlayFrameAnchor = overlaySettings.overlayFrameAnchor; this.scale = overlaySettings.scale; this.rotationDegrees = overlaySettings.rotationDegrees; } @@ -109,49 +109,49 @@ public final class OverlaySettings { return this; } - // TODO: b/262694346 - Rename this method to setBackgroundAnchor in a follow-up CL. /** - * Sets the coordinates for the anchor point of the overlay within the video frame. + * Sets the coordinates for the anchor point of the overlay within the background frame. * *

The coordinates are specified in Normalised Device Coordinates (NDCs) relative to the - * video frame. Set to always return {@code (0,0)} (the center of the video frame) by default. + * background frame. Set to always return {@code (0,0)} (the center of the background frame) by + * default. * - *

For example, a value of {@code (+1,+1)} will move the overlay's {@linkplain - * #setOverlayAnchor anchor point} to the top right corner of the video frame. + *

For example, a value of {@code (+1,+1)} will move the overlay frames's {@linkplain + * #setOverlayFrameAnchor anchor point} to the top right corner of the background frame. * * @param x The NDC x-coordinate in the range [-1, 1]. * @param y The NDC y-coordinate in the range [-1, 1]. */ @CanIgnoreReturnValue - public Builder setVideoFrameAnchor( + public Builder setBackgroundFrameAnchor( @FloatRange(from = -1, to = 1) float x, @FloatRange(from = -1, to = 1) float y) { checkArgument(-1 <= x && x <= 1); checkArgument(-1 <= y && y <= 1); - this.videoFrameAnchor = Pair.create(x, y); + this.backgroundFrameAnchor = Pair.create(x, y); return this; } /** - * Sets the coordinates for the anchor point of the overlay. + * Sets the coordinates for the anchor point of the overlay frame. * - *

The anchor point is the point inside the overlay that is placed on the {@linkplain - * #setVideoFrameAnchor video frame anchor} + *

The anchor point is the point inside the overlay frame that is placed on the {@linkplain + * #setBackgroundFrameAnchor background frame anchor} * *

The coordinates are specified in Normalised Device Coordinates (NDCs) relative to the - * overlay frame. Set to return {@code (0,0)} (the center of the overlay) by default. + * overlay frame. Set to return {@code (0,0)} (the center of the overlay frame) by default. * - *

For example, a value of {@code (+1,-1)} will result in the overlay being positioned from - * the bottom right corner of its frame. + *

For example, a value of {@code (+1,-1)} will result in the overlay frame being positioned + * with its bottom right corner positioned at the background frame anchor. * * @param x The NDC x-coordinate in the range [-1, 1]. * @param y The NDC y-coordinate in the range [-1, 1]. */ @CanIgnoreReturnValue - public Builder setOverlayAnchor( + public Builder setOverlayFrameAnchor( @FloatRange(from = -1, to = 1) float x, @FloatRange(from = -1, to = 1) float y) { checkArgument(-1 <= x && x <= 1); checkArgument(-1 <= y && y <= 1); - this.overlayAnchor = Pair.create(x, y); + this.overlayFrameAnchor = Pair.create(x, y); return this; } @@ -183,7 +183,7 @@ public final class OverlaySettings { /** Creates an instance of {@link OverlaySettings}, using defaults if values are unset. */ public OverlaySettings build() { return new OverlaySettings( - useHdr, alphaScale, videoFrameAnchor, overlayAnchor, scale, rotationDegrees); + useHdr, alphaScale, backgroundFrameAnchor, overlayFrameAnchor, scale, rotationDegrees); } } } diff --git a/libraries/effect/src/main/java/androidx/media3/effect/SamplerOverlayMatrixProvider.java b/libraries/effect/src/main/java/androidx/media3/effect/SamplerOverlayMatrixProvider.java index 5fe575c1dd..73935a4b61 100644 --- a/libraries/effect/src/main/java/androidx/media3/effect/SamplerOverlayMatrixProvider.java +++ b/libraries/effect/src/main/java/androidx/media3/effect/SamplerOverlayMatrixProvider.java @@ -38,9 +38,9 @@ import androidx.media3.common.util.Size; OverlaySettings samplerOverlaySettings = overlaySettings .buildUpon() - .setOverlayAnchor( - /* x= */ -1 * overlaySettings.overlayAnchor.first, - /* y= */ -1 * overlaySettings.overlayAnchor.second) + .setOverlayFrameAnchor( + /* x= */ -1 * overlaySettings.overlayFrameAnchor.first, + /* y= */ -1 * overlaySettings.overlayFrameAnchor.second) .build(); // When sampling from a (for example, texture) sampler, the transformation matrix applied to a diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/DefaultVideoCompositorPixelTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/DefaultVideoCompositorPixelTest.java index ddc818a018..8553f4e188 100644 --- a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/DefaultVideoCompositorPixelTest.java +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/DefaultVideoCompositorPixelTest.java @@ -561,8 +561,8 @@ public final class DefaultVideoCompositorPixelTest { // This tests all OverlaySettings builder variables. return new OverlaySettings.Builder() .setScale(.25f, .5f) - .setOverlayAnchor(1, -1) - .setVideoFrameAnchor(.9f, -.7f) + .setOverlayFrameAnchor(1, -1) + .setBackgroundFrameAnchor(.9f, -.7f) .setRotationDegrees(20) .setAlphaScale(.5f) .build(); @@ -640,8 +640,8 @@ public final class DefaultVideoCompositorPixelTest { @Override public OverlaySettings getOverlaySettings(int inputId, long presentationTimeUs) { return new OverlaySettings.Builder() - .setOverlayAnchor(-1, -1) - .setVideoFrameAnchor(-1, -1 + 2f * inputId / NUMBER_OF_INPUT_STREAMS) + .setOverlayFrameAnchor(-1, -1) + .setBackgroundFrameAnchor(-1, -1 + 2f * inputId / NUMBER_OF_INPUT_STREAMS) .build(); } }; @@ -966,7 +966,7 @@ public final class DefaultVideoCompositorPixelTest { @Override public OverlaySettings getOverlaySettings(long presentationTimeUs) { return new OverlaySettings.Builder() - .setVideoFrameAnchor(/* x= */ 0f, /* y= */ 0.5f) + .setBackgroundFrameAnchor(/* x= */ 0f, /* y= */ 0.5f) .build(); } }));