1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.video.cts;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import android.media.MediaFormat;
22 import android.os.Build;
23 import android.util.Log;
24 
25 import androidx.test.filters.LargeTest;
26 
27 import com.android.compatibility.common.util.ApiTest;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.List;
38 
39 /**
40  * Operating rate is expected to be met by encoder only in surface mode and not in byte buffer mode.
41  * As camera has limited frame rates and resolutions, it is not possible to test encoder
42  * operating rate alone. So we are going ahead with transcode tests as a way to verify
43  * encoder performances. This test calls decoder to decode to a surface that is coupled to encoder.
44  * This assumes encoder will not be faster than decode and expects half the operating rate
45  * to be met for encoders.
46  */
47 @RunWith(Parameterized.class)
48 public class CodecEncoderPerformanceTest extends CodecEncoderPerformanceTestBase {
49     private static final String LOG_TAG = CodecEncoderPerformanceTest.class.getSimpleName();
50 
CodecEncoderPerformanceTest(String decoderName, String testFile, String encoderMime, String encoderName, int bitrate, int keyPriority, float scalingFactor, boolean isAsync, int maxBFrames)51     public CodecEncoderPerformanceTest(String decoderName, String testFile, String encoderMime,
52             String encoderName, int bitrate, int keyPriority, float scalingFactor,
53             boolean isAsync, int maxBFrames) {
54         super(decoderName, testFile, encoderMime, encoderName, bitrate, keyPriority, scalingFactor,
55                 isAsync, maxBFrames);
56     }
57 
58     @Parameterized.Parameters(name = "{index}_{0}_{2}_{3}_{5}_{6}_{7}_{8}")
input()59     public static Collection<Object[]> input() throws IOException {
60         final List<Object[]> exhaustiveArgsList = Arrays.asList(new Object[][]{
61                 // Filename, Recommended AVC bitrate
62                 {"crowd_run_720x480_30fps_avc.mp4", 2000000},
63                 {"crowd_run_1280x720_30fps_avc.mp4", 4000000},
64                 {"crowd_run_1920x1080_30fps_avc.mp4", 8000000},
65                 {"crowd_run_3840x2160_30fps_hevc.mp4", 20000000},
66                 // TODO (b/194721211) Enable 8k tests
67                 //{"crowd_run_7680x4320_30fps_hevc.mp4", 40000000},
68         });
69         // Prepares the params list with the supported Hardware decoder, encoders in the device
70         // combined with the key priority and scaling factor
71         final List<Object[]> argsList = new ArrayList<>();
72         for (Object[] arg : exhaustiveArgsList) {
73             // Gets the format for the first video track found
74             MediaFormat format = getVideoFormat((String) arg[0]);
75             if (format == null) {
76                 Log.e(LOG_TAG, "Test vector is ignored as it has no video tracks present " +
77                         "in the file: " + ((String) arg[0]));
78                 continue;
79             }
80             String decoderMime = format.getString(MediaFormat.KEY_MIME);
81             ArrayList<MediaFormat> formatsList = new ArrayList<>();
82             formatsList.add(format);
83             ArrayList<String> listOfDecoders = selectHardwareCodecs(decoderMime, formatsList,
84                     null, false);
85             if (listOfDecoders.size() == 0) continue;
86             String decoder = listOfDecoders.get(0);
87             boolean[] boolStates = {true, false};
88             for (String encoderMime : getMimesOfAvailableHardwareVideoEncoders()) {
89                 // Calculate the bitrate based on the encode mime.
90                 int bitrate = (int)(((int) arg[1]) * getBitrateScalingFactor(encoderMime));
91                 MediaFormat mockFmt = setUpEncoderFormat(format, encoderMime, bitrate);
92                 ArrayList<MediaFormat> mockFmtList = new ArrayList<>();
93                 mockFmtList.add(mockFmt);
94                 ArrayList<String> listOfEncoders = selectHardwareCodecs(encoderMime, mockFmtList,
95                         null, true);
96                 for (String encoder : listOfEncoders) {
97                     for (int keyPriority : KEY_PRIORITIES_LIST) {
98                         for (float scalingFactor : SCALING_FACTORS_LIST) {
99                             for (boolean isAsync : boolStates) {
100                                 if (keyPriority == 1 || (scalingFactor > 0.0
101                                         && scalingFactor <= 1.0)) {
102                                     argsList.add(
103                                             new Object[]{decoder, arg[0], encoderMime, encoder,
104                                                     bitrate, keyPriority, scalingFactor, isAsync,
105                                                     0});
106                                     if (encoderMime.equals("video/avc") || encoderMime.equals(
107                                             "video/hevc")) {
108                                         argsList.add(
109                                                 new Object[]{decoder, arg[0], encoderMime, encoder,
110                                                         bitrate, keyPriority, scalingFactor,
111                                                         isAsync, 2});
112                                     }
113                                 }
114                             }
115                         }
116                     }
117                 }
118             }
119         }
120         return argsList;
121     }
122 
123     /**
124      * Validates performance of hardware accelerated video encoders
125      */
126     @ApiTest(apis = {"android.media.MediaFormat#KEY_PRIORITY",
127             "android.media.MediaFormat#KEY_OPERATING_RATE"})
128     @LargeTest
129     @Test(timeout = PER_TEST_TIMEOUT_LARGE_TEST_MS)
testPerformanceOfHardwareVideoEncoders()130     public void testPerformanceOfHardwareVideoEncoders() throws IOException, InterruptedException {
131         encode();
132         String log = String.format("DecodeMime: %s, Decoder: %s, resolution: %dp, EncodeMime: %s," +
133                 " Encoder: %s, Key-priority: %d :: ", mDecoderMime, mDecoderName, mHeight,
134                 mEncoderMime, mEncoderName, mKeyPriority);
135         int maxExpectedFps = getMaxExpectedFps(mWidth, mHeight);
136         double fpsToleranceFactor = FPS_TOLERANCE_FACTOR;
137         if (VNDK_VERSION <= Build.VERSION_CODES.TIRAMISU) {
138             fpsToleranceFactor = Math.min(0.9, fpsToleranceFactor);
139         }
140         double expectedFps =
141                 Math.min(mOperatingRateExpected * fpsToleranceFactor, maxExpectedFps);
142         Log.d(LOG_TAG, log + "act/exp fps: " + mAchievedFps + "/" + expectedFps);
143         assertTrue("Unable to achieve the expected rate. " + log + "act/exp fps: " + mAchievedFps
144                 + "/" + expectedFps, mAchievedFps >= expectedFps);
145     }
146 }
147