blob: 0ffd989cb6515f0e5e9d22001b87cbf645cc7998 [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 Antone78bcb92017-10-31 09:53:08 -070013#ifndef MEDIA_BASE_VIDEOCOMMON_H_
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#define 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "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) \
Yves Gerey665174f2018-06-19 15:03:05 +020033 (fps ? rtc::kNumNanosecsPerSec / fps : rtc::kNumNanosecsPerSec / 10000)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35//////////////////////////////////////////////////////////////////////////////
36// Definition of FourCC codes
37//////////////////////////////////////////////////////////////////////////////
38// Convert four characters to a FourCC code.
39// Needs to be a macro otherwise the OS X compiler complains when the kFormat*
40// constants are used in a switch.
Yves Gerey665174f2018-06-19 15:03:05 +020041#define CRICKET_FOURCC(a, b, c, d) \
Peter Boström0c4e06b2015-10-07 12:23:21 +020042 ((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \
43 (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044// Some pages discussing FourCC codes:
45// http://www.fourcc.org/yuv.php
46// http://v4l2spec.bytesex.org/spec/book1.htm
47// http://developer.apple.com/quicktime/icefloe/dispatch020.html
48// http://msdn.microsoft.com/library/windows/desktop/dd206750.aspx#nv12
49// http://people.xiph.org/~xiphmont/containers/nut/nut4cc.txt
50
51// FourCC codes grouped according to implementation efficiency.
52// Primary formats should convert in 1 efficient step.
53// Secondary formats are converted in 2 steps.
54// Auxilliary formats call primary converters.
55enum FourCC {
56 // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010057 FOURCC_I420 = CRICKET_FOURCC('I', '4', '2', '0'),
58 FOURCC_I422 = CRICKET_FOURCC('I', '4', '2', '2'),
59 FOURCC_I444 = CRICKET_FOURCC('I', '4', '4', '4'),
60 FOURCC_I411 = CRICKET_FOURCC('I', '4', '1', '1'),
61 FOURCC_I400 = CRICKET_FOURCC('I', '4', '0', '0'),
62 FOURCC_NV21 = CRICKET_FOURCC('N', 'V', '2', '1'),
63 FOURCC_NV12 = CRICKET_FOURCC('N', 'V', '1', '2'),
64 FOURCC_YUY2 = CRICKET_FOURCC('Y', 'U', 'Y', '2'),
65 FOURCC_UYVY = CRICKET_FOURCC('U', 'Y', 'V', 'Y'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67 // 2 Secondary YUV formats: row biplanar.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010068 FOURCC_M420 = CRICKET_FOURCC('M', '4', '2', '0'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
70 // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010071 FOURCC_ARGB = CRICKET_FOURCC('A', 'R', 'G', 'B'),
72 FOURCC_BGRA = CRICKET_FOURCC('B', 'G', 'R', 'A'),
73 FOURCC_ABGR = CRICKET_FOURCC('A', 'B', 'G', 'R'),
74 FOURCC_24BG = CRICKET_FOURCC('2', '4', 'B', 'G'),
Yves Gerey665174f2018-06-19 15:03:05 +020075 FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '),
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010076 FOURCC_RGBA = CRICKET_FOURCC('R', 'G', 'B', 'A'),
77 FOURCC_RGBP = CRICKET_FOURCC('R', 'G', 'B', 'P'), // bgr565.
78 FOURCC_RGBO = CRICKET_FOURCC('R', 'G', 'B', 'O'), // abgr1555.
79 FOURCC_R444 = CRICKET_FOURCC('R', '4', '4', '4'), // argb4444.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
81 // 4 Secondary RGB formats: 4 Bayer Patterns.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010082 FOURCC_RGGB = CRICKET_FOURCC('R', 'G', 'G', 'B'),
83 FOURCC_BGGR = CRICKET_FOURCC('B', 'G', 'G', 'R'),
84 FOURCC_GRBG = CRICKET_FOURCC('G', 'R', 'B', 'G'),
85 FOURCC_GBRG = CRICKET_FOURCC('G', 'B', 'R', 'G'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086
87 // 1 Primary Compressed YUV format.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010088 FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90 // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010091 FOURCC_YV12 = CRICKET_FOURCC('Y', 'V', '1', '2'),
92 FOURCC_YV16 = CRICKET_FOURCC('Y', 'V', '1', '6'),
93 FOURCC_YV24 = CRICKET_FOURCC('Y', 'V', '2', '4'),
94 FOURCC_YU12 = CRICKET_FOURCC('Y', 'U', '1', '2'), // Linux version of I420.
95 FOURCC_J420 = CRICKET_FOURCC('J', '4', '2', '0'),
96 FOURCC_J400 = CRICKET_FOURCC('J', '4', '0', '0'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010098 // 14 Auxiliary aliases. CanonicalFourCC() maps these to canonical FOURCC.
99 FOURCC_IYUV = CRICKET_FOURCC('I', 'Y', 'U', 'V'), // Alias for I420.
100 FOURCC_YU16 = CRICKET_FOURCC('Y', 'U', '1', '6'), // Alias for I422.
101 FOURCC_YU24 = CRICKET_FOURCC('Y', 'U', '2', '4'), // Alias for I444.
102 FOURCC_YUYV = CRICKET_FOURCC('Y', 'U', 'Y', 'V'), // Alias for YUY2.
103 FOURCC_YUVS = CRICKET_FOURCC('y', 'u', 'v', 's'), // Alias for YUY2 on Mac.
104 FOURCC_HDYC = CRICKET_FOURCC('H', 'D', 'Y', 'C'), // Alias for UYVY.
105 FOURCC_2VUY = CRICKET_FOURCC('2', 'v', 'u', 'y'), // Alias for UYVY on Mac.
106 FOURCC_JPEG = CRICKET_FOURCC('J', 'P', 'E', 'G'), // Alias for MJPG.
107 FOURCC_DMB1 = CRICKET_FOURCC('d', 'm', 'b', '1'), // Alias for MJPG on Mac.
108 FOURCC_BA81 = CRICKET_FOURCC('B', 'A', '8', '1'), // Alias for BGGR.
109 FOURCC_RGB3 = CRICKET_FOURCC('R', 'G', 'B', '3'), // Alias for RAW.
110 FOURCC_BGR3 = CRICKET_FOURCC('B', 'G', 'R', '3'), // Alias for 24BG.
111 FOURCC_CM32 = CRICKET_FOURCC(0, 0, 0, 32), // BGRA kCMPixelFormat_32ARGB
112 FOURCC_CM24 = CRICKET_FOURCC(0, 0, 0, 24), // RAW kCMPixelFormat_24RGB
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 // 1 Auxiliary compressed YUV format set aside for capturer.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +0100115 FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116};
117
Patrik Höglund4e76ebb2017-12-01 11:54:47 +0100118#undef CRICKET_FOURCC
119
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000120// Match any fourcc.
121
122// We move this out of the enum because using it in many places caused
123// the compiler to get grumpy, presumably since the above enum is
124// backed by an int.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125static const uint32_t FOURCC_ANY = 0xFFFFFFFF;
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127// Converts fourcc aliases into canonical ones.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200128uint32_t CanonicalFourCC(uint32_t fourcc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
130// Get FourCC code as a string.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200131inline std::string GetFourccName(uint32_t fourcc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 std::string name;
133 name.push_back(static_cast<char>(fourcc & 0xFF));
134 name.push_back(static_cast<char>((fourcc >> 8) & 0xFF));
135 name.push_back(static_cast<char>((fourcc >> 16) & 0xFF));
136 name.push_back(static_cast<char>((fourcc >> 24) & 0xFF));
137 return name;
138}
139
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140//////////////////////////////////////////////////////////////////////////////
141// Definition of VideoFormat.
142//////////////////////////////////////////////////////////////////////////////
143
144// VideoFormat with Plain Old Data for global variables.
145struct VideoFormatPod {
Yves Gerey665174f2018-06-19 15:03:05 +0200146 int width; // Number of pixels.
147 int height; // Number of pixels.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200148 int64_t interval; // Nanoseconds.
149 uint32_t fourcc; // Color space. FOURCC_ANY means that any color space is OK.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150};
151
152struct VideoFormat : VideoFormatPod {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200153 static const int64_t kMinimumInterval =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000154 rtc::kNumNanosecsPerSec / 10000; // 10k fps.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
Yves Gerey665174f2018-06-19 15:03:05 +0200156 VideoFormat() { Construct(0, 0, 0, 0); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157
Peter Boström0c4e06b2015-10-07 12:23:21 +0200158 VideoFormat(int w, int h, int64_t interval_ns, uint32_t cc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 Construct(w, h, interval_ns, cc);
160 }
161
162 explicit VideoFormat(const VideoFormatPod& format) {
163 Construct(format.width, format.height, format.interval, format.fourcc);
164 }
165
Peter Boström0c4e06b2015-10-07 12:23:21 +0200166 void Construct(int w, int h, int64_t interval_ns, uint32_t cc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 width = w;
168 height = h;
169 interval = interval_ns;
170 fourcc = cc;
171 }
172
Peter Boström0c4e06b2015-10-07 12:23:21 +0200173 static int64_t FpsToInterval(int fps) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 }
176
Peter Boström0c4e06b2015-10-07 12:23:21 +0200177 static int IntervalToFps(int64_t interval) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +0000178 if (!interval) {
179 return 0;
180 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 return static_cast<int>(rtc::kNumNanosecsPerSec / interval);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 }
183
Peter Boström0c4e06b2015-10-07 12:23:21 +0200184 static float IntervalToFpsFloat(int64_t interval) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000185 if (!interval) {
186 return 0.f;
187 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 return static_cast<float>(rtc::kNumNanosecsPerSec) /
Yves Gerey665174f2018-06-19 15:03:05 +0200189 static_cast<float>(interval);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000190 }
191
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 bool operator==(const VideoFormat& format) const {
193 return width == format.width && height == format.height &&
Yves Gerey665174f2018-06-19 15:03:05 +0200194 interval == format.interval && fourcc == format.fourcc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 }
196
197 bool operator!=(const VideoFormat& format) const {
198 return !(*this == format);
199 }
200
201 bool operator<(const VideoFormat& format) const {
202 return (fourcc < format.fourcc) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200203 (fourcc == format.fourcc && width < format.width) ||
204 (fourcc == format.fourcc && width == format.width &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 height < format.height) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200206 (fourcc == format.fourcc && width == format.width &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 height == format.height && interval > format.interval);
208 }
209
210 int framerate() const { return IntervalToFps(interval); }
211
212 // Check if both width and height are 0.
213 bool IsSize0x0() const { return 0 == width && 0 == height; }
214
215 // Check if this format is less than another one by comparing the resolution
216 // and frame rate.
217 bool IsPixelRateLess(const VideoFormat& format) const {
218 return width * height * framerate() <
Yves Gerey665174f2018-06-19 15:03:05 +0200219 format.width * format.height * format.framerate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 }
221
222 // Get a string presentation in the form of "fourcc width x height x fps"
223 std::string ToString() const;
224};
225
226} // namespace cricket
227
Steve Antone78bcb92017-10-31 09:53:08 -0700228#endif // MEDIA_BASE_VIDEOCOMMON_H_