blob: 59dc55fb30bad769b49f12b7372053aad4712486 [file] [log] [blame]
ilnikd60d06a2017-04-05 03:02:20 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_VIDEO_CODECS_VIDEO_ENCODER_H_
12#define API_VIDEO_CODECS_VIDEO_ENCODER_H_
ilnikd60d06a2017-04-05 03:02:20 -070013
Erik Språngdbdd8392019-01-17 15:27:50 +010014#include <limits>
ilnikd60d06a2017-04-05 03:02:20 -070015#include <memory>
16#include <string>
17#include <vector>
18
Erik Språngdbdd8392019-01-17 15:27:50 +010019#include "absl/container/inlined_vector.h"
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020020#include "absl/types/optional.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020021#include "api/video/encoded_image.h"
Erik Språngec475652018-05-15 15:12:55 +020022#include "api/video/video_bitrate_allocation.h"
Erik Språngf93eda12019-01-16 17:10:57 +010023#include "api/video/video_codec_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/video/video_frame.h"
Niels Möller802506c2018-05-31 10:44:51 +020025#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
Mirko Bonadei276827c2018-10-16 14:13:50 +020027#include "rtc_base/system/rtc_export.h"
ilnikd60d06a2017-04-05 03:02:20 -070028
29namespace webrtc {
30
31class RTPFragmentationHeader;
32// TODO(pbos): Expose these through a public (root) header or change these APIs.
33struct CodecSpecificInfo;
ilnikd60d06a2017-04-05 03:02:20 -070034
35class 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
mflodman351424e2017-08-10 02:43:14 -070047 explicit Result(Error error) : error(error) {}
ilnikd60d06a2017-04-05 03:02:20 -070048 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020062 // 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
ilnikd60d06a2017-04-05 03:02:20 -070071 // 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020077 virtual void OnDroppedFrame(DropReason reason) {}
ilnikd60d06a2017-04-05 03:02:20 -070078};
79
Mirko Bonadei276827c2018-10-16 14:13:50 +020080class RTC_EXPORT VideoEncoder {
ilnikd60d06a2017-04-05 03:02:20 -070081 public:
ilnikd60d06a2017-04-05 03:02:20 -070082 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öller225c7872018-02-22 15:03:53 +010088 // Quality scaling is enabled if thresholds are provided.
ilnikd60d06a2017-04-05 03:02:20 -070089 struct ScalingSettings {
Niels Möller225c7872018-02-22 15:03:53 +010090 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 Chapovalov0bc58cf2018-06-21 13:32:56 +020097 // rather than a magic value. However, absl::optional is not trivially copy
Niels Möller225c7872018-02-22 15:03:53 +010098 // 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);
mflodman351424e2017-08-10 02:43:14 -0700105 ScalingSettings(const ScalingSettings&);
Niels Möller225c7872018-02-22 15:03:53 +0100106 ScalingSettings(KOff); // NOLINT(runtime/explicit)
mflodman351424e2017-08-10 02:43:14 -0700107 ~ScalingSettings();
108
Erik Språnge2fd86a2018-10-24 11:32:39 +0200109 absl::optional<QpThresholds> thresholds;
asapersson142fcc92017-08-17 08:58:54 -0700110
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ånge2fd86a2018-10-24 11:32:39 +0200115 int min_pixels_per_frame = 320 * 180;
Niels Möller225c7872018-02-22 15:03:53 +0100116
117 private:
118 // Private constructor; to get an object without thresholds, use
119 // the magic constant ScalingSettings::kOff.
120 ScalingSettings();
ilnikd60d06a2017-04-05 03:02:20 -0700121 };
ilnikd60d06a2017-04-05 03:02:20 -0700122
Erik Språnge2fd86a2018-10-24 11:32:39 +0200123 // Struct containing metadata about the encoder implementing this interface.
124 struct EncoderInfo {
Erik Språngdbdd8392019-01-17 15:27:50 +0100125 static constexpr uint8_t kMaxFramerateFraction =
126 std::numeric_limits<uint8_t>::max();
127
Erik Språnge2fd86a2018-10-24 11:32:39 +0200128 EncoderInfo();
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100129 EncoderInfo(const EncoderInfo&);
130
Erik Språnge2fd86a2018-10-24 11:32:39 +0200131 ~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ångd3438aa2018-11-08 16:56:43 +0100143
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 Dvornicic897a9912018-11-30 13:12:21 +0100156
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ångdbdd8392019-01-17 15:27:50 +0100167
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ånge2fd86a2018-10-24 11:32:39 +0200193 };
194
ilnikd60d06a2017-04-05 03:02:20 -0700195 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
ilnikd60d06a2017-04-05 03:02:20 -0700213 // 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
ilnikd60d06a2017-04-05 03:02:20 -0700244 virtual int32_t Encode(const VideoFrame& frame,
Niels Möllerc8d2e732019-03-06 12:00:33 +0100245 const std::vector<FrameType>* frame_types);
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,
ilnikd60d06a2017-04-05 03:02:20 -0700249 const CodecSpecificInfo* codec_specific_info,
Niels Möllerc8d2e732019-03-06 12:00:33 +0100250 const std::vector<FrameType>* frame_types);
ilnikd60d06a2017-04-05 03:02:20 -0700251
ilnikd60d06a2017-04-05 03:02:20 -0700252 // 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.
mflodman351424e2017-08-10 02:43:14 -0700259 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700260
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ång566124a2018-04-23 12:32:22 +0200263 virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700264 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700265
Erik Språngd3438aa2018-11-08 16:56:43 +0100266 // 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ånge2fd86a2018-10-24 11:32:39 +0200270 virtual EncoderInfo GetEncoderInfo() const;
ilnikd60d06a2017-04-05 03:02:20 -0700271};
ilnikd60d06a2017-04-05 03:02:20 -0700272} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200273#endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_