blob: d029efe1104c1964184a443f42681444602e22b1 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +01002 * Copyright (c) 2016-2019 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 */
24#include "arm_compute/core/CL/kernels/CLChannelCombineKernel.h"
25
26#include "arm_compute/core/CL/CLKernelLibrary.h"
27#include "arm_compute/core/CL/ICLMultiImage.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/MultiImageInfo.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Validate.h"
37#include "arm_compute/core/Window.h"
38
39#include <set>
40#include <string>
41
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010042namespace arm_compute
43{
44namespace
45{
46constexpr unsigned int num_elems_processed_per_iteration = 16;
47} // namespace
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048
49CLChannelCombineKernel::CLChannelCombineKernel()
50 : _planes{ { nullptr } }, _output(nullptr), _output_multi(nullptr), _x_subsampling{ { 1, 1, 1 } }, _y_subsampling{ { 1, 1, 1 } }
51{
52}
53
54void CLChannelCombineKernel::configure(const ICLTensor *plane0, const ICLTensor *plane1, const ICLTensor *plane2, const ICLTensor *plane3, ICLTensor *output)
55{
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000056 ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output);
57 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0);
58 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1);
59 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2);
60 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
61
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8);
63 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8);
64 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8);
65 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422);
66
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000067 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8);
68 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8);
69 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8);
70
71 const Format output_format = output->info()->format();
72
73 // Check if horizontal dimension of Y plane is even and validate horizontal sub-sampling dimensions for U and V planes
74 if(Format::YUYV422 == output_format || Format::UYVY422 == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075 {
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000076 // Validate Y plane of input and output
77 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(output_format, plane0, output);
78
79 // Validate U and V plane of the input
80 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), plane1, plane2);
81 }
82
83 _planes[0] = plane0;
84 _planes[1] = plane1;
85 _planes[2] = plane2;
86 _planes[3] = nullptr;
87
88 // Validate the last input tensor only for RGBA format
89 if(Format::RGBA8888 == output_format)
90 {
91 ARM_COMPUTE_ERROR_ON_NULLPTR(plane3);
92 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane3);
93
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane3, Format::U8);
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000095 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane3, 1, DataType::U8);
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 _planes[3] = plane3;
98 }
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000099
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 _output = output;
101 _output_multi = nullptr;
102
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000103 // Half the processed elements for U and V channels due to horizontal sub-sampling of 2
104 if(Format::YUYV422 == output_format || Format::UYVY422 == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 {
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000106 _x_subsampling[1] = 2;
107 _x_subsampling[2] = 2;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 }
109
110 // Create kernel
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000111 std::string kernel_name = "channel_combine_" + string_from_format(output_format);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name));
113
114 // Configure window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
116
117 AccessWindowHorizontal plane0_access(plane0->info(), 0, num_elems_processed_per_iteration);
118 AccessWindowRectangle plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]);
119 AccessWindowRectangle plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]);
120 AccessWindowHorizontal plane3_access(plane3 == nullptr ? nullptr : plane3->info(), 0, num_elems_processed_per_iteration);
121 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
122
123 update_window_and_padding(win, plane0_access, plane1_access, plane2_access, plane3_access, output_access);
124
125 ValidRegion valid_region = intersect_valid_regions(plane0->info()->valid_region(),
126 plane1->info()->valid_region(),
127 plane2->info()->valid_region());
128 if(plane3 != nullptr)
129 {
130 valid_region = intersect_valid_regions(plane3->info()->valid_region(), valid_region);
131 }
132 output_access.set_valid_region(win, ValidRegion(valid_region.anchor, output->info()->tensor_shape()));
133
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100134 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135}
136
137void CLChannelCombineKernel::configure(const ICLImage *plane0, const ICLImage *plane1, const ICLImage *plane2, ICLMultiImage *output)
138{
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000139 ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0);
141 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1);
142 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2);
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000143
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8);
145 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8);
146 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8);
147 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
148
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000149 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8);
150 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8);
151 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8);
152
153 const Format output_format = output->info()->format();
154
155 // Validate shape of Y plane to be even and shape of sub-sampling dimensions for U and V planes
156 // Perform validation only for formats which require sub-sampling.
157 if(Format::YUV444 != output_format)
158 {
159 // Validate Y plane of input and output
160 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(output_format, plane0, output->plane(0));
161
162 // Validate U and V plane of the input
163 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), plane1, plane2);
164
165 // Validate second plane U (NV12 and NV21 have a UV88 combined plane while IYUV has only the U plane)
166 // MultiImage generates the correct tensor shape but also check in case the tensor shape of planes was changed to a wrong size
167 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), output->plane(1));
168
169 // Validate the last plane V of format IYUV
170 if(Format::IYUV == output_format)
171 {
172 // Validate Y plane of the output
173 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), output->plane(2));
174 }
175 }
176
177 // Set input tensors
178 _planes[0] = plane0;
179 _planes[1] = plane1;
180 _planes[2] = plane2;
181 _planes[3] = nullptr;
182
183 // Set output tensor
184 _output = nullptr;
185 _output_multi = output;
186
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 bool has_two_planars = false;
188
189 // Set sub-sampling parameters for each plane
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 std::string kernel_name;
191 std::set<std::string> build_opts;
192
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000193 if(Format::NV12 == output_format || Format::NV21 == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 {
195 _x_subsampling = { { 1, 2, 2 } };
196 _y_subsampling = { { 1, 2, 2 } };
197 kernel_name = "channel_combine_NV";
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000198 build_opts.emplace(Format::NV12 == output_format ? "-DNV12" : "-DNV21");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199 has_two_planars = true;
200 }
201 else
202 {
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000203 if(Format::IYUV == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 {
205 _x_subsampling = { { 1, 2, 2 } };
206 _y_subsampling = { { 1, 2, 2 } };
207 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100208
209 kernel_name = "copy_planes_3p";
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000210 build_opts.emplace(Format::IYUV == output_format ? "-DIYUV" : "-DYUV444");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 }
212
213 // Create kernel
214 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
215
216 // Configure window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 Window win = calculate_max_window(*plane0->info(), Steps(num_elems_processed_per_iteration));
218
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000219 AccessWindowRectangle input_plane0_access(plane0->info(), 0, 0, num_elems_processed_per_iteration, 1.f);
220 AccessWindowRectangle input_plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]);
221 AccessWindowRectangle input_plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]);
222 AccessWindowRectangle output_plane0_access(output->plane(0)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f, 1.f / _y_subsampling[1]);
223 AccessWindowRectangle output_plane1_access(output->plane(1)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]);
224 AccessWindowRectangle output_plane2_access(has_two_planars ? nullptr : output->plane(2)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225
226 update_window_and_padding(win,
227 input_plane0_access, input_plane1_access, input_plane2_access,
228 output_plane0_access, output_plane1_access, output_plane2_access);
229
230 ValidRegion plane0_valid_region = plane0->info()->valid_region();
231 ValidRegion output_plane1_region = has_two_planars ? intersect_valid_regions(plane1->info()->valid_region(), plane2->info()->valid_region()) : plane2->info()->valid_region();
232 output_plane0_access.set_valid_region(win, ValidRegion(plane0_valid_region.anchor, output->plane(0)->info()->tensor_shape()));
233 output_plane1_access.set_valid_region(win, ValidRegion(output_plane1_region.anchor, output->plane(1)->info()->tensor_shape()));
234 output_plane2_access.set_valid_region(win, ValidRegion(plane2->info()->valid_region().anchor, output->plane(2)->info()->tensor_shape()));
235
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100236 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237}
238
239void CLChannelCombineKernel::run(const Window &window, cl::CommandQueue &queue)
240{
241 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
242 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
243
244 Window slice = window.first_slice_window_2D();
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000245 slice.set_dimension_step(Window::DimY, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246
247 do
248 {
249 // Subsampling in plane 1
250 Window win_sub_plane1(slice);
251 win_sub_plane1.set(Window::DimX, Window::Dimension(win_sub_plane1.x().start() / _x_subsampling[1], win_sub_plane1.x().end() / _x_subsampling[1], win_sub_plane1.x().step() / _x_subsampling[1]));
252 win_sub_plane1.set(Window::DimY, Window::Dimension(win_sub_plane1.y().start() / _y_subsampling[1], win_sub_plane1.y().end() / _y_subsampling[1], 1));
253
254 // Subsampling in plane 2
255 Window win_sub_plane2(slice);
256 win_sub_plane2.set(Window::DimX, Window::Dimension(win_sub_plane2.x().start() / _x_subsampling[2], win_sub_plane2.x().end() / _x_subsampling[2], win_sub_plane2.x().step() / _x_subsampling[2]));
257 win_sub_plane2.set(Window::DimY, Window::Dimension(win_sub_plane2.y().start() / _y_subsampling[2], win_sub_plane2.y().end() / _y_subsampling[2], 1));
258
259 unsigned int idx = 0;
260
261 // Set inputs
262 add_2D_tensor_argument(idx, _planes[0], slice);
263 add_2D_tensor_argument(idx, _planes[1], win_sub_plane1);
264 add_2D_tensor_argument(idx, _planes[2], win_sub_plane2);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100265 add_2D_tensor_argument_if((nullptr != _planes[3]), idx, _planes[3], slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100266
267 // Set outputs
268 if(nullptr != _output) // Single planar output
269 {
270 add_2D_tensor_argument(idx, _output, slice);
271 }
272 else // Multi-planar output
273 {
274 // Reduce slice in case of subsampling to avoid out-of bounds access
275 slice.set(Window::DimY, Window::Dimension(slice.y().start() / _y_subsampling[1], slice.y().end() / _y_subsampling[1], 1));
276
277 add_2D_tensor_argument(idx, _output_multi->cl_plane(0), slice);
278 add_2D_tensor_argument(idx, _output_multi->cl_plane(1), win_sub_plane1);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100279 add_2D_tensor_argument_if((3 == num_planes_from_format(_output_multi->info()->format())), idx, _output_multi->cl_plane(2), win_sub_plane2);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100280
281 _kernel.setArg(idx++, slice.y().end());
282 }
283
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100284 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100285 }
286 while(window.slide_window_slice_2D(slice));
287}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100288} // namespace arm_compute