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