blob: f01e833f38478fa1a0a5e5a0aeb39d2cf71de9d7 [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
25// TODO(janahan): For now, a hard-coded ssrc is used as the video ssrc.
26// This is because when the video frame is passed to the mediaprocessor for
27// processing, it doesn't have the correct ssrc. Since currently only Tx
28// Video processing is supported, this is ok. When we switch over to trigger
29// from capturer, this should be fixed and this const removed.
Peter Boström0c4e06b2015-10-07 12:23:21 +020030const uint32_t kDummyVideoSsrc = 0xFFFFFFFF;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32// Minimum interval is 10k fps.
33#define FPS_TO_INTERVAL(fps) \
Yves Gerey665174f2018-06-19 15:03:05 +020034 (fps ? rtc::kNumNanosecsPerSec / fps : 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.
Yves Gerey665174f2018-06-19 15:03:05 +020042#define CRICKET_FOURCC(a, b, c, d) \
Peter Boström0c4e06b2015-10-07 12:23:21 +020043 ((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.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010058 FOURCC_I420 = CRICKET_FOURCC('I', '4', '2', '0'),
59 FOURCC_I422 = CRICKET_FOURCC('I', '4', '2', '2'),
60 FOURCC_I444 = CRICKET_FOURCC('I', '4', '4', '4'),
61 FOURCC_I411 = CRICKET_FOURCC('I', '4', '1', '1'),
62 FOURCC_I400 = CRICKET_FOURCC('I', '4', '0', '0'),
63 FOURCC_NV21 = CRICKET_FOURCC('N', 'V', '2', '1'),
64 FOURCC_NV12 = CRICKET_FOURCC('N', 'V', '1', '2'),
65 FOURCC_YUY2 = CRICKET_FOURCC('Y', 'U', 'Y', '2'),
66 FOURCC_UYVY = CRICKET_FOURCC('U', 'Y', 'V', 'Y'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
68 // 2 Secondary YUV formats: row biplanar.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010069 FOURCC_M420 = CRICKET_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.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010072 FOURCC_ARGB = CRICKET_FOURCC('A', 'R', 'G', 'B'),
73 FOURCC_BGRA = CRICKET_FOURCC('B', 'G', 'R', 'A'),
74 FOURCC_ABGR = CRICKET_FOURCC('A', 'B', 'G', 'R'),
75 FOURCC_24BG = CRICKET_FOURCC('2', '4', 'B', 'G'),
Yves Gerey665174f2018-06-19 15:03:05 +020076 FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '),
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010077 FOURCC_RGBA = CRICKET_FOURCC('R', 'G', 'B', 'A'),
78 FOURCC_RGBP = CRICKET_FOURCC('R', 'G', 'B', 'P'), // bgr565.
79 FOURCC_RGBO = CRICKET_FOURCC('R', 'G', 'B', 'O'), // abgr1555.
80 FOURCC_R444 = CRICKET_FOURCC('R', '4', '4', '4'), // argb4444.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
82 // 4 Secondary RGB formats: 4 Bayer Patterns.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010083 FOURCC_RGGB = CRICKET_FOURCC('R', 'G', 'G', 'B'),
84 FOURCC_BGGR = CRICKET_FOURCC('B', 'G', 'G', 'R'),
85 FOURCC_GRBG = CRICKET_FOURCC('G', 'R', 'B', 'G'),
86 FOURCC_GBRG = CRICKET_FOURCC('G', 'B', 'R', 'G'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
88 // 1 Primary Compressed YUV format.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010089 FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
91 // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010092 FOURCC_YV12 = CRICKET_FOURCC('Y', 'V', '1', '2'),
93 FOURCC_YV16 = CRICKET_FOURCC('Y', 'V', '1', '6'),
94 FOURCC_YV24 = CRICKET_FOURCC('Y', 'V', '2', '4'),
95 FOURCC_YU12 = CRICKET_FOURCC('Y', 'U', '1', '2'), // Linux version of I420.
96 FOURCC_J420 = CRICKET_FOURCC('J', '4', '2', '0'),
97 FOURCC_J400 = CRICKET_FOURCC('J', '4', '0', '0'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
Patrik Höglund4e76ebb2017-12-01 11:54:47 +010099 // 14 Auxiliary aliases. CanonicalFourCC() maps these to canonical FOURCC.
100 FOURCC_IYUV = CRICKET_FOURCC('I', 'Y', 'U', 'V'), // Alias for I420.
101 FOURCC_YU16 = CRICKET_FOURCC('Y', 'U', '1', '6'), // Alias for I422.
102 FOURCC_YU24 = CRICKET_FOURCC('Y', 'U', '2', '4'), // Alias for I444.
103 FOURCC_YUYV = CRICKET_FOURCC('Y', 'U', 'Y', 'V'), // Alias for YUY2.
104 FOURCC_YUVS = CRICKET_FOURCC('y', 'u', 'v', 's'), // Alias for YUY2 on Mac.
105 FOURCC_HDYC = CRICKET_FOURCC('H', 'D', 'Y', 'C'), // Alias for UYVY.
106 FOURCC_2VUY = CRICKET_FOURCC('2', 'v', 'u', 'y'), // Alias for UYVY on Mac.
107 FOURCC_JPEG = CRICKET_FOURCC('J', 'P', 'E', 'G'), // Alias for MJPG.
108 FOURCC_DMB1 = CRICKET_FOURCC('d', 'm', 'b', '1'), // Alias for MJPG on Mac.
109 FOURCC_BA81 = CRICKET_FOURCC('B', 'A', '8', '1'), // Alias for BGGR.
110 FOURCC_RGB3 = CRICKET_FOURCC('R', 'G', 'B', '3'), // Alias for RAW.
111 FOURCC_BGR3 = CRICKET_FOURCC('B', 'G', 'R', '3'), // Alias for 24BG.
112 FOURCC_CM32 = CRICKET_FOURCC(0, 0, 0, 32), // BGRA kCMPixelFormat_32ARGB
113 FOURCC_CM24 = CRICKET_FOURCC(0, 0, 0, 24), // RAW kCMPixelFormat_24RGB
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
115 // 1 Auxiliary compressed YUV format set aside for capturer.
Patrik Höglund4e76ebb2017-12-01 11:54:47 +0100116 FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117};
118
Patrik Höglund4e76ebb2017-12-01 11:54:47 +0100119#undef CRICKET_FOURCC
120
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000121// Match any fourcc.
122
123// We move this out of the enum because using it in many places caused
124// the compiler to get grumpy, presumably since the above enum is
125// backed by an int.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126static const uint32_t FOURCC_ANY = 0xFFFFFFFF;
pthatcher@webrtc.org40b276e2014-12-12 02:44:30 +0000127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128// Converts fourcc aliases into canonical ones.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200129uint32_t CanonicalFourCC(uint32_t fourcc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
131// Get FourCC code as a string.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200132inline std::string GetFourccName(uint32_t fourcc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 std::string name;
134 name.push_back(static_cast<char>(fourcc & 0xFF));
135 name.push_back(static_cast<char>((fourcc >> 8) & 0xFF));
136 name.push_back(static_cast<char>((fourcc >> 16) & 0xFF));
137 name.push_back(static_cast<char>((fourcc >> 24) & 0xFF));
138 return name;
139}
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141//////////////////////////////////////////////////////////////////////////////
142// Definition of VideoFormat.
143//////////////////////////////////////////////////////////////////////////////
144
145// VideoFormat with Plain Old Data for global variables.
146struct VideoFormatPod {
Yves Gerey665174f2018-06-19 15:03:05 +0200147 int width; // Number of pixels.
148 int height; // Number of pixels.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 int64_t interval; // Nanoseconds.
150 uint32_t fourcc; // Color space. FOURCC_ANY means that any color space is OK.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151};
152
Mirko Bonadei1ddc5b62018-10-19 10:35:14 +0200153struct RTC_EXPORT VideoFormat : VideoFormatPod {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200154 static const int64_t kMinimumInterval =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000155 rtc::kNumNanosecsPerSec / 10000; // 10k fps.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
Yves Gerey665174f2018-06-19 15:03:05 +0200157 VideoFormat() { Construct(0, 0, 0, 0); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158
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) /
Yves Gerey665174f2018-06-19 15:03:05 +0200190 static_cast<float>(interval);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000191 }
192
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 bool operator==(const VideoFormat& format) const {
194 return width == format.width && height == format.height &&
Yves Gerey665174f2018-06-19 15:03:05 +0200195 interval == format.interval && fourcc == format.fourcc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 }
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) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200204 (fourcc == format.fourcc && width < format.width) ||
205 (fourcc == format.fourcc && width == format.width &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 height < format.height) ||
Yves Gerey665174f2018-06-19 15:03:05 +0200207 (fourcc == format.fourcc && width == format.width &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 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() <
Yves Gerey665174f2018-06-19 15:03:05 +0200220 format.width * format.height * format.framerate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 }
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
Steve Anton10542f22019-01-11 09:11:00 -0800229#endif // MEDIA_BASE_VIDEO_COMMON_H_