1 /*******************************************************************************
2 * Copyright 2016-2019 Intel Corporation.
3 *
4 * This software and the related documents are Intel copyrighted  materials,  and
5 * your use of  them is  governed by the  express license  under which  they were
6 * provided to you (License).  Unless the License provides otherwise, you may not
7 * use, modify, copy, publish, distribute,  disclose or transmit this software or
8 * the related documents without Intel's prior written permission.
9 *
10 * This software and the related documents  are provided as  is,  with no express
11 * or implied  warranties,  other  than those  that are  expressly stated  in the
12 * License.
13 *******************************************************************************/
14 
15 #if !defined( __IPP_IW_OWN__ )
16 #define __IPP_IW_OWN__
17 
18 #include <string.h>
19 
20 #include "iw_config.h"
21 #include "iw/iw_core.h"
22 #include "iw/iw_image.h"
23 
24 #ifdef ICV_BASE
25 #include "ippicv.h"
26 #else
27 #include "ipp.h"
28 #if IW_ENABLE_THREADING_LAYER
29 #include "ippcore_tl.h"
30 #include "ippi_tl.h"
31 #endif
32 #endif
33 
34 #ifdef _MSC_VER
35 #pragma warning (disable:4505) /* Unreferenced local function has been removed */
36 #endif
37 
38 #ifndef IW_BUILD
39 #error this is a private header
40 #endif
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* /////////////////////////////////////////////////////////////////////////////
47 //                   Base IW internal definitions
48 ///////////////////////////////////////////////////////////////////////////// */
49 void* IPP_STDCALL ownAlignedMalloc(size_t iSize, size_t iAlign);
50 void  IPP_STDCALL ownAlignedFree(void* pBuffer);
51 
52 #define OWN_IDX_INVALID   0xFFFFFFFF
53 
54 #define OWN_IS_EXCEED(LEN, TYPE) ((LEN) > (TYPE)(LEN))
55 #define OWN_IS_EXCEED_INT(LEN) OWN_IS_EXCEED((LEN), Ipp32s)
56 
57 #define OWN_MEM_ALLOC(SIZE) ippMalloc_L(SIZE)
58 #define OWN_MEM_RESET(SPEC) ippsZero_8u((Ipp8u*)(SPEC), sizeof(*(SPEC)))
59 #define OWN_MEM_FREE(SPEC)  ippFree(SPEC)
60 
61 // Static space safe memory operations
62 #define OWN_SAFE_MALLOC(SIZE)         ownAlignedMalloc((size_t)(SIZE), 64);
63 #define OWN_SAFE_RESET(PTR, SIZE)     memset((void*)(PTR), 0, (size_t)(SIZE));
64 #define OWN_SAFE_FREE(PTR)            ownAlignedFree((void*)(PTR));
65 #define OWN_SAFE_COPY(SRC, DST, SIZE) memcpy((void*)(DST), (void*)(SRC), (size_t)(SIZE));
66 
67 #define OWN_STATUS_OK    0
68 #define OWN_STATUS_FAIL -1
69 
70 #define OWN_INIT_MAGIC_NUM 0x8117e881
71 
72 // Additional build definitions for data type groups
73 #if defined IW_ENABLE_DATA_TYPE_8U || defined IW_ENABLE_DATA_TYPE_8S
74 #define IW_ENABLE_DATA_DEPTH_8  1
75 #endif
76 #if defined IW_ENABLE_DATA_TYPE_16U || defined IW_ENABLE_DATA_TYPE_16S
77 #define IW_ENABLE_DATA_DEPTH_16 1
78 #endif
79 #if defined IW_ENABLE_DATA_TYPE_32U || defined IW_ENABLE_DATA_TYPE_32S || defined IW_ENABLE_DATA_TYPE_32F
80 #define IW_ENABLE_DATA_DEPTH_32 1
81 #endif
82 #if defined IW_ENABLE_DATA_TYPE_64U || defined IW_ENABLE_DATA_TYPE_64S || defined IW_ENABLE_DATA_TYPE_64F
83 #define IW_ENABLE_DATA_DEPTH_64 1
84 #endif
85 
86 /* /////////////////////////////////////////////////////////////////////////////
87 //                   Utility functions
88 ///////////////////////////////////////////////////////////////////////////// */
89 #define OWN_ROUND_FUN(SRC, DST, LEN) ippsRound_64f(SRC, DST, (int)LEN)
90 
91 #define OWN_ARRAY_SATURATED_CAST(TYPE, NAME)                                                                    \
92 {                                                                                                               \
93     IwSize i;                                                                                                   \
94     OWN_ROUND_FUN(pVal, pBuffer, len);                                                                          \
95     for(i = 0; i < len; i++)                                                                                    \
96     {                                                                                                           \
97         if(pVal[i] > IPP_MIN_##NAME)                                                                            \
98         {                                                                                                       \
99             if(pVal[i] < IPP_MAX_##NAME)                                                                        \
100                 ((TYPE*)pBuffer)[i] = (TYPE)(pBuffer[i]);                                                       \
101             else                                                                                                \
102                 ((TYPE*)pBuffer)[i] = (TYPE)IPP_MAX_##NAME;                                                     \
103         }                                                                                                       \
104         else                                                                                                    \
105             ((TYPE*)pBuffer)[i] = (TYPE)IPP_MIN_##NAME;                                                         \
106     }                                                                                                           \
107 }
108 #define OWN_SATURATED_CAST(TYPE, NAME)                                                                          \
109 {                                                                                                               \
110     OWN_ROUND_FUN(&val, &val, 1);                                                                               \
111     if(val > IPP_MIN_##NAME)                                                                                    \
112     {                                                                                                           \
113         if(val < IPP_MAX_##NAME)                                                                                \
114             val = (TYPE)(val);                                                                                  \
115         else                                                                                                    \
116             val = (TYPE)IPP_MAX_##NAME;                                                                         \
117     }                                                                                                           \
118     else                                                                                                        \
119         val = (TYPE)IPP_MIN_##NAME;                                                                             \
120 }
ownCastArray_64f8u(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)121 static IW_INLINE Ipp8u* ownCastArray_64f8u(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
122 {
123     OWN_ARRAY_SATURATED_CAST(Ipp8u, 8U)
124     return (Ipp8u*)pBuffer;
125 }
ownCastArray_64f8s(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)126 static IW_INLINE Ipp8s* ownCastArray_64f8s(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
127 {
128     OWN_ARRAY_SATURATED_CAST(Ipp8s, 8S)
129     return (Ipp8s*)pBuffer;
130 }
ownCastArray_64f16u(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)131 static IW_INLINE Ipp16u* ownCastArray_64f16u(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
132 {
133     OWN_ARRAY_SATURATED_CAST(Ipp16u, 16U)
134     return (Ipp16u*)pBuffer;
135 }
ownCastArray_64f16s(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)136 static IW_INLINE Ipp16s* ownCastArray_64f16s(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
137 {
138     OWN_ARRAY_SATURATED_CAST(Ipp16s, 16S)
139     return (Ipp16s*)pBuffer;
140 }
ownCastArray_64f32u(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)141 static IW_INLINE Ipp32u* ownCastArray_64f32u(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
142 {
143     OWN_ARRAY_SATURATED_CAST(Ipp32u, 32U)
144     return (Ipp32u*)pBuffer;
145 }
ownCastArray_64f32s(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)146 static IW_INLINE Ipp32s* ownCastArray_64f32s(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
147 {
148     OWN_ARRAY_SATURATED_CAST(Ipp32s, 32S)
149     return (Ipp32s*)pBuffer;
150 }
ownCastArray_64f32f(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)151 static IW_INLINE Ipp32f* ownCastArray_64f32f(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
152 {
153     ippsConvert_64f32f(pVal, (Ipp32f*)pBuffer, (int)len);
154     return (Ipp32f*)pBuffer;
155 }
ownCastArray_64f64f(const Ipp64f * pVal,Ipp64f * pBuffer,IwSize len)156 static IW_INLINE Ipp64f* ownCastArray_64f64f(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len)
157 {
158     ippsCopy_64f(pVal, pBuffer, (int)len);
159     return pBuffer;
160 }
ownCastArray_64f(const Ipp64f * pVal,Ipp64f * pBuffer,IppDataType dataType,IwSize len)161 static void ownCastArray_64f(const Ipp64f *pVal, Ipp64f *pBuffer, IppDataType dataType, IwSize len)
162 {
163     switch(dataType)
164     {
165     case ipp8u:  ownCastArray_64f8u (pVal, pBuffer, len);    return;
166     case ipp8s:  ownCastArray_64f8s (pVal, pBuffer, len);    return;
167     case ipp16u: ownCastArray_64f16u(pVal, pBuffer, len);    return;
168     case ipp16s: ownCastArray_64f16s(pVal, pBuffer, len);    return;
169     case ipp32u: ownCastArray_64f32u(pVal, pBuffer, len);    return;
170     case ipp32s: ownCastArray_64f32s(pVal, pBuffer, len);    return;
171     case ipp32f: ownCastArray_64f32f(pVal, pBuffer, len);    return;
172     case ipp64f: ownCastArray_64f64f(pVal, pBuffer, len);    return;
173     default: return;
174     }
175 }
176 
ownCast_64f8u(Ipp64f val)177 static IW_INLINE Ipp8u ownCast_64f8u(Ipp64f val)
178 {
179     OWN_SATURATED_CAST(Ipp8u, 8U)
180     return (Ipp8u)val;
181 }
ownCast_64f8s(Ipp64f val)182 static IW_INLINE Ipp8s ownCast_64f8s(Ipp64f val)
183 {
184     OWN_SATURATED_CAST(Ipp8s, 8S)
185     return (Ipp8s)val;
186 }
ownCast_64f16u(Ipp64f val)187 static IW_INLINE Ipp16u ownCast_64f16u(Ipp64f val)
188 {
189     OWN_SATURATED_CAST(Ipp16u, 16U)
190     return (Ipp16u)val;
191 }
ownCast_64f16s(Ipp64f val)192 static IW_INLINE Ipp16s ownCast_64f16s(Ipp64f val)
193 {
194     OWN_SATURATED_CAST(Ipp16s, 16S)
195     return (Ipp16s)val;
196 }
ownCast_64f32u(Ipp64f val)197 static IW_INLINE Ipp32u ownCast_64f32u(Ipp64f val)
198 {
199     OWN_SATURATED_CAST(Ipp32u, 32U)
200     return (Ipp32u)val;
201 }
ownCast_64f32s(Ipp64f val)202 static IW_INLINE Ipp32s ownCast_64f32s(Ipp64f val)
203 {
204     OWN_SATURATED_CAST(Ipp32s, 32S)
205     return (Ipp32s)val;
206 }
ownCast_64f32f(Ipp64f val)207 static IW_INLINE Ipp32f ownCast_64f32f(Ipp64f val)
208 {
209     if(val > IPP_MAXABS_32F)       val = IPP_MAXABS_32F;
210     else if(val < -IPP_MAXABS_32F) val = -IPP_MAXABS_32F;
211     return (Ipp32f)val;
212 }
213 
214 typedef void* (*OwnCastArray_ptr)(const Ipp64f *pVal, Ipp64f *pBuffer, IwSize len);
215 
216 /* /////////////////////////////////////////////////////////////////////////////
217 //                   Long types compatibility checkers
218 ///////////////////////////////////////////////////////////////////////////// */
ownLongCompatCheckValue(IwSize val,int * pVal)219 static IW_INLINE IppStatus ownLongCompatCheckValue(IwSize val, int *pVal)
220 {
221 #if defined (_M_AMD64) || defined (__x86_64__)
222     if(OWN_IS_EXCEED_INT(val))
223         return ippStsSizeErr;
224     else
225 #endif
226     if(pVal)
227         *pVal = (int)val;
228     return ippStsNoErr;
229 }
230 
231 /* /////////////////////////////////////////////////////////////////////////////
232 //                   OwnVector - C Vector
233 ///////////////////////////////////////////////////////////////////////////// */
234 
235 // Simple C vector interface
236 typedef struct _OwnVector
237 {
238     Ipp8u  *m_pBuffer;
239     size_t  m_bufferLen;
240     size_t  m_elemSize;
241     size_t  m_size;
242 } OwnVector;
243 
244 IW_DECL(void) ownVector_Reserve(OwnVector *pVector, size_t reserveSize);
245 IW_DECL(void) ownVector_Init(OwnVector *pVector, size_t elemSize, size_t reserve);
246 IW_DECL(void) ownVector_Release(OwnVector *pVector);
247 IW_DECL(void) ownVector_Resize(OwnVector *pVector, size_t newSize);
248 IW_DECL(void) ownVector_PushBack(OwnVector *pVector, void *pData);
249 IW_DECL(void) ownVector_PopBack(OwnVector *pVector, void *pData);
250 
251 /* /////////////////////////////////////////////////////////////////////////////
252 //                   Shared memory interface for temporary buffers
253 ///////////////////////////////////////////////////////////////////////////// */
254 void* IPP_STDCALL ownSharedMalloc(IwSize size);
255 void  IPP_STDCALL ownSharedFree(void* pBuffer);
256 
257 /* /////////////////////////////////////////////////////////////////////////////
258 //                   OWN ROI manipulation
259 ///////////////////////////////////////////////////////////////////////////// */
260 typedef enum _OwnTileInitType
261 {
262     ownTileInitNone   = 0,
263     ownTileInitSimple = 0xA1A2A3,
264     ownTileInitPipe   = 0xB1B2B3
265 } OwnRoiInitType;
266 
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif
272