• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2020 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 "src/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/TensorInfo.h"
33 #include "arm_compute/core/Types.h"
34 #include "arm_compute/core/Validate.h"
35 #include "arm_compute/core/Window.h"
36 #include "src/core/NEON/INEKernel.h"
37 #include "src/core/helpers/AutoConfiguration.h"
38 #include "src/core/helpers/WindowHelpers.h"
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 
NEChannelExtractKernel()49 NEChannelExtractKernel::NEChannelExtractKernel()
50     : _func(nullptr), _lut_index(0)
51 {
52 }
53 
configure(const ITensor * input,Channel channel,ITensor * output)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 
61     // Check if input tensor has a valid format
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 
65     ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
66     ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
67 
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);
71 
72     unsigned int subsampling = 1;
73 
74     if(format == Format::YUYV422 || format == Format::UYVY422)
75     {
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);
78 
79         if(channel != Channel::Y)
80         {
81             subsampling = 2;
82         }
83     }
84 
85     TensorShape output_shape = calculate_subsampled_shape(input->info()->tensor_shape(), format, channel);
86     set_shape_if_empty(*output->info(), output_shape);
87 
88     ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output_shape, output->info()->tensor_shape());
89 
90     _input     = input;
91     _output    = output;
92     _lut_index = channel_idx_from_format(format, channel);
93 
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 
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);
120     update_window_and_padding(win, input_access, output_access);
121 
122     ValidRegion input_valid_region = input->info()->valid_region();
123     output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
124 
125     INEKernel::configure(win);
126 }
127 
configure(const IMultiImage * input,Channel channel,IImage * output)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 
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)
143     {
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);
146     }
147 
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
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 
158     _input     = input_plane;
159     _output    = output;
160     _lut_index = channel_idx_from_format(format, channel);
161 
162     unsigned int num_elems_processed_per_iteration = 32;
163 
164     _func = &NEChannelExtractKernel::copy_plane;
165 
166     if((format == Format::NV12 || format == Format::NV21) && channel != Channel::Y)
167     {
168         num_elems_processed_per_iteration = 16;
169         _func                             = &NEChannelExtractKernel::extract_1C_from_2C_img;
170     }
171 
172     Window win = calculate_max_window(*_input->info(), Steps(num_elems_processed_per_iteration));
173 
174     AccessWindowHorizontal input_access(_input->info(), 0, num_elems_processed_per_iteration);
175     AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
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 
run(const Window & window,const ThreadInfo & info)182 void NEChannelExtractKernel::run(const Window &window, const ThreadInfo &info)
183 {
184     ARM_COMPUTE_UNUSED(info);
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 
extract_1C_from_2C_img(const Window & win)192 void NEChannelExtractKernel::extract_1C_from_2C_img(const Window &win)
193 {
194     Iterator in(_input, win);
195     Iterator out(_output, win);
196 
197     execute_window_loop(win, [&](const Coordinates &)
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 
extract_1C_from_3C_img(const Window & win)207 void NEChannelExtractKernel::extract_1C_from_3C_img(const Window &win)
208 {
209     Iterator in(_input, win);
210     Iterator out(_output, win);
211 
212     execute_window_loop(win, [&](const Coordinates &)
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 
extract_1C_from_4C_img(const Window & win)222 void NEChannelExtractKernel::extract_1C_from_4C_img(const Window &win)
223 {
224     Iterator in(_input, win);
225     Iterator out(_output, win);
226 
227     execute_window_loop(win, [&](const Coordinates &)
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 
extract_YUYV_uv(const Window & win)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 
247     execute_window_loop(win, [&](const Coordinates &)
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 
copy_plane(const Window & win)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 }
270