pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | */ |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame^] | 10 | #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 11 | |
| 12 | namespace webrtc { |
| 13 | |
| 14 | static const int kMinFps = 10; |
| 15 | static const int kMeasureSeconds = 5; |
| 16 | static const int kFramedropPercentThreshold = 60; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 17 | |
Peter Boström | 6a688f5 | 2015-06-22 08:02:58 +0200 | [diff] [blame] | 18 | const int QualityScaler::kDefaultLowQpDenominator = 3; |
| 19 | // Note that this is the same for width and height to permit 120x90 in both |
| 20 | // portrait and landscape mode. |
| 21 | const int QualityScaler::kDefaultMinDownscaleDimension = 90; |
| 22 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 23 | QualityScaler::QualityScaler() |
Peter Boström | 6a688f5 | 2015-06-22 08:02:58 +0200 | [diff] [blame] | 24 | : num_samples_(0), |
| 25 | low_qp_threshold_(-1), |
| 26 | downscale_shift_(0), |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 27 | framerate_down_(false), |
Peter Boström | 6a688f5 | 2015-06-22 08:02:58 +0200 | [diff] [blame] | 28 | min_width_(kDefaultMinDownscaleDimension), |
| 29 | min_height_(kDefaultMinDownscaleDimension) { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Peter Boström | 1741770 | 2015-09-25 17:03:26 +0200 | [diff] [blame] | 32 | void QualityScaler::Init(int low_qp_threshold, |
| 33 | int high_qp_threshold, |
| 34 | bool use_framerate_reduction) { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 35 | ClearSamples(); |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 36 | low_qp_threshold_ = low_qp_threshold; |
Peter Boström | 1741770 | 2015-09-25 17:03:26 +0200 | [diff] [blame] | 37 | high_qp_threshold_ = high_qp_threshold; |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 38 | use_framerate_reduction_ = use_framerate_reduction; |
| 39 | target_framerate_ = -1; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 40 | } |
| 41 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 42 | void QualityScaler::SetMinResolution(int min_width, int min_height) { |
| 43 | min_width_ = min_width; |
| 44 | min_height_ = min_height; |
| 45 | } |
| 46 | |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 47 | // Report framerate(fps) to estimate # of samples. |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 48 | void QualityScaler::ReportFramerate(int framerate) { |
| 49 | num_samples_ = static_cast<size_t>( |
| 50 | kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate)); |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 51 | framerate_ = framerate; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 52 | } |
| 53 | |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 54 | void QualityScaler::ReportQP(int qp) { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 55 | framedrop_percent_.AddSample(0); |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 56 | average_qp_.AddSample(qp); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void QualityScaler::ReportDroppedFrame() { |
| 60 | framedrop_percent_.AddSample(100); |
| 61 | } |
| 62 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 63 | void QualityScaler::OnEncodeFrame(const VideoFrame& frame) { |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 64 | // Should be set through InitEncode -> Should be set by now. |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 65 | assert(low_qp_threshold_ >= 0); |
| 66 | assert(num_samples_ > 0); |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 67 | res_.width = frame.width(); |
| 68 | res_.height = frame.height(); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 69 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 70 | // Update scale factor. |
jackychen | 5af6d47 | 2015-05-21 14:11:36 -0700 | [diff] [blame] | 71 | int avg_drop = 0; |
| 72 | int avg_qp = 0; |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 73 | |
| 74 | // When encoder consistently overshoots, framerate reduction and spatial |
| 75 | // resizing will be triggered to get a smoother video. |
Peter Boström | 1741770 | 2015-09-25 17:03:26 +0200 | [diff] [blame] | 76 | if ((framedrop_percent_.GetAverage(num_samples_, &avg_drop) && |
| 77 | avg_drop >= kFramedropPercentThreshold) || |
| 78 | (average_qp_.GetAverage(num_samples_, &avg_qp) && |
| 79 | avg_qp > high_qp_threshold_)) { |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 80 | // Reducing frame rate before spatial resolution change. |
| 81 | // Reduce frame rate only when it is above a certain number. |
| 82 | // Only one reduction is allowed for now. |
| 83 | // TODO(jackychen): Allow more than one framerate reduction. |
| 84 | if (use_framerate_reduction_ && !framerate_down_ && framerate_ >= 20) { |
| 85 | target_framerate_ = framerate_ / 2; |
| 86 | framerate_down_ = true; |
| 87 | // If frame rate has been updated, clear the buffer. We don't want |
| 88 | // spatial resolution to change right after frame rate change. |
| 89 | ClearSamples(); |
| 90 | } else { |
| 91 | AdjustScale(false); |
| 92 | } |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 93 | } else if (average_qp_.GetAverage(num_samples_, &avg_qp) && |
| 94 | avg_qp <= low_qp_threshold_) { |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 95 | if (use_framerate_reduction_ && framerate_down_) { |
| 96 | target_framerate_ = -1; |
| 97 | framerate_down_ = false; |
| 98 | ClearSamples(); |
| 99 | } else { |
| 100 | AdjustScale(true); |
| 101 | } |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 102 | } |
| 103 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 104 | assert(downscale_shift_ >= 0); |
| 105 | for (int shift = downscale_shift_; |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 106 | shift > 0 && (res_.width / 2 >= min_width_) && |
| 107 | (res_.height / 2 >= min_height_); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 108 | --shift) { |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 109 | res_.width /= 2; |
| 110 | res_.height /= 2; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 111 | } |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 112 | } |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 113 | |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 114 | QualityScaler::Resolution QualityScaler::GetScaledResolution() const { |
| 115 | return res_; |
| 116 | } |
| 117 | |
| 118 | int QualityScaler::GetTargetFramerate() const { |
| 119 | return target_framerate_; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 122 | const VideoFrame& QualityScaler::GetScaledFrame(const VideoFrame& frame) { |
jackychen | 6e2ce6e | 2015-07-13 16:26:33 -0700 | [diff] [blame] | 123 | Resolution res = GetScaledResolution(); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 124 | if (res.width == frame.width()) |
| 125 | return frame; |
| 126 | |
| 127 | scaler_.Set(frame.width(), |
| 128 | frame.height(), |
| 129 | res.width, |
| 130 | res.height, |
| 131 | kI420, |
| 132 | kI420, |
| 133 | kScaleBox); |
| 134 | if (scaler_.Scale(frame, &scaled_frame_) != 0) |
| 135 | return frame; |
| 136 | |
| 137 | scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms()); |
| 138 | scaled_frame_.set_timestamp(frame.timestamp()); |
| 139 | scaled_frame_.set_render_time_ms(frame.render_time_ms()); |
| 140 | |
| 141 | return scaled_frame_; |
| 142 | } |
| 143 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 144 | void QualityScaler::ClearSamples() { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 145 | framedrop_percent_.Reset(); |
jackychen | 98d8cf5 | 2015-05-21 11:12:02 -0700 | [diff] [blame] | 146 | average_qp_.Reset(); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void QualityScaler::AdjustScale(bool up) { |
| 150 | downscale_shift_ += up ? -1 : 1; |
| 151 | if (downscale_shift_ < 0) |
| 152 | downscale_shift_ = 0; |
| 153 | ClearSamples(); |
| 154 | } |
| 155 | |
| 156 | } // namespace webrtc |