blob: 10ad383cf196911ce698358b1edbadcae3a1499c [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011// Common definition for video, including fourcc and VideoFormat.
12
kjellandera96e2d72016-02-04 23:52:28 -080013#ifndef WEBRTC_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT
14#define WEBRTC_MEDIA_BASE_VIDEOCOMMON_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000015
pbosc7c26a02017-01-02 08:42:32 -080016#include <stdint.h>
17
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
19
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/timeutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
24// TODO(janahan): For now, a hard-coded ssrc is used as the video ssrc.
25// This is because when the video frame is passed to the mediaprocessor for
26// processing, it doesn't have the correct ssrc. Since currently only Tx
27// Video processing is supported, this is ok. When we switch over to trigger
28// from capturer, this should be fixed and this const removed.
Peter Boström0c4e06b2015-10-07 12:23:21 +020029const uint32_t kDummyVideoSsrc = 0xFFFFFFFF;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31// Minimum interval is 10k fps.
32#define FPS_TO_INTERVAL(fps) \
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033 (fps ? rtc::kNumNanosecsPerSec / fps : \
34 rtc::kNumNanosecsPerSec / 10000)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36//////////////////////////////////////////////////////////////////////////////
37// Definition of FourCC codes
38//////////////////////////////////////////////////////////////////////////////
39// Convert four characters to a FourCC code.
40// Needs to be a macro otherwise the OS X compiler complains when the kFormat*
41// constants are used in a switch.
Peter Boström0c4e06b2015-10-07 12:23:21 +020042#define FOURCC(a, b, c, d) \
43 ((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \
44 (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045// Some pages discussing FourCC codes:
46// http://www.fourcc.org/yuv.php
47// http://v4l2spec.bytesex.org/spec/book1.htm
48// http://developer.apple.com/quicktime/icefloe/dispatch020.html
49// http://msdn.microsoft.com/library/windows/desktop/dd206750.aspx#nv12
50// http://people.xiph.org/~xiphmont/containers/nut/nut4cc.txt
51
52// FourCC codes grouped according to implementation efficiency.
53// Primary formats should convert in 1 efficient step.
54// Secondary formats are converted in 2 steps.
55// Auxilliary formats call primary converters.
56enum FourCC {
57 // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed.
58 FOURCC_I420 = FOURCC('I', '4', '2', '0'),
59 FOURCC_I422 = FOURCC('I', '4', '2', '2'),
60 FOURCC_I444 = FOURCC('I', '4', '4', '4'),
61 FOURCC_I411 = FOURCC('I', '4', '1', '1'),
62 FOURCC_I400 = FOURCC('I', '4', '0', '0'),
63 FOURCC_NV21 = FOURCC('N', 'V', '2', '1'),
64 FOURCC_NV12 = FOURCC('N', 'V', '1', '2'),
65 FOURCC_YUY2 = FOURCC('Y', 'U', 'Y', '2'),
66 FOURCC_UYVY = FOURCC('U', 'Y', 'V', 'Y'),
67
68 // 2 Secondary YUV formats: row biplanar.
69 FOURCC_M420 = FOURCC('M', '4', '2', '0'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71 // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp.
72 FOURCC_ARGB = FOURCC('A', 'R', 'G', 'B'),
73 FOURCC_BGRA = FOURCC('B', 'G', 'R', 'A'),
74 FOURCC_ABGR = FOURCC('A', 'B', 'G', 'R'),
75 FOURCC_24BG = FOURCC('2', '4', 'B', 'G'),
76 FOURCC_RAW = FOURCC('r', 'a', 'w', ' '),
77 FOURCC_RGBA = FOURCC('R', 'G', 'B', 'A'),
78 FOURCC_RGBP = FOURCC('R', 'G', 'B', 'P'), // bgr565.
79 FOURCC_RGBO = FOURCC('R', 'G', 'B', 'O'), // abgr1555.
80 FOURCC_R444 = FOURCC('R', '4', '4', '4'), // argb4444.
81
82 // 4 Secondary RGB formats: 4 Bayer Patterns.
83 FOURCC_RGGB = FOURCC('R', 'G', 'G', 'B'),
84 FOURCC_BGGR = FOURCC('B', 'G', 'G', 'R'),
85 FOURCC_GRBG = FOURCC('G', 'R', 'B', 'G'),
86 FOURCC_GBRG = FOURCC('G', 'B', 'R', 'G'),
87
88 // 1 Primary Compressed YUV format.
89 FOURCC_MJPG = FOURCC('M', 'J', 'P', 'G'),
90
91 // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias.
92 FOURCC_YV12 = FOURCC('Y', 'V', '1', '2'),
93 FOURCC_YV16 = FOURCC('Y', 'V', '1', '6'),
94 FOURCC_YV24 = FOURCC('Y', 'V', '2', '4'),
95 FOURCC_YU12 = FOURCC('Y', 'U', '1', '2'), // Linux version of I420.
96 FOURCC_J420 = FOURCC('J', '4', '2', '0'),
97 FOURCC_J400 = FOURCC('J', '4', '0', '0'),
98
99 // 14 Auxiliary aliases. CanonicalFourCC() maps these to canonical fourcc.
100 FOURCC_IYUV = FOURCC('I', 'Y', 'U', 'V'), // Alias for I420.
101 FOURCC_YU16 = FOURCC('Y', 'U', '1', '6'), // Alias for I422.
102 FOURCC_YU24 = FOURCC('Y', 'U', '2', '4'), // Alias for I444.
103 FOURCC_YUYV = FOURCC('Y', 'U', 'Y', 'V'), // Alias for YUY2.
104 FOURCC_YUVS = FOURCC('y', 'u', 'v', 's'), // Alias for YUY2 on Mac.
105 FOURCC_HDYC = FOURCC('H', 'D', 'Y', 'C'), // Alias for UYVY.
106 FOURCC_2VUY = FOURCC('2', 'v', 'u', 'y'), // Alias for UYVY on Mac.
107 FOURCC_JPEG = FOURCC('J', 'P', 'E', 'G'), // Alias for MJPG.
108 FOURCC_DMB1 = FOURCC('d', 'm', 'b', '1'), // Alias for MJPG on Mac.
109 FOURCC_BA81 = FOURCC('B', 'A', '8', '1'), // Alias for BGGR.
110 FOURCC_RGB3 = FOURCC('R', 'G', 'B', '3'), // Alias for RAW.
111 FOURCC_BGR3 = FOURCC('B', 'G', 'R', '3'), // Alias for 24BG.
112 FOURCC_CM32 = FOURCC(0, 0, 0, 32), // Alias for BGRA kCMPixelFormat_32ARGB
113 FOURCC_CM24 = FOURCC(0, 0, 0, 24), // Alias for RAW kCMPixelFormat_24RGB
114
115 // 1 Auxiliary compressed YUV format set aside for capturer.
116 FOURCC_H264 = FOURCC('H', '2', '6', '4'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117};
118
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000119// Match any fourcc.
120
121// We move this out of the enum because using it in many places caused
122// the compiler to get grumpy, presumably since the above enum is
123// backed by an int.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200124static const uint32_t FOURCC_ANY = 0xFFFFFFFF;
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126// Converts fourcc aliases into canonical ones.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200127uint32_t CanonicalFourCC(uint32_t fourcc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
129// Get FourCC code as a string.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200130inline std::string GetFourccName(uint32_t fourcc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 std::string name;
132 name.push_back(static_cast<char>(fourcc & 0xFF));
133 name.push_back(static_cast<char>((fourcc >> 8) & 0xFF));
134 name.push_back(static_cast<char>((fourcc >> 16) & 0xFF));
135 name.push_back(static_cast<char>((fourcc >> 24) & 0xFF));
136 return name;
137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139//////////////////////////////////////////////////////////////////////////////
140// Definition of VideoFormat.
141//////////////////////////////////////////////////////////////////////////////
142
143// VideoFormat with Plain Old Data for global variables.
144struct VideoFormatPod {
145 int width; // Number of pixels.
146 int height; // Number of pixels.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 int64_t interval; // Nanoseconds.
148 uint32_t fourcc; // Color space. FOURCC_ANY means that any color space is OK.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149};
150
151struct VideoFormat : VideoFormatPod {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152 static const int64_t kMinimumInterval =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153 rtc::kNumNanosecsPerSec / 10000; // 10k fps.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
155 VideoFormat() {
156 Construct(0, 0, 0, 0);
157 }
158
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 VideoFormat(int w, int h, int64_t interval_ns, uint32_t cc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 Construct(w, h, interval_ns, cc);
161 }
162
163 explicit VideoFormat(const VideoFormatPod& format) {
164 Construct(format.width, format.height, format.interval, format.fourcc);
165 }
166
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 void Construct(int w, int h, int64_t interval_ns, uint32_t cc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 width = w;
169 height = h;
170 interval = interval_ns;
171 fourcc = cc;
172 }
173
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174 static int64_t FpsToInterval(int fps) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000175 return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 }
177
Peter Boström0c4e06b2015-10-07 12:23:21 +0200178 static int IntervalToFps(int64_t interval) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +0000179 if (!interval) {
180 return 0;
181 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000182 return static_cast<int>(rtc::kNumNanosecsPerSec / interval);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 }
184
Peter Boström0c4e06b2015-10-07 12:23:21 +0200185 static float IntervalToFpsFloat(int64_t interval) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000186 if (!interval) {
187 return 0.f;
188 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000189 return static_cast<float>(rtc::kNumNanosecsPerSec) /
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000190 static_cast<float>(interval);
191 }
192
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 bool operator==(const VideoFormat& format) const {
194 return width == format.width && height == format.height &&
195 interval == format.interval && fourcc == format.fourcc;
196 }
197
198 bool operator!=(const VideoFormat& format) const {
199 return !(*this == format);
200 }
201
202 bool operator<(const VideoFormat& format) const {
203 return (fourcc < format.fourcc) ||
204 (fourcc == format.fourcc && width < format.width) ||
205 (fourcc == format.fourcc && width == format.width &&
206 height < format.height) ||
207 (fourcc == format.fourcc && width == format.width &&
208 height == format.height && interval > format.interval);
209 }
210
211 int framerate() const { return IntervalToFps(interval); }
212
213 // Check if both width and height are 0.
214 bool IsSize0x0() const { return 0 == width && 0 == height; }
215
216 // Check if this format is less than another one by comparing the resolution
217 // and frame rate.
218 bool IsPixelRateLess(const VideoFormat& format) const {
219 return width * height * framerate() <
220 format.width * format.height * format.framerate();
221 }
222
223 // Get a string presentation in the form of "fourcc width x height x fps"
224 std::string ToString() const;
225};
226
227} // namespace cricket
228
kjellandera96e2d72016-02-04 23:52:28 -0800229#endif // WEBRTC_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT