VXLIB User Guide
VXLIB_box.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_box_priv.h"
41 #include "VXLIB_types.h"
42 #include <cstdint>
43 
44 /**********************************************************************************************************************/
45 /* */
46 /* VXLIB_box_getHandleSize */
47 /* */
48 /**********************************************************************************************************************/
49 
50 // this method calculates and returns the size of the handle for the VXLIB_box kernel
52 {
53  int32_t privBufSize = sizeof(VXLIB_box_PrivArgs);
54  return privBufSize;
55 }
56 
57 /**********************************************************************************************************************/
58 /* */
59 /* VXLIB_box_init_checkParams */
60 /* */
61 /**********************************************************************************************************************/
62 
63 // this method checks the initialization parameters for the VXLIB_box kernel
66  const VXLIB_bufParams2D_t *bufParamsIn,
67  const VXLIB_bufParams2D_t *bufParamsOut,
68  const VXLIB_box_InitArgs *pKerInitArgs)
69 {
70  VXLIB_STATUS status = VXLIB_SUCCESS;
71 
72  // obtain input buffer parameters
73  uint32_t dTypeIn = bufParamsIn->data_type;
74  uint32_t widthIn = bufParamsIn->dim_x;
75  uint32_t heightIn = bufParamsIn->dim_y;
76 
77  // obtain output buffer parameters
78  uint32_t dTypeOut = bufParamsOut->data_type;
79  uint32_t widthOut = bufParamsOut->dim_x;
80  uint32_t heightOut = bufParamsOut->dim_y;
81 
82  // get init arg
83  size_t padLeft = pKerInitArgs->padLeft;
84  size_t padRight = pKerInitArgs->padRight;
85  size_t padTop = pKerInitArgs->padTop;
86  size_t padBottom = pKerInitArgs->padBottom;
87 
88  size_t filterDim = pKerInitArgs->filterSize;
89 
90  bool isNotPadded = (padLeft == 0) && (padRight == 0) && (padTop == 0) && (padBottom == 0);
91 
92  uint32_t strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
93  uint32_t strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
94 
95  // check for null handles
96  if (handle == NULL) {
97  status = VXLIB_ERR_NULL_POINTER;
98  }
99 
100  // check for valid datatype combinations
101  else if ((dTypeIn != VXLIB_UINT8) || (dTypeOut != VXLIB_UINT8)) {
102  status = VXLIB_ERR_INVALID_TYPE;
103  }
104 
105  // disqualify padded implementation
106  else if (!isNotPadded) {
107  status = VXLIB_ERR_NOT_IMPLEMENTED;
108  }
109 
110  // check valid dimensions; input height, width must be greater, equal to filter size for non padded implementation
111  else if ((widthIn < widthOut) || (heightIn - filterDim + 1 != heightOut) || (strideInElements < widthIn) ||
112  (strideOutElements < widthOut) || (widthIn < filterDim) || (heightIn < filterDim)) {
114  }
115 
116  else {
117  status = VXLIB_SUCCESS;
118  }
119 
120  return status;
121 }
122 
123 /**********************************************************************************************************************/
124 /* */
125 /* VXLIB_box_exec_checkParams */
126 /* */
127 /**********************************************************************************************************************/
128 
129 // this method checks the execution parameters for the VXLIB_box kernel
130 VXLIB_STATUS VXLIB_box_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pOut)
131 {
132  VXLIB_STATUS status;
133 
134 #if VXLIB_DEBUGPRINT
135  printf("Enter VXLIB_box_exec_checkParams\n");
136 #endif
137  if ((handle == NULL) || (pIn == NULL) || (pOut == NULL)) {
138  status = VXLIB_ERR_NULL_POINTER;
139  }
140  else {
141  status = VXLIB_SUCCESS;
142  }
143 
144  return status;
145 }
146 
147 /**********************************************************************************************************************/
148 /* */
149 /* VXLIB_box_init */
150 /* */
151 /**********************************************************************************************************************/
152 
153 // this method is the user-level initialization function for the VXLIB_box kernel
155  VXLIB_bufParams2D_t *bufParamsIn,
156  VXLIB_bufParams2D_t *bufParamsOut,
157  const VXLIB_box_InitArgs *pKerInitArgs)
158 {
159  VXLIB_STATUS status = VXLIB_SUCCESS;
160  VXLIB_box_PrivArgs *pKerPrivArgs = (VXLIB_box_PrivArgs *) handle;
161 
162  // copy pKerinitArgs into pKerPrivargs
163  pKerPrivArgs->pKerInitArgs = *pKerInitArgs;
164 
165  // set priv args
166  pKerPrivArgs->width = bufParamsIn->dim_x;
167  pKerPrivArgs->height = bufParamsIn->dim_y;
168 
169  // compute stride in elements as SE/SA params are set to work with given element type
170  pKerPrivArgs->strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
171  pKerPrivArgs->strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
172 
173 #if VXLIB_DEBUGPRINT
174  printf("VXLIB_DEBUGPRINT Enter VXLIB_box_init\n");
175 #endif
176 
177  // obtain buffer datatypes
178  uint32_t dTypeIn = bufParamsIn->data_type;
179  uint32_t dTypeOut = bufParamsOut->data_type;
180 
181  // determine natural C vs optimized function call
182  if (pKerInitArgs->funcStyle == VXLIB_FUNCTION_NATC) {
183 
184  // set function pointer for natural C function with appropriate template parameters based on datatypes
185 
186  if (VXLIB_BOX_I8U_O8U) {
188  }
189  else {
190  status = VXLIB_ERR_INVALID_TYPE;
191  }
192  }
193  else { // Optimized function
194 
195  // set function pointer for natural C function with appropriate template parameters based on datatypes
196 
197  if (VXLIB_BOX_I8U_O8U) {
198  status = VXLIB_box_init_ci<VXLIB_BOX_DTYPE_I8U_O8U>(handle, bufParamsIn, bufParamsOut, pKerInitArgs);
200  }
201  else {
202  status = VXLIB_ERR_INVALID_TYPE;
203  }
204  }
205 
206  return status;
207 }
208 
209 /**********************************************************************************************************************/
210 /* */
211 /* VXLIB_box_exec */
212 /* */
213 /**********************************************************************************************************************/
214 
215 // this method is the user-level execution function for the VXLIB_box kernel
217 VXLIB_box_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
218 {
219  VXLIB_STATUS status;
220 
221 #if VXLIB_DEBUGPRINT
222  printf("VXLIB_DEBUGPRINT Enter VXLIB_box_exec\n");
223 #endif
224 
225  VXLIB_box_PrivArgs *pKerPrivArgs = (VXLIB_box_PrivArgs *) handle;
226 
227  status = pKerPrivArgs->execute(handle, pIn, pOut);
228 
229  return status;
230 }
template VXLIB_STATUS VXLIB_box_init_ci< VXLIB_BOX_DTYPE_I8U_O8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_box_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_box_exec_ci< VXLIB_BOX_TYPENAME_I8U_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
template VXLIB_STATUS VXLIB_box_exec_cn< VXLIB_BOX_TYPENAME_I8U_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_box.
#define VXLIB_BOX_I8U_O8U
Macros that will be useful to check for datatype combinations.
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_NOT_IMPLEMENTED
Definition: VXLIB_types.h:227
@ 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
@ 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_UINT8
VXLIB_STATUS VXLIB_box_init(VXLIB_kernelHandle handle, VXLIB_bufParams2D_t *bufParamsIn, VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_box_InitArgs *pKerInitArgs)
This function should be called before the VXLIB_box_exec function is called. This function takes care...
Definition: VXLIB_box.cpp:154
VXLIB_STATUS VXLIB_box_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pOut)
This function checks the validity of the parameters passed to VXLIB_box_exec function....
Definition: VXLIB_box.cpp:130
VXLIB_STATUS VXLIB_box_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
This function is the main kernel compute function.
Definition: VXLIB_box.cpp:217
VXLIB_STATUS VXLIB_box_init_checkParams(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_box_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to VXLIB_box_init function....
Definition: VXLIB_box.cpp:65
int32_t VXLIB_box_getHandleSize(VXLIB_box_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
Definition: VXLIB_box.cpp:51
Structure containing the parameters to initialize the kernel.
Definition: VXLIB_box.h:83
int8_t filterSize
Width and height of filter
Definition: VXLIB_box.h:88
int32_t padRight
Definition: VXLIB_box.h:92
int32_t padBottom
Definition: VXLIB_box.h:94
int32_t padLeft
Padding options
Definition: VXLIB_box.h:91
int8_t funcStyle
Variant of the function, refer to VXLIB_FUNCTION_STYLE
Definition: VXLIB_box.h:85
Structure that is reserved for internal use by the kernel.
size_t strideOutElements
Stride of output in elements.
size_t height
Height of image
VXLIB_box_InitArgs pKerInitArgs
Initargs of the kernel.
pFxnVXLIB_box_exec execute
Function pointer to point to the right execution variant between VXLIB_box_exec_cn and VXLIB_box_exec...
size_t strideInElements
Stride of input in elements.
size_t width
Width of image
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.