blob: 483aa4c0b5144e43945446c83416be61a717d40a [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
Jenkins514be652019-02-28 12:25:18 +00002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier871448e2017-03-24 14:54:29 +00003 *
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/runtime/NEON/functions/NEScale.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"
Anthony Barbier871448e2017-03-24 14:54:29 +000030#include "arm_compute/core/PixelValue.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Window.h"
Kaizen8938bd32017-09-28 14:38:23 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000034#include "arm_compute/runtime/TensorAllocator.h"
Kaizen8938bd32017-09-28 14:38:23 +010035#include "support/ToolchainSupport.h"
Anthony Barbier871448e2017-03-24 14:54:29 +000036
37#include <cmath>
38#include <cstddef>
39#include <utility>
40
41using namespace arm_compute;
42
43namespace
44{
Anthony Barbier8140e1e2017-12-14 23:48:46 +000045void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size, SamplingPolicy sampling_policy)
Anthony Barbier871448e2017-03-24 14:54:29 +000046{
47 ARM_COMPUTE_ERROR_ON(nullptr == offsets);
Anthony Barbier8140e1e2017-12-14 23:48:46 +000048 ARM_COMPUTE_UNUSED(sampling_policy);
Jenkins514be652019-02-28 12:25:18 +000049 float sampling_offset = 0.0f;
50 if(sampling_policy == SamplingPolicy::CENTER)
51 {
52 sampling_offset = 0.5f;
53 }
Anthony Barbier871448e2017-03-24 14:54:29 +000054
55 Window win;
56 win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
57 win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
58
59 if(dx != nullptr && dy != nullptr)
60 {
61 // Pre-compute the offset and pixel's distance for BILINEAR interpolation
62 Iterator offsets_it(offsets, win);
63 Iterator dx_it(dx, win);
64 Iterator dy_it(dy, win);
65
66 execute_window_loop(win, [&](const Coordinates & id)
67 {
Jenkins514be652019-02-28 12:25:18 +000068 const float in_x = (id.x() + sampling_offset) * wr - sampling_offset;
69 const float in_y = (id.y() + sampling_offset) * hr - sampling_offset;
Anthony Barbier871448e2017-03-24 14:54:29 +000070 const int in_xi = std::floor(in_x);
71 const int in_yi = std::floor(in_y);
72
Jenkinsb3a371b2018-05-23 11:36:53 +010073 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * static_cast<int>(input_element_size);
Anthony Barbier871448e2017-03-24 14:54:29 +000074 *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
75 *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
76 },
77 offsets_it, dx_it, dy_it);
78 }
79 else
80 {
81 // Pre-compute the offset for NEAREST interpolation
82 Iterator offsets_it(offsets, win);
83
84 execute_window_loop(win, [&](const Coordinates & id)
85 {
86 const size_t in_xi = (id.x() + 0.5f) * wr;
Anthony Barbier871448e2017-03-24 14:54:29 +000087
Anthony Barbiera4376382017-04-12 15:12:46 +010088 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
Anthony Barbier871448e2017-03-24 14:54:29 +000089 },
90 offsets_it);
91 }
92}
93} // namespace
94
Kaizen8938bd32017-09-28 14:38:23 +010095NEScale::NEScale() // NOLINT
96 : _offsets(),
97 _dx(),
98 _dy(),
99 _scale_kernel(),
100 _border_handler()
Anthony Barbier871448e2017-03-24 14:54:29 +0000101{
102}
103
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000104void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy)
Anthony Barbier871448e2017-03-24 14:54:29 +0000105{
Jenkinsb3a371b2018-05-23 11:36:53 +0100106 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
107 ARM_COMPUTE_ERROR_THROW_ON(NEScale::validate(input->info(), output->info(), policy, border_mode, constant_border_value, sampling_policy));
Anthony Barbier871448e2017-03-24 14:54:29 +0000108
Jenkinsb3a371b2018-05-23 11:36:53 +0100109 // Get data layout and width/height indices
110 const DataLayout data_layout = input->info()->data_layout();
111 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
112 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier871448e2017-03-24 14:54:29 +0000113
114 // Get the tensor shape
Jenkinsb3a371b2018-05-23 11:36:53 +0100115 const TensorShape shape(output->info()->dimension(idx_width), output->info()->dimension(idx_height));
Anthony Barbier871448e2017-03-24 14:54:29 +0000116
117 // Compute the ratio between source width/height and destination width/height
Jenkinsb3a371b2018-05-23 11:36:53 +0100118 const auto wr = static_cast<float>(input->info()->dimension(idx_width)) / static_cast<float>(output->info()->dimension(idx_width));
119 const auto hr = static_cast<float>(input->info()->dimension(idx_height)) / static_cast<float>(output->info()->dimension(idx_height));
Anthony Barbier871448e2017-03-24 14:54:29 +0000120
Anthony Barbiera4376382017-04-12 15:12:46 +0100121 // Get the element size of the input image
Anthony Barbier871448e2017-03-24 14:54:29 +0000122 const size_t input_element_size = input->info()->element_size();
123
124 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
125 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
126 {
127 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
128 }
129
Anthony Barbier871448e2017-03-24 14:54:29 +0000130 switch(policy)
131 {
132 case InterpolationPolicy::NEAREST_NEIGHBOR:
133 {
134 TensorInfo tensor_info_offsets(shape, Format::S32);
Anthony Barbier871448e2017-03-24 14:54:29 +0000135 _offsets.allocator()->init(tensor_info_offsets);
136
Jenkinsb3a371b2018-05-23 11:36:53 +0100137 _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, policy, border_mode, sampling_policy);
Anthony Barbier871448e2017-03-24 14:54:29 +0000138
139 // Allocate once the configure methods have been called
140 _offsets.allocator()->allocate();
141
142 // Pre-compute offsets for nearest interpolation
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000143 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, sampling_policy);
Anthony Barbier871448e2017-03-24 14:54:29 +0000144 break;
145 }
146 case InterpolationPolicy::BILINEAR:
147 {
148 TensorInfo tensor_info_offsets(shape, Format::S32);
149 TensorInfo tensor_info_dxdy(shape, Format::F32);
150
Anthony Barbier871448e2017-03-24 14:54:29 +0000151 _offsets.allocator()->init(tensor_info_offsets);
152 _dx.allocator()->init(tensor_info_dxdy);
153 _dy.allocator()->init(tensor_info_dxdy);
154
Jenkinsb3a371b2018-05-23 11:36:53 +0100155 _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, policy, border_mode, sampling_policy);
Anthony Barbier871448e2017-03-24 14:54:29 +0000156
157 // Allocate once the configure methods have been called
158 _offsets.allocator()->allocate();
159 _dx.allocator()->allocate();
160 _dy.allocator()->allocate();
161
162 // Pre-compute dx, dy and offsets for bilinear interpolation
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000163 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, sampling_policy);
Anthony Barbier871448e2017-03-24 14:54:29 +0000164 break;
165 }
166 case InterpolationPolicy::AREA:
167 {
Jenkinsb3a371b2018-05-23 11:36:53 +0100168 _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, policy, border_mode);
Anthony Barbier871448e2017-03-24 14:54:29 +0000169 break;
170 }
171 default:
172 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
173 }
174
Jenkins514be652019-02-28 12:25:18 +0000175 _border_handler.configure(input, _scale_kernel.border_size(), border_mode, constant_border_value);
Kaizen8938bd32017-09-28 14:38:23 +0100176}
177
Jenkinsb3a371b2018-05-23 11:36:53 +0100178Status NEScale::validate(const ITensorInfo *input, const ITensorInfo *output, InterpolationPolicy policy,
179 BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy)
180{
181 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Jenkins514be652019-02-28 12:25:18 +0000182 ARM_COMPUTE_RETURN_ERROR_ON(sampling_policy != SamplingPolicy::CENTER && sampling_policy != SamplingPolicy::TOP_LEFT);
Jenkinsb3a371b2018-05-23 11:36:53 +0100183 ARM_COMPUTE_UNUSED(border_mode, constant_border_value);
184
185 ITensorInfo *offsets = nullptr;
186 ITensorInfo *dx = nullptr;
187 ITensorInfo *dy = nullptr;
188
189 // Get data layout and width/height indices
190 const DataLayout data_layout = input->data_layout();
191 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
192 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
193
194 // Get the tensor shape of auxilary buffers
195 const TensorShape shape(output->dimension(idx_width), output->dimension(idx_height));
196
197 TensorInfo tensor_info_offsets(shape, Format::S32);
198 TensorInfo tensor_info_dx(shape, Format::F32);
199 TensorInfo tensor_info_dy(shape, Format::F32);
200
201 switch(policy)
202 {
203 case InterpolationPolicy::NEAREST_NEIGHBOR:
204 offsets = &tensor_info_offsets;
205 break;
206 case InterpolationPolicy::BILINEAR:
207 offsets = &tensor_info_offsets;
208 dx = &tensor_info_dx;
209 dy = &tensor_info_dy;
210 break;
211 default:
212 break;
213 }
214
215 ARM_COMPUTE_RETURN_ON_ERROR(NEScaleKernel::validate(input->clone().get(), dx, dy, offsets, output->clone().get(),
216 policy, border_mode, sampling_policy));
217 return Status{};
218}
219
Kaizen8938bd32017-09-28 14:38:23 +0100220void NEScale::run()
221{
222 NEScheduler::get().schedule(&_border_handler, Window::DimZ);
223 NEScheduler::get().schedule(&_scale_kernel, Window::DimY);
Anthony Barbier871448e2017-03-24 14:54:29 +0000224}