vp9_vaapi_video_encoder_delegate.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2019 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 MEDIA_GPU_VAAPI_VP9_VAAPI_VIDEO_ENCODER_DELEGATE_H_
  5. #define MEDIA_GPU_VAAPI_VP9_VAAPI_VIDEO_ENCODER_DELEGATE_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "media/base/video_bitrate_allocation.h"
  10. #include "media/filters/vp9_parser.h"
  11. #include "media/gpu/vaapi/vaapi_video_encoder_delegate.h"
  12. #include "media/gpu/video_rate_control.h"
  13. #include "media/gpu/vp9_picture.h"
  14. #include "media/gpu/vp9_reference_frame_vector.h"
  15. namespace libvpx {
  16. struct VP9FrameParamsQpRTC;
  17. class VP9RateControlRTC;
  18. struct VP9RateControlRtcConfig;
  19. } // namespace libvpx
  20. namespace media {
  21. class VaapiWrapper;
  22. class VP9SVCLayers;
  23. class VP9VaapiVideoEncoderDelegate : public VaapiVideoEncoderDelegate {
  24. public:
  25. struct EncodeParams {
  26. EncodeParams();
  27. // Produce a keyframe at least once per this many frames.
  28. size_t kf_period_frames;
  29. // Bitrate allocation in bps.
  30. VideoBitrateAllocation bitrate_allocation;
  31. // Framerate in FPS.
  32. uint32_t framerate;
  33. // Quantization parameter. They are vp9 ac/dc indices and their ranges are
  34. // 0-255.
  35. uint8_t min_qp;
  36. uint8_t max_qp;
  37. };
  38. VP9VaapiVideoEncoderDelegate(scoped_refptr<VaapiWrapper> vaapi_wrapper,
  39. base::RepeatingClosure error_cb);
  40. VP9VaapiVideoEncoderDelegate(const VP9VaapiVideoEncoderDelegate&) = delete;
  41. VP9VaapiVideoEncoderDelegate& operator=(const VP9VaapiVideoEncoderDelegate&) =
  42. delete;
  43. ~VP9VaapiVideoEncoderDelegate() override;
  44. // VaapiVideoEncoderDelegate implementation.
  45. bool Initialize(const VideoEncodeAccelerator::Config& config,
  46. const VaapiVideoEncoderDelegate::Config& ave_config) override;
  47. bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
  48. uint32_t framerate) override;
  49. gfx::Size GetCodedSize() const override;
  50. size_t GetMaxNumOfRefFrames() const override;
  51. std::vector<gfx::Size> GetSVCLayerResolutions() override;
  52. private:
  53. friend class VP9VaapiVideoEncoderDelegateTest;
  54. friend class VaapiVideoEncodeAcceleratorTest;
  55. using VP9RateControl = VideoRateControl<libvpx::VP9RateControlRtcConfig,
  56. libvpx::VP9RateControlRTC,
  57. libvpx::VP9FrameParamsQpRTC>;
  58. void set_rate_ctrl_for_testing(std::unique_ptr<VP9RateControl> rate_ctrl);
  59. bool ApplyPendingUpdateRates();
  60. bool PrepareEncodeJob(EncodeJob& encode_job) override;
  61. BitstreamBufferMetadata GetMetadata(const EncodeJob& encode_job,
  62. size_t payload_size) override;
  63. void BitrateControlUpdate(uint64_t encoded_chunk_size_bytes) override;
  64. Vp9FrameHeader GetDefaultFrameHeader(const bool keyframe) const;
  65. void SetFrameHeader(bool keyframe,
  66. VP9Picture* picture,
  67. std::array<bool, kVp9NumRefsPerFrame>* ref_frames_used);
  68. void UpdateReferenceFrames(scoped_refptr<VP9Picture> picture);
  69. bool SubmitFrameParameters(
  70. EncodeJob& job,
  71. const EncodeParams& encode_params,
  72. scoped_refptr<VP9Picture> pic,
  73. const Vp9ReferenceFrameVector& ref_frames,
  74. const std::array<bool, kVp9NumRefsPerFrame>& ref_frames_used);
  75. gfx::Size visible_size_;
  76. gfx::Size coded_size_; // Macroblock-aligned.
  77. // Frame count since last keyframe, reset to 0 every keyframe period.
  78. size_t frame_num_ = 0;
  79. size_t ref_frame_index_ = 0;
  80. EncodeParams current_params_;
  81. Vp9ReferenceFrameVector reference_frames_;
  82. std::unique_ptr<VP9SVCLayers> svc_layers_;
  83. absl::optional<std::pair<VideoBitrateAllocation, uint32_t>>
  84. pending_update_rates_;
  85. std::unique_ptr<VP9RateControl> rate_ctrl_;
  86. };
  87. } // namespace media
  88. #endif // MEDIA_GPU_VAAPI_VP9_VAAPI_VIDEO_ENCODER_DELEGATE_H_