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/single_thread_task_runner.h" |
| 18 #include "base/threading/thread_checker.h" |
| 19 #include "base/win/scoped_comptr.h" |
| 20 #include "media/gpu/media_gpu_export.h" |
| 21 #include "media/video/video_encode_accelerator.h" |
| 22 |
| 23 namespace media { |
| 24 |
| 25 // Media Foundation implementation of the VideoEncodeAccelerator interface for |
| 26 // Windows. |
| 27 // This class is pinned to the thread on which it is constructed and DCHECKs |
| 28 // that it is never accessed from any other. It starts an encoder thread on |
| 29 // which VideoEncodeAccelerator implementation tasks are posted. |
| 30 class MEDIA_GPU_EXPORT MediaFoundationVideoEncodeAccelerator |
| 31 : public VideoEncodeAccelerator { |
| 32 public: |
| 33 MediaFoundationVideoEncodeAccelerator(); |
| 34 ~MediaFoundationVideoEncodeAccelerator() override; |
| 35 |
| 36 // VideoEncodeAccelerator implementation. |
| 37 VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() override; |
| 38 bool Initialize(VideoPixelFormat input_format, |
| 39 const gfx::Size& input_visible_size, |
| 40 VideoCodecProfile output_profile, |
| 41 uint32_t initial_bitrate, |
| 42 Client* client) override; |
| 43 void Encode(const scoped_refptr<VideoFrame>& frame, |
| 44 bool force_keyframe) override; |
| 45 void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override; |
| 46 void RequestEncodingParametersChange(uint32_t bitrate, |
| 47 uint32_t framerate) override; |
| 48 void Destroy() override; |
| 49 |
| 50 // Preload dlls required for encoding. |
| 51 static void PreSandboxInitialization(); |
| 52 |
| 53 private: |
| 54 // Holds output buffers coming from the client ready to be filled. |
| 55 struct BitstreamBufferRef; |
| 56 |
| 57 // Holds output buffers coming from the encoder. |
| 58 class EncodeOutput; |
| 59 |
| 60 // Initializes and allocates memory for input and output samples. |
| 61 bool InitializeInputOutputSamples(); |
| 62 |
| 63 // Initializes encoder parameters for real-time use. |
| 64 bool SetEncoderModes(); |
| 65 |
| 66 // Encoding tasks to be run on |encoder_thread_|. |
| 67 void EncodeTask(const scoped_refptr<VideoFrame>& frame, bool force_keyframe); |
| 68 |
| 69 // Checks for and copies encoded output on |encoder_thread_|. |
| 70 void ProcessOutput(); |
| 71 |
| 72 // Inserts the output buffers for reuse on |encoder_thread_|. |
| 73 void UseOutputBitstreamBufferTask( |
| 74 std::unique_ptr<BitstreamBufferRef> buffer_ref); |
| 75 |
| 76 // Copies EncodeOutput into a BitstreamBuffer and returns it to the |client_|. |
| 77 void ReturnBitstreamBuffer( |
| 78 std::unique_ptr<EncodeOutput> encode_output, |
| 79 std::unique_ptr<MediaFoundationVideoEncodeAccelerator::BitstreamBufferRef> |
| 80 buffer_ref); |
| 81 |
| 82 // Changes encode parameters on |encoder_thread_|. |
| 83 void RequestEncodingParametersChangeTask(uint32_t bitrate, |
| 84 uint32_t framerate); |
| 85 |
| 86 // Destroys encode session on |encoder_thread_|. |
| 87 void DestroyTask(); |
| 88 |
| 89 // Bitstream buffers ready to be used to return encoded output as a FIFO. |
| 90 std::deque<std::unique_ptr<BitstreamBufferRef>> bitstream_buffer_queue_; |
| 91 |
| 92 // EncodeOutput needs to be copied into a BitstreamBufferRef as a FIFO. |
| 93 std::deque<std::unique_ptr<EncodeOutput>> encoder_output_queue_; |
| 94 |
| 95 gfx::Size input_visible_size_; |
| 96 size_t bitstream_buffer_size_; |
| 97 int32_t frame_rate_; |
| 98 int32_t target_bitrate_; |
| 99 size_t u_plane_offset_; |
| 100 size_t v_plane_offset_; |
| 101 |
| 102 // Our original calling task runner for the child thread. |
| 103 const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_; |
| 104 |
| 105 // To expose client callbacks from VideoEncodeAccelerator. |
| 106 // NOTE: all calls to this object *MUST* be executed on |client_task_runner_|. |
| 107 base::WeakPtr<Client> client_; |
| 108 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; |
| 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 // Thread checker to enforce that this object is used on a specific thread. |
| 122 // It is pinned on |client_task_runner_| thread. |
| 123 base::ThreadChecker thread_checker_; |
| 124 |
| 125 // This thread services tasks posted from the VEA API entry points by the |
| 126 // GPU child thread and CompressionCallback() posted from device thread. |
| 127 base::Thread encoder_thread_; |
| 128 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; |
| 129 |
| 130 // Declared last to ensure that all weak pointers are invalidated before |
| 131 // other destructors run. |
| 132 base::WeakPtrFactory<MediaFoundationVideoEncodeAccelerator> |
| 133 encoder_task_weak_factory_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(MediaFoundationVideoEncodeAccelerator); |
| 136 }; |
| 137 |
| 138 } // namespace media |
| 139 |
| 140 #endif // MEDIA_GPU_MEDIA_FOUNDATION_VIDEO_ENCODE_ACCELERATOR_WIN_H_ |
OLD | NEW |