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

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_encoder.mm

Issue 2484493002: Prepare iOS H264 HW encoder for High Profile (Closed)
Patch Set: Rebase Created 4 years, 1 month 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
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 * 9 *
10 */ 10 */
11 11
12 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_encoder.h" 12 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_encoder.h"
13 13
14 #include <memory> 14 #include <memory>
15 #include <string> 15 #include <string>
16 #include <vector> 16 #include <vector>
17 17
18 #if defined(WEBRTC_IOS) 18 #if defined(WEBRTC_IOS)
19 #import "WebRTC/UIDevice+RTCDevice.h" 19 #import "WebRTC/UIDevice+RTCDevice.h"
20 #include "RTCUIApplication.h" 20 #include "RTCUIApplication.h"
21 #endif 21 #endif
22 #include "libyuv/convert_from.h" 22 #include "libyuv/convert_from.h"
23 #include "webrtc/base/checks.h" 23 #include "webrtc/base/checks.h"
24 #include "webrtc/base/logging.h" 24 #include "webrtc/base/logging.h"
25 #include "webrtc/common_video/h264/profile_level_id.h"
25 #include "webrtc/common_video/include/corevideo_frame_buffer.h" 26 #include "webrtc/common_video/include/corevideo_frame_buffer.h"
26 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h" 27 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h"
27 #include "webrtc/system_wrappers/include/clock.h" 28 #include "webrtc/system_wrappers/include/clock.h"
28 29
29 namespace internal { 30 namespace internal {
30 31
31 // The ratio between kVTCompressionPropertyKey_DataRateLimits and 32 // The ratio between kVTCompressionPropertyKey_DataRateLimits and
32 // kVTCompressionPropertyKey_AverageBitRate. The data rate limit is set higher 33 // kVTCompressionPropertyKey_AverageBitRate. The data rate limit is set higher
33 // than the average bit rate to avoid undershooting the target. 34 // than the average bit rate to avoid undershooting the target.
34 const float kLimitToAverageBitRateFactor = 1.5f; 35 const float kLimitToAverageBitRateFactor = 1.5f;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 CMSampleBufferRef sample_buffer) { 218 CMSampleBufferRef sample_buffer) {
218 std::unique_ptr<FrameEncodeParams> encode_params( 219 std::unique_ptr<FrameEncodeParams> encode_params(
219 reinterpret_cast<FrameEncodeParams*>(params)); 220 reinterpret_cast<FrameEncodeParams*>(params));
220 encode_params->encoder->OnEncodedFrame( 221 encode_params->encoder->OnEncodedFrame(
221 status, info_flags, sample_buffer, encode_params->codec_specific_info, 222 status, info_flags, sample_buffer, encode_params->codec_specific_info,
222 encode_params->width, encode_params->height, 223 encode_params->width, encode_params->height,
223 encode_params->render_time_ms, encode_params->timestamp, 224 encode_params->render_time_ms, encode_params->timestamp,
224 encode_params->rotation); 225 encode_params->rotation);
225 } 226 }
226 227
228 // Extract VideoToolbox profile out of the cricket::VideoCodec. If there is no
229 // specific VideoToolbox profile for the specified level, AutoLevel will be
230 // returned. The user must initialize the encoder with a resolution and
231 // framerate conforming to the selected H264 level regardless.
232 CFStringRef ExtractProfile(const cricket::VideoCodec& codec) {
233 const rtc::Optional<webrtc::H264::ProfileLevelId> profile_level_id =
234 webrtc::H264::ParseSdpProfileLevelId(codec.params);
235 RTC_DCHECK(profile_level_id);
236 switch (profile_level_id->profile) {
237 case webrtc::H264::kProfileConstrainedBaseline:
238 case webrtc::H264::kProfileBaseline:
239 switch (profile_level_id->level) {
240 case webrtc::H264::kLevel4:
241 return kVTProfileLevel_H264_Baseline_4_0;
242 case webrtc::H264::kLevel4_2:
243 return kVTProfileLevel_H264_Baseline_4_2;
244 case webrtc::H264::kLevel5:
245 return kVTProfileLevel_H264_Baseline_5_0;
246 case webrtc::H264::kLevel5_1:
247 return kVTProfileLevel_H264_Baseline_5_1;
248 case webrtc::H264::kLevel5_2:
249 return kVTProfileLevel_H264_Baseline_5_2;
250 default:
tkchin_webrtc 2016/11/14 18:27:27 usually in iOS code we use compiler error to catch
magjed_webrtc 2016/11/15 17:14:24 Cool, didn't know the compiler would help finding
251 return kVTProfileLevel_H264_Baseline_AutoLevel;
252 }
253
254 case webrtc::H264::kProfileMain:
255 switch (profile_level_id->level) {
256 case webrtc::H264::kLevel4_2:
257 return kVTProfileLevel_H264_Main_4_2;
258 case webrtc::H264::kLevel5_1:
259 return kVTProfileLevel_H264_Main_5_1;
260 case webrtc::H264::kLevel5_2:
261 return kVTProfileLevel_H264_Main_5_2;
262 default:
263 return kVTProfileLevel_H264_Main_AutoLevel;
264 }
265
266 case webrtc::H264::kProfileConstrainedHigh:
267 case webrtc::H264::kProfileHigh:
268 switch (profile_level_id->level) {
269 case webrtc::H264::kLevel3:
270 return kVTProfileLevel_H264_High_3_0;
271 case webrtc::H264::kLevel3_1:
272 return kVTProfileLevel_H264_High_3_1;
273 case webrtc::H264::kLevel3_2:
274 return kVTProfileLevel_H264_High_3_2;
275 case webrtc::H264::kLevel4:
276 return kVTProfileLevel_H264_High_4_0;
277 case webrtc::H264::kLevel4_1:
278 return kVTProfileLevel_H264_High_4_1;
279 case webrtc::H264::kLevel4_2:
280 return kVTProfileLevel_H264_High_4_2;
281 case webrtc::H264::kLevel5_1:
282 return kVTProfileLevel_H264_High_5_1;
283 case webrtc::H264::kLevel5_2:
284 return kVTProfileLevel_H264_High_5_2;
285 default:
286 return kVTProfileLevel_H264_High_AutoLevel;
287 }
288 }
289 }
290
227 } // namespace internal 291 } // namespace internal
228 292
229 namespace webrtc { 293 namespace webrtc {
230 294
231 // .5 is set as a mininum to prevent overcompensating for large temporary 295 // .5 is set as a mininum to prevent overcompensating for large temporary
232 // overshoots. We don't want to degrade video quality too badly. 296 // overshoots. We don't want to degrade video quality too badly.
233 // .95 is set to prevent oscillations. When a lower bitrate is set on the 297 // .95 is set to prevent oscillations. When a lower bitrate is set on the
234 // encoder than previously set, its output seems to have a brief period of 298 // encoder than previously set, its output seems to have a brief period of
235 // drastically reduced bitrate, so we want to avoid that. In steady state 299 // drastically reduced bitrate, so we want to avoid that. In steady state
236 // conditions, 0.95 seems to give us better overall bitrate over long periods 300 // conditions, 0.95 seems to give us better overall bitrate over long periods
237 // of time. 301 // of time.
238 H264VideoToolboxEncoder::H264VideoToolboxEncoder() 302 H264VideoToolboxEncoder::H264VideoToolboxEncoder(
303 const cricket::VideoCodec& codec)
239 : callback_(nullptr), 304 : callback_(nullptr),
240 compression_session_(nullptr), 305 compression_session_(nullptr),
241 bitrate_adjuster_(Clock::GetRealTimeClock(), .5, .95) { 306 bitrate_adjuster_(Clock::GetRealTimeClock(), .5, .95),
242 } 307 profile_(internal::ExtractProfile(codec)) {}
243 308
244 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() { 309 H264VideoToolboxEncoder::~H264VideoToolboxEncoder() {
245 DestroyCompressionSession(); 310 DestroyCompressionSession();
246 } 311 }
247 312
248 int H264VideoToolboxEncoder::InitEncode(const VideoCodec* codec_settings, 313 int H264VideoToolboxEncoder::InitEncode(const VideoCodec* codec_settings,
249 int number_of_cores, 314 int number_of_cores,
250 size_t max_payload_size) { 315 size_t max_payload_size) {
251 RTC_DCHECK(codec_settings); 316 RTC_DCHECK(codec_settings);
252 RTC_DCHECK_EQ(codec_settings->codecType, kVideoCodecH264); 317 RTC_DCHECK_EQ(codec_settings->codecType, kVideoCodecH264);
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 ConfigureCompressionSession(); 554 ConfigureCompressionSession();
490 return WEBRTC_VIDEO_CODEC_OK; 555 return WEBRTC_VIDEO_CODEC_OK;
491 } 556 }
492 557
493 void H264VideoToolboxEncoder::ConfigureCompressionSession() { 558 void H264VideoToolboxEncoder::ConfigureCompressionSession() {
494 RTC_DCHECK(compression_session_); 559 RTC_DCHECK(compression_session_);
495 internal::SetVTSessionProperty(compression_session_, 560 internal::SetVTSessionProperty(compression_session_,
496 kVTCompressionPropertyKey_RealTime, true); 561 kVTCompressionPropertyKey_RealTime, true);
497 internal::SetVTSessionProperty(compression_session_, 562 internal::SetVTSessionProperty(compression_session_,
498 kVTCompressionPropertyKey_ProfileLevel, 563 kVTCompressionPropertyKey_ProfileLevel,
499 kVTProfileLevel_H264_Baseline_AutoLevel); 564 profile_);
500 internal::SetVTSessionProperty(compression_session_, 565 internal::SetVTSessionProperty(compression_session_,
501 kVTCompressionPropertyKey_AllowFrameReordering, 566 kVTCompressionPropertyKey_AllowFrameReordering,
502 false); 567 false);
503 SetEncoderBitrateBps(target_bitrate_bps_); 568 SetEncoderBitrateBps(target_bitrate_bps_);
504 // TODO(tkchin): Look at entropy mode and colorspace matrices. 569 // TODO(tkchin): Look at entropy mode and colorspace matrices.
505 // TODO(tkchin): Investigate to see if there's any way to make this work. 570 // TODO(tkchin): Investigate to see if there's any way to make this work.
506 // May need it to interop with Android. Currently this call just fails. 571 // May need it to interop with Android. Currently this call just fails.
507 // On inspecting encoder output on iOS8, this value is set to 6. 572 // On inspecting encoder output on iOS8, this value is set to 6.
508 // internal::SetVTSessionProperty(compression_session_, 573 // internal::SetVTSessionProperty(compression_session_,
509 // kVTCompressionPropertyKey_MaxFrameDelayCount, 574 // kVTCompressionPropertyKey_MaxFrameDelayCount,
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 EncodedImageCallback::Result result = 716 EncodedImageCallback::Result result =
652 callback_->OnEncodedImage(frame, &codec_specific_info, header.get()); 717 callback_->OnEncodedImage(frame, &codec_specific_info, header.get());
653 if (result.error != EncodedImageCallback::Result::OK) { 718 if (result.error != EncodedImageCallback::Result::OK) {
654 LOG(LS_ERROR) << "Encode callback failed: " << result.error; 719 LOG(LS_ERROR) << "Encode callback failed: " << result.error;
655 return; 720 return;
656 } 721 }
657 bitrate_adjuster_.Update(frame._size); 722 bitrate_adjuster_.Update(frame._size);
658 } 723 }
659 724
660 } // namespace webrtc 725 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698