1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  */
11 
12 // Everything declared/defined in this header is only required when WebRTC is
13 // build with H264 support, please do not move anything out of the
14 // #ifdef unless needed and tested.
15 #ifdef WEBRTC_USE_H264
16 
17 #include "modules/video_coding/codecs/h264/h264_encoder_impl.h"
18 
19 #include <limits>
20 #include <string>
21 
22 #include "absl/strings/match.h"
23 #include "common_video/libyuv/include/webrtc_libyuv.h"
24 #include "modules/video_coding/utility/simulcast_rate_allocator.h"
25 #include "modules/video_coding/utility/simulcast_utility.h"
26 #include "rtc_base/checks.h"
27 #include "rtc_base/logging.h"
28 #include "rtc_base/time_utils.h"
29 #include "system_wrappers/include/metrics.h"
30 #include "third_party/libyuv/include/libyuv/convert.h"
31 #include "third_party/libyuv/include/libyuv/scale.h"
32 #include "third_party/openh264/src/codec/api/svc/codec_api.h"
33 #include "third_party/openh264/src/codec/api/svc/codec_app_def.h"
34 #include "third_party/openh264/src/codec/api/svc/codec_def.h"
35 #include "third_party/openh264/src/codec/api/svc/codec_ver.h"
36 
37 namespace webrtc {
38 
39 namespace {
40 
41 const bool kOpenH264EncoderDetailedLogging = false;
42 
43 // QP scaling thresholds.
44 static const int kLowH264QpThreshold = 24;
45 static const int kHighH264QpThreshold = 37;
46 
47 // Used by histograms. Values of entries should not be changed.
48 enum H264EncoderImplEvent {
49   kH264EncoderEventInit = 0,
50   kH264EncoderEventError = 1,
51   kH264EncoderEventMax = 16,
52 };
53 
NumberOfThreads(int width,int height,int number_of_cores)54 int NumberOfThreads(int width, int height, int number_of_cores) {
55   // TODO(hbos): In Chromium, multiple threads do not work with sandbox on Mac,
56   // see crbug.com/583348. Until further investigated, only use one thread.
57   //  if (width * height >= 1920 * 1080 && number_of_cores > 8) {
58   //    return 8;  // 8 threads for 1080p on high perf machines.
59   //  } else if (width * height > 1280 * 960 && number_of_cores >= 6) {
60   //    return 3;  // 3 threads for 1080p.
61   //  } else if (width * height > 640 * 480 && number_of_cores >= 3) {
62   //    return 2;  // 2 threads for qHD/HD.
63   //  } else {
64   //    return 1;  // 1 thread for VGA or less.
65   //  }
66   // TODO(sprang): Also check sSliceArgument.uiSliceNum om GetEncoderPrams(),
67   //               before enabling multithreading here.
68   return 1;
69 }
70 
ConvertToVideoFrameType(EVideoFrameType type)71 VideoFrameType ConvertToVideoFrameType(EVideoFrameType type) {
72   switch (type) {
73     case videoFrameTypeIDR:
74       return VideoFrameType::kVideoFrameKey;
75     case videoFrameTypeSkip:
76     case videoFrameTypeI:
77     case videoFrameTypeP:
78     case videoFrameTypeIPMixed:
79       return VideoFrameType::kVideoFrameDelta;
80     case videoFrameTypeInvalid:
81       break;
82   }
83   RTC_NOTREACHED() << "Unexpected/invalid frame type: " << type;
84   return VideoFrameType::kEmptyFrame;
85 }
86 
87 }  // namespace
88 
89 // Helper method used by H264EncoderImpl::Encode.
90 // Copies the encoded bytes from |info| to |encoded_image|. The
91 // |encoded_image->_buffer| may be deleted and reallocated if a bigger buffer is
92 // required.
93 //
94 // After OpenH264 encoding, the encoded bytes are stored in |info| spread out
95 // over a number of layers and "NAL units". Each NAL unit is a fragment starting
96 // with the four-byte start code {0,0,0,1}. All of this data (including the
97 // start codes) is copied to the |encoded_image->_buffer|.
RtpFragmentize(EncodedImage * encoded_image,SFrameBSInfo * info)98 static void RtpFragmentize(EncodedImage* encoded_image, SFrameBSInfo* info) {
99   // Calculate minimum buffer size required to hold encoded data.
100   size_t required_capacity = 0;
101   size_t fragments_count = 0;
102   for (int layer = 0; layer < info->iLayerNum; ++layer) {
103     const SLayerBSInfo& layerInfo = info->sLayerInfo[layer];
104     for (int nal = 0; nal < layerInfo.iNalCount; ++nal, ++fragments_count) {
105       RTC_CHECK_GE(layerInfo.pNalLengthInByte[nal], 0);
106       // Ensure |required_capacity| will not overflow.
107       RTC_CHECK_LE(layerInfo.pNalLengthInByte[nal],
108                    std::numeric_limits<size_t>::max() - required_capacity);
109       required_capacity += layerInfo.pNalLengthInByte[nal];
110     }
111   }
112   // TODO(nisse): Use a cache or buffer pool to avoid allocation?
113   encoded_image->SetEncodedData(EncodedImageBuffer::Create(required_capacity));
114 
115   // Iterate layers and NAL units, note each NAL unit as a fragment and copy
116   // the data to |encoded_image->_buffer|.
117   const uint8_t start_code[4] = {0, 0, 0, 1};
118   size_t frag = 0;
119   encoded_image->set_size(0);
120   for (int layer = 0; layer < info->iLayerNum; ++layer) {
121     const SLayerBSInfo& layerInfo = info->sLayerInfo[layer];
122     // Iterate NAL units making up this layer, noting fragments.
123     size_t layer_len = 0;
124     for (int nal = 0; nal < layerInfo.iNalCount; ++nal, ++frag) {
125       // Because the sum of all layer lengths, |required_capacity|, fits in a
126       // |size_t|, we know that any indices in-between will not overflow.
127       RTC_DCHECK_GE(layerInfo.pNalLengthInByte[nal], 4);
128       RTC_DCHECK_EQ(layerInfo.pBsBuf[layer_len + 0], start_code[0]);
129       RTC_DCHECK_EQ(layerInfo.pBsBuf[layer_len + 1], start_code[1]);
130       RTC_DCHECK_EQ(layerInfo.pBsBuf[layer_len + 2], start_code[2]);
131       RTC_DCHECK_EQ(layerInfo.pBsBuf[layer_len + 3], start_code[3]);
132       layer_len += layerInfo.pNalLengthInByte[nal];
133     }
134     // Copy the entire layer's data (including start codes).
135     memcpy(encoded_image->data() + encoded_image->size(), layerInfo.pBsBuf,
136            layer_len);
137     encoded_image->set_size(encoded_image->size() + layer_len);
138   }
139 }
140 
H264EncoderImpl(const cricket::VideoCodec & codec)141 H264EncoderImpl::H264EncoderImpl(const cricket::VideoCodec& codec)
142     : packetization_mode_(H264PacketizationMode::SingleNalUnit),
143       max_payload_size_(0),
144       number_of_cores_(0),
145       encoded_image_callback_(nullptr),
146       has_reported_init_(false),
147       has_reported_error_(false) {
148   RTC_CHECK(absl::EqualsIgnoreCase(codec.name, cricket::kH264CodecName));
149   std::string packetization_mode_string;
150   if (codec.GetParam(cricket::kH264FmtpPacketizationMode,
151                      &packetization_mode_string) &&
152       packetization_mode_string == "1") {
153     packetization_mode_ = H264PacketizationMode::NonInterleaved;
154   }
155   downscaled_buffers_.reserve(kMaxSimulcastStreams - 1);
156   encoded_images_.reserve(kMaxSimulcastStreams);
157   encoders_.reserve(kMaxSimulcastStreams);
158   configurations_.reserve(kMaxSimulcastStreams);
159   tl0sync_limit_.reserve(kMaxSimulcastStreams);
160 }
161 
~H264EncoderImpl()162 H264EncoderImpl::~H264EncoderImpl() {
163   Release();
164 }
165 
InitEncode(const VideoCodec * inst,const VideoEncoder::Settings & settings)166 int32_t H264EncoderImpl::InitEncode(const VideoCodec* inst,
167                                     const VideoEncoder::Settings& settings) {
168   ReportInit();
169   if (!inst || inst->codecType != kVideoCodecH264) {
170     ReportError();
171     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
172   }
173   if (inst->maxFramerate == 0) {
174     ReportError();
175     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
176   }
177   if (inst->width < 1 || inst->height < 1) {
178     ReportError();
179     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
180   }
181 
182   int32_t release_ret = Release();
183   if (release_ret != WEBRTC_VIDEO_CODEC_OK) {
184     ReportError();
185     return release_ret;
186   }
187 
188   int number_of_streams = SimulcastUtility::NumberOfSimulcastStreams(*inst);
189   bool doing_simulcast = (number_of_streams > 1);
190 
191   if (doing_simulcast &&
192       !SimulcastUtility::ValidSimulcastParameters(*inst, number_of_streams)) {
193     return WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED;
194   }
195   downscaled_buffers_.resize(number_of_streams - 1);
196   encoded_images_.resize(number_of_streams);
197   encoders_.resize(number_of_streams);
198   pictures_.resize(number_of_streams);
199   configurations_.resize(number_of_streams);
200   tl0sync_limit_.resize(number_of_streams);
201 
202   number_of_cores_ = settings.number_of_cores;
203   max_payload_size_ = settings.max_payload_size;
204   codec_ = *inst;
205 
206   // Code expects simulcastStream resolutions to be correct, make sure they are
207   // filled even when there are no simulcast layers.
208   if (codec_.numberOfSimulcastStreams == 0) {
209     codec_.simulcastStream[0].width = codec_.width;
210     codec_.simulcastStream[0].height = codec_.height;
211   }
212 
213   for (int i = 0, idx = number_of_streams - 1; i < number_of_streams;
214        ++i, --idx) {
215     ISVCEncoder* openh264_encoder;
216     // Create encoder.
217     if (WelsCreateSVCEncoder(&openh264_encoder) != 0) {
218       // Failed to create encoder.
219       RTC_LOG(LS_ERROR) << "Failed to create OpenH264 encoder";
220       RTC_DCHECK(!openh264_encoder);
221       Release();
222       ReportError();
223       return WEBRTC_VIDEO_CODEC_ERROR;
224     }
225     RTC_DCHECK(openh264_encoder);
226     if (kOpenH264EncoderDetailedLogging) {
227       int trace_level = WELS_LOG_DETAIL;
228       openh264_encoder->SetOption(ENCODER_OPTION_TRACE_LEVEL, &trace_level);
229     }
230     // else WELS_LOG_DEFAULT is used by default.
231 
232     // Store h264 encoder.
233     encoders_[i] = openh264_encoder;
234 
235     // Set internal settings from codec_settings
236     configurations_[i].simulcast_idx = idx;
237     configurations_[i].sending = false;
238     configurations_[i].width = codec_.simulcastStream[idx].width;
239     configurations_[i].height = codec_.simulcastStream[idx].height;
240     configurations_[i].max_frame_rate = static_cast<float>(codec_.maxFramerate);
241     configurations_[i].frame_dropping_on = codec_.H264()->frameDroppingOn;
242     configurations_[i].key_frame_interval = codec_.H264()->keyFrameInterval;
243     configurations_[i].num_temporal_layers =
244         codec_.simulcastStream[idx].numberOfTemporalLayers;
245 
246     // Create downscaled image buffers.
247     if (i > 0) {
248       downscaled_buffers_[i - 1] = I420Buffer::Create(
249           configurations_[i].width, configurations_[i].height,
250           configurations_[i].width, configurations_[i].width / 2,
251           configurations_[i].width / 2);
252     }
253 
254     // Codec_settings uses kbits/second; encoder uses bits/second.
255     configurations_[i].max_bps = codec_.maxBitrate * 1000;
256     configurations_[i].target_bps = codec_.startBitrate * 1000;
257 
258     // Create encoder parameters based on the layer configuration.
259     SEncParamExt encoder_params = CreateEncoderParams(i);
260 
261     // Initialize.
262     if (openh264_encoder->InitializeExt(&encoder_params) != 0) {
263       RTC_LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder";
264       Release();
265       ReportError();
266       return WEBRTC_VIDEO_CODEC_ERROR;
267     }
268     // TODO(pbos): Base init params on these values before submitting.
269     int video_format = EVideoFormatType::videoFormatI420;
270     openh264_encoder->SetOption(ENCODER_OPTION_DATAFORMAT, &video_format);
271 
272     // Initialize encoded image. Default buffer size: size of unencoded data.
273 
274     const size_t new_capacity =
275         CalcBufferSize(VideoType::kI420, codec_.simulcastStream[idx].width,
276                        codec_.simulcastStream[idx].height);
277     encoded_images_[i].SetEncodedData(EncodedImageBuffer::Create(new_capacity));
278     encoded_images_[i]._completeFrame = true;
279     encoded_images_[i]._encodedWidth = codec_.simulcastStream[idx].width;
280     encoded_images_[i]._encodedHeight = codec_.simulcastStream[idx].height;
281     encoded_images_[i].set_size(0);
282 
283     tl0sync_limit_[i] = configurations_[i].num_temporal_layers;
284   }
285 
286   SimulcastRateAllocator init_allocator(codec_);
287   VideoBitrateAllocation allocation =
288       init_allocator.Allocate(VideoBitrateAllocationParameters(
289           DataRate::KilobitsPerSec(codec_.startBitrate), codec_.maxFramerate));
290   SetRates(RateControlParameters(allocation, codec_.maxFramerate));
291   return WEBRTC_VIDEO_CODEC_OK;
292 }
293 
Release()294 int32_t H264EncoderImpl::Release() {
295   while (!encoders_.empty()) {
296     ISVCEncoder* openh264_encoder = encoders_.back();
297     if (openh264_encoder) {
298       RTC_CHECK_EQ(0, openh264_encoder->Uninitialize());
299       WelsDestroySVCEncoder(openh264_encoder);
300     }
301     encoders_.pop_back();
302   }
303   downscaled_buffers_.clear();
304   configurations_.clear();
305   encoded_images_.clear();
306   pictures_.clear();
307   tl0sync_limit_.clear();
308   return WEBRTC_VIDEO_CODEC_OK;
309 }
310 
RegisterEncodeCompleteCallback(EncodedImageCallback * callback)311 int32_t H264EncoderImpl::RegisterEncodeCompleteCallback(
312     EncodedImageCallback* callback) {
313   encoded_image_callback_ = callback;
314   return WEBRTC_VIDEO_CODEC_OK;
315 }
316 
SetRates(const RateControlParameters & parameters)317 void H264EncoderImpl::SetRates(const RateControlParameters& parameters) {
318   if (encoders_.empty()) {
319     RTC_LOG(LS_WARNING) << "SetRates() while uninitialized.";
320     return;
321   }
322 
323   if (parameters.framerate_fps < 1.0) {
324     RTC_LOG(LS_WARNING) << "Invalid frame rate: " << parameters.framerate_fps;
325     return;
326   }
327 
328   if (parameters.bitrate.get_sum_bps() == 0) {
329     // Encoder paused, turn off all encoding.
330     for (size_t i = 0; i < configurations_.size(); ++i) {
331       configurations_[i].SetStreamState(false);
332     }
333     return;
334   }
335 
336   codec_.maxFramerate = static_cast<uint32_t>(parameters.framerate_fps);
337 
338   size_t stream_idx = encoders_.size() - 1;
339   for (size_t i = 0; i < encoders_.size(); ++i, --stream_idx) {
340     // Update layer config.
341     configurations_[i].target_bps =
342         parameters.bitrate.GetSpatialLayerSum(stream_idx);
343     configurations_[i].max_frame_rate = parameters.framerate_fps;
344 
345     if (configurations_[i].target_bps) {
346       configurations_[i].SetStreamState(true);
347 
348       // Update h264 encoder.
349       SBitrateInfo target_bitrate;
350       memset(&target_bitrate, 0, sizeof(SBitrateInfo));
351       target_bitrate.iLayer = SPATIAL_LAYER_ALL,
352       target_bitrate.iBitrate = configurations_[i].target_bps;
353       encoders_[i]->SetOption(ENCODER_OPTION_BITRATE, &target_bitrate);
354       encoders_[i]->SetOption(ENCODER_OPTION_FRAME_RATE,
355                               &configurations_[i].max_frame_rate);
356     } else {
357       configurations_[i].SetStreamState(false);
358     }
359   }
360 }
361 
Encode(const VideoFrame & input_frame,const std::vector<VideoFrameType> * frame_types)362 int32_t H264EncoderImpl::Encode(
363     const VideoFrame& input_frame,
364     const std::vector<VideoFrameType>* frame_types) {
365   if (encoders_.empty()) {
366     ReportError();
367     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
368   }
369   if (!encoded_image_callback_) {
370     RTC_LOG(LS_WARNING)
371         << "InitEncode() has been called, but a callback function "
372            "has not been set with RegisterEncodeCompleteCallback()";
373     ReportError();
374     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
375   }
376 
377   rtc::scoped_refptr<const I420BufferInterface> frame_buffer =
378       input_frame.video_frame_buffer()->ToI420();
379 
380   bool send_key_frame = false;
381   for (size_t i = 0; i < configurations_.size(); ++i) {
382     if (configurations_[i].key_frame_request && configurations_[i].sending) {
383       send_key_frame = true;
384       break;
385     }
386   }
387 
388   if (!send_key_frame && frame_types) {
389     for (size_t i = 0; i < configurations_.size(); ++i) {
390       const size_t simulcast_idx =
391           static_cast<size_t>(configurations_[i].simulcast_idx);
392       if (configurations_[i].sending && simulcast_idx < frame_types->size() &&
393           (*frame_types)[simulcast_idx] == VideoFrameType::kVideoFrameKey) {
394         send_key_frame = true;
395         break;
396       }
397     }
398   }
399 
400   RTC_DCHECK_EQ(configurations_[0].width, frame_buffer->width());
401   RTC_DCHECK_EQ(configurations_[0].height, frame_buffer->height());
402 
403   // Encode image for each layer.
404   for (size_t i = 0; i < encoders_.size(); ++i) {
405     // EncodeFrame input.
406     pictures_[i] = {0};
407     pictures_[i].iPicWidth = configurations_[i].width;
408     pictures_[i].iPicHeight = configurations_[i].height;
409     pictures_[i].iColorFormat = EVideoFormatType::videoFormatI420;
410     pictures_[i].uiTimeStamp = input_frame.ntp_time_ms();
411     // Downscale images on second and ongoing layers.
412     if (i == 0) {
413       pictures_[i].iStride[0] = frame_buffer->StrideY();
414       pictures_[i].iStride[1] = frame_buffer->StrideU();
415       pictures_[i].iStride[2] = frame_buffer->StrideV();
416       pictures_[i].pData[0] = const_cast<uint8_t*>(frame_buffer->DataY());
417       pictures_[i].pData[1] = const_cast<uint8_t*>(frame_buffer->DataU());
418       pictures_[i].pData[2] = const_cast<uint8_t*>(frame_buffer->DataV());
419     } else {
420       pictures_[i].iStride[0] = downscaled_buffers_[i - 1]->StrideY();
421       pictures_[i].iStride[1] = downscaled_buffers_[i - 1]->StrideU();
422       pictures_[i].iStride[2] = downscaled_buffers_[i - 1]->StrideV();
423       pictures_[i].pData[0] =
424           const_cast<uint8_t*>(downscaled_buffers_[i - 1]->DataY());
425       pictures_[i].pData[1] =
426           const_cast<uint8_t*>(downscaled_buffers_[i - 1]->DataU());
427       pictures_[i].pData[2] =
428           const_cast<uint8_t*>(downscaled_buffers_[i - 1]->DataV());
429       // Scale the image down a number of times by downsampling factor.
430       libyuv::I420Scale(pictures_[i - 1].pData[0], pictures_[i - 1].iStride[0],
431                         pictures_[i - 1].pData[1], pictures_[i - 1].iStride[1],
432                         pictures_[i - 1].pData[2], pictures_[i - 1].iStride[2],
433                         configurations_[i - 1].width,
434                         configurations_[i - 1].height, pictures_[i].pData[0],
435                         pictures_[i].iStride[0], pictures_[i].pData[1],
436                         pictures_[i].iStride[1], pictures_[i].pData[2],
437                         pictures_[i].iStride[2], configurations_[i].width,
438                         configurations_[i].height, libyuv::kFilterBilinear);
439     }
440 
441     if (!configurations_[i].sending) {
442       continue;
443     }
444     if (frame_types != nullptr) {
445       // Skip frame?
446       if ((*frame_types)[i] == VideoFrameType::kEmptyFrame) {
447         continue;
448       }
449     }
450     if (send_key_frame) {
451       // API doc says ForceIntraFrame(false) does nothing, but calling this
452       // function forces a key frame regardless of the |bIDR| argument's value.
453       // (If every frame is a key frame we get lag/delays.)
454       encoders_[i]->ForceIntraFrame(true);
455       configurations_[i].key_frame_request = false;
456     }
457     // EncodeFrame output.
458     SFrameBSInfo info;
459     memset(&info, 0, sizeof(SFrameBSInfo));
460 
461     // Encode!
462     int enc_ret = encoders_[i]->EncodeFrame(&pictures_[i], &info);
463     if (enc_ret != 0) {
464       RTC_LOG(LS_ERROR)
465           << "OpenH264 frame encoding failed, EncodeFrame returned " << enc_ret
466           << ".";
467       ReportError();
468       return WEBRTC_VIDEO_CODEC_ERROR;
469     }
470 
471     encoded_images_[i]._encodedWidth = configurations_[i].width;
472     encoded_images_[i]._encodedHeight = configurations_[i].height;
473     encoded_images_[i].SetTimestamp(input_frame.timestamp());
474     encoded_images_[i]._frameType = ConvertToVideoFrameType(info.eFrameType);
475     encoded_images_[i].SetSpatialIndex(configurations_[i].simulcast_idx);
476 
477     // Split encoded image up into fragments. This also updates
478     // |encoded_image_|.
479     RtpFragmentize(&encoded_images_[i], &info);
480 
481     // Encoder can skip frames to save bandwidth in which case
482     // |encoded_images_[i]._length| == 0.
483     if (encoded_images_[i].size() > 0) {
484       // Parse QP.
485       h264_bitstream_parser_.ParseBitstream(encoded_images_[i].data(),
486                                             encoded_images_[i].size());
487       h264_bitstream_parser_.GetLastSliceQp(&encoded_images_[i].qp_);
488 
489       // Deliver encoded image.
490       CodecSpecificInfo codec_specific;
491       codec_specific.codecType = kVideoCodecH264;
492       codec_specific.codecSpecific.H264.packetization_mode =
493           packetization_mode_;
494       codec_specific.codecSpecific.H264.temporal_idx = kNoTemporalIdx;
495       codec_specific.codecSpecific.H264.idr_frame =
496           info.eFrameType == videoFrameTypeIDR;
497       codec_specific.codecSpecific.H264.base_layer_sync = false;
498       if (configurations_[i].num_temporal_layers > 1) {
499         const uint8_t tid = info.sLayerInfo[0].uiTemporalId;
500         codec_specific.codecSpecific.H264.temporal_idx = tid;
501         codec_specific.codecSpecific.H264.base_layer_sync =
502             tid > 0 && tid < tl0sync_limit_[i];
503         if (codec_specific.codecSpecific.H264.base_layer_sync) {
504           tl0sync_limit_[i] = tid;
505         }
506         if (tid == 0) {
507           tl0sync_limit_[i] = configurations_[i].num_temporal_layers;
508         }
509       }
510       encoded_image_callback_->OnEncodedImage(encoded_images_[i],
511                                               &codec_specific);
512     }
513   }
514   return WEBRTC_VIDEO_CODEC_OK;
515 }
516 
517 // Initialization parameters.
518 // There are two ways to initialize. There is SEncParamBase (cleared with
519 // memset(&p, 0, sizeof(SEncParamBase)) used in Initialize, and SEncParamExt
520 // which is a superset of SEncParamBase (cleared with GetDefaultParams) used
521 // in InitializeExt.
CreateEncoderParams(size_t i) const522 SEncParamExt H264EncoderImpl::CreateEncoderParams(size_t i) const {
523   SEncParamExt encoder_params;
524   encoders_[i]->GetDefaultParams(&encoder_params);
525   if (codec_.mode == VideoCodecMode::kRealtimeVideo) {
526     encoder_params.iUsageType = CAMERA_VIDEO_REAL_TIME;
527   } else if (codec_.mode == VideoCodecMode::kScreensharing) {
528     encoder_params.iUsageType = SCREEN_CONTENT_REAL_TIME;
529   } else {
530     RTC_NOTREACHED();
531   }
532   encoder_params.iPicWidth = configurations_[i].width;
533   encoder_params.iPicHeight = configurations_[i].height;
534   encoder_params.iTargetBitrate = configurations_[i].target_bps;
535   // Keep unspecified. WebRTC's max codec bitrate is not the same setting
536   // as OpenH264's iMaxBitrate. More details in https://crbug.com/webrtc/11543
537   encoder_params.iMaxBitrate = UNSPECIFIED_BIT_RATE;
538   // Rate Control mode
539   encoder_params.iRCMode = RC_BITRATE_MODE;
540   encoder_params.fMaxFrameRate = configurations_[i].max_frame_rate;
541 
542   // The following parameters are extension parameters (they're in SEncParamExt,
543   // not in SEncParamBase).
544   encoder_params.bEnableFrameSkip = configurations_[i].frame_dropping_on;
545   // |uiIntraPeriod|    - multiple of GOP size
546   // |keyFrameInterval| - number of frames
547   encoder_params.uiIntraPeriod = configurations_[i].key_frame_interval;
548   encoder_params.uiMaxNalSize = 0;
549   // Threading model: use auto.
550   //  0: auto (dynamic imp. internal encoder)
551   //  1: single thread (default value)
552   // >1: number of threads
553   encoder_params.iMultipleThreadIdc = NumberOfThreads(
554       encoder_params.iPicWidth, encoder_params.iPicHeight, number_of_cores_);
555   // The base spatial layer 0 is the only one we use.
556   encoder_params.sSpatialLayers[0].iVideoWidth = encoder_params.iPicWidth;
557   encoder_params.sSpatialLayers[0].iVideoHeight = encoder_params.iPicHeight;
558   encoder_params.sSpatialLayers[0].fFrameRate = encoder_params.fMaxFrameRate;
559   encoder_params.sSpatialLayers[0].iSpatialBitrate =
560       encoder_params.iTargetBitrate;
561   encoder_params.sSpatialLayers[0].iMaxSpatialBitrate =
562       encoder_params.iMaxBitrate;
563   encoder_params.iTemporalLayerNum = configurations_[i].num_temporal_layers;
564   if (encoder_params.iTemporalLayerNum > 1) {
565     encoder_params.iNumRefFrame = 1;
566   }
567   RTC_LOG(INFO) << "OpenH264 version is " << OPENH264_MAJOR << "."
568                 << OPENH264_MINOR;
569   switch (packetization_mode_) {
570     case H264PacketizationMode::SingleNalUnit:
571       // Limit the size of the packets produced.
572       encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1;
573       encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode =
574           SM_SIZELIMITED_SLICE;
575       encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceSizeConstraint =
576           static_cast<unsigned int>(max_payload_size_);
577       RTC_LOG(INFO) << "Encoder is configured with NALU constraint: "
578                     << max_payload_size_ << " bytes";
579       break;
580     case H264PacketizationMode::NonInterleaved:
581       // When uiSliceMode = SM_FIXEDSLCNUM_SLICE, uiSliceNum = 0 means auto
582       // design it with cpu core number.
583       // TODO(sprang): Set to 0 when we understand why the rate controller borks
584       //               when uiSliceNum > 1.
585       encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceNum = 1;
586       encoder_params.sSpatialLayers[0].sSliceArgument.uiSliceMode =
587           SM_FIXEDSLCNUM_SLICE;
588       break;
589   }
590   return encoder_params;
591 }
592 
ReportInit()593 void H264EncoderImpl::ReportInit() {
594   if (has_reported_init_)
595     return;
596   RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event",
597                             kH264EncoderEventInit, kH264EncoderEventMax);
598   has_reported_init_ = true;
599 }
600 
ReportError()601 void H264EncoderImpl::ReportError() {
602   if (has_reported_error_)
603     return;
604   RTC_HISTOGRAM_ENUMERATION("WebRTC.Video.H264EncoderImpl.Event",
605                             kH264EncoderEventError, kH264EncoderEventMax);
606   has_reported_error_ = true;
607 }
608 
GetEncoderInfo() const609 VideoEncoder::EncoderInfo H264EncoderImpl::GetEncoderInfo() const {
610   EncoderInfo info;
611   info.supports_native_handle = false;
612   info.implementation_name = "OpenH264";
613   info.scaling_settings =
614       VideoEncoder::ScalingSettings(kLowH264QpThreshold, kHighH264QpThreshold);
615   info.is_hardware_accelerated = false;
616   info.has_internal_source = false;
617   info.supports_simulcast = true;
618   return info;
619 }
620 
SetStreamState(bool send_stream)621 void H264EncoderImpl::LayerConfig::SetStreamState(bool send_stream) {
622   if (send_stream && !sending) {
623     // Need a key frame if we have not sent this stream before.
624     key_frame_request = true;
625   }
626   sending = send_stream;
627 }
628 
629 }  // namespace webrtc
630 
631 #endif  // WEBRTC_USE_H264
632