xref: /aosp_15_r20/external/webrtc/modules/video_coding/codecs/vp8/libvpx_vp8_encoder.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 The WebRTC 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 MODULES_VIDEO_CODING_CODECS_VP8_LIBVPX_VP8_ENCODER_H_
12 #define MODULES_VIDEO_CODING_CODECS_VP8_LIBVPX_VP8_ENCODER_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "api/fec_controller_override.h"
19 #include "api/video/encoded_image.h"
20 #include "api/video/video_frame.h"
21 #include "api/video_codecs/video_encoder.h"
22 #include "api/video_codecs/vp8_frame_buffer_controller.h"
23 #include "api/video_codecs/vp8_frame_config.h"
24 #include "modules/video_coding/codecs/interface/libvpx_interface.h"
25 #include "modules/video_coding/codecs/vp8/include/vp8.h"
26 #include "modules/video_coding/include/video_codec_interface.h"
27 #include "modules/video_coding/utility/framerate_controller_deprecated.h"
28 #include "modules/video_coding/utility/vp8_constants.h"
29 #include "rtc_base/experiments/cpu_speed_experiment.h"
30 #include "rtc_base/experiments/encoder_info_settings.h"
31 #include "rtc_base/experiments/rate_control_settings.h"
32 #include "vpx/vp8cx.h"
33 #include "vpx/vpx_encoder.h"
34 
35 namespace webrtc {
36 
37 class LibvpxVp8Encoder : public VideoEncoder {
38  public:
39   LibvpxVp8Encoder(std::unique_ptr<LibvpxInterface> interface,
40                    VP8Encoder::Settings settings);
41   ~LibvpxVp8Encoder() override;
42 
43   int Release() override;
44 
45   void SetFecControllerOverride(
46       FecControllerOverride* fec_controller_override) override;
47 
48   int InitEncode(const VideoCodec* codec_settings,
49                  const VideoEncoder::Settings& settings) override;
50 
51   int Encode(const VideoFrame& input_image,
52              const std::vector<VideoFrameType>* frame_types) override;
53 
54   int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
55 
56   void SetRates(const RateControlParameters& parameters) override;
57 
58   void OnPacketLossRateUpdate(float packet_loss_rate) override;
59 
60   void OnRttUpdate(int64_t rtt_ms) override;
61 
62   void OnLossNotification(const LossNotification& loss_notification) override;
63 
64   EncoderInfo GetEncoderInfo() const override;
65 
66   static vpx_enc_frame_flags_t EncodeFlags(const Vp8FrameConfig& references);
67 
68  private:
69   // Get the cpu_speed setting for encoder based on resolution and/or platform.
70   int GetCpuSpeed(int width, int height);
71 
72   // Determine number of encoder threads to use.
73   int NumberOfThreads(int width, int height, int number_of_cores);
74 
75   // Call encoder initialize function and set control settings.
76   int InitAndSetControlSettings();
77 
78   void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
79                              const vpx_codec_cx_pkt& pkt,
80                              int stream_idx,
81                              int encoder_idx,
82                              uint32_t timestamp);
83 
84   int GetEncodedPartitions(const VideoFrame& input_image,
85                            bool retransmission_allowed);
86 
87   // Set the stream state for stream `stream_idx`.
88   void SetStreamState(bool send_stream, int stream_idx);
89 
90   uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
91 
92   uint32_t FrameDropThreshold(size_t spatial_idx) const;
93 
94   size_t SteadyStateSize(int sid, int tid);
95 
96   bool UpdateVpxConfiguration(size_t stream_index);
97 
98   void MaybeUpdatePixelFormat(vpx_img_fmt fmt);
99   // Prepares `raw_image_` to reference image data of `buffer`, or of mapped or
100   // scaled versions of `buffer`. Returns a list of buffers that got referenced
101   // as a result, allowing the caller to keep references to them until after
102   // encoding has finished. On failure to convert the buffer, an empty list is
103   // returned.
104   std::vector<rtc::scoped_refptr<VideoFrameBuffer>> PrepareBuffers(
105       rtc::scoped_refptr<VideoFrameBuffer> buffer);
106 
107   const std::unique_ptr<LibvpxInterface> libvpx_;
108 
109   const CpuSpeedExperiment experimental_cpu_speed_config_arm_;
110   const RateControlSettings rate_control_settings_;
111 
112   EncodedImageCallback* encoded_complete_callback_ = nullptr;
113   VideoCodec codec_;
114   bool inited_ = false;
115   int64_t timestamp_ = 0;
116   int qp_max_ = 56;
117   int cpu_speed_default_ = -6;
118   int number_of_cores_ = 0;
119   uint32_t rc_max_intra_target_ = 0;
120   int num_active_streams_ = 0;
121   const std::unique_ptr<Vp8FrameBufferControllerFactory>
122       frame_buffer_controller_factory_;
123   std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_;
124   const std::vector<VideoEncoder::ResolutionBitrateLimits>
125       resolution_bitrate_limits_;
126   std::vector<bool> key_frame_request_;
127   std::vector<bool> send_stream_;
128   std::vector<int> cpu_speed_;
129   std::vector<vpx_image_t> raw_images_;
130   std::vector<EncodedImage> encoded_images_;
131   std::vector<vpx_codec_ctx_t> encoders_;
132   std::vector<vpx_codec_enc_cfg_t> vpx_configs_;
133   std::vector<Vp8EncoderConfig> config_overrides_;
134   std::vector<vpx_rational_t> downsampling_factors_;
135 
136   // Variable frame-rate screencast related fields and methods.
137   const struct VariableFramerateExperiment {
138     bool enabled = false;
139     // Framerate is limited to this value in steady state.
140     float framerate_limit = 5.0;
141     // This qp or below is considered a steady state.
142     int steady_state_qp = kVp8SteadyStateQpThreshold;
143     // Frames of at least this percentage below ideal for configured bitrate are
144     // considered in a steady state.
145     int steady_state_undershoot_percentage = 30;
146   } variable_framerate_experiment_;
147   static VariableFramerateExperiment ParseVariableFramerateConfig(
148       std::string group_name);
149   FramerateControllerDeprecated framerate_controller_;
150   int num_steady_state_frames_ = 0;
151 
152   FecControllerOverride* fec_controller_override_ = nullptr;
153 
154   const LibvpxVp8EncoderInfoSettings encoder_info_override_;
155 };
156 
157 }  // namespace webrtc
158 
159 #endif  // MODULES_VIDEO_CODING_CODECS_VP8_LIBVPX_VP8_ENCODER_H_
160