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 with the repeated | |
24 // last column / row of the visible rect. | |
25 void FillRegionOutsideVisibleRect(uint8_t* frame, | |
26 const gfx::Size& frame_size, | |
27 const gfx::Size& visible_size) { | |
28 if (visible_size.IsEmpty()) | |
miu
2016/05/03 19:05:43
This just occurred to me: If coded_size is non-emp
xjz
2016/05/03 23:30:15
Done.
| |
29 return; | |
30 | |
31 const int stride = frame_size.width(); | |
32 if (visible_size.width() < stride) { | |
33 const int pad_length = stride - visible_size.width(); | |
34 uint8_t* dst = frame + visible_size.width(); | |
35 for (int i = 0; i < visible_size.height(); ++i, dst += stride) | |
36 std::memset(dst, *(dst - 1), pad_length); | |
37 } | |
38 | |
39 if (visible_size.height() < frame_size.height()) { | |
40 uint8_t* dst = frame + visible_size.height() * stride; | |
41 uint8_t* src = dst - stride; | |
42 for (int i = visible_size.height(); i < frame_size.height(); | |
43 ++i, dst += stride) | |
44 std::memcpy(dst, src, stride); | |
45 } | |
46 } | |
47 | |
22 } // namespace | 48 } // namespace |
23 | 49 |
24 gfx::Size GetNaturalSize(const gfx::Size& visible_size, | 50 gfx::Size GetNaturalSize(const gfx::Size& visible_size, |
25 int aspect_ratio_numerator, | 51 int aspect_ratio_numerator, |
26 int aspect_ratio_denominator) { | 52 int aspect_ratio_denominator) { |
27 if (aspect_ratio_denominator == 0 || | 53 if (aspect_ratio_denominator == 0 || |
28 aspect_ratio_numerator < 0 || | 54 aspect_ratio_numerator < 0 || |
29 aspect_ratio_denominator < 0) | 55 aspect_ratio_denominator < 0) |
30 return gfx::Size(); | 56 return gfx::Size(); |
31 | 57 |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 media::VideoFrame::WrapVideoFrame(frame, PIXEL_FORMAT_I420, | 358 media::VideoFrame::WrapVideoFrame(frame, PIXEL_FORMAT_I420, |
333 frame->visible_rect(), | 359 frame->visible_rect(), |
334 frame->natural_size()); | 360 frame->natural_size()); |
335 if (!wrapped_frame) | 361 if (!wrapped_frame) |
336 return nullptr; | 362 return nullptr; |
337 wrapped_frame->AddDestructionObserver( | 363 wrapped_frame->AddDestructionObserver( |
338 base::Bind(&ReleaseOriginalFrame, frame)); | 364 base::Bind(&ReleaseOriginalFrame, frame)); |
339 return wrapped_frame; | 365 return wrapped_frame; |
340 } | 366 } |
341 | 367 |
368 bool I420CopyWithPadding(const VideoFrame& src_frame, VideoFrame* dst_frame) { | |
369 if (!dst_frame) | |
miu
2016/05/03 19:05:43
Need to check for non-memory-backed VideoFrames:
xjz
2016/05/03 23:30:15
Done.
| |
370 return false; | |
371 | |
372 DCHECK_GE(dst_frame->coded_size().width(), src_frame.visible_rect().width()); | |
373 DCHECK_GE(dst_frame->coded_size().height(), | |
374 src_frame.visible_rect().height()); | |
375 DCHECK(dst_frame->visible_rect().origin().IsOrigin()); | |
376 | |
377 if (libyuv::I420Copy(src_frame.visible_data(media::VideoFrame::kYPlane), | |
378 src_frame.stride(media::VideoFrame::kYPlane), | |
379 src_frame.visible_data(media::VideoFrame::kUPlane), | |
380 src_frame.stride(media::VideoFrame::kUPlane), | |
381 src_frame.visible_data(media::VideoFrame::kVPlane), | |
382 src_frame.stride(media::VideoFrame::kVPlane), | |
383 dst_frame->data(media::VideoFrame::kYPlane), | |
384 dst_frame->stride(media::VideoFrame::kYPlane), | |
385 dst_frame->data(media::VideoFrame::kUPlane), | |
386 dst_frame->stride(media::VideoFrame::kUPlane), | |
387 dst_frame->data(media::VideoFrame::kVPlane), | |
388 dst_frame->stride(media::VideoFrame::kVPlane), | |
389 src_frame.visible_rect().width(), | |
390 src_frame.visible_rect().height())) | |
391 return false; | |
392 | |
393 // Padding the region outside the visible rect with the repeated last | |
394 // column / row of the visible rect. This can improve the coding efficiency. | |
395 FillRegionOutsideVisibleRect(dst_frame->data(media::VideoFrame::kYPlane), | |
396 dst_frame->coded_size(), | |
397 src_frame.visible_rect().size()); | |
398 FillRegionOutsideVisibleRect( | |
399 dst_frame->data(media::VideoFrame::kUPlane), | |
400 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kUPlane, | |
401 dst_frame->coded_size()), | |
402 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kUPlane, | |
403 src_frame.visible_rect().size())); | |
404 FillRegionOutsideVisibleRect( | |
405 dst_frame->data(media::VideoFrame::kVPlane), | |
406 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kVPlane, | |
407 dst_frame->coded_size()), | |
408 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kVPlane, | |
409 src_frame.visible_rect().size())); | |
410 return true; | |
411 } | |
412 | |
342 } // namespace media | 413 } // namespace media |
OLD | NEW |