Skip to content

Commit

Permalink
Fix texture transformation in gldemo
Browse files Browse the repository at this point in the history
`SurfaceTexture` provides a transform matrix with each buffer. Previously
gldemo ignored this but it is important to apply it to have the video render
properly.

The transformation matrix from the surface texture includes flipping so this
change removes the hard-coded flipping from `a_texcoord`.

Issue: #8992

#minor-release

PiperOrigin-RevId: 377271389
  • Loading branch information
andrewlewis authored and marcbaechinger committed Jun 3, 2021
1 parent 271011c commit 2260678
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
([#8985](https://github.com/google/ExoPlayer/issues/8985)).
* RTSP:
* Add support for RTSP basic and digest authentication.
* GL demo app:
* Fix texture transformation to avoid green bars shown on some videos
([#8992](https://github.com/google/ExoPlayer/issues/8992)).

### 2.14.0 (2021-05-13)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
attribute vec2 a_position;
attribute vec2 a_texcoord;
attribute vec4 a_position;
attribute vec4 a_texcoord;
uniform mat4 tex_transform;
varying vec2 v_texcoord;
void main() {
gl_Position = vec4(a_position.x, a_position.y, 0, 1);
v_texcoord = a_texcoord;
gl_Position = a_position;
v_texcoord = (tex_transform * a_texcoord).xy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public void initialize() {
GlUtil.Uniform[] uniforms = GlUtil.getUniforms(program);
for (GlUtil.Attribute attribute : attributes) {
if (attribute.name.equals("a_position")) {
attribute.setBuffer(new float[] {-1, -1, 1, -1, -1, 1, 1, 1}, 2);
attribute.setBuffer(new float[] {-1, -1, 0, 1, 1, -1, 0, 1, -1, 1, 0, 1, 1, 1, 0, 1}, 4);
} else if (attribute.name.equals("a_texcoord")) {
attribute.setBuffer(new float[] {0, 1, 1, 1, 0, 0, 1, 0}, 2);
attribute.setBuffer(new float[] {0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1}, 4);
}
}
this.attributes = attributes;
Expand All @@ -111,7 +111,7 @@ public void setSurfaceSize(int width, int height) {
}

@Override
public void draw(int frameTexture, long frameTimestampUs) {
public void draw(int frameTexture, long frameTimestampUs, float[] transformMatrix) {
// Draw to the canvas and store it in a texture.
String text = String.format(Locale.US, "%.02f", frameTimestampUs / (float) C.MICROS_PER_SECOND);
overlayBitmap.eraseColor(Color.TRANSPARENT);
Expand Down Expand Up @@ -140,6 +140,9 @@ public void draw(int frameTexture, long frameTimestampUs) {
case "scaleY":
uniform.setFloat(bitmapScaleY);
break;
case "tex_transform":
uniform.setFloats(transformMatrix);
break;
default: // fall out
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.view.Surface;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.Assertions;
Expand Down Expand Up @@ -61,8 +62,9 @@ public interface VideoProcessor {
*
* @param frameTexture The ID of a GL texture containing a video frame.
* @param frameTimestampUs The presentation timestamp of the frame, in microseconds.
* @param transformMatrix The 4 * 4 transform matrix to be applied to the texture.
*/
void draw(int frameTexture, long frameTimestampUs);
void draw(int frameTexture, long frameTimestampUs, float[] transformMatrix);
}

private static final int EGL_PROTECTED_CONTENT_EXT = 0x32C0;
Expand Down Expand Up @@ -214,6 +216,7 @@ private final class VideoRenderer implements GLSurfaceView.Renderer, VideoFrameM
private final VideoProcessor videoProcessor;
private final AtomicBoolean frameAvailable;
private final TimedValueQueue<Long> sampleTimestampQueue;
private final float[] transformMatrix;

private int texture;
@Nullable private SurfaceTexture surfaceTexture;
Expand All @@ -229,6 +232,8 @@ public VideoRenderer(VideoProcessor videoProcessor) {
sampleTimestampQueue = new TimedValueQueue<>();
width = -1;
height = -1;
frameTimestampUs = C.TIME_UNSET;
transformMatrix = new float[16];
}

@Override
Expand Down Expand Up @@ -271,13 +276,14 @@ public void onDrawFrame(GL10 gl) {
SurfaceTexture surfaceTexture = Assertions.checkNotNull(this.surfaceTexture);
surfaceTexture.updateTexImage();
long lastFrameTimestampNs = surfaceTexture.getTimestamp();
Long frameTimestampUs = sampleTimestampQueue.poll(lastFrameTimestampNs);
@Nullable Long frameTimestampUs = sampleTimestampQueue.poll(lastFrameTimestampNs);
if (frameTimestampUs != null) {
this.frameTimestampUs = frameTimestampUs;
}
surfaceTexture.getTransformMatrix(transformMatrix);
}

videoProcessor.draw(texture, frameTimestampUs);
videoProcessor.draw(texture, frameTimestampUs, transformMatrix);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Uniform(int program, int index) {
location = GLES20.glGetUniformLocation(program, this.name);
this.type = type[0];

value = new float[1];
value = new float[16];
}

/**
Expand All @@ -160,9 +160,14 @@ public void setFloat(float value) {
this.value[0] = value;
}

/** Configures {@link #bind()} to use the specified float[] {@code value} for this uniform. */
public void setFloats(float[] value) {
System.arraycopy(value, 0, this.value, 0, value.length);
}

/**
* Sets the uniform to whatever value was passed via {@link #setSamplerTexId(int, int)} or
* {@link #setFloat(float)}.
* Sets the uniform to whatever value was passed via {@link #setSamplerTexId(int, int)}, {@link
* #setFloat(float)} or {@link #setFloats(float[])}.
*
* <p>Should be called before each drawing call.
*/
Expand All @@ -173,6 +178,12 @@ public void bind() {
return;
}

if (type == GLES20.GL_FLOAT_MAT4) {
GLES20.glUniformMatrix4fv(location, 1, false, value, 0);
checkGlError();
return;
}

if (texId == 0) {
throw new IllegalStateException("call setSamplerTexId before bind");
}
Expand Down

0 comments on commit 2260678

Please sign in to comment.