• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <math.h>
12 #include <assert.h>
13 #include "./vpx_dsp_rtcd.h"
14 #include "vpx_dsp/psnr.h"
15 #include "vpx_scale/yv12config.h"
16 
vpx_sse_to_psnr(double samples,double peak,double sse)17 double vpx_sse_to_psnr(double samples, double peak, double sse) {
18   if (sse > 0.0) {
19     const double psnr = 10.0 * log10(samples * peak * peak / sse);
20     return psnr > MAX_PSNR ? MAX_PSNR : psnr;
21   } else {
22     return MAX_PSNR;
23   }
24 }
25 
26 /* TODO(yaowu): The block_variance calls the unoptimized versions of variance()
27  * and highbd_8_variance(). It should not.
28  */
encoder_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int w,int h)29 static int64_t encoder_sse(const uint8_t *a, int a_stride, const uint8_t *b,
30                            int b_stride, int w, int h) {
31   int i, j;
32   int64_t sse = 0;
33 
34   for (i = 0; i < h; i++) {
35     for (j = 0; j < w; j++) {
36       const int diff = a[j] - b[j];
37       sse += diff * diff;
38     }
39 
40     a += a_stride;
41     b += b_stride;
42   }
43 
44   return sse;
45 }
46 
47 #if CONFIG_VP9_HIGHBITDEPTH
encoder_highbd_8_sse(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int w,int h)48 static int64_t encoder_highbd_8_sse(const uint8_t *a8, int a_stride,
49                                     const uint8_t *b8, int b_stride, int w,
50                                     int h) {
51   int i, j;
52   int64_t sse = 0;
53 
54   uint16_t *a = CONVERT_TO_SHORTPTR(a8);
55   uint16_t *b = CONVERT_TO_SHORTPTR(b8);
56 
57   for (i = 0; i < h; i++) {
58     for (j = 0; j < w; j++) {
59       const int diff = a[j] - b[j];
60       sse += diff * diff;
61     }
62     a += a_stride;
63     b += b_stride;
64   }
65 
66   return sse;
67 }
68 #endif  // CONFIG_VP9_HIGHBITDEPTH
69 
get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)70 static int64_t get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
71                        int b_stride, int width, int height) {
72   const int dw = width % 16;
73   const int dh = height % 16;
74   int64_t total_sse = 0;
75   int x, y;
76 
77   if (dw > 0) {
78     total_sse += encoder_sse(&a[width - dw], a_stride, &b[width - dw], b_stride,
79                              dw, height);
80   }
81 
82   if (dh > 0) {
83     total_sse +=
84         encoder_sse(&a[(height - dh) * a_stride], a_stride,
85                     &b[(height - dh) * b_stride], b_stride, width - dw, dh);
86   }
87 
88   for (y = 0; y < height / 16; ++y) {
89     const uint8_t *pa = a;
90     const uint8_t *pb = b;
91     unsigned int sse;
92     for (x = 0; x < width / 16; ++x) {
93       vpx_mse16x16(pa, a_stride, pb, b_stride, &sse);
94       total_sse += sse;
95 
96       pa += 16;
97       pb += 16;
98     }
99 
100     a += 16 * a_stride;
101     b += 16 * b_stride;
102   }
103 
104   return total_sse;
105 }
106 
107 #if CONFIG_VP9_HIGHBITDEPTH
highbd_get_sse_shift(const uint8_t * a8,int a_stride,const uint8_t * b8,int b_stride,int width,int height,unsigned int input_shift)108 static int64_t highbd_get_sse_shift(const uint8_t *a8, int a_stride,
109                                     const uint8_t *b8, int b_stride, int width,
110                                     int height, unsigned int input_shift) {
111   const uint16_t *a = CONVERT_TO_SHORTPTR(a8);
112   const uint16_t *b = CONVERT_TO_SHORTPTR(b8);
113   int64_t total_sse = 0;
114   int x, y;
115   for (y = 0; y < height; ++y) {
116     for (x = 0; x < width; ++x) {
117       int64_t diff;
118       diff = (a[x] >> input_shift) - (b[x] >> input_shift);
119       total_sse += diff * diff;
120     }
121     a += a_stride;
122     b += b_stride;
123   }
124   return total_sse;
125 }
126 
highbd_get_sse(const uint8_t * a,int a_stride,const uint8_t * b,int b_stride,int width,int height)127 static int64_t highbd_get_sse(const uint8_t *a, int a_stride, const uint8_t *b,
128                               int b_stride, int width, int height) {
129   int64_t total_sse = 0;
130   int x, y;
131   const int dw = width % 16;
132   const int dh = height % 16;
133   if (dw > 0) {
134     total_sse += encoder_highbd_8_sse(&a[width - dw], a_stride, &b[width - dw],
135                                       b_stride, dw, height);
136   }
137   if (dh > 0) {
138     total_sse += encoder_highbd_8_sse(&a[(height - dh) * a_stride], a_stride,
139                                       &b[(height - dh) * b_stride], b_stride,
140                                       width - dw, dh);
141   }
142   for (y = 0; y < height / 16; ++y) {
143     const uint8_t *pa = a;
144     const uint8_t *pb = b;
145     unsigned int sse;
146     for (x = 0; x < width / 16; ++x) {
147       vpx_highbd_8_mse16x16(pa, a_stride, pb, b_stride, &sse);
148       total_sse += sse;
149       pa += 16;
150       pb += 16;
151     }
152     a += 16 * a_stride;
153     b += 16 * b_stride;
154   }
155   return total_sse;
156 }
157 #endif  // CONFIG_VP9_HIGHBITDEPTH
158 
vpx_get_y_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)159 int64_t vpx_get_y_sse(const YV12_BUFFER_CONFIG *a,
160                       const YV12_BUFFER_CONFIG *b) {
161   assert(a->y_crop_width == b->y_crop_width);
162   assert(a->y_crop_height == b->y_crop_height);
163 
164   return get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
165                  a->y_crop_width, a->y_crop_height);
166 }
167 
168 #if CONFIG_VP9_HIGHBITDEPTH
vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b)169 int64_t vpx_highbd_get_y_sse(const YV12_BUFFER_CONFIG *a,
170                              const YV12_BUFFER_CONFIG *b) {
171   assert(a->y_crop_width == b->y_crop_width);
172   assert(a->y_crop_height == b->y_crop_height);
173   assert((a->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
174   assert((b->flags & YV12_FLAG_HIGHBITDEPTH) != 0);
175 
176   return highbd_get_sse(a->y_buffer, a->y_stride, b->y_buffer, b->y_stride,
177                         a->y_crop_width, a->y_crop_height);
178 }
179 #endif  // CONFIG_VP9_HIGHBITDEPTH
180 
181 #if CONFIG_VP9_HIGHBITDEPTH
vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr,uint32_t bit_depth,uint32_t in_bit_depth)182 void vpx_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
183                           const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
184                           uint32_t bit_depth, uint32_t in_bit_depth) {
185   const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
186   const int heights[3] = { a->y_crop_height, a->uv_crop_height,
187                            a->uv_crop_height };
188   const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
189   const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
190   const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
191   const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
192   int i;
193   uint64_t total_sse = 0;
194   uint32_t total_samples = 0;
195   const double peak = (double)((1 << in_bit_depth) - 1);
196   const unsigned int input_shift = bit_depth - in_bit_depth;
197 
198   for (i = 0; i < 3; ++i) {
199     const int w = widths[i];
200     const int h = heights[i];
201     const uint32_t samples = w * h;
202     uint64_t sse;
203     if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
204       if (input_shift) {
205         sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
206                                    b_strides[i], w, h, input_shift);
207       } else {
208         sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
209                              b_strides[i], w, h);
210       }
211     } else {
212       sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
213     }
214     psnr->sse[1 + i] = sse;
215     psnr->samples[1 + i] = samples;
216     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
217 
218     total_sse += sse;
219     total_samples += samples;
220   }
221 
222   psnr->sse[0] = total_sse;
223   psnr->samples[0] = total_samples;
224   psnr->psnr[0] =
225       vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
226 }
227 
228 #endif  // !CONFIG_VP9_HIGHBITDEPTH
229 
vpx_calc_psnr(const YV12_BUFFER_CONFIG * a,const YV12_BUFFER_CONFIG * b,PSNR_STATS * psnr)230 void vpx_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
231                    PSNR_STATS *psnr) {
232   static const double peak = 255.0;
233   const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
234   const int heights[3] = { a->y_crop_height, a->uv_crop_height,
235                            a->uv_crop_height };
236   const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
237   const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
238   const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
239   const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
240   int i;
241   uint64_t total_sse = 0;
242   uint32_t total_samples = 0;
243 
244   for (i = 0; i < 3; ++i) {
245     const int w = widths[i];
246     const int h = heights[i];
247     const uint32_t samples = w * h;
248     const uint64_t sse =
249         get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
250     psnr->sse[1 + i] = sse;
251     psnr->samples[1 + i] = samples;
252     psnr->psnr[1 + i] = vpx_sse_to_psnr(samples, peak, (double)sse);
253 
254     total_sse += sse;
255     total_samples += samples;
256   }
257 
258   psnr->sse[0] = total_sse;
259   psnr->samples[0] = total_samples;
260   psnr->psnr[0] =
261       vpx_sse_to_psnr((double)total_samples, peak, (double)total_sse);
262 }
263