Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLChannelCombineKernel.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/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/MultiImageInfo.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Utils.h" |
| 36 | #include "arm_compute/core/Validate.h" |
| 37 | #include "arm_compute/core/Window.h" |
| 38 | |
| 39 | #include <set> |
| 40 | #include <string> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | CLChannelCombineKernel::CLChannelCombineKernel() |
| 45 | : _planes{ { nullptr } }, _output(nullptr), _output_multi(nullptr), _x_subsampling{ { 1, 1, 1 } }, _y_subsampling{ { 1, 1, 1 } } |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void CLChannelCombineKernel::configure(const ICLTensor *plane0, const ICLTensor *plane1, const ICLTensor *plane2, const ICLTensor *plane3, ICLTensor *output) |
| 50 | { |
| 51 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8); |
| 52 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8); |
| 53 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8); |
| 54 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422); |
| 55 | |
| 56 | const Format fmt = output->info()->format(); |
| 57 | _planes[0] = plane0; |
| 58 | _planes[1] = plane1; |
| 59 | _planes[2] = plane2; |
| 60 | if(Format::RGBA8888 == fmt) |
| 61 | { |
| 62 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane3, Format::U8); |
| 63 | _planes[3] = plane3; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | _planes[3] = nullptr; |
| 68 | } |
| 69 | _output = output; |
| 70 | _output_multi = nullptr; |
| 71 | |
| 72 | // Half the processed elements for U,V channels due to sub-sampling of 2 |
| 73 | if(Format::YUYV422 == fmt || Format::UYVY422 == fmt) |
| 74 | { |
| 75 | _x_subsampling = { { 1, 2, 2 } }; |
| 76 | _y_subsampling = { { 1, 2, 2 } }; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | _x_subsampling = { { 1, 1, 1 } }; |
| 81 | _y_subsampling = { { 1, 1, 1 } }; |
| 82 | } |
| 83 | |
| 84 | // Create kernel |
| 85 | std::string kernel_name = "channel_combine_" + string_from_format(fmt); |
| 86 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name)); |
| 87 | |
| 88 | // Configure window |
| 89 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 90 | |
| 91 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 92 | |
| 93 | AccessWindowHorizontal plane0_access(plane0->info(), 0, num_elems_processed_per_iteration); |
| 94 | AccessWindowRectangle plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]); |
| 95 | AccessWindowRectangle plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]); |
| 96 | AccessWindowHorizontal plane3_access(plane3 == nullptr ? nullptr : plane3->info(), 0, num_elems_processed_per_iteration); |
| 97 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 98 | |
| 99 | update_window_and_padding(win, plane0_access, plane1_access, plane2_access, plane3_access, output_access); |
| 100 | |
| 101 | ValidRegion valid_region = intersect_valid_regions(plane0->info()->valid_region(), |
| 102 | plane1->info()->valid_region(), |
| 103 | plane2->info()->valid_region()); |
| 104 | if(plane3 != nullptr) |
| 105 | { |
| 106 | valid_region = intersect_valid_regions(plane3->info()->valid_region(), valid_region); |
| 107 | } |
| 108 | output_access.set_valid_region(win, ValidRegion(valid_region.anchor, output->info()->tensor_shape())); |
| 109 | |
| 110 | ICLKernel::configure(win); |
| 111 | } |
| 112 | |
| 113 | void CLChannelCombineKernel::configure(const ICLImage *plane0, const ICLImage *plane1, const ICLImage *plane2, ICLMultiImage *output) |
| 114 | { |
| 115 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0); |
| 116 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1); |
| 117 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2); |
| 118 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8); |
| 119 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8); |
| 120 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8); |
| 121 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444); |
| 122 | |
| 123 | _planes[0] = plane0; |
| 124 | _planes[1] = plane1; |
| 125 | _planes[2] = plane2; |
| 126 | _planes[3] = nullptr; |
| 127 | _output = nullptr; |
| 128 | _output_multi = output; |
| 129 | bool has_two_planars = false; |
| 130 | |
| 131 | // Set sub-sampling parameters for each plane |
| 132 | const Format fmt = output->info()->format(); |
| 133 | std::string kernel_name; |
| 134 | std::set<std::string> build_opts; |
| 135 | |
| 136 | if(Format::NV12 == fmt || Format::NV21 == fmt) |
| 137 | { |
| 138 | _x_subsampling = { { 1, 2, 2 } }; |
| 139 | _y_subsampling = { { 1, 2, 2 } }; |
| 140 | kernel_name = "channel_combine_NV"; |
| 141 | build_opts.emplace(Format::NV12 == fmt ? "-DNV12" : "-DNV21"); |
| 142 | has_two_planars = true; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | if(Format::IYUV == fmt) |
| 147 | { |
| 148 | _x_subsampling = { { 1, 2, 2 } }; |
| 149 | _y_subsampling = { { 1, 2, 2 } }; |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | _x_subsampling = { { 1, 1, 1 } }; |
| 154 | _y_subsampling = { { 1, 1, 1 } }; |
| 155 | } |
| 156 | |
| 157 | kernel_name = "copy_planes_3p"; |
| 158 | build_opts.emplace(Format::IYUV == fmt ? "-DIYUV" : "-DYUV444"); |
| 159 | } |
| 160 | |
| 161 | // Create kernel |
| 162 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 163 | |
| 164 | // Configure window |
| 165 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 166 | |
| 167 | Window win = calculate_max_window(*plane0->info(), Steps(num_elems_processed_per_iteration)); |
| 168 | |
| 169 | AccessWindowHorizontal input_plane0_access(plane0->info(), 0, num_elems_processed_per_iteration); |
| 170 | AccessWindowRectangle input_plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]); |
| 171 | AccessWindowRectangle input_plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]); |
| 172 | AccessWindowRectangle output_plane0_access(output->plane(0)->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f, 1.f / _y_subsampling[1]); |
| 173 | AccessWindowRectangle output_plane1_access(output->plane(1)->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]); |
| 174 | AccessWindowRectangle output_plane2_access(has_two_planars ? nullptr : output->plane(2)->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]); |
| 175 | |
| 176 | update_window_and_padding(win, |
| 177 | input_plane0_access, input_plane1_access, input_plane2_access, |
| 178 | output_plane0_access, output_plane1_access, output_plane2_access); |
| 179 | |
| 180 | ValidRegion plane0_valid_region = plane0->info()->valid_region(); |
| 181 | ValidRegion output_plane1_region = has_two_planars ? intersect_valid_regions(plane1->info()->valid_region(), plane2->info()->valid_region()) : plane2->info()->valid_region(); |
| 182 | output_plane0_access.set_valid_region(win, ValidRegion(plane0_valid_region.anchor, output->plane(0)->info()->tensor_shape())); |
| 183 | output_plane1_access.set_valid_region(win, ValidRegion(output_plane1_region.anchor, output->plane(1)->info()->tensor_shape())); |
| 184 | output_plane2_access.set_valid_region(win, ValidRegion(plane2->info()->valid_region().anchor, output->plane(2)->info()->tensor_shape())); |
| 185 | |
| 186 | ICLKernel::configure(win); |
| 187 | } |
| 188 | |
| 189 | void CLChannelCombineKernel::run(const Window &window, cl::CommandQueue &queue) |
| 190 | { |
| 191 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 192 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 193 | |
| 194 | Window slice = window.first_slice_window_2D(); |
| 195 | |
| 196 | do |
| 197 | { |
| 198 | // Subsampling in plane 1 |
| 199 | Window win_sub_plane1(slice); |
| 200 | win_sub_plane1.set(Window::DimX, Window::Dimension(win_sub_plane1.x().start() / _x_subsampling[1], win_sub_plane1.x().end() / _x_subsampling[1], win_sub_plane1.x().step() / _x_subsampling[1])); |
| 201 | win_sub_plane1.set(Window::DimY, Window::Dimension(win_sub_plane1.y().start() / _y_subsampling[1], win_sub_plane1.y().end() / _y_subsampling[1], 1)); |
| 202 | |
| 203 | // Subsampling in plane 2 |
| 204 | Window win_sub_plane2(slice); |
| 205 | win_sub_plane2.set(Window::DimX, Window::Dimension(win_sub_plane2.x().start() / _x_subsampling[2], win_sub_plane2.x().end() / _x_subsampling[2], win_sub_plane2.x().step() / _x_subsampling[2])); |
| 206 | win_sub_plane2.set(Window::DimY, Window::Dimension(win_sub_plane2.y().start() / _y_subsampling[2], win_sub_plane2.y().end() / _y_subsampling[2], 1)); |
| 207 | |
| 208 | unsigned int idx = 0; |
| 209 | |
| 210 | // Set inputs |
| 211 | add_2D_tensor_argument(idx, _planes[0], slice); |
| 212 | add_2D_tensor_argument(idx, _planes[1], win_sub_plane1); |
| 213 | add_2D_tensor_argument(idx, _planes[2], win_sub_plane2); |
| 214 | |
| 215 | if(nullptr != _planes[3]) |
| 216 | { |
| 217 | add_2D_tensor_argument(idx, _planes[3], slice); |
| 218 | } |
| 219 | |
| 220 | // Set outputs |
| 221 | if(nullptr != _output) // Single planar output |
| 222 | { |
| 223 | add_2D_tensor_argument(idx, _output, slice); |
| 224 | } |
| 225 | else // Multi-planar output |
| 226 | { |
| 227 | // Reduce slice in case of subsampling to avoid out-of bounds access |
| 228 | slice.set(Window::DimY, Window::Dimension(slice.y().start() / _y_subsampling[1], slice.y().end() / _y_subsampling[1], 1)); |
| 229 | |
| 230 | add_2D_tensor_argument(idx, _output_multi->cl_plane(0), slice); |
| 231 | add_2D_tensor_argument(idx, _output_multi->cl_plane(1), win_sub_plane1); |
| 232 | |
| 233 | if(3 == num_planes_from_format(_output_multi->info()->format())) |
| 234 | { |
| 235 | add_2D_tensor_argument(idx, _output_multi->cl_plane(2), win_sub_plane2); |
| 236 | } |
| 237 | |
| 238 | _kernel.setArg(idx++, slice.y().end()); |
| 239 | } |
| 240 | |
| 241 | enqueue(queue, *this, slice); |
| 242 | } |
| 243 | while(window.slide_window_slice_2D(slice)); |
| 244 | } |