video_encoder_vpx.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2013 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_VIDEO_ENCODER_VPX_H_
  5. #define REMOTING_CODEC_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/scoped_vpx_codec.h"
  12. #include "remoting/codec/video_encoder.h"
  13. #include "remoting/codec/video_encoder_helper.h"
  14. typedef struct vpx_image vpx_image_t;
  15. namespace webrtc {
  16. class DesktopRegion;
  17. class DesktopSize;
  18. } // namespace webrtc
  19. namespace remoting {
  20. class VideoEncoderVpx : public VideoEncoder {
  21. public:
  22. // Create encoder for the specified protocol.
  23. static std::unique_ptr<VideoEncoderVpx> CreateForVP8();
  24. static std::unique_ptr<VideoEncoderVpx> CreateForVP9();
  25. VideoEncoderVpx(const VideoEncoderVpx&) = delete;
  26. VideoEncoderVpx& operator=(const VideoEncoderVpx&) = delete;
  27. ~VideoEncoderVpx() override;
  28. void SetTickClockForTests(const base::TickClock* tick_clock);
  29. // VideoEncoder interface.
  30. void SetLosslessEncode(bool want_lossless) override;
  31. void SetLosslessColor(bool want_lossless) override;
  32. std::unique_ptr<VideoPacket> Encode(
  33. const webrtc::DesktopFrame& frame) override;
  34. private:
  35. explicit VideoEncoderVpx(bool use_vp9);
  36. // (Re)Configures this instance to encode frames of the specified |size|,
  37. // with the configured lossless color & encoding modes.
  38. void Configure(const webrtc::DesktopSize& size);
  39. // Prepares |image_| for encoding. Writes updated rectangles into
  40. // |updated_region|.
  41. void PrepareImage(const webrtc::DesktopFrame& frame,
  42. webrtc::DesktopRegion* updated_region);
  43. // Updates the active map according to |updated_region|. Active map is then
  44. // given to the encoder to speed up encoding.
  45. void SetActiveMapFromRegion(const webrtc::DesktopRegion& updated_region);
  46. // Adds areas changed in the most recent frame to |updated_region|. This
  47. // includes both content changes and areas enhanced by cyclic refresh.
  48. void UpdateRegionFromActiveMap(webrtc::DesktopRegion* updated_region);
  49. // True if the encoder is for VP9, false for VP8.
  50. const bool use_vp9_;
  51. // Options controlling VP9 encode quantization and color space.
  52. // These are always off (false) for VP8.
  53. bool lossless_encode_ = false;
  54. bool lossless_color_ = false;
  55. // Holds the initialized & configured codec.
  56. ScopedVpxCodec codec_;
  57. // Used to generate zero-based frame timestamps.
  58. base::TimeTicks timestamp_base_;
  59. // VPX image and buffer to hold the actual YUV planes.
  60. std::unique_ptr<vpx_image_t> image_;
  61. std::unique_ptr<uint8_t[]> image_buffer_;
  62. // Active map used to optimize out processing of un-changed macroblocks.
  63. std::unique_ptr<uint8_t[]> active_map_;
  64. webrtc::DesktopSize active_map_size_;
  65. // True if the codec wants unchanged frames to finish topping-off with.
  66. bool encode_unchanged_frame_;
  67. // Used to help initialize VideoPackets from DesktopFrames.
  68. VideoEncoderHelper helper_;
  69. raw_ptr<const base::TickClock> clock_;
  70. };
  71. } // namespace remoting
  72. #endif // REMOTING_CODEC_VIDEO_ENCODER_VPX_H_