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