video_encode_accelerator_adapter.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2020 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_VIDEO_VIDEO_ENCODE_ACCELERATOR_ADAPTER_H_
  5. #define MEDIA_VIDEO_VIDEO_ENCODE_ACCELERATOR_ADAPTER_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/containers/circular_deque.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/memory/unsafe_shared_memory_pool.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/time/time.h"
  15. #include "media/base/media_export.h"
  16. #include "media/base/video_encoder.h"
  17. #include "media/video/video_encode_accelerator.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. #include "ui/gfx/color_space.h"
  20. #include "ui/gfx/geometry/size.h"
  21. namespace base {
  22. class SequencedTaskRunner;
  23. }
  24. namespace media {
  25. class GpuVideoAcceleratorFactories;
  26. class MediaLog;
  27. class H264AnnexBToAvcBitstreamConverter;
  28. // This class is a somewhat complex adapter from VideoEncodeAccelerator
  29. // to VideoEncoder, it takes cares of such things as
  30. // - managing and copying GPU/shared memory buffers
  31. // - managing hops between task runners, for VEA and callbacks
  32. // - keeping track of the state machine. Forbiding encodes during flush etc.
  33. class MEDIA_EXPORT VideoEncodeAcceleratorAdapter
  34. : public VideoEncoder,
  35. public VideoEncodeAccelerator::Client {
  36. public:
  37. VideoEncodeAcceleratorAdapter(
  38. GpuVideoAcceleratorFactories* gpu_factories,
  39. std::unique_ptr<MediaLog> media_log,
  40. scoped_refptr<base::SequencedTaskRunner> callback_task_runner);
  41. ~VideoEncodeAcceleratorAdapter() override;
  42. enum class InputBufferKind { Any, GpuMemBuf, CpuMemBuf };
  43. // A way to force a certain way of submitting frames to VEA.
  44. void SetInputBufferPreferenceForTesting(InputBufferKind type);
  45. // VideoEncoder implementation.
  46. void Initialize(VideoCodecProfile profile,
  47. const Options& options,
  48. OutputCB output_cb,
  49. EncoderStatusCB done_cb) override;
  50. void Encode(scoped_refptr<VideoFrame> frame,
  51. bool key_frame,
  52. EncoderStatusCB done_cb) override;
  53. void ChangeOptions(const Options& options,
  54. OutputCB output_cb,
  55. EncoderStatusCB done_cb) override;
  56. void Flush(EncoderStatusCB done_cb) override;
  57. // VideoEncodeAccelerator::Client implementation
  58. void RequireBitstreamBuffers(unsigned int input_count,
  59. const gfx::Size& input_coded_size,
  60. size_t output_buffer_size) override;
  61. void BitstreamBufferReady(int32_t buffer_id,
  62. const BitstreamBufferMetadata& metadata) override;
  63. void NotifyError(VideoEncodeAccelerator::Error error) override;
  64. void NotifyEncoderInfoChange(const VideoEncoderInfo& info) override;
  65. // For async disposal by AsyncDestroyVideoEncoder
  66. static void DestroyAsync(std::unique_ptr<VideoEncodeAcceleratorAdapter> self);
  67. private:
  68. enum class State {
  69. kNotInitialized,
  70. kWaitingForFirstFrame,
  71. kInitializing,
  72. kReadyToEncode,
  73. kFlushing
  74. };
  75. struct PendingOp {
  76. PendingOp();
  77. ~PendingOp();
  78. EncoderStatusCB done_callback;
  79. base::TimeDelta timestamp;
  80. gfx::ColorSpace color_space;
  81. };
  82. void FlushCompleted(bool success);
  83. void InitCompleted(EncoderStatus status);
  84. void InitializeOnAcceleratorThread(VideoCodecProfile profile,
  85. const Options& options,
  86. OutputCB output_cb,
  87. EncoderStatusCB done_cb);
  88. void InitializeInternalOnAcceleratorThread();
  89. void EncodeOnAcceleratorThread(scoped_refptr<VideoFrame> frame,
  90. bool key_frame,
  91. EncoderStatusCB done_cb);
  92. void FlushOnAcceleratorThread(EncoderStatusCB done_cb);
  93. void ChangeOptionsOnAcceleratorThread(const Options options,
  94. OutputCB output_cb,
  95. EncoderStatusCB done_cb);
  96. template <class T>
  97. T WrapCallback(T cb);
  98. EncoderStatus::Or<scoped_refptr<VideoFrame>> PrepareGpuFrame(
  99. const gfx::Size& size,
  100. scoped_refptr<VideoFrame> src_frame);
  101. EncoderStatus::Or<scoped_refptr<VideoFrame>> PrepareCpuFrame(
  102. const gfx::Size& size,
  103. scoped_refptr<VideoFrame> src_frame);
  104. scoped_refptr<base::UnsafeSharedMemoryPool> output_pool_;
  105. scoped_refptr<base::UnsafeSharedMemoryPool> input_pool_;
  106. std::unique_ptr<base::UnsafeSharedMemoryPool::Handle> output_handle_holder_;
  107. size_t input_buffer_size_;
  108. std::unique_ptr<VideoEncodeAccelerator> accelerator_;
  109. raw_ptr<GpuVideoAcceleratorFactories> gpu_factories_;
  110. std::unique_ptr<MediaLog> media_log_;
  111. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  112. // If |h264_converter_| is null, we output in annexb format. Otherwise, we
  113. // output in avc format.
  114. std::unique_ptr<H264AnnexBToAvcBitstreamConverter> h264_converter_;
  115. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  116. // These are encodes that have been sent to the accelerator but have not yet
  117. // had their encoded data returned via BitstreamBufferReady().
  118. base::circular_deque<std::unique_ptr<PendingOp>> active_encodes_;
  119. // Color space associated w/ the last frame sent to accelerator for encoding.
  120. gfx::ColorSpace last_frame_color_space_;
  121. std::unique_ptr<PendingOp> pending_flush_;
  122. // For calling accelerator_ methods
  123. scoped_refptr<base::SequencedTaskRunner> accelerator_task_runner_;
  124. SEQUENCE_CHECKER(accelerator_sequence_checker_);
  125. // For calling user provided callbacks
  126. scoped_refptr<base::SequencedTaskRunner> callback_task_runner_;
  127. State state_ = State::kNotInitialized;
  128. absl::optional<bool> flush_support_;
  129. // True if underlying instance of VEA can handle GPU backed frames with a
  130. // size different from what VEA was configured for.
  131. bool gpu_resize_supported_ = false;
  132. // These are encodes that have not been sent to the accelerator.
  133. std::vector<std::unique_ptr<PendingEncode>> pending_encodes_;
  134. VideoPixelFormat format_;
  135. InputBufferKind input_buffer_preference_ = InputBufferKind::Any;
  136. std::vector<uint8_t> resize_buf_;
  137. VideoCodecProfile profile_ = VIDEO_CODEC_PROFILE_UNKNOWN;
  138. VideoEncodeAccelerator::SupportedRateControlMode supported_rc_modes_ =
  139. VideoEncodeAccelerator::kNoMode;
  140. Options options_;
  141. OutputCB output_cb_;
  142. gfx::Size input_coded_size_;
  143. };
  144. } // namespace media
  145. #endif // MEDIA_VIDEO_VIDEO_ENCODE_ACCELERATOR_ADAPTER_H_