1 // libjingle 2 // Copyright 2004 Google Inc. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are met: 6 // 7 // 1. Redistributions of source code must retain the above copyright notice, 8 // this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright notice, 10 // this list of conditions and the following disclaimer in the documentation 11 // and/or other materials provided with the distribution. 12 // 3. The name of the author may not be used to endorse or promote products 13 // derived from this software without specific prior written permission. 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 // 26 // Common definition for video, including fourcc and VideoFormat. 27 28 #ifndef TALK_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT 29 #define TALK_MEDIA_BASE_VIDEOCOMMON_H_ 30 31 #include <string> 32 33 #include "webrtc/base/basictypes.h" 34 #include "webrtc/base/timeutils.h" 35 36 namespace cricket { 37 38 // TODO(janahan): For now, a hard-coded ssrc is used as the video ssrc. 39 // This is because when the video frame is passed to the mediaprocessor for 40 // processing, it doesn't have the correct ssrc. Since currently only Tx 41 // Video processing is supported, this is ok. When we switch over to trigger 42 // from capturer, this should be fixed and this const removed. 43 const uint32 kDummyVideoSsrc = 0xFFFFFFFF; 44 45 // Minimum interval is 10k fps. 46 #define FPS_TO_INTERVAL(fps) \ 47 (fps ? rtc::kNumNanosecsPerSec / fps : \ 48 rtc::kNumNanosecsPerSec / 10000) 49 50 ////////////////////////////////////////////////////////////////////////////// 51 // Definition of FourCC codes 52 ////////////////////////////////////////////////////////////////////////////// 53 // Convert four characters to a FourCC code. 54 // Needs to be a macro otherwise the OS X compiler complains when the kFormat* 55 // constants are used in a switch. 56 #define FOURCC(a, b, c, d) ( \ 57 (static_cast<uint32>(a)) | (static_cast<uint32>(b) << 8) | \ 58 (static_cast<uint32>(c) << 16) | (static_cast<uint32>(d) << 24)) 59 // Some pages discussing FourCC codes: 60 // http://www.fourcc.org/yuv.php 61 // http://v4l2spec.bytesex.org/spec/book1.htm 62 // http://developer.apple.com/quicktime/icefloe/dispatch020.html 63 // http://msdn.microsoft.com/library/windows/desktop/dd206750.aspx#nv12 64 // http://people.xiph.org/~xiphmont/containers/nut/nut4cc.txt 65 66 // FourCC codes grouped according to implementation efficiency. 67 // Primary formats should convert in 1 efficient step. 68 // Secondary formats are converted in 2 steps. 69 // Auxilliary formats call primary converters. 70 enum FourCC { 71 // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed. 72 FOURCC_I420 = FOURCC('I', '4', '2', '0'), 73 FOURCC_I422 = FOURCC('I', '4', '2', '2'), 74 FOURCC_I444 = FOURCC('I', '4', '4', '4'), 75 FOURCC_I411 = FOURCC('I', '4', '1', '1'), 76 FOURCC_I400 = FOURCC('I', '4', '0', '0'), 77 FOURCC_NV21 = FOURCC('N', 'V', '2', '1'), 78 FOURCC_NV12 = FOURCC('N', 'V', '1', '2'), 79 FOURCC_YUY2 = FOURCC('Y', 'U', 'Y', '2'), 80 FOURCC_UYVY = FOURCC('U', 'Y', 'V', 'Y'), 81 82 // 2 Secondary YUV formats: row biplanar. 83 FOURCC_M420 = FOURCC('M', '4', '2', '0'), 84 FOURCC_Q420 = FOURCC('Q', '4', '2', '0'), 85 86 // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp. 87 FOURCC_ARGB = FOURCC('A', 'R', 'G', 'B'), 88 FOURCC_BGRA = FOURCC('B', 'G', 'R', 'A'), 89 FOURCC_ABGR = FOURCC('A', 'B', 'G', 'R'), 90 FOURCC_24BG = FOURCC('2', '4', 'B', 'G'), 91 FOURCC_RAW = FOURCC('r', 'a', 'w', ' '), 92 FOURCC_RGBA = FOURCC('R', 'G', 'B', 'A'), 93 FOURCC_RGBP = FOURCC('R', 'G', 'B', 'P'), // bgr565. 94 FOURCC_RGBO = FOURCC('R', 'G', 'B', 'O'), // abgr1555. 95 FOURCC_R444 = FOURCC('R', '4', '4', '4'), // argb4444. 96 97 // 4 Secondary RGB formats: 4 Bayer Patterns. 98 FOURCC_RGGB = FOURCC('R', 'G', 'G', 'B'), 99 FOURCC_BGGR = FOURCC('B', 'G', 'G', 'R'), 100 FOURCC_GRBG = FOURCC('G', 'R', 'B', 'G'), 101 FOURCC_GBRG = FOURCC('G', 'B', 'R', 'G'), 102 103 // 1 Primary Compressed YUV format. 104 FOURCC_MJPG = FOURCC('M', 'J', 'P', 'G'), 105 106 // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias. 107 FOURCC_YV12 = FOURCC('Y', 'V', '1', '2'), 108 FOURCC_YV16 = FOURCC('Y', 'V', '1', '6'), 109 FOURCC_YV24 = FOURCC('Y', 'V', '2', '4'), 110 FOURCC_YU12 = FOURCC('Y', 'U', '1', '2'), // Linux version of I420. 111 FOURCC_J420 = FOURCC('J', '4', '2', '0'), 112 FOURCC_J400 = FOURCC('J', '4', '0', '0'), 113 114 // 14 Auxiliary aliases. CanonicalFourCC() maps these to canonical fourcc. 115 FOURCC_IYUV = FOURCC('I', 'Y', 'U', 'V'), // Alias for I420. 116 FOURCC_YU16 = FOURCC('Y', 'U', '1', '6'), // Alias for I422. 117 FOURCC_YU24 = FOURCC('Y', 'U', '2', '4'), // Alias for I444. 118 FOURCC_YUYV = FOURCC('Y', 'U', 'Y', 'V'), // Alias for YUY2. 119 FOURCC_YUVS = FOURCC('y', 'u', 'v', 's'), // Alias for YUY2 on Mac. 120 FOURCC_HDYC = FOURCC('H', 'D', 'Y', 'C'), // Alias for UYVY. 121 FOURCC_2VUY = FOURCC('2', 'v', 'u', 'y'), // Alias for UYVY on Mac. 122 FOURCC_JPEG = FOURCC('J', 'P', 'E', 'G'), // Alias for MJPG. 123 FOURCC_DMB1 = FOURCC('d', 'm', 'b', '1'), // Alias for MJPG on Mac. 124 FOURCC_BA81 = FOURCC('B', 'A', '8', '1'), // Alias for BGGR. 125 FOURCC_RGB3 = FOURCC('R', 'G', 'B', '3'), // Alias for RAW. 126 FOURCC_BGR3 = FOURCC('B', 'G', 'R', '3'), // Alias for 24BG. 127 FOURCC_CM32 = FOURCC(0, 0, 0, 32), // Alias for BGRA kCMPixelFormat_32ARGB 128 FOURCC_CM24 = FOURCC(0, 0, 0, 24), // Alias for RAW kCMPixelFormat_24RGB 129 130 // 1 Auxiliary compressed YUV format set aside for capturer. 131 FOURCC_H264 = FOURCC('H', '2', '6', '4'), 132 133 // Match any fourcc. 134 FOURCC_ANY = 0xFFFFFFFF, 135 }; 136 137 // Converts fourcc aliases into canonical ones. 138 uint32 CanonicalFourCC(uint32 fourcc); 139 140 // Get FourCC code as a string. 141 inline std::string GetFourccName(uint32 fourcc) { 142 std::string name; 143 name.push_back(static_cast<char>(fourcc & 0xFF)); 144 name.push_back(static_cast<char>((fourcc >> 8) & 0xFF)); 145 name.push_back(static_cast<char>((fourcc >> 16) & 0xFF)); 146 name.push_back(static_cast<char>((fourcc >> 24) & 0xFF)); 147 return name; 148 } 149 150 // Computes a scale less to fit in max_pixels while maintaining aspect ratio. 151 void ComputeScaleMaxPixels(int frame_width, int frame_height, int max_pixels, 152 int* scaled_width, int* scaled_height); 153 154 // For low fps, max pixels limit is set to Retina MacBookPro 15" resolution of 155 // 2880 x 1800 as of 4/18/2013. 156 // For high fps, maximum pixels limit is set based on common 24" monitor 157 // resolution of 2048 x 1280 as of 6/13/2013. The Retina resolution is 158 // therefore reduced to 1440 x 900. 159 void ComputeScale(int frame_width, int frame_height, int fps, 160 int* scaled_width, int* scaled_height); 161 162 // Compute the frame size that conversion should crop to based on aspect ratio. 163 // Ensures size is multiple of 2 due to I420 and conversion limitations. 164 void ComputeCrop(int cropped_format_width, int cropped_format_height, 165 int frame_width, int frame_height, 166 int pixel_width, int pixel_height, 167 int rotation, 168 int* cropped_width, int* cropped_height); 169 170 // Compute the frame size that makes pixels square pixel aspect ratio. 171 void ComputeScaleToSquarePixels(int in_width, int in_height, 172 int pixel_width, int pixel_height, 173 int* scaled_width, int* scaled_height); 174 175 ////////////////////////////////////////////////////////////////////////////// 176 // Definition of VideoFormat. 177 ////////////////////////////////////////////////////////////////////////////// 178 179 // VideoFormat with Plain Old Data for global variables. 180 struct VideoFormatPod { 181 int width; // Number of pixels. 182 int height; // Number of pixels. 183 int64 interval; // Nanoseconds. 184 uint32 fourcc; // Color space. FOURCC_ANY means that any color space is OK. 185 }; 186 187 struct VideoFormat : VideoFormatPod { 188 static const int64 kMinimumInterval = 189 rtc::kNumNanosecsPerSec / 10000; // 10k fps. 190 191 VideoFormat() { 192 Construct(0, 0, 0, 0); 193 } 194 195 VideoFormat(int w, int h, int64 interval_ns, uint32 cc) { 196 Construct(w, h, interval_ns, cc); 197 } 198 199 explicit VideoFormat(const VideoFormatPod& format) { 200 Construct(format.width, format.height, format.interval, format.fourcc); 201 } 202 203 void Construct(int w, int h, int64 interval_ns, uint32 cc) { 204 width = w; 205 height = h; 206 interval = interval_ns; 207 fourcc = cc; 208 } 209 210 static int64 FpsToInterval(int fps) { 211 return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval; 212 } 213 214 static int IntervalToFps(int64 interval) { 215 if (!interval) { 216 return 0; 217 } 218 return static_cast<int>(rtc::kNumNanosecsPerSec / interval); 219 } 220 221 static float IntervalToFpsFloat(int64 interval) { 222 if (!interval) { 223 return 0.f; 224 } 225 return static_cast<float>(rtc::kNumNanosecsPerSec) / 226 static_cast<float>(interval); 227 } 228 229 bool operator==(const VideoFormat& format) const { 230 return width == format.width && height == format.height && 231 interval == format.interval && fourcc == format.fourcc; 232 } 233 234 bool operator!=(const VideoFormat& format) const { 235 return !(*this == format); 236 } 237 238 bool operator<(const VideoFormat& format) const { 239 return (fourcc < format.fourcc) || 240 (fourcc == format.fourcc && width < format.width) || 241 (fourcc == format.fourcc && width == format.width && 242 height < format.height) || 243 (fourcc == format.fourcc && width == format.width && 244 height == format.height && interval > format.interval); 245 } 246 247 int framerate() const { return IntervalToFps(interval); } 248 249 // Check if both width and height are 0. 250 bool IsSize0x0() const { return 0 == width && 0 == height; } 251 252 // Check if this format is less than another one by comparing the resolution 253 // and frame rate. 254 bool IsPixelRateLess(const VideoFormat& format) const { 255 return width * height * framerate() < 256 format.width * format.height * format.framerate(); 257 } 258 259 // Get a string presentation in the form of "fourcc width x height x fps" 260 std::string ToString() const; 261 }; 262 263 } // namespace cricket 264 265 #endif // TALK_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT 266