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