]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - tests/tiny_psnr.c
1/0 fix for files with size=0
[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         int64_t b= exp16_table[i];
72         if(a<(b<<16)) continue;
73         out |= 1<<i;
74         a = ((a/b)<<16) + (((a%b)<<16) + b/2)/b;
75     }
76     return out;
77 }
78
79 #endif
80 static uint64_t int_sqrt(uint64_t a)
81 {
82     uint64_t ret=0;
83     int s;
84     uint64_t ret_sq=0;
85
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;
91         }
92     }
93     return ret;
94 }
95
96 int main(int argc,char* argv[]){
97     int i, j;
98     uint64_t sse=0;
99     uint64_t dev;
100     FILE *f[2];
101     uint8_t buf[2][SIZE];
102     uint64_t psnr;
103     
104     if(argc!=3){
105         printf("tiny_psnr <file1> <file2>\n");
106         return -1;
107     }
108     
109     f[0]= fopen(argv[1], "r");
110     f[1]= fopen(argv[2], "r");
111
112     for(i=0;;){
113         if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
114         if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
115         
116         for(j=0; j<SIZE; i++,j++){
117             const int a= buf[0][j];
118             const int b= buf[1][j];
119             sse += (a-b) * (a-b);
120         }
121     }
122     
123     if(!i) i=1;
124     dev= int_sqrt((sse*F*F)/i);
125     if(sse)
126         psnr= (log16(256*256*255*255LL*i/sse)*284619LL*F + (1<<31)) / (1LL<<32);
127     else
128         psnr= 100*F-1; //floating point free infinity :)
129     
130     printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n", 
131         (int)(dev/F), (int)(dev%F), 
132         (int)(psnr/F), (int)(psnr%F),
133         i);
134     return 0;
135 }
136
137