You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
7.2KB

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