Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEChannelExtractKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IAccessWindow.h" |
| 29 | #include "arm_compute/core/IMultiImage.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/MultiImageInfo.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 36 | #include "src/core/NEON/INEKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 37 | #include "src/core/helpers/AutoConfiguration.h" |
| 38 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <arm_neon.h> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | namespace arm_compute |
| 45 | { |
| 46 | class Coordinates; |
| 47 | } // namespace arm_compute |
| 48 | |
| 49 | NEChannelExtractKernel::NEChannelExtractKernel() |
| 50 | : _func(nullptr), _lut_index(0) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void NEChannelExtractKernel::configure(const ITensor *input, Channel channel, ITensor *output) |
| 55 | { |
| 56 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 57 | ARM_COMPUTE_ERROR_ON(input == output); |
| 58 | |
| 59 | set_format_if_unknown(*output->info(), Format::U8); |
| 60 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 61 | // Check if input tensor has a valid format |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::UYVY422, Format::YUYV422); |
| 63 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
| 64 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 66 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 68 | // Check if channel is valid for given format |
| 69 | const Format format = input->info()->format(); |
| 70 | ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 72 | unsigned int subsampling = 1; |
| 73 | |
| 74 | if(format == Format::YUYV422 || format == Format::UYVY422) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 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); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 79 | if(channel != Channel::Y) |
| 80 | { |
| 81 | subsampling = 2; |
| 82 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 85 | TensorShape output_shape = calculate_subsampled_shape(input->info()->tensor_shape(), format, channel); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | set_shape_if_empty(*output->info(), output_shape); |
| 87 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 90 | _input = input; |
| 91 | _output = output; |
| 92 | _lut_index = channel_idx_from_format(format, channel); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 94 | unsigned int num_elems_processed_per_iteration = 16; |
| 95 | |
| 96 | if(format == Format::YUYV422 || format == Format::UYVY422) |
| 97 | { |
| 98 | _func = &NEChannelExtractKernel::extract_1C_from_2C_img; |
| 99 | |
| 100 | if(channel != Channel::Y) // Channel::U or Channel::V |
| 101 | { |
| 102 | num_elems_processed_per_iteration = 32; |
| 103 | _func = &NEChannelExtractKernel::extract_YUYV_uv; |
| 104 | } |
| 105 | } |
| 106 | else // Format::RGB888 or Format::RGBA8888 |
| 107 | { |
| 108 | _func = &NEChannelExtractKernel::extract_1C_from_3C_img; |
| 109 | |
| 110 | if(format == Format::RGBA8888) |
| 111 | { |
| 112 | _func = &NEChannelExtractKernel::extract_1C_from_4C_img; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 117 | |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 118 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 119 | AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration, 1, 1.f / subsampling, 1.f / subsampling); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 120 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | |
| 122 | ValidRegion input_valid_region = input->info()->valid_region(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape())); |
| 124 | |
| 125 | INEKernel::configure(win); |
| 126 | } |
| 127 | |
| 128 | void NEChannelExtractKernel::configure(const IMultiImage *input, Channel channel, IImage *output) |
| 129 | { |
| 130 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 131 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
| 132 | |
| 133 | set_format_if_unknown(*output->info(), Format::U8); |
| 134 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 135 | const Format format = input->info()->format(); |
| 136 | ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel); |
| 137 | |
| 138 | // Get input plane |
| 139 | const IImage *input_plane = input->plane(plane_idx_from_channel(format, channel)); |
| 140 | ARM_COMPUTE_ERROR_ON_NULLPTR(input_plane); |
| 141 | |
| 142 | if(Channel::Y == channel && format != Format::YUV444) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 144 | // Check if the width of the tensor shape is even for formats with subsampled channels (UYVY422 and YUYV422) |
| 145 | ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(format, input_plane); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 148 | // Calculate 2x2 subsampled tensor shape |
| 149 | TensorShape output_shape = calculate_subsampled_shape(input->plane(0)->info()->tensor_shape(), format, channel); |
| 150 | set_shape_if_empty(*output->info(), output_shape); |
| 151 | |
| 152 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape()); |
| 153 | |
| 154 | // Check if input tensor has a valid format |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444); |
| 156 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
| 157 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 158 | _input = input_plane; |
| 159 | _output = output; |
| 160 | _lut_index = channel_idx_from_format(format, channel); |
| 161 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | unsigned int num_elems_processed_per_iteration = 32; |
| 163 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 164 | _func = &NEChannelExtractKernel::copy_plane; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 166 | if((format == Format::NV12 || format == Format::NV21) && channel != Channel::Y) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | { |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 168 | num_elems_processed_per_iteration = 16; |
| 169 | _func = &NEChannelExtractKernel::extract_1C_from_2C_img; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 172 | Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration)); |
| 173 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | AccessWindowHorizontal input_access(_input->info(), 0, num_elems_processed_per_iteration); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 175 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | update_window_and_padding(win, input_access, output_access); |
| 177 | output_access.set_valid_region(win, _input->info()->valid_region()); |
| 178 | |
| 179 | INEKernel::configure(win); |
| 180 | } |
| 181 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 182 | void NEChannelExtractKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 183 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 184 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 186 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 187 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 188 | |
| 189 | (this->*_func)(window); |
| 190 | } |
| 191 | |
| 192 | void NEChannelExtractKernel::extract_1C_from_2C_img(const Window &win) |
| 193 | { |
| 194 | Iterator in(_input, win); |
| 195 | Iterator out(_output, win); |
| 196 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 197 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | { |
| 199 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 200 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 201 | const auto pixels = vld2q_u8(in_ptr); |
| 202 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 203 | }, |
| 204 | in, out); |
| 205 | } |
| 206 | |
| 207 | void NEChannelExtractKernel::extract_1C_from_3C_img(const Window &win) |
| 208 | { |
| 209 | Iterator in(_input, win); |
| 210 | Iterator out(_output, win); |
| 211 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 212 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | { |
| 214 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 215 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 216 | const auto pixels = vld3q_u8(in_ptr); |
| 217 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 218 | }, |
| 219 | in, out); |
| 220 | } |
| 221 | |
| 222 | void NEChannelExtractKernel::extract_1C_from_4C_img(const Window &win) |
| 223 | { |
| 224 | Iterator in(_input, win); |
| 225 | Iterator out(_output, win); |
| 226 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 227 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | { |
| 229 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 230 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 231 | const auto pixels = vld4q_u8(in_ptr); |
| 232 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 233 | }, |
| 234 | in, out); |
| 235 | } |
| 236 | |
| 237 | void NEChannelExtractKernel::extract_YUYV_uv(const Window &win) |
| 238 | { |
| 239 | ARM_COMPUTE_ERROR_ON(win.x().step() % 2); |
| 240 | |
| 241 | Window win_out(win); |
| 242 | win_out.set_dimension_step(Window::DimX, win.x().step() / 2); |
| 243 | |
| 244 | Iterator in(_input, win); |
| 245 | Iterator out(_output, win_out); |
| 246 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 247 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | { |
| 249 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 250 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 251 | const auto pixels = vld4q_u8(in_ptr); |
| 252 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 253 | }, |
| 254 | in, out); |
| 255 | } |
| 256 | |
| 257 | void NEChannelExtractKernel::copy_plane(const Window &win) |
| 258 | { |
| 259 | Iterator in(_input, win); |
| 260 | Iterator out(_output, win); |
| 261 | |
| 262 | execute_window_loop(win, [&](const Coordinates &) |
| 263 | { |
| 264 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 265 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 266 | vst4_u8(out_ptr, vld4_u8(in_ptr)); |
| 267 | }, |
| 268 | in, out); |
| 269 | } |