mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-28 14:38:21 +01:00
Merge "WebPPictureDistortion: support ARGB format for 'pic' when computing distortion."
This commit is contained in:
commit
020fd099f6
@ -1110,16 +1110,19 @@ int main(int argc, const char *argv[]) {
|
|||||||
if (print_distortion >= 0) { // print distortion
|
if (print_distortion >= 0) { // print distortion
|
||||||
static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
|
static const char* distortion_names[] = { "PSNR", "SSIM", "LSIM" };
|
||||||
float values[5];
|
float values[5];
|
||||||
// Comparison is performed in YUVA colorspace.
|
if (picture.use_argb != original_picture.use_argb) {
|
||||||
if (original_picture.use_argb &&
|
// Somehow, the WebPEncode() call converted the original picture.
|
||||||
!WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A)) {
|
// We need to make both match before calling WebPPictureDistortion().
|
||||||
fprintf(stderr, "Error while converting original picture to YUVA.\n");
|
int ok = 0;
|
||||||
|
if (picture.use_argb) {
|
||||||
|
ok = WebPPictureYUVAToARGB(&original_picture);
|
||||||
|
} else {
|
||||||
|
ok = WebPPictureARGBToYUVA(&original_picture, WEBP_YUV420A);
|
||||||
|
}
|
||||||
|
if (!ok) {
|
||||||
|
fprintf(stderr, "Error while converting original picture.\n");
|
||||||
goto Error;
|
goto Error;
|
||||||
}
|
}
|
||||||
if (picture.use_argb &&
|
|
||||||
!WebPPictureARGBToYUVA(&picture, WEBP_YUV420A)) {
|
|
||||||
fprintf(stderr, "Error while converting compressed picture to YUVA.\n");
|
|
||||||
goto Error;
|
|
||||||
}
|
}
|
||||||
if (!WebPPictureDistortion(&picture, &original_picture,
|
if (!WebPPictureDistortion(&picture, &original_picture,
|
||||||
print_distortion, values)) {
|
print_distortion, values)) {
|
||||||
@ -1127,9 +1130,14 @@ int main(int argc, const char *argv[]) {
|
|||||||
goto Error;
|
goto Error;
|
||||||
}
|
}
|
||||||
if (!short_output) {
|
if (!short_output) {
|
||||||
fprintf(stderr, "%s: Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
|
fprintf(stderr, "%s: ", distortion_names[print_distortion]);
|
||||||
distortion_names[print_distortion],
|
if (picture.use_argb) {
|
||||||
|
fprintf(stderr, "B:%.2f G:%.2f R:%.2f A:%.2f Total:%.2f\n",
|
||||||
values[0], values[1], values[2], values[3], values[4]);
|
values[0], values[1], values[2], values[3], values[4]);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Y:%.2f U:%.2f V:%.2f A:%.2f Total:%.2f\n",
|
||||||
|
values[0], values[1], values[2], values[3], values[4]);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
|
fprintf(stderr, "%7d %.4f\n", picture.stats->coded_size, values[4]);
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,10 @@
|
|||||||
// Author: Skal (pascal.massimino@gmail.com)
|
// Author: Skal (pascal.massimino@gmail.com)
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "./vp8enci.h"
|
#include "./vp8enci.h"
|
||||||
|
#include "../utils/utils.h"
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// local-min distortion
|
// local-min distortion
|
||||||
@ -23,9 +25,9 @@
|
|||||||
|
|
||||||
#define RADIUS 2 // search radius. Shouldn't be too large.
|
#define RADIUS 2 // search radius. Shouldn't be too large.
|
||||||
|
|
||||||
static float AccumulateLSIM(const uint8_t* src, int src_stride,
|
static void AccumulateLSIM(const uint8_t* src, int src_stride,
|
||||||
const uint8_t* ref, int ref_stride,
|
const uint8_t* ref, int ref_stride,
|
||||||
int w, int h) {
|
int w, int h, DistoStats* stats) {
|
||||||
int x, y;
|
int x, y;
|
||||||
double total_sse = 0.;
|
double total_sse = 0.;
|
||||||
for (y = 0; y < h; ++y) {
|
for (y = 0; y < h; ++y) {
|
||||||
@ -38,16 +40,22 @@ static float AccumulateLSIM(const uint8_t* src, int src_stride,
|
|||||||
const double value = (double)ref[y * ref_stride + x];
|
const double value = (double)ref[y * ref_stride + x];
|
||||||
int i, j;
|
int i, j;
|
||||||
for (j = y_0; j < y_1; ++j) {
|
for (j = y_0; j < y_1; ++j) {
|
||||||
const uint8_t* s = src + j * src_stride;
|
const uint8_t* const s = src + j * src_stride;
|
||||||
for (i = x_0; i < x_1; ++i) {
|
for (i = x_0; i < x_1; ++i) {
|
||||||
const double sse = (double)(s[i] - value) * (s[i] - value);
|
const double diff = s[i] - value;
|
||||||
|
const double sse = diff * diff;
|
||||||
if (sse < best_sse) best_sse = sse;
|
if (sse < best_sse) best_sse = sse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
total_sse += best_sse;
|
total_sse += best_sse;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (float)total_sse;
|
stats->w = w * h;
|
||||||
|
stats->xm = 0;
|
||||||
|
stats->ym = 0;
|
||||||
|
stats->xxm = total_sse;
|
||||||
|
stats->yym = 0;
|
||||||
|
stats->xxm = 0;
|
||||||
}
|
}
|
||||||
#undef RADIUS
|
#undef RADIUS
|
||||||
|
|
||||||
@ -64,62 +72,74 @@ static float GetPSNR(const double v) {
|
|||||||
int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
|
int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
|
||||||
int type, float result[5]) {
|
int type, float result[5]) {
|
||||||
DistoStats stats[5];
|
DistoStats stats[5];
|
||||||
int has_alpha;
|
int w, h;
|
||||||
int uv_w, uv_h;
|
|
||||||
|
memset(stats, 0, sizeof(stats));
|
||||||
|
|
||||||
if (src == NULL || ref == NULL ||
|
if (src == NULL || ref == NULL ||
|
||||||
src->width != ref->width || src->height != ref->height ||
|
src->width != ref->width || src->height != ref->height ||
|
||||||
src->y == NULL || ref->y == NULL ||
|
src->use_argb != ref->use_argb || result == NULL) {
|
||||||
src->u == NULL || ref->u == NULL ||
|
|
||||||
src->v == NULL || ref->v == NULL ||
|
|
||||||
result == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// TODO(skal): provide distortion for ARGB too.
|
|
||||||
if (src->use_argb == 1 || src->use_argb != ref->use_argb) {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
w = src->width;
|
||||||
|
h = src->height;
|
||||||
|
|
||||||
|
if (src->use_argb == 1) {
|
||||||
|
if (src->argb == NULL || ref->argb == NULL) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
int i, j, c;
|
||||||
|
uint8_t* tmp1, *tmp2;
|
||||||
|
uint8_t* const tmp_plane =
|
||||||
|
(uint8_t*)WebPSafeMalloc(2ULL * w * h, sizeof(*tmp_plane));
|
||||||
|
if (tmp_plane == NULL) return 0;
|
||||||
|
tmp1 = tmp_plane;
|
||||||
|
tmp2 = tmp_plane + w * h;
|
||||||
|
for (c = 0; c < 4; ++c) {
|
||||||
|
for (j = 0; j < h; ++j) {
|
||||||
|
for (i = 0; i < w; ++i) {
|
||||||
|
tmp1[j * w + i] = src->argb[i + j * src->argb_stride] >> (c * 8);
|
||||||
|
tmp2[j * w + i] = ref->argb[i + j * ref->argb_stride] >> (c * 8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type >= 2) {
|
||||||
|
AccumulateLSIM(tmp1, w, tmp2, w, w, h, &stats[c]);
|
||||||
|
} else {
|
||||||
|
VP8SSIMAccumulatePlane(tmp1, w, tmp2, w, w, h, &stats[c]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(tmp_plane);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
int has_alpha, uv_w, uv_h;
|
||||||
|
if (src->y == NULL || ref->y == NULL ||
|
||||||
|
src->u == NULL || ref->u == NULL ||
|
||||||
|
src->v == NULL || ref->v == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT);
|
has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT);
|
||||||
if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) ||
|
if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) ||
|
||||||
(has_alpha && (src->a == NULL || ref->a == NULL))) {
|
(has_alpha && (src->a == NULL || ref->a == NULL))) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(stats, 0, sizeof(stats));
|
|
||||||
|
|
||||||
uv_w = (src->width + 1) >> 1;
|
uv_w = (src->width + 1) >> 1;
|
||||||
uv_h = (src->height + 1) >> 1;
|
uv_h = (src->height + 1) >> 1;
|
||||||
if (type >= 2) {
|
if (type >= 2) {
|
||||||
float sse[4];
|
AccumulateLSIM(src->y, src->y_stride, ref->y, ref->y_stride,
|
||||||
sse[0] = AccumulateLSIM(src->y, src->y_stride,
|
w, h, &stats[0]);
|
||||||
ref->y, ref->y_stride, src->width, src->height);
|
AccumulateLSIM(src->u, src->uv_stride, ref->u, ref->uv_stride,
|
||||||
sse[1] = AccumulateLSIM(src->u, src->uv_stride,
|
uv_w, uv_h, &stats[1]);
|
||||||
ref->u, ref->uv_stride, uv_w, uv_h);
|
AccumulateLSIM(src->v, src->uv_stride, ref->v, ref->uv_stride,
|
||||||
sse[2] = AccumulateLSIM(src->v, src->uv_stride,
|
uv_w, uv_h, &stats[2]);
|
||||||
ref->v, ref->uv_stride, uv_w, uv_h);
|
|
||||||
sse[3] = has_alpha ? AccumulateLSIM(src->a, src->a_stride,
|
|
||||||
ref->a, ref->a_stride,
|
|
||||||
src->width, src->height)
|
|
||||||
: 0.f;
|
|
||||||
result[0] = GetPSNR(sse[0] / (src->width * src->height));
|
|
||||||
result[1] = GetPSNR(sse[1] / (uv_w * uv_h));
|
|
||||||
result[2] = GetPSNR(sse[2] / (uv_w * uv_h));
|
|
||||||
result[3] = GetPSNR(sse[3] / (src->width * src->height));
|
|
||||||
{
|
|
||||||
double total_sse = sse[0] + sse[1] + sse[2];
|
|
||||||
int total_pixels = src->width * src->height + 2 * uv_w * uv_h;
|
|
||||||
if (has_alpha) {
|
if (has_alpha) {
|
||||||
total_pixels += src->width * src->height;
|
AccumulateLSIM(src->a, src->a_stride, ref->a, ref->a_stride,
|
||||||
total_sse += sse[3];
|
w, h, &stats[3]);
|
||||||
}
|
|
||||||
result[4] = GetPSNR(total_sse / total_pixels);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int c;
|
|
||||||
VP8SSIMAccumulatePlane(src->y, src->y_stride,
|
VP8SSIMAccumulatePlane(src->y, src->y_stride,
|
||||||
ref->y, ref->y_stride,
|
ref->y, ref->y_stride,
|
||||||
src->width, src->height, &stats[0]);
|
w, h, &stats[0]);
|
||||||
VP8SSIMAccumulatePlane(src->u, src->uv_stride,
|
VP8SSIMAccumulatePlane(src->u, src->uv_stride,
|
||||||
ref->u, ref->uv_stride,
|
ref->u, ref->uv_stride,
|
||||||
uv_w, uv_h, &stats[1]);
|
uv_w, uv_h, &stats[1]);
|
||||||
@ -129,8 +149,13 @@ int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref,
|
|||||||
if (has_alpha) {
|
if (has_alpha) {
|
||||||
VP8SSIMAccumulatePlane(src->a, src->a_stride,
|
VP8SSIMAccumulatePlane(src->a, src->a_stride,
|
||||||
ref->a, ref->a_stride,
|
ref->a, ref->a_stride,
|
||||||
src->width, src->height, &stats[3]);
|
w, h, &stats[3]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Final stat calculations.
|
||||||
|
{
|
||||||
|
int c;
|
||||||
for (c = 0; c <= 4; ++c) {
|
for (c = 0; c <= 4; ++c) {
|
||||||
if (type == 1) {
|
if (type == 1) {
|
||||||
const double v = VP8SSIMGet(&stats[c]);
|
const double v = VP8SSIMGet(&stats[c]);
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define WEBP_ENCODER_ABI_VERSION 0x0207 // MAJOR(8b) + MINOR(8b)
|
#define WEBP_ENCODER_ABI_VERSION 0x0208 // MAJOR(8b) + MINOR(8b)
|
||||||
|
|
||||||
// Note: forward declaring enumerations is not allowed in (strict) C and C++,
|
// Note: forward declaring enumerations is not allowed in (strict) C and C++,
|
||||||
// the types are left here for reference.
|
// the types are left here for reference.
|
||||||
@ -379,8 +379,8 @@ WEBP_EXTERN(void) WebPPictureFree(WebPPicture* picture);
|
|||||||
// Returns false in case of memory allocation error.
|
// Returns false in case of memory allocation error.
|
||||||
WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
|
WEBP_EXTERN(int) WebPPictureCopy(const WebPPicture* src, WebPPicture* dst);
|
||||||
|
|
||||||
// Compute PSNR, SSIM or LSIM distortion metric between two pictures.
|
// Compute PSNR, SSIM or LSIM distortion metric between two pictures. Results
|
||||||
// Result is in dB, stores in result[] in the Y/U/V/Alpha/All order.
|
// are in dB, stored in result[] in the Y/U/V/Alpha/All or B/G/R/A/All order.
|
||||||
// Returns false in case of error (src and ref don't have same dimension, ...)
|
// Returns false in case of error (src and ref don't have same dimension, ...)
|
||||||
// Warning: this function is rather CPU-intensive.
|
// Warning: this function is rather CPU-intensive.
|
||||||
WEBP_EXTERN(int) WebPPictureDistortion(
|
WEBP_EXTERN(int) WebPPictureDistortion(
|
||||||
|
Loading…
Reference in New Issue
Block a user