| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 uint32_t CanonicalFourCC(uint32_t fourcc) { | 44 uint32_t CanonicalFourCC(uint32_t fourcc) { |
| 45 for (uint32_t i = 0; i < arraysize(kFourCCAliases); ++i) { | 45 for (uint32_t i = 0; i < arraysize(kFourCCAliases); ++i) { |
| 46 if (kFourCCAliases[i].alias == fourcc) { | 46 if (kFourCCAliases[i].alias == fourcc) { |
| 47 return kFourCCAliases[i].canonical; | 47 return kFourCCAliases[i].canonical; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 // Not an alias, so return it as-is. | 50 // Not an alias, so return it as-is. |
| 51 return fourcc; | 51 return fourcc; |
| 52 } | 52 } |
| 53 | 53 |
| 54 static 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 | |
| 62 static const int kNumScaleFactors = arraysize(kScaleFactors); | |
| 63 | |
| 64 // Finds the scale factor that, when applied to width and height, produces | |
| 65 // fewer than num_pixels. | |
| 66 static 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 | |
| 87 // Computes a scale less to fit in max_pixels while maintaining aspect ratio. | |
| 88 void ComputeScaleMaxPixels(int frame_width, int frame_height, int max_pixels, | |
| 89 int* scaled_width, int* scaled_height) { | |
| 90 ASSERT(scaled_width != NULL); | |
| 91 ASSERT(scaled_height != NULL); | |
| 92 ASSERT(max_pixels > 0); | |
| 93 const int kMaxWidth = 4096; | |
| 94 const int kMaxHeight = 3072; | |
| 95 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. | |
| 109 if (new_frame_width * new_frame_height > max_pixels) { | |
| 110 // 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>( | |
| 113 max_pixels) * new_frame_width / new_frame_height)); | |
| 114 new_frame_height = max_pixels / new_frame_width; | |
| 115 } | |
| 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 | |
| 123 // Compute a size to scale frames to that is below maximum compression | |
| 124 // and rendering size with the same aspect ratio. | |
| 125 void 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 | |
| 137 // Compute size to crop video frame to. | |
| 138 // If cropped_format_* is 0, return the frame_* size as is. | |
| 139 void ComputeCrop(int cropped_format_width, int cropped_format_height, | |
| 140 int frame_width, int frame_height, | |
| 141 int pixel_width, int pixel_height, | |
| 142 int rotation, | |
| 143 int* cropped_width, int* cropped_height) { | |
| 144 // Transform screen crop to camera space if rotated. | |
| 145 if (rotation == 90 || rotation == 270) { | |
| 146 std::swap(cropped_format_width, cropped_format_height); | |
| 147 } | |
| 148 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); | |
| 171 // kAspectThresh is the maximum aspect ratio difference that we'll accept | |
| 172 // for cropping. The value 1.34 allows cropping from 4:3 to 16:9. | |
| 173 // Set to zero to disable cropping entirely. | |
| 174 // TODO(fbarchard): crop to multiple of 16 width for better performance. | |
| 175 const float kAspectThresh = 1.34f; | |
| 176 // 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 | |
| 181 // that avoids rounding errors. | |
| 182 frame_width = static_cast<int>((crop_aspect * frame_height * | |
| 183 pixel_height) / pixel_width + 0.5f) & ~3; | |
| 184 } else if (frame_aspect < crop_aspect && | |
| 185 frame_aspect > crop_aspect / kAspectThresh) { | |
| 186 frame_height = static_cast<int>((frame_width * pixel_width) / | |
| 187 (crop_aspect * pixel_height) + 0.5f) & ~1; | |
| 188 } | |
| 189 *cropped_width = frame_width; | |
| 190 *cropped_height = frame_height; | |
| 191 } | |
| 192 | |
| 193 // Compute the frame size that makes pixels square pixel aspect ratio. | |
| 194 void 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. | |
| 198 *scaled_height = in_height * pixel_height / pixel_width; | |
| 199 } | |
| 200 | |
| 201 // The C++ standard requires a namespace-scope definition of static const | 54 // The C++ standard requires a namespace-scope definition of static const |
| 202 // integral types even when they are initialized in the declaration (see | 55 // 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 | 56 // [class.static.data]/4), but MSVC with /Ze is non-conforming and treats that |
| 204 // as a multiply defined symbol error. See Also: | 57 // as a multiply defined symbol error. See Also: |
| 205 // http://msdn.microsoft.com/en-us/library/34h23df8.aspx | 58 // http://msdn.microsoft.com/en-us/library/34h23df8.aspx |
| 206 #ifndef _MSC_EXTENSIONS | 59 #ifndef _MSC_EXTENSIONS |
| 207 const int64_t VideoFormat::kMinimumInterval; // Initialized in header. | 60 const int64_t VideoFormat::kMinimumInterval; // Initialized in header. |
| 208 #endif | 61 #endif |
| 209 | 62 |
| 210 std::string VideoFormat::ToString() const { | 63 std::string VideoFormat::ToString() const { |
| 211 std::string fourcc_name = GetFourccName(fourcc) + " "; | 64 std::string fourcc_name = GetFourccName(fourcc) + " "; |
| 212 for (std::string::const_iterator i = fourcc_name.begin(); | 65 for (std::string::const_iterator i = fourcc_name.begin(); |
| 213 i < fourcc_name.end(); ++i) { | 66 i < fourcc_name.end(); ++i) { |
| 214 // Test character is printable; Avoid isprint() which asserts on negatives. | 67 // Test character is printable; Avoid isprint() which asserts on negatives. |
| 215 if (*i < 32 || *i >= 127) { | 68 if (*i < 32 || *i >= 127) { |
| 216 fourcc_name = ""; | 69 fourcc_name = ""; |
| 217 break; | 70 break; |
| 218 } | 71 } |
| 219 } | 72 } |
| 220 | 73 |
| 221 std::ostringstream ss; | 74 std::ostringstream ss; |
| 222 ss << fourcc_name << width << "x" << height << "x" | 75 ss << fourcc_name << width << "x" << height << "x" |
| 223 << IntervalToFpsFloat(interval); | 76 << IntervalToFpsFloat(interval); |
| 224 return ss.str(); | 77 return ss.str(); |
| 225 } | 78 } |
| 226 | 79 |
| 227 } // namespace cricket | 80 } // namespace cricket |
| OLD | NEW |