vaapi_video_encoder_delegate.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2018 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_VAAPI_VIDEO_ENCODER_DELEGATE_H_
  5. #define MEDIA_GPU_VAAPI_VAAPI_VIDEO_ENCODER_DELEGATE_H_
  6. #include <va/va.h>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/time/time.h"
  13. #include "media/base/video_bitrate_allocation.h"
  14. #include "media/base/video_codecs.h"
  15. #include "media/video/video_encode_accelerator.h"
  16. #include "media/video/video_encoder_info.h"
  17. #include "ui/gfx/geometry/size.h"
  18. namespace media {
  19. struct BitstreamBufferMetadata;
  20. class CodecPicture;
  21. class ScopedVABuffer;
  22. class VideoFrame;
  23. class VaapiWrapper;
  24. // An VaapiVideoEncoderDelegate performs high-level, platform-independent
  25. // encoding process tasks, such as managing codec state, reference frames, etc.,
  26. // but may require support from an external accelerator (typically a hardware
  27. // accelerator) to offload some stages of the actual encoding process, using
  28. // the parameters that the Delegate prepares beforehand.
  29. //
  30. // For each frame to be encoded, clients provide an EncodeJob object to be set
  31. // up by a Delegate subclass with job parameters, and execute the job
  32. // afterwards. Any resources required for the job are also provided by the
  33. // clients, and associated with the EncodeJob object.
  34. class VaapiVideoEncoderDelegate {
  35. public:
  36. VaapiVideoEncoderDelegate(scoped_refptr<VaapiWrapper> vaapi_wrapper,
  37. base::RepeatingClosure error_cb);
  38. virtual ~VaapiVideoEncoderDelegate();
  39. struct Config {
  40. // Maximum number of reference frames.
  41. // For H.264 encoding, the value represents the maximum number of reference
  42. // frames for both the reference picture list 0 (bottom 16 bits) and the
  43. // reference picture list 1 (top 16 bits).
  44. size_t max_num_ref_frames;
  45. };
  46. // EncodeResult owns the necessary resource to keep the encoded buffer. The
  47. // encoded buffer can be downloaded with the EncodeResult, for example, by
  48. // calling VaapiWrapper::DownloadFromVABuffer().
  49. class EncodeResult {
  50. public:
  51. EncodeResult(std::unique_ptr<ScopedVABuffer> coded_buffer,
  52. const BitstreamBufferMetadata& metadata);
  53. ~EncodeResult();
  54. VABufferID coded_buffer_id() const;
  55. const BitstreamBufferMetadata& metadata() const;
  56. private:
  57. const std::unique_ptr<ScopedVABuffer> coded_buffer_;
  58. const BitstreamBufferMetadata metadata_;
  59. };
  60. // An abstraction of an encode job for one frame. Parameters required for an
  61. // EncodeJob to be executed are prepared by an VaapiVideoEncoderDelegate,
  62. // while the accelerator-specific callbacks required to set up and execute it
  63. // are provided by the accelerator itself, based on these parameters.
  64. // Accelerators are also responsible for providing any resources (such as
  65. // memory for output, etc.) as needed.
  66. class EncodeJob {
  67. public:
  68. // Creates an EncodeJob to encode |input_frame|, which will be executed by
  69. // calling ExecuteSetupCallbacks() in VaapiVideoEncoderDelegate::Encode().
  70. // If |keyframe| is true, requests this job to produce a keyframe.
  71. EncodeJob(bool keyframe,
  72. base::TimeDelta timestamp,
  73. VASurfaceID input_surface_id);
  74. // Constructor for VA-API.
  75. EncodeJob(bool keyframe,
  76. base::TimeDelta timestamp,
  77. VASurfaceID input_surface_id,
  78. scoped_refptr<CodecPicture> picture,
  79. std::unique_ptr<ScopedVABuffer> coded_buffer);
  80. EncodeJob(const EncodeJob&) = delete;
  81. EncodeJob& operator=(const EncodeJob&) = delete;
  82. ~EncodeJob();
  83. // Creates EncodeResult with |metadata|. This passes ownership of the
  84. // resources owned by EncodeJob and therefore must be called with
  85. // std::move().
  86. std::unique_ptr<EncodeResult> CreateEncodeResult(
  87. const BitstreamBufferMetadata& metadata) &&;
  88. // Requests this job to produce a keyframe; requesting a keyframe may not
  89. // always result in one being produced by the encoder (e.g. if it would
  90. // not fit in the bitrate budget).
  91. void ProduceKeyframe() { keyframe_ = true; }
  92. // Returns true if this job has been requested to produce a keyframe.
  93. bool IsKeyframeRequested() const { return keyframe_; }
  94. base::TimeDelta timestamp() const;
  95. // VA-API specific methods.
  96. VABufferID coded_buffer_id() const;
  97. VASurfaceID input_surface_id() const;
  98. const scoped_refptr<CodecPicture>& picture() const;
  99. private:
  100. // True if this job is to produce a keyframe.
  101. bool keyframe_;
  102. // |timestamp_| to be added to the produced encoded chunk.
  103. const base::TimeDelta timestamp_;
  104. // VA-API specific members.
  105. // Input surface ID and size for video frame data or scaled data.
  106. const VASurfaceID input_surface_id_;
  107. const scoped_refptr<CodecPicture> picture_;
  108. // Buffer that will contain the output bitstream data for this frame.
  109. std::unique_ptr<ScopedVABuffer> coded_buffer_;
  110. };
  111. // Initializes the encoder with requested parameter set |config| and
  112. // |ave_config|. Returns false if the requested set of parameters is not
  113. // supported, true on success.
  114. virtual bool Initialize(
  115. const VideoEncodeAccelerator::Config& config,
  116. const VaapiVideoEncoderDelegate::Config& ave_config) = 0;
  117. // Updates current framerate and/or bitrate to |framerate| in FPS
  118. // and the specified video bitrate allocation.
  119. virtual bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
  120. uint32_t framerate) = 0;
  121. // Returns coded size for the input buffers required to encode, in pixels;
  122. // typically visible size adjusted to match codec alignment requirements.
  123. virtual gfx::Size GetCodedSize() const = 0;
  124. // Returns minimum size in bytes for bitstream buffers required to fit output
  125. // stream buffers produced.
  126. virtual size_t GetBitstreamBufferSize() const;
  127. // Returns maximum number of reference frames that may be used by the
  128. // encoder to encode one frame. The client should be able to provide up to
  129. // at least this many frames simultaneously for encode to make progress.
  130. virtual size_t GetMaxNumOfRefFrames() const = 0;
  131. // Prepares and submits the encode operation to underlying driver for an
  132. // EncodeJob for one frame and returns true on success.
  133. bool Encode(EncodeJob& encode_job);
  134. // Creates and returns the encode result for specified EncodeJob by
  135. // synchronizing the corresponding encode operation.
  136. std::unique_ptr<EncodeResult> GetEncodeResult(
  137. std::unique_ptr<EncodeJob> encode_job);
  138. // Gets the active spatial layer resolutions for K-SVC encoding, VaapiVEA
  139. // can get this info from the encoder delegate. Returns empty vector on
  140. // failure.
  141. virtual std::vector<gfx::Size> GetSVCLayerResolutions() = 0;
  142. protected:
  143. virtual BitstreamBufferMetadata GetMetadata(const EncodeJob& encode_job,
  144. size_t payload_size);
  145. const scoped_refptr<VaapiWrapper> vaapi_wrapper_;
  146. base::RepeatingClosure error_cb_;
  147. SEQUENCE_CHECKER(sequence_checker_);
  148. private:
  149. // Prepares a new |encode_job| to be executed in Accelerator and returns true
  150. // on success. The caller may then call ExecuteSetupCallbacks() on the job to
  151. // run them.
  152. virtual bool PrepareEncodeJob(EncodeJob& encode_job) = 0;
  153. // Notifies the encoded chunk size in bytes to update a bitrate controller in
  154. // VaapiVideoEncoderDelegate. This should be called only if constant
  155. // quantization encoding is used, which currently is true for VP8 and VP9.
  156. virtual void BitrateControlUpdate(uint64_t encoded_chunk_size_bytes);
  157. };
  158. } // namespace media
  159. #endif // MEDIA_GPU_VAAPI_VAAPI_VIDEO_ENCODER_DELEGATE_H_