Preserve aspect ratio when scaling resolution is insufficient.

PiperOrigin-RevId: 509546771
This commit is contained in:
samrobinson 2023-02-14 17:08:04 +00:00 committed by christosts
parent b7d68fb62e
commit ccc349abfd
2 changed files with 19 additions and 3 deletions

View File

@ -252,9 +252,8 @@ public final class EncoderUtil {
}
}
// Fix frame being too wide or too tall.
width = videoCapabilities.getSupportedWidths().clamp(width);
int adjustedHeight = videoCapabilities.getSupportedHeightsFor(width).clamp(height);
int supportedWidth = videoCapabilities.getSupportedWidths().clamp(width);
int adjustedHeight = videoCapabilities.getSupportedHeightsFor(supportedWidth).clamp(height);
if (adjustedHeight != height) {
width =
alignResolution((int) round((double) width * adjustedHeight / height), widthAlignment);

View File

@ -163,6 +163,23 @@ public class EncoderUtilTest {
assertThat(closestSupportedResolution.getHeight()).isEqualTo(1080);
}
@Test
public void getSupportedResolution_requestedReallyLarge_matchesAspectRatio() {
ImmutableList<MediaCodecInfo> supportedEncoders = EncoderUtil.getSupportedEncoders(MIME_TYPE);
MediaCodecInfo encoderInfo = supportedEncoders.get(0);
double aspectRatio = 1.5;
@Nullable
Size closestSupportedResolution =
EncoderUtil.getSupportedResolution(
encoderInfo, MIME_TYPE, (int) (aspectRatio * 5000), 5000);
assertThat(closestSupportedResolution).isNotNull();
assertThat(
(double) closestSupportedResolution.getWidth() / closestSupportedResolution.getHeight())
.isEqualTo(aspectRatio);
}
/**
* @see EncoderUtil#getSupportedEncodersForHdrEditing(String, ColorInfo)
*/