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

Side by Side Diff: webrtc/common_video/i420_video_frame_unittest.cc

Issue 2020593002: Refactor scaling. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Clarify some comments. Created 4 years, 6 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 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <math.h> 11 #include <math.h>
12 #include <string.h> 12 #include <string.h>
13 13
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webrtc/base/bind.h" 15 #include "webrtc/base/bind.h"
16 #include "webrtc/test/fake_texture_frame.h" 16 #include "webrtc/test/fake_texture_frame.h"
17 #include "webrtc/test/frame_utils.h" 17 #include "webrtc/test/frame_utils.h"
18 #include "webrtc/video_frame.h" 18 #include "webrtc/video_frame.h"
19 19
20 namespace webrtc { 20 namespace webrtc {
21 21
22 int ExpectedSize(int plane_stride, int image_height, PlaneType type); 22 namespace {
23
24 int ExpectedSize(int plane_stride, int image_height, PlaneType type) {
25 if (type == kYPlane)
26 return plane_stride * image_height;
27 return plane_stride * ((image_height + 1) / 2);
28 }
29
30 rtc::scoped_refptr<I420Buffer> CreateGradient(int width, int height) {
31 rtc::scoped_refptr<I420Buffer> buffer(
32 new rtc::RefCountedObject<I420Buffer>(width, height));
33 // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h
34 for (int x = 0; x < width; x++) {
35 for (int y = 0; y < height; y++) {
36 buffer->MutableDataY()[x + y * width] =
37 128 * (x * height + y * width) / (width * height);
38 }
39 }
40 int chroma_width = (width + 1) / 2;
41 int chroma_height = (height + 1) / 2;
42 for (int x = 0; x < chroma_width; x++) {
43 for (int y = 0; y < chroma_height; y++) {
44 buffer->MutableDataU()[x + y * chroma_width] =
45 255 * x / (chroma_width - 1);
46 buffer->MutableDataV()[x + y * chroma_width] =
47 255 * y / (chroma_height - 1);
48 }
49 }
50 return buffer;
51 }
52
53 void CheckCrop(webrtc::VideoFrameBuffer* frame,
54 // The offsets and sizes describe the rectangle extracted from
55 // the original (gradient) frame, in relative coordinates where
56 // the original frame correspond to the unit square,
57 // 0.0 <= x, y < 1.0.
58 double offset_x,
59 double offset_y,
60 double rel_width,
61 double rel_height) {
62 int width = frame->width();
63 int height = frame->height();
64 // Check that pixel values in the corners match the gradient used
65 // for initialization.
perkj_webrtc 2016/06/02 13:43:08 for ...{ for { } }
nisse-webrtc 2016/06/03 13:55:02 Done.
66 for (int i = 0; i < 2; i++)
67 for (int j = 0; j < 2; j++) {
68 // Pixel coordinates of the corner.
69 int x = i * (width - 1);
70 int y = j * (height - 1);
71 // Relative coordinates, range 0.0 - 1.0 correspond to the
72 // size of the uncropped input frame.
73 double orig_x = offset_x + i * rel_width;
74 double orig_y = offset_y + j * rel_height;
75
76 EXPECT_NEAR(frame->DataY()[x + y * frame->StrideY()] / 256.0,
77 (orig_x + orig_y) / 2, 0.02);
78 EXPECT_NEAR(frame->DataU()[x / 2 + (y / 2) * frame->StrideU()] / 256.0,
79 orig_x, 0.02);
80 EXPECT_NEAR(frame->DataV()[x / 2 + (y / 2) * frame->StrideV()] / 256.0,
81 orig_y, 0.02);
82 }
83 }
84 } // namespace
23 85
24 TEST(TestVideoFrame, InitialValues) { 86 TEST(TestVideoFrame, InitialValues) {
25 VideoFrame frame; 87 VideoFrame frame;
26 EXPECT_TRUE(frame.IsZeroSize()); 88 EXPECT_TRUE(frame.IsZeroSize());
27 EXPECT_EQ(kVideoRotation_0, frame.rotation()); 89 EXPECT_EQ(kVideoRotation_0, frame.rotation());
28 } 90 }
29 91
30 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { 92 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) {
31 VideoFrame frame; 93 VideoFrame frame;
32 VideoFrame frame2; 94 VideoFrame frame2;
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 TEST(TestI420FrameBuffer, Copy) { 296 TEST(TestI420FrameBuffer, Copy) {
235 rtc::scoped_refptr<I420Buffer> buf1( 297 rtc::scoped_refptr<I420Buffer> buf1(
236 new rtc::RefCountedObject<I420Buffer>(20, 10)); 298 new rtc::RefCountedObject<I420Buffer>(20, 10));
237 memset(buf1->MutableDataY(), 1, 200); 299 memset(buf1->MutableDataY(), 1, 200);
238 memset(buf1->MutableDataU(), 2, 50); 300 memset(buf1->MutableDataU(), 2, 50);
239 memset(buf1->MutableDataV(), 3, 50); 301 memset(buf1->MutableDataV(), 3, 50);
240 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1); 302 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(buf1);
241 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2)); 303 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2));
242 } 304 }
243 305
306 TEST(TestI420FrameBuffer, Scale) {
307 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100);
308
309 // Pure scaling, no cropping.
310 rtc::scoped_refptr<I420Buffer> scaled(
311 new rtc::RefCountedObject<I420Buffer>(150, 75));
312
313 scaled->CropAndScale(buf, 0, 0, 200, 100);
314 CheckCrop(scaled, 0.0, 0.0, 1.0, 1.0);
315 }
316
317 TEST(TestI420FrameBuffer, CropXCenter) {
318 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100);
319
320 // Pure center cropping, no scaling.
321 rtc::scoped_refptr<I420Buffer> scaled(
322 new rtc::RefCountedObject<I420Buffer>(100, 100));
323
324 scaled->CropAndScale(buf, 50, 0, 100, 100);
325 CheckCrop(scaled, 0.25, 0.0, 0.5, 1.0);
326 }
327
328 TEST(TestI420FrameBuffer, CropXNotCenter) {
329 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100);
330
331 // Non-center cropping, no scaling.
332 rtc::scoped_refptr<I420Buffer> scaled(
333 new rtc::RefCountedObject<I420Buffer>(100, 100));
334
335 scaled->CropAndScale(buf, 25, 0, 100, 100);
336 CheckCrop(scaled, 0.125, 0.0, 0.5, 1.0);
337 }
338
339 TEST(TestI420FrameBuffer, CropYCenter) {
340 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200);
341
342 // Pure center cropping, no scaling.
343 rtc::scoped_refptr<I420Buffer> scaled(
344 new rtc::RefCountedObject<I420Buffer>(100, 100));
345
346 scaled->CropAndScale(buf, 0, 50, 100, 100);
347 CheckCrop(scaled, 0.0, 0.25, 1.0, 0.5);
348 }
349
350 TEST(TestI420FrameBuffer, CropYNotCenter) {
351 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200);
352
353 // Non-center cropping, no scaling.
354 rtc::scoped_refptr<I420Buffer> scaled(
355 new rtc::RefCountedObject<I420Buffer>(100, 100));
356
357 scaled->CropAndScale(buf, 0, 25, 100, 100);
358 CheckCrop(scaled, 0.0, 0.125, 1.0, 0.5);
359 }
360
361 TEST(TestI420FrameBuffer, CropAndScale16x9) {
362 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(640, 480);
363
364 // Center crop to 640 x 360 (16/9 aspect), then scale down by 2.
365 rtc::scoped_refptr<I420Buffer> scaled(
366 new rtc::RefCountedObject<I420Buffer>(320, 180));
367
368 scaled->CropAndScale(buf, 0, 60, 640, 360);
369 CheckCrop(scaled, 0.0, 0.125, 1.0, 0.75);
370 }
371
244 } // namespace webrtc 372 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698