mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Use getSupportedWidth/HeightFor() API for finding resolution.
Previously, we've used getSupportedHeights/Widths() to find the supported resolution. However, the height/width can be over-reported when using these APIs. For example, getSupportedWidths and getSupportedHeights can both return 3840, but the supported height when using 3840 as width is only 2160. PiperOrigin-RevId: 432926192
This commit is contained in:
parent
494fc40810
commit
e5c229be00
2 changed files with 91 additions and 7 deletions
|
|
@ -90,18 +90,13 @@ public final class EncoderUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix frame being too wide or too tall.
|
// Fix frame being too wide or too tall.
|
||||||
int adjustedHeight = videoEncoderCapabilities.getSupportedHeights().clamp(height);
|
width = videoEncoderCapabilities.getSupportedWidths().clamp(width);
|
||||||
|
int adjustedHeight = videoEncoderCapabilities.getSupportedHeightsFor(width).clamp(height);
|
||||||
if (adjustedHeight != height) {
|
if (adjustedHeight != height) {
|
||||||
width = (int) round((double) width * adjustedHeight / height);
|
width = (int) round((double) width * adjustedHeight / height);
|
||||||
height = adjustedHeight;
|
height = adjustedHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
int adjustedWidth = videoEncoderCapabilities.getSupportedWidths().clamp(width);
|
|
||||||
if (adjustedWidth != width) {
|
|
||||||
height = (int) round((double) height * adjustedWidth / width);
|
|
||||||
width = adjustedWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fix pixel alignment.
|
// Fix pixel alignment.
|
||||||
width = alignResolution(width, videoEncoderCapabilities.getWidthAlignment());
|
width = alignResolution(width, videoEncoderCapabilities.getWidthAlignment());
|
||||||
height = alignResolution(height, videoEncoderCapabilities.getHeightAlignment());
|
height = alignResolution(height, videoEncoderCapabilities.getHeightAlignment());
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2022 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.android.exoplayer2.transformer;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.media.MediaCodecInfo;
|
||||||
|
import android.media.MediaFormat;
|
||||||
|
import android.util.Pair;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
import com.google.android.exoplayer2.util.MimeTypes;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.shadows.MediaCodecInfoBuilder;
|
||||||
|
import org.robolectric.shadows.ShadowMediaCodecList;
|
||||||
|
|
||||||
|
/** Unit test for {@link EncoderUtil}. */
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public class EncoderUtilTest {
|
||||||
|
private static final String MIME_TYPE = MimeTypes.VIDEO_H264;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MediaFormat avcFormat = new MediaFormat();
|
||||||
|
avcFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_VIDEO_AVC);
|
||||||
|
MediaCodecInfo.CodecProfileLevel profileLevel = new MediaCodecInfo.CodecProfileLevel();
|
||||||
|
profileLevel.profile = MediaCodecInfo.CodecProfileLevel.AVCProfileHigh;
|
||||||
|
// Using Level4 gives us 8192 16x16 blocks. If using width 1920 uses 120 blocks, 8192 / 120 = 68
|
||||||
|
// blocks will be left for encoding height 1088.
|
||||||
|
profileLevel.level = MediaCodecInfo.CodecProfileLevel.AVCLevel4;
|
||||||
|
|
||||||
|
ShadowMediaCodecList.addCodec(
|
||||||
|
MediaCodecInfoBuilder.newBuilder()
|
||||||
|
.setName("test.transformer.avc.encoder")
|
||||||
|
.setIsEncoder(true)
|
||||||
|
.setCapabilities(
|
||||||
|
MediaCodecInfoBuilder.CodecCapabilitiesBuilder.newBuilder()
|
||||||
|
.setMediaFormat(avcFormat)
|
||||||
|
.setIsEncoder(true)
|
||||||
|
.setColorFormats(
|
||||||
|
new int[] {MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible})
|
||||||
|
.setProfileLevels(new MediaCodecInfo.CodecProfileLevel[] {profileLevel})
|
||||||
|
.build())
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getClosestSupportedResolution_withSupportedResolution_succeeds() {
|
||||||
|
ImmutableList<MediaCodecInfo> supportedEncoders = EncoderUtil.getSupportedEncoders(MIME_TYPE);
|
||||||
|
MediaCodecInfo encoderInfo = supportedEncoders.get(0);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
Pair<Integer, Integer> closestSupportedResolution =
|
||||||
|
EncoderUtil.getClosestSupportedResolution(encoderInfo, MIME_TYPE, 1920, 1080);
|
||||||
|
|
||||||
|
assertThat(closestSupportedResolution).isNotNull();
|
||||||
|
assertThat(closestSupportedResolution).isEqualTo(Pair.create(1920, 1080));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getClosestSupportedResolution_withWidthTooBig_findsMostCloselySupportedResolution() {
|
||||||
|
ImmutableList<MediaCodecInfo> supportedEncoders = EncoderUtil.getSupportedEncoders(MIME_TYPE);
|
||||||
|
MediaCodecInfo encoderInfo = supportedEncoders.get(0);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
Pair<Integer, Integer> closestSupportedResolution =
|
||||||
|
EncoderUtil.getClosestSupportedResolution(encoderInfo, MIME_TYPE, 1920, 1920);
|
||||||
|
|
||||||
|
assertThat(closestSupportedResolution).isNotNull();
|
||||||
|
assertThat(closestSupportedResolution).isEqualTo(Pair.create(1088, 1088));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue