ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
| 12 | #define API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 13 | |
Erik Språng | dbdd839 | 2019-01-17 15:27:50 +0100 | [diff] [blame] | 14 | #include <limits> |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Erik Språng | dbdd839 | 2019-01-17 15:27:50 +0100 | [diff] [blame] | 19 | #include "absl/container/inlined_vector.h" |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Erik Språng | 4d9df38 | 2019-03-27 15:00:43 +0100 | [diff] [blame] | 21 | #include "api/units/data_rate.h" |
Niels Möller | 4dc66c5 | 2018-10-05 14:17:58 +0200 | [diff] [blame] | 22 | #include "api/video/encoded_image.h" |
Erik Språng | ec47565 | 2018-05-15 15:12:55 +0200 | [diff] [blame] | 23 | #include "api/video/video_bitrate_allocation.h" |
Erik Språng | f93eda1 | 2019-01-16 17:10:57 +0100 | [diff] [blame] | 24 | #include "api/video/video_codec_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "api/video/video_frame.h" |
Niels Möller | 802506c | 2018-05-31 10:44:51 +0200 | [diff] [blame] | 26 | #include "api/video_codecs/video_codec.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
Mirko Bonadei | 276827c | 2018-10-16 14:13:50 +0200 | [diff] [blame] | 28 | #include "rtc_base/system/rtc_export.h" |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | class RTPFragmentationHeader; |
| 33 | // TODO(pbos): Expose these through a public (root) header or change these APIs. |
| 34 | struct CodecSpecificInfo; |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 35 | |
| 36 | class EncodedImageCallback { |
| 37 | public: |
| 38 | virtual ~EncodedImageCallback() {} |
| 39 | |
| 40 | struct Result { |
| 41 | enum Error { |
| 42 | OK, |
| 43 | |
| 44 | // Failed to send the packet. |
| 45 | ERROR_SEND_FAILED, |
| 46 | }; |
| 47 | |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 48 | explicit Result(Error error) : error(error) {} |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 49 | Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {} |
| 50 | |
| 51 | Error error; |
| 52 | |
| 53 | // Frame ID assigned to the frame. The frame ID should be the same as the ID |
| 54 | // seen by the receiver for this frame. RTP timestamp of the frame is used |
| 55 | // as frame ID when RTP is used to send video. Must be used only when |
| 56 | // error=OK. |
| 57 | uint32_t frame_id = 0; |
| 58 | |
| 59 | // Tells the encoder that the next frame is should be dropped. |
| 60 | bool drop_next_frame = false; |
| 61 | }; |
| 62 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 63 | // Used to signal the encoder about reason a frame is dropped. |
| 64 | // kDroppedByMediaOptimizations - dropped by MediaOptimizations (for rate |
| 65 | // limiting purposes). |
| 66 | // kDroppedByEncoder - dropped by encoder's internal rate limiter. |
| 67 | enum class DropReason : uint8_t { |
| 68 | kDroppedByMediaOptimizations, |
| 69 | kDroppedByEncoder |
| 70 | }; |
| 71 | |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 72 | // Callback function which is called when an image has been encoded. |
| 73 | virtual Result OnEncodedImage( |
| 74 | const EncodedImage& encoded_image, |
| 75 | const CodecSpecificInfo* codec_specific_info, |
| 76 | const RTPFragmentationHeader* fragmentation) = 0; |
| 77 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 78 | virtual void OnDroppedFrame(DropReason reason) {} |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Mirko Bonadei | 276827c | 2018-10-16 14:13:50 +0200 | [diff] [blame] | 81 | class RTC_EXPORT VideoEncoder { |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 82 | public: |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 83 | struct QpThresholds { |
| 84 | QpThresholds(int l, int h) : low(l), high(h) {} |
| 85 | QpThresholds() : low(-1), high(-1) {} |
| 86 | int low; |
| 87 | int high; |
| 88 | }; |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 89 | // Quality scaling is enabled if thresholds are provided. |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 90 | struct ScalingSettings { |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 91 | private: |
| 92 | // Private magic type for kOff, implicitly convertible to |
| 93 | // ScalingSettings. |
| 94 | struct KOff {}; |
| 95 | |
| 96 | public: |
| 97 | // TODO(nisse): Would be nicer if kOff were a constant ScalingSettings |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 98 | // rather than a magic value. However, absl::optional is not trivially copy |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 99 | // constructible, and hence a constant ScalingSettings needs a static |
| 100 | // initializer, which is strongly discouraged in Chrome. We can hopefully |
| 101 | // fix this when we switch to absl::optional or std::optional. |
| 102 | static constexpr KOff kOff = {}; |
| 103 | |
| 104 | ScalingSettings(int low, int high); |
| 105 | ScalingSettings(int low, int high, int min_pixels); |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 106 | ScalingSettings(const ScalingSettings&); |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 107 | ScalingSettings(KOff); // NOLINT(runtime/explicit) |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 108 | ~ScalingSettings(); |
| 109 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 110 | absl::optional<QpThresholds> thresholds; |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 111 | |
| 112 | // We will never ask for a resolution lower than this. |
| 113 | // TODO(kthelgason): Lower this limit when better testing |
| 114 | // on MediaCodec and fallback implementations are in place. |
| 115 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 116 | int min_pixels_per_frame = 320 * 180; |
Niels Möller | 225c787 | 2018-02-22 15:03:53 +0100 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | // Private constructor; to get an object without thresholds, use |
| 120 | // the magic constant ScalingSettings::kOff. |
| 121 | ScalingSettings(); |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 122 | }; |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 123 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 124 | // Struct containing metadata about the encoder implementing this interface. |
| 125 | struct EncoderInfo { |
Erik Språng | dbdd839 | 2019-01-17 15:27:50 +0100 | [diff] [blame] | 126 | static constexpr uint8_t kMaxFramerateFraction = |
| 127 | std::numeric_limits<uint8_t>::max(); |
| 128 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 129 | EncoderInfo(); |
Mirta Dvornicic | 897a991 | 2018-11-30 13:12:21 +0100 | [diff] [blame] | 130 | EncoderInfo(const EncoderInfo&); |
| 131 | |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 132 | ~EncoderInfo(); |
| 133 | |
| 134 | // Any encoder implementation wishing to use the WebRTC provided |
| 135 | // quality scaler must populate this field. |
| 136 | ScalingSettings scaling_settings; |
| 137 | |
| 138 | // If true, encoder supports working with a native handle (e.g. texture |
| 139 | // handle for hw codecs) rather than requiring a raw I420 buffer. |
| 140 | bool supports_native_handle; |
| 141 | |
| 142 | // The name of this particular encoder implementation, e.g. "libvpx". |
| 143 | std::string implementation_name; |
Erik Språng | d3438aa | 2018-11-08 16:56:43 +0100 | [diff] [blame] | 144 | |
| 145 | // If this field is true, the encoder rate controller must perform |
| 146 | // well even in difficult situations, and produce close to the specified |
| 147 | // target bitrate seen over a reasonable time window, drop frames if |
| 148 | // necessary in order to keep the rate correct, and react quickly to |
| 149 | // changing bitrate targets. If this method returns true, we disable the |
| 150 | // frame dropper in the media optimization module and rely entirely on the |
| 151 | // encoder to produce media at a bitrate that closely matches the target. |
| 152 | // Any overshooting may result in delay buildup. If this method returns |
| 153 | // false (default behavior), the media opt frame dropper will drop input |
| 154 | // frames if it suspect encoder misbehavior. Misbehavior is common, |
| 155 | // especially in hardware codecs. Disable media opt at your own risk. |
| 156 | bool has_trusted_rate_controller; |
Mirta Dvornicic | 897a991 | 2018-11-30 13:12:21 +0100 | [diff] [blame] | 157 | |
| 158 | // If this field is true, the encoder uses hardware support and different |
| 159 | // thresholds will be used in CPU adaptation. |
| 160 | bool is_hardware_accelerated; |
| 161 | |
| 162 | // If this field is true, the encoder uses internal camera sources, meaning |
| 163 | // that it does not require/expect frames to be delivered via |
| 164 | // webrtc::VideoEncoder::Encode. |
| 165 | // Internal source encoders are deprecated and support for them will be |
| 166 | // phased out. |
| 167 | bool has_internal_source; |
Erik Språng | dbdd839 | 2019-01-17 15:27:50 +0100 | [diff] [blame] | 168 | |
| 169 | // For each spatial layer (simulcast stream or SVC layer), represented as an |
| 170 | // element in |fps_allocation| a vector indicates how many temporal layers |
| 171 | // the encoder is using for that spatial layer. |
| 172 | // For each spatial/temporal layer pair, the frame rate fraction is given as |
| 173 | // an 8bit unsigned integer where 0 = 0% and 255 = 100%. |
| 174 | // |
| 175 | // If the vector is empty for a given spatial layer, it indicates that frame |
| 176 | // rates are not defined and we can't count on any specific frame rate to be |
| 177 | // generated. Likely this indicates Vp8TemporalLayersType::kBitrateDynamic. |
| 178 | // |
| 179 | // The encoder may update this on a per-frame basis in response to both |
| 180 | // internal and external signals. |
| 181 | // |
| 182 | // Spatial layers are treated independently, but temporal layers are |
| 183 | // cumulative. For instance, if: |
| 184 | // fps_allocation[0][0] = kFullFramerate / 2; |
| 185 | // fps_allocation[0][1] = kFullFramerate; |
| 186 | // Then half of the frames are in the base layer and half is in TL1, but |
| 187 | // since TL1 is assumed to depend on the base layer, the frame rate is |
| 188 | // indicated as the full 100% for the top layer. |
| 189 | // |
| 190 | // Defaults to a single spatial layer containing a single temporal layer |
| 191 | // with a 100% frame rate fraction. |
| 192 | absl::InlinedVector<uint8_t, kMaxTemporalStreams> |
| 193 | fps_allocation[kMaxSpatialLayers]; |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 194 | }; |
| 195 | |
Erik Språng | 4d9df38 | 2019-03-27 15:00:43 +0100 | [diff] [blame] | 196 | struct RateControlParameters { |
| 197 | // Target bitrate, per spatial/temporal layer. |
| 198 | // A target bitrate of 0bps indicates a layer should not be encoded at all. |
| 199 | VideoBitrateAllocation bitrate; |
| 200 | // Target framerate, in fps. A value <= 0.0 is invalid and should be |
| 201 | // interpreted as framerate target not available. In this case the encoder |
| 202 | // should fall back to the max framerate specified in |codec_settings| of |
| 203 | // the last InitEncode() call. |
| 204 | double framerate_fps; |
| 205 | // The network bandwidth available for video. This is at least |
| 206 | // |bitrate.get_sum_bps()|, but may be higher if the application is not |
| 207 | // network constrained. |
| 208 | DataRate bandwidth_allocation; |
| 209 | }; |
| 210 | |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 211 | static VideoCodecVP8 GetDefaultVp8Settings(); |
| 212 | static VideoCodecVP9 GetDefaultVp9Settings(); |
| 213 | static VideoCodecH264 GetDefaultH264Settings(); |
| 214 | |
| 215 | virtual ~VideoEncoder() {} |
| 216 | |
| 217 | // Initialize the encoder with the information from the codecSettings |
| 218 | // |
| 219 | // Input: |
| 220 | // - codec_settings : Codec settings |
| 221 | // - number_of_cores : Number of cores available for the encoder |
| 222 | // - max_payload_size : The maximum size each payload is allowed |
| 223 | // to have. Usually MTU - overhead. |
| 224 | // |
| 225 | // Return value : Set bit rate if OK |
| 226 | // <0 - Errors: |
| 227 | // WEBRTC_VIDEO_CODEC_ERR_PARAMETER |
| 228 | // WEBRTC_VIDEO_CODEC_ERR_SIZE |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 229 | // WEBRTC_VIDEO_CODEC_MEMORY |
| 230 | // WEBRTC_VIDEO_CODEC_ERROR |
| 231 | virtual int32_t InitEncode(const VideoCodec* codec_settings, |
| 232 | int32_t number_of_cores, |
| 233 | size_t max_payload_size) = 0; |
| 234 | |
| 235 | // Register an encode complete callback object. |
| 236 | // |
| 237 | // Input: |
| 238 | // - callback : Callback object which handles encoded images. |
| 239 | // |
| 240 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
| 241 | virtual int32_t RegisterEncodeCompleteCallback( |
| 242 | EncodedImageCallback* callback) = 0; |
| 243 | |
| 244 | // Free encoder memory. |
| 245 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
| 246 | virtual int32_t Release() = 0; |
| 247 | |
| 248 | // Encode an I420 image (as a part of a video stream). The encoded image |
| 249 | // will be returned to the user through the encode complete callback. |
| 250 | // |
| 251 | // Input: |
| 252 | // - frame : Image to be encoded |
| 253 | // - frame_types : Frame type to be generated by the encoder. |
| 254 | // |
| 255 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK |
| 256 | // <0 - Errors: |
| 257 | // WEBRTC_VIDEO_CODEC_ERR_PARAMETER |
| 258 | // WEBRTC_VIDEO_CODEC_MEMORY |
| 259 | // WEBRTC_VIDEO_CODEC_ERROR |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 260 | virtual int32_t Encode(const VideoFrame& frame, |
Niels Möller | 9d766b9 | 2019-03-28 09:19:35 +0100 | [diff] [blame] | 261 | const std::vector<VideoFrameType>* frame_types) = 0; |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 262 | |
Erik Språng | 4d9df38 | 2019-03-27 15:00:43 +0100 | [diff] [blame] | 263 | // DEPRECATED! Instead use the one below: |
| 264 | // void SetRateAllocation(const VideoBitrateAllocation&, DataRate, uint32) |
| 265 | // For now has a default implementation that call RTC_NOTREACHED(). |
| 266 | // TODO(bugs.webrtc.org/10481): Remove this once all usage is gone. |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 267 | virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate); |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 268 | |
Erik Språng | 4d9df38 | 2019-03-27 15:00:43 +0100 | [diff] [blame] | 269 | // DEPRECATED! Instead, use void SetRates(const RateControlParameters&); |
| 270 | // For now has a default implementation that calls |
| 271 | // int32_t SetRates(uin32_t, uint32_t) with |allocation.get_sum_kbps()| and |
| 272 | // |framerate| as arguments. This will be removed. |
| 273 | // TODO(bugs.webrtc.org/10481): Remove this once all usage is gone. |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 274 | virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation, |
mflodman | 351424e | 2017-08-10 02:43:14 -0700 | [diff] [blame] | 275 | uint32_t framerate); |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 276 | |
Erik Språng | 4d9df38 | 2019-03-27 15:00:43 +0100 | [diff] [blame] | 277 | // Sets rate control parameters: bitrate, framerate, etc. These settings are |
| 278 | // instantaneous (i.e. not moving averages) and should apply from now until |
| 279 | // the next call to SetRates(). |
| 280 | // Default implementation will call SetRateAllocation() with appropriate |
| 281 | // members of |parameters| as parameters. |
| 282 | virtual void SetRates(const RateControlParameters& parameters); |
| 283 | |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 284 | // Inform the encoder when the packet loss rate changes. |
| 285 | // |
| 286 | // Input: - packet_loss_rate : The packet loss rate (0.0 to 1.0). |
| 287 | virtual void OnPacketLossRateUpdate(float packet_loss_rate); |
| 288 | |
| 289 | // Inform the encoder when the round trip time changes. |
| 290 | // |
| 291 | // Input: - rtt_ms : The new RTT, in milliseconds. |
| 292 | virtual void OnRttUpdate(int64_t rtt_ms); |
| 293 | |
Erik Språng | d3438aa | 2018-11-08 16:56:43 +0100 | [diff] [blame] | 294 | // Returns meta-data about the encoder, such as implementation name. |
| 295 | // The output of this method may change during runtime. For instance if a |
| 296 | // hardware encoder fails, it may fall back to doing software encoding using |
| 297 | // an implementation with different characteristics. |
Erik Språng | e2fd86a | 2018-10-24 11:32:39 +0200 | [diff] [blame] | 298 | virtual EncoderInfo GetEncoderInfo() const; |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 299 | }; |
ilnik | d60d06a | 2017-04-05 03:02:20 -0700 | [diff] [blame] | 300 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 301 | #endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_ |