webrtc_video_encoder_av1.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2022 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_AV1_H_
  5. #define REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_AV1_H_
  6. #include "base/callback.h"
  7. #include "remoting/codec/encoder_bitrate_filter.h"
  8. #include "remoting/codec/video_encoder_active_map.h"
  9. #include "remoting/codec/webrtc_video_encoder.h"
  10. #include "third_party/libaom/source/libaom/aom/aom_encoder.h"
  11. #include "third_party/libaom/source/libaom/aom/aom_image.h"
  12. #include "third_party/libaom/source/libaom/aom/aomcx.h"
  13. namespace webrtc {
  14. class DesktopFrame;
  15. class DesktopRegion;
  16. class DesktopSize;
  17. } // namespace webrtc
  18. namespace remoting {
  19. // AV1 encoder implementation for WebRTC transport, params are optimized for
  20. // real-time screen sharing.
  21. class WebrtcVideoEncoderAV1 : public WebrtcVideoEncoder {
  22. public:
  23. WebrtcVideoEncoderAV1();
  24. WebrtcVideoEncoderAV1(const WebrtcVideoEncoderAV1&) = delete;
  25. WebrtcVideoEncoderAV1& operator=(const WebrtcVideoEncoderAV1&) = delete;
  26. ~WebrtcVideoEncoderAV1() override;
  27. // WebrtcVideoEncoder interface.
  28. void SetLosslessEncode(bool want_lossless) override;
  29. void SetLosslessColor(bool want_lossless) override;
  30. void Encode(std::unique_ptr<webrtc::DesktopFrame> frame,
  31. const FrameParams& params,
  32. EncodeCallback done) override;
  33. private:
  34. void ConfigureCodecParams();
  35. bool InitializeCodec(const webrtc::DesktopSize& size);
  36. void UpdateConfig(const FrameParams& params);
  37. void PrepareImage(const webrtc::DesktopFrame* frame,
  38. webrtc::DesktopRegion& updated_region);
  39. using scoped_aom_codec =
  40. std::unique_ptr<aom_codec_ctx_t, void (*)(aom_codec_ctx_t*)>;
  41. scoped_aom_codec codec_;
  42. aom_codec_enc_cfg_t config_ = {};
  43. using scoped_aom_image = std::unique_ptr<aom_image_t, void (*)(aom_image_t*)>;
  44. scoped_aom_image image_;
  45. // Active map used to optimize out processing of unchanged macroblocks.
  46. VideoEncoderActiveMap active_map_;
  47. // Disable |active_map_| until we've verified it improves performance.
  48. const bool use_active_map_ = false;
  49. // This timestamp is monotonically increased using the current frame duration.
  50. // It's only used for rate control and is not related to the timestamps on the
  51. // incoming frames to encode.
  52. aom_codec_pts_t artificial_timestamp_us_ = 0;
  53. EncoderBitrateFilter bitrate_filter_;
  54. };
  55. } // namespace remoting
  56. #endif // REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_AV1_H_