Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2023 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 | #ifndef ARM_COMPUTE_CORE_UTILS_FORMATUTILS_H |
| 25 | #define ARM_COMPUTE_CORE_UTILS_FORMATUTILS_H |
| 26 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CoreTypes.h" |
| 28 | #include "arm_compute/core/Error.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | /** The size in bytes of the pixel format |
| 33 | * |
| 34 | * @param[in] format Input format |
| 35 | * |
| 36 | * @return The size in bytes of the pixel format |
| 37 | */ |
| 38 | inline size_t pixel_size_from_format(Format format) |
| 39 | { |
| 40 | switch(format) |
| 41 | { |
| 42 | case Format::U8: |
| 43 | return 1; |
| 44 | case Format::U16: |
| 45 | case Format::S16: |
| 46 | case Format::BFLOAT16: |
| 47 | case Format::F16: |
| 48 | case Format::UV88: |
| 49 | case Format::YUYV422: |
| 50 | case Format::UYVY422: |
| 51 | return 2; |
| 52 | case Format::RGB888: |
| 53 | return 3; |
| 54 | case Format::RGBA8888: |
| 55 | return 4; |
| 56 | case Format::U32: |
| 57 | case Format::S32: |
| 58 | case Format::F32: |
| 59 | return 4; |
| 60 | //Doesn't make sense for planar formats: |
| 61 | case Format::NV12: |
| 62 | case Format::NV21: |
| 63 | case Format::IYUV: |
| 64 | case Format::YUV444: |
| 65 | default: |
| 66 | ARM_COMPUTE_ERROR("Undefined pixel size for given format"); |
| 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** Return the plane index of a given channel given an input format. |
| 72 | * |
| 73 | * @param[in] format Input format |
| 74 | * @param[in] channel Input channel |
| 75 | * |
| 76 | * @return The plane index of the specific channel of the specific format |
| 77 | */ |
| 78 | inline int plane_idx_from_channel(Format format, Channel channel) |
| 79 | { |
| 80 | switch(format) |
| 81 | { |
| 82 | // Single planar formats have a single plane |
| 83 | case Format::U8: |
| 84 | case Format::U16: |
| 85 | case Format::S16: |
| 86 | case Format::U32: |
| 87 | case Format::S32: |
| 88 | case Format::BFLOAT16: |
| 89 | case Format::F16: |
| 90 | case Format::F32: |
| 91 | case Format::UV88: |
| 92 | case Format::RGB888: |
| 93 | case Format::RGBA8888: |
| 94 | case Format::YUYV422: |
| 95 | case Format::UYVY422: |
| 96 | return 0; |
| 97 | // Multi planar formats |
| 98 | case Format::NV12: |
| 99 | case Format::NV21: |
| 100 | { |
| 101 | // Channel U and V share the same plane of format UV88 |
| 102 | switch(channel) |
| 103 | { |
| 104 | case Channel::Y: |
| 105 | return 0; |
| 106 | case Channel::U: |
| 107 | case Channel::V: |
| 108 | return 1; |
| 109 | default: |
| 110 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 111 | return 0; |
| 112 | } |
| 113 | } |
| 114 | case Format::IYUV: |
| 115 | case Format::YUV444: |
| 116 | { |
| 117 | switch(channel) |
| 118 | { |
| 119 | case Channel::Y: |
| 120 | return 0; |
| 121 | case Channel::U: |
| 122 | return 1; |
| 123 | case Channel::V: |
| 124 | return 2; |
| 125 | default: |
| 126 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | default: |
| 131 | ARM_COMPUTE_ERROR("Not supported format"); |
| 132 | return 0; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** Return the channel index of a given channel given an input format. |
| 137 | * |
| 138 | * @param[in] format Input format |
| 139 | * @param[in] channel Input channel |
| 140 | * |
| 141 | * @return The channel index of the specific channel of the specific format |
| 142 | */ |
| 143 | inline int channel_idx_from_format(Format format, Channel channel) |
| 144 | { |
| 145 | switch(format) |
| 146 | { |
| 147 | case Format::RGB888: |
| 148 | { |
| 149 | switch(channel) |
| 150 | { |
| 151 | case Channel::R: |
| 152 | return 0; |
| 153 | case Channel::G: |
| 154 | return 1; |
| 155 | case Channel::B: |
| 156 | return 2; |
| 157 | default: |
| 158 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | case Format::RGBA8888: |
| 163 | { |
| 164 | switch(channel) |
| 165 | { |
| 166 | case Channel::R: |
| 167 | return 0; |
| 168 | case Channel::G: |
| 169 | return 1; |
| 170 | case Channel::B: |
| 171 | return 2; |
| 172 | case Channel::A: |
| 173 | return 3; |
| 174 | default: |
| 175 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | case Format::YUYV422: |
| 180 | { |
| 181 | switch(channel) |
| 182 | { |
| 183 | case Channel::Y: |
| 184 | return 0; |
| 185 | case Channel::U: |
| 186 | return 1; |
| 187 | case Channel::V: |
| 188 | return 3; |
| 189 | default: |
| 190 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 191 | return 0; |
| 192 | } |
| 193 | } |
| 194 | case Format::UYVY422: |
| 195 | { |
| 196 | switch(channel) |
| 197 | { |
| 198 | case Channel::Y: |
| 199 | return 1; |
| 200 | case Channel::U: |
| 201 | return 0; |
| 202 | case Channel::V: |
| 203 | return 2; |
| 204 | default: |
| 205 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 206 | return 0; |
| 207 | } |
| 208 | } |
| 209 | case Format::NV12: |
| 210 | { |
| 211 | switch(channel) |
| 212 | { |
| 213 | case Channel::Y: |
| 214 | return 0; |
| 215 | case Channel::U: |
| 216 | return 0; |
| 217 | case Channel::V: |
| 218 | return 1; |
| 219 | default: |
| 220 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | case Format::NV21: |
| 225 | { |
| 226 | switch(channel) |
| 227 | { |
| 228 | case Channel::Y: |
| 229 | return 0; |
| 230 | case Channel::U: |
| 231 | return 1; |
| 232 | case Channel::V: |
| 233 | return 0; |
| 234 | default: |
| 235 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | case Format::YUV444: |
| 240 | case Format::IYUV: |
| 241 | { |
| 242 | switch(channel) |
| 243 | { |
| 244 | case Channel::Y: |
| 245 | return 0; |
| 246 | case Channel::U: |
| 247 | return 0; |
| 248 | case Channel::V: |
| 249 | return 0; |
| 250 | default: |
| 251 | ARM_COMPUTE_ERROR("Not supported channel"); |
| 252 | return 0; |
| 253 | } |
| 254 | } |
| 255 | default: |
| 256 | ARM_COMPUTE_ERROR("Not supported format"); |
| 257 | return 0; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** Return the number of planes for a given format |
| 262 | * |
| 263 | * @param[in] format Input format |
| 264 | * |
| 265 | * @return The number of planes for a given image format. |
| 266 | */ |
| 267 | inline size_t num_planes_from_format(Format format) |
| 268 | { |
| 269 | switch(format) |
| 270 | { |
| 271 | case Format::U8: |
| 272 | case Format::S16: |
| 273 | case Format::U16: |
| 274 | case Format::S32: |
| 275 | case Format::U32: |
| 276 | case Format::BFLOAT16: |
| 277 | case Format::F16: |
| 278 | case Format::F32: |
| 279 | case Format::RGB888: |
| 280 | case Format::RGBA8888: |
| 281 | case Format::YUYV422: |
| 282 | case Format::UYVY422: |
| 283 | return 1; |
| 284 | case Format::NV12: |
| 285 | case Format::NV21: |
| 286 | return 2; |
| 287 | case Format::IYUV: |
| 288 | case Format::YUV444: |
| 289 | return 3; |
| 290 | default: |
| 291 | ARM_COMPUTE_ERROR("Not supported format"); |
| 292 | return 0; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** Return the number of channels for a given single-planar pixel format |
| 297 | * |
| 298 | * @param[in] format Input format |
| 299 | * |
| 300 | * @return The number of channels for a given image format. |
| 301 | */ |
| 302 | inline size_t num_channels_from_format(Format format) |
| 303 | { |
| 304 | switch(format) |
| 305 | { |
| 306 | case Format::U8: |
| 307 | case Format::U16: |
| 308 | case Format::S16: |
| 309 | case Format::U32: |
| 310 | case Format::S32: |
| 311 | case Format::BFLOAT16: |
| 312 | case Format::F16: |
| 313 | case Format::F32: |
| 314 | return 1; |
| 315 | // Because the U and V channels are subsampled |
| 316 | // these formats appear like having only 2 channels: |
| 317 | case Format::YUYV422: |
| 318 | case Format::UYVY422: |
| 319 | return 2; |
| 320 | case Format::UV88: |
| 321 | return 2; |
| 322 | case Format::RGB888: |
| 323 | return 3; |
| 324 | case Format::RGBA8888: |
| 325 | return 4; |
| 326 | //Doesn't make sense for planar formats: |
| 327 | case Format::NV12: |
| 328 | case Format::NV21: |
| 329 | case Format::IYUV: |
| 330 | case Format::YUV444: |
| 331 | default: |
| 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** Convert a tensor format into a string. |
| 337 | * |
| 338 | * @param[in] format @ref Format to be translated to string. |
| 339 | * |
| 340 | * @return The string describing the format. |
| 341 | */ |
| 342 | const std::string &string_from_format(Format format); |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 343 | } |
| 344 | #endif /*ARM_COMPUTE_CORE_UTILS_FORMATUTILS_H */ |