VXLIB User Guide
VXLIB_gaussian.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_gaussian_priv.h"
41 #include "VXLIB_types.h"
42 
43 /**********************************************************************************************************************/
44 /* */
45 /* VXLIB_gaussian_getHandleSize */
46 /* */
47 /**********************************************************************************************************************/
48 
49 // this method calculates and returns the size of the handle for the VXLIB_gaussian kernel
51 {
52  int32_t privBufSize = sizeof(VXLIB_gaussian_PrivArgs);
53  return privBufSize;
54 }
55 
56 /**********************************************************************************************************************/
57 /* */
58 /* VXLIB_gaussian_init_checkParams */
59 /* */
60 /**********************************************************************************************************************/
61 
62 // this method checks the initialization parameters for the VXLIB_gaussian kernel
65  const VXLIB_bufParams2D_t *bufParamsIn,
66  const VXLIB_bufParams2D_t *bufParamsOut,
67  const VXLIB_gaussian_InitArgs *pKerInitArgs)
68 {
69  VXLIB_STATUS status = VXLIB_SUCCESS;
70 
71  // check for null handles
72  if (handle == NULL || bufParamsIn == NULL || bufParamsOut == NULL || pKerInitArgs == NULL) {
73  status = VXLIB_ERR_NULL_POINTER;
74  }
75 
76  // check for valid datatype combinations
77  else if ((bufParamsIn->data_type != VXLIB_UINT8) || (bufParamsOut->data_type != VXLIB_UINT8)) {
78  status = VXLIB_ERR_INVALID_TYPE;
79  }
80 
81  // disqualify padded implementation
82  else if (!((pKerInitArgs->padLeft == 0) && (pKerInitArgs->padRight == 0) && (pKerInitArgs->padTop == 0) &&
83  (pKerInitArgs->padBottom == 0))) {
85  }
86 
87  // check valid dimensions; input height, width must be greater, equal to filter size for non padded implementation
88  else if ((bufParamsIn->dim_x < bufParamsOut->dim_x) ||
89  (bufParamsIn->dim_y - pKerInitArgs->filterSize + 1 != bufParamsOut->dim_y) ||
90  (bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type) < bufParamsIn->dim_x) ||
91  (bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type) < bufParamsOut->dim_x) ||
92  (bufParamsIn->dim_x < pKerInitArgs->filterSize) || (bufParamsIn->dim_y < pKerInitArgs->filterSize)) {
94  }
95 
96  // shift not applicable for 3x3 gaussian filter (any shift given for 3x3 will be ignored)
97  else if (pKerInitArgs->filterSize == VXLIB_GAUSSIAN_FILTER_5x5 &&
99  status = VXLIB_ERR_INVALID_SHIFT;
100  }
101 
102  else {
103  status = VXLIB_SUCCESS;
104  }
105 
106  return status;
107 }
108 
109 /**********************************************************************************************************************/
110 /* */
111 /* VXLIB_gaussian_exec_checkParams */
112 /* */
113 /**********************************************************************************************************************/
114 
115 // this method checks the execution parameters for the VXLIB_gaussian kernel
117 VXLIB_gaussian_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pOut)
118 {
119  VXLIB_STATUS status;
120 
121 #if VXLIB_DEBUGPRINT
122  printf("Enter VXLIB_gaussian_exec_checkParams\n");
123 #endif
124  if ((handle == NULL) || (pIn == NULL) || (pOut == NULL)) {
125  status = VXLIB_ERR_NULL_POINTER;
126  }
127  else {
128  status = VXLIB_SUCCESS;
129  }
130 
131  return status;
132 }
133 
134 /**********************************************************************************************************************/
135 /* */
136 /* VXLIB_gaussian_init */
137 /* */
138 /**********************************************************************************************************************/
139 
140 // this method is the user-level initialization function for the VXLIB_gaussian kernel
142  VXLIB_bufParams2D_t *bufParamsIn,
143  VXLIB_bufParams2D_t *bufParamsOut,
144  const VXLIB_gaussian_InitArgs *pKerInitArgs)
145 {
146  VXLIB_STATUS status = VXLIB_SUCCESS;
147  VXLIB_gaussian_PrivArgs *pKerPrivArgs = (VXLIB_gaussian_PrivArgs *) handle;
148 
149  // copy pKerinitArgs into pKerPrivargs
150  pKerPrivArgs->pKerInitArgs = *pKerInitArgs;
151 
152  // set priv args
153  pKerPrivArgs->width = bufParamsIn->dim_x;
154  pKerPrivArgs->height = bufParamsIn->dim_y;
155 
156  // compute stride in elements as SE/SA params are set to work with given element type
157  pKerPrivArgs->strideInElements = bufParamsIn->stride_y / VXLIB_sizeof(bufParamsIn->data_type);
158  pKerPrivArgs->strideOutElements = bufParamsOut->stride_y / VXLIB_sizeof(bufParamsOut->data_type);
159 
160 #if VXLIB_DEBUGPRINT
161  printf("VXLIB_DEBUGPRINT Enter VXLIB_gaussian_init\n");
162 #endif
163 
164  // obtain buffer datatypes
165  uint32_t dTypeIn = bufParamsIn->data_type;
166  uint32_t dTypeOut = bufParamsOut->data_type;
167 
168  // determine natural C vs optimized function call
169  if (pKerInitArgs->funcStyle == VXLIB_FUNCTION_NATC) {
170 
171  // set function pointer for natural C function with appropriate template parameters based on datatypes
172 
175  }
176  else {
177  status = VXLIB_ERR_INVALID_TYPE;
178  }
179  }
180  else { // Optimized function
181 
182  // set function pointer for natural C function with appropriate template parameters based on datatypes
183 
185  status = VXLIB_gaussian_init_ci<VXLIB_GAUSSIAN_DTYPE_I8U_O8U>(handle, bufParamsIn, bufParamsOut, pKerInitArgs);
187  }
188  else {
189  status = VXLIB_ERR_INVALID_TYPE;
190  }
191  }
192 
193  return status;
194 }
195 
196 /**********************************************************************************************************************/
197 /* */
198 /* VXLIB_gaussian_exec */
199 /* */
200 /**********************************************************************************************************************/
201 
202 // this method is the user-level execution function for the VXLIB_gaussian kernel
204 VXLIB_gaussian_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
205 {
206  VXLIB_STATUS status;
207 
208 #if VXLIB_DEBUGPRINT
209  printf("VXLIB_DEBUGPRINT Enter VXLIB_gaussian_exec\n");
210 #endif
211 
212  VXLIB_gaussian_PrivArgs *pKerPrivArgs = (VXLIB_gaussian_PrivArgs *) handle;
213 
214  status = pKerPrivArgs->execute(handle, pIn, pOut);
215 
216  return status;
217 }
template VXLIB_STATUS VXLIB_gaussian_exec_ci< VXLIB_GAUSSIAN_TYPENAME_I8U_O8U >(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
template VXLIB_STATUS VXLIB_gaussian_init_ci< VXLIB_GAUSSIAN_DTYPE_I8U_O8U >(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_gaussian_InitArgs *pKerInitArgs)
template VXLIB_STATUS VXLIB_gaussian_exec_cn< VXLIB_GAUSSIAN_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_gaussian.
#define VXLIB_GAUSSIAN_FILTER_5x5
Macros for 5x5 filter dimension.
#define VXLIB_GAUSSIAN_FILTER_5x5_MAX_SHIFT
Macros for maximum shift parameter for 5x5 filter.
#define VXLIB_GAUSSIAN_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_INVALID_SHIFT
Definition: VXLIB_types.h:234
@ 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_gaussian_init(VXLIB_kernelHandle handle, VXLIB_bufParams2D_t *bufParamsIn, VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_gaussian_InitArgs *pKerInitArgs)
This function should be called before the VXLIB_gaussian_exec function is called. This function takes...
VXLIB_STATUS VXLIB_gaussian_init_checkParams(VXLIB_kernelHandle handle, const VXLIB_bufParams2D_t *bufParamsIn, const VXLIB_bufParams2D_t *bufParamsOut, const VXLIB_gaussian_InitArgs *pKerInitArgs)
This function checks the validity of the parameters passed to VXLIB_gaussian_init function....
VXLIB_STATUS VXLIB_gaussian_exec_checkParams(VXLIB_kernelHandle handle, const void *restrict pIn, const void *restrict pOut)
This function checks the validity of the parameters passed to VXLIB_gaussian_exec function....
int32_t VXLIB_gaussian_getHandleSize(VXLIB_gaussian_InitArgs *pKerInitArgs)
This is a query function to calculate the size of internal handle.
VXLIB_STATUS VXLIB_gaussian_exec(VXLIB_kernelHandle handle, void *restrict pIn, void *restrict pOut)
This function is the main kernel compute 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.
int8_t funcStyle
Variant of the function, refer to VXLIB_FUNCTION_STYLE
uint8_t shift
Shift parameter for 5x5 filter
int8_t filterSize
Width and height of filter
int32_t padLeft
Padding options
Structure that is reserved for internal use by the kernel.
size_t width
Width of image
size_t height
Height of image
size_t strideOutElements
Stride of output in elements.
pFxnVXLIB_gaussian_exec execute
Function pointer to point to the right execution variant between VXLIB_gaussian_exec_cn and VXLIB_gau...
size_t strideInElements
Stride of input in elements.
VXLIB_gaussian_InitArgs pKerInitArgs
Initargs of the kernel.