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
This commit is contained in:
huangdarwin 2023-09-01 11:25:58 -07:00 committed by Copybara-Service
parent 70ad637e52
commit 2bdda35731
7 changed files with 67 additions and 66 deletions

View File

@ -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();
}

View File

@ -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 {

View File

@ -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();

View File

@ -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<Float, Float> videoFrameAnchor = overlaySettings.videoFrameAnchor;
Pair<Float, Float> 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<Float, Float> overlayAnchor = overlaySettings.overlayAnchor;
Pair<Float, Float> 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);

View File

@ -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<Float, Float> videoFrameAnchor;
public final Pair<Float, Float> overlayAnchor;
public final Pair<Float, Float> backgroundFrameAnchor;
public final Pair<Float, Float> overlayFrameAnchor;
public final Pair<Float, Float> scale;
public final float rotationDegrees;
private OverlaySettings(
boolean useHdr,
float alphaScale,
Pair<Float, Float> videoFrameAnchor,
Pair<Float, Float> overlayAnchor,
Pair<Float, Float> backgroundFrameAnchor,
Pair<Float, Float> overlayFrameAnchor,
Pair<Float, Float> 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<Float, Float> videoFrameAnchor;
private Pair<Float, Float> overlayAnchor;
private Pair<Float, Float> backgroundFrameAnchor;
private Pair<Float, Float> overlayFrameAnchor;
private Pair<Float, Float> 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.
*
* <p>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.
*
* <p>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.
* <p>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.
*
* <p>The anchor point is the point inside the overlay that is placed on the {@linkplain
* #setVideoFrameAnchor video frame anchor}
* <p>The anchor point is the point inside the overlay frame that is placed on the {@linkplain
* #setBackgroundFrameAnchor background frame anchor}
*
* <p>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.
*
* <p>For example, a value of {@code (+1,-1)} will result in the overlay being positioned from
* the bottom right corner of its frame.
* <p>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);
}
}
}

View File

@ -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

View File

@ -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();
}
}));