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