libvpx_vp8_encoder.h (6132B)
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 <cstddef> 15 #include <cstdint> 16 #include <memory> 17 #include <optional> 18 #include <vector> 19 20 #include "api/environment/environment.h" 21 #include "api/fec_controller_override.h" 22 #include "api/scoped_refptr.h" 23 #include "api/units/time_delta.h" 24 #include "api/units/timestamp.h" 25 #include "api/video/encoded_image.h" 26 #include "api/video/video_frame.h" 27 #include "api/video/video_frame_buffer.h" 28 #include "api/video/video_frame_type.h" 29 #include "api/video_codecs/video_codec.h" 30 #include "api/video_codecs/video_encoder.h" 31 #include "api/video_codecs/vp8_frame_buffer_controller.h" 32 #include "api/video_codecs/vp8_frame_config.h" 33 #include "modules/video_coding/codecs/interface/libvpx_interface.h" 34 #include "modules/video_coding/codecs/vp8/include/vp8.h" 35 #include "modules/video_coding/include/video_codec_interface.h" 36 #include "modules/video_coding/utility/corruption_detection_settings_generator.h" 37 #include "modules/video_coding/utility/frame_sampler.h" 38 #include "modules/video_coding/utility/framerate_controller_deprecated.h" 39 #include "rtc_base/experiments/encoder_info_settings.h" 40 #include "rtc_base/experiments/rate_control_settings.h" 41 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h" 42 #include "third_party/libvpx/source/libvpx/vpx/vpx_image.h" 43 44 namespace webrtc { 45 46 class LibvpxVp8Encoder : public VideoEncoder { 47 public: 48 LibvpxVp8Encoder(const Environment& env, 49 Vp8EncoderSettings settings, 50 std::unique_ptr<LibvpxInterface> interface); 51 52 ~LibvpxVp8Encoder() override; 53 54 int Release() override; 55 56 void SetFecControllerOverride( 57 FecControllerOverride* fec_controller_override) override; 58 59 int InitEncode(const VideoCodec* codec_settings, 60 const VideoEncoder::Settings& settings) override; 61 62 int Encode(const VideoFrame& input_image, 63 const std::vector<VideoFrameType>* frame_types) override; 64 65 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; 66 67 void SetRates(const RateControlParameters& parameters) override; 68 69 void OnPacketLossRateUpdate(float packet_loss_rate) override; 70 71 void OnRttUpdate(int64_t rtt_ms) override; 72 73 void OnLossNotification(const LossNotification& loss_notification) override; 74 75 EncoderInfo GetEncoderInfo() const override; 76 77 static vpx_enc_frame_flags_t EncodeFlags(const Vp8FrameConfig& references); 78 79 private: 80 // Get the cpu_speed setting for encoder based on resolution and/or platform. 81 int GetCpuSpeed(int width, int height); 82 83 // Determine number of encoder threads to use. 84 int NumberOfThreads(int width, int height, int number_of_cores); 85 86 // Call encoder initialize function and set control settings. 87 int InitAndSetControlSettings(); 88 89 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific, 90 const vpx_codec_cx_pkt& pkt, 91 int stream_idx, 92 int encoder_idx, 93 uint32_t timestamp); 94 95 int GetEncodedPartitions(const VideoFrame& input_image, 96 bool retransmission_allowed); 97 98 // Set the stream state for stream `stream_idx`. 99 void SetStreamState(bool send_stream, int stream_idx); 100 101 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size); 102 103 uint32_t FrameDropThreshold(size_t spatial_idx) const; 104 105 size_t SteadyStateSize(int sid, int tid); 106 107 bool UpdateVpxConfiguration(size_t stream_index); 108 109 void MaybeUpdatePixelFormat(vpx_img_fmt fmt); 110 // Prepares `raw_image_` to reference image data of `buffer`, or of mapped or 111 // scaled versions of `buffer`. Returns a list of buffers that got referenced 112 // as a result, allowing the caller to keep references to them until after 113 // encoding has finished. On failure to convert the buffer, an empty list is 114 // returned. 115 std::vector<scoped_refptr<VideoFrameBuffer>> PrepareBuffers( 116 scoped_refptr<VideoFrameBuffer> buffer); 117 118 const Environment env_; 119 const std::unique_ptr<LibvpxInterface> libvpx_; 120 121 const RateControlSettings rate_control_settings_; 122 123 EncodedImageCallback* encoded_complete_callback_ = nullptr; 124 VideoCodec codec_; 125 bool inited_ = false; 126 int64_t timestamp_ = 0; 127 int qp_max_ = 56; 128 int cpu_speed_default_ = -6; 129 int number_of_cores_ = 0; 130 uint32_t rc_max_intra_target_ = 0; 131 int num_active_streams_ = 0; 132 std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_; 133 const std::vector<VideoEncoder::ResolutionBitrateLimits> 134 resolution_bitrate_limits_; 135 std::vector<bool> key_frame_request_; 136 std::vector<bool> send_stream_; 137 std::vector<int> cpu_speed_; 138 std::vector<vpx_image_t> raw_images_; 139 std::vector<EncodedImage> encoded_images_; 140 std::vector<vpx_codec_ctx_t> encoders_; 141 std::vector<vpx_codec_enc_cfg_t> vpx_configs_; 142 std::vector<Vp8EncoderConfig> config_overrides_; 143 std::vector<vpx_rational_t> downsampling_factors_; 144 std::vector<Timestamp> last_encoder_output_time_; 145 146 FramerateControllerDeprecated framerate_controller_; 147 int num_steady_state_frames_ = 0; 148 149 FecControllerOverride* fec_controller_override_ = nullptr; 150 151 const LibvpxVp8EncoderInfoSettings encoder_info_override_; 152 153 std::optional<TimeDelta> max_frame_drop_interval_; 154 155 bool android_specific_threading_settings_; 156 157 std::unique_ptr<CorruptionDetectionSettingsGenerator> 158 corruption_detection_settings_generator_; 159 160 // Determine whether the frame should be sampled for PSNR. 161 FrameSampler psnr_frame_sampler_; 162 // TODO(webrtc:388070060): Remove after rollout. 163 const bool calculate_psnr_; 164 }; 165 166 } // namespace webrtc 167 168 #endif // MODULES_VIDEO_CODING_CODECS_VP8_LIBVPX_VP8_ENCODER_H_