media_foundation_video_encode_accelerator_win.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2016 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_WINDOWS_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_
  5. #define MEDIA_GPU_WINDOWS_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_
  6. #include <mfapi.h>
  7. #include <mfidl.h>
  8. #include <stdint.h>
  9. #include <strmif.h>
  10. #include <wrl/client.h>
  11. #include <memory>
  12. #include "base/bind.h"
  13. #include "base/containers/circular_deque.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/task/single_thread_task_runner.h"
  16. #include "base/threading/thread.h"
  17. #include "base/win/windows_types.h"
  18. #include "gpu/config/gpu_driver_bug_workarounds.h"
  19. #include "gpu/config/gpu_preferences.h"
  20. #include "media/base/bitrate.h"
  21. #include "media/base/video_codecs.h"
  22. #include "media/base/win/dxgi_device_manager.h"
  23. #include "media/gpu/media_gpu_export.h"
  24. #include "media/video/h264_parser.h"
  25. #include "media/video/video_encode_accelerator.h"
  26. namespace media {
  27. // Media Foundation implementation of the VideoEncodeAccelerator interface for
  28. // Windows.
  29. // This class saves the task runner on which it is constructed and runs client
  30. // callbacks using that same task runner.
  31. // This class has DCHECKs to makes sure that methods are called in the
  32. // correct task runners. It starts an internal encoder thread on which
  33. // VideoEncodeAccelerator implementation tasks are posted.
  34. class MEDIA_GPU_EXPORT MediaFoundationVideoEncodeAccelerator
  35. : public VideoEncodeAccelerator {
  36. public:
  37. explicit MediaFoundationVideoEncodeAccelerator(
  38. const gpu::GpuPreferences& gpu_preferences,
  39. const gpu::GpuDriverBugWorkarounds& gpu_workarounds,
  40. CHROME_LUID luid);
  41. MediaFoundationVideoEncodeAccelerator(
  42. const MediaFoundationVideoEncodeAccelerator&) = delete;
  43. MediaFoundationVideoEncodeAccelerator& operator=(
  44. const MediaFoundationVideoEncodeAccelerator&) = delete;
  45. // VideoEncodeAccelerator implementation.
  46. VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override;
  47. VideoEncodeAccelerator::SupportedProfiles GetSupportedProfilesLight()
  48. override;
  49. bool Initialize(const Config& config,
  50. Client* client,
  51. std::unique_ptr<MediaLog> media_log) override;
  52. void Encode(scoped_refptr<VideoFrame> frame, bool force_keyframe) override;
  53. void UseOutputBitstreamBuffer(BitstreamBuffer buffer) override;
  54. void RequestEncodingParametersChange(const Bitrate& bitrate,
  55. uint32_t framerate) override;
  56. void Destroy() override;
  57. bool IsGpuFrameResizeSupported() override;
  58. // Preloads dlls required for encoding. Returns true if all required dlls are
  59. // correctly loaded.
  60. static bool PreSandboxInitialization();
  61. enum class DriverVendor { kOther, kNvidia, kIntel, kAMD };
  62. protected:
  63. ~MediaFoundationVideoEncodeAccelerator() override;
  64. private:
  65. // Holds output buffers coming from the client ready to be filled.
  66. struct BitstreamBufferRef;
  67. // Holds output buffers coming from the encoder.
  68. class EncodeOutput;
  69. // Get supported profiles for specific codec.
  70. VideoEncodeAccelerator::SupportedProfiles GetSupportedProfilesForCodec(
  71. VideoCodec codec,
  72. bool populate_svc_info);
  73. // Activates the asynchronous encoder instance |encoder_| according to codec
  74. // merit.
  75. bool ActivateAsyncEncoder(IMFActivate** pp_activates,
  76. uint32_t activate_count,
  77. bool is_constrained_h264);
  78. // Initializes and allocates memory for input and output parameters.
  79. bool InitializeInputOutputParameters(VideoCodecProfile output_profile,
  80. bool is_constrained_h264);
  81. // Initializes encoder parameters for real-time use.
  82. bool SetEncoderModes();
  83. // Helper function to notify the client of an error on
  84. // |main_client_task_runner_|.
  85. void NotifyError(VideoEncodeAccelerator::Error error);
  86. // Encoding task to be run on |encoder_thread_task_runner_|.
  87. void EncodeTask(scoped_refptr<VideoFrame> frame, bool force_keyframe);
  88. // Processes the input video frame for the encoder.
  89. HRESULT ProcessInput(scoped_refptr<VideoFrame> frame, bool force_keyframe);
  90. // Populates input sample buffer with contents of a video frame
  91. HRESULT PopulateInputSampleBuffer(scoped_refptr<VideoFrame> frame);
  92. HRESULT PopulateInputSampleBufferGpu(scoped_refptr<VideoFrame> frame);
  93. // Assign TemporalID by bitstream or external state machine(based on SVC
  94. // Spec).
  95. bool AssignTemporalId(Microsoft::WRL::ComPtr<IMFMediaBuffer> output_buffer,
  96. size_t size,
  97. int* temporal_id,
  98. bool keyframe);
  99. int AssignTemporalIdBySvcSpec(bool keyframe);
  100. bool temporalScalableCoding() { return num_temporal_layers_ > 1; }
  101. // Checks for and copies encoded output on |encoder_thread_task_runner_|.
  102. void ProcessOutput();
  103. // Drains pending output samples on |encoder_thread_task_runner_|.
  104. void DrainPendingOutputs();
  105. // Tries to deliver the input frame to the encoder.
  106. bool TryToDeliverInputFrame(scoped_refptr<VideoFrame> frame,
  107. bool force_keyframe);
  108. // Tries to return a bitstream buffer to the client.
  109. void TryToReturnBitstreamBuffer();
  110. // Inserts the output buffers for reuse on |encoder_thread_task_runner_|.
  111. void UseOutputBitstreamBufferTask(
  112. std::unique_ptr<BitstreamBufferRef> buffer_ref);
  113. // Changes encode parameters on |encoder_thread_task_runner_|.
  114. void RequestEncodingParametersChangeTask(const Bitrate& bitrate,
  115. uint32_t framerate);
  116. // Destroys encode session on |encoder_thread_task_runner_|.
  117. void DestroyTask();
  118. // Initialize the encoder on |encoder_thread_task_runner_|.
  119. void EncoderInitializeTask(const Config& config,
  120. std::unique_ptr<MediaLog> media_log);
  121. // Releases resources encoder holds.
  122. void ReleaseEncoderResources();
  123. // Initialize video processing (for scaling)
  124. HRESULT InitializeD3DVideoProcessing(ID3D11Texture2D* input_texture);
  125. // Perform D3D11 scaling operation
  126. HRESULT PerformD3DScaling(ID3D11Texture2D* input_texture);
  127. const bool compatible_with_win7_;
  128. const bool disable_dynamic_framerate_update_;
  129. // Bitstream buffers ready to be used to return encoded output as a FIFO.
  130. base::circular_deque<std::unique_ptr<BitstreamBufferRef>>
  131. bitstream_buffer_queue_;
  132. // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO.
  133. base::circular_deque<std::unique_ptr<EncodeOutput>> encoder_output_queue_;
  134. // Counter of outputs which is used to assign temporal layer indexes
  135. // according to the corresponding layer pattern. Reset for every key frame.
  136. uint32_t outputs_since_keyframe_count_ = 0;
  137. // This parser is used to assign temporalId.
  138. H264Parser h264_parser_;
  139. gfx::Size input_visible_size_;
  140. size_t bitstream_buffer_size_;
  141. uint32_t frame_rate_;
  142. Bitrate bitrate_;
  143. bool low_latency_mode_;
  144. int num_temporal_layers_ = 1;
  145. // Codec type used for encoding.
  146. VideoCodec codec_ = VideoCodec::kUnknown;
  147. // Vendor of the active video encoder.
  148. DriverVendor vendor_ = DriverVendor::kOther;
  149. // Group of picture length for encoded output stream, indicates the
  150. // distance between two key frames.
  151. uint32_t gop_length_;
  152. Microsoft::WRL::ComPtr<IMFActivate> activate_;
  153. Microsoft::WRL::ComPtr<IMFTransform> encoder_;
  154. Microsoft::WRL::ComPtr<ICodecAPI> codec_api_;
  155. Microsoft::WRL::ComPtr<IMFMediaEventGenerator> event_generator_;
  156. DWORD input_stream_id_;
  157. DWORD output_stream_id_;
  158. Microsoft::WRL::ComPtr<IMFMediaType> imf_input_media_type_;
  159. Microsoft::WRL::ComPtr<IMFMediaType> imf_output_media_type_;
  160. bool input_required_;
  161. Microsoft::WRL::ComPtr<IMFSample> input_sample_;
  162. Microsoft::WRL::ComPtr<IMFSample> output_sample_;
  163. Microsoft::WRL::ComPtr<ID3D11VideoProcessor> video_processor_;
  164. Microsoft::WRL::ComPtr<ID3D11VideoProcessorEnumerator>
  165. video_processor_enumerator_;
  166. Microsoft::WRL::ComPtr<ID3D11VideoDevice> video_device_;
  167. Microsoft::WRL::ComPtr<ID3D11VideoContext> video_context_;
  168. D3D11_VIDEO_PROCESSOR_CONTENT_DESC vp_desc_ = {};
  169. Microsoft::WRL::ComPtr<ID3D11Texture2D> scaled_d3d11_texture_;
  170. Microsoft::WRL::ComPtr<ID3D11VideoProcessorOutputView> vp_output_view_;
  171. // To expose client callbacks from VideoEncodeAccelerator.
  172. // NOTE: all calls to this object *MUST* be executed on
  173. // |main_client_task_runner_|.
  174. base::WeakPtr<Client> main_client_;
  175. std::unique_ptr<base::WeakPtrFactory<Client>> main_client_weak_factory_;
  176. scoped_refptr<base::SequencedTaskRunner> main_client_task_runner_;
  177. SEQUENCE_CHECKER(sequence_checker_);
  178. // This thread services tasks posted from the VEA API entry points
  179. // and runs them on a thread that can do heavy work and call MF COM interface.
  180. scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_;
  181. SEQUENCE_CHECKER(encode_sequence_checker_);
  182. // DXGI device manager for handling hardware input textures
  183. scoped_refptr<DXGIDeviceManager> dxgi_device_manager_;
  184. // Preferred adapter for DXGIDeviceManager.
  185. const CHROME_LUID luid_;
  186. // A buffer used as a scratch space for I420 to NV12 conversion
  187. std::vector<uint8_t> resize_buffer_;
  188. // Declared last to ensure that all weak pointers are invalidated before
  189. // other destructors run.
  190. base::WeakPtr<MediaFoundationVideoEncodeAccelerator> encoder_weak_ptr_;
  191. base::WeakPtrFactory<MediaFoundationVideoEncodeAccelerator>
  192. encoder_task_weak_factory_{this};
  193. };
  194. } // namespace media
  195. #endif // MEDIA_GPU_WINDOWS_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_