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; |
| 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 int video_format = EVideoFormatType::videoFormatI420; | 252 int video_format = EVideoFormatType::videoFormatI420; |
239 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, | 253 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 RTPFragmentationHeader frag_header; | 377 RTPFragmentationHeader frag_header; |
364 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, *frame_buffer, &info, | 378 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, *frame_buffer, &info, |
365 &frag_header); | 379 &frag_header); |
366 | 380 |
367 // Encoder can skip frames to save bandwidth in which case | 381 // Encoder can skip frames to save bandwidth in which case |
368 // |encoded_image_._length| == 0. | 382 // |encoded_image_._length| == 0. |
369 if (encoded_image_._length > 0) { | 383 if (encoded_image_._length > 0) { |
370 // Deliver encoded image. | 384 // Deliver encoded image. |
371 CodecSpecificInfo codec_specific; | 385 CodecSpecificInfo codec_specific; |
372 codec_specific.codecType = kVideoCodecH264; | 386 codec_specific.codecType = kVideoCodecH264; |
| 387 codec_specific.codecSpecific.H264.packetization_mode = packetization_mode_; |
373 encoded_image_callback_->OnEncodedImage(encoded_image_, &codec_specific, | 388 encoded_image_callback_->OnEncodedImage(encoded_image_, &codec_specific, |
374 &frag_header); | 389 &frag_header); |
375 | 390 |
376 // Parse and report QP. | 391 // Parse and report QP. |
377 h264_bitstream_parser_.ParseBitstream(encoded_image_._buffer, | 392 h264_bitstream_parser_.ParseBitstream(encoded_image_._buffer, |
378 encoded_image_._length); | 393 encoded_image_._length); |
379 h264_bitstream_parser_.GetLastSliceQp(&encoded_image_.qp_); | 394 h264_bitstream_parser_.GetLastSliceQp(&encoded_image_.qp_); |
380 } | 395 } |
381 return WEBRTC_VIDEO_CODEC_OK; | 396 return WEBRTC_VIDEO_CODEC_OK; |
382 } | 397 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 encoder_params.iMultipleThreadIdc = NumberOfThreads( | 442 encoder_params.iMultipleThreadIdc = NumberOfThreads( |
428 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); | 443 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); |
429 // The base spatial layer 0 is the only one we use. | 444 // The base spatial layer 0 is the only one we use. |
430 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; | 445 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; |
431 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; | 446 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; |
432 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; | 447 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; |
433 encoder_params.sSpatialLayers[0].iSpatialBitrate = | 448 encoder_params.sSpatialLayers[0].iSpatialBitrate = |
434 encoder_params.iTargetBitrate; | 449 encoder_params.iTargetBitrate; |
435 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = | 450 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = |
436 encoder_params.iMaxBitrate; | 451 encoder_params.iMaxBitrate; |
| 452 LOG(INFO) << "OpenH264 version is " << OPENH264_MAJOR << "." |
| 453 << OPENH264_MINOR; |
| 454 switch (packetization_mode_) { |
| 455 case H264PacketizationMode::SingleNalUnit: |
| 456 // Limit the size of the packets produced. |
437 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | 457 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) |
438 // Slice num according to number of threads. | 458 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_DYN_SLICE; |
439 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; | 459 // The slice size is max payload size - room for a NAL header. |
| 460 // The constant 50 is NAL_HEADER_ADD_0X30BYTES in openh264 source, |
| 461 // but is not exported. |
| 462 const kNalHeaderSizeAllocation = 50; |
| 463 encoder_params.sSpatialLayers[0] |
| 464 .sSliceCfg.sSliceArgument.uiSliceSizeConstraint = |
| 465 static_cast<unsigned int>(max_payload_size_ - |
| 466 kNalHeaderSizeAllocation); |
| 467 encoder_params.uiMaxNalSize = |
| 468 static_cast<unsigned int>(max_payload_size_); |
440 #else | 469 #else |
441 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto design | 470 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; |
442 // it with cpu core number. | 471 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = |
443 // TODO(sprang): Set to 0 when we understand why the rate controller borks | 472 SM_SIZELIMITED_SLICE; |
444 // when uiSliceNum > 1. | 473 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint = |
445 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; | 474 static_cast<unsigned int>(max_payload_size_); |
446 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | |
447 SM_FIXEDSLCNUM_SLICE; | |
448 #endif | 475 #endif |
449 | 476 break; |
| 477 case H264PacketizationMode::NonInterleaved: |
| 478 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) |
| 479 // Slice num according to number of threads. |
| 480 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; |
| 481 #else |
| 482 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto |
| 483 // design it with cpu core number. |
| 484 // TODO(sprang): Set to 0 when we understand why the rate controller borks |
| 485 // when uiSliceNum > 1. |
| 486 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1; |
| 487 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = |
| 488 SM_FIXEDSLCNUM_SLICE; |
| 489 #endif |
| 490 break; |
| 491 } |
450 return encoder_params; | 492 return encoder_params; |
451 } | 493 } |
452 | 494 |
453 void H264EncoderImpl::ReportInit() { | 495 void H264EncoderImpl::ReportInit() { |
454 if (has_reported_init_) | 496 if (has_reported_init_) |
455 return; | 497 return; |
456 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", | 498 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", |
457 kH264EncoderEventInit, | 499 kH264EncoderEventInit, |
458 kH264EncoderEventMax); | 500 kH264EncoderEventMax); |
459 has_reported_init_ = true; | 501 has_reported_init_ = true; |
(...skipping 15 matching lines...) Expand all Loading... |
475 | 517 |
476 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { | 518 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { |
477 return WEBRTC_VIDEO_CODEC_OK; | 519 return WEBRTC_VIDEO_CODEC_OK; |
478 } | 520 } |
479 | 521 |
480 VideoEncoder::ScalingSettings H264EncoderImpl::GetScalingSettings() const { | 522 VideoEncoder::ScalingSettings H264EncoderImpl::GetScalingSettings() const { |
481 return VideoEncoder::ScalingSettings(true); | 523 return VideoEncoder::ScalingSettings(true); |
482 } | 524 } |
483 | 525 |
484 } // namespace webrtc | 526 } // namespace webrtc |
OLD | NEW |