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 | */ |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 10 | #include "webrtc/modules/video_coding/utility/include/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; |
| 17 | static const int kLowQpThresholdDenominator = 3; |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 18 | static const double kFramesizeFlucThreshold = 0.11; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 19 | |
| 20 | QualityScaler::QualityScaler() |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 21 | : num_samples_(0), low_qp_threshold_(-1), downscale_shift_(0), |
| 22 | min_width_(0), min_height_(0) { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void QualityScaler::Init(int max_qp) { |
| 26 | ClearSamples(); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 27 | low_qp_threshold_ = max_qp / kLowQpThresholdDenominator; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 28 | } |
| 29 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 30 | void QualityScaler::SetMinResolution(int min_width, int min_height) { |
| 31 | min_width_ = min_width; |
| 32 | min_height_ = min_height; |
| 33 | } |
| 34 | |
| 35 | // TODO(jackychen): target_framesize should be calculated from average bitrate |
| 36 | // in the measured period of time. |
| 37 | // Report framerate(fps) and target_bitrate(kbit/s) to estimate # of samples |
| 38 | // and get target_framesize_. |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 39 | void QualityScaler::ReportFramerate(int framerate) { |
| 40 | num_samples_ = static_cast<size_t>( |
| 41 | kMeasureSeconds * (framerate < kMinFps ? kMinFps : framerate)); |
| 42 | } |
| 43 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 44 | void QualityScaler::ReportNormalizedQP(int qp) { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 45 | framedrop_percent_.AddSample(0); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 46 | frame_quality_.AddSample(static_cast<double>(qp) / low_qp_threshold_); |
| 47 | } |
| 48 | |
| 49 | void QualityScaler::ReportNormalizedFrameSizeFluctuation( |
| 50 | double framesize_deviation) { |
| 51 | framedrop_percent_.AddSample(0); |
| 52 | frame_quality_.AddSample(framesize_deviation / kFramesizeFlucThreshold); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void QualityScaler::ReportDroppedFrame() { |
| 56 | framedrop_percent_.AddSample(100); |
| 57 | } |
| 58 | |
| 59 | QualityScaler::Resolution QualityScaler::GetScaledResolution( |
| 60 | const I420VideoFrame& frame) { |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 61 | // Should be set through InitEncode -> Should be set by now. |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 62 | assert(low_qp_threshold_ >= 0); |
| 63 | assert(num_samples_ > 0); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 64 | |
| 65 | Resolution res; |
| 66 | res.width = frame.width(); |
| 67 | res.height = frame.height(); |
| 68 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 69 | // Update scale factor. |
| 70 | int avg_drop; |
| 71 | double avg_quality; |
| 72 | if (framedrop_percent_.GetAverage(num_samples_, &avg_drop) && |
| 73 | avg_drop >= kFramedropPercentThreshold) { |
| 74 | AdjustScale(false); |
| 75 | } else if (frame_quality_.GetAverage(num_samples_, &avg_quality) && |
| 76 | avg_quality <= 1.0) { |
| 77 | AdjustScale(true); |
| 78 | } |
| 79 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 80 | assert(downscale_shift_ >= 0); |
| 81 | for (int shift = downscale_shift_; |
| 82 | shift > 0 && res.width > 1 && res.height > 1; |
| 83 | --shift) { |
| 84 | res.width >>= 1; |
| 85 | res.height >>= 1; |
| 86 | } |
| 87 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 88 | // Set this limitation for VP8 HW encoder to avoid crash. |
| 89 | if (min_width_ > 0 && res.width * res.height < min_width_ * min_height_) { |
| 90 | res.width = min_width_; |
| 91 | res.height = min_height_; |
| 92 | } |
| 93 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 94 | return res; |
| 95 | } |
| 96 | |
| 97 | const I420VideoFrame& QualityScaler::GetScaledFrame( |
| 98 | const I420VideoFrame& frame) { |
| 99 | Resolution res = GetScaledResolution(frame); |
| 100 | if (res.width == frame.width()) |
| 101 | return frame; |
| 102 | |
| 103 | scaler_.Set(frame.width(), |
| 104 | frame.height(), |
| 105 | res.width, |
| 106 | res.height, |
| 107 | kI420, |
| 108 | kI420, |
| 109 | kScaleBox); |
| 110 | if (scaler_.Scale(frame, &scaled_frame_) != 0) |
| 111 | return frame; |
| 112 | |
| 113 | scaled_frame_.set_ntp_time_ms(frame.ntp_time_ms()); |
| 114 | scaled_frame_.set_timestamp(frame.timestamp()); |
| 115 | scaled_frame_.set_render_time_ms(frame.render_time_ms()); |
| 116 | |
| 117 | return scaled_frame_; |
| 118 | } |
| 119 | |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 120 | void QualityScaler::ClearSamples() { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 121 | framedrop_percent_.Reset(); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame^] | 122 | frame_quality_.Reset(); |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void QualityScaler::AdjustScale(bool up) { |
| 126 | downscale_shift_ += up ? -1 : 1; |
| 127 | if (downscale_shift_ < 0) |
| 128 | downscale_shift_ = 0; |
| 129 | ClearSamples(); |
| 130 | } |
| 131 | |
| 132 | } // namespace webrtc |