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 "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
11 #include "base/numerics/safe_math.h" | 11 #include "base/numerics/safe_math.h" |
12 #include "media/base/video_frame.h" | 12 #include "media/base/video_frame.h" |
13 #include "media/base/yuv_convert.h" | 13 #include "media/base/yuv_convert.h" |
| 14 #include "third_party/libyuv/include/libyuv.h" |
14 | 15 |
15 namespace media { | 16 namespace media { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Empty method used for keeping a reference to the original media::VideoFrame. | 20 // Empty method used for keeping a reference to the original media::VideoFrame. |
20 void ReleaseOriginalFrame(const scoped_refptr<media::VideoFrame>& frame) {} | 21 void ReleaseOriginalFrame(const scoped_refptr<media::VideoFrame>& frame) {} |
21 | 22 |
| 23 // Helper to apply padding to the region outside visible rect up to the coded |
| 24 // size with the repeated last column / row of the visible rect. |
| 25 void FillRegionOutsideVisibleRect(uint8_t* data, |
| 26 size_t stride, |
| 27 const gfx::Size& coded_size, |
| 28 const gfx::Size& visible_size) { |
| 29 if (visible_size.IsEmpty()) { |
| 30 if (!coded_size.IsEmpty()) |
| 31 memset(data, 0, coded_size.height() * stride); |
| 32 return; |
| 33 } |
| 34 |
| 35 const int coded_width = coded_size.width(); |
| 36 if (visible_size.width() < coded_width) { |
| 37 const int pad_length = coded_width - visible_size.width(); |
| 38 uint8_t* dst = data + visible_size.width(); |
| 39 for (int i = 0; i < visible_size.height(); ++i, dst += stride) |
| 40 std::memset(dst, *(dst - 1), pad_length); |
| 41 } |
| 42 |
| 43 if (visible_size.height() < coded_size.height()) { |
| 44 uint8_t* dst = data + visible_size.height() * stride; |
| 45 uint8_t* src = dst - stride; |
| 46 for (int i = visible_size.height(); i < coded_size.height(); |
| 47 ++i, dst += stride) |
| 48 std::memcpy(dst, src, coded_width); |
| 49 } |
| 50 } |
| 51 |
22 } // namespace | 52 } // namespace |
23 | 53 |
24 gfx::Size GetNaturalSize(const gfx::Size& visible_size, | 54 gfx::Size GetNaturalSize(const gfx::Size& visible_size, |
25 int aspect_ratio_numerator, | 55 int aspect_ratio_numerator, |
26 int aspect_ratio_denominator) { | 56 int aspect_ratio_denominator) { |
27 if (aspect_ratio_denominator == 0 || | 57 if (aspect_ratio_denominator == 0 || |
28 aspect_ratio_numerator < 0 || | 58 aspect_ratio_numerator < 0 || |
29 aspect_ratio_denominator < 0) | 59 aspect_ratio_denominator < 0) |
30 return gfx::Size(); | 60 return gfx::Size(); |
31 | 61 |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 media::VideoFrame::WrapVideoFrame(frame, PIXEL_FORMAT_I420, | 362 media::VideoFrame::WrapVideoFrame(frame, PIXEL_FORMAT_I420, |
333 frame->visible_rect(), | 363 frame->visible_rect(), |
334 frame->natural_size()); | 364 frame->natural_size()); |
335 if (!wrapped_frame) | 365 if (!wrapped_frame) |
336 return nullptr; | 366 return nullptr; |
337 wrapped_frame->AddDestructionObserver( | 367 wrapped_frame->AddDestructionObserver( |
338 base::Bind(&ReleaseOriginalFrame, frame)); | 368 base::Bind(&ReleaseOriginalFrame, frame)); |
339 return wrapped_frame; | 369 return wrapped_frame; |
340 } | 370 } |
341 | 371 |
| 372 bool I420CopyWithPadding(const VideoFrame& src_frame, VideoFrame* dst_frame) { |
| 373 if (!dst_frame || !dst_frame->IsMappable()) |
| 374 return false; |
| 375 |
| 376 DCHECK_GE(dst_frame->coded_size().width(), src_frame.visible_rect().width()); |
| 377 DCHECK_GE(dst_frame->coded_size().height(), |
| 378 src_frame.visible_rect().height()); |
| 379 DCHECK(dst_frame->visible_rect().origin().IsOrigin()); |
| 380 |
| 381 if (libyuv::I420Copy(src_frame.visible_data(VideoFrame::kYPlane), |
| 382 src_frame.stride(VideoFrame::kYPlane), |
| 383 src_frame.visible_data(VideoFrame::kUPlane), |
| 384 src_frame.stride(VideoFrame::kUPlane), |
| 385 src_frame.visible_data(VideoFrame::kVPlane), |
| 386 src_frame.stride(VideoFrame::kVPlane), |
| 387 dst_frame->data(VideoFrame::kYPlane), |
| 388 dst_frame->stride(VideoFrame::kYPlane), |
| 389 dst_frame->data(VideoFrame::kUPlane), |
| 390 dst_frame->stride(VideoFrame::kUPlane), |
| 391 dst_frame->data(VideoFrame::kVPlane), |
| 392 dst_frame->stride(VideoFrame::kVPlane), |
| 393 src_frame.visible_rect().width(), |
| 394 src_frame.visible_rect().height())) |
| 395 return false; |
| 396 |
| 397 // Padding the region outside the visible rect with the repeated last |
| 398 // column / row of the visible rect. This can improve the coding efficiency. |
| 399 FillRegionOutsideVisibleRect(dst_frame->data(VideoFrame::kYPlane), |
| 400 dst_frame->stride(VideoFrame::kYPlane), |
| 401 dst_frame->coded_size(), |
| 402 src_frame.visible_rect().size()); |
| 403 FillRegionOutsideVisibleRect( |
| 404 dst_frame->data(VideoFrame::kUPlane), |
| 405 dst_frame->stride(VideoFrame::kUPlane), |
| 406 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, VideoFrame::kUPlane, |
| 407 dst_frame->coded_size()), |
| 408 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, VideoFrame::kUPlane, |
| 409 src_frame.visible_rect().size())); |
| 410 FillRegionOutsideVisibleRect( |
| 411 dst_frame->data(VideoFrame::kVPlane), |
| 412 dst_frame->stride(VideoFrame::kVPlane), |
| 413 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, VideoFrame::kVPlane, |
| 414 dst_frame->coded_size()), |
| 415 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, VideoFrame::kVPlane, |
| 416 src_frame.visible_rect().size())); |
| 417 |
| 418 return true; |
| 419 } |
| 420 |
342 } // namespace media | 421 } // namespace media |
OLD | NEW |