Index: media/base/video_util.cc |
diff --git a/media/base/video_util.cc b/media/base/video_util.cc |
index d7946743eae091cef3f9952c4a0d6cb5acd4ab40..e04a5a3c5439538f2f9f7d91578b8fea63aa86bd 100644 |
--- a/media/base/video_util.cc |
+++ b/media/base/video_util.cc |
@@ -270,6 +270,23 @@ void RotatePlaneByPixels( |
} |
} |
+// Common logic for the letterboxing and scale-within/scale-encompassing |
+// functions. Scales |size| to either fit within or encompass |target|, |
+// depending on whether |fit_within_target| is true. |
+static gfx::Size ScaleSizeToTarget(const gfx::Size& size, |
+ const gfx::Size& target, |
+ bool fit_within_target) { |
+ if (size.IsEmpty()) |
+ return gfx::Size(); // Corner case: Aspect ratio is undefined. |
+ |
+ const int64 x = static_cast<int64>(size.width()) * target.height(); |
+ const int64 y = static_cast<int64>(size.height()) * target.width(); |
+ const bool use_target_width = fit_within_target ? (y < x) : (x < y); |
+ return use_target_width ? |
+ gfx::Size(target.width(), static_cast<int>(y / size.width())) : |
+ gfx::Size(static_cast<int>(x / size.height()), target.height()); |
+} |
+ |
gfx::Rect ComputeLetterboxRegion(const gfx::Rect& bounds, |
const gfx::Size& content) { |
// If |content| has an undefined aspect ratio, let's not try to divide by |
@@ -277,19 +294,33 @@ gfx::Rect ComputeLetterboxRegion(const gfx::Rect& bounds, |
if (content.IsEmpty()) |
return gfx::Rect(); |
- int64 x = static_cast<int64>(content.width()) * bounds.height(); |
- int64 y = static_cast<int64>(content.height()) * bounds.width(); |
- |
- gfx::Size letterbox(bounds.width(), bounds.height()); |
- if (y < x) |
- letterbox.set_height(static_cast<int>(y / content.width())); |
- else |
- letterbox.set_width(static_cast<int>(x / content.height())); |
gfx::Rect result = bounds; |
- result.ClampToCenteredSize(letterbox); |
+ result.ClampToCenteredSize(ScaleSizeToTarget(content, bounds.size(), true)); |
return result; |
} |
+gfx::Size ScaleSizeToFitWithinTarget(const gfx::Size& size, |
+ const gfx::Size& target) { |
+ return ScaleSizeToTarget(size, target, true); |
+} |
+ |
+gfx::Size ScaleSizeToEncompassTarget(const gfx::Size& size, |
+ const gfx::Size& target) { |
+ return ScaleSizeToTarget(size, target, false); |
+} |
+ |
+gfx::Size PadToMatchAspectRatio(const gfx::Size& size, |
+ const gfx::Size& target) { |
+ if (target.IsEmpty()) |
+ return gfx::Size(); // Aspect ratio is undefined. |
+ |
+ const int64 x = static_cast<int64>(size.width()) * target.height(); |
+ const int64 y = static_cast<int64>(size.height()) * target.width(); |
+ if (x < y) |
+ return gfx::Size(static_cast<int>(y / target.height()), size.height()); |
+ return gfx::Size(size.width(), static_cast<int>(x / target.width())); |
+} |
+ |
void CopyRGBToVideoFrame(const uint8* source, |
int stride, |
const gfx::Rect& region_in_frame, |