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