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 #include "webrtc/modules/video_coding/codecs/h264/h264_encoder_impl.h" |
| 13 |
| 14 #include <iostream> |
| 15 |
| 16 // OpenH264 |
| 17 #include "codec_api.h" |
| 18 #include "codec_app_def.h" |
| 19 #include "codec_def.h" |
| 20 |
| 21 #include "webrtc/base/checks.h" |
| 22 #include "webrtc/base/logging.h" |
| 23 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 24 |
| 25 namespace webrtc { |
| 26 |
| 27 namespace { |
| 28 const bool OPENH264_ENCODER_LOGGING = false; |
| 29 } // anonymous namespace |
| 30 |
| 31 static VideoFrameType EVideoFrameType_to_VideoFrameType( |
| 32 const EVideoFrameType& type) { |
| 33 switch (type) { |
| 34 case videoFrameTypeInvalid: |
| 35 case videoFrameTypeSkip: |
| 36 return kSkipFrame; |
| 37 case videoFrameTypeIDR: |
| 38 case videoFrameTypeI: |
| 39 case videoFrameTypeP: |
| 40 case videoFrameTypeIPMixed: |
| 41 return kDeltaFrame; |
| 42 default: |
| 43 LOG(LS_WARNING) << "Unknown EVideoFrameType: " << type; |
| 44 return kDeltaFrame; |
| 45 } |
| 46 } |
| 47 |
| 48 // Copies the encoded bytes from |info| to |encoded_image| and updates the |
| 49 // fragmentation information of |frag_header|. |
| 50 // After OpenH264 encoding, the encoded bytes are stored in |info| spread out |
| 51 // over a number of layers and "NAL units". Each NAL unit is a fragment starting |
| 52 // with the four-byte NAL header {0,0,0,1}. To save bytes, the NAL headers are |
| 53 // excluded when copying to |encoded_image->_buffer|. However these headers must |
| 54 // be included for an H264Decoder to be able to decode the data. |
| 55 // When fragments are sent over a network, the receiving end must re-insert |
| 56 // the NAL headers before each fragment. |
| 57 // For image data encoded and decoded locally, RTPDefragmentize can be used |
| 58 // to convert (EncodedImage, RTPFragmentationHeader) with NAL headers excluded |
| 59 // to a decodable EncodedImage buffer with NAL headers included. |
| 60 static void RTPFragmentize(EncodedImage* encoded_image, |
| 61 const VideoFrame& frame, |
| 62 SFrameBSInfo* info, |
| 63 RTPFragmentationHeader* frag_header) { |
| 64 encoded_image->_length = 0; |
| 65 std::vector<int> frags; |
| 66 |
| 67 int aLen = 0, iLayerLen = 0; |
| 68 for (int iLayer = 0; iLayer < info->iLayerNum; ++iLayer) { |
| 69 iLayerLen = 0; |
| 70 const SLayerBSInfo& layerInfo = info->sLayerInfo[iLayer]; |
| 71 // Copy the layer data to |encoded_image_._buffer|, excluding the 4-byte |
| 72 // NAL headers. |
| 73 for (int iNal = 0; iNal < layerInfo.iNalCount; ++iNal) { |
| 74 memcpy(encoded_image->_buffer + encoded_image->_length, |
| 75 layerInfo.pBsBuf + iLayerLen + 4, |
| 76 (layerInfo.pNalLengthInByte[iNal] - 4) * sizeof(unsigned char)); |
| 77 encoded_image->_length += (layerInfo.pNalLengthInByte[iNal] - 4); |
| 78 frags.push_back(layerInfo.pNalLengthInByte[iNal] - 4); |
| 79 |
| 80 iLayerLen += layerInfo.pNalLengthInByte[iNal]; |
| 81 } |
| 82 aLen += iLayerLen; |
| 83 } |
| 84 |
| 85 frag_header->VerifyAndAllocateFragmentationHeader(frags.size()); |
| 86 for (size_t i = 0, off = 0; i < frags.size(); ++i) { |
| 87 frag_header->fragmentationOffset[i] = off; |
| 88 frag_header->fragmentationLength[i] = frags[i]; |
| 89 off += frags[i]; |
| 90 } |
| 91 } |
| 92 |
| 93 void H264EncoderImpl::RTPDefragmentize( |
| 94 const EncodedImage& encoded_image, |
| 95 const RTPFragmentationHeader* frag_header, |
| 96 uint8_t* enc_buffer_with_nal, size_t enc_buffer_with_nal_length) { |
| 97 DCHECK_GE(enc_buffer_with_nal_length, |
| 98 RTPDefragmentizeBufferLengthWithNAL(encoded_image, frag_header)); |
| 99 const unsigned char nal_header[] = { 0, 0, 0, 1 }; |
| 100 for (size_t i = 0; i < frag_header->fragmentationVectorSize; ++i) { |
| 101 DCHECK_LE(frag_header->fragmentationOffset[i] + |
| 102 frag_header->fragmentationLength[i], |
| 103 encoded_image._length); |
| 104 |
| 105 // Insert a NAL header. |
| 106 memcpy(enc_buffer_with_nal, nal_header, 4); |
| 107 // Copy fragment data. |
| 108 memcpy(enc_buffer_with_nal + 4, |
| 109 encoded_image._buffer + frag_header->fragmentationOffset[i], |
| 110 frag_header->fragmentationLength[i]); |
| 111 |
| 112 enc_buffer_with_nal += (4 + frag_header->fragmentationLength[i]); |
| 113 } |
| 114 } |
| 115 |
| 116 size_t H264EncoderImpl::RTPDefragmentizeBufferLengthWithNAL( |
| 117 const EncodedImage& encoded_image, |
| 118 const webrtc::RTPFragmentationHeader* frag_header) { |
| 119 return encoded_image._length + |
| 120 4 * static_cast<size_t>(frag_header->fragmentationVectorSize); |
| 121 } |
| 122 |
| 123 H264EncoderImpl::H264EncoderImpl() |
| 124 : openh264_encoder_(nullptr), |
| 125 encoded_image_callback_(nullptr) { |
| 126 } |
| 127 |
| 128 H264EncoderImpl::~H264EncoderImpl() { |
| 129 Release(); |
| 130 } |
| 131 |
| 132 int32_t H264EncoderImpl::InitEncode(const VideoCodec* codec_settings, |
| 133 int32_t /*number_of_cores*/, |
| 134 size_t /*max_payload_size*/) { |
| 135 if (!codec_settings || |
| 136 codec_settings->codecType != VideoCodecType::kVideoCodecH264) { |
| 137 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 138 } |
| 139 if (codec_settings->maxFramerate == 0) |
| 140 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 141 if (codec_settings->width < 1 || codec_settings->height < 1) |
| 142 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 143 |
| 144 int release_ret = Release(); |
| 145 if (release_ret != WEBRTC_VIDEO_CODEC_OK) |
| 146 return release_ret; |
| 147 DCHECK(!openh264_encoder_); |
| 148 |
| 149 // Create encoder. |
| 150 if (WelsCreateSVCEncoder(&openh264_encoder_) != 0) { |
| 151 // Failed to create encoder. |
| 152 LOG(LS_ERROR) << "Failed to create OpenH264 encoder"; |
| 153 DCHECK(!openh264_encoder_); |
| 154 return WEBRTC_VIDEO_CODEC_ERROR; |
| 155 } |
| 156 DCHECK(openh264_encoder_); |
| 157 if (&codec_settings_ != codec_settings) |
| 158 codec_settings_ = *codec_settings; |
| 159 |
| 160 if (codec_settings_.targetBitrate == 0) |
| 161 codec_settings_.targetBitrate = codec_settings_.startBitrate; |
| 162 |
| 163 // Note: H264 codec specifics are ignored: |
| 164 // - codec_settings->codecSpecific.H264.frameDroppingOn |
| 165 // - codec_settings->codecSpecific.H264.keyFrameInterval |
| 166 |
| 167 // Initialization parameters. |
| 168 // There are two ways to initialize. There is SEncParamBase (cleared with |
| 169 // memset(&p, 0, sizeof(SEncParamBase)) used in Initialize, and SEncParamExt |
| 170 // which is a superset of SEncParamBase (cleared with GetDefaultParams) used |
| 171 // in InitializeExt. We use SEncParamBase/Initialize. |
| 172 SEncParamBase init_params; |
| 173 memset(&init_params, 0, sizeof(SEncParamBase)); |
| 174 if (codec_settings_.mode == kRealtimeVideo) { |
| 175 init_params.iUsageType = CAMERA_VIDEO_REAL_TIME; |
| 176 } else if (codec_settings_.mode == kScreensharing) { |
| 177 init_params.iUsageType = SCREEN_CONTENT_REAL_TIME; |
| 178 } else { |
| 179 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 180 } |
| 181 init_params.iPicWidth = codec_settings_.width; |
| 182 init_params.iPicHeight = codec_settings_.height; |
| 183 // iTargetBitrate is in bit/s, targetBitrate is in kbit/s. |
| 184 init_params.iTargetBitrate = codec_settings_.targetBitrate * 1000; |
| 185 // Rate Control mode |
| 186 init_params.iRCMode = RC_QUALITY_MODE; |
| 187 init_params.fMaxFrameRate = static_cast<float>(codec_settings_.maxFramerate); |
| 188 |
| 189 // Initialize. |
| 190 if (openh264_encoder_->Initialize(&init_params) != 0) { |
| 191 // Failed to initialize. |
| 192 LOG(LS_ERROR) << "Failed to initialize OpenH264 encoder"; |
| 193 Release(); |
| 194 return WEBRTC_VIDEO_CODEC_ERROR; |
| 195 } |
| 196 |
| 197 // Initialize encoded image. |
| 198 // (Safe?) assumption: encoded data is always less than unencoded data. |
| 199 // (Unsafe?) assumption: don't need to realloc buffer if frame size differs. |
| 200 encoded_image_._size = CalcBufferSize( |
| 201 VideoType::kI420, codec_settings_.width, codec_settings_.height); |
| 202 encoded_image_._buffer = new uint8_t[encoded_image_._size]; |
| 203 encoded_image_._completeFrame = true; |
| 204 encoded_image_._encodedWidth = 0; |
| 205 encoded_image_._encodedHeight = 0; |
| 206 encoded_image_._length = 0; |
| 207 return WEBRTC_VIDEO_CODEC_OK; |
| 208 } |
| 209 |
| 210 int32_t H264EncoderImpl::Release() { |
| 211 if (openh264_encoder_) { |
| 212 int uninit_ret = openh264_encoder_->Uninitialize(); |
| 213 if (uninit_ret != 0) { |
| 214 LOG(LS_WARNING) << "OpenH264 encoder's Uninitialize() returned " |
| 215 << "unsuccessful: " << uninit_ret; |
| 216 } |
| 217 WelsDestroySVCEncoder(openh264_encoder_); |
| 218 openh264_encoder_ = nullptr; |
| 219 } |
| 220 if (encoded_image_._buffer != nullptr) { |
| 221 delete[] encoded_image_._buffer; |
| 222 encoded_image_._buffer = nullptr; |
| 223 } |
| 224 return WEBRTC_VIDEO_CODEC_OK; |
| 225 } |
| 226 |
| 227 int32_t H264EncoderImpl::RegisterEncodeCompleteCallback( |
| 228 EncodedImageCallback* callback) { |
| 229 encoded_image_callback_ = callback; |
| 230 return WEBRTC_VIDEO_CODEC_OK; |
| 231 } |
| 232 |
| 233 int32_t H264EncoderImpl::SetRates(uint32_t bitrate, uint32_t framerate) { |
| 234 if (bitrate <= 0 || framerate <= 0) { |
| 235 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 236 } |
| 237 codec_settings_.targetBitrate = bitrate; |
| 238 codec_settings_.maxFramerate = framerate; |
| 239 return WEBRTC_VIDEO_CODEC_OK; |
| 240 } |
| 241 |
| 242 int32_t H264EncoderImpl::Encode( |
| 243 const VideoFrame& frame, const CodecSpecificInfo* codec_specific_info, |
| 244 const std::vector<VideoFrameType>* frame_types) { |
| 245 if (!IsInitialized()) |
| 246 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 247 if (frame.IsZeroSize()) |
| 248 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 249 if (!encoded_image_callback_) { |
| 250 LOG(LS_WARNING) << "InitEncode() has been called, but a callback function " |
| 251 << "has not been set with RegisterEncodeCompleteCallback()"; |
| 252 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 253 } |
| 254 |
| 255 // Make |codec_settings_|'s size reflect the latest frame's size. |
| 256 // (Not necessary for encoding to work.) |
| 257 if (codec_settings_.width != frame.width() || |
| 258 codec_settings_.height != frame.height()) { |
| 259 codec_settings_.width = frame.width(); |
| 260 codec_settings_.height = frame.height(); |
| 261 } |
| 262 |
| 263 // Set encoder options. |
| 264 int video_format = EVideoFormatType::videoFormatI420; |
| 265 openh264_encoder_->SetOption(ENCODER_OPTION_DATAFORMAT, |
| 266 &video_format); |
| 267 SBitrateInfo target_bitrate; |
| 268 memset(&target_bitrate, 0, sizeof(SBitrateInfo)); |
| 269 target_bitrate.iLayer = SPATIAL_LAYER_ALL, |
| 270 target_bitrate.iBitrate = codec_settings_.targetBitrate * 1000; |
| 271 openh264_encoder_->SetOption(ENCODER_OPTION_BITRATE, |
| 272 &target_bitrate); |
| 273 float max_framerate = static_cast<float>(codec_settings_.maxFramerate); |
| 274 openh264_encoder_->SetOption(ENCODER_OPTION_FRAME_RATE, |
| 275 &max_framerate); |
| 276 |
| 277 if (OPENH264_ENCODER_LOGGING) { |
| 278 int trace_level = WELS_LOG_DETAIL; |
| 279 openh264_encoder_->SetOption(ENCODER_OPTION_TRACE_LEVEL, |
| 280 &trace_level); |
| 281 } |
| 282 |
| 283 // EncodeFrame input. |
| 284 SSourcePicture picture; |
| 285 memset(&picture, 0, sizeof(SSourcePicture)); |
| 286 picture.iPicWidth = frame.width(); |
| 287 picture.iPicHeight = frame.height(); |
| 288 picture.iColorFormat = video_format; |
| 289 picture.uiTimeStamp = frame.timestamp(); |
| 290 picture.iStride[0] = frame.stride(kYPlane); |
| 291 picture.iStride[1] = frame.stride(kUPlane); |
| 292 picture.iStride[2] = frame.stride(kVPlane); |
| 293 picture.pData[0] = const_cast<uint8_t*>(frame.buffer(kYPlane)); |
| 294 picture.pData[1] = const_cast<uint8_t*>(frame.buffer(kUPlane)); |
| 295 picture.pData[2] = const_cast<uint8_t*>(frame.buffer(kVPlane)); |
| 296 |
| 297 // EncodeFrame output. |
| 298 SFrameBSInfo info; |
| 299 memset(&info, 0, sizeof(SFrameBSInfo)); |
| 300 |
| 301 // Encode! |
| 302 if (openh264_encoder_->EncodeFrame(&picture, &info) != 0) { |
| 303 LOG(LS_ERROR) << "OpenH264 frame encoding failed (EncodeFrame)"; |
| 304 return WEBRTC_VIDEO_CODEC_ERROR; |
| 305 } |
| 306 |
| 307 encoded_image_._encodedWidth = frame.width(); |
| 308 encoded_image_._encodedHeight = frame.height(); |
| 309 encoded_image_._timeStamp = frame.timestamp(); |
| 310 encoded_image_.capture_time_ms_ = frame.render_time_ms(); |
| 311 encoded_image_._frameType = EVideoFrameType_to_VideoFrameType( |
| 312 info.eFrameType); |
| 313 |
| 314 // Split encoded image up into fragments. This also updates |encoded_image_|. |
| 315 RTPFragmentationHeader frag_header; |
| 316 RTPFragmentize(&encoded_image_, frame, &info, &frag_header); |
| 317 |
| 318 // Encoder can skip frames to save bandwidth in which case |
| 319 // |encoded_image_._length| == 0. |
| 320 if (encoded_image_._length > 0) { |
| 321 // Deliver encoded image. |
| 322 encoded_image_callback_->Encoded(encoded_image_, codec_specific_info, |
| 323 &frag_header); |
| 324 } |
| 325 return WEBRTC_VIDEO_CODEC_OK; |
| 326 } |
| 327 |
| 328 bool H264EncoderImpl::IsInitialized() { |
| 329 return openh264_encoder_ != nullptr; |
| 330 } |
| 331 |
| 332 int32_t H264EncoderImpl::SetChannelParameters( |
| 333 uint32_t packet_loss, int64_t rtt) { |
| 334 return WEBRTC_VIDEO_CODEC_OK; |
| 335 } |
| 336 |
| 337 int32_t H264EncoderImpl::SetPeriodicKeyFrames(bool enable) { |
| 338 return WEBRTC_VIDEO_CODEC_OK; |
| 339 } |
| 340 |
| 341 int32_t H264EncoderImpl::CodecConfigParameters(uint8_t* buffer, int32_t size) { |
| 342 return WEBRTC_VIDEO_CODEC_OK; |
| 343 } |
| 344 |
| 345 void H264EncoderImpl::OnDroppedFrame() { |
| 346 } |
| 347 |
| 348 } // namespace webrtc |
OLD | NEW |