From c566ed91adad4bb19684351c7901f0fbbe1ea25f Mon Sep 17 00:00:00 2001 From: hschlueter Date: Wed, 19 Jan 2022 16:41:14 +0000 Subject: [PATCH] Make defensive copies of the transformation matrix. TransformationRequest is otherwise immutable, so if we modify the transformationMatrix in place (done before this cl) this may cause confusing behaviour for apps when they reuse a TransformationRequest. PiperOrigin-RevId: 422822916 --- .../media3/transformer/TransformationRequest.java | 4 ++-- .../media3/transformer/VideoSamplePipeline.java | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/TransformationRequest.java b/libraries/transformer/src/main/java/androidx/media3/transformer/TransformationRequest.java index 1958e20e49..6b7e1ed5b1 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/TransformationRequest.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/TransformationRequest.java @@ -56,7 +56,7 @@ public final class TransformationRequest { } private Builder(TransformationRequest transformationRequest) { - this.transformationMatrix = transformationRequest.transformationMatrix; + this.transformationMatrix = new Matrix(transformationRequest.transformationMatrix); this.flattenForSlowMotion = transformationRequest.flattenForSlowMotion; this.outputHeight = transformationRequest.outputHeight; this.audioMimeType = transformationRequest.audioMimeType; @@ -86,7 +86,7 @@ public final class TransformationRequest { // 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 = new Matrix(transformationMatrix); return this; } diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/VideoSamplePipeline.java b/libraries/transformer/src/main/java/androidx/media3/transformer/VideoSamplePipeline.java index f448093999..40fed2fbf3 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/VideoSamplePipeline.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/VideoSamplePipeline.java @@ -20,6 +20,7 @@ import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Util.SDK_INT; import android.content.Context; +import android.graphics.Matrix; import android.media.MediaCodec; import android.media.MediaFormat; import androidx.annotation.Nullable; @@ -87,18 +88,19 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ? inputFormatAspectRatio : 1.0f / inputFormatAspectRatio; + Matrix transformationMatrix = new Matrix(transformationRequest.transformationMatrix); // Scale frames by input aspect ratio, to account for FrameEditor's square normalized device // coordinates (-1 to 1) and preserve frame relative dimensions during transformations // (ex. rotations). After this scaling, transformationMatrix operations operate on a rectangle // for x from -displayAspectRatio to displayAspectRatio, and y from -1 to 1 - transformationRequest.transformationMatrix.preScale(displayAspectRatio, 1); - transformationRequest.transformationMatrix.postScale(1.0f / displayAspectRatio, 1); + transformationMatrix.preScale(displayAspectRatio, 1); + transformationMatrix.postScale(1.0f / displayAspectRatio, 1); // The decoder rotates videos to their intended display orientation. The frameEditor rotates // them back for improved encoder compatibility. // TODO(b/201293185): After fragment shader transformations are implemented, put // postRotate in a later vertex shader. - transformationRequest.transformationMatrix.postRotate(outputRotationDegrees); + transformationMatrix.postRotate(outputRotationDegrees); encoder = encoderFactory.createForVideoEncoding( @@ -113,17 +115,18 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; .build()); if (inputFormat.height != outputHeight || inputFormat.width != outputWidth - || !transformationRequest.transformationMatrix.isIdentity()) { + || !transformationMatrix.isIdentity()) { frameEditor = FrameEditor.create( context, outputWidth, outputHeight, inputFormat.pixelWidthHeightRatio, - transformationRequest.transformationMatrix, + transformationMatrix, /* outputSurface= */ checkNotNull(encoder.getInputSurface()), debugViewProvider); } + decoder = decoderFactory.createForVideoDecoding( inputFormat,