blob: 6c5ac3c45b93b0bb6c2980de0cacce522df1b91d [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/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 Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/PixelValue.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Window.h"
Georgios Pinitas658039b2017-09-15 16:30:50 +010033#include "arm_compute/runtime/NEON/NEScheduler.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010035#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <cmath>
38#include <cstddef>
39#include <utility>
40
41using namespace arm_compute;
42
43namespace
44{
45void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size)
46{
47 ARM_COMPUTE_ERROR_ON(nullptr == offsets);
48
49 Window win;
50 win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
51 win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
52
53 if(dx != nullptr && dy != nullptr)
54 {
55 // Pre-compute the offset and pixel's distance for BILINEAR interpolation
56 Iterator offsets_it(offsets, win);
57 Iterator dx_it(dx, win);
58 Iterator dy_it(dy, win);
59
60 execute_window_loop(win, [&](const Coordinates & id)
61 {
62 const float in_x = (id.x() + 0.5f) * wr - 0.5f;
63 const float in_y = (id.y() + 0.5f) * hr - 0.5f;
64 const int in_xi = std::floor(in_x);
65 const int in_yi = std::floor(in_y);
66
67 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
68 *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
69 *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
70 },
71 offsets_it, dx_it, dy_it);
72 }
73 else
74 {
75 // Pre-compute the offset for NEAREST interpolation
76 Iterator offsets_it(offsets, win);
77
78 execute_window_loop(win, [&](const Coordinates & id)
79 {
80 const size_t in_xi = (id.x() + 0.5f) * wr;
81
82 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
83 },
84 offsets_it);
85 }
86}
87} // namespace
88
Georgios Pinitas658039b2017-09-15 16:30:50 +010089NEScale::NEScale(std::shared_ptr<IMemoryManager> memory_manager) // NOLINT
90 : _memory_group(std::move(memory_manager)),
91 _offsets(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010092 _dx(),
Georgios Pinitas658039b2017-09-15 16:30:50 +010093 _dy(),
94 _scale_kernel(),
95 _border_handler()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096{
97}
98
99void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value)
100{
101 ARM_COMPUTE_ERROR_ON(nullptr == input);
102 ARM_COMPUTE_ERROR_ON(nullptr == output);
103
104 for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
105 {
106 ARM_COMPUTE_ERROR_ON(input->info()->dimension(i) != output->info()->dimension(i));
107 }
108
109 // Get the tensor shape
110 const TensorShape shape(output->info()->dimension(0), output->info()->dimension(1));
111
112 // Compute the ratio between source width/height and destination width/height
113 const auto wr = static_cast<float>(input->info()->dimension(0)) / static_cast<float>(output->info()->dimension(0));
114 const auto hr = static_cast<float>(input->info()->dimension(1)) / static_cast<float>(output->info()->dimension(1));
115
116 // Get the element size of the input image
117 const size_t input_element_size = input->info()->element_size();
118
119 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
120 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
121 {
122 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
123 }
124
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 // Check if the border mode is UNDEFINED
126 const bool border_undefined = border_mode == BorderMode::UNDEFINED;
127
128 switch(policy)
129 {
130 case InterpolationPolicy::NEAREST_NEIGHBOR:
131 {
132 TensorInfo tensor_info_offsets(shape, Format::S32);
133 _offsets.allocator()->init(tensor_info_offsets);
Georgios Pinitas658039b2017-09-15 16:30:50 +0100134 _memory_group.manage(&_offsets);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135
Georgios Pinitas658039b2017-09-15 16:30:50 +0100136 _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, policy, border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137
138 // Allocate once the configure methods have been called
139 _offsets.allocator()->allocate();
140
141 // Pre-compute offsets for nearest interpolation
142 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size);
143 break;
144 }
145 case InterpolationPolicy::BILINEAR:
146 {
147 TensorInfo tensor_info_offsets(shape, Format::S32);
148 TensorInfo tensor_info_dxdy(shape, Format::F32);
149
150 _offsets.allocator()->init(tensor_info_offsets);
151 _dx.allocator()->init(tensor_info_dxdy);
152 _dy.allocator()->init(tensor_info_dxdy);
153
Georgios Pinitas658039b2017-09-15 16:30:50 +0100154 // Manage intermediate buffers
155 _memory_group.manage(&_offsets);
156 _memory_group.manage(&_dx);
157 _memory_group.manage(&_dy);
158
159 _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, policy, border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 // Allocate once the configure methods have been called
162 _offsets.allocator()->allocate();
163 _dx.allocator()->allocate();
164 _dy.allocator()->allocate();
165
166 // Pre-compute dx, dy and offsets for bilinear interpolation
167 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size);
168 break;
169 }
170 case InterpolationPolicy::AREA:
171 {
Georgios Pinitas658039b2017-09-15 16:30:50 +0100172 _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, policy, border_undefined);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173 break;
174 }
175 default:
176 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
177 }
178
Georgios Pinitas658039b2017-09-15 16:30:50 +0100179 _border_handler.configure(input, _scale_kernel.border_size(), border_mode, PixelValue(constant_border_value));
180}
181
182void NEScale::run()
183{
184 _memory_group.acquire();
185
186 NEScheduler::get().schedule(&_border_handler, Window::DimZ);
187 NEScheduler::get().schedule(&_scale_kernel, Window::DimY);
188
189 _memory_group.release();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190}