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_ 29 #define TALK_MEDIA_BASE_VIDEOCOMMON_H_ 30 31 #include <string> 32 33 #include "talk/base/basictypes.h" 34 #include "talk/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 ? talk_base::kNumNanosecsPerSec / fps : \ 48 talk_base::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 void ComputeScale(int frame_width, int frame_height, int fps, 151 int* scaled_width, int* scaled_height); 152 153 // Compute the frame size that conversion should crop to based on aspect ratio. 154 // Ensures size is multiple of 2 due to I420 and conversion limitations. 155 void ComputeCrop(int cropped_format_width, int cropped_format_height, 156 int frame_width, int frame_height, 157 int pixel_width, int pixel_height, 158 int rotation, 159 int* cropped_width, int* cropped_height); 160 161 ////////////////////////////////////////////////////////////////////////////// 162 // Definition of VideoFormat. 163 ////////////////////////////////////////////////////////////////////////////// 164 165 // VideoFormat with Plain Old Data for global variables. 166 struct VideoFormatPod { 167 int width; // Number of pixels. 168 int height; // Number of pixels. 169 int64 interval; // Nanoseconds. 170 uint32 fourcc; // Color space. FOURCC_ANY means that any color space is OK. 171 }; 172 173 struct VideoFormat : VideoFormatPod { 174 static const int64 kMinimumInterval = 175 talk_base::kNumNanosecsPerSec / 10000; // 10k fps. 176 177 VideoFormat() { 178 Construct(0, 0, 0, 0); 179 } 180 181 VideoFormat(int w, int h, int64 interval_ns, uint32 cc) { 182 Construct(w, h, interval_ns, cc); 183 } 184 185 explicit VideoFormat(const VideoFormatPod& format) { 186 Construct(format.width, format.height, format.interval, format.fourcc); 187 } 188 189 void Construct(int w, int h, int64 interval_ns, uint32 cc) { 190 width = w; 191 height = h; 192 interval = interval_ns; 193 fourcc = cc; 194 } 195 196 static int64 FpsToInterval(int fps) { 197 return fps ? talk_base::kNumNanosecsPerSec / fps : kMinimumInterval; 198 } 199 200 static int IntervalToFps(int64 interval) { 201 // Normalize the interval first. 202 interval = talk_base::_max(interval, kMinimumInterval); 203 return static_cast<int>(talk_base::kNumNanosecsPerSec / interval); 204 } 205 206 bool operator==(const VideoFormat& format) const { 207 return width == format.width && height == format.height && 208 interval == format.interval && fourcc == format.fourcc; 209 } 210 211 bool operator!=(const VideoFormat& format) const { 212 return !(*this == format); 213 } 214 215 bool operator<(const VideoFormat& format) const { 216 return (fourcc < format.fourcc) || 217 (fourcc == format.fourcc && width < format.width) || 218 (fourcc == format.fourcc && width == format.width && 219 height < format.height) || 220 (fourcc == format.fourcc && width == format.width && 221 height == format.height && interval > format.interval); 222 } 223 224 int framerate() const { return IntervalToFps(interval); } 225 226 // Check if both width and height are 0. 227 bool IsSize0x0() const { return 0 == width && 0 == height; } 228 229 // Check if this format is less than another one by comparing the resolution 230 // and frame rate. 231 bool IsPixelRateLess(const VideoFormat& format) const { 232 return width * height * framerate() < 233 format.width * format.height * format.framerate(); 234 } 235 236 // Get a string presentation in the form of "fourcc width x height x fps" 237 std::string ToString() const; 238 }; 239 240 } // namespace cricket 241 242 #endif // TALK_MEDIA_BASE_VIDEOCOMMON_H_ 243