Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/video_util.h" | 5 #include "media/base/video_util.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 uint8* dest_ptr = dest; | 263 uint8* dest_ptr = dest; |
| 264 for (int col = 0; col < num_cols; ++col) { | 264 for (int col = 0; col < num_cols; ++col) { |
| 265 *dest_ptr = *src_ptr++; | 265 *dest_ptr = *src_ptr++; |
| 266 dest_ptr += dest_col_step; | 266 dest_ptr += dest_col_step; |
| 267 } | 267 } |
| 268 src += src_stride; | 268 src += src_stride; |
| 269 dest += dest_row_step; | 269 dest += dest_row_step; |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 | 272 |
| 273 // Common logic for the letterboxing and scale-within/scale-encompassing | |
| 274 // functions. Scales |size| to either fit within or encompass |target|, | |
| 275 // depending on whether |fit_within_target| is true. | |
| 276 template<bool fit_within_target> | |
|
DaleCurtis
2015/05/09 01:50:21
Seems like this is just a static function with an
miu
2015/05/09 03:52:56
Done.
| |
| 277 static gfx::Size ScaleSizeToTarget(const gfx::Size& size, | |
| 278 const gfx::Size& target) { | |
| 279 if (size.IsEmpty()) | |
| 280 return gfx::Size(); // Corner case: Aspect ratio is undefined. | |
| 281 | |
| 282 // Scale |size|, preserving aspect ratio. | |
| 283 const int64 x = static_cast<int64>(size.width()) * target.height(); | |
| 284 const int64 y = static_cast<int64>(size.height()) * target.width(); | |
| 285 const bool use_target_width = fit_within_target ? (y < x) : (x < y); | |
| 286 return use_target_width ? | |
| 287 gfx::Size(target.width(), static_cast<int>(y / size.width())) : | |
| 288 gfx::Size(static_cast<int>(x / size.height()), target.height()); | |
| 289 } | |
| 290 | |
| 273 gfx::Rect ComputeLetterboxRegion(const gfx::Rect& bounds, | 291 gfx::Rect ComputeLetterboxRegion(const gfx::Rect& bounds, |
| 274 const gfx::Size& content) { | 292 const gfx::Size& content) { |
| 275 // If |content| has an undefined aspect ratio, let's not try to divide by | 293 // If |content| has an undefined aspect ratio, let's not try to divide by |
| 276 // zero. | 294 // zero. |
| 277 if (content.IsEmpty()) | 295 if (content.IsEmpty()) |
| 278 return gfx::Rect(); | 296 return gfx::Rect(); |
| 279 | 297 |
| 280 int64 x = static_cast<int64>(content.width()) * bounds.height(); | 298 gfx::Rect result = bounds; |
| 281 int64 y = static_cast<int64>(content.height()) * bounds.width(); | 299 result.ClampToCenteredSize(ScaleSizeToTarget<true>(content, bounds.size())); |
| 300 return result; | |
| 301 } | |
| 282 | 302 |
| 283 gfx::Size letterbox(bounds.width(), bounds.height()); | 303 gfx::Size ScaleSizeToFitWithinTarget(const gfx::Size& size, |
| 284 if (y < x) | 304 const gfx::Size& target) { |
| 285 letterbox.set_height(static_cast<int>(y / content.width())); | 305 return ScaleSizeToTarget<true>(size, target); |
| 306 } | |
| 307 | |
| 308 gfx::Size ScaleSizeToEncompassTarget(const gfx::Size& size, | |
| 309 const gfx::Size& target) { | |
| 310 return ScaleSizeToTarget<false>(size, target); | |
| 311 } | |
| 312 | |
| 313 gfx::Size PadToMatchAspectRatio(const gfx::Size& size, | |
| 314 const gfx::Size& target) { | |
| 315 if (target.IsEmpty()) | |
| 316 return gfx::Size(); // Aspect ratio is undefined. | |
| 317 | |
| 318 const int64 x = static_cast<int64>(size.width()) * target.height(); | |
| 319 const int64 y = static_cast<int64>(size.height()) * target.width(); | |
| 320 if (x < y) | |
| 321 return gfx::Size(static_cast<int>(y / target.height()), size.height()); | |
| 286 else | 322 else |
|
DaleCurtis
2015/05/09 01:50:21
Remove else?
miu
2015/05/09 03:52:56
Done.
| |
| 287 letterbox.set_width(static_cast<int>(x / content.height())); | 323 return gfx::Size(size.width(), static_cast<int>(x / target.width())); |
| 288 gfx::Rect result = bounds; | |
| 289 result.ClampToCenteredSize(letterbox); | |
| 290 return result; | |
| 291 } | 324 } |
| 292 | 325 |
| 293 void CopyRGBToVideoFrame(const uint8* source, | 326 void CopyRGBToVideoFrame(const uint8* source, |
| 294 int stride, | 327 int stride, |
| 295 const gfx::Rect& region_in_frame, | 328 const gfx::Rect& region_in_frame, |
| 296 VideoFrame* frame) { | 329 VideoFrame* frame) { |
| 297 const int kY = VideoFrame::kYPlane; | 330 const int kY = VideoFrame::kYPlane; |
| 298 const int kU = VideoFrame::kUPlane; | 331 const int kU = VideoFrame::kUPlane; |
| 299 const int kV = VideoFrame::kVPlane; | 332 const int kV = VideoFrame::kVPlane; |
| 300 CHECK_EQ(frame->stride(kU), frame->stride(kV)); | 333 CHECK_EQ(frame->stride(kU), frame->stride(kV)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 314 frame->data(kU) + uv_offset, | 347 frame->data(kU) + uv_offset, |
| 315 frame->data(kV) + uv_offset, | 348 frame->data(kV) + uv_offset, |
| 316 region_in_frame.width(), | 349 region_in_frame.width(), |
| 317 region_in_frame.height(), | 350 region_in_frame.height(), |
| 318 stride, | 351 stride, |
| 319 frame->stride(kY), | 352 frame->stride(kY), |
| 320 uv_stride); | 353 uv_stride); |
| 321 } | 354 } |
| 322 | 355 |
| 323 } // namespace media | 356 } // namespace media |
| OLD | NEW |