blob: 381ddb770ad64cad880328c40961ac7ad91f3c6b [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
Steve Anton10542f22019-01-11 09:11:00 -080013#ifndef MEDIA_BASE_VIDEO_COMMON_H_
14#define MEDIA_BASE_VIDEO_COMMON_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
Mirko Bonadei1ddc5b62018-10-19 10:35:14 +020020#include "rtc_base/system/rtc_export.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025//////////////////////////////////////////////////////////////////////////////
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.
Yves Gerey665174f2018-06-19 15:03:05 +020031#define CRICKET_FOURCC(a, b, c, d) \
Peter Boström0c4e06b2015-10-07 12:23:21 +020032 ((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \
33 (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034// 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.
45enum FourCC {
46 // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010047 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'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
57 // 2 Secondary YUV formats: row biplanar.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010058 FOURCC_M420 = CRICKET_FOURCC('M', '4', '2', '0'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60 // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010061 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'),
Yves Gerey665174f2018-06-19 15:03:05 +020065 FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '),
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010066 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71 // 4 Secondary RGB formats: 4 Bayer Patterns.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010072 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'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77 // 1 Primary Compressed YUV format.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010078 FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
80 // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010081 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'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010088 // 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
104 // 1 Auxiliary compressed YUV format set aside for capturer.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +0100105 FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106};
107
Patrik Höglund4e76ebb2017-12-01 11:54:47 +0100108#undef CRICKET_FOURCC
109
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000110// 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.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200115static const uint32_t FOURCC_ANY = 0xFFFFFFFF;
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117// Converts fourcc aliases into canonical ones.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200118uint32_t CanonicalFourCC(uint32_t fourcc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
120// Get FourCC code as a string.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200121inline std::string GetFourccName(uint32_t fourcc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130//////////////////////////////////////////////////////////////////////////////
131// Definition of VideoFormat.
132//////////////////////////////////////////////////////////////////////////////
133
134// VideoFormat with Plain Old Data for global variables.
135struct VideoFormatPod {
Yves Gerey665174f2018-06-19 15:03:05 +0200136 int width; // Number of pixels.
137 int height; // Number of pixels.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200138 int64_t interval; // Nanoseconds.
139 uint32_t fourcc; // Color space. FOURCC_ANY means that any color space is OK.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140};
141
Mirko Bonadei1ddc5b62018-10-19 10:35:14 +0200142struct RTC_EXPORT VideoFormat : VideoFormatPod {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 static const int64_t kMinimumInterval =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000144 rtc::kNumNanosecsPerSec / 10000; // 10k fps.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145
Yves Gerey665174f2018-06-19 15:03:05 +0200146 VideoFormat() { Construct(0, 0, 0, 0); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
Peter Boström0c4e06b2015-10-07 12:23:21 +0200148 VideoFormat(int w, int h, int64_t interval_ns, uint32_t cc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 Construct(w, h, interval_ns, cc);
150 }
151
152 explicit VideoFormat(const VideoFormatPod& format) {
153 Construct(format.width, format.height, format.interval, format.fourcc);
154 }
155
Peter Boström0c4e06b2015-10-07 12:23:21 +0200156 void Construct(int w, int h, int64_t interval_ns, uint32_t cc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 width = w;
158 height = h;
159 interval = interval_ns;
160 fourcc = cc;
161 }
162
Peter Boström0c4e06b2015-10-07 12:23:21 +0200163 static int64_t FpsToInterval(int fps) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164 return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
166
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 static int IntervalToFps(int64_t interval) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +0000168 if (!interval) {
169 return 0;
170 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 return static_cast<int>(rtc::kNumNanosecsPerSec / interval);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 }
173
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174 static float IntervalToFpsFloat(int64_t interval) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000175 if (!interval) {
176 return 0.f;
177 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000178 return static_cast<float>(rtc::kNumNanosecsPerSec) /
Yves Gerey665174f2018-06-19 15:03:05 +0200179 static_cast<float>(interval);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000180 }
181
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 bool operator==(const VideoFormat& format) const {
183 return width == format.width && height == format.height &&
Yves Gerey665174f2018-06-19 15:03:05 +0200184 interval == format.interval && fourcc == format.fourcc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 }
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) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200193 (fourcc == format.fourcc && width < format.width) ||
194 (fourcc == format.fourcc && width == format.width &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 height < format.height) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200196 (fourcc == format.fourcc && width == format.width &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 height == format.height && interval > format.interval);
198 }
199
200 int framerate() const { return IntervalToFps(interval); }
201
202 // Check if both width and height are 0.
203 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.
207 bool IsPixelRateLess(const VideoFormat& format) const {
208 return width * height * framerate() <
Yves Gerey665174f2018-06-19 15:03:05 +0200209 format.width * format.height * format.framerate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 }
211
212 // Get a string presentation in the form of "fourcc width x height x fps"
213 std::string ToString() const;
214};
215
216} // namespace cricket
217
Steve Anton10542f22019-01-11 09:11:00 -0800218#endif // MEDIA_BASE_VIDEO_COMMON_H_