diff --git a/test/testsupport/ivf_video_frame_generator.cc b/test/testsupport/ivf_video_frame_generator.cc index 52989b1e04..1d60a86490 100644 --- a/test/testsupport/ivf_video_frame_generator.cc +++ b/test/testsupport/ivf_video_frame_generator.cc @@ -58,8 +58,8 @@ IvfVideoFrameGenerator::IvfVideoFrameGenerator(const Environment& env, : callback_(this), file_reader_(IvfFileReader::Create(FileWrapper::OpenReadOnly(file_name))), video_decoder_(CreateDecoder(env, file_reader_->GetVideoCodecType())), - width_(file_reader_->GetFrameWidth()), - height_(file_reader_->GetFrameHeight()), + original_resolution_({.width = file_reader_->GetFrameWidth(), + .height = file_reader_->GetFrameHeight()}), fps_hint_(fps_hint) { RTC_CHECK(video_decoder_) << "No decoder found for file's video codec type"; VideoDecoder::Settings decoder_settings; @@ -110,12 +110,18 @@ FrameGeneratorInterface::VideoFrameData IvfVideoFrameGenerator::NextFrame() { MutexLock frame_lock(&frame_decode_lock_); rtc::scoped_refptr buffer = next_frame_->video_frame_buffer(); - if (width_ != static_cast(buffer->width()) || - height_ != static_cast(buffer->height())) { + + // Set original resolution to resolution of decoded frame. + original_resolution_ = {.width = static_cast(buffer->width()), + .height = static_cast(buffer->width())}; + + if (output_resolution_.has_value() && + (output_resolution_->width != original_resolution_.width || + output_resolution_->height != original_resolution_.height)) { // Video adapter has requested a down-scale. Allocate a new buffer and // return scaled version. - rtc::scoped_refptr scaled_buffer = - I420Buffer::Create(width_, height_); + rtc::scoped_refptr scaled_buffer = I420Buffer::Create( + output_resolution_->width, output_resolution_->height); scaled_buffer->ScaleFrom(*buffer->ToI420()); buffer = scaled_buffer; } @@ -139,13 +145,12 @@ void IvfVideoFrameGenerator::SkipNextFrame() { void IvfVideoFrameGenerator::ChangeResolution(size_t width, size_t height) { MutexLock lock(&lock_); - width_ = width; - height_ = height; + output_resolution_ = {.width = width, .height = height}; } FrameGeneratorInterface::Resolution IvfVideoFrameGenerator::GetResolution() const { - return {.width = width_, .height = height_}; + return output_resolution_.value_or(original_resolution_); } int32_t IvfVideoFrameGenerator::DecodedCallback::Decoded( diff --git a/test/testsupport/ivf_video_frame_generator.h b/test/testsupport/ivf_video_frame_generator.h index c5406f3333..1423ac86b1 100644 --- a/test/testsupport/ivf_video_frame_generator.h +++ b/test/testsupport/ivf_video_frame_generator.h @@ -67,8 +67,13 @@ class IvfVideoFrameGenerator : public FrameGeneratorInterface { std::unique_ptr file_reader_; std::unique_ptr video_decoder_; - size_t width_; - size_t height_; + // Resolution of IVF. Initially readed from IVF header and then set to + // resolution of decoded frame. + Resolution original_resolution_; + // Resolution of output frames. When set, the decoded frames scaled to + // `output_resolution_`. Otherwise the decoded resolution, which may vary from + // frame to frame, is preserved. + std::optional output_resolution_; std::optional fps_hint_; // This lock is used to ensure that all API method will be called diff --git a/test/testsupport/ivf_video_frame_generator_unittest.cc b/test/testsupport/ivf_video_frame_generator_unittest.cc index db7cd8fad0..100b47e765 100644 --- a/test/testsupport/ivf_video_frame_generator_unittest.cc +++ b/test/testsupport/ivf_video_frame_generator_unittest.cc @@ -221,5 +221,17 @@ TEST_F(IvfVideoFrameGeneratorTest, H264) { } #endif +TEST_F(IvfVideoFrameGeneratorTest, ScalesResolution) { + CreateTestVideoFile(VideoCodecType::kVideoCodecVP8, CreateVp8Encoder(env_)); + IvfVideoFrameGenerator generator(env_, file_name_, /*fps_hint=*/123); + generator.ChangeResolution(kWidth * 2, kHeight / 2); + rtc::scoped_refptr frame_buffer = + generator.NextFrame().buffer; + frame_buffer = generator.NextFrame().buffer; + ASSERT_TRUE(frame_buffer); + EXPECT_EQ(frame_buffer->width(), kWidth * 2); + EXPECT_EQ(frame_buffer->height(), kHeight / 2); +} + } // namespace test } // namespace webrtc