Path: blob/master/thirdparty/libwebp/src/enc/picture_psnr_enc.c
21928 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// WebPPicture tools for measuring distortion10//11// Author: Skal ([email protected])1213#include "src/webp/encode.h"1415#if !(defined(WEBP_DISABLE_STATS) || defined(WEBP_REDUCE_SIZE))1617#include <math.h>18#include <stdlib.h>1920#include "src/webp/types.h"21#include "src/dsp/dsp.h"22#include "src/enc/vp8i_enc.h"23#include "src/utils/utils.h"2425typedef double (*AccumulateFunc)(const uint8_t* src, int src_stride,26const uint8_t* ref, int ref_stride,27int w, int h);2829//------------------------------------------------------------------------------30// local-min distortion31//32// For every pixel in the *reference* picture, we search for the local best33// match in the compressed image. This is not a symmetrical measure.3435#define RADIUS 2 // search radius. Shouldn't be too large.3637static double AccumulateLSIM(const uint8_t* src, int src_stride,38const uint8_t* ref, int ref_stride,39int w, int h) {40int x, y;41double total_sse = 0.;42for (y = 0; y < h; ++y) {43const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS;44const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1;45for (x = 0; x < w; ++x) {46const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS;47const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1;48double best_sse = 255. * 255.;49const double value = (double)ref[y * ref_stride + x];50int i, j;51for (j = y_0; j < y_1; ++j) {52const uint8_t* const s = src + j * src_stride;53for (i = x_0; i < x_1; ++i) {54const double diff = s[i] - value;55const double sse = diff * diff;56if (sse < best_sse) best_sse = sse;57}58}59total_sse += best_sse;60}61}62return total_sse;63}64#undef RADIUS6566static double AccumulateSSE(const uint8_t* src, int src_stride,67const uint8_t* ref, int ref_stride,68int w, int h) {69int y;70double total_sse = 0.;71for (y = 0; y < h; ++y) {72total_sse += VP8AccumulateSSE(src, ref, w);73src += src_stride;74ref += ref_stride;75}76return total_sse;77}7879//------------------------------------------------------------------------------8081static double AccumulateSSIM(const uint8_t* src, int src_stride,82const uint8_t* ref, int ref_stride,83int w, int h) {84const int w0 = (w < VP8_SSIM_KERNEL) ? w : VP8_SSIM_KERNEL;85const int w1 = w - VP8_SSIM_KERNEL - 1;86const int h0 = (h < VP8_SSIM_KERNEL) ? h : VP8_SSIM_KERNEL;87const int h1 = h - VP8_SSIM_KERNEL - 1;88int x, y;89double sum = 0.;90for (y = 0; y < h0; ++y) {91for (x = 0; x < w; ++x) {92sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);93}94}95for (; y < h1; ++y) {96for (x = 0; x < w0; ++x) {97sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);98}99for (; x < w1; ++x) {100const int off1 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * src_stride;101const int off2 = x - VP8_SSIM_KERNEL + (y - VP8_SSIM_KERNEL) * ref_stride;102sum += VP8SSIMGet(src + off1, src_stride, ref + off2, ref_stride);103}104for (; x < w; ++x) {105sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);106}107}108for (; y < h; ++y) {109for (x = 0; x < w; ++x) {110sum += VP8SSIMGetClipped(src, src_stride, ref, ref_stride, x, y, w, h);111}112}113return sum;114}115116//------------------------------------------------------------------------------117// Distortion118119// Max value returned in case of exact similarity.120static const double kMinDistortion_dB = 99.;121122static double GetPSNR(double v, double size) {123return (v > 0. && size > 0.) ? -4.3429448 * log(v / (size * 255 * 255.))124: kMinDistortion_dB;125}126127static double GetLogSSIM(double v, double size) {128v = (size > 0.) ? v / size : 1.;129return (v < 1.) ? -10.0 * log10(1. - v) : kMinDistortion_dB;130}131132int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,133const uint8_t* ref, size_t ref_stride,134int width, int height, size_t x_step,135int type, float* distortion, float* result) {136uint8_t* allocated = NULL;137const AccumulateFunc metric = (type == 0) ? AccumulateSSE :138(type == 1) ? AccumulateSSIM :139AccumulateLSIM;140if (src == NULL || ref == NULL ||141src_stride < x_step * width || ref_stride < x_step * width ||142result == NULL || distortion == NULL) {143return 0;144}145146VP8SSIMDspInit();147if (x_step != 1) { // extract a packed plane if needed148int x, y;149uint8_t* tmp1;150uint8_t* tmp2;151allocated =152(uint8_t*)WebPSafeMalloc(2ULL * width * height, sizeof(*allocated));153if (allocated == NULL) return 0;154tmp1 = allocated;155tmp2 = tmp1 + (size_t)width * height;156for (y = 0; y < height; ++y) {157for (x = 0; x < width; ++x) {158tmp1[x + y * width] = src[x * x_step + y * src_stride];159tmp2[x + y * width] = ref[x * x_step + y * ref_stride];160}161}162src = tmp1;163ref = tmp2;164}165*distortion = (float)metric(src, width, ref, width, width, height);166WebPSafeFree(allocated);167168*result = (type == 1) ? (float)GetLogSSIM(*distortion, (double)width * height)169: (float)GetPSNR(*distortion, (double)width * height);170return 1;171}172173#ifdef WORDS_BIGENDIAN174#define BLUE_OFFSET 3 // uint32_t 0x000000ff is 0x00,00,00,ff in memory175#else176#define BLUE_OFFSET 0 // uint32_t 0x000000ff is 0xff,00,00,00 in memory177#endif178179int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,180int type, float results[5]) {181int w, h, c;182int ok = 0;183WebPPicture p0, p1;184double total_size = 0., total_distortion = 0.;185if (src == NULL || ref == NULL ||186src->width != ref->width || src->height != ref->height ||187results == NULL) {188return 0;189}190191VP8SSIMDspInit();192if (!WebPPictureInit(&p0) || !WebPPictureInit(&p1)) return 0;193w = src->width;194h = src->height;195if (!WebPPictureView(src, 0, 0, w, h, &p0)) goto Error;196if (!WebPPictureView(ref, 0, 0, w, h, &p1)) goto Error;197198// We always measure distortion in ARGB space.199if (p0.use_argb == 0 && !WebPPictureYUVAToARGB(&p0)) goto Error;200if (p1.use_argb == 0 && !WebPPictureYUVAToARGB(&p1)) goto Error;201for (c = 0; c < 4; ++c) {202float distortion;203const size_t stride0 = 4 * (size_t)p0.argb_stride;204const size_t stride1 = 4 * (size_t)p1.argb_stride;205// results are reported as BGRA206const int offset = c ^ BLUE_OFFSET;207if (!WebPPlaneDistortion((const uint8_t*)p0.argb + offset, stride0,208(const uint8_t*)p1.argb + offset, stride1,209w, h, 4, type, &distortion, results + c)) {210goto Error;211}212total_distortion += distortion;213total_size += w * h;214}215216results[4] = (type == 1) ? (float)GetLogSSIM(total_distortion, total_size)217: (float)GetPSNR(total_distortion, total_size);218ok = 1;219220Error:221WebPPictureFree(&p0);222WebPPictureFree(&p1);223return ok;224}225226#undef BLUE_OFFSET227228#else // defined(WEBP_DISABLE_STATS)229int WebPPlaneDistortion(const uint8_t* src, size_t src_stride,230const uint8_t* ref, size_t ref_stride,231int width, int height, size_t x_step,232int type, float* distortion, float* result) {233(void)src;234(void)src_stride;235(void)ref;236(void)ref_stride;237(void)width;238(void)height;239(void)x_step;240(void)type;241if (distortion == NULL || result == NULL) return 0;242*distortion = 0.f;243*result = 0.f;244return 1;245}246247int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,248int type, float results[5]) {249int i;250(void)src;251(void)ref;252(void)type;253if (results == NULL) return 0;254for (i = 0; i < 5; ++i) results[i] = 0.f;255return 1;256}257258#endif // !defined(WEBP_DISABLE_STATS)259260261