blob: bac24718bad40e8d4ac61d91f93a787b6e078d65 [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/core/NEON/kernels/NEChannelExtractKernel.h"
25
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"
32#include "arm_compute/core/NEON/INEKernel.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38#include <arm_neon.h>
39
40using namespace arm_compute;
41
42namespace arm_compute
43{
44class Coordinates;
45} // namespace arm_compute
46
47NEChannelExtractKernel::NEChannelExtractKernel()
48 : _func(nullptr), _lut_index(0)
49{
50}
51
52void NEChannelExtractKernel::configure(const ITensor *input, Channel channel, ITensor *output)
53{
54 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
55 ARM_COMPUTE_ERROR_ON(input == output);
56
57 set_format_if_unknown(*output->info(), Format::U8);
58
59 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::UYVY422, Format::YUYV422);
60 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
61
62 unsigned int num_elems_processed_per_iteration = 8;
63
64 // Check format and channel
65 const Format format = input->info()->format();
66 const unsigned int subsampling = (format == Format::YUYV422 || format == Format::UYVY422) && channel != Channel::Y ? 2 : 1;
67 TensorShape output_shape;
68
69 switch(format)
70 {
71 case Format::RGB888:
72 case Format::RGBA8888:
73 num_elems_processed_per_iteration = 16;
74 output_shape = input->info()->tensor_shape();
75
76 if(format == Format::RGB888)
77 {
78 _func = &NEChannelExtractKernel::extract_1C_from_3C_img;
79 }
80 else if(format == Format::RGBA8888)
81 {
82 _func = &NEChannelExtractKernel::extract_1C_from_4C_img;
83 }
84
85 switch(channel)
86 {
87 case Channel::R:
88 _lut_index = 0;
89 break;
90 case Channel::G:
91 _lut_index = 1;
92 break;
93 case Channel::B:
94 _lut_index = 2;
95 break;
96 case Channel::A:
97 if(format == Format::RGBA8888)
98 {
99 _lut_index = 3;
100 _func = &NEChannelExtractKernel::extract_1C_from_4C_img;
101 break;
102 }
103 default:
104 ARM_COMPUTE_ERROR("Not supported channel for this format.");
105 break;
106 }
107 break;
108 case Format::YUYV422:
109 case Format::UYVY422:
110 output_shape = input->info()->tensor_shape();
111
112 if(channel != Channel::Y)
113 {
114 output_shape.set(0, output_shape[0] / 2);
115 }
116
117 switch(channel)
118 {
119 case Channel::Y:
120 num_elems_processed_per_iteration = 16;
121 _func = &NEChannelExtractKernel::extract_1C_from_2C_img;
122 _lut_index = (Format::YUYV422 == format) ? 0 : 1;
123 break;
124 case Channel::U:
125 num_elems_processed_per_iteration = 32;
126 _func = &NEChannelExtractKernel::extract_YUYV_uv;
127 _lut_index = (Format::YUYV422 == format) ? 1 : 0;
128 break;
129 case Channel::V:
130 num_elems_processed_per_iteration = 32;
131 _func = &NEChannelExtractKernel::extract_YUYV_uv;
132 _lut_index = (Format::YUYV422 == format) ? 3 : 2;
133 break;
134 default:
135 ARM_COMPUTE_ERROR("Not supported channel for this format.");
136 break;
137 }
138 break;
139 default:
140 ARM_COMPUTE_ERROR("Not supported format.");
141 break;
142 }
143
144 set_shape_if_empty(*output->info(), output_shape);
145
146 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
147
148 _input = input;
149 _output = output;
150
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100151 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
152 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
153 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / subsampling, 1.f / subsampling);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100155 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
157 ValidRegion input_valid_region = input->info()->valid_region();
158
159 output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
160
161 INEKernel::configure(win);
162}
163
164void NEChannelExtractKernel::configure(const IMultiImage *input, Channel channel, IImage *output)
165{
166 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
167 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
168
169 set_format_if_unknown(*output->info(), Format::U8);
170
171 switch(input->info()->format())
172 {
173 case Format::NV12:
174 case Format::NV21:
175 case Format::IYUV:
176 switch(channel)
177 {
178 case Channel::Y:
179 set_shape_if_empty(*output->info(), input->plane(0)->info()->tensor_shape());
180 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input->plane(0), output);
181 break;
182 case Channel::U:
183 case Channel::V:
184 set_shape_if_empty(*output->info(), input->plane(1)->info()->tensor_shape());
185 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input->plane(1), output);
186 break;
187 default:
188 ARM_COMPUTE_ERROR("Unsupported channel for selected format");
189 }
190 break;
191 case Format::YUV444:
192 set_shape_if_empty(*output->info(), input->plane(0)->info()->tensor_shape());
193 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input->plane(0), output);
194 break;
195 default:
196 ARM_COMPUTE_ERROR("Unsupported format");
197 }
198
199 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
200 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
201
202 unsigned int num_elems_processed_per_iteration = 32;
203
204 const Format &format = input->info()->format();
205
206 switch(format)
207 {
208 case Format::NV12:
209 case Format::NV21:
210 switch(channel)
211 {
212 case Channel::Y:
213 _input = input->plane(0);
214 _func = &NEChannelExtractKernel::copy_plane;
215 break;
216 case Channel::U:
217 _input = input->plane(1);
218 num_elems_processed_per_iteration = 16;
219 _func = &NEChannelExtractKernel::extract_1C_from_2C_img;
220 _lut_index = (Format::NV12 == format) ? 0 : 1;
221 break;
222 case Channel::V:
223 _input = input->plane(1);
224 num_elems_processed_per_iteration = 16;
225 _func = &NEChannelExtractKernel::extract_1C_from_2C_img;
226 _lut_index = (Format::NV12 == format) ? 1 : 0;
227 break;
228 default:
229 ARM_COMPUTE_ERROR("Not supported channel for this format.");
230 break;
231 }
232 break;
233 case Format::IYUV:
234 case Format::YUV444:
235 _func = &NEChannelExtractKernel::copy_plane;
236 switch(channel)
237 {
238 case Channel::Y:
239 _input = input->plane(0);
240 break;
241 case Channel::U:
242 _input = input->plane(1);
243 break;
244 case Channel::V:
245 _input = input->plane(2);
246 break;
247 default:
248 ARM_COMPUTE_ERROR("Not supported channel for this format.");
249 break;
250 }
251 break;
252 default:
253 ARM_COMPUTE_ERROR("Not supported format.");
254 break;
255 }
256
257 _output = output;
258 Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100259 AccessWindowHorizontal input_access(_input->info(), 0, num_elems_processed_per_iteration);
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100260 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261 update_window_and_padding(win, input_access, output_access);
262 output_access.set_valid_region(win, _input->info()->valid_region());
263
264 INEKernel::configure(win);
265}
266
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100267void NEChannelExtractKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100268{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100269 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
271 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
272 ARM_COMPUTE_ERROR_ON(_func == nullptr);
273
274 (this->*_func)(window);
275}
276
277void NEChannelExtractKernel::extract_1C_from_2C_img(const Window &win)
278{
279 Iterator in(_input, win);
280 Iterator out(_output, win);
281
282 execute_window_loop(win, [&](const Coordinates & id)
283 {
284 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
285 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
286 const auto pixels = vld2q_u8(in_ptr);
287 vst1q_u8(out_ptr, pixels.val[_lut_index]);
288 },
289 in, out);
290}
291
292void NEChannelExtractKernel::extract_1C_from_3C_img(const Window &win)
293{
294 Iterator in(_input, win);
295 Iterator out(_output, win);
296
297 execute_window_loop(win, [&](const Coordinates & id)
298 {
299 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
300 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
301 const auto pixels = vld3q_u8(in_ptr);
302 vst1q_u8(out_ptr, pixels.val[_lut_index]);
303 },
304 in, out);
305}
306
307void NEChannelExtractKernel::extract_1C_from_4C_img(const Window &win)
308{
309 Iterator in(_input, win);
310 Iterator out(_output, win);
311
312 execute_window_loop(win, [&](const Coordinates & id)
313 {
314 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
315 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
316 const auto pixels = vld4q_u8(in_ptr);
317 vst1q_u8(out_ptr, pixels.val[_lut_index]);
318 },
319 in, out);
320}
321
322void NEChannelExtractKernel::extract_YUYV_uv(const Window &win)
323{
324 ARM_COMPUTE_ERROR_ON(win.x().step() % 2);
325
326 Window win_out(win);
327 win_out.set_dimension_step(Window::DimX, win.x().step() / 2);
328
329 Iterator in(_input, win);
330 Iterator out(_output, win_out);
331
332 execute_window_loop(win, [&](const Coordinates & id)
333 {
334 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
335 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
336 const auto pixels = vld4q_u8(in_ptr);
337 vst1q_u8(out_ptr, pixels.val[_lut_index]);
338 },
339 in, out);
340}
341
342void NEChannelExtractKernel::copy_plane(const Window &win)
343{
344 Iterator in(_input, win);
345 Iterator out(_output, win);
346
347 execute_window_loop(win, [&](const Coordinates &)
348 {
349 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
350 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
351 vst4_u8(out_ptr, vld4_u8(in_ptr));
352 },
353 in, out);
354}