tiny_psnr: switch f32 handling to floating point
[libav.git] / tests / tiny_psnr.c
CommitLineData
67cbe681
MN
1/*
2 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3 *
2912e87a 4 * This file is part of Libav.
244e1e64 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
67cbe681
MN
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
244e1e64 9 * version 2.1 of the License, or (at your option) any later version.
67cbe681 10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
67cbe681
MN
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
67cbe681
MN
19 */
20
21#include <stdio.h>
ba96e97f 22#include <stdlib.h>
c43d77c1 23#include <string.h>
67cbe681 24#include <inttypes.h>
9e2a16e1 25#include <assert.h>
dd33637c
AK
26#include <math.h>
27#include <float.h>
67cbe681 28
9f1280de 29#include "libavutil/intfloat.h"
dd33637c 30#include "libavutil/intreadwrite.h"
9f1280de 31
b481bbc3 32#define FFMIN(a, b) ((a) > (b) ? (b) : (a))
67cbe681
MN
33#define F 100
34#define SIZE 2048
35
b481bbc3
GN
36uint64_t exp16_table[21] = {
37 65537,
38 65538,
39 65540,
40 65544,
41 65552,
42 65568,
43 65600,
44 65664,
45 65793,
46 66050,
47 66568,
48 67616,
49 69763,
50 74262,
51 84150,
52 108051,
53 178145,
54 484249,
55 3578144,
56 195360063,
57 582360139072LL,
9e2a16e1 58};
e9b67fe4 59
9e2a16e1 60// 16.16 fixpoint log()
b481bbc3
GN
61static int64_t log16(uint64_t a)
62{
9e2a16e1 63 int i;
b481bbc3
GN
64 int out = 0;
65
66 if (a < 1 << 16)
67 return -log16((1LL << 32) / a);
68 a <<= 16;
69
70 for (i = 20; i >= 0; i--) {
71 int64_t b = exp16_table[i];
72 if (a < (b << 16))
73 continue;
74 out |= 1 << i;
75 a = ((a / b) << 16) + (((a % b) << 16) + b / 2) / b;
9e2a16e1
MN
76 }
77 return out;
78}
79
67cbe681
MN
80static uint64_t int_sqrt(uint64_t a)
81{
b481bbc3
GN
82 uint64_t ret = 0;
83 uint64_t ret_sq = 0;
67cbe681 84 int s;
67cbe681 85
b481bbc3
GN
86 for (s = 31; s >= 0; s--) {
87 uint64_t b = ret_sq + (1ULL << (s * 2)) + (ret << s) * 2;
88 if (b <= a) {
89 ret_sq = b;
90 ret += 1ULL << s;
67cbe681
MN
91 }
92 }
93 return ret;
94}
95
9f1280de
MR
96static int16_t get_s16l(uint8_t *p)
97{
98 union {
99 uint16_t u;
100 int16_t s;
101 } v;
102 v.u = p[0] | p[1] << 8;
103 return v.s;
104}
105
106static float get_f32l(uint8_t *p)
107{
108 union av_intfloat32 v;
109 v.i = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
110 return v.f;
111}
112
dd33637c
AK
113static double get_f64l(uint8_t *p)
114{
115 return av_int2double(AV_RL64(p));
116}
117
b481bbc3
GN
118int main(int argc, char *argv[])
119{
67cbe681 120 int i, j;
b481bbc3 121 uint64_t sse = 0;
dd33637c 122 double sse_d = 0.0;
67cbe681
MN
123 FILE *f[2];
124 uint8_t buf[2][SIZE];
9f1280de
MR
125 int len = 1;
126 int64_t max;
b481bbc3
GN
127 int shift = argc < 5 ? 0 : atoi(argv[4]);
128 int skip_bytes = argc < 6 ? 0 : atoi(argv[5]);
129 int size0 = 0;
130 int size1 = 0;
dd33637c
AK
131 uint64_t maxdist = 0;
132 double maxdist_d = 0.0;
b481bbc3
GN
133
134 if (argc < 3) {
b8889ea5 135 printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n");
c43d77c1 136 printf("WAV headers are skipped automatically.\n");
3ec8d24a 137 return 1;
67cbe681 138 }
115329f1 139
9f1280de
MR
140 if (argc > 3) {
141 if (!strcmp(argv[3], "u8")) {
142 len = 1;
143 } else if (!strcmp(argv[3], "s16")) {
144 len = 2;
145 } else if (!strcmp(argv[3], "f32")) {
146 len = 4;
dd33637c
AK
147 } else if (!strcmp(argv[3], "f64")) {
148 len = 8;
9f1280de
MR
149 } else {
150 char *end;
151 len = strtol(argv[3], &end, 0);
3dc06b69 152 if (*end || len < 1 || len > 2) {
9f1280de
MR
153 fprintf(stderr, "Unsupported sample format: %s\n", argv[3]);
154 return 1;
155 }
156 }
157 }
158
bf868c4a 159 max = (1LL << (8 * len)) - 1;
9f1280de 160
b481bbc3
GN
161 f[0] = fopen(argv[1], "rb");
162 f[1] = fopen(argv[2], "rb");
163 if (!f[0] || !f[1]) {
cc8de8e8 164 fprintf(stderr, "Could not open input files.\n");
3ec8d24a 165 return 1;
e740c796 166 }
c43d77c1
MR
167
168 for (i = 0; i < 2; i++) {
169 uint8_t *p = buf[i];
cd3cb048
MR
170 if (fread(p, 1, 12, f[i]) != 12)
171 return 1;
b481bbc3
GN
172 if (!memcmp(p, "RIFF", 4) &&
173 !memcmp(p + 8, "WAVE", 4)) {
cd3cb048
MR
174 if (fread(p, 1, 8, f[i]) != 8)
175 return 1;
c43d77c1 176 while (memcmp(p, "data", 4)) {
b481bbc3 177 int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
c43d77c1 178 fseek(f[i], s, SEEK_CUR);
cd3cb048
MR
179 if (fread(p, 1, 8, f[i]) != 8)
180 return 1;
c43d77c1
MR
181 }
182 } else {
183 fseek(f[i], -12, SEEK_CUR);
184 }
185 }
186
b481bbc3 187 fseek(f[shift < 0], abs(shift), SEEK_CUR);
67cbe681 188
b481bbc3
GN
189 fseek(f[0], skip_bytes, SEEK_CUR);
190 fseek(f[1], skip_bytes, SEEK_CUR);
b8889ea5 191
b481bbc3
GN
192 for (;;) {
193 int s0 = fread(buf[0], 1, SIZE, f[0]);
194 int s1 = fread(buf[1], 1, SIZE, f[1]);
115329f1 195
9f1280de 196 for (j = 0; j < FFMIN(s0, s1); j += len) {
dd33637c
AK
197 switch (len) {
198 case 1:
199 case 2: {
200 int64_t a = buf[0][j];
201 int64_t b = buf[1][j];
202 int dist;
203 if (len == 2) {
204 a = get_s16l(buf[0] + j);
205 b = get_s16l(buf[1] + j);
206 } else {
207 a = buf[0][j];
208 b = buf[1][j];
209 }
210 sse += (a - b) * (a - b);
211 dist = abs(a - b);
212 if (dist > maxdist)
213 maxdist = dist;
214 break;
215 }
216 case 4:
217 case 8: {
218 double dist, a, b;
219 if (len == 8) {
220 a = get_f64l(buf[0] + j);
221 b = get_f64l(buf[1] + j);
222 } else {
223 a = get_f32l(buf[0] + j);
224 b = get_f32l(buf[1] + j);
225 }
226 dist = fabs(a - b);
227 sse_d += (a - b) * (a - b);
228 if (dist > maxdist_d)
229 maxdist_d = dist;
230 break;
231 }
0c90161f 232 }
67cbe681 233 }
1e90317b
MN
234 size0 += s0;
235 size1 += s1;
b481bbc3 236 if (s0 + s1 <= 0)
1e90317b 237 break;
67cbe681 238 }
115329f1 239
b481bbc3
GN
240 i = FFMIN(size0, size1) / len;
241 if (!i)
242 i = 1;
dd33637c
AK
243 switch (len) {
244 case 1:
245 case 2: {
246 uint64_t psnr;
247 uint64_t dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i);
248 if (sse)
249 psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) *
250 284619LL * F + (1LL << 31)) / (1LL << 32);
251 else
252 psnr = 1000 * F - 1; // floating point free infinity :)
253
254 printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5"PRIu64" bytes:%9d/%9d\n",
255 (int)(dev / F), (int)(dev % F),
256 (int)(psnr / F), (int)(psnr % F),
257 maxdist, size0, size1);
258 break;
259 }
260 case 4:
261 case 8: {
262 char psnr_str[64];
263 double dev = sqrt(sse_d / i);
264 uint64_t scale = (len == 4) ? (1ULL << 24) : (1ULL << 32);
265
266 if (sse_d) {
267 double psnr = 2 * log(DBL_MAX) - log(i / sse_d);
268 snprintf(psnr_str, sizeof(psnr_str), "%5.02f", psnr);
269 } else
270 snprintf(psnr_str, sizeof(psnr_str), "inf");
271
272 maxdist = maxdist_d * scale;
273
274 printf("stddev:%10.2f PSNR:%s MAXDIFF:%10"PRIu64" bytes:%9d/%9d\n",
275 dev * scale, psnr_str, maxdist, size0, size1);
276 break;
277 }
278 }
67cbe681
MN
279 return 0;
280}