1*77c1e3ccSAndroid Build Coastguard Worker /* 2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2021, Alliance for Open Media. All rights reserved. 3*77c1e3ccSAndroid Build Coastguard Worker * 4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and 5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can 7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the 9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10*77c1e3ccSAndroid Build Coastguard Worker */ 11*77c1e3ccSAndroid Build Coastguard Worker 12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_RATECTRL_RTC_H_ 13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_RATECTRL_RTC_H_ 14*77c1e3ccSAndroid Build Coastguard Worker 15*77c1e3ccSAndroid Build Coastguard Worker #include <cstdint> 16*77c1e3ccSAndroid Build Coastguard Worker #include <memory> 17*77c1e3ccSAndroid Build Coastguard Worker 18*77c1e3ccSAndroid Build Coastguard Worker struct AV1_COMP; 19*77c1e3ccSAndroid Build Coastguard Worker 20*77c1e3ccSAndroid Build Coastguard Worker namespace aom { 21*77c1e3ccSAndroid Build Coastguard Worker 22*77c1e3ccSAndroid Build Coastguard Worker // These constants come from AV1 spec. 23*77c1e3ccSAndroid Build Coastguard Worker static constexpr size_t kAV1MaxLayers = 32; 24*77c1e3ccSAndroid Build Coastguard Worker static constexpr size_t kAV1MaxTemporalLayers = 8; 25*77c1e3ccSAndroid Build Coastguard Worker static constexpr size_t kAV1MaxSpatialLayers = 4; 26*77c1e3ccSAndroid Build Coastguard Worker 27*77c1e3ccSAndroid Build Coastguard Worker enum FrameType { kKeyFrame, kInterFrame }; 28*77c1e3ccSAndroid Build Coastguard Worker 29*77c1e3ccSAndroid Build Coastguard Worker struct AV1RateControlRtcConfig { 30*77c1e3ccSAndroid Build Coastguard Worker public: 31*77c1e3ccSAndroid Build Coastguard Worker AV1RateControlRtcConfig(); 32*77c1e3ccSAndroid Build Coastguard Worker 33*77c1e3ccSAndroid Build Coastguard Worker int width; 34*77c1e3ccSAndroid Build Coastguard Worker int height; 35*77c1e3ccSAndroid Build Coastguard Worker // Flag indicating if the content is screen or not. 36*77c1e3ccSAndroid Build Coastguard Worker bool is_screen = false; 37*77c1e3ccSAndroid Build Coastguard Worker // 0-63 38*77c1e3ccSAndroid Build Coastguard Worker int max_quantizer; 39*77c1e3ccSAndroid Build Coastguard Worker int min_quantizer; 40*77c1e3ccSAndroid Build Coastguard Worker int64_t target_bandwidth; 41*77c1e3ccSAndroid Build Coastguard Worker int64_t buf_initial_sz; 42*77c1e3ccSAndroid Build Coastguard Worker int64_t buf_optimal_sz; 43*77c1e3ccSAndroid Build Coastguard Worker int64_t buf_sz; 44*77c1e3ccSAndroid Build Coastguard Worker int undershoot_pct; 45*77c1e3ccSAndroid Build Coastguard Worker int overshoot_pct; 46*77c1e3ccSAndroid Build Coastguard Worker int max_intra_bitrate_pct; 47*77c1e3ccSAndroid Build Coastguard Worker int max_inter_bitrate_pct; 48*77c1e3ccSAndroid Build Coastguard Worker int frame_drop_thresh; 49*77c1e3ccSAndroid Build Coastguard Worker int max_consec_drop_ms; 50*77c1e3ccSAndroid Build Coastguard Worker double framerate; 51*77c1e3ccSAndroid Build Coastguard Worker int layer_target_bitrate[kAV1MaxLayers]; 52*77c1e3ccSAndroid Build Coastguard Worker int ts_rate_decimator[kAV1MaxTemporalLayers]; 53*77c1e3ccSAndroid Build Coastguard Worker int aq_mode; 54*77c1e3ccSAndroid Build Coastguard Worker // Number of spatial layers 55*77c1e3ccSAndroid Build Coastguard Worker int ss_number_layers; 56*77c1e3ccSAndroid Build Coastguard Worker // Number of temporal layers 57*77c1e3ccSAndroid Build Coastguard Worker int ts_number_layers; 58*77c1e3ccSAndroid Build Coastguard Worker int max_quantizers[kAV1MaxLayers]; 59*77c1e3ccSAndroid Build Coastguard Worker int min_quantizers[kAV1MaxLayers]; 60*77c1e3ccSAndroid Build Coastguard Worker int scaling_factor_num[kAV1MaxSpatialLayers]; 61*77c1e3ccSAndroid Build Coastguard Worker int scaling_factor_den[kAV1MaxSpatialLayers]; 62*77c1e3ccSAndroid Build Coastguard Worker }; 63*77c1e3ccSAndroid Build Coastguard Worker 64*77c1e3ccSAndroid Build Coastguard Worker struct AV1FrameParamsRTC { 65*77c1e3ccSAndroid Build Coastguard Worker FrameType frame_type; 66*77c1e3ccSAndroid Build Coastguard Worker int spatial_layer_id; 67*77c1e3ccSAndroid Build Coastguard Worker int temporal_layer_id; 68*77c1e3ccSAndroid Build Coastguard Worker }; 69*77c1e3ccSAndroid Build Coastguard Worker 70*77c1e3ccSAndroid Build Coastguard Worker struct AV1LoopfilterLevel { 71*77c1e3ccSAndroid Build Coastguard Worker int filter_level[2]; 72*77c1e3ccSAndroid Build Coastguard Worker int filter_level_u; 73*77c1e3ccSAndroid Build Coastguard Worker int filter_level_v; 74*77c1e3ccSAndroid Build Coastguard Worker }; 75*77c1e3ccSAndroid Build Coastguard Worker 76*77c1e3ccSAndroid Build Coastguard Worker struct AV1CdefInfo { 77*77c1e3ccSAndroid Build Coastguard Worker int cdef_strength_y; 78*77c1e3ccSAndroid Build Coastguard Worker int cdef_strength_uv; 79*77c1e3ccSAndroid Build Coastguard Worker int damping; 80*77c1e3ccSAndroid Build Coastguard Worker }; 81*77c1e3ccSAndroid Build Coastguard Worker 82*77c1e3ccSAndroid Build Coastguard Worker struct AV1SegmentationData { 83*77c1e3ccSAndroid Build Coastguard Worker const uint8_t *segmentation_map; 84*77c1e3ccSAndroid Build Coastguard Worker size_t segmentation_map_size; 85*77c1e3ccSAndroid Build Coastguard Worker const int *delta_q; 86*77c1e3ccSAndroid Build Coastguard Worker size_t delta_q_size; 87*77c1e3ccSAndroid Build Coastguard Worker }; 88*77c1e3ccSAndroid Build Coastguard Worker 89*77c1e3ccSAndroid Build Coastguard Worker enum class FrameDropDecision { 90*77c1e3ccSAndroid Build Coastguard Worker kOk, // Frame is encoded. 91*77c1e3ccSAndroid Build Coastguard Worker kDrop, // Frame is dropped. 92*77c1e3ccSAndroid Build Coastguard Worker }; 93*77c1e3ccSAndroid Build Coastguard Worker 94*77c1e3ccSAndroid Build Coastguard Worker class AV1RateControlRTC { 95*77c1e3ccSAndroid Build Coastguard Worker public: 96*77c1e3ccSAndroid Build Coastguard Worker static std::unique_ptr<AV1RateControlRTC> Create( 97*77c1e3ccSAndroid Build Coastguard Worker const AV1RateControlRtcConfig &cfg); 98*77c1e3ccSAndroid Build Coastguard Worker ~AV1RateControlRTC(); 99*77c1e3ccSAndroid Build Coastguard Worker 100*77c1e3ccSAndroid Build Coastguard Worker bool UpdateRateControl(const AV1RateControlRtcConfig &rc_cfg); 101*77c1e3ccSAndroid Build Coastguard Worker // GetQP() needs to be called after ComputeQP() to get the latest QP 102*77c1e3ccSAndroid Build Coastguard Worker int GetQP() const; 103*77c1e3ccSAndroid Build Coastguard Worker // GetLoopfilterLevel() needs to be called after ComputeQP() 104*77c1e3ccSAndroid Build Coastguard Worker AV1LoopfilterLevel GetLoopfilterLevel() const; 105*77c1e3ccSAndroid Build Coastguard Worker // GetCdefInfo() needs to be called after ComputeQP() 106*77c1e3ccSAndroid Build Coastguard Worker AV1CdefInfo GetCdefInfo() const; 107*77c1e3ccSAndroid Build Coastguard Worker // Returns the segmentation map used for cyclic refresh, based on 4x4 blocks. 108*77c1e3ccSAndroid Build Coastguard Worker bool GetSegmentationData(AV1SegmentationData *segmentation_data) const; 109*77c1e3ccSAndroid Build Coastguard Worker // ComputeQP returns the QP if the frame is not dropped (kOk return), 110*77c1e3ccSAndroid Build Coastguard Worker // otherwise it returns kDrop and subsequent GetQP and PostEncodeUpdate 111*77c1e3ccSAndroid Build Coastguard Worker // are not to be called (av1_rc_postencode_update_drop_frame is already 112*77c1e3ccSAndroid Build Coastguard Worker // called via ComputeQP if drop is decided). 113*77c1e3ccSAndroid Build Coastguard Worker FrameDropDecision ComputeQP(const AV1FrameParamsRTC &frame_params); 114*77c1e3ccSAndroid Build Coastguard Worker // Feedback to rate control with the size of current encoded frame 115*77c1e3ccSAndroid Build Coastguard Worker void PostEncodeUpdate(uint64_t encoded_frame_size); 116*77c1e3ccSAndroid Build Coastguard Worker 117*77c1e3ccSAndroid Build Coastguard Worker private: 118*77c1e3ccSAndroid Build Coastguard Worker AV1RateControlRTC() = default; 119*77c1e3ccSAndroid Build Coastguard Worker bool InitRateControl(const AV1RateControlRtcConfig &cfg); 120*77c1e3ccSAndroid Build Coastguard Worker AV1_COMP *cpi_; 121*77c1e3ccSAndroid Build Coastguard Worker int initial_width_; 122*77c1e3ccSAndroid Build Coastguard Worker int initial_height_; 123*77c1e3ccSAndroid Build Coastguard Worker }; 124*77c1e3ccSAndroid Build Coastguard Worker 125*77c1e3ccSAndroid Build Coastguard Worker } // namespace aom 126*77c1e3ccSAndroid Build Coastguard Worker 127*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AV1_RATECTRL_RTC_H_ 128