blob: f6f06c24e320ab3f51ade5ae08179a3af857d2c9 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
kjellandera96e2d72016-02-04 23:52:28 -080011#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <limits.h> // For INT_MAX
14#include <math.h>
15#include <sstream>
16
tfarina5237aaf2015-11-10 23:44:30 -080017#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/common.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
20namespace cricket {
21
22struct FourCCAliasEntry {
Peter Boström0c4e06b2015-10-07 12:23:21 +020023 uint32_t alias;
24 uint32_t canonical;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025};
26
27static const FourCCAliasEntry kFourCCAliases[] = {
28 {FOURCC_IYUV, FOURCC_I420},
29 {FOURCC_YU16, FOURCC_I422},
30 {FOURCC_YU24, FOURCC_I444},
31 {FOURCC_YUYV, FOURCC_YUY2},
32 {FOURCC_YUVS, FOURCC_YUY2},
33 {FOURCC_HDYC, FOURCC_UYVY},
34 {FOURCC_2VUY, FOURCC_UYVY},
35 {FOURCC_JPEG, FOURCC_MJPG}, // Note: JPEG has DHT while MJPG does not.
36 {FOURCC_DMB1, FOURCC_MJPG},
37 {FOURCC_BA81, FOURCC_BGGR},
38 {FOURCC_RGB3, FOURCC_RAW},
39 {FOURCC_BGR3, FOURCC_24BG},
40 {FOURCC_CM32, FOURCC_BGRA},
41 {FOURCC_CM24, FOURCC_RAW},
42};
43
Peter Boström0c4e06b2015-10-07 12:23:21 +020044uint32_t CanonicalFourCC(uint32_t fourcc) {
kjellandera96e2d72016-02-04 23:52:28 -080045 for (uint32_t i = 0; i < arraysize(kFourCCAliases); ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046 if (kFourCCAliases[i].alias == fourcc) {
47 return kFourCCAliases[i].canonical;
48 }
49 }
50 // Not an alias, so return it as-is.
51 return fourcc;
52}
53
54static float kScaleFactors[] = {
55 1.f / 1.f, // Full size.
56 1.f / 2.f, // 1/2 scale.
57 1.f / 4.f, // 1/4 scale.
58 1.f / 8.f, // 1/8 scale.
59 1.f / 16.f // 1/16 scale.
60};
61
tfarina5237aaf2015-11-10 23:44:30 -080062static const int kNumScaleFactors = arraysize(kScaleFactors);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64// Finds the scale factor that, when applied to width and height, produces
65// fewer than num_pixels.
66static float FindLowerScale(int width, int height, int target_num_pixels) {
67 if (!target_num_pixels) {
68 return 0.f;
69 }
70 int best_distance = INT_MAX;
71 int best_index = kNumScaleFactors - 1; // Default to max scale.
72 for (int i = 0; i < kNumScaleFactors; ++i) {
73 int test_num_pixels = static_cast<int>(width * kScaleFactors[i] *
74 height * kScaleFactors[i]);
75 int diff = target_num_pixels - test_num_pixels;
76 if (diff >= 0 && diff < best_distance) {
77 best_distance = diff;
78 best_index = i;
79 if (best_distance == 0) { // Found exact match.
80 break;
81 }
82 }
83 }
84 return kScaleFactors[best_index];
85}
86
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000087// Computes a scale less to fit in max_pixels while maintaining aspect ratio.
88void ComputeScaleMaxPixels(int frame_width, int frame_height, int max_pixels,
89 int* scaled_width, int* scaled_height) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 ASSERT(scaled_width != NULL);
91 ASSERT(scaled_height != NULL);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000092 ASSERT(max_pixels > 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 const int kMaxWidth = 4096;
94 const int kMaxHeight = 3072;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 int new_frame_width = frame_width;
96 int new_frame_height = frame_height;
97
98 // Limit width.
99 if (new_frame_width > kMaxWidth) {
100 new_frame_height = new_frame_height * kMaxWidth / new_frame_width;
101 new_frame_width = kMaxWidth;
102 }
103 // Limit height.
104 if (new_frame_height > kMaxHeight) {
105 new_frame_width = new_frame_width * kMaxHeight / new_frame_height;
106 new_frame_height = kMaxHeight;
107 }
108 // Limit number of pixels.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000109 if (new_frame_width * new_frame_height > max_pixels) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 // Compute new width such that width * height is less than maximum but
111 // maintains original captured frame aspect ratio.
112 new_frame_width = static_cast<int>(sqrtf(static_cast<float>(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000113 max_pixels) * new_frame_width / new_frame_height));
114 new_frame_height = max_pixels / new_frame_width;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 }
116 // Snap to a scale factor that is less than or equal to target pixels.
117 float scale = FindLowerScale(frame_width, frame_height,
118 new_frame_width * new_frame_height);
119 *scaled_width = static_cast<int>(frame_width * scale + .5f);
120 *scaled_height = static_cast<int>(frame_height * scale + .5f);
121}
122
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000123// Compute a size to scale frames to that is below maximum compression
124// and rendering size with the same aspect ratio.
125void ComputeScale(int frame_width, int frame_height, int fps,
126 int* scaled_width, int* scaled_height) {
127 // Maximum pixels limit is set to Retina MacBookPro 15" resolution of
128 // 2880 x 1800 as of 4/18/2013.
129 // For high fps, maximum pixels limit is set based on common 24" monitor
130 // resolution of 2048 x 1280 as of 6/13/2013. The Retina resolution is
131 // therefore reduced to 1440 x 900.
132 int max_pixels = (fps > 5) ? 2048 * 1280 : 2880 * 1800;
133 ComputeScaleMaxPixels(
134 frame_width, frame_height, max_pixels, scaled_width, scaled_height);
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137// Compute size to crop video frame to.
138// If cropped_format_* is 0, return the frame_* size as is.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000139void ComputeCrop(int cropped_format_width, int cropped_format_height,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 int frame_width, int frame_height,
141 int pixel_width, int pixel_height,
142 int rotation,
143 int* cropped_width, int* cropped_height) {
wu@webrtc.org97077a32013-10-25 21:18:33 +0000144 // Transform screen crop to camera space if rotated.
145 if (rotation == 90 || rotation == 270) {
146 std::swap(cropped_format_width, cropped_format_height);
147 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 ASSERT(cropped_format_width >= 0);
149 ASSERT(cropped_format_height >= 0);
150 ASSERT(frame_width > 0);
151 ASSERT(frame_height > 0);
152 ASSERT(pixel_width >= 0);
153 ASSERT(pixel_height >= 0);
154 ASSERT(rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270);
155 ASSERT(cropped_width != NULL);
156 ASSERT(cropped_height != NULL);
157 if (!pixel_width) {
158 pixel_width = 1;
159 }
160 if (!pixel_height) {
161 pixel_height = 1;
162 }
163 // if cropped_format is 0x0 disable cropping.
164 if (!cropped_format_height) {
165 cropped_format_height = 1;
166 }
167 float frame_aspect = static_cast<float>(frame_width * pixel_width) /
168 static_cast<float>(frame_height * pixel_height);
169 float crop_aspect = static_cast<float>(cropped_format_width) /
170 static_cast<float>(cropped_format_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 // kAspectThresh is the maximum aspect ratio difference that we'll accept
wu@webrtc.org97077a32013-10-25 21:18:33 +0000172 // for cropping. The value 1.34 allows cropping from 4:3 to 16:9.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 // Set to zero to disable cropping entirely.
174 // TODO(fbarchard): crop to multiple of 16 width for better performance.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000175 const float kAspectThresh = 1.34f;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 // Wide aspect - crop horizontally
177 if (frame_aspect > crop_aspect &&
178 frame_aspect < crop_aspect * kAspectThresh) {
179 // Round width down to multiple of 4 to avoid odd chroma width.
180 // Width a multiple of 4 allows a half size image to have chroma channel
wu@webrtc.org97077a32013-10-25 21:18:33 +0000181 // that avoids rounding errors.
182 frame_width = static_cast<int>((crop_aspect * frame_height *
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 pixel_height) / pixel_width + 0.5f) & ~3;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000184 } else if (frame_aspect < crop_aspect &&
185 frame_aspect > crop_aspect / kAspectThresh) {
186 frame_height = static_cast<int>((frame_width * pixel_width) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 (crop_aspect * pixel_height) + 0.5f) & ~1;
188 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000189 *cropped_width = frame_width;
190 *cropped_height = frame_height;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191}
192
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000193// Compute the frame size that makes pixels square pixel aspect ratio.
194void ComputeScaleToSquarePixels(int in_width, int in_height,
195 int pixel_width, int pixel_height,
196 int* scaled_width, int* scaled_height) {
197 *scaled_width = in_width; // Keep width the same.
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000198 *scaled_height = in_height * pixel_height / pixel_width;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000199}
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201// The C++ standard requires a namespace-scope definition of static const
202// integral types even when they are initialized in the declaration (see
203// [class.static.data]/4), but MSVC with /Ze is non-conforming and treats that
204// as a multiply defined symbol error. See Also:
205// http://msdn.microsoft.com/en-us/library/34h23df8.aspx
206#ifndef _MSC_EXTENSIONS
Peter Boström0c4e06b2015-10-07 12:23:21 +0200207const int64_t VideoFormat::kMinimumInterval; // Initialized in header.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208#endif
209
210std::string VideoFormat::ToString() const {
211 std::string fourcc_name = GetFourccName(fourcc) + " ";
212 for (std::string::const_iterator i = fourcc_name.begin();
213 i < fourcc_name.end(); ++i) {
214 // Test character is printable; Avoid isprint() which asserts on negatives.
215 if (*i < 32 || *i >= 127) {
216 fourcc_name = "";
217 break;
218 }
219 }
220
221 std::ostringstream ss;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000222 ss << fourcc_name << width << "x" << height << "x"
223 << IntervalToFpsFloat(interval);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 return ss.str();
225}
226
227} // namespace cricket