blob: 2856b1c3d4224c429248b305e6f0b08e832037c4 [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
14#include <memory>
15#include <string>
16#include <vector>
17
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020018#include "absl/types/optional.h"
Niels Möller4dc66c52018-10-05 14:17:58 +020019#include "api/video/encoded_image.h"
Erik Språngec475652018-05-15 15:12:55 +020020#include "api/video/video_bitrate_allocation.h"
Erik Språngf93eda12019-01-16 17:10:57 +010021#include "api/video/video_codec_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/video_frame.h"
Niels Möller802506c2018-05-31 10:44:51 +020023#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
Mirko Bonadei276827c2018-10-16 14:13:50 +020025#include "rtc_base/system/rtc_export.h"
ilnikd60d06a2017-04-05 03:02:20 -070026
27namespace webrtc {
28
29class RTPFragmentationHeader;
30// TODO(pbos): Expose these through a public (root) header or change these APIs.
31struct CodecSpecificInfo;
ilnikd60d06a2017-04-05 03:02:20 -070032
33class 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
mflodman351424e2017-08-10 02:43:14 -070045 explicit Result(Error error) : error(error) {}
ilnikd60d06a2017-04-05 03:02:20 -070046 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020060 // 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
ilnikd60d06a2017-04-05 03:02:20 -070069 // 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020075 virtual void OnDroppedFrame(DropReason reason) {}
ilnikd60d06a2017-04-05 03:02:20 -070076};
77
Mirko Bonadei276827c2018-10-16 14:13:50 +020078class RTC_EXPORT VideoEncoder {
ilnikd60d06a2017-04-05 03:02:20 -070079 public:
ilnikd60d06a2017-04-05 03:02:20 -070080 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öller225c7872018-02-22 15:03:53 +010086 // Quality scaling is enabled if thresholds are provided.
ilnikd60d06a2017-04-05 03:02:20 -070087 struct ScalingSettings {
Niels Möller225c7872018-02-22 15:03:53 +010088 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 Chapovalov0bc58cf2018-06-21 13:32:56 +020095 // rather than a magic value. However, absl::optional is not trivially copy
Niels Möller225c7872018-02-22 15:03:53 +010096 // 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);
mflodman351424e2017-08-10 02:43:14 -0700103 ScalingSettings(const ScalingSettings&);
Niels Möller225c7872018-02-22 15:03:53 +0100104 ScalingSettings(KOff); // NOLINT(runtime/explicit)
mflodman351424e2017-08-10 02:43:14 -0700105 ~ScalingSettings();
106
Erik Språnge2fd86a2018-10-24 11:32:39 +0200107 absl::optional<QpThresholds> thresholds;
asapersson142fcc92017-08-17 08:58:54 -0700108
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ånge2fd86a2018-10-24 11:32:39 +0200113 int min_pixels_per_frame = 320 * 180;
Niels Möller225c7872018-02-22 15:03:53 +0100114
115 private:
116 // Private constructor; to get an object without thresholds, use
117 // the magic constant ScalingSettings::kOff.
118 ScalingSettings();
ilnikd60d06a2017-04-05 03:02:20 -0700119 };
ilnikd60d06a2017-04-05 03:02:20 -0700120
Erik Språnge2fd86a2018-10-24 11:32:39 +0200121 // Struct containing metadata about the encoder implementing this interface.
122 struct EncoderInfo {
123 EncoderInfo();
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100124 EncoderInfo(const EncoderInfo&);
125
Erik Språnge2fd86a2018-10-24 11:32:39 +0200126 ~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ångd3438aa2018-11-08 16:56:43 +0100138
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 Dvornicic897a9912018-11-30 13:12:21 +0100151
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ånge2fd86a2018-10-24 11:32:39 +0200162 };
163
ilnikd60d06a2017-04-05 03:02:20 -0700164 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
ilnikd60d06a2017-04-05 03:02:20 -0700182 // 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
ilnikd60d06a2017-04-05 03:02:20 -0700213 virtual int32_t Encode(const VideoFrame& frame,
214 const CodecSpecificInfo* codec_specific_info,
215 const std::vector<FrameType>* frame_types) = 0;
216
ilnikd60d06a2017-04-05 03:02:20 -0700217 // 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.
mflodman351424e2017-08-10 02:43:14 -0700224 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700225
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ång566124a2018-04-23 12:32:22 +0200228 virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700229 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700230
Erik Språngd3438aa2018-11-08 16:56:43 +0100231 // 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ånge2fd86a2018-10-24 11:32:39 +0200235 virtual EncoderInfo GetEncoderInfo() const;
ilnikd60d06a2017-04-05 03:02:20 -0700236};
ilnikd60d06a2017-04-05 03:02:20 -0700237} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200238#endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_