xref: /aosp_15_r20/external/webrtc/api/video/test/i444_buffer_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 
2 /*
3  *  Copyright (c) 2021 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/i444_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<I444BufferInterface> buf,int col,int row)22 int GetY(rtc::scoped_refptr<I444BufferInterface> buf, int col, int row) {
23   return buf->DataY()[row * buf->StrideY() + col];
24 }
25 
GetU(rtc::scoped_refptr<I444BufferInterface> buf,int col,int row)26 int GetU(rtc::scoped_refptr<I444BufferInterface> buf, int col, int row) {
27   return buf->DataU()[row * buf->StrideU() + col];
28 }
29 
GetV(rtc::scoped_refptr<I444BufferInterface> buf,int col,int row)30 int GetV(rtc::scoped_refptr<I444BufferInterface> buf, int col, int row) {
31   return buf->DataV()[row * buf->StrideV() + col];
32 }
33 
FillI444Buffer(rtc::scoped_refptr<I444Buffer> buf)34 void FillI444Buffer(rtc::scoped_refptr<I444Buffer> 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       buf->MutableDataU()[row * buf->StrideU() + col] = U;
42       buf->MutableDataV()[row * buf->StrideV() + col] = V;
43     }
44   }
45 }
46 
47 }  // namespace
48 
TEST(I444BufferTest,InitialData)49 TEST(I444BufferTest, InitialData) {
50   constexpr int stride = 3;
51   constexpr int width = 3;
52   constexpr int height = 3;
53 
54   rtc::scoped_refptr<I444Buffer> i444_buffer(I444Buffer::Create(width, height));
55   EXPECT_EQ(width, i444_buffer->width());
56   EXPECT_EQ(height, i444_buffer->height());
57   EXPECT_EQ(stride, i444_buffer->StrideY());
58   EXPECT_EQ(stride, i444_buffer->StrideU());
59   EXPECT_EQ(stride, i444_buffer->StrideV());
60   EXPECT_EQ(3, i444_buffer->ChromaWidth());
61   EXPECT_EQ(3, i444_buffer->ChromaHeight());
62 }
63 
TEST(I444BufferTest,ReadPixels)64 TEST(I444BufferTest, ReadPixels) {
65   constexpr int width = 3;
66   constexpr int height = 3;
67 
68   rtc::scoped_refptr<I444Buffer> i444_buffer(I444Buffer::Create(width, height));
69   // Y = 1, U = 2, V = 3.
70   FillI444Buffer(i444_buffer);
71   for (int row = 0; row < height; row++) {
72     for (int col = 0; col < width; col++) {
73       EXPECT_EQ(1, GetY(i444_buffer, col, row));
74       EXPECT_EQ(2, GetU(i444_buffer, col, row));
75       EXPECT_EQ(3, GetV(i444_buffer, col, row));
76     }
77   }
78 }
79 
TEST(I444BufferTest,ToI420)80 TEST(I444BufferTest, ToI420) {
81   constexpr int width = 3;
82   constexpr int height = 3;
83   constexpr int size_y = width * height;
84   constexpr int size_u = (width + 1) / 2 * (height + 1) / 2;
85   constexpr int size_v = (width + 1) / 2 * (height + 1) / 2;
86   rtc::scoped_refptr<I420Buffer> reference(I420Buffer::Create(width, height));
87   memset(reference->MutableDataY(), 8, size_y);
88   memset(reference->MutableDataU(), 4, size_u);
89   memset(reference->MutableDataV(), 2, size_v);
90 
91   rtc::scoped_refptr<I444Buffer> i444_buffer(I444Buffer::Create(width, height));
92   // Convert the reference buffer to I444.
93   memset(i444_buffer->MutableDataY(), 8, size_y);
94   memset(i444_buffer->MutableDataU(), 4, size_y);
95   memset(i444_buffer->MutableDataV(), 2, size_y);
96 
97   // Confirm YUV values are as expected.
98   for (int row = 0; row < height; row++) {
99     for (int col = 0; col < width; col++) {
100       EXPECT_EQ(8, GetY(i444_buffer, col, row));
101       EXPECT_EQ(4, GetU(i444_buffer, col, row));
102       EXPECT_EQ(2, GetV(i444_buffer, col, row));
103     }
104   }
105 
106   rtc::scoped_refptr<I420BufferInterface> i420_buffer(i444_buffer->ToI420());
107   EXPECT_EQ(height, i420_buffer->height());
108   EXPECT_EQ(width, i420_buffer->width());
109   EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer));
110 }
111 
112 }  // namespace webrtc
113