OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 * | 9 * |
10 */ | 10 */ |
11 | 11 |
12 #include "webrtc/modules/video_coding/codecs/vp9/vp9_impl.h" | 12 #include "webrtc/modules/video_coding/codecs/vp9/vp9_impl.h" |
13 | 13 |
14 #include <stdlib.h> | 14 #include <stdlib.h> |
15 #include <string.h> | 15 #include <string.h> |
16 #include <time.h> | 16 #include <time.h> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "vpx/vpx_encoder.h" | 19 #include "vpx/vpx_encoder.h" |
20 #include "vpx/vpx_decoder.h" | 20 #include "vpx/vpx_decoder.h" |
21 #include "vpx/vp8cx.h" | 21 #include "vpx/vp8cx.h" |
22 #include "vpx/vp8dx.h" | 22 #include "vpx/vp8dx.h" |
23 | 23 |
24 #include "webrtc/base/bind.h" | 24 #include "webrtc/base/bind.h" |
25 #include "webrtc/base/checks.h" | 25 #include "webrtc/base/checks.h" |
26 #include "webrtc/base/trace_event.h" | 26 #include "webrtc/base/trace_event.h" |
27 #include "webrtc/common.h" | 27 #include "webrtc/common.h" |
28 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 28 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
29 #include "webrtc/modules/interface/module_common_types.h" | 29 #include "webrtc/modules/interface/module_common_types.h" |
| 30 #include "webrtc/modules/video_coding/codecs/vp9/screenshare_layers.h" |
30 #include "webrtc/system_wrappers/interface/logging.h" | 31 #include "webrtc/system_wrappers/interface/logging.h" |
31 #include "webrtc/system_wrappers/interface/tick_util.h" | 32 #include "webrtc/system_wrappers/interface/tick_util.h" |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 // VP9DecoderImpl::ReturnFrame helper function used with WrappedI420Buffer. | 36 // VP9DecoderImpl::ReturnFrame helper function used with WrappedI420Buffer. |
36 static void WrappedI420BufferNoLongerUsedCb( | 37 static void WrappedI420BufferNoLongerUsedCb( |
37 webrtc::Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer) { | 38 webrtc::Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer) { |
38 img_buffer->Release(); | 39 img_buffer->Release(); |
39 } | 40 } |
(...skipping 29 matching lines...) Expand all Loading... |
69 inited_(false), | 70 inited_(false), |
70 timestamp_(0), | 71 timestamp_(0), |
71 picture_id_(0), | 72 picture_id_(0), |
72 cpu_speed_(3), | 73 cpu_speed_(3), |
73 rc_max_intra_target_(0), | 74 rc_max_intra_target_(0), |
74 encoder_(NULL), | 75 encoder_(NULL), |
75 config_(NULL), | 76 config_(NULL), |
76 raw_(NULL), | 77 raw_(NULL), |
77 input_image_(NULL), | 78 input_image_(NULL), |
78 tl0_pic_idx_(0), | 79 tl0_pic_idx_(0), |
79 gof_idx_(0), | 80 frames_since_kf_(0), |
80 num_temporal_layers_(0), | 81 num_temporal_layers_(0), |
81 num_spatial_layers_(0) { | 82 num_spatial_layers_(0), |
| 83 frames_encoded_(0), |
| 84 // Use two spatial when screensharing with flexible mode. |
| 85 spatial_layer_(new ScreenshareLayersVP9(2)) { |
82 memset(&codec_, 0, sizeof(codec_)); | 86 memset(&codec_, 0, sizeof(codec_)); |
83 uint32_t seed = static_cast<uint32_t>(TickTime::MillisecondTimestamp()); | 87 uint32_t seed = static_cast<uint32_t>(TickTime::MillisecondTimestamp()); |
84 srand(seed); | 88 srand(seed); |
85 } | 89 } |
86 | 90 |
87 VP9EncoderImpl::~VP9EncoderImpl() { | 91 VP9EncoderImpl::~VP9EncoderImpl() { |
88 Release(); | 92 Release(); |
89 } | 93 } |
90 | 94 |
91 int VP9EncoderImpl::Release() { | 95 int VP9EncoderImpl::Release() { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 175 } |
172 if (new_framerate < 1) { | 176 if (new_framerate < 1) { |
173 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 177 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
174 } | 178 } |
175 // Update bit rate | 179 // Update bit rate |
176 if (codec_.maxBitrate > 0 && new_bitrate_kbit > codec_.maxBitrate) { | 180 if (codec_.maxBitrate > 0 && new_bitrate_kbit > codec_.maxBitrate) { |
177 new_bitrate_kbit = codec_.maxBitrate; | 181 new_bitrate_kbit = codec_.maxBitrate; |
178 } | 182 } |
179 config_->rc_target_bitrate = new_bitrate_kbit; | 183 config_->rc_target_bitrate = new_bitrate_kbit; |
180 codec_.maxFramerate = new_framerate; | 184 codec_.maxFramerate = new_framerate; |
| 185 spatial_layer_->ConfigureBitrate(new_bitrate_kbit, 0); |
181 | 186 |
182 if (!SetSvcRates()) { | 187 if (!SetSvcRates()) { |
183 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 188 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
184 } | 189 } |
185 | 190 |
186 // Update encoder context | 191 // Update encoder context |
187 if (vpx_codec_enc_config_set(encoder_, config_)) { | 192 if (vpx_codec_enc_config_set(encoder_, config_)) { |
188 return WEBRTC_VIDEO_CODEC_ERROR; | 193 return WEBRTC_VIDEO_CODEC_ERROR; |
189 } | 194 } |
190 return WEBRTC_VIDEO_CODEC_OK; | 195 return WEBRTC_VIDEO_CODEC_OK; |
(...skipping 18 matching lines...) Expand all Loading... |
209 if (number_of_cores < 1) { | 214 if (number_of_cores < 1) { |
210 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 215 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
211 } | 216 } |
212 if (inst->codecSpecific.VP9.numberOfTemporalLayers > 3) { | 217 if (inst->codecSpecific.VP9.numberOfTemporalLayers > 3) { |
213 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 218 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
214 } | 219 } |
215 // libvpx currently supports only one or two spatial layers. | 220 // libvpx currently supports only one or two spatial layers. |
216 if (inst->codecSpecific.VP9.numberOfSpatialLayers > 2) { | 221 if (inst->codecSpecific.VP9.numberOfSpatialLayers > 2) { |
217 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 222 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
218 } | 223 } |
| 224 |
219 int retVal = Release(); | 225 int retVal = Release(); |
220 if (retVal < 0) { | 226 if (retVal < 0) { |
221 return retVal; | 227 return retVal; |
222 } | 228 } |
223 if (encoder_ == NULL) { | 229 if (encoder_ == NULL) { |
224 encoder_ = new vpx_codec_ctx_t; | 230 encoder_ = new vpx_codec_ctx_t; |
225 } | 231 } |
226 if (config_ == NULL) { | 232 if (config_ == NULL) { |
227 config_ = new vpx_codec_enc_cfg_t; | 233 config_ = new vpx_codec_enc_cfg_t; |
228 } | 234 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 1 : 0; | 293 1 : 0; |
288 // Determine number of threads based on the image size and #cores. | 294 // Determine number of threads based on the image size and #cores. |
289 config_->g_threads = NumberOfThreads(config_->g_w, | 295 config_->g_threads = NumberOfThreads(config_->g_w, |
290 config_->g_h, | 296 config_->g_h, |
291 number_of_cores); | 297 number_of_cores); |
292 | 298 |
293 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h); | 299 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h); |
294 | 300 |
295 // TODO(asapersson): Check configuration of temporal switch up and increase | 301 // TODO(asapersson): Check configuration of temporal switch up and increase |
296 // pattern length. | 302 // pattern length. |
297 if (num_temporal_layers_ == 1) { | 303 is_flexible_mode_ = inst->codecSpecific.VP9.flexibleMode; |
| 304 if (is_flexible_mode_) { |
| 305 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS; |
| 306 config_->ts_number_layers = num_temporal_layers_; |
| 307 if (codec_.mode == kScreensharing) |
| 308 spatial_layer_->ConfigureBitrate(inst->startBitrate, 0); |
| 309 } else if (num_temporal_layers_ == 1) { |
298 gof_.SetGofInfoVP9(kTemporalStructureMode1); | 310 gof_.SetGofInfoVP9(kTemporalStructureMode1); |
299 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING; | 311 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING; |
300 config_->ts_number_layers = 1; | 312 config_->ts_number_layers = 1; |
301 config_->ts_rate_decimator[0] = 1; | 313 config_->ts_rate_decimator[0] = 1; |
302 config_->ts_periodicity = 1; | 314 config_->ts_periodicity = 1; |
303 config_->ts_layer_id[0] = 0; | 315 config_->ts_layer_id[0] = 0; |
304 } else if (num_temporal_layers_ == 2) { | 316 } else if (num_temporal_layers_ == 2) { |
305 gof_.SetGofInfoVP9(kTemporalStructureMode2); | 317 gof_.SetGofInfoVP9(kTemporalStructureMode2); |
306 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101; | 318 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101; |
307 config_->ts_number_layers = 2; | 319 config_->ts_number_layers = 2; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { | 361 int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { |
350 config_->ss_number_layers = num_spatial_layers_; | 362 config_->ss_number_layers = num_spatial_layers_; |
351 | 363 |
352 int scaling_factor_num = 256; | 364 int scaling_factor_num = 256; |
353 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | 365 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { |
354 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 366 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; |
355 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 367 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; |
356 // 1:2 scaling in each dimension. | 368 // 1:2 scaling in each dimension. |
357 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | 369 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; |
358 svc_internal_.svc_params.scaling_factor_den[i] = 256; | 370 svc_internal_.svc_params.scaling_factor_den[i] = 256; |
359 scaling_factor_num /= 2; | 371 if (codec_.mode != kScreensharing) |
| 372 scaling_factor_num /= 2; |
360 } | 373 } |
361 | 374 |
362 if (!SetSvcRates()) { | 375 if (!SetSvcRates()) { |
363 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 376 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
364 } | 377 } |
365 | 378 |
366 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 379 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { |
367 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 380 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
368 } | 381 } |
369 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); | 382 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 | 461 |
449 // Image in vpx_image_t format. | 462 // Image in vpx_image_t format. |
450 // Input image is const. VPX's raw image is not defined as const. | 463 // Input image is const. VPX's raw image is not defined as const. |
451 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(input_image.buffer(kYPlane)); | 464 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(input_image.buffer(kYPlane)); |
452 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(input_image.buffer(kUPlane)); | 465 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(input_image.buffer(kUPlane)); |
453 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(input_image.buffer(kVPlane)); | 466 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(input_image.buffer(kVPlane)); |
454 raw_->stride[VPX_PLANE_Y] = input_image.stride(kYPlane); | 467 raw_->stride[VPX_PLANE_Y] = input_image.stride(kYPlane); |
455 raw_->stride[VPX_PLANE_U] = input_image.stride(kUPlane); | 468 raw_->stride[VPX_PLANE_U] = input_image.stride(kUPlane); |
456 raw_->stride[VPX_PLANE_V] = input_image.stride(kVPlane); | 469 raw_->stride[VPX_PLANE_V] = input_image.stride(kVPlane); |
457 | 470 |
458 int flags = 0; | 471 vpx_enc_frame_flags_t flags = 0; |
459 bool send_keyframe = (frame_type == kKeyFrame); | 472 bool send_keyframe = (frame_type == kKeyFrame); |
460 if (send_keyframe) { | 473 if (send_keyframe) { |
461 // Key frame request from caller. | 474 // Key frame request from caller. |
462 flags = VPX_EFLAG_FORCE_KF; | 475 flags = VPX_EFLAG_FORCE_KF; |
463 } | 476 } |
| 477 |
| 478 if (is_flexible_mode_) { |
| 479 SuperFrameRefSettings settings; |
| 480 vpx_svc_ref_frame_config enc_layer_conf; |
| 481 if (codec_.mode == kRealtimeVideo) { |
| 482 // Real time video not yet implemented in flexible mode. |
| 483 RTC_NOTREACHED(); |
| 484 } else { |
| 485 settings = spatial_layer_->GetSuperFrameSettings(input_image.timestamp(), |
| 486 send_keyframe); |
| 487 } |
| 488 enc_layer_conf = GenerateRefsAndFlags(settings); |
| 489 vpx_svc_layer_id layer_id; |
| 490 layer_id.temporal_layer_id = 0; |
| 491 layer_id.spatial_layer_id = settings.start_layer; |
| 492 vpx_codec_control(encoder_, VP9E_SET_SVC_LAYER_ID, &layer_id); |
| 493 vpx_codec_control(encoder_, VP9E_SET_SVC_REF_FRAME_CONFIG, &enc_layer_conf); |
| 494 } |
| 495 |
464 assert(codec_.maxFramerate > 0); | 496 assert(codec_.maxFramerate > 0); |
465 uint32_t duration = 90000 / codec_.maxFramerate; | 497 uint32_t duration = 90000 / codec_.maxFramerate; |
466 if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags, | 498 if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags, |
467 VPX_DL_REALTIME)) { | 499 VPX_DL_REALTIME)) { |
468 return WEBRTC_VIDEO_CODEC_ERROR; | 500 return WEBRTC_VIDEO_CODEC_ERROR; |
469 } | 501 } |
470 timestamp_ += duration; | 502 timestamp_ += duration; |
471 | 503 |
472 return WEBRTC_VIDEO_CODEC_OK; | 504 return WEBRTC_VIDEO_CODEC_OK; |
473 } | 505 } |
474 | 506 |
475 void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific, | 507 void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific, |
476 const vpx_codec_cx_pkt& pkt, | 508 const vpx_codec_cx_pkt& pkt, |
477 uint32_t timestamp) { | 509 uint32_t timestamp) { |
478 assert(codec_specific != NULL); | 510 assert(codec_specific != NULL); |
479 codec_specific->codecType = kVideoCodecVP9; | 511 codec_specific->codecType = kVideoCodecVP9; |
480 CodecSpecificInfoVP9 *vp9_info = &(codec_specific->codecSpecific.VP9); | 512 CodecSpecificInfoVP9 *vp9_info = &(codec_specific->codecSpecific.VP9); |
481 // TODO(asapersson): Set correct values. | 513 // TODO(asapersson): Set correct values. |
482 vp9_info->inter_pic_predicted = | 514 vp9_info->inter_pic_predicted = |
483 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? false : true; | 515 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? false : true; |
484 vp9_info->flexible_mode = codec_.codecSpecific.VP9.flexibleMode; | 516 vp9_info->flexible_mode = codec_.codecSpecific.VP9.flexibleMode; |
485 vp9_info->ss_data_available = ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && | 517 vp9_info->ss_data_available = ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && |
486 !codec_.codecSpecific.VP9.flexibleMode) | 518 !codec_.codecSpecific.VP9.flexibleMode) |
487 ? true | 519 ? true |
488 : false; | 520 : false; |
489 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) { | 521 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) |
490 gof_idx_ = 0; | 522 frames_since_kf_ = 0; |
491 } | |
492 | 523 |
493 vpx_svc_layer_id_t layer_id = {0}; | 524 vpx_svc_layer_id_t layer_id = {0}; |
494 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); | 525 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); |
495 | 526 |
496 assert(num_temporal_layers_ > 0); | 527 assert(num_temporal_layers_ > 0); |
497 assert(num_spatial_layers_ > 0); | 528 assert(num_spatial_layers_ > 0); |
498 if (num_temporal_layers_ == 1) { | 529 if (num_temporal_layers_ == 1) { |
499 assert(layer_id.temporal_layer_id == 0); | 530 assert(layer_id.temporal_layer_id == 0); |
500 vp9_info->temporal_idx = kNoTemporalIdx; | 531 vp9_info->temporal_idx = kNoTemporalIdx; |
501 } else { | 532 } else { |
502 vp9_info->temporal_idx = layer_id.temporal_layer_id; | 533 vp9_info->temporal_idx = layer_id.temporal_layer_id; |
503 } | 534 } |
504 if (num_spatial_layers_ == 1) { | 535 if (num_spatial_layers_ == 1) { |
505 assert(layer_id.spatial_layer_id == 0); | 536 assert(layer_id.spatial_layer_id == 0); |
506 vp9_info->spatial_idx = kNoSpatialIdx; | 537 vp9_info->spatial_idx = kNoSpatialIdx; |
507 } else { | 538 } else { |
508 vp9_info->spatial_idx = layer_id.spatial_layer_id; | 539 vp9_info->spatial_idx = layer_id.spatial_layer_id; |
509 } | 540 } |
510 if (layer_id.spatial_layer_id != 0) { | 541 if (layer_id.spatial_layer_id != 0) { |
511 vp9_info->ss_data_available = false; | 542 vp9_info->ss_data_available = false; |
512 } | 543 } |
513 | 544 |
514 if (vp9_info->flexible_mode) { | |
515 vp9_info->gof_idx = kNoGofIdx; | |
516 } else { | |
517 vp9_info->gof_idx = | |
518 static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof); | |
519 } | |
520 | |
521 // TODO(asapersson): this info has to be obtained from the encoder. | 545 // TODO(asapersson): this info has to be obtained from the encoder. |
522 vp9_info->temporal_up_switch = true; | 546 vp9_info->temporal_up_switch = true; |
523 | 547 |
524 if (layer_id.spatial_layer_id == 0) { | 548 if (layer_id.spatial_layer_id == spatial_layer_->GetStartLayer()) { |
525 picture_id_ = (picture_id_ + 1) & 0x7FFF; | 549 picture_id_ = (picture_id_ + 1) & 0x7FFF; |
526 // TODO(asapersson): this info has to be obtained from the encoder. | 550 // TODO(asapersson): this info has to be obtained from the encoder. |
527 vp9_info->inter_layer_predicted = false; | 551 vp9_info->inter_layer_predicted = false; |
528 } else { | 552 } else { |
529 // TODO(asapersson): this info has to be obtained from the encoder. | 553 // TODO(asapersson): this info has to be obtained from the encoder. |
530 vp9_info->inter_layer_predicted = true; | 554 vp9_info->inter_layer_predicted = true; |
531 } | 555 } |
532 | 556 |
533 vp9_info->picture_id = picture_id_; | 557 vp9_info->picture_id = picture_id_; |
534 | 558 |
535 if (!vp9_info->flexible_mode) { | 559 if (!vp9_info->flexible_mode) { |
536 if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) { | 560 if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) { |
537 tl0_pic_idx_++; | 561 tl0_pic_idx_++; |
538 } | 562 } |
539 vp9_info->tl0_pic_idx = tl0_pic_idx_; | 563 vp9_info->tl0_pic_idx = tl0_pic_idx_; |
540 } | 564 } |
541 | 565 |
542 // Always populate this, so that the packetizer can properly set the marker | 566 // Always populate this, so that the packetizer can properly set the marker |
543 // bit. | 567 // bit. |
544 vp9_info->num_spatial_layers = num_spatial_layers_; | 568 vp9_info->num_spatial_layers = num_spatial_layers_; |
| 569 |
| 570 vp9_info->num_ref_pics = 0; |
| 571 if (vp9_info->flexible_mode) { |
| 572 vp9_info->gof_idx = kNoGofIdx; |
| 573 vp9_info->num_ref_pics = num_ref_pics_[layer_id.spatial_layer_id]; |
| 574 for (int i = 0; i < num_ref_pics_[layer_id.spatial_layer_id]; ++i) { |
| 575 vp9_info->p_diff[i] = p_diff_[layer_id.spatial_layer_id][i]; |
| 576 } |
| 577 } else { |
| 578 vp9_info->gof_idx = |
| 579 static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof); |
| 580 } |
| 581 ++frames_since_kf_; |
| 582 |
545 if (vp9_info->ss_data_available) { | 583 if (vp9_info->ss_data_available) { |
546 vp9_info->spatial_layer_resolution_present = true; | 584 vp9_info->spatial_layer_resolution_present = true; |
547 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { | 585 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { |
548 vp9_info->width[i] = codec_.width * | 586 vp9_info->width[i] = codec_.width * |
549 svc_internal_.svc_params.scaling_factor_num[i] / | 587 svc_internal_.svc_params.scaling_factor_num[i] / |
550 svc_internal_.svc_params.scaling_factor_den[i]; | 588 svc_internal_.svc_params.scaling_factor_den[i]; |
551 vp9_info->height[i] = codec_.height * | 589 vp9_info->height[i] = codec_.height * |
552 svc_internal_.svc_params.scaling_factor_num[i] / | 590 svc_internal_.svc_params.scaling_factor_num[i] / |
553 svc_internal_.svc_params.scaling_factor_den[i]; | 591 svc_internal_.svc_params.scaling_factor_den[i]; |
554 } | 592 } |
(...skipping 15 matching lines...) Expand all Loading... |
570 | 608 |
571 assert(pkt->kind == VPX_CODEC_CX_FRAME_PKT); | 609 assert(pkt->kind == VPX_CODEC_CX_FRAME_PKT); |
572 memcpy(&encoded_image_._buffer[encoded_image_._length], pkt->data.frame.buf, | 610 memcpy(&encoded_image_._buffer[encoded_image_._length], pkt->data.frame.buf, |
573 pkt->data.frame.sz); | 611 pkt->data.frame.sz); |
574 frag_info.fragmentationOffset[part_idx] = encoded_image_._length; | 612 frag_info.fragmentationOffset[part_idx] = encoded_image_._length; |
575 frag_info.fragmentationLength[part_idx] = | 613 frag_info.fragmentationLength[part_idx] = |
576 static_cast<uint32_t>(pkt->data.frame.sz); | 614 static_cast<uint32_t>(pkt->data.frame.sz); |
577 frag_info.fragmentationPlType[part_idx] = 0; | 615 frag_info.fragmentationPlType[part_idx] = 0; |
578 frag_info.fragmentationTimeDiff[part_idx] = 0; | 616 frag_info.fragmentationTimeDiff[part_idx] = 0; |
579 encoded_image_._length += static_cast<uint32_t>(pkt->data.frame.sz); | 617 encoded_image_._length += static_cast<uint32_t>(pkt->data.frame.sz); |
| 618 |
| 619 vpx_svc_layer_id_t layer_id = {0}; |
| 620 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); |
| 621 if (is_flexible_mode_ && codec_.mode == kScreensharing) |
| 622 spatial_layer_->LayerFrameEncoded(encoded_image_._length, |
| 623 layer_id.spatial_layer_id); |
| 624 |
580 assert(encoded_image_._length <= encoded_image_._size); | 625 assert(encoded_image_._length <= encoded_image_._size); |
581 | 626 |
582 // End of frame. | 627 // End of frame. |
583 // Check if encoded frame is a key frame. | 628 // Check if encoded frame is a key frame. |
584 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) { | 629 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) { |
585 encoded_image_._frameType = kKeyFrame; | 630 encoded_image_._frameType = kKeyFrame; |
586 } | 631 } |
587 PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp()); | 632 PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp()); |
588 | 633 |
589 if (encoded_image_._length > 0) { | 634 if (encoded_image_._length > 0) { |
590 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length); | 635 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length); |
591 encoded_image_._timeStamp = input_image_->timestamp(); | 636 encoded_image_._timeStamp = input_image_->timestamp(); |
592 encoded_image_.capture_time_ms_ = input_image_->render_time_ms(); | 637 encoded_image_.capture_time_ms_ = input_image_->render_time_ms(); |
593 encoded_image_._encodedHeight = raw_->d_h; | 638 encoded_image_._encodedHeight = raw_->d_h; |
594 encoded_image_._encodedWidth = raw_->d_w; | 639 encoded_image_._encodedWidth = raw_->d_w; |
595 encoded_complete_callback_->Encoded(encoded_image_, &codec_specific, | 640 encoded_complete_callback_->Encoded(encoded_image_, &codec_specific, |
596 &frag_info); | 641 &frag_info); |
597 } | 642 } |
598 return WEBRTC_VIDEO_CODEC_OK; | 643 return WEBRTC_VIDEO_CODEC_OK; |
599 } | 644 } |
600 | 645 |
| 646 vpx_svc_ref_frame_config VP9EncoderImpl::GenerateRefsAndFlags( |
| 647 const SuperFrameRefSettings& settings) { |
| 648 static const vpx_enc_frame_flags_t kAllFlags = |
| 649 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST | |
| 650 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF; |
| 651 vpx_svc_ref_frame_config sf_conf; |
| 652 if (settings.is_keyframe) { |
| 653 // Used later on to make sure we don't make any invalid references. |
| 654 memset(buf_upd_at_frame_, -1, sizeof(buf_upd_at_frame_)); |
| 655 for (int l = settings.start_layer; l <= settings.stop_layer; ++l) { |
| 656 num_ref_pics_[l] = 0; |
| 657 buf_upd_at_frame_[settings.layer[l].upd_buf] = frames_encoded_; |
| 658 sf_conf.lst_fb_idx[l] = settings.layer[l].upd_buf; |
| 659 } |
| 660 } else { |
| 661 for (int layer_idx = settings.start_layer; layer_idx <= settings.stop_layer; |
| 662 ++layer_idx) { |
| 663 vpx_enc_frame_flags_t layer_flags = kAllFlags; |
| 664 num_ref_pics_[layer_idx] = 0; |
| 665 int8_t refs[3] = {settings.layer[layer_idx].ref_buf1, |
| 666 settings.layer[layer_idx].ref_buf2, |
| 667 settings.layer[layer_idx].ref_buf3}; |
| 668 |
| 669 for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) { |
| 670 if (refs[ref_idx] == -1) |
| 671 continue; |
| 672 |
| 673 RTC_DCHECK_GE(refs[ref_idx], 0); |
| 674 RTC_DCHECK_LE(refs[ref_idx], 7); |
| 675 // Easier to remove flags from all flags rather than having to |
| 676 // build the flags from 0. |
| 677 switch (num_ref_pics_[layer_idx]) { |
| 678 case 0: { |
| 679 sf_conf.lst_fb_idx[layer_idx] = refs[ref_idx]; |
| 680 layer_flags &= ~VP8_EFLAG_NO_REF_LAST; |
| 681 break; |
| 682 } |
| 683 case 1: { |
| 684 sf_conf.gld_fb_idx[layer_idx] = refs[ref_idx]; |
| 685 layer_flags &= ~VP8_EFLAG_NO_REF_GF; |
| 686 break; |
| 687 } |
| 688 case 2: { |
| 689 sf_conf.alt_fb_idx[layer_idx] = refs[ref_idx]; |
| 690 layer_flags &= ~VP8_EFLAG_NO_REF_ARF; |
| 691 break; |
| 692 } |
| 693 } |
| 694 // Make sure we don't reference a buffer that hasn't been |
| 695 // used at all or hasn't been used since a keyframe. |
| 696 RTC_DCHECK_NE(buf_upd_at_frame_[refs[ref_idx]], -1); |
| 697 |
| 698 p_diff_[layer_idx][num_ref_pics_[layer_idx]] = |
| 699 frames_encoded_ - buf_upd_at_frame_[refs[ref_idx]]; |
| 700 num_ref_pics_[layer_idx]++; |
| 701 } |
| 702 |
| 703 bool upd_buf_same_as_a_ref = false; |
| 704 if (settings.layer[layer_idx].upd_buf != -1) { |
| 705 for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) { |
| 706 if (settings.layer[layer_idx].upd_buf == refs[ref_idx]) { |
| 707 switch (ref_idx) { |
| 708 case 0: { |
| 709 layer_flags &= ~VP8_EFLAG_NO_UPD_LAST; |
| 710 break; |
| 711 } |
| 712 case 1: { |
| 713 layer_flags &= ~VP8_EFLAG_NO_UPD_GF; |
| 714 break; |
| 715 } |
| 716 case 2: { |
| 717 layer_flags &= ~VP8_EFLAG_NO_UPD_ARF; |
| 718 break; |
| 719 } |
| 720 } |
| 721 upd_buf_same_as_a_ref = true; |
| 722 break; |
| 723 } |
| 724 } |
| 725 if (!upd_buf_same_as_a_ref) { |
| 726 // If we have three references and a buffer is specified to be |
| 727 // updated, |
| 728 // then that buffer must be the same as one of the three references. |
| 729 RTC_CHECK_LT(num_ref_pics_[layer_idx], kMaxVp9RefPics); |
| 730 |
| 731 sf_conf.alt_fb_idx[layer_idx] = settings.layer[layer_idx].upd_buf; |
| 732 layer_flags ^= VP8_EFLAG_NO_UPD_ARF; |
| 733 } |
| 734 |
| 735 buf_upd_at_frame_[settings.layer[layer_idx].upd_buf] = frames_encoded_; |
| 736 sf_conf.frame_flags[layer_idx] = layer_flags; |
| 737 } |
| 738 } |
| 739 } |
| 740 frames_encoded_++; |
| 741 return sf_conf; |
| 742 } |
| 743 |
601 int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 744 int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
602 return WEBRTC_VIDEO_CODEC_OK; | 745 return WEBRTC_VIDEO_CODEC_OK; |
603 } | 746 } |
604 | 747 |
605 int VP9EncoderImpl::RegisterEncodeCompleteCallback( | 748 int VP9EncoderImpl::RegisterEncodeCompleteCallback( |
606 EncodedImageCallback* callback) { | 749 EncodedImageCallback* callback) { |
607 encoded_complete_callback_ = callback; | 750 encoded_complete_callback_ = callback; |
608 return WEBRTC_VIDEO_CODEC_OK; | 751 return WEBRTC_VIDEO_CODEC_OK; |
609 } | 752 } |
610 | 753 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 decoder_ = NULL; | 918 decoder_ = NULL; |
776 } | 919 } |
777 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 920 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers |
778 // still referenced externally are deleted once fully released, not returning | 921 // still referenced externally are deleted once fully released, not returning |
779 // to the pool. | 922 // to the pool. |
780 frame_buffer_pool_.ClearPool(); | 923 frame_buffer_pool_.ClearPool(); |
781 inited_ = false; | 924 inited_ = false; |
782 return WEBRTC_VIDEO_CODEC_OK; | 925 return WEBRTC_VIDEO_CODEC_OK; |
783 } | 926 } |
784 } // namespace webrtc | 927 } // namespace webrtc |
OLD | NEW |