From 38d11b852971d98510beccf03ecf57f0b4fda0e2 Mon Sep 17 00:00:00 2001 From: "marpan@webrtc.org" Date: Mon, 26 Jan 2015 15:21:36 +0000 Subject: [PATCH] Enable encoder multi-threading for VP9. R=stefan@webrtc.org TBR=stefan@webrtc.org BUG= Review URL: https://webrtc-codereview.appspot.com/41489004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@8150 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../video_coding/codecs/vp9/vp9_impl.cc | 25 +++++++++++++++++++ .../video_coding/codecs/vp9/vp9_impl.h | 3 +++ 2 files changed, 28 insertions(+) diff --git a/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc b/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc index 5491bd08ab..e1e83d07bd 100644 --- a/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc +++ b/webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc @@ -181,9 +181,29 @@ int VP9EncoderImpl::InitEncode(const VideoCodec* inst, } else { config_->kf_mode = VPX_KF_DISABLED; } + + // Determine number of threads based on the image size and #cores. + config_->g_threads = NumberOfThreads(config_->g_w, + config_->g_h, + number_of_cores); return InitAndSetControlSettings(inst); } +int VP9EncoderImpl::NumberOfThreads(int width, + int height, + int number_of_cores) { + // Keep the number of encoder threads equal to the possible number of column + // tiles, which is (1, 2, 4, 8). See comments below for VP9E_SET_TILE_COLUMNS. + if (width * height >= 1280 * 720 && number_of_cores > 4) { + return 4; + } else if (width * height >= 640 * 480 && number_of_cores > 2) { + return 2; + } else { + // 1 thread less than VGA. + return 1; + } +} + int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) { return WEBRTC_VIDEO_CODEC_UNINITIALIZED; @@ -198,6 +218,11 @@ int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) { rc_max_intra_target_); vpx_codec_control(encoder_, VP9E_SET_AQ_MODE, inst->codecSpecific.VP9.adaptiveQpMode ? 3 : 0); + // Control function to set the number of column tiles in encoding a frame, in + // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns. + // The number tile columns will be capped by the encoder based on image size + // (minimum width of tile column is 256 pixels, maximum is 4096). + vpx_codec_control(encoder_, VP9E_SET_TILE_COLUMNS, (config_->g_threads >> 1)); // TODO(marpan): Enable in future libvpx roll: waiting for SSE2 optimization. // #if !defined(WEBRTC_ARCH_ARM) // vpx_codec_control(encoder_, VP9E_SET_NOISE_SENSITIVITY, diff --git a/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h b/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h index 6c9f1ab564..9d46d0d7bd 100644 --- a/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h +++ b/webrtc/modules/video_coding/codecs/vp9/vp9_impl.h @@ -43,6 +43,9 @@ class VP9EncoderImpl : public VP9Encoder { virtual int SetRates(uint32_t new_bitrate_kbit, uint32_t frame_rate) OVERRIDE; private: + // Determine number of encoder threads to use. + int NumberOfThreads(int width, int height, int number_of_cores); + // Call encoder initialize function and set control settings. int InitAndSetControlSettings(const VideoCodec* inst);