| 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 */ |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // Copy the entire layer's data (including start codes). | 145 // Copy the entire layer's data (including start codes). |
| 146 memcpy(encoded_image->_buffer + encoded_image->_length, | 146 memcpy(encoded_image->_buffer + encoded_image->_length, |
| 147 layerInfo.pBsBuf, | 147 layerInfo.pBsBuf, |
| 148 layer_len); | 148 layer_len); |
| 149 encoded_image->_length += layer_len; | 149 encoded_image->_length += layer_len; |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 H264EncoderImpl::H264EncoderImpl() | 153 H264EncoderImpl::H264EncoderImpl() |
| 154 : openh264_encoder_(nullptr), | 154 : openh264_encoder_(nullptr), |
| 155 width_(0), | |
| 156 height_(0), | |
| 157 max_frame_rate_(0.0f), | |
| 158 target_bps_(0), | |
| 159 max_bps_(0), | |
| 160 mode_(kRealtimeVideo), | |
| 161 frame_dropping_on_(false), | |
| 162 key_frame_interval_(0), | |
| 163 max_payload_size_(0), | |
| 164 number_of_cores_(0), | 155 number_of_cores_(0), |
| 165 encoded_image_callback_(nullptr), | 156 encoded_image_callback_(nullptr), |
| 166 has_reported_init_(false), | 157 has_reported_init_(false), |
| 167 has_reported_error_(false) {} | 158 has_reported_error_(false) {} |
| 168 | 159 |
| 169 H264EncoderImpl::~H264EncoderImpl() { | 160 H264EncoderImpl::~H264EncoderImpl() { |
| 170 Release(); | 161 Release(); |
| 171 } | 162 } |
| 172 | 163 |
| 173 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, | 164 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, |
| 174 int32_t number_of_cores, | 165 int32_t number_of_cores, |
| 175 size_t max_payload_size) { | 166 size_t /*max_payload_size*/) { |
| 176 ReportInit(); | 167 ReportInit(); |
| 177 if (!codec_settings || | 168 if (!codec_settings || |
| 178 codec_settings->codecType != kVideoCodecH264) { | 169 codec_settings->codecType != kVideoCodecH264) { |
| 179 ReportError(); | 170 ReportError(); |
| 180 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 171 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 181 } | 172 } |
| 182 if (codec_settings->maxFramerate == 0) { | 173 if (codec_settings->maxFramerate == 0) { |
| 183 ReportError(); | 174 ReportError(); |
| 184 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 175 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 185 } | 176 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 212 // else WELS_LOG_DEFAULT is used by default. | 203 // else WELS_LOG_DEFAULT is used by default. |
| 213 | 204 |
| 214 number_of_cores_ = number_of_cores; | 205 number_of_cores_ = number_of_cores; |
| 215 // Set internal settings from codec_settings | 206 // Set internal settings from codec_settings |
| 216 width_ = codec_settings->width; | 207 width_ = codec_settings->width; |
| 217 height_ = codec_settings->height; | 208 height_ = codec_settings->height; |
| 218 max_frame_rate_ = static_cast<float>(codec_settings->maxFramerate); | 209 max_frame_rate_ = static_cast<float>(codec_settings->maxFramerate); |
| 219 mode_ = codec_settings->mode; | 210 mode_ = codec_settings->mode; |
| 220 frame_dropping_on_ = codec_settings->H264().frameDroppingOn; | 211 frame_dropping_on_ = codec_settings->H264().frameDroppingOn; |
| 221 key_frame_interval_ = codec_settings->H264().keyFrameInterval; | 212 key_frame_interval_ = codec_settings->H264().keyFrameInterval; |
| 222 max_payload_size_ = max_payload_size; | |
| 223 | 213 |
| 224 // Codec_settings uses kbits/second; encoder uses bits/second. | 214 // Codec_settings uses kbits/second; encoder uses bits/second. |
| 225 max_bps_ = codec_settings->maxBitrate * 1000; | 215 max_bps_ = codec_settings->maxBitrate * 1000; |
| 226 if (codec_settings->targetBitrate == 0) | 216 if (codec_settings->targetBitrate == 0) |
| 227 target_bps_ = codec_settings->startBitrate * 1000; | 217 target_bps_ = codec_settings->startBitrate * 1000; |
| 228 else | 218 else |
| 229 target_bps_ = codec_settings->targetBitrate * 1000; | 219 target_bps_ = codec_settings->targetBitrate * 1000; |
| 230 RTC_DCHECK(codec_settings->H264().packetization_mode != | |
| 231 kH264PacketizationModeNotSet); | |
| 232 packetization_mode_ = codec_settings->H264().packetization_mode; | |
| 233 | 220 |
| 234 SEncParamExt encoder_params = CreateEncoderParams(); | 221 SEncParamExt encoder_params = CreateEncoderParams(); |
| 235 | |
| 236 // Initialize. | 222 // Initialize. |
| 237 if (openh264_encoder_->InitializeExt(&encoder_params) != 0) { | 223 if (openh264_encoder_->InitializeExt(&encoder_params) != 0) { |
| 238 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; | 224 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; |
| 239 Release(); | 225 Release(); |
| 240 ReportError(); | 226 ReportError(); |
| 241 return WEBRTC_VIDEO_CODEC_ERROR; | 227 return WEBRTC_VIDEO_CODEC_ERROR; |
| 242 } | 228 } |
| 243 // TODO(pbos): Base init params on these values before submitting. | 229 // TODO(pbos): Base init params on these values before submitting. |
| 244 quality_scaler_.Init(codec_settings->codecType, codec_settings->startBitrate, | 230 quality_scaler_.Init(codec_settings->codecType, codec_settings->startBitrate, |
| 245 codec_settings->width, codec_settings->height, | 231 codec_settings->width, codec_settings->height, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 RTPFragmentationHeader frag_header; | 370 RTPFragmentationHeader frag_header; |
| 385 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, *frame_buffer, &info, | 371 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, *frame_buffer, &info, |
| 386 &frag_header); | 372 &frag_header); |
| 387 | 373 |
| 388 // Encoder can skip frames to save bandwidth in which case | 374 // Encoder can skip frames to save bandwidth in which case |
| 389 // |encoded_image_._length| == 0. | 375 // |encoded_image_._length| == 0. |
| 390 if (encoded_image_._length > 0) { | 376 if (encoded_image_._length > 0) { |
| 391 // Deliver encoded image. | 377 // Deliver encoded image. |
| 392 CodecSpecificInfo codec_specific; | 378 CodecSpecificInfo codec_specific; |
| 393 codec_specific.codecType = kVideoCodecH264; | 379 codec_specific.codecType = kVideoCodecH264; |
| 394 codec_specific.codecSpecific.H264.packetization_mode = packetization_mode_; | |
| 395 encoded_image_callback_->OnEncodedImage(encoded_image_, &codec_specific, | 380 encoded_image_callback_->OnEncodedImage(encoded_image_, &codec_specific, |
| 396 &frag_header); | 381 &frag_header); |
| 397 | 382 |
| 398 // Parse and report QP. | 383 // Parse and report QP. |
| 399 h264_bitstream_parser_.ParseBitstream(encoded_image_._buffer, | 384 h264_bitstream_parser_.ParseBitstream(encoded_image_._buffer, |
| 400 encoded_image_._length); | 385 encoded_image_._length); |
| 401 int qp = -1; | 386 int qp = -1; |
| 402 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) | 387 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) |
| 403 quality_scaler_.ReportQP(qp); | 388 quality_scaler_.ReportQP(qp); |
| 404 } else { | 389 } else { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 encoder_params.iMultipleThreadIdc = NumberOfThreads( | 438 encoder_params.iMultipleThreadIdc = NumberOfThreads( |
| 454 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); | 439 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); |
| 455 // The base spatial layer 0 is the only one we use. | 440 // The base spatial layer 0 is the only one we use. |
| 456 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; | 441 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; |
| 457 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; | 442 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; |
| 458 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; | 443 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; |
| 459 encoder_params.sSpatialLayers[0].iSpatialBitrate = | 444 encoder_params.sSpatialLayers[0].iSpatialBitrate = |
| 460 encoder_params.iTargetBitrate; | 445 encoder_params.iTargetBitrate; |
| 461 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = | 446 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = |
| 462 encoder_params.iMaxBitrate; | 447 encoder_params.iMaxBitrate; |
| 463 LOG(INFO) << "OpenH264 version is " << OPENH264_MAJOR << "." | |
| 464 << OPENH264_MINOR; | |
| 465 switch (packetization_mode_) { | |
| 466 case kH264PacketizationMode0: | |
| 467 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | 448 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) |
| 468 // Limit the size of packets produced. | 449 // Slice num according to number of threads. |
| 469 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_DYN_SLICE; | 450 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; |
| 470 // The slice size is max payload size - room for a NAL header. | |
| 471 // The constant 50 is NAL_HEADER_ADD_0X30BYTES in openh264 source, | |
| 472 // but is not exported. | |
| 473 encoder_params.sSpatialLayers[0] | |
| 474 .sSliceCfg.sSliceArgument.uiSliceSizeConstraint = | |
| 475 static_cast<unsigned int>(max_payload_size_ - 50); | |
| 476 encoder_params.uiMaxNalSize = | |
| 477 static_cast<unsigned int>(max_payload_size_); | |
| 478 #else | 451 #else |
| 479 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto | 452 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto design |
| 480 // design | 453 // it with cpu core number. |
| 481 // it with cpu core number. | 454 // TODO(sprang): Set to 0 when we understand why the rate controller borks |
| 482 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; | 455 // when uiSliceNum > 1. |
| 483 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | 456 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; |
| 484 SM_SIZELIMITED_SLICE; | 457 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = |
| 485 encoder_params.sSpatialLayers[0] | 458 SM_FIXEDSLCNUM_SLICE; |
| 486 .sSliceArgument.uiSliceSizeConstraint = | |
| 487 static_cast<unsigned int>(max_payload_size_); | |
| 488 #endif | 459 #endif |
| 489 break; | 460 |
| 490 case kH264PacketizationMode1: | |
| 491 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | |
| 492 // Slice num according to number of threads. | |
| 493 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; | |
| 494 #else | |
| 495 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto | |
| 496 // design it with cpu core number. | |
| 497 // TODO(sprang): Set to 0 when we understand why the rate controller borks | |
| 498 // when uiSliceNum > 1. | |
| 499 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; | |
| 500 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | |
| 501 SM_FIXEDSLCNUM_SLICE; | |
| 502 #endif | |
| 503 break; | |
| 504 default: | |
| 505 RTC_NOTREACHED() << "Illegal packetization mode specified"; | |
| 506 } | |
| 507 return encoder_params; | 461 return encoder_params; |
| 508 } | 462 } |
| 509 | 463 |
| 510 void H264EncoderImpl::ReportInit() { | 464 void H264EncoderImpl::ReportInit() { |
| 511 if (has_reported_init_) | 465 if (has_reported_init_) |
| 512 return; | 466 return; |
| 513 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", | 467 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", |
| 514 kH264EncoderEventInit, | 468 kH264EncoderEventInit, |
| 515 kH264EncoderEventMax); | 469 kH264EncoderEventMax); |
| 516 has_reported_init_ = true; | 470 has_reported_init_ = true; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 532 | 486 |
| 533 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { | 487 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { |
| 534 return WEBRTC_VIDEO_CODEC_OK; | 488 return WEBRTC_VIDEO_CODEC_OK; |
| 535 } | 489 } |
| 536 | 490 |
| 537 void H264EncoderImpl::OnDroppedFrame() { | 491 void H264EncoderImpl::OnDroppedFrame() { |
| 538 quality_scaler_.ReportDroppedFrame(); | 492 quality_scaler_.ReportDroppedFrame(); |
| 539 } | 493 } |
| 540 | 494 |
| 541 } // namespace webrtc | 495 } // namespace webrtc |
| OLD | NEW |