blob: a4b110b6b93d313a8caef03ffe3719ba11c17bb6 [file] [log] [blame]
Emircan Uysaler901e0ff2018-06-26 12:22:38 -07001/*
2 * Copyright (c) 2018 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
14#include "api/video/i010_buffer.h"
15#include "api/video/i420_buffer.h"
16#include "api/video/video_frame.h"
17#include "rtc_base/bind.h"
18#include "rtc_base/timeutils.h"
19#include "test/fake_texture_frame.h"
20#include "test/frame_utils.h"
21#include "test/gtest.h"
22
23namespace webrtc {
24
25namespace {
26
27// Helper class to delegate calls to appropriate container.
28class PlanarYuvBufferFactory {
29 public:
30 static rtc::scoped_refptr<PlanarYuvBuffer> Create(VideoFrameBuffer::Type type,
31 int width,
32 int height) {
33 switch (type) {
34 case VideoFrameBuffer::Type::kI420:
35 return I420Buffer::Create(width, height);
36 case VideoFrameBuffer::Type::kI010:
37 return I010Buffer::Create(width, height);
38 default:
39 RTC_NOTREACHED();
40 }
41 return nullptr;
42 }
43
44 static rtc::scoped_refptr<PlanarYuvBuffer> Copy(const VideoFrameBuffer& src) {
45 switch (src.type()) {
46 case VideoFrameBuffer::Type::kI420:
47 return I420Buffer::Copy(src);
48 case VideoFrameBuffer::Type::kI010:
49 return I010Buffer::Copy(*src.GetI010());
50 default:
51 RTC_NOTREACHED();
52 }
53 return nullptr;
54 }
55
56 static rtc::scoped_refptr<PlanarYuvBuffer> Rotate(const VideoFrameBuffer& src,
57 VideoRotation rotation) {
58 switch (src.type()) {
59 case VideoFrameBuffer::Type::kI420:
60 return I420Buffer::Rotate(src, rotation);
61 case VideoFrameBuffer::Type::kI010:
62 return I010Buffer::Rotate(*src.GetI010(), rotation);
63 default:
64 RTC_NOTREACHED();
65 }
66 return nullptr;
67 }
68
69 static rtc::scoped_refptr<PlanarYuvBuffer> CropAndScaleFrom(
70 const VideoFrameBuffer& src,
71 int offset_x,
72 int offset_y,
73 int crop_width,
74 int crop_height) {
75 switch (src.type()) {
76 case VideoFrameBuffer::Type::kI420: {
77 rtc::scoped_refptr<I420Buffer> buffer =
78 I420Buffer::Create(crop_width, crop_height);
79 buffer->CropAndScaleFrom(*src.GetI420(), offset_x, offset_y, crop_width,
80 crop_height);
81 return buffer;
82 }
83 case VideoFrameBuffer::Type::kI010: {
84 rtc::scoped_refptr<I010Buffer> buffer =
85 I010Buffer::Create(crop_width, crop_height);
86 buffer->CropAndScaleFrom(*src.GetI010(), offset_x, offset_y, crop_width,
87 crop_height);
88 return buffer;
89 }
90 default:
91 RTC_NOTREACHED();
92 }
93 return nullptr;
94 }
95
96 static rtc::scoped_refptr<PlanarYuvBuffer> CropAndScaleFrom(
97 const VideoFrameBuffer& src,
98 int crop_width,
99 int crop_height) {
100 const int out_width =
101 std::min(src.width(), crop_width * src.height() / crop_height);
102 const int out_height =
103 std::min(src.height(), crop_height * src.width() / crop_width);
104 return CropAndScaleFrom(src, (src.width() - out_width) / 2,
105 (src.height() - out_height) / 2, out_width,
106 out_height);
107 }
108
109 static rtc::scoped_refptr<PlanarYuvBuffer>
110 ScaleFrom(const VideoFrameBuffer& src, int crop_width, int crop_height) {
111 switch (src.type()) {
112 case VideoFrameBuffer::Type::kI420: {
113 rtc::scoped_refptr<I420Buffer> buffer =
114 I420Buffer::Create(crop_width, crop_height);
115 buffer->ScaleFrom(*src.GetI420());
116 return buffer;
117 }
118 case VideoFrameBuffer::Type::kI010: {
119 rtc::scoped_refptr<I010Buffer> buffer =
120 I010Buffer::Create(crop_width, crop_height);
121 buffer->ScaleFrom(*src.GetI010());
122 return buffer;
123 }
124 default:
125 RTC_NOTREACHED();
126 }
127 return nullptr;
128 }
129};
130
131rtc::scoped_refptr<PlanarYuvBuffer> CreateGradient(VideoFrameBuffer::Type type,
132 int width,
133 int height) {
134 rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height));
135 // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h
136 for (int x = 0; x < width; x++) {
137 for (int y = 0; y < height; y++) {
138 buffer->MutableDataY()[x + y * width] =
139 128 * (x * height + y * width) / (width * height);
140 }
141 }
142 int chroma_width = buffer->ChromaWidth();
143 int chroma_height = buffer->ChromaHeight();
144 for (int x = 0; x < chroma_width; x++) {
145 for (int y = 0; y < chroma_height; y++) {
146 buffer->MutableDataU()[x + y * chroma_width] =
147 255 * x / (chroma_width - 1);
148 buffer->MutableDataV()[x + y * chroma_width] =
149 255 * y / (chroma_height - 1);
150 }
151 }
152 if (type == VideoFrameBuffer::Type::kI420)
153 return buffer;
154
155 RTC_DCHECK(type == VideoFrameBuffer::Type::kI010);
156 return I010Buffer::Copy(*buffer);
157}
158
159// The offsets and sizes describe the rectangle extracted from the
160// original (gradient) frame, in relative coordinates where the
161// original frame correspond to the unit square, 0.0 <= x, y < 1.0.
162void CheckCrop(const webrtc::I420BufferInterface& frame,
163 double offset_x,
164 double offset_y,
165 double rel_width,
166 double rel_height) {
167 int width = frame.width();
168 int height = frame.height();
169 // Check that pixel values in the corners match the gradient used
170 // for initialization.
171 for (int i = 0; i < 2; i++) {
172 for (int j = 0; j < 2; j++) {
173 // Pixel coordinates of the corner.
174 int x = i * (width - 1);
175 int y = j * (height - 1);
176 // Relative coordinates, range 0.0 - 1.0 correspond to the
177 // size of the uncropped input frame.
178 double orig_x = offset_x + i * rel_width;
179 double orig_y = offset_y + j * rel_height;
180
181 EXPECT_NEAR(frame.DataY()[x + y * frame.StrideY()] / 256.0,
182 (orig_x + orig_y) / 2, 0.02);
183 EXPECT_NEAR(frame.DataU()[x / 2 + (y / 2) * frame.StrideU()] / 256.0,
184 orig_x, 0.02);
185 EXPECT_NEAR(frame.DataV()[x / 2 + (y / 2) * frame.StrideV()] / 256.0,
186 orig_y, 0.02);
187 }
188 }
189}
190
191void CheckRotate(int width,
192 int height,
193 webrtc::VideoRotation rotation,
194 const webrtc::I420BufferInterface& rotated) {
195 int rotated_width = width;
196 int rotated_height = height;
197
198 if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) {
199 std::swap(rotated_width, rotated_height);
200 }
201 EXPECT_EQ(rotated_width, rotated.width());
202 EXPECT_EQ(rotated_height, rotated.height());
203
204 // Clock-wise order (with 0,0 at top-left)
205 const struct {
206 int x;
207 int y;
208 } corners[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
209 // Corresponding corner colors of the frame produced by CreateGradient.
210 const struct {
211 int y;
212 int u;
213 int v;
214 } colors[] = {{0, 0, 0}, {127, 255, 0}, {255, 255, 255}, {127, 0, 255}};
215 int corner_offset = static_cast<int>(rotation) / 90;
216
217 for (int i = 0; i < 4; i++) {
218 int j = (i + corner_offset) % 4;
219 int x = corners[j].x * (rotated_width - 1);
220 int y = corners[j].y * (rotated_height - 1);
221 EXPECT_EQ(colors[i].y, rotated.DataY()[x + y * rotated.StrideY()]);
222 EXPECT_EQ(colors[i].u,
223 rotated.DataU()[(x / 2) + (y / 2) * rotated.StrideU()]);
224 EXPECT_EQ(colors[i].v,
225 rotated.DataV()[(x / 2) + (y / 2) * rotated.StrideV()]);
226 }
227}
228
229} // namespace
230
231TEST(TestVideoFrame, WidthHeightValues) {
232 VideoFrame frame(I420Buffer::Create(10, 10, 10, 14, 90),
233 webrtc::kVideoRotation_0,
234 789 * rtc::kNumMicrosecsPerMillisec);
235 const int valid_value = 10;
236 EXPECT_EQ(valid_value, frame.width());
237 EXPECT_EQ(valid_value, frame.height());
238 frame.set_timestamp(123u);
239 EXPECT_EQ(123u, frame.timestamp());
240 frame.set_ntp_time_ms(456);
241 EXPECT_EQ(456, frame.ntp_time_ms());
242 EXPECT_EQ(789, frame.render_time_ms());
243}
244
245TEST(TestVideoFrame, ShallowCopy) {
246 uint32_t timestamp = 1;
247 int64_t ntp_time_ms = 2;
248 int64_t timestamp_us = 3;
249 int stride_y = 15;
250 int stride_u = 10;
251 int stride_v = 10;
252 int width = 15;
253 int height = 15;
254
255 const int kSizeY = 400;
256 const int kSizeU = 100;
257 const int kSizeV = 100;
258 const VideoRotation kRotation = kVideoRotation_270;
259 uint8_t buffer_y[kSizeY];
260 uint8_t buffer_u[kSizeU];
261 uint8_t buffer_v[kSizeV];
262 memset(buffer_y, 16, kSizeY);
263 memset(buffer_u, 8, kSizeU);
264 memset(buffer_v, 4, kSizeV);
265
266 VideoFrame frame1(I420Buffer::Copy(width, height, buffer_y, stride_y,
267 buffer_u, stride_u, buffer_v, stride_v),
268 kRotation, 0);
269 frame1.set_timestamp(timestamp);
270 frame1.set_ntp_time_ms(ntp_time_ms);
271 frame1.set_timestamp_us(timestamp_us);
272 VideoFrame frame2(frame1);
273
274 EXPECT_EQ(frame1.video_frame_buffer(), frame2.video_frame_buffer());
275 rtc::scoped_refptr<I420BufferInterface> yuv1 =
276 frame1.video_frame_buffer()->GetI420();
277 rtc::scoped_refptr<I420BufferInterface> yuv2 =
278 frame2.video_frame_buffer()->GetI420();
279 EXPECT_EQ(yuv1->DataY(), yuv2->DataY());
280 EXPECT_EQ(yuv1->DataU(), yuv2->DataU());
281 EXPECT_EQ(yuv1->DataV(), yuv2->DataV());
282
283 EXPECT_EQ(frame2.timestamp(), frame1.timestamp());
284 EXPECT_EQ(frame2.ntp_time_ms(), frame1.ntp_time_ms());
285 EXPECT_EQ(frame2.timestamp_us(), frame1.timestamp_us());
286 EXPECT_EQ(frame2.rotation(), frame1.rotation());
287
288 frame2.set_timestamp(timestamp + 1);
289 frame2.set_ntp_time_ms(ntp_time_ms + 1);
290 frame2.set_timestamp_us(timestamp_us + 1);
291 frame2.set_rotation(kVideoRotation_90);
292
293 EXPECT_NE(frame2.timestamp(), frame1.timestamp());
294 EXPECT_NE(frame2.ntp_time_ms(), frame1.ntp_time_ms());
295 EXPECT_NE(frame2.timestamp_us(), frame1.timestamp_us());
296 EXPECT_NE(frame2.rotation(), frame1.rotation());
297}
298
299TEST(TestVideoFrame, TextureInitialValues) {
300 VideoFrame frame = test::FakeNativeBuffer::CreateFrame(
301 640, 480, 100, 10, webrtc::kVideoRotation_0);
302 EXPECT_EQ(640, frame.width());
303 EXPECT_EQ(480, frame.height());
304 EXPECT_EQ(100u, frame.timestamp());
305 EXPECT_EQ(10, frame.render_time_ms());
306 ASSERT_TRUE(frame.video_frame_buffer() != nullptr);
307 EXPECT_TRUE(frame.video_frame_buffer()->type() ==
308 VideoFrameBuffer::Type::kNative);
309
310 frame.set_timestamp(200);
311 EXPECT_EQ(200u, frame.timestamp());
312 frame.set_timestamp_us(20);
313 EXPECT_EQ(20, frame.timestamp_us());
314}
315
316class TestPlanarYuvBuffer
317 : public ::testing::TestWithParam<VideoFrameBuffer::Type> {};
318
319rtc::scoped_refptr<I420Buffer> CreateAndFillBuffer() {
320 auto buf = I420Buffer::Create(20, 10);
321 memset(buf->MutableDataY(), 1, 200);
322 memset(buf->MutableDataU(), 2, 50);
323 memset(buf->MutableDataV(), 3, 50);
324 return buf;
325}
326
327TEST_P(TestPlanarYuvBuffer, Copy) {
328 rtc::scoped_refptr<PlanarYuvBuffer> buf1;
329 switch (GetParam()) {
330 case VideoFrameBuffer::Type::kI420: {
331 buf1 = CreateAndFillBuffer();
332 break;
333 }
334 case VideoFrameBuffer::Type::kI010: {
335 buf1 = I010Buffer::Copy(*CreateAndFillBuffer());
336 break;
337 }
338 default:
339 RTC_NOTREACHED();
340 }
341
342 rtc::scoped_refptr<PlanarYuvBuffer> buf2 =
343 PlanarYuvBufferFactory::Copy(*buf1);
344 EXPECT_TRUE(test::FrameBufsEqual(buf1->ToI420(), buf2->ToI420()));
345}
346
347TEST_P(TestPlanarYuvBuffer, Scale) {
348 rtc::scoped_refptr<PlanarYuvBuffer> buf =
349 CreateGradient(GetParam(), 200, 100);
350
351 // Pure scaling, no cropping.
352 rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
353 PlanarYuvBufferFactory::ScaleFrom(*buf, 150, 75);
354 CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.0, 1.0, 1.0);
355}
356
357TEST_P(TestPlanarYuvBuffer, CropXCenter) {
358 rtc::scoped_refptr<PlanarYuvBuffer> buf =
359 CreateGradient(GetParam(), 200, 100);
360
361 // Pure center cropping, no scaling.
362 rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
363 PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 50, 0, 100, 100);
364 CheckCrop(*scaled_buffer->ToI420(), 0.25, 0.0, 0.5, 1.0);
365}
366
367TEST_P(TestPlanarYuvBuffer, CropXNotCenter) {
368 rtc::scoped_refptr<PlanarYuvBuffer> buf =
369 CreateGradient(GetParam(), 200, 100);
370
371 // Non-center cropping, no scaling.
372 rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
373 PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 25, 0, 100, 100);
374 CheckCrop(*scaled_buffer->ToI420(), 0.125, 0.0, 0.5, 1.0);
375}
376
377TEST_P(TestPlanarYuvBuffer, CropYCenter) {
378 rtc::scoped_refptr<PlanarYuvBuffer> buf =
379 CreateGradient(GetParam(), 100, 200);
380
381 // Pure center cropping, no scaling.
382 rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
383 PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 0, 50, 100, 100);
384 CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.25, 1.0, 0.5);
385}
386
387TEST_P(TestPlanarYuvBuffer, CropYNotCenter) {
388 rtc::scoped_refptr<PlanarYuvBuffer> buf =
389 CreateGradient(GetParam(), 100, 200);
390
391 // Pure center cropping, no scaling.
392 rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
393 PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 0, 25, 100, 100);
394 CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.125, 1.0, 0.5);
395}
396
397TEST_P(TestPlanarYuvBuffer, CropAndScale16x9) {
398 rtc::scoped_refptr<PlanarYuvBuffer> buf =
399 CreateGradient(GetParam(), 640, 480);
400
401 // Pure center cropping, no scaling.
402 rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
403 PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 320, 180);
404 CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.125, 1.0, 0.75);
405}
406
407INSTANTIATE_TEST_CASE_P(,
408 TestPlanarYuvBuffer,
409 ::testing::Values(VideoFrameBuffer::Type::kI420,
410 VideoFrameBuffer::Type::kI010));
411
412class TestPlanarYuvBufferRotate
413 : public ::testing::TestWithParam<
414 std::tuple<webrtc::VideoRotation, VideoFrameBuffer::Type>> {};
415
416TEST_P(TestPlanarYuvBufferRotate, Rotates) {
417 const webrtc::VideoRotation rotation = std::get<0>(GetParam());
418 const VideoFrameBuffer::Type type = std::get<1>(GetParam());
419 rtc::scoped_refptr<PlanarYuvBuffer> buffer = CreateGradient(type, 640, 480);
420 rtc::scoped_refptr<PlanarYuvBuffer> rotated_buffer =
421 PlanarYuvBufferFactory::Rotate(*buffer, rotation);
422 CheckRotate(640, 480, rotation, *rotated_buffer->ToI420());
423}
424
425INSTANTIATE_TEST_CASE_P(
426 Rotate,
427 TestPlanarYuvBufferRotate,
428 ::testing::Combine(::testing::Values(kVideoRotation_0,
429 kVideoRotation_90,
430 kVideoRotation_180,
431 kVideoRotation_270),
432 ::testing::Values(VideoFrameBuffer::Type::kI420,
433 VideoFrameBuffer::Type::kI010)));
434
435} // namespace webrtc