1 /**
2  *
3  *  Copyright 2016-2020 Netflix, Inc.
4  *
5  *     Licensed under the BSD+Patent License (the "License");
6  *     you may not use this file except in compliance with the License.
7  *     You may obtain a copy of the License at
8  *
9  *         https://opensource.org/licenses/BSDplusPatent
10  *
11  *     Unless required by applicable law or agreed to in writing, software
12  *     distributed under the License is distributed on an "AS IS" BASIS,
13  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *     See the License for the specific language governing permissions and
15  *     limitations under the License.
16  *
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <math.h>
27 
28 #include "mem.h"
29 #include "psnr_tools.h"
30 #include "psnr_options.h"
31 
32 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
33 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
34 
compute_psnr(const float * ref,const float * dis,int w,int h,int ref_stride,int dis_stride,double * score,double peak,double psnr_max)35 int compute_psnr(const float *ref, const float *dis, int w, int h, int ref_stride, int dis_stride, double *score, double peak, double psnr_max)
36 {
37     double noise_ = 0;
38 
39     int ref_stride_ = ref_stride / sizeof(float);
40     int dis_stride_ = dis_stride / sizeof(float);
41 
42     for (int i = 0; i < h; ++i)
43     {
44         for (int j = 0; j < w; ++j)
45         {
46             float ref_ = ref[i * ref_stride_ + j];
47             float dis_ = dis[i * dis_stride_ + j];
48             float diff = ref_ - dis_;
49             noise_ += diff * diff;
50         }
51     }
52     noise_ /= (w * h);
53 
54     double eps = 1e-10;
55     *score = MIN(10 * log10(peak * peak / MAX(noise_, eps)), psnr_max);
56 
57     return 0;
58 }
59