1 /*
2  *  Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 // Common definition for video, including fourcc and VideoFormat.
12 
13 #ifndef MEDIA_BASE_VIDEO_COMMON_H_
14 #define MEDIA_BASE_VIDEO_COMMON_H_
15 
16 #include <stdint.h>
17 
18 #include <string>
19 
20 #include "rtc_base/system/rtc_export.h"
21 #include "rtc_base/time_utils.h"
22 
23 namespace cricket {
24 
25 //////////////////////////////////////////////////////////////////////////////
26 // Definition of FourCC codes
27 //////////////////////////////////////////////////////////////////////////////
28 // Convert four characters to a FourCC code.
29 // Needs to be a macro otherwise the OS X compiler complains when the kFormat*
30 // constants are used in a switch.
31 #define CRICKET_FOURCC(a, b, c, d)                                \
32   ((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \
33    (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24))
34 // Some pages discussing FourCC codes:
35 //   http://www.fourcc.org/yuv.php
36 //   http://v4l2spec.bytesex.org/spec/book1.htm
37 //   http://developer.apple.com/quicktime/icefloe/dispatch020.html
38 //   http://msdn.microsoft.com/library/windows/desktop/dd206750.aspx#nv12
39 //   http://people.xiph.org/~xiphmont/containers/nut/nut4cc.txt
40 
41 // FourCC codes grouped according to implementation efficiency.
42 // Primary formats should convert in 1 efficient step.
43 // Secondary formats are converted in 2 steps.
44 // Auxilliary formats call primary converters.
45 enum FourCC {
46   // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed.
47   FOURCC_I420 = CRICKET_FOURCC('I', '4', '2', '0'),
48   FOURCC_I422 = CRICKET_FOURCC('I', '4', '2', '2'),
49   FOURCC_I444 = CRICKET_FOURCC('I', '4', '4', '4'),
50   FOURCC_I411 = CRICKET_FOURCC('I', '4', '1', '1'),
51   FOURCC_I400 = CRICKET_FOURCC('I', '4', '0', '0'),
52   FOURCC_NV21 = CRICKET_FOURCC('N', 'V', '2', '1'),
53   FOURCC_NV12 = CRICKET_FOURCC('N', 'V', '1', '2'),
54   FOURCC_YUY2 = CRICKET_FOURCC('Y', 'U', 'Y', '2'),
55   FOURCC_UYVY = CRICKET_FOURCC('U', 'Y', 'V', 'Y'),
56 
57   // 2 Secondary YUV formats: row biplanar.
58   FOURCC_M420 = CRICKET_FOURCC('M', '4', '2', '0'),
59 
60   // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp.
61   FOURCC_ARGB = CRICKET_FOURCC('A', 'R', 'G', 'B'),
62   FOURCC_BGRA = CRICKET_FOURCC('B', 'G', 'R', 'A'),
63   FOURCC_ABGR = CRICKET_FOURCC('A', 'B', 'G', 'R'),
64   FOURCC_24BG = CRICKET_FOURCC('2', '4', 'B', 'G'),
65   FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '),
66   FOURCC_RGBA = CRICKET_FOURCC('R', 'G', 'B', 'A'),
67   FOURCC_RGBP = CRICKET_FOURCC('R', 'G', 'B', 'P'),  // bgr565.
68   FOURCC_RGBO = CRICKET_FOURCC('R', 'G', 'B', 'O'),  // abgr1555.
69   FOURCC_R444 = CRICKET_FOURCC('R', '4', '4', '4'),  // argb4444.
70 
71   // 4 Secondary RGB formats: 4 Bayer Patterns.
72   FOURCC_RGGB = CRICKET_FOURCC('R', 'G', 'G', 'B'),
73   FOURCC_BGGR = CRICKET_FOURCC('B', 'G', 'G', 'R'),
74   FOURCC_GRBG = CRICKET_FOURCC('G', 'R', 'B', 'G'),
75   FOURCC_GBRG = CRICKET_FOURCC('G', 'B', 'R', 'G'),
76 
77   // 1 Primary Compressed YUV format.
78   FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'),
79 
80   // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias.
81   FOURCC_YV12 = CRICKET_FOURCC('Y', 'V', '1', '2'),
82   FOURCC_YV16 = CRICKET_FOURCC('Y', 'V', '1', '6'),
83   FOURCC_YV24 = CRICKET_FOURCC('Y', 'V', '2', '4'),
84   FOURCC_YU12 = CRICKET_FOURCC('Y', 'U', '1', '2'),  // Linux version of I420.
85   FOURCC_J420 = CRICKET_FOURCC('J', '4', '2', '0'),
86   FOURCC_J400 = CRICKET_FOURCC('J', '4', '0', '0'),
87 
88   // 14 Auxiliary aliases.  CanonicalFourCC() maps these to canonical FOURCC.
89   FOURCC_IYUV = CRICKET_FOURCC('I', 'Y', 'U', 'V'),  // Alias for I420.
90   FOURCC_YU16 = CRICKET_FOURCC('Y', 'U', '1', '6'),  // Alias for I422.
91   FOURCC_YU24 = CRICKET_FOURCC('Y', 'U', '2', '4'),  // Alias for I444.
92   FOURCC_YUYV = CRICKET_FOURCC('Y', 'U', 'Y', 'V'),  // Alias for YUY2.
93   FOURCC_YUVS = CRICKET_FOURCC('y', 'u', 'v', 's'),  // Alias for YUY2 on Mac.
94   FOURCC_HDYC = CRICKET_FOURCC('H', 'D', 'Y', 'C'),  // Alias for UYVY.
95   FOURCC_2VUY = CRICKET_FOURCC('2', 'v', 'u', 'y'),  // Alias for UYVY on Mac.
96   FOURCC_JPEG = CRICKET_FOURCC('J', 'P', 'E', 'G'),  // Alias for MJPG.
97   FOURCC_DMB1 = CRICKET_FOURCC('d', 'm', 'b', '1'),  // Alias for MJPG on Mac.
98   FOURCC_BA81 = CRICKET_FOURCC('B', 'A', '8', '1'),  // Alias for BGGR.
99   FOURCC_RGB3 = CRICKET_FOURCC('R', 'G', 'B', '3'),  // Alias for RAW.
100   FOURCC_BGR3 = CRICKET_FOURCC('B', 'G', 'R', '3'),  // Alias for 24BG.
101   FOURCC_CM32 = CRICKET_FOURCC(0, 0, 0, 32),  // BGRA kCMPixelFormat_32ARGB
102   FOURCC_CM24 = CRICKET_FOURCC(0, 0, 0, 24),  // RAW kCMPixelFormat_24RGB
103 
104   // 1 Auxiliary compressed YUV format set aside for capturer.
105   FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'),
106 };
107 
108 #undef CRICKET_FOURCC
109 
110 // Match any fourcc.
111 
112 // We move this out of the enum because using it in many places caused
113 // the compiler to get grumpy, presumably since the above enum is
114 // backed by an int.
115 static const uint32_t FOURCC_ANY = 0xFFFFFFFF;
116 
117 // Converts fourcc aliases into canonical ones.
118 uint32_t CanonicalFourCC(uint32_t fourcc);
119 
120 // Get FourCC code as a string.
GetFourccName(uint32_t fourcc)121 inline std::string GetFourccName(uint32_t fourcc) {
122   std::string name;
123   name.push_back(static_cast<char>(fourcc & 0xFF));
124   name.push_back(static_cast<char>((fourcc >> 8) & 0xFF));
125   name.push_back(static_cast<char>((fourcc >> 16) & 0xFF));
126   name.push_back(static_cast<char>((fourcc >> 24) & 0xFF));
127   return name;
128 }
129 
130 //////////////////////////////////////////////////////////////////////////////
131 // Definition of VideoFormat.
132 //////////////////////////////////////////////////////////////////////////////
133 
134 // VideoFormat with Plain Old Data for global variables.
135 struct VideoFormatPod {
136   int width;         // Number of pixels.
137   int height;        // Number of pixels.
138   int64_t interval;  // Nanoseconds.
139   uint32_t fourcc;  // Color space. FOURCC_ANY means that any color space is OK.
140 };
141 
142 struct RTC_EXPORT VideoFormat : VideoFormatPod {
143   static const int64_t kMinimumInterval =
144       rtc::kNumNanosecsPerSec / 10000;  // 10k fps.
145 
VideoFormatVideoFormat146   VideoFormat() { Construct(0, 0, 0, 0); }
147 
VideoFormatVideoFormat148   VideoFormat(int w, int h, int64_t interval_ns, uint32_t cc) {
149     Construct(w, h, interval_ns, cc);
150   }
151 
VideoFormatVideoFormat152   explicit VideoFormat(const VideoFormatPod& format) {
153     Construct(format.width, format.height, format.interval, format.fourcc);
154   }
155 
ConstructVideoFormat156   void Construct(int w, int h, int64_t interval_ns, uint32_t cc) {
157     width = w;
158     height = h;
159     interval = interval_ns;
160     fourcc = cc;
161   }
162 
FpsToIntervalVideoFormat163   static int64_t FpsToInterval(int fps) {
164     return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval;
165   }
166 
IntervalToFpsVideoFormat167   static int IntervalToFps(int64_t interval) {
168     if (!interval) {
169       return 0;
170     }
171     return static_cast<int>(rtc::kNumNanosecsPerSec / interval);
172   }
173 
IntervalToFpsFloatVideoFormat174   static float IntervalToFpsFloat(int64_t interval) {
175     if (!interval) {
176       return 0.f;
177     }
178     return static_cast<float>(rtc::kNumNanosecsPerSec) /
179            static_cast<float>(interval);
180   }
181 
182   bool operator==(const VideoFormat& format) const {
183     return width == format.width && height == format.height &&
184            interval == format.interval && fourcc == format.fourcc;
185   }
186 
187   bool operator!=(const VideoFormat& format) const {
188     return !(*this == format);
189   }
190 
191   bool operator<(const VideoFormat& format) const {
192     return (fourcc < format.fourcc) ||
193            (fourcc == format.fourcc && width < format.width) ||
194            (fourcc == format.fourcc && width == format.width &&
195             height < format.height) ||
196            (fourcc == format.fourcc && width == format.width &&
197             height == format.height && interval > format.interval);
198   }
199 
framerateVideoFormat200   int framerate() const { return IntervalToFps(interval); }
201 
202   // Check if both width and height are 0.
IsSize0x0VideoFormat203   bool IsSize0x0() const { return 0 == width && 0 == height; }
204 
205   // Check if this format is less than another one by comparing the resolution
206   // and frame rate.
IsPixelRateLessVideoFormat207   bool IsPixelRateLess(const VideoFormat& format) const {
208     return width * height * framerate() <
209            format.width * format.height * format.framerate();
210   }
211 
212   // Get a string presentation in the form of "fourcc width x height x fps"
213   std::string ToString() const;
214 };
215 
216 // Returns the largest positive integer that divides both |a| and |b|.
217 int GreatestCommonDivisor(int a, int b);
218 
219 // Returns the smallest positive integer that is divisible by both |a| and |b|.
220 int LeastCommonMultiple(int a, int b);
221 
222 }  // namespace cricket
223 
224 #endif  // MEDIA_BASE_VIDEO_COMMON_H_
225