OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ |
| 6 #define MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ |
| 7 |
| 8 #include <mfapi.h> |
| 9 #include <mfidl.h> |
| 10 #include <stdint.h> |
| 11 #include <strmif.h> |
| 12 |
| 13 #include <deque> |
| 14 #include <memory> |
| 15 |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/threading/thread_checker.h" |
| 20 #include "base/win/scoped_comptr.h" |
| 21 #include "media/gpu/media_gpu_export.h" |
| 22 #include "media/video/video_encode_accelerator.h" |
| 23 |
| 24 namespace media { |
| 25 |
| 26 // Media Foundation implementation of the VideoEncodeAccelerator interface for |
| 27 // Windows. |
| 28 // This class saves the task runner on which it is constructed and returns |
| 29 // encoded data to the client using that same task runner. This class has |
| 30 // DCHECKs to makes sure that methods are called in sequence. It starts an |
| 31 // internal encoder thread on which VideoEncodeAccelerator implementation tasks |
| 32 // are posted. |
| 33 class MEDIA_GPU_EXPORT MediaFoundationVideoEncodeAccelerator |
| 34 : public VideoEncodeAccelerator { |
| 35 public: |
| 36 MediaFoundationVideoEncodeAccelerator(); |
| 37 |
| 38 // VideoEncodeAccelerator implementation. |
| 39 VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override; |
| 40 bool Initialize(VideoPixelFormat input_format, |
| 41 const gfx::Size& input_visible_size, |
| 42 VideoCodecProfile output_profile, |
| 43 uint32_t initial_bitrate, |
| 44 Client* client) override; |
| 45 void Encode(const scoped_refptr<VideoFrame>& frame, |
| 46 bool force_keyframe) override; |
| 47 void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override; |
| 48 void RequestEncodingParametersChange(uint32_t bitrate, |
| 49 uint32_t framerate) override; |
| 50 void Destroy() override; |
| 51 |
| 52 // Preload dlls required for encoding. |
| 53 static void PreSandboxInitialization(); |
| 54 |
| 55 protected: |
| 56 ~MediaFoundationVideoEncodeAccelerator() override; |
| 57 |
| 58 private: |
| 59 // Holds output buffers coming from the client ready to be filled. |
| 60 struct BitstreamBufferRef; |
| 61 |
| 62 // Holds output buffers coming from the encoder. |
| 63 class EncodeOutput; |
| 64 |
| 65 // Initializes and allocates memory for input and output samples. |
| 66 bool InitializeInputOutputSamples(); |
| 67 |
| 68 // Initializes encoder parameters for real-time use. |
| 69 bool SetEncoderModes(); |
| 70 |
| 71 // Helper function to notify the client of an error on |client_task_runner_|. |
| 72 void NotifyError(VideoEncodeAccelerator::Error error); |
| 73 |
| 74 // Encoding tasks to be run on |encoder_thread_|. |
| 75 void EncodeTask(const scoped_refptr<VideoFrame>& frame, bool force_keyframe); |
| 76 |
| 77 // Checks for and copies encoded output on |encoder_thread_|. |
| 78 void ProcessOutput(); |
| 79 |
| 80 // Inserts the output buffers for reuse on |encoder_thread_|. |
| 81 void UseOutputBitstreamBufferTask( |
| 82 std::unique_ptr<BitstreamBufferRef> buffer_ref); |
| 83 |
| 84 // Copies EncodeOutput into a BitstreamBuffer and returns it to the |client_|. |
| 85 void ReturnBitstreamBuffer( |
| 86 std::unique_ptr<EncodeOutput> encode_output, |
| 87 std::unique_ptr<MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef> |
| 88 buffer_ref); |
| 89 |
| 90 // Changes encode parameters on |encoder_thread_|. |
| 91 void RequestEncodingParametersChangeTask(uint32_t bitrate, |
| 92 uint32_t framerate); |
| 93 |
| 94 // Destroys encode session on |encoder_thread_|. |
| 95 void DestroyTask(); |
| 96 |
| 97 // Bitstream buffers ready to be used to return encoded output as a FIFO. |
| 98 std::deque<std::unique_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; |
| 99 |
| 100 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. |
| 101 std::deque<std::unique_ptr<EncodeOutput>> encoder_output_queue_; |
| 102 |
| 103 gfx::Size input_visible_size_; |
| 104 size_t bitstream_buffer_size_; |
| 105 int32_t frame_rate_; |
| 106 int32_t target_bitrate_; |
| 107 size_t u_plane_offset_; |
| 108 size_t v_plane_offset_; |
| 109 |
| 110 base::win::ScopedComPtr<IMFTransform> encoder_; |
| 111 base::win::ScopedComPtr<ICodecAPI> codec_api_; |
| 112 |
| 113 DWORD input_stream_count_min_; |
| 114 DWORD input_stream_count_max_; |
| 115 DWORD output_stream_count_min_; |
| 116 DWORD output_stream_count_max_; |
| 117 |
| 118 base::win::ScopedComPtr<IMFSample> input_sample_; |
| 119 base::win::ScopedComPtr<IMFSample> output_sample_; |
| 120 |
| 121 // To expose client callbacks from VideoEncodeAccelerator. |
| 122 // NOTE: all calls to this object *MUST* be executed on |client_task_runner_|. |
| 123 base::WeakPtr<Client> client_; |
| 124 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; |
| 125 |
| 126 // Our original calling task runner for the child thread. |
| 127 const scoped_refptr<base::SequencedTaskRunner> client_task_runner_; |
| 128 // Sequence checker to enforce that the methods of this object are called in |
| 129 // sequence. |
| 130 base::SequenceChecker sequence_checker_; |
| 131 |
| 132 // This thread services tasks posted from the VEA API entry points by the |
| 133 // GPU child thread and CompressionCallback() posted from device thread. |
| 134 base::Thread encoder_thread_; |
| 135 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; |
| 136 |
| 137 // Declared last to ensure that all weak pointers are invalidated before |
| 138 // other destructors run. |
| 139 base::WeakPtrFactory<MediaFoundationVideoEncodeAccelerator> |
| 140 encoder_task_weak_factory_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(MediaFoundationVideoEncodeAccelerator); |
| 143 }; |
| 144 |
| 145 } // namespace media |
| 146 |
| 147 #endif // MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ |
OLD | NEW |