blob: 7911b948ae9abfca5d94883ec82704ede0b3d7c9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +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/CLChannelExtractKernel.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/Coordinates.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/MultiImageInfo.h"
33#include "arm_compute/core/TensorInfo.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39#include <set>
40#include <string>
41
42using namespace arm_compute;
43
44CLChannelExtractKernel::CLChannelExtractKernel()
45 : _input(nullptr), _output(nullptr), _num_elems_processed_per_iteration(8), _subsampling(1)
46{
47}
48
49void CLChannelExtractKernel::configure(const ICLTensor *input, Channel channel, ICLTensor *output)
50{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010051 configure(CLKernelLibrary::get().get_compile_context(), input, channel, output);
52}
53
Manuel Bottini256c0b92020-04-21 13:29:30 +010054void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, Channel channel, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010055{
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010056 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
57 ARM_COMPUTE_ERROR_ON(input == output);
58
59 set_format_if_unknown(*output->info(), Format::U8);
60
61 // Check if input tensor has a valid format
Anthony Barbier6ff3b192017-09-04 18:44:23 +010062 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422);
63 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010064 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
65
66 // Check if channel is valid for given format
67 const Format format = input->info()->format();
68 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
69
70 // Half the processed elements for U,V channels due to sub-sampling of 2
71 _subsampling = 1;
72
73 if(format == Format::YUYV422 || format == Format::UYVY422)
74 {
75 // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422)
76 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input);
77
78 if(channel != Channel::Y)
79 {
80 _subsampling = 2;
81 }
82 }
83
84 // Calculate output tensor shape using subsampling
85 TensorShape output_shape = calculate_subsampled_shape(input->info()->tensor_shape(), format, channel);
86 set_shape_if_empty(*output->info(), output_shape);
87
88 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 _input = input;
91 _output = output;
92
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 // Create kernel
94 std::string kernel_name = "channel_extract_" + string_from_format(format);
95 std::set<std::string> build_opts = { ("-DCHANNEL_" + string_from_channel(channel)) };
Manuel Bottini4c6bd512020-04-08 10:15:51 +010096 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 // Configure window
99 Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
100 AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration);
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100101 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 +0100102
103 update_window_and_padding(win, input_access, output_access);
104
105 ValidRegion input_valid_region = input->info()->valid_region();
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100106 output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100108 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109}
110
111void CLChannelExtractKernel::configure(const ICLMultiImage *input, Channel channel, ICLImage *output)
112{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100113 configure(CLKernelLibrary::get().get_compile_context(), input, channel, output);
114}
115
Manuel Bottini256c0b92020-04-21 13:29:30 +0100116void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLMultiImage *input, Channel channel, ICLImage *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100117{
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100118 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100120
121 set_format_if_unknown(*output->info(), Format::U8);
122
123 // Check if channel is valid for given format
124 const Format format = input->info()->format();
125 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
126
127 // Get input plane from the given channel
128 const ICLImage *input_plane = input->cl_plane(plane_idx_from_channel(format, channel));
129 ARM_COMPUTE_ERROR_ON_NULLPTR(input_plane);
130
131 if(Channel::Y == channel && format != Format::YUV444)
132 {
133 // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422)
134 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input_plane);
135 }
136
137 // Calculate 2x2 subsampled tensor shape
138 TensorShape output_shape = calculate_subsampled_shape(input->cl_plane(0)->info()->tensor_shape(), format, channel);
139 set_shape_if_empty(*output->info(), output_shape);
140
141 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape());
142
143 // Check if input tensor has a valid format
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
145 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147 _output = output;
148 _input = input_plane;
149 _subsampling = 1;
150
151 // Create kernel
152 std::string kernel_name;
153 std::set<std::string> build_opts;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100154 if(Channel::Y == channel || Format::IYUV == format || Format::YUV444 == format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 {
156 kernel_name = "copy_plane";
157 }
158 else
159 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100160 kernel_name = "channel_extract_" + string_from_format(format);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 build_opts.insert(("-DCHANNEL_" + string_from_channel(channel)));
162 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100163 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164
165 // Configure window
166 Window win = calculate_max_window(*input_plane->info(), Steps(_num_elems_processed_per_iteration));
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100167 AccessWindowHorizontal input_access(input_plane->info(), 0, _num_elems_processed_per_iteration);
168 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100170 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171
172 output_access.set_valid_region(win, input_plane->info()->valid_region());
173
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100174 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175}
176
177void CLChannelExtractKernel::run(const Window &window, cl::CommandQueue &queue)
178{
179 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
180 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
181
182 Window slice = window.first_slice_window_2D();
183
184 do
185 {
186 Window win_sub(slice);
187 win_sub.set(Window::DimX, Window::Dimension(win_sub.x().start() / _subsampling, win_sub.x().end() / _subsampling, win_sub.x().step() / _subsampling));
188 win_sub.set(Window::DimY, Window::Dimension(win_sub.y().start() / _subsampling, win_sub.y().end() / _subsampling, 1));
189
190 unsigned int idx = 0;
191 add_2D_tensor_argument(idx, _input, slice);
192 add_2D_tensor_argument(idx, _output, win_sub);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100193 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 }
195 while(window.slide_window_slice_2D(slice));
196}