1 /* 2 * Copyright (c) 2020 The WebM 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 */ 10 11 #ifndef VPX_VP9_RATECTRL_RTC_H_ 12 #define VPX_VP9_RATECTRL_RTC_H_ 13 14 #include <cstdint> 15 #include <memory> 16 17 #include "vp9/common/vp9_enums.h" 18 #include "vp9/vp9_iface_common.h" 19 #include "vp9/encoder/vp9_aq_cyclicrefresh.h" 20 #include "vp9/vp9_cx_iface.h" 21 #include "vpx/internal/vpx_ratectrl_rtc.h" 22 #include "vpx_mem/vpx_mem.h" 23 24 struct VP9_COMP; 25 26 namespace libvpx { 27 struct VP9RateControlRtcConfig : public VpxRateControlRtcConfig { 28 public: VP9RateControlRtcConfigVP9RateControlRtcConfig29 VP9RateControlRtcConfig() { 30 ss_number_layers = 1; 31 vp9_zero(max_quantizers); 32 vp9_zero(min_quantizers); 33 vp9_zero(scaling_factor_den); 34 vp9_zero(scaling_factor_num); 35 vp9_zero(layer_target_bitrate); 36 vp9_zero(ts_rate_decimator); 37 scaling_factor_num[0] = 1; 38 scaling_factor_den[0] = 1; 39 max_quantizers[0] = max_quantizer; 40 min_quantizers[0] = min_quantizer; 41 max_consec_drop = INT_MAX; 42 } 43 44 // Number of spatial layers 45 int ss_number_layers; 46 int max_quantizers[VPX_MAX_LAYERS]; 47 int min_quantizers[VPX_MAX_LAYERS]; 48 int scaling_factor_num[VPX_SS_MAX_LAYERS]; 49 int scaling_factor_den[VPX_SS_MAX_LAYERS]; 50 // This is only for SVC for now. 51 int max_consec_drop; 52 }; 53 54 struct VP9FrameParamsQpRTC { 55 RcFrameType frame_type; 56 int spatial_layer_id; 57 int temporal_layer_id; 58 }; 59 60 struct VP9SegmentationData { 61 const uint8_t *segmentation_map; 62 size_t segmentation_map_size; 63 const int *delta_q; 64 size_t delta_q_size; 65 }; 66 67 // This interface allows using VP9 real-time rate control without initializing 68 // the encoder. To use this interface, you need to link with libvpxrc.a. 69 // 70 // #include "vp9/ratectrl_rtc.h" 71 // VP9RateControlRtcConfig cfg; 72 // VP9FrameParamsQpRTC frame_params; 73 // 74 // YourFunctionToInitializeConfig(cfg); 75 // std::unique_ptr<VP9RateControlRTC> rc_api = VP9RateControlRTC::Create(cfg); 76 // // start encoding 77 // while (frame_to_encode) { 78 // if (config_changed) 79 // rc_api->UpdateRateControl(cfg); 80 // YourFunctionToFillFrameParams(frame_params); 81 // rc_api->ComputeQP(frame_params); 82 // YourFunctionToUseQP(rc_api->GetQP()); 83 // YourFunctionToUseLoopfilter(rc_api->GetLoopfilterLevel()); 84 // // After encoding 85 // rc_api->PostEncode(encoded_frame_size, frame_params); 86 // } 87 class VP9RateControlRTC { 88 public: 89 static std::unique_ptr<VP9RateControlRTC> Create( 90 const VP9RateControlRtcConfig &cfg); 91 ~VP9RateControlRTC(); 92 93 bool UpdateRateControl(const VP9RateControlRtcConfig &rc_cfg); 94 // GetQP() needs to be called after ComputeQP() to get the latest QP 95 int GetQP() const; 96 int GetLoopfilterLevel() const; 97 bool GetSegmentationData(VP9SegmentationData *segmentation_data) const; 98 // ComputeQP computes the QP if the frame is not dropped (kOk return), 99 // otherwise it returns kDrop and subsequent GetQP and PostEncodeUpdate 100 // are not to be called (vp9_rc_postencode_update_drop_frame is already 101 // called via ComputeQP if drop is decided). 102 FrameDropDecision ComputeQP(const VP9FrameParamsQpRTC &frame_params); 103 // Feedback to rate control with the size of current encoded frame 104 void PostEncodeUpdate(uint64_t encoded_frame_size, 105 const VP9FrameParamsQpRTC &frame_params); 106 107 private: VP9RateControlRTC()108 VP9RateControlRTC() {} 109 bool InitRateControl(const VP9RateControlRtcConfig &cfg); 110 struct VP9_COMP *cpi_; 111 }; 112 113 } // namespace libvpx 114 115 #endif // VPX_VP9_RATECTRL_RTC_H_ 116