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 | { |
| 52 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422); |
| 53 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_ERROR_ON(static_cast<const void *>(input) == static_cast<void *>(output)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | |
| 56 | _input = input; |
| 57 | _output = output; |
| 58 | |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 59 | // Check format |
| 60 | const Format format = input->info()->format(); |
| 61 | ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel); |
| 62 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 63 | // Create kernel |
| 64 | std::string kernel_name = "channel_extract_" + string_from_format(format); |
| 65 | std::set<std::string> build_opts = { ("-DCHANNEL_" + string_from_channel(channel)) }; |
| 66 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 67 | |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 68 | // Half the processed elements for U,V channels due to sub-sampling of 2 |
| 69 | _subsampling = ((Format::YUYV422 == format || Format::UYVY422 == format) && Channel::Y != channel) ? 2 : 1; |
| 70 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | // Configure window |
| 72 | Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration)); |
| 73 | AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | update_window_and_padding(win, input_access, output_access); |
| 77 | |
| 78 | ValidRegion input_valid_region = input->info()->valid_region(); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 79 | 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] | 80 | |
| 81 | ICLKernel::configure(win); |
| 82 | } |
| 83 | |
| 84 | void CLChannelExtractKernel::configure(const ICLMultiImage *input, Channel channel, ICLImage *output) |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
| 87 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444); |
| 88 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_ON(static_cast<const void *>(input) == static_cast<void *>(output)); |
| 90 | |
| 91 | // Get format |
| 92 | const Format fmt = input->info()->format(); |
| 93 | |
| 94 | // Get input plane |
| 95 | const ICLImage *input_plane = input->cl_plane(plane_idx_from_channel(fmt, channel)); |
| 96 | ARM_COMPUTE_ERROR_ON(nullptr == input_plane); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | |
| 98 | _output = output; |
| 99 | _input = input_plane; |
| 100 | _subsampling = 1; |
| 101 | |
| 102 | // Create kernel |
| 103 | std::string kernel_name; |
| 104 | std::set<std::string> build_opts; |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 105 | if(Channel::Y == channel || Format::IYUV == fmt || Format::YUV444 == fmt) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | { |
| 107 | kernel_name = "copy_plane"; |
| 108 | } |
| 109 | else |
| 110 | { |
Anthony Barbier | 1fbb812 | 2018-02-19 19:36:02 +0000 | [diff] [blame] | 111 | kernel_name = "channel_extract_" + string_from_format(fmt); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 112 | build_opts.insert(("-DCHANNEL_" + string_from_channel(channel))); |
| 113 | } |
| 114 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 115 | |
| 116 | // Configure window |
| 117 | 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] | 118 | AccessWindowHorizontal input_access(input_plane->info(), 0, _num_elems_processed_per_iteration); |
| 119 | AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 121 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
| 123 | output_access.set_valid_region(win, input_plane->info()->valid_region()); |
| 124 | |
| 125 | ICLKernel::configure(win); |
| 126 | } |
| 127 | |
| 128 | void CLChannelExtractKernel::run(const Window &window, cl::CommandQueue &queue) |
| 129 | { |
| 130 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 131 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 132 | |
| 133 | Window slice = window.first_slice_window_2D(); |
| 134 | |
| 135 | do |
| 136 | { |
| 137 | Window win_sub(slice); |
| 138 | win_sub.set(Window::DimX, Window::Dimension(win_sub.x().start() / _subsampling, win_sub.x().end() / _subsampling, win_sub.x().step() / _subsampling)); |
| 139 | win_sub.set(Window::DimY, Window::Dimension(win_sub.y().start() / _subsampling, win_sub.y().end() / _subsampling, 1)); |
| 140 | |
| 141 | unsigned int idx = 0; |
| 142 | add_2D_tensor_argument(idx, _input, slice); |
| 143 | add_2D_tensor_argument(idx, _output, win_sub); |
| 144 | enqueue(queue, *this, slice); |
| 145 | } |
| 146 | while(window.slide_window_slice_2D(slice)); |
| 147 | } |