Remove texture copy from SpeedChangeShaderProgram

Introduces PassthroughShaderProgram to make the effect more efficient.

PiperOrigin-RevId: 603670438
This commit is contained in:
tofunmi 2024-02-02 06:27:45 -08:00 committed by Copybara-Service
parent 8b6c8fc480
commit 271eb88b48
5 changed files with 123 additions and 17 deletions

View File

@ -69,7 +69,7 @@ public final class FrameDropEffect implements GlEffect {
if (inputFrameRate == C.RATE_UNSET) {
return new DefaultFrameDroppingShaderProgram(context, useHdr, targetFrameRate);
} else {
return new SimpleFrameDroppingShaderProgram(context, useHdr, inputFrameRate, targetFrameRate);
return new SimpleFrameDroppingShaderProgram(inputFrameRate, targetFrameRate);
}
}

View File

@ -0,0 +1,109 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
package androidx.media3.effect;
import static androidx.media3.common.util.Assertions.checkState;
import androidx.media3.common.C;
import androidx.media3.common.GlObjectsProvider;
import androidx.media3.common.GlTextureInfo;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.UnstableApi;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.concurrent.Executor;
/**
* A {@linkplain GlShaderProgram} that passes a frame from the input to the output listener without
* copying.
*
* <p>This shader program can only process one input frame at a time.
*/
@UnstableApi
public class PassthroughShaderProgram implements GlShaderProgram {
private InputListener inputListener;
private OutputListener outputListener;
private ErrorListener errorListener;
private Executor errorListenerExecutor;
private int texIdInUse;
public PassthroughShaderProgram() {
inputListener = new InputListener() {};
outputListener = new OutputListener() {};
errorListener = (frameProcessingException) -> {};
errorListenerExecutor = MoreExecutors.directExecutor();
texIdInUse = C.INDEX_UNSET;
}
@Override
public void setInputListener(InputListener inputListener) {
this.inputListener = inputListener;
if (texIdInUse == C.INDEX_UNSET) {
inputListener.onReadyToAcceptInputFrame();
}
}
@Override
public void setOutputListener(OutputListener outputListener) {
this.outputListener = outputListener;
}
@Override
public void setErrorListener(Executor executor, ErrorListener errorListener) {
this.errorListenerExecutor = executor;
this.errorListener = errorListener;
}
@Override
public void queueInputFrame(
GlObjectsProvider glObjectsProvider, GlTextureInfo inputTexture, long presentationTimeUs) {
texIdInUse = inputTexture.texId;
outputListener.onOutputFrameAvailable(inputTexture, presentationTimeUs);
}
@Override
public void releaseOutputFrame(GlTextureInfo outputTexture) {
checkState(outputTexture.texId == texIdInUse);
texIdInUse = C.INDEX_UNSET;
inputListener.onInputFrameProcessed(outputTexture);
inputListener.onReadyToAcceptInputFrame();
}
@Override
public void signalEndOfCurrentInputStream() {
outputListener.onCurrentOutputStreamEnded();
}
@Override
public void flush() {
texIdInUse = C.INDEX_UNSET;
inputListener.onFlush();
}
@Override
public void release() throws VideoFrameProcessingException {
texIdInUse = C.INDEX_UNSET;
}
protected final InputListener getInputListener() {
return inputListener;
}
protected final void onError(Exception e) {
errorListenerExecutor.execute(
() -> errorListener.onError(VideoFrameProcessingException.from(e)));
}
}

View File

@ -19,7 +19,6 @@ package androidx.media3.effect;
import static androidx.media3.common.util.Assertions.checkArgument;
import static java.lang.Math.round;
import android.content.Context;
import androidx.media3.common.GlObjectsProvider;
import androidx.media3.common.GlTextureInfo;
import androidx.media3.common.VideoFrameProcessingException;
@ -31,7 +30,7 @@ import androidx.media3.common.VideoFrameProcessingException;
* <p>For example, if the input stream came in at 60fps and the targeted frame rate was 20fps, every
* 3rd frame would be kept. If n is not an integer, then we round to the nearest one.
*/
/* package */ final class SimpleFrameDroppingShaderProgram extends FrameCacheGlShaderProgram {
/* package */ final class SimpleFrameDroppingShaderProgram extends PassthroughShaderProgram {
private final int n;
@ -40,16 +39,11 @@ import androidx.media3.common.VideoFrameProcessingException;
/**
* Creates a new instance.
*
* @param context The {@link Context}.
* @param useHdr Whether input textures come from an HDR source. If {@code true}, colors will be
* in linear RGB BT.2020. If {@code false}, colors will be in linear RGB BT.709.
* @param inputFrameRate The number of frames per second the input stream should have.
* @param targetFrameRate The number of frames per second the output video should roughly have.
*/
public SimpleFrameDroppingShaderProgram(
Context context, boolean useHdr, float inputFrameRate, float targetFrameRate)
throws VideoFrameProcessingException {
super(context, /* capacity= */ 1, useHdr);
public SimpleFrameDroppingShaderProgram(float inputFrameRate, float targetFrameRate) {
super();
n = round(inputFrameRate / targetFrameRate);
checkArgument(n >= 1, "The input frame rate should be greater than the target frame rate.");
}
@ -77,4 +71,10 @@ import androidx.media3.common.VideoFrameProcessingException;
super.flush();
framesReceived = 0;
}
@Override
public void release() throws VideoFrameProcessingException {
super.release();
framesReceived = 0;
}
}

View File

@ -41,7 +41,7 @@ public final class SpeedChangeEffect implements GlEffect {
@Override
public GlShaderProgram toGlShaderProgram(Context context, boolean useHdr)
throws VideoFrameProcessingException {
return new SpeedChangeShaderProgram(context, speed, useHdr);
return new SpeedChangeShaderProgram(speed);
}
@Override

View File

@ -15,21 +15,18 @@
*/
package androidx.media3.effect;
import android.content.Context;
import androidx.media3.common.GlObjectsProvider;
import androidx.media3.common.GlTextureInfo;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.util.UnstableApi;
/** Applies a speed change by updating the frame timestamps. */
@UnstableApi
/* package */ final class SpeedChangeShaderProgram extends FrameCacheGlShaderProgram {
/* package */ final class SpeedChangeShaderProgram extends PassthroughShaderProgram {
private final float speed;
public SpeedChangeShaderProgram(Context context, float speed, boolean useHdr)
throws VideoFrameProcessingException {
super(context, /* capacity= */ 1, useHdr);
public SpeedChangeShaderProgram(float speed) {
super();
this.speed = speed;
}