xref: /aosp_15_r20/external/webrtc/api/video/test/i210_buffer_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/i210_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 {
22 
GetY(rtc::scoped_refptr<I210BufferInterface> buf,int col,int row)23 int GetY(rtc::scoped_refptr<I210BufferInterface> buf, int col, int row) {
24   return buf->DataY()[row * buf->StrideY() + col];
25 }
26 
GetU(rtc::scoped_refptr<I210BufferInterface> buf,int col,int row)27 int GetU(rtc::scoped_refptr<I210BufferInterface> buf, int col, int row) {
28   return buf->DataU()[row * buf->StrideU() + col];
29 }
30 
GetV(rtc::scoped_refptr<I210BufferInterface> buf,int col,int row)31 int GetV(rtc::scoped_refptr<I210BufferInterface> buf, int col, int row) {
32   return buf->DataV()[row * buf->StrideV() + col];
33 }
34 
FillI210Buffer(rtc::scoped_refptr<I210Buffer> buf)35 void FillI210Buffer(rtc::scoped_refptr<I210Buffer> buf) {
36   const uint16_t Y = 4;
37   const uint16_t U = 8;
38   const uint16_t V = 16;
39   for (int row = 0; row < buf->height(); ++row) {
40     for (int col = 0; col < buf->width(); ++col) {
41       buf->MutableDataY()[row * buf->StrideY() + col] = Y;
42     }
43   }
44   for (int row = 0; row < buf->ChromaHeight(); ++row) {
45     for (int col = 0; col < buf->ChromaWidth(); ++col) {
46       buf->MutableDataU()[row * buf->StrideU() + col] = U;
47       buf->MutableDataV()[row * buf->StrideV() + col] = V;
48     }
49   }
50 }
51 
52 }  // namespace
53 
TEST(I210BufferTest,InitialData)54 TEST(I210BufferTest, InitialData) {
55   constexpr int stride = 3;
56   constexpr int halfstride = (stride + 1) >> 1;
57   constexpr int width = 3;
58   constexpr int halfwidth = (width + 1) >> 1;
59   constexpr int height = 3;
60 
61   rtc::scoped_refptr<I210Buffer> i210_buffer(I210Buffer::Create(width, height));
62   EXPECT_EQ(width, i210_buffer->width());
63   EXPECT_EQ(height, i210_buffer->height());
64   EXPECT_EQ(stride, i210_buffer->StrideY());
65   EXPECT_EQ(halfstride, i210_buffer->StrideU());
66   EXPECT_EQ(halfstride, i210_buffer->StrideV());
67   EXPECT_EQ(halfwidth, i210_buffer->ChromaWidth());
68   EXPECT_EQ(height, i210_buffer->ChromaHeight());
69 }
70 
TEST(I210BufferTest,ReadPixels)71 TEST(I210BufferTest, ReadPixels) {
72   constexpr int width = 3;
73   constexpr int halfwidth = (width + 1) >> 1;
74   constexpr int height = 3;
75 
76   rtc::scoped_refptr<I210Buffer> i210_buffer(I210Buffer::Create(width, height));
77   // Y = 4, U = 8, V = 16.
78   FillI210Buffer(i210_buffer);
79   for (int row = 0; row < height; row++) {
80     for (int col = 0; col < width; col++) {
81       EXPECT_EQ(4, GetY(i210_buffer, col, row));
82     }
83   }
84   for (int row = 0; row < height; row++) {
85     for (int col = 0; col < halfwidth; col++) {
86       EXPECT_EQ(8, GetU(i210_buffer, col, row));
87       EXPECT_EQ(16, GetV(i210_buffer, col, row));
88     }
89   }
90 }
91 
TEST(I210BufferTest,ToI420)92 TEST(I210BufferTest, ToI420) {
93   constexpr int width = 3;
94   constexpr int halfwidth = (width + 1) >> 1;
95   constexpr int height = 3;
96   constexpr int size = width * 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(), 1, size);
100   memset(reference->MutableDataU(), 2, quartersize);
101   memset(reference->MutableDataV(), 4, quartersize);
102 
103   rtc::scoped_refptr<I210Buffer> i210_buffer(I210Buffer::Create(width, height));
104   // Y = 4, U = 8, V = 16.
105   FillI210Buffer(i210_buffer);
106 
107   // Confirm YUV values are as expected.
108   for (int row = 0; row < height; row++) {
109     for (int col = 0; col < width; col++) {
110       EXPECT_EQ(4, GetY(i210_buffer, col, row));
111     }
112   }
113   for (int row = 0; row < height; row++) {
114     for (int col = 0; col < halfwidth; col++) {
115       EXPECT_EQ(8, GetU(i210_buffer, col, row));
116       EXPECT_EQ(16, GetV(i210_buffer, col, row));
117     }
118   }
119 
120   rtc::scoped_refptr<I420BufferInterface> i420_buffer(i210_buffer->ToI420());
121   EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer));
122   EXPECT_EQ(height, i420_buffer->height());
123   EXPECT_EQ(width, i420_buffer->width());
124 }
125 
126 }  // namespace webrtc
127