Compute Library
 21.02
CLChannelExtractKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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  */
25 
34 #include "arm_compute/core/Utils.h"
38 
39 #include <set>
40 #include <string>
41 
42 using namespace arm_compute;
43 
45  : _input(nullptr), _output(nullptr), _num_elems_processed_per_iteration(8), _subsampling(1)
46 {
47 }
48 
50 {
51  configure(CLKernelLibrary::get().get_compile_context(), input, channel, output);
52 }
53 
54 void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, Channel channel, ICLTensor *output)
55 {
56  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
57  ARM_COMPUTE_ERROR_ON(input == output);
58 
60 
61  // Check if input tensor has a valid format
65 
66  // Check if channel is valid for given format
67  const Format format = input->info()->format();
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)
77 
78  if(channel != Channel::Y)
79  {
80  _subsampling = 2;
81  }
82  }
83 
84  // Calculate output tensor shape using subsampling
87 
89 
90  _input = input;
91  _output = output;
92 
93  // 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)) };
96  _kernel = create_kernel(compile_context, kernel_name, build_opts);
97 
98  // 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);
101  AccessWindowRectangle output_access(output->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f / _subsampling, 1.f / _subsampling);
102 
103  update_window_and_padding(win, input_access, output_access);
104 
105  ValidRegion input_valid_region = input->info()->valid_region();
106  output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
107 
108  ICLKernel::configure_internal(win);
109 }
110 
112 {
113  configure(CLKernelLibrary::get().get_compile_context(), input, channel, output);
114 }
115 
116 void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLMultiImage *input, Channel channel, ICLImage *output)
117 {
118  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
120 
122 
123  // Check if channel is valid for given format
124  const Format format = input->info()->format();
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 
142 
143  // Check if input tensor has a valid format
146 
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;
154  if(Channel::Y == channel || Format::IYUV == format || Format::YUV444 == format)
155  {
156  kernel_name = "copy_plane";
157  }
158  else
159  {
160  kernel_name = "channel_extract_" + string_from_format(format);
161  build_opts.insert(("-DCHANNEL_" + string_from_channel(channel)));
162  }
163  _kernel = create_kernel(compile_context, kernel_name, build_opts);
164 
165  // Configure window
166  Window win = calculate_max_window(*input_plane->info(), Steps(_num_elems_processed_per_iteration));
167  AccessWindowHorizontal input_access(input_plane->info(), 0, _num_elems_processed_per_iteration);
168  AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
169 
170  update_window_and_padding(win, input_access, output_access);
171 
172  output_access.set_valid_region(win, input_plane->info()->valid_region());
173 
174  ICLKernel::configure_internal(win);
175 }
176 
177 void CLChannelExtractKernel::run(const Window &window, cl::CommandQueue &queue)
178 {
181 
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);
193  enqueue(queue, *this, slice, lws_hint());
194  }
195  while(window.slide_window_slice_2D(slice));
196 }
bool set_format_if_unknown(ITensorInfo &info, Format format)
Set the format, data type and number of channels to the specified value if the current data type is u...
Window first_slice_window_2D() const
First 2D slice of the window.
Definition: Window.h:283
A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
Shape of a tensor.
Definition: TensorShape.h:39
int plane_idx_from_channel(Format format, Channel channel)
Return the plane index of a given channel given an input format.
Definition: Utils.h:262
TensorShape calculate_subsampled_shape(const TensorShape &shape, Format format, Channel channel=Channel::UNKNOWN)
Calculate subsampled shape for a given format and channel.
Definition: Utils.h:774
void enqueue(IGCKernel &kernel, const Window &window, const gles::NDRange &lws=gles::NDRange(1U, 1U, 1U))
Add the kernel to the command queue with the given window.
Definition: IGCKernel.cpp:41
#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t)
Definition: Validate.h:856
const std::string & string_from_channel(Channel channel)
Convert a channel identity into a string.
Definition: Utils.cpp:102
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
cl::NDRange lws_hint() const
Return the Local-Workgroup-Size hint.
Definition: ICLKernel.h:276
1 channel, 1 U8 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Interface for OpenCL multi-planar images.
Definition: ICLMultiImage.h:36
A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling.
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
virtual ICLImage * cl_plane(unsigned int index)=0
Return a pointer to the requested OpenCL plane of the image.
#define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t,...)
Definition: Validate.h:643
A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling.
bool slide_window_slice_2D(Window &slice) const
Slide the passed 2D window slice.
Definition: Window.h:323
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
3 channels, 1 U8 per channel
#define ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(...)
Definition: Validate.h:318
Implementation of a rectangular access pattern.
virtual Format format() const =0
Colour format of the image.
cl::Kernel create_kernel(const CLCompileContext &ctx, const std::string &kernel_name, const std::set< std::string > &build_opts=std::set< std::string >())
Creates an opencl kernel using a compile context.
Definition: CLHelpers.cpp:403
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Channel
Available channels.
Definition: Types.h:487
Format
Image colour formats.
Definition: Types.h:54
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Implementation of a row access pattern.
std::string kernel_name
void configure(const ICLTensor *input, Channel channel, ICLTensor *output)
Set the input and output of the kernel.
A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
bool set_shape_if_empty(ITensorInfo &info, const TensorShape &shape)
Set the shape to the specified value if the current assignment is empty.
void run(const Window &window, cl::CommandQueue &queue) override
Enqueue the OpenCL kernel to process the given window on the passed OpenCL command queue...
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
virtual const MultiImageInfo * info() const =0
Interface to be implemented by the child class to return the multi-planar image&#39;s metadata...
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
CLCompileContext class.
A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
4 channels, 1 U8 per channel
void add_2D_tensor_argument(unsigned int &idx, const ICLTensor *tensor, const Window &window)
Add the passed 2D tensor&#39;s parameters to the object&#39;s kernel&#39;s arguments starting from the index idx...
Definition: ICLKernel.h:148
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:286
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
constexpr const Dimension & y() const
Alias to access the second dimension of the window.
Definition: Window.h:154
Wrapper to configure the Khronos OpenCL C++ header.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
Container for valid region of a window.
Definition: Types.h:188
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes.
Format format() const
Colour format of the image.
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:260
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
SimpleTensor< T > slice(const SimpleTensor< T > &src, Coordinates starts, Coordinates ends)
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145
const std::string & string_from_format(Format format)
Convert a tensor format into a string.
Definition: Utils.cpp:76
#define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c)
Definition: Validate.h:903