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