blob: cce2dbda53f6f57c6504c735e325fd4db915c941 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/video/video_frame.h"
Niels Möller802506c2018-05-31 10:44:51 +020022#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
Mirko Bonadei276827c2018-10-16 14:13:50 +020024#include "rtc_base/system/rtc_export.h"
ilnikd60d06a2017-04-05 03:02:20 -070025
26namespace webrtc {
27
28class RTPFragmentationHeader;
29// TODO(pbos): Expose these through a public (root) header or change these APIs.
30struct CodecSpecificInfo;
ilnikd60d06a2017-04-05 03:02:20 -070031
32class 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
mflodman351424e2017-08-10 02:43:14 -070044 explicit Result(Error error) : error(error) {}
ilnikd60d06a2017-04-05 03:02:20 -070045 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020059 // 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
ilnikd60d06a2017-04-05 03:02:20 -070068 // 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020074 virtual void OnDroppedFrame(DropReason reason) {}
ilnikd60d06a2017-04-05 03:02:20 -070075};
76
Mirko Bonadei276827c2018-10-16 14:13:50 +020077class RTC_EXPORT VideoEncoder {
ilnikd60d06a2017-04-05 03:02:20 -070078 public:
ilnikd60d06a2017-04-05 03:02:20 -070079 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öller225c7872018-02-22 15:03:53 +010085 // Quality scaling is enabled if thresholds are provided.
ilnikd60d06a2017-04-05 03:02:20 -070086 struct ScalingSettings {
Niels Möller225c7872018-02-22 15:03:53 +010087 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 Chapovalov0bc58cf2018-06-21 13:32:56 +020094 // rather than a magic value. However, absl::optional is not trivially copy
Niels Möller225c7872018-02-22 15:03:53 +010095 // 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);
mflodman351424e2017-08-10 02:43:14 -0700102 ScalingSettings(const ScalingSettings&);
Niels Möller225c7872018-02-22 15:03:53 +0100103 ScalingSettings(KOff); // NOLINT(runtime/explicit)
mflodman351424e2017-08-10 02:43:14 -0700104 ~ScalingSettings();
105
Erik Språnge2fd86a2018-10-24 11:32:39 +0200106 absl::optional<QpThresholds> thresholds;
asapersson142fcc92017-08-17 08:58:54 -0700107
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ånge2fd86a2018-10-24 11:32:39 +0200112 int min_pixels_per_frame = 320 * 180;
Niels Möller225c7872018-02-22 15:03:53 +0100113
114 private:
115 // Private constructor; to get an object without thresholds, use
116 // the magic constant ScalingSettings::kOff.
117 ScalingSettings();
ilnikd60d06a2017-04-05 03:02:20 -0700118 };
ilnikd60d06a2017-04-05 03:02:20 -0700119
Erik Språnge2fd86a2018-10-24 11:32:39 +0200120 // Struct containing metadata about the encoder implementing this interface.
121 struct EncoderInfo {
122 EncoderInfo();
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100123 EncoderInfo(const EncoderInfo&);
124
Erik Språnge2fd86a2018-10-24 11:32:39 +0200125 ~EncoderInfo();
126
127 // Any encoder implementation wishing to use the WebRTC provided
128 // quality scaler must populate this field.
129 ScalingSettings scaling_settings;
130
131 // If true, encoder supports working with a native handle (e.g. texture
132 // handle for hw codecs) rather than requiring a raw I420 buffer.
133 bool supports_native_handle;
134
135 // The name of this particular encoder implementation, e.g. "libvpx".
136 std::string implementation_name;
Erik Språngd3438aa2018-11-08 16:56:43 +0100137
138 // If this field is true, the encoder rate controller must perform
139 // well even in difficult situations, and produce close to the specified
140 // target bitrate seen over a reasonable time window, drop frames if
141 // necessary in order to keep the rate correct, and react quickly to
142 // changing bitrate targets. If this method returns true, we disable the
143 // frame dropper in the media optimization module and rely entirely on the
144 // encoder to produce media at a bitrate that closely matches the target.
145 // Any overshooting may result in delay buildup. If this method returns
146 // false (default behavior), the media opt frame dropper will drop input
147 // frames if it suspect encoder misbehavior. Misbehavior is common,
148 // especially in hardware codecs. Disable media opt at your own risk.
149 bool has_trusted_rate_controller;
Mirta Dvornicic897a9912018-11-30 13:12:21 +0100150
151 // If this field is true, the encoder uses hardware support and different
152 // thresholds will be used in CPU adaptation.
153 bool is_hardware_accelerated;
154
155 // If this field is true, the encoder uses internal camera sources, meaning
156 // that it does not require/expect frames to be delivered via
157 // webrtc::VideoEncoder::Encode.
158 // Internal source encoders are deprecated and support for them will be
159 // phased out.
160 bool has_internal_source;
Erik Språnge2fd86a2018-10-24 11:32:39 +0200161 };
162
ilnikd60d06a2017-04-05 03:02:20 -0700163 static VideoCodecVP8 GetDefaultVp8Settings();
164 static VideoCodecVP9 GetDefaultVp9Settings();
165 static VideoCodecH264 GetDefaultH264Settings();
166
167 virtual ~VideoEncoder() {}
168
169 // Initialize the encoder with the information from the codecSettings
170 //
171 // Input:
172 // - codec_settings : Codec settings
173 // - number_of_cores : Number of cores available for the encoder
174 // - max_payload_size : The maximum size each payload is allowed
175 // to have. Usually MTU - overhead.
176 //
177 // Return value : Set bit rate if OK
178 // <0 - Errors:
179 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
180 // WEBRTC_VIDEO_CODEC_ERR_SIZE
ilnikd60d06a2017-04-05 03:02:20 -0700181 // WEBRTC_VIDEO_CODEC_MEMORY
182 // WEBRTC_VIDEO_CODEC_ERROR
183 virtual int32_t InitEncode(const VideoCodec* codec_settings,
184 int32_t number_of_cores,
185 size_t max_payload_size) = 0;
186
187 // Register an encode complete callback object.
188 //
189 // Input:
190 // - callback : Callback object which handles encoded images.
191 //
192 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
193 virtual int32_t RegisterEncodeCompleteCallback(
194 EncodedImageCallback* callback) = 0;
195
196 // Free encoder memory.
197 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
198 virtual int32_t Release() = 0;
199
200 // Encode an I420 image (as a part of a video stream). The encoded image
201 // will be returned to the user through the encode complete callback.
202 //
203 // Input:
204 // - frame : Image to be encoded
205 // - frame_types : Frame type to be generated by the encoder.
206 //
207 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
208 // <0 - Errors:
209 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
210 // WEBRTC_VIDEO_CODEC_MEMORY
211 // WEBRTC_VIDEO_CODEC_ERROR
ilnikd60d06a2017-04-05 03:02:20 -0700212 virtual int32_t Encode(const VideoFrame& frame,
213 const CodecSpecificInfo* codec_specific_info,
214 const std::vector<FrameType>* frame_types) = 0;
215
ilnikd60d06a2017-04-05 03:02:20 -0700216 // Inform the encoder about the new target bit rate.
217 //
218 // Input:
219 // - bitrate : New target bit rate
220 // - framerate : The target frame rate
221 //
222 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
mflodman351424e2017-08-10 02:43:14 -0700223 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700224
225 // Default fallback: Just use the sum of bitrates as the single target rate.
226 // TODO(sprang): Remove this default implementation when we remove SetRates().
Erik Språng566124a2018-04-23 12:32:22 +0200227 virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700228 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700229
Erik Språngd3438aa2018-11-08 16:56:43 +0100230 // Returns meta-data about the encoder, such as implementation name.
231 // The output of this method may change during runtime. For instance if a
232 // hardware encoder fails, it may fall back to doing software encoding using
233 // an implementation with different characteristics.
Erik Språnge2fd86a2018-10-24 11:32:39 +0200234 virtual EncoderInfo GetEncoderInfo() const;
ilnikd60d06a2017-04-05 03:02:20 -0700235};
ilnikd60d06a2017-04-05 03:02:20 -0700236} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200237#endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_