2 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
27 uint64_t exp16_table[21]={
51 // 16.16 fixpoint exp()
52 static unsigned int exp16(unsigned int a){
58 out= (out*exp16_table[i] + (1<<15))>>16;
63 // 16.16 fixpoint log()
64 static int64_t log16(uint64_t a){
69 return -log16((1LL<<32) / a);
73 int64_t b= exp16_table[i];
74 if(a<(b<<16)) continue;
76 a = ((a/b)<<16) + (((a%b)<<16) + b/2)/b;
82 static uint64_t int_sqrt(uint64_t a)
89 uint64_t b= ret_sq + (1ULL<<(s*2)) + (ret<<s)*2;
98 int main(int argc,char* argv[]){
103 uint8_t buf[2][SIZE];
105 int len= argc<4 ? 1 : 2;
106 int64_t max= (1<<(8*len))-1;
109 printf("tiny_psnr <file1> <file2>\n");
113 f[0]= fopen(argv[1], "rb");
114 f[1]= fopen(argv[2], "rb");
117 if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
118 if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
120 for(j=0; j<SIZE; i++,j++){
121 int64_t a= buf[0][j];
122 int64_t b= buf[1][j];
124 a= (int16_t)(a | (buf[0][++j]<<8));
125 b= (int16_t)(b | (buf[1][ j]<<8));
127 sse += (a-b) * (a-b);
132 dev= int_sqrt( ((sse/i)*F*F) + (((sse%i)*F*F) + i/2)/i );
134 psnr= ((2*log16(max<<16) + log16(i) - log16(sse))*284619LL*F + (1<<31)) / (1LL<<32);
136 psnr= 100*F-1; //floating point free infinity :)
138 printf("stddev:%3d.%02d PSNR:%2d.%02d bytes:%d\n",
139 (int)(dev/F), (int)(dev%F),
140 (int)(psnr/F), (int)(psnr%F),