tiny_psnr.c 8.3 KB

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