xref: /aosp_15_r20/external/libaom/test/util.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_TEST_UTIL_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_TEST_UTIL_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
18*77c1e3ccSAndroid Build Coastguard Worker #include "gtest/gtest.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_image.h"
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/aom_timer.h"
21*77c1e3ccSAndroid Build Coastguard Worker 
22*77c1e3ccSAndroid Build Coastguard Worker // Macros
23*77c1e3ccSAndroid Build Coastguard Worker #define GET_PARAM(k) std::get<k>(GetParam())
24*77c1e3ccSAndroid Build Coastguard Worker 
is_extension_y4m(const char * filename)25*77c1e3ccSAndroid Build Coastguard Worker inline int is_extension_y4m(const char *filename) {
26*77c1e3ccSAndroid Build Coastguard Worker   const char *dot = strrchr(filename, '.');
27*77c1e3ccSAndroid Build Coastguard Worker   if (!dot || dot == filename) return 0;
28*77c1e3ccSAndroid Build Coastguard Worker 
29*77c1e3ccSAndroid Build Coastguard Worker   return !strcmp(dot, ".y4m");
30*77c1e3ccSAndroid Build Coastguard Worker }
31*77c1e3ccSAndroid Build Coastguard Worker 
compute_psnr(const aom_image_t * img1,const aom_image_t * img2)32*77c1e3ccSAndroid Build Coastguard Worker inline double compute_psnr(const aom_image_t *img1, const aom_image_t *img2) {
33*77c1e3ccSAndroid Build Coastguard Worker   assert((img1->fmt == img2->fmt) && (img1->d_w == img2->d_w) &&
34*77c1e3ccSAndroid Build Coastguard Worker          (img1->d_h == img2->d_h));
35*77c1e3ccSAndroid Build Coastguard Worker 
36*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int width_y = img1->d_w;
37*77c1e3ccSAndroid Build Coastguard Worker   const unsigned int height_y = img1->d_h;
38*77c1e3ccSAndroid Build Coastguard Worker   unsigned int i, j;
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker   int64_t sqrerr = 0;
41*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < height_y; ++i)
42*77c1e3ccSAndroid Build Coastguard Worker     for (j = 0; j < width_y; ++j) {
43*77c1e3ccSAndroid Build Coastguard Worker       int64_t d = img1->planes[AOM_PLANE_Y][i * img1->stride[AOM_PLANE_Y] + j] -
44*77c1e3ccSAndroid Build Coastguard Worker                   img2->planes[AOM_PLANE_Y][i * img2->stride[AOM_PLANE_Y] + j];
45*77c1e3ccSAndroid Build Coastguard Worker       sqrerr += d * d;
46*77c1e3ccSAndroid Build Coastguard Worker     }
47*77c1e3ccSAndroid Build Coastguard Worker   double mse = static_cast<double>(sqrerr) / (width_y * height_y);
48*77c1e3ccSAndroid Build Coastguard Worker   double psnr = 100.0;
49*77c1e3ccSAndroid Build Coastguard Worker   if (mse > 0.0) {
50*77c1e3ccSAndroid Build Coastguard Worker     psnr = 10 * log10(255.0 * 255.0 / mse);
51*77c1e3ccSAndroid Build Coastguard Worker   }
52*77c1e3ccSAndroid Build Coastguard Worker   return psnr;
53*77c1e3ccSAndroid Build Coastguard Worker }
54*77c1e3ccSAndroid Build Coastguard Worker 
get_time_mark(aom_usec_timer * t)55*77c1e3ccSAndroid Build Coastguard Worker static inline double get_time_mark(aom_usec_timer *t) {
56*77c1e3ccSAndroid Build Coastguard Worker   aom_usec_timer_mark(t);
57*77c1e3ccSAndroid Build Coastguard Worker   return static_cast<double>(aom_usec_timer_elapsed(t));
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker 
60*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_TEST_UTIL_H_
61