blob: 764b5ed8e37e3238c60996c14327c1a4db62137e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
marpan@webrtc.org9d76b4e2012-02-28 23:39:312 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 */
10
11#ifndef WEBRTC_MODULES_VIDEO_CODING_QM_SELECT_H_
12#define WEBRTC_MODULES_VIDEO_CODING_QM_SELECT_H_
13
pbos@webrtc.orga4407322013-07-16 12:32:0514#include "webrtc/common_types.h"
15#include "webrtc/typedefs.h"
marpan@webrtc.org9d76b4e2012-02-28 23:39:3116
marpan@google.com86548c62011-07-12 17:12:5717/******************************************************/
18/* Quality Modes: Resolution and Robustness settings */
19/******************************************************/
niklase@google.com470e71d2011-07-07 08:21:2520
marpan@webrtc.org9d76b4e2012-02-28 23:39:3121namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:2522struct VideoContentMetrics;
23
marpan@webrtc.org9d76b4e2012-02-28 23:39:3124struct VCMResolutionScale {
25 VCMResolutionScale()
marpan@webrtc.orge22d81c2012-03-20 18:21:5326 : codec_width(640),
27 codec_height(480),
28 frame_rate(30.0f),
29 spatial_width_fact(1.0f),
marpan@webrtc.orgaccf6072012-03-07 17:16:1030 spatial_height_fact(1.0f),
31 temporal_fact(1.0f),
marpan@webrtc.orge22d81c2012-03-20 18:21:5332 change_resolution_spatial(false),
philipel9d3ab612015-12-21 12:12:3933 change_resolution_temporal(false) {}
marpan@webrtc.orge22d81c2012-03-20 18:21:5334 uint16_t codec_width;
35 uint16_t codec_height;
36 float frame_rate;
marpan@webrtc.orgaccf6072012-03-07 17:16:1037 float spatial_width_fact;
38 float spatial_height_fact;
39 float temporal_fact;
marpan@webrtc.orge22d81c2012-03-20 18:21:5340 bool change_resolution_spatial;
41 bool change_resolution_temporal;
niklase@google.com470e71d2011-07-07 08:21:2542};
43
marpan@webrtc.orgaccf6072012-03-07 17:16:1044enum ImageType {
philipel9d3ab612015-12-21 12:12:3945 kQCIF = 0, // 176x144
46 kHCIF, // 264x216 = half(~3/4x3/4) CIF.
47 kQVGA, // 320x240 = quarter VGA.
48 kCIF, // 352x288
49 kHVGA, // 480x360 = half(~3/4x3/4) VGA.
50 kVGA, // 640x480
51 kQFULLHD, // 960x540 = quarter FULLHD, and half(~3/4x3/4) WHD.
52 kWHD, // 1280x720
53 kFULLHD, // 1920x1080
marpan@webrtc.orgaccf6072012-03-07 17:16:1054 kNumImageTypes
55};
56
philipel9d3ab612015-12-21 12:12:3957const uint32_t kSizeOfImageType[kNumImageTypes] = {
58 25344, 57024, 76800, 101376, 172800, 307200, 518400, 921600, 2073600};
marpan@webrtc.orgaccf6072012-03-07 17:16:1059
marpan@webrtc.orge22d81c2012-03-20 18:21:5360enum FrameRateLevelClass {
61 kFrameRateLow,
62 kFrameRateMiddle1,
63 kFrameRateMiddle2,
64 kFrameRateHigh
65};
66
philipel9d3ab612015-12-21 12:12:3967enum ContentLevelClass { kLow, kHigh, kDefault };
niklase@google.com470e71d2011-07-07 08:21:2568
marpan@webrtc.org9d76b4e2012-02-28 23:39:3169struct VCMContFeature {
philipel9d3ab612015-12-21 12:12:3970 VCMContFeature() : value(0.0f), level(kDefault) {}
marpan@webrtc.org9d76b4e2012-02-28 23:39:3171 void Reset() {
72 value = 0.0f;
73 level = kDefault;
74 }
75 float value;
marpan@webrtc.orge22d81c2012-03-20 18:21:5376 ContentLevelClass level;
marpan@webrtc.org9d76b4e2012-02-28 23:39:3177};
niklase@google.com470e71d2011-07-07 08:21:2578
philipel9d3ab612015-12-21 12:12:3979enum UpDownAction { kUpResolution, kDownResolution };
niklase@google.com470e71d2011-07-07 08:21:2580
marpan@webrtc.orgaccf6072012-03-07 17:16:1081enum SpatialAction {
82 kNoChangeSpatial,
philipel9d3ab612015-12-21 12:12:3983 kOneHalfSpatialUniform, // 3/4 x 3/4: 9/6 ~1/2 pixel reduction.
84 kOneQuarterSpatialUniform, // 1/2 x 1/2: 1/4 pixel reduction.
marpan@webrtc.orgaccf6072012-03-07 17:16:1085 kNumModesSpatial
86};
87
88enum TemporalAction {
89 kNoChangeTemporal,
philipel9d3ab612015-12-21 12:12:3990 kTwoThirdsTemporal, // 2/3 frame rate reduction
91 kOneHalfTemporal, // 1/2 frame rate reduction
marpan@webrtc.orgaccf6072012-03-07 17:16:1092 kNumModesTemporal
93};
94
95struct ResolutionAction {
philipel9d3ab612015-12-21 12:12:3996 ResolutionAction() : spatial(kNoChangeSpatial), temporal(kNoChangeTemporal) {}
marpan@webrtc.orgaccf6072012-03-07 17:16:1097 SpatialAction spatial;
98 TemporalAction temporal;
99};
100
101// Down-sampling factors for spatial (width and height), and temporal.
philipel9d3ab612015-12-21 12:12:39102const float kFactorWidthSpatial[kNumModesSpatial] = {1.0f, 4.0f / 3.0f, 2.0f};
marpan@webrtc.orgaccf6072012-03-07 17:16:10103
philipel9d3ab612015-12-21 12:12:39104const float kFactorHeightSpatial[kNumModesSpatial] = {1.0f, 4.0f / 3.0f, 2.0f};
marpan@webrtc.orgaccf6072012-03-07 17:16:10105
philipel9d3ab612015-12-21 12:12:39106const float kFactorTemporal[kNumModesTemporal] = {1.0f, 1.5f, 2.0f};
marpan@webrtc.orgaccf6072012-03-07 17:16:10107
marpan@webrtc.org9d76b4e2012-02-28 23:39:31108enum EncoderState {
109 kStableEncoding, // Low rate mis-match, stable buffer levels.
110 kStressedEncoding, // Significant over-shooting of target rate,
111 // Buffer under-flow, etc.
112 kEasyEncoding // Significant under-shooting of target rate.
niklase@google.com470e71d2011-07-07 08:21:25113};
114
marpan@google.com86548c62011-07-12 17:12:57115// QmMethod class: main class for resolution and robustness settings
116
marpan@webrtc.org9d76b4e2012-02-28 23:39:31117class VCMQmMethod {
118 public:
119 VCMQmMethod();
120 virtual ~VCMQmMethod();
niklase@google.com470e71d2011-07-07 08:21:25121
marpan@webrtc.org9d76b4e2012-02-28 23:39:31122 // Reset values
123 void ResetQM();
124 virtual void Reset() = 0;
niklase@google.com470e71d2011-07-07 08:21:25125
marpan@webrtc.org9d76b4e2012-02-28 23:39:31126 // Compute content class.
127 uint8_t ComputeContentClass();
niklase@google.com470e71d2011-07-07 08:21:25128
marpan@webrtc.org9d76b4e2012-02-28 23:39:31129 // Update with the content metrics.
marpan@webrtc.orgaccf6072012-03-07 17:16:10130 void UpdateContent(const VideoContentMetrics* content_metrics);
niklase@google.com470e71d2011-07-07 08:21:25131
marpan@webrtc.org9d76b4e2012-02-28 23:39:31132 // Compute spatial texture magnitude and level.
133 // Spatial texture is a spatial prediction error measure.
134 void ComputeSpatial();
marpan@webrtc.orgbd5648f2012-02-17 23:16:58135
marpan@webrtc.org9d76b4e2012-02-28 23:39:31136 // Compute motion magnitude and level for NFD metric.
137 // NFD is normalized frame difference (normalized by spatial variance).
138 void ComputeMotionNFD();
niklase@google.com470e71d2011-07-07 08:21:25139
marpan@webrtc.org9d76b4e2012-02-28 23:39:31140 // Get the imageType (CIF, VGA, HD, etc) for the system width/height.
marpan@webrtc.orgaccf6072012-03-07 17:16:10141 ImageType GetImageType(uint16_t width, uint16_t height);
142
143 // Return the closest image type.
144 ImageType FindClosestImageType(uint16_t width, uint16_t height);
marpan@webrtc.orgbd5648f2012-02-17 23:16:58145
marpan@webrtc.org9d76b4e2012-02-28 23:39:31146 // Get the frame rate level.
marpan@webrtc.orge22d81c2012-03-20 18:21:53147 FrameRateLevelClass FrameRateLevel(float frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25148
marpan@webrtc.org9d76b4e2012-02-28 23:39:31149 protected:
150 // Content Data.
marpan@webrtc.orgaccf6072012-03-07 17:16:10151 const VideoContentMetrics* content_metrics_;
niklase@google.com470e71d2011-07-07 08:21:25152
marpan@webrtc.org9d76b4e2012-02-28 23:39:31153 // Encoder frame sizes and native frame sizes.
marpan@webrtc.orgaccf6072012-03-07 17:16:10154 uint16_t width_;
155 uint16_t height_;
marpan@webrtc.orge22d81c2012-03-20 18:21:53156 float user_frame_rate_;
marpan@webrtc.orgaccf6072012-03-07 17:16:10157 uint16_t native_width_;
158 uint16_t native_height_;
marpan@webrtc.orge22d81c2012-03-20 18:21:53159 float native_frame_rate_;
marpan@webrtc.orgaccf6072012-03-07 17:16:10160 float aspect_ratio_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31161 // Image type and frame rate leve, for the current encoder resolution.
marpan@webrtc.orgaccf6072012-03-07 17:16:10162 ImageType image_type_;
marpan@webrtc.orge22d81c2012-03-20 18:21:53163 FrameRateLevelClass framerate_level_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31164 // Content class data.
marpan@webrtc.orgaccf6072012-03-07 17:16:10165 VCMContFeature motion_;
166 VCMContFeature spatial_;
167 uint8_t content_class_;
168 bool init_;
marpan@google.com86548c62011-07-12 17:12:57169};
170
171// Resolution settings class
172
marpan@webrtc.org9d76b4e2012-02-28 23:39:31173class VCMQmResolution : public VCMQmMethod {
174 public:
175 VCMQmResolution();
176 virtual ~VCMQmResolution();
marpan@google.com86548c62011-07-12 17:12:57177
marpan@webrtc.org9d76b4e2012-02-28 23:39:31178 // Reset all quantities.
179 virtual void Reset();
marpan@google.com86548c62011-07-12 17:12:57180
marpan@webrtc.org9d76b4e2012-02-28 23:39:31181 // Reset rate quantities and counters after every SelectResolution() call.
182 void ResetRates();
marpan@google.com86548c62011-07-12 17:12:57183
marpan@webrtc.org9d76b4e2012-02-28 23:39:31184 // Reset down-sampling state.
185 void ResetDownSamplingState();
marpan@google.com86548c62011-07-12 17:12:57186
marpan@webrtc.org9d76b4e2012-02-28 23:39:31187 // Get the encoder state.
188 EncoderState GetEncoderState();
marpan@google.com86548c62011-07-12 17:12:57189
marpan@webrtc.org9d76b4e2012-02-28 23:39:31190 // Initialize after SetEncodingData in media_opt.
marpan@webrtc.orgaccf6072012-03-07 17:16:10191 int Initialize(float bitrate,
192 float user_framerate,
193 uint16_t width,
194 uint16_t height,
195 int num_layers);
marpan@google.com86548c62011-07-12 17:12:57196
marpan@webrtc.org9d76b4e2012-02-28 23:39:31197 // Update the encoder frame size.
marpan@webrtc.orge22d81c2012-03-20 18:21:53198 void UpdateCodecParameters(float frame_rate, uint16_t width, uint16_t height);
marpan@google.com86548c62011-07-12 17:12:57199
marpan@webrtc.org9d76b4e2012-02-28 23:39:31200 // Update with actual bit rate (size of the latest encoded frame)
201 // and frame type, after every encoded frame.
pbos@webrtc.org273a4142014-12-01 15:23:21202 void UpdateEncodedSize(size_t encoded_size);
marpan@google.com86548c62011-07-12 17:12:57203
marpan@webrtc.org9d76b4e2012-02-28 23:39:31204 // Update with new target bitrate, actual encoder sent rate, frame_rate,
205 // loss rate: every ~1 sec from SetTargetRates in media_opt.
marpan@webrtc.orgaccf6072012-03-07 17:16:10206 void UpdateRates(float target_bitrate,
207 float encoder_sent_rate,
208 float incoming_framerate,
209 uint8_t packet_loss);
marpan@google.com86548c62011-07-12 17:12:57210
marpan@webrtc.org9d76b4e2012-02-28 23:39:31211 // Extract ST (spatio-temporal) resolution action.
212 // Inputs: qm: Reference to the quality modes pointer.
213 // Output: the spatial and/or temporal scale change.
214 int SelectResolution(VCMResolutionScale** qm);
niklase@google.com470e71d2011-07-07 08:21:25215
marpan@webrtc.orge22d81c2012-03-20 18:21:53216 private:
marpan@webrtc.orgaccf6072012-03-07 17:16:10217 // Set the default resolution action.
218 void SetDefaultAction();
219
marpan@webrtc.org9d76b4e2012-02-28 23:39:31220 // Compute rates for the selection of down-sampling action.
221 void ComputeRatesForSelection();
niklase@google.com470e71d2011-07-07 08:21:25222
marpan@webrtc.org9d76b4e2012-02-28 23:39:31223 // Compute the encoder state.
224 void ComputeEncoderState();
niklase@google.com470e71d2011-07-07 08:21:25225
marpan@webrtc.org9d76b4e2012-02-28 23:39:31226 // Return true if the action is to go back up in resolution.
227 bool GoingUpResolution();
228
229 // Return true if the action is to go down in resolution.
230 bool GoingDownResolution();
231
232 // Check the condition for going up in resolution by the scale factors:
233 // |facWidth|, |facHeight|, |facTemp|.
234 // |scaleFac| is a scale factor for the transition rate.
marpan@webrtc.orgaccf6072012-03-07 17:16:10235 bool ConditionForGoingUp(float fac_width,
236 float fac_height,
237 float fac_temp,
238 float scale_fac);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31239
240 // Get the bitrate threshold for the resolution action.
241 // The case |facWidth|=|facHeight|=|facTemp|==1 is for down-sampling action.
242 // |scaleFac| is a scale factor for the transition rate.
marpan@webrtc.orgaccf6072012-03-07 17:16:10243 float GetTransitionRate(float fac_width,
244 float fac_height,
245 float fac_temp,
246 float scale_fac);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31247
marpan@webrtc.orgaccf6072012-03-07 17:16:10248 // Update the down-sampling state.
249 void UpdateDownsamplingState(UpDownAction up_down);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31250
marpan@webrtc.orge22d81c2012-03-20 18:21:53251 // Update the codec frame size and frame rate.
252 void UpdateCodecResolution();
253
marpan@webrtc.orgaccf6072012-03-07 17:16:10254 // Return a state based on average target rate relative transition rate.
255 uint8_t RateClass(float transition_rate);
256
257 // Adjust the action selected from the table.
marpan@webrtc.org9d76b4e2012-02-28 23:39:31258 void AdjustAction();
259
marpan@webrtc.orge22d81c2012-03-20 18:21:53260 // Covert 2 stages of 3/4 (=9/16) spatial decimation to 1/2.
261 void ConvertSpatialFractionalToWhole();
262
marpan@webrtc.orgc5b392e2012-06-29 21:44:55263 // Returns true if the new frame sizes, under the selected spatial action,
264 // are of even size.
265 bool EvenFrameSize();
marpan@webrtc.orgaccf6072012-03-07 17:16:10266
267 // Insert latest down-sampling action into the history list.
268 void InsertLatestDownAction();
269
270 // Remove the last (first element) down-sampling action from the list.
271 void RemoveLastDownAction();
272
273 // Check constraints on the amount of down-sampling allowed.
274 void ConstrainAmountOfDownSampling();
275
276 // For going up in resolution: pick spatial or temporal action,
277 // if both actions were separately selected.
278 void PickSpatialOrTemporal();
279
marpan@webrtc.org9d76b4e2012-02-28 23:39:31280 // Select the directional (1x2 or 2x1) spatial down-sampling action.
marpan@webrtc.orgaccf6072012-03-07 17:16:10281 void SelectSpatialDirectionMode(float transition_rate);
marpan@webrtc.org9d76b4e2012-02-28 23:39:31282
philipel9d3ab612015-12-21 12:12:39283 enum { kDownActionHistorySize = 10 };
marpan@webrtc.orgaccf6072012-03-07 17:16:10284
285 VCMResolutionScale* qm_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31286 // Encoder rate control parameters.
marpan@webrtc.orgaccf6072012-03-07 17:16:10287 float target_bitrate_;
marpan@webrtc.orgaccf6072012-03-07 17:16:10288 float incoming_framerate_;
289 float per_frame_bandwidth_;
290 float buffer_level_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31291
292 // Data accumulated every ~1sec from MediaOpt.
marpan@webrtc.orgaccf6072012-03-07 17:16:10293 float sum_target_rate_;
294 float sum_incoming_framerate_;
295 float sum_rate_MM_;
296 float sum_rate_MM_sgn_;
297 float sum_packet_loss_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31298 // Counters.
marpan@webrtc.orgaccf6072012-03-07 17:16:10299 uint32_t frame_cnt_;
300 uint32_t frame_cnt_delta_;
301 uint32_t update_rate_cnt_;
302 uint32_t low_buffer_cnt_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31303
304 // Resolution state parameters.
marpan@webrtc.orgaccf6072012-03-07 17:16:10305 float state_dec_factor_spatial_;
306 float state_dec_factor_temporal_;
marpan@webrtc.org9d76b4e2012-02-28 23:39:31307
308 // Quantities used for selection.
marpan@webrtc.orgaccf6072012-03-07 17:16:10309 float avg_target_rate_;
310 float avg_incoming_framerate_;
311 float avg_ratio_buffer_low_;
312 float avg_rate_mismatch_;
313 float avg_rate_mismatch_sgn_;
314 float avg_packet_loss_;
315 EncoderState encoder_state_;
316 ResolutionAction action_;
317 // Short history of the down-sampling actions from the Initialize() state.
318 // This is needed for going up in resolution. Since the total amount of
319 // down-sampling actions are constrained, the length of the list need not be
320 // large: i.e., (4/3) ^{kDownActionHistorySize} <= kMaxDownSample.
321 ResolutionAction down_action_history_[kDownActionHistorySize];
322 int num_layers_;
marpan@google.com86548c62011-07-12 17:12:57323};
niklase@google.com470e71d2011-07-07 08:21:25324
marpan@webrtc.org9d76b4e2012-02-28 23:39:31325// Robustness settings class.
niklase@google.com470e71d2011-07-07 08:21:25326
marpan@webrtc.org9d76b4e2012-02-28 23:39:31327class VCMQmRobustness : public VCMQmMethod {
328 public:
329 VCMQmRobustness();
330 ~VCMQmRobustness();
niklase@google.com470e71d2011-07-07 08:21:25331
marpan@webrtc.org9d76b4e2012-02-28 23:39:31332 virtual void Reset();
niklase@google.com470e71d2011-07-07 08:21:25333
marpan@webrtc.org9d76b4e2012-02-28 23:39:31334 // Adjust FEC rate based on content: every ~1 sec from SetTargetRates.
335 // Returns an adjustment factor.
marpan@webrtc.orgaccf6072012-03-07 17:16:10336 float AdjustFecFactor(uint8_t code_rate_delta,
337 float total_rate,
338 float framerate,
pkasting@chromium.org16825b12015-01-12 21:51:21339 int64_t rtt_time,
marpan@webrtc.orgaccf6072012-03-07 17:16:10340 uint8_t packet_loss);
marpan@google.com86548c62011-07-12 17:12:57341
marpan@webrtc.org9d76b4e2012-02-28 23:39:31342 // Set the UEP protection on/off.
marpan@webrtc.orgaccf6072012-03-07 17:16:10343 bool SetUepProtection(uint8_t code_rate_delta,
344 float total_rate,
345 uint8_t packet_loss,
346 bool frame_type);
marpan@google.com86548c62011-07-12 17:12:57347
marpan@webrtc.org9d76b4e2012-02-28 23:39:31348 private:
349 // Previous state of network parameters.
marpan@webrtc.orgaccf6072012-03-07 17:16:10350 float prev_total_rate_;
pkasting@chromium.org16825b12015-01-12 21:51:21351 int64_t prev_rtt_time_;
marpan@webrtc.orgaccf6072012-03-07 17:16:10352 uint8_t prev_packet_loss_;
353 uint8_t prev_code_rate_delta_;
niklase@google.com470e71d2011-07-07 08:21:25354};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26355} // namespace webrtc
marpan@webrtc.org9d76b4e2012-02-28 23:39:31356#endif // WEBRTC_MODULES_VIDEO_CODING_QM_SELECT_H_