ippasync.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Copyright (C) 2015, Itseez Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification,
  20. // are permitted provided that the following conditions are met:
  21. //
  22. // * Redistribution's of source code must retain the above copyright notice,
  23. // this list of conditions and the following disclaimer.
  24. //
  25. // * Redistribution's in binary form must reproduce the above copyright notice,
  26. // this list of conditions and the following disclaimer in the documentation
  27. // and/or other materials provided with the distribution.
  28. //
  29. // * The name of the copyright holders may not be used to endorse or promote products
  30. // derived from this software without specific prior written permission.
  31. //
  32. // This software is provided by the copyright holders and contributors "as is" and
  33. // any express or implied warranties, including, but not limited to, the implied
  34. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  35. // In no event shall the Intel Corporation or contributors be liable for any direct,
  36. // indirect, incidental, special, exemplary, or consequential damages
  37. // (including, but not limited to, procurement of substitute goods or services;
  38. // loss of use, data, or profits; or business interruption) however caused
  39. // and on any theory of liability, whether in contract, strict liability,
  40. // or tort (including negligence or otherwise) arising in any way out of
  41. // the use of this software, even if advised of the possibility of such damage.
  42. //
  43. //M*/
  44. #ifndef OPENCV_CORE_IPPASYNC_HPP
  45. #define OPENCV_CORE_IPPASYNC_HPP
  46. #ifdef HAVE_IPP_A
  47. #include "opencv2/core.hpp"
  48. #include <ipp_async_op.h>
  49. #include <ipp_async_accel.h>
  50. namespace cv
  51. {
  52. namespace hpp
  53. {
  54. /** @addtogroup core_ipp
  55. This section describes conversion between OpenCV and [Intel&reg; IPP Asynchronous
  56. C/C++](http://software.intel.com/en-us/intel-ipp-preview) library. [Getting Started
  57. Guide](http://registrationcenter.intel.com/irc_nas/3727/ipp_async_get_started.htm) help you to
  58. install the library, configure header and library build paths.
  59. */
  60. //! @{
  61. //! convert OpenCV data type to hppDataType
  62. inline int toHppType(const int cvType)
  63. {
  64. int depth = CV_MAT_DEPTH(cvType);
  65. int hppType = depth == CV_8U ? HPP_DATA_TYPE_8U :
  66. depth == CV_16U ? HPP_DATA_TYPE_16U :
  67. depth == CV_16S ? HPP_DATA_TYPE_16S :
  68. depth == CV_32S ? HPP_DATA_TYPE_32S :
  69. depth == CV_32F ? HPP_DATA_TYPE_32F :
  70. depth == CV_64F ? HPP_DATA_TYPE_64F : -1;
  71. CV_Assert( hppType >= 0 );
  72. return hppType;
  73. }
  74. //! convert hppDataType to OpenCV data type
  75. inline int toCvType(const int hppType)
  76. {
  77. int cvType = hppType == HPP_DATA_TYPE_8U ? CV_8U :
  78. hppType == HPP_DATA_TYPE_16U ? CV_16U :
  79. hppType == HPP_DATA_TYPE_16S ? CV_16S :
  80. hppType == HPP_DATA_TYPE_32S ? CV_32S :
  81. hppType == HPP_DATA_TYPE_32F ? CV_32F :
  82. hppType == HPP_DATA_TYPE_64F ? CV_64F : -1;
  83. CV_Assert( cvType >= 0 );
  84. return cvType;
  85. }
  86. /** @brief Convert hppiMatrix to Mat.
  87. This function allocates and initializes new matrix (if needed) that has the same size and type as
  88. input matrix. Supports CV_8U, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
  89. @param src input hppiMatrix.
  90. @param dst output matrix.
  91. @param accel accelerator instance (see hpp::getHpp for the list of acceleration framework types).
  92. @param cn number of channels.
  93. */
  94. inline void copyHppToMat(hppiMatrix* src, Mat& dst, hppAccel accel, int cn)
  95. {
  96. hppDataType type;
  97. hpp32u width, height;
  98. hppStatus sts;
  99. if (src == NULL)
  100. return dst.release();
  101. sts = hppiInquireMatrix(src, &type, &width, &height);
  102. CV_Assert( sts == HPP_STATUS_NO_ERROR);
  103. int matType = CV_MAKETYPE(toCvType(type), cn);
  104. CV_Assert(width%cn == 0);
  105. width /= cn;
  106. dst.create((int)height, (int)width, (int)matType);
  107. size_t newSize = (size_t)(height*(hpp32u)(dst.step));
  108. sts = hppiGetMatrixData(accel,src,(hpp32u)(dst.step),dst.data,&newSize);
  109. CV_Assert( sts == HPP_STATUS_NO_ERROR);
  110. }
  111. /** @brief Create Mat from hppiMatrix.
  112. This function allocates and initializes the Mat that has the same size and type as input matrix.
  113. Supports CV_8U, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
  114. @param src input hppiMatrix.
  115. @param accel accelerator instance (see hpp::getHpp for the list of acceleration framework types).
  116. @param cn number of channels.
  117. @sa howToUseIPPAconversion, hpp::copyHppToMat, hpp::getHpp.
  118. */
  119. inline Mat getMat(hppiMatrix* src, hppAccel accel, int cn)
  120. {
  121. Mat dst;
  122. copyHppToMat(src, dst, accel, cn);
  123. return dst;
  124. }
  125. /** @brief Create hppiMatrix from Mat.
  126. This function allocates and initializes the hppiMatrix that has the same size and type as input
  127. matrix, returns the hppiMatrix*.
  128. If you want to use zero-copy for GPU you should to have 4KB aligned matrix data. See details
  129. [hppiCreateSharedMatrix](http://software.intel.com/ru-ru/node/501697).
  130. Supports CV_8U, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
  131. @note The hppiMatrix pointer to the image buffer in system memory refers to the src.data. Control
  132. the lifetime of the matrix and don't change its data, if there is no special need.
  133. @param src input matrix.
  134. @param accel accelerator instance. Supports type:
  135. - **HPP_ACCEL_TYPE_CPU** - accelerated by optimized CPU instructions.
  136. - **HPP_ACCEL_TYPE_GPU** - accelerated by GPU programmable units or fixed-function
  137. accelerators.
  138. - **HPP_ACCEL_TYPE_ANY** - any acceleration or no acceleration available.
  139. @sa howToUseIPPAconversion, hpp::getMat
  140. */
  141. inline hppiMatrix* getHpp(const Mat& src, hppAccel accel)
  142. {
  143. int htype = toHppType(src.type());
  144. int cn = src.channels();
  145. CV_Assert(src.data);
  146. hppAccelType accelType = hppQueryAccelType(accel);
  147. if (accelType!=HPP_ACCEL_TYPE_CPU)
  148. {
  149. hpp32u pitch, size;
  150. hppQueryMatrixAllocParams(accel, src.cols*cn, src.rows, htype, &pitch, &size);
  151. if (pitch!=0 && size!=0)
  152. if ((int)(src.data)%4096==0 && pitch==(hpp32u)(src.step))
  153. {
  154. return hppiCreateSharedMatrix(htype, src.cols*cn, src.rows, src.data, pitch, size);
  155. }
  156. }
  157. return hppiCreateMatrix(htype, src.cols*cn, src.rows, src.data, (hpp32s)(src.step));;
  158. }
  159. //! @}
  160. }}
  161. #endif
  162. #endif