VXLIB User Guide
VXLIB_median.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  ******************************************************************************/
33 
34 /**********************************************************************************************************************/
35 /* */
36 /* INCLUDES */
37 /* */
38 /**********************************************************************************************************************/
39 
40 #include "VXLIB_median_priv.h"
41 
42 /**********************************************************************************************************************/
43 /* */
44 /* VXLIB_median_getHandleSize */
45 /* */
46 /**********************************************************************************************************************/
47 
48 // this method calculates and returns the size of the handle for the VXLIB_median kernel
50 {
51  int32_t privBufSize = sizeof(VXLIB_median_PrivArgs);
52  return privBufSize;
53 }
54 
55 /**********************************************************************************************************************/
56 /* */
57 /* VXLIB_median_init_checkParams */
58 /* */
59 /**********************************************************************************************************************/
60 
61 // this method checks the initialization parameters for the VXLIB_median kernel
64  const VXLIB_bufParams2D_t *bufParamsIn,
65  const VXLIB_bufParams2D_t *bufParamsMask,
66  const VXLIB_bufParams2D_t *bufParamsOut,
67  const VXLIB_bufParams2D_t *bufParamsScratch,
68  const VXLIB_median_InitArgs *pKerInitArgs)
69 {
70  VXLIB_DEBUGPRINTFN(0, "%s\n", "Enter function");
71  VXLIB_STATUS status = VXLIB_SUCCESS;
72 
73  // check for dimensions and datatype combinations
74  if ((handle == NULL) || (bufParamsIn == NULL) || (bufParamsMask == NULL) || (bufParamsOut == NULL) ||
75  (bufParamsScratch == NULL) || (pKerInitArgs == NULL)) {
76  status = VXLIB_ERR_NULL_POINTER;
77  }
78 
79  if (status == VXLIB_SUCCESS) {
80  // obtain input buffer parameters
81  uint32_t dType = bufParamsIn->data_type;
82  uint32_t widthIn = bufParamsIn->dim_x;
83  uint32_t heightIn = bufParamsIn->dim_y;
84  uint32_t strideIn = bufParamsIn->stride_y;
85 
86  // obtain mask buffer parameters
87  uint32_t widthMask = bufParamsMask->dim_x;
88  uint32_t heightMask = bufParamsMask->dim_y;
89  uint32_t strideMask = bufParamsMask->stride_y;
90 
91  // obtain output buffer parameters
92  uint32_t widthOut = bufParamsOut->dim_x;
93  uint32_t heightOut = bufParamsOut->dim_y;
94  uint32_t strideOut = bufParamsOut->stride_y;
95 
96  uint32_t strideInElements = strideIn / VXLIB_sizeof(dType);
97  uint32_t strideMaskElements = strideMask / VXLIB_sizeof(dType);
98  uint32_t strideOutElements = strideOut / VXLIB_sizeof(dType);
99 
100  if (strideInElements < widthIn || strideMaskElements < widthMask || strideOutElements < widthOut) {
102  }
103  else if (!(VXLIB_MEDIAN_8U) && !(VXLIB_MEDIAN_8S) && !(VXLIB_MEDIAN_16U) && !(VXLIB_MEDIAN_16S)) {
104  status = VXLIB_ERR_INVALID_TYPE;
105  }
106  else if ((widthOut != (widthIn - (widthMask - 1u))) || (heightOut != (heightIn - (heightMask - 1u)))) {
108  }
109  else if (heightMask < 1u || widthMask < 1u) {
111  }
112  else if (heightMask > 9u || widthMask > 9u) {
114  }
115  else {
116  status = VXLIB_SUCCESS;
117  }
118  }
119 
120  VXLIB_DEBUGPRINTFN(0, "Exit with status %d\n", status);
121  return status;
122 }
123 
124 /**********************************************************************************************************************/
125 /* */
126 /* VXLIB_median_exec_checkParams */
127 /* */
128 /**********************************************************************************************************************/
129 
130 // this method checks the execution parameters for the VXLIB_median kernel
132  const void *restrict pIn,
133  const void *restrict pMask,
134  const void *restrict pOut,
135  const void *restrict pScratch)
136 {
137  VXLIB_STATUS status;
138 
139 #if VXLIB_DEBUGPRINT
140  printf("Enter VXLIB_median_exec_checkParams\n");
141 #endif
142  if ((handle == NULL) || (pIn == NULL) || (pMask == NULL) || (pOut == NULL) || (pScratch == NULL)) {
143  status = VXLIB_ERR_NULL_POINTER;
144  }
145  else {
146  status = VXLIB_SUCCESS;
147  }
148 
149  return status;
150 }
151 
152 /**********************************************************************************************************************/
153 /* */
154 /* VXLIB_median_init */
155 /* */
156 /**********************************************************************************************************************/
157 
158 // this method is the user-level initialization function for the VXLIB_median kernel
160  VXLIB_bufParams2D_t *bufParamsIn,
161  VXLIB_bufParams2D_t *bufParamsMask,
162  VXLIB_bufParams2D_t *bufParamsOut,
163  VXLIB_bufParams2D_t *bufParamsScratch,
164  const VXLIB_median_InitArgs *pKerInitArgs)
165 {
166  VXLIB_STATUS status = VXLIB_SUCCESS;
167  VXLIB_median_PrivArgs *pKerPrivArgs = (VXLIB_median_PrivArgs *) handle;
168 
169  // copy pKerinitArgs into pKerPrivargs
170  pKerPrivArgs->pKerInitArgs = *pKerInitArgs;
171 
172  // set width and height of image
173  pKerPrivArgs->width = bufParamsIn->dim_x;
174  pKerPrivArgs->height = bufParamsIn->dim_y;
175  pKerPrivArgs->M = bufParamsMask->dim_y;
176  pKerPrivArgs->N = bufParamsMask->dim_x;
177  pKerPrivArgs->widthOut = bufParamsOut->dim_x;
178  pKerPrivArgs->heightOut = bufParamsOut->dim_y;
179  pKerPrivArgs->widthScratch = bufParamsScratch->dim_x;
180  pKerPrivArgs->heightScratch = bufParamsScratch->dim_y;
181 
182  // compute stride in elements as SE/SA params are set to work with given element type
183  pKerPrivArgs->strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
184  pKerPrivArgs->strideMaskElements = bufParamsMask->stride_y / VXLIB_sizeof(bufParamsMask->data_type);
185  pKerPrivArgs->strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
186  pKerPrivArgs->strideScratchElements = bufParamsScratch->stride_y / VXLIB_sizeof(bufParamsScratch->data_type);
187 
188 #if VXLIB_DEBUGPRINT
189  printf("VXLIB_DEBUGPRINT Enter VXLIB_median_init\n");
190 #endif
191 
192  // obtain buffer datatypes
193  uint32_t dType = bufParamsIn->data_type;
194  // uint32_t dTypeMask = bufParamsMask->data_type;
195  // uint32_t dTypeOut = bufParamsOut->data_type;
196 
197  // determine natural C vs optimized function call
198  if (pKerInitArgs->funcStyle == VXLIB_FUNCTION_NATC) {
199 
200  // set function pointer for natural C function with appropriate template parameters based on datatypes
201  if (VXLIB_MEDIAN_8U) {
203  }
204  else if (VXLIB_MEDIAN_8S) {
206  }
207  else if (VXLIB_MEDIAN_16U) {
209  }
210  else if (VXLIB_MEDIAN_16S) {
212  }
213  else {
214  status = VXLIB_ERR_INVALID_TYPE;
215  }
216  }
217  else { // Optimized function
218 
219  // set function pointer for natural C function with appropriate template parameters based on datatypes
220  if (!(pKerPrivArgs->pKerInitArgs.kernelType)) {
221  if (VXLIB_MEDIAN_8U) {
223  status = VXLIB_median_3X3_init_ci<VXLIB_MEDIAN_TYPENAME_8U>(handle, bufParamsIn, bufParamsMask,
224  bufParamsOut, bufParamsScratch, pKerInitArgs);
225  }
226  else if (VXLIB_MEDIAN_8S) {
228  status = VXLIB_median_3X3_init_ci<VXLIB_MEDIAN_TYPENAME_8S>(handle, bufParamsIn, bufParamsMask,
229  bufParamsOut, bufParamsScratch, pKerInitArgs);
230  }
231  else if (VXLIB_MEDIAN_16U) {
233  status = VXLIB_median_3X3_init_ci<VXLIB_MEDIAN_TYPENAME_16U>(handle, bufParamsIn, bufParamsMask,
234  bufParamsOut, bufParamsScratch, pKerInitArgs);
235  }
236  else if (VXLIB_MEDIAN_16S) {
238  status = VXLIB_median_3X3_init_ci<VXLIB_MEDIAN_TYPENAME_16S>(handle, bufParamsIn, bufParamsMask,
239  bufParamsOut, bufParamsScratch, pKerInitArgs);
240  }
241  else {
242  status = VXLIB_ERR_INVALID_TYPE;
243  }
244  }
245  else {
246  if (VXLIB_MEDIAN_8U) {
248  status = VXLIB_median_MXN_init_ci<VXLIB_MEDIAN_TYPENAME_8U>(handle, bufParamsIn, bufParamsMask,
249  bufParamsOut, bufParamsScratch, pKerInitArgs);
250  }
251  else if (VXLIB_MEDIAN_8S) {
253  status = VXLIB_median_MXN_init_ci<VXLIB_MEDIAN_TYPENAME_8S>(handle, bufParamsIn, bufParamsMask,
254  bufParamsOut, bufParamsScratch, pKerInitArgs);
255  }
256  else if (VXLIB_MEDIAN_16U) {
258  status = VXLIB_median_MXN_init_ci<VXLIB_MEDIAN_TYPENAME_16U>(handle, bufParamsIn, bufParamsMask,
259  bufParamsOut, bufParamsScratch, pKerInitArgs);
260  }
261  else if (VXLIB_MEDIAN_16S) {
263  status = VXLIB_median_MXN_init_ci<VXLIB_MEDIAN_TYPENAME_16S>(handle, bufParamsIn, bufParamsMask,
264  bufParamsOut, bufParamsScratch, pKerInitArgs);
265  }
266  else {
267  status = VXLIB_ERR_INVALID_TYPE;
268  }
269  }
270  }
271 
272  return status;
273 }
274 
275 /**********************************************************************************************************************/
276 /* */
277 /* VXLIB_median_exec */
278 /* */
279 /**********************************************************************************************************************/
280 
281 // this method is the user-level execution function for the VXLIB_median kernel
283  void *restrict pIn,
284  void *restrict pMask,
285  void *restrict pOut,
286  void *restrict pScratch)
287 {
288  VXLIB_STATUS status;
289 
290 #if VXLIB_DEBUGPRINT
291  printf("VXLIB_DEBUGPRINT Enter VXLIB_median_exec\n");
292 #endif
293 
294  VXLIB_median_PrivArgs *pKerPrivArgs = (VXLIB_median_PrivArgs *) handle;
295 
296  status = pKerPrivArgs->execute(handle, pIn, pMask, pOut, pScratch);
297 
298  return status;
299 }
300 
301 /* ======================================================================== */
302 /* End of file: VXLIB_median.cpp */
303 /* ======================================================================== */
template VXLIB_STATUS VXLIB_median_MXN_exec_ci< VXLIB_MEDIAN_TYPENAME_16U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_MXN_init_ci< VXLIB_MEDIAN_TYPENAME_16U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_MXN_exec_ci< VXLIB_MEDIAN_TYPENAME_8S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_MXN_init_ci< VXLIB_MEDIAN_TYPENAME_16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_MXN_exec_ci< VXLIB_MEDIAN_TYPENAME_16S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_MXN_exec_ci< VXLIB_MEDIAN_TYPENAME_8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_MXN_init_ci< VXLIB_MEDIAN_TYPENAME_8S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_MXN_init_ci< VXLIB_MEDIAN_TYPENAME_8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_3X3_exec_ci< VXLIB_MEDIAN_TYPENAME_8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_3X3_exec_ci< VXLIB_MEDIAN_TYPENAME_16S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_3X3_init_ci< VXLIB_MEDIAN_TYPENAME_16U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_3X3_init_ci< VXLIB_MEDIAN_TYPENAME_16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_3X3_init_ci< VXLIB_MEDIAN_TYPENAME_8S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_3X3_init_ci< VXLIB_MEDIAN_TYPENAME_8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_median_3X3_exec_ci< VXLIB_MEDIAN_TYPENAME_16U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_3X3_exec_ci< VXLIB_MEDIAN_TYPENAME_8S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_exec_cn< VXLIB_MEDIAN_TYPENAME_16S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_exec_cn< VXLIB_MEDIAN_TYPENAME_8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_exec_cn< VXLIB_MEDIAN_TYPENAME_16U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
template VXLIB_STATUS VXLIB_median_exec_cn< VXLIB_MEDIAN_TYPENAME_8S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_median.
#define VXLIB_MEDIAN_16U
#define VXLIB_MEDIAN_8U
Macros that will be useful to check for datatype combinations.
#define VXLIB_MEDIAN_8S
#define VXLIB_MEDIAN_16S
void * VXLIB_kernelHandle
Handle type for VXLIB operations.
Definition: VXLIB_types.h:247
VXLIB_STATUS_NAME
The enumeration of all status codes.
Definition: VXLIB_types.h:220
@ VXLIB_ERR_INVALID_DIMENSION
Definition: VXLIB_types.h:225
@ VXLIB_ERR_NULL_POINTER
Definition: VXLIB_types.h:226
@ VXLIB_SUCCESS
Definition: VXLIB_types.h:221
@ VXLIB_ERR_INVALID_TYPE
Definition: VXLIB_types.h:224
#define VXLIB_DEBUGPRINTFN(N, fmt,...)
Definition: VXLIB_types.h:94
@ VXLIB_FUNCTION_NATC
Definition: VXLIB_types.h:251
static int32_t VXLIB_sizeof(uint32_t type)
Inline function returns number of bytes per element given a type of VXLIB_data_type_e.
VXLIB_STATUS VXLIB_median_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pMask, const void *restrict pOut, const void *restrict pScratch)
This function checks the validity of the parameters passed to VXLIB_median_exec function....
int32_t VXLIB_median_getHandleSize(VXLIB_median_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
VXLIB_STATUS VXLIB_median_init(VXLIB_kernelHandle handle, VXLIB_bufParams2D_t *bufParamsIn, VXLIB_bufParams2D_t *bufParamsMask, VXLIB_bufParams2D_t *bufParamsOut, VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
This function should be called before the VXLIB_median_exec function is called. This function takes c...
VXLIB_STATUS VXLIB_median_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pMask, void *restrict pOut, void *restrict pScratch)
This function is the main kernel compute function.
VXLIB_STATUS VXLIB_median_init_checkParams(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsMask, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_bufParams2D_t *bufParamsScratch, const VXLIB_median_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to VXLIB_median_init function....
A structure for a 2 dimensional buffer descriptor.
uint32_t dim_y
Height of buffer in Y dimension in elements.
uint32_t dim_x
Width of buffer in X dimension in elements.
uint32_t data_type
Values are of type VXLIB_data_type_e.
int32_t stride_y
Stride in Y dimension in bytes.
Structure containing the parameters to initialize the kernel.
Definition: VXLIB_median.h:77
int8_t funcStyle
Variant of the function, refer to VXLIB_FUNCTION_STYLE
Definition: VXLIB_median.h:79
bool kernelType
To select between 3 X 3 OR M X N kernel size 0 for 3 X 3 1 for M X N.
Definition: VXLIB_median.h:84
Structure that is reserved for internal use by the kernel.
size_t height
Height of image
size_t N
Width of image
size_t strideInElements
Stride of INPUT in elements.
size_t M
Height of image
size_t width
Width of image
size_t strideScratchElements
Stride of scratch in elements.
size_t widthOut
Width of image
size_t strideOutElements
Stride of output in elements.
pFxnVXLIB_median_exec execute
Function pointer to point to the right execution variant between VXLIB_median_exec_cn and VXLIB_media...
VXLIB_median_InitArgs pKerInitArgs
Initargs of the kernel.
size_t heightOut
Height of image
size_t heightScratch
Height of image
size_t widthScratch
Width of scratch
size_t strideMaskElements
Stride of MASK in elements.