Skip to content

Commit

Permalink
Allow changing input pixelWidthHeightRatio for GlEffectsFrameProcessor.
Browse files Browse the repository at this point in the history
pixelWidthHeightRatio is now passed to setInputFrameInfo instead of
the factory.

PiperOrigin-RevId: 457696703
  • Loading branch information
hmsch authored and rohitjoins committed Jul 7, 2022
1 parent 40350bc commit bfa663d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 41 deletions.
Expand Up @@ -366,7 +366,6 @@ public void onFrameProcessingEnded() {
frameProcessingEnded = true;
}
},
pixelWidthHeightRatio,
/* streamOffsetUs= */ 0L,
effects,
/* outputSurfaceProvider= */ (requestedWidth, requestedHeight) -> {
Expand All @@ -381,7 +380,8 @@ public void onFrameProcessingEnded() {
},
Transformer.DebugViewProvider.NONE,
/* enableExperimentalHdrEditing= */ false));
glEffectsFrameProcessor.setInputFrameInfo(new FrameInfo(inputWidth, inputHeight));
glEffectsFrameProcessor.setInputFrameInfo(
new FrameInfo(inputWidth, inputHeight, pixelWidthHeightRatio));
glEffectsFrameProcessor.registerInputFrame();

// Queue the first video frame from the extractor.
Expand Down
Expand Up @@ -23,15 +23,17 @@
public final int width;
/** The height of the frame, in pixels. */
public final int height;
/** The ratio of width over height for each pixel. */
public final float pixelWidthHeightRatio;

// TODO(b/227625423): Add pixelWidthHeightRatio.
// TODO(b/227624622): Add color space information for HDR.

public FrameInfo(int width, int height) {
public FrameInfo(int width, int height, float pixelWidthHeightRatio) {
checkArgument(width > 0, "width must be positive, but is: " + width);
checkArgument(height > 0, "height must be positive, but is: " + height);

this.width = width;
this.height = height;
this.pixelWidthHeightRatio = pixelWidthHeightRatio;
}
}
Expand Up @@ -46,6 +46,9 @@ interface Listener {
*
* <p>The new input information is applied from the next frame {@linkplain #registerInputFrame()
* registered} onwards.
*
* <p>Pixels are expanded using the {@link FrameInfo#pixelWidthHeightRatio} so that the output
* frames' pixels have a ratio of 1.
*/
void setInputFrameInfo(FrameInfo inputFrameInfo);

Expand Down
Expand Up @@ -50,8 +50,6 @@
*
* @param context A {@link Context}.
* @param listener A {@link Listener}.
* @param pixelWidthHeightRatio The ratio of width over height for each pixel. Pixels are expanded
* by this ratio so that the output frame's pixels have a ratio of 1.
* @param effects The {@link GlEffect GlEffects} to apply to each frame.
* @param outputSurfaceProvider A {@link SurfaceInfo.Provider} managing the output {@link
* Surface}.
Expand All @@ -64,7 +62,6 @@
public static GlEffectsFrameProcessor create(
Context context,
FrameProcessor.Listener listener,
float pixelWidthHeightRatio,
long streamOffsetUs,
List<GlEffect> effects,
SurfaceInfo.Provider outputSurfaceProvider,
Expand All @@ -80,7 +77,6 @@ public static GlEffectsFrameProcessor create(
createOpenGlObjectsAndFrameProcessor(
context,
listener,
pixelWidthHeightRatio,
streamOffsetUs,
effects,
outputSurfaceProvider,
Expand Down Expand Up @@ -110,7 +106,6 @@ public static GlEffectsFrameProcessor create(
private static GlEffectsFrameProcessor createOpenGlObjectsAndFrameProcessor(
Context context,
FrameProcessor.Listener listener,
float pixelWidthHeightRatio,
long streamOffsetUs,
List<GlEffect> effects,
SurfaceInfo.Provider outputSurfaceProvider,
Expand All @@ -136,20 +131,12 @@ private static GlEffectsFrameProcessor createOpenGlObjectsAndFrameProcessor(
GlUtil.focusPlaceholderEglSurface(eglContext, eglDisplay);
}

ImmutableList.Builder<GlMatrixTransformation> matrixTransformationListBuilder =
new ImmutableList.Builder<>();
if (pixelWidthHeightRatio != 1f) {
matrixTransformationListBuilder.add(
createPixelWidthHeightRatioTransformation(pixelWidthHeightRatio));
}

ImmutableList<GlTextureProcessor> textureProcessors =
getGlTextureProcessorsForGlEffects(
context,
effects,
eglDisplay,
eglContext,
matrixTransformationListBuilder,
outputSurfaceProvider,
streamOffsetUs,
listener,
Expand All @@ -173,26 +160,6 @@ private static GlEffectsFrameProcessor createOpenGlObjectsAndFrameProcessor(
textureProcessors);
}

/**
* Returns a new {@link GlMatrixTransformation} to expand or shrink the frame based on the {@code
* pixelWidthHeightRatio}.
*
* <p>If {@code pixelWidthHeightRatio} is 1, this method returns an identity transformation that
* can be ignored.
*/
private static GlMatrixTransformation createPixelWidthHeightRatioTransformation(
float pixelWidthHeightRatio) {
if (pixelWidthHeightRatio > 1f) {
return new ScaleToFitTransformation.Builder()
.setScale(/* scaleX= */ pixelWidthHeightRatio, /* scaleY= */ 1f)
.build();
} else {
return new ScaleToFitTransformation.Builder()
.setScale(/* scaleX= */ 1f, /* scaleY= */ 1f / pixelWidthHeightRatio)
.build();
}
}

/**
* Combines consecutive {@link GlMatrixTransformation} instances into a single {@link
* MatrixTransformationProcessor} and converts all other {@link GlEffect} instances to separate
Expand All @@ -207,7 +174,6 @@ private static ImmutableList<GlTextureProcessor> getGlTextureProcessorsForGlEffe
List<GlEffect> effects,
EGLDisplay eglDisplay,
EGLContext eglContext,
ImmutableList.Builder<GlMatrixTransformation> matrixTransformationListBuilder,
SurfaceInfo.Provider outputSurfaceProvider,
long streamOffsetUs,
FrameProcessor.Listener listener,
Expand All @@ -216,6 +182,8 @@ private static ImmutableList<GlTextureProcessor> getGlTextureProcessorsForGlEffe
throws FrameProcessingException {
ImmutableList.Builder<GlTextureProcessor> textureProcessorListBuilder =
new ImmutableList.Builder<>();
ImmutableList.Builder<GlMatrixTransformation> matrixTransformationListBuilder =
new ImmutableList.Builder<>();
for (int i = 0; i < effects.size(); i++) {
GlEffect effect = effects.get(i);
if (effect instanceof GlMatrixTransformation) {
Expand Down Expand Up @@ -396,7 +364,7 @@ private void processInputFrame() {
long presentationTimeUs = inputFrameTimeNs / 1000 - streamOffsetUs;
inputSurfaceTexture.getTransformMatrix(inputSurfaceTextureTransformMatrix);
inputExternalTextureProcessor.setTextureTransformMatrix(inputSurfaceTextureTransformMatrix);
FrameInfo inputFrameInfo = pendingInputFrames.remove();
FrameInfo inputFrameInfo = adjustForPixelWidthHeightRatio(pendingInputFrames.remove());
checkState(
inputExternalTextureProcessor.maybeQueueInputFrame(
new TextureInfo(
Expand All @@ -409,6 +377,27 @@ private void processInputFrame() {
// asynchronously by the texture processors chained after it.
}

/**
* Expands or shrinks the frame based on the {@link FrameInfo#pixelWidthHeightRatio} and returns a
* new {@link FrameInfo} instance with scaled dimensions and {@link
* FrameInfo#pixelWidthHeightRatio} 1.
*/
private FrameInfo adjustForPixelWidthHeightRatio(FrameInfo frameInfo) {
if (frameInfo.pixelWidthHeightRatio > 1f) {
return new FrameInfo(
(int) (frameInfo.width * frameInfo.pixelWidthHeightRatio),
frameInfo.height,
/* pixelWidthHeightRatio= */ 1);
} else if (frameInfo.pixelWidthHeightRatio < 1f) {
return new FrameInfo(
frameInfo.width,
(int) (frameInfo.height / frameInfo.pixelWidthHeightRatio),
/* pixelWidthHeightRatio= */ 1);
} else {
return frameInfo;
}
}

/**
* Propagates the end-of-stream signal through the texture processors once no more input frames
* are pending.
Expand Down
Expand Up @@ -119,7 +119,6 @@ public void onFrameProcessingEnded() {
}
}
},
inputFormat.pixelWidthHeightRatio,
streamOffsetUs,
effectsListBuilder.build(),
/* outputSurfaceProvider= */ encoderWrapper,
Expand All @@ -129,7 +128,8 @@ public void onFrameProcessingEnded() {
throw TransformationException.createForFrameProcessingException(
e, TransformationException.ERROR_CODE_GL_INIT_FAILED);
}
frameProcessor.setInputFrameInfo(new FrameInfo(decodedWidth, decodedHeight));
frameProcessor.setInputFrameInfo(
new FrameInfo(decodedWidth, decodedHeight, inputFormat.pixelWidthHeightRatio));

decoder =
decoderFactory.createForVideoDecoding(
Expand Down

0 comments on commit bfa663d

Please sign in to comment.