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

Side by Side Diff: media/base/video_util_unittest.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: Add unit test and addressed Xiaohan's comments. 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 <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 {
xhwang 2016/05/05 22:00:21 nit: move this whole anonymous namespace block int
xjz 2016/05/05 22:52:19 These are helper functions. I think it might be be
16
17 // Helper function used to verify the data in the coded region after copying the
18 // visible region and padding the remaining area.
19 bool VerifyCodedRegion(const uint8_t* src,
20 size_t src_stride,
21 const gfx::Size& visible_size,
22 const uint8_t* dst,
23 size_t dst_stride,
24 const gfx::Size& coded_size) {
xhwang 2016/05/05 22:00:21 It's probably more readable if you s/visible_size/
xjz 2016/05/05 22:52:19 Done.
25 if (!src || !dst)
26 return false;
27
28 const size_t width = visible_size.width();
xhwang 2016/05/05 22:00:21 nit: src_width
xjz 2016/05/05 22:52:19 Done.
29 const size_t height = visible_size.height();
30 const size_t dst_width = coded_size.width();
31 const size_t dst_height = coded_size.height();
32 if (width > dst_width || width > src_stride || height > dst_height ||
33 visible_size.IsEmpty() || coded_size.IsEmpty())
34 return false;
35
36 const uint8_t *src_ptr = src, *dst_ptr = dst;
37 for (size_t i = 0; i < height;
38 ++i, src_ptr += src_stride, dst_ptr += dst_stride) {
39 size_t j = 0;
40 for (; j < width; ++j) {
41 if (src_ptr[j] != dst_ptr[j])
42 return false;
43 }
xhwang 2016/05/05 22:00:21 Use memcmp for this one?
xjz 2016/05/05 22:52:19 Done.
44 uint8_t last_pixel = src_ptr[j - 1];
xhwang 2016/05/05 22:00:21 width - 1 might be a bit more readable.
xjz 2016/05/05 22:52:19 Done.
45 for (; j < dst_width; ++j) {
46 if (last_pixel != dst_ptr[j])
47 return false;
48 }
xhwang 2016/05/05 22:00:21 ditto about memcmp
xjz 2016/05/05 22:52:19 Done.
49 }
50 if (height < dst_height) {
51 src_ptr = dst + (height - 1) * dst_stride;
52 for (size_t i = height; i < dst_height; ++i, dst_ptr += dst_stride) {
53 for (size_t j = 0; j < dst_width; ++j) {
54 if (src_ptr[j] != dst_ptr[j])
55 return false;
56 }
57 }
58 }
59 return true;
60 }
61
62 bool VerifyCopyWithPadding(const media::VideoFrame& src_frame,
63 const media::VideoFrame& dst_frame) {
64 if (!src_frame.IsMappable() || !dst_frame.IsMappable() ||
65 src_frame.visible_rect().size() != dst_frame.visible_rect().size())
66 return false;
67
68 if (!VerifyCodedRegion(src_frame.visible_data(media::VideoFrame::kYPlane),
69 src_frame.stride(media::VideoFrame::kYPlane),
70 src_frame.visible_rect().size(),
71 dst_frame.data(media::VideoFrame::kYPlane),
72 dst_frame.stride(media::VideoFrame::kYPlane),
73 dst_frame.coded_size()))
74 return false;
75 if (!VerifyCodedRegion(
76 src_frame.visible_data(media::VideoFrame::kUPlane),
77 src_frame.stride(media::VideoFrame::kUPlane),
78 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
79 media::VideoFrame::kUPlane,
80 src_frame.visible_rect().size()),
81 dst_frame.data(media::VideoFrame::kUPlane),
82 dst_frame.stride(media::VideoFrame::kUPlane),
83 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
84 media::VideoFrame::kUPlane,
85 dst_frame.coded_size())))
86 return false;
87 if (!VerifyCodedRegion(
88 src_frame.visible_data(media::VideoFrame::kVPlane),
89 src_frame.stride(media::VideoFrame::kVPlane),
90 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
91 media::VideoFrame::kVPlane,
92 src_frame.visible_rect().size()),
93 dst_frame.data(media::VideoFrame::kVPlane),
94 dst_frame.stride(media::VideoFrame::kVPlane),
95 media::VideoFrame::PlaneSize(media::PIXEL_FORMAT_I420,
96 media::VideoFrame::kVPlane,
97 dst_frame.coded_size())))
98 return false;
99
100 return true;
101 }
102 }
103
15 namespace media { 104 namespace media {
16 105
17 class VideoUtilTest : public testing::Test { 106 class VideoUtilTest : public testing::Test {
18 public: 107 public:
19 VideoUtilTest() 108 VideoUtilTest()
20 : height_(0), 109 : height_(0),
21 y_stride_(0), 110 y_stride_(0),
22 u_stride_(0), 111 u_stride_(0),
23 v_stride_(0) { 112 v_stride_(0) {
24 } 113 }
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 (y / 2) * frame->stride(VideoFrame::kVPlane) + (x / 2)], 444 (y / 2) * frame->stride(VideoFrame::kVPlane) + (x / 2)],
356 inside ? 0x03 : 0x80); 445 inside ? 0x03 : 0x80);
357 } 446 }
358 } 447 }
359 } 448 }
360 } 449 }
361 } 450 }
362 } 451 }
363 } 452 }
364 453
454 TEST_F(VideoUtilTest, I420CopyWithPadding) {
455 gfx::Size visible_size(40, 30);
456 scoped_refptr<VideoFrame> src_frame(VideoFrame::CreateFrame(
457 PIXEL_FORMAT_I420, visible_size, gfx::Rect(visible_size), visible_size,
458 base::TimeDelta()));
459 // Expect to return false when copying to an empty buffer.
460 EXPECT_FALSE(I420CopyWithPadding(*src_frame, nullptr));
461
462 scoped_refptr<VideoFrame> dst_frame(VideoFrame::CreateFrame(
463 PIXEL_FORMAT_I420, visible_size, gfx::Rect(visible_size), visible_size,
464 base::TimeDelta()));
465 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get()));
466 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame));
467
468 gfx::Size coded_size(60, 40);
469 dst_frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, coded_size,
470 gfx::Rect(visible_size), coded_size,
471 base::TimeDelta());
472 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get()));
473 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame));
474
475 gfx::Size odd_size(39, 31);
476 src_frame =
477 VideoFrame::CreateFrame(PIXEL_FORMAT_I420, odd_size, gfx::Rect(odd_size),
478 odd_size, base::TimeDelta());
479 dst_frame = VideoFrame::CreateFrame(PIXEL_FORMAT_I420, coded_size,
480 gfx::Rect(odd_size), coded_size,
481 base::TimeDelta());
482 EXPECT_TRUE(I420CopyWithPadding(*src_frame, dst_frame.get()));
483 EXPECT_TRUE(VerifyCopyWithPadding(*src_frame, *dst_frame));
484 }
485
365 } // namespace media 486 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698