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