Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
| 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 | |
| 43 | using namespace arm_compute; |
| 44 | |
| 45 | CLChannelExtractKernel::CLChannelExtractKernel() |
| 46 | : _input(nullptr), _output(nullptr), _num_elems_processed_per_iteration(8), _subsampling(1) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void CLChannelExtractKernel::configure(const ICLTensor *input, Channel channel, ICLTensor *output) |
| 51 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 52 | configure(CLKernelLibrary::get().get_compile_context(), input, channel, output); |
| 53 | } |
| 54 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 55 | void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, Channel channel, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 56 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 57 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | 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 Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 65 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
| 91 | _input = input; |
| 92 | _output = output; |
| 93 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | // 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 Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 97 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | // 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 Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 102 | AccessWindowRectangle output_access(output->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f / _subsampling, 1.f / _subsampling); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
| 104 | update_window_and_padding(win, input_access, output_access); |
| 105 | |
| 106 | ValidRegion input_valid_region = input->info()->valid_region(); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 107 | output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 109 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void CLChannelExtractKernel::configure(const ICLMultiImage *input, Channel channel, ICLImage *output) |
| 113 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 114 | configure(CLKernelLibrary::get().get_compile_context(), input, channel, output); |
| 115 | } |
| 116 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 117 | void CLChannelExtractKernel::configure(const CLCompileContext &compile_context, const ICLMultiImage *input, Channel channel, ICLImage *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 118 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 119 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 121 | |
| 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 145 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | |
| 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 Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 155 | if(Channel::Y == channel || Format::IYUV == format || Format::YUV444 == format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | { |
| 157 | kernel_name = "copy_plane"; |
| 158 | } |
| 159 | else |
| 160 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 161 | kernel_name = "channel_extract_" + string_from_format(format); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | build_opts.insert(("-DCHANNEL_" + string_from_channel(channel))); |
| 163 | } |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 164 | _kernel = create_kernel(compile_context, kernel_name, build_opts); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | |
| 166 | // Configure window |
| 167 | Window win = calculate_max_window(*input_plane->info(), Steps(_num_elems_processed_per_iteration)); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 168 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 171 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | |
| 173 | output_access.set_valid_region(win, input_plane->info()->valid_region()); |
| 174 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 175 | ICLKernel::configure_internal(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void 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 Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 194 | enqueue(queue, *this, slice, lws_hint()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 195 | } |
| 196 | while(window.slide_window_slice_2D(slice)); |
| 197 | } |