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