blob: cd897d0d9588248a60484e89e8b26709f021ad80 [file] [log] [blame]
Sami Kalliomäkie2410e92017-06-02 14:46:12 +02001/*
2 * Copyright 2017 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
11package org.webrtc;
12
Artem Titarenko69540f42018-12-10 12:30:46 +010013import android.support.annotation.Nullable;
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010014import org.webrtc.EncodedImage;
15
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020016/**
17 * Interface for a video encoder that can be used with WebRTC. All calls will be made on the
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +020018 * encoding thread. The encoder may be constructed on a different thread and changing thread after
19 * calling release is allowed.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020020 */
21public interface VideoEncoder {
22 /** Settings passed to the encoder by WebRTC. */
23 public class Settings {
24 public final int numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070025 public final int width;
26 public final int height;
27 public final int startBitrate; // Kilobits per second.
28 public final int maxFramerate;
Rasmus Brandt85f20cb2018-08-23 13:52:45 +020029 public final int numberOfSimulcastStreams;
sakal07a3bd72017-09-04 03:57:21 -070030 public final boolean automaticResizeOn;
Elad Alon370f93a2019-06-11 14:57:57 +020031 public final Capabilities capabilities;
32
33 // TODO(bugs.webrtc.org/10720): Remove.
34 @Deprecated
35 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
36 int numberOfSimulcastStreams, boolean automaticResizeOn) {
37 this(numberOfCores, width, height, startBitrate, maxFramerate, numberOfSimulcastStreams,
38 automaticResizeOn, new VideoEncoder.Capabilities(false /* lossNotification */));
39 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020040
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010041 @CalledByNative("Settings")
sakal07a3bd72017-09-04 03:57:21 -070042 public Settings(int numberOfCores, int width, int height, int startBitrate, int maxFramerate,
Elad Alon370f93a2019-06-11 14:57:57 +020043 int numberOfSimulcastStreams, boolean automaticResizeOn, Capabilities capabilities) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020044 this.numberOfCores = numberOfCores;
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070045 this.width = width;
46 this.height = height;
47 this.startBitrate = startBitrate;
48 this.maxFramerate = maxFramerate;
Rasmus Brandt85f20cb2018-08-23 13:52:45 +020049 this.numberOfSimulcastStreams = numberOfSimulcastStreams;
sakal07a3bd72017-09-04 03:57:21 -070050 this.automaticResizeOn = automaticResizeOn;
Elad Alon370f93a2019-06-11 14:57:57 +020051 this.capabilities = capabilities;
52 }
53 }
54
55 /** Capabilities (loss notification, etc.) passed to the encoder by WebRTC. */
56 public class Capabilities {
57 /**
58 * The remote side has support for the loss notification RTCP feedback message format, and will
59 * be sending these feedback messages if necessary.
60 */
61 public final boolean lossNotification;
62
63 @CalledByNative("Capabilities")
64 public Capabilities(boolean lossNotification) {
65 this.lossNotification = lossNotification;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020066 }
67 }
68
69 /** Additional info for encoding. */
70 public class EncodeInfo {
71 public final EncodedImage.FrameType[] frameTypes;
72
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +010073 @CalledByNative("EncodeInfo")
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020074 public EncodeInfo(EncodedImage.FrameType[] frameTypes) {
75 this.frameTypes = frameTypes;
76 }
77 }
78
79 // TODO(sakal): Add values to these classes as necessary.
80 /** Codec specific information about the encoded frame. */
81 public class CodecSpecificInfo {}
82
83 public class CodecSpecificInfoVP8 extends CodecSpecificInfo {}
84
85 public class CodecSpecificInfoVP9 extends CodecSpecificInfo {}
86
87 public class CodecSpecificInfoH264 extends CodecSpecificInfo {}
88
Yura Yaroshevich3fb51d22021-04-12 15:41:21 +030089 public class CodecSpecificInfoAV1 extends CodecSpecificInfo {}
90
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020091 /**
92 * Represents bitrate allocated for an encoder to produce frames. Bitrate can be divided between
93 * spatial and temporal layers.
94 */
95 public class BitrateAllocation {
96 // First index is the spatial layer and second the temporal layer.
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -070097 public final int[][] bitratesBbs;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +020098
99 /**
100 * Initializes the allocation with a two dimensional array of bitrates. The first index of the
101 * array is the spatial layer and the second index in the temporal layer.
102 */
Magnus Jedvert1f2a3e72017-11-23 16:56:44 +0100103 @CalledByNative("BitrateAllocation")
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -0700104 public BitrateAllocation(int[][] bitratesBbs) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200105 this.bitratesBbs = bitratesBbs;
106 }
107
108 /**
109 * Gets the total bitrate allocated for all layers.
110 */
Bjorn Mellem5c4eebb2017-06-12 09:21:03 -0700111 public int getSum() {
112 int sum = 0;
113 for (int[] spatialLayer : bitratesBbs) {
114 for (int bitrate : spatialLayer) {
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200115 sum += bitrate;
116 }
117 }
118 return sum;
119 }
120 }
121
122 /** Settings for WebRTC quality based scaling. */
123 public class ScalingSettings {
124 public final boolean on;
Sami Kalliomäkie7592d82018-03-22 13:32:44 +0100125 @Nullable public final Integer low;
126 @Nullable public final Integer high;
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200127
128 /**
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100129 * Settings to disable quality based scaling.
130 */
131 public static final ScalingSettings OFF = new ScalingSettings();
132
133 /**
134 * Creates settings to enable quality based scaling.
135 *
136 * @param low Average QP at which to scale up the resolution.
137 * @param high Average QP at which to scale down the resolution.
138 */
139 public ScalingSettings(int low, int high) {
140 this.on = true;
141 this.low = low;
142 this.high = high;
143 }
144
145 private ScalingSettings() {
146 this.on = false;
147 this.low = null;
148 this.high = null;
149 }
150
151 // TODO(bugs.webrtc.org/8830): Below constructors are deprecated.
152 // Default thresholds are going away, so thresholds have to be set
153 // when scaling is on.
154 /**
sakal07a3bd72017-09-04 03:57:21 -0700155 * Creates quality based scaling setting.
156 *
157 * @param on True if quality scaling is turned on.
158 */
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100159 @Deprecated
sakal07a3bd72017-09-04 03:57:21 -0700160 public ScalingSettings(boolean on) {
161 this.on = on;
162 this.low = null;
163 this.high = null;
164 }
165
166 /**
167 * Creates quality based scaling settings with custom thresholds.
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200168 *
169 * @param on True if quality scaling is turned on.
170 * @param low Average QP at which to scale up the resolution.
171 * @param high Average QP at which to scale down the resolution.
172 */
Niels Möllerd0dd90b2018-02-08 10:15:34 +0100173 @Deprecated
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200174 public ScalingSettings(boolean on, int low, int high) {
175 this.on = on;
176 this.low = low;
177 this.high = high;
178 }
Sami Kalliomäki8619e8a2018-04-17 14:44:36 +0200179
180 @Override
181 public String toString() {
182 return on ? "[ " + low + ", " + high + " ]" : "OFF";
183 }
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200184 }
185
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200186 /**
Sergey Silkin3d642f82019-07-03 15:09:33 +0200187 * Bitrate limits for resolution.
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200188 */
Sergey Silkin3d642f82019-07-03 15:09:33 +0200189 public class ResolutionBitrateLimits {
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200190 /**
Sergey Silkin3d642f82019-07-03 15:09:33 +0200191 * Maximum size of video frame, in pixels, the bitrate limits are intended for.
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200192 */
193 public final int frameSizePixels;
194
195 /**
196 * Recommended minimum bitrate to start encoding.
197 */
198 public final int minStartBitrateBps;
199
200 /**
201 * Recommended minimum bitrate.
202 */
203 public final int minBitrateBps;
204
205 /**
206 * Recommended maximum bitrate.
207 */
208 public final int maxBitrateBps;
209
Sergey Silkin3d642f82019-07-03 15:09:33 +0200210 public ResolutionBitrateLimits(
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200211 int frameSizePixels, int minStartBitrateBps, int minBitrateBps, int maxBitrateBps) {
212 this.frameSizePixels = frameSizePixels;
213 this.minStartBitrateBps = minStartBitrateBps;
214 this.minBitrateBps = minBitrateBps;
215 this.maxBitrateBps = maxBitrateBps;
216 }
217
Sergey Silkin3d642f82019-07-03 15:09:33 +0200218 @CalledByNative("ResolutionBitrateLimits")
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200219 public int getFrameSizePixels() {
220 return frameSizePixels;
221 }
222
Sergey Silkin3d642f82019-07-03 15:09:33 +0200223 @CalledByNative("ResolutionBitrateLimits")
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200224 public int getMinStartBitrateBps() {
225 return minStartBitrateBps;
226 }
227
Sergey Silkin3d642f82019-07-03 15:09:33 +0200228 @CalledByNative("ResolutionBitrateLimits")
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200229 public int getMinBitrateBps() {
230 return minBitrateBps;
231 }
232
Sergey Silkin3d642f82019-07-03 15:09:33 +0200233 @CalledByNative("ResolutionBitrateLimits")
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200234 public int getMaxBitrateBps() {
235 return maxBitrateBps;
236 }
237 }
238
Sergey Silkin1db921e2021-08-18 09:46:16 +0200239 /** Rate control parameters. */
240 public class RateControlParameters {
241 /**
242 * Adjusted target bitrate, per spatial/temporal layer. May be lower or higher than the target
243 * depending on encoder behaviour.
244 */
245 public final BitrateAllocation bitrate;
246
247 /**
248 * Target framerate, in fps. A value <= 0.0 is invalid and should be interpreted as framerate
249 * target not available. In this case the encoder should fall back to the max framerate
250 * specified in `codec_settings` of the last InitEncode() call.
251 */
252 public final double framerateFps;
253
254 @CalledByNative("RateControlParameters")
255 public RateControlParameters(BitrateAllocation bitrate, double framerateFps) {
256 this.bitrate = bitrate;
257 this.framerateFps = framerateFps;
258 }
259 }
260
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200261 public interface Callback {
Sami Kalliomäki11c51dd2018-02-07 12:50:47 +0100262 /**
Artem Titovd7ac5812021-07-27 12:23:39 +0200263 * Old encoders assume that the byte buffer held by `frame` is not accessed after the call to
Niels Möller67309ef2019-09-23 12:47:16 +0200264 * this method returns. If the pipeline downstream needs to hold on to the buffer, it then has
265 * to make its own copy. We want to move to a model where no copying is needed, and instead use
266 * retain()/release() to signal to the encoder when it is safe to reuse the buffer.
267 *
268 * Over the transition, implementations of this class should use the maybeRetain() method if
269 * they want to keep a reference to the buffer, and fall back to copying if that method returns
270 * false.
Sami Kalliomäki11c51dd2018-02-07 12:50:47 +0100271 */
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200272 void onEncodedFrame(EncodedImage frame, CodecSpecificInfo info);
273 }
274
275 /**
Rasmus Brandt42a2fc92018-07-09 13:38:01 +0200276 * The encoder implementation backing this interface is either 1) a Java
277 * encoder (e.g., an Android platform encoder), or alternatively 2) a native
278 * encoder (e.g., a software encoder or a C++ encoder adapter).
279 *
280 * For case 1), createNativeVideoEncoder() should return zero.
281 * In this case, we expect the native library to call the encoder through
282 * JNI using the Java interface declared below.
283 *
284 * For case 2), createNativeVideoEncoder() should return a non-zero value.
285 * In this case, we expect the native library to treat the returned value as
286 * a raw pointer of type webrtc::VideoEncoder* (ownership is transferred to
287 * the caller). The native library should then directly call the
288 * webrtc::VideoEncoder interface without going through JNI. All calls to
289 * the Java interface methods declared below should thus throw an
290 * UnsupportedOperationException.
291 */
292 @CalledByNative
293 default long createNativeVideoEncoder() {
294 return 0;
295 }
296
297 /**
298 * Returns true if the encoder is backed by hardware.
299 */
300 @CalledByNative
301 default boolean isHardwareEncoder() {
302 return true;
303 }
304
305 /**
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200306 * Initializes the encoding process. Call before any calls to encode.
307 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100308 @CalledByNative VideoCodecStatus initEncode(Settings settings, Callback encodeCallback);
309
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200310 /**
311 * Releases the encoder. No more calls to encode will be made after this call.
312 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100313 @CalledByNative VideoCodecStatus release();
314
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200315 /**
316 * Requests the encoder to encode a frame.
317 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100318 @CalledByNative VideoCodecStatus encode(VideoFrame frame, EncodeInfo info);
319
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200320 /** Sets the bitrate allocation and the target framerate for the encoder. */
Sergey Silkin1db921e2021-08-18 09:46:16 +0200321 VideoCodecStatus setRateAllocation(BitrateAllocation allocation, int framerate);
322
323 /** Sets the bitrate allocation and the target framerate for the encoder. */
324 default @CalledByNative VideoCodecStatus setRates(RateControlParameters rcParameters) {
325 // Round frame rate up to avoid overshoots.
326 int framerateFps = (int) Math.ceil(rcParameters.framerateFps);
327 return setRateAllocation(rcParameters.bitrate, framerateFps);
328 }
Magnus Jedvert56231d02017-10-31 17:47:06 +0100329
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200330 /** Any encoder that wants to use WebRTC provided quality scaler must implement this method. */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100331 @CalledByNative ScalingSettings getScalingSettings();
332
Sergey Silkin3d642f82019-07-03 15:09:33 +0200333 /** Returns the list of bitrate limits. */
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200334 @CalledByNative
Sergey Silkin3d642f82019-07-03 15:09:33 +0200335 default ResolutionBitrateLimits[] getResolutionBitrateLimits() {
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200336 // TODO(ssilkin): Update downstream projects and remove default implementation.
Sergey Silkin3d642f82019-07-03 15:09:33 +0200337 ResolutionBitrateLimits bitrate_limits[] = {};
338 return bitrate_limits;
Sergey Silkinbe0adee2019-06-26 14:11:03 +0200339 }
340
Sami Kalliomäki5f5fc682017-10-19 11:34:08 +0200341 /**
342 * Should return a descriptive name for the implementation. Gets called once and cached. May be
343 * called from arbitrary thread.
344 */
Magnus Jedvert56231d02017-10-31 17:47:06 +0100345 @CalledByNative String getImplementationName();
Sami Kalliomäkie2410e92017-06-02 14:46:12 +0200346}