Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: webrtc/modules/video_coding/codecs/h264/h264_encoder_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698