Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [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/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 | { |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output); |
| 52 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0); |
| 53 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1); |
| 54 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2); |
| 55 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
| 56 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8); |
| 58 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8); |
| 59 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8); |
| 60 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422); |
| 61 | |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8); |
| 63 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8); |
| 64 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8); |
| 65 | |
| 66 | const Format output_format = output->info()->format(); |
| 67 | |
| 68 | // Check if horizontal dimension of Y plane is even and validate horizontal sub-sampling dimensions for U and V planes |
| 69 | if(Format::YUYV422 == output_format || Format::UYVY422 == output_format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | { |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 71 | // Validate Y plane of input and output |
| 72 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(output_format, plane0, output); |
| 73 | |
| 74 | // Validate U and V plane of the input |
| 75 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), plane1, plane2); |
| 76 | } |
| 77 | |
| 78 | _planes[0] = plane0; |
| 79 | _planes[1] = plane1; |
| 80 | _planes[2] = plane2; |
| 81 | _planes[3] = nullptr; |
| 82 | |
| 83 | // Validate the last input tensor only for RGBA format |
| 84 | if(Format::RGBA8888 == output_format) |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR_ON_NULLPTR(plane3); |
| 87 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane3); |
| 88 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane3, Format::U8); |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 90 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane3, 1, DataType::U8); |
| 91 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | _planes[3] = plane3; |
| 93 | } |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 94 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | _output = output; |
| 96 | _output_multi = nullptr; |
| 97 | |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 98 | // Half the processed elements for U and V channels due to horizontal sub-sampling of 2 |
| 99 | if(Format::YUYV422 == output_format || Format::UYVY422 == output_format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | { |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 101 | _x_subsampling[1] = 2; |
| 102 | _x_subsampling[2] = 2; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Create kernel |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 106 | std::string kernel_name = "channel_combine_" + string_from_format(output_format); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name)); |
| 108 | |
| 109 | // Configure window |
| 110 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 111 | |
| 112 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 113 | |
| 114 | AccessWindowHorizontal plane0_access(plane0->info(), 0, num_elems_processed_per_iteration); |
| 115 | AccessWindowRectangle plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]); |
| 116 | AccessWindowRectangle plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]); |
| 117 | AccessWindowHorizontal plane3_access(plane3 == nullptr ? nullptr : plane3->info(), 0, num_elems_processed_per_iteration); |
| 118 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 119 | |
| 120 | update_window_and_padding(win, plane0_access, plane1_access, plane2_access, plane3_access, output_access); |
| 121 | |
| 122 | ValidRegion valid_region = intersect_valid_regions(plane0->info()->valid_region(), |
| 123 | plane1->info()->valid_region(), |
| 124 | plane2->info()->valid_region()); |
| 125 | if(plane3 != nullptr) |
| 126 | { |
| 127 | valid_region = intersect_valid_regions(plane3->info()->valid_region(), valid_region); |
| 128 | } |
| 129 | output_access.set_valid_region(win, ValidRegion(valid_region.anchor, output->info()->tensor_shape())); |
| 130 | |
| 131 | ICLKernel::configure(win); |
| 132 | } |
| 133 | |
| 134 | void CLChannelCombineKernel::configure(const ICLImage *plane0, const ICLImage *plane1, const ICLImage *plane2, ICLMultiImage *output) |
| 135 | { |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 136 | ARM_COMPUTE_ERROR_ON_NULLPTR(plane0, plane1, plane2, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 137 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane0); |
| 138 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane1); |
| 139 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(plane2); |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 140 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane0, Format::U8); |
| 142 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane1, Format::U8); |
| 143 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(plane2, Format::U8); |
| 144 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444); |
| 145 | |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 146 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane0, 1, DataType::U8); |
| 147 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane1, 1, DataType::U8); |
| 148 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(plane2, 1, DataType::U8); |
| 149 | |
| 150 | const Format output_format = output->info()->format(); |
| 151 | |
| 152 | // Validate shape of Y plane to be even and shape of sub-sampling dimensions for U and V planes |
| 153 | // Perform validation only for formats which require sub-sampling. |
| 154 | if(Format::YUV444 != output_format) |
| 155 | { |
| 156 | // Validate Y plane of input and output |
| 157 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(output_format, plane0, output->plane(0)); |
| 158 | |
| 159 | // Validate U and V plane of the input |
| 160 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), plane1, plane2); |
| 161 | |
| 162 | // Validate second plane U (NV12 and NV21 have a UV88 combined plane while IYUV has only the U plane) |
| 163 | // MultiImage generates the correct tensor shape but also check in case the tensor shape of planes was changed to a wrong size |
| 164 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), output->plane(1)); |
| 165 | |
| 166 | // Validate the last plane V of format IYUV |
| 167 | if(Format::IYUV == output_format) |
| 168 | { |
| 169 | // Validate Y plane of the output |
| 170 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(output_format, plane0->info()->tensor_shape(), output->plane(2)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Set input tensors |
| 175 | _planes[0] = plane0; |
| 176 | _planes[1] = plane1; |
| 177 | _planes[2] = plane2; |
| 178 | _planes[3] = nullptr; |
| 179 | |
| 180 | // Set output tensor |
| 181 | _output = nullptr; |
| 182 | _output_multi = output; |
| 183 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | bool has_two_planars = false; |
| 185 | |
| 186 | // Set sub-sampling parameters for each plane |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | std::string kernel_name; |
| 188 | std::set<std::string> build_opts; |
| 189 | |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 190 | if(Format::NV12 == output_format || Format::NV21 == output_format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | { |
| 192 | _x_subsampling = { { 1, 2, 2 } }; |
| 193 | _y_subsampling = { { 1, 2, 2 } }; |
| 194 | kernel_name = "channel_combine_NV"; |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 195 | build_opts.emplace(Format::NV12 == output_format ? "-DNV12" : "-DNV21"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | has_two_planars = true; |
| 197 | } |
| 198 | else |
| 199 | { |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 200 | if(Format::IYUV == output_format) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | { |
| 202 | _x_subsampling = { { 1, 2, 2 } }; |
| 203 | _y_subsampling = { { 1, 2, 2 } }; |
| 204 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | |
| 206 | kernel_name = "copy_planes_3p"; |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 207 | build_opts.emplace(Format::IYUV == output_format ? "-DIYUV" : "-DYUV444"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Create kernel |
| 211 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts)); |
| 212 | |
| 213 | // Configure window |
| 214 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 215 | |
| 216 | Window win = calculate_max_window(*plane0->info(), Steps(num_elems_processed_per_iteration)); |
| 217 | |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 218 | AccessWindowRectangle input_plane0_access(plane0->info(), 0, 0, num_elems_processed_per_iteration, 1.f); |
| 219 | AccessWindowRectangle input_plane1_access(plane1->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]); |
| 220 | AccessWindowRectangle input_plane2_access(plane2->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]); |
| 221 | AccessWindowRectangle output_plane0_access(output->plane(0)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f, 1.f / _y_subsampling[1]); |
| 222 | AccessWindowRectangle output_plane1_access(output->plane(1)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[1], 1.f / _y_subsampling[1]); |
| 223 | AccessWindowRectangle output_plane2_access(has_two_planars ? nullptr : output->plane(2)->info(), 0, 0, num_elems_processed_per_iteration, 1.f, 1.f / _x_subsampling[2], 1.f / _y_subsampling[2]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | |
| 225 | update_window_and_padding(win, |
| 226 | input_plane0_access, input_plane1_access, input_plane2_access, |
| 227 | output_plane0_access, output_plane1_access, output_plane2_access); |
| 228 | |
| 229 | ValidRegion plane0_valid_region = plane0->info()->valid_region(); |
| 230 | ValidRegion output_plane1_region = has_two_planars ? intersect_valid_regions(plane1->info()->valid_region(), plane2->info()->valid_region()) : plane2->info()->valid_region(); |
| 231 | output_plane0_access.set_valid_region(win, ValidRegion(plane0_valid_region.anchor, output->plane(0)->info()->tensor_shape())); |
| 232 | output_plane1_access.set_valid_region(win, ValidRegion(output_plane1_region.anchor, output->plane(1)->info()->tensor_shape())); |
| 233 | output_plane2_access.set_valid_region(win, ValidRegion(plane2->info()->valid_region().anchor, output->plane(2)->info()->tensor_shape())); |
| 234 | |
| 235 | ICLKernel::configure(win); |
| 236 | } |
| 237 | |
| 238 | void CLChannelCombineKernel::run(const Window &window, cl::CommandQueue &queue) |
| 239 | { |
| 240 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 241 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 242 | |
| 243 | Window slice = window.first_slice_window_2D(); |
Ioan-Cristian Szabo | ae3c8ab | 2017-11-16 17:55:03 +0000 | [diff] [blame] | 244 | slice.set_dimension_step(Window::DimY, 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 245 | |
| 246 | do |
| 247 | { |
| 248 | // Subsampling in plane 1 |
| 249 | Window win_sub_plane1(slice); |
| 250 | 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])); |
| 251 | 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)); |
| 252 | |
| 253 | // Subsampling in plane 2 |
| 254 | Window win_sub_plane2(slice); |
| 255 | 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])); |
| 256 | 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)); |
| 257 | |
| 258 | unsigned int idx = 0; |
| 259 | |
| 260 | // Set inputs |
| 261 | add_2D_tensor_argument(idx, _planes[0], slice); |
| 262 | add_2D_tensor_argument(idx, _planes[1], win_sub_plane1); |
| 263 | add_2D_tensor_argument(idx, _planes[2], win_sub_plane2); |
| 264 | |
| 265 | if(nullptr != _planes[3]) |
| 266 | { |
| 267 | add_2D_tensor_argument(idx, _planes[3], slice); |
| 268 | } |
| 269 | |
| 270 | // Set outputs |
| 271 | if(nullptr != _output) // Single planar output |
| 272 | { |
| 273 | add_2D_tensor_argument(idx, _output, slice); |
| 274 | } |
| 275 | else // Multi-planar output |
| 276 | { |
| 277 | // Reduce slice in case of subsampling to avoid out-of bounds access |
| 278 | slice.set(Window::DimY, Window::Dimension(slice.y().start() / _y_subsampling[1], slice.y().end() / _y_subsampling[1], 1)); |
| 279 | |
| 280 | add_2D_tensor_argument(idx, _output_multi->cl_plane(0), slice); |
| 281 | add_2D_tensor_argument(idx, _output_multi->cl_plane(1), win_sub_plane1); |
| 282 | |
| 283 | if(3 == num_planes_from_format(_output_multi->info()->format())) |
| 284 | { |
| 285 | add_2D_tensor_argument(idx, _output_multi->cl_plane(2), win_sub_plane2); |
| 286 | } |
| 287 | |
| 288 | _kernel.setArg(idx++, slice.y().end()); |
| 289 | } |
| 290 | |
| 291 | enqueue(queue, *this, slice); |
| 292 | } |
| 293 | while(window.slide_window_slice_2D(slice)); |
| 294 | } |