1 /*
2  * Copyright (C) 2023 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NativeVideoQualityUtils"
19 
20 #include <jni.h>
21 #include <log/log.h>
22 
23 #include <Eigen/Dense>
24 #include <Eigen/QR>
25 #include <string>
26 #include <vector>
27 
28 // Migrate this method to std::format when C++20 becomes available
29 template <typename... Args>
StringFormat(const std::string & format,Args...args)30 std::string StringFormat(const std::string& format, Args... args) {
31     auto size = std::snprintf(nullptr, 0, format.c_str(), args...);
32     if (size < 0) return {};
33     std::vector<char> buffer(size + 1); // Add 1 for terminating null byte
34     std::snprintf(buffer.data(), buffer.size(), format.c_str(), args...);
35     return std::string(buffer.data(), size); // Exclude the terminating null byte
36 }
37 
polyEval(std::vector<double> & coeffs,double x)38 double polyEval(std::vector<double>& coeffs, double x) {
39     double y = coeffs[0];
40     double xn = x;
41     for (int i = 1; i < coeffs.size(); i++) {
42         y += (coeffs[i] * xn);
43         xn *= x;
44     }
45     return y;
46 }
47 
polyIntegrate(std::vector<double> & coeffs,double coi=0.0)48 std::vector<double> polyIntegrate(std::vector<double>& coeffs, double coi = 0.0) {
49     std::vector<double> integratedCoeffs(coeffs.size() + 1);
50     integratedCoeffs[0] = coi; // constant of integration
51     for (int i = 1; i < coeffs.size() + 1; i++) {
52         integratedCoeffs[i] = coeffs[i - 1] / i;
53     }
54     return integratedCoeffs;
55 }
56 
polyFit(std::vector<double> & rates,std::vector<double> & qualities,int order)57 std::vector<double> polyFit(std::vector<double>& rates, std::vector<double>& qualities, int order) {
58     // y = X * a, y is vector of qualities, X is vandermonde matrix and a is vector of coeffs.
59     Eigen::MatrixXd X(rates.size(), order + 1);
60     Eigen::MatrixXd y(qualities.size(), 1);
61     for (int i = 0; i < rates.size(); ++i) {
62         y(i, 0) = qualities[i];
63         double element = 1;
64         for (int j = 0; j < order + 1; ++j) {
65             X(i, j) = element;
66             element *= rates[i];
67         }
68     }
69     // QR decomposition
70     Eigen::MatrixXd a = X.colPivHouseholderQr().solve(y);
71     std::vector<double> coeffs(order + 1);
72     for (int i = 0; i < order + 1; i++) {
73         coeffs[i] = a(i, 0);
74     }
75     return coeffs;
76 }
77 
getAvgImprovement(std::vector<double> & xA,std::vector<double> & yA,std::vector<double> & xB,std::vector<double> & yB,int order)78 double getAvgImprovement(std::vector<double>& xA, std::vector<double>& yA, std::vector<double>& xB,
79                          std::vector<double>& yB, int order) {
80     std::vector<double> coeffsA = polyFit(xA, yA, order);
81     std::vector<double> coeffsB = polyFit(xB, yB, order);
82     std::vector<double> integratedCoeffsA = polyIntegrate(coeffsA);
83     std::vector<double> integratedCoeffsB = polyIntegrate(coeffsB);
84     double minX = std::max(*std::min_element(xA.begin(), xA.end()),
85                            *std::min_element(xB.begin(), xB.end()));
86     double maxX = std::min(*std::max_element(xA.begin(), xA.end()),
87                            *std::max_element(xB.begin(), xB.end()));
88     double areaA = polyEval(integratedCoeffsA, maxX) - polyEval(integratedCoeffsA, minX);
89     double areaB = polyEval(integratedCoeffsB, maxX) - polyEval(integratedCoeffsB, minX);
90     return (areaB - areaA) / (maxX - minX);
91 }
92 
nativeGetBDRate(JNIEnv * env,jobject,jdoubleArray jQualityA,jdoubleArray jRatesA,jdoubleArray jQualityB,jdoubleArray jRatesB,jboolean selBdSnr,jobject jRetMsg)93 static jdouble nativeGetBDRate(JNIEnv* env, jobject, jdoubleArray jQualityA, jdoubleArray jRatesA,
94                                jdoubleArray jQualityB, jdoubleArray jRatesB, jboolean selBdSnr,
95                                jobject jRetMsg) {
96     jsize len[4]{env->GetArrayLength(jQualityA), env->GetArrayLength(jRatesA),
97                  env->GetArrayLength(jQualityB), env->GetArrayLength(jRatesB)};
98     std::string msg;
99     if (len[0] != len[1] || len[0] != len[2] || len[0] != len[3]) {
100         msg = StringFormat("array length of quality and bit rates for set A/B are not same, "
101                            "lengths are %d %d %d %d \n",
102                            (int)len[0], (int)len[1], (int)len[2], (int)len[3]);
103     } else if (len[0] < 4) {
104         msg = StringFormat("too few data-points present for bd rate analysis, count %d \n", len[0]);
105     } else {
106         std::vector<double> ratesA(len[0]);
107         env->GetDoubleArrayRegion(jRatesA, 0, len[0], &ratesA[0]);
108         std::vector<double> ratesB(len[0]);
109         env->GetDoubleArrayRegion(jRatesB, 0, len[0], &ratesB[0]);
110         std::vector<double> qualitiesA(len[0]);
111         env->GetDoubleArrayRegion(jQualityA, 0, len[0], &qualitiesA[0]);
112         std::vector<double> qualitiesB(len[0]);
113         env->GetDoubleArrayRegion(jQualityB, 0, len[0], &qualitiesB[0]);
114         // log rate
115         for (int i = 0; i < len[0]; i++) {
116             ratesA[i] = std::log(ratesA[i]);
117             ratesB[i] = std::log(ratesB[i]);
118         }
119         const int order = 3;
120         if (selBdSnr) {
121             return getAvgImprovement(ratesA, qualitiesA, ratesB, qualitiesB, order);
122         } else {
123             double bdRate = getAvgImprovement(qualitiesA, ratesA, qualitiesB, ratesB, order);
124             // In really bad formed data the exponent can grow too large clamp it.
125             if (bdRate > 200) {
126                 bdRate = 200;
127             }
128             bdRate = (std::exp(bdRate) - 1) * 100;
129             return bdRate;
130         }
131     }
132     jclass clazz = env->GetObjectClass(jRetMsg);
133     jmethodID mId =
134             env->GetMethodID(clazz, "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
135     env->CallObjectMethod(jRetMsg, mId, env->NewStringUTF(msg.c_str()));
136     return 0;
137 }
138 
registerAndroidVideoCodecCtsVQUtils(JNIEnv * env)139 int registerAndroidVideoCodecCtsVQUtils(JNIEnv* env) {
140     const JNINativeMethod methodTable[] = {
141             {"nativeGetBDRate", "([D[D[D[DZLjava/lang/StringBuilder;)D", (void*)nativeGetBDRate},
142     };
143     jclass c = env->FindClass("android/videocodec/cts/VideoEncoderQualityRegressionTestBase");
144     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
145 }
146 
JNI_OnLoad(JavaVM * vm,void *)147 extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
148     JNIEnv* env;
149     if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) return JNI_ERR;
150     if (registerAndroidVideoCodecCtsVQUtils(env) != JNI_OK) return JNI_ERR;
151     return JNI_VERSION_1_6;
152 }
153