Skip to content

Commit

Permalink
Only use a FrameEditor if editing is needed.
Browse files Browse the repository at this point in the history
When no editing is needed, the OpenGL steps can be skipped.

PiperOrigin-RevId: 413884305
  • Loading branch information
hmsch authored and ojw28 committed Dec 6, 2021
1 parent 1a1636e commit 5422175
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -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();

Expand All @@ -66,9 +70,12 @@ public void repeatedTranscode_givesConsistentLengthOutput() throws Exception {
@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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.
Expand All @@ -39,12 +40,12 @@
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)
Expand Down Expand Up @@ -79,17 +80,23 @@ public VideoSamplePipeline(
throw createRendererException(
e, rendererIndex, inputFormat, PlaybackException.ERROR_CODE_UNSPECIFIED);
}
frameEditor =
FrameEditor.create(
context,
outputWidth,
outputHeight,
transformation.transformationMatrix,
/* outputSurface= */ checkNotNull(encoder.getInputSurface()));
if (inputFormat.height != transformation.outputHeight
|| !transformation.transformationMatrix.isIdentity()) {
frameEditor =
FrameEditor.create(
context,
outputWidth,
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);
Expand All @@ -102,24 +109,27 @@ public boolean processData() {
return false;
}

if (frameEditor.hasInputData()) {
waitingForPopulatedDecoderSurface = false;
frameEditor.processData();
return true;
}

if (waitingForPopulatedDecoderSurface) {
return false;
if (frameEditor != null) {
if (frameEditor.hasInputData()) {
waitingForFrameEditorInput = false;
frameEditor.processData();
return true;
}
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 false;
return decoderHasOutputBuffer && !waitingForFrameEditorInput;
}

@Override
Expand Down Expand Up @@ -164,7 +174,9 @@ public void releaseOutputBuffer() {

@Override
public void release() {
frameEditor.release();
if (frameEditor != null) {
frameEditor.release();
}
decoder.release();
encoder.release();
}
Expand Down

0 comments on commit 5422175

Please sign in to comment.