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