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