Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 2cc5ed2

Browse files
daijhDuan, Xiande
authored andcommitted
Add interface to update encoder size
Signed-off-by: jdai12 <[email protected]> Change-Id: Iab60c9c66c52ee7ac27d29a1f24370f5228a22b2 Reviewed-on: https://git-ccr-1.devtools.intel.com/gerrit/61891 Reviewed-by: Duan, Xiande <[email protected]> Tested-by: Duan, Xiande <[email protected]>
1 parent 6f2760e commit 2cc5ed2

File tree

7 files changed

+66
-0
lines changed

7 files changed

+66
-0
lines changed

webrtc/api/video_codecs/video_encoder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ class VideoEncoder {
186186
virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
187187
virtual bool SupportsNativeHandle() const { return false; }
188188
virtual const char* ImplementationName() const { return "unknown"; }
189+
190+
// Update for dynamic resolution encoding, by Jianhui
191+
virtual int32_t SetResolution(uint32_t width, uint32_t height) {
192+
return -1;
193+
}
189194
};
190195

191196
} // namespace webrtc

webrtc/modules/video_coding/codecs/h264/h264_encoder_impl.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,4 +500,26 @@ VideoEncoder::ScalingSettings H264EncoderImpl::GetScalingSettings() const {
500500
return VideoEncoder::ScalingSettings(true);
501501
}
502502

503+
int32_t H264EncoderImpl::SetResolution(uint32_t width, uint32_t height)
504+
{
505+
width_ = width;
506+
height_ = height;
507+
508+
SEncParamExt encoder_params = CreateEncoderParams();
509+
if (openh264_encoder_->SetOption(ENCODER_OPTION_SVC_ENCODE_PARAM_EXT, &encoder_params) != 0) {
510+
return WEBRTC_VIDEO_CODEC_ERROR;
511+
}
512+
513+
// Initialize encoded image. Default buffer size: size of unencoded data.
514+
encoded_image_._size =
515+
CalcBufferSize(kI420, width_, height_);
516+
encoded_image_._buffer = new uint8_t[encoded_image_._size];
517+
encoded_image_buffer_.reset(encoded_image_._buffer);
518+
encoded_image_._completeFrame = true;
519+
encoded_image_._encodedWidth = 0;
520+
encoded_image_._encodedHeight = 0;
521+
encoded_image_._length = 0;
522+
return WEBRTC_VIDEO_CODEC_OK;
523+
}
524+
503525
} // namespace webrtc

webrtc/modules/video_coding/codecs/h264/h264_encoder_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class H264EncoderImpl : public H264Encoder {
6666
return packetization_mode_;
6767
}
6868

69+
int32_t SetResolution(uint32_t width, uint32_t height) override;
70+
6971
private:
7072
bool IsInitialized() const;
7173
SEncParamExt CreateEncoderParams() const;

webrtc/modules/video_coding/codecs/vp8/vp8_impl.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,11 @@ int VP8EncoderImpl::RegisterEncodeCompleteCallback(
925925
return WEBRTC_VIDEO_CODEC_OK;
926926
}
927927

928+
int32_t VP8EncoderImpl::SetResolution(uint32_t width, uint32_t height)
929+
{
930+
return UpdateCodecFrameSize(width, height);
931+
}
932+
928933
VP8DecoderImpl::VP8DecoderImpl()
929934
: use_postproc_arm_(webrtc::field_trial::FindFullName(
930935
kVp8PostProcArmFieldTrial) == "Enabled"),

webrtc/modules/video_coding/codecs/vp8/vp8_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class VP8EncoderImpl : public VP8Encoder {
6161

6262
const char* ImplementationName() const override;
6363

64+
int32_t SetResolution(uint32_t width, uint32_t height) override;
65+
6466
private:
6567
void SetupTemporalLayers(int num_streams,
6668
int num_temporal_layers,

webrtc/modules/video_coding/codecs/vp9/vp9_impl.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,34 @@ const char* VP9EncoderImpl::ImplementationName() const {
833833
return "libvpx";
834834
}
835835

836+
int32_t VP9EncoderImpl::SetResolution(uint32_t width, uint32_t height)
837+
{
838+
codec_.width = width;
839+
codec_.height = height;
840+
if (codec_.numberOfSimulcastStreams <= 1) {
841+
// For now scaling is only used for single-layer streams.
842+
codec_.simulcastStream[0].width = width;
843+
codec_.simulcastStream[0].height = height;
844+
}
845+
// Update the cpu_speed setting for resolution change.
846+
vpx_codec_control(encoder_, VP8E_SET_CPUUSED,
847+
GetCpuSpeed(codec_.width, codec_.height));
848+
raw_->w = codec_.width;
849+
raw_->h = codec_.height;
850+
raw_->d_w = codec_.width;
851+
raw_->d_h = codec_.height;
852+
vpx_img_set_rect(raw_, 0, 0, codec_.width, codec_.height);
853+
854+
// Update encoder context for new frame size.
855+
// Change of frame size will automatically trigger a key frame.
856+
config_->g_w = codec_.width;
857+
config_->g_h = codec_.height;
858+
if (vpx_codec_enc_config_set(encoder_, config_)) {
859+
return WEBRTC_VIDEO_CODEC_ERROR;
860+
}
861+
return WEBRTC_VIDEO_CODEC_OK;
862+
}
863+
836864
bool VP9Decoder::IsSupported() {
837865
return true;
838866
}

webrtc/modules/video_coding/codecs/vp9/vp9_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class VP9EncoderImpl : public VP9Encoder {
6565
bool is_keyframe = false;
6666
};
6767

68+
int32_t SetResolution(uint32_t width, uint32_t height) override;
69+
6870
private:
6971
// Determine number of encoder threads to use.
7072
int NumberOfThreads(int width, int height, int number_of_cores);

0 commit comments

Comments
 (0)