| 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 API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
| 12 | #define API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
| 13 |
| 14 | #include <limits> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/container/inlined_vector.h" |
| 20 | #include "absl/types/optional.h" |
| 21 | #include "api/fec_controller_override.h" |
| 22 | #include "api/units/data_rate.h" |
| 23 | #include "api/video/encoded_image.h" |
| 24 | #include "api/video/video_bitrate_allocation.h" |
| 25 | #include "api/video/video_codec_constants.h" |
| 26 | #include "api/video/video_frame.h" |
| 27 | #include "api/video_codecs/video_codec.h" |
| 28 | #include "rtc_base/checks.h" |
| 29 | #include "rtc_base/system/rtc_export.h" |
| 30 |
| 31 | namespace webrtc { |
| 32 |
| 33 | class RTPFragmentationHeader; |
| 34 | // TODO(pbos): Expose these through a public (root) header or change these APIs. |
| 35 | struct CodecSpecificInfo; |
| 36 |
| 37 | constexpr int kDefaultMinPixelsPerFrame = 320 * 180; |
| 38 |
| 39 | class RTC_EXPORT EncodedImageCallback { |
| 40 | public: |
| 41 | virtual ~EncodedImageCallback() {} |
| 42 |
| 43 | struct Result { |
| 44 | enum Error { |
| 45 | OK, |
| 46 |
| 47 | // Failed to send the packet. |
| 48 | ERROR_SEND_FAILED, |
| 49 | }; |
| 50 |
| 51 | explicit Result(Error error) : error(error) {} |
| 52 | Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {} |
| 53 |
| 54 | Error error; |
| 55 |
| 56 | // Frame ID assigned to the frame. The frame ID should be the same as the ID |
| 57 | // seen by the receiver for this frame. RTP timestamp of the frame is used |
| 58 | // as frame ID when RTP is used to send video. Must be used only when |
| 59 | // error=OK. |
| 60 | uint32_t frame_id = 0; |
| 61 |
| 62 | // Tells the encoder that the next frame is should be dropped. |
| 63 | bool drop_next_frame = false; |
| 64 | }; |
| 65 |
| 66 | // Used to signal the encoder about reason a frame is dropped. |
| 67 | // kDroppedByMediaOptimizations - dropped by MediaOptimizations (for rate |
| 68 | // limiting purposes). |
| 69 | // kDroppedByEncoder - dropped by encoder's internal rate limiter. |
| 70 | enum class DropReason : uint8_t { |
| 71 | kDroppedByMediaOptimizations, |
| 72 | kDroppedByEncoder |
| 73 | }; |
| 74 |
| 75 | // Callback function which is called when an image has been encoded. |
| 76 | // Deprecated, use OnEncodedImage below instead, see bugs.webrtc.org/6471 |
| 77 | virtual Result OnEncodedImage(const EncodedImage& encoded_image, |
| 78 | const CodecSpecificInfo* codec_specific_info, |
| 79 | const RTPFragmentationHeader* fragmentation); |
| 80 |
| 81 | // Callback function which is called when an image has been encoded. |
| 82 | // TODO(bugs.webrtc.org/6471): Make pure virtual |
| 83 | // when OnEncodedImage above is deleted. |
| 84 | virtual Result OnEncodedImage(const EncodedImage& encoded_image, |
| 85 | const CodecSpecificInfo* codec_specific_info); |
| 86 |
| 87 | virtual void OnDroppedFrame(DropReason reason) {} |
| 88 | }; |
| 89 |
| 90 | class RTC_EXPORT VideoEncoder { |
| 91 | public: |
| 92 | struct QpThresholds { |
| 93 | QpThresholds(int l, int h) : low(l), high(h) {} |
| 94 | QpThresholds() : low(-1), high(-1) {} |
| 95 | int low; |
| 96 | int high; |
| 97 | }; |
| 98 |
| 99 | // Quality scaling is enabled if thresholds are provided. |
| 100 | struct RTC_EXPORT ScalingSettings { |
| 101 | private: |
| 102 | // Private magic type for kOff, implicitly convertible to |
| 103 | // ScalingSettings. |
| 104 | struct KOff {}; |
| 105 |
| 106 | public: |
| 107 | // TODO(nisse): Would be nicer if kOff were a constant ScalingSettings |
| 108 | // rather than a magic value. However, absl::optional is not trivially copy |
| 109 | // constructible, and hence a constant ScalingSettings needs a static |
| 110 | // initializer, which is strongly discouraged in Chrome. We can hopefully |
| 111 | // fix this when we switch to absl::optional or std::optional. |
| 112 | static constexpr KOff kOff = {}; |
| 113 |
| 114 | ScalingSettings(int low, int high); |
| 115 | ScalingSettings(int low, int high, int min_pixels); |
| 116 | ScalingSettings(const ScalingSettings&); |
| 117 | ScalingSettings(KOff); // NOLINT(runtime/explicit) |
| 118 | ~ScalingSettings(); |
| 119 |
| 120 | absl::optional<QpThresholds> thresholds; |
| 121 |
| 122 | // We will never ask for a resolution lower than this. |
| 123 | // TODO(kthelgason): Lower this limit when better testing |
| 124 | // on MediaCodec and fallback implementations are in place. |
| 125 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
| 126 | int min_pixels_per_frame = kDefaultMinPixelsPerFrame; |
| 127 |
| 128 | private: |
| 129 | // Private constructor; to get an object without thresholds, use |
| 130 | // the magic constant ScalingSettings::kOff. |
| 131 | ScalingSettings(); |
| 132 | }; |
| 133 |
| 134 | // Bitrate limits for resolution. |
| 135 | struct ResolutionBitrateLimits { |
| 136 | ResolutionBitrateLimits(int frame_size_pixels, |
| 137 | int min_start_bitrate_bps, |
| 138 | int min_bitrate_bps, |
| 139 | int max_bitrate_bps) |
| 140 | : frame_size_pixels(frame_size_pixels), |
| 141 | min_start_bitrate_bps(min_start_bitrate_bps), |
| 142 | min_bitrate_bps(min_bitrate_bps), |
| 143 | max_bitrate_bps(max_bitrate_bps) {} |
| 144 | // Size of video frame, in pixels, the bitrate thresholds are intended for. |
| 145 | int frame_size_pixels = 0; |
| 146 | // Recommended minimum bitrate to start encoding. |
| 147 | int min_start_bitrate_bps = 0; |
| 148 | // Recommended minimum bitrate. |
| 149 | int min_bitrate_bps = 0; |
| 150 | // Recommended maximum bitrate. |
| 151 | int max_bitrate_bps = 0; |
| 152 |
| 153 | bool operator==(const ResolutionBitrateLimits& rhs) const; |
| 154 | bool operator!=(const ResolutionBitrateLimits& rhs) const { |
| 155 | return !(*this == rhs); |
| 156 | } |
| 157 | }; |
| 158 |
| 159 | // Struct containing metadata about the encoder implementing this interface. |
| 160 | struct RTC_EXPORT EncoderInfo { |
| 161 | static constexpr uint8_t kMaxFramerateFraction = |
| 162 | std::numeric_limits<uint8_t>::max(); |
| 163 |
| 164 | EncoderInfo(); |
| 165 | EncoderInfo(const EncoderInfo&); |
| 166 |
| 167 | ~EncoderInfo(); |
| 168 |
| 169 | std::string ToString() const; |
| 170 | bool operator==(const EncoderInfo& rhs) const; |
| 171 | bool operator!=(const EncoderInfo& rhs) const { return !(*this == rhs); } |
| 172 |
| 173 | // Any encoder implementation wishing to use the WebRTC provided |
| 174 | // quality scaler must populate this field. |
| 175 | ScalingSettings scaling_settings; |
| 176 |
| 177 | // The width and height of the incoming video frames should be divisible |
| 178 | // by |requested_resolution_alignment|. If they are not, the encoder may |
| 179 | // drop the incoming frame. |
| 180 | // For example: With I420, this value would be a multiple of 2. |
| 181 | // Note that this field is unrelated to any horizontal or vertical stride |
| 182 | // requirements the encoder has on the incoming video frame buffers. |
| 183 | int requested_resolution_alignment; |
| 184 |
| 185 | // If true, encoder supports working with a native handle (e.g. texture |
| 186 | // handle for hw codecs) rather than requiring a raw I420 buffer. |
| 187 | bool supports_native_handle; |
| 188 |
| 189 | // The name of this particular encoder implementation, e.g. "libvpx". |
| 190 | std::string implementation_name; |
| 191 |
| 192 | // If this field is true, the encoder rate controller must perform |
| 193 | // well even in difficult situations, and produce close to the specified |
| 194 | // target bitrate seen over a reasonable time window, drop frames if |
| 195 | // necessary in order to keep the rate correct, and react quickly to |
| 196 | // changing bitrate targets. If this method returns true, we disable the |
| 197 | // frame dropper in the media optimization module and rely entirely on the |
| 198 | // encoder to produce media at a bitrate that closely matches the target. |
| 199 | // Any overshooting may result in delay buildup. If this method returns |
| 200 | // false (default behavior), the media opt frame dropper will drop input |
| 201 | // frames if it suspect encoder misbehavior. Misbehavior is common, |
| 202 | // especially in hardware codecs. Disable media opt at your own risk. |
| 203 | bool has_trusted_rate_controller; |
| 204 |
| 205 | // If this field is true, the encoder uses hardware support and different |
| 206 | // thresholds will be used in CPU adaptation. |
| 207 | bool is_hardware_accelerated; |
| 208 |
| 209 | // If this field is true, the encoder uses internal camera sources, meaning |
| 210 | // that it does not require/expect frames to be delivered via |
| 211 | // webrtc::VideoEncoder::Encode. |
| 212 | // Internal source encoders are deprecated and support for them will be |
| 213 | // phased out. |
| 214 | bool has_internal_source; |
| 215 |
| 216 | // For each spatial layer (simulcast stream or SVC layer), represented as an |
| 217 | // element in |fps_allocation| a vector indicates how many temporal layers |
| 218 | // the encoder is using for that spatial layer. |
| 219 | // For each spatial/temporal layer pair, the frame rate fraction is given as |
| 220 | // an 8bit unsigned integer where 0 = 0% and 255 = 100%. |
| 221 | // |
| 222 | // If the vector is empty for a given spatial layer, it indicates that frame |
| 223 | // rates are not defined and we can't count on any specific frame rate to be |
| 224 | // generated. Likely this indicates Vp8TemporalLayersType::kBitrateDynamic. |
| 225 | // |
| 226 | // The encoder may update this on a per-frame basis in response to both |
| 227 | // internal and external signals. |
| 228 | // |
| 229 | // Spatial layers are treated independently, but temporal layers are |
| 230 | // cumulative. For instance, if: |
| 231 | // fps_allocation[0][0] = kFullFramerate / 2; |
| 232 | // fps_allocation[0][1] = kFullFramerate; |
| 233 | // Then half of the frames are in the base layer and half is in TL1, but |
| 234 | // since TL1 is assumed to depend on the base layer, the frame rate is |
| 235 | // indicated as the full 100% for the top layer. |
| 236 | // |
| 237 | // Defaults to a single spatial layer containing a single temporal layer |
| 238 | // with a 100% frame rate fraction. |
| 239 | absl::InlinedVector<uint8_t, kMaxTemporalStreams> |
| 240 | fps_allocation[kMaxSpatialLayers]; |
| 241 |
| 242 | // Recommended bitrate limits for different resolutions. |
| 243 | std::vector<ResolutionBitrateLimits> resolution_bitrate_limits; |
| 244 |
| 245 | // Obtains the limits from |resolution_bitrate_limits| that best matches the |
| 246 | // |frame_size_pixels|. |
| 247 | absl::optional<ResolutionBitrateLimits> |
| 248 | GetEncoderBitrateLimitsForResolution(int frame_size_pixels) const; |
| 249 |
| 250 | // If true, this encoder has internal support for generating simulcast |
| 251 | // streams. Otherwise, an adapter class will be needed. |
| 252 | // Even if true, the config provided to InitEncode() might not be supported, |
| 253 | // in such case the encoder should return |
| 254 | // WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED. |
| 255 | bool supports_simulcast; |
| 256 | }; |
| 257 |
| 258 | struct RTC_EXPORT RateControlParameters { |
| 259 | RateControlParameters(); |
| 260 | RateControlParameters(const VideoBitrateAllocation& bitrate, |
| 261 | double framerate_fps); |
| 262 | RateControlParameters(const VideoBitrateAllocation& bitrate, |
| 263 | double framerate_fps, |
| 264 | DataRate bandwidth_allocation); |
| 265 | virtual ~RateControlParameters(); |
| 266 |
| 267 | // Target bitrate, per spatial/temporal layer. |
| 268 | // A target bitrate of 0bps indicates a layer should not be encoded at all. |
| 269 | VideoBitrateAllocation bitrate; |
| 270 | // Target framerate, in fps. A value <= 0.0 is invalid and should be |
| 271 | // interpreted as framerate target not available. In this case the encoder |
| 272 | // should fall back to the max framerate specified in |codec_settings| of |
| 273 | // the last InitEncode() call. |
| 274 | double framerate_fps; |
| 275 | // The network bandwidth available for video. This is at least |
| 276 | // |bitrate.get_sum_bps()|, but may be higher if the application is not |
| 277 | // network constrained. |
| 278 | DataRate bandwidth_allocation; |
| 279 |
| 280 | bool operator==(const RateControlParameters& rhs) const; |
| 281 | bool operator!=(const RateControlParameters& rhs) const; |
| 282 | }; |
| 283 |
| 284 | struct LossNotification { |
| 285 | // The timestamp of the last decodable frame *prior* to the last received. |
| 286 | // (The last received - described below - might itself be decodable or not.) |
| 287 | uint32_t timestamp_of_last_decodable; |
| 288 | // The timestamp of the last received frame. |
| 289 | uint32_t timestamp_of_last_received; |
| 290 | // Describes whether the dependencies of the last received frame were |
| 291 | // all decodable. |
| 292 | // |false| if some dependencies were undecodable, |true| if all dependencies |
| 293 | // were decodable, and |nullopt| if the dependencies are unknown. |
| 294 | absl::optional<bool> dependencies_of_last_received_decodable; |
| 295 | // Describes whether the received frame was decodable. |
| 296 | // |false| if some dependency was undecodable or if some packet belonging |
| 297 | // to the last received frame was missed. |
| 298 | // |true| if all dependencies were decodable and all packets belonging |
| 299 | // to the last received frame were received. |
| 300 | // |nullopt| if no packet belonging to the last frame was missed, but the |
| 301 | // last packet in the frame was not yet received. |
| 302 | absl::optional<bool> last_received_decodable; |
| 303 | }; |
| 304 |
| 305 | // Negotiated capabilities which the VideoEncoder may expect the other |
| 306 | // side to use. |
| 307 | struct Capabilities { |
| 308 | explicit Capabilities(bool loss_notification) |
| 309 | : loss_notification(loss_notification) {} |
| 310 | bool loss_notification; |
| 311 | }; |
| 312 |
| 313 | struct Settings { |
| 314 | Settings(const Capabilities& capabilities, |
| 315 | int number_of_cores, |
| 316 | size_t max_payload_size) |
| 317 | : capabilities(capabilities), |
| 318 | number_of_cores(number_of_cores), |
| 319 | max_payload_size(max_payload_size) {} |
| 320 |
| 321 | Capabilities capabilities; |
| 322 | int number_of_cores; |
| 323 | size_t max_payload_size; |
| 324 | }; |
| 325 |
| 326 | static VideoCodecVP8 GetDefaultVp8Settings(); |
| 327 | static VideoCodecVP9 GetDefaultVp9Settings(); |
| 328 | static VideoCodecH264 GetDefaultH264Settings(); |
| 329 | #ifndef DISABLE_H265 |
| 330 | static VideoCodecH265 GetDefaultH265Settings(); |
| 331 | #endif |
| 332 |
| 333 | virtual ~VideoEncoder() {} |
| 334 |
| 335 | // Set a FecControllerOverride, through which the encoder may override |
| 336 | // decisions made by FecController. |
| 337 | // TODO(bugs.webrtc.org/10769): Update downstream, then make pure-virtual. |
| 338 | virtual void SetFecControllerOverride( |
| 339 | FecControllerOverride* fec_controller_override); |
| 340 |
| 341 | // Initialize the encoder with the information from the codecSettings |
| 342 | // |
| 343 | // Input: |
| 344 | // - codec_settings : Codec settings |
| 345 | // - settings : Settings affecting the encoding itself. |
| 346 | // Input for deprecated version: |
| 347 | // - number_of_cores : Number of cores available for the encoder |
| 348 | // - max_payload_size : The maximum size each payload is allowed |
| 349 | // to have. Usually MTU - overhead. |
| 350 | // |
| 351 | // Return value : Set bit rate if OK |
| 352 | // <0 - Errors: |
| 353 | // WEBRTC_VIDEO_CODEC_ERR_PARAMETER |
| 354 | // WEBRTC_VIDEO_CODEC_ERR_SIZE |
| 355 | // WEBRTC_VIDEO_CODEC_MEMORY |
| 356 | // WEBRTC_VIDEO_CODEC_ERROR |
| 357 | // TODO(bugs.webrtc.org/10720): After updating downstream projects and posting |
| 358 | // an announcement to discuss-webrtc, remove the three-parameters variant |
| 359 | // and make the two-parameters variant pure-virtual. |
| 360 | /* RTC_DEPRECATED */ virtual int32_t InitEncode( |
| 361 | const VideoCodec* codec_settings, |
| 362 | int32_t number_of_cores, |
| 363 | size_t max_payload_size); |
| 364 | virtual int InitEncode(const VideoCodec* codec_settings, |
| 365 | const VideoEncoder::Settings& settings); |
| 366 |
| 367 | // Register an encode complete callback object. |
| 368 | // |
| 369 | // Input: |
| 370 | // - callback : Callback object which handles encoded images. |
| 371 | // |
| 372 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
| 373 | virtual int32_t RegisterEncodeCompleteCallback( |
| 374 | EncodedImageCallback* callback) = 0; |
| 375 |
| 376 | // Free encoder memory. |
| 377 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
| 378 | virtual int32_t Release() = 0; |
| 379 |
| 380 | // Encode an I420 image (as a part of a video stream). The encoded image |
| 381 | // will be returned to the user through the encode complete callback. |
| 382 | // |
| 383 | // Input: |
| 384 | // - frame : Image to be encoded |
| 385 | // - frame_types : Frame type to be generated by the encoder. |
| 386 | // |
| 387 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK |
| 388 | // <0 - Errors: |
| 389 | // WEBRTC_VIDEO_CODEC_ERR_PARAMETER |
| 390 | // WEBRTC_VIDEO_CODEC_MEMORY |
| 391 | // WEBRTC_VIDEO_CODEC_ERROR |
| 392 | virtual int32_t Encode(const VideoFrame& frame, |
| 393 | const std::vector<VideoFrameType>* frame_types) = 0; |
| 394 |
| 395 | // Sets rate control parameters: bitrate, framerate, etc. These settings are |
| 396 | // instantaneous (i.e. not moving averages) and should apply from now until |
| 397 | // the next call to SetRates(). |
| 398 | virtual void SetRates(const RateControlParameters& parameters) = 0; |
| 399 |
| 400 | // Inform the encoder when the packet loss rate changes. |
| 401 | // |
| 402 | // Input: - packet_loss_rate : The packet loss rate (0.0 to 1.0). |
| 403 | virtual void OnPacketLossRateUpdate(float packet_loss_rate); |
| 404 |
| 405 | // Inform the encoder when the round trip time changes. |
| 406 | // |
| 407 | // Input: - rtt_ms : The new RTT, in milliseconds. |
| 408 | virtual void OnRttUpdate(int64_t rtt_ms); |
| 409 |
| 410 | // Called when a loss notification is received. |
| 411 | virtual void OnLossNotification(const LossNotification& loss_notification); |
| 412 |
| 413 | // Returns meta-data about the encoder, such as implementation name. |
| 414 | // The output of this method may change during runtime. For instance if a |
| 415 | // hardware encoder fails, it may fall back to doing software encoding using |
| 416 | // an implementation with different characteristics. |
| 417 | virtual EncoderInfo GetEncoderInfo() const; |
| 418 | }; |
| 419 | } // namespace webrtc |
| 420 | #endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
| 421 |