OLD | NEW |
(Empty) | |
| 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 // TODO(hbos): This is essentially a copy of an encoder class in WebRTC that as |
| 13 // of this statement has not yet landed, but that I want to have accessible in |
| 14 // Chromium before that CL lands. This is because I use it in order to validate |
| 15 // the build files for OpenH264 and the WebRTC encoder/decoder CL cannot land |
| 16 // until I can build OpenH264 from source. Once the build files are stable I |
| 17 // will land both CLs and remove this copy of the encoder. |
| 18 |
| 19 #include "openh264/testing/h264_encoder_impl.h" |
| 20 |
| 21 // OpenH264 |
| 22 #include "openh264/src/codec/api/svc/codec_api.h" |
| 23 #include "openh264/src/codec/api/svc/codec_app_def.h" |
| 24 #include "openh264/src/codec/api/svc/codec_def.h" |
| 25 |
| 26 #include "webrtc/base/checks.h" |
| 27 #include "webrtc/base/logging.h" |
| 28 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 29 |
| 30 using rtc::LS_WARNING; |
| 31 using rtc::LS_ERROR; |
| 32 using webrtc::kRealtimeVideo; |
| 33 using webrtc::kScreensharing; |
| 34 using webrtc::kUPlane; |
| 35 using webrtc::kVideoFrameDelta; |
| 36 using webrtc::kVideoFrameKey; |
| 37 using webrtc::kVPlane; |
| 38 using webrtc::kYPlane; |
| 39 using webrtc::RTPFragmentationHeader; |
| 40 using webrtc::VideoType; |
| 41 using webrtc::VideoCodecType; |
| 42 |
| 43 namespace openh264 { |
| 44 |
| 45 namespace { |
| 46 const bool kOpenH264EncoderDetailedLogging = false; |
| 47 } // namespace |
| 48 |
| 49 static FrameType EVideoFrameType_to_FrameType( |
| 50 EVideoFrameType type) { |
| 51 switch (type) { |
| 52 case videoFrameTypeInvalid: |
| 53 return kVideoFrameDelta; // TODO(hbos): handle error |
| 54 case videoFrameTypeSkip: |
| 55 return kVideoFrameDelta; |
| 56 case videoFrameTypeIDR: |
| 57 return kVideoFrameKey; |
| 58 case videoFrameTypeI: |
| 59 case videoFrameTypeP: |
| 60 case videoFrameTypeIPMixed: |
| 61 return kVideoFrameDelta; |
| 62 default: |
| 63 return kVideoFrameDelta; |
| 64 } |
| 65 } |
| 66 // Helper method used by H264EncoderImpl::Encode. |
| 67 // Copies the encoded bytes from |info| to |encoded_image| and updates the |
| 68 // fragmentation information of |frag_header|. The |encoded_image->_buffer| may |
| 69 // be deleted and reallocated if a bigger buffer is required. |
| 70 // After OpenH264 encoding, the encoded bytes are stored in |info| spread out |
| 71 // over a number of layers and "NAL units". Each NAL unit is a fragment starting |
| 72 // with the four-byte start code {0,0,0,1}. All of this data (including the |
| 73 // start codes) is copied to the |encoded_image->_buffer| and the |frag_header| |
| 74 // is updated to point to each fragment, with offsets and lengths set as to |
| 75 // exclude the start codes. |
| 76 static void RtpFragmentize(EncodedImage* encoded_image, |
| 77 rtc::scoped_ptr<uint8_t[]>* encoded_image_buffer, |
| 78 const VideoFrame& frame, |
| 79 SFrameBSInfo* info, |
| 80 RTPFragmentationHeader* frag_header) { |
| 81 // Calculate minimum buffer size required to hold encoded data. |
| 82 size_t required_size = 0; |
| 83 size_t fragments_count = 0; |
| 84 for (int iLayer = 0; iLayer < info->iLayerNum; ++iLayer) { |
| 85 const SLayerBSInfo& layerInfo = info->sLayerInfo[iLayer]; |
| 86 for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) { |
| 87 required_size += layerInfo.pNalLengthInByte[iNal]; |
| 88 ++fragments_count; |
| 89 } |
| 90 } |
| 91 if (encoded_image->_size < required_size) { |
| 92 // Increase buffer size. Allocate enough to hold an unencoded image, this |
| 93 // should be more than enough to hold any encoded data of future frames of |
| 94 // the same size (avoiding possible future reallocation due to variations in |
| 95 // required size). |
| 96 encoded_image->_size = CalcBufferSize( |
| 97 VideoType::kI420, frame.width(), frame.height()); |
| 98 if (encoded_image->_size < required_size) { |
| 99 // Encoded data > unencoded data, wtf? Allocate required bytes. |
| 100 encoded_image->_size = required_size; |
| 101 } |
| 102 encoded_image->_buffer = new uint8_t[encoded_image->_size]; |
| 103 encoded_image_buffer->reset(encoded_image->_buffer); |
| 104 } |
| 105 |
| 106 // Iterate layers and NAL units, note each NAL unit as a fragment and copy |
| 107 // the data to |encoded_image->_buffer|. |
| 108 const uint8_t kStartCode[4] = {0, 0, 0, 1}; |
| 109 frag_header->VerifyAndAllocateFragmentationHeader(fragments_count); |
| 110 size_t frag_i = 0; |
| 111 encoded_image->_length = 0; |
| 112 for (int iLayer = 0; iLayer < info->iLayerNum; ++iLayer) { |
| 113 const SLayerBSInfo& layerInfo = info->sLayerInfo[iLayer]; |
| 114 // Iterate NAL units making up this layer, noting fragments. |
| 115 size_t iLayerLen = 0; |
| 116 for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal, ++frag_i) { |
| 117 RTC_DCHECK_EQ(layerInfo.pBsBuf[iLayerLen+0], kStartCode[0]); |
| 118 RTC_DCHECK_EQ(layerInfo.pBsBuf[iLayerLen+1], kStartCode[1]); |
| 119 RTC_DCHECK_EQ(layerInfo.pBsBuf[iLayerLen+2], kStartCode[2]); |
| 120 RTC_DCHECK_EQ(layerInfo.pBsBuf[iLayerLen+3], kStartCode[3]); |
| 121 frag_header->fragmentationOffset[frag_i] = |
| 122 encoded_image->_length + iLayerLen + sizeof(kStartCode); |
| 123 frag_header->fragmentationLength[frag_i] = |
| 124 layerInfo.pNalLengthInByte[iNal] - sizeof(kStartCode); |
| 125 iLayerLen += layerInfo.pNalLengthInByte[iNal]; |
| 126 } |
| 127 // Copy the entire layer's data (including start codes). |
| 128 memcpy(encoded_image->_buffer + encoded_image->_length, |
| 129 layerInfo.pBsBuf, |
| 130 iLayerLen * sizeof(unsigned char)); |
| 131 encoded_image->_length += iLayerLen; |
| 132 } |
| 133 } |
| 134 |
| 135 H264EncoderImpl::H264EncoderImpl() |
| 136 : openh264_encoder_(nullptr), |
| 137 encoded_image_callback_(nullptr) { |
| 138 } |
| 139 |
| 140 H264EncoderImpl::~H264EncoderImpl() { |
| 141 Release(); |
| 142 } |
| 143 |
| 144 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, |
| 145 int32_t /*number_of_cores*/, |
| 146 size_t /*max_payload_size*/) { |
| 147 if (!codec_settings || |
| 148 codec_settings->codecType != VideoCodecType::kVideoCodecH264) { |
| 149 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 150 } |
| 151 if (codec_settings->maxFramerate == 0) |
| 152 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 153 if (codec_settings->width < 1 || codec_settings->height < 1) |
| 154 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 155 |
| 156 int release_ret = Release(); |
| 157 if (release_ret != WEBRTC_VIDEO_CODEC_OK) |
| 158 return release_ret; |
| 159 RTC_DCHECK(!openh264_encoder_); |
| 160 |
| 161 // Create encoder. |
| 162 if (WelsCreateSVCEncoder(&openh264_encoder_) != 0) { |
| 163 // Failed to create encoder. |
| 164 RTC_DCHECK(!openh264_encoder_); |
| 165 return WEBRTC_VIDEO_CODEC_ERROR; |
| 166 } |
| 167 RTC_DCHECK(openh264_encoder_); |
| 168 if (kOpenH264EncoderDetailedLogging) { |
| 169 int trace_level = WELS_LOG_DETAIL; |
| 170 openh264_encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, |
| 171 &trace_level); |
| 172 } |
| 173 // else WELS_LOG_DEFAULT is used by default. |
| 174 |
| 175 codec_settings_ = *codec_settings; |
| 176 if (codec_settings_.targetBitrate == 0) |
| 177 codec_settings_.targetBitrate = codec_settings_.startBitrate; |
| 178 |
| 179 // Initialization parameters. |
| 180 // There are two ways to initialize. There is SEncParamBase (cleared with |
| 181 // memset(&p, 0, sizeof(SEncParamBase)) used in Initialize, and SEncParamExt |
| 182 // which is a superset of SEncParamBase (cleared with GetDefaultParams) used |
| 183 // in InitializeExt. |
| 184 SEncParamExt init_params; |
| 185 openh264_encoder_->GetDefaultParams(&init_params); |
| 186 if (codec_settings_.mode == kRealtimeVideo) { |
| 187 init_params.iUsageType = CAMERA_VIDEO_REAL_TIME; |
| 188 } else if (codec_settings_.mode == kScreensharing) { |
| 189 init_params.iUsageType = SCREEN_CONTENT_REAL_TIME; |
| 190 } else { |
| 191 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 192 } |
| 193 init_params.iPicWidth = codec_settings_.width; |
| 194 init_params.iPicHeight = codec_settings_.height; |
| 195 // |init_params| uses bit/s, |codec_settings_| uses kbit/s. |
| 196 init_params.iTargetBitrate = codec_settings_.targetBitrate * 1000; |
| 197 init_params.iMaxBitrate = codec_settings_.maxBitrate * 1000; |
| 198 // Rate Control mode |
| 199 init_params.iRCMode = RC_BITRATE_MODE; |
| 200 init_params.fMaxFrameRate = static_cast<float>(codec_settings_.maxFramerate); |
| 201 |
| 202 // The following parameters are extension parameters (they're in SEncParamExt, |
| 203 // not in SEncParamBase). |
| 204 init_params.bEnableFrameSkip = |
| 205 codec_settings_.codecSpecific.H264.frameDroppingOn; |
| 206 // |uiIntraPeriod| - multiple of GOP size |
| 207 // |keyFrameInterval| - number of frames |
| 208 init_params.uiIntraPeriod = |
| 209 codec_settings_.codecSpecific.H264.keyFrameInterval; |
| 210 init_params.uiMaxNalSize = 0; |
| 211 // Threading model: use auto. |
| 212 // 0: auto (dynamic imp. internal encoder) |
| 213 // 1: single thread (default value) |
| 214 // >1: number of threads |
| 215 init_params.iMultipleThreadIdc = 0; |
| 216 // The base spatial layer 0 is the only one we use. |
| 217 init_params.sSpatialLayers[0].iVideoWidth = init_params.iPicWidth; |
| 218 init_params.sSpatialLayers[0].iVideoHeight = init_params.iPicHeight; |
| 219 init_params.sSpatialLayers[0].fFrameRate = init_params.fMaxFrameRate; |
| 220 init_params.sSpatialLayers[0].iSpatialBitrate = init_params.iTargetBitrate; |
| 221 init_params.sSpatialLayers[0].iMaxSpatialBitrate = init_params.iMaxBitrate; |
| 222 // Slice num according to number of threads. |
| 223 init_params.sSpatialLayers[0].sSliceCfg.uiSliceMode = SM_AUTO_SLICE; |
| 224 |
| 225 // Initialize. |
| 226 if (openh264_encoder_->InitializeExt(&init_params) != 0) { |
| 227 Release(); |
| 228 return WEBRTC_VIDEO_CODEC_ERROR; |
| 229 } |
| 230 int video_format = EVideoFormatType::videoFormatI420; |
| 231 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, |
| 232 &video_format); |
| 233 |
| 234 // Initialize encoded image. Default buffer size: size of unencoded data. |
| 235 encoded_image_._size = CalcBufferSize( |
| 236 VideoType::kI420, codec_settings_.width, codec_settings_.height); |
| 237 encoded_image_._buffer = new uint8_t[encoded_image_._size]; |
| 238 encoded_image_buffer_.reset(encoded_image_._buffer); |
| 239 encoded_image_._completeFrame = true; |
| 240 encoded_image_._encodedWidth = 0; |
| 241 encoded_image_._encodedHeight = 0; |
| 242 encoded_image_._length = 0; |
| 243 return WEBRTC_VIDEO_CODEC_OK; |
| 244 } |
| 245 |
| 246 int32_t H264EncoderImpl::Release() { |
| 247 if (openh264_encoder_) { |
| 248 openh264_encoder_->Uninitialize(); |
| 249 WelsDestroySVCEncoder(openh264_encoder_); |
| 250 openh264_encoder_ = nullptr; |
| 251 } |
| 252 if (encoded_image_._buffer != nullptr) { |
| 253 encoded_image_._buffer = nullptr; |
| 254 encoded_image_buffer_.reset(); |
| 255 } |
| 256 return WEBRTC_VIDEO_CODEC_OK; |
| 257 } |
| 258 |
| 259 int32_t H264EncoderImpl::RegisterEncodeCompleteCallback( |
| 260 EncodedImageCallback* callback) { |
| 261 encoded_image_callback_ = callback; |
| 262 return WEBRTC_VIDEO_CODEC_OK; |
| 263 } |
| 264 |
| 265 int32_t H264EncoderImpl::SetRates(uint32_t bitrate, uint32_t framerate) { |
| 266 if (bitrate <= 0 || framerate <= 0) { |
| 267 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 268 } |
| 269 codec_settings_.targetBitrate = bitrate; |
| 270 codec_settings_.maxFramerate = framerate; |
| 271 |
| 272 SBitrateInfo target_bitrate; |
| 273 memset(&target_bitrate, 0, sizeof(SBitrateInfo)); |
| 274 target_bitrate.iLayer = SPATIAL_LAYER_ALL, |
| 275 target_bitrate.iBitrate = codec_settings_.targetBitrate * 1000; |
| 276 openh264_encoder_->SetOption(ENCODER_OPTION_BITRATE, |
| 277 &target_bitrate); |
| 278 float max_framerate = static_cast<float>(codec_settings_.maxFramerate); |
| 279 openh264_encoder_->SetOption(ENCODER_OPTION_FRAME_RATE, |
| 280 &max_framerate); |
| 281 return WEBRTC_VIDEO_CODEC_OK; |
| 282 } |
| 283 |
| 284 int32_t H264EncoderImpl::Encode( |
| 285 const VideoFrame& frame, const CodecSpecificInfo* codec_specific_info, |
| 286 const std::vector<FrameType>* frame_types) { |
| 287 if (!IsInitialized()) |
| 288 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 289 if (frame.IsZeroSize()) |
| 290 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 291 if (!encoded_image_callback_) { |
| 292 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 293 } |
| 294 if (frame.width() != codec_settings_.width || |
| 295 frame.height() != codec_settings_.height) { |
| 296 return WEBRTC_VIDEO_CODEC_ERR_SIZE; |
| 297 } |
| 298 |
| 299 bool force_key_frame = false; |
| 300 if (frame_types != nullptr) { |
| 301 // We only support a single stream. |
| 302 RTC_DCHECK_EQ(frame_types->size(), static_cast<size_t>(1)); |
| 303 // Force key frame? |
| 304 force_key_frame = (*frame_types)[0] == kVideoFrameKey; |
| 305 } |
| 306 if (force_key_frame) { |
| 307 // Only need to call ForceIntraFrame when true. API doc says |
| 308 // ForceIntraFrame(false) does nothing but really if you call it for every |
| 309 // frame it introduces massive delays and lag in the video stream. |
| 310 openh264_encoder_->ForceIntraFrame(true); |
| 311 } |
| 312 |
| 313 // EncodeFrame input. |
| 314 SSourcePicture picture; |
| 315 memset(&picture, 0, sizeof(SSourcePicture)); |
| 316 picture.iPicWidth = frame.width(); |
| 317 picture.iPicHeight = frame.height(); |
| 318 picture.iColorFormat = EVideoFormatType::videoFormatI420; |
| 319 picture.uiTimeStamp = frame.ntp_time_ms(); |
| 320 picture.iStride[0] = frame.stride(kYPlane); |
| 321 picture.iStride[1] = frame.stride(kUPlane); |
| 322 picture.iStride[2] = frame.stride(kVPlane); |
| 323 picture.pData[0] = const_cast<uint8_t*>(frame.buffer(kYPlane)); |
| 324 picture.pData[1] = const_cast<uint8_t*>(frame.buffer(kUPlane)); |
| 325 picture.pData[2] = const_cast<uint8_t*>(frame.buffer(kVPlane)); |
| 326 |
| 327 // EncodeFrame output. |
| 328 SFrameBSInfo info; |
| 329 memset(&info, 0, sizeof(SFrameBSInfo)); |
| 330 |
| 331 // Encode! |
| 332 int enc_ret = openh264_encoder_->EncodeFrame(&picture, &info); |
| 333 if (enc_ret != 0) { |
| 334 return WEBRTC_VIDEO_CODEC_ERROR; |
| 335 } |
| 336 |
| 337 encoded_image_._encodedWidth = frame.width(); |
| 338 encoded_image_._encodedHeight = frame.height(); |
| 339 encoded_image_._timeStamp = frame.timestamp(); |
| 340 encoded_image_.ntp_time_ms_ = frame.ntp_time_ms(); |
| 341 encoded_image_.capture_time_ms_ = frame.render_time_ms(); |
| 342 encoded_image_._frameType = EVideoFrameType_to_FrameType( |
| 343 info.eFrameType); |
| 344 |
| 345 // Split encoded image up into fragments. This also updates |encoded_image_|. |
| 346 RTPFragmentationHeader frag_header; |
| 347 RtpFragmentize(&encoded_image_, &encoded_image_buffer_, |
| 348 frame, &info, &frag_header); |
| 349 |
| 350 // Encoder can skip frames to save bandwidth in which case |
| 351 // |encoded_image_._length| == 0. |
| 352 if (encoded_image_._length > 0) { |
| 353 // Deliver encoded image. |
| 354 encoded_image_callback_->Encoded(encoded_image_, codec_specific_info, |
| 355 &frag_header); |
| 356 } |
| 357 return WEBRTC_VIDEO_CODEC_OK; |
| 358 } |
| 359 |
| 360 bool H264EncoderImpl::IsInitialized() { |
| 361 return openh264_encoder_ != nullptr; |
| 362 } |
| 363 |
| 364 int32_t H264EncoderImpl::SetChannelParameters( |
| 365 uint32_t packet_loss, int64_t rtt) { |
| 366 return WEBRTC_VIDEO_CODEC_OK; |
| 367 } |
| 368 |
| 369 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { |
| 370 return WEBRTC_VIDEO_CODEC_OK; |
| 371 } |
| 372 |
| 373 void H264EncoderImpl::OnDroppedFrame() { |
| 374 } |
| 375 |
| 376 } // namespace openh264 |
OLD | NEW |