henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | // libjingle |
| 2 | // Copyright 2010 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 | #include "talk/media/base/videocommon.h" |
| 27 | |
| 28 | #include <limits.h> // For INT_MAX |
| 29 | #include <math.h> |
| 30 | #include <sstream> |
| 31 | |
| 32 | #include "talk/base/common.h" |
| 33 | |
| 34 | namespace cricket { |
| 35 | |
| 36 | struct FourCCAliasEntry { |
| 37 | uint32 alias; |
| 38 | uint32 canonical; |
| 39 | }; |
| 40 | |
| 41 | static const FourCCAliasEntry kFourCCAliases[] = { |
| 42 | {FOURCC_IYUV, FOURCC_I420}, |
| 43 | {FOURCC_YU16, FOURCC_I422}, |
| 44 | {FOURCC_YU24, FOURCC_I444}, |
| 45 | {FOURCC_YUYV, FOURCC_YUY2}, |
| 46 | {FOURCC_YUVS, FOURCC_YUY2}, |
| 47 | {FOURCC_HDYC, FOURCC_UYVY}, |
| 48 | {FOURCC_2VUY, FOURCC_UYVY}, |
| 49 | {FOURCC_JPEG, FOURCC_MJPG}, // Note: JPEG has DHT while MJPG does not. |
| 50 | {FOURCC_DMB1, FOURCC_MJPG}, |
| 51 | {FOURCC_BA81, FOURCC_BGGR}, |
| 52 | {FOURCC_RGB3, FOURCC_RAW}, |
| 53 | {FOURCC_BGR3, FOURCC_24BG}, |
| 54 | {FOURCC_CM32, FOURCC_BGRA}, |
| 55 | {FOURCC_CM24, FOURCC_RAW}, |
| 56 | }; |
| 57 | |
| 58 | uint32 CanonicalFourCC(uint32 fourcc) { |
| 59 | for (int i = 0; i < ARRAY_SIZE(kFourCCAliases); ++i) { |
| 60 | if (kFourCCAliases[i].alias == fourcc) { |
| 61 | return kFourCCAliases[i].canonical; |
| 62 | } |
| 63 | } |
| 64 | // Not an alias, so return it as-is. |
| 65 | return fourcc; |
| 66 | } |
| 67 | |
| 68 | static float kScaleFactors[] = { |
| 69 | 1.f / 1.f, // Full size. |
| 70 | 1.f / 2.f, // 1/2 scale. |
| 71 | 1.f / 4.f, // 1/4 scale. |
| 72 | 1.f / 8.f, // 1/8 scale. |
| 73 | 1.f / 16.f // 1/16 scale. |
| 74 | }; |
| 75 | |
| 76 | static const int kNumScaleFactors = ARRAY_SIZE(kScaleFactors); |
| 77 | |
| 78 | // Finds the scale factor that, when applied to width and height, produces |
| 79 | // fewer than num_pixels. |
| 80 | static float FindLowerScale(int width, int height, int target_num_pixels) { |
| 81 | if (!target_num_pixels) { |
| 82 | return 0.f; |
| 83 | } |
| 84 | int best_distance = INT_MAX; |
| 85 | int best_index = kNumScaleFactors - 1; // Default to max scale. |
| 86 | for (int i = 0; i < kNumScaleFactors; ++i) { |
| 87 | int test_num_pixels = static_cast<int>(width * kScaleFactors[i] * |
| 88 | height * kScaleFactors[i]); |
| 89 | int diff = target_num_pixels - test_num_pixels; |
| 90 | if (diff >= 0 && diff < best_distance) { |
| 91 | best_distance = diff; |
| 92 | best_index = i; |
| 93 | if (best_distance == 0) { // Found exact match. |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return kScaleFactors[best_index]; |
| 99 | } |
| 100 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame^] | 101 | // Computes a scale less to fit in max_pixels while maintaining aspect ratio. |
| 102 | void ComputeScaleMaxPixels(int frame_width, int frame_height, int max_pixels, |
| 103 | int* scaled_width, int* scaled_height) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | ASSERT(scaled_width != NULL); |
| 105 | ASSERT(scaled_height != NULL); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame^] | 106 | ASSERT(max_pixels > 0); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | // For VP8 the values for max width and height can be found here |
| 108 | // webrtc/src/video_engine/vie_defines.h (kViEMaxCodecWidth and |
| 109 | // kViEMaxCodecHeight) |
| 110 | const int kMaxWidth = 4096; |
| 111 | const int kMaxHeight = 3072; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | int new_frame_width = frame_width; |
| 113 | int new_frame_height = frame_height; |
| 114 | |
| 115 | // Limit width. |
| 116 | if (new_frame_width > kMaxWidth) { |
| 117 | new_frame_height = new_frame_height * kMaxWidth / new_frame_width; |
| 118 | new_frame_width = kMaxWidth; |
| 119 | } |
| 120 | // Limit height. |
| 121 | if (new_frame_height > kMaxHeight) { |
| 122 | new_frame_width = new_frame_width * kMaxHeight / new_frame_height; |
| 123 | new_frame_height = kMaxHeight; |
| 124 | } |
| 125 | // Limit number of pixels. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame^] | 126 | if (new_frame_width * new_frame_height > max_pixels) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | // Compute new width such that width * height is less than maximum but |
| 128 | // maintains original captured frame aspect ratio. |
| 129 | new_frame_width = static_cast<int>(sqrtf(static_cast<float>( |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame^] | 130 | max_pixels) * new_frame_width / new_frame_height)); |
| 131 | new_frame_height = max_pixels / new_frame_width; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | } |
| 133 | // Snap to a scale factor that is less than or equal to target pixels. |
| 134 | float scale = FindLowerScale(frame_width, frame_height, |
| 135 | new_frame_width * new_frame_height); |
| 136 | *scaled_width = static_cast<int>(frame_width * scale + .5f); |
| 137 | *scaled_height = static_cast<int>(frame_height * scale + .5f); |
| 138 | } |
| 139 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame^] | 140 | // Compute a size to scale frames to that is below maximum compression |
| 141 | // and rendering size with the same aspect ratio. |
| 142 | void ComputeScale(int frame_width, int frame_height, int fps, |
| 143 | int* scaled_width, int* scaled_height) { |
| 144 | // Maximum pixels limit is set to Retina MacBookPro 15" resolution of |
| 145 | // 2880 x 1800 as of 4/18/2013. |
| 146 | // For high fps, maximum pixels limit is set based on common 24" monitor |
| 147 | // resolution of 2048 x 1280 as of 6/13/2013. The Retina resolution is |
| 148 | // therefore reduced to 1440 x 900. |
| 149 | int max_pixels = (fps > 5) ? 2048 * 1280 : 2880 * 1800; |
| 150 | ComputeScaleMaxPixels( |
| 151 | frame_width, frame_height, max_pixels, scaled_width, scaled_height); |
| 152 | } |
| 153 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | // Compute size to crop video frame to. |
| 155 | // If cropped_format_* is 0, return the frame_* size as is. |
| 156 | void ComputeCrop(int cropped_format_width, |
| 157 | int cropped_format_height, |
| 158 | int frame_width, int frame_height, |
| 159 | int pixel_width, int pixel_height, |
| 160 | int rotation, |
| 161 | int* cropped_width, int* cropped_height) { |
| 162 | ASSERT(cropped_format_width >= 0); |
| 163 | ASSERT(cropped_format_height >= 0); |
| 164 | ASSERT(frame_width > 0); |
| 165 | ASSERT(frame_height > 0); |
| 166 | ASSERT(pixel_width >= 0); |
| 167 | ASSERT(pixel_height >= 0); |
| 168 | ASSERT(rotation == 0 || rotation == 90 || rotation == 180 || rotation == 270); |
| 169 | ASSERT(cropped_width != NULL); |
| 170 | ASSERT(cropped_height != NULL); |
| 171 | if (!pixel_width) { |
| 172 | pixel_width = 1; |
| 173 | } |
| 174 | if (!pixel_height) { |
| 175 | pixel_height = 1; |
| 176 | } |
| 177 | // if cropped_format is 0x0 disable cropping. |
| 178 | if (!cropped_format_height) { |
| 179 | cropped_format_height = 1; |
| 180 | } |
| 181 | float frame_aspect = static_cast<float>(frame_width * pixel_width) / |
| 182 | static_cast<float>(frame_height * pixel_height); |
| 183 | float crop_aspect = static_cast<float>(cropped_format_width) / |
| 184 | static_cast<float>(cropped_format_height); |
| 185 | int new_frame_width = frame_width; |
| 186 | int new_frame_height = frame_height; |
| 187 | if (rotation == 90 || rotation == 270) { |
| 188 | frame_aspect = 1.0f / frame_aspect; |
| 189 | new_frame_width = frame_height; |
| 190 | new_frame_height = frame_width; |
| 191 | } |
| 192 | |
| 193 | // kAspectThresh is the maximum aspect ratio difference that we'll accept |
| 194 | // for cropping. The value 1.33 is based on 4:3 being cropped to 16:9. |
| 195 | // Set to zero to disable cropping entirely. |
| 196 | // TODO(fbarchard): crop to multiple of 16 width for better performance. |
| 197 | const float kAspectThresh = 16.f / 9.f / (4.f / 3.f) + 0.01f; // 1.33 |
| 198 | // Wide aspect - crop horizontally |
| 199 | if (frame_aspect > crop_aspect && |
| 200 | frame_aspect < crop_aspect * kAspectThresh) { |
| 201 | // Round width down to multiple of 4 to avoid odd chroma width. |
| 202 | // Width a multiple of 4 allows a half size image to have chroma channel |
| 203 | // that avoids rounding errors. lmi and webrtc have odd width limitations. |
| 204 | new_frame_width = static_cast<int>((crop_aspect * frame_height * |
| 205 | pixel_height) / pixel_width + 0.5f) & ~3; |
| 206 | } else if (crop_aspect > frame_aspect && |
| 207 | crop_aspect < frame_aspect * kAspectThresh) { |
| 208 | new_frame_height = static_cast<int>((frame_width * pixel_width) / |
| 209 | (crop_aspect * pixel_height) + 0.5f) & ~1; |
| 210 | } |
| 211 | |
| 212 | *cropped_width = new_frame_width; |
| 213 | *cropped_height = new_frame_height; |
| 214 | if (rotation == 90 || rotation == 270) { |
| 215 | *cropped_width = new_frame_height; |
| 216 | *cropped_height = new_frame_width; |
| 217 | } |
| 218 | } |
| 219 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame^] | 220 | // Compute the frame size that makes pixels square pixel aspect ratio. |
| 221 | void ComputeScaleToSquarePixels(int in_width, int in_height, |
| 222 | int pixel_width, int pixel_height, |
| 223 | int* scaled_width, int* scaled_height) { |
| 224 | *scaled_width = in_width; // Keep width the same. |
| 225 | *scaled_height = in_height * pixel_width / pixel_height; |
| 226 | } |
| 227 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 228 | // The C++ standard requires a namespace-scope definition of static const |
| 229 | // integral types even when they are initialized in the declaration (see |
| 230 | // [class.static.data]/4), but MSVC with /Ze is non-conforming and treats that |
| 231 | // as a multiply defined symbol error. See Also: |
| 232 | // http://msdn.microsoft.com/en-us/library/34h23df8.aspx |
| 233 | #ifndef _MSC_EXTENSIONS |
| 234 | const int64 VideoFormat::kMinimumInterval; // Initialized in header. |
| 235 | #endif |
| 236 | |
| 237 | std::string VideoFormat::ToString() const { |
| 238 | std::string fourcc_name = GetFourccName(fourcc) + " "; |
| 239 | for (std::string::const_iterator i = fourcc_name.begin(); |
| 240 | i < fourcc_name.end(); ++i) { |
| 241 | // Test character is printable; Avoid isprint() which asserts on negatives. |
| 242 | if (*i < 32 || *i >= 127) { |
| 243 | fourcc_name = ""; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | std::ostringstream ss; |
| 249 | ss << fourcc_name << width << "x" << height << "x" << IntervalToFps(interval); |
| 250 | return ss.str(); |
| 251 | } |
| 252 | |
| 253 | } // namespace cricket |