From 9e7644c20c431f46514dc1b7e33d822d7226fe69 Mon Sep 17 00:00:00 2001 From: "hlundin@google.com" Date: Wed, 8 Jun 2011 07:02:33 +0000 Subject: [PATCH] Change implementation of Reset function in VP8 wrapper The Reset function was modified so that the encoder is destroyed and recreated on reset. Initialization of the encoder and setting of the encoder speed is now done in a private method, to avoid code duplication. (It is used both in InitEncode and in Reset.) This change is needed to make the unit tests pass with newer versions of libvpx. Review URL: http://webrtc-codereview.appspot.com/33004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@56 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../codecs/vp8/main/interface/vp8.h | 4 ++ .../codecs/vp8/main/source/vp8.cc | 51 +++++++++++++------ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/modules/video_coding/codecs/vp8/main/interface/vp8.h b/modules/video_coding/codecs/vp8/main/interface/vp8.h index ec798b08ab..8768b41793 100644 --- a/modules/video_coding/codecs/vp8/main/interface/vp8.h +++ b/modules/video_coding/codecs/vp8/main/interface/vp8.h @@ -128,6 +128,9 @@ public: static WebRtc_Word32 VersionStatic(WebRtc_Word8 *version, WebRtc_Word32 length); private: +// Call encoder initialize function and set speed. + WebRtc_Word32 InitAndSetSpeed(); + EncodedImage _encodedImage; EncodedImageCallback* _encodedCompleteCallback; WebRtc_Word32 _width; @@ -143,6 +146,7 @@ private: bool _haveReceivedAcknowledgement; WebRtc_UWord16 _pictureIDLastSentRef; WebRtc_UWord16 _pictureIDLastAcknowledgedRef; + int _cpuSpeed; vpx_codec_ctx_t* _encoder; vpx_codec_enc_cfg_t* _cfg; diff --git a/modules/video_coding/codecs/vp8/main/source/vp8.cc b/modules/video_coding/codecs/vp8/main/source/vp8.cc index 22ed5f9553..f1f059355f 100644 --- a/modules/video_coding/codecs/vp8/main/source/vp8.cc +++ b/modules/video_coding/codecs/vp8/main/source/vp8.cc @@ -49,6 +49,7 @@ VP8Encoder::VP8Encoder(): _haveReceivedAcknowledgement(false), _pictureIDLastSentRef(0), _pictureIDLastAcknowledgedRef(0), + _cpuSpeed(-6), // default value _encoder(NULL), _cfg(NULL), _raw(NULL) @@ -120,13 +121,20 @@ VP8Encoder::Reset() { return WEBRTC_VIDEO_CODEC_UNINITIALIZED; } - // reinitialize encoder to initial state - if (vpx_codec_enc_init(_encoder, vpx_codec_vp8_cx(), _cfg, 0)) + + if (_encoder != NULL) { - return WEBRTC_VIDEO_CODEC_ERROR; + if (vpx_codec_destroy(_encoder)) + { + return WEBRTC_VIDEO_CODEC_MEMORY; + } + delete _encoder; + _encoder = NULL; } - return WEBRTC_VIDEO_CODEC_OK; + _encoder = new vpx_codec_ctx_t; + + return InitAndSetSpeed(); } WebRtc_Word32 @@ -146,7 +154,8 @@ VP8Encoder::SetRates(WebRtc_UWord32 newBitRateKbit, WebRtc_UWord32 newFrameRate) } // update bit rate - if (_maxBitRateKbit > 0 && newBitRateKbit > static_cast(_maxBitRateKbit)) + if (_maxBitRateKbit > 0 && + newBitRateKbit > static_cast(_maxBitRateKbit)) { newBitRateKbit = _maxBitRateKbit; } @@ -295,35 +304,47 @@ VP8Encoder::InitEncode(const VideoCodec* inst, _cfg->kf_max_dist = 300; } - // construct encoder context - if (vpx_codec_enc_init(_encoder, vpx_codec_vp8_cx(), _cfg, 0)) - { - return WEBRTC_VIDEO_CODEC_UNINITIALIZED; - } switch (inst->codecSpecific.VP8.complexity) { case kComplexityHigh: { - vpx_codec_control(_encoder, VP8E_SET_CPUUSED, -5); + _cpuSpeed = -5; break; } case kComplexityHigher: { - vpx_codec_control(_encoder, VP8E_SET_CPUUSED, -4); + _cpuSpeed = -4; break; } case kComplexityMax: { - vpx_codec_control(_encoder, VP8E_SET_CPUUSED, -3); - break; + _cpuSpeed = -3; + break; } default: { - vpx_codec_control(_encoder, VP8E_SET_CPUUSED, -6); + _cpuSpeed = -6; break; } } + return InitAndSetSpeed(); +} + +WebRtc_Word32 +VP8Encoder::InitAndSetSpeed() +{ + // construct encoder context + vpx_codec_enc_cfg_t cfg_copy = *_cfg; + if (vpx_codec_enc_init(_encoder, vpx_codec_vp8_cx(), _cfg, 0)) + { + return WEBRTC_VIDEO_CODEC_UNINITIALIZED; + } + + vpx_codec_control(_encoder, VP8E_SET_CPUUSED, _cpuSpeed); + + *_cfg = cfg_copy; + _inited = true; return WEBRTC_VIDEO_CODEC_OK; }