blob: a2b24de0b4193c0fa3ff6532322edd7b0b8873d9 [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/NEChannelCombineKernel.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/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <arm_neon.h>
38
39using namespace arm_compute;
40
41namespace arm_compute
42{
43class Coordinates;
44} // namespace arm_compute
45
46NEChannelCombineKernel::NEChannelCombineKernel()
47 : _func(nullptr), _planes{ { nullptr } }, _output(nullptr), _output_multi(nullptr), _x_subsampling{ { 1, 1, 1 } }, _y_subsampling{ { 1, 1, 1 } }, _num_elems_processed_per_iteration(8),
48_is_parallelizable(true)
49{
50}
51
52void NEChannelCombineKernel::configure(const ITensor *plane0, const ITensor *plane1, const ITensor *plane2, const ITensor *plane3, ITensor *output)
53{
54 ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output);
55 ARM_COMPUTE_ERROR_ON(plane0 == output);
56 ARM_COMPUTE_ERROR_ON(plane1 == output);
57 ARM_COMPUTE_ERROR_ON(plane2 == output);
58
59 set_format_if_unknown(*plane0->info(), Format::U8);
60 set_format_if_unknown(*plane1->info(), Format::U8);
61 set_format_if_unknown(*plane2->info(), Format::U8);
62
63 if(plane3 != nullptr)
64 {
65 set_format_if_unknown(*plane3->info(), Format::U8);
66 }
67
68 set_shape_if_empty(*output->info(), plane0->info()->tensor_shape());
69
70 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8);
71 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8);
72 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8);
73 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::RGB888, Format::RGBA8888, Format::UYVY422, Format::YUYV422);
74 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(plane0, plane1, plane2);
75
76 if(plane3 != nullptr)
77 {
78 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(plane0, plane3);
79 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(plane0, plane3);
80 }
81
82 const Format &output_format = output->info()->format();
83
84 if(output_format == Format::RGBA8888)
85 {
86 ARM_COMPUTE_ERROR_ON(plane3 == output);
87 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane3, 1, DataType::U8);
88 }
89
90 _planes[0] = plane0;
91 _planes[1] = plane1;
92 _planes[2] = plane2;
93 _planes[3] = plane3;
94 _output = output;
95 _output_multi = nullptr;
96
97 _num_elems_processed_per_iteration = 8;
98 _is_parallelizable = true;
99
100 switch(output_format)
101 {
102 case Format::RGB888:
103 _func = &NEChannelCombineKernel::combine_3C;
104 break;
105 case Format::RGBA8888:
106 _func = &NEChannelCombineKernel::combine_4C;
107 break;
108 case Format::UYVY422:
109 _x_subsampling[1] = 2;
110 _x_subsampling[2] = 2;
111 _num_elems_processed_per_iteration = 16;
112 _func = &NEChannelCombineKernel::combine_YUV_1p<true>;
113 break;
114 case Format::YUYV422:
115 _x_subsampling[1] = 2;
116 _x_subsampling[2] = 2;
117 _num_elems_processed_per_iteration = 16;
118 _func = &NEChannelCombineKernel::combine_YUV_1p<false>;
119 break;
120 default:
121 ARM_COMPUTE_ERROR("Not supported format.");
122 break;
123 }
124
125 TensorShape subsampled_shape_plane1{ plane0->info()->tensor_shape() };
126 subsampled_shape_plane1.set(0, subsampled_shape_plane1[0] / _x_subsampling[1]);
127 TensorShape subsampled_shape_plane2{ plane0->info()->tensor_shape() };
128 subsampled_shape_plane2.set(0, subsampled_shape_plane2[0] / _x_subsampling[2]);
129
130 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(plane1->info()->tensor_shape(), subsampled_shape_plane1);
131 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(plane2->info()->tensor_shape(), subsampled_shape_plane2);
132
133 Window win = calculate_max_window(*plane0->info(), Steps(_num_elems_processed_per_iteration));
134
135 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
136 AccessWindowHorizontal plane0_access(plane0->info(), 0, _num_elems_processed_per_iteration / _x_subsampling[1], 1.f / _x_subsampling[0]);
137 AccessWindowHorizontal plane1_access(plane1->info(), 0, _num_elems_processed_per_iteration / _x_subsampling[1], 1.f / _x_subsampling[1]);
138 AccessWindowHorizontal plane2_access(plane2->info(), 0, _num_elems_processed_per_iteration / _x_subsampling[1], 1.f / _x_subsampling[2]);
139 AccessWindowHorizontal plane3_access(plane3 == nullptr ? nullptr : plane3->info(), 0, _num_elems_processed_per_iteration);
140
141 update_window_and_padding(
142 win,
143 plane0_access,
144 plane1_access,
145 plane2_access,
146 plane3_access,
147 output_access);
148
149 ValidRegion valid_region = intersect_valid_regions(plane0->info()->valid_region(),
150 plane1->info()->valid_region(),
151 plane2->info()->valid_region());
152
153 if(plane3 != nullptr)
154 {
155 valid_region = intersect_valid_regions(plane3->info()->valid_region(), valid_region);
156 }
157
158 output_access.set_valid_region(win, ValidRegion(valid_region.anchor, output->info()->tensor_shape()));
159
160 INEKernel::configure(win);
161}
162
163void NEChannelCombineKernel::configure(const IImage *plane0, const IImage *plane1, const IImage *plane2, IMultiImage *output)
164{
165 ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output);
166 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0);
167 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1);
168 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2);
169
170 set_format_if_unknown(*plane0->info(), Format::U8);
171 set_format_if_unknown(*plane1->info(), Format::U8);
172 set_format_if_unknown(*plane2->info(), Format::U8);
173
174 set_shape_if_empty(*output->plane(0)->info(), plane0->info()->tensor_shape());
175
176 switch(output->info()->format())
177 {
178 case Format::NV12:
179 case Format::NV21:
180 case Format::IYUV:
181 {
182 TensorShape subsampled_shape = plane0->info()->tensor_shape();
183 subsampled_shape.set(0, subsampled_shape[0] / 2);
184 subsampled_shape.set(1, subsampled_shape[1] / 2);
185
186 set_shape_if_empty(*output->plane(1)->info(), subsampled_shape);
187
188 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->plane(1)->info()->tensor_shape(), subsampled_shape);
189
190 if(output->info()->format() == Format::IYUV)
191 {
192 set_shape_if_empty(*output->plane(2)->info(), subsampled_shape);
193
194 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->plane(2)->info()->tensor_shape(), subsampled_shape);
195 }
196 break;
197 }
198 case Format::YUV444:
199 set_shape_if_empty(*output->plane(1)->info(), plane0->info()->tensor_shape());
200 set_shape_if_empty(*output->plane(2)->info(), plane0->info()->tensor_shape());
201
202 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(plane1, plane2, output->plane(1), output->plane(2));
203 break;
204 default:
205 ARM_COMPUTE_ERROR("Unsupported format");
206 }
207
208 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(plane0, output->plane(0));
209 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8);
210 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8);
211 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8);
212 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
213 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(plane0, plane1, plane2);
214
215 _planes[0] = plane0;
216 _planes[1] = plane1;
217 _planes[2] = plane2;
218 _planes[3] = nullptr;
219 _output = nullptr;
220 _output_multi = output;
221 bool has_two_planes = false;
222 unsigned int num_elems_written_plane1 = 8;
223
224 _num_elems_processed_per_iteration = 8;
225 _is_parallelizable = true;
226
227 const Format &output_format = output->info()->format();
228
229 switch(output_format)
230 {
231 case Format::NV12:
232 case Format::NV21:
233 _x_subsampling = { { 1, 2, 2 } };
234 _y_subsampling = { { 1, 2, 2 } };
235 _func = &NEChannelCombineKernel::combine_YUV_2p;
236 has_two_planes = true;
237 num_elems_written_plane1 = 16;
238 break;
239 case Format::IYUV:
240 _is_parallelizable = false;
241 _x_subsampling = { { 1, 2, 2 } };
242 _y_subsampling = { { 1, 2, 2 } };
243 _func = &NEChannelCombineKernel::combine_YUV_3p;
244 break;
245 case Format::YUV444:
246 _is_parallelizable = false;
247 _x_subsampling = { { 1, 1, 1 } };
248 _y_subsampling = { { 1, 1, 1 } };
249 _func = &NEChannelCombineKernel::combine_YUV_3p;
250 break;
251 default:
252 ARM_COMPUTE_ERROR("Not supported format.");
253 break;
254 }
255
256 const unsigned int y_step = *std::max_element(_y_subsampling.begin(), _y_subsampling.end());
257
258 Window win = calculate_max_window(*plane0->info(), Steps(_num_elems_processed_per_iteration, y_step));
259 AccessWindowRectangle output_plane0_access(output->plane(0)->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f, 1.f / _y_subsampling[0]);
260 AccessWindowRectangle output_plane1_access(output->plane(1)->info(), 0, 0, num_elems_written_plane1, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]);
261 AccessWindowRectangle output_plane2_access(has_two_planes ? nullptr : output->plane(2)->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]);
262
263 update_window_and_padding(win,
264 AccessWindowHorizontal(plane0->info(), 0, _num_elems_processed_per_iteration),
265 AccessWindowRectangle(plane1->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]),
266 AccessWindowRectangle(plane2->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]),
267 output_plane0_access,
268 output_plane1_access,
269 output_plane2_access);
270
271 ValidRegion plane0_valid_region = plane0->info()->valid_region();
272
273 ValidRegion output_plane1_region = has_two_planes ? intersect_valid_regions(plane1->info()->valid_region(), plane2->info()->valid_region()) : plane2->info()->valid_region();
274
275 output_plane0_access.set_valid_region(win, ValidRegion(plane0_valid_region.anchor, output->plane(0)->info()->tensor_shape()));
276 output_plane1_access.set_valid_region(win, ValidRegion(output_plane1_region.anchor, output->plane(1)->info()->tensor_shape()));
277 output_plane2_access.set_valid_region(win, ValidRegion(plane2->info()->valid_region().anchor, output->plane(2)->info()->tensor_shape()));
278
279 INEKernel::configure(win);
280}
281
282bool NEChannelCombineKernel::is_parallelisable() const
283{
284 return _is_parallelizable;
285}
286
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100287void NEChannelCombineKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100289 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
291 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
292 ARM_COMPUTE_ERROR_ON(_func == nullptr);
293
294 (this->*_func)(window);
295}
296
297void NEChannelCombineKernel::combine_3C(const Window &win)
298{
299 Iterator p0(_planes[0], win);
300 Iterator p1(_planes[1], win);
301 Iterator p2(_planes[2], win);
302 Iterator out(_output, win);
303
304 execute_window_loop(win, [&](const Coordinates & id)
305 {
306 const auto p0_ptr = static_cast<uint8_t *>(p0.ptr());
307 const auto p1_ptr = static_cast<uint8_t *>(p1.ptr());
308 const auto p2_ptr = static_cast<uint8_t *>(p2.ptr());
309 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
310
311 const uint8x8x3_t pixels =
312 {
313 {
314 vld1_u8(p0_ptr),
315 vld1_u8(p1_ptr),
316 vld1_u8(p2_ptr)
317 }
318 };
319
320 vst3_u8(out_ptr, pixels);
321 },
322 p0, p1, p2, out);
323}
324
325void NEChannelCombineKernel::combine_4C(const Window &win)
326{
327 Iterator p0(_planes[0], win);
328 Iterator p1(_planes[1], win);
329 Iterator p2(_planes[2], win);
330 Iterator p3(_planes[3], win);
331 Iterator out(_output, win);
332
333 execute_window_loop(win, [&](const Coordinates & id)
334 {
335 const auto p0_ptr = static_cast<uint8_t *>(p0.ptr());
336 const auto p1_ptr = static_cast<uint8_t *>(p1.ptr());
337 const auto p2_ptr = static_cast<uint8_t *>(p2.ptr());
338 const auto p3_ptr = static_cast<uint8_t *>(p3.ptr());
339 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
340
341 const uint8x8x4_t pixels =
342 {
343 {
344 vld1_u8(p0_ptr),
345 vld1_u8(p1_ptr),
346 vld1_u8(p2_ptr),
347 vld1_u8(p3_ptr)
348 }
349 };
350
351 vst4_u8(out_ptr, pixels);
352 },
353 p0, p1, p2, p3, out);
354}
355
356template <bool is_uyvy>
357void NEChannelCombineKernel::combine_YUV_1p(const Window &win)
358{
359 // Create sub-sampled uv window and init uv planes
360 Window win_uv(win);
361 win_uv.set_dimension_step(0, win.x().step() / _x_subsampling[1]);
362 win_uv.validate();
363
364 Iterator p0(_planes[0], win);
365 Iterator p1(_planes[1], win_uv);
366 Iterator p2(_planes[2], win_uv);
367 Iterator out(_output, win);
368
369 constexpr auto shift = is_uyvy ? 1 : 0;
370
371 execute_window_loop(win, [&](const Coordinates & id)
372 {
373 const auto p0_ptr = static_cast<uint8_t *>(p0.ptr());
374 const auto p1_ptr = static_cast<uint8_t *>(p1.ptr());
375 const auto p2_ptr = static_cast<uint8_t *>(p2.ptr());
376 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
377
378 const uint8x8x2_t pixels_y = vld2_u8(p0_ptr);
379 const uint8x8x2_t pixels_uv =
380 {
381 {
382 vld1_u8(p1_ptr),
383 vld1_u8(p2_ptr)
384 }
385 };
386
387 uint8x8x4_t pixels{ {} };
388 pixels.val[0 + shift] = pixels_y.val[0];
389 pixels.val[1 - shift] = pixels_uv.val[0];
390 pixels.val[2 + shift] = pixels_y.val[1];
391 pixels.val[3 - shift] = pixels_uv.val[1];
392
393 vst4_u8(out_ptr, pixels);
394 },
395 p0, p1, p2, out);
396}
397
398void NEChannelCombineKernel::combine_YUV_2p(const Window &win)
399{
400 ARM_COMPUTE_ERROR_ON(win.x().start() % _x_subsampling[1]);
401 ARM_COMPUTE_ERROR_ON(win.y().start() % _y_subsampling[1]);
402
403 // Copy first plane
404 copy_plane(win, 0);
405
406 // Update UV window
407 Window uv_win(win);
408 uv_win.set(Window::DimX, Window::Dimension(uv_win.x().start() / _x_subsampling[1], uv_win.x().end() / _x_subsampling[1], _num_elems_processed_per_iteration));
409 uv_win.set(Window::DimY, Window::Dimension(uv_win.y().start() / _y_subsampling[1], uv_win.y().end() / _y_subsampling[1], 1));
410 uv_win.validate();
411
412 // Update output win
413 Window out_win(win);
414 out_win.set(Window::DimX, Window::Dimension(out_win.x().start(), out_win.x().end(), out_win.x().step() * 2));
415 out_win.set(Window::DimY, Window::Dimension(out_win.y().start() / _y_subsampling[1], out_win.y().end() / _y_subsampling[1], 1));
416 out_win.validate();
417
418 // Construct second plane
419 const int shift = (Format::NV12 == _output_multi->info()->format()) ? 0 : 1;
420 Iterator p1(_planes[1 + shift], uv_win);
421 Iterator p2(_planes[2 - shift], uv_win);
422 Iterator out(_output_multi->plane(1), out_win);
423
424 execute_window_loop(out_win, [&](const Coordinates & id)
425 {
426 const uint8x8x2_t pixels =
427 {
428 {
429 vld1_u8(p1.ptr()),
430 vld1_u8(p2.ptr())
431 }
432 };
433
434 vst2_u8(out.ptr(), pixels);
435 },
436 p1, p2, out);
437}
438
439void NEChannelCombineKernel::combine_YUV_3p(const Window &win)
440{
441 copy_plane(win, 0);
442 copy_plane(win, 1);
443 copy_plane(win, 2);
444}
445
446void NEChannelCombineKernel::copy_plane(const Window &win, uint32_t plane_id)
447{
448 ARM_COMPUTE_ERROR_ON(win.x().start() % _x_subsampling[plane_id]);
449 ARM_COMPUTE_ERROR_ON(win.y().start() % _y_subsampling[plane_id]);
450
451 // Update window
452 Window tmp_win(win);
453 tmp_win.set(Window::DimX, Window::Dimension(tmp_win.x().start() / _x_subsampling[plane_id], tmp_win.x().end() / _x_subsampling[plane_id], _num_elems_processed_per_iteration));
454 tmp_win.set(Window::DimY, Window::Dimension(tmp_win.y().start() / _y_subsampling[plane_id], tmp_win.y().end() / _y_subsampling[plane_id], 1));
455 tmp_win.validate();
456
457 Iterator in(_planes[plane_id], tmp_win);
458 Iterator out(_output_multi->plane(plane_id), tmp_win);
459
460 execute_window_loop(tmp_win, [&](const Coordinates & id)
461 {
462 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
463 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
464
465 vst1_u8(out_ptr, vld1_u8(in_ptr));
466 },
467 in, out);
468}