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

Unified Diff: webrtc/common_video/i420_video_frame_unittest.cc

Issue 1973873003: Delete AndroidVideoCapturer::FrameFactory. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix width/height typo. Tests. Formatting. 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 side-by-side diff with in-line comments
Download patch
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 bc2bac920ad7c411e6c85630de21c027be81b7c1..fbdeaaa05980f5fe1b69577ae7887a792e0c0b86 100644
--- a/webrtc/common_video/i420_video_frame_unittest.cc
+++ b/webrtc/common_video/i420_video_frame_unittest.cc
@@ -19,7 +19,63 @@
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++)
magjed_webrtc 2016/05/17 12:12:40 nit: Add {}
nisse-webrtc 2016/05/18 11:16:33 Done.
+ 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++)
magjed_webrtc 2016/05/17 12:12:40 nit: Add {}
nisse-webrtc 2016/05/18 11:16:33 Done.
+ 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,
+ 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.
+ 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;
@@ -250,4 +306,58 @@ 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 =
+ I420Buffer::CropAndScale(buf, 0, 0, 200, 100, 150, 75);
+ 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 =
+ I420Buffer::CropAndScale(buf, 50, 0, 100, 100, 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 =
+ I420Buffer::CropAndScale(buf, 25, 0, 100, 100, 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 =
+ I420Buffer::CropAndScale(buf, 0, 50, 100, 100, 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 =
+ I420Buffer::CropAndScale(buf, 0, 25, 100, 100, 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 =
+ I420Buffer::CropAndScale(buf, 0, 60, 640, 360, 320, 180);
+ CheckCrop(scaled, 0.0, 0.125, 1.0, 0.75);
+}
+
} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698