1c75bee0537331e807c0fcca125867ef507a6934
[platform/upstream/armcl.git] / src / runtime / NEON / functions / NEGaussianPyramid.cpp
1 /*
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/NEGaussianPyramid.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/NEON/kernels/NEGaussianPyramidKernel.h"
30 #include "arm_compute/core/NEON/kernels/NEScaleKernel.h"
31 #include "arm_compute/core/PixelValue.h"
32 #include "arm_compute/core/TensorInfo.h"
33 #include "arm_compute/core/Validate.h"
34 #include "arm_compute/runtime/NEON/NEScheduler.h"
35 #include "arm_compute/runtime/NEON/functions/NEGaussian5x5.h"
36 #include "arm_compute/runtime/Pyramid.h"
37 #include "arm_compute/runtime/Tensor.h"
38 #include "arm_compute/runtime/TensorAllocator.h"
39
40 #include <cstddef>
41
42 using namespace arm_compute;
43
44 NEGaussianPyramid::NEGaussianPyramid()
45     : _input(nullptr), _pyramid(nullptr), _tmp()
46 {
47 }
48
49 NEGaussianPyramidHalf::NEGaussianPyramidHalf()
50     : _border_handler(), _horizontal_reduction(), _vertical_reduction()
51 {
52 }
53
54 void NEGaussianPyramidHalf::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
55 {
56     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
57     ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
58     ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
59     ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
60     ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
61     ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_HALF != pyramid->info()->scale());
62
63     /* Get number of pyramid levels */
64     const size_t num_levels = pyramid->info()->num_levels();
65
66     _input   = input;
67     _pyramid = pyramid;
68
69     if(num_levels > 1)
70     {
71         _border_handler       = arm_compute::cpp14::make_unique<NEFillBorderKernel[]>(num_levels - 1);
72         _horizontal_reduction = arm_compute::cpp14::make_unique<NEGaussianPyramidHorKernel[]>(num_levels - 1);
73         _vertical_reduction   = arm_compute::cpp14::make_unique<NEGaussianPyramidVertKernel[]>(num_levels - 1);
74
75         // Apply half scale to the X dimension of the tensor shape
76         TensorShape tensor_shape = pyramid->info()->tensor_shape();
77         tensor_shape.set(0, (pyramid->info()->width() + 1) * SCALE_PYRAMID_HALF);
78
79         PyramidInfo pyramid_info;
80         pyramid_info.init(num_levels - 1, SCALE_PYRAMID_HALF, tensor_shape, Format::S16);
81
82         _tmp.init_auto_padding(pyramid_info);
83         _tmp.allocate();
84
85         for(unsigned int i = 0; i < num_levels - 1; ++i)
86         {
87             /* Configure border */
88             _border_handler[i].configure(_pyramid->get_pyramid_level(i), 2, border_mode, PixelValue(constant_border_value));
89
90             /* Configure horizontal kernel */
91             _horizontal_reduction[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode == BorderMode::UNDEFINED);
92
93             /* Configure vertical kernel */
94             _vertical_reduction[i].configure(_tmp.get_pyramid_level(i), _pyramid->get_pyramid_level(i + 1), border_mode == BorderMode::UNDEFINED);
95         }
96     }
97 }
98
99 void NEGaussianPyramidHalf::run()
100 {
101     ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
102
103     /* Get number of pyramid levels */
104     const size_t num_levels = _pyramid->info()->num_levels();
105
106     /* The first level of the pyramid has the input image */
107     _pyramid->get_pyramid_level(0)->copy_from(*_input);
108
109     for(unsigned int i = 0; i < num_levels - 1; ++i)
110     {
111         _border_handler[i].run(_border_handler[i].window());
112         NEScheduler::get().multithread(_horizontal_reduction.get() + i);
113         NEScheduler::get().multithread(_vertical_reduction.get() + i);
114     }
115 }
116
117 NEGaussianPyramidOrb::NEGaussianPyramidOrb()
118     : _offsets(), _gaus5x5(), _scale_nearest()
119 {
120 }
121
122 void NEGaussianPyramidOrb::configure(const ITensor *input, IPyramid *pyramid, BorderMode border_mode, uint8_t constant_border_value)
123 {
124     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
125     ARM_COMPUTE_ERROR_ON(nullptr == pyramid);
126     ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions());
127     ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->info()->width());
128     ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->info()->height());
129     ARM_COMPUTE_ERROR_ON(SCALE_PYRAMID_ORB != pyramid->info()->scale());
130
131     /* Get number of pyramid levels */
132     const size_t num_levels = pyramid->info()->num_levels();
133
134     _input   = input;
135     _pyramid = pyramid;
136
137     if(num_levels > 1)
138     {
139         _gaus5x5       = arm_compute::cpp14::make_unique<NEGaussian5x5[]>(num_levels - 1);
140         _scale_nearest = arm_compute::cpp14::make_unique<NEScaleKernel[]>(num_levels - 1);
141         _offsets       = arm_compute::cpp14::make_unique<Image[]>(num_levels - 1);
142
143         PyramidInfo pyramid_info;
144         pyramid_info.init(num_levels - 1, SCALE_PYRAMID_ORB, pyramid->info()->tensor_shape(), Format::U8);
145
146         _tmp.init_auto_padding(pyramid_info);
147         _tmp.allocate();
148
149         for(unsigned int i = 0; i < num_levels - 1; ++i)
150         {
151             const size_t width  = _pyramid->get_pyramid_level(i + 1)->info()->dimension(0);
152             const size_t height = _pyramid->get_pyramid_level(i + 1)->info()->dimension(1);
153
154             /* Allocate Image for the offsets used by NEAREST interpolation */
155             TensorInfo tensor_info(TensorShape(width, height), Format::S32);
156             tensor_info.auto_padding();
157             _offsets[i].allocator()->init(tensor_info);
158             _offsets[i].allocator()->allocate();
159
160             /* Configure gaussian 5x5 */
161             _gaus5x5[i].configure(_pyramid->get_pyramid_level(i), _tmp.get_pyramid_level(i), border_mode, constant_border_value);
162
163             /* Configure scale image kernel */
164             _scale_nearest[i].configure(_tmp.get_pyramid_level(i), nullptr, nullptr, _offsets.get() + i, _pyramid->get_pyramid_level(i + 1), InterpolationPolicy::NEAREST_NEIGHBOR,
165                                         border_mode == BorderMode::UNDEFINED);
166         }
167     }
168 }
169
170 void NEGaussianPyramidOrb::run()
171 {
172     ARM_COMPUTE_ERROR_ON_MSG(_pyramid == nullptr, "Unconfigured function");
173
174     /* Get number of pyramid levels */
175     const size_t num_levels = _pyramid->info()->num_levels();
176
177     /* The first level of the pyramid has the input image */
178     _pyramid->get_pyramid_level(0)->copy_from(*_input);
179
180     for(unsigned int i = 0; i < num_levels - 1; ++i)
181     {
182         _gaus5x5[i].run();
183         NEScheduler::get().multithread(_scale_nearest.get() + i);
184     }
185 }