1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/codec/video_encoder_vpx.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "remoting/codec/codec_test.h"
12 #include "remoting/proto/video.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
18 // xRGB pixel colors for use by tests.
19 const uint32 kBlueColor
= 0x0000ff;
20 const uint32 kGreenColor
= 0x00ff00;
22 // Creates a frame stippled between blue and red pixels, which is useful for
23 // lossy/lossless encode and color tests.
24 static scoped_ptr
<webrtc::DesktopFrame
> CreateTestFrame(
25 const webrtc::DesktopSize
& frame_size
) {
26 scoped_ptr
<webrtc::DesktopFrame
> frame(
27 new webrtc::BasicDesktopFrame(frame_size
));
28 for (int x
= 0; x
< frame_size
.width(); ++x
) {
29 for (int y
= 0; y
< frame_size
.height(); ++y
) {
30 uint8
* pixel_u8
= frame
->data() + (y
* frame
->stride()) +
31 (x
* webrtc::DesktopFrame::kBytesPerPixel
);
32 *(reinterpret_cast<uint32
*>(pixel_u8
)) =
33 ((x
+ y
) & 1) ? kGreenColor
: kBlueColor
;
39 TEST(VideoEncoderVpxTest
, TestVp8VideoEncoder
) {
40 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP8());
41 TestVideoEncoder(encoder
.get(), false);
44 TEST(VideoEncoderVpxTest
, TestVp9VideoEncoder
) {
45 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP9());
46 // VP9 encoder defaults to lossless encode and lossy (I420) color.
47 TestVideoEncoder(encoder
.get(), false);
50 // Test that the VP9 encoder can switch between lossy & lossless encode.
51 TEST(VideoEncoderVpxTest
, TestVp9VideoEncoderLossyEncode
) {
52 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP9());
54 webrtc::DesktopSize
frame_size(1024, 768);
55 scoped_ptr
<webrtc::DesktopFrame
> frame(CreateTestFrame(frame_size
));
56 frame
->mutable_updated_region()->SetRect(
57 webrtc::DesktopRect::MakeSize(frame_size
));
59 // Lossy encode the first frame.
60 encoder
->SetLosslessEncode(false);
61 scoped_ptr
<VideoPacket
> lossy_packet
= encoder
->Encode(*frame
);
63 // Lossless encode the second frame.
64 encoder
->SetLosslessEncode(true);
65 scoped_ptr
<VideoPacket
> lossless_packet
= encoder
->Encode(*frame
);
66 // Lossless encode is so good that the frames are smaller than the lossy
67 // encodes. This comparison needs to be revisited.
68 // http://crbug.com/439166
69 // EXPECT_GT(lossless_packet->data().size(), lossy_packet->data().size());
71 // Lossy encode one more frame.
72 encoder
->SetLosslessEncode(false);
73 lossy_packet
= encoder
->Encode(*frame
);
75 // EXPECT_LT(lossy_packet->data().size(), lossless_packet->data().size());
78 // Test that the VP9 encoder can switch between lossy & lossless color.
79 TEST(VideoEncoderVpxTest
, TestVp9VideoEncoderLossyColor
) {
80 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP9());
82 webrtc::DesktopSize
frame_size(1024, 768);
83 scoped_ptr
<webrtc::DesktopFrame
> frame(CreateTestFrame(frame_size
));
84 frame
->mutable_updated_region()->SetRect(
85 webrtc::DesktopRect::MakeSize(frame_size
));
87 // Lossy encode the first frame.
88 encoder
->SetLosslessColor(false);
89 scoped_ptr
<VideoPacket
> lossy_packet
= encoder
->Encode(*frame
);
91 // Lossless encode the second frame.
92 encoder
->SetLosslessColor(true);
93 scoped_ptr
<VideoPacket
> lossless_packet
= encoder
->Encode(*frame
);
94 EXPECT_GT(lossless_packet
->data().size(), lossy_packet
->data().size());
96 // Lossy encode one more frame.
97 encoder
->SetLosslessColor(false);
98 lossy_packet
= encoder
->Encode(*frame
);
99 EXPECT_LT(lossy_packet
->data().size(), lossless_packet
->data().size());
102 // Test that the VP8 encoder ignores lossless modes without crashing.
103 TEST(VideoEncoderVpxTest
, TestVp8VideoEncoderIgnoreLossy
) {
104 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP8());
106 webrtc::DesktopSize
frame_size(1024, 768);
107 scoped_ptr
<webrtc::DesktopFrame
> frame(CreateTestFrame(frame_size
));
108 frame
->mutable_updated_region()->SetRect(
109 webrtc::DesktopRect::MakeSize(frame_size
));
111 // Encode a frame, to give the encoder a chance to crash if misconfigured.
112 encoder
->SetLosslessEncode(true);
113 encoder
->SetLosslessColor(true);
114 scoped_ptr
<VideoPacket
> packet
= encoder
->Encode(*frame
);
118 // Test that calling Encode with a differently-sized media::ScreenCaptureData
119 // does not leak memory.
120 TEST(VideoEncoderVpxTest
, TestSizeChangeNoLeak
) {
121 webrtc::DesktopSize
frame_size(1000, 1000);
123 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP8());
125 // Create first frame & encode it.
126 scoped_ptr
<webrtc::DesktopFrame
> frame(CreateTestFrame(frame_size
));
127 frame
->mutable_updated_region()->SetRect(
128 webrtc::DesktopRect::MakeSize(frame_size
));
129 scoped_ptr
<VideoPacket
> packet
= encoder
->Encode(*frame
);
132 // Halve the size of the frame, and updated region, and encode again.
133 frame_size
.set(frame_size
.width(), frame_size
.height() / 2);
134 frame
= CreateTestFrame(frame_size
);
135 frame
->mutable_updated_region()->SetRect(
136 webrtc::DesktopRect::MakeSize(frame_size
));
137 packet
= encoder
->Encode(*frame
);
141 // Test that the DPI information is correctly propagated from the
142 // media::ScreenCaptureData to the VideoPacket.
143 TEST(VideoEncoderVpxTest
, TestDpiPropagation
) {
144 webrtc::DesktopSize
frame_size(32, 32);
146 scoped_ptr
<VideoEncoderVpx
> encoder(VideoEncoderVpx::CreateForVP8());
148 scoped_ptr
<webrtc::DesktopFrame
> frame(CreateTestFrame(frame_size
));
149 frame
->set_dpi(webrtc::DesktopVector(96, 97));
150 scoped_ptr
<VideoPacket
> packet
= encoder
->Encode(*frame
);
151 EXPECT_EQ(packet
->format().x_dpi(), 96);
152 EXPECT_EQ(packet
->format().y_dpi(), 97);
155 } // namespace remoting