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/NEON/kernels/NEChannelExtractKernel.h" |
| 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" |
| 32 | #include "arm_compute/core/NEON/INEKernel.h" |
| 33 | #include "arm_compute/core/TensorInfo.h" |
| 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
| 37 | |
| 38 | #include <arm_neon.h> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | class Coordinates; |
| 45 | } // namespace arm_compute |
| 46 | |
| 47 | NEChannelExtractKernel::NEChannelExtractKernel() |
| 48 | : _func(nullptr), _lut_index(0) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void NEChannelExtractKernel::configure(const ITensor *input, Channel channel, ITensor *output) |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 55 | ARM_COMPUTE_ERROR_ON(input == output); |
| 56 | |
| 57 | set_format_if_unknown(*output->info(), Format::U8); |
| 58 | |
| 59 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::UYVY422, Format::YUYV422); |
| 60 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
| 61 | |
| 62 | unsigned int num_elems_processed_per_iteration = 8; |
| 63 | |
| 64 | // Check format and channel |
| 65 | const Format format = input->info()->format(); |
| 66 | const unsigned int subsampling = (format == Format::YUYV422 || format == Format::UYVY422) && channel != Channel::Y ? 2 : 1; |
| 67 | TensorShape output_shape; |
| 68 | |
| 69 | switch(format) |
| 70 | { |
| 71 | case Format::RGB888: |
| 72 | case Format::RGBA8888: |
| 73 | num_elems_processed_per_iteration = 16; |
| 74 | output_shape = input->info()->tensor_shape(); |
| 75 | |
| 76 | if(format == Format::RGB888) |
| 77 | { |
| 78 | _func = &NEChannelExtractKernel::extract_1C_from_3C_img; |
| 79 | } |
| 80 | else if(format == Format::RGBA8888) |
| 81 | { |
| 82 | _func = &NEChannelExtractKernel::extract_1C_from_4C_img; |
| 83 | } |
| 84 | |
| 85 | switch(channel) |
| 86 | { |
| 87 | case Channel::R: |
| 88 | _lut_index = 0; |
| 89 | break; |
| 90 | case Channel::G: |
| 91 | _lut_index = 1; |
| 92 | break; |
| 93 | case Channel::B: |
| 94 | _lut_index = 2; |
| 95 | break; |
| 96 | case Channel::A: |
| 97 | if(format == Format::RGBA8888) |
| 98 | { |
| 99 | _lut_index = 3; |
| 100 | _func = &NEChannelExtractKernel::extract_1C_from_4C_img; |
| 101 | break; |
| 102 | } |
| 103 | default: |
| 104 | ARM_COMPUTE_ERROR("Not supported channel for this format."); |
| 105 | break; |
| 106 | } |
| 107 | break; |
| 108 | case Format::YUYV422: |
| 109 | case Format::UYVY422: |
| 110 | output_shape = input->info()->tensor_shape(); |
| 111 | |
| 112 | if(channel != Channel::Y) |
| 113 | { |
| 114 | output_shape.set(0, output_shape[0] / 2); |
| 115 | } |
| 116 | |
| 117 | switch(channel) |
| 118 | { |
| 119 | case Channel::Y: |
| 120 | num_elems_processed_per_iteration = 16; |
| 121 | _func = &NEChannelExtractKernel::extract_1C_from_2C_img; |
| 122 | _lut_index = (Format::YUYV422 == format) ? 0 : 1; |
| 123 | break; |
| 124 | case Channel::U: |
| 125 | num_elems_processed_per_iteration = 32; |
| 126 | _func = &NEChannelExtractKernel::extract_YUYV_uv; |
| 127 | _lut_index = (Format::YUYV422 == format) ? 1 : 0; |
| 128 | break; |
| 129 | case Channel::V: |
| 130 | num_elems_processed_per_iteration = 32; |
| 131 | _func = &NEChannelExtractKernel::extract_YUYV_uv; |
| 132 | _lut_index = (Format::YUYV422 == format) ? 3 : 2; |
| 133 | break; |
| 134 | default: |
| 135 | ARM_COMPUTE_ERROR("Not supported channel for this format."); |
| 136 | break; |
| 137 | } |
| 138 | break; |
| 139 | default: |
| 140 | ARM_COMPUTE_ERROR("Not supported format."); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | set_shape_if_empty(*output->info(), output_shape); |
| 145 | |
| 146 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape); |
| 147 | |
| 148 | _input = input; |
| 149 | _output = output; |
| 150 | |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 151 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 152 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
| 153 | 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] | 154 | |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 155 | update_window_and_padding(win, input_access, output_access); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | |
| 157 | ValidRegion input_valid_region = input->info()->valid_region(); |
| 158 | |
| 159 | output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape())); |
| 160 | |
| 161 | INEKernel::configure(win); |
| 162 | } |
| 163 | |
| 164 | void NEChannelExtractKernel::configure(const IMultiImage *input, Channel channel, IImage *output) |
| 165 | { |
| 166 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 167 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output); |
| 168 | |
| 169 | set_format_if_unknown(*output->info(), Format::U8); |
| 170 | |
| 171 | switch(input->info()->format()) |
| 172 | { |
| 173 | case Format::NV12: |
| 174 | case Format::NV21: |
| 175 | case Format::IYUV: |
| 176 | switch(channel) |
| 177 | { |
| 178 | case Channel::Y: |
| 179 | set_shape_if_empty(*output->info(), input->plane(0)->info()->tensor_shape()); |
| 180 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input->plane(0), output); |
| 181 | break; |
| 182 | case Channel::U: |
| 183 | case Channel::V: |
| 184 | set_shape_if_empty(*output->info(), input->plane(1)->info()->tensor_shape()); |
| 185 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input->plane(1), output); |
| 186 | break; |
| 187 | default: |
| 188 | ARM_COMPUTE_ERROR("Unsupported channel for selected format"); |
| 189 | } |
| 190 | break; |
| 191 | case Format::YUV444: |
| 192 | set_shape_if_empty(*output->info(), input->plane(0)->info()->tensor_shape()); |
| 193 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input->plane(0), output); |
| 194 | break; |
| 195 | default: |
| 196 | ARM_COMPUTE_ERROR("Unsupported format"); |
| 197 | } |
| 198 | |
| 199 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444); |
| 200 | ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8); |
| 201 | |
| 202 | unsigned int num_elems_processed_per_iteration = 32; |
| 203 | |
| 204 | const Format &format = input->info()->format(); |
| 205 | |
| 206 | switch(format) |
| 207 | { |
| 208 | case Format::NV12: |
| 209 | case Format::NV21: |
| 210 | switch(channel) |
| 211 | { |
| 212 | case Channel::Y: |
| 213 | _input = input->plane(0); |
| 214 | _func = &NEChannelExtractKernel::copy_plane; |
| 215 | break; |
| 216 | case Channel::U: |
| 217 | _input = input->plane(1); |
| 218 | num_elems_processed_per_iteration = 16; |
| 219 | _func = &NEChannelExtractKernel::extract_1C_from_2C_img; |
| 220 | _lut_index = (Format::NV12 == format) ? 0 : 1; |
| 221 | break; |
| 222 | case Channel::V: |
| 223 | _input = input->plane(1); |
| 224 | num_elems_processed_per_iteration = 16; |
| 225 | _func = &NEChannelExtractKernel::extract_1C_from_2C_img; |
| 226 | _lut_index = (Format::NV12 == format) ? 1 : 0; |
| 227 | break; |
| 228 | default: |
| 229 | ARM_COMPUTE_ERROR("Not supported channel for this format."); |
| 230 | break; |
| 231 | } |
| 232 | break; |
| 233 | case Format::IYUV: |
| 234 | case Format::YUV444: |
| 235 | _func = &NEChannelExtractKernel::copy_plane; |
| 236 | switch(channel) |
| 237 | { |
| 238 | case Channel::Y: |
| 239 | _input = input->plane(0); |
| 240 | break; |
| 241 | case Channel::U: |
| 242 | _input = input->plane(1); |
| 243 | break; |
| 244 | case Channel::V: |
| 245 | _input = input->plane(2); |
| 246 | break; |
| 247 | default: |
| 248 | ARM_COMPUTE_ERROR("Not supported channel for this format."); |
| 249 | break; |
| 250 | } |
| 251 | break; |
| 252 | default: |
| 253 | ARM_COMPUTE_ERROR("Not supported format."); |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | _output = output; |
| 258 | Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 259 | AccessWindowHorizontal input_access(_input->info(), 0, num_elems_processed_per_iteration); |
Georgios Pinitas | b158fba | 2017-07-05 16:50:24 +0100 | [diff] [blame] | 260 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 261 | update_window_and_padding(win, input_access, output_access); |
| 262 | output_access.set_valid_region(win, _input->info()->valid_region()); |
| 263 | |
| 264 | INEKernel::configure(win); |
| 265 | } |
| 266 | |
| 267 | void NEChannelExtractKernel::run(const Window &window) |
| 268 | { |
| 269 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 270 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 271 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 272 | |
| 273 | (this->*_func)(window); |
| 274 | } |
| 275 | |
| 276 | void NEChannelExtractKernel::extract_1C_from_2C_img(const Window &win) |
| 277 | { |
| 278 | Iterator in(_input, win); |
| 279 | Iterator out(_output, win); |
| 280 | |
| 281 | execute_window_loop(win, [&](const Coordinates & id) |
| 282 | { |
| 283 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 284 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 285 | const auto pixels = vld2q_u8(in_ptr); |
| 286 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 287 | }, |
| 288 | in, out); |
| 289 | } |
| 290 | |
| 291 | void NEChannelExtractKernel::extract_1C_from_3C_img(const Window &win) |
| 292 | { |
| 293 | Iterator in(_input, win); |
| 294 | Iterator out(_output, win); |
| 295 | |
| 296 | execute_window_loop(win, [&](const Coordinates & id) |
| 297 | { |
| 298 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 299 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 300 | const auto pixels = vld3q_u8(in_ptr); |
| 301 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 302 | }, |
| 303 | in, out); |
| 304 | } |
| 305 | |
| 306 | void NEChannelExtractKernel::extract_1C_from_4C_img(const Window &win) |
| 307 | { |
| 308 | Iterator in(_input, win); |
| 309 | Iterator out(_output, win); |
| 310 | |
| 311 | execute_window_loop(win, [&](const Coordinates & id) |
| 312 | { |
| 313 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 314 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 315 | const auto pixels = vld4q_u8(in_ptr); |
| 316 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 317 | }, |
| 318 | in, out); |
| 319 | } |
| 320 | |
| 321 | void NEChannelExtractKernel::extract_YUYV_uv(const Window &win) |
| 322 | { |
| 323 | ARM_COMPUTE_ERROR_ON(win.x().step() % 2); |
| 324 | |
| 325 | Window win_out(win); |
| 326 | win_out.set_dimension_step(Window::DimX, win.x().step() / 2); |
| 327 | |
| 328 | Iterator in(_input, win); |
| 329 | Iterator out(_output, win_out); |
| 330 | |
| 331 | execute_window_loop(win, [&](const Coordinates & id) |
| 332 | { |
| 333 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 334 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 335 | const auto pixels = vld4q_u8(in_ptr); |
| 336 | vst1q_u8(out_ptr, pixels.val[_lut_index]); |
| 337 | }, |
| 338 | in, out); |
| 339 | } |
| 340 | |
| 341 | void NEChannelExtractKernel::copy_plane(const Window &win) |
| 342 | { |
| 343 | Iterator in(_input, win); |
| 344 | Iterator out(_output, win); |
| 345 | |
| 346 | execute_window_loop(win, [&](const Coordinates &) |
| 347 | { |
| 348 | const auto in_ptr = static_cast<uint8_t *>(in.ptr()); |
| 349 | const auto out_ptr = static_cast<uint8_t *>(out.ptr()); |
| 350 | vst4_u8(out_ptr, vld4_u8(in_ptr)); |
| 351 | }, |
| 352 | in, out); |
| 353 | } |