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