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 | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 13 | #ifndef MEDIA_BASE_VIDEO_COMMON_H_ |
| 14 | #define MEDIA_BASE_VIDEO_COMMON_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 | 1ddc5b6 | 2018-10-19 10:35:14 +0200 | [diff] [blame] | 20 | #include "rtc_base/system/rtc_export.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | ////////////////////////////////////////////////////////////////////////////// |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | #define CRICKET_FOURCC(a, b, c, d) \ |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 32 | ((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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | // 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. |
| 45 | enum FourCC { |
| 46 | // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 47 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | |
| 57 | // 2 Secondary YUV formats: row biplanar. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 58 | FOURCC_M420 = CRICKET_FOURCC('M', '4', '2', '0'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
| 60 | // 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] | 61 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 65 | FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '), |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 66 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | |
| 71 | // 4 Secondary RGB formats: 4 Bayer Patterns. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 72 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | |
| 77 | // 1 Primary Compressed YUV format. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 78 | FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | |
| 80 | // 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] | 81 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 88 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | |
| 104 | // 1 Auxiliary compressed YUV format set aside for capturer. |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 105 | FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Patrik Höglund | 4e76ebb | 2017-12-01 11:54:47 +0100 | [diff] [blame] | 108 | #undef CRICKET_FOURCC |
| 109 | |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 110 | // 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 115 | static const uint32_t FOURCC_ANY = 0xFFFFFFFF; |
pthatcher@webrtc.org | 40b276e | 2014-12-12 02:44:30 +0000 | [diff] [blame] | 116 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | // Converts fourcc aliases into canonical ones. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 118 | uint32_t CanonicalFourCC(uint32_t fourcc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | |
| 120 | // Get FourCC code as a string. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 121 | inline std::string GetFourccName(uint32_t fourcc) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | // Definition of VideoFormat. |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | // VideoFormat with Plain Old Data for global variables. |
| 135 | struct VideoFormatPod { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 136 | int width; // Number of pixels. |
| 137 | int height; // Number of pixels. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 138 | int64_t interval; // Nanoseconds. |
| 139 | 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] | 140 | }; |
| 141 | |
Mirko Bonadei | 1ddc5b6 | 2018-10-19 10:35:14 +0200 | [diff] [blame] | 142 | struct RTC_EXPORT VideoFormat : VideoFormatPod { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 143 | static const int64_t kMinimumInterval = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 144 | rtc::kNumNanosecsPerSec / 10000; // 10k fps. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 145 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | VideoFormat() { Construct(0, 0, 0, 0); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 148 | 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] | 149 | 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 156 | 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] | 157 | width = w; |
| 158 | height = h; |
| 159 | interval = interval_ns; |
| 160 | fourcc = cc; |
| 161 | } |
| 162 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 163 | static int64_t FpsToInterval(int fps) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 164 | return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 167 | static int IntervalToFps(int64_t interval) { |
henrike@webrtc.org | 18e5911 | 2014-03-14 17:19:38 +0000 | [diff] [blame] | 168 | if (!interval) { |
| 169 | return 0; |
| 170 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 171 | return static_cast<int>(rtc::kNumNanosecsPerSec / interval); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 174 | static float IntervalToFpsFloat(int64_t interval) { |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 175 | if (!interval) { |
| 176 | return 0.f; |
| 177 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 178 | return static_cast<float>(rtc::kNumNanosecsPerSec) / |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 179 | static_cast<float>(interval); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 180 | } |
| 181 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | bool operator==(const VideoFormat& format) const { |
| 183 | return width == format.width && height == format.height && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 184 | interval == format.interval && fourcc == format.fourcc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 185 | } |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 193 | (fourcc == format.fourcc && width < format.width) || |
| 194 | (fourcc == format.fourcc && width == format.width && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | height < format.height) || |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 196 | (fourcc == format.fourcc && width == format.width && |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 209 | format.width * format.height * format.framerate(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | } |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 218 | #endif // MEDIA_BASE_VIDEO_COMMON_H_ |