va_intel_interop.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* origin: libva-1.3.1/test/decode/mpeg2vldemo.cpp */
  2. /*
  3. * Copyright (c) 2007-2008 Intel Corporation. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  20. * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
  21. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <iostream>
  26. #include <stdexcept>
  27. #include <string>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <getopt.h>
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <assert.h>
  37. #include <va/va.h>
  38. #include "display.cpp.inc"
  39. #include "opencv2/core.hpp"
  40. #include "opencv2/imgproc.hpp"
  41. #include "opencv2/highgui.hpp"
  42. #include "opencv2/core/va_intel.hpp"
  43. #define CHECK_VASTATUS(_status,_func) \
  44. if (_status != VA_STATUS_SUCCESS) \
  45. { \
  46. char str[256]; \
  47. snprintf(str, sizeof(str)-1, "%s:%s (%d) failed(status=0x%08x),exit\n", __func__, _func, __LINE__, _status); \
  48. throw std::runtime_error(str); \
  49. }
  50. class CmdlineParser
  51. {
  52. public:
  53. enum { fnInput=0, fnOutput1, fnOutput2, _fnNumFiles }; // file name indices
  54. CmdlineParser(int argc, char** argv):
  55. m_argc(argc), m_argv(argv)
  56. {}
  57. void usage()
  58. {
  59. fprintf(stderr,
  60. "Usage: va_intel_interop [-f] infile outfile1 outfile2\n\n"
  61. "Interop ON/OFF version\n\n"
  62. "where: -f option indicates interop is off (fallback mode); interop is on by default\n"
  63. " infile is to be existing, contains input image data (bmp, jpg, png, tiff, etc)\n"
  64. " outfile1 is to be created, contains original surface data (NV12)\n"
  65. " outfile2 is to be created, contains processed surface data (NV12)\n");
  66. }
  67. // true => go, false => usage/exit; extra args/unknown options are ignored for simplicity
  68. bool run()
  69. {
  70. int n = 0;
  71. for (int i = 0; i < _fnNumFiles; ++i)
  72. m_files[i] = 0;
  73. m_interop = true;
  74. for (int i = 1; i < m_argc; ++i)
  75. {
  76. const char *arg = m_argv[i];
  77. if (arg[0] == '-') // option
  78. {
  79. if (!strcmp(arg, "-f"))
  80. m_interop = false;
  81. }
  82. else // parameter
  83. {
  84. if (n < _fnNumFiles)
  85. m_files[n++] = arg;
  86. }
  87. }
  88. return bool(n >= _fnNumFiles);
  89. }
  90. bool isInterop() const
  91. {
  92. return m_interop;
  93. }
  94. const char* getFile(int n) const
  95. {
  96. return ((n >= 0) && (n < _fnNumFiles)) ? m_files[n] : 0;
  97. }
  98. private:
  99. int m_argc;
  100. char** m_argv;
  101. const char* m_files[_fnNumFiles];
  102. bool m_interop;
  103. };
  104. class Timer
  105. {
  106. public:
  107. enum UNITS
  108. {
  109. USEC = 0,
  110. MSEC,
  111. SEC
  112. };
  113. Timer() : m_t0(0), m_diff(0)
  114. {
  115. m_tick_frequency = (float)cv::getTickFrequency();
  116. m_unit_mul[USEC] = 1000000;
  117. m_unit_mul[MSEC] = 1000;
  118. m_unit_mul[SEC] = 1;
  119. }
  120. void clear()
  121. {
  122. m_t0 = m_diff = 0;
  123. }
  124. void start()
  125. {
  126. m_t0 = cv::getTickCount();
  127. }
  128. void stop()
  129. {
  130. m_diff = cv::getTickCount() - m_t0;
  131. }
  132. float time(UNITS u = MSEC)
  133. {
  134. float sec = m_diff / m_tick_frequency;
  135. return sec * m_unit_mul[u];
  136. }
  137. public:
  138. float m_tick_frequency;
  139. int64 m_t0;
  140. int64 m_diff;
  141. int m_unit_mul[3];
  142. };
  143. static void checkIfAvailableYUV420()
  144. {
  145. VAEntrypoint entrypoints[5];
  146. int num_entrypoints,vld_entrypoint;
  147. VAConfigAttrib attrib;
  148. VAStatus status;
  149. status = vaQueryConfigEntrypoints(va::display, VAProfileMPEG2Main, entrypoints, &num_entrypoints);
  150. CHECK_VASTATUS(status, "vaQueryConfigEntrypoints");
  151. for (vld_entrypoint = 0; vld_entrypoint < num_entrypoints; ++vld_entrypoint)
  152. {
  153. if (entrypoints[vld_entrypoint] == VAEntrypointVLD)
  154. break;
  155. }
  156. if (vld_entrypoint == num_entrypoints)
  157. throw std::runtime_error("Failed to find VLD entry point");
  158. attrib.type = VAConfigAttribRTFormat;
  159. vaGetConfigAttributes(va::display, VAProfileMPEG2Main, VAEntrypointVLD, &attrib, 1);
  160. if ((attrib.value & VA_RT_FORMAT_YUV420) == 0)
  161. throw std::runtime_error("Desired YUV420 RT format not found");
  162. }
  163. static cv::UMat readImage(const char* fileName)
  164. {
  165. cv::Mat m = cv::imread(fileName);
  166. if (m.empty())
  167. throw std::runtime_error("Failed to load image: " + std::string(fileName));
  168. return m.getUMat(cv::ACCESS_RW);
  169. }
  170. static void writeImage(const cv::UMat& u, const char* fileName, bool doInterop)
  171. {
  172. std::string fn = std::string(fileName) + std::string(doInterop ? ".on" : ".off") + std::string(".jpg");
  173. cv::imwrite(fn, u);
  174. }
  175. static float run(const char* infile, const char* outfile1, const char* outfile2, bool doInterop)
  176. {
  177. VASurfaceID surface;
  178. VAStatus status;
  179. Timer t;
  180. // initialize CL context for CL/VA interop
  181. cv::va_intel::ocl::initializeContextFromVA(va::display, doInterop);
  182. // load input image
  183. cv::UMat u1 = readImage(infile);
  184. cv::Size size2 = u1.size();
  185. status = vaCreateSurfaces(va::display, VA_RT_FORMAT_YUV420, size2.width, size2.height, &surface, 1, NULL, 0);
  186. CHECK_VASTATUS(status, "vaCreateSurfaces");
  187. // transfer image into VA surface, make sure all CL initialization is done (kernels etc)
  188. cv::va_intel::convertToVASurface(va::display, u1, surface, size2);
  189. cv::va_intel::convertFromVASurface(va::display, surface, size2, u1);
  190. cv::UMat u2;
  191. cv::blur(u1, u2, cv::Size(7, 7), cv::Point(-3, -3));
  192. // measure performance on some image processing
  193. writeImage(u1, outfile1, doInterop);
  194. t.start();
  195. cv::va_intel::convertFromVASurface(va::display, surface, size2, u1);
  196. cv::blur(u1, u2, cv::Size(7, 7), cv::Point(-3, -3));
  197. cv::va_intel::convertToVASurface(va::display, u2, surface, size2);
  198. t.stop();
  199. writeImage(u2, outfile2, doInterop);
  200. vaDestroySurfaces(va::display, &surface,1);
  201. return t.time(Timer::MSEC);
  202. }
  203. int main(int argc, char** argv)
  204. {
  205. try
  206. {
  207. CmdlineParser cmd(argc, argv);
  208. if (!cmd.run())
  209. {
  210. cmd.usage();
  211. return 0;
  212. }
  213. if (!va::openDisplay())
  214. throw std::runtime_error("Failed to open VA display for CL-VA interoperability");
  215. std::cout << "VA display opened successfully" << std::endl;
  216. checkIfAvailableYUV420();
  217. const char* infile = cmd.getFile(CmdlineParser::fnInput);
  218. const char* outfile1 = cmd.getFile(CmdlineParser::fnOutput1);
  219. const char* outfile2 = cmd.getFile(CmdlineParser::fnOutput2);
  220. bool doInterop = cmd.isInterop();
  221. float time = run(infile, outfile1, outfile2, doInterop);
  222. std::cout << "Interop " << (doInterop ? "ON " : "OFF") << ": processing time, msec: " << time << std::endl;
  223. }
  224. catch (const std::exception& ex)
  225. {
  226. std::cerr << "ERROR: " << ex.what() << std::endl;
  227. }
  228. va::closeDisplay();
  229. return 0;
  230. }