blob: d0d1c6852ff86e7373c38d99d5e259c0f748ba6d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2016-2020 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEChannelExtractKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/IAccessWindow.h"
29#include "arm_compute/core/IMultiImage.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/MultiImageInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010036#include "src/core/NEON/INEKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40#include <arm_neon.h>
41
42using namespace arm_compute;
43
44namespace arm_compute
45{
46class Coordinates;
47} // namespace arm_compute
48
49NEChannelExtractKernel::NEChannelExtractKernel()
50 : _func(nullptr), _lut_index(0)
51{
52}
53
54void NEChannelExtractKernel::configure(const ITensor *input, Channel channel, ITensor *output)
55{
56 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
57 ARM_COMPUTE_ERROR_ON(input == output);
58
59 set_format_if_unknown(*output->info(), Format::U8);
60
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010061 // Check if input tensor has a valid format
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::UYVY422, Format::YUYV422);
63 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
64
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010065 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
66 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010068 // Check if channel is valid for given format
69 const Format format = input->info()->format();
70 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010072 unsigned int subsampling = 1;
73
74 if(format == Format::YUYV422 || format == Format::UYVY422)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010076 // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422)
77 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010079 if(channel != Channel::Y)
80 {
81 subsampling = 2;
82 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 }
84
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010085 TensorShape output_shape = calculate_subsampled_shape(input->info()->tensor_shape(), format, channel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 set_shape_if_empty(*output->info(), output_shape);
87
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010088 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape());
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010090 _input = input;
91 _output = output;
92 _lut_index = channel_idx_from_format(format, channel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010094 unsigned int num_elems_processed_per_iteration = 16;
95
96 if(format == Format::YUYV422 || format == Format::UYVY422)
97 {
98 _func = &NEChannelExtractKernel::extract_1C_from_2C_img;
99
100 if(channel != Channel::Y) // Channel::U or Channel::V
101 {
102 num_elems_processed_per_iteration = 32;
103 _func = &NEChannelExtractKernel::extract_YUYV_uv;
104 }
105 }
106 else // Format::RGB888 or Format::RGBA8888
107 {
108 _func = &NEChannelExtractKernel::extract_1C_from_3C_img;
109
110 if(format == Format::RGBA8888)
111 {
112 _func = &NEChannelExtractKernel::extract_1C_from_4C_img;
113 }
114 }
115
116 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
117
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100118 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
119 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / subsampling, 1.f / subsampling);
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100120 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122 ValidRegion input_valid_region = input->info()->valid_region();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
124
125 INEKernel::configure(win);
126}
127
128void NEChannelExtractKernel::configure(const IMultiImage *input, Channel channel, IImage *output)
129{
130 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
131 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
132
133 set_format_if_unknown(*output->info(), Format::U8);
134
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100135 const Format format = input->info()->format();
136 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
137
138 // Get input plane
139 const IImage *input_plane = input->plane(plane_idx_from_channel(format, channel));
140 ARM_COMPUTE_ERROR_ON_NULLPTR(input_plane);
141
142 if(Channel::Y == channel && format != Format::YUV444)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100144 // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422)
145 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input_plane);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146 }
147
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100148 // Calculate 2x2 subsampled tensor shape
149 TensorShape output_shape = calculate_subsampled_shape(input->plane(0)->info()->tensor_shape(), format, channel);
150 set_shape_if_empty(*output->info(), output_shape);
151
152 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape());
153
154 // Check if input tensor has a valid format
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
156 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
157
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100158 _input = input_plane;
159 _output = output;
160 _lut_index = channel_idx_from_format(format, channel);
161
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 unsigned int num_elems_processed_per_iteration = 32;
163
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100164 _func = &NEChannelExtractKernel::copy_plane;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100166 if((format == Format::NV12 || format == Format::NV21) && channel != Channel::Y)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100168 num_elems_processed_per_iteration = 16;
169 _func = &NEChannelExtractKernel::extract_1C_from_2C_img;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170 }
171
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100172 Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
173
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174 AccessWindowHorizontal input_access(_input->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100175 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 update_window_and_padding(win, input_access, output_access);
177 output_access.set_valid_region(win, _input->info()->valid_region());
178
179 INEKernel::configure(win);
180}
181
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100182void NEChannelExtractKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100184 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
186 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
187 ARM_COMPUTE_ERROR_ON(_func == nullptr);
188
189 (this->*_func)(window);
190}
191
192void NEChannelExtractKernel::extract_1C_from_2C_img(const Window &win)
193{
194 Iterator in(_input, win);
195 Iterator out(_output, win);
196
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100197 execute_window_loop(win, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 {
199 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
200 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
201 const auto pixels = vld2q_u8(in_ptr);
202 vst1q_u8(out_ptr, pixels.val[_lut_index]);
203 },
204 in, out);
205}
206
207void NEChannelExtractKernel::extract_1C_from_3C_img(const Window &win)
208{
209 Iterator in(_input, win);
210 Iterator out(_output, win);
211
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100212 execute_window_loop(win, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 {
214 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
215 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
216 const auto pixels = vld3q_u8(in_ptr);
217 vst1q_u8(out_ptr, pixels.val[_lut_index]);
218 },
219 in, out);
220}
221
222void NEChannelExtractKernel::extract_1C_from_4C_img(const Window &win)
223{
224 Iterator in(_input, win);
225 Iterator out(_output, win);
226
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100227 execute_window_loop(win, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228 {
229 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
230 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
231 const auto pixels = vld4q_u8(in_ptr);
232 vst1q_u8(out_ptr, pixels.val[_lut_index]);
233 },
234 in, out);
235}
236
237void NEChannelExtractKernel::extract_YUYV_uv(const Window &win)
238{
239 ARM_COMPUTE_ERROR_ON(win.x().step() % 2);
240
241 Window win_out(win);
242 win_out.set_dimension_step(Window::DimX, win.x().step() / 2);
243
244 Iterator in(_input, win);
245 Iterator out(_output, win_out);
246
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100247 execute_window_loop(win, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248 {
249 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
250 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
251 const auto pixels = vld4q_u8(in_ptr);
252 vst1q_u8(out_ptr, pixels.val[_lut_index]);
253 },
254 in, out);
255}
256
257void NEChannelExtractKernel::copy_plane(const Window &win)
258{
259 Iterator in(_input, win);
260 Iterator out(_output, win);
261
262 execute_window_loop(win, [&](const Coordinates &)
263 {
264 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
265 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
266 vst4_u8(out_ptr, vld4_u8(in_ptr));
267 },
268 in, out);
269}