blob: 7b6aac206bfa7bf283387eb0c7f4e0b46b8ce710 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2010 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 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
28#include "talk/media/base/videocommon.h"
29
30#include <limits.h> // For INT_MAX
31#include <math.h>
32#include <sstream>
33
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/common.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36namespace cricket {
37
38struct FourCCAliasEntry {
Peter Boström0c4e06b2015-10-07 12:23:21 +020039 uint32_t alias;
40 uint32_t canonical;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041};
42
43static const FourCCAliasEntry kFourCCAliases[] = {
44 {FOURCC_IYUV, FOURCC_I420},
45 {FOURCC_YU16, FOURCC_I422},
46 {FOURCC_YU24, FOURCC_I444},
47 {FOURCC_YUYV, FOURCC_YUY2},
48 {FOURCC_YUVS, FOURCC_YUY2},
49 {FOURCC_HDYC, FOURCC_UYVY},
50 {FOURCC_2VUY, FOURCC_UYVY},
51 {FOURCC_JPEG, FOURCC_MJPG}, // Note: JPEG has DHT while MJPG does not.
52 {FOURCC_DMB1, FOURCC_MJPG},
53 {FOURCC_BA81, FOURCC_BGGR},
54 {FOURCC_RGB3, FOURCC_RAW},
55 {FOURCC_BGR3, FOURCC_24BG},
56 {FOURCC_CM32, FOURCC_BGRA},
57 {FOURCC_CM24, FOURCC_RAW},
58};
59
Peter Boström0c4e06b2015-10-07 12:23:21 +020060uint32_t CanonicalFourCC(uint32_t fourcc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 for (int i = 0; i < ARRAY_SIZE(kFourCCAliases); ++i) {
62 if (kFourCCAliases[i].alias == fourcc) {
63 return kFourCCAliases[i].canonical;
64 }
65 }
66 // Not an alias, so return it as-is.
67 return fourcc;
68}
69
70static float kScaleFactors[] = {
71 1.f / 1.f, // Full size.
72 1.f / 2.f, // 1/2 scale.
73 1.f / 4.f, // 1/4 scale.
74 1.f / 8.f, // 1/8 scale.
75 1.f / 16.f // 1/16 scale.
76};
77
78static const int kNumScaleFactors = ARRAY_SIZE(kScaleFactors);
79
80// Finds the scale factor that, when applied to width and height, produces
81// fewer than num_pixels.
82static float FindLowerScale(int width, int height, int target_num_pixels) {
83 if (!target_num_pixels) {
84 return 0.f;
85 }
86 int best_distance = INT_MAX;
87 int best_index = kNumScaleFactors - 1; // Default to max scale.
88 for (int i = 0; i < kNumScaleFactors; ++i) {
89 int test_num_pixels = static_cast<int>(width * kScaleFactors[i] *
90 height * kScaleFactors[i]);
91 int diff = target_num_pixels - test_num_pixels;
92 if (diff >= 0 && diff < best_distance) {
93 best_distance = diff;
94 best_index = i;
95 if (best_distance == 0) { // Found exact match.
96 break;
97 }
98 }
99 }
100 return kScaleFactors[best_index];
101}
102
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000103// Computes a scale less to fit in max_pixels while maintaining aspect ratio.
104void ComputeScaleMaxPixels(int frame_width, int frame_height, int max_pixels,
105 int* scaled_width, int* scaled_height) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 ASSERT(scaled_width != NULL);
107 ASSERT(scaled_height != NULL);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000108 ASSERT(max_pixels > 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 // For VP8 the values for max width and height can be found here
110 // webrtc/src/video_engine/vie_defines.h (kViEMaxCodecWidth and
111 // kViEMaxCodecHeight)
112 const int kMaxWidth = 4096;
113 const int kMaxHeight = 3072;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 int new_frame_width = frame_width;
115 int new_frame_height = frame_height;
116
117 // Limit width.
118 if (new_frame_width > kMaxWidth) {
119 new_frame_height = new_frame_height * kMaxWidth / new_frame_width;
120 new_frame_width = kMaxWidth;
121 }
122 // Limit height.
123 if (new_frame_height > kMaxHeight) {
124 new_frame_width = new_frame_width * kMaxHeight / new_frame_height;
125 new_frame_height = kMaxHeight;
126 }
127 // Limit number of pixels.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000128 if (new_frame_width * new_frame_height > max_pixels) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Compute new width such that width * height is less than maximum but
130 // maintains original captured frame aspect ratio.
131 new_frame_width = static_cast<int>(sqrtf(static_cast<float>(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000132 max_pixels) * new_frame_width / new_frame_height));
133 new_frame_height = max_pixels / new_frame_width;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 }
135 // Snap to a scale factor that is less than or equal to target pixels.
136 float scale = FindLowerScale(frame_width, frame_height,
137 new_frame_width * new_frame_height);
138 *scaled_width = static_cast<int>(frame_width * scale + .5f);
139 *scaled_height = static_cast<int>(frame_height * scale + .5f);
140}
141
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000142// Compute a size to scale frames to that is below maximum compression
143// and rendering size with the same aspect ratio.
144void ComputeScale(int frame_width, int frame_height, int fps,
145 int* scaled_width, int* scaled_height) {
146 // Maximum pixels limit is set to Retina MacBookPro 15" resolution of
147 // 2880 x 1800 as of 4/18/2013.
148 // For high fps, maximum pixels limit is set based on common 24" monitor
149 // resolution of 2048 x 1280 as of 6/13/2013. The Retina resolution is
150 // therefore reduced to 1440 x 900.
151 int max_pixels = (fps > 5) ? 2048 * 1280 : 2880 * 1800;
152 ComputeScaleMaxPixels(
153 frame_width, frame_height, max_pixels, scaled_width, scaled_height);
154}
155
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156// Compute size to crop video frame to.
157// If cropped_format_* is 0, return the frame_* size as is.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000158void ComputeCrop(int cropped_format_width, int cropped_format_height,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 int frame_width, int frame_height,
160 int pixel_width, int pixel_height,
161 int rotation,
162 int* cropped_width, int* cropped_height) {
wu@webrtc.org97077a32013-10-25 21:18:33 +0000163 // Transform screen crop to camera space if rotated.
164 if (rotation == 90 || rotation == 270) {
165 std::swap(cropped_format_width, cropped_format_height);
166 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 ASSERT(cropped_format_width >= 0);
168 ASSERT(cropped_format_height >= 0);
169 ASSERT(frame_width > 0);
170 ASSERT(frame_height > 0);
171 ASSERT(pixel_width >= 0);
172 ASSERT(pixel_height >= 0);
173 ASSERT(rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270);
174 ASSERT(cropped_width != NULL);
175 ASSERT(cropped_height != NULL);
176 if (!pixel_width) {
177 pixel_width = 1;
178 }
179 if (!pixel_height) {
180 pixel_height = 1;
181 }
182 // if cropped_format is 0x0 disable cropping.
183 if (!cropped_format_height) {
184 cropped_format_height = 1;
185 }
186 float frame_aspect = static_cast<float>(frame_width * pixel_width) /
187 static_cast<float>(frame_height * pixel_height);
188 float crop_aspect = static_cast<float>(cropped_format_width) /
189 static_cast<float>(cropped_format_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 // kAspectThresh is the maximum aspect ratio difference that we'll accept
wu@webrtc.org97077a32013-10-25 21:18:33 +0000191 // for cropping. The value 1.34 allows cropping from 4:3 to 16:9.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 // Set to zero to disable cropping entirely.
193 // TODO(fbarchard): crop to multiple of 16 width for better performance.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000194 const float kAspectThresh = 1.34f;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 // Wide aspect - crop horizontally
196 if (frame_aspect > crop_aspect &&
197 frame_aspect < crop_aspect * kAspectThresh) {
198 // Round width down to multiple of 4 to avoid odd chroma width.
199 // Width a multiple of 4 allows a half size image to have chroma channel
wu@webrtc.org97077a32013-10-25 21:18:33 +0000200 // that avoids rounding errors.
201 frame_width = static_cast<int>((crop_aspect * frame_height *
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 pixel_height) / pixel_width + 0.5f) & ~3;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000203 } else if (frame_aspect < crop_aspect &&
204 frame_aspect > crop_aspect / kAspectThresh) {
205 frame_height = static_cast<int>((frame_width * pixel_width) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 (crop_aspect * pixel_height) + 0.5f) & ~1;
207 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000208 *cropped_width = frame_width;
209 *cropped_height = frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210}
211
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000212// Compute the frame size that makes pixels square pixel aspect ratio.
213void ComputeScaleToSquarePixels(int in_width, int in_height,
214 int pixel_width, int pixel_height,
215 int* scaled_width, int* scaled_height) {
216 *scaled_width = in_width; // Keep width the same.
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000217 *scaled_height = in_height * pixel_height / pixel_width;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000218}
219
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220// The C++ standard requires a namespace-scope definition of static const
221// integral types even when they are initialized in the declaration (see
222// [class.static.data]/4), but MSVC with /Ze is non-conforming and treats that
223// as a multiply defined symbol error. See Also:
224// http://msdn.microsoft.com/en-us/library/34h23df8.aspx
225#ifndef _MSC_EXTENSIONS
Peter Boström0c4e06b2015-10-07 12:23:21 +0200226const int64_t VideoFormat::kMinimumInterval; // Initialized in header.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227#endif
228
229std::string VideoFormat::ToString() const {
230 std::string fourcc_name = GetFourccName(fourcc) + " ";
231 for (std::string::const_iterator i = fourcc_name.begin();
232 i < fourcc_name.end(); ++i) {
233 // Test character is printable; Avoid isprint() which asserts on negatives.
234 if (*i < 32 || *i >= 127) {
235 fourcc_name = "";
236 break;
237 }
238 }
239
240 std::ostringstream ss;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000241 ss << fourcc_name << width << "x" << height << "x"
242 << IntervalToFpsFloat(interval);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 return ss.str();
244}
245
246} // namespace cricket