Skia
2D Graphics Library
SkYUVAInfo.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkYUVAInfo_DEFINED
9 #define SkYUVAInfo_DEFINED
10 
11 #include "include/codec/SkEncodedOrigin.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkTypes.h"
16 
17 #include <array>
18 #include <cstddef>
19 #include <cstdint>
20 #include <tuple>
21 
26 class SK_API SkYUVAInfo {
27 public:
28  enum YUVAChannels { kY, kU, kV, kA, kLast = kA };
29  static constexpr int kYUVAChannelCount = static_cast<int>(YUVAChannels::kLast + 1);
30 
31  struct YUVALocation; // For internal use.
32  using YUVALocations = std::array<YUVALocation, kYUVAChannelCount>;
33 
47  enum class PlaneConfig {
48  kUnknown,
49 
50  kY_U_V,
51  kY_V_U,
52  kY_UV,
53  kY_VU,
54  kYUV,
55  kUYV,
56 
57  kY_U_V_A,
58  kY_V_U_A,
59  kY_UV_A,
60  kY_VU_A,
61  kYUVA,
62  kUYVA,
63 
64  kLast = kUYVA
65  };
66 
73  enum class Subsampling {
74  kUnknown,
75 
76  k444,
77  k422,
78  k420,
79  k440,
80  k411,
81  k410,
82 
83  kLast = k410
84  };
85 
91  enum class Siting {
95  kCentered,
96  };
97 
98  static constexpr int kMaxPlanes = 4;
99 
101  static std::tuple<int, int> SubsamplingFactors(Subsampling);
102 
108  static std::tuple<int, int> PlaneSubsamplingFactors(PlaneConfig, Subsampling, int planeIdx);
109 
118  static int PlaneDimensions(SkISize imageDimensions,
119  PlaneConfig,
120  Subsampling,
121  SkEncodedOrigin,
122  SkISize planeDimensions[kMaxPlanes]);
123 
125  static constexpr int NumPlanes(PlaneConfig);
126 
131  static constexpr int NumChannelsInPlane(PlaneConfig, int i);
132 
138  static YUVALocations GetYUVALocations(PlaneConfig, const uint32_t* planeChannelFlags);
139 
141  static bool HasAlpha(PlaneConfig);
142 
143  SkYUVAInfo() = default;
144  SkYUVAInfo(const SkYUVAInfo&) = default;
145 
150  SkYUVAInfo(SkISize dimensions,
151  PlaneConfig,
152  Subsampling,
154  SkEncodedOrigin origin = kTopLeft_SkEncodedOrigin,
155  Siting sitingX = Siting::kCentered,
156  Siting sitingY = Siting::kCentered);
157 
158  SkYUVAInfo& operator=(const SkYUVAInfo& that) = default;
159 
160  PlaneConfig planeConfig() const { return fPlaneConfig; }
161  Subsampling subsampling() const { return fSubsampling; }
162 
163  std::tuple<int, int> planeSubsamplingFactors(int planeIdx) const {
164  return PlaneSubsamplingFactors(fPlaneConfig, fSubsampling, planeIdx);
165  }
166 
171  SkISize dimensions() const { return fDimensions; }
172  int width() const { return fDimensions.width(); }
173  int height() const { return fDimensions.height(); }
174 
175  SkYUVColorSpace yuvColorSpace() const { return fYUVColorSpace; }
176  Siting sitingX() const { return fSitingX; }
177  Siting sitingY() const { return fSitingY; }
178 
179  SkEncodedOrigin origin() const { return fOrigin; }
180 
182  return SkEncodedOriginToMatrix(fOrigin, this->width(), this->height());
183  }
184 
185  bool hasAlpha() const { return HasAlpha(fPlaneConfig); }
186 
192  int planeDimensions(SkISize planeDimensions[kMaxPlanes]) const {
193  return PlaneDimensions(fDimensions, fPlaneConfig, fSubsampling, fOrigin, planeDimensions);
194  }
195 
201  size_t computeTotalBytes(const size_t rowBytes[kMaxPlanes],
202  size_t planeSizes[kMaxPlanes] = nullptr) const;
203 
204  int numPlanes() const { return NumPlanes(fPlaneConfig); }
205 
206  int numChannelsInPlane(int i) const { return NumChannelsInPlane(fPlaneConfig, i); }
207 
214  YUVALocations toYUVALocations(const uint32_t* channelFlags) const;
215 
223 
229 
230  bool operator==(const SkYUVAInfo& that) const;
231  bool operator!=(const SkYUVAInfo& that) const { return !(*this == that); }
232 
233  bool isValid() const { return fPlaneConfig != PlaneConfig::kUnknown; }
234 
235 private:
236  SkISize fDimensions = {0, 0};
237 
238  PlaneConfig fPlaneConfig = PlaneConfig::kUnknown;
239  Subsampling fSubsampling = Subsampling::kUnknown;
240 
242 
247  SkEncodedOrigin fOrigin = kTopLeft_SkEncodedOrigin;
248 
249  Siting fSitingX = Siting::kCentered;
250  Siting fSitingY = Siting::kCentered;
251 };
252 
253 constexpr int SkYUVAInfo::NumPlanes(PlaneConfig planeConfig) {
254  switch (planeConfig) {
255  case PlaneConfig::kUnknown: return 0;
256  case PlaneConfig::kY_U_V: return 3;
257  case PlaneConfig::kY_V_U: return 3;
258  case PlaneConfig::kY_UV: return 2;
259  case PlaneConfig::kY_VU: return 2;
260  case PlaneConfig::kYUV: return 1;
261  case PlaneConfig::kUYV: return 1;
262  case PlaneConfig::kY_U_V_A: return 4;
263  case PlaneConfig::kY_V_U_A: return 4;
264  case PlaneConfig::kY_UV_A: return 3;
265  case PlaneConfig::kY_VU_A: return 3;
266  case PlaneConfig::kYUVA: return 1;
267  case PlaneConfig::kUYVA: return 1;
268  }
269  SkUNREACHABLE;
270 }
271 
272 constexpr int SkYUVAInfo::NumChannelsInPlane(PlaneConfig config, int i) {
273  switch (config) {
275  return 0;
276 
279  return i >= 0 && i < 3 ? 1 : 0;
282  switch (i) {
283  case 0: return 1;
284  case 1: return 2;
285  default: return 0;
286  }
289  return i == 0 ? 3 : 0;
292  return i >= 0 && i < 4 ? 1 : 0;
295  switch (i) {
296  case 0: return 1;
297  case 1: return 2;
298  case 2: return 1;
299  default: return 0;
300  }
303  return i == 0 ? 4 : 0;
304  }
305  return 0;
306 }
307 
308 #endif
SkYUVColorSpace
Definition: SkImageInfo.h:68
@ kIdentity_SkYUVColorSpace
maps Y->R, U->G, V->B
Definition: SkImageInfo.h:79
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
Specifies the structure of planes for a YUV image with optional alpha.
Definition: SkYUVAInfo.h:26
SkYUVAInfo(const SkYUVAInfo &)=default
int width() const
Definition: SkYUVAInfo.h:172
Siting sitingY() const
Definition: SkYUVAInfo.h:177
Siting
Describes how subsampled chroma values are sited relative to luma values.
Definition: SkYUVAInfo.h:91
SkMatrix originMatrix() const
Definition: SkYUVAInfo.h:181
SkYUVAInfo & operator=(const SkYUVAInfo &that)=default
SkYUVAInfo()=default
SkYUVColorSpace yuvColorSpace() const
Definition: SkYUVAInfo.h:175
int numPlanes() const
Definition: SkYUVAInfo.h:204
YUVALocations toYUVALocations(const uint32_t *channelFlags) const
Given a set of channel flags for each plane, converts this->planeConfig() to YUVALocations representa...
PlaneConfig
Specifies how YUV (and optionally A) are divided among planes.
Definition: SkYUVAInfo.h:47
@ kY_U_V_A
Plane 0: Y, Plane 1: U, Plane 2: V, Plane 3: A.
@ kY_U_V
Plane 0: Y, Plane 1: U, Plane 2: V.
@ kYUVA
Plane 0: YUVA.
@ kY_V_U_A
Plane 0: Y, Plane 1: V, Plane 2: U, Plane 3: A.
@ kUYVA
Plane 0: UYVA.
@ kY_VU
Plane 0: Y, Plane 1: VU.
@ kY_UV
Plane 0: Y, Plane 1: UV.
@ kY_VU_A
Plane 0: Y, Plane 1: VU, Plane 2: A.
@ kY_V_U
Plane 0: Y, Plane 1: V, Plane 2: U.
@ kY_UV_A
Plane 0: Y, Plane 1: UV, Plane 2: A.
std::tuple< int, int > planeSubsamplingFactors(int planeIdx) const
Definition: SkYUVAInfo.h:163
bool hasAlpha() const
Definition: SkYUVAInfo.h:185
YUVAChannels
Definition: SkYUVAInfo.h:28
@ kA
Definition: SkYUVAInfo.h:28
static constexpr int NumChannelsInPlane(PlaneConfig, int i)
Number of Y, U, V, A channels in the ith plane for a given PlaneConfig (or 0 if i is invalid).
Definition: SkYUVAInfo.h:272
Subsampling
UV subsampling is also specified in the enum value names using J:a:b notation (e.g.
Definition: SkYUVAInfo.h:73
size_t computeTotalBytes(const size_t rowBytes[kMaxPlanes], size_t planeSizes[kMaxPlanes]=nullptr) const
Given a per-plane row bytes, determine size to allocate for all planes.
SkYUVAInfo makeDimensions(SkISize) const
Makes a SkYUVAInfo that is identical to this one but with the passed dimensions.
SkYUVAInfo(SkISize dimensions, PlaneConfig, Subsampling, SkYUVColorSpace, SkEncodedOrigin origin=kTopLeft_SkEncodedOrigin, Siting sitingX=Siting::kCentered, Siting sitingY=Siting::kCentered)
'dimensions' should specify the size of the full resolution image (after planes have been oriented to...
int height() const
Definition: SkYUVAInfo.h:173
static constexpr int NumPlanes(PlaneConfig)
Number of planes for a given PlaneConfig.
Definition: SkYUVAInfo.h:253
SkYUVAInfo makeSubsampling(SkYUVAInfo::Subsampling) const
Makes a SkYUVAInfo that is identical to this one but with the passed Subsampling.
static YUVALocations GetYUVALocations(PlaneConfig, const uint32_t *planeChannelFlags)
Given a PlaneConfig and a set of channel flags for each plane, convert to YUVALocations representatio...
PlaneConfig planeConfig() const
Definition: SkYUVAInfo.h:160
int planeDimensions(SkISize planeDimensions[kMaxPlanes]) const
Returns the number of planes and initializes planeDimensions[0]..planeDimensions[<ret>] to the expect...
Definition: SkYUVAInfo.h:192
std::array< YUVALocation, kYUVAChannelCount > YUVALocations
Definition: SkYUVAInfo.h:32
static bool HasAlpha(PlaneConfig)
Does the PlaneConfig have alpha values?
SkEncodedOrigin origin() const
Definition: SkYUVAInfo.h:179
static std::tuple< int, int > PlaneSubsamplingFactors(PlaneConfig, Subsampling, int planeIdx)
SubsamplingFactors(Subsampling) if planedIdx refers to a U/V plane and otherwise {1,...
bool isValid() const
Definition: SkYUVAInfo.h:233
Subsampling subsampling() const
Definition: SkYUVAInfo.h:161
bool operator!=(const SkYUVAInfo &that) const
Definition: SkYUVAInfo.h:231
SkISize dimensions() const
Dimensions of the full resolution image (after planes have been oriented to how the image is displaye...
Definition: SkYUVAInfo.h:171
static int PlaneDimensions(SkISize imageDimensions, PlaneConfig, Subsampling, SkEncodedOrigin, SkISize planeDimensions[kMaxPlanes])
Given image dimensions, a planer configuration, subsampling, and origin, determine the expected size ...
static std::tuple< int, int > SubsamplingFactors(Subsampling)
ratio of Y/A values to U/V values in x and y.
int numChannelsInPlane(int i) const
Definition: SkYUVAInfo.h:206
Siting sitingX() const
Definition: SkYUVAInfo.h:176
bool operator==(const SkYUVAInfo &that) const
Definition: SkSize.h:15