floatimg_cmp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg 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.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <inttypes.h>
  23. #include <float.h>
  24. #include "libavutil/avutil.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/intfloat.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/lfg.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/parseutils.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libswscale/swscale.h"
  33. #define DEFAULT_W 96
  34. #define DEFAULT_H 96
  35. static const enum AVPixelFormat pix_fmts[] = {
  36. AV_PIX_FMT_YUV444P16LE,
  37. AV_PIX_FMT_YUV444P,
  38. AV_PIX_FMT_YUV444P9LE, AV_PIX_FMT_YUV444P10LE,
  39. AV_PIX_FMT_YUV444P12LE, AV_PIX_FMT_YUV444P14LE,
  40. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  41. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  42. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  43. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  44. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  45. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
  46. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
  47. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  48. AV_PIX_FMT_GBRP9LE,
  49. AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRAP10LE,
  50. AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRAP12LE,
  51. AV_PIX_FMT_GBRP14LE,
  52. AV_PIX_FMT_GBRP16LE, AV_PIX_FMT_GBRAP16LE
  53. };
  54. const char *usage = "floatimg_cmp -pixel_format <pix_fmt> -size <image_size> -ref <testfile>\n";
  55. int main(int argc, char **argv)
  56. {
  57. enum AVPixelFormat inFormat = AV_PIX_FMT_NONE;
  58. enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
  59. const AVPixFmtDescriptor *desc;
  60. uint8_t *ptr;
  61. uint32_t *in, *out;
  62. uint8_t *rgbIn[4] = {NULL, NULL, NULL, NULL};
  63. uint8_t *rgbOut[4] = {NULL, NULL, NULL, NULL};
  64. int rgbStride[4];
  65. uint8_t *dst[4] = {NULL, NULL, NULL, NULL};
  66. int dstStride[4];
  67. int i, x, y, p, size, count;
  68. int res = -1;
  69. int w = -1;
  70. int h = -1;
  71. union av_intfloat32 v0, v1;
  72. double sum;
  73. float minimum, maximum, diff;
  74. struct SwsContext *sws = NULL;
  75. AVLFG rand;
  76. FILE *fp = NULL;
  77. for (i = 1; i < argc; i += 2) {
  78. if (argv[i][0] != '-' || i + 1 == argc)
  79. goto bad_option;
  80. if (!strcmp(argv[i], "-ref")) {
  81. fp = fopen(argv[i + 1], "rb");
  82. if (!fp) {
  83. fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
  84. goto end;
  85. }
  86. } else if (!strcmp(argv[i], "-size")) {
  87. res = av_parse_video_size(&w, &h, argv[i + 1]);
  88. if (res < 0) {
  89. fprintf(stderr, "invalid video size %s\n", argv[i + 1]);
  90. goto end;
  91. }
  92. } else if (!strcmp(argv[i], "-pixel_format")) {
  93. inFormat = av_get_pix_fmt(argv[i + 1]);
  94. if (inFormat == AV_PIX_FMT_NONE) {
  95. fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
  96. goto end;
  97. }
  98. } else {
  99. bad_option:
  100. fprintf(stderr, "%s", usage);
  101. fprintf(stderr, "bad option or argument missing (%s)\n", argv[i]);
  102. goto end;
  103. };
  104. }
  105. if (!fp) {
  106. inFormat = AV_PIX_FMT_GBRPF32LE;
  107. w = DEFAULT_W;
  108. h = DEFAULT_H;
  109. }
  110. if (w <= 0 || h <= 0) {
  111. fprintf(stderr, "%s", usage);
  112. fprintf(stderr, "invalid -video_size\n");
  113. goto end;
  114. }
  115. if (inFormat == AV_PIX_FMT_NONE) {
  116. fprintf(stderr, "%s", usage);
  117. fprintf(stderr, "invalid input pixel format\n");
  118. goto end;
  119. }
  120. desc = av_pix_fmt_desc_get(inFormat);
  121. if (!(desc->flags & AV_PIX_FMT_FLAG_FLOAT)) {
  122. fprintf(stderr, "input pixel format not floating point.\n");
  123. goto end;
  124. }
  125. res = av_image_fill_linesizes(rgbStride, inFormat, w);
  126. if (res < 0) {
  127. fprintf(stderr, "av_image_fill_linesizes failed\n");
  128. goto end;
  129. }
  130. for (p = 0; p < 4; p++) {
  131. rgbStride[p] = FFALIGN(rgbStride[p], 16);
  132. if (rgbStride[p]) {
  133. rgbIn[p] = av_mallocz(rgbStride[p] * h + 16);
  134. rgbOut[p] = av_mallocz(rgbStride[p] * h + 16);
  135. }
  136. if (rgbStride[p] && (!rgbIn[p] || !rgbOut[p])) {
  137. goto end;
  138. }
  139. }
  140. for (i = 0; i < FF_ARRAY_ELEMS(pix_fmts); i++) {
  141. dstFormat = pix_fmts[i];
  142. if (fp) {
  143. fseek(fp, 0, SEEK_SET);
  144. for (p = 0; p < 4; p++) {
  145. if (!rgbStride[p])
  146. continue;
  147. ptr = rgbIn[p];
  148. for (y = 0; y < h; y++) {
  149. size = fread(ptr, 1, w*4, fp);
  150. if (size != w*4) {
  151. fprintf(stderr, "read error: %d\n", size);
  152. goto end;
  153. }
  154. ptr += rgbStride[p];
  155. }
  156. }
  157. } else {
  158. // fill src with random values between 0.0 - 1.0
  159. av_lfg_init(&rand, 1);
  160. for (p = 0; p < 4; p++) {
  161. if (!rgbStride[p])
  162. continue;
  163. for (y = 0; y < h; y++) {
  164. in = (uint32_t*)(rgbIn[p] + y * rgbStride[p]);
  165. for (x = 0; x < w; x++) {
  166. v0.f = (float)av_lfg_get(&rand)/(float)(UINT32_MAX);
  167. *in++ = AV_RL32(&v0.i);
  168. }
  169. }
  170. }
  171. }
  172. // setup intermediate image
  173. for (p = 0; p < 4; p++) {
  174. av_freep(&dst[p]);
  175. }
  176. res = av_image_fill_linesizes(dstStride, dstFormat, w);
  177. if (res < 0) {
  178. fprintf(stderr, "av_image_fill_linesizes failed\n");
  179. goto end;
  180. }
  181. for (p = 0; p < 4; p++) {
  182. dstStride[p] = FFALIGN(dstStride[p], 16);
  183. if (dstStride[p]) {
  184. dst[p] = av_mallocz(dstStride[p] * h + 16);
  185. }
  186. if (dstStride[p] && !dst[p]) {
  187. goto end;
  188. }
  189. }
  190. // srcFormat -> dstFormat
  191. sws = sws_getContext(w, h, inFormat, w, h,
  192. dstFormat, SWS_BILINEAR, NULL, NULL, NULL);
  193. if (!sws) {
  194. fprintf(stderr, "Failed to get %s -> %s\n", av_get_pix_fmt_name(inFormat), av_get_pix_fmt_name(dstFormat) );
  195. goto end;
  196. }
  197. res = sws_scale(sws, (const uint8_t *const *)rgbIn, rgbStride, 0, h, dst, dstStride);
  198. if (res < 0 || res != h) {
  199. fprintf(stderr, "sws_scale failed\n");
  200. res = -1;
  201. goto end;
  202. }
  203. sws_freeContext(sws);
  204. // dstFormat -> srcFormat
  205. sws = sws_getContext(w, h, dstFormat, w, h,
  206. inFormat, SWS_BILINEAR, NULL, NULL, NULL);
  207. if(!sws) {
  208. fprintf(stderr, "Failed to get %s -> %s\n", av_get_pix_fmt_name(dstFormat), av_get_pix_fmt_name(inFormat) );
  209. goto end;
  210. }
  211. res = sws_scale(sws, (const uint8_t *const *)dst, dstStride, 0, h, rgbOut, rgbStride);
  212. if (res < 0 || res != h) {
  213. fprintf(stderr, "sws_scale failed\n");
  214. res = -1;
  215. goto end;
  216. }
  217. sws_freeContext(sws);
  218. sws = NULL;
  219. minimum = FLT_MAX;
  220. maximum = -FLT_MAX;
  221. count = 0;
  222. sum = 0.0;
  223. for (p = 0; p < 4; p++) {
  224. if (!rgbStride[p])
  225. continue;
  226. for (y = 0; y < h; y++) {
  227. in = (uint32_t*)(rgbIn[p] + y * rgbStride[p]);
  228. out = (uint32_t*)(rgbOut[p] + y * rgbStride[p]);
  229. for (x = 0; x < w; x++) {
  230. if (desc->flags & AV_PIX_FMT_FLAG_BE) {
  231. v0.i = AV_RB32(in);
  232. v1.i = AV_RB32(out);
  233. } else {
  234. v0.i = AV_RL32(in);
  235. v1.i = AV_RL32(out);
  236. }
  237. diff = fabsf(v0.f - v1.f);
  238. sum += diff;
  239. minimum = FFMIN(minimum, diff);
  240. maximum = FFMAX(maximum, diff);
  241. count++;
  242. in++;
  243. out++;
  244. }
  245. }
  246. }
  247. fprintf(stdout, "%s -> %s -> %s\n", av_get_pix_fmt_name(inFormat), av_get_pix_fmt_name(dstFormat), av_get_pix_fmt_name(inFormat) );
  248. fprintf(stdout, "avg diff: %f\nmin diff: %f\nmax diff: %f\n", sum / count, minimum, maximum);
  249. res = 0;
  250. }
  251. end:
  252. sws_freeContext(sws);
  253. for (p = 0; p < 4; p++) {
  254. av_freep(&rgbIn[p]);
  255. av_freep(&rgbOut[p]);
  256. av_freep(&dst[p]);
  257. }
  258. if (fp)
  259. fclose(fp);
  260. return res;
  261. }