VXLIB User Guide
VXLIB_convolve.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_bufParams.h"
41 #include "VXLIB_convolve_priv.h"
42 #include "VXLIB_types.h"
43 #include <cstdint>
44 
45 /**********************************************************************************************************************/
46 /* */
47 /* VXLIB_convolve_getHandleSize */
48 /* */
49 /**********************************************************************************************************************/
50 
51 // this method calculates and returns the size of the handle for the VXLIB_convolve kernel
53 {
54  int32_t privBufSize = sizeof(VXLIB_convolve_PrivArgs);
55  return privBufSize;
56 }
57 
58 /**********************************************************************************************************************/
59 /* */
60 /* VXLIB_convolve_init_checkParams */
61 /* */
62 /**********************************************************************************************************************/
63 
64 // this method checks the initialization parameters for the VXLIB_convolve kernel
67  const VXLIB_bufParams2D_t *bufParamsIn,
68  const VXLIB_bufParams2D_t *bufParamsFilter,
69  const VXLIB_bufParams2D_t *bufParamsOut,
70  const VXLIB_convolve_InitArgs *pKerInitArgs)
71 {
72  VXLIB_STATUS status = VXLIB_SUCCESS;
73 
74  // obtain input buffer parameters
75  uint32_t dTypeIn = bufParamsIn->data_type;
76  uint32_t widthIn = bufParamsIn->dim_x;
77  uint32_t heightIn = bufParamsIn->dim_y;
78 
79  // get filter parameters
80  uint32_t dTypeFilter = bufParamsFilter->data_type;
81  int32_t filterHeight = pKerInitArgs->filterHeight;
82  int32_t filterWidth = pKerInitArgs->filterWidth;
83  size_t scale = pKerInitArgs->scale;
84 
85  // obtain output buffer parameters
86  uint32_t dTypeOut = bufParamsOut->data_type;
87  uint32_t widthOut = bufParamsOut->dim_x;
88  uint32_t heightOut = bufParamsOut->dim_y;
89 
90  // get init arg
91  size_t padLeft = pKerInitArgs->padLeft;
92  size_t padRight = pKerInitArgs->padRight;
93  size_t padTop = pKerInitArgs->padTop;
94  size_t padBottom = pKerInitArgs->padBottom;
95 
96  bool isNotPadded = (padLeft == 0) && (padRight == 0) && (padTop == 0) && (padBottom == 0);
97 
98  uint32_t strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
99  uint32_t strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
100 
101  // check for null handles
102  if (handle == NULL) {
103  status = VXLIB_ERR_NULL_POINTER;
104  }
105 
106  // check for valid datatype combinations
109  }
110 
111  // check for valid shift parameter
112  else if (scale > 31U) {
114  }
115 
116  // disqualify padded implementation
117  else if (!isNotPadded) {
119  }
120 
121  // check valid dimensions; input height, width must be greater, equal to filter size for non padded implementation
122  else if ((widthIn < widthOut) || (heightIn - filterHeight + 1 != heightOut) || (strideInElements < widthIn) ||
123  (strideOutElements < widthOut) || (widthIn < filterWidth) || (heightIn < filterHeight)) {
125  }
126 
127  else {
128  status = VXLIB_SUCCESS;
129  }
130 
131  return status;
132 }
133 
134 /**********************************************************************************************************************/
135 /* */
136 /* VXLIB_convolve_exec_checkParams */
137 /* */
138 /**********************************************************************************************************************/
139 
140 // this method checks the execution parameters for the VXLIB_convolve kernel
143  const void *restrict pIn,
144  const void *restrict pFilter,
145  const void *restrict pOut)
146 {
147  VXLIB_STATUS status;
148 
149 #if VXLIB_DEBUGPRINT
150  printf("Enter VXLIB_convolve_exec_checkParams\n");
151 #endif
152  if ((handle == NULL) || (pIn == NULL) || (pFilter == NULL) || (pOut == NULL)) {
153  status = VXLIB_ERR_NULL_POINTER;
154  }
155  else {
156  status = VXLIB_SUCCESS;
157  }
158 
159  return status;
160 }
161 
162 /**********************************************************************************************************************/
163 /* */
164 /* VXLIB_convolve_init */
165 /* */
166 /**********************************************************************************************************************/
167 
168 // this method is the user-level initialization function for the VXLIB_convolve kernel
170  VXLIB_bufParams2D_t *bufParamsIn,
171  VXLIB_bufParams2D_t *bufParamsFilter,
172  VXLIB_bufParams2D_t *bufParamsOut,
173  const VXLIB_convolve_InitArgs *pKerInitArgs)
174 {
175  VXLIB_STATUS status = VXLIB_SUCCESS;
176  VXLIB_convolve_PrivArgs *pKerPrivArgs = (VXLIB_convolve_PrivArgs *) handle;
177 
178  // copy pKerinitArgs into pKerPrivargs
179  pKerPrivArgs->pKerInitArgs = *pKerInitArgs;
180 
181  // set priv args
182  pKerPrivArgs->width = bufParamsIn->dim_x;
183  pKerPrivArgs->height = bufParamsIn->dim_y;
184 
185  // compute stride in elements as SE/SA params are set to work with given element type
186  pKerPrivArgs->strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
187  pKerPrivArgs->strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
188 
189 #if VXLIB_DEBUGPRINT
190  printf("VXLIB_DEBUGPRINT Enter VXLIB_convolve_init\n");
191 #endif
192 
193  // obtain buffer datatypes
194  uint32_t dTypeIn = bufParamsIn->data_type;
195  uint32_t dTypeFilter = bufParamsFilter->data_type;
196  uint32_t dTypeOut = bufParamsOut->data_type;
197 
198  // determine natural C vs optimized function call
199  if (pKerInitArgs->funcStyle == VXLIB_FUNCTION_NATC) {
200 
201  // set function pointer for natural C function with appropriate template parameters based on datatypes
202 
205  }
206 
207  else if (VXLIB_CONVOLVE_I8U_C16S_O8U) {
209  }
210 
211  else {
212  status = VXLIB_ERR_INVALID_TYPE;
213  }
214  }
215  else { // Optimized function
216 
217  // set function pointer for natural C function with appropriate template parameters based on datatypes
218 
220  status = VXLIB_convolve_init_ci<VXLIB_CONVOLVE_DTYPE_I8U_C16S_O8U>(handle, bufParamsIn, bufParamsFilter,
221  bufParamsOut, pKerInitArgs);
223  }
224 
225  else if (VXLIB_CONVOLVE_I8U_C16S_O16S) {
226  status = VXLIB_convolve_init_ci<VXLIB_CONVOLVE_DTYPE_I8U_C16S_O16S>(handle, bufParamsIn, bufParamsFilter,
227  bufParamsOut, pKerInitArgs);
229  }
230 
231  else {
232  status = VXLIB_ERR_INVALID_TYPE;
233  }
234  }
235 
236  return status;
237 }
238 
239 /**********************************************************************************************************************/
240 /* */
241 /* VXLIB_convolve_exec */
242 /* */
243 /**********************************************************************************************************************/
244 
245 // this method is the user-level execution function for the VXLIB_convolve kernel
247 VXLIB_convolve_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
248 {
249  VXLIB_STATUS status;
250 
251 #if VXLIB_DEBUGPRINT
252  printf("VXLIB_DEBUGPRINT Enter VXLIB_convolve_exec\n");
253 #endif
254 
255  VXLIB_convolve_PrivArgs *pKerPrivArgs = (VXLIB_convolve_PrivArgs *) handle;
256 
257  status = pKerPrivArgs->execute(handle, pIn, pFilter, pOut);
258 
259  return status;
260 }
template VXLIB_STATUS VXLIB_convolve_exec_ci< VXLIB_CONVOLVE_TYPENAME_I8U_C16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
template VXLIB_STATUS VXLIB_convolve_init_ci< VXLIB_CONVOLVE_DTYPE_I8U_C16S_O8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsFilter, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_convolve_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_convolve_exec_ci< VXLIB_CONVOLVE_TYPENAME_I8U_C16S_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
template VXLIB_STATUS VXLIB_convolve_init_ci< VXLIB_CONVOLVE_DTYPE_I8U_C16S_O16S >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsFilter, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_convolve_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_convolve_exec_cn< VXLIB_CONVOLVE_TYPENAME_I8U_C16S_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
template VXLIB_STATUS VXLIB_convolve_exec_cn< VXLIB_CONVOLVE_TYPENAME_I8U_C16S_O16S >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
Header file for kernel's internal use. For the kernel's interface, please see VXLIB_convolve.
#define VXLIB_CONVOLVE_I8U_C16S_O16S
#define VXLIB_CONVOLVE_I8U_C16S_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_CONVOLVE_INVALID_SHIFT
Definition: VXLIB_types.h:241
@ VXLIB_ERR_CONVOLVE_INVALID_DIMENSION
Definition: VXLIB_types.h:240
@ VXLIB_ERR_NULL_POINTER
Definition: VXLIB_types.h:226
@ VXLIB_ERR_CONVOLVE_INVALID_TYPE_COMBINATION
Definition: VXLIB_types.h:239
@ VXLIB_SUCCESS
Definition: VXLIB_types.h:221
@ VXLIB_ERR_CONVOLVE_PADDED_NOT_IMPLEMENTED
Definition: VXLIB_types.h:238
@ 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_STATUS VXLIB_convolve_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pFilter, const void *restrict pOut)
This function checks the validity of the parameters passed to VXLIB_convolve_exec function....
VXLIB_STATUS VXLIB_convolve_init(VXLIB_kernelHandle handle, VXLIB_bufParams2D_t *bufParamsIn, VXLIB_bufParams2D_t *bufParamsFilter, VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_convolve_InitArgs *pKerInitArgs)
This function should be called before the VXLIB_convolve_exec function is called. This function takes...
VXLIB_STATUS VXLIB_convolve_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pFilter, void *restrict pOut)
This function is the main kernel compute function.
VXLIB_STATUS VXLIB_convolve_init_checkParams(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsFilter, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_convolve_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to VXLIB_convolve_init function....
int32_t VXLIB_convolve_getHandleSize(VXLIB_convolve_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
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.
int32_t padLeft
Padding options
int32_t filterWidth
filter columns
int32_t filterHeight
filter rows
uint32_t scale
scale factor
int8_t funcStyle
Variant of the function, refer to VXLIB_FUNCTION_STYLE
Structure that is reserved for internal use by the kernel.
VXLIB_convolve_InitArgs pKerInitArgs
Initargs of the kernel.
pFxnVXLIB_convolve_exec execute
Function pointer to point to the right execution variant between VXLIB_convolve_exec_cn and VXLIB_con...
size_t width
Width of image
size_t strideInElements
Stride of input in elements.
size_t strideOutElements
Stride of output in elements.
size_t height
Height of image