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