i410_buffer_unittest.cc (4049B)
1 /* 2 * Copyright (c) 2023 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/i410_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 constexpr uint16_t kYValue = 4; 26 constexpr uint16_t kUValue = 8; 27 constexpr uint16_t kVValue = 16; 28 29 int GetY(scoped_refptr<I410BufferInterface> buf, int col, int row) { 30 return buf->DataY()[row * buf->StrideY() + col]; 31 } 32 33 int GetU(scoped_refptr<I410BufferInterface> buf, int col, int row) { 34 return buf->DataU()[row * buf->StrideU() + col]; 35 } 36 37 int GetV(scoped_refptr<I410BufferInterface> buf, int col, int row) { 38 return buf->DataV()[row * buf->StrideV() + col]; 39 } 40 41 void FillI410Buffer(scoped_refptr<I410Buffer> buf) { 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] = kYValue; 45 buf->MutableDataU()[row * buf->StrideU() + col] = kUValue; 46 buf->MutableDataV()[row * buf->StrideV() + col] = kVValue; 47 } 48 } 49 } 50 51 } // namespace 52 53 TEST(I410BufferTest, InitialData) { 54 constexpr int stride = 3; 55 constexpr int width = 3; 56 constexpr int height = 3; 57 58 scoped_refptr<I410Buffer> i410_buffer(I410Buffer::Create(width, height)); 59 EXPECT_EQ(width, i410_buffer->width()); 60 EXPECT_EQ(height, i410_buffer->height()); 61 EXPECT_EQ(stride, i410_buffer->StrideY()); 62 EXPECT_EQ(stride, i410_buffer->StrideU()); 63 EXPECT_EQ(stride, i410_buffer->StrideV()); 64 EXPECT_EQ(3, i410_buffer->ChromaWidth()); 65 EXPECT_EQ(3, i410_buffer->ChromaHeight()); 66 } 67 68 TEST(I410BufferTest, ReadPixels) { 69 constexpr int width = 3; 70 constexpr int height = 3; 71 72 scoped_refptr<I410Buffer> i410_buffer(I410Buffer::Create(width, height)); 73 FillI410Buffer(i410_buffer); 74 for (int row = 0; row < height; row++) { 75 for (int col = 0; col < width; col++) { 76 EXPECT_EQ(kYValue, GetY(i410_buffer, col, row)); 77 EXPECT_EQ(kUValue, GetU(i410_buffer, col, row)); 78 EXPECT_EQ(kVValue, GetV(i410_buffer, col, row)); 79 } 80 } 81 } 82 83 TEST(I410BufferTest, ToI420) { 84 // libyuv I410ToI420 only handles correctly even sizes and skips last row/col 85 // if odd. 86 constexpr int width = 4; 87 constexpr int height = 4; 88 constexpr int size_y = width * height; 89 constexpr int size_u = (width + 1) / 2 * (height + 1) / 2; 90 constexpr int size_v = (width + 1) / 2 * (height + 1) / 2; 91 scoped_refptr<I420Buffer> reference(I420Buffer::Create(width, height)); 92 // I410 is 10-bit while I420 is 8 bit, so last 2 bits would be discarded. 93 memset(reference->MutableDataY(), kYValue >> 2, size_y); 94 memset(reference->MutableDataU(), kUValue >> 2, size_u); 95 memset(reference->MutableDataV(), kVValue >> 2, size_v); 96 97 scoped_refptr<I410Buffer> i410_buffer(I410Buffer::Create(width, height)); 98 FillI410Buffer(i410_buffer); 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(kYValue, GetY(i410_buffer, col, row)); 104 EXPECT_EQ(kUValue, GetU(i410_buffer, col, row)); 105 EXPECT_EQ(kVValue, GetV(i410_buffer, col, row)); 106 } 107 } 108 109 scoped_refptr<I420BufferInterface> i420_buffer(i410_buffer->ToI420()); 110 111 // Confirm YUV values are as expected. 112 for (int row = 0; row < height; row++) { 113 for (int col = 0; col < width; col++) { 114 EXPECT_EQ(1, i420_buffer->DataY()[row * i420_buffer->StrideY() + col]); 115 } 116 } 117 118 EXPECT_EQ(height, i420_buffer->height()); 119 EXPECT_EQ(width, i420_buffer->width()); 120 EXPECT_TRUE(test::FrameBufsEqual(reference, i420_buffer)); 121 } 122 123 } // namespace webrtc