jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 11 | // Common definition for video, including fourcc and VideoFormat. |
| 12 | |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 13 | #ifndef MEDIA_BASE_VIDEOCOMMON_H_ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #define MEDIA_BASE_VIDEOCOMMON_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 16 | #include <stdint.h> |
| 17 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/timeutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 29 | const uint32_t kDummyVideoSsrc = 0xFFFFFFFF; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
| 31 | // Minimum interval is 10k fps. |
| 32 | #define FPS_TO_INTERVAL(fps) \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 33 | (fps ? rtc::kNumNanosecsPerSec / fps : rtc::kNumNanosecsPerSec / 10000) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 41 | #define CRICKET_FOURCC(a, b, c, d) \ |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 42 | ((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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | // 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. |
| 55 | enum FourCC { |
| 56 | // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 57 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | |
| 67 | // 2 Secondary YUV formats: row biplanar. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 68 | FOURCC_M420 = CRICKET_FOURCC('M', '4', '2', '0'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | |
| 70 | // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 71 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 75 | FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '), |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 76 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 80 | |
| 81 | // 4 Secondary RGB formats: 4 Bayer Patterns. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 82 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | |
| 87 | // 1 Primary Compressed YUV format. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 88 | FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | |
| 90 | // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 91 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 98 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
| 114 | // 1 Auxiliary compressed YUV format set aside for capturer. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 115 | FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 118 | #undef CRICKET_FOURCC |
| 119 | |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 120 | // 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 125 | static const uint32_t FOURCC_ANY = 0xFFFFFFFF; |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 126 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | // Converts fourcc aliases into canonical ones. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 128 | uint32_t CanonicalFourCC(uint32_t fourcc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | |
| 130 | // Get FourCC code as a string. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 131 | inline std::string GetFourccName(uint32_t fourcc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | // Definition of VideoFormat. |
| 142 | ////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | // VideoFormat with Plain Old Data for global variables. |
| 145 | struct VideoFormatPod { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 146 | int width; // Number of pixels. |
| 147 | int height; // Number of pixels. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 148 | int64_t interval; // Nanoseconds. |
| 149 | uint32_t fourcc; // Color space. FOURCC_ANY means that any color space is OK. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | struct VideoFormat : VideoFormatPod { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 153 | static const int64_t kMinimumInterval = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 154 | rtc::kNumNanosecsPerSec / 10000; // 10k fps. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 156 | VideoFormat() { Construct(0, 0, 0, 0); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | VideoFormat(int w, int h, int64_t interval_ns, uint32_t cc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 159 | 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 166 | void Construct(int w, int h, int64_t interval_ns, uint32_t cc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | width = w; |
| 168 | height = h; |
| 169 | interval = interval_ns; |
| 170 | fourcc = cc; |
| 171 | } |
| 172 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 173 | static int64_t FpsToInterval(int fps) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 174 | return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 177 | static int IntervalToFps(int64_t interval) { |
henrike@webrtc.org | 18e5911 | 2014-03-14 17:19:38 +0000 | [diff] [blame] | 178 | if (!interval) { |
| 179 | return 0; |
| 180 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 181 | return static_cast<int>(rtc::kNumNanosecsPerSec / interval); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 184 | static float IntervalToFpsFloat(int64_t interval) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 185 | if (!interval) { |
| 186 | return 0.f; |
| 187 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 188 | return static_cast<float>(rtc::kNumNanosecsPerSec) / |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 189 | static_cast<float>(interval); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 190 | } |
| 191 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | bool operator==(const VideoFormat& format) const { |
| 193 | return width == format.width && height == format.height && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 194 | interval == format.interval && fourcc == format.fourcc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | } |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 203 | (fourcc == format.fourcc && width < format.width) || |
| 204 | (fourcc == format.fourcc && width == format.width && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | height < format.height) || |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 206 | (fourcc == format.fourcc && width == format.width && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 219 | format.width * format.height * format.framerate(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 220 | } |
| 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 Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 228 | #endif // MEDIA_BASE_VIDEOCOMMON_H_ |