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:
parent
244777234b
commit
ef1788dcea
@ -19,6 +19,7 @@ import static androidx.media3.transformer.mh.AndroidTestUtil.runTransformer;
|
||||
import static com.google.common.truth.Truth.assertWithMessage;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Matrix;
|
||||
import androidx.media3.common.MimeTypes;
|
||||
import androidx.media3.transformer.Transformer;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
@ -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();
|
||||
|
||||
|
@ -28,6 +28,7 @@ import androidx.media3.decoder.DecoderInputBuffer;
|
||||
import androidx.media3.exoplayer.ExoPlaybackException;
|
||||
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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user