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

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

Issue 1958913005: Memory copy the VideoFrame to match the requirement for HW encoders. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit tests to pass msan. 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
« no previous file with comments | « media/base/video_util.cc ('k') | media/cast/sender/external_video_encoder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "media/base/video_frame.h" 12 #include "media/base/video_frame.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace {
16
17 // Initialize a plane's visible rect with value circularly from 0 to 255.
18 void InitializePlane(uint8_t* data, int stride, const gfx::Size& visible_size) {
xhwang 2016/05/10 19:04:04 "Initialize" is too generic. How about something l
xjz 2016/05/10 21:08:44 Done.
19 if (!data || visible_size.width() > stride)
xhwang 2016/05/10 19:04:04 DCHECK here. Otherwise we could silently return.
xjz 2016/05/10 21:08:44 Done.
20 return;
21
22 uint32_t val = 0;
23 uint8_t* src = data;
24 for (int i = 0; i < visible_size.height(); ++i, src += stride) {
25 for (int j = 0; j < visible_size.width(); ++j, ++val)
26 src[j] = val & 0xff;
27 }
28 }
29
30 // Create a VideoFrame and initialize the visible rect using |InitializePlane|.
xhwang 2016/05/10 19:04:04 Could you add a comment why CreateColorFrame() won
xjz 2016/05/10 21:08:44 Done.
31 scoped_refptr<media::VideoFrame> CreateInitializedFrame(
xhwang 2016/05/10 19:04:04 ditto: "Initialized" seems too generic.
xjz 2016/05/10 21:08:44 Done.
32 media::VideoPixelFormat format,
33 const gfx::Size& coded_size,
34 const gfx::Rect& visible_rect,
35 const gfx::Size& natural_size,
36 base::TimeDelta timestamp) {
37 scoped_refptr<media::VideoFrame> frame(media::VideoFrame::CreateFrame(
38 format, coded_size, visible_rect, natural_size, timestamp));
xhwang 2016/05/10 19:04:04 nit: add an empty line here
xjz 2016/05/10 21:08:44 Done.
39 InitializePlane(frame->data(media::VideoFrame::kYPlane),
40 frame->stride(media::VideoFrame::kYPlane),
41 frame->visible_rect().size());
42 InitializePlane(
43 frame->data(media::VideoFrame::kUPlane),
44 frame->stride(media::VideoFrame::kUPlane),
45 media::VideoFrame::PlaneSize(format, media::VideoFrame::kUPlane,
46 frame->visible_rect().size()));
47 InitializePlane(
48 frame->data(media::VideoFrame::kVPlane),
49 frame->stride(media::VideoFrame::kVPlane),
50 media::VideoFrame::PlaneSize(format, media::VideoFrame::kVPlane,
51 frame->visible_rect().size()));
52 return frame;
53 }
54
55 // Helper function used to verify the data in the coded region after copying the
56 // visible region and padding the remaining area.
57 bool VerifyPlanCopyWithPadding(const uint8_t* src,
58 size_t src_stride,
59 // Size of visible region.
60 const gfx::Size& src_size,
61 const uint8_t* dst,
62 size_t dst_stride,
63 // Coded size of |dst|.
64 const gfx::Size& dst_size) {
65 if (!src || !dst)
66 return false;
67
68 const size_t src_width = src_size.width();
69 const size_t src_height = src_size.height();
70 const size_t dst_width = dst_size.width();
71 const size_t dst_height = dst_size.height();
72 if (src_width > dst_width || src_width > src_stride ||
73 src_height > dst_height || src_size.IsEmpty() || dst_size.IsEmpty())
74 return false;
75
76 const uint8_t *src_ptr = src, *dst_ptr = dst;
77 for (size_t i = 0; i < src_height;
78 ++i, src_ptr += src_stride, dst_ptr += dst_stride) {
79 if (memcmp(src_ptr, dst_ptr, src_width))
80 return false;
81 for (size_t j = src_width; j < dst_width; ++j) {
82 if (src_ptr[src_width - 1] != dst_ptr[j])
83 return false;
84 }
85 }
86 if (src_height < dst_height) {
87 src_ptr = dst + (src_height - 1) * dst_stride;
88 if (memcmp(src_ptr, dst_ptr, dst_width))
89 return false;
90 }
91 return true;
92 }
93
94 bool VerifyCopyWithPadding(const media::VideoFrame& src_frame,
95 const media::VideoFrame& dst_frame) {
96 if (!src_frame.IsMappable() || !dst_frame.IsMappable() ||
97 src_frame.visible_rect().size() != dst_frame.visible_rect().size())
98 return false;
99
100 if (!VerifyPlanCopyWithPadding(
101 src_frame.visible_data(media::VideoFrame::kYPlane),
102 src_frame.stride(media::VideoFrame::kYPlane),
103 src_frame.visible_rect().size(),
104 dst_frame.data(media::VideoFrame::kYPlane),
105 dst_frame.stride(media::VideoFrame::kYPlane), dst_frame.coded_size()))
106 return false;
107 if (!VerifyPlanCopyWithPadding(
108 src_frame.visible_data(media::VideoFrame::kUPlane),
109 src_frame.stride(media::VideoFrame::kUPlane),
110 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
111 media::VideoFrame::kUPlane,
112 src_frame.visible_rect().size()),
113 dst_frame.data(media::VideoFrame::kUPlane),
114 dst_frame.stride(media::VideoFrame::kUPlane),
115 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
116 media::VideoFrame::kUPlane,
117 dst_frame.coded_size())))
118 return false;
119 if (!VerifyPlanCopyWithPadding(
120 src_frame.visible_data(media::VideoFrame::kVPlane),
121 src_frame.stride(media::VideoFrame::kVPlane),
122 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
123 media::VideoFrame::kVPlane,
124 src_frame.visible_rect().size()),
125 dst_frame.data(media::VideoFrame::kVPlane),
126 dst_frame.stride(media::VideoFrame::kVPlane),
127 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
128 media::VideoFrame::kVPlane,
129 dst_frame.coded_size())))
130 return false;
131
132 return true;
133 }
134
135 } // namespace
136
15 namespace media { 137 namespace media {
16 138
17 class VideoUtilTest : public testing::Test { 139 class VideoUtilTest : public testing::Test {
18 public: 140 public:
19 VideoUtilTest() 141 VideoUtilTest()
20 : height_(0), 142 : height_(0),
21 y_stride_(0), 143 y_stride_(0),
22 u_stride_(0), 144 u_stride_(0),
23 v_stride_(0) { 145 v_stride_(0) {
24 } 146 }
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 (y / 2) * frame->stride(VideoFrame::kVPlane) + (x / 2)], 477 (y / 2) * frame->stride(VideoFrame::kVPlane) + (x / 2)],
356 inside ? 0x03 : 0x80); 478 inside ? 0x03 : 0x80);
357 } 479 }
358 } 480 }
359 } 481 }
360 } 482 }
361 } 483 }
362 } 484 }
363 } 485 }
364 486
487 TEST_F(VideoUtilTest, I420CopyWithPadding) {
488 gfx::Size visible_size(40, 30);
489 scoped_refptr<VideoFrame> src_frame = CreateInitializedFrame(
490 PIXEL_FORMAT_I420, visible_size, gfx::Rect(visible_size), visible_size,
491 base::TimeDelta());
492 // Expect to return false when copying to an empty buffer.
493 EXPECT_FALSE(I420CopyWithPadding(*src_frame, nullptr));
494
495 scoped_refptr<VideoFrame> dst_frame = CreateInitializedFrame(
496 PIXEL_FORMAT_I420, visible_size, gfx::Rect(visible_size), visible_size,
497 base::TimeDelta());
498 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get()));
499 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame));
500
501 gfx::Size coded_size(60, 40);
502 dst_frame = CreateInitializedFrame(PIXEL_FORMAT_I420, coded_size,
503 gfx::Rect(visible_size), coded_size,
504 base::TimeDelta());
505 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get()));
506 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame));
507
508 gfx::Size odd_size(39, 31);
509 src_frame =
510 CreateInitializedFrame(PIXEL_FORMAT_I420, odd_size, gfx::Rect(odd_size),
511 odd_size, base::TimeDelta());
512 dst_frame =
513 CreateInitializedFrame(PIXEL_FORMAT_I420, coded_size, gfx::Rect(odd_size),
514 coded_size, base::TimeDelta());
515 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get()));
516 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame));
517 }
518
365 } // namespace media 519 } // namespace media
OLDNEW
« no previous file with comments | « media/base/video_util.cc ('k') | media/cast/sender/external_video_encoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698