blob: a175c131afef799622c79df387e5e4465f92f16f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001// 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
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000028#ifndef TALK_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029#define TALK_MEDIA_BASE_VIDEOCOMMON_H_
30
31#include <string>
32
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/basictypes.h"
34#include "webrtc/base/timeutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36namespace 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.
43const uint32 kDummyVideoSsrc = 0xFFFFFFFF;
44
45// Minimum interval is 10k fps.
46#define FPS_TO_INTERVAL(fps) \
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047 (fps ? rtc::kNumNanosecsPerSec / fps : \
48 rtc::kNumNanosecsPerSec / 10000)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
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.
70enum 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.
138uint32 CanonicalFourCC(uint32 fourcc);
139
140// Get FourCC code as a string.
141inline 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
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000150// Computes a scale less to fit in max_pixels while maintaining aspect ratio.
151void 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159void 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.
164void 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
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000170// Compute the frame size that makes pixels square pixel aspect ratio.
171void ComputeScaleToSquarePixels(int in_width, int in_height,
172 int pixel_width, int pixel_height,
173 int* scaled_width, int* scaled_height);
174
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175//////////////////////////////////////////////////////////////////////////////
176// Definition of VideoFormat.
177//////////////////////////////////////////////////////////////////////////////
178
179// VideoFormat with Plain Old Data for global variables.
180struct 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
187struct VideoFormat : VideoFormatPod {
188 static const int64 kMinimumInterval =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000189 rtc::kNumNanosecsPerSec / 10000; // 10k fps.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190
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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000211 return fps ? rtc::kNumNanosecsPerSec / fps : kMinimumInterval;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 }
213
214 static int IntervalToFps(int64 interval) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +0000215 if (!interval) {
216 return 0;
217 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000218 return static_cast<int>(rtc::kNumNanosecsPerSec / interval);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 }
220
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000221 static float IntervalToFpsFloat(int64 interval) {
222 if (!interval) {
223 return 0.f;
224 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000225 return static_cast<float>(rtc::kNumNanosecsPerSec) /
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000226 static_cast<float>(interval);
227 }
228
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 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
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000265#endif // TALK_MEDIA_BASE_VIDEOCOMMON_H_ // NOLINT