blob: d1335c5558fdf6de9383ced55e43d2ff403937e6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "helpers.h"
25
26#ifndef DATA_TYPE
27#define DATA_TYPE short
28#endif
29
30#ifndef COMPUTE_TYPE
31#define COMPUTE_TYPE int
32#endif
33
34#ifndef DATA_TYPE_OUT
35#define DATA_TYPE_OUT uchar
36#endif
37
38/** Compute a 1D horizontal convolution of size 5 for 8 bytes assuming the input is made of 1 channel of 1 byte (i.e 8 pixels).
39 *
40 * @param[in] left_pixel Pointer to the left pixel
41 * @param[in] left1_coeff Weight of the most left pixel
42 * @param[in] left2_coeff Weight of the left pixel
43 * @param[in] middle_coeff Weight of the middle pixel
44 * @param[in] right1_coeff Weight of the right pixel
45 * @param[in] right2_coeff Weight of the most right pixel
46 *
47 * @return a short8 containing 8 convoluted values.
48 */
49VEC_DATA_TYPE(DATA_TYPE, 8)
50convolution1x5(
51 __global const uchar *left_pixel,
52 const short left1_coeff,
53 const short left2_coeff,
54 const short middle_coeff,
55 const short right1_coeff,
56 const short right2_coeff)
57{
58 uchar16 temp = vload16(0, left_pixel);
59
60 VEC_DATA_TYPE(DATA_TYPE, 8)
61 left1 = CONVERT(temp.s01234567, VEC_DATA_TYPE(DATA_TYPE, 8));
62 VEC_DATA_TYPE(DATA_TYPE, 8)
63 left2 = CONVERT(temp.s12345678, VEC_DATA_TYPE(DATA_TYPE, 8));
64 VEC_DATA_TYPE(DATA_TYPE, 8)
65 middle = CONVERT(temp.s23456789, VEC_DATA_TYPE(DATA_TYPE, 8));
66 VEC_DATA_TYPE(DATA_TYPE, 8)
67 right1 = CONVERT(temp.s3456789a, VEC_DATA_TYPE(DATA_TYPE, 8));
68 VEC_DATA_TYPE(DATA_TYPE, 8)
69 right2 = CONVERT(temp.s456789ab, VEC_DATA_TYPE(DATA_TYPE, 8));
70
71 return left1 * (VEC_DATA_TYPE(DATA_TYPE, 8))left1_coeff + left2 * (VEC_DATA_TYPE(DATA_TYPE, 8))left2_coeff
72 + middle * (VEC_DATA_TYPE(DATA_TYPE, 8))middle_coeff + right1 * (VEC_DATA_TYPE(DATA_TYPE, 8))right1_coeff + right2 * (VEC_DATA_TYPE(DATA_TYPE, 8))right2_coeff;
73}
74
75/** Compute a 1D vertical convolution of size 5 for 8 bytes assuming the input is made of 1 channel of 1 byte (i.e 8 pixels).
76 *
77 * @param[in] src Pointer to source image.
78 * @param[in] up1_coeff Weight of the most up pixel
79 * @param[in] up2_coeff Weight of the up pixel
80 * @param[in] middle_coeff Weight of the middle pixel
81 * @param[in] down1_coeff Weight of the down pixel
82 * @param[in] down2_coeff Weight of the most down pixel
83 *
84 * @return a short8 containing 8 convoluted values.
85 */
86VEC_DATA_TYPE(COMPUTE_TYPE, 8)
87convolution5x1(
88 Image *src,
89 const short up1_coeff,
90 const short up2_coeff,
91 const short middle_coeff,
92 const short down1_coeff,
93 const short down2_coeff)
94{
95 VEC_DATA_TYPE(COMPUTE_TYPE, 8)
96 val;
97 VEC_DATA_TYPE(COMPUTE_TYPE, 8)
98 out = (VEC_DATA_TYPE(COMPUTE_TYPE, 8))0;
99
100 val = CONVERT(vload8(0, (__global DATA_TYPE *)offset(src, 0, -2)), VEC_DATA_TYPE(COMPUTE_TYPE, 8));
101 out += val * (VEC_DATA_TYPE(COMPUTE_TYPE, 8))up1_coeff;
102
103 val = CONVERT(vload8(0, (__global DATA_TYPE *)offset(src, 0, -1)), VEC_DATA_TYPE(COMPUTE_TYPE, 8));
104 out += val * (VEC_DATA_TYPE(COMPUTE_TYPE, 8))up2_coeff;
105
106 val = CONVERT(vload8(0, (__global DATA_TYPE *)offset(src, 0, 0)), VEC_DATA_TYPE(COMPUTE_TYPE, 8));
107 out += val * (VEC_DATA_TYPE(COMPUTE_TYPE, 8))middle_coeff;
108
109 val = CONVERT(vload8(0, (__global DATA_TYPE *)offset(src, 0, 1)), VEC_DATA_TYPE(COMPUTE_TYPE, 8));
110 out += val * (VEC_DATA_TYPE(COMPUTE_TYPE, 8))down1_coeff;
111
112 val = CONVERT(vload8(0, (__global DATA_TYPE *)offset(src, 0, 2)), VEC_DATA_TYPE(COMPUTE_TYPE, 8));
113 out += val * (VEC_DATA_TYPE(COMPUTE_TYPE, 8))down2_coeff;
114
115 return out;
116}
117
118/** Apply a 5x5 convolution matrix to a single channel U8 input image and return the result.
119 *
120 * Convolution matrix layout:\n
121 * [ mat0, mat1, mat2, mat3 , mat4 ]\n
122 * [ mat5, mat6, mat7, mat8, mat9 ]\n
123 * [ mat10, mat11, mat12, mat13, mat14 ]\n
124 * [ mat15, mat16, mat17, mat18, mat19 ]\n
125 * [ mat20, mat21, mat22, mat23, mat24 ]
126 *
127 * @param[in] src A pointer to source Image structure.
128 * @param[in] mat0 Coefficient from the convolution matrix
129 * @param[in] mat1 Coefficient from the convolution matrix
130 * @param[in] mat2 Coefficient from the convolution matrix
131 * @param[in] mat3 Coefficient from the convolution matrix
132 * @param[in] mat4 Coefficient from the convolution matrix
133 * @param[in] mat5 Coefficient from the convolution matrix
134 * @param[in] mat6 Coefficient from the convolution matrix
135 * @param[in] mat0 Coefficient from the convolution matrix
136 * @param[in] mat7 Coefficient from the convolution matrix
137 * @param[in] mat8 Coefficient from the convolution matrix
138 * @param[in] mat9 Coefficient from the convolution matrix
139 * @param[in] mat10 Coefficient from the convolution matrix
140 * @param[in] mat11 Coefficient from the convolution matrix
141 * @param[in] mat12 Coefficient from the convolution matrix
142 * @param[in] mat13 Coefficient from the convolution matrix
143 * @param[in] mat14 Coefficient from the convolution matrix
144 * @param[in] mat15 Coefficient from the convolution matrix
145 * @param[in] mat16 Coefficient from the convolution matrix
146 * @param[in] mat10 Coefficient from the convolution matrix
147 * @param[in] mat17 Coefficient from the convolution matrix
148 * @param[in] mat18 Coefficient from the convolution matrix
149 * @param[in] mat19 Coefficient from the convolution matrix
150 * @param[in] mat20 Coefficient from the convolution matrix
151 * @param[in] mat21 Coefficient from the convolution matrix
152 * @param[in] mat22 Coefficient from the convolution matrix
153 * @param[in] mat23 Coefficient from the convolution matrix
154 * @param[in] mat24 Coefficient from the convolution matrix
155 * @param[in] scale Convolution matrix scale (Sum of the coefficients, or 1 if the sum is 0)
156 *
157 * @return a short8 containing 8 convoluted and scaled values.
158 */
159short8 convolution5x5(
160 Image *src,
161 const short mat0, const short mat1, const short mat2, const short mat3, const short mat4,
162 const short mat5, const short mat6, const short mat7, const short mat8, const short mat9,
163 const short mat10, const short mat11, const short mat12, const short mat13, const short mat14,
164 const short mat15, const short mat16, const short mat17, const short mat18, const short mat19,
165 const short mat20, const short mat21, const short mat22, const short mat23, const short mat24,
166 uint scale)
167{
168 VEC_DATA_TYPE(DATA_TYPE, 8)
169 pixels;
170
171 pixels = convolution1x5(offset(src, -2, -2), mat0, mat1, mat2, mat3, mat4);
172 pixels += convolution1x5(offset(src, -2, -1), mat5, mat6, mat7, mat8, mat9);
173 pixels += convolution1x5(offset(src, -2, 0), mat10, mat11, mat12, mat13, mat14);
174 pixels += convolution1x5(offset(src, -2, 1), mat15, mat16, mat17, mat18, mat19);
175 pixels += convolution1x5(offset(src, -2, 2), mat20, mat21, mat22, mat23, mat24);
176
177 if(scale > 0)
178 {
179 pixels /= (VEC_DATA_TYPE(DATA_TYPE, 8))scale;
180 }
181
182 return convert_short8_sat(pixels);
183}
184
185#ifndef DYNAMIC_MATRIX_CONVOLUTION
186
187/** Apply a 1x5 static convolution matrix to a single channel U8 input image and output a single temporary channel image(Support U16, S16, S32).
188 *
189 * @attention The matrix coefficients (MAT0, MAT1, MAT2, MAT3, MAT4) and DATA_TYPE need to be passed at compile time:\n
190 * e.g. -DMAT0=1 -DMAT2=2, -DMAT3=3, -DMAT4=4, -DDATA_TYPE=int
191 *
192 * @param[in] src_ptr Pointer to the source image. Supported data types: U8
193 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
194 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
195 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
196 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
197 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
198 * @param[out] dst_ptr Pointer to the destination image. Supported data types: U16, S16, S32
199 * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes)
200 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
201 * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes)
202 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
203 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image
204 */
205__kernel void convolution_separable1x5_static(
206 IMAGE_DECLARATION(src),
207 IMAGE_DECLARATION(dst))
208{
209 Image src = CONVERT_TO_IMAGE_STRUCT(src);
210 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
211
212 // Output pixels
213 VEC_DATA_TYPE(DATA_TYPE, 8)
214 pixels = convolution1x5(offset(&src, -2, 0), MAT0, MAT1, MAT2, MAT3, MAT4);
215
216 // Store result in dst
217 vstore8(pixels, 0, (__global DATA_TYPE *)dst.ptr);
218}
219
220/** Apply a 5x1 static convolution matrix to a single channel U8 input image and output a single channel image.
221 *
222 * @attention The matrix coefficients (MAT5, MAT6, MAT7, MAT8, MAT9, SCALE), COMPUTE_TYPE and DATA_TYPE_OUT need to be passed at compile time:\n
223 * e.g. -DMAT5=1 -DMAT6=2, -DMAT7=3, -DMAT8=4, -DMAT9=5, -DSCALE=6, -DCOMPUTE_TYPE=int, -DDATA_TYPE_OUT=int
224 *
225 * @param[in] src_ptr Pointer to the source image. Supported data types: U16, S16, S32
226 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
227 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
228 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
229 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
230 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
231 * @param[out] dst_ptr Pointer to the destination image. Supported data types: U8, S16
232 * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes)
233 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
234 * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes)
235 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
236 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image
237 */
238__kernel void convolution_separable5x1_static(
239 IMAGE_DECLARATION(src),
240 IMAGE_DECLARATION(dst))
241{
242 Image src = CONVERT_TO_IMAGE_STRUCT(src);
243 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
244
245 // Output pixels
246 VEC_DATA_TYPE(COMPUTE_TYPE, 8)
247 pixels = convolution5x1(&src, MAT5, MAT6, MAT7, MAT8, MAT9);
248
249 // Divide by the scale
250 pixels /= (VEC_DATA_TYPE(COMPUTE_TYPE, 8))SCALE;
251
252 // Store result in dst
253 vstore8(CONVERT_SAT(pixels, VEC_DATA_TYPE(DATA_TYPE_OUT, 8)), 0, (__global DATA_TYPE_OUT *)dst.ptr);
254}
255
256/** Apply a static 5x5 convolution matrix to a single channel U8 input image and output a single channel image including borders
257 *
258 * @attention The matrix coefficients(MAT0, MAT1, ... MAT24, SCALE), DATA_TYPE_OUT need to be passed at compile time:\n
259 * e.g. -DMAT0=1 -DMAT1=2, ... -DMAT24=24, -DSCALE=6, -DDATA_TYPE_OUT=int
260 *
261 * @param[in] src_ptr Pointer to the source image. Supported data types: U8
262 * @param[in] src_stride_x Stride of the source image in X dimension (in bytes)
263 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
264 * @param[in] src_stride_y Stride of the source image in Y dimension (in bytes)
265 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
266 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source image
267 * @param[out] dst_ptr Pointer to the destination image. Supported data types: U8, S16
268 * @param[in] dst_stride_x Stride of the destination image in X dimension (in bytes)
269 * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes)
270 * @param[in] dst_stride_y Stride of the destination image in Y dimension (in bytes)
271 * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes)
272 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination image
273 */
274__kernel void convolution5x5_static(
275 IMAGE_DECLARATION(src),
276 IMAGE_DECLARATION(dst))
277{
278 Image src = CONVERT_TO_IMAGE_STRUCT(src);
279 Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
280
281 short8 pixels = convolution5x5(&src,
282 MAT0, MAT1, MAT2, MAT3, MAT4, MAT5, MAT6, MAT7, MAT8, MAT9, MAT10, MAT11, MAT12, MAT13,
283 MAT14, MAT15, MAT16, MAT17, MAT18, MAT19, MAT20, MAT21, MAT22, MAT23, MAT24, SCALE);
284
285 // Store the result as is in dst
286 vstore8(CONVERT_SAT(pixels, VEC_DATA_TYPE(DATA_TYPE_OUT, 8)), 0, (__global DATA_TYPE_OUT *)dst.ptr);
287}
288
289#endif // DYNAMIC_MATRIX_CONVOLUTION