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_DUCKY_ENCODE_H_ 13 #define AOM_AV1_QMODE_RC_DUCKY_ENCODE_H_ 14 15 #include <cstddef> 16 #include <cstdint> 17 #include <memory> 18 #include <string> 19 #include <vector> 20 21 #include "aom/aom_encoder.h" 22 #include "av1/encoder/firstpass.h" 23 #include "av1/qmode_rc/ratectrl_qmode_interface.h" 24 25 namespace aom { 26 struct VideoInfo { 27 int frame_width; 28 int frame_height; 29 aom_rational_t frame_rate; 30 aom_img_fmt_t img_fmt; 31 int frame_count; 32 std::string file_path; 33 }; 34 35 struct EncodeFrameResult { 36 std::vector<uint8_t> bitstream_buf; 37 // TODO(angiebird): update global_coding_idx and global_order_idx properly. 38 int global_coding_idx; 39 int global_order_idx; 40 int q_index; 41 int rdmult; 42 int rate; 43 int64_t dist; 44 double psnr; 45 }; 46 47 enum class EncodeFrameMode { 48 kNone, // Let native AV1 determine q index and rdmult 49 kQindex, // DuckyEncode determines q index and AV1 determines rdmult 50 kQindexRdmult, // DuckyEncode determines q index and rdmult 51 }; 52 53 enum class EncodeGopMode { 54 kNone, // native AV1 decides GOP 55 kGopRcl, // rate control lib decides GOP 56 }; 57 58 struct EncodeFrameDecision { 59 EncodeFrameMode qp_mode; 60 EncodeGopMode gop_mode; 61 FrameEncodeParameters parameters; 62 }; 63 64 using GopEncodeInfoList = std::vector<GopEncodeInfo>; 65 66 // DuckyEncode is an experimental encoder c++ interface for two-pass mode. 67 // This object can be used to do zero or more encode passes, where each encode 68 // pass consists of: 69 // - StartEncode() 70 // - Zero or more calls to EncodeFrame() 71 // - EndEncode() 72 // Encode passes may not overlap, and any other sequence of these calls is 73 // invalid. 74 class DuckyEncode { 75 public: 76 explicit DuckyEncode(const VideoInfo &video_info, BLOCK_SIZE sb_size, 77 int max_ref_frames, int speed, int base_qindex); 78 ~DuckyEncode(); 79 std::vector<FIRSTPASS_STATS> ComputeFirstPassStats(); 80 void StartEncode(const std::vector<FIRSTPASS_STATS> &stats_list); 81 82 TplGopStats ObtainTplStats(const GopStruct gop_struct, 83 bool rate_dist_present); 84 85 std::vector<TplGopStats> ComputeTplStats( 86 const std::vector<FIRSTPASS_STATS> &stats_list, 87 const GopStructList &gop_list, 88 const GopEncodeInfoList &gop_encode_info_list); 89 90 std::vector<TplGopStats> ComputeTwoPassTplStats( 91 const std::vector<FIRSTPASS_STATS> &stats_list, 92 const GopStructList &gop_list, 93 const GopEncodeInfoList &gop_encode_info_list, 94 const GopEncodeInfoList &alt_gop_encode_info_list); 95 96 std::vector<EncodeFrameResult> EncodeVideo( 97 const GopStructList &gop_list, 98 const GopEncodeInfoList &gop_encode_info_list); 99 EncodeFrameResult EncodeFrame(const EncodeFrameDecision &decision); 100 void EndEncode(); 101 void AllocateBitstreamBuffer(const VideoInfo &video_info); 102 103 private: 104 void InitEncoder(aom_enc_pass pass, 105 const std::vector<FIRSTPASS_STATS> *stats_list); 106 void FreeEncoder(); 107 108 private: 109 class EncodeImpl; 110 std::unique_ptr<EncodeImpl> impl_ptr_; 111 bool write_temp_delimiter_; 112 std::vector<uint8_t> bitstream_buf_; 113 size_t pending_ctx_size_; 114 }; 115 } // namespace aom 116 117 #endif // AOM_AV1_QMODE_RC_DUCKY_ENCODE_H_ 118