Move VP9 frame rate controller to separate class.

Bug: webrtc:9669
Change-Id: I6f30587778e9783182af11d2410464024918e171
Reviewed-on: https://webrtc-review.googlesource.com/96201
Commit-Queue: Sergey Silkin <ssilkin@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24487}
This commit is contained in:
Sergey Silkin
2018-08-29 01:05:26 +02:00
committed by Commit Bot
parent 5a998d7246
commit ae3144c51f
6 changed files with 230 additions and 47 deletions

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/video_coding/utility/framerate_controller.h"
#include "rtc_base/checks.h"
namespace webrtc {
FramerateController::FramerateController(float target_framerate_fps)
: min_frame_interval_ms_(0), framerate_estimator_(1000.0, 1000.0) {
SetTargetRate(target_framerate_fps);
}
void FramerateController::SetTargetRate(float target_framerate_fps) {
if (target_framerate_fps_ != target_framerate_fps) {
framerate_estimator_.Reset();
if (last_timestamp_ms_) {
framerate_estimator_.Update(1, *last_timestamp_ms_);
}
const size_t target_frame_interval_ms = 1000 / target_framerate_fps;
target_framerate_fps_ = target_framerate_fps;
min_frame_interval_ms_ = 85 * target_frame_interval_ms / 100;
}
}
float FramerateController::GetTargetRate() {
return *target_framerate_fps_;
}
void FramerateController::Reset() {
framerate_estimator_.Reset();
last_timestamp_ms_.reset();
}
bool FramerateController::DropFrame(uint32_t timestamp_ms) const {
if (timestamp_ms < last_timestamp_ms_) {
// Timestamp jumps backward. We can't make adequate drop decision. Don't
// drop this frame. Stats will be reset in AddFrame().
return false;
}
if (Rate(timestamp_ms).value_or(*target_framerate_fps_) >
target_framerate_fps_) {
return true;
}
if (last_timestamp_ms_) {
const int64_t diff_ms =
static_cast<int64_t>(timestamp_ms) - *last_timestamp_ms_;
if (diff_ms < min_frame_interval_ms_) {
return true;
}
}
return false;
}
void FramerateController::AddFrame(uint32_t timestamp_ms) {
if (timestamp_ms < last_timestamp_ms_) {
// Timestamp jumps backward.
Reset();
}
framerate_estimator_.Update(1, timestamp_ms);
last_timestamp_ms_ = timestamp_ms;
}
absl::optional<float> FramerateController::Rate(uint32_t timestamp_ms) const {
return framerate_estimator_.Rate(timestamp_ms);
}
} // namespace webrtc

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef MODULES_VIDEO_CODING_UTILITY_FRAMERATE_CONTROLLER_H_
#define MODULES_VIDEO_CODING_UTILITY_FRAMERATE_CONTROLLER_H_
#include "absl/types/optional.h"
#include "rtc_base/rate_statistics.h"
namespace webrtc {
class FramerateController {
public:
explicit FramerateController(float target_framerate_fps);
void SetTargetRate(float target_framerate_fps);
float GetTargetRate();
// Advices user to drop next frame in order to reach target framerate.
bool DropFrame(uint32_t timestamp_ms) const;
void AddFrame(uint32_t timestamp_ms);
void Reset();
private:
absl::optional<float> Rate(uint32_t timestamp_ms) const;
absl::optional<float> target_framerate_fps_;
absl::optional<uint32_t> last_timestamp_ms_;
uint32_t min_frame_interval_ms_;
RateStatistics framerate_estimator_;
};
} // namespace webrtc
#endif // MODULES_VIDEO_CODING_UTILITY_FRAMERATE_CONTROLLER_H_

View File

@ -0,0 +1,88 @@
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/video_coding/utility/framerate_controller.h"
#include "test/gtest.h"
namespace webrtc {
TEST(FramerateController, KeepTargetFramerate) {
const float input_framerate_fps = 20;
const float target_framerate_fps = 5;
const float max_abs_framerate_error_fps = target_framerate_fps * 0.1f;
const size_t input_duration_secs = 3;
const size_t num_input_frames = input_duration_secs * input_framerate_fps;
FramerateController framerate_controller(target_framerate_fps);
size_t num_dropped_frames = 0;
for (size_t frame_num = 0; frame_num < num_input_frames; ++frame_num) {
const uint32_t timestamp_ms =
static_cast<uint32_t>(1000 * frame_num / input_framerate_fps);
if (framerate_controller.DropFrame(timestamp_ms)) {
++num_dropped_frames;
} else {
framerate_controller.AddFrame(timestamp_ms);
}
}
const float output_framerate_fps =
static_cast<float>(num_input_frames - num_dropped_frames) /
input_duration_secs;
EXPECT_NEAR(output_framerate_fps, target_framerate_fps,
max_abs_framerate_error_fps);
}
TEST(FramerateController, DoNotDropAnyFramesIfTargerEqualsInput) {
const float input_framerate_fps = 30;
const size_t input_duration_secs = 3;
const size_t num_input_frames = input_duration_secs * input_framerate_fps;
FramerateController framerate_controller(input_framerate_fps);
size_t num_dropped_frames = 0;
for (size_t frame_num = 0; frame_num < num_input_frames; ++frame_num) {
const uint32_t timestamp_ms =
static_cast<uint32_t>(1000 * frame_num / input_framerate_fps);
if (framerate_controller.DropFrame(timestamp_ms)) {
++num_dropped_frames;
} else {
framerate_controller.AddFrame(timestamp_ms);
}
}
EXPECT_EQ(num_dropped_frames, 0U);
}
TEST(FramerateController, DoNotDropFrameWhenTimestampJumpsBackward) {
FramerateController framerate_controller(30);
ASSERT_FALSE(framerate_controller.DropFrame(66));
framerate_controller.AddFrame(66);
EXPECT_FALSE(framerate_controller.DropFrame(33));
}
TEST(FramerateController, DropFrameIfItIsTooCloseToPreviousFrame) {
FramerateController framerate_controller(30);
ASSERT_FALSE(framerate_controller.DropFrame(33));
framerate_controller.AddFrame(33);
EXPECT_TRUE(framerate_controller.DropFrame(34));
}
TEST(FramerateController, FrameDroppingStartsFromSecondInputFrame) {
const float input_framerate_fps = 23;
const float target_framerate_fps = 19;
const uint32_t input_frame_duration_ms =
static_cast<uint32_t>(1000 / input_framerate_fps);
FramerateController framerate_controller(target_framerate_fps);
ASSERT_FALSE(framerate_controller.DropFrame(1 * input_frame_duration_ms));
framerate_controller.AddFrame(1 * input_frame_duration_ms);
EXPECT_TRUE(framerate_controller.DropFrame(2 * input_frame_duration_ms));
}
} // namespace webrtc