blob: b2f9a39baa948765818ee6b40aa465a15009ad81 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/optional.h"
Erik Språngec475652018-05-15 15:12:55 +020019#include "api/video/video_bitrate_allocation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/video_frame.h"
Niels Möller802506c2018-05-31 10:44:51 +020021#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_video/include/video_frame.h"
23#include "rtc_base/checks.h"
ilnikd60d06a2017-04-05 03:02:20 -070024
25namespace webrtc {
26
27class RTPFragmentationHeader;
28// TODO(pbos): Expose these through a public (root) header or change these APIs.
29struct CodecSpecificInfo;
ilnikd60d06a2017-04-05 03:02:20 -070030
31class 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
mflodman351424e2017-08-10 02:43:14 -070043 explicit Result(Error error) : error(error) {}
ilnikd60d06a2017-04-05 03:02:20 -070044 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020058 // 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
ilnikd60d06a2017-04-05 03:02:20 -070067 // 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 Nikolaevskiyd79314f2017-10-23 10:45:37 +020073 virtual void OnDroppedFrame(DropReason reason) {}
ilnikd60d06a2017-04-05 03:02:20 -070074};
75
76class VideoEncoder {
77 public:
ilnikd60d06a2017-04-05 03:02:20 -070078 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öller225c7872018-02-22 15:03:53 +010084 // Quality scaling is enabled if thresholds are provided.
ilnikd60d06a2017-04-05 03:02:20 -070085 struct ScalingSettings {
Niels Möller225c7872018-02-22 15:03:53 +010086 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
93 // rather than a magic value. However, rtc::Optional is not trivially copy
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);
mflodman351424e2017-08-10 02:43:14 -0700101 ScalingSettings(const ScalingSettings&);
Niels Möller225c7872018-02-22 15:03:53 +0100102 ScalingSettings(KOff); // NOLINT(runtime/explicit)
mflodman351424e2017-08-10 02:43:14 -0700103 ~ScalingSettings();
104
ilnikd60d06a2017-04-05 03:02:20 -0700105 const rtc::Optional<QpThresholds> thresholds;
asapersson142fcc92017-08-17 08:58:54 -0700106
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öller225c7872018-02-22 15:03:53 +0100112
113 private:
114 // Private constructor; to get an object without thresholds, use
115 // the magic constant ScalingSettings::kOff.
116 ScalingSettings();
ilnikd60d06a2017-04-05 03:02:20 -0700117 };
ilnikd60d06a2017-04-05 03:02:20 -0700118
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.
mflodman351424e2017-08-10 02:43:14 -0700192 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700193
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ång566124a2018-04-23 12:32:22 +0200196 virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700197 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700198
199 // Any encoder implementation wishing to use the WebRTC provided
200 // quality scaler must implement this method.
mflodman351424e2017-08-10 02:43:14 -0700201 virtual ScalingSettings GetScalingSettings() const;
ilnikd60d06a2017-04-05 03:02:20 -0700202
mflodman351424e2017-08-10 02:43:14 -0700203 virtual bool SupportsNativeHandle() const;
204 virtual const char* ImplementationName() const;
ilnikd60d06a2017-04-05 03:02:20 -0700205};
ilnikd60d06a2017-04-05 03:02:20 -0700206} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200207#endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_