Take encoder resolution fallback into account in video sample pipeline.

If the encoder picks a fallback resolution the video pipeline needs
to take this into account when configuring the frameEditor and when
setting up the fallback TransformationRequest that's passed to the
fallbackListener.

PiperOrigin-RevId: 424611290
This commit is contained in:
hschlueter 2022-01-27 15:55:06 +00:00 committed by Andrew Lewis
parent 901a5a4845
commit 8123ef5d71
2 changed files with 31 additions and 17 deletions

View File

@ -113,7 +113,7 @@ import org.checkerframework.dataflow.qual.Pure;
encoder = encoderFactory.createForAudioEncoding(requestedOutputFormat, allowedOutputMimeTypes); encoder = encoderFactory.createForAudioEncoding(requestedOutputFormat, allowedOutputMimeTypes);
fallbackListener.onTransformationRequestFinalized( fallbackListener.onTransformationRequestFinalized(
createFallbackRequest( createFallbackTransformationRequest(
transformationRequest, requestedOutputFormat, encoder.getConfigurationFormat())); transformationRequest, requestedOutputFormat, encoder.getConfigurationFormat()));
} }
@ -329,7 +329,7 @@ import org.checkerframework.dataflow.qual.Pure;
} }
@Pure @Pure
private static TransformationRequest createFallbackRequest( private static TransformationRequest createFallbackTransformationRequest(
TransformationRequest transformationRequest, Format requestedFormat, Format actualFormat) { TransformationRequest transformationRequest, Format requestedFormat, Format actualFormat) {
// TODO(b/210591626): Also update bitrate and other params once encoder configuration and // TODO(b/210591626): Also update bitrate and other params once encoder configuration and
// fallback are implemented. // fallback are implemented.

View File

@ -76,9 +76,10 @@ import org.checkerframework.dataflow.qual.Pure;
outputHeight = transformationRequest.outputHeight; outputHeight = transformationRequest.outputHeight;
} }
if (inputFormat.height > inputFormat.width) {
// The encoder may not support encoding in portrait orientation, so the decoded video is // The encoder may not support encoding in portrait orientation, so the decoded video is
// rotated to landscape orientation and a rotation is added back later to the output format. // rotated to landscape orientation and a rotation is added back later to the output format.
boolean swapEncodingDimensions = inputFormat.height > inputFormat.width;
if (swapEncodingDimensions) {
outputRotationDegrees = (inputFormat.rotationDegrees + 90) % 360; outputRotationDegrees = (inputFormat.rotationDegrees + 90) % 360;
int temp = outputWidth; int temp = outputWidth;
outputWidth = outputHeight; outputWidth = outputHeight;
@ -116,18 +117,22 @@ import org.checkerframework.dataflow.qual.Pure;
: inputFormat.sampleMimeType) : inputFormat.sampleMimeType)
.build(); .build();
encoder = encoderFactory.createForVideoEncoding(requestedOutputFormat, allowedOutputMimeTypes); encoder = encoderFactory.createForVideoEncoding(requestedOutputFormat, allowedOutputMimeTypes);
Format actualOutputFormat = encoder.getConfigurationFormat();
fallbackListener.onTransformationRequestFinalized( fallbackListener.onTransformationRequestFinalized(
createFallbackRequest( createFallbackTransformationRequest(
transformationRequest, requestedOutputFormat, encoder.getConfigurationFormat())); transformationRequest,
!swapEncodingDimensions,
requestedOutputFormat,
actualOutputFormat));
if (inputFormat.height != outputHeight if (inputFormat.height != actualOutputFormat.height
|| inputFormat.width != outputWidth || inputFormat.width != actualOutputFormat.width
|| !transformationMatrix.isIdentity()) { || !transformationMatrix.isIdentity()) {
frameEditor = frameEditor =
FrameEditor.create( FrameEditor.create(
context, context,
outputWidth, actualOutputFormat.width,
outputHeight, actualOutputFormat.height,
inputFormat.pixelWidthHeightRatio, inputFormat.pixelWidthHeightRatio,
transformationMatrix, transformationMatrix,
/* outputSurface= */ checkNotNull(encoder.getInputSurface()), /* outputSurface= */ checkNotNull(encoder.getInputSurface()),
@ -270,13 +275,22 @@ import org.checkerframework.dataflow.qual.Pure;
} }
@Pure @Pure
private static TransformationRequest createFallbackRequest( private static TransformationRequest createFallbackTransformationRequest(
TransformationRequest transformationRequest, Format requestedFormat, Format actualFormat) { TransformationRequest transformationRequest,
// TODO(b/210591626): Also update resolution, bitrate etc. once encoder configuration and boolean resolutionIsHeight,
// fallback are implemented. Format requestedFormat,
if (Util.areEqual(requestedFormat.sampleMimeType, actualFormat.sampleMimeType)) { Format actualFormat) {
// TODO(b/210591626): Also update bitrate etc. once encoder configuration and fallback are
// implemented.
if (Util.areEqual(requestedFormat.sampleMimeType, actualFormat.sampleMimeType)
&& ((!resolutionIsHeight && requestedFormat.width == actualFormat.width)
|| (resolutionIsHeight && requestedFormat.height == actualFormat.height))) {
return transformationRequest; return transformationRequest;
} }
return transformationRequest.buildUpon().setVideoMimeType(actualFormat.sampleMimeType).build(); return transformationRequest
.buildUpon()
.setVideoMimeType(actualFormat.sampleMimeType)
.setResolution(resolutionIsHeight ? requestedFormat.height : requestedFormat.width)
.build();
} }
} }