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.

227 lines
5.7KB

  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 <assert.h>
  25. #include "libavutil/intfloat.h"
  26. #define FFMIN(a, b) ((a) > (b) ? (b) : (a))
  27. #define F 100
  28. #define SIZE 2048
  29. uint64_t exp16_table[21] = {
  30. 65537,
  31. 65538,
  32. 65540,
  33. 65544,
  34. 65552,
  35. 65568,
  36. 65600,
  37. 65664,
  38. 65793,
  39. 66050,
  40. 66568,
  41. 67616,
  42. 69763,
  43. 74262,
  44. 84150,
  45. 108051,
  46. 178145,
  47. 484249,
  48. 3578144,
  49. 195360063,
  50. 582360139072LL,
  51. };
  52. // 16.16 fixpoint log()
  53. static int64_t log16(uint64_t a)
  54. {
  55. int i;
  56. int out = 0;
  57. if (a < 1 << 16)
  58. return -log16((1LL << 32) / a);
  59. a <<= 16;
  60. for (i = 20; i >= 0; i--) {
  61. int64_t b = exp16_table[i];
  62. if (a < (b << 16))
  63. continue;
  64. out |= 1 << i;
  65. a = ((a / b) << 16) + (((a % b) << 16) + b / 2) / b;
  66. }
  67. return out;
  68. }
  69. static uint64_t int_sqrt(uint64_t a)
  70. {
  71. uint64_t ret = 0;
  72. uint64_t ret_sq = 0;
  73. int s;
  74. for (s = 31; s >= 0; s--) {
  75. uint64_t b = ret_sq + (1ULL << (s * 2)) + (ret << s) * 2;
  76. if (b <= a) {
  77. ret_sq = b;
  78. ret += 1ULL << s;
  79. }
  80. }
  81. return ret;
  82. }
  83. static int16_t get_s16l(uint8_t *p)
  84. {
  85. union {
  86. uint16_t u;
  87. int16_t s;
  88. } v;
  89. v.u = p[0] | p[1] << 8;
  90. return v.s;
  91. }
  92. static float get_f32l(uint8_t *p)
  93. {
  94. union av_intfloat32 v;
  95. v.i = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
  96. return v.f;
  97. }
  98. int main(int argc, char *argv[])
  99. {
  100. int i, j;
  101. uint64_t sse = 0;
  102. uint64_t dev;
  103. FILE *f[2];
  104. uint8_t buf[2][SIZE];
  105. uint64_t psnr;
  106. int len = 1;
  107. int64_t max;
  108. int shift = argc < 5 ? 0 : atoi(argv[4]);
  109. int skip_bytes = argc < 6 ? 0 : atoi(argv[5]);
  110. int size0 = 0;
  111. int size1 = 0;
  112. int maxdist = 0;
  113. if (argc < 3) {
  114. printf("tiny_psnr <file1> <file2> [<elem size> [<shift> [<skip bytes>]]]\n");
  115. printf("WAV headers are skipped automatically.\n");
  116. return 1;
  117. }
  118. if (argc > 3) {
  119. if (!strcmp(argv[3], "u8")) {
  120. len = 1;
  121. } else if (!strcmp(argv[3], "s16")) {
  122. len = 2;
  123. } else if (!strcmp(argv[3], "f32")) {
  124. len = 4;
  125. } else {
  126. char *end;
  127. len = strtol(argv[3], &end, 0);
  128. if (*end || len < 1 || len > 2) {
  129. fprintf(stderr, "Unsupported sample format: %s\n", argv[3]);
  130. return 1;
  131. }
  132. }
  133. }
  134. max = (1LL << (8 * len)) - 1;
  135. f[0] = fopen(argv[1], "rb");
  136. f[1] = fopen(argv[2], "rb");
  137. if (!f[0] || !f[1]) {
  138. fprintf(stderr, "Could not open input files.\n");
  139. return 1;
  140. }
  141. for (i = 0; i < 2; i++) {
  142. uint8_t *p = buf[i];
  143. if (fread(p, 1, 12, f[i]) != 12)
  144. return 1;
  145. if (!memcmp(p, "RIFF", 4) &&
  146. !memcmp(p + 8, "WAVE", 4)) {
  147. if (fread(p, 1, 8, f[i]) != 8)
  148. return 1;
  149. while (memcmp(p, "data", 4)) {
  150. int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
  151. fseek(f[i], s, SEEK_CUR);
  152. if (fread(p, 1, 8, f[i]) != 8)
  153. return 1;
  154. }
  155. } else {
  156. fseek(f[i], -12, SEEK_CUR);
  157. }
  158. }
  159. fseek(f[shift < 0], abs(shift), SEEK_CUR);
  160. fseek(f[0], skip_bytes, SEEK_CUR);
  161. fseek(f[1], skip_bytes, SEEK_CUR);
  162. for (;;) {
  163. int s0 = fread(buf[0], 1, SIZE, f[0]);
  164. int s1 = fread(buf[1], 1, SIZE, f[1]);
  165. for (j = 0; j < FFMIN(s0, s1); j += len) {
  166. int64_t a = buf[0][j];
  167. int64_t b = buf[1][j];
  168. int dist;
  169. if (len == 2) {
  170. a = get_s16l(buf[0] + j);
  171. b = get_s16l(buf[1] + j);
  172. } else if (len == 4) {
  173. a = get_f32l(buf[0] + j) * (1 << 24);
  174. b = get_f32l(buf[1] + j) * (1 << 24);
  175. } else {
  176. a = buf[0][j];
  177. b = buf[1][j];
  178. }
  179. sse += (a - b) * (a - b);
  180. dist = abs(a - b);
  181. if (dist > maxdist)
  182. maxdist = dist;
  183. }
  184. size0 += s0;
  185. size1 += s1;
  186. if (s0 + s1 <= 0)
  187. break;
  188. }
  189. i = FFMIN(size0, size1) / len;
  190. if (!i)
  191. i = 1;
  192. dev = int_sqrt(((sse / i) * F * F) + (((sse % i) * F * F) + i / 2) / i);
  193. if (sse)
  194. psnr = ((2 * log16(max << 16) + log16(i) - log16(sse)) *
  195. 284619LL * F + (1LL << 31)) / (1LL << 32);
  196. else
  197. psnr = 1000 * F - 1; // floating point free infinity :)
  198. printf("stddev:%5d.%02d PSNR:%3d.%02d MAXDIFF:%5d bytes:%9d/%9d\n",
  199. (int)(dev / F), (int)(dev % F),
  200. (int)(psnr / F), (int)(psnr % F),
  201. maxdist, size0, size1);
  202. return 0;
  203. }