blob: 90face2ccc2839771d39b5e1d0308317b662e10b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottini4c6bd512020-04-08 10:15:51 +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 */
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{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010056 configure(CLKernelLibrary::get().get_compile_context(), plane0, plane1, plane2, plane3, output);
57}
58
59void CLChannelCombineKernel::configure(CLCompileContext &compile_context, const ICLTensor *plane0, const ICLTensor *plane1, const ICLTensor *plane2, const ICLTensor *plane3, ICLTensor *output)
60{
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000061 ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output);
62 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0);
63 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1);
64 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2);
65 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
66
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8);
68 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8);
69 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8);
70 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422);
71
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000072 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8);
73 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8);
74 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8);
75
76 const Format output_format = output->info()->format();
77
78 // Check if horizontal dimension of Y plane is even and validate horizontal sub-sampling dimensions for U and V planes
79 if(Format::YUYV422 == output_format || Format::UYVY422 == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 {
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +000081 // Validate Y plane of input and output
82 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(output_format, plane0, output);
83
84 // Validate U and V plane of the input
85 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), plane1, plane2);
86 }
87
88 _planes[0] = plane0;
89 _planes[1] = plane1;
90 _planes[2] = plane2;
91 _planes[3] = nullptr;
92
93 // Validate the last input tensor only for RGBA format
94 if(Format::RGBA8888 == output_format)
95 {
96 ARM_COMPUTE_ERROR_ON_NULLPTR(plane3);
97 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane3);
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane3, Format::U8);
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000100 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane3, 1, DataType::U8);
101
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 _planes[3] = plane3;
103 }
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000104
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 _output = output;
106 _output_multi = nullptr;
107
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000108 // Half the processed elements for U and V channels due to horizontal sub-sampling of 2
109 if(Format::YUYV422 == output_format || Format::UYVY422 == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 {
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000111 _x_subsampling[1] = 2;
112 _x_subsampling[2] = 2;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 }
114
115 // Create kernel
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000116 std::string kernel_name = "channel_combine_" + string_from_format(output_format);
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100117 _kernel = create_kernel(compile_context, kernel_name);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
119 // Configure window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
121
122 AccessWindowHorizontal plane0_access(plane0->info(), 0, num_elems_processed_per_iteration);
123 AccessWindowRectangle plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]);
124 AccessWindowRectangle plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]);
125 AccessWindowHorizontal plane3_access(plane3 == nullptr ? nullptr : plane3->info(), 0, num_elems_processed_per_iteration);
126 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
127
128 update_window_and_padding(win, plane0_access, plane1_access, plane2_access, plane3_access, output_access);
129
130 ValidRegion valid_region = intersect_valid_regions(plane0->info()->valid_region(),
131 plane1->info()->valid_region(),
132 plane2->info()->valid_region());
133 if(plane3 != nullptr)
134 {
135 valid_region = intersect_valid_regions(plane3->info()->valid_region(), valid_region);
136 }
137 output_access.set_valid_region(win, ValidRegion(valid_region.anchor, output->info()->tensor_shape()));
138
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100139 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140}
141
142void CLChannelCombineKernel::configure(const ICLImage *plane0, const ICLImage *plane1, const ICLImage *plane2, ICLMultiImage *output)
143{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100144 configure(CLKernelLibrary::get().get_compile_context(), plane0, plane1, plane2, output);
145}
146
147void CLChannelCombineKernel::configure(CLCompileContext &compile_context, const ICLImage *plane0, const ICLImage *plane1, const ICLImage *plane2, ICLMultiImage *output)
148{
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000149 ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0);
151 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1);
152 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2);
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000153
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8);
155 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8);
156 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8);
157 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
158
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000159 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8);
160 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8);
161 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8);
162
163 const Format output_format = output->info()->format();
164
165 // Validate shape of Y plane to be even and shape of sub-sampling dimensions for U and V planes
166 // Perform validation only for formats which require sub-sampling.
167 if(Format::YUV444 != output_format)
168 {
169 // Validate Y plane of input and output
170 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(output_format, plane0, output->plane(0));
171
172 // Validate U and V plane of the input
173 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), plane1, plane2);
174
175 // Validate second plane U (NV12 and NV21 have a UV88 combined plane while IYUV has only the U plane)
176 // MultiImage generates the correct tensor shape but also check in case the tensor shape of planes was changed to a wrong size
177 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), output->plane(1));
178
179 // Validate the last plane V of format IYUV
180 if(Format::IYUV == output_format)
181 {
182 // Validate Y plane of the output
183 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), output->plane(2));
184 }
185 }
186
187 // Set input tensors
188 _planes[0] = plane0;
189 _planes[1] = plane1;
190 _planes[2] = plane2;
191 _planes[3] = nullptr;
192
193 // Set output tensor
194 _output = nullptr;
195 _output_multi = output;
196
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197 bool has_two_planars = false;
198
199 // Set sub-sampling parameters for each plane
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 std::string kernel_name;
201 std::set<std::string> build_opts;
202
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000203 if(Format::NV12 == output_format || Format::NV21 == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204 {
205 _x_subsampling = { { 1, 2, 2 } };
206 _y_subsampling = { { 1, 2, 2 } };
207 kernel_name = "channel_combine_NV";
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000208 build_opts.emplace(Format::NV12 == output_format ? "-DNV12" : "-DNV21");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 has_two_planars = true;
210 }
211 else
212 {
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000213 if(Format::IYUV == output_format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 {
215 _x_subsampling = { { 1, 2, 2 } };
216 _y_subsampling = { { 1, 2, 2 } };
217 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218
219 kernel_name = "copy_planes_3p";
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000220 build_opts.emplace(Format::IYUV == output_format ? "-DIYUV" : "-DYUV444");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 }
222
223 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100224 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225
226 // Configure window
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227 Window win = calculate_max_window(*plane0->info(), Steps(num_elems_processed_per_iteration));
228
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000229 AccessWindowRectangle input_plane0_access(plane0->info(), 0, 0, num_elems_processed_per_iteration, 1.f);
230 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]);
231 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]);
232 AccessWindowRectangle output_plane0_access(output->plane(0)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f, 1.f / _y_subsampling[1]);
233 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]);
234 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 +0100235
236 update_window_and_padding(win,
237 input_plane0_access, input_plane1_access, input_plane2_access,
238 output_plane0_access, output_plane1_access, output_plane2_access);
239
240 ValidRegion plane0_valid_region = plane0->info()->valid_region();
241 ValidRegion output_plane1_region = has_two_planars ? intersect_valid_regions(plane1->info()->valid_region(), plane2->info()->valid_region()) : plane2->info()->valid_region();
242 output_plane0_access.set_valid_region(win, ValidRegion(plane0_valid_region.anchor, output->plane(0)->info()->tensor_shape()));
243 output_plane1_access.set_valid_region(win, ValidRegion(output_plane1_region.anchor, output->plane(1)->info()->tensor_shape()));
244 output_plane2_access.set_valid_region(win, ValidRegion(plane2->info()->valid_region().anchor, output->plane(2)->info()->tensor_shape()));
245
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100246 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247}
248
249void CLChannelCombineKernel::run(const Window &window, cl::CommandQueue &queue)
250{
251 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
252 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
253
254 Window slice = window.first_slice_window_2D();
Ioan-Cristian Szaboae3c8ab2017-11-16 17:55:03 +0000255 slice.set_dimension_step(Window::DimY, 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100256
257 do
258 {
259 // Subsampling in plane 1
260 Window win_sub_plane1(slice);
261 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]));
262 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));
263
264 // Subsampling in plane 2
265 Window win_sub_plane2(slice);
266 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]));
267 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));
268
269 unsigned int idx = 0;
270
271 // Set inputs
272 add_2D_tensor_argument(idx, _planes[0], slice);
273 add_2D_tensor_argument(idx, _planes[1], win_sub_plane1);
274 add_2D_tensor_argument(idx, _planes[2], win_sub_plane2);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100275 add_2D_tensor_argument_if((nullptr != _planes[3]), idx, _planes[3], slice);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276
277 // Set outputs
278 if(nullptr != _output) // Single planar output
279 {
280 add_2D_tensor_argument(idx, _output, slice);
281 }
282 else // Multi-planar output
283 {
284 // Reduce slice in case of subsampling to avoid out-of bounds access
285 slice.set(Window::DimY, Window::Dimension(slice.y().start() / _y_subsampling[1], slice.y().end() / _y_subsampling[1], 1));
286
287 add_2D_tensor_argument(idx, _output_multi->cl_plane(0), slice);
288 add_2D_tensor_argument(idx, _output_multi->cl_plane(1), win_sub_plane1);
Michalis Spyroue1651a52019-07-11 15:00:49 +0100289 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 +0100290
291 _kernel.setArg(idx++, slice.y().end());
292 }
293
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100294 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 }
296 while(window.slide_window_slice_2D(slice));
297}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100298} // namespace arm_compute