File: ratectrl_rtc.h

package info (click to toggle)
libvpx 1.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,932 kB
  • sloc: ansic: 252,377; cpp: 115,241; asm: 22,233; sh: 5,291; python: 4,391; perl: 2,010; makefile: 431
file content (106 lines) | stat: -rw-r--r-- 3,549 bytes parent folder | download | duplicates (27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 *  Copyright (c) 2020 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef VPX_VP9_RATECTRL_RTC_H_
#define VPX_VP9_RATECTRL_RTC_H_

#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>

#include "vpx/vpx_encoder.h"
#include "vpx/internal/vpx_ratectrl_rtc.h"

struct VP9_COMP;

namespace libvpx {
struct VP9RateControlRtcConfig : public VpxRateControlRtcConfig {
  VP9RateControlRtcConfig() {
    memset(layer_target_bitrate, 0, sizeof(layer_target_bitrate));
    memset(ts_rate_decimator, 0, sizeof(ts_rate_decimator));
    scaling_factor_num[0] = 1;
    scaling_factor_den[0] = 1;
    max_quantizers[0] = max_quantizer;
    min_quantizers[0] = min_quantizer;
  }

  // Number of spatial layers
  int ss_number_layers = 1;
  int max_quantizers[VPX_MAX_LAYERS] = {};
  int min_quantizers[VPX_MAX_LAYERS] = {};
  int scaling_factor_num[VPX_SS_MAX_LAYERS] = {};
  int scaling_factor_den[VPX_SS_MAX_LAYERS] = {};
  // This is only for SVC for now.
  int max_consec_drop = std::numeric_limits<int>::max();
};

struct VP9FrameParamsQpRTC {
  RcFrameType frame_type;
  int spatial_layer_id;
  int temporal_layer_id;
};

struct VP9SegmentationData {
  const uint8_t *segmentation_map;
  size_t segmentation_map_size;
  const int *delta_q;
  size_t delta_q_size;
};

// This interface allows using VP9 real-time rate control without initializing
// the encoder. To use this interface, you need to link with libvpxrc.a.
//
// #include "vp9/ratectrl_rtc.h"
// VP9RateControlRtcConfig cfg;
// VP9FrameParamsQpRTC frame_params;
//
// YourFunctionToInitializeConfig(cfg);
// std::unique_ptr<VP9RateControlRTC> rc_api = VP9RateControlRTC::Create(cfg);
// // start encoding
// while (frame_to_encode) {
//   if (config_changed)
//     rc_api->UpdateRateControl(cfg);
//   YourFunctionToFillFrameParams(frame_params);
//   rc_api->ComputeQP(frame_params);
//   YourFunctionToUseQP(rc_api->GetQP());
//   YourFunctionToUseLoopfilter(rc_api->GetLoopfilterLevel());
//   // After encoding
//   rc_api->PostEncode(encoded_frame_size, frame_params);
// }
class VP9RateControlRTC {
 public:
  static std::unique_ptr<VP9RateControlRTC> Create(
      const VP9RateControlRtcConfig &cfg);
  ~VP9RateControlRTC();

  bool UpdateRateControl(const VP9RateControlRtcConfig &rc_cfg);
  // GetQP() needs to be called after ComputeQP() to get the latest QP
  int GetQP() const;
  int GetLoopfilterLevel() const;
  bool GetSegmentationData(VP9SegmentationData *segmentation_data) const;
  // ComputeQP computes the QP if the frame is not dropped (kOk return),
  // otherwise it returns kDrop and subsequent GetQP and PostEncodeUpdate
  // are not to be called (vp9_rc_postencode_update_drop_frame is already
  // called via ComputeQP if drop is decided).
  FrameDropDecision ComputeQP(const VP9FrameParamsQpRTC &frame_params);
  // Feedback to rate control with the size of current encoded frame
  void PostEncodeUpdate(uint64_t encoded_frame_size,
                        const VP9FrameParamsQpRTC &frame_params);

 private:
  VP9RateControlRTC() = default;
  bool InitRateControl(const VP9RateControlRtcConfig &cfg);
  struct VP9_COMP *cpi_ = nullptr;
};

}  // namespace libvpx

#endif  // VPX_VP9_RATECTRL_RTC_H_