1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

tiny_psnr: switch f32 handling to floating point

Also add support for f64.
This commit is contained in:
Anton Khirnov 2013-10-14 21:10:08 +02:00
parent 29c455ce3d
commit dd33637c18

View File

@ -23,8 +23,11 @@
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <assert.h> #include <assert.h>
#include <math.h>
#include <float.h>
#include "libavutil/intfloat.h" #include "libavutil/intfloat.h"
#include "libavutil/intreadwrite.h"
#define FFMIN(a, b) ((a) > (b) ? (b) : (a)) #define FFMIN(a, b) ((a) > (b) ? (b) : (a))
#define F 100 #define F 100
@ -107,21 +110,26 @@ static float get_f32l(uint8_t *p)
return v.f; return v.f;
} }
static double get_f64l(uint8_t *p)
{
return av_int2double(AV_RL64(p));
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i, j; int i, j;
uint64_t sse = 0; uint64_t sse = 0;
uint64_t dev; double sse_d = 0.0;
FILE *f[2]; FILE *f[2];
uint8_t buf[2][SIZE]; uint8_t buf[2][SIZE];
uint64_t psnr;
int len = 1; int len = 1;
int64_t max; int64_t max;
int shift = argc < 5 ? 0 : atoi(argv[4]); int shift = argc < 5 ? 0 : atoi(argv[4]);
int skip_bytes = argc < 6 ? 0 : atoi(argv[5]); int skip_bytes = argc < 6 ? 0 : atoi(argv[5]);
int size0 = 0; int size0 = 0;
int size1 = 0; int size1 = 0;
int maxdist = 0; uint64_t maxdist = 0;
double maxdist_d = 0.0;
if (argc < 3) { if (argc < 3) {
printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n"); printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n");
@ -136,6 +144,8 @@ int main(int argc, char *argv[])
len = 2; len = 2;
} else if (!strcmp(argv[3], "f32")) { } else if (!strcmp(argv[3], "f32")) {
len = 4; len = 4;
} else if (!strcmp(argv[3], "f64")) {
len = 8;
} else { } else {
char *end; char *end;
len = strtol(argv[3], &end, 0); len = strtol(argv[3], &end, 0);
@ -184,23 +194,42 @@ int main(int argc, char *argv[])
int s1 = fread(buf[1], 1, SIZE, f[1]); int s1 = fread(buf[1], 1, SIZE, f[1]);
for (j = 0; j < FFMIN(s0, s1); j += len) { for (j = 0; j < FFMIN(s0, s1); j += len) {
int64_t a = buf[0][j]; switch (len) {
int64_t b = buf[1][j]; case 1:
int dist; case 2: {
if (len == 2) { int64_t a = buf[0][j];
a = get_s16l(buf[0] + j); int64_t b = buf[1][j];
b = get_s16l(buf[1] + j); int dist;
} else if (len == 4) { if (len == 2) {
a = get_f32l(buf[0] + j) * (1 << 24); a = get_s16l(buf[0] + j);
b = get_f32l(buf[1] + j) * (1 << 24); b = get_s16l(buf[1] + j);
} else { } else {
a = buf[0][j]; a = buf[0][j];
b = buf[1][j]; b = buf[1][j];
}
sse += (a - b) * (a - b);
dist = abs(a - b);
if (dist > maxdist)
maxdist = dist;
break;
}
case 4:
case 8: {
double dist, a, b;
if (len == 8) {
a = get_f64l(buf[0] + j);
b = get_f64l(buf[1] + j);
} else {
a = get_f32l(buf[0] + j);
b = get_f32l(buf[1] + j);
}
dist = fabs(a - b);
sse_d += (a - b) * (a - b);
if (dist > maxdist_d)
maxdist_d = dist;
break;
}
} }
sse += (a - b) * (a - b);
dist = abs(a - b);
if (dist > maxdist)
maxdist = dist;
} }
size0 += s0; size0 += s0;
size1 += s1; size1 += s1;
@ -211,16 +240,41 @@ int main(int argc, char *argv[])
i = FFMIN(size0, size1) / len; i = FFMIN(size0, size1) / len;
if (!i) if (!i)
i = 1; i = 1;
dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i); switch (len) {
if (sse) case 1:
psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) * case 2: {
284619LL * F + (1LL << 31)) / (1LL << 32); uint64_t psnr;
else uint64_t dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i);
psnr = 1000 * F - 1; // floating point free infinity :) if (sse)
psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) *
284619LL * F + (1LL << 31)) / (1LL << 32);
else
psnr = 1000 * F - 1; // floating point free infinity :)
printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5d bytes:%9d/%9d\n", printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5"PRIu64" bytes:%9d/%9d\n",
(int)(dev / F), (int)(dev % F), (int)(dev / F), (int)(dev % F),
(int)(psnr / F), (int)(psnr % F), (int)(psnr / F), (int)(psnr % F),
maxdist, size0, size1); maxdist, size0, size1);
break;
}
case 4:
case 8: {
char psnr_str[64];
double dev = sqrt(sse_d / i);
uint64_t scale = (len == 4) ? (1ULL << 24) : (1ULL << 32);
if (sse_d) {
double psnr = 2 * log(DBL_MAX) - log(i / sse_d);
snprintf(psnr_str, sizeof(psnr_str), "%5.02f", psnr);
} else
snprintf(psnr_str, sizeof(psnr_str), "inf");
maxdist = maxdist_d * scale;
printf("stddev:%10.2f PSNR:%s MAXDIFF:%10"PRIu64" bytes:%9d/%9d\n",
dev * scale, psnr_str, maxdist, size0, size1);
break;
}
}
return 0; return 0;
} }