Index: webrtc/common_video/i420_video_frame_unittest.cc |
diff --git a/webrtc/common_video/i420_video_frame_unittest.cc b/webrtc/common_video/i420_video_frame_unittest.cc |
index 5144c2715f3f488f9b8e07fa50770180f24ed38a..000b6b50e0e99707384e1aae1661088f344ac0e8 100644 |
--- a/webrtc/common_video/i420_video_frame_unittest.cc |
+++ b/webrtc/common_video/i420_video_frame_unittest.cc |
@@ -19,7 +19,69 @@ |
namespace webrtc { |
-int ExpectedSize(int plane_stride, int image_height, PlaneType type); |
+namespace { |
+ |
+int ExpectedSize(int plane_stride, int image_height, PlaneType type) { |
+ if (type == kYPlane) |
+ return plane_stride * image_height; |
+ return plane_stride * ((image_height + 1) / 2); |
+} |
+ |
+rtc::scoped_refptr<I420Buffer> CreateGradient(int width, int height) { |
+ rtc::scoped_refptr<I420Buffer> buffer( |
+ new rtc::RefCountedObject<I420Buffer>(width, height)); |
+ // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h |
+ for (int x = 0; x < width; x++) { |
+ for (int y = 0; y < height; y++) { |
+ buffer->MutableDataY()[x + y * width] = |
+ 128 * (x * height + y * width) / (width * height); |
+ } |
+ } |
+ int chroma_width = (width + 1) / 2; |
+ int chroma_height = (height + 1) / 2; |
+ for (int x = 0; x < chroma_width; x++) { |
+ for (int y = 0; y < chroma_height; y++) { |
+ buffer->MutableDataU()[x + y * chroma_width] = |
+ 255 * x / (chroma_width - 1); |
+ buffer->MutableDataV()[x + y * chroma_width] = |
+ 255 * y / (chroma_height - 1); |
+ } |
+ } |
+ return buffer; |
+} |
+ |
+void CheckCrop(webrtc::VideoFrameBuffer* frame, |
+ // The offsets and sizes describe the rectangle extracted from |
+ // the original (gradient) frame, in relative coordinates where |
+ // the original frame correspond to the unit square, |
+ // 0.0 <= x, y < 1.0. |
+ double offset_x, |
+ double offset_y, |
+ double rel_width, |
+ double rel_height) { |
+ int width = frame->width(); |
+ int height = frame->height(); |
+ // Check that pixel values in the corners match the gradient used |
+ // for initialization. |
perkj_webrtc
2016/06/02 13:43:08
for ...{
for {
}
}
nisse-webrtc
2016/06/03 13:55:02
Done.
|
+ for (int i = 0; i < 2; i++) |
+ for (int j = 0; j < 2; j++) { |
+ // Pixel coordinates of the corner. |
+ int x = i * (width - 1); |
+ int y = j * (height - 1); |
+ // Relative coordinates, range 0.0 - 1.0 correspond to the |
+ // size of the uncropped input frame. |
+ double orig_x = offset_x + i * rel_width; |
+ double orig_y = offset_y + j * rel_height; |
+ |
+ EXPECT_NEAR(frame->DataY()[x + y * frame->StrideY()] / 256.0, |
+ (orig_x + orig_y) / 2, 0.02); |
+ EXPECT_NEAR(frame->DataU()[x / 2 + (y / 2) * frame->StrideU()] / 256.0, |
+ orig_x, 0.02); |
+ EXPECT_NEAR(frame->DataV()[x / 2 + (y / 2) * frame->StrideV()] / 256.0, |
+ orig_y, 0.02); |
+ } |
+} |
+} // namespace |
TEST(TestVideoFrame, InitialValues) { |
VideoFrame frame; |
@@ -241,4 +303,70 @@ TEST(TestI420FrameBuffer, Copy) { |
EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); |
} |
+TEST(TestI420FrameBuffer, Scale) { |
+ rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); |
+ |
+ // Pure scaling, no cropping. |
+ rtc::scoped_refptr<I420Buffer> scaled( |
+ new rtc::RefCountedObject<I420Buffer>(150, 75)); |
+ |
+ scaled->CropAndScale(buf, 0, 0, 200, 100); |
+ CheckCrop(scaled, 0.0, 0.0, 1.0, 1.0); |
+} |
+ |
+TEST(TestI420FrameBuffer, CropXCenter) { |
+ rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); |
+ |
+ // Pure center cropping, no scaling. |
+ rtc::scoped_refptr<I420Buffer> scaled( |
+ new rtc::RefCountedObject<I420Buffer>(100, 100)); |
+ |
+ scaled->CropAndScale(buf, 50, 0, 100, 100); |
+ CheckCrop(scaled, 0.25, 0.0, 0.5, 1.0); |
+} |
+ |
+TEST(TestI420FrameBuffer, CropXNotCenter) { |
+ rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100); |
+ |
+ // Non-center cropping, no scaling. |
+ rtc::scoped_refptr<I420Buffer> scaled( |
+ new rtc::RefCountedObject<I420Buffer>(100, 100)); |
+ |
+ scaled->CropAndScale(buf, 25, 0, 100, 100); |
+ CheckCrop(scaled, 0.125, 0.0, 0.5, 1.0); |
+} |
+ |
+TEST(TestI420FrameBuffer, CropYCenter) { |
+ rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); |
+ |
+ // Pure center cropping, no scaling. |
+ rtc::scoped_refptr<I420Buffer> scaled( |
+ new rtc::RefCountedObject<I420Buffer>(100, 100)); |
+ |
+ scaled->CropAndScale(buf, 0, 50, 100, 100); |
+ CheckCrop(scaled, 0.0, 0.25, 1.0, 0.5); |
+} |
+ |
+TEST(TestI420FrameBuffer, CropYNotCenter) { |
+ rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200); |
+ |
+ // Non-center cropping, no scaling. |
+ rtc::scoped_refptr<I420Buffer> scaled( |
+ new rtc::RefCountedObject<I420Buffer>(100, 100)); |
+ |
+ scaled->CropAndScale(buf, 0, 25, 100, 100); |
+ CheckCrop(scaled, 0.0, 0.125, 1.0, 0.5); |
+} |
+ |
+TEST(TestI420FrameBuffer, CropAndScale16x9) { |
+ rtc::scoped_refptr<I420Buffer> buf = CreateGradient(640, 480); |
+ |
+ // Center crop to 640 x 360 (16/9 aspect), then scale down by 2. |
+ rtc::scoped_refptr<I420Buffer> scaled( |
+ new rtc::RefCountedObject<I420Buffer>(320, 180)); |
+ |
+ scaled->CropAndScale(buf, 0, 60, 640, 360); |
+ CheckCrop(scaled, 0.0, 0.125, 1.0, 0.75); |
+} |
+ |
} // namespace webrtc |