Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: media/base/video_util.cc

Issue 1913503002: Memory copy the VideoFrame to match the requirement for HW encoders. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments and rebased. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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()) {
29 if (!frame_size.IsEmpty())
30 memset(frame, 0, frame_size.GetArea());
31 return;
32 }
33
34 const int stride = frame_size.width();
35 if (visible_size.width() < stride) {
36 const int pad_length = stride - visible_size.width();
37 uint8_t* dst = frame + visible_size.width();
38 for (int i = 0; i < visible_size.height(); ++i, dst += stride)
39 std::memset(dst, *(dst - 1), pad_length);
40 }
41
42 if (visible_size.height() < frame_size.height()) {
43 uint8_t* dst = frame + visible_size.height() * stride;
44 uint8_t* src = dst - stride;
45 for (int i = visible_size.height(); i < frame_size.height();
46 ++i, dst += stride)
47 std::memcpy(dst, src, stride);
48 }
49 }
50
22 } // namespace 51 } // namespace
23 52
24 gfx::Size GetNaturalSize(const gfx::Size& visible_size, 53 gfx::Size GetNaturalSize(const gfx::Size& visible_size,
25 int aspect_ratio_numerator, 54 int aspect_ratio_numerator,
26 int aspect_ratio_denominator) { 55 int aspect_ratio_denominator) {
27 if (aspect_ratio_denominator == 0 || 56 if (aspect_ratio_denominator == 0 ||
28 aspect_ratio_numerator < 0 || 57 aspect_ratio_numerator < 0 ||
29 aspect_ratio_denominator < 0) 58 aspect_ratio_denominator < 0)
30 return gfx::Size(); 59 return gfx::Size();
31 60
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 media::VideoFrame::WrapVideoFrame(frame, PIXEL_FORMAT_I420, 361 media::VideoFrame::WrapVideoFrame(frame, PIXEL_FORMAT_I420,
333 frame->visible_rect(), 362 frame->visible_rect(),
334 frame->natural_size()); 363 frame->natural_size());
335 if (!wrapped_frame) 364 if (!wrapped_frame)
336 return nullptr; 365 return nullptr;
337 wrapped_frame->AddDestructionObserver( 366 wrapped_frame->AddDestructionObserver(
338 base::Bind(&ReleaseOriginalFrame, frame)); 367 base::Bind(&ReleaseOriginalFrame, frame));
339 return wrapped_frame; 368 return wrapped_frame;
340 } 369 }
341 370
371 bool I420CopyWithPadding(const VideoFrame& src_frame, VideoFrame* dst_frame) {
372 if (!dst_frame || !dst_frame->IsMappable())
373 return false;
374
375 DCHECK_GE(dst_frame->coded_size().width(), src_frame.visible_rect().width());
376 DCHECK_GE(dst_frame->coded_size().height(),
377 src_frame.visible_rect().height());
378 DCHECK(dst_frame->visible_rect().origin().IsOrigin());
379
380 if (libyuv::I420Copy(src_frame.visible_data(media::VideoFrame::kYPlane),
381 src_frame.stride(media::VideoFrame::kYPlane),
382 src_frame.visible_data(media::VideoFrame::kUPlane),
383 src_frame.stride(media::VideoFrame::kUPlane),
384 src_frame.visible_data(media::VideoFrame::kVPlane),
385 src_frame.stride(media::VideoFrame::kVPlane),
386 dst_frame->data(media::VideoFrame::kYPlane),
387 dst_frame->stride(media::VideoFrame::kYPlane),
388 dst_frame->data(media::VideoFrame::kUPlane),
389 dst_frame->stride(media::VideoFrame::kUPlane),
390 dst_frame->data(media::VideoFrame::kVPlane),
391 dst_frame->stride(media::VideoFrame::kVPlane),
392 src_frame.visible_rect().width(),
393 src_frame.visible_rect().height()))
394 return false;
395
396 // Padding the region outside the visible rect with the repeated last
397 // column / row of the visible rect. This can improve the coding efficiency.
398 FillRegionOutsideVisibleRect(dst_frame->data(media::VideoFrame::kYPlane),
399 dst_frame->coded_size(),
400 src_frame.visible_rect().size());
401 FillRegionOutsideVisibleRect(
402 dst_frame->data(media::VideoFrame::kUPlane),
403 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kUPlane,
404 dst_frame->coded_size()),
405 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kUPlane,
406 src_frame.visible_rect().size()));
407 FillRegionOutsideVisibleRect(
408 dst_frame->data(media::VideoFrame::kVPlane),
409 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kVPlane,
410 dst_frame->coded_size()),
411 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, media::VideoFrame::kVPlane,
412 src_frame.visible_rect().size()));
413 return true;
414 }
415
342 } // namespace media 416 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698