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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 // Copy the entire layer's data (including start codes). | 143 // Copy the entire layer's data (including start codes). |
144 memcpy(encoded_image->_buffer + encoded_image->_length, | 144 memcpy(encoded_image->_buffer + encoded_image->_length, |
145 layerInfo.pBsBuf, | 145 layerInfo.pBsBuf, |
146 layer_len); | 146 layer_len); |
147 encoded_image->_length += layer_len; | 147 encoded_image->_length += layer_len; |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 H264EncoderImpl::H264EncoderImpl() | 151 H264EncoderImpl::H264EncoderImpl() |
152 : openh264_encoder_(nullptr), | 152 : openh264_encoder_(nullptr), |
153 max_payload_size_(0), | |
153 number_of_cores_(0), | 154 number_of_cores_(0), |
154 encoded_image_callback_(nullptr), | 155 encoded_image_callback_(nullptr), |
155 has_reported_init_(false), | 156 has_reported_init_(false), |
156 has_reported_error_(false) {} | 157 has_reported_error_(false) {} |
157 | 158 |
158 H264EncoderImpl::~H264EncoderImpl() { | 159 H264EncoderImpl::~H264EncoderImpl() { |
159 Release(); | 160 Release(); |
160 } | 161 } |
161 | 162 |
162 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, | 163 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, |
163 int32_t number_of_cores, | 164 int32_t number_of_cores, |
164 size_t /*max_payload_size*/) { | 165 size_t max_payload_size) { |
165 ReportInit(); | 166 ReportInit(); |
166 if (!codec_settings || | 167 if (!codec_settings || |
167 codec_settings->codecType != kVideoCodecH264) { | 168 codec_settings->codecType != kVideoCodecH264) { |
168 ReportError(); | 169 ReportError(); |
169 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 170 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
170 } | 171 } |
171 if (codec_settings->maxFramerate == 0) { | 172 if (codec_settings->maxFramerate == 0) { |
172 ReportError(); | 173 ReportError(); |
173 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 174 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
174 } | 175 } |
(...skipping 19 matching lines...) Expand all Loading... | |
194 } | 195 } |
195 RTC_DCHECK(openh264_encoder_); | 196 RTC_DCHECK(openh264_encoder_); |
196 if (kOpenH264EncoderDetailedLogging) { | 197 if (kOpenH264EncoderDetailedLogging) { |
197 int trace_level = WELS_LOG_DETAIL; | 198 int trace_level = WELS_LOG_DETAIL; |
198 openh264_encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, | 199 openh264_encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, |
199 &trace_level); | 200 &trace_level); |
200 } | 201 } |
201 // else WELS_LOG_DEFAULT is used by default. | 202 // else WELS_LOG_DEFAULT is used by default. |
202 | 203 |
203 number_of_cores_ = number_of_cores; | 204 number_of_cores_ = number_of_cores; |
205 RTC_DCHECK(codec_settings->codecSpecific.H264.packetization_mode != | |
206 kH264PacketizationModeNotSet); | |
hbos
2016/10/31 16:27:15
nit: RTC_DCHECK_NE
| |
204 codec_settings_ = *codec_settings; | 207 codec_settings_ = *codec_settings; |
208 max_payload_size_ = max_payload_size; | |
205 if (codec_settings_.targetBitrate == 0) | 209 if (codec_settings_.targetBitrate == 0) |
206 codec_settings_.targetBitrate = codec_settings_.startBitrate; | 210 codec_settings_.targetBitrate = codec_settings_.startBitrate; |
207 | 211 |
208 SEncParamExt encoder_params = CreateEncoderParams(); | 212 SEncParamExt encoder_params = CreateEncoderParams(); |
213 | |
209 // Initialize. | 214 // Initialize. |
210 if (openh264_encoder_->InitializeExt(&encoder_params) != 0) { | 215 if (openh264_encoder_->InitializeExt(&encoder_params) != 0) { |
211 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; | 216 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; |
212 Release(); | 217 Release(); |
213 ReportError(); | 218 ReportError(); |
214 return WEBRTC_VIDEO_CODEC_ERROR; | 219 return WEBRTC_VIDEO_CODEC_ERROR; |
215 } | 220 } |
216 // TODO(pbos): Base init params on these values before submitting. | 221 // TODO(pbos): Base init params on these values before submitting. |
217 quality_scaler_.Init(codec_settings_.codecType, codec_settings_.startBitrate, | 222 quality_scaler_.Init(codec_settings_.codecType, codec_settings_.startBitrate, |
218 codec_settings_.width, codec_settings_.height, | 223 codec_settings_.width, codec_settings_.height, |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
360 RTPFragmentationHeader frag_header; | 365 RTPFragmentationHeader frag_header; |
361 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, *frame_buffer, &info, | 366 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, *frame_buffer, &info, |
362 &frag_header); | 367 &frag_header); |
363 | 368 |
364 // Encoder can skip frames to save bandwidth in which case | 369 // Encoder can skip frames to save bandwidth in which case |
365 // |encoded_image_._length| == 0. | 370 // |encoded_image_._length| == 0. |
366 if (encoded_image_._length > 0) { | 371 if (encoded_image_._length > 0) { |
367 // Deliver encoded image. | 372 // Deliver encoded image. |
368 CodecSpecificInfo codec_specific; | 373 CodecSpecificInfo codec_specific; |
369 codec_specific.codecType = kVideoCodecH264; | 374 codec_specific.codecType = kVideoCodecH264; |
375 codec_specific.codecSpecific.H264.packetization_mode = | |
376 codec_settings_.codecSpecific.H264.packetization_mode; | |
370 encoded_image_callback_->Encoded(encoded_image_, &codec_specific, | 377 encoded_image_callback_->Encoded(encoded_image_, &codec_specific, |
371 &frag_header); | 378 &frag_header); |
372 | 379 |
373 // Parse and report QP. | 380 // Parse and report QP. |
374 h264_bitstream_parser_.ParseBitstream(encoded_image_._buffer, | 381 h264_bitstream_parser_.ParseBitstream(encoded_image_._buffer, |
375 encoded_image_._length); | 382 encoded_image_._length); |
376 int qp = -1; | 383 int qp = -1; |
377 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) | 384 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) |
378 quality_scaler_.ReportQP(qp); | 385 quality_scaler_.ReportQP(qp); |
379 } else { | 386 } else { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 encoder_params.iMultipleThreadIdc = NumberOfThreads( | 439 encoder_params.iMultipleThreadIdc = NumberOfThreads( |
433 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); | 440 encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_); |
434 // The base spatial layer 0 is the only one we use. | 441 // The base spatial layer 0 is the only one we use. |
435 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; | 442 encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth; |
436 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; | 443 encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight; |
437 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; | 444 encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate; |
438 encoder_params.sSpatialLayers[0].iSpatialBitrate = | 445 encoder_params.sSpatialLayers[0].iSpatialBitrate = |
439 encoder_params.iTargetBitrate; | 446 encoder_params.iTargetBitrate; |
440 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = | 447 encoder_params.sSpatialLayers[0].iMaxSpatialBitrate = |
441 encoder_params.iMaxBitrate; | 448 encoder_params.iMaxBitrate; |
449 switch (codec_settings_.H264().packetization_mode) { | |
450 case kH264PacketizationMode0: | |
451 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | |
452 // Limit the size of packets produced. | |
453 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_DYN_SLICE; | |
454 encoder_params.sSpatialLayers[0] | |
455 .sSliceCfg.sSliceArgument.uiSliceSizeConstraint = | |
456 static_cast<unsigned int>(max_payload_size_); | |
457 #else | |
458 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto | |
459 // design | |
460 // it with cpu core number. | |
461 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 0; | |
462 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | |
463 SM_SIZELIMITED_SLICE; | |
464 encoder_params.sSpatialLayers[0] | |
465 .sSliceCfg.sSliceArgument.uiSliceSizeConstraint = | |
466 static_cast<unsigned int>(max_payload_size_); | |
467 #endif | |
468 break; | |
469 case kH264PacketizationMode1: | |
442 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) | 470 #if (OPENH264_MAJOR == 1) && (OPENH264_MINOR <= 5) |
443 // Slice num according to number of threads. | 471 // Slice num according to number of threads. |
444 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; | 472 encoder_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; |
445 #else | 473 #else |
446 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto design | 474 // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto design |
447 // it with cpu core number. | 475 // it with cpu core number. |
448 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 0; | 476 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 0; |
449 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = | 477 encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode = |
450 SM_FIXEDSLCNUM_SLICE; | 478 SM_FIXEDSLCNUM_SLICE; |
451 #endif | 479 #endif |
452 | 480 break; |
481 default: | |
482 RTC_NOTREACHED() << "Illegal packetization mode specified"; | |
483 } | |
453 return encoder_params; | 484 return encoder_params; |
454 } | 485 } |
455 | 486 |
456 void H264EncoderImpl::ReportInit() { | 487 void H264EncoderImpl::ReportInit() { |
457 if (has_reported_init_) | 488 if (has_reported_init_) |
458 return; | 489 return; |
459 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", | 490 RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event", |
460 kH264EncoderEventInit, | 491 kH264EncoderEventInit, |
461 kH264EncoderEventMax); | 492 kH264EncoderEventMax); |
462 has_reported_init_ = true; | 493 has_reported_init_ = true; |
(...skipping 15 matching lines...) Expand all Loading... | |
478 | 509 |
479 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { | 510 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { |
480 return WEBRTC_VIDEO_CODEC_OK; | 511 return WEBRTC_VIDEO_CODEC_OK; |
481 } | 512 } |
482 | 513 |
483 void H264EncoderImpl::OnDroppedFrame() { | 514 void H264EncoderImpl::OnDroppedFrame() { |
484 quality_scaler_.ReportDroppedFrame(); | 515 quality_scaler_.ReportDroppedFrame(); |
485 } | 516 } |
486 | 517 |
487 } // namespace webrtc | 518 } // namespace webrtc |
OLD | NEW |