Only use a FrameEditor if editing is needed.

When no editing is needed, the OpenGL steps can be skipped.

PiperOrigin-RevId: 413884305
This commit is contained in:
hschlueter 2021-12-03 10:34:55 +00:00 committed by Oliver Woodman
parent 1a1636ef30
commit 5422175ec1
2 changed files with 42 additions and 23 deletions

View File

@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.transformer.mh.AndroidTestUtil.runTr
import static com.google.common.truth.Truth.assertWithMessage;
import android.content.Context;
import android.graphics.Matrix;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.transformer.Transformer;
@ -38,9 +39,12 @@ public final class RepeatedTranscodeTransformationTest {
@Test
public void repeatedTranscode_givesConsistentLengthOutput() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
Matrix transformationMatrix = new Matrix();
transformationMatrix.postTranslate((float) 0.1, (float) 0.1);
Transformer transformer =
new Transformer.Builder(context)
.setVideoMimeType(MimeTypes.VIDEO_H265)
.setTransformationMatrix(transformationMatrix)
.setAudioMimeType(MimeTypes.AUDIO_AMR_NB)
.build();
@ -66,9 +70,12 @@ public final class RepeatedTranscodeTransformationTest {
@Test
public void repeatedTranscodeNoAudio_givesConsistentLengthOutput() throws Exception {
Context context = ApplicationProvider.getApplicationContext();
Matrix transformationMatrix = new Matrix();
transformationMatrix.postTranslate((float) 0.1, (float) 0.1);
Transformer transformer =
new Transformer.Builder(context)
.setVideoMimeType(MimeTypes.VIDEO_H265)
.setTransformationMatrix(transformationMatrix)
.setRemoveAudio(true)
.build();

View File

@ -28,6 +28,7 @@ import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/**
* Pipeline to decode video samples, apply transformations on the raw samples, and re-encode them.
@ -39,12 +40,12 @@ import java.io.IOException;
private final DecoderInputBuffer decoderInputBuffer;
private final MediaCodecAdapterWrapper decoder;
private final FrameEditor frameEditor;
private final MediaCodecAdapterWrapper encoder;
private final DecoderInputBuffer encoderOutputBuffer;
private boolean waitingForPopulatedDecoderSurface;
private @MonotonicNonNull FrameEditor frameEditor;
private boolean waitingForFrameEditorInput;
public VideoSamplePipeline(
Context context, Format inputFormat, Transformation transformation, int rendererIndex)
@ -79,6 +80,8 @@ import java.io.IOException;
throw createRendererException(
e, rendererIndex, inputFormat, PlaybackException.ERROR_CODE_UNSPECIFIED);
}
if (inputFormat.height != transformation.outputHeight
|| !transformation.transformationMatrix.isIdentity()) {
frameEditor =
FrameEditor.create(
context,
@ -86,10 +89,14 @@ import java.io.IOException;
outputHeight,
transformation.transformationMatrix,
/* outputSurface= */ checkNotNull(encoder.getInputSurface()));
}
try {
decoder =
MediaCodecAdapterWrapper.createForVideoDecoding(
inputFormat, frameEditor.getInputSurface());
inputFormat,
frameEditor == null
? checkNotNull(encoder.getInputSurface())
: frameEditor.getInputSurface());
} catch (IOException e) {
throw createRendererException(
e, rendererIndex, inputFormat, PlaybackException.ERROR_CODE_DECODER_INIT_FAILED);
@ -102,25 +109,28 @@ import java.io.IOException;
return false;
}
if (frameEditor != null) {
if (frameEditor.hasInputData()) {
waitingForPopulatedDecoderSurface = false;
waitingForFrameEditorInput = false;
frameEditor.processData();
return true;
}
if (waitingForPopulatedDecoderSurface) {
if (waitingForFrameEditorInput) {
return false;
}
}
if (decoder.getOutputBufferInfo() != null) {
boolean decoderHasOutputBuffer = decoder.getOutputBufferInfo() != null;
if (decoderHasOutputBuffer) {
decoder.releaseOutputBuffer(/* render= */ true);
waitingForPopulatedDecoderSurface = true;
waitingForFrameEditorInput = frameEditor != null;
}
if (decoder.isEnded()) {
encoder.signalEndOfInputStream();
}
return false;
}
return decoderHasOutputBuffer && !waitingForFrameEditorInput;
}
@Override
@Nullable
@ -164,7 +174,9 @@ import java.io.IOException;
@Override
public void release() {
if (frameEditor != null) {
frameEditor.release();
}
decoder.release();
encoder.release();
}