blob: 025c54b6ff6f11b71a0f9ab2a4d8dcf9cdfa6ec6 [file] [log] [blame]
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <math.h>
12#include <string.h>
13
Henrik Kjellandera80c16a2017-07-01 16:48:15 +020014#include "webrtc/api/video/i420_buffer.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/api/video/video_frame.h"
16#include "webrtc/rtc_base/bind.h"
17#include "webrtc/rtc_base/timeutils.h"
Peter Boströmeb66e802015-06-05 11:08:03 +020018#include "webrtc/test/fake_texture_frame.h"
Niels Möller739fcb92016-02-29 13:11:45 +010019#include "webrtc/test/frame_utils.h"
kwibergac9f8762016-09-30 22:29:43 -070020#include "webrtc/test/gtest.h"
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000021
22namespace webrtc {
23
Niels Möller718a7632016-06-13 13:06:01 +020024namespace {
25
Niels Möller718a7632016-06-13 13:06:01 +020026rtc::scoped_refptr<I420Buffer> CreateGradient(int width, int height) {
27 rtc::scoped_refptr<I420Buffer> buffer(
nisseac62bd42016-06-20 03:38:52 -070028 I420Buffer::Create(width, height));
Niels Möller718a7632016-06-13 13:06:01 +020029 // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h
30 for (int x = 0; x < width; x++) {
31 for (int y = 0; y < height; y++) {
32 buffer->MutableDataY()[x + y * width] =
33 128 * (x * height + y * width) / (width * height);
34 }
35 }
magjed3f075492017-06-01 10:02:26 -070036 int chroma_width = buffer->ChromaWidth();
37 int chroma_height = buffer->ChromaHeight();
Niels Möller718a7632016-06-13 13:06:01 +020038 for (int x = 0; x < chroma_width; x++) {
39 for (int y = 0; y < chroma_height; y++) {
40 buffer->MutableDataU()[x + y * chroma_width] =
41 255 * x / (chroma_width - 1);
42 buffer->MutableDataV()[x + y * chroma_width] =
43 255 * y / (chroma_height - 1);
44 }
45 }
46 return buffer;
47}
48
49// The offsets and sizes describe the rectangle extracted from the
50// original (gradient) frame, in relative coordinates where the
51// original frame correspond to the unit square, 0.0 <= x, y < 1.0.
magjed3f075492017-06-01 10:02:26 -070052void CheckCrop(const webrtc::I420BufferInterface& frame,
Niels Möller718a7632016-06-13 13:06:01 +020053 double offset_x,
54 double offset_y,
55 double rel_width,
56 double rel_height) {
nissef33970b2016-10-18 06:01:34 -070057 int width = frame.width();
58 int height = frame.height();
Niels Möller718a7632016-06-13 13:06:01 +020059 // Check that pixel values in the corners match the gradient used
60 // for initialization.
61 for (int i = 0; i < 2; i++) {
62 for (int j = 0; j < 2; j++) {
63 // Pixel coordinates of the corner.
64 int x = i * (width - 1);
65 int y = j * (height - 1);
66 // Relative coordinates, range 0.0 - 1.0 correspond to the
67 // size of the uncropped input frame.
68 double orig_x = offset_x + i * rel_width;
69 double orig_y = offset_y + j * rel_height;
70
nissef33970b2016-10-18 06:01:34 -070071 EXPECT_NEAR(frame.DataY()[x + y * frame.StrideY()] / 256.0,
Niels Möller718a7632016-06-13 13:06:01 +020072 (orig_x + orig_y) / 2, 0.02);
nissef33970b2016-10-18 06:01:34 -070073 EXPECT_NEAR(frame.DataU()[x / 2 + (y / 2) * frame.StrideU()] / 256.0,
Niels Möller718a7632016-06-13 13:06:01 +020074 orig_x, 0.02);
nissef33970b2016-10-18 06:01:34 -070075 EXPECT_NEAR(frame.DataV()[x / 2 + (y / 2) * frame.StrideV()] / 256.0,
Niels Möller718a7632016-06-13 13:06:01 +020076 orig_y, 0.02);
77 }
78 }
79}
80
magjed3f075492017-06-01 10:02:26 -070081void CheckRotate(int width,
82 int height,
83 webrtc::VideoRotation rotation,
84 const webrtc::I420BufferInterface& rotated) {
nissef33970b2016-10-18 06:01:34 -070085 int rotated_width = width;
86 int rotated_height = height;
87
88 if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) {
89 std::swap(rotated_width, rotated_height);
90 }
91 EXPECT_EQ(rotated_width, rotated.width());
92 EXPECT_EQ(rotated_height, rotated.height());
93
94 // Clock-wise order (with 0,0 at top-left)
95 const struct { int x; int y; } corners[] = {
96 { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 }
97 };
98 // Corresponding corner colors of the frame produced by CreateGradient.
99 const struct { int y; int u; int v; } colors[] = {
100 {0, 0, 0}, { 127, 255, 0}, { 255, 255, 255 }, {127, 0, 255}
101 };
102 int corner_offset = static_cast<int>(rotation) / 90;
103
104 for (int i = 0; i < 4; i++) {
105 int j = (i + corner_offset) % 4;
106 int x = corners[j].x * (rotated_width - 1);
107 int y = corners[j].y * (rotated_height - 1);
108 EXPECT_EQ(colors[i].y, rotated.DataY()[x + y * rotated.StrideY()]);
109 EXPECT_EQ(colors[i].u,
110 rotated.DataU()[(x / 2) + (y / 2) * rotated.StrideU()]);
111 EXPECT_EQ(colors[i].v,
112 rotated.DataV()[(x / 2) + (y / 2) * rotated.StrideV()]);
113 }
114}
115
Niels Möller718a7632016-06-13 13:06:01 +0200116} // namespace
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000117
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700118TEST(TestVideoFrame, WidthHeightValues) {
nissef122a852016-10-04 23:27:30 -0700119 VideoFrame frame(I420Buffer::Create(10, 10, 10, 14, 90),
120 webrtc::kVideoRotation_0,
121 789 * rtc::kNumMicrosecsPerMillisec);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000122 const int valid_value = 10;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000123 EXPECT_EQ(valid_value, frame.width());
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000124 EXPECT_EQ(valid_value, frame.height());
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000125 frame.set_timestamp(123u);
126 EXPECT_EQ(123u, frame.timestamp());
127 frame.set_ntp_time_ms(456);
128 EXPECT_EQ(456, frame.ntp_time_ms());
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000129 EXPECT_EQ(789, frame.render_time_ms());
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000130}
131
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700132TEST(TestVideoFrame, ShallowCopy) {
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000133 uint32_t timestamp = 1;
134 int64_t ntp_time_ms = 2;
nisse1c0dea82017-01-30 02:43:18 -0800135 int64_t timestamp_us = 3;
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000136 int stride_y = 15;
137 int stride_u = 10;
138 int stride_v = 10;
139 int width = 15;
140 int height = 15;
141
142 const int kSizeY = 400;
143 const int kSizeU = 100;
144 const int kSizeV = 100;
145 const VideoRotation kRotation = kVideoRotation_270;
146 uint8_t buffer_y[kSizeY];
147 uint8_t buffer_u[kSizeU];
148 uint8_t buffer_v[kSizeV];
149 memset(buffer_y, 16, kSizeY);
150 memset(buffer_u, 8, kSizeU);
151 memset(buffer_v, 4, kSizeV);
nisse2f58ec82016-11-28 03:47:52 -0800152
nissef0a7c5a2016-10-31 05:48:07 -0700153 VideoFrame frame1(
nisse2f58ec82016-11-28 03:47:52 -0800154 I420Buffer::Copy(width, height,
155 buffer_y, stride_y,
156 buffer_u, stride_u,
157 buffer_v, stride_v),
nissef0a7c5a2016-10-31 05:48:07 -0700158 kRotation, 0);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000159 frame1.set_timestamp(timestamp);
160 frame1.set_ntp_time_ms(ntp_time_ms);
nisse1c0dea82017-01-30 02:43:18 -0800161 frame1.set_timestamp_us(timestamp_us);
nissef0a7c5a2016-10-31 05:48:07 -0700162 VideoFrame frame2(frame1);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000163
nissef0a7c5a2016-10-31 05:48:07 -0700164 EXPECT_EQ(frame1.video_frame_buffer(), frame2.video_frame_buffer());
magjed3f075492017-06-01 10:02:26 -0700165 rtc::scoped_refptr<I420BufferInterface> yuv1 =
166 frame1.video_frame_buffer()->GetI420();
167 rtc::scoped_refptr<I420BufferInterface> yuv2 =
168 frame2.video_frame_buffer()->GetI420();
169 EXPECT_EQ(yuv1->DataY(), yuv2->DataY());
170 EXPECT_EQ(yuv1->DataU(), yuv2->DataU());
171 EXPECT_EQ(yuv1->DataV(), yuv2->DataV());
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000172
173 EXPECT_EQ(frame2.timestamp(), frame1.timestamp());
174 EXPECT_EQ(frame2.ntp_time_ms(), frame1.ntp_time_ms());
nisse1c0dea82017-01-30 02:43:18 -0800175 EXPECT_EQ(frame2.timestamp_us(), frame1.timestamp_us());
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000176 EXPECT_EQ(frame2.rotation(), frame1.rotation());
177
178 frame2.set_timestamp(timestamp + 1);
179 frame2.set_ntp_time_ms(ntp_time_ms + 1);
nisse1c0dea82017-01-30 02:43:18 -0800180 frame2.set_timestamp_us(timestamp_us + 1);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000181 frame2.set_rotation(kVideoRotation_90);
182
183 EXPECT_NE(frame2.timestamp(), frame1.timestamp());
184 EXPECT_NE(frame2.ntp_time_ms(), frame1.ntp_time_ms());
nisse1c0dea82017-01-30 02:43:18 -0800185 EXPECT_NE(frame2.timestamp_us(), frame1.timestamp_us());
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000186 EXPECT_NE(frame2.rotation(), frame1.rotation());
187}
188
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700189TEST(TestVideoFrame, TextureInitialValues) {
Magnus Jedvert90e31902017-06-07 11:32:50 +0200190 VideoFrame frame = test::FakeNativeBuffer::CreateFrame(
191 640, 480, 100, 10, webrtc::kVideoRotation_0);
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000192 EXPECT_EQ(640, frame.width());
193 EXPECT_EQ(480, frame.height());
194 EXPECT_EQ(100u, frame.timestamp());
195 EXPECT_EQ(10, frame.render_time_ms());
nisse26acec42016-04-15 03:43:39 -0700196 ASSERT_TRUE(frame.video_frame_buffer() != nullptr);
Magnus Jedvert90e31902017-06-07 11:32:50 +0200197 EXPECT_TRUE(frame.video_frame_buffer()->type() ==
198 VideoFrameBuffer::Type::kNative);
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000199
200 frame.set_timestamp(200);
201 EXPECT_EQ(200u, frame.timestamp());
nisse1c0dea82017-01-30 02:43:18 -0800202 frame.set_timestamp_us(20);
203 EXPECT_EQ(20, frame.timestamp_us());
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000204}
205
nisse7cc9cc02016-03-29 23:44:19 -0700206TEST(TestI420FrameBuffer, Copy) {
207 rtc::scoped_refptr<I420Buffer> buf1(
nisseac62bd42016-06-20 03:38:52 -0700208 I420Buffer::Create(20, 10));
nisse06176e42016-04-18 05:34:40 -0700209 memset(buf1->MutableDataY(), 1, 200);
210 memset(buf1->MutableDataU(), 2, 50);
211 memset(buf1->MutableDataV(), 3, 50);
nissee3fe4a72016-11-10 08:44:38 -0800212 rtc::scoped_refptr<I420Buffer> buf2 = I420Buffer::Copy(*buf1);
nisse7cc9cc02016-03-29 23:44:19 -0700213 EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2));
214}
215
Niels Möller718a7632016-06-13 13:06:01 +0200216TEST(TestI420FrameBuffer, Scale) {
217 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100);
218
219 // Pure scaling, no cropping.
220 rtc::scoped_refptr<I420Buffer> scaled_buffer(
nisseac62bd42016-06-20 03:38:52 -0700221 I420Buffer::Create(150, 75));
Niels Möller718a7632016-06-13 13:06:01 +0200222
nissee3fe4a72016-11-10 08:44:38 -0800223 scaled_buffer->ScaleFrom(*buf);
nissef33970b2016-10-18 06:01:34 -0700224 CheckCrop(*scaled_buffer, 0.0, 0.0, 1.0, 1.0);
Niels Möller718a7632016-06-13 13:06:01 +0200225}
226
227TEST(TestI420FrameBuffer, CropXCenter) {
228 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100);
229
230 // Pure center cropping, no scaling.
231 rtc::scoped_refptr<I420Buffer> scaled_buffer(
nisseac62bd42016-06-20 03:38:52 -0700232 I420Buffer::Create(100, 100));
Niels Möller718a7632016-06-13 13:06:01 +0200233
nissee3fe4a72016-11-10 08:44:38 -0800234 scaled_buffer->CropAndScaleFrom(*buf, 50, 0, 100, 100);
nissef33970b2016-10-18 06:01:34 -0700235 CheckCrop(*scaled_buffer, 0.25, 0.0, 0.5, 1.0);
Niels Möller718a7632016-06-13 13:06:01 +0200236}
237
238TEST(TestI420FrameBuffer, CropXNotCenter) {
239 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(200, 100);
240
241 // Non-center cropping, no scaling.
242 rtc::scoped_refptr<I420Buffer> scaled_buffer(
nisseac62bd42016-06-20 03:38:52 -0700243 I420Buffer::Create(100, 100));
Niels Möller718a7632016-06-13 13:06:01 +0200244
nissee3fe4a72016-11-10 08:44:38 -0800245 scaled_buffer->CropAndScaleFrom(*buf, 25, 0, 100, 100);
nissef33970b2016-10-18 06:01:34 -0700246 CheckCrop(*scaled_buffer, 0.125, 0.0, 0.5, 1.0);
Niels Möller718a7632016-06-13 13:06:01 +0200247}
248
249TEST(TestI420FrameBuffer, CropYCenter) {
250 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200);
251
252 // Pure center cropping, no scaling.
253 rtc::scoped_refptr<I420Buffer> scaled_buffer(
nisseac62bd42016-06-20 03:38:52 -0700254 I420Buffer::Create(100, 100));
Niels Möller718a7632016-06-13 13:06:01 +0200255
nissee3fe4a72016-11-10 08:44:38 -0800256 scaled_buffer->CropAndScaleFrom(*buf, 0, 50, 100, 100);
nissef33970b2016-10-18 06:01:34 -0700257 CheckCrop(*scaled_buffer, 0.0, 0.25, 1.0, 0.5);
Niels Möller718a7632016-06-13 13:06:01 +0200258}
259
260TEST(TestI420FrameBuffer, CropYNotCenter) {
261 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(100, 200);
262
263 // Non-center cropping, no scaling.
264 rtc::scoped_refptr<I420Buffer> scaled_buffer(
nisseac62bd42016-06-20 03:38:52 -0700265 I420Buffer::Create(100, 100));
Niels Möller718a7632016-06-13 13:06:01 +0200266
nissee3fe4a72016-11-10 08:44:38 -0800267 scaled_buffer->CropAndScaleFrom(*buf, 0, 25, 100, 100);
nissef33970b2016-10-18 06:01:34 -0700268 CheckCrop(*scaled_buffer, 0.0, 0.125, 1.0, 0.5);
Niels Möller718a7632016-06-13 13:06:01 +0200269}
270
271TEST(TestI420FrameBuffer, CropAndScale16x9) {
272 rtc::scoped_refptr<I420Buffer> buf = CreateGradient(640, 480);
273
274 // Center crop to 640 x 360 (16/9 aspect), then scale down by 2.
275 rtc::scoped_refptr<I420Buffer> scaled_buffer(
nisseac62bd42016-06-20 03:38:52 -0700276 I420Buffer::Create(320, 180));
Niels Möller718a7632016-06-13 13:06:01 +0200277
nissee3fe4a72016-11-10 08:44:38 -0800278 scaled_buffer->CropAndScaleFrom(*buf);
nissef33970b2016-10-18 06:01:34 -0700279 CheckCrop(*scaled_buffer, 0.0, 0.125, 1.0, 0.75);
Niels Möller718a7632016-06-13 13:06:01 +0200280}
281
nissef33970b2016-10-18 06:01:34 -0700282class TestI420BufferRotate
283 : public ::testing::TestWithParam<webrtc::VideoRotation> {};
284
285TEST_P(TestI420BufferRotate, Rotates) {
magjed3f075492017-06-01 10:02:26 -0700286 rtc::scoped_refptr<I420BufferInterface> buffer = CreateGradient(640, 480);
287 rtc::scoped_refptr<I420BufferInterface> rotated_buffer =
nisseaf916892017-01-10 07:44:26 -0800288 I420Buffer::Rotate(*buffer, GetParam());
nissef33970b2016-10-18 06:01:34 -0700289 CheckRotate(640, 480, GetParam(), *rotated_buffer);
290}
291
292INSTANTIATE_TEST_CASE_P(Rotate, TestI420BufferRotate,
293 ::testing::Values(kVideoRotation_0,
294 kVideoRotation_90,
295 kVideoRotation_180,
296 kVideoRotation_270));
297
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000298} // namespace webrtc