|
15 | 15 |
|
16 | 16 | #include "api/video/i010_buffer.h" |
17 | 17 | #include "api/video/i420_buffer.h" |
| 18 | +#include "api/video/nv12_buffer.h" |
18 | 19 | #include "rtc_base/bind.h" |
19 | 20 | #include "rtc_base/time_utils.h" |
20 | 21 | #include "test/fake_texture_frame.h" |
@@ -157,6 +158,29 @@ rtc::scoped_refptr<PlanarYuvBuffer> CreateGradient(VideoFrameBuffer::Type type, |
157 | 158 | return I010Buffer::Copy(*buffer); |
158 | 159 | } |
159 | 160 |
|
| 161 | +rtc::scoped_refptr<NV12BufferInterface> CreateNV12Gradient(int width, |
| 162 | + int height) { |
| 163 | + rtc::scoped_refptr<NV12Buffer> buffer(NV12Buffer::Create(width, height)); |
| 164 | + // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h |
| 165 | + for (int x = 0; x < width; x++) { |
| 166 | + for (int y = 0; y < height; y++) { |
| 167 | + buffer->MutableDataY()[x + y * width] = |
| 168 | + 128 * (x * height + y * width) / (width * height); |
| 169 | + } |
| 170 | + } |
| 171 | + int chroma_width = buffer->ChromaWidth(); |
| 172 | + int chroma_height = buffer->ChromaHeight(); |
| 173 | + for (int x = 0; x < chroma_width; x++) { |
| 174 | + for (int y = 0; y < chroma_height; y++) { |
| 175 | + buffer->MutableDataUV()[x * 2 + y * buffer->StrideUV()] = |
| 176 | + 255 * x / (chroma_width - 1); |
| 177 | + buffer->MutableDataUV()[x * 2 + 1 + y * buffer->StrideUV()] = |
| 178 | + 255 * y / (chroma_height - 1); |
| 179 | + } |
| 180 | + } |
| 181 | + return buffer; |
| 182 | +} |
| 183 | + |
160 | 184 | // The offsets and sizes describe the rectangle extracted from the |
161 | 185 | // original (gradient) frame, in relative coordinates where the |
162 | 186 | // original frame correspond to the unit square, 0.0 <= x, y < 1.0. |
@@ -495,6 +519,35 @@ INSTANTIATE_TEST_SUITE_P(All, |
495 | 519 | ::testing::Values(VideoFrameBuffer::Type::kI420, |
496 | 520 | VideoFrameBuffer::Type::kI010)); |
497 | 521 |
|
| 522 | +TEST(TestNV12Buffer, CropAndScale) { |
| 523 | + const int kSourceWidth = 640; |
| 524 | + const int kSourceHeight = 480; |
| 525 | + const int kScaledWidth = 320; |
| 526 | + const int kScaledHeight = 240; |
| 527 | + const int kCropLeft = 40; |
| 528 | + const int kCropTop = 30; |
| 529 | + const int kCropRight = 0; |
| 530 | + const int kCropBottom = 30; |
| 531 | + |
| 532 | + rtc::scoped_refptr<VideoFrameBuffer> buf = |
| 533 | + CreateNV12Gradient(kSourceWidth, kSourceHeight); |
| 534 | + |
| 535 | + rtc::scoped_refptr<VideoFrameBuffer> scaled_buffer = buf->CropAndScale( |
| 536 | + kCropLeft, kCropTop, kSourceWidth - kCropLeft - kCropRight, |
| 537 | + kSourceHeight - kCropTop - kCropBottom, kScaledWidth, kScaledHeight); |
| 538 | + |
| 539 | + // Parameters to CheckCrop indicate what part of the source frame is in the |
| 540 | + // scaled frame. |
| 541 | + const float kOffsetX = (kCropLeft + 0.0) / kSourceWidth; |
| 542 | + const float kOffsetY = (kCropTop + 0.0) / kSourceHeight; |
| 543 | + const float kRelativeWidth = |
| 544 | + (kSourceWidth - kCropLeft - kCropRight + 0.0) / kSourceWidth; |
| 545 | + const float kRelativeHeight = |
| 546 | + (kSourceHeight - kCropTop - kCropBottom + 0.0) / kSourceHeight; |
| 547 | + CheckCrop(*scaled_buffer->ToI420(), kOffsetX, kOffsetY, kRelativeWidth, |
| 548 | + kRelativeHeight); |
| 549 | +} |
| 550 | + |
498 | 551 | class TestPlanarYuvBufferRotate |
499 | 552 | : public ::testing::TestWithParam< |
500 | 553 | std::tuple<webrtc::VideoRotation, VideoFrameBuffer::Type>> {}; |
|
0 commit comments