Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_ssim.c

Issue 1124333011: libvpx: Pull from upstream (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libvpx.git@master
Patch Set: only update to last nights LKGR Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ssim.h ('k') | source/libvpx/vp9/encoder/vp9_subexp.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <math.h>
11 #include "./vp9_rtcd.h" 12 #include "./vp9_rtcd.h"
12
13 #include "vp9/encoder/vp9_ssim.h" 13 #include "vp9/encoder/vp9_ssim.h"
14 14
15 void vp9_ssim_parms_16x16_c(uint8_t *s, int sp, uint8_t *r, 15 void vp9_ssim_parms_16x16_c(uint8_t *s, int sp, uint8_t *r,
16 int rp, unsigned long *sum_s, unsigned long *sum_r, 16 int rp, unsigned long *sum_s, unsigned long *sum_r,
17 unsigned long *sum_sq_s, unsigned long *sum_sq_r, 17 unsigned long *sum_sq_s, unsigned long *sum_sq_r,
18 unsigned long *sum_sxr) { 18 unsigned long *sum_sxr) {
19 int i, j; 19 int i, j;
20 for (i = 0; i < 16; i++, s += sp, r += rp) { 20 for (i = 0; i < 16; i++, s += sp, r += rp) {
21 for (j = 0; j < 16; j++) { 21 for (j = 0; j < 16; j++) {
22 *sum_s += s[j]; 22 *sum_s += s[j];
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 source->uv_stride, dest->uv_stride, 194 source->uv_stride, dest->uv_stride,
195 source->uv_crop_width, source->uv_crop_height); 195 source->uv_crop_width, source->uv_crop_height);
196 *ssim_y = a; 196 *ssim_y = a;
197 *ssim_u = b; 197 *ssim_u = b;
198 *ssim_v = c; 198 *ssim_v = c;
199 ssim_all = (a * 4 + b + c) / 6; 199 ssim_all = (a * 4 + b + c) / 6;
200 200
201 return ssim_all; 201 return ssim_all;
202 } 202 }
203 203
204 // traditional ssim as per: http://en.wikipedia.org/wiki/Structural_similarity
205 //
206 // Re working out the math ->
207 //
208 // ssim(x,y) = (2*mean(x)*mean(y) + c1)*(2*cov(x,y)+c2) /
209 // ((mean(x)^2+mean(y)^2+c1)*(var(x)+var(y)+c2))
210 //
211 // mean(x) = sum(x) / n
212 //
213 // cov(x,y) = (n*sum(xi*yi)-sum(x)*sum(y))/(n*n)
214 //
215 // var(x) = (n*sum(xi*xi)-sum(xi)*sum(xi))/(n*n)
216 //
217 // ssim(x,y) =
218 // (2*sum(x)*sum(y)/(n*n) + c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))/(n*n)+c2) /
219 // (((sum(x)*sum(x)+sum(y)*sum(y))/(n*n) +c1) *
220 // ((n*sum(xi*xi) - sum(xi)*sum(xi))/(n*n)+
221 // (n*sum(yi*yi) - sum(yi)*sum(yi))/(n*n)+c2)))
222 //
223 // factoring out n*n
224 //
225 // ssim(x,y) =
226 // (2*sum(x)*sum(y) + n*n*c1)*(2*(n*sum(xi*yi)-sum(x)*sum(y))+n*n*c2) /
227 // (((sum(x)*sum(x)+sum(y)*sum(y)) + n*n*c1) *
228 // (n*sum(xi*xi)-sum(xi)*sum(xi)+n*sum(yi*yi)-sum(yi)*sum(yi)+n*n*c2))
229 //
230 // Replace c1 with n*n * c1 for the final step that leads to this code:
231 // The final step scales by 12 bits so we don't lose precision in the constants.
232
233 double ssimv_similarity(Ssimv *sv, int64_t n) {
234 // Scale the constants by number of pixels.
235 const int64_t c1 = (cc1 * n * n) >> 12;
236 const int64_t c2 = (cc2 * n * n) >> 12;
237
238 const double l = 1.0 * (2 * sv->sum_s * sv->sum_r + c1) /
239 (sv->sum_s * sv->sum_s + sv->sum_r * sv->sum_r + c1);
240
241 // Since these variables are unsigned sums, convert to double so
242 // math is done in double arithmetic.
243 const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2)
244 / (n * sv->sum_sq_s - sv->sum_s * sv->sum_s + n * sv->sum_sq_r
245 - sv->sum_r * sv->sum_r + c2);
246
247 return l * v;
248 }
249
250 // The first term of the ssim metric is a luminance factor.
251 //
252 // (2*mean(x)*mean(y) + c1)/ (mean(x)^2+mean(y)^2+c1)
253 //
254 // This luminance factor is super sensitive to the dark side of luminance
255 // values and completely insensitive on the white side. check out 2 sets
256 // (1,3) and (250,252) the term gives ( 2*1*3/(1+9) = .60
257 // 2*250*252/ (250^2+252^2) => .99999997
258 //
259 // As a result in this tweaked version of the calculation in which the
260 // luminance is taken as percentage off from peak possible.
261 //
262 // 255 * 255 - (sum_s - sum_r) / count * (sum_s - sum_r) / count
263 //
264 double ssimv_similarity2(Ssimv *sv, int64_t n) {
265 // Scale the constants by number of pixels.
266 const int64_t c1 = (cc1 * n * n) >> 12;
267 const int64_t c2 = (cc2 * n * n) >> 12;
268
269 const double mean_diff = (1.0 * sv->sum_s - sv->sum_r) / n;
270 const double l = (255 * 255 - mean_diff * mean_diff + c1) / (255 * 255 + c1);
271
272 // Since these variables are unsigned, sums convert to double so
273 // math is done in double arithmetic.
274 const double v = (2.0 * n * sv->sum_sxr - 2 * sv->sum_s * sv->sum_r + c2)
275 / (n * sv->sum_sq_s - sv->sum_s * sv->sum_s +
276 n * sv->sum_sq_r - sv->sum_r * sv->sum_r + c2);
277
278 return l * v;
279 }
280 void ssimv_parms(uint8_t *img1, int img1_pitch, uint8_t *img2, int img2_pitch,
281 Ssimv *sv) {
282 vp9_ssim_parms_8x8(img1, img1_pitch, img2, img2_pitch,
283 &sv->sum_s, &sv->sum_r, &sv->sum_sq_s, &sv->sum_sq_r,
284 &sv->sum_sxr);
285 }
286
287 double vp9_get_ssim_metrics(uint8_t *img1, int img1_pitch,
288 uint8_t *img2, int img2_pitch,
289 int width, int height,
290 Ssimv *sv2, Metrics *m,
291 int do_inconsistency) {
292 double dssim_total = 0;
293 double ssim_total = 0;
294 double ssim2_total = 0;
295 double inconsistency_total = 0;
296 int i, j;
297 int c = 0;
298 double norm;
299 double old_ssim_total = 0;
300 vp9_clear_system_state();
301 // We can sample points as frequently as we like start with 1 per 4x4.
302 for (i = 0; i < height; i += 4,
303 img1 += img1_pitch * 4, img2 += img2_pitch * 4) {
304 for (j = 0; j < width; j += 4, ++c) {
305 Ssimv sv = {0};
306 double ssim;
307 double ssim2;
308 double dssim;
309 uint32_t var_new;
310 uint32_t var_old;
311 uint32_t mean_new;
312 uint32_t mean_old;
313 double ssim_new;
314 double ssim_old;
315
316 // Not sure there's a great way to handle the edge pixels
317 // in ssim when using a window. Seems biased against edge pixels
318 // however you handle this. This uses only samples that are
319 // fully in the frame.
320 if (j + 8 <= width && i + 8 <= height) {
321 ssimv_parms(img1 + j, img1_pitch, img2 + j, img2_pitch, &sv);
322 }
323
324 ssim = ssimv_similarity(&sv, 64);
325 ssim2 = ssimv_similarity2(&sv, 64);
326
327 sv.ssim = ssim2;
328
329 // dssim is calculated to use as an actual error metric and
330 // is scaled up to the same range as sum square error.
331 // Since we are subsampling every 16th point maybe this should be
332 // *16 ?
333 dssim = 255 * 255 * (1 - ssim2) / 2;
334
335 // Here I introduce a new error metric: consistency-weighted
336 // SSIM-inconsistency. This metric isolates frames where the
337 // SSIM 'suddenly' changes, e.g. if one frame in every 8 is much
338 // sharper or blurrier than the others. Higher values indicate a
339 // temporally inconsistent SSIM. There are two ideas at work:
340 //
341 // 1) 'SSIM-inconsistency': the total inconsistency value
342 // reflects how much SSIM values are changing between this
343 // source / reference frame pair and the previous pair.
344 //
345 // 2) 'consistency-weighted': weights de-emphasize areas in the
346 // frame where the scene content has changed. Changes in scene
347 // content are detected via changes in local variance and local
348 // mean.
349 //
350 // Thus the overall measure reflects how inconsistent the SSIM
351 // values are, over consistent regions of the frame.
352 //
353 // The metric has three terms:
354 //
355 // term 1 -> uses change in scene Variance to weight error score
356 // 2 * var(Fi)*var(Fi-1) / (var(Fi)^2+var(Fi-1)^2)
357 // larger changes from one frame to the next mean we care
358 // less about consistency.
359 //
360 // term 2 -> uses change in local scene luminance to weight error
361 // 2 * avg(Fi)*avg(Fi-1) / (avg(Fi)^2+avg(Fi-1)^2)
362 // larger changes from one frame to the next mean we care
363 // less about consistency.
364 //
365 // term3 -> measures inconsistency in ssim scores between frames
366 // 1 - ( 2 * ssim(Fi)*ssim(Fi-1)/(ssim(Fi)^2+sssim(Fi-1)^2).
367 //
368 // This term compares the ssim score for the same location in 2
369 // subsequent frames.
370 var_new = sv.sum_sq_s - sv.sum_s * sv.sum_s / 64;
371 var_old = sv2[c].sum_sq_s - sv2[c].sum_s * sv2[c].sum_s / 64;
372 mean_new = sv.sum_s;
373 mean_old = sv2[c].sum_s;
374 ssim_new = sv.ssim;
375 ssim_old = sv2[c].ssim;
376
377 if (do_inconsistency) {
378 // We do the metric once for every 4x4 block in the image. Since
379 // we are scaling the error to SSE for use in a psnr calculation
380 // 1.0 = 4x4x255x255 the worst error we can possibly have.
381 static const double kScaling = 4. * 4 * 255 * 255;
382
383 // The constants have to be non 0 to avoid potential divide by 0
384 // issues other than that they affect kind of a weighting between
385 // the terms. No testing of what the right terms should be has been
386 // done.
387 static const double c1 = 1, c2 = 1, c3 = 1;
388
389 // This measures how much consistent variance is in two consecutive
390 // source frames. 1.0 means they have exactly the same variance.
391 const double variance_term = (2.0 * var_old * var_new + c1) /
392 (1.0 * var_old * var_old + 1.0 * var_new * var_new + c1);
393
394 // This measures how consistent the local mean are between two
395 // consecutive frames. 1.0 means they have exactly the same mean.
396 const double mean_term = (2.0 * mean_old * mean_new + c2) /
397 (1.0 * mean_old * mean_old + 1.0 * mean_new * mean_new + c2);
398
399 // This measures how consistent the ssims of two
400 // consecutive frames is. 1.0 means they are exactly the same.
401 double ssim_term = pow((2.0 * ssim_old * ssim_new + c3) /
402 (ssim_old * ssim_old + ssim_new * ssim_new + c3),
403 5);
404
405 double this_inconsistency;
406
407 // Floating point math sometimes makes this > 1 by a tiny bit.
408 // We want the metric to scale between 0 and 1.0 so we can convert
409 // it to an snr scaled value.
410 if (ssim_term > 1)
411 ssim_term = 1;
412
413 // This converts the consistency metric to an inconsistency metric
414 // ( so we can scale it like psnr to something like sum square error.
415 // The reason for the variance and mean terms is the assumption that
416 // if there are big changes in the source we shouldn't penalize
417 // inconsistency in ssim scores a bit less as it will be less visible
418 // to the user.
419 this_inconsistency = (1 - ssim_term) * variance_term * mean_term;
420
421 this_inconsistency *= kScaling;
422 inconsistency_total += this_inconsistency;
423 }
424 sv2[c] = sv;
425 ssim_total += ssim;
426 ssim2_total += ssim2;
427 dssim_total += dssim;
428
429 old_ssim_total += ssim_old;
430 }
431 old_ssim_total += 0;
432 }
433
434 norm = 1. / (width / 4) / (height / 4);
435 ssim_total *= norm;
436 ssim2_total *= norm;
437 m->ssim2 = ssim2_total;
438 m->ssim = ssim_total;
439 if (old_ssim_total == 0)
440 inconsistency_total = 0;
441
442 m->ssimc = inconsistency_total;
443
444 m->dssim = dssim_total;
445 return inconsistency_total;
446 }
447
448
204 #if CONFIG_VP9_HIGHBITDEPTH 449 #if CONFIG_VP9_HIGHBITDEPTH
205 double vp9_highbd_calc_ssim(YV12_BUFFER_CONFIG *source, 450 double vp9_highbd_calc_ssim(YV12_BUFFER_CONFIG *source,
206 YV12_BUFFER_CONFIG *dest, 451 YV12_BUFFER_CONFIG *dest,
207 double *weight, unsigned int bd) { 452 double *weight, unsigned int bd) {
208 double a, b, c; 453 double a, b, c;
209 double ssimv; 454 double ssimv;
210 455
211 a = vp9_highbd_ssim2(source->y_buffer, dest->y_buffer, 456 a = vp9_highbd_ssim2(source->y_buffer, dest->y_buffer,
212 source->y_stride, dest->y_stride, 457 source->y_stride, dest->y_stride,
213 source->y_crop_width, source->y_crop_height, bd); 458 source->y_crop_width, source->y_crop_height, bd);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 source->uv_stride, dest->uv_stride, 490 source->uv_stride, dest->uv_stride,
246 source->uv_crop_width, source->uv_crop_height, bd); 491 source->uv_crop_width, source->uv_crop_height, bd);
247 *ssim_y = a; 492 *ssim_y = a;
248 *ssim_u = b; 493 *ssim_u = b;
249 *ssim_v = c; 494 *ssim_v = c;
250 ssim_all = (a * 4 + b + c) / 6; 495 ssim_all = (a * 4 + b + c) / 6;
251 496
252 return ssim_all; 497 return ssim_all;
253 } 498 }
254 #endif // CONFIG_VP9_HIGHBITDEPTH 499 #endif // CONFIG_VP9_HIGHBITDEPTH
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_ssim.h ('k') | source/libvpx/vp9/encoder/vp9_subexp.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698