blob: 3147a698adff8220414e1c67ab0d7193784fbdec [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
287void NEChannelCombineKernel::run(const Window &window)
288{
289 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
290 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
291 ARM_COMPUTE_ERROR_ON(_func == nullptr);
292
293 (this->*_func)(window);
294}
295
296void NEChannelCombineKernel::combine_3C(const Window &win)
297{
298 Iterator p0(_planes[0], win);
299 Iterator p1(_planes[1], win);
300 Iterator p2(_planes[2], win);
301 Iterator out(_output, win);
302
303 execute_window_loop(win, [&](const Coordinates & id)
304 {
305 const auto p0_ptr = static_cast<uint8_t *>(p0.ptr());
306 const auto p1_ptr = static_cast<uint8_t *>(p1.ptr());
307 const auto p2_ptr = static_cast<uint8_t *>(p2.ptr());
308 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
309
310 const uint8x8x3_t pixels =
311 {
312 {
313 vld1_u8(p0_ptr),
314 vld1_u8(p1_ptr),
315 vld1_u8(p2_ptr)
316 }
317 };
318
319 vst3_u8(out_ptr, pixels);
320 },
321 p0, p1, p2, out);
322}
323
324void NEChannelCombineKernel::combine_4C(const Window &win)
325{
326 Iterator p0(_planes[0], win);
327 Iterator p1(_planes[1], win);
328 Iterator p2(_planes[2], win);
329 Iterator p3(_planes[3], win);
330 Iterator out(_output, win);
331
332 execute_window_loop(win, [&](const Coordinates & id)
333 {
334 const auto p0_ptr = static_cast<uint8_t *>(p0.ptr());
335 const auto p1_ptr = static_cast<uint8_t *>(p1.ptr());
336 const auto p2_ptr = static_cast<uint8_t *>(p2.ptr());
337 const auto p3_ptr = static_cast<uint8_t *>(p3.ptr());
338 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
339
340 const uint8x8x4_t pixels =
341 {
342 {
343 vld1_u8(p0_ptr),
344 vld1_u8(p1_ptr),
345 vld1_u8(p2_ptr),
346 vld1_u8(p3_ptr)
347 }
348 };
349
350 vst4_u8(out_ptr, pixels);
351 },
352 p0, p1, p2, p3, out);
353}
354
355template <bool is_uyvy>
356void NEChannelCombineKernel::combine_YUV_1p(const Window &win)
357{
358 // Create sub-sampled uv window and init uv planes
359 Window win_uv(win);
360 win_uv.set_dimension_step(0, win.x().step() / _x_subsampling[1]);
361 win_uv.validate();
362
363 Iterator p0(_planes[0], win);
364 Iterator p1(_planes[1], win_uv);
365 Iterator p2(_planes[2], win_uv);
366 Iterator out(_output, win);
367
368 constexpr auto shift = is_uyvy ? 1 : 0;
369
370 execute_window_loop(win, [&](const Coordinates & id)
371 {
372 const auto p0_ptr = static_cast<uint8_t *>(p0.ptr());
373 const auto p1_ptr = static_cast<uint8_t *>(p1.ptr());
374 const auto p2_ptr = static_cast<uint8_t *>(p2.ptr());
375 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
376
377 const uint8x8x2_t pixels_y = vld2_u8(p0_ptr);
378 const uint8x8x2_t pixels_uv =
379 {
380 {
381 vld1_u8(p1_ptr),
382 vld1_u8(p2_ptr)
383 }
384 };
385
386 uint8x8x4_t pixels{ {} };
387 pixels.val[0 + shift] = pixels_y.val[0];
388 pixels.val[1 - shift] = pixels_uv.val[0];
389 pixels.val[2 + shift] = pixels_y.val[1];
390 pixels.val[3 - shift] = pixels_uv.val[1];
391
392 vst4_u8(out_ptr, pixels);
393 },
394 p0, p1, p2, out);
395}
396
397void NEChannelCombineKernel::combine_YUV_2p(const Window &win)
398{
399 ARM_COMPUTE_ERROR_ON(win.x().start() % _x_subsampling[1]);
400 ARM_COMPUTE_ERROR_ON(win.y().start() % _y_subsampling[1]);
401
402 // Copy first plane
403 copy_plane(win, 0);
404
405 // Update UV window
406 Window uv_win(win);
407 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));
408 uv_win.set(Window::DimY, Window::Dimension(uv_win.y().start() / _y_subsampling[1], uv_win.y().end() / _y_subsampling[1], 1));
409 uv_win.validate();
410
411 // Update output win
412 Window out_win(win);
413 out_win.set(Window::DimX, Window::Dimension(out_win.x().start(), out_win.x().end(), out_win.x().step() * 2));
414 out_win.set(Window::DimY, Window::Dimension(out_win.y().start() / _y_subsampling[1], out_win.y().end() / _y_subsampling[1], 1));
415 out_win.validate();
416
417 // Construct second plane
418 const int shift = (Format::NV12 == _output_multi->info()->format()) ? 0 : 1;
419 Iterator p1(_planes[1 + shift], uv_win);
420 Iterator p2(_planes[2 - shift], uv_win);
421 Iterator out(_output_multi->plane(1), out_win);
422
423 execute_window_loop(out_win, [&](const Coordinates & id)
424 {
425 const uint8x8x2_t pixels =
426 {
427 {
428 vld1_u8(p1.ptr()),
429 vld1_u8(p2.ptr())
430 }
431 };
432
433 vst2_u8(out.ptr(), pixels);
434 },
435 p1, p2, out);
436}
437
438void NEChannelCombineKernel::combine_YUV_3p(const Window &win)
439{
440 copy_plane(win, 0);
441 copy_plane(win, 1);
442 copy_plane(win, 2);
443}
444
445void NEChannelCombineKernel::copy_plane(const Window &win, uint32_t plane_id)
446{
447 ARM_COMPUTE_ERROR_ON(win.x().start() % _x_subsampling[plane_id]);
448 ARM_COMPUTE_ERROR_ON(win.y().start() % _y_subsampling[plane_id]);
449
450 // Update window
451 Window tmp_win(win);
452 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));
453 tmp_win.set(Window::DimY, Window::Dimension(tmp_win.y().start() / _y_subsampling[plane_id], tmp_win.y().end() / _y_subsampling[plane_id], 1));
454 tmp_win.validate();
455
456 Iterator in(_planes[plane_id], tmp_win);
457 Iterator out(_output_multi->plane(plane_id), tmp_win);
458
459 execute_window_loop(tmp_win, [&](const Coordinates & id)
460 {
461 const auto in_ptr = static_cast<uint8_t *>(in.ptr());
462 const auto out_ptr = static_cast<uint8_t *>(out.ptr());
463
464 vst1_u8(out_ptr, vld1_u8(in_ptr));
465 },
466 in, out);
467}