1 /* 2 * Copyright (c) 2022, Alliance for Open Media. All rights reserved 3 * 4 * This source code is subject to the terms of the BSD 2 Clause License and 5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6 * was not distributed with this source code in the LICENSE file, you can 7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8 * Media Patent License 1.0 was not distributed with this source code in the 9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10 */ 11 12 #ifndef AOM_AV1_QMODE_RC_RATECTRL_QMODE_H_ 13 #define AOM_AV1_QMODE_RC_RATECTRL_QMODE_H_ 14 15 #include <deque> 16 #include <queue> 17 #include <unordered_map> 18 #include <vector> 19 #include "av1/encoder/firstpass.h" 20 #include "av1/qmode_rc/ratectrl_qmode_interface.h" 21 #include "av1/qmode_rc/reference_manager.h" 22 23 namespace aom { 24 25 constexpr int kLayerDepthOffset = 1; 26 constexpr int kMinIntervalToAddArf = 3; 27 constexpr int kMinArfInterval = (kMinIntervalToAddArf + 1) / 2; 28 29 struct TplUnitDepStats { 30 double propagation_cost; 31 double intra_cost; 32 double inter_cost; 33 std::array<MotionVector, kBlockRefCount> mv; 34 std::array<int, kBlockRefCount> ref_frame_index; 35 }; 36 37 struct TplFrameDepStats { 38 int unit_size; // equivalent to min_block_size 39 double rdcost; // overall rate-distortion cost 40 std::vector<std::vector<TplUnitDepStats>> unit_stats; 41 }; 42 43 struct TplGopDepStats { 44 std::vector<TplFrameDepStats> frame_dep_stats_list; 45 }; 46 47 GopFrame GopFrameInvalid(); 48 49 // Set up is_key_frame, is_arf_frame, is_show_frame, is_golden_frame and 50 // encode_ref_mode in GopFrame based on gop_frame_type 51 void SetGopFrameByType(GopFrameType gop_frame_type, GopFrame *gop_frame); 52 53 GopFrame GopFrameBasic(int global_coding_idx_offset, 54 int global_order_idx_offset, int coding_idx, 55 int order_idx, int depth, int display_idx, 56 GopFrameType gop_frame_type); 57 58 GopStruct ConstructGop(RefFrameManager *ref_frame_manager, int show_frame_count, 59 bool has_key_frame, int global_coding_idx_offset, 60 int global_order_idx_offset); 61 62 // Creates a TplFrameDepStats containing an 2D array of default-initialized 63 // TplUnitDepStats, with dimensions of 64 // ceil(frame_height / min_block_size) x ceil(frame_width / min_block_size). 65 // i.e., there will be one entry for each square block of size min_block_size, 66 // and blocks along the bottom or right edge of the frame may extend beyond the 67 // edges of the frame. 68 TplFrameDepStats CreateTplFrameDepStats(int frame_height, int frame_width, 69 int min_block_size); 70 71 TplUnitDepStats TplBlockStatsToDepStats(const TplBlockStats &block_stats, 72 int unit_count); 73 74 StatusOr<TplFrameDepStats> CreateTplFrameDepStatsWithoutPropagation( 75 const TplFrameStats &frame_stats); 76 77 std::vector<int> GetKeyFrameList(const FirstpassInfo &first_pass_info); 78 79 double TplFrameDepStatsAccumulateIntraCost( 80 const TplFrameDepStats &frame_dep_stats); 81 82 double TplFrameDepStatsAccumulateInterCost( 83 const TplFrameDepStats &frame_dep_stats); 84 85 double TplFrameDepStatsAccumulate(const TplFrameDepStats &frame_dep_stats); 86 87 void TplFrameDepStatsPropagate(int coding_idx, 88 const RefFrameTable &ref_frame_table, 89 TplGopDepStats *tpl_gop_dep_stats); 90 91 int GetBlockOverlapArea(int r0, int c0, int r1, int c1, int size); 92 93 namespace internal { 94 std::unordered_map<int, int> KMeans(std::vector<uint8_t> qindices, int k); 95 } 96 97 StatusOr<TplGopDepStats> ComputeTplGopDepStats( 98 const TplGopStats &tpl_gop_stats, 99 const std::vector<LookaheadStats> &lookahead_stats, 100 const std::vector<RefFrameTable> &ref_frame_table_list); 101 102 class AV1RateControlQMode : public AV1RateControlQModeInterface { 103 public: 104 Status SetRcParam(const RateControlParam &rc_param) override; 105 StatusOr<GopStructList> DetermineGopInfo( 106 const FirstpassInfo &firstpass_info) override; 107 StatusOr<GopEncodeInfo> GetGopEncodeInfo( 108 const GopStruct &gop_struct, const TplGopStats &tpl_gop_stats, 109 const std::vector<LookaheadStats> &lookahead_stats, 110 const FirstpassInfo &firstpass_info, 111 const RefFrameTable &ref_frame_table_snapshot) override; 112 StatusOr<GopEncodeInfo> GetTplPassGopEncodeInfo( 113 const GopStruct &gop_struct, 114 const FirstpassInfo &firstpass_info) override; 115 116 // Public for testing only. 117 // Returns snapshots of the ref frame before and after each frame in 118 // gop_struct. The returned list will have n+1 entries for n frames. 119 // If this is first GOP, ref_frame_table is ignored and all refs are assumed 120 // invalid; otherwise ref_frame_table is used as the initial state. 121 std::vector<RefFrameTable> GetRefFrameTableList( 122 const GopStruct &gop_struct, 123 const std::vector<LookaheadStats> &lookahead_stats, 124 RefFrameTable ref_frame_table); 125 126 private: 127 RateControlParam rc_param_; 128 129 // Private methods to determine GOP encode info with different stats 130 StatusOr<GopEncodeInfo> GetGopEncodeInfoWithNoStats( 131 const GopStruct &gop_struct); 132 StatusOr<GopEncodeInfo> GetGopEncodeInfoWithFp( 133 const GopStruct &gop_struct, const FirstpassInfo &firstpass_info); 134 StatusOr<GopEncodeInfo> GetGopEncodeInfoWithTpl( 135 const GopStruct &gop_struct, const TplGopStats &tpl_gop_stats, 136 const std::vector<LookaheadStats> &lookahead_stats, 137 const RefFrameTable &ref_frame_table_snapshot_init); 138 }; 139 } // namespace aom 140 141 #endif // AOM_AV1_QMODE_RC_RATECTRL_QMODE_H_ 142