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