blob: 13ae8f5ef4543bf3bee969c623528f31245f313e [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"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/MultiImageInfo.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Utils.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
39
40#include <set>
41#include <string>
42
43using namespace arm_compute;
44
45CLChannelExtractKernel::CLChannelExtractKernel()
46 : _input(nullptr), _output(nullptr), _num_elems_processed_per_iteration(8), _subsampling(1)
47{
48}
49
50void CLChannelExtractKernel::configure(const ICLTensor *input, Channel channel, ICLTensor *output)
51{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010052 configure(CLKernelLibrary::get().get_compile_context(), input, channel, output);
53}
54
Manuel Bottini256c0b92020-04-21 13:29:30 +010055void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, Channel channel, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010056{
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010057 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
58 ARM_COMPUTE_ERROR_ON(input == output);
59
60 set_format_if_unknown(*output->info(), Format::U8);
61
62 // Check if input tensor has a valid format
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422);
64 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010065 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
66
67 // Check if channel is valid for given format
68 const Format format = input->info()->format();
69 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
70
71 // Half the processed elements for U,V channels due to sub-sampling of 2
72 _subsampling = 1;
73
74 if(format == Format::YUYV422 || format == Format::UYVY422)
75 {
76 // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422)
77 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input);
78
79 if(channel != Channel::Y)
80 {
81 _subsampling = 2;
82 }
83 }
84
85 // Calculate output tensor shape using subsampling
86 TensorShape output_shape = calculate_subsampled_shape(input->info()->tensor_shape(), format, channel);
87 set_shape_if_empty(*output->info(), output_shape);
88
89 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090
91 _input = input;
92 _output = output;
93
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 // Create kernel
95 std::string kernel_name = "channel_extract_" + string_from_format(format);
96 std::set<std::string> build_opts = { ("-DCHANNEL_" + string_from_channel(channel)) };
Manuel Bottini4c6bd512020-04-08 10:15:51 +010097 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 // Configure window
100 Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
101 AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration);
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100102 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 +0100103
104 update_window_and_padding(win, input_access, output_access);
105
106 ValidRegion input_valid_region = input->info()->valid_region();
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100107 output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100109 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110}
111
112void CLChannelExtractKernel::configure(const ICLMultiImage *input, Channel channel, ICLImage *output)
113{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100114 configure(CLKernelLibrary::get().get_compile_context(), input, channel, output);
115}
116
Manuel Bottini256c0b92020-04-21 13:29:30 +0100117void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLMultiImage *input, Channel channel, ICLImage *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100118{
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100119 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100121
122 set_format_if_unknown(*output->info(), Format::U8);
123
124 // Check if channel is valid for given format
125 const Format format = input->info()->format();
126 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
127
128 // Get input plane from the given channel
129 const ICLImage *input_plane = input->cl_plane(plane_idx_from_channel(format, channel));
130 ARM_COMPUTE_ERROR_ON_NULLPTR(input_plane);
131
132 if(Channel::Y == channel && format != Format::YUV444)
133 {
134 // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422)
135 ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input_plane);
136 }
137
138 // Calculate 2x2 subsampled tensor shape
139 TensorShape output_shape = calculate_subsampled_shape(input->cl_plane(0)->info()->tensor_shape(), format, channel);
140 set_shape_if_empty(*output->info(), output_shape);
141
142 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape());
143
144 // Check if input tensor has a valid format
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
146 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
148 _output = output;
149 _input = input_plane;
150 _subsampling = 1;
151
152 // Create kernel
153 std::string kernel_name;
154 std::set<std::string> build_opts;
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100155 if(Channel::Y == channel || Format::IYUV == format || Format::YUV444 == format)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 {
157 kernel_name = "copy_plane";
158 }
159 else
160 {
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +0100161 kernel_name = "channel_extract_" + string_from_format(format);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 build_opts.insert(("-DCHANNEL_" + string_from_channel(channel)));
163 }
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100164 _kernel = create_kernel(compile_context, kernel_name, build_opts);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165
166 // Configure window
167 Window win = calculate_max_window(*input_plane->info(), Steps(_num_elems_processed_per_iteration));
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100168 AccessWindowHorizontal input_access(input_plane->info(), 0, _num_elems_processed_per_iteration);
169 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100171 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172
173 output_access.set_valid_region(win, input_plane->info()->valid_region());
174
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100175 ICLKernel::configure_internal(win);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176}
177
178void CLChannelExtractKernel::run(const Window &window, cl::CommandQueue &queue)
179{
180 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
181 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
182
183 Window slice = window.first_slice_window_2D();
184
185 do
186 {
187 Window win_sub(slice);
188 win_sub.set(Window::DimX, Window::Dimension(win_sub.x().start() / _subsampling, win_sub.x().end() / _subsampling, win_sub.x().step() / _subsampling));
189 win_sub.set(Window::DimY, Window::Dimension(win_sub.y().start() / _subsampling, win_sub.y().end() / _subsampling, 1));
190
191 unsigned int idx = 0;
192 add_2D_tensor_argument(idx, _input, slice);
193 add_2D_tensor_argument(idx, _output, win_sub);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100194 enqueue(queue, *this, slice, lws_hint());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 }
196 while(window.slide_window_slice_2D(slice));
197}