blob: 43ef6199ba8b1b8247bbaf258e8a8e0b3c6fcece [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas393fa4c2018-05-08 15:54:53 +01002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070045void precompute_dx_dy_offsets(ITensor *dx, ITensor *dy, ITensor *offsets, float wr, float hr, size_t input_element_size, SamplingPolicy sampling_policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046{
47 ARM_COMPUTE_ERROR_ON(nullptr == offsets);
Daniil Efremov02bf80d2017-11-22 00:26:51 +070048 ARM_COMPUTE_ERROR_ON(sampling_policy != SamplingPolicy::CENTER);
49 ARM_COMPUTE_UNUSED(sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050
51 Window win;
52 win.set(Window::DimX, Window::Dimension(0, offsets->info()->dimension(0), 1));
53 win.set(Window::DimY, Window::Dimension(0, offsets->info()->dimension(1), 1));
54
55 if(dx != nullptr && dy != nullptr)
56 {
57 // Pre-compute the offset and pixel's distance for BILINEAR interpolation
58 Iterator offsets_it(offsets, win);
59 Iterator dx_it(dx, win);
60 Iterator dy_it(dy, win);
61
62 execute_window_loop(win, [&](const Coordinates & id)
63 {
64 const float in_x = (id.x() + 0.5f) * wr - 0.5f;
65 const float in_y = (id.y() + 0.5f) * hr - 0.5f;
66 const int in_xi = std::floor(in_x);
67 const int in_yi = std::floor(in_y);
68
69 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
70 *reinterpret_cast<float *>(dx_it.ptr()) = in_x - in_xi;
71 *reinterpret_cast<float *>(dy_it.ptr()) = in_y - in_yi;
72 },
73 offsets_it, dx_it, dy_it);
74 }
75 else
76 {
77 // Pre-compute the offset for NEAREST interpolation
78 Iterator offsets_it(offsets, win);
79
80 execute_window_loop(win, [&](const Coordinates & id)
81 {
82 const size_t in_xi = (id.x() + 0.5f) * wr;
83
84 *reinterpret_cast<int32_t *>(offsets_it.ptr()) = in_xi * input_element_size;
85 },
86 offsets_it);
87 }
88}
89} // namespace
90
Georgios Pinitas3021edf2017-09-18 17:55:22 +010091NEScale::NEScale() // NOLINT
92 : _offsets(),
Moritz Pflanzerf4af76e2017-09-06 07:42:43 +010093 _dx(),
Georgios Pinitas658039b2017-09-15 16:30:50 +010094 _dy(),
95 _scale_kernel(),
96 _border_handler()
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097{
98}
99
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700100void NEScale::configure(ITensor *input, ITensor *output, InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, SamplingPolicy sampling_policy)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101{
102 ARM_COMPUTE_ERROR_ON(nullptr == input);
103 ARM_COMPUTE_ERROR_ON(nullptr == output);
104
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100105 // Get data layout and width/height indices
106 const DataLayout data_layout = input->info()->data_layout();
107 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
108 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109
110 // Get the tensor shape
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100111 const TensorShape shape(output->info()->dimension(idx_width), output->info()->dimension(idx_height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
113 // Compute the ratio between source width/height and destination width/height
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100114 const auto wr = static_cast<float>(input->info()->dimension(idx_width)) / static_cast<float>(output->info()->dimension(idx_width));
115 const auto hr = static_cast<float>(input->info()->dimension(idx_height)) / static_cast<float>(output->info()->dimension(idx_height));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116
117 // Get the element size of the input image
118 const size_t input_element_size = input->info()->element_size();
119
120 // Area interpolation behaves as Nearest Neighbour in case of up-sampling
121 if(policy == InterpolationPolicy::AREA && wr <= 1.f && hr <= 1.f)
122 {
123 policy = InterpolationPolicy::NEAREST_NEIGHBOR;
124 }
125
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 switch(policy)
127 {
128 case InterpolationPolicy::NEAREST_NEIGHBOR:
129 {
130 TensorInfo tensor_info_offsets(shape, Format::S32);
131 _offsets.allocator()->init(tensor_info_offsets);
132
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100133 _scale_kernel.configure(input, nullptr, nullptr, &_offsets, output, policy, border_mode, sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134
135 // Allocate once the configure methods have been called
136 _offsets.allocator()->allocate();
137
138 // Pre-compute offsets for nearest interpolation
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700139 precompute_dx_dy_offsets(nullptr, nullptr, &_offsets, wr, hr, input_element_size, sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 break;
141 }
142 case InterpolationPolicy::BILINEAR:
143 {
144 TensorInfo tensor_info_offsets(shape, Format::S32);
145 TensorInfo tensor_info_dxdy(shape, Format::F32);
146
147 _offsets.allocator()->init(tensor_info_offsets);
148 _dx.allocator()->init(tensor_info_dxdy);
149 _dy.allocator()->init(tensor_info_dxdy);
150
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100151 _scale_kernel.configure(input, &_dx, &_dy, &_offsets, output, policy, border_mode, sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152
153 // Allocate once the configure methods have been called
154 _offsets.allocator()->allocate();
155 _dx.allocator()->allocate();
156 _dy.allocator()->allocate();
157
158 // Pre-compute dx, dy and offsets for bilinear interpolation
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700159 precompute_dx_dy_offsets(&_dx, &_dy, &_offsets, wr, hr, input_element_size, sampling_policy);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 break;
161 }
162 case InterpolationPolicy::AREA:
163 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100164 _scale_kernel.configure(input, nullptr, nullptr, nullptr, output, policy, border_mode);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 break;
166 }
167 default:
168 ARM_COMPUTE_ERROR("Unsupported interpolation mode");
169 }
170
Georgios Pinitas658039b2017-09-15 16:30:50 +0100171 _border_handler.configure(input, _scale_kernel.border_size(), border_mode, PixelValue(constant_border_value));
172}
173
174void NEScale::run()
175{
Georgios Pinitas658039b2017-09-15 16:30:50 +0100176 NEScheduler::get().schedule(&_border_handler, Window::DimZ);
177 NEScheduler::get().schedule(&_scale_kernel, Window::DimY);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178}