xref: /aosp_15_r20/external/libultrahdr/benchmark/benchmark_test.cpp (revision 89a0ef05262152531a00a15832a2d3b1e3990773)
1*89a0ef05SAndroid Build Coastguard Worker /*
2*89a0ef05SAndroid Build Coastguard Worker  * Copyright 2023 The Android Open Source Project
3*89a0ef05SAndroid Build Coastguard Worker  *
4*89a0ef05SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*89a0ef05SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*89a0ef05SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*89a0ef05SAndroid Build Coastguard Worker  *
8*89a0ef05SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*89a0ef05SAndroid Build Coastguard Worker  *
10*89a0ef05SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*89a0ef05SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*89a0ef05SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*89a0ef05SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*89a0ef05SAndroid Build Coastguard Worker  * limitations under the License.
15*89a0ef05SAndroid Build Coastguard Worker  */
16*89a0ef05SAndroid Build Coastguard Worker 
17*89a0ef05SAndroid Build Coastguard Worker #include <fstream>
18*89a0ef05SAndroid Build Coastguard Worker #include <iostream>
19*89a0ef05SAndroid Build Coastguard Worker #include <cstring>
20*89a0ef05SAndroid Build Coastguard Worker 
21*89a0ef05SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
22*89a0ef05SAndroid Build Coastguard Worker 
23*89a0ef05SAndroid Build Coastguard Worker #include "ultrahdr_api.h"
24*89a0ef05SAndroid Build Coastguard Worker 
25*89a0ef05SAndroid Build Coastguard Worker #ifdef __ANDROID__
26*89a0ef05SAndroid Build Coastguard Worker std::string kTestImagesPath = "/sdcard/test/UltrahdrBenchmarkTestRes-1.2/";
27*89a0ef05SAndroid Build Coastguard Worker 
28*89a0ef05SAndroid Build Coastguard Worker #ifdef LOG_NDEBUG
29*89a0ef05SAndroid Build Coastguard Worker #include "android/log.h"
30*89a0ef05SAndroid Build Coastguard Worker 
31*89a0ef05SAndroid Build Coastguard Worker #ifndef LOG_TAG
32*89a0ef05SAndroid Build Coastguard Worker #define LOG_TAG "UHDR_BENCHMARK"
33*89a0ef05SAndroid Build Coastguard Worker #endif
34*89a0ef05SAndroid Build Coastguard Worker 
35*89a0ef05SAndroid Build Coastguard Worker #ifndef ALOGE
36*89a0ef05SAndroid Build Coastguard Worker #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
37*89a0ef05SAndroid Build Coastguard Worker #endif
38*89a0ef05SAndroid Build Coastguard Worker 
39*89a0ef05SAndroid Build Coastguard Worker #else
40*89a0ef05SAndroid Build Coastguard Worker #define ALOGE(...) ((void)0)
41*89a0ef05SAndroid Build Coastguard Worker #endif
42*89a0ef05SAndroid Build Coastguard Worker 
43*89a0ef05SAndroid Build Coastguard Worker #else
44*89a0ef05SAndroid Build Coastguard Worker std::string kTestImagesPath = "./data/UltrahdrBenchmarkTestRes-1.2/";
45*89a0ef05SAndroid Build Coastguard Worker 
46*89a0ef05SAndroid Build Coastguard Worker #ifdef LOG_NDEBUG
47*89a0ef05SAndroid Build Coastguard Worker #include <cstdio>
48*89a0ef05SAndroid Build Coastguard Worker 
49*89a0ef05SAndroid Build Coastguard Worker #define ALOGE(...)                \
50*89a0ef05SAndroid Build Coastguard Worker   do {                            \
51*89a0ef05SAndroid Build Coastguard Worker     fprintf(stderr, __VA_ARGS__); \
52*89a0ef05SAndroid Build Coastguard Worker     fprintf(stderr, "\n");        \
53*89a0ef05SAndroid Build Coastguard Worker   } while (0)
54*89a0ef05SAndroid Build Coastguard Worker 
55*89a0ef05SAndroid Build Coastguard Worker #else
56*89a0ef05SAndroid Build Coastguard Worker #define ALOGE(...) ((void)0)
57*89a0ef05SAndroid Build Coastguard Worker #endif
58*89a0ef05SAndroid Build Coastguard Worker 
59*89a0ef05SAndroid Build Coastguard Worker #endif
60*89a0ef05SAndroid Build Coastguard Worker 
61*89a0ef05SAndroid Build Coastguard Worker std::vector<std::string> kDecodeAPITestImages = {
62*89a0ef05SAndroid Build Coastguard Worker     "mountains_singlechannelgainmap.jpg",
63*89a0ef05SAndroid Build Coastguard Worker     "mountains_multichannelgainmap.jpg",
64*89a0ef05SAndroid Build Coastguard Worker     "mountains_singlechannelgamma.jpg",
65*89a0ef05SAndroid Build Coastguard Worker     "mountains_multichannelgamma.jpg",
66*89a0ef05SAndroid Build Coastguard Worker };
67*89a0ef05SAndroid Build Coastguard Worker 
68*89a0ef05SAndroid Build Coastguard Worker std::vector<std::string> kEncodeApi0TestImages12MpName = {
69*89a0ef05SAndroid Build Coastguard Worker     "mountains_rgba1010102.raw",
70*89a0ef05SAndroid Build Coastguard Worker     "mountains_rgba16F.raw",
71*89a0ef05SAndroid Build Coastguard Worker     "mountains_p010.p010",
72*89a0ef05SAndroid Build Coastguard Worker };
73*89a0ef05SAndroid Build Coastguard Worker 
74*89a0ef05SAndroid Build Coastguard Worker std::vector<std::pair<std::string, std::string>> kEncodeApi1TestImages12MpName = {
75*89a0ef05SAndroid Build Coastguard Worker     {"mountains_rgba1010102.raw", "mountains_rgba8888.raw"},
76*89a0ef05SAndroid Build Coastguard Worker     {"mountains_rgba16F.raw", "mountains_rgba8888.raw"},
77*89a0ef05SAndroid Build Coastguard Worker     {"mountains_p010.p010", "mountains_yuv420.yuv"},
78*89a0ef05SAndroid Build Coastguard Worker };
79*89a0ef05SAndroid Build Coastguard Worker 
80*89a0ef05SAndroid Build Coastguard Worker using TestParamsDecodeAPI = std::tuple<std::string, uhdr_color_transfer_t, uhdr_img_fmt_t, bool>;
81*89a0ef05SAndroid Build Coastguard Worker using TestParamsEncoderAPI0 =
82*89a0ef05SAndroid Build Coastguard Worker     std::tuple<std::string, int, int, uhdr_color_gamut_t, uhdr_color_transfer_t, int, float>;
83*89a0ef05SAndroid Build Coastguard Worker using TestParamsEncoderAPI1 =
84*89a0ef05SAndroid Build Coastguard Worker     std::tuple<std::string, std::string, int, int, uhdr_color_gamut_t, uhdr_color_transfer_t,
85*89a0ef05SAndroid Build Coastguard Worker                uhdr_color_gamut_t, int, float, uhdr_enc_preset_t>;
86*89a0ef05SAndroid Build Coastguard Worker 
87*89a0ef05SAndroid Build Coastguard Worker std::vector<TestParamsDecodeAPI> testParamsDecodeAPI;
88*89a0ef05SAndroid Build Coastguard Worker std::vector<TestParamsEncoderAPI0> testParamsAPI0;
89*89a0ef05SAndroid Build Coastguard Worker std::vector<TestParamsEncoderAPI1> testParamsAPI1;
90*89a0ef05SAndroid Build Coastguard Worker 
imgFmtToString(const uhdr_img_fmt of)91*89a0ef05SAndroid Build Coastguard Worker std::string imgFmtToString(const uhdr_img_fmt of) {
92*89a0ef05SAndroid Build Coastguard Worker   switch (of) {
93*89a0ef05SAndroid Build Coastguard Worker     case UHDR_IMG_FMT_32bppRGBA8888:
94*89a0ef05SAndroid Build Coastguard Worker       return "rgba8888";
95*89a0ef05SAndroid Build Coastguard Worker     case UHDR_IMG_FMT_64bppRGBAHalfFloat:
96*89a0ef05SAndroid Build Coastguard Worker       return "64rgbaHalftoFloat";
97*89a0ef05SAndroid Build Coastguard Worker     case UHDR_IMG_FMT_32bppRGBA1010102:
98*89a0ef05SAndroid Build Coastguard Worker       return "rgba1010102";
99*89a0ef05SAndroid Build Coastguard Worker     default:
100*89a0ef05SAndroid Build Coastguard Worker       return "Unknown";
101*89a0ef05SAndroid Build Coastguard Worker   }
102*89a0ef05SAndroid Build Coastguard Worker }
103*89a0ef05SAndroid Build Coastguard Worker 
colorGamutToString(const uhdr_color_gamut_t cg)104*89a0ef05SAndroid Build Coastguard Worker std::string colorGamutToString(const uhdr_color_gamut_t cg) {
105*89a0ef05SAndroid Build Coastguard Worker   switch (cg) {
106*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CG_BT_709:
107*89a0ef05SAndroid Build Coastguard Worker       return "bt709";
108*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CG_DISPLAY_P3:
109*89a0ef05SAndroid Build Coastguard Worker       return "p3";
110*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CG_BT_2100:
111*89a0ef05SAndroid Build Coastguard Worker       return "bt2100";
112*89a0ef05SAndroid Build Coastguard Worker     default:
113*89a0ef05SAndroid Build Coastguard Worker       return "Unknown";
114*89a0ef05SAndroid Build Coastguard Worker   }
115*89a0ef05SAndroid Build Coastguard Worker }
116*89a0ef05SAndroid Build Coastguard Worker 
tfToString(const uhdr_color_transfer_t of)117*89a0ef05SAndroid Build Coastguard Worker std::string tfToString(const uhdr_color_transfer_t of) {
118*89a0ef05SAndroid Build Coastguard Worker   switch (of) {
119*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CT_LINEAR:
120*89a0ef05SAndroid Build Coastguard Worker       return "linear";
121*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CT_HLG:
122*89a0ef05SAndroid Build Coastguard Worker       return "hlg";
123*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CT_PQ:
124*89a0ef05SAndroid Build Coastguard Worker       return "pq";
125*89a0ef05SAndroid Build Coastguard Worker     case UHDR_CT_SRGB:
126*89a0ef05SAndroid Build Coastguard Worker       return "srgb";
127*89a0ef05SAndroid Build Coastguard Worker     default:
128*89a0ef05SAndroid Build Coastguard Worker       return "Unknown";
129*89a0ef05SAndroid Build Coastguard Worker   }
130*89a0ef05SAndroid Build Coastguard Worker }
131*89a0ef05SAndroid Build Coastguard Worker 
132*89a0ef05SAndroid Build Coastguard Worker #define READ_BYTES(DESC, ADDR, LEN)                                         \
133*89a0ef05SAndroid Build Coastguard Worker   DESC.read(static_cast<char*>(ADDR), (LEN));                               \
134*89a0ef05SAndroid Build Coastguard Worker   if (DESC.gcount() != (LEN)) {                                             \
135*89a0ef05SAndroid Build Coastguard Worker     ALOGE("Failed to read: %u bytes, read: %zu bytes", LEN, DESC.gcount()); \
136*89a0ef05SAndroid Build Coastguard Worker     return false;                                                           \
137*89a0ef05SAndroid Build Coastguard Worker   }
138*89a0ef05SAndroid Build Coastguard Worker 
loadFile(const char * filename,uhdr_raw_image_t * handle)139*89a0ef05SAndroid Build Coastguard Worker static bool loadFile(const char* filename, uhdr_raw_image_t* handle) {
140*89a0ef05SAndroid Build Coastguard Worker   std::ifstream ifd(filename, std::ios::binary);
141*89a0ef05SAndroid Build Coastguard Worker   if (ifd.good()) {
142*89a0ef05SAndroid Build Coastguard Worker     if (handle->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
143*89a0ef05SAndroid Build Coastguard Worker       const int bpp = 2;
144*89a0ef05SAndroid Build Coastguard Worker       READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], handle->w * handle->h * bpp)
145*89a0ef05SAndroid Build Coastguard Worker       READ_BYTES(ifd, handle->planes[UHDR_PLANE_UV], (handle->w / 2) * (handle->h / 2) * bpp * 2)
146*89a0ef05SAndroid Build Coastguard Worker       return true;
147*89a0ef05SAndroid Build Coastguard Worker     } else if (handle->fmt == UHDR_IMG_FMT_32bppRGBA1010102 ||
148*89a0ef05SAndroid Build Coastguard Worker                handle->fmt == UHDR_IMG_FMT_32bppRGBA8888 ||
149*89a0ef05SAndroid Build Coastguard Worker                handle->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat) {
150*89a0ef05SAndroid Build Coastguard Worker       const int bpp = handle->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat ? 8 : 4;
151*89a0ef05SAndroid Build Coastguard Worker       READ_BYTES(ifd, handle->planes[UHDR_PLANE_PACKED], handle->w * handle->h * bpp)
152*89a0ef05SAndroid Build Coastguard Worker       return true;
153*89a0ef05SAndroid Build Coastguard Worker     } else if (handle->fmt == UHDR_IMG_FMT_12bppYCbCr420) {
154*89a0ef05SAndroid Build Coastguard Worker       READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], handle->w * handle->h)
155*89a0ef05SAndroid Build Coastguard Worker       READ_BYTES(ifd, handle->planes[UHDR_PLANE_U], (handle->w / 2) * (handle->h / 2))
156*89a0ef05SAndroid Build Coastguard Worker       READ_BYTES(ifd, handle->planes[UHDR_PLANE_V], (handle->w / 2) * (handle->h / 2))
157*89a0ef05SAndroid Build Coastguard Worker       return true;
158*89a0ef05SAndroid Build Coastguard Worker     }
159*89a0ef05SAndroid Build Coastguard Worker     return false;
160*89a0ef05SAndroid Build Coastguard Worker   }
161*89a0ef05SAndroid Build Coastguard Worker   ALOGE("Unable to open file: %s", filename);
162*89a0ef05SAndroid Build Coastguard Worker   return false;
163*89a0ef05SAndroid Build Coastguard Worker }
164*89a0ef05SAndroid Build Coastguard Worker 
loadFile(const char * filename,void * & result,int length)165*89a0ef05SAndroid Build Coastguard Worker static bool loadFile(const char* filename, void*& result, int length) {
166*89a0ef05SAndroid Build Coastguard Worker   std::ifstream ifd(filename, std::ios::binary | std::ios::ate);
167*89a0ef05SAndroid Build Coastguard Worker   if (ifd.good()) {
168*89a0ef05SAndroid Build Coastguard Worker     int size = ifd.tellg();
169*89a0ef05SAndroid Build Coastguard Worker     if (size < length) {
170*89a0ef05SAndroid Build Coastguard Worker       ALOGE("Requested to read %d bytes from file: %s, file contains only %d bytes", length,
171*89a0ef05SAndroid Build Coastguard Worker             filename, size);
172*89a0ef05SAndroid Build Coastguard Worker       return false;
173*89a0ef05SAndroid Build Coastguard Worker     }
174*89a0ef05SAndroid Build Coastguard Worker     ifd.seekg(0, std::ios::beg);
175*89a0ef05SAndroid Build Coastguard Worker     result = malloc(length);
176*89a0ef05SAndroid Build Coastguard Worker     if (result == nullptr) {
177*89a0ef05SAndroid Build Coastguard Worker       ALOGE("Failed to allocate memory to store contents of file: %s", filename);
178*89a0ef05SAndroid Build Coastguard Worker       return false;
179*89a0ef05SAndroid Build Coastguard Worker     }
180*89a0ef05SAndroid Build Coastguard Worker     READ_BYTES(ifd, result, length)
181*89a0ef05SAndroid Build Coastguard Worker     return true;
182*89a0ef05SAndroid Build Coastguard Worker   }
183*89a0ef05SAndroid Build Coastguard Worker   ALOGE("Unable to open file: %s", filename);
184*89a0ef05SAndroid Build Coastguard Worker   return false;
185*89a0ef05SAndroid Build Coastguard Worker }
186*89a0ef05SAndroid Build Coastguard Worker 
187*89a0ef05SAndroid Build Coastguard Worker class DecBenchmark {
188*89a0ef05SAndroid Build Coastguard Worker  public:
189*89a0ef05SAndroid Build Coastguard Worker   std::string mUhdrFile;
190*89a0ef05SAndroid Build Coastguard Worker   uhdr_color_transfer_t mTf;
191*89a0ef05SAndroid Build Coastguard Worker   uhdr_img_fmt_t mOfmt;
192*89a0ef05SAndroid Build Coastguard Worker   bool mEnableGLES;
193*89a0ef05SAndroid Build Coastguard Worker 
194*89a0ef05SAndroid Build Coastguard Worker   uhdr_compressed_image_t mUhdrImg{};
195*89a0ef05SAndroid Build Coastguard Worker 
DecBenchmark(TestParamsDecodeAPI testParams)196*89a0ef05SAndroid Build Coastguard Worker   DecBenchmark(TestParamsDecodeAPI testParams) {
197*89a0ef05SAndroid Build Coastguard Worker     mUhdrFile = std::get<0>(testParams);
198*89a0ef05SAndroid Build Coastguard Worker     mTf = std::get<1>(testParams);
199*89a0ef05SAndroid Build Coastguard Worker     mOfmt = std::get<2>(testParams);
200*89a0ef05SAndroid Build Coastguard Worker     mEnableGLES = std::get<3>(testParams);
201*89a0ef05SAndroid Build Coastguard Worker   }
~DecBenchmark()202*89a0ef05SAndroid Build Coastguard Worker   ~DecBenchmark() {
203*89a0ef05SAndroid Build Coastguard Worker     if (mUhdrImg.data) {
204*89a0ef05SAndroid Build Coastguard Worker       free(mUhdrImg.data);
205*89a0ef05SAndroid Build Coastguard Worker       mUhdrImg.data = nullptr;
206*89a0ef05SAndroid Build Coastguard Worker     }
207*89a0ef05SAndroid Build Coastguard Worker   }
208*89a0ef05SAndroid Build Coastguard Worker 
209*89a0ef05SAndroid Build Coastguard Worker   bool fillJpegImageHandle(uhdr_compressed_image_t* uhdrImg, std::string mUhdrFile);
210*89a0ef05SAndroid Build Coastguard Worker };
211*89a0ef05SAndroid Build Coastguard Worker 
fillJpegImageHandle(uhdr_compressed_image_t * uhdrImg,std::string filename)212*89a0ef05SAndroid Build Coastguard Worker bool DecBenchmark::fillJpegImageHandle(uhdr_compressed_image_t* uhdrImg, std::string filename) {
213*89a0ef05SAndroid Build Coastguard Worker   std::ifstream ifd(filename, std::ios::binary | std::ios::ate);
214*89a0ef05SAndroid Build Coastguard Worker   if (ifd.good()) {
215*89a0ef05SAndroid Build Coastguard Worker     int size = ifd.tellg();
216*89a0ef05SAndroid Build Coastguard Worker     uhdrImg->capacity = size;
217*89a0ef05SAndroid Build Coastguard Worker     uhdrImg->data_sz = size;
218*89a0ef05SAndroid Build Coastguard Worker     uhdrImg->data = nullptr;
219*89a0ef05SAndroid Build Coastguard Worker     uhdrImg->cg = UHDR_CG_UNSPECIFIED;
220*89a0ef05SAndroid Build Coastguard Worker     uhdrImg->ct = UHDR_CT_UNSPECIFIED;
221*89a0ef05SAndroid Build Coastguard Worker     uhdrImg->range = UHDR_CR_UNSPECIFIED;
222*89a0ef05SAndroid Build Coastguard Worker     ifd.close();
223*89a0ef05SAndroid Build Coastguard Worker     return loadFile(filename.c_str(), uhdrImg->data, size);
224*89a0ef05SAndroid Build Coastguard Worker   }
225*89a0ef05SAndroid Build Coastguard Worker   return false;
226*89a0ef05SAndroid Build Coastguard Worker }
227*89a0ef05SAndroid Build Coastguard Worker 
228*89a0ef05SAndroid Build Coastguard Worker class EncBenchmark {
229*89a0ef05SAndroid Build Coastguard Worker  public:
230*89a0ef05SAndroid Build Coastguard Worker   std::string mHdrFile, mSdrFile;
231*89a0ef05SAndroid Build Coastguard Worker   uhdr_color_gamut_t mHdrCg, mSdrCg;
232*89a0ef05SAndroid Build Coastguard Worker   uhdr_img_fmt_t mHdrCf, mSdrCf;
233*89a0ef05SAndroid Build Coastguard Worker   int mWidth, mHeight;
234*89a0ef05SAndroid Build Coastguard Worker   uhdr_color_transfer_t mHdrCt, mSdrCt = UHDR_CT_SRGB;
235*89a0ef05SAndroid Build Coastguard Worker   int mUseMultiChannelGainMap;
236*89a0ef05SAndroid Build Coastguard Worker   int mMapDimensionScaleFactor = 1;
237*89a0ef05SAndroid Build Coastguard Worker   float mGamma;
238*89a0ef05SAndroid Build Coastguard Worker   uhdr_enc_preset_t mEncPreset;
239*89a0ef05SAndroid Build Coastguard Worker 
240*89a0ef05SAndroid Build Coastguard Worker   uhdr_raw_image_t mHdrImg{}, mSdrImg{};
241*89a0ef05SAndroid Build Coastguard Worker 
EncBenchmark(TestParamsEncoderAPI0 testParams)242*89a0ef05SAndroid Build Coastguard Worker   EncBenchmark(TestParamsEncoderAPI0 testParams) {
243*89a0ef05SAndroid Build Coastguard Worker     mHdrFile = std::get<0>(testParams);
244*89a0ef05SAndroid Build Coastguard Worker     mWidth = std::get<1>(testParams);
245*89a0ef05SAndroid Build Coastguard Worker     mHeight = std::get<2>(testParams);
246*89a0ef05SAndroid Build Coastguard Worker     mHdrCg = std::get<3>(testParams);
247*89a0ef05SAndroid Build Coastguard Worker     mHdrCt = std::get<4>(testParams);
248*89a0ef05SAndroid Build Coastguard Worker     mUseMultiChannelGainMap = std::get<5>(testParams);
249*89a0ef05SAndroid Build Coastguard Worker     mGamma = std::get<6>(testParams);
250*89a0ef05SAndroid Build Coastguard Worker   };
251*89a0ef05SAndroid Build Coastguard Worker 
EncBenchmark(TestParamsEncoderAPI1 testParams)252*89a0ef05SAndroid Build Coastguard Worker   EncBenchmark(TestParamsEncoderAPI1 testParams) {
253*89a0ef05SAndroid Build Coastguard Worker     mHdrFile = std::get<0>(testParams);
254*89a0ef05SAndroid Build Coastguard Worker     mSdrFile = std::get<1>(testParams);
255*89a0ef05SAndroid Build Coastguard Worker     mWidth = std::get<2>(testParams);
256*89a0ef05SAndroid Build Coastguard Worker     mHeight = std::get<3>(testParams);
257*89a0ef05SAndroid Build Coastguard Worker     mHdrCg = std::get<4>(testParams);
258*89a0ef05SAndroid Build Coastguard Worker     mHdrCt = std::get<5>(testParams);
259*89a0ef05SAndroid Build Coastguard Worker     mSdrCg = std::get<6>(testParams);
260*89a0ef05SAndroid Build Coastguard Worker     mUseMultiChannelGainMap = std::get<7>(testParams);
261*89a0ef05SAndroid Build Coastguard Worker     mGamma = std::get<8>(testParams);
262*89a0ef05SAndroid Build Coastguard Worker     mEncPreset = std::get<9>(testParams);
263*89a0ef05SAndroid Build Coastguard Worker   }
264*89a0ef05SAndroid Build Coastguard Worker 
~EncBenchmark()265*89a0ef05SAndroid Build Coastguard Worker   ~EncBenchmark() {
266*89a0ef05SAndroid Build Coastguard Worker     int count = sizeof mHdrImg.planes / sizeof mHdrImg.planes[0];
267*89a0ef05SAndroid Build Coastguard Worker     for (int i = 0; i < count; i++) {
268*89a0ef05SAndroid Build Coastguard Worker       if (mHdrImg.planes[i]) {
269*89a0ef05SAndroid Build Coastguard Worker         free(mHdrImg.planes[i]);
270*89a0ef05SAndroid Build Coastguard Worker         mHdrImg.planes[i] = nullptr;
271*89a0ef05SAndroid Build Coastguard Worker       }
272*89a0ef05SAndroid Build Coastguard Worker       if (mSdrImg.planes[i]) {
273*89a0ef05SAndroid Build Coastguard Worker         free(mSdrImg.planes[i]);
274*89a0ef05SAndroid Build Coastguard Worker         mSdrImg.planes[i] = nullptr;
275*89a0ef05SAndroid Build Coastguard Worker       }
276*89a0ef05SAndroid Build Coastguard Worker     }
277*89a0ef05SAndroid Build Coastguard Worker   }
278*89a0ef05SAndroid Build Coastguard Worker 
279*89a0ef05SAndroid Build Coastguard Worker   bool fillRawImageHandle(uhdr_raw_image_t* rawImg, int width, int height, std::string file,
280*89a0ef05SAndroid Build Coastguard Worker                           uhdr_img_fmt_t cf, uhdr_color_gamut_t cg, uhdr_color_transfer_t ct);
281*89a0ef05SAndroid Build Coastguard Worker };
282*89a0ef05SAndroid Build Coastguard Worker 
fillRawImageHandle(uhdr_raw_image_t * rawImg,int width,int height,std::string file,uhdr_img_fmt_t cf,uhdr_color_gamut_t cg,uhdr_color_transfer_t ct)283*89a0ef05SAndroid Build Coastguard Worker bool EncBenchmark::fillRawImageHandle(uhdr_raw_image_t* rawImg, int width, int height,
284*89a0ef05SAndroid Build Coastguard Worker                                       std::string file, uhdr_img_fmt_t cf, uhdr_color_gamut_t cg,
285*89a0ef05SAndroid Build Coastguard Worker                                       uhdr_color_transfer_t ct) {
286*89a0ef05SAndroid Build Coastguard Worker   rawImg->fmt = cf;
287*89a0ef05SAndroid Build Coastguard Worker   rawImg->cg = cg;
288*89a0ef05SAndroid Build Coastguard Worker   rawImg->ct = ct;
289*89a0ef05SAndroid Build Coastguard Worker   rawImg->w = width;
290*89a0ef05SAndroid Build Coastguard Worker   rawImg->h = height;
291*89a0ef05SAndroid Build Coastguard Worker   if (cf == UHDR_IMG_FMT_24bppYCbCrP010) {
292*89a0ef05SAndroid Build Coastguard Worker     const int bpp = 2;
293*89a0ef05SAndroid Build Coastguard Worker     rawImg->range = std::rand() % 2 ? UHDR_CR_FULL_RANGE : UHDR_CR_LIMITED_RANGE;
294*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_Y] = malloc(width * height * bpp);
295*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_UV] = malloc((width / 2) * (height / 2) * bpp * 2);
296*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_V] = nullptr;
297*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_Y] = width;
298*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_UV] = width;
299*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_V] = 0;
300*89a0ef05SAndroid Build Coastguard Worker     return loadFile(file.c_str(), rawImg);
301*89a0ef05SAndroid Build Coastguard Worker   } else if (cf == UHDR_IMG_FMT_32bppRGBA1010102 || cf == UHDR_IMG_FMT_32bppRGBA8888 ||
302*89a0ef05SAndroid Build Coastguard Worker              cf == UHDR_IMG_FMT_64bppRGBAHalfFloat) {
303*89a0ef05SAndroid Build Coastguard Worker     const int bpp = cf == UHDR_IMG_FMT_64bppRGBAHalfFloat ? 8 : 4;
304*89a0ef05SAndroid Build Coastguard Worker     rawImg->range = UHDR_CR_FULL_RANGE;
305*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_PACKED] = malloc(width * height * bpp);
306*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_UV] = nullptr;
307*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_V] = nullptr;
308*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_PACKED] = width;
309*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_UV] = 0;
310*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_V] = 0;
311*89a0ef05SAndroid Build Coastguard Worker     return loadFile(file.c_str(), rawImg);
312*89a0ef05SAndroid Build Coastguard Worker   } else if (cf == UHDR_IMG_FMT_12bppYCbCr420) {
313*89a0ef05SAndroid Build Coastguard Worker     rawImg->range = UHDR_CR_FULL_RANGE;
314*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_Y] = malloc(width * height);
315*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_U] = malloc((width / 2) * (height / 2));
316*89a0ef05SAndroid Build Coastguard Worker     rawImg->planes[UHDR_PLANE_V] = malloc((width / 2) * (height / 2));
317*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_Y] = width;
318*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_U] = width / 2;
319*89a0ef05SAndroid Build Coastguard Worker     rawImg->stride[UHDR_PLANE_V] = width / 2;
320*89a0ef05SAndroid Build Coastguard Worker     return loadFile(file.c_str(), rawImg);
321*89a0ef05SAndroid Build Coastguard Worker   }
322*89a0ef05SAndroid Build Coastguard Worker   return false;
323*89a0ef05SAndroid Build Coastguard Worker }
324*89a0ef05SAndroid Build Coastguard Worker 
BM_UHDRDecode(benchmark::State & s,TestParamsDecodeAPI testVectors)325*89a0ef05SAndroid Build Coastguard Worker static void BM_UHDRDecode(benchmark::State& s, TestParamsDecodeAPI testVectors) {
326*89a0ef05SAndroid Build Coastguard Worker   DecBenchmark benchmark(testVectors);
327*89a0ef05SAndroid Build Coastguard Worker 
328*89a0ef05SAndroid Build Coastguard Worker   s.SetLabel(benchmark.mUhdrFile + ", OutputFormat: " + imgFmtToString(benchmark.mOfmt) +
329*89a0ef05SAndroid Build Coastguard Worker              ", ColorTransfer: " + tfToString(benchmark.mTf) +
330*89a0ef05SAndroid Build Coastguard Worker              ", enableGLES: " + (benchmark.mEnableGLES ? "true" : "false"));
331*89a0ef05SAndroid Build Coastguard Worker 
332*89a0ef05SAndroid Build Coastguard Worker   benchmark.mUhdrFile = kTestImagesPath + "jpegr/" + benchmark.mUhdrFile;
333*89a0ef05SAndroid Build Coastguard Worker 
334*89a0ef05SAndroid Build Coastguard Worker   if (!benchmark.fillJpegImageHandle(&benchmark.mUhdrImg, benchmark.mUhdrFile)) {
335*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("unable to load file : " + benchmark.mUhdrFile);
336*89a0ef05SAndroid Build Coastguard Worker     return;
337*89a0ef05SAndroid Build Coastguard Worker   }
338*89a0ef05SAndroid Build Coastguard Worker 
339*89a0ef05SAndroid Build Coastguard Worker #define RET_IF_ERR(x)                                                       \
340*89a0ef05SAndroid Build Coastguard Worker   {                                                                         \
341*89a0ef05SAndroid Build Coastguard Worker     uhdr_error_info_t status = (x);                                         \
342*89a0ef05SAndroid Build Coastguard Worker     if (status.error_code != UHDR_CODEC_OK) {                               \
343*89a0ef05SAndroid Build Coastguard Worker       uhdr_release_decoder(decHandle);                                      \
344*89a0ef05SAndroid Build Coastguard Worker       s.SkipWithError(status.has_detail ? status.detail : "Unknown error"); \
345*89a0ef05SAndroid Build Coastguard Worker       return;                                                               \
346*89a0ef05SAndroid Build Coastguard Worker     }                                                                       \
347*89a0ef05SAndroid Build Coastguard Worker   }
348*89a0ef05SAndroid Build Coastguard Worker 
349*89a0ef05SAndroid Build Coastguard Worker   uhdr_codec_private_t* decHandle = uhdr_create_decoder();
350*89a0ef05SAndroid Build Coastguard Worker   for (auto _ : s) {
351*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_dec_set_image(decHandle, &benchmark.mUhdrImg))
352*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_dec_set_out_color_transfer(decHandle, benchmark.mTf))
353*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_dec_set_out_img_format(decHandle, benchmark.mOfmt))
354*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enable_gpu_acceleration(decHandle, benchmark.mEnableGLES))
355*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_decode(decHandle))
356*89a0ef05SAndroid Build Coastguard Worker     uhdr_reset_decoder(decHandle);
357*89a0ef05SAndroid Build Coastguard Worker   }
358*89a0ef05SAndroid Build Coastguard Worker   uhdr_release_decoder(decHandle);
359*89a0ef05SAndroid Build Coastguard Worker #undef RET_IF_ERR
360*89a0ef05SAndroid Build Coastguard Worker }
361*89a0ef05SAndroid Build Coastguard Worker 
362*89a0ef05SAndroid Build Coastguard Worker #define RET_IF_ERR(x)                                                       \
363*89a0ef05SAndroid Build Coastguard Worker   {                                                                         \
364*89a0ef05SAndroid Build Coastguard Worker     uhdr_error_info_t status = (x);                                         \
365*89a0ef05SAndroid Build Coastguard Worker     if (status.error_code != UHDR_CODEC_OK) {                               \
366*89a0ef05SAndroid Build Coastguard Worker       uhdr_release_encoder(encHandle);                                      \
367*89a0ef05SAndroid Build Coastguard Worker       s.SkipWithError(status.has_detail ? status.detail : "Unknown error"); \
368*89a0ef05SAndroid Build Coastguard Worker       return;                                                               \
369*89a0ef05SAndroid Build Coastguard Worker     }                                                                       \
370*89a0ef05SAndroid Build Coastguard Worker   }
371*89a0ef05SAndroid Build Coastguard Worker 
BM_UHDREncode_Api0(benchmark::State & s,TestParamsEncoderAPI0 testVectors)372*89a0ef05SAndroid Build Coastguard Worker static void BM_UHDREncode_Api0(benchmark::State& s, TestParamsEncoderAPI0 testVectors) {
373*89a0ef05SAndroid Build Coastguard Worker   EncBenchmark benchmark(testVectors);
374*89a0ef05SAndroid Build Coastguard Worker 
375*89a0ef05SAndroid Build Coastguard Worker   s.SetLabel(
376*89a0ef05SAndroid Build Coastguard Worker       benchmark.mHdrFile + ", " + std::to_string(benchmark.mWidth) + "x" +
377*89a0ef05SAndroid Build Coastguard Worker       std::to_string(benchmark.mHeight) + ", " + colorGamutToString(benchmark.mHdrCg) + ", " +
378*89a0ef05SAndroid Build Coastguard Worker       (benchmark.mHdrFile.find("rgba16F") != std::string::npos ? "linear"
379*89a0ef05SAndroid Build Coastguard Worker                                                                : tfToString(benchmark.mHdrCt)) +
380*89a0ef05SAndroid Build Coastguard Worker       ", " +
381*89a0ef05SAndroid Build Coastguard Worker       (benchmark.mUseMultiChannelGainMap == 0 ? "singlechannelgainmap" : "multichannelgainmap") +
382*89a0ef05SAndroid Build Coastguard Worker       ", gamma: " + std::to_string(benchmark.mGamma));
383*89a0ef05SAndroid Build Coastguard Worker 
384*89a0ef05SAndroid Build Coastguard Worker   if (benchmark.mHdrFile.find("p010") != std::string::npos) {
385*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrFile = kTestImagesPath + "p010/" + benchmark.mHdrFile;
386*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCf = UHDR_IMG_FMT_24bppYCbCrP010;
387*89a0ef05SAndroid Build Coastguard Worker   } else if (benchmark.mHdrFile.find("rgba1010102") != std::string::npos) {
388*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrFile = kTestImagesPath + "rgba1010102/" + benchmark.mHdrFile;
389*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCf = UHDR_IMG_FMT_32bppRGBA1010102;
390*89a0ef05SAndroid Build Coastguard Worker   } else if (benchmark.mHdrFile.find("rgba16F") != std::string::npos) {
391*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrFile = kTestImagesPath + "rgba16F/" + benchmark.mHdrFile;
392*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCf = UHDR_IMG_FMT_64bppRGBAHalfFloat;
393*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCt = UHDR_CT_LINEAR;
394*89a0ef05SAndroid Build Coastguard Worker   } else {
395*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("Invalid file format : " + benchmark.mHdrFile);
396*89a0ef05SAndroid Build Coastguard Worker     return;
397*89a0ef05SAndroid Build Coastguard Worker   }
398*89a0ef05SAndroid Build Coastguard Worker 
399*89a0ef05SAndroid Build Coastguard Worker   if (!benchmark.fillRawImageHandle(&benchmark.mHdrImg, benchmark.mWidth, benchmark.mHeight,
400*89a0ef05SAndroid Build Coastguard Worker                                     benchmark.mHdrFile, benchmark.mHdrCf, benchmark.mHdrCg,
401*89a0ef05SAndroid Build Coastguard Worker                                     benchmark.mHdrCt)) {
402*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("unable to load file : " + benchmark.mHdrFile);
403*89a0ef05SAndroid Build Coastguard Worker     return;
404*89a0ef05SAndroid Build Coastguard Worker   }
405*89a0ef05SAndroid Build Coastguard Worker 
406*89a0ef05SAndroid Build Coastguard Worker   uhdr_codec_private_t* encHandle = uhdr_create_encoder();
407*89a0ef05SAndroid Build Coastguard Worker   for (auto _ : s) {
408*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_raw_image(encHandle, &benchmark.mHdrImg, UHDR_HDR_IMG))
409*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(
410*89a0ef05SAndroid Build Coastguard Worker         uhdr_enc_set_using_multi_channel_gainmap(encHandle, benchmark.mUseMultiChannelGainMap))
411*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_gainmap_scale_factor(encHandle, benchmark.mMapDimensionScaleFactor))
412*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_gainmap_gamma(encHandle, benchmark.mGamma))
413*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_encode(encHandle))
414*89a0ef05SAndroid Build Coastguard Worker     uhdr_reset_encoder(encHandle);
415*89a0ef05SAndroid Build Coastguard Worker   }
416*89a0ef05SAndroid Build Coastguard Worker   uhdr_release_encoder(encHandle);
417*89a0ef05SAndroid Build Coastguard Worker }
418*89a0ef05SAndroid Build Coastguard Worker 
BM_UHDREncode_Api1(benchmark::State & s,TestParamsEncoderAPI1 testVectors)419*89a0ef05SAndroid Build Coastguard Worker static void BM_UHDREncode_Api1(benchmark::State& s, TestParamsEncoderAPI1 testVectors) {
420*89a0ef05SAndroid Build Coastguard Worker   EncBenchmark benchmark(testVectors);
421*89a0ef05SAndroid Build Coastguard Worker 
422*89a0ef05SAndroid Build Coastguard Worker   s.SetLabel(
423*89a0ef05SAndroid Build Coastguard Worker       benchmark.mHdrFile + ", " + benchmark.mSdrFile + ", " + std::to_string(benchmark.mWidth) +
424*89a0ef05SAndroid Build Coastguard Worker       "x" + std::to_string(benchmark.mHeight) + ", hdrCg: " + colorGamutToString(benchmark.mHdrCg) +
425*89a0ef05SAndroid Build Coastguard Worker       ", hdrCt: " +
426*89a0ef05SAndroid Build Coastguard Worker       (benchmark.mHdrFile.find("rgba16F") != std::string::npos ? "linear"
427*89a0ef05SAndroid Build Coastguard Worker                                                                : tfToString(benchmark.mHdrCt)) +
428*89a0ef05SAndroid Build Coastguard Worker       ", sdrCg: " + colorGamutToString(benchmark.mSdrCg) + ", " +
429*89a0ef05SAndroid Build Coastguard Worker       (benchmark.mUseMultiChannelGainMap == 0 ? "singlechannelgainmap" : "multichannelgainmap") +
430*89a0ef05SAndroid Build Coastguard Worker       ", gamma: " + std::to_string(benchmark.mGamma) + ", " +
431*89a0ef05SAndroid Build Coastguard Worker       (benchmark.mEncPreset == UHDR_USAGE_BEST_QUALITY ? "best_quality" : "realtime"));
432*89a0ef05SAndroid Build Coastguard Worker 
433*89a0ef05SAndroid Build Coastguard Worker   if (benchmark.mHdrFile.find("p010") != std::string::npos) {
434*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrFile = kTestImagesPath + "p010/" + benchmark.mHdrFile;
435*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCf = UHDR_IMG_FMT_24bppYCbCrP010;
436*89a0ef05SAndroid Build Coastguard Worker   } else if (benchmark.mHdrFile.find("rgba1010102") != std::string::npos) {
437*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrFile = kTestImagesPath + "rgba1010102/" + benchmark.mHdrFile;
438*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCf = UHDR_IMG_FMT_32bppRGBA1010102;
439*89a0ef05SAndroid Build Coastguard Worker   } else if (benchmark.mHdrFile.find("rgba16F") != std::string::npos) {
440*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrFile = kTestImagesPath + "rgba16F/" + benchmark.mHdrFile;
441*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCf = UHDR_IMG_FMT_64bppRGBAHalfFloat;
442*89a0ef05SAndroid Build Coastguard Worker     benchmark.mHdrCt = UHDR_CT_LINEAR;
443*89a0ef05SAndroid Build Coastguard Worker   } else {
444*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("Invalid hdr file format : " + benchmark.mHdrFile);
445*89a0ef05SAndroid Build Coastguard Worker     return;
446*89a0ef05SAndroid Build Coastguard Worker   }
447*89a0ef05SAndroid Build Coastguard Worker 
448*89a0ef05SAndroid Build Coastguard Worker   if (benchmark.mSdrFile.find("yuv420") != std::string::npos) {
449*89a0ef05SAndroid Build Coastguard Worker     benchmark.mSdrFile = kTestImagesPath + "yuv420/" + benchmark.mSdrFile;
450*89a0ef05SAndroid Build Coastguard Worker     benchmark.mSdrCf = UHDR_IMG_FMT_12bppYCbCr420;
451*89a0ef05SAndroid Build Coastguard Worker   } else if (benchmark.mSdrFile.find("rgba8888") != std::string::npos) {
452*89a0ef05SAndroid Build Coastguard Worker     benchmark.mSdrFile = kTestImagesPath + "rgba8888/" + benchmark.mSdrFile;
453*89a0ef05SAndroid Build Coastguard Worker     benchmark.mSdrCf = UHDR_IMG_FMT_32bppRGBA8888;
454*89a0ef05SAndroid Build Coastguard Worker   } else {
455*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("Invalid sdr file format : " + benchmark.mSdrFile);
456*89a0ef05SAndroid Build Coastguard Worker     return;
457*89a0ef05SAndroid Build Coastguard Worker   }
458*89a0ef05SAndroid Build Coastguard Worker 
459*89a0ef05SAndroid Build Coastguard Worker   if (!benchmark.fillRawImageHandle(&benchmark.mHdrImg, benchmark.mWidth, benchmark.mHeight,
460*89a0ef05SAndroid Build Coastguard Worker                                     benchmark.mHdrFile, benchmark.mHdrCf, benchmark.mHdrCg,
461*89a0ef05SAndroid Build Coastguard Worker                                     benchmark.mHdrCt)) {
462*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("unable to load file : " + benchmark.mHdrFile);
463*89a0ef05SAndroid Build Coastguard Worker     return;
464*89a0ef05SAndroid Build Coastguard Worker   }
465*89a0ef05SAndroid Build Coastguard Worker   if (!benchmark.fillRawImageHandle(&benchmark.mSdrImg, benchmark.mWidth, benchmark.mHeight,
466*89a0ef05SAndroid Build Coastguard Worker                                     benchmark.mSdrFile, benchmark.mSdrCf, benchmark.mSdrCg,
467*89a0ef05SAndroid Build Coastguard Worker                                     benchmark.mSdrCt)) {
468*89a0ef05SAndroid Build Coastguard Worker     s.SkipWithError("unable to load sdr file : " + benchmark.mSdrFile);
469*89a0ef05SAndroid Build Coastguard Worker     return;
470*89a0ef05SAndroid Build Coastguard Worker   }
471*89a0ef05SAndroid Build Coastguard Worker 
472*89a0ef05SAndroid Build Coastguard Worker   uhdr_codec_private_t* encHandle = uhdr_create_encoder();
473*89a0ef05SAndroid Build Coastguard Worker   for (auto _ : s) {
474*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_raw_image(encHandle, &benchmark.mHdrImg, UHDR_HDR_IMG))
475*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_raw_image(encHandle, &benchmark.mSdrImg, UHDR_SDR_IMG))
476*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(
477*89a0ef05SAndroid Build Coastguard Worker         uhdr_enc_set_using_multi_channel_gainmap(encHandle, benchmark.mUseMultiChannelGainMap))
478*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_gainmap_scale_factor(encHandle, benchmark.mMapDimensionScaleFactor))
479*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_gainmap_gamma(encHandle, benchmark.mGamma))
480*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_enc_set_preset(encHandle, benchmark.mEncPreset))
481*89a0ef05SAndroid Build Coastguard Worker     RET_IF_ERR(uhdr_encode(encHandle))
482*89a0ef05SAndroid Build Coastguard Worker     uhdr_reset_encoder(encHandle);
483*89a0ef05SAndroid Build Coastguard Worker   }
484*89a0ef05SAndroid Build Coastguard Worker   uhdr_release_encoder(encHandle);
485*89a0ef05SAndroid Build Coastguard Worker }
486*89a0ef05SAndroid Build Coastguard Worker 
addTestVectors()487*89a0ef05SAndroid Build Coastguard Worker void addTestVectors() {
488*89a0ef05SAndroid Build Coastguard Worker   for (const auto& uhdrFile : kDecodeAPITestImages) {
489*89a0ef05SAndroid Build Coastguard Worker     /* Decode API - uhdrFile, colorTransfer, imgFormat, enableGLES */
490*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back({uhdrFile, UHDR_CT_HLG, UHDR_IMG_FMT_32bppRGBA1010102, false});
491*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back({uhdrFile, UHDR_CT_PQ, UHDR_IMG_FMT_32bppRGBA1010102, false});
492*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back(
493*89a0ef05SAndroid Build Coastguard Worker         {uhdrFile, UHDR_CT_LINEAR, UHDR_IMG_FMT_64bppRGBAHalfFloat, false});
494*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back({uhdrFile, UHDR_CT_HLG, UHDR_IMG_FMT_32bppRGBA1010102, true});
495*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back({uhdrFile, UHDR_CT_PQ, UHDR_IMG_FMT_32bppRGBA1010102, true});
496*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back(
497*89a0ef05SAndroid Build Coastguard Worker         {uhdrFile, UHDR_CT_LINEAR, UHDR_IMG_FMT_64bppRGBAHalfFloat, true});
498*89a0ef05SAndroid Build Coastguard Worker     testParamsDecodeAPI.push_back({uhdrFile, UHDR_CT_SRGB, UHDR_IMG_FMT_32bppRGBA8888, false});
499*89a0ef05SAndroid Build Coastguard Worker   }
500*89a0ef05SAndroid Build Coastguard Worker 
501*89a0ef05SAndroid Build Coastguard Worker   for (const auto& hdrFile : kEncodeApi0TestImages12MpName) {
502*89a0ef05SAndroid Build Coastguard Worker     /* Encode API 0 - hdrFile, width, height, hdrColorGamut, hdrColorTransfer,
503*89a0ef05SAndroid Build Coastguard Worker        useMultiChannelGainmap, gamma */
504*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI0.push_back({hdrFile, 4080, 3072, UHDR_CG_BT_2100, UHDR_CT_PQ, 0, 1.0f});
505*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI0.push_back({hdrFile, 4080, 3072, UHDR_CG_BT_2100, UHDR_CT_PQ, 1, 1.0f});
506*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI0.push_back({hdrFile, 4080, 3072, UHDR_CG_BT_2100, UHDR_CT_PQ, 0, 1.571f});
507*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI0.push_back({hdrFile, 4080, 3072, UHDR_CG_BT_2100, UHDR_CT_PQ, 1, 1.616f});
508*89a0ef05SAndroid Build Coastguard Worker   }
509*89a0ef05SAndroid Build Coastguard Worker 
510*89a0ef05SAndroid Build Coastguard Worker   for (const auto& inputFiles : kEncodeApi1TestImages12MpName) {
511*89a0ef05SAndroid Build Coastguard Worker     /* Encode API 1 - hdrFile, sdrFile, width, height, hdrColorGamut, hdrColorTransfer,
512*89a0ef05SAndroid Build Coastguard Worker        sdrColorGamut, useMultiChannelGainmap, gamma, encPreset */
513*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
514*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 0, 1.0f, UHDR_USAGE_REALTIME});
515*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
516*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 1, 1.0f, UHDR_USAGE_REALTIME});
517*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
518*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 0, 1.571f, UHDR_USAGE_REALTIME});
519*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
520*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 0, 1.0f, UHDR_USAGE_BEST_QUALITY});
521*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
522*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 1, 1.571f, UHDR_USAGE_REALTIME});
523*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
524*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 1, 1.0f, UHDR_USAGE_BEST_QUALITY});
525*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
526*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 0, 1.571f, UHDR_USAGE_BEST_QUALITY});
527*89a0ef05SAndroid Build Coastguard Worker     testParamsAPI1.push_back({inputFiles.first, inputFiles.second, 4080, 3072, UHDR_CG_BT_2100,
528*89a0ef05SAndroid Build Coastguard Worker                               UHDR_CT_PQ, UHDR_CG_BT_709, 1, 1.571f, UHDR_USAGE_BEST_QUALITY});
529*89a0ef05SAndroid Build Coastguard Worker   }
530*89a0ef05SAndroid Build Coastguard Worker }
531*89a0ef05SAndroid Build Coastguard Worker 
registerBenchmarks()532*89a0ef05SAndroid Build Coastguard Worker void registerBenchmarks() {
533*89a0ef05SAndroid Build Coastguard Worker   for (auto& param : testParamsDecodeAPI) {
534*89a0ef05SAndroid Build Coastguard Worker     benchmark::RegisterBenchmark("BM_UHDRDecode", BM_UHDRDecode, param)
535*89a0ef05SAndroid Build Coastguard Worker         ->Unit(benchmark::kMillisecond);
536*89a0ef05SAndroid Build Coastguard Worker   }
537*89a0ef05SAndroid Build Coastguard Worker   for (auto& param : testParamsAPI0) {
538*89a0ef05SAndroid Build Coastguard Worker     benchmark::RegisterBenchmark("BM_UHDREncode_Api0", BM_UHDREncode_Api0, param)
539*89a0ef05SAndroid Build Coastguard Worker         ->Unit(benchmark::kMillisecond);
540*89a0ef05SAndroid Build Coastguard Worker   }
541*89a0ef05SAndroid Build Coastguard Worker   for (auto& param : testParamsAPI1) {
542*89a0ef05SAndroid Build Coastguard Worker     benchmark::RegisterBenchmark("BM_UHDREncode_Api1", BM_UHDREncode_Api1, param)
543*89a0ef05SAndroid Build Coastguard Worker         ->Unit(benchmark::kMillisecond);
544*89a0ef05SAndroid Build Coastguard Worker   }
545*89a0ef05SAndroid Build Coastguard Worker }
546*89a0ef05SAndroid Build Coastguard Worker 
main(int argc,char ** argv)547*89a0ef05SAndroid Build Coastguard Worker int main(int argc, char** argv) {
548*89a0ef05SAndroid Build Coastguard Worker   addTestVectors();
549*89a0ef05SAndroid Build Coastguard Worker   registerBenchmarks();
550*89a0ef05SAndroid Build Coastguard Worker   benchmark::Initialize(&argc, argv);
551*89a0ef05SAndroid Build Coastguard Worker   benchmark::RunSpecifiedBenchmarks(nullptr, nullptr);
552*89a0ef05SAndroid Build Coastguard Worker   benchmark::Shutdown();
553*89a0ef05SAndroid Build Coastguard Worker   return 0;
554*89a0ef05SAndroid Build Coastguard Worker }
555