blob: 488eee11764df1f36fc2fdf0fb63bae8f540c418 [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 "arm_compute/core/NEON/kernels/NESobel5x5Kernel.h"
25
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33
34#include <arm_neon.h>
35#include <cstddef>
36#include <cstdint>
37
38using namespace arm_compute;
39
40NESobel5x5HorKernel::NESobel5x5HorKernel()
41 : _input(nullptr), _output_x(nullptr), _output_y(nullptr), _run_sobel_x(false), _run_sobel_y(false), _border_size(0)
42{
43}
44
45BorderSize NESobel5x5HorKernel::border_size() const
46{
47 return _border_size;
48}
49
50void NESobel5x5HorKernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
51{
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
53 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
54
55 _run_sobel_x = output_x != nullptr;
56 _run_sobel_y = output_y != nullptr;
57
58 if(_run_sobel_x)
59 {
60 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16);
61 }
62
63 if(_run_sobel_y)
64 {
65 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16);
66 }
67
68 _input = input;
69 _output_x = output_x;
70 _output_y = output_y;
71 _border_size = BorderSize(border_undefined ? 0 : 2, 2);
72
73 // Configure kernel window
74 constexpr unsigned int num_elems_processed_per_iteration = 8;
75 constexpr unsigned int num_elems_read_per_iteration = 16;
76 constexpr unsigned int num_elems_written_per_iteration = 8;
77
78 Window win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
79 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
80 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
81
82 update_window_and_padding(win,
83 AccessWindowHorizontal(input->info(), -border_size().left, num_elems_read_per_iteration),
84 output_x_access,
85 output_y_access);
86
87 output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
88 output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
89
90 INEKernel::configure(win);
91}
92
93void NESobel5x5HorKernel::run(const Window &window)
94{
95 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
96 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
97
98 Window win_in(window);
99 win_in.shift(Window::DimX, -2);
100
101 Iterator input(_input, win_in);
102 Iterator output_x;
103 Iterator output_y;
104
105 if(_run_sobel_x)
106 {
107 output_x = Iterator(_output_x, window);
108 }
109
110 if(_run_sobel_y)
111 {
112 output_y = Iterator(_output_y, window);
113 }
114
115 if(_run_sobel_y && _run_sobel_x)
116 {
117 static const int16x8_t six = vdupq_n_s16(6);
118 static const int16x8_t four = vdupq_n_s16(4);
119 static const int16x8_t two = vdupq_n_s16(2);
120 static const int16x8_t minustwo = vdupq_n_s16(-2);
121
122 execute_window_loop(window, [&](const Coordinates & id)
123 {
124 const uint8x16_t data = vld1q_u8(input.ptr());
125
126 const int16x8x2_t data_s16 =
127 {
128 {
129 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
130 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
131 }
132 };
133
134 int16x8_t out_y = data_s16.val[0];
135 out_y = vmlaq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four);
136 out_y = vmlaq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six);
137 out_y = vmlaq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four);
138 out_y = vaddq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 4));
139
140 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out_y);
141
142 int16x8_t out_x = vnegq_s16(data_s16.val[0]);
143 out_x = vmlaq_s16(out_x, vextq_s16(data_s16.val[0], data_s16.val[1], 1), minustwo);
144 out_x = vmlaq_s16(out_x, vextq_s16(data_s16.val[0], data_s16.val[1], 3), two);
145 out_x = vaddq_s16(out_x, vextq_s16(data_s16.val[0], data_s16.val[1], 4));
146
147 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out_x);
148 },
149 input, output_x, output_y);
150 }
151 else if(_run_sobel_x)
152 {
153 static const int16x8_t two = vdupq_n_s16(2);
154 static const int16x8_t minustwo = vdupq_n_s16(-2);
155
156 execute_window_loop(window, [&](const Coordinates & id)
157 {
158 const uint8x16_t data = vld1q_u8(input.ptr());
159
160 const int16x8x2_t data_s16 =
161 {
162 {
163 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
164 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
165 }
166 };
167
168 int16x8_t out = vnegq_s16(data_s16.val[0]);
169 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), minustwo);
170 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), two);
171 out = vaddq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 4));
172
173 vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out);
174 },
175 input, output_x);
176 }
177 else if(_run_sobel_y)
178 {
179 static const int16x8_t six = vdupq_n_s16(6);
180 static const int16x8_t four = vdupq_n_s16(4);
181
182 execute_window_loop(window, [&](const Coordinates & id)
183 {
184 const uint8x16_t data = vld1q_u8(input.ptr());
185
186 const int16x8x2_t data_s16 =
187 {
188 {
189 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
190 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
191 }
192 };
193
194 int16x8_t out = data_s16.val[0];
195 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four);
196 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six);
197 out = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four);
198 out = vaddq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 4));
199
200 vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out);
201 },
202 input, output_y);
203 }
204}
205
206NESobel5x5VertKernel::NESobel5x5VertKernel()
207 : _input_x(nullptr), _input_y(nullptr), _output_x(nullptr), _output_y(nullptr), _run_sobel_x(false), _run_sobel_y(false)
208{
209}
210
211BorderSize NESobel5x5VertKernel::border_size() const
212{
213 return BorderSize(2, 0);
214}
215
216void NESobel5x5VertKernel::configure(ITensor *input_x, ITensor *input_y, ITensor *output_x, ITensor *output_y, bool border_undefined)
217{
218 ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));
219
220 _run_sobel_x = output_x != nullptr;
221 _run_sobel_y = output_y != nullptr;
222
223 if(_run_sobel_x)
224 {
225 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input_x, Format::S16);
226 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_x, Format::S16);
227 }
228
229 if(_run_sobel_y)
230 {
231 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input_y, Format::S16);
232 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_y, Format::S16);
233 }
234
235 _input_x = input_x;
236 _input_y = input_y;
237 _output_x = output_x;
238 _output_y = output_y;
239
240 const ITensor *const input = _run_sobel_x ? input_x : input_y;
241
242 // Configure kernel window
243 constexpr unsigned int num_elems_processed_per_iteration = 16;
244 constexpr unsigned int num_elems_read_per_iteration = 16;
245 constexpr unsigned int num_elems_written_per_iteration = 16;
246 constexpr unsigned int num_rows_read_per_iteration = 5;
247
248 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
249 AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
250 AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);
251
252 update_window_and_padding(win,
253 AccessWindowRectangle(input_x == nullptr ? nullptr : input_x->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
254 AccessWindowRectangle(input_y == nullptr ? nullptr : input_y->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
255 output_x_access,
256 output_y_access);
257
258 output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
259 output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
260
261 INEKernel::configure(win);
262}
263
264void NESobel5x5VertKernel::run(const Window &window)
265{
266 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
267 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
268
269 Iterator input_x;
270 Iterator input_y;
271 Iterator output_x;
272 Iterator output_y;
273
274 const int16_t *input_x_low2_ptr = nullptr;
275 const int16_t *input_x_low_ptr = nullptr;
276 const int16_t *input_x_mid_ptr = nullptr;
277 const int16_t *input_x_top_ptr = nullptr;
278 const int16_t *input_x_top2_ptr = nullptr;
279
280 const int16_t *input_y_low2_ptr = nullptr;
281 const int16_t *input_y_low_ptr = nullptr;
282 const int16_t *input_y_top_ptr = nullptr;
283 const int16_t *input_y_top2_ptr = nullptr;
284
285 if(_run_sobel_x)
286 {
287 input_x = Iterator(_input_x, window);
288 output_x = Iterator(_output_x, window);
289 input_x_top2_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, -2)));
290 input_x_top_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, -1)));
291 input_x_mid_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, 0)));
292 input_x_low_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, 1)));
293 input_x_low2_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, 2)));
294 }
295
296 if(_run_sobel_y)
297 {
298 input_y = Iterator(_input_y, window);
299 output_y = Iterator(_output_y, window);
300 input_y_top2_ptr = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, -2)));
301 input_y_top_ptr = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, -1)));
302 input_y_low_ptr = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, 1)));
303 input_y_low2_ptr = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, 2)));
304 }
305
306 static const int16x8_t six = vdupq_n_s16(6);
307 static const int16x8_t four = vdupq_n_s16(4);
308 static const int16x8_t two = vdupq_n_s16(2);
309 static const int16x8_t minustwo = vdupq_n_s16(-2);
310
311 if(_run_sobel_x)
312 {
313 execute_window_loop(window, [&](const Coordinates & id)
314 {
315 // Convert offset from uint8_t* to uint16_t*
316 const size_t input_offset_high_s16 = input_x.offset() / 2;
317 const size_t input_offset_low_s16 = input_offset_high_s16 + 8;
318
319 //HIGH DATA
320 //top2
321 int16x8_t data_high = vld1q_s16(input_x_top2_ptr + input_offset_high_s16);
322 int16x8_t out_high = data_high;
323 //top
324 data_high = vld1q_s16(input_x_top_ptr + input_offset_high_s16);
325 out_high = vmlaq_s16(out_high, data_high, four);
326 //mid
327 data_high = vld1q_s16(input_x_mid_ptr + input_offset_high_s16);
328 out_high = vmlaq_s16(out_high, data_high, six);
329 //low
330 data_high = vld1q_s16(input_x_low_ptr + input_offset_high_s16);
331 out_high = vmlaq_s16(out_high, data_high, four);
332 //low2
333 data_high = vld1q_s16(input_x_low2_ptr + input_offset_high_s16);
334 out_high = vaddq_s16(out_high, data_high);
335
336 vst1q_s16((reinterpret_cast<int16_t *>(output_x.ptr())), out_high);
337
338 //LOW DATA
339 //top2
340 int16x8_t data_low = vld1q_s16(input_x_top2_ptr + input_offset_low_s16);
341 int16x8_t out_low = data_low;
342 //top
343 data_low = vld1q_s16(input_x_top_ptr + input_offset_low_s16);
344 out_low = vmlaq_s16(out_low, data_low, four);
345 //mid
346 data_low = vld1q_s16(input_x_mid_ptr + input_offset_low_s16);
347 out_low = vmlaq_s16(out_low, data_low, six);
348 //low
349 data_low = vld1q_s16(input_x_low_ptr + input_offset_low_s16);
350 out_low = vmlaq_s16(out_low, data_low, four);
351 //low2
352 data_low = vld1q_s16(input_x_low2_ptr + input_offset_low_s16);
353 out_low = vaddq_s16(out_low, data_low);
354
355 vst1q_s16((reinterpret_cast<int16_t *>(output_x.ptr())) + 8, out_low);
356 },
357 input_x, output_x);
358 }
359
360 if(_run_sobel_y)
361 {
362 execute_window_loop(window, [&](const Coordinates & id)
363 {
364 // Convert offset from uint8_t* to uint16_t*
365 const size_t input_offset_high_s16 = input_y.offset() / 2;
366 const size_t input_offset_low_s16 = input_offset_high_s16 + 8;
367
368 //HIGH DATA
369 //top2
370 int16x8_t data_high = vld1q_s16(input_y_top2_ptr + input_offset_high_s16);
371 int16x8_t out_high = vnegq_s16(data_high);
372 //top
373 data_high = vld1q_s16(input_y_top_ptr + input_offset_high_s16);
374 out_high = vmlaq_s16(out_high, data_high, minustwo);
375 //low
376 data_high = vld1q_s16(input_y_low_ptr + input_offset_high_s16);
377 out_high = vmlaq_s16(out_high, data_high, two);
378 //low2
379 data_high = vld1q_s16(input_y_low2_ptr + input_offset_high_s16);
380 out_high = vaddq_s16(out_high, data_high);
381
382 vst1q_s16((reinterpret_cast<int16_t *>(output_y.ptr())), out_high);
383
384 //LOW DATA
385 //top2
386 int16x8_t data_low = vld1q_s16(input_y_top2_ptr + input_offset_low_s16);
387 int16x8_t out_low = vnegq_s16(data_low);
388 //top
389 data_low = vld1q_s16(input_y_top_ptr + input_offset_low_s16);
390 out_low = vmlaq_s16(out_low, data_low, minustwo);
391 //low
392 data_low = vld1q_s16(input_y_low_ptr + input_offset_low_s16);
393 out_low = vmlaq_s16(out_low, data_low, two);
394 //low2
395 data_low = vld1q_s16(input_y_low2_ptr + input_offset_low_s16);
396 out_low = vaddq_s16(out_low, data_low);
397
398 vst1q_s16((reinterpret_cast<int16_t *>(output_y.ptr())) + 8, out_low);
399 },
400 input_y, output_y);
401 }
402}