Add I444Buffer tests in video_frame_unittest.cc

@nisse suggested to add test for I444Buffer to VideoFrameBuffer test
in https://webrtc-review.googlesource.com/c/src/+/251303 and I agree that
they should exist.

Didn't get the time to add them before the change was merged, so here
they are.

I took the liberty of reworking the tests to extend support to any buffer
type based on PlanarYuvBuffer (only limited by interface method
implementations) but still retain the same functionality.

Bug: webrtc:13669
Change-Id: I67998b635e05e1403e2dc6cfe3483590a5788453
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251460
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36071}
This commit is contained in:
Stefan Mitic 2022-02-24 08:54:33 -08:00 committed by WebRTC LUCI CQ
parent ecd5ba15cb
commit 1535b0a34b

View File

@ -15,6 +15,7 @@
#include "api/video/i010_buffer.h"
#include "api/video/i420_buffer.h"
#include "api/video/i444_buffer.h"
#include "api/video/nv12_buffer.h"
#include "rtc_base/time_utils.h"
#include "test/fake_texture_frame.h"
@ -25,114 +26,11 @@ namespace webrtc {
namespace {
// Helper class to delegate calls to appropriate container.
class PlanarYuvBufferFactory {
public:
static rtc::scoped_refptr<PlanarYuvBuffer> Create(VideoFrameBuffer::Type type,
int width,
int height) {
switch (type) {
case VideoFrameBuffer::Type::kI420:
return I420Buffer::Create(width, height);
case VideoFrameBuffer::Type::kI010:
return I010Buffer::Create(width, height);
default:
RTC_DCHECK_NOTREACHED();
}
return nullptr;
}
static rtc::scoped_refptr<PlanarYuvBuffer> Copy(const VideoFrameBuffer& src) {
switch (src.type()) {
case VideoFrameBuffer::Type::kI420:
return I420Buffer::Copy(src);
case VideoFrameBuffer::Type::kI010:
return I010Buffer::Copy(*src.GetI010());
default:
RTC_DCHECK_NOTREACHED();
}
return nullptr;
}
static rtc::scoped_refptr<PlanarYuvBuffer> Rotate(const VideoFrameBuffer& src,
VideoRotation rotation) {
switch (src.type()) {
case VideoFrameBuffer::Type::kI420:
return I420Buffer::Rotate(src, rotation);
case VideoFrameBuffer::Type::kI010:
return I010Buffer::Rotate(*src.GetI010(), rotation);
default:
RTC_DCHECK_NOTREACHED();
}
return nullptr;
}
static rtc::scoped_refptr<PlanarYuvBuffer> CropAndScaleFrom(
const VideoFrameBuffer& src,
int offset_x,
int offset_y,
int crop_width,
int crop_height) {
switch (src.type()) {
case VideoFrameBuffer::Type::kI420: {
rtc::scoped_refptr<I420Buffer> buffer =
I420Buffer::Create(crop_width, crop_height);
buffer->CropAndScaleFrom(*src.GetI420(), offset_x, offset_y, crop_width,
crop_height);
return buffer;
}
case VideoFrameBuffer::Type::kI010: {
rtc::scoped_refptr<I010Buffer> buffer =
I010Buffer::Create(crop_width, crop_height);
buffer->CropAndScaleFrom(*src.GetI010(), offset_x, offset_y, crop_width,
crop_height);
return buffer;
}
default:
RTC_DCHECK_NOTREACHED();
}
return nullptr;
}
static rtc::scoped_refptr<PlanarYuvBuffer> CropAndScaleFrom(
const VideoFrameBuffer& src,
int crop_width,
int crop_height) {
const int out_width =
std::min(src.width(), crop_width * src.height() / crop_height);
const int out_height =
std::min(src.height(), crop_height * src.width() / crop_width);
return CropAndScaleFrom(src, (src.width() - out_width) / 2,
(src.height() - out_height) / 2, out_width,
out_height);
}
static rtc::scoped_refptr<PlanarYuvBuffer>
ScaleFrom(const VideoFrameBuffer& src, int crop_width, int crop_height) {
switch (src.type()) {
case VideoFrameBuffer::Type::kI420: {
rtc::scoped_refptr<I420Buffer> buffer =
I420Buffer::Create(crop_width, crop_height);
buffer->ScaleFrom(*src.GetI420());
return buffer;
}
case VideoFrameBuffer::Type::kI010: {
rtc::scoped_refptr<I010Buffer> buffer =
I010Buffer::Create(crop_width, crop_height);
buffer->ScaleFrom(*src.GetI010());
return buffer;
}
default:
RTC_DCHECK_NOTREACHED();
}
return nullptr;
}
};
rtc::scoped_refptr<PlanarYuvBuffer> CreateGradient(VideoFrameBuffer::Type type,
int width,
int height) {
rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height));
// Helper function to create a buffer and fill it with a gradient for
// PlanarYuvBuffer based buffers.
template <class T>
rtc::scoped_refptr<T> CreateGradient(int width, int height) {
rtc::scoped_refptr<T> buffer(T::Create(width, height));
// Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
@ -150,13 +48,10 @@ rtc::scoped_refptr<PlanarYuvBuffer> CreateGradient(VideoFrameBuffer::Type type,
255 * y / (chroma_height - 1);
}
}
if (type == VideoFrameBuffer::Type::kI420)
return buffer;
RTC_DCHECK(type == VideoFrameBuffer::Type::kI010);
return I010Buffer::Copy(*buffer);
}
// Helper function to create a buffer and fill it with a gradient.
rtc::scoped_refptr<NV12BufferInterface> CreateNV12Gradient(int width,
int height) {
rtc::scoped_refptr<NV12Buffer> buffer(NV12Buffer::Create(width, height));
@ -183,13 +78,16 @@ rtc::scoped_refptr<NV12BufferInterface> CreateNV12Gradient(int width,
// The offsets and sizes describe the rectangle extracted from the
// original (gradient) frame, in relative coordinates where the
// original frame correspond to the unit square, 0.0 <= x, y < 1.0.
void CheckCrop(const webrtc::I420BufferInterface& frame,
template <class T>
void CheckCrop(const T& frame,
double offset_x,
double offset_y,
double rel_width,
double rel_height) {
int width = frame.width();
int height = frame.height();
int plane_divider = frame.type() == VideoFrameBuffer::Type::kI444 ? 1 : 2;
// Check that pixel values in the corners match the gradient used
// for initialization.
for (int i = 0; i < 2; i++) {
@ -204,18 +102,23 @@ void CheckCrop(const webrtc::I420BufferInterface& frame,
EXPECT_NEAR(frame.DataY()[x + y * frame.StrideY()] / 256.0,
(orig_x + orig_y) / 2, 0.02);
EXPECT_NEAR(frame.DataU()[x / 2 + (y / 2) * frame.StrideU()] / 256.0,
EXPECT_NEAR(frame.DataU()[x / plane_divider +
(y / plane_divider) * frame.StrideU()] /
256.0,
orig_x, 0.02);
EXPECT_NEAR(frame.DataV()[x / 2 + (y / 2) * frame.StrideV()] / 256.0,
EXPECT_NEAR(frame.DataV()[x / plane_divider +
(y / plane_divider) * frame.StrideV()] /
256.0,
orig_y, 0.02);
}
}
}
template <class T>
void CheckRotate(int width,
int height,
webrtc::VideoRotation rotation,
const webrtc::I420BufferInterface& rotated) {
const T& rotated) {
int rotated_width = width;
int rotated_height = height;
@ -238,15 +141,18 @@ void CheckRotate(int width,
} colors[] = {{0, 0, 0}, {127, 255, 0}, {255, 255, 255}, {127, 0, 255}};
int corner_offset = static_cast<int>(rotation) / 90;
int plane_divider = rotated.type() == VideoFrameBuffer::Type::kI444 ? 1 : 2;
for (int i = 0; i < 4; i++) {
int j = (i + corner_offset) % 4;
int x = corners[j].x * (rotated_width - 1);
int y = corners[j].y * (rotated_height - 1);
EXPECT_EQ(colors[i].y, rotated.DataY()[x + y * rotated.StrideY()]);
EXPECT_EQ(colors[i].u,
rotated.DataU()[(x / 2) + (y / 2) * rotated.StrideU()]);
rotated.DataU()[(x / plane_divider) +
(y / plane_divider) * rotated.StrideU()]);
EXPECT_EQ(colors[i].v,
rotated.DataV()[(x / 2) + (y / 2) * rotated.StrideV()]);
rotated.DataV()[(x / plane_divider) +
(y / plane_divider) * rotated.StrideV()]);
}
}
@ -344,101 +250,142 @@ TEST(TestVideoFrame, TextureInitialValues) {
EXPECT_EQ(20, frame.timestamp_us());
}
class TestPlanarYuvBuffer
: public ::testing::TestWithParam<VideoFrameBuffer::Type> {};
template <typename T>
class TestPlanarYuvBuffer : public ::testing::Test {};
TYPED_TEST_SUITE_P(TestPlanarYuvBuffer);
rtc::scoped_refptr<I420Buffer> CreateAndFillBuffer() {
auto buf = I420Buffer::Create(20, 10);
template <class T>
rtc::scoped_refptr<T> CreateAndFillBuffer() {
auto buf = T::Create(20, 10);
memset(buf->MutableDataY(), 1, 200);
if (buf->type() == VideoFrameBuffer::Type::kI444) {
memset(buf->MutableDataU(), 2, 200);
memset(buf->MutableDataV(), 3, 200);
} else {
memset(buf->MutableDataU(), 2, 50);
memset(buf->MutableDataV(), 3, 50);
}
return buf;
}
TEST_P(TestPlanarYuvBuffer, Copy) {
rtc::scoped_refptr<PlanarYuvBuffer> buf1;
switch (GetParam()) {
case VideoFrameBuffer::Type::kI420: {
buf1 = CreateAndFillBuffer();
break;
}
case VideoFrameBuffer::Type::kI010: {
buf1 = I010Buffer::Copy(*CreateAndFillBuffer());
break;
}
default:
RTC_DCHECK_NOTREACHED();
TYPED_TEST_P(TestPlanarYuvBuffer, Copy) {
rtc::scoped_refptr<TypeParam> buf1 = CreateAndFillBuffer<TypeParam>();
rtc::scoped_refptr<TypeParam> buf2 = TypeParam::Copy(*buf1);
EXPECT_TRUE(test::FrameBufsEqual(buf1, buf2));
}
rtc::scoped_refptr<PlanarYuvBuffer> buf2 =
PlanarYuvBufferFactory::Copy(*buf1);
EXPECT_TRUE(test::FrameBufsEqual(buf1->ToI420(), buf2->ToI420()));
}
TEST_P(TestPlanarYuvBuffer, Scale) {
rtc::scoped_refptr<PlanarYuvBuffer> buf =
CreateGradient(GetParam(), 200, 100);
// Pure scaling, no cropping.
rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
PlanarYuvBufferFactory::ScaleFrom(*buf, 150, 75);
CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.0, 1.0, 1.0);
}
TEST_P(TestPlanarYuvBuffer, CropXCenter) {
rtc::scoped_refptr<PlanarYuvBuffer> buf =
CreateGradient(GetParam(), 200, 100);
TYPED_TEST_P(TestPlanarYuvBuffer, CropXCenter) {
rtc::scoped_refptr<TypeParam> buf = CreateGradient<TypeParam>(200, 100);
// Pure center cropping, no scaling.
rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 50, 0, 100, 100);
CheckCrop(*scaled_buffer->ToI420(), 0.25, 0.0, 0.5, 1.0);
rtc::scoped_refptr<TypeParam> scaled_buffer = TypeParam::Create(100, 100);
scaled_buffer->CropAndScaleFrom(*buf, 50, 0, 100, 100);
CheckCrop<TypeParam>(*scaled_buffer, 0.25, 0.0, 0.5, 1.0);
}
TEST_P(TestPlanarYuvBuffer, CropXNotCenter) {
rtc::scoped_refptr<PlanarYuvBuffer> buf =
CreateGradient(GetParam(), 200, 100);
TYPED_TEST_P(TestPlanarYuvBuffer, CropXNotCenter) {
rtc::scoped_refptr<TypeParam> buf = CreateGradient<TypeParam>(200, 100);
// Non-center cropping, no scaling.
rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 25, 0, 100, 100);
CheckCrop(*scaled_buffer->ToI420(), 0.125, 0.0, 0.5, 1.0);
rtc::scoped_refptr<TypeParam> scaled_buffer = TypeParam::Create(100, 100);
scaled_buffer->CropAndScaleFrom(*buf, 25, 0, 100, 100);
CheckCrop<TypeParam>(*scaled_buffer, 0.125, 0.0, 0.5, 1.0);
}
TEST_P(TestPlanarYuvBuffer, CropYCenter) {
rtc::scoped_refptr<PlanarYuvBuffer> buf =
CreateGradient(GetParam(), 100, 200);
TYPED_TEST_P(TestPlanarYuvBuffer, CropYCenter) {
rtc::scoped_refptr<TypeParam> buf = CreateGradient<TypeParam>(100, 200);
// Pure center cropping, no scaling.
rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 0, 50, 100, 100);
CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.25, 1.0, 0.5);
rtc::scoped_refptr<TypeParam> scaled_buffer = TypeParam::Create(100, 100);
scaled_buffer->CropAndScaleFrom(*buf, 0, 50, 100, 100);
CheckCrop<TypeParam>(*scaled_buffer, 0.0, 0.25, 1.0, 0.5);
}
TEST_P(TestPlanarYuvBuffer, CropYNotCenter) {
rtc::scoped_refptr<PlanarYuvBuffer> buf =
CreateGradient(GetParam(), 100, 200);
TYPED_TEST_P(TestPlanarYuvBuffer, CropYNotCenter) {
rtc::scoped_refptr<TypeParam> buf = CreateGradient<TypeParam>(100, 200);
// Pure center cropping, no scaling.
rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 0, 25, 100, 100);
CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.125, 1.0, 0.5);
rtc::scoped_refptr<TypeParam> scaled_buffer = TypeParam::Create(100, 100);
scaled_buffer->CropAndScaleFrom(*buf, 0, 25, 100, 100);
CheckCrop<TypeParam>(*scaled_buffer, 0.0, 0.125, 1.0, 0.5);
}
TEST_P(TestPlanarYuvBuffer, CropAndScale16x9) {
rtc::scoped_refptr<PlanarYuvBuffer> buf =
CreateGradient(GetParam(), 640, 480);
TYPED_TEST_P(TestPlanarYuvBuffer, CropAndScale16x9) {
const int buffer_width = 640;
const int buffer_height = 480;
const int crop_width = 320;
const int crop_height = 180;
rtc::scoped_refptr<TypeParam> buf = CreateGradient<TypeParam>(640, 480);
// Pure center cropping, no scaling.
rtc::scoped_refptr<PlanarYuvBuffer> scaled_buffer =
PlanarYuvBufferFactory::CropAndScaleFrom(*buf, 320, 180);
CheckCrop(*scaled_buffer->ToI420(), 0.0, 0.125, 1.0, 0.75);
const int out_width =
std::min(buffer_width, crop_width * buffer_height / crop_height);
const int out_height =
std::min(buffer_height, crop_height * buffer_width / crop_width);
rtc::scoped_refptr<TypeParam> scaled_buffer =
TypeParam::Create(out_width, out_height);
scaled_buffer->CropAndScaleFrom(*buf, (buffer_width - out_width) / 2,
(buffer_height - out_height) / 2, out_width,
out_height);
CheckCrop<TypeParam>(*scaled_buffer, 0.0, 0.125, 1.0, 0.75);
}
INSTANTIATE_TEST_SUITE_P(All,
TestPlanarYuvBuffer,
::testing::Values(VideoFrameBuffer::Type::kI420,
VideoFrameBuffer::Type::kI010));
REGISTER_TYPED_TEST_SUITE_P(TestPlanarYuvBuffer,
Copy,
CropXCenter,
CropXNotCenter,
CropYCenter,
CropYNotCenter,
CropAndScale16x9);
using TestTypesAll = ::testing::Types<I420Buffer, I010Buffer, I444Buffer>;
INSTANTIATE_TYPED_TEST_SUITE_P(All, TestPlanarYuvBuffer, TestTypesAll);
template <class T>
class TestPlanarYuvBufferScale : public ::testing::Test {};
TYPED_TEST_SUITE_P(TestPlanarYuvBufferScale);
TYPED_TEST_P(TestPlanarYuvBufferScale, Scale) {
rtc::scoped_refptr<TypeParam> buf = CreateGradient<TypeParam>(200, 100);
// Pure scaling, no cropping.
rtc::scoped_refptr<TypeParam> scaled_buffer = TypeParam::Create(150, 75);
scaled_buffer->ScaleFrom(*buf);
CheckCrop<TypeParam>(*scaled_buffer, 0.0, 0.0, 1.0, 1.0);
}
REGISTER_TYPED_TEST_SUITE_P(TestPlanarYuvBufferScale, Scale);
using TestTypesScale = ::testing::Types<I420Buffer, I010Buffer>;
INSTANTIATE_TYPED_TEST_SUITE_P(All, TestPlanarYuvBufferScale, TestTypesScale);
template <class T>
class TestPlanarYuvBufferRotate : public ::testing::Test {
public:
std::vector<webrtc::VideoRotation> RotationParams = {
kVideoRotation_0, kVideoRotation_90, kVideoRotation_180,
kVideoRotation_270};
};
TYPED_TEST_SUITE_P(TestPlanarYuvBufferRotate);
TYPED_TEST_P(TestPlanarYuvBufferRotate, Rotates) {
for (const webrtc::VideoRotation& rotation : this->RotationParams) {
rtc::scoped_refptr<TypeParam> buffer = CreateGradient<TypeParam>(640, 480);
rtc::scoped_refptr<TypeParam> rotated_buffer =
TypeParam::Rotate(*buffer, rotation);
CheckRotate(640, 480, rotation, *rotated_buffer);
}
}
REGISTER_TYPED_TEST_SUITE_P(TestPlanarYuvBufferRotate, Rotates);
using TestTypesRotate = ::testing::Types<I420Buffer, I010Buffer, I444Buffer>;
INSTANTIATE_TYPED_TEST_SUITE_P(Rotate,
TestPlanarYuvBufferRotate,
TestTypesRotate);
TEST(TestNV12Buffer, CropAndScale) {
const int kSourceWidth = 640;
@ -469,29 +416,6 @@ TEST(TestNV12Buffer, CropAndScale) {
kRelativeHeight);
}
class TestPlanarYuvBufferRotate
: public ::testing::TestWithParam<
std::tuple<webrtc::VideoRotation, VideoFrameBuffer::Type>> {};
TEST_P(TestPlanarYuvBufferRotate, Rotates) {
const webrtc::VideoRotation rotation = std::get<0>(GetParam());
const VideoFrameBuffer::Type type = std::get<1>(GetParam());
rtc::scoped_refptr<PlanarYuvBuffer> buffer = CreateGradient(type, 640, 480);
rtc::scoped_refptr<PlanarYuvBuffer> rotated_buffer =
PlanarYuvBufferFactory::Rotate(*buffer, rotation);
CheckRotate(640, 480, rotation, *rotated_buffer->ToI420());
}
INSTANTIATE_TEST_SUITE_P(
Rotate,
TestPlanarYuvBufferRotate,
::testing::Combine(::testing::Values(kVideoRotation_0,
kVideoRotation_90,
kVideoRotation_180,
kVideoRotation_270),
::testing::Values(VideoFrameBuffer::Type::kI420,
VideoFrameBuffer::Type::kI010)));
TEST(TestUpdateRect, CanCompare) {
VideoFrame::UpdateRect a = {0, 0, 100, 200};
VideoFrame::UpdateRect b = {0, 0, 100, 200};