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/SkSize.h"
14 
15 #include <array>
16 #include <tuple>
17 
23 public:
24  enum YUVAChannels { kY, kU, kV, kA, kLast = kA };
25  static constexpr int kYUVAChannelCount = static_cast<int>(YUVAChannels::kLast + 1);
26 
27  struct YUVALocation; // For internal use.
28  using YUVALocations = std::array<YUVALocation, kYUVAChannelCount>;
29 
43  enum class PlaneConfig {
44  kUnknown,
45 
46  kY_U_V,
47  kY_V_U,
48  kY_UV,
49  kY_VU,
50  kYUV,
51  kUYV,
52 
53  kY_U_V_A,
54  kY_V_U_A,
55  kY_UV_A,
56  kY_VU_A,
57  kYUVA,
58  kUYVA,
59 
60  kLast = kUYVA
61  };
62 
69  enum class Subsampling {
70  kUnknown,
71 
72  k444,
73  k422,
74  k420,
75  k440,
76  k411,
77  k410,
78 
79  kLast = k410
80  };
81 
87  enum class Siting {
91  kCentered,
92  };
93 
94  static constexpr int kMaxPlanes = 4;
95 
97  static std::tuple<int, int> SubsamplingFactors(Subsampling);
98 
104  static std::tuple<int, int> PlaneSubsamplingFactors(PlaneConfig, Subsampling, int planeIdx);
105 
114  static int PlaneDimensions(SkISize imageDimensions,
115  PlaneConfig,
116  Subsampling,
117  SkEncodedOrigin,
118  SkISize planeDimensions[kMaxPlanes]);
119 
121  static constexpr int NumPlanes(PlaneConfig);
122 
127  static constexpr int NumChannelsInPlane(PlaneConfig, int i);
128 
134  static YUVALocations GetYUVALocations(PlaneConfig, const uint32_t* planeChannelFlags);
135 
137  static bool HasAlpha(PlaneConfig);
138 
139  SkYUVAInfo() = default;
140  SkYUVAInfo(const SkYUVAInfo&) = default;
141 
146  SkYUVAInfo(SkISize dimensions,
147  PlaneConfig,
148  Subsampling,
150  SkEncodedOrigin origin = kTopLeft_SkEncodedOrigin,
151  Siting sitingX = Siting::kCentered,
152  Siting sitingY = Siting::kCentered);
153 
154  SkYUVAInfo& operator=(const SkYUVAInfo& that) = default;
155 
156  PlaneConfig planeConfig() const { return fPlaneConfig; }
157  Subsampling subsampling() const { return fSubsampling; }
158 
159  std::tuple<int, int> planeSubsamplingFactors(int planeIdx) const {
160  return PlaneSubsamplingFactors(fPlaneConfig, fSubsampling, planeIdx);
161  }
162 
167  SkISize dimensions() const { return fDimensions; }
168  int width() const { return fDimensions.width(); }
169  int height() const { return fDimensions.height(); }
170 
171  SkYUVColorSpace yuvColorSpace() const { return fYUVColorSpace; }
172  Siting sitingX() const { return fSitingX; }
173  Siting sitingY() const { return fSitingY; }
174 
175  SkEncodedOrigin origin() const { return fOrigin; }
176 
178  return SkEncodedOriginToMatrix(fOrigin, this->width(), this->height());
179  }
180 
181  bool hasAlpha() const { return HasAlpha(fPlaneConfig); }
182 
188  int planeDimensions(SkISize planeDimensions[kMaxPlanes]) const {
189  return PlaneDimensions(fDimensions, fPlaneConfig, fSubsampling, fOrigin, planeDimensions);
190  }
191 
197  size_t computeTotalBytes(const size_t rowBytes[kMaxPlanes],
198  size_t planeSizes[kMaxPlanes] = nullptr) const;
199 
200  int numPlanes() const { return NumPlanes(fPlaneConfig); }
201 
202  int numChannelsInPlane(int i) const { return NumChannelsInPlane(fPlaneConfig, i); }
203 
210  YUVALocations toYUVALocations(const uint32_t* channelFlags) const;
211 
219 
225 
226  bool operator==(const SkYUVAInfo& that) const;
227  bool operator!=(const SkYUVAInfo& that) const { return !(*this == that); }
228 
229  bool isValid() const { return fPlaneConfig != PlaneConfig::kUnknown; }
230 
231 private:
232  SkISize fDimensions = {0, 0};
233 
234  PlaneConfig fPlaneConfig = PlaneConfig::kUnknown;
235  Subsampling fSubsampling = Subsampling::kUnknown;
236 
238 
243  SkEncodedOrigin fOrigin = kTopLeft_SkEncodedOrigin;
244 
245  Siting fSitingX = Siting::kCentered;
246  Siting fSitingY = Siting::kCentered;
247 };
248 
249 constexpr int SkYUVAInfo::NumPlanes(PlaneConfig planeConfig) {
250  switch (planeConfig) {
251  case PlaneConfig::kUnknown: return 0;
252  case PlaneConfig::kY_U_V: return 3;
253  case PlaneConfig::kY_V_U: return 3;
254  case PlaneConfig::kY_UV: return 2;
255  case PlaneConfig::kY_VU: return 2;
256  case PlaneConfig::kYUV: return 1;
257  case PlaneConfig::kUYV: return 1;
258  case PlaneConfig::kY_U_V_A: return 4;
259  case PlaneConfig::kY_V_U_A: return 4;
260  case PlaneConfig::kY_UV_A: return 3;
261  case PlaneConfig::kY_VU_A: return 3;
262  case PlaneConfig::kYUVA: return 1;
263  case PlaneConfig::kUYVA: return 1;
264  }
266 }
267 
268 constexpr int SkYUVAInfo::NumChannelsInPlane(PlaneConfig config, int i) {
269  switch (config) {
271  return 0;
272 
275  return i >= 0 && i < 3 ? 1 : 0;
278  switch (i) {
279  case 0: return 1;
280  case 1: return 2;
281  default: return 0;
282  }
285  return i == 0 ? 3 : 0;
288  return i >= 0 && i < 4 ? 1 : 0;
291  switch (i) {
292  case 0: return 1;
293  case 1: return 2;
294  case 2: return 1;
295  default: return 0;
296  }
299  return i == 0 ? 4 : 0;
300  }
301  return 0;
302 }
303 
304 #endif
SkYUVAInfo::NumPlanes
static constexpr int NumPlanes(PlaneConfig)
Number of planes for a given PlaneConfig.
Definition: SkYUVAInfo.h:249
SkYUVAInfo::dimensions
SkISize dimensions() const
Dimensions of the full resolution image (after planes have been oriented to how the image is displaye...
Definition: SkYUVAInfo.h:167
SkSize.h
SkYUVAInfo::yuvColorSpace
SkYUVColorSpace yuvColorSpace() const
Definition: SkYUVAInfo.h:171
SkYUVColorSpace
SkYUVColorSpace
Definition: SkImageInfo.h:148
SkYUVAInfo::width
int width() const
Definition: SkYUVAInfo.h:168
SkYUVAInfo::planeSubsamplingFactors
std::tuple< int, int > planeSubsamplingFactors(int planeIdx) const
Definition: SkYUVAInfo.h:159
SkYUVAInfo::origin
SkEncodedOrigin origin() const
Definition: SkYUVAInfo.h:175
SkYUVAInfo::makeDimensions
SkYUVAInfo makeDimensions(SkISize) const
Makes a SkYUVAInfo that is identical to this one but with the passed dimensions.
SkYUVAInfo::planeConfig
PlaneConfig planeConfig() const
Definition: SkYUVAInfo.h:156
SkYUVAInfo::SkYUVAInfo
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...
SkYUVAInfo::SubsamplingFactors
static std::tuple< int, int > SubsamplingFactors(Subsampling)
ratio of Y/A values to U/V values in x and y.
SkYUVAInfo::sitingX
Siting sitingX() const
Definition: SkYUVAInfo.h:172
SkColorChannel::kA
@ kA
SkYUVAInfo::GetYUVALocations
static YUVALocations GetYUVALocations(PlaneConfig, const uint32_t *planeChannelFlags)
Given a PlaneConfig and a set of channel flags for each plane, convert to YUVALocations representatio...
SkYUVAInfo::YUVAChannels
YUVAChannels
Definition: SkYUVAInfo.h:24
SkYUVAInfo::HasAlpha
static bool HasAlpha(PlaneConfig)
Does the PlaneConfig have alpha values?
SkYUVAInfo::PlaneConfig::kY_UV_A
@ kY_UV_A
Plane 0: Y, Plane 1: UV, Plane 2: A.
SkYUVAInfo::PlaneConfig::kY_V_U
@ kY_V_U
Plane 0: Y, Plane 1: V, Plane 2: U.
SkYUVAInfo::PlaneConfig::kUYV
@ kUYV
Plane 0: UYV.
SkYUVAInfo::Subsampling
Subsampling
UV subsampling is also specified in the enum value names using J:a:b notation (e.g.
Definition: SkYUVAInfo.h:69
SkYUVAInfo::computeTotalBytes
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.
SK_API
#define SK_API
Definition: SkTypes.h:181
SkYUVAInfo::height
int height() const
Definition: SkYUVAInfo.h:169
SkYUVAInfo::toYUVALocations
YUVALocations toYUVALocations(const uint32_t *channelFlags) const
Given a set of channel flags for each plane, converts this->planeConfig() to YUVALocations representa...
SkYUVAInfo::PlaneSubsamplingFactors
static std::tuple< int, int > PlaneSubsamplingFactors(PlaneConfig, Subsampling, int planeIdx)
SubsamplingFactors(Subsampling) if planedIdx refers to a U/V plane and otherwise {1,...
SkSVGAttribute::kY
@ kY
SkISize
Definition: SkSize.h:13
SkYUVAInfo::planeDimensions
int planeDimensions(SkISize planeDimensions[kMaxPlanes]) const
Returns the number of planes and initializes planeDimensions[0]..planeDimensions[<ret>] to the expect...
Definition: SkYUVAInfo.h:188
SkYUVAInfo::NumChannelsInPlane
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:268
SkYUVAInfo::PlaneConfig::kUYVA
@ kUYVA
Plane 0: UYVA.
SkYUVAInfo::PlaneConfig::kYUVA
@ kYUVA
Plane 0: YUVA.
SkYUVAInfo::isValid
bool isValid() const
Definition: SkYUVAInfo.h:229
SkYUVAInfo::PlaneConfig::kY_VU_A
@ kY_VU_A
Plane 0: Y, Plane 1: VU, Plane 2: A.
SkYUVAInfo::sitingY
Siting sitingY() const
Definition: SkYUVAInfo.h:173
SkYUVAInfo::originMatrix
SkMatrix originMatrix() const
Definition: SkYUVAInfo.h:177
SkYUVAInfo::operator==
bool operator==(const SkYUVAInfo &that) const
SkYUVAInfo::YUVALocations
std::array< YUVALocation, kYUVAChannelCount > YUVALocations
Definition: SkYUVAInfo.h:28
SkYUVAInfo::numChannelsInPlane
int numChannelsInPlane(int i) const
Definition: SkYUVAInfo.h:202
SkUNREACHABLE
#define SkUNREACHABLE
Definition: SkTypes.h:256
SkYUVAInfo::makeSubsampling
SkYUVAInfo makeSubsampling(SkYUVAInfo::Subsampling) const
Makes a SkYUVAInfo that is identical to this one but with the passed Subsampling.
SkMatrix
Definition: SkMatrix.h:48
SkCoverageMode::kLast
@ kLast
SkYUVAInfo
Specifies the structure of planes for a YUV image with optional alpha.
Definition: SkYUVAInfo.h:22
SkYUVAInfo::hasAlpha
bool hasAlpha() const
Definition: SkYUVAInfo.h:181
SkYUVAInfo::numPlanes
int numPlanes() const
Definition: SkYUVAInfo.h:200
SkYUVAInfo::PlaneConfig::kY_UV
@ kY_UV
Plane 0: Y, Plane 1: UV.
SkYUVAInfo::PlaneDimensions
static int PlaneDimensions(SkISize imageDimensions, PlaneConfig, Subsampling, SkEncodedOrigin, SkISize planeDimensions[kMaxPlanes])
Given image dimensions, a planer configuration, subsampling, and origin, determine the expected size ...
kIdentity_SkYUVColorSpace
@ kIdentity_SkYUVColorSpace
maps Y->R, U->G, V->B
Definition: SkImageInfo.h:159
SkYUVAInfo::operator!=
bool operator!=(const SkYUVAInfo &that) const
Definition: SkYUVAInfo.h:227
SkYUVAInfo::PlaneConfig
PlaneConfig
Specifies how YUV (and optionally A) are divided among planes.
Definition: SkYUVAInfo.h:43
SkYUVAInfo::operator=
SkYUVAInfo & operator=(const SkYUVAInfo &that)=default
SkYUVAInfo::Siting
Siting
Describes how subsampled chroma values are sited relative to luma values.
Definition: SkYUVAInfo.h:87
SkYUVAInfo::PlaneConfig::kUnknown
@ kUnknown
SkYUVAInfo::subsampling
Subsampling subsampling() const
Definition: SkYUVAInfo.h:157
SkYUVAInfo::PlaneConfig::kYUV
@ kYUV
Plane 0: YUV.
SkYUVAInfo::PlaneConfig::kY_V_U_A
@ kY_V_U_A
Plane 0: Y, Plane 1: V, Plane 2: U, Plane 3: A.
SkYUVAInfo::SkYUVAInfo
SkYUVAInfo()=default
SkYUVAInfo::SkYUVAInfo
SkYUVAInfo(const SkYUVAInfo &)=default
SkYUVAInfo::PlaneConfig::kY_U_V_A
@ kY_U_V_A
Plane 0: Y, Plane 1: U, Plane 2: V, Plane 3: A.
SkYUVAInfo::PlaneConfig::kY_VU
@ kY_VU
Plane 0: Y, Plane 1: VU.
SkImageInfo.h
SkYUVAInfo::PlaneConfig::kY_U_V
@ kY_U_V
Plane 0: Y, Plane 1: U, Plane 2: V.
SkSVGAttribute::kUnknown
@ kUnknown