blob: d0c633555dfcdc58789013e0f07d293e13805b66 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabo91d20d92017-10-27 17:35:40 +01002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/CLChannelExtractKernel.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/Coordinates.h"
31#include "arm_compute/core/Error.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/MultiImageInfo.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/core/Utils.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
39
40#include <set>
41#include <string>
42
43using namespace arm_compute;
44
45CLChannelExtractKernel::CLChannelExtractKernel()
46 : _input(nullptr), _output(nullptr), _num_elems_processed_per_iteration(8), _subsampling(1)
47{
48}
49
50void CLChannelExtractKernel::configure(const ICLTensor *input, Channel channel, ICLTensor *output)
51{
52 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::RGB888, Format::RGBA8888, Format::YUYV422, Format::UYVY422);
53 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
Anthony Barbier1fbb8122018-02-19 19:36:02 +000054 ARM_COMPUTE_ERROR_ON(static_cast<const void *>(input) == static_cast<void *>(output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055
56 _input = input;
57 _output = output;
58
Anthony Barbier1fbb8122018-02-19 19:36:02 +000059 // Check format
60 const Format format = input->info()->format();
61 ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(format, channel);
62
Anthony Barbier6ff3b192017-09-04 18:44:23 +010063 // Create kernel
64 std::string kernel_name = "channel_extract_" + string_from_format(format);
65 std::set<std::string> build_opts = { ("-DCHANNEL_" + string_from_channel(channel)) };
66 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
67
Anthony Barbier1fbb8122018-02-19 19:36:02 +000068 // Half the processed elements for U,V channels due to sub-sampling of 2
69 _subsampling = ((Format::YUYV422 == format || Format::UYVY422 == format) && Channel::Y != channel) ? 2 : 1;
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071 // Configure window
72 Window win = calculate_max_window(*input->info(), Steps(_num_elems_processed_per_iteration));
73 AccessWindowHorizontal input_access(input->info(), 0, _num_elems_processed_per_iteration);
Georgios Pinitasb158fba2017-07-05 16:50:24 +010074 AccessWindowRectangle output_access(output->info(), 0, 0, _num_elems_processed_per_iteration, 1, 1.f / _subsampling, 1.f / _subsampling);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075
76 update_window_and_padding(win, input_access, output_access);
77
78 ValidRegion input_valid_region = input->info()->valid_region();
Georgios Pinitasb158fba2017-07-05 16:50:24 +010079 output_access.set_valid_region(win, ValidRegion(input_valid_region.anchor, output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 ICLKernel::configure(win);
82}
83
84void CLChannelExtractKernel::configure(const ICLMultiImage *input, Channel channel, ICLImage *output)
85{
86 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(output);
87 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input, Format::NV12, Format::NV21, Format::IYUV, Format::YUV444);
88 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output, Format::U8);
Anthony Barbier1fbb8122018-02-19 19:36:02 +000089 ARM_COMPUTE_ERROR_ON(static_cast<const void *>(input) == static_cast<void *>(output));
90
91 // Get format
92 const Format fmt = input->info()->format();
93
94 // Get input plane
95 const ICLImage *input_plane = input->cl_plane(plane_idx_from_channel(fmt, channel));
96 ARM_COMPUTE_ERROR_ON(nullptr == input_plane);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097
98 _output = output;
99 _input = input_plane;
100 _subsampling = 1;
101
102 // Create kernel
103 std::string kernel_name;
104 std::set<std::string> build_opts;
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000105 if(Channel::Y == channel || Format::IYUV == fmt || Format::YUV444 == fmt)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 {
107 kernel_name = "copy_plane";
108 }
109 else
110 {
Anthony Barbier1fbb8122018-02-19 19:36:02 +0000111 kernel_name = "channel_extract_" + string_from_format(fmt);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 build_opts.insert(("-DCHANNEL_" + string_from_channel(channel)));
113 }
114 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
115
116 // Configure window
117 Window win = calculate_max_window(*input_plane->info(), Steps(_num_elems_processed_per_iteration));
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100118 AccessWindowHorizontal input_access(input_plane->info(), 0, _num_elems_processed_per_iteration);
119 AccessWindowHorizontal output_access(output->info(), 0, _num_elems_processed_per_iteration);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120
Georgios Pinitasb158fba2017-07-05 16:50:24 +0100121 update_window_and_padding(win, input_access, output_access);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122
123 output_access.set_valid_region(win, input_plane->info()->valid_region());
124
125 ICLKernel::configure(win);
126}
127
128void CLChannelExtractKernel::run(const Window &window, cl::CommandQueue &queue)
129{
130 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
131 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
132
133 Window slice = window.first_slice_window_2D();
134
135 do
136 {
137 Window win_sub(slice);
138 win_sub.set(Window::DimX, Window::Dimension(win_sub.x().start() / _subsampling, win_sub.x().end() / _subsampling, win_sub.x().step() / _subsampling));
139 win_sub.set(Window::DimY, Window::Dimension(win_sub.y().start() / _subsampling, win_sub.y().end() / _subsampling, 1));
140
141 unsigned int idx = 0;
142 add_2D_tensor_argument(idx, _input, slice);
143 add_2D_tensor_argument(idx, _output, win_sub);
144 enqueue(queue, *this, slice);
145 }
146 while(window.slide_window_slice_2D(slice));
147}