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 // Note: ported from Chromium commit head: 27c98933749f
5
6 #ifndef FOURCC_H_
7 #define FOURCC_H_
8
9 #include <stdint.h>
10 #include <string>
11
12 #include "base/optional.h"
13 #include "video_pixel_format.h"
14
15 namespace media {
16
17 // Composes a Fourcc value.
ComposeFourcc(char a,char b,char c,char d)18 constexpr uint32_t ComposeFourcc(char a, char b, char c, char d) {
19 return static_cast<uint32_t>(a) | (static_cast<uint32_t>(b) << 8) |
20 (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24);
21 }
22
23 // Fourcc enum holder and converters.
24 // Usage:
25 // Fourcc f1(Fourcc::AR24);
26 // EXPECT_EQ("AR24", f1.ToString());
27 // Fourcc f2 = Fourcc::FromVideoPixelFormat(PIXEL_FORMAT_ARGB);
28 // EXPECT_EQ(f2, f1);
29 class Fourcc {
30 public:
31 enum Value : uint32_t {
32 // RGB formats.
33 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-rgb.html
34 // Maps to PIXEL_FORMAT_ARGB, V4L2_PIX_FMT_ABGR32, VA_FOURCC_BGRA.
35 // 32bpp BGRA (byte-order), 1 plane.
36 AR24 = ComposeFourcc('A', 'R', '2', '4'),
37
38 // Maps to PIXEL_FORMAT_ABGR, V4L2_PIX_FMT_RGBA32, VA_FOURCC_RGBA.
39 // 32bpp RGBA (byte-order), 1 plane
40 AB24 = ComposeFourcc('A', 'B', '2', '4'),
41
42 // Maps to PIXEL_FORMAT_XRGB, V4L2_PIX_FMT_XBGR32, VA_FOURCC_BGRX.
43 // 32bpp BGRX (byte-order), 1 plane.
44 XR24 = ComposeFourcc('X', 'R', '2', '4'),
45
46 // Maps to PIXEL_FORMAT_XBGR, V4L2_PIX_FMT_RGBX32, VA_FOURCC_RGBX.
47 // 32bpp RGBX (byte-order), 1 plane.
48 XB24 = ComposeFourcc('X', 'B', '2', '4'),
49
50 // Maps to PIXEL_FORMAT_BGRA, V4L2_PIX_FMT_RGB32, VA_FOURCC_ARGB.
51 // 32bpp ARGB (byte-order), 1 plane.
52 // Note that V4L2_PIX_FMT_RGB32("RGB4") is deprecated and replaced by
53 // V4L2_PIX_FMT_ARGB32("BA24"), however, some board relies on the fourcc
54 // mapping so we keep it as-is.
55 RGB4 = ComposeFourcc('R', 'G', 'B', '4'),
56
57 // YUV420 single-planar formats.
58 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-yuv420.html
59 // Maps to PIXEL_FORMAT_I420, V4L2_PIX_FMT_YUV420, VA_FOURCC_I420.
60 // 12bpp YUV planar 1x1 Y, 2x2 UV samples.
61 YU12 = ComposeFourcc('Y', 'U', '1', '2'),
62 // Maps to PIXEL_FORMAT_YV12, V4L2_PIX_FMT_YVU420, VA_FOURCC_YV12.
63 // 12bpp YVU planar 1x1 Y, 2x2 VU samples.
64 YV12 = ComposeFourcc('Y', 'V', '1', '2'),
65
66 // YUV420 multi-planar format.
67 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-yuv420m.htm
68 // Maps to PIXEL_FORMAT_I420, V4L2_PIX_FMT_YUV420M.
69 YM12 = ComposeFourcc('Y', 'M', '1', '2'),
70 // Maps to PIXEL_FORMAT_YV12, V4L2_PIX_FMT_YVU420M.
71 YM21 = ComposeFourcc('Y', 'M', '2', '1'),
72
73 // YUYV format.
74 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-yuyv.html
75 // Maps to PIXEL_FORMAT_YUY2, V4L2_PIX_FMT_YUYV, VA_FOURCC_YUY2.
76 // 16bpp YUV planar (YUV 4:2:2), YUYV (byte-order), 1 plane.
77 YUYV = ComposeFourcc('Y', 'U', 'Y', 'V'),
78
79 // NV12 single-planar format.
80 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-nv12.html
81 // Maps to PIXEL_FORMAT_NV12, V4L2_PIX_FMT_NV12, VA_FOURCC_NV12.
82 // 12bpp with Y plane followed by a 2x2 interleaved UV plane.
83 NV12 = ComposeFourcc('N', 'V', '1', '2'),
84 // Maps to PIXEL_FORMAT_NV21, V4L2_PIX_FMT_NV21, VA_FOURCC_NV21.
85 // 12bpp with Y plane followed by a 2x2 interleaved VU plane.
86 NV21 = ComposeFourcc('N', 'V', '2', '1'),
87
88 // NV12 multi-planar format.
89 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-nv12m.html
90 // Maps to PIXEL_FORMAT_NV12, V4L2_PIX_FMT_NV12M,
91 NM12 = ComposeFourcc('N', 'M', '1', '2'),
92 // Maps to PIXEL_FORMAT_NV21, V4L2_PIX_FMT_NV21M.
93 NM21 = ComposeFourcc('N', 'M', '2', '1'),
94
95 // YUV422 multi-planar format.
96 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-yuv422m.html
97 // Maps to PIXEL_FORMAT_I422, V4L2_PIX_FMT_YUV422M
98 // 16bpp YUV planar 1x1 Y, 2x1 UV samples.
99 YM16 = ComposeFourcc('Y', 'M', '1', '6'),
100
101 // V4L2 proprietary format.
102 // https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/pixfmt-reserved.html
103 // Maps to V4L2_PIX_FMT_MT21C.
104 // It is used for MT8173 hardware video decoder output and should be
105 // converted by MT8173 image processor for compositor to render.
106 MT21 = ComposeFourcc('M', 'T', '2', '1'),
107 // Maps to V4L2_PIX_FMT_MM21.
108 // It is used for MT8183 hardware video decoder.
109 MM21 = ComposeFourcc('M', 'M', '2', '1'),
110 };
111
112 explicit Fourcc(Fourcc::Value fourcc);
113 Fourcc& operator=(const Fourcc& fourcc);
114 ~Fourcc();
115
116 bool operator==(const Fourcc& rhs) const { return value_ == rhs.value_; }
117
118 // Factory methods:
119
120 // Builds a Fourcc from a given fourcc code. This will return a valid
121 // Fourcc if the argument is part of the |Value| enum, or nullopt otherwise.
122 static base::Optional<Fourcc> FromUint32(uint32_t fourcc);
123
124 // Converts a VideoPixelFormat to Fourcc.
125 // Returns nullopt for invalid input.
126 // Note that a VideoPixelFormat may have two Fourcc counterparts. Caller has
127 // to specify if it is for single-planar or multi-planar format.
128 static base::Optional<Fourcc> FromVideoPixelFormat(
129 VideoPixelFormat pixel_format,
130 bool single_planar = true);
131 // Converts a V4L2PixFmt to Fourcc.
132 // Returns nullopt for invalid input.
133 static base::Optional<Fourcc> FromV4L2PixFmt(uint32_t v4l2_pix_fmt);
134
135 // Value getters:
136 // Returns the VideoPixelFormat counterpart of the value.
137 // Returns PIXEL_FORMAT_UNKNOWN if no mapping is found.
138 VideoPixelFormat ToVideoPixelFormat() const;
139 // Returns the V4L2PixFmt counterpart of the value.
140 // Returns 0 if no mapping is found.
141 uint32_t ToV4L2PixFmt() const;
142
143 // Returns the single-planar Fourcc of the value. If value is a single-planar,
144 // returns the same Fourcc. Returns nullopt if no mapping is found.
145 base::Optional<Fourcc> ToSinglePlanar() const;
146
147 // Returns whether |value_| is multi planar format.
148 bool IsMultiPlanar() const;
149
150 // Outputs human readable fourcc string, e.g. "NV12".
151 std::string ToString() const;
152
153 private:
154 Value value_;
155 };
156
157 bool operator!=(const Fourcc& lhs, const Fourcc& rhs);
158
159 } // namespace media
160
161 #endif // FOURCC_H_
162