]>
Commit | Line | Data |
---|---|---|
6930f277 JB |
1 | diff -Nrup -U 8 a/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc b/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc |
2 | --- a/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc 2020-02-18 02:37:58.000000000 +0300 | |
3 | +++ b/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc 2020-03-02 00:34:19.000000000 +0300 | |
4 | @@ -79,17 +79,19 @@ VP9EncoderImpl::VP9EncoderImpl() | |
5 | num_temporal_layers_(0), | |
6 | num_spatial_layers_(0), | |
7 | num_cores_(0), | |
8 | is_flexible_mode_(false), | |
9 | frames_encoded_(0), | |
10 | // Use two spatial when screensharing with flexible mode. | |
11 | spatial_layer_(new ScreenshareLayersVP9(2)) { | |
12 | memset(&codec_, 0, sizeof(codec_)); | |
13 | +#ifdef LIBVPX_SVC | |
14 | memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t)); | |
15 | +#endif | |
16 | uint32_t seed = rtc::Time32(); | |
17 | srand(seed); | |
18 | } | |
19 | ||
20 | VP9EncoderImpl::~VP9EncoderImpl() { | |
21 | Release(); | |
22 | } | |
23 | ||
24 | @@ -115,55 +117,66 @@ int VP9EncoderImpl::Release() { | |
25 | } | |
26 | inited_ = false; | |
27 | return WEBRTC_VIDEO_CODEC_OK; | |
28 | } | |
29 | ||
30 | bool VP9EncoderImpl::ExplicitlyConfiguredSpatialLayers() const { | |
31 | // We check target_bitrate_bps of the 0th layer to see if the spatial layers | |
32 | // (i.e. bitrates) were explicitly configured. | |
33 | +#ifdef LIBVPX_SVC | |
34 | return num_spatial_layers_ > 1 && | |
35 | codec_.spatialLayers[0].target_bitrate_bps > 0; | |
36 | +#else | |
37 | + return false; | |
38 | +#endif | |
39 | } | |
40 | ||
41 | bool VP9EncoderImpl::SetSvcRates() { | |
42 | uint8_t i = 0; | |
43 | ||
44 | if (ExplicitlyConfiguredSpatialLayers()) { | |
45 | +#ifdef LIBVPX_SVC | |
46 | if (num_temporal_layers_ > 1) { | |
47 | LOG(LS_ERROR) << "Multiple temporal layers when manually specifying " | |
48 | "spatial layers not implemented yet!"; | |
49 | return false; | |
50 | } | |
51 | int total_bitrate_bps = 0; | |
52 | for (i = 0; i < num_spatial_layers_; ++i) | |
53 | total_bitrate_bps += codec_.spatialLayers[i].target_bitrate_bps; | |
54 | // If total bitrate differs now from what has been specified at the | |
55 | // beginning, update the bitrates in the same ratio as before. | |
56 | for (i = 0; i < num_spatial_layers_; ++i) { | |
57 | config_->ss_target_bitrate[i] = config_->layer_target_bitrate[i] = | |
58 | static_cast<int>(static_cast<int64_t>(config_->rc_target_bitrate) * | |
59 | codec_.spatialLayers[i].target_bitrate_bps / | |
60 | total_bitrate_bps); | |
61 | } | |
62 | +#endif | |
63 | } else { | |
64 | float rate_ratio[VPX_MAX_LAYERS] = {0}; | |
65 | float total = 0; | |
66 | ||
67 | +#ifdef LIBVPX_SVC | |
68 | for (i = 0; i < num_spatial_layers_; ++i) { | |
69 | if (svc_params_.scaling_factor_num[i] <= 0 || | |
70 | svc_params_.scaling_factor_den[i] <= 0) { | |
71 | LOG(LS_ERROR) << "Scaling factors not specified!"; | |
72 | return false; | |
73 | } | |
74 | rate_ratio[i] = | |
75 | static_cast<float>(svc_params_.scaling_factor_num[i]) / | |
76 | svc_params_.scaling_factor_den[i]; | |
77 | total += rate_ratio[i]; | |
78 | } | |
79 | +#else | |
80 | + rate_ratio[0] = 1; | |
81 | + total = 1; | |
82 | +#endif | |
83 | ||
84 | for (i = 0; i < num_spatial_layers_; ++i) { | |
85 | config_->ss_target_bitrate[i] = static_cast<unsigned int>( | |
86 | config_->rc_target_bitrate * rate_ratio[i] / total); | |
87 | if (num_temporal_layers_ == 1) { | |
88 | config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i]; | |
89 | } else if (num_temporal_layers_ == 2) { | |
90 | config_->layer_target_bitrate[i * num_temporal_layers_] = | |
91 | @@ -390,16 +403,17 @@ int VP9EncoderImpl::NumberOfThreads(int | |
92 | return 2; | |
93 | } else { | |
94 | // 1 thread less than VGA. | |
95 | return 1; | |
96 | } | |
97 | } | |
98 | ||
99 | int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | |
100 | +#ifdef LIBVPX_SVC | |
101 | // Set QP-min/max per spatial and temporal layer. | |
102 | int tot_num_layers = num_spatial_layers_ * num_temporal_layers_; | |
103 | for (int i = 0; i < tot_num_layers; ++i) { | |
104 | svc_params_.max_quantizers[i] = config_->rc_max_quantizer; | |
105 | svc_params_.min_quantizers[i] = config_->rc_min_quantizer; | |
106 | } | |
107 | config_->ss_number_layers = num_spatial_layers_; | |
108 | if (ExplicitlyConfiguredSpatialLayers()) { | |
109 | @@ -413,37 +427,41 @@ int VP9EncoderImpl::InitAndSetControlSet | |
110 | for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | |
111 | // 1:2 scaling in each dimension. | |
112 | svc_params_.scaling_factor_num[i] = scaling_factor_num; | |
113 | svc_params_.scaling_factor_den[i] = 256; | |
114 | if (codec_.mode != kScreensharing) | |
115 | scaling_factor_num /= 2; | |
116 | } | |
117 | } | |
118 | +#endif | |
119 | ||
120 | if (!SetSvcRates()) { | |
121 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | |
122 | } | |
123 | ||
124 | if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | |
125 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | |
126 | } | |
127 | vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | |
128 | vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT, | |
129 | rc_max_intra_target_); | |
130 | vpx_codec_control(encoder_, VP9E_SET_AQ_MODE, | |
131 | inst->VP9().adaptiveQpMode ? 3 : 0); | |
132 | ||
133 | +#ifdef LIBVPX_SVC | |
134 | vpx_codec_control( | |
135 | encoder_, VP9E_SET_SVC, | |
136 | (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) ? 1 : 0); | |
137 | if (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) { | |
138 | vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, | |
139 | &svc_params_); | |
140 | } | |
141 | +#endif | |
142 | + | |
143 | // Register callback for getting each spatial layer. | |
144 | vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = { | |
145 | VP9EncoderImpl::EncoderOutputCodedPacketCallback, | |
146 | reinterpret_cast<void*>(this)}; | |
147 | vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK, | |
148 | reinterpret_cast<void*>(&cbp)); | |
149 | ||
150 | // Control function to set the number of column tiles in encoding a frame, in | |
151 | @@ -527,16 +545,17 @@ int VP9EncoderImpl::Encode(const VideoFr | |
152 | ||
153 | vpx_enc_frame_flags_t flags = 0; | |
154 | bool send_keyframe = (frame_type == kVideoFrameKey); | |
155 | if (send_keyframe) { | |
156 | // Key frame request from caller. | |
157 | flags = VPX_EFLAG_FORCE_KF; | |
158 | } | |
159 | ||
160 | +#ifdef LIBVPX_SVC | |
161 | if (is_flexible_mode_) { | |
162 | SuperFrameRefSettings settings; | |
163 | ||
164 | // These structs are copied when calling vpx_codec_control, | |
165 | // therefore it is ok for them to go out of scope. | |
166 | vpx_svc_ref_frame_config enc_layer_conf; | |
167 | vpx_svc_layer_id layer_id; | |
168 | ||
169 | @@ -548,16 +567,17 @@ int VP9EncoderImpl::Encode(const VideoFr | |
170 | send_keyframe); | |
171 | } | |
172 | enc_layer_conf = GenerateRefsAndFlags(settings); | |
173 | layer_id.temporal_layer_id = 0; | |
174 | layer_id.spatial_layer_id = settings.start_layer; | |
175 | vpx_codec_control(encoder_, VP9E_SET_SVC_LAYER_ID, &layer_id); | |
176 | vpx_codec_control(encoder_, VP9E_SET_SVC_REF_FRAME_CONFIG, &enc_layer_conf); | |
177 | } | |
178 | +#endif | |
179 | ||
180 | assert(codec_.maxFramerate > 0); | |
181 | uint32_t duration = 90000 / codec_.maxFramerate; | |
182 | if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags, | |
183 | VPX_DL_REALTIME)) { | |
184 | return WEBRTC_VIDEO_CODEC_ERROR; | |
185 | } | |
186 | timestamp_ += duration; | |
187 | @@ -686,29 +706,31 @@ void VP9EncoderImpl::PopulateCodecSpecif | |
188 | vp9_info->p_diff[i] = p_diff_[layer_id.spatial_layer_id][i]; | |
189 | } | |
190 | } else { | |
191 | vp9_info->gof_idx = | |
192 | static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof); | |
193 | vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx]; | |
194 | } | |
195 | ||
196 | +#ifdef LIBVPX_SVC | |
197 | if (vp9_info->ss_data_available) { | |
198 | vp9_info->spatial_layer_resolution_present = true; | |
199 | for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { | |
200 | vp9_info->width[i] = codec_.width * | |
201 | svc_params_.scaling_factor_num[i] / | |
202 | svc_params_.scaling_factor_den[i]; | |
203 | vp9_info->height[i] = codec_.height * | |
204 | svc_params_.scaling_factor_num[i] / | |
205 | svc_params_.scaling_factor_den[i]; | |
206 | } | |
207 | - if (!vp9_info->flexible_mode) { | |
208 | - vp9_info->gof.CopyGofInfoVP9(gof_); | |
209 | - } | |
210 | + } | |
211 | +#endif | |
212 | + if (!vp9_info->flexible_mode) { | |
213 | + vp9_info->gof.CopyGofInfoVP9(gof_); | |
214 | } | |
215 | } | |
216 | ||
217 | int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) { | |
218 | RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT); | |
219 | ||
220 | if (pkt->data.frame.sz > encoded_image_._size) { | |
221 | delete[] encoded_image_._buffer; | |
222 | @@ -756,16 +778,17 @@ int VP9EncoderImpl::GetEncodedLayerFrame | |
223 | vpx_codec_control(encoder_, VP8E_GET_LAST_QUANTIZER, &qp); | |
224 | encoded_image_.qp_ = qp; | |
225 | encoded_complete_callback_->OnEncodedImage(encoded_image_, &codec_specific, | |
226 | &frag_info); | |
227 | } | |
228 | return WEBRTC_VIDEO_CODEC_OK; | |
229 | } | |
230 | ||
231 | +#ifdef LIBVPX_SVC | |
232 | vpx_svc_ref_frame_config VP9EncoderImpl::GenerateRefsAndFlags( | |
233 | const SuperFrameRefSettings& settings) { | |
234 | static const vpx_enc_frame_flags_t kAllFlags = | |
235 | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST | | |
236 | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF; | |
237 | vpx_svc_ref_frame_config sf_conf = {}; | |
238 | if (settings.is_keyframe) { | |
239 | // Used later on to make sure we don't make any invalid references. | |
240 | @@ -857,16 +880,17 @@ vpx_svc_ref_frame_config VP9EncoderImpl: | |
241 | buffer_updated_at_frame_[updated_buffer] = frames_encoded_; | |
242 | sf_conf.frame_flags[layer_idx] = layer_flags; | |
243 | } | |
244 | } | |
245 | } | |
246 | ++frames_encoded_; | |
247 | return sf_conf; | |
248 | } | |
249 | +#endif | |
250 | ||
251 | int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | |
252 | return WEBRTC_VIDEO_CODEC_OK; | |
253 | } | |
254 | ||
255 | int VP9EncoderImpl::RegisterEncodeCompleteCallback( | |
256 | EncodedImageCallback* callback) { | |
257 | encoded_complete_callback_ = callback; | |
258 | diff -Nrup -U 8 a/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h b/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h | |
259 | --- a/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h 2020-02-18 02:37:58.000000000 +0300 | |
260 | +++ b/media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h 2020-03-02 00:32:05.000000000 +0300 | |
261 | @@ -77,24 +77,26 @@ class VP9EncoderImpl : public VP9Encoder | |
262 | ||
263 | void PopulateCodecSpecific(CodecSpecificInfo* codec_specific, | |
264 | const vpx_codec_cx_pkt& pkt, | |
265 | uint32_t timestamp); | |
266 | ||
267 | bool ExplicitlyConfiguredSpatialLayers() const; | |
268 | bool SetSvcRates(); | |
269 | ||
270 | +#ifdef LIBVPX_SVC | |
271 | // Used for flexible mode to set the flags and buffer references used | |
272 | // by the encoder. Also calculates the references used by the RTP | |
273 | // packetizer. | |
274 | // | |
275 | // Has to be called for every frame (keyframes included) to update the | |
276 | // state used to calculate references. | |
277 | vpx_svc_ref_frame_config GenerateRefsAndFlags( | |
278 | const SuperFrameRefSettings& settings); | |
279 | +#endif | |
280 | ||
281 | virtual int GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt); | |
282 | ||
283 | // Callback function for outputting packets per spatial layer. | |
284 | static void EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt, | |
285 | void* user_data); | |
286 | ||
287 | // Determine maximum target for Intra frames | |
288 | @@ -111,17 +113,19 @@ class VP9EncoderImpl : public VP9Encoder | |
289 | bool inited_; | |
290 | int64_t timestamp_; | |
291 | uint16_t picture_id_; | |
292 | int cpu_speed_; | |
293 | uint32_t rc_max_intra_target_; | |
294 | vpx_codec_ctx_t* encoder_; | |
295 | vpx_codec_enc_cfg_t* config_; | |
296 | vpx_image_t* raw_; | |
297 | +#ifdef LIBVPX_SVC | |
298 | vpx_svc_extra_cfg_t svc_params_; | |
299 | +#endif | |
300 | const VideoFrame* input_image_; | |
301 | GofInfoVP9 gof_; // Contains each frame's temporal information for | |
302 | // non-flexible mode. | |
303 | uint8_t tl0_pic_idx_; // Only used in non-flexible mode. | |
304 | size_t frames_since_kf_; | |
305 | uint8_t num_temporal_layers_; | |
306 | uint8_t num_spatial_layers_; | |
307 | uint8_t num_cores_; |