i422_buffer_unittest.cc (4271B)
1 /* 2 * Copyright (c) 2022 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 "api/video/i422_buffer.h" 12 13 #include <cstdint> 14 #include <cstring> 15 16 #include "api/scoped_refptr.h" 17 #include "api/video/i420_buffer.h" 18 #include "api/video/video_frame_buffer.h" 19 #include "test/frame_utils.h" 20 #include "test/gtest.h" 21 22 namespace webrtc { 23 24 namespace { 25 int GetY(scoped_refptr<I422BufferInterface> buf, int col, int row) { 26 return buf->DataY()[row * buf->StrideY() + col]; 27 } 28 29 int GetU(scoped_refptr<I422BufferInterface> buf, int col, int row) { 30 return buf->DataU()[row * buf->StrideU() + col]; 31 } 32 33 int GetV(scoped_refptr<I422BufferInterface> buf, int col, int row) { 34 return buf->DataV()[row * buf->StrideV() + col]; 35 } 36 37 void FillI422Buffer(scoped_refptr<I422Buffer> buf) { 38 const uint8_t Y = 1; 39 const uint8_t U = 2; 40 const uint8_t V = 3; 41 for (int row = 0; row < buf->height(); ++row) { 42 for (int col = 0; col < buf->width(); ++col) { 43 buf->MutableDataY()[row * buf->StrideY() + col] = Y; 44 } 45 } 46 for (int row = 0; row < buf->ChromaHeight(); ++row) { 47 for (int col = 0; col < buf->ChromaWidth(); ++col) { 48 buf->MutableDataU()[row * buf->StrideU() + col] = U; 49 buf->MutableDataV()[row * buf->StrideV() + col] = V; 50 } 51 } 52 } 53 54 } // namespace 55 56 TEST(I422BufferTest, InitialData) { 57 constexpr int stride = 3; 58 constexpr int halfstride = (stride + 1) >> 1; 59 constexpr int width = 3; 60 constexpr int halfwidth = (width + 1) >> 1; 61 constexpr int height = 3; 62 63 scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height)); 64 EXPECT_EQ(width, i422_buffer->width()); 65 EXPECT_EQ(height, i422_buffer->height()); 66 EXPECT_EQ(stride, i422_buffer->StrideY()); 67 EXPECT_EQ(halfstride, i422_buffer->StrideU()); 68 EXPECT_EQ(halfstride, i422_buffer->StrideV()); 69 EXPECT_EQ(halfwidth, i422_buffer->ChromaWidth()); 70 EXPECT_EQ(height, i422_buffer->ChromaHeight()); 71 } 72 73 TEST(I422BufferTest, ReadPixels) { 74 constexpr int width = 3; 75 constexpr int halfwidth = (width + 1) >> 1; 76 constexpr int height = 3; 77 78 scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height)); 79 // Y = 1, U = 2, V = 3. 80 FillI422Buffer(i422_buffer); 81 for (int row = 0; row < height; row++) { 82 for (int col = 0; col < width; col++) { 83 EXPECT_EQ(1, GetY(i422_buffer, col, row)); 84 } 85 } 86 for (int row = 0; row < height; row++) { 87 for (int col = 0; col < halfwidth; col++) { 88 EXPECT_EQ(2, GetU(i422_buffer, col, row)); 89 EXPECT_EQ(3, GetV(i422_buffer, col, row)); 90 } 91 } 92 } 93 94 TEST(I422BufferTest, ToI420) { 95 constexpr int width = 3; 96 constexpr int halfwidth = (width + 1) >> 1; 97 constexpr int height = 3; 98 constexpr int size = width * height; 99 constexpr int halfsize = (width + 1) / 2 * height; 100 constexpr int quartersize = (width + 1) / 2 * (height + 1) / 2; 101 scoped_refptr<I420Buffer> reference(I420Buffer::Create(width, height)); 102 memset(reference->MutableDataY(), 8, size); 103 memset(reference->MutableDataU(), 4, quartersize); 104 memset(reference->MutableDataV(), 2, quartersize); 105 106 scoped_refptr<I422Buffer> i422_buffer(I422Buffer::Create(width, height)); 107 // Convert the reference buffer to I422. 108 memset(i422_buffer->MutableDataY(), 8, size); 109 memset(i422_buffer->MutableDataU(), 4, halfsize); 110 memset(i422_buffer->MutableDataV(), 2, halfsize); 111 112 // Confirm YUV values are as expected. 113 for (int row = 0; row < height; row++) { 114 for (int col = 0; col < width; col++) { 115 EXPECT_EQ(8, GetY(i422_buffer, col, row)); 116 } 117 } 118 for (int row = 0; row < height; row++) { 119 for (int col = 0; col < halfwidth; col++) { 120 EXPECT_EQ(4, GetU(i422_buffer, col, row)); 121 EXPECT_EQ(2, GetV(i422_buffer, col, row)); 122 } 123 } 124 125 scoped_refptr<I420BufferInterface> i420_buffer(i422_buffer->ToI420()); 126 EXPECT_EQ(height, i420_buffer->height()); 127 EXPECT_EQ(width, i420_buffer->width()); 128 EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer)); 129 } 130 131 } // namespace webrtc