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 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 13 | #ifndef WEBRTC_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT |
| 14 | #define WEBRTC_MEDIA_BASE_VIDEOCOMMON_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
| 16 | #include <string> |
| 17 | |
pbos | 7eb0e23 | 2017-01-02 07:32:25 -0800 | [diff] [blame] | 18 | #include "webrtc/base/basictypes.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 19 | #include "webrtc/base/timeutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
| 21 | namespace cricket { |
| 22 | |
| 23 | // TODO(janahan): For now, a hard-coded ssrc is used as the video ssrc. |
| 24 | // This is because when the video frame is passed to the mediaprocessor for |
| 25 | // processing, it doesn't have the correct ssrc. Since currently only Tx |
| 26 | // Video processing is supported, this is ok. When we switch over to trigger |
| 27 | // from capturer, this should be fixed and this const removed. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 28 | const uint32_t kDummyVideoSsrc = 0xFFFFFFFF; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | // Minimum interval is 10k fps. |
| 31 | #define FPS_TO_INTERVAL(fps) \ |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 32 | (fps ? rtc::kNumNanosecsPerSec / fps : \ |
| 33 | 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. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 41 | #define FOURCC(a, b, c, d) \ |
| 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. |
| 57 | FOURCC_I420 = FOURCC('I', '4', '2', '0'), |
| 58 | FOURCC_I422 = FOURCC('I', '4', '2', '2'), |
| 59 | FOURCC_I444 = FOURCC('I', '4', '4', '4'), |
| 60 | FOURCC_I411 = FOURCC('I', '4', '1', '1'), |
| 61 | FOURCC_I400 = FOURCC('I', '4', '0', '0'), |
| 62 | FOURCC_NV21 = FOURCC('N', 'V', '2', '1'), |
| 63 | FOURCC_NV12 = FOURCC('N', 'V', '1', '2'), |
| 64 | FOURCC_YUY2 = FOURCC('Y', 'U', 'Y', '2'), |
| 65 | FOURCC_UYVY = FOURCC('U', 'Y', 'V', 'Y'), |
| 66 | |
| 67 | // 2 Secondary YUV formats: row biplanar. |
| 68 | FOURCC_M420 = 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. |
| 71 | FOURCC_ARGB = FOURCC('A', 'R', 'G', 'B'), |
| 72 | FOURCC_BGRA = FOURCC('B', 'G', 'R', 'A'), |
| 73 | FOURCC_ABGR = FOURCC('A', 'B', 'G', 'R'), |
| 74 | FOURCC_24BG = FOURCC('2', '4', 'B', 'G'), |
| 75 | FOURCC_RAW = FOURCC('r', 'a', 'w', ' '), |
| 76 | FOURCC_RGBA = FOURCC('R', 'G', 'B', 'A'), |
| 77 | FOURCC_RGBP = FOURCC('R', 'G', 'B', 'P'), // bgr565. |
| 78 | FOURCC_RGBO = FOURCC('R', 'G', 'B', 'O'), // abgr1555. |
| 79 | FOURCC_R444 = FOURCC('R', '4', '4', '4'), // argb4444. |
| 80 | |
| 81 | // 4 Secondary RGB formats: 4 Bayer Patterns. |
| 82 | FOURCC_RGGB = FOURCC('R', 'G', 'G', 'B'), |
| 83 | FOURCC_BGGR = FOURCC('B', 'G', 'G', 'R'), |
| 84 | FOURCC_GRBG = FOURCC('G', 'R', 'B', 'G'), |
| 85 | FOURCC_GBRG = FOURCC('G', 'B', 'R', 'G'), |
| 86 | |
| 87 | // 1 Primary Compressed YUV format. |
| 88 | FOURCC_MJPG = FOURCC('M', 'J', 'P', 'G'), |
| 89 | |
| 90 | // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias. |
| 91 | FOURCC_YV12 = FOURCC('Y', 'V', '1', '2'), |
| 92 | FOURCC_YV16 = FOURCC('Y', 'V', '1', '6'), |
| 93 | FOURCC_YV24 = FOURCC('Y', 'V', '2', '4'), |
| 94 | FOURCC_YU12 = FOURCC('Y', 'U', '1', '2'), // Linux version of I420. |
| 95 | FOURCC_J420 = FOURCC('J', '4', '2', '0'), |
| 96 | FOURCC_J400 = FOURCC('J', '4', '0', '0'), |
| 97 | |
| 98 | // 14 Auxiliary aliases. CanonicalFourCC() maps these to canonical fourcc. |
| 99 | FOURCC_IYUV = FOURCC('I', 'Y', 'U', 'V'), // Alias for I420. |
| 100 | FOURCC_YU16 = FOURCC('Y', 'U', '1', '6'), // Alias for I422. |
| 101 | FOURCC_YU24 = FOURCC('Y', 'U', '2', '4'), // Alias for I444. |
| 102 | FOURCC_YUYV = FOURCC('Y', 'U', 'Y', 'V'), // Alias for YUY2. |
| 103 | FOURCC_YUVS = FOURCC('y', 'u', 'v', 's'), // Alias for YUY2 on Mac. |
| 104 | FOURCC_HDYC = FOURCC('H', 'D', 'Y', 'C'), // Alias for UYVY. |
| 105 | FOURCC_2VUY = FOURCC('2', 'v', 'u', 'y'), // Alias for UYVY on Mac. |
| 106 | FOURCC_JPEG = FOURCC('J', 'P', 'E', 'G'), // Alias for MJPG. |
| 107 | FOURCC_DMB1 = FOURCC('d', 'm', 'b', '1'), // Alias for MJPG on Mac. |
| 108 | FOURCC_BA81 = FOURCC('B', 'A', '8', '1'), // Alias for BGGR. |
| 109 | FOURCC_RGB3 = FOURCC('R', 'G', 'B', '3'), // Alias for RAW. |
| 110 | FOURCC_BGR3 = FOURCC('B', 'G', 'R', '3'), // Alias for 24BG. |
| 111 | FOURCC_CM32 = FOURCC(0, 0, 0, 32), // Alias for BGRA kCMPixelFormat_32ARGB |
| 112 | FOURCC_CM24 = FOURCC(0, 0, 0, 24), // Alias for RAW kCMPixelFormat_24RGB |
| 113 | |
| 114 | // 1 Auxiliary compressed YUV format set aside for capturer. |
| 115 | FOURCC_H264 = FOURCC('H', '2', '6', '4'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 118 | // Match any fourcc. |
| 119 | |
| 120 | // We move this out of the enum because using it in many places caused |
| 121 | // the compiler to get grumpy, presumably since the above enum is |
| 122 | // backed by an int. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 123 | static const uint32_t FOURCC_ANY = 0xFFFFFFFF; |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 124 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | // Converts fourcc aliases into canonical ones. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 126 | uint32_t CanonicalFourCC(uint32_t fourcc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | |
| 128 | // Get FourCC code as a string. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 129 | inline std::string GetFourccName(uint32_t fourcc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | std::string name; |
| 131 | name.push_back(static_cast<char>(fourcc & 0xFF)); |
| 132 | name.push_back(static_cast<char>((fourcc >> 8) & 0xFF)); |
| 133 | name.push_back(static_cast<char>((fourcc >> 16) & 0xFF)); |
| 134 | name.push_back(static_cast<char>((fourcc >> 24) & 0xFF)); |
| 135 | return name; |
| 136 | } |
| 137 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | ////////////////////////////////////////////////////////////////////////////// |
| 139 | // Definition of VideoFormat. |
| 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | // VideoFormat with Plain Old Data for global variables. |
| 143 | struct VideoFormatPod { |
| 144 | int width; // Number of pixels. |
| 145 | int height; // Number of pixels. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 146 | int64_t interval; // Nanoseconds. |
| 147 | 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] | 148 | }; |
| 149 | |
| 150 | struct VideoFormat : VideoFormatPod { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 151 | static const int64_t kMinimumInterval = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 152 | rtc::kNumNanosecsPerSec / 10000; // 10k fps. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | |
| 154 | VideoFormat() { |
| 155 | Construct(0, 0, 0, 0); |
| 156 | } |
| 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) / |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 189 | static_cast<float>(interval); |
| 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 && |
| 194 | interval == format.interval && fourcc == format.fourcc; |
| 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) || |
| 203 | (fourcc == format.fourcc && width < format.width) || |
| 204 | (fourcc == format.fourcc && width == format.width && |
| 205 | height < format.height) || |
| 206 | (fourcc == format.fourcc && width == format.width && |
| 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() < |
| 219 | format.width * format.height * format.framerate(); |
| 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 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 228 | #endif // WEBRTC_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT |