fourcc.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_GPU_CHROMEOS_FOURCC_H_
  5. #define MEDIA_GPU_CHROMEOS_FOURCC_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "media/base/video_types.h"
  9. #include "media/gpu/buildflags.h"
  10. #include "media/gpu/media_gpu_export.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace media {
  13. // Composes a Fourcc value.
  14. constexpr uint32_t ComposeFourcc(char a, char b, char c, char d) {
  15. return static_cast<uint32_t>(a) | (static_cast<uint32_t>(b) << 8) |
  16. (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24);
  17. }
  18. // Fourcc enum holder and converters.
  19. // Usage:
  20. // Fourcc f1(Fourcc::NV12);
  21. // EXPECT_EQ("NV12", f1.ToString());
  22. // Fourcc f2 = Fourcc::FromVideoPixelFormat(PIXEL_FORMAT_NV12);
  23. // EXPECT_EQ(f2, f1);
  24. class MEDIA_GPU_EXPORT Fourcc {
  25. public:
  26. enum Value : uint32_t {
  27. // YUV420 single-planar formats.
  28. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-yuv420.html
  29. // Maps to PIXEL_FORMAT_I420, V4L2_PIX_FMT_YUV420, VA_FOURCC_I420.
  30. // 12bpp YUV planar 1x1 Y, 2x2 UV samples.
  31. YU12 = ComposeFourcc('Y', 'U', '1', '2'),
  32. // Maps to PIXEL_FORMAT_YV12, V4L2_PIX_FMT_YVU420, VA_FOURCC_YV12.
  33. // 12bpp YVU planar 1x1 Y, 2x2 VU samples.
  34. YV12 = ComposeFourcc('Y', 'V', '1', '2'),
  35. // YUV420 multi-planar format.
  36. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-yuv420m.html
  37. // Maps to PIXEL_FORMAT_I420, V4L2_PIX_FMT_YUV420M.
  38. YM12 = ComposeFourcc('Y', 'M', '1', '2'),
  39. // Maps to PIXEL_FORMAT_YV12, V4L2_PIX_FMT_YVU420M.
  40. YM21 = ComposeFourcc('Y', 'M', '2', '1'),
  41. // YUYV format.
  42. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-yuyv.html
  43. // Maps to PIXEL_FORMAT_YUY2, V4L2_PIX_FMT_YUYV, VA_FOURCC_YUY2.
  44. // 16bpp YUV planar (YUV 4:2:2), YUYV (byte-order), 1 plane.
  45. YUYV = ComposeFourcc('Y', 'U', 'Y', 'V'),
  46. // NV12 single-planar format.
  47. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-nv12.html
  48. // Maps to PIXEL_FORMAT_NV12, V4L2_PIX_FMT_NV12, VA_FOURCC_NV12.
  49. // 12bpp with Y plane followed by a 2x2 interleaved UV plane.
  50. NV12 = ComposeFourcc('N', 'V', '1', '2'),
  51. // Maps to PIXEL_FORMAT_NV21, V4L2_PIX_FMT_NV21, VA_FOURCC_NV21.
  52. // 12bpp with Y plane followed by a 2x2 interleaved VU plane.
  53. NV21 = ComposeFourcc('N', 'V', '2', '1'),
  54. // NV12 multi-planar format.
  55. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-nv12m.html
  56. // Maps to PIXEL_FORMAT_NV12, V4L2_PIX_FMT_NV12M,
  57. NM12 = ComposeFourcc('N', 'M', '1', '2'),
  58. // Maps to PIXEL_FORMAT_NV21, V4L2_PIX_FMT_NV21M.
  59. NM21 = ComposeFourcc('N', 'M', '2', '1'),
  60. // YUV422 single-planar format.
  61. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-yuv422p.html
  62. // Maps to PIXEL_FORMAT_I422, V4L2_PIX_FMT_YUV422P.
  63. YU16 = ComposeFourcc('4', '2', '2', 'P'),
  64. // YUV422 multi-planar format.
  65. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-yuv422m.html
  66. // Maps to PIXEL_FORMAT_I422, V4L2_PIX_FMT_YUV422M
  67. // 16bpp YUV planar 1x1 Y, 2x1 UV samples.
  68. YM16 = ComposeFourcc('Y', 'M', '1', '6'),
  69. // V4L2 proprietary format.
  70. // https://linuxtv.org/downloads/v4l-dvb-apis-new/userspace-api/v4l/pixfmt-reserved.html
  71. // Maps to V4L2_PIX_FMT_MT21C.
  72. // It is used for MT8173 hardware video decoder output and should be
  73. // converted by MT8173 image processor for compositor to render.
  74. MT21 = ComposeFourcc('M', 'T', '2', '1'),
  75. // Maps to V4L2_PIX_FMT_MM21.
  76. // It is used for MT8183 hardware video decoder.
  77. MM21 = ComposeFourcc('M', 'M', '2', '1'),
  78. // Two-plane 10-bit YUV 4:2:0. Each sample is a two-byte little-endian value
  79. // with the bottom six bits ignored.
  80. P010 = ComposeFourcc('P', '0', '1', '0'),
  81. };
  82. explicit constexpr Fourcc(Fourcc::Value fourcc) : value_(fourcc) {}
  83. bool operator==(const Fourcc& rhs) const { return value_ == rhs.value_; }
  84. // Factory methods:
  85. // Builds a Fourcc from a given fourcc code. This will return a valid
  86. // Fourcc if the argument is part of the |Value| enum, or nullopt otherwise.
  87. static absl::optional<Fourcc> FromUint32(uint32_t fourcc);
  88. // Converts a VideoPixelFormat to Fourcc.
  89. // Returns nullopt for invalid input.
  90. // Note that a VideoPixelFormat may have two Fourcc counterparts. Caller has
  91. // to specify if it is for single-planar or multi-planar format.
  92. static absl::optional<Fourcc> FromVideoPixelFormat(
  93. VideoPixelFormat pixel_format,
  94. bool single_planar = true);
  95. #if BUILDFLAG(USE_V4L2_CODEC)
  96. // Converts a V4L2PixFmt to Fourcc.
  97. // Returns nullopt for invalid input.
  98. static absl::optional<Fourcc> FromV4L2PixFmt(uint32_t v4l2_pix_fmt);
  99. #endif // BUILDFLAG(USE_V4L2_CODEC)
  100. #if BUILDFLAG(USE_VAAPI)
  101. // Converts a VAFourCC to Fourcc.
  102. // Returns nullopt for invalid input.
  103. static absl::optional<Fourcc> FromVAFourCC(uint32_t va_fourcc);
  104. #endif // BUILDFLAG(USE_VAAPI)
  105. // Value getters:
  106. // Returns the VideoPixelFormat counterpart of the value.
  107. // Returns PIXEL_FORMAT_UNKNOWN if no mapping is found.
  108. VideoPixelFormat ToVideoPixelFormat() const;
  109. #if BUILDFLAG(USE_V4L2_CODEC)
  110. // Returns the V4L2PixFmt counterpart of the value.
  111. // Returns 0 if no mapping is found.
  112. uint32_t ToV4L2PixFmt() const;
  113. #endif // BUILDFLAG(USE_V4L2_CODEC)
  114. #if BUILDFLAG(USE_VAAPI)
  115. // Returns the VAFourCC counterpart of the value.
  116. // Returns nullopt if no mapping is found.
  117. absl::optional<uint32_t> ToVAFourCC() const;
  118. #endif // BUILDFLAG(USE_VAAPI)
  119. // Returns the single-planar Fourcc of the value. If value is a single-planar,
  120. // returns the same Fourcc. Returns nullopt if no mapping is found.
  121. absl::optional<Fourcc> ToSinglePlanar() const;
  122. // Returns whether |value_| is multi planar format.
  123. bool IsMultiPlanar() const;
  124. // Outputs human readable fourcc string, e.g. "NV12".
  125. std::string ToString() const;
  126. private:
  127. Value value_;
  128. };
  129. MEDIA_GPU_EXPORT bool operator!=(const Fourcc& lhs, const Fourcc& rhs);
  130. } // namespace media
  131. #endif // MEDIA_GPU_CHROMEOS_FOURCC_H_