Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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/h264/h264_encoder_impl.h" | 12 #include "webrtc/modules/video_coding/codecs/h264/h264_encoder_impl.h" |
| 13 | 13 |
| 14 #include <limits> | 14 #include <limits> |
| 15 #include <string> | |
| 15 | 16 |
| 16 #include "third_party/openh264/src/codec/api/svc/codec_api.h" | 17 #include "third_party/openh264/src/codec/api/svc/codec_api.h" |
| 17 #include "third_party/openh264/src/codec/api/svc/codec_app_def.h" | 18 #include "third_party/openh264/src/codec/api/svc/codec_app_def.h" |
| 18 #include "third_party/openh264/src/codec/api/svc/codec_def.h" | 19 #include "third_party/openh264/src/codec/api/svc/codec_def.h" |
| 19 #include "third_party/openh264/src/codec/api/svc/codec_ver.h" | 20 #include "third_party/openh264/src/codec/api/svc/codec_ver.h" |
| 20 | 21 |
| 21 #include "webrtc/base/checks.h" | 22 #include "webrtc/base/checks.h" |
| 22 #include "webrtc/base/logging.h" | 23 #include "webrtc/base/logging.h" |
| 23 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 24 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 25 #include "webrtc/media/base/mediaconstants.h" | |
| 24 #include "webrtc/system_wrappers/include/metrics.h" | 26 #include "webrtc/system_wrappers/include/metrics.h" |
| 25 | 27 |
| 26 namespace webrtc { | 28 namespace webrtc { |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 const bool kOpenH264EncoderDetailedLogging = false; | 32 const bool kOpenH264EncoderDetailedLogging = false; |
| 31 | 33 |
| 32 // Used by histograms. Values of entries should not be changed. | 34 // Used by histograms. Values of entries should not be changed. |
| 33 enum H264EncoderImplEvent { | 35 enum H264EncoderImplEvent { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 layer_len += layerInfo.pNalLengthInByte[nal]; | 145 layer_len += layerInfo.pNalLengthInByte[nal]; |
| 144 } | 146 } |
| 145 // Copy the entire layer's data (including start codes). | 147 // Copy the entire layer's data (including start codes). |
| 146 memcpy(encoded_image->_buffer + encoded_image->_length, | 148 memcpy(encoded_image->_buffer + encoded_image->_length, |
| 147 layerInfo.pBsBuf, | 149 layerInfo.pBsBuf, |
| 148 layer_len); | 150 layer_len); |
| 149 encoded_image->_length += layer_len; | 151 encoded_image->_length += layer_len; |
| 150 } | 152 } |
| 151 } | 153 } |
| 152 | 154 |
| 153 H264EncoderImpl::H264EncoderImpl() | 155 H264EncoderImpl::H264EncoderImpl(const cricket::VideoCodec& codec) |
| 154 : openh264_encoder_(nullptr), | 156 : openh264_encoder_(nullptr), |
| 155 width_(0), | 157 width_(0), |
| 156 height_(0), | 158 height_(0), |
| 157 max_frame_rate_(0.0f), | 159 max_frame_rate_(0.0f), |
| 158 target_bps_(0), | 160 target_bps_(0), |
| 159 max_bps_(0), | 161 max_bps_(0), |
| 160 mode_(kRealtimeVideo), | 162 mode_(kRealtimeVideo), |
| 161 frame_dropping_on_(false), | 163 frame_dropping_on_(false), |
| 162 key_frame_interval_(0), | 164 key_frame_interval_(0), |
| 165 packetization_mode_(H264PacketizationMode::SingleNalUnit), | |
| 166 max_payload_size_(0), | |
| 163 number_of_cores_(0), | 167 number_of_cores_(0), |
| 164 encoded_image_callback_(nullptr), | 168 encoded_image_callback_(nullptr), |
| 165 has_reported_init_(false), | 169 has_reported_init_(false), |
| 166 has_reported_error_(false) {} | 170 has_reported_error_(false) { |
| 171 RTC_CHECK(cricket::CodecNamesEq(codec.name, cricket::kH264CodecName)); | |
| 172 std::string packetization_mode_string; | |
|
magjed_webrtc
2016/12/02 12:34:26
nit: It feels unnecessary to do std::string copies
hta-webrtc
2016/12/02 12:48:47
Only by bypassing codec.GetParam. I don't want thi
| |
| 173 if (codec.GetParam(cricket::kH264FmtpPacketizationMode, | |
| 174 &packetization_mode_string) && | |
| 175 packetization_mode_string == "1") { | |
| 176 packetization_mode_ = H264PacketizationMode::NonInterleaved; | |
| 177 } | |
| 178 } | |
| 167 | 179 |
| 168 H264EncoderImpl::~H264EncoderImpl() { | 180 H264EncoderImpl::~H264EncoderImpl() { |
| 169 Release(); | 181 Release(); |
| 170 } | 182 } |
| 171 | 183 |
| 172 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, | 184 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, |
| 173 int32_t number_of_cores, | 185 int32_t number_of_cores, |
| 174 size_t /*max_payload_size*/) { | 186 size_t max_payload_size) { |
| 175 ReportInit(); | 187 ReportInit(); |
| 176 if (!codec_settings || | 188 if (!codec_settings || |
| 177 codec_settings->codecType != kVideoCodecH264) { | 189 codec_settings->codecType != kVideoCodecH264) { |
| 178 ReportError(); | 190 ReportError(); |
| 179 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 191 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 180 } | 192 } |
| 181 if (codec_settings->maxFramerate == 0) { | 193 if (codec_settings->maxFramerate == 0) { |
| 182 ReportError(); | 194 ReportError(); |
| 183 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 195 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 184 } | 196 } |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 211 // else WELS_LOG_DEFAULT is used by default. | 223 // else WELS_LOG_DEFAULT is used by default. |
| 212 | 224 |
| 213 number_of_cores_ = number_of_cores; | 225 number_of_cores_ = number_of_cores; |
| 214 // Set internal settings from codec_settings | 226 // Set internal settings from codec_settings |
| 215 width_ = codec_settings->width; | 227 width_ = codec_settings->width; |
| 216 height_ = codec_settings->height; | 228 height_ = codec_settings->height; |
| 217 max_frame_rate_ = static_cast<float>(codec_settings->maxFramerate); | 229 max_frame_rate_ = static_cast<float>(codec_settings->maxFramerate); |
| 218 mode_ = codec_settings->mode; | 230 mode_ = codec_settings->mode; |
| 219 frame_dropping_on_ = codec_settings->H264().frameDroppingOn; | 231 frame_dropping_on_ = codec_settings->H264().frameDroppingOn; |
| 220 key_frame_interval_ = codec_settings->H264().keyFrameInterval; | 232 key_frame_interval_ = codec_settings->H264().keyFrameInterval; |
| 233 max_payload_size_ = max_payload_size; | |
| 221 | 234 |
| 222 // Codec_settings uses kbits/second; encoder uses bits/second. | 235 // Codec_settings uses kbits/second; encoder uses bits/second. |
| 223 max_bps_ = codec_settings->maxBitrate * 1000; | 236 max_bps_ = codec_settings->maxBitrate * 1000; |
| 224 if (codec_settings->targetBitrate == 0) | 237 if (codec_settings->targetBitrate == 0) |
| 225 target_bps_ = codec_settings->startBitrate * 1000; | 238 target_bps_ = codec_settings->startBitrate * 1000; |
| 226 else | 239 else |
| 227 target_bps_ = codec_settings->targetBitrate * 1000; | 240 target_bps_ = codec_settings->targetBitrate * 1000; |
| 228 | 241 |
| 229 SEncParamExt encoder_params = CreateEncoderParams(); | 242 SEncParamExt encoder_params = CreateEncoderParams(); |
| 243 | |
| 230 // Initialize. | 244 // Initialize. |
| 231 if (openh264_encoder_->InitializeExt(&encoder_params) != 0) { | 245 if (openh264_encoder_->InitializeExt(&encoder_params) != 0) { |
| 232 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; | 246 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; |
| 233 Release(); | 247 Release(); |
| 234 ReportError(); | 248 ReportError(); |
| 235 return WEBRTC_VIDEO_CODEC_ERROR; | 249 return WEBRTC_VIDEO_CODEC_ERROR; |
| 236 } | 250 } |
| 237 // TODO(pbos): Base init params on these values before submitting. | 251 // TODO(pbos): Base init params on these values before submitting. |
| 238 quality_scaler_.Init(codec_settings->codecType, codec_settings->startBitrate, | 252 quality_scaler_.Init(codec_settings->codecType, codec_settings->startBitrate, |
| 239 codec_settings->width, codec_settings->height, | 253 codec_settings->width, codec_settings->height, |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 encoded_image_._length); | 403 encoded_image_._length); |
| 390 int qp = -1; | 404 int qp = -1; |
| 391 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { | 405 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { |
| 392 quality_scaler_.ReportQP(qp); | 406 quality_scaler_.ReportQP(qp); |
| 393 encoded_image_.qp_ = qp; | 407 encoded_image_.qp_ = qp; |
| 394 } | 408 } |
| 395 | 409 |
| 396 // Deliver encoded image. | 410 // Deliver encoded image. |
| 397 CodecSpecificInfo codec_specific; | 411 CodecSpecificInfo codec_specific; |
| 398 codec_specific.codecType = kVideoCodecH264; | 412 codec_specific.codecType = kVideoCodecH264; |
| 413 codec_specific.codecSpecific.H264.packetization_mode = packetization_mode_; | |
| 399 encoded_image_callback_->OnEncodedImage(encoded_image_, &codec_specific, | 414 encoded_image_callback_->OnEncodedImage(encoded_image_, &codec_specific, |
| 400 &frag_header); | 415 &frag_header); |
| 401 } else { | 416 } else { |
| 402 quality_scaler_.ReportDroppedFrame(); | 417 quality_scaler_.ReportDroppedFrame(); |
| 403 } | 418 } |
| 404 return WEBRTC_VIDEO_CODEC_OK; | 419 return WEBRTC_VIDEO_CODEC_OK; |
| 405 } | 420 } |
| 406 | 421 |
| 407 const char* H264EncoderImpl::ImplementationName() const { | 422 const char* H264EncoderImpl::ImplementationName() const { |
| 408 return "OpenH264"; | 423 return "OpenH264"; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 450 encoder_params.iMultipleThreadIdc = NumberOfThreads( | 465 encoder_params.iMultipleThreadIdc = NumberOfThreads( |
| 451 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); | 466 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); |
| 452 // The base spatial layer 0 is the only one we use. | 467 // The base spatial layer 0 is the only one we use. |
| 453 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; | 468 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; |
| 454 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; | 469 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; |
| 455 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; | 470 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; |
| 456 encoder_params.sSpatialLayers[0].iSpatialBitrate = | 471 encoder_params.sSpatialLayers[0].iSpatialBitrate = |
| 457 encoder_params.iTargetBitrate; | 472 encoder_params.iTargetBitrate; |
| 458 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = | 473 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = |
| 459 encoder_params.iMaxBitrate; | 474 encoder_params.iMaxBitrate; |
| 475 LOG(INFO) << "OpenH264 version is " << OPENH264_MAJOR << "." | |
| 476 << OPENH264_MINOR; | |
| 477 switch (packetization_mode_) { | |
| 478 case H264PacketizationMode::SingleNalUnit: | |
| 479 // Limit the size of the packets produced. | |
|
hbos
2016/12/02 11:58:42
nit: indent this:
case H264PacketizationMode::
hta-webrtc
2016/12/02 12:48:47
git cl format did this. I haven't figured out wher
| |
| 460 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | 480 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) |
| 461 // Slice num according to number of threads. | 481 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_DYN_SLICE; |
| 462 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; | 482 // The slice size is max payload size - room for a NAL header. |
| 483 // The constant 50 is NAL_HEADER_ADD_0X30BYTES in openh264 source, | |
| 484 // but is not exported. | |
| 485 const kNalHeaderSizeAllocation = 50; | |
| 486 encoder_params.sSpatialLayers[0] | |
| 487 .sSliceCfg.sSliceArgument.uiSliceSizeConstraint = | |
| 488 static_cast<unsigned int>(max_payload_size_ - | |
| 489 kNalHeaderSizeAllocation); | |
| 490 encoder_params.uiMaxNalSize = | |
| 491 static_cast<unsigned int>(max_payload_size_); | |
| 463 #else | 492 #else |
| 464 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto design | 493 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; |
| 465 // it with cpu core number. | 494 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = |
| 466 // TODO(sprang): Set to 0 when we understand why the rate controller borks | 495 SM_SIZELIMITED_SLICE; |
| 467 // when uiSliceNum > 1. | 496 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint = |
| 468 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; | 497 static_cast<unsigned int>(max_payload_size_); |
| 469 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | |
| 470 SM_FIXEDSLCNUM_SLICE; | |
| 471 #endif | 498 #endif |
| 472 | 499 break; |
| 500 case H264PacketizationMode::NonInterleaved: | |
| 501 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | |
| 502 // Slice num according to number of threads. | |
| 503 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; | |
| 504 #else | |
| 505 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto | |
| 506 // design it with cpu core number. | |
| 507 // TODO(sprang): Set to 0 when we understand why the rate controller borks | |
| 508 // when uiSliceNum > 1. | |
| 509 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; | |
| 510 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | |
| 511 SM_FIXEDSLCNUM_SLICE; | |
| 512 #endif | |
| 513 break; | |
| 514 } | |
| 473 return encoder_params; | 515 return encoder_params; |
| 474 } | 516 } |
| 475 | 517 |
| 476 void H264EncoderImpl::ReportInit() { | 518 void H264EncoderImpl::ReportInit() { |
| 477 if (has_reported_init_) | 519 if (has_reported_init_) |
| 478 return; | 520 return; |
| 479 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", | 521 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", |
| 480 kH264EncoderEventInit, | 522 kH264EncoderEventInit, |
| 481 kH264EncoderEventMax); | 523 kH264EncoderEventMax); |
| 482 has_reported_init_ = true; | 524 has_reported_init_ = true; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 498 | 540 |
| 499 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { | 541 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { |
| 500 return WEBRTC_VIDEO_CODEC_OK; | 542 return WEBRTC_VIDEO_CODEC_OK; |
| 501 } | 543 } |
| 502 | 544 |
| 503 void H264EncoderImpl::OnDroppedFrame() { | 545 void H264EncoderImpl::OnDroppedFrame() { |
| 504 quality_scaler_.ReportDroppedFrame(); | 546 quality_scaler_.ReportDroppedFrame(); |
| 505 } | 547 } |
| 506 | 548 |
| 507 } // namespace webrtc | 549 } // namespace webrtc |
| OLD | NEW |