Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ioan-Cristian Szabo | 91d20d9 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 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 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 53 | ARM_COMPUTE_ERROR_ON(input == output); |
| 54 | |
| 55 | set_format_if_unknown(*output->info(), Format::U8); |
| 56 | |
| 57 | // Check if input tensor has a valid format |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422); |
| 59 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
| 61 | |
| 62 | // Check if channel is valid for given format |
| 63 | const Format format = input->info()->format(); |
| 64 | ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel); |
| 65 | |
| 66 | // Half the processed elements for U,V channels due to sub-sampling of 2 |
| 67 | _subsampling = 1; |
| 68 | |
| 69 | if(format == Format::YUYV422 || format == Format::UYVY422) |
| 70 | { |
| 71 | // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422) |
| 72 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input); |
| 73 | |
| 74 | if(channel != Channel::Y) |
| 75 | { |
| 76 | _subsampling = 2; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Calculate output tensor shape using subsampling |
| 81 | TensorShape output_shape = calculate_subsampled_shape(input->info()->tensor_shape(), format, channel); |
| 82 | set_shape_if_empty(*output->info(), output_shape); |
| 83 | |
| 84 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | _input = input; |
| 87 | _output = output; |
| 88 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | // Create kernel |
| 90 | std::string kernel_name = "channel_extract_" + string_from_format(format); |
| 91 | std::set<std::string> build_opts = { ("-DCHANNEL_" + string_from_channel(channel)) }; |
| 92 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 93 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | // Configure window |
| 95 | Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration)); |
| 96 | AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 97 | 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] | 98 | |
| 99 | update_window_and_padding(win, input_access, output_access); |
| 100 | |
| 101 | ValidRegion input_valid_region = input->info()->valid_region(); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 102 | 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] | 103 | |
| 104 | ICLKernel::configure(win); |
| 105 | } |
| 106 | |
| 107 | void CLChannelExtractKernel::configure(const ICLMultiImage *input, Channel channel, ICLImage *output) |
| 108 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 109 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 111 | |
| 112 | set_format_if_unknown(*output->info(), Format::U8); |
| 113 | |
| 114 | // Check if channel is valid for given format |
| 115 | const Format format = input->info()->format(); |
| 116 | ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel); |
| 117 | |
| 118 | // Get input plane from the given channel |
| 119 | const ICLImage *input_plane = input->cl_plane(plane_idx_from_channel(format, channel)); |
| 120 | ARM_COMPUTE_ERROR_ON_NULLPTR(input_plane); |
| 121 | |
| 122 | if(Channel::Y == channel && format != Format::YUV444) |
| 123 | { |
| 124 | // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422) |
| 125 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input_plane); |
| 126 | } |
| 127 | |
| 128 | // Calculate 2x2 subsampled tensor shape |
| 129 | TensorShape output_shape = calculate_subsampled_shape(input->cl_plane(0)->info()->tensor_shape(), format, channel); |
| 130 | set_shape_if_empty(*output->info(), output_shape); |
| 131 | |
| 132 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape()); |
| 133 | |
| 134 | // Check if input tensor has a valid format |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444); |
| 136 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | |
| 138 | _output = output; |
| 139 | _input = input_plane; |
| 140 | _subsampling = 1; |
| 141 | |
| 142 | // Create kernel |
| 143 | std::string kernel_name; |
| 144 | std::set<std::string> build_opts; |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 145 | if(Channel::Y == channel || Format::IYUV == format || Format::YUV444 == format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | { |
| 147 | kernel_name = "copy_plane"; |
| 148 | } |
| 149 | else |
| 150 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 151 | kernel_name = "channel_extract_" + string_from_format(format); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 152 | build_opts.insert(("-DCHANNEL_" + string_from_channel(channel))); |
| 153 | } |
| 154 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 155 | |
| 156 | // Configure window |
| 157 | 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] | 158 | AccessWindowHorizontal input_access(input_plane->info(), 0, _num_elems_processed_per_iteration); |
| 159 | AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 160 | |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 161 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | |
| 163 | output_access.set_valid_region(win, input_plane->info()->valid_region()); |
| 164 | |
| 165 | ICLKernel::configure(win); |
| 166 | } |
| 167 | |
| 168 | void CLChannelExtractKernel::run(const Window &window, cl::CommandQueue &queue) |
| 169 | { |
| 170 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 171 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 172 | |
| 173 | Window slice = window.first_slice_window_2D(); |
| 174 | |
| 175 | do |
| 176 | { |
| 177 | Window win_sub(slice); |
| 178 | win_sub.set(Window::DimX, Window::Dimension(win_sub.x().start() / _subsampling, win_sub.x().end() / _subsampling, win_sub.x().step() / _subsampling)); |
| 179 | win_sub.set(Window::DimY, Window::Dimension(win_sub.y().start() / _subsampling, win_sub.y().end() / _subsampling, 1)); |
| 180 | |
| 181 | unsigned int idx = 0; |
| 182 | add_2D_tensor_argument(idx, _input, slice); |
| 183 | add_2D_tensor_argument(idx, _output, win_sub); |
| 184 | enqueue(queue, *this, slice); |
| 185 | } |
| 186 | while(window.slide_window_slice_2D(slice)); |
| 187 | } |