]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - tests/tiny_psnr.c
fixpoint log() for tiny_psnr so it can output actual PSNR
[frescor/ffmpeg.git] / tests / tiny_psnr.c
1 /*
2  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <assert.h>
23
24 #define F 100
25 #define SIZE 2048
26
27 uint64_t exp16_table[20]={
28      65537,
29      65538,
30      65540,
31      65544,
32      65552,
33      65568,
34      65600,
35      65664,
36      65793,
37      66050,
38      66568,
39      67616,
40      69763,
41      74262,
42      84150,
43     108051,
44     178145,
45     484249,
46    3578144,
47  195360063,
48 };
49 #if 1
50 // 16.16 fixpoint exp()
51 static unsigned int exp16(unsigned int a){
52     int i;
53     int out= 1<<16;
54
55     for(i=19;i>=0;i--){
56         if(a&(1<<i))
57             out= (out*exp16_table[i] + (1<<15))>>16;
58     }
59
60     return out;
61 }
62 // 16.16 fixpoint log()
63 static uint64_t log16(uint64_t a){
64     int i;
65     int out=0;
66     
67     assert(a >= (1<<16));
68     a<<=16;
69     
70     for(i=19;i>=0;i--){
71         if(a<(exp16_table[i]<<16)) continue;
72         out |= 1<<i;
73         a = ((a<<16) + exp16_table[i]/2)/exp16_table[i];
74     }
75     return out;
76 }
77
78 #endif
79 static uint64_t int_sqrt(uint64_t a)
80 {
81     uint64_t ret=0;
82     int s;
83     uint64_t ret_sq=0;
84
85     for(s=31; s>=0; s--){
86         uint64_t b= ret_sq + (1ULL<<(s*2)) + (ret<<s)*2;
87         if(b<=a){
88             ret_sq=b;
89             ret+= 1ULL<<s;
90         }
91     }
92     return ret;
93 }
94
95 int main(int argc,char* argv[]){
96     int i, j;
97     uint64_t sse=0;
98     uint64_t dev;
99     FILE *f[2];
100     uint8_t buf[2][SIZE];
101     uint64_t psnr;
102     
103     if(argc!=3){
104         printf("tiny_psnr <file1> <file2>\n");
105         return -1;
106     }
107     
108     f[0]= fopen(argv[1], "r");
109     f[1]= fopen(argv[2], "r");
110
111     for(i=0;;){
112         if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
113         if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
114         
115         for(j=0; j<SIZE; i++,j++){
116             const int a= buf[0][j];
117             const int b= buf[1][j];
118             sse += (a-b) * (a-b);
119         }
120     }
121     
122     dev= int_sqrt((sse*F*F)/i);
123     if(sse)
124         psnr= (log16(256*256*255*255LL*i/sse)*284619LL*F + (1<<31)) / (1LL<<32);
125     else
126         psnr= 100*F-1; //floating point free infinity :)
127     
128     printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n", 
129         (int)(dev/F), (int)(dev%F), 
130         (int)(psnr/F), (int)(psnr%F),
131         i);
132     return 0;
133 }
134
135