videocommon.cc revision 5f93d0a140515e3b8cdd1b9a4c6f5871144e5dee
1/*
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 */
27
28#include "talk/media/base/videocommon.h"
29
30#include <limits.h>  // For INT_MAX
31#include <math.h>
32#include <sstream>
33
34#include "webrtc/base/common.h"
35
36namespace cricket {
37
38struct FourCCAliasEntry {
39  uint32 alias;
40  uint32 canonical;
41};
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
60uint32 CanonicalFourCC(uint32 fourcc) {
61  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
103// 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) {
106  ASSERT(scaled_width != NULL);
107  ASSERT(scaled_height != NULL);
108  ASSERT(max_pixels > 0);
109  // 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;
114  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.
128  if (new_frame_width * new_frame_height > max_pixels) {
129    // 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>(
132        max_pixels) * new_frame_width / new_frame_height));
133    new_frame_height = max_pixels / new_frame_width;
134  }
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
142// 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
156// Compute size to crop video frame to.
157// If cropped_format_* is 0, return the frame_* size as is.
158void ComputeCrop(int cropped_format_width, int cropped_format_height,
159                 int frame_width, int frame_height,
160                 int pixel_width, int pixel_height,
161                 int rotation,
162                 int* cropped_width, int* cropped_height) {
163  // Transform screen crop to camera space if rotated.
164  if (rotation == 90 || rotation == 270) {
165    std::swap(cropped_format_width, cropped_format_height);
166  }
167  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);
190  // kAspectThresh is the maximum aspect ratio difference that we'll accept
191  // for cropping.  The value 1.34 allows cropping from 4:3 to 16:9.
192  // Set to zero to disable cropping entirely.
193  // TODO(fbarchard): crop to multiple of 16 width for better performance.
194  const float kAspectThresh = 1.34f;
195  // 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
200    // that avoids rounding errors.
201    frame_width = static_cast<int>((crop_aspect * frame_height *
202        pixel_height) / pixel_width + 0.5f) & ~3;
203  } else if (frame_aspect < crop_aspect &&
204             frame_aspect > crop_aspect / kAspectThresh) {
205    frame_height = static_cast<int>((frame_width * pixel_width) /
206        (crop_aspect * pixel_height) + 0.5f) & ~1;
207  }
208  *cropped_width = frame_width;
209  *cropped_height = frame_height;
210}
211
212// 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.
217  *scaled_height = in_height * pixel_height / pixel_width;
218}
219
220// 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
226const int64 VideoFormat::kMinimumInterval;  // Initialized in header.
227#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;
241  ss << fourcc_name << width << "x" << height << "x"
242     << IntervalToFpsFloat(interval);
243  return ss.str();
244}
245
246}  // namespace cricket
247