1
2 /*
3 * Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
4 *
5 * Use of this source code is governed by a BSD-style license
6 * that can be found in the LICENSE file in the root of the source
7 * tree. An additional intellectual property rights grant can be found
8 * in the file PATENTS. All contributing project authors may
9 * be found in the AUTHORS file in the root of the source tree.
10 */
11
12 #include "api/video/i422_buffer.h"
13
14 #include "api/video/i420_buffer.h"
15 #include "test/frame_utils.h"
16 #include "test/gmock.h"
17 #include "test/gtest.h"
18
19 namespace webrtc {
20
21 namespace {
GetY(rtc::scoped_refptr<I422BufferInterface> buf,int col,int row)22 int GetY(rtc::scoped_refptr<I422BufferInterface> buf, int col, int row) {
23 return buf->DataY()[row * buf->StrideY() + col];
24 }
25
GetU(rtc::scoped_refptr<I422BufferInterface> buf,int col,int row)26 int GetU(rtc::scoped_refptr<I422BufferInterface> buf, int col, int row) {
27 return buf->DataU()[row * buf->StrideU() + col];
28 }
29
GetV(rtc::scoped_refptr<I422BufferInterface> buf,int col,int row)30 int GetV(rtc::scoped_refptr<I422BufferInterface> buf, int col, int row) {
31 return buf->DataV()[row * buf->StrideV() + col];
32 }
33
FillI422Buffer(rtc::scoped_refptr<I422Buffer> buf)34 void FillI422Buffer(rtc::scoped_refptr<I422Buffer> buf) {
35 const uint8_t Y = 1;
36 const uint8_t U = 2;
37 const uint8_t V = 3;
38 for (int row = 0; row < buf->height(); ++row) {
39 for (int col = 0; col < buf->width(); ++col) {
40 buf->MutableDataY()[row * buf->StrideY() + col] = Y;
41 }
42 }
43 for (int row = 0; row < buf->ChromaHeight(); ++row) {
44 for (int col = 0; col < buf->ChromaWidth(); ++col) {
45 buf->MutableDataU()[row * buf->StrideU() + col] = U;
46 buf->MutableDataV()[row * buf->StrideV() + col] = V;
47 }
48 }
49 }
50
51 } // namespace
52
TEST(I422BufferTest,InitialData)53 TEST(I422BufferTest, InitialData) {
54 constexpr int stride = 3;
55 constexpr int halfstride = (stride + 1) >> 1;
56 constexpr int width = 3;
57 constexpr int halfwidth = (width + 1) >> 1;
58 constexpr int height = 3;
59
60 rtc::scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height));
61 EXPECT_EQ(width, i422_buffer->width());
62 EXPECT_EQ(height, i422_buffer->height());
63 EXPECT_EQ(stride, i422_buffer->StrideY());
64 EXPECT_EQ(halfstride, i422_buffer->StrideU());
65 EXPECT_EQ(halfstride, i422_buffer->StrideV());
66 EXPECT_EQ(halfwidth, i422_buffer->ChromaWidth());
67 EXPECT_EQ(height, i422_buffer->ChromaHeight());
68 }
69
TEST(I422BufferTest,ReadPixels)70 TEST(I422BufferTest, ReadPixels) {
71 constexpr int width = 3;
72 constexpr int halfwidth = (width + 1) >> 1;
73 constexpr int height = 3;
74
75 rtc::scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height));
76 // Y = 1, U = 2, V = 3.
77 FillI422Buffer(i422_buffer);
78 for (int row = 0; row < height; row++) {
79 for (int col = 0; col < width; col++) {
80 EXPECT_EQ(1, GetY(i422_buffer, col, row));
81 }
82 }
83 for (int row = 0; row < height; row++) {
84 for (int col = 0; col < halfwidth; col++) {
85 EXPECT_EQ(2, GetU(i422_buffer, col, row));
86 EXPECT_EQ(3, GetV(i422_buffer, col, row));
87 }
88 }
89 }
90
TEST(I422BufferTest,ToI420)91 TEST(I422BufferTest, ToI420) {
92 constexpr int width = 3;
93 constexpr int halfwidth = (width + 1) >> 1;
94 constexpr int height = 3;
95 constexpr int size = width * height;
96 constexpr int halfsize = (width + 1) / 2 * height;
97 constexpr int quartersize = (width + 1) / 2 * (height + 1) / 2;
98 rtc::scoped_refptr<I420Buffer> reference(I420Buffer::Create(width, height));
99 memset(reference->MutableDataY(), 8, size);
100 memset(reference->MutableDataU(), 4, quartersize);
101 memset(reference->MutableDataV(), 2, quartersize);
102
103 rtc::scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height));
104 // Convert the reference buffer to I422.
105 memset(i422_buffer->MutableDataY(), 8, size);
106 memset(i422_buffer->MutableDataU(), 4, halfsize);
107 memset(i422_buffer->MutableDataV(), 2, halfsize);
108
109 // Confirm YUV values are as expected.
110 for (int row = 0; row < height; row++) {
111 for (int col = 0; col < width; col++) {
112 EXPECT_EQ(8, GetY(i422_buffer, col, row));
113 }
114 }
115 for (int row = 0; row < height; row++) {
116 for (int col = 0; col < halfwidth; col++) {
117 EXPECT_EQ(4, GetU(i422_buffer, col, row));
118 EXPECT_EQ(2, GetV(i422_buffer, col, row));
119 }
120 }
121
122 rtc::scoped_refptr<I420BufferInterface> i420_buffer(i422_buffer->ToI420());
123 EXPECT_EQ(height, i420_buffer->height());
124 EXPECT_EQ(width, i420_buffer->width());
125 EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer));
126 }
127
128 } // namespace webrtc
129