pixfmt_best.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "libavutil/pixdesc.c"
  19. static const enum AVPixelFormat pixfmt_list[] = {
  20. AV_PIX_FMT_MONOWHITE,
  21. AV_PIX_FMT_GRAY8,
  22. AV_PIX_FMT_GRAY10,
  23. AV_PIX_FMT_GRAY16,
  24. AV_PIX_FMT_YUV420P,
  25. AV_PIX_FMT_YUV420P10,
  26. AV_PIX_FMT_YUV420P16,
  27. AV_PIX_FMT_YUV422P,
  28. AV_PIX_FMT_YUV422P10,
  29. AV_PIX_FMT_YUV422P16,
  30. AV_PIX_FMT_YUV444P,
  31. AV_PIX_FMT_YUV444P10,
  32. AV_PIX_FMT_YUV444P16,
  33. AV_PIX_FMT_RGB565,
  34. AV_PIX_FMT_RGB24,
  35. AV_PIX_FMT_RGB48,
  36. AV_PIX_FMT_VDPAU,
  37. AV_PIX_FMT_VAAPI,
  38. };
  39. static enum AVPixelFormat find_best(enum AVPixelFormat pixfmt)
  40. {
  41. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  42. int i;
  43. for (i = 0; i < FF_ARRAY_ELEMS(pixfmt_list); i++)
  44. best = av_find_best_pix_fmt_of_2(best, pixfmt_list[i],
  45. pixfmt, 0, NULL);
  46. return best;
  47. }
  48. int main(void)
  49. {
  50. enum AVPixelFormat output;
  51. int i, pass = 0, fail = 0;
  52. #define TEST(input, expected) do { \
  53. output = find_best(input); \
  54. if (output != expected) { \
  55. printf("Matching %s: got %s, expected %s\n", \
  56. av_get_pix_fmt_name(input), \
  57. av_get_pix_fmt_name(output), \
  58. av_get_pix_fmt_name(expected)); \
  59. ++fail; \
  60. } else \
  61. ++pass; \
  62. } while (0)
  63. // Same formats.
  64. for (i = 0; i < FF_ARRAY_ELEMS(pixfmt_list); i++)
  65. TEST(pixfmt_list[i], pixfmt_list[i]);
  66. // Formats containing the same data in different layouts.
  67. TEST(AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_MONOWHITE);
  68. TEST(AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P);
  69. TEST(AV_PIX_FMT_P010, AV_PIX_FMT_YUV420P10);
  70. TEST(AV_PIX_FMT_P016, AV_PIX_FMT_YUV420P16);
  71. TEST(AV_PIX_FMT_NV16, AV_PIX_FMT_YUV422P);
  72. TEST(AV_PIX_FMT_NV24, AV_PIX_FMT_YUV444P);
  73. TEST(AV_PIX_FMT_YUYV422, AV_PIX_FMT_YUV422P);
  74. TEST(AV_PIX_FMT_UYVY422, AV_PIX_FMT_YUV422P);
  75. TEST(AV_PIX_FMT_BGR565, AV_PIX_FMT_RGB565);
  76. TEST(AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24);
  77. TEST(AV_PIX_FMT_GBRP, AV_PIX_FMT_RGB24);
  78. TEST(AV_PIX_FMT_0RGB, AV_PIX_FMT_RGB24);
  79. TEST(AV_PIX_FMT_GBRP16, AV_PIX_FMT_RGB48);
  80. // Formats additionally containing alpha (here ignored).
  81. TEST(AV_PIX_FMT_YA8, AV_PIX_FMT_GRAY8);
  82. TEST(AV_PIX_FMT_YA16, AV_PIX_FMT_GRAY16);
  83. TEST(AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P);
  84. TEST(AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P);
  85. TEST(AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P);
  86. TEST(AV_PIX_FMT_AYUV64, AV_PIX_FMT_YUV444P16);
  87. TEST(AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB24);
  88. TEST(AV_PIX_FMT_ABGR, AV_PIX_FMT_RGB24);
  89. TEST(AV_PIX_FMT_GBRAP, AV_PIX_FMT_RGB24);
  90. TEST(AV_PIX_FMT_RGBA64, AV_PIX_FMT_RGB48);
  91. TEST(AV_PIX_FMT_BGRA64, AV_PIX_FMT_RGB48);
  92. TEST(AV_PIX_FMT_GBRAP16, AV_PIX_FMT_RGB48);
  93. // Formats requiring upsampling to represent exactly.
  94. TEST(AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16);
  95. TEST(AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV420P);
  96. TEST(AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P);
  97. TEST(AV_PIX_FMT_UYYVYY411, AV_PIX_FMT_YUV422P);
  98. TEST(AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P);
  99. TEST(AV_PIX_FMT_YUV440P10, AV_PIX_FMT_YUV444P10);
  100. TEST(AV_PIX_FMT_YUV440P12, AV_PIX_FMT_YUV444P16);
  101. TEST(AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10);
  102. TEST(AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV420P16);
  103. TEST(AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10);
  104. TEST(AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P16);
  105. TEST(AV_PIX_FMT_BGR4, AV_PIX_FMT_RGB565);
  106. TEST(AV_PIX_FMT_RGB444, AV_PIX_FMT_RGB565);
  107. TEST(AV_PIX_FMT_RGB555, AV_PIX_FMT_RGB565);
  108. TEST(AV_PIX_FMT_GBRP10, AV_PIX_FMT_RGB48);
  109. TEST(AV_PIX_FMT_GBRAP10, AV_PIX_FMT_RGB48);
  110. TEST(AV_PIX_FMT_GBRAP12, AV_PIX_FMT_RGB48);
  111. // Formats containing the same data in different endianness.
  112. TEST(AV_PIX_FMT_GRAY10BE, AV_PIX_FMT_GRAY10);
  113. TEST(AV_PIX_FMT_GRAY10LE, AV_PIX_FMT_GRAY10);
  114. TEST(AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16);
  115. TEST(AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16);
  116. TEST(AV_PIX_FMT_YUV422P10BE, AV_PIX_FMT_YUV422P10);
  117. TEST(AV_PIX_FMT_YUV422P10LE, AV_PIX_FMT_YUV422P10);
  118. TEST(AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUV444P16);
  119. TEST(AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUV444P16);
  120. TEST(AV_PIX_FMT_RGB565BE, AV_PIX_FMT_RGB565);
  121. TEST(AV_PIX_FMT_RGB565LE, AV_PIX_FMT_RGB565);
  122. TEST(AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGB48);
  123. TEST(AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48);
  124. // Opaque formats are least unlike each other.
  125. TEST(AV_PIX_FMT_DXVA2_VLD, AV_PIX_FMT_VDPAU);
  126. printf("%d tests passed, %d tests failed.\n", pass, fail);
  127. return !!fail;
  128. }