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