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 |
564 if (layer_id.spatial_layer_id == 0) { | 592 bool is_first_frame = false; |
| 593 if (is_flexible_mode_) { |
| 594 is_first_frame = |
| 595 layer_id.spatial_layer_id == spatial_layer_->GetStartLayer(); |
| 596 } else { |
| 597 is_first_frame = layer_id.spatial_layer_id == 0; |
| 598 } |
| 599 |
| 600 if (is_first_frame) { |
565 picture_id_ = (picture_id_ + 1) & 0x7FFF; | 601 picture_id_ = (picture_id_ + 1) & 0x7FFF; |
566 // TODO(asapersson): this info has to be obtained from the encoder. | 602 // TODO(asapersson): this info has to be obtained from the encoder. |
567 vp9_info->inter_layer_predicted = false; | 603 vp9_info->inter_layer_predicted = false; |
568 } else { | 604 } else { |
569 // TODO(asapersson): this info has to be obtained from the encoder. | 605 // TODO(asapersson): this info has to be obtained from the encoder. |
570 vp9_info->inter_layer_predicted = true; | 606 vp9_info->inter_layer_predicted = true; |
571 } | 607 } |
572 | 608 |
573 vp9_info->picture_id = picture_id_; | 609 vp9_info->picture_id = picture_id_; |
574 | 610 |
575 if (!vp9_info->flexible_mode) { | 611 if (!vp9_info->flexible_mode) { |
576 if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) { | 612 if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) { |
577 tl0_pic_idx_++; | 613 tl0_pic_idx_++; |
578 } | 614 } |
579 vp9_info->tl0_pic_idx = tl0_pic_idx_; | 615 vp9_info->tl0_pic_idx = tl0_pic_idx_; |
580 } | 616 } |
581 | 617 |
582 // Always populate this, so that the packetizer can properly set the marker | 618 // Always populate this, so that the packetizer can properly set the marker |
583 // bit. | 619 // bit. |
584 vp9_info->num_spatial_layers = num_spatial_layers_; | 620 vp9_info->num_spatial_layers = num_spatial_layers_; |
| 621 |
| 622 vp9_info->num_ref_pics = 0; |
| 623 if (vp9_info->flexible_mode) { |
| 624 vp9_info->gof_idx = kNoGofIdx; |
| 625 vp9_info->num_ref_pics = num_ref_pics_[layer_id.spatial_layer_id]; |
| 626 for (int i = 0; i < num_ref_pics_[layer_id.spatial_layer_id]; ++i) { |
| 627 vp9_info->p_diff[i] = p_diff_[layer_id.spatial_layer_id][i]; |
| 628 } |
| 629 } else { |
| 630 vp9_info->gof_idx = |
| 631 static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof); |
| 632 } |
| 633 ++frames_since_kf_; |
| 634 |
585 if (vp9_info->ss_data_available) { | 635 if (vp9_info->ss_data_available) { |
586 vp9_info->spatial_layer_resolution_present = true; | 636 vp9_info->spatial_layer_resolution_present = true; |
587 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { | 637 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) { |
588 vp9_info->width[i] = codec_.width * | 638 vp9_info->width[i] = codec_.width * |
589 svc_internal_.svc_params.scaling_factor_num[i] / | 639 svc_internal_.svc_params.scaling_factor_num[i] / |
590 svc_internal_.svc_params.scaling_factor_den[i]; | 640 svc_internal_.svc_params.scaling_factor_den[i]; |
591 vp9_info->height[i] = codec_.height * | 641 vp9_info->height[i] = codec_.height * |
592 svc_internal_.svc_params.scaling_factor_num[i] / | 642 svc_internal_.svc_params.scaling_factor_num[i] / |
593 svc_internal_.svc_params.scaling_factor_den[i]; | 643 svc_internal_.svc_params.scaling_factor_den[i]; |
594 } | 644 } |
(...skipping 15 matching lines...) Expand all Loading... |
610 | 660 |
611 assert(pkt->kind == VPX_CODEC_CX_FRAME_PKT); | 661 assert(pkt->kind == VPX_CODEC_CX_FRAME_PKT); |
612 memcpy(&encoded_image_._buffer[encoded_image_._length], pkt->data.frame.buf, | 662 memcpy(&encoded_image_._buffer[encoded_image_._length], pkt->data.frame.buf, |
613 pkt->data.frame.sz); | 663 pkt->data.frame.sz); |
614 frag_info.fragmentationOffset[part_idx] = encoded_image_._length; | 664 frag_info.fragmentationOffset[part_idx] = encoded_image_._length; |
615 frag_info.fragmentationLength[part_idx] = | 665 frag_info.fragmentationLength[part_idx] = |
616 static_cast<uint32_t>(pkt->data.frame.sz); | 666 static_cast<uint32_t>(pkt->data.frame.sz); |
617 frag_info.fragmentationPlType[part_idx] = 0; | 667 frag_info.fragmentationPlType[part_idx] = 0; |
618 frag_info.fragmentationTimeDiff[part_idx] = 0; | 668 frag_info.fragmentationTimeDiff[part_idx] = 0; |
619 encoded_image_._length += static_cast<uint32_t>(pkt->data.frame.sz); | 669 encoded_image_._length += static_cast<uint32_t>(pkt->data.frame.sz); |
| 670 |
| 671 vpx_svc_layer_id_t layer_id = {0}; |
| 672 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id); |
| 673 if (is_flexible_mode_ && codec_.mode == kScreensharing) |
| 674 spatial_layer_->LayerFrameEncoded( |
| 675 static_cast<unsigned int>(encoded_image_._length), |
| 676 layer_id.spatial_layer_id); |
| 677 |
620 assert(encoded_image_._length <= encoded_image_._size); | 678 assert(encoded_image_._length <= encoded_image_._size); |
621 | 679 |
622 // End of frame. | 680 // End of frame. |
623 // Check if encoded frame is a key frame. | 681 // Check if encoded frame is a key frame. |
624 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) { | 682 if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) { |
625 encoded_image_._frameType = kVideoFrameKey; | 683 encoded_image_._frameType = kVideoFrameKey; |
626 } | 684 } |
627 PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp()); | 685 PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp()); |
628 | 686 |
629 if (encoded_image_._length > 0) { | 687 if (encoded_image_._length > 0) { |
630 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length); | 688 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length); |
631 encoded_image_._timeStamp = input_image_->timestamp(); | 689 encoded_image_._timeStamp = input_image_->timestamp(); |
632 encoded_image_.capture_time_ms_ = input_image_->render_time_ms(); | 690 encoded_image_.capture_time_ms_ = input_image_->render_time_ms(); |
633 encoded_image_._encodedHeight = raw_->d_h; | 691 encoded_image_._encodedHeight = raw_->d_h; |
634 encoded_image_._encodedWidth = raw_->d_w; | 692 encoded_image_._encodedWidth = raw_->d_w; |
635 encoded_complete_callback_->Encoded(encoded_image_, &codec_specific, | 693 encoded_complete_callback_->Encoded(encoded_image_, &codec_specific, |
636 &frag_info); | 694 &frag_info); |
637 } | 695 } |
638 return WEBRTC_VIDEO_CODEC_OK; | 696 return WEBRTC_VIDEO_CODEC_OK; |
639 } | 697 } |
640 | 698 |
| 699 vpx_svc_ref_frame_config VP9EncoderImpl::GenerateRefsAndFlags( |
| 700 const SuperFrameRefSettings& settings) { |
| 701 static const vpx_enc_frame_flags_t kAllFlags = |
| 702 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST | |
| 703 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF; |
| 704 vpx_svc_ref_frame_config sf_conf = {}; |
| 705 if (settings.is_keyframe) { |
| 706 // Used later on to make sure we don't make any invalid references. |
| 707 memset(buffer_updated_at_frame_, -1, sizeof(buffer_updated_at_frame_)); |
| 708 for (int layer = settings.start_layer; layer <= settings.stop_layer; |
| 709 ++layer) { |
| 710 num_ref_pics_[layer] = 0; |
| 711 buffer_updated_at_frame_[settings.layer[layer].upd_buf] = frames_encoded_; |
| 712 // When encoding a keyframe only the alt_fb_idx is used |
| 713 // to specify which layer ends up in which buffer. |
| 714 sf_conf.alt_fb_idx[layer] = settings.layer[layer].upd_buf; |
| 715 } |
| 716 } else { |
| 717 for (int layer_idx = settings.start_layer; layer_idx <= settings.stop_layer; |
| 718 ++layer_idx) { |
| 719 vpx_enc_frame_flags_t layer_flags = kAllFlags; |
| 720 num_ref_pics_[layer_idx] = 0; |
| 721 int8_t refs[3] = {settings.layer[layer_idx].ref_buf1, |
| 722 settings.layer[layer_idx].ref_buf2, |
| 723 settings.layer[layer_idx].ref_buf3}; |
| 724 |
| 725 for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) { |
| 726 if (refs[ref_idx] == -1) |
| 727 continue; |
| 728 |
| 729 RTC_DCHECK_GE(refs[ref_idx], 0); |
| 730 RTC_DCHECK_LE(refs[ref_idx], 7); |
| 731 // Easier to remove flags from all flags rather than having to |
| 732 // build the flags from 0. |
| 733 switch (num_ref_pics_[layer_idx]) { |
| 734 case 0: { |
| 735 sf_conf.lst_fb_idx[layer_idx] = refs[ref_idx]; |
| 736 layer_flags &= ~VP8_EFLAG_NO_REF_LAST; |
| 737 break; |
| 738 } |
| 739 case 1: { |
| 740 sf_conf.gld_fb_idx[layer_idx] = refs[ref_idx]; |
| 741 layer_flags &= ~VP8_EFLAG_NO_REF_GF; |
| 742 break; |
| 743 } |
| 744 case 2: { |
| 745 sf_conf.alt_fb_idx[layer_idx] = refs[ref_idx]; |
| 746 layer_flags &= ~VP8_EFLAG_NO_REF_ARF; |
| 747 break; |
| 748 } |
| 749 } |
| 750 // Make sure we don't reference a buffer that hasn't been |
| 751 // used at all or hasn't been used since a keyframe. |
| 752 RTC_DCHECK_NE(buffer_updated_at_frame_[refs[ref_idx]], -1); |
| 753 |
| 754 p_diff_[layer_idx][num_ref_pics_[layer_idx]] = |
| 755 frames_encoded_ - buffer_updated_at_frame_[refs[ref_idx]]; |
| 756 num_ref_pics_[layer_idx]++; |
| 757 } |
| 758 |
| 759 bool upd_buf_same_as_a_ref = false; |
| 760 if (settings.layer[layer_idx].upd_buf != -1) { |
| 761 for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) { |
| 762 if (settings.layer[layer_idx].upd_buf == refs[ref_idx]) { |
| 763 switch (ref_idx) { |
| 764 case 0: { |
| 765 layer_flags &= ~VP8_EFLAG_NO_UPD_LAST; |
| 766 break; |
| 767 } |
| 768 case 1: { |
| 769 layer_flags &= ~VP8_EFLAG_NO_UPD_GF; |
| 770 break; |
| 771 } |
| 772 case 2: { |
| 773 layer_flags &= ~VP8_EFLAG_NO_UPD_ARF; |
| 774 break; |
| 775 } |
| 776 } |
| 777 upd_buf_same_as_a_ref = true; |
| 778 break; |
| 779 } |
| 780 } |
| 781 if (!upd_buf_same_as_a_ref) { |
| 782 // If we have three references and a buffer is specified to be |
| 783 // updated, |
| 784 // then that buffer must be the same as one of the three references. |
| 785 RTC_CHECK_LT(num_ref_pics_[layer_idx], kMaxVp9RefPics); |
| 786 |
| 787 sf_conf.alt_fb_idx[layer_idx] = settings.layer[layer_idx].upd_buf; |
| 788 layer_flags ^= VP8_EFLAG_NO_UPD_ARF; |
| 789 } |
| 790 |
| 791 int updated_buffer = settings.layer[layer_idx].upd_buf; |
| 792 buffer_updated_at_frame_[updated_buffer] = frames_encoded_; |
| 793 sf_conf.frame_flags[layer_idx] = layer_flags; |
| 794 } |
| 795 } |
| 796 } |
| 797 frames_encoded_++; |
| 798 return sf_conf; |
| 799 } |
| 800 |
641 int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { | 801 int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) { |
642 return WEBRTC_VIDEO_CODEC_OK; | 802 return WEBRTC_VIDEO_CODEC_OK; |
643 } | 803 } |
644 | 804 |
645 int VP9EncoderImpl::RegisterEncodeCompleteCallback( | 805 int VP9EncoderImpl::RegisterEncodeCompleteCallback( |
646 EncodedImageCallback* callback) { | 806 EncodedImageCallback* callback) { |
647 encoded_complete_callback_ = callback; | 807 encoded_complete_callback_ = callback; |
648 return WEBRTC_VIDEO_CODEC_OK; | 808 return WEBRTC_VIDEO_CODEC_OK; |
649 } | 809 } |
650 | 810 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
815 decoder_ = NULL; | 975 decoder_ = NULL; |
816 } | 976 } |
817 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers | 977 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers |
818 // still referenced externally are deleted once fully released, not returning | 978 // still referenced externally are deleted once fully released, not returning |
819 // to the pool. | 979 // to the pool. |
820 frame_buffer_pool_.ClearPool(); | 980 frame_buffer_pool_.ClearPool(); |
821 inited_ = false; | 981 inited_ = false; |
822 return WEBRTC_VIDEO_CODEC_OK; | 982 return WEBRTC_VIDEO_CODEC_OK; |
823 } | 983 } |
824 } // namespace webrtc | 984 } // namespace webrtc |
OLD | NEW |