blob: 19c77b62ba20d47a58040a39b520b006a6d73e39 [file] [log] [blame]
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001/*
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 */
11
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#ifndef MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
13#define MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_
philipelcce46fc2015-12-21 03:04:49 -080014
Mirko Bonadei95adedb2018-11-19 09:52:37 +010015#ifdef RTC_ENABLE_VP9
16
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020017#include <map>
kwiberg3f55dea2016-02-29 05:51:59 -080018#include <memory>
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +010019#include <string>
philipelcce46fc2015-12-21 03:04:49 -080020#include <vector>
marpan@webrtc.org5b883172014-11-01 06:10:48 +000021
Elad Alon8f01c4e2019-06-28 15:19:43 +020022#include "api/fec_controller_override.h"
Elad Alon370f93a2019-06-11 14:57:57 +020023#include "api/video_codecs/video_encoder.h"
Emircan Uysaler98badbc2018-06-28 10:59:02 -070024#include "media/base/vp9_profile.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020025#include "modules/video_coding/codecs/vp9/include/vp9.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/video_coding/codecs/vp9/vp9_frame_buffer_pool.h"
Sergey Silkinae3144c2018-08-29 01:05:26 +020027#include "modules/video_coding/utility/framerate_controller.h"
johannkoenig8225c402017-01-26 13:23:44 -080028#include "vpx/vp8cx.h"
pbos@webrtc.orge728ee02014-12-17 13:43:55 +000029#include "vpx/vpx_decoder.h"
30#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000031
32namespace webrtc {
33
34class VP9EncoderImpl : public VP9Encoder {
35 public:
Emircan Uysaler98badbc2018-06-28 10:59:02 -070036 explicit VP9EncoderImpl(const cricket::VideoCodec& codec);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000037
Elad Alon8f01c4e2019-06-28 15:19:43 +020038 ~VP9EncoderImpl() override;
39
40 void SetFecControllerOverride(
41 FecControllerOverride* fec_controller_override) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000042
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000043 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000044
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000045 int InitEncode(const VideoCodec* codec_settings,
Elad Alon370f93a2019-06-11 14:57:57 +020046 const Settings& settings) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000047
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070048 int Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +010049 const std::vector<VideoFrameType>* frame_types) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000050
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000051 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000052
Erik Språng16cb8f52019-04-12 13:59:09 +020053 void SetRates(const RateControlParameters& parameters) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000054
Erik Språng727d1642018-11-07 16:54:15 +010055 EncoderInfo GetEncoderInfo() const override;
Peter Boströmb7d9a972015-12-18 16:01:11 +010056
marpan@webrtc.org5b883172014-11-01 06:10:48 +000057 private:
marpan@webrtc.org38d11b82015-01-26 15:21:36 +000058 // Determine number of encoder threads to use.
59 int NumberOfThreads(int width, int height, int number_of_cores);
60
marpan@webrtc.org5b883172014-11-01 06:10:48 +000061 // Call encoder initialize function and set control settings.
62 int InitAndSetControlSettings(const VideoCodec* inst);
63
64 void PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
Niels Möllerd3b8c632018-08-27 15:33:42 +020065 absl::optional<int>* spatial_idx,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000066 const vpx_codec_cx_pkt& pkt,
Sergey Silkin88ce4ef2018-11-23 13:59:24 +010067 uint32_t timestamp);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020068 void FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
69 const size_t pic_num,
70 const bool inter_layer_predicted,
71 CodecSpecificInfoVP9* vp9_info);
72 void UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
73 const size_t pic_num);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010074 vpx_svc_ref_frame_config_t SetReferences(
75 bool is_key_pic,
76 size_t first_active_spatial_layer_id);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000077
sprangce4aef12015-11-02 07:23:20 -080078 bool ExplicitlyConfiguredSpatialLayers() const;
Erik Språng566124a2018-04-23 12:32:22 +020079 bool SetSvcRates(const VideoBitrateAllocation& bitrate_allocation);
asaperssona9455ab2015-07-31 06:10:09 -070080
81 virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt);
82
83 // Callback function for outputting packets per spatial layer.
84 static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
85 void* user_data);
marpan@webrtc.org5b883172014-11-01 06:10:48 +000086
Sergey Silkinbc0f0d32018-04-24 21:29:14 +020087 void DeliverBufferedFrame(bool end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +020088
Sergey Silkinae3144c2018-08-29 01:05:26 +020089 bool DropFrame(uint8_t spatial_idx, uint32_t rtp_timestamp);
Sergey Silkinbe71a1e2018-05-17 16:46:43 +020090
marpan@webrtc.org5b883172014-11-01 06:10:48 +000091 // Determine maximum target for Intra frames
92 //
93 // Input:
94 // - optimal_buffer_size : Optimal buffer size
95 // Return Value : Max target size for Intra frames represented as
96 // percentage of the per frame bandwidth
97 uint32_t MaxIntraTarget(uint32_t optimal_buffer_size);
98
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +010099 size_t SteadyStateSize(int sid, int tid);
100
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000101 EncodedImage encoded_image_;
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200102 CodecSpecificInfo codec_specific_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000103 EncodedImageCallback* encoded_complete_callback_;
104 VideoCodec codec_;
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700105 const VP9Profile profile_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000106 bool inited_;
107 int64_t timestamp_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000108 int cpu_speed_;
109 uint32_t rc_max_intra_target_;
110 vpx_codec_ctx_t* encoder_;
111 vpx_codec_enc_cfg_t* config_;
112 vpx_image_t* raw_;
johannkoenig8225c402017-01-26 13:23:44 -0800113 vpx_svc_extra_cfg_t svc_params_;
asaperssona9455ab2015-07-31 06:10:09 -0700114 const VideoFrame* input_image_;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200115 GofInfoVP9 gof_; // Contains each frame's temporal information for
116 // non-flexible mode.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200117 bool force_key_frame_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200118 size_t pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700119 uint8_t num_temporal_layers_;
“Michael23c5a992018-06-21 11:07:21 -0500120 uint8_t num_spatial_layers_; // Number of configured SLs
121 uint8_t num_active_spatial_layers_; // Number of actively encoded SLs
Sergey Silkine7ce8882018-10-03 18:04:57 +0200122 bool layer_deactivation_requires_key_frame_;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200123 bool is_svc_;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200124 InterLayerPredMode inter_layer_pred_;
Sergey Silkin390f3582018-10-01 17:13:50 +0200125 bool external_ref_control_;
Erik Språngd3438aa2018-11-08 16:56:43 +0100126 const bool trusted_rate_controller_;
Erik Språng7a3fe892019-04-15 12:22:55 +0200127 const bool dynamic_rate_settings_;
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +0200128 bool layer_buffering_;
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100129 const bool full_superframe_drop_;
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200130 vpx_svc_frame_drop_t svc_drop_frame_;
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100131 bool first_frame_in_picture_;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100132 VideoBitrateAllocation current_bitrate_allocation_;
Ilya Nikolaevskiy90d6efb2019-09-25 09:06:50 +0000133 absl::optional<RateControlParameters> requested_rate_settings_;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100134 bool ss_info_needed_;
philipelcfc319b2015-11-10 07:17:23 -0800135
Sergey Silkin96f2c972018-09-05 21:07:17 +0200136 std::vector<FramerateController> framerate_controller_;
Sergey Silkind902d582018-05-18 17:31:19 +0200137
philipelcfc319b2015-11-10 07:17:23 -0800138 // Used for flexible mode.
139 bool is_flexible_mode_;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200140 struct RefFrameBuffer {
141 RefFrameBuffer(size_t pic_num,
142 size_t spatial_layer_id,
143 size_t temporal_layer_id)
144 : pic_num(pic_num),
145 spatial_layer_id(spatial_layer_id),
146 temporal_layer_id(temporal_layer_id) {}
147 RefFrameBuffer() {}
Sergey Silkin82276592018-08-27 14:54:20 +0200148
149 bool operator==(const RefFrameBuffer& o) {
150 return pic_num == o.pic_num && spatial_layer_id == o.spatial_layer_id &&
151 temporal_layer_id == o.temporal_layer_id;
152 }
153
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200154 size_t pic_num = 0;
155 size_t spatial_layer_id = 0;
156 size_t temporal_layer_id = 0;
157 };
158 std::map<size_t, RefFrameBuffer> ref_buf_;
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +0100159
160 // Variable frame-rate related fields and methods.
161 const struct VariableFramerateExperiment {
162 bool enabled;
163 // Framerate is limited to this value in steady state.
164 float framerate_limit;
165 // This qp or below is considered a steady state.
166 int steady_state_qp;
167 // Frames of at least this percentage below ideal for configured bitrate are
168 // considered in a steady state.
169 int steady_state_undershoot_percentage;
170 // Number of consecutive frames with good QP and size required to detect
171 // the steady state.
172 int frames_before_steady_state;
173 } variable_framerate_experiment_;
174 static VariableFramerateExperiment ParseVariableFramerateConfig(
175 std::string group_name);
176 FramerateController variable_framerate_controller_;
177 int num_steady_state_frames_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000178};
179
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000180class VP9DecoderImpl : public VP9Decoder {
181 public:
182 VP9DecoderImpl();
183
184 virtual ~VP9DecoderImpl();
185
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000186 int InitDecode(const VideoCodec* inst, int number_of_cores) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000187
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000188 int Decode(const EncodedImage& input_image,
189 bool missing_frames,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000190 int64_t /*render_time_ms*/) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000191
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000192 int RegisterDecodeCompleteCallback(DecodedImageCallback* callback) override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000193
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000194 int Release() override;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000195
Peter Boströmb7d9a972015-12-18 16:01:11 +0100196 const char* ImplementationName() const override;
197
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000198 private:
Ilya Nikolaevskiya0e26092019-07-12 11:20:47 +0200199 int ReturnFrame(const vpx_image_t* img,
200 uint32_t timestamp,
201 int qp,
202 const webrtc::ColorSpace* explicit_color_space);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000203
Henrik Boström9695d852015-05-06 10:42:15 +0200204 // Memory pool used to share buffers between libvpx and webrtc.
205 Vp9FrameBufferPool frame_buffer_pool_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000206 DecodedImageCallback* decode_complete_callback_;
207 bool inited_;
pbos@webrtc.orge728ee02014-12-17 13:43:55 +0000208 vpx_codec_ctx_t* decoder_;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000209 bool key_frame_required_;
210};
211} // namespace webrtc
212
Mirko Bonadei95adedb2018-11-19 09:52:37 +0100213#endif // RTC_ENABLE_VP9
214
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200215#endif // MODULES_VIDEO_CODING_CODECS_VP9_VP9_IMPL_H_