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[20]={
50 // 16.16 fixpoint exp()
51 static unsigned int exp16(unsigned int a){
57 out= (out*exp16_table[i] + (1<<15))>>16;
62 // 16.16 fixpoint log()
63 static uint64_t log16(uint64_t a){
71 if(a<(exp16_table[i]<<16)) continue;
73 a = ((a<<16) + exp16_table[i]/2)/exp16_table[i];
79 static uint64_t int_sqrt(uint64_t a)
86 uint64_t b= ret_sq + (1ULL<<(s*2)) + (ret<<s)*2;
95 int main(int argc,char* argv[]){
100 uint8_t buf[2][SIZE];
104 printf("tiny_psnr <file1> <file2>\n");
108 f[0]= fopen(argv[1], "r");
109 f[1]= fopen(argv[2], "r");
112 if( fread(buf[0], SIZE, 1, f[0]) != 1) break;
113 if( fread(buf[1], SIZE, 1, f[1]) != 1) break;
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);
122 dev= int_sqrt((sse*F*F)/i);
124 psnr= (log16(256*256*255*255LL*i/sse)*284619LL*F + (1<<31)) / (1LL<<32);
126 psnr= 100*F-1; //floating point free infinity :)
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),