Commit f216fa20 by huangdarwin Committed by Ian Baker

Transformer GL: Clarify variables and comments.

Simplifying and clarifying variables, and adding comments.

Tested by confirming demo-gl and demo-transformer both
correctly display videos

PiperOrigin-RevId: 421792079
parent f673676c
...@@ -86,23 +86,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -86,23 +86,9 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
program.setBufferAttribute( program.setBufferAttribute(
"a_position", "a_position", GlUtil.getNormalizedCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT);
new float[] {
-1, -1, 0, 1,
1, -1, 0, 1,
-1, 1, 0, 1,
1, 1, 0, 1
},
4);
program.setBufferAttribute( program.setBufferAttribute(
"a_texcoord", "a_texcoord", GlUtil.getTextureCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT);
new float[] {
0, 0, 0, 1,
1, 0, 0, 1,
0, 1, 0, 1,
1, 1, 0, 1
},
4);
GLES20.glGenTextures(1, textures, 0); GLES20.glGenTextures(1, textures, 0);
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
......
...@@ -202,6 +202,9 @@ public final class GlUtil { ...@@ -202,6 +202,9 @@ public final class GlUtil {
/** Whether to throw a {@link GlException} in case of an OpenGL error. */ /** Whether to throw a {@link GlException} in case of an OpenGL error. */
public static boolean glAssertionsEnabled = false; public static boolean glAssertionsEnabled = false;
/** Number of vertices in a rectangle. */
public static final int RECTANGLE_VERTICES_COUNT = 4;
private static final String TAG = "GlUtil"; private static final String TAG = "GlUtil";
private static final String EXTENSION_PROTECTED_CONTENT = "EGL_EXT_protected_content"; private static final String EXTENSION_PROTECTED_CONTENT = "EGL_EXT_protected_content";
...@@ -210,6 +213,26 @@ public final class GlUtil { ...@@ -210,6 +213,26 @@ public final class GlUtil {
/** Class only contains static methods. */ /** Class only contains static methods. */
private GlUtil() {} private GlUtil() {}
/** Bounds of normalized device coordinates, commonly used for defining viewport boundaries. */
public static float[] getNormalizedCoordinateBounds() {
return new float[] {
-1, -1, 0, 1,
1, -1, 0, 1,
-1, 1, 0, 1,
1, 1, 0, 1
};
}
/** Typical bounds used for sampling from textures. */
public static float[] getTextureCoordinateBounds() {
return new float[] {
0, 0, 0, 1,
1, 0, 0, 1,
0, 1, 0, 1,
1, 1, 0, 1
};
}
/** /**
* Returns whether creating a GL context with {@value #EXTENSION_PROTECTED_CONTENT} is possible. * Returns whether creating a GL context with {@value #EXTENSION_PROTECTED_CONTENT} is possible.
* If {@code true}, the device supports a protected output path for DRM content when using GL. * If {@code true}, the device supports a protected output path for DRM content when using GL.
......
...@@ -11,12 +11,12 @@ ...@@ -11,12 +11,12 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
attribute vec4 aPosition; attribute vec4 aFramePosition;
attribute vec4 aTexCoords; attribute vec4 aTexCoords;
uniform mat4 uTexTransform; uniform mat4 uTexTransform;
uniform mat4 uTransformationMatrix; uniform mat4 uTransformationMatrix;
varying vec2 vTexCoords; varying vec2 vTexCoords;
void main() { void main() {
gl_Position = uTransformationMatrix * aPosition; gl_Position = uTransformationMatrix * aFramePosition;
vTexCoords = (uTexTransform * aTexCoords).xy; vTexCoords = (uTexTransform * aTexCoords).xy;
} }
...@@ -131,24 +131,11 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -131,24 +131,11 @@ import java.util.concurrent.atomic.AtomicInteger;
GlUtil.Program glProgram = GlUtil.Program glProgram =
new GlUtil.Program(context, VERTEX_SHADER_FILE_PATH, FRAGMENT_SHADER_FILE_PATH); new GlUtil.Program(context, VERTEX_SHADER_FILE_PATH, FRAGMENT_SHADER_FILE_PATH);
// Draw the frame on the entire normalized device coordinate space, from -1 to 1, for x and y.
glProgram.setBufferAttribute( glProgram.setBufferAttribute(
"aPosition", "aFramePosition", GlUtil.getNormalizedCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT);
new float[] {
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
},
/* size= */ 4);
glProgram.setBufferAttribute( glProgram.setBufferAttribute(
"aTexCoords", "aTexCoords", GlUtil.getTextureCoordinateBounds(), GlUtil.RECTANGLE_VERTICES_COUNT);
new float[] {
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
},
/* size= */ 4);
glProgram.setSamplerTexIdUniform("uTexSampler", textureId, /* unit= */ 0); glProgram.setSamplerTexIdUniform("uTexSampler", textureId, /* unit= */ 0);
float[] transformationMatrixArray = getGlMatrixArray(transformationMatrix); float[] transformationMatrixArray = getGlMatrixArray(transformationMatrix);
......
...@@ -61,6 +61,9 @@ public final class TransformationRequest { ...@@ -61,6 +61,9 @@ public final class TransformationRequest {
* <p>This can be used to perform operations supported by {@link Matrix}, like scaling and * <p>This can be used to perform operations supported by {@link Matrix}, like scaling and
* rotating the video. * rotating the video.
* *
* <p>The video dimensions will be on the x axis, from -aspectRatio to aspectRatio, and on the y
* axis, from -1 to 1.
*
* <p>For now, resolution will not be affected by this method. * <p>For now, resolution will not be affected by this method.
* *
* @param transformationMatrix The transformation to apply to video frames. * @param transformationMatrix The transformation to apply to video frames.
...@@ -71,6 +74,10 @@ public final class TransformationRequest { ...@@ -71,6 +74,10 @@ public final class TransformationRequest {
// allow transformations to change the resolution, by scaling to the appropriate min/max // allow transformations to change the resolution, by scaling to the appropriate min/max
// values. This will also be required to create the VertexTransformation class, in order to // values. This will also be required to create the VertexTransformation class, in order to
// have aspect ratio helper methods (which require resolution to change). // have aspect ratio helper methods (which require resolution to change).
// TODO(b/213198690): Consider changing how transformationMatrix is applied, so that
// dimensions will be from -1 to 1 on both x and y axes, but transformations will be applied
// in a predictable manner.
this.transformationMatrix = transformationMatrix; this.transformationMatrix = transformationMatrix;
return this; return this;
} }
......
...@@ -63,12 +63,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -63,12 +63,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
// Scale width and height to desired transformationRequest.outputHeight, preserving aspect // Scale width and height to desired transformationRequest.outputHeight, preserving aspect
// ratio. // ratio.
// TODO(internal b/209781577): Think about which edge length should be set for portrait videos. // TODO(internal b/209781577): Think about which edge length should be set for portrait videos.
float inputAspectRatio = (float) inputFormat.width / inputFormat.height; float inputFormatAspectRatio = (float) inputFormat.width / inputFormat.height;
int outputWidth = inputFormat.width; int outputWidth = inputFormat.width;
int outputHeight = inputFormat.height; int outputHeight = inputFormat.height;
if (transformationRequest.outputHeight != C.LENGTH_UNSET if (transformationRequest.outputHeight != C.LENGTH_UNSET
&& transformationRequest.outputHeight != inputFormat.height) { && transformationRequest.outputHeight != inputFormat.height) {
outputWidth = Math.round(inputAspectRatio * transformationRequest.outputHeight); outputWidth = Math.round(inputFormatAspectRatio * transformationRequest.outputHeight);
outputHeight = transformationRequest.outputHeight; outputHeight = transformationRequest.outputHeight;
} }
...@@ -82,14 +82,17 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -82,14 +82,17 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
} else { } else {
outputRotationDegrees = inputFormat.rotationDegrees; outputRotationDegrees = inputFormat.rotationDegrees;
} }
if ((inputFormat.rotationDegrees % 180) != 0) { float displayAspectRatio =
inputAspectRatio = 1.0f / inputAspectRatio; (inputFormat.rotationDegrees % 180) == 0
} ? inputFormatAspectRatio
: 1.0f / inputFormatAspectRatio;
// Scale frames by input aspect ratio, to account for FrameEditor normalized device coordinates // Scale frames by input aspect ratio, to account for FrameEditor's square normalized device
// (-1 to 1) and preserve frame relative dimensions during transformations (ex. rotations). // coordinates (-1 to 1) and preserve frame relative dimensions during transformations
transformationRequest.transformationMatrix.preScale(inputAspectRatio, 1); // (ex. rotations). After this scaling, transformationMatrix operations operate on a rectangle
transformationRequest.transformationMatrix.postScale(1.0f / inputAspectRatio, 1); // for x from -displayAspectRatio to displayAspectRatio, and y from -1 to 1
transformationRequest.transformationMatrix.preScale(displayAspectRatio, 1);
transformationRequest.transformationMatrix.postScale(1.0f / displayAspectRatio, 1);
// The decoder rotates videos to their intended display orientation. The frameEditor rotates // The decoder rotates videos to their intended display orientation. The frameEditor rotates
// them back for improved encoder compatibility. // them back for improved encoder compatibility.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment