Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/NERemapKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.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/TensorInfo.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 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | class Coordinates; |
| 43 | } // namespace arm_compute |
| 44 | |
| 45 | namespace |
| 46 | { |
| 47 | inline int32x4_t offset_nearest_interpolation(const float *mapx_ptr, const float *mapy_ptr, const float32x4_t &width, const float32x4_t &height, const int32x4_t &stride) |
| 48 | { |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 49 | const float32x4_t lowerxy = vdupq_n_f32(-1.f); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | |
| 51 | float32x4_t x = vld1q_f32(mapx_ptr); |
| 52 | float32x4_t y = vld1q_f32(mapy_ptr); |
| 53 | |
| 54 | // Clamp x coordinates |
| 55 | x = vmaxq_f32(lowerxy, vminq_f32(x, width)); |
| 56 | y = vmaxq_f32(lowerxy, vminq_f32(y, height)); |
| 57 | |
| 58 | const int32x4_t x_s32 = vcvtq_s32_f32(x); |
| 59 | const int32x4_t y_s32 = vcvtq_s32_f32(y); |
| 60 | |
| 61 | return vmlaq_s32(x_s32, y_s32, stride); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
| 65 | |
| 66 | NERemapKernel::NERemapKernel() |
| 67 | : _func(nullptr), _input(nullptr), _output(nullptr), _map_x(nullptr), _map_y(nullptr) |
| 68 | { |
| 69 | } |
| 70 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 71 | BorderSize NERemapKernel::border_size() const |
| 72 | { |
| 73 | return BorderSize(1); |
| 74 | } |
| 75 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | void NERemapKernel::configure(const ITensor *input, const ITensor *map_x, const ITensor *map_y, ITensor *output, InterpolationPolicy policy) |
| 77 | { |
| 78 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 79 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 80 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(map_x, 1, DataType::F32); |
| 81 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(map_y, 1, DataType::F32); |
| 82 | |
| 83 | _input = input; |
| 84 | _output = output; |
| 85 | _map_x = map_x; |
| 86 | _map_y = map_y; |
| 87 | |
| 88 | switch(policy) |
| 89 | { |
| 90 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 91 | { |
| 92 | _func = &NERemapKernel::remap_nearest; |
| 93 | break; |
| 94 | } |
| 95 | case InterpolationPolicy::BILINEAR: |
| 96 | { |
| 97 | _func = &NERemapKernel::remap_bilinear; |
| 98 | break; |
| 99 | } |
| 100 | default: |
| 101 | ARM_COMPUTE_ERROR("Unsupported interpolation mode"); |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 106 | |
| 107 | // Configure kernel window |
| 108 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 109 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 110 | const int total_right = ceil_to_multiple(input->info()->dimension(0), num_elems_processed_per_iteration); |
| 111 | const int access_right = total_right + (((total_right - input->info()->dimension(0)) == 0) ? border_size().right : 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 113 | AccessWindowStatic input_access(input->info(), -border_size().left, -border_size().top, access_right, input->info()->dimension(1) + border_size().bottom); |
| 114 | |
| 115 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 116 | AccessWindowHorizontal mapx_access(map_x->info(), 0, 0, num_elems_processed_per_iteration); |
| 117 | AccessWindowHorizontal mapy_access(map_y->info(), 0, 0, num_elems_processed_per_iteration); |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 118 | |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 119 | update_window_and_padding(win, input_access, mapx_access, mapy_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | |
Sanghoon Lee | fc35b51 | 2017-10-18 12:09:04 +0100 | [diff] [blame] | 121 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
| 123 | INEKernel::configure(win); |
| 124 | } |
| 125 | |
| 126 | void NERemapKernel::remap_nearest(const Window &window) |
| 127 | { |
| 128 | // Don't increment in X and Y direction for the input tensor |
| 129 | // A pointer to the start of this plane is needed as base for the precomputed offsets |
| 130 | Window win_in(window); |
| 131 | win_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 132 | win_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 133 | |
| 134 | Iterator in(_input, win_in); |
| 135 | Iterator out(_output, window); |
| 136 | Iterator mapx(_map_x, window); |
| 137 | Iterator mapy(_map_y, window); |
| 138 | |
| 139 | const float32x4_t width = vdupq_n_f32(static_cast<float>(_input->info()->dimension(0))); |
| 140 | const float32x4_t height = vdupq_n_f32(static_cast<float>(_input->info()->dimension(1))); |
| 141 | const int32x4_t in_stride = vdupq_n_s32(static_cast<int32_t>(_input->info()->strides_in_bytes()[1])); |
| 142 | |
| 143 | execute_window_loop(window, [&](const Coordinates & id) |
| 144 | { |
| 145 | const auto mapx_ptr = reinterpret_cast<const float *>(mapx.ptr()); |
| 146 | const auto mapy_ptr = reinterpret_cast<const float *>(mapy.ptr()); |
| 147 | const uint8_t *in_ptr = in.ptr(); |
| 148 | |
| 149 | const int32x4_t offset0 = offset_nearest_interpolation(mapx_ptr + 0, mapy_ptr + 0, width, height, in_stride); |
| 150 | const int32x4_t offset1 = offset_nearest_interpolation(mapx_ptr + 4, mapy_ptr + 4, width, height, in_stride); |
| 151 | const int32x4_t offset2 = offset_nearest_interpolation(mapx_ptr + 8, mapy_ptr + 8, width, height, in_stride); |
| 152 | const int32x4_t offset3 = offset_nearest_interpolation(mapx_ptr + 12, mapy_ptr + 12, width, height, in_stride); |
| 153 | |
Pablo Tello | fc1ffe6 | 2018-01-23 08:31:41 +0000 | [diff] [blame] | 154 | uint8x16_t tmp = vdupq_n_u8(0); |
| 155 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset0, 0)], tmp, 0); |
| 156 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset0, 1)], tmp, 1); |
| 157 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset0, 2)], tmp, 2); |
| 158 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset0, 3)], tmp, 3); |
| 159 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset1, 0)], tmp, 4); |
| 160 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset1, 1)], tmp, 5); |
| 161 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset1, 2)], tmp, 6); |
| 162 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset1, 3)], tmp, 7); |
| 163 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset2, 0)], tmp, 8); |
| 164 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset2, 1)], tmp, 9); |
| 165 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset2, 2)], tmp, 10); |
| 166 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset2, 3)], tmp, 11); |
| 167 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset3, 0)], tmp, 12); |
| 168 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset3, 1)], tmp, 13); |
| 169 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset3, 2)], tmp, 14); |
| 170 | tmp = vsetq_lane_u8(in_ptr[vgetq_lane_s32(offset3, 3)], tmp, 15); |
| 171 | vst1q_u8(out.ptr(), tmp); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | }, |
| 173 | in, out, mapx, mapy); |
| 174 | } |
| 175 | |
| 176 | void NERemapKernel::remap_bilinear(const Window &window) |
| 177 | { |
| 178 | // Don't increment in X and Y direction for the input tensor |
| 179 | // A pointer to the start of this plane is needed as base for the precomputed offsets |
| 180 | Window win_in(window); |
| 181 | win_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 182 | win_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 183 | |
| 184 | Iterator in(_input, win_in); |
| 185 | Iterator out(_output, window); |
| 186 | Iterator mapx(_map_x, window); |
| 187 | Iterator mapy(_map_y, window); |
| 188 | |
| 189 | const size_t width = _input->info()->dimension(0); |
| 190 | const size_t height = _input->info()->dimension(1); |
| 191 | const size_t in_stride = _input->info()->strides_in_bytes()[1]; |
| 192 | |
| 193 | execute_window_loop(window, [&](const Coordinates & id) |
| 194 | { |
| 195 | const auto mapx_ptr = reinterpret_cast<float *>(mapx.ptr()); |
| 196 | const auto mapy_ptr = reinterpret_cast<float *>(mapy.ptr()); |
| 197 | const uint8_t *in_ptr = in.ptr(); |
| 198 | |
| 199 | uint8x8_t tmp0 = vdup_n_u8(0); |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 200 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[0], mapy_ptr[0]), tmp0, 0); |
| 201 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[1], mapy_ptr[1]), tmp0, 1); |
| 202 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[2], mapy_ptr[2]), tmp0, 2); |
| 203 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[3], mapy_ptr[3]), tmp0, 3); |
| 204 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[4], mapy_ptr[4]), tmp0, 4); |
| 205 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[5], mapy_ptr[5]), tmp0, 5); |
| 206 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[6], mapy_ptr[6]), tmp0, 6); |
| 207 | tmp0 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[7], mapy_ptr[7]), tmp0, 7); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | |
| 209 | uint8x8_t tmp1 = vdup_n_u8(0); |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 210 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[8], mapy_ptr[8]), tmp1, 0); |
| 211 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[9], mapy_ptr[9]), tmp1, 1); |
| 212 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[10], mapy_ptr[10]), tmp1, 2); |
| 213 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[11], mapy_ptr[11]), tmp1, 3); |
| 214 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[12], mapy_ptr[12]), tmp1, 4); |
| 215 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[13], mapy_ptr[13]), tmp1, 5); |
| 216 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[14], mapy_ptr[14]), tmp1, 6); |
| 217 | tmp1 = vset_lane_u8(pixel_bilinear_c1_clamp(in_ptr, in_stride, width, height, mapx_ptr[15], mapy_ptr[15]), tmp1, 7); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 218 | |
| 219 | vst1q_u8(out.ptr(), vcombine_u8(tmp0, tmp1)); |
| 220 | }, |
| 221 | in, out, mapx, mapy); |
| 222 | } |
| 223 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 224 | void NERemapKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 225 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 226 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 227 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 228 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 229 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 230 | |
| 231 | (this->*_func)(window); |
| 232 | } |