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