• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // libjingle
2 // Copyright 2011, 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_SESSION_PHONE_VIDEOCOMMON_H_
29 #define TALK_SESSION_PHONE_VIDEOCOMMON_H_
30 
31 #include <string>
32 
33 #include "talk/base/basictypes.h"
34 
35 namespace cricket {
36 
37 //////////////////////////////////////////////////////////////////////////////
38 // Definition of fourcc.
39 //////////////////////////////////////////////////////////////////////////////
40 // Convert four characters to a fourcc code.
41 // Needs to be a macro otherwise the OS X compiler complains when the kFormat*
42 // constants are used in a switch.
43 #define FOURCC(a, b, c, d) (\
44     (static_cast<uint32>(a)) | (static_cast<uint32>(b) << 8) | \
45     (static_cast<uint32>(c) << 16) | (static_cast<uint32>(d) << 24))
46 
47 // Get the name, that is, string with four characters, of a fourcc code.
GetFourccName(uint32 fourcc)48 inline std::string GetFourccName(uint32 fourcc) {
49   std::string name;
50   name.push_back(static_cast<char>(fourcc & 0xFF));
51   name.push_back(static_cast<char>((fourcc >> 8) & 0xFF));
52   name.push_back(static_cast<char>((fourcc >> 16) & 0xFF));
53   name.push_back(static_cast<char>((fourcc >> 24) & 0xFF));
54   return name;
55 }
56 
57 // FourCC codes used in Google Talk.
58 // Some good pages discussing FourCC codes:
59 //   http://developer.apple.com/quicktime/icefloe/dispatch020.html
60 //   http://www.fourcc.org/yuv.php
61 enum FourCC {
62   // Canonical fourcc codes used in our code.
63   FOURCC_I420 = FOURCC('I', '4', '2', '0'),
64   FOURCC_YUY2 = FOURCC('Y', 'U', 'Y', '2'),
65   FOURCC_UYVY = FOURCC('U', 'Y', 'V', 'Y'),
66   FOURCC_M420 = FOURCC('M', '4', '2', '0'),
67   FOURCC_24BG = FOURCC('2', '4', 'B', 'G'),
68   FOURCC_ABGR = FOURCC('A', 'B', 'G', 'R'),
69   FOURCC_BGRA = FOURCC('B', 'G', 'R', 'A'),
70   FOURCC_ARGB = FOURCC('A', 'R', 'G', 'B'),
71   FOURCC_MJPG = FOURCC('M', 'J', 'P', 'G'),
72   FOURCC_RAW  = FOURCC('r', 'a', 'w', ' '),
73   FOURCC_NV21 = FOURCC('N', 'V', '2', '1'),
74   FOURCC_NV12 = FOURCC('N', 'V', '1', '2'),
75   // Next four are Bayer RGB formats. The four characters define the order of
76   // the colours in each 2x2 pixel grid, going left-to-right and top-to-bottom.
77   FOURCC_RGGB = FOURCC('R', 'G', 'G', 'B'),
78   FOURCC_BGGR = FOURCC('B', 'G', 'G', 'R'),
79   FOURCC_GRBG = FOURCC('G', 'R', 'B', 'G'),
80   FOURCC_GBRG = FOURCC('G', 'B', 'R', 'G'),
81 
82   // Aliases for canonical fourcc codes, replaced with their canonical
83   // equivalents by CanonicalFourCC().
84   FOURCC_IYUV = FOURCC('I', 'Y', 'U', 'V'),  // Alias for I420
85   FOURCC_YU12 = FOURCC('Y', 'U', '1', '2'),  // Alias for I420
86   FOURCC_YUYV = FOURCC('Y', 'U', 'Y', 'V'),  // Alias for YUY2
87   FOURCC_YUVS = FOURCC('y', 'u', 'v', 's'),  // Alias for YUY2 on Mac
88   FOURCC_HDYC = FOURCC('H', 'D', 'Y', 'C'),  // Alias for UYVY
89   FOURCC_2VUY = FOURCC('2', 'v', 'u', 'y'),  // Alias for UYVY
90   FOURCC_JPEG = FOURCC('J', 'P', 'E', 'G'),  // Alias for MJPG
91   FOURCC_BA81 = FOURCC('B', 'A', '8', '1'),  // Alias for BGGR
92 
93   // Match any fourcc.
94   FOURCC_ANY  = 0xFFFFFFFF,
95 };
96 
97 // Converts fourcc aliases into canonical ones.
98 uint32 CanonicalFourCC(uint32 fourcc);
99 
100 //////////////////////////////////////////////////////////////////////////////
101 // Definition of VideoFormat.
102 //////////////////////////////////////////////////////////////////////////////
103 
104 static const int64 kNumNanosecsPerSec = 1000000000;
105 
106 struct VideoFormat {
107   static const int64 kMinimumInterval = kNumNanosecsPerSec / 10000;  // 10k fps
108 
VideoFormatVideoFormat109   VideoFormat() : width(0), height(0), interval(0), fourcc(0) {}
110 
VideoFormatVideoFormat111   VideoFormat(int w, int h, int64 interval_ns, uint32 cc)
112       : width(w),
113         height(h),
114         interval(interval_ns),
115         fourcc(cc) {
116   }
117 
VideoFormatVideoFormat118   VideoFormat(const VideoFormat& format)
119       : width(format.width),
120         height(format.height),
121         interval(format.interval),
122         fourcc(format.fourcc) {
123   }
124 
FpsToIntervalVideoFormat125   static int64 FpsToInterval(int fps) {
126     return fps ? kNumNanosecsPerSec / fps : kMinimumInterval;
127   }
128 
IntervalToFpsVideoFormat129   static int IntervalToFps(int64 interval) {
130     // Normalize the interval first.
131     interval = talk_base::_max(interval, kMinimumInterval);
132     return static_cast<int>(kNumNanosecsPerSec / interval);
133   }
134 
135   bool operator==(const VideoFormat& format) const {
136     return width == format.width && height == format.height &&
137         interval == format.interval && fourcc == format.fourcc;
138   }
139 
140   bool operator!=(const VideoFormat& format) const {
141     return !(*this == format);
142   }
143 
144   bool operator<(const VideoFormat& format) const {
145     return (fourcc < format.fourcc) ||
146         (fourcc == format.fourcc && width < format.width) ||
147         (fourcc == format.fourcc && width == format.width &&
148             height < format.height) ||
149         (fourcc == format.fourcc && width == format.width &&
150             height == format.height && interval > format.interval);
151   }
152 
framerateVideoFormat153   int framerate() const { return IntervalToFps(interval); }
154 
155   int    width;     // in number of pixels
156   int    height;    // in number of pixels
157   int64  interval;  // in nanoseconds
158   uint32 fourcc;    // color space. FOURCC_ANY means that any color space is OK.
159 };
160 
161 // Result of video capturer start.
162 enum CaptureResult {
163   CR_SUCCESS,    // The capturer starts successfully.
164   CR_PENDING,    // The capturer is pending to start the capture device.
165   CR_FAILURE,    // The capturer fails to start.
166   CR_NO_DEVICE,  // The capturer has no device and fails to start.
167 };
168 
169 }  // namespace cricket
170 
171 #endif  // TALK_SESSION_PHONE_VIDEOCOMMON_H_
172