webrtc_video_encoder_vpx.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_VPX_H_
  5. #define REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_VPX_H_
  6. #include <stdint.h>
  7. #include "base/callback.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/time/default_tick_clock.h"
  10. #include "base/time/time.h"
  11. #include "remoting/codec/encoder_bitrate_filter.h"
  12. #include "remoting/codec/scoped_vpx_codec.h"
  13. #include "remoting/codec/video_encoder_active_map.h"
  14. #include "remoting/codec/webrtc_video_encoder.h"
  15. #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"
  16. typedef struct vpx_image vpx_image_t;
  17. namespace webrtc {
  18. class DesktopRegion;
  19. class DesktopSize;
  20. } // namespace webrtc
  21. namespace remoting {
  22. // This is a copy of VideoEncoderVpx with enhancements to encoder for use
  23. // over WebRTC as transport. The original VideoEncoderVpx should be deleted
  24. // once the old implementation is no longer in use.
  25. class WebrtcVideoEncoderVpx : public WebrtcVideoEncoder {
  26. public:
  27. // Creates encoder for the specified protocol.
  28. static std::unique_ptr<WebrtcVideoEncoder> CreateForVP8();
  29. static std::unique_ptr<WebrtcVideoEncoder> CreateForVP9();
  30. WebrtcVideoEncoderVpx(const WebrtcVideoEncoderVpx&) = delete;
  31. WebrtcVideoEncoderVpx& operator=(const WebrtcVideoEncoderVpx&) = delete;
  32. ~WebrtcVideoEncoderVpx() override;
  33. void SetTickClockForTests(const base::TickClock* tick_clock);
  34. // WebrtcVideoEncoder interface.
  35. void SetLosslessEncode(bool want_lossless) override;
  36. void SetLosslessColor(bool want_lossless) override;
  37. void SetEncoderSpeed(int encoder_speed) override;
  38. void Encode(std::unique_ptr<webrtc::DesktopFrame> frame,
  39. const FrameParams& params,
  40. EncodeCallback done) override;
  41. private:
  42. explicit WebrtcVideoEncoderVpx(bool use_vp9);
  43. // (Re)Configures this instance to encode frames of the specified |size|,
  44. // with the configured lossless color & encoding modes.
  45. void Configure(const webrtc::DesktopSize& size);
  46. // Updates codec configuration.
  47. void UpdateConfig(const FrameParams& params);
  48. // Prepares |image_| for encoding. Writes updated rectangles into
  49. // |updated_region|.
  50. void PrepareImage(const webrtc::DesktopFrame* frame,
  51. webrtc::DesktopRegion* updated_region);
  52. // Clears active map.
  53. void ClearActiveMap();
  54. // Updates the active map according to |updated_region|. Active map is then
  55. // given to the encoder to speed up encoding.
  56. void SetActiveMapFromRegion(const webrtc::DesktopRegion& updated_region);
  57. // Adds areas changed in the most recent frame to |updated_region|. This
  58. // includes both content changes and areas enhanced by cyclic refresh.
  59. void UpdateRegionFromActiveMap(webrtc::DesktopRegion* updated_region);
  60. // True if the encoder is for VP9, false for VP8.
  61. const bool use_vp9_;
  62. // Options controlling VP9 encode quantization, color space, and speed.
  63. // These are not used when configuring VP8.
  64. bool lossless_encode_ = false;
  65. bool lossless_color_ = false;
  66. int vp9_encoder_speed_ = -1;
  67. // Holds the initialized & configured codec.
  68. ScopedVpxCodec codec_;
  69. vpx_codec_enc_cfg_t config_;
  70. // Used to generate zero-based frame timestamps.
  71. base::TimeTicks timestamp_base_;
  72. // vpx_image_t has a custom deallocator which needs to be called before
  73. // deletion.
  74. using scoped_vpx_image = std::unique_ptr<vpx_image_t, void (*)(vpx_image_t*)>;
  75. // VPX image descriptor and pixel buffer.
  76. scoped_vpx_image image_;
  77. // Active map used to optimize out processing of unchanged macroblocks.
  78. VideoEncoderActiveMap active_map_;
  79. // TODO(joedow): Remove this flag after we're done with performance tuning.
  80. const bool use_active_map_ = true;
  81. raw_ptr<const base::TickClock> clock_;
  82. EncoderBitrateFilter bitrate_filter_;
  83. };
  84. } // namespace remoting
  85. #endif // REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_VPX_H_