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/include/module_common_types.h" | 29 #include "webrtc/modules/include/module_common_types.h" |
30 #include "webrtc/modules/video_coding/codecs/vp9/screenshare_layers.h" | |
30 #include "webrtc/system_wrappers/include/logging.h" | 31 #include "webrtc/system_wrappers/include/logging.h" |
31 #include "webrtc/system_wrappers/include/tick_util.h" | 32 #include "webrtc/system_wrappers/include/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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 } | 205 } |
202 if (new_framerate < 1) { | 206 if (new_framerate < 1) { |
203 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 207 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
204 } | 208 } |
205 // Update bit rate | 209 // Update bit rate |
206 if (codec_.maxBitrate > 0 && new_bitrate_kbit > codec_.maxBitrate) { | 210 if (codec_.maxBitrate > 0 && new_bitrate_kbit > codec_.maxBitrate) { |
207 new_bitrate_kbit = codec_.maxBitrate; | 211 new_bitrate_kbit = codec_.maxBitrate; |
208 } | 212 } |
209 config_->rc_target_bitrate = new_bitrate_kbit; | 213 config_->rc_target_bitrate = new_bitrate_kbit; |
210 codec_.maxFramerate = new_framerate; | 214 codec_.maxFramerate = new_framerate; |
215 spatial_layer_->ConfigureBitrate(new_bitrate_kbit, 0); | |
211 | 216 |
212 if (!SetSvcRates()) { | 217 if (!SetSvcRates()) { |
213 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 218 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
214 } | 219 } |
215 | 220 |
216 // Update encoder context | 221 // Update encoder context |
217 if (vpx_codec_enc_config_set(encoder_, config_)) { | 222 if (vpx_codec_enc_config_set(encoder_, config_)) { |
218 return WEBRTC_VIDEO_CODEC_ERROR; | 223 return WEBRTC_VIDEO_CODEC_ERROR; |
219 } | 224 } |
220 return WEBRTC_VIDEO_CODEC_OK; | 225 return WEBRTC_VIDEO_CODEC_OK; |
(...skipping 18 matching lines...) Expand all Loading... | |
239 if (number_of_cores < 1) { | 244 if (number_of_cores < 1) { |
240 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 245 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
241 } | 246 } |
242 if (inst->codecSpecific.VP9.numberOfTemporalLayers > 3) { | 247 if (inst->codecSpecific.VP9.numberOfTemporalLayers > 3) { |
243 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 248 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
244 } | 249 } |
245 // libvpx currently supports only one or two spatial layers. | 250 // libvpx currently supports only one or two spatial layers. |
246 if (inst->codecSpecific.VP9.numberOfSpatialLayers > 2) { | 251 if (inst->codecSpecific.VP9.numberOfSpatialLayers > 2) { |
247 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 252 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
248 } | 253 } |
254 | |
249 int retVal = Release(); | 255 int retVal = Release(); |
250 if (retVal < 0) { | 256 if (retVal < 0) { |
251 return retVal; | 257 return retVal; |
252 } | 258 } |
253 if (encoder_ == NULL) { | 259 if (encoder_ == NULL) { |
254 encoder_ = new vpx_codec_ctx_t; | 260 encoder_ = new vpx_codec_ctx_t; |
255 } | 261 } |
256 if (config_ == NULL) { | 262 if (config_ == NULL) { |
257 config_ = new vpx_codec_enc_cfg_t; | 263 config_ = new vpx_codec_enc_cfg_t; |
258 } | 264 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 1 : 0; | 323 1 : 0; |
318 // Determine number of threads based on the image size and #cores. | 324 // Determine number of threads based on the image size and #cores. |
319 config_->g_threads = NumberOfThreads(config_->g_w, | 325 config_->g_threads = NumberOfThreads(config_->g_w, |
320 config_->g_h, | 326 config_->g_h, |
321 number_of_cores); | 327 number_of_cores); |
322 | 328 |
323 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h); | 329 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h); |
324 | 330 |
325 // TODO(asapersson): Check configuration of temporal switch up and increase | 331 // TODO(asapersson): Check configuration of temporal switch up and increase |
326 // pattern length. | 332 // pattern length. |
327 if (num_temporal_layers_ == 1) { | 333 is_flexible_mode_ = inst->codecSpecific.VP9.flexibleMode; |
334 if (is_flexible_mode_) { | |
335 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS; | |
336 config_->ts_number_layers = num_temporal_layers_; | |
337 if (codec_.mode == kScreensharing) | |
338 spatial_layer_->ConfigureBitrate(inst->startBitrate, 0); | |
339 } else if (num_temporal_layers_ == 1) { | |
328 gof_.SetGofInfoVP9(kTemporalStructureMode1); | 340 gof_.SetGofInfoVP9(kTemporalStructureMode1); |
329 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING; | 341 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING; |
330 config_->ts_number_layers = 1; | 342 config_->ts_number_layers = 1; |
331 config_->ts_rate_decimator[0] = 1; | 343 config_->ts_rate_decimator[0] = 1; |
332 config_->ts_periodicity = 1; | 344 config_->ts_periodicity = 1; |
333 config_->ts_layer_id[0] = 0; | 345 config_->ts_layer_id[0] = 0; |
334 } else if (num_temporal_layers_ == 2) { | 346 } else if (num_temporal_layers_ == 2) { |
335 gof_.SetGofInfoVP9(kTemporalStructureMode2); | 347 gof_.SetGofInfoVP9(kTemporalStructureMode2); |
336 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101; | 348 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101; |
337 config_->ts_number_layers = 2; | 349 config_->ts_number_layers = 2; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 svc_internal_.svc_params.scaling_factor_den[i] = layer.scaling_factor_den; | 400 svc_internal_.svc_params.scaling_factor_den[i] = layer.scaling_factor_den; |
389 } | 401 } |
390 } else { | 402 } else { |
391 int scaling_factor_num = 256; | 403 int scaling_factor_num = 256; |
392 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { | 404 for (int i = num_spatial_layers_ - 1; i >= 0; --i) { |
393 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; | 405 svc_internal_.svc_params.max_quantizers[i] = config_->rc_max_quantizer; |
394 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; | 406 svc_internal_.svc_params.min_quantizers[i] = config_->rc_min_quantizer; |
395 // 1:2 scaling in each dimension. | 407 // 1:2 scaling in each dimension. |
396 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; | 408 svc_internal_.svc_params.scaling_factor_num[i] = scaling_factor_num; |
397 svc_internal_.svc_params.scaling_factor_den[i] = 256; | 409 svc_internal_.svc_params.scaling_factor_den[i] = 256; |
398 scaling_factor_num /= 2; | 410 if (codec_.mode != kScreensharing) |
411 scaling_factor_num /= 2; | |
399 } | 412 } |
400 } | 413 } |
401 | 414 |
402 if (!SetSvcRates()) { | 415 if (!SetSvcRates()) { |
403 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 416 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
404 } | 417 } |
405 | 418 |
406 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { | 419 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { |
407 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 420 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
408 } | 421 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
488 | 501 |
489 // Image in vpx_image_t format. | 502 // Image in vpx_image_t format. |
490 // Input image is const. VPX's raw image is not defined as const. | 503 // Input image is const. VPX's raw image is not defined as const. |
491 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(input_image.buffer(kYPlane)); | 504 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(input_image.buffer(kYPlane)); |
492 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(input_image.buffer(kUPlane)); | 505 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(input_image.buffer(kUPlane)); |
493 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(input_image.buffer(kVPlane)); | 506 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(input_image.buffer(kVPlane)); |
494 raw_->stride[VPX_PLANE_Y] = input_image.stride(kYPlane); | 507 raw_->stride[VPX_PLANE_Y] = input_image.stride(kYPlane); |
495 raw_->stride[VPX_PLANE_U] = input_image.stride(kUPlane); | 508 raw_->stride[VPX_PLANE_U] = input_image.stride(kUPlane); |
496 raw_->stride[VPX_PLANE_V] = input_image.stride(kVPlane); | 509 raw_->stride[VPX_PLANE_V] = input_image.stride(kVPlane); |
497 | 510 |
498 int flags = 0; | 511 vpx_enc_frame_flags_t flags = 0; |
499 bool send_keyframe = (frame_type == kVideoFrameKey); | 512 bool send_keyframe = (frame_type == kVideoFrameKey); |
500 if (send_keyframe) { | 513 if (send_keyframe) { |
501 // Key frame request from caller. | 514 // Key frame request from caller. |
502 flags = VPX_EFLAG_FORCE_KF; | 515 flags = VPX_EFLAG_FORCE_KF; |
503 } | 516 } |
517 | |
518 if (is_flexible_mode_) { | |
519 SuperFrameRefSettings settings; | |
520 | |
521 // These structs are copied when calling vpx_codec_control, | |
522 // therefore it is ok for them to go out of scope. | |
523 vpx_svc_ref_frame_config enc_layer_conf; | |
524 vpx_svc_layer_id layer_id; | |
525 | |
526 if (codec_.mode == kRealtimeVideo) { | |
527 // Real time video not yet implemented in flexible mode. | |
528 RTC_NOTREACHED(); | |
529 } else { | |
530 settings = spatial_layer_->GetSuperFrameSettings(input_image.timestamp(), | |
531 send_keyframe); | |
532 } | |
533 enc_layer_conf = GenerateRefsAndFlags(settings); | |
534 layer_id.temporal_layer_id = 0; | |
535 layer_id.spatial_layer_id = settings.start_layer; | |
536 vpx_codec_control(encoder_, VP9E_SET_SVC_LAYER_ID, &layer_id); | |
537 vpx_codec_control(encoder_, VP9E_SET_SVC_REF_FRAME_CONFIG, &enc_layer_conf); | |
538 } | |
539 | |
504 assert(codec_.maxFramerate > 0); | 540 assert(codec_.maxFramerate > 0); |
505 uint32_t duration = 90000 / codec_.maxFramerate; | 541 uint32_t duration = 90000 / codec_.maxFramerate; |
506 if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags, | 542 if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags, |
507 VPX_DL_REALTIME)) { | 543 VPX_DL_REALTIME)) { |
508 return WEBRTC_VIDEO_CODEC_ERROR; | 544 return WEBRTC_VIDEO_CODEC_ERROR; |
509 } | 545 } |
510 timestamp_ += duration; | 546 timestamp_ += duration; |
511 | 547 |
512 return WEBRTC_VIDEO_CODEC_OK; | 548 return WEBRTC_VIDEO_CODEC_OK; |
513 } | 549 } |
514 | 550 |
515 void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific, | 551 void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific, |
516 const vpx_codec_cx_pkt& pkt, | 552 const vpx_codec_cx_pkt& pkt, |
517 uint32_t timestamp) { | 553 uint32_t timestamp) { |
518 assert(codec_specific != NULL); | 554 assert(codec_specific != NULL); |
519 codec_specific->codecType = kVideoCodecVP9; | 555 codec_specific->codecType = kVideoCodecVP9; |
520 CodecSpecificInfoVP9 *vp9_info = &(codec_specific->codecSpecific.VP9); | 556 CodecSpecificInfoVP9 *vp9_info = &(codec_specific->codecSpecific.VP9); |
521 // TODO(asapersson): Set correct values. | 557 // TODO(asapersson): Set correct values. |
522 vp9_info->inter_pic_predicted = | 558 vp9_info->inter_pic_predicted = |
523 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? false : true; | 559 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? false : true; |
524 vp9_info->flexible_mode = codec_.codecSpecific.VP9.flexibleMode; | 560 vp9_info->flexible_mode = codec_.codecSpecific.VP9.flexibleMode; |
525 vp9_info->ss_data_available = ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && | 561 vp9_info->ss_data_available = ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && |
526 !codec_.codecSpecific.VP9.flexibleMode) | 562 !codec_.codecSpecific.VP9.flexibleMode) |
527 ? true | 563 ? true |
528 : false; | 564 : false; |
529 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) { | 565 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) |
530 gof_idx_ = 0; | 566 frames_since_kf_ = 0; |
531 } | |
532 | 567 |
533 vpx_svc_layer_id_t layer_id = {0}; | 568 vpx_svc_layer_id_t layer_id = {0}; |
534 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); | 569 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); |
535 | 570 |
536 assert(num_temporal_layers_ > 0); | 571 assert(num_temporal_layers_ > 0); |
537 assert(num_spatial_layers_ > 0); | 572 assert(num_spatial_layers_ > 0); |
538 if (num_temporal_layers_ == 1) { | 573 if (num_temporal_layers_ == 1) { |
539 assert(layer_id.temporal_layer_id == 0); | 574 assert(layer_id.temporal_layer_id == 0); |
540 vp9_info->temporal_idx = kNoTemporalIdx; | 575 vp9_info->temporal_idx = kNoTemporalIdx; |
541 } else { | 576 } else { |
542 vp9_info->temporal_idx = layer_id.temporal_layer_id; | 577 vp9_info->temporal_idx = layer_id.temporal_layer_id; |
543 } | 578 } |
544 if (num_spatial_layers_ == 1) { | 579 if (num_spatial_layers_ == 1) { |
545 assert(layer_id.spatial_layer_id == 0); | 580 assert(layer_id.spatial_layer_id == 0); |
546 vp9_info->spatial_idx = kNoSpatialIdx; | 581 vp9_info->spatial_idx = kNoSpatialIdx; |
547 } else { | 582 } else { |
548 vp9_info->spatial_idx = layer_id.spatial_layer_id; | 583 vp9_info->spatial_idx = layer_id.spatial_layer_id; |
549 } | 584 } |
550 if (layer_id.spatial_layer_id != 0) { | 585 if (layer_id.spatial_layer_id != 0) { |
551 vp9_info->ss_data_available = false; | 586 vp9_info->ss_data_available = false; |
552 } | 587 } |
553 | 588 |
554 if (vp9_info->flexible_mode) { | |
555 vp9_info->gof_idx = kNoGofIdx; | |
556 } else { | |
557 vp9_info->gof_idx = | |
558 static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof); | |
559 } | |
560 | |
561 // TODO(asapersson): this info has to be obtained from the encoder. | 589 // TODO(asapersson): this info has to be obtained from the encoder. |
562 vp9_info->temporal_up_switch = true; | 590 vp9_info->temporal_up_switch = true; |
563 | 591 |
åsapersson
2015/11/06 17:08:03
only use if screenshare?
philipel1
2015/11/06 17:20:48
Done.
| |
564 if (layer_id.spatial_layer_id == 0) { | 592 if (layer_id.spatial_layer_id == spatial_layer_->GetStartLayer()) { |
565 picture_id_ = (picture_id_ + 1) & 0x7FFF; | 593 picture_id_ = (picture_id_ + 1) & 0x7FFF; |
566 // TODO(asapersson): this info has to be obtained from the encoder. | 594 // TODO(asapersson): this info has to be obtained from the encoder. |
567 vp9_info->inter_layer_predicted = false; | 595 vp9_info->inter_layer_predicted = false; |
568 } else { | 596 } else { |
569 // TODO(asapersson): this info has to be obtained from the encoder. | 597 // TODO(asapersson): this info has to be obtained from the encoder. |
570 vp9_info->inter_layer_predicted = true; | 598 vp9_info->inter_layer_predicted = true; |
571 } | 599 } |
572 | 600 |
573 vp9_info->picture_id = picture_id_; | 601 vp9_info->picture_id = picture_id_; |
574 | 602 |
575 if (!vp9_info->flexible_mode) { | 603 if (!vp9_info->flexible_mode) { |
576 if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) { | 604 if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) { |
577 tl0_pic_idx_++; | 605 tl0_pic_idx_++; |
578 } | 606 } |
579 vp9_info->tl0_pic_idx = tl0_pic_idx_; | 607 vp9_info->tl0_pic_idx = tl0_pic_idx_; |
580 } | 608 } |
581 | 609 |
582 // Always populate this, so that the packetizer can properly set the marker | 610 // Always populate this, so that the packetizer can properly set the marker |
583 // bit. | 611 // bit. |
584 vp9_info->num_spatial_layers = num_spatial_layers_; | 612 vp9_info->num_spatial_layers = num_spatial_layers_; |
613 | |
614 vp9_info->num_ref_pics = 0; | |
615 if (vp9_info->flexible_mode) { | |
616 vp9_info->gof_idx = kNoGofIdx; | |
617 vp9_info->num_ref_pics = num_ref_pics_[layer_id.spatial_layer_id]; | |
618 for (int i = 0; i < num_ref_pics_[layer_id.spatial_layer_id]; ++i) { | |
619 vp9_info->p_diff[i] = p_diff_[layer_id.spatial_layer_id][i]; | |
620 } | |
621 } else { | |
622 vp9_info->gof_idx = | |
623 static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof); | |
624 } | |
625 ++frames_since_kf_; | |
626 | |
585 if (vp9_info->ss_data_available) { | 627 if (vp9_info->ss_data_available) { |
586 vp9_info->spatial_layer_resolution_present = true; | 628 vp9_info->spatial_layer_resolution_present = true; |
587 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { | 629 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { |
588 vp9_info->width[i] = codec_.width * | 630 vp9_info->width[i] = codec_.width * |
589 svc_internal_.svc_params.scaling_factor_num[i] / | 631 svc_internal_.svc_params.scaling_factor_num[i] / |
590 svc_internal_.svc_params.scaling_factor_den[i]; | 632 svc_internal_.svc_params.scaling_factor_den[i]; |
591 vp9_info->height[i] = codec_.height * | 633 vp9_info->height[i] = codec_.height * |
592 svc_internal_.svc_params.scaling_factor_num[i] / | 634 svc_internal_.svc_params.scaling_factor_num[i] / |
593 svc_internal_.svc_params.scaling_factor_den[i]; | 635 svc_internal_.svc_params.scaling_factor_den[i]; |
594 } | 636 } |
(...skipping 15 matching lines...) Expand all Loading... | |
610 | 652 |
611 assert(pkt->kind == VPX_CODEC_CX_FRAME_PKT); | 653 assert(pkt->kind == VPX_CODEC_CX_FRAME_PKT); |
612 memcpy(&encoded_image_._buffer[encoded_image_._length], pkt->data.frame.buf, | 654 memcpy(&encoded_image_._buffer[encoded_image_._length], pkt->data.frame.buf, |
613 pkt->data.frame.sz); | 655 pkt->data.frame.sz); |
614 frag_info.fragmentationOffset[part_idx] = encoded_image_._length; | 656 frag_info.fragmentationOffset[part_idx] = encoded_image_._length; |
615 frag_info.fragmentationLength[part_idx] = | 657 frag_info.fragmentationLength[part_idx] = |
616 static_cast<uint32_t>(pkt->data.frame.sz); | 658 static_cast<uint32_t>(pkt->data.frame.sz); |
617 frag_info.fragmentationPlType[part_idx] = 0; | 659 frag_info.fragmentationPlType[part_idx] = 0; |
618 frag_info.fragmentationTimeDiff[part_idx] = 0; | 660 frag_info.fragmentationTimeDiff[part_idx] = 0; |
619 encoded_image_._length += static_cast<uint32_t>(pkt->data.frame.sz); | 661 encoded_image_._length += static_cast<uint32_t>(pkt->data.frame.sz); |
662 | |
663 vpx_svc_layer_id_t layer_id = {0}; | |
664 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); | |
665 if (is_flexible_mode_ && codec_.mode == kScreensharing) | |
666 spatial_layer_->LayerFrameEncoded( | |
667 static_cast<unsigned int>(encoded_image_._length), | |
668 layer_id.spatial_layer_id); | |
669 | |
620 assert(encoded_image_._length <= encoded_image_._size); | 670 assert(encoded_image_._length <= encoded_image_._size); |
621 | 671 |
622 // End of frame. | 672 // End of frame. |
623 // Check if encoded frame is a key frame. | 673 // Check if encoded frame is a key frame. |
624 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) { | 674 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) { |
625 encoded_image_._frameType = kVideoFrameKey; | 675 encoded_image_._frameType = kVideoFrameKey; |
626 } | 676 } |
627 PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp()); | 677 PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp()); |
628 | 678 |
629 if (encoded_image_._length > 0) { | 679 if (encoded_image_._length > 0) { |
630 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length); | 680 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length); |
631 encoded_image_._timeStamp = input_image_->timestamp(); | 681 encoded_image_._timeStamp = input_image_->timestamp(); |
632 encoded_image_.capture_time_ms_ = input_image_->render_time_ms(); | 682 encoded_image_.capture_time_ms_ = input_image_->render_time_ms(); |
633 encoded_image_._encodedHeight = raw_->d_h; | 683 encoded_image_._encodedHeight = raw_->d_h; |
634 encoded_image_._encodedWidth = raw_->d_w; | 684 encoded_image_._encodedWidth = raw_->d_w; |
635 encoded_complete_callback_->Encoded(encoded_image_, &codec_specific, | 685 encoded_complete_callback_->Encoded(encoded_image_, &codec_specific, |
636 &frag_info); | 686 &frag_info); |
637 } | 687 } |
638 return WEBRTC_VIDEO_CODEC_OK; | 688 return WEBRTC_VIDEO_CODEC_OK; |
639 } | 689 } |
640 | 690 |
691 vpx_svc_ref_frame_config VP9EncoderImpl::GenerateRefsAndFlags( | |
692 const SuperFrameRefSettings& settings) { | |
693 static const vpx_enc_frame_flags_t kAllFlags = | |
694 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST | | |
695 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF; | |
696 vpx_svc_ref_frame_config sf_conf = {}; | |
697 if (settings.is_keyframe) { | |
698 // Used later on to make sure we don't make any invalid references. | |
699 memset(buffer_updated_at_frame_, -1, sizeof(buffer_updated_at_frame_)); | |
700 for (int layer = settings.start_layer; layer <= settings.stop_layer; | |
701 ++layer) { | |
702 num_ref_pics_[layer] = 0; | |
703 buffer_updated_at_frame_[settings.layer[layer].upd_buf] = frames_encoded_; | |
704 // When encoding a keyframe only the alt_fb_idx is used | |
705 // to specify which layer ends up in which buffer. | |
706 sf_conf.alt_fb_idx[layer] = settings.layer[layer].upd_buf; | |
707 } | |
708 } else { | |
709 for (int layer_idx = settings.start_layer; layer_idx <= settings.stop_layer; | |
710 ++layer_idx) { | |
711 vpx_enc_frame_flags_t layer_flags = kAllFlags; | |
712 num_ref_pics_[layer_idx] = 0; | |
713 int8_t refs[3] = {settings.layer[layer_idx].ref_buf1, | |
714 settings.layer[layer_idx].ref_buf2, | |
715 settings.layer[layer_idx].ref_buf3}; | |
716 | |
717 for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) { | |
718 if (refs[ref_idx] == -1) | |
719 continue; | |
720 | |
721 RTC_DCHECK_GE(refs[ref_idx], 0); | |
722 RTC_DCHECK_LE(refs[ref_idx], 7); | |
723 // Easier to remove flags from all flags rather than having to | |
724 // build the flags from 0. | |
725 switch (num_ref_pics_[layer_idx]) { | |
726 case 0: { | |
727 sf_conf.lst_fb_idx[layer_idx] = refs[ref_idx]; | |
728 layer_flags &= ~VP8_EFLAG_NO_REF_LAST; | |
729 break; | |
730 } | |
731 case 1: { | |
732 sf_conf.gld_fb_idx[layer_idx] = refs[ref_idx]; | |
733 layer_flags &= ~VP8_EFLAG_NO_REF_GF; | |
734 break; | |
735 } | |
736 case 2: { | |
737 sf_conf.alt_fb_idx[layer_idx] = refs[ref_idx]; | |
738 layer_flags &= ~VP8_EFLAG_NO_REF_ARF; | |
739 break; | |
740 } | |
741 } | |
742 // Make sure we don't reference a buffer that hasn't been | |
743 // used at all or hasn't been used since a keyframe. | |
744 RTC_DCHECK_NE(buffer_updated_at_frame_[refs[ref_idx]], -1); | |
745 | |
746 p_diff_[layer_idx][num_ref_pics_[layer_idx]] = | |
747 frames_encoded_ - buffer_updated_at_frame_[refs[ref_idx]]; | |
748 num_ref_pics_[layer_idx]++; | |
749 } | |
750 | |
751 bool upd_buf_same_as_a_ref = false; | |
752 if (settings.layer[layer_idx].upd_buf != -1) { | |
753 for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) { | |
754 if (settings.layer[layer_idx].upd_buf == refs[ref_idx]) { | |
755 switch (ref_idx) { | |
756 case 0: { | |
757 layer_flags &= ~VP8_EFLAG_NO_UPD_LAST; | |
758 break; | |
759 } | |
760 case 1: { | |
761 layer_flags &= ~VP8_EFLAG_NO_UPD_GF; | |
762 break; | |
763 } | |
764 case 2: { | |
765 layer_flags &= ~VP8_EFLAG_NO_UPD_ARF; | |
766 break; | |
767 } | |
768 } | |
769 upd_buf_same_as_a_ref = true; | |
770 break; | |
771 } | |
772 } | |
773 if (!upd_buf_same_as_a_ref) { | |
774 // If we have three references and a buffer is specified to be | |
775 // updated, | |
776 // then that buffer must be the same as one of the three references. | |
777 RTC_CHECK_LT(num_ref_pics_[layer_idx], kMaxVp9RefPics); | |
778 | |
779 sf_conf.alt_fb_idx[layer_idx] = settings.layer[layer_idx].upd_buf; | |
780 layer_flags ^= VP8_EFLAG_NO_UPD_ARF; | |
781 } | |
782 | |
783 int updated_buffer = settings.layer[layer_idx].upd_buf; | |
784 buffer_updated_at_frame_[updated_buffer] = frames_encoded_; | |
785 sf_conf.frame_flags[layer_idx] = layer_flags; | |
786 } | |
787 } | |
788 } | |
789 frames_encoded_++; | |
790 return sf_conf; | |
791 } | |
792 | |
641 int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 793 int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
642 return WEBRTC_VIDEO_CODEC_OK; | 794 return WEBRTC_VIDEO_CODEC_OK; |
643 } | 795 } |
644 | 796 |
645 int VP9EncoderImpl::RegisterEncodeCompleteCallback( | 797 int VP9EncoderImpl::RegisterEncodeCompleteCallback( |
646 EncodedImageCallback* callback) { | 798 EncodedImageCallback* callback) { |
647 encoded_complete_callback_ = callback; | 799 encoded_complete_callback_ = callback; |
648 return WEBRTC_VIDEO_CODEC_OK; | 800 return WEBRTC_VIDEO_CODEC_OK; |
649 } | 801 } |
650 | 802 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
815 decoder_ = NULL; | 967 decoder_ = NULL; |
816 } | 968 } |
817 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 969 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers |
818 // still referenced externally are deleted once fully released, not returning | 970 // still referenced externally are deleted once fully released, not returning |
819 // to the pool. | 971 // to the pool. |
820 frame_buffer_pool_.ClearPool(); | 972 frame_buffer_pool_.ClearPool(); |
821 inited_ = false; | 973 inited_ = false; |
822 return WEBRTC_VIDEO_CODEC_OK; | 974 return WEBRTC_VIDEO_CODEC_OK; |
823 } | 975 } |
824 } // namespace webrtc | 976 } // namespace webrtc |
OLD | NEW |