OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_ENCODE_ACCELERATOR_H_ | |
6 #define CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_ENCODE_ACCELERATOR_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <list> | |
12 #include <memory> | |
13 #include <queue> | |
14 | |
15 #include "base/macros.h" | |
16 #include "base/memory/linked_ptr.h" | |
17 #include "base/threading/thread.h" | |
18 #include "content/common/content_export.h" | |
19 #include "content/common/gpu/media/h264_dpb.h" | |
20 #include "content/common/gpu/media/va_surface.h" | |
21 #include "content/common/gpu/media/vaapi_wrapper.h" | |
22 #include "media/filters/h264_bitstream_buffer.h" | |
23 #include "media/video/video_encode_accelerator.h" | |
24 | |
25 namespace content { | |
26 | |
27 // A VideoEncodeAccelerator implementation that uses VA-API | |
28 // (http://www.freedesktop.org/wiki/Software/vaapi) for HW-accelerated | |
29 // video encode. | |
30 class CONTENT_EXPORT VaapiVideoEncodeAccelerator | |
31 : public media::VideoEncodeAccelerator { | |
32 public: | |
33 VaapiVideoEncodeAccelerator(); | |
34 ~VaapiVideoEncodeAccelerator() override; | |
35 | |
36 // media::VideoEncodeAccelerator implementation. | |
37 media::VideoEncodeAccelerator::SupportedProfiles GetSupportedProfiles() | |
38 override; | |
39 bool Initialize(media::VideoPixelFormat format, | |
40 const gfx::Size& input_visible_size, | |
41 media::VideoCodecProfile output_profile, | |
42 uint32_t initial_bitrate, | |
43 Client* client) override; | |
44 void Encode(const scoped_refptr<media::VideoFrame>& frame, | |
45 bool force_keyframe) override; | |
46 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer) override; | |
47 void RequestEncodingParametersChange(uint32_t bitrate, | |
48 uint32_t framerate) override; | |
49 void Destroy() override; | |
50 | |
51 private: | |
52 // Reference picture list. | |
53 typedef std::list<scoped_refptr<VASurface> > RefPicList; | |
54 | |
55 // Encode job for one frame. Created when an input frame is awaiting and | |
56 // enough resources are available to proceed. Once the job is prepared and | |
57 // submitted to the hardware, it awaits on the submitted_encode_jobs_ queue | |
58 // for an output bitstream buffer to become available. Once one is ready, | |
59 // the encoded bytes are downloaded to it and job resources are released | |
60 // and become available for reuse. | |
61 struct EncodeJob { | |
62 // Input surface for video frame data. | |
63 scoped_refptr<VASurface> input_surface; | |
64 // Surface for a reconstructed picture, which is used for reference | |
65 // for subsequent frames. | |
66 scoped_refptr<VASurface> recon_surface; | |
67 // Buffer that will contain output bitstream for this frame. | |
68 VABufferID coded_buffer; | |
69 // Reference surfaces required to encode this picture. We keep references | |
70 // to them here, because we may discard some of them from ref_pic_list* | |
71 // before the HW job is done. | |
72 RefPicList reference_surfaces; | |
73 // True if this job will produce a keyframe. Used to report | |
74 // to BitstreamBufferReady(). | |
75 bool keyframe; | |
76 | |
77 EncodeJob(); | |
78 ~EncodeJob(); | |
79 }; | |
80 | |
81 // Encoder state. | |
82 enum State { | |
83 kUninitialized, | |
84 kEncoding, | |
85 kError, | |
86 }; | |
87 | |
88 // Holds input frames coming from the client ready to be encoded. | |
89 struct InputFrameRef; | |
90 // Holds output buffers coming from the client ready to be filled. | |
91 struct BitstreamBufferRef; | |
92 | |
93 // Tasks for each of the VEA interface calls to be executed on the | |
94 // encoder thread. | |
95 void InitializeTask(); | |
96 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame, | |
97 bool force_keyframe); | |
98 void UseOutputBitstreamBufferTask( | |
99 std::unique_ptr<BitstreamBufferRef> buffer_ref); | |
100 void RequestEncodingParametersChangeTask(uint32_t bitrate, | |
101 uint32_t framerate); | |
102 void DestroyTask(); | |
103 | |
104 // Prepare and schedule an encode job if we have an input to encode | |
105 // and enough resources to proceed. | |
106 void EncodeFrameTask(); | |
107 | |
108 // Fill current_sps_/current_pps_ with current values. | |
109 void UpdateSPS(); | |
110 void UpdatePPS(); | |
111 void UpdateRates(uint32_t bitrate, uint32_t framerate); | |
112 | |
113 // Generate packed SPS and PPS in packed_sps_/packed_pps_, using | |
114 // values in current_sps_/current_pps_. | |
115 void GeneratePackedSPS(); | |
116 void GeneratePackedPPS(); | |
117 | |
118 // Check if we have sufficient resources for a new encode job, claim them and | |
119 // fill current_encode_job_ with them. | |
120 // Return false if we cannot start a new job yet, true otherwise. | |
121 bool PrepareNextJob(); | |
122 | |
123 // Begin a new frame, making it a keyframe if |force_keyframe| is true, | |
124 // updating current_pic_. | |
125 void BeginFrame(bool force_keyframe); | |
126 | |
127 // End current frame, updating reference picture lists and storing current | |
128 // job in the jobs awaiting completion on submitted_encode_jobs_. | |
129 void EndFrame(); | |
130 | |
131 // Submit parameters for the current frame to the hardware. | |
132 bool SubmitFrameParameters(); | |
133 // Submit keyframe headers to the hardware if the current frame is a keyframe. | |
134 bool SubmitHeadersIfNeeded(); | |
135 | |
136 // Upload image data from |frame| to the input surface for current job. | |
137 bool UploadFrame(const scoped_refptr<media::VideoFrame>& frame); | |
138 | |
139 // Execute encode in hardware. This does not block and will return before | |
140 // the job is finished. | |
141 bool ExecuteEncode(); | |
142 | |
143 // Callback that returns a no longer used VASurfaceID to | |
144 // available_va_surface_ids_ for reuse. | |
145 void RecycleVASurfaceID(VASurfaceID va_surface_id); | |
146 | |
147 // Tries to return a bitstream buffer if both a submitted job awaits to | |
148 // be completed and we have bitstream buffers from the client available | |
149 // to download the encoded data to. | |
150 void TryToReturnBitstreamBuffer(); | |
151 | |
152 // Puts the encoder into en error state and notifies client about the error. | |
153 void NotifyError(Error error); | |
154 | |
155 // Sets the encoder state on the correct thread. | |
156 void SetState(State state); | |
157 | |
158 // VaapiWrapper is the owner of all HW resources (surfaces and buffers) | |
159 // and will free them on destruction. | |
160 scoped_refptr<VaapiWrapper> vaapi_wrapper_; | |
161 | |
162 // Input profile and sizes. | |
163 media::VideoCodecProfile profile_; | |
164 gfx::Size visible_size_; | |
165 gfx::Size coded_size_; // Macroblock-aligned. | |
166 // Width/height in macroblocks. | |
167 unsigned int mb_width_; | |
168 unsigned int mb_height_; | |
169 | |
170 // Maximum size of the reference list 0. | |
171 unsigned int max_ref_idx_l0_size_; | |
172 | |
173 // Initial QP. | |
174 unsigned int qp_; | |
175 | |
176 // IDR frame period. | |
177 unsigned int idr_period_; | |
178 // I frame period. | |
179 unsigned int i_period_; | |
180 // IP period, i.e. how often do we need to have either an I or a P frame in | |
181 // the stream. Period of 1 means we can have no B frames. | |
182 unsigned int ip_period_; | |
183 | |
184 // Size in bytes required for input bitstream buffers. | |
185 size_t output_buffer_byte_size_; | |
186 | |
187 // All of the members below must be accessed on the encoder_thread_, | |
188 // while it is running. | |
189 | |
190 // Encoder state. Encode tasks will only run in kEncoding state. | |
191 State state_; | |
192 | |
193 // frame_num to be used for the next frame. | |
194 unsigned int frame_num_; | |
195 // idr_pic_id to be used for the next frame. | |
196 unsigned int idr_pic_id_; | |
197 | |
198 // Current bitrate in bps. | |
199 unsigned int bitrate_; | |
200 // Current fps. | |
201 unsigned int framerate_; | |
202 // CPB size in bits, i.e. bitrate in kbps * window size in ms/1000. | |
203 unsigned int cpb_size_; | |
204 // True if the parameters have changed and we need to submit a keyframe | |
205 // with updated parameters. | |
206 bool encoding_parameters_changed_; | |
207 | |
208 // Job currently being prepared for encode. | |
209 std::unique_ptr<EncodeJob> current_encode_job_; | |
210 | |
211 // Current SPS, PPS and their packed versions. Packed versions are their NALUs | |
212 // in AnnexB format *without* emulation prevention three-byte sequences | |
213 // (those will be added by the driver). | |
214 media::H264SPS current_sps_; | |
215 media::H264BitstreamBuffer packed_sps_; | |
216 media::H264PPS current_pps_; | |
217 media::H264BitstreamBuffer packed_pps_; | |
218 | |
219 // Picture currently being prepared for encode. | |
220 scoped_refptr<H264Picture> current_pic_; | |
221 | |
222 // VA surfaces available for reuse. | |
223 std::vector<VASurfaceID> available_va_surface_ids_; | |
224 | |
225 // VA buffers for coded frames. | |
226 std::vector<VABufferID> available_va_buffer_ids_; | |
227 | |
228 // Currently active reference surfaces. | |
229 RefPicList ref_pic_list0_; | |
230 | |
231 // Callback via which finished VA surfaces are returned to us. | |
232 VASurface::ReleaseCB va_surface_release_cb_; | |
233 | |
234 // VideoFrames passed from the client, waiting to be encoded. | |
235 std::queue<linked_ptr<InputFrameRef> > encoder_input_queue_; | |
236 | |
237 // BitstreamBuffers mapped, ready to be filled. | |
238 std::queue<linked_ptr<BitstreamBufferRef> > available_bitstream_buffers_; | |
239 | |
240 // Jobs submitted for encode, awaiting bitstream buffers to become available. | |
241 std::queue<linked_ptr<EncodeJob> > submitted_encode_jobs_; | |
242 | |
243 // Encoder thread. All tasks are executed on it. | |
244 base::Thread encoder_thread_; | |
245 scoped_refptr<base::SingleThreadTaskRunner> encoder_thread_task_runner_; | |
246 | |
247 const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_; | |
248 | |
249 // To expose client callbacks from VideoEncodeAccelerator. | |
250 // NOTE: all calls to these objects *MUST* be executed on | |
251 // child_task_runner_. | |
252 std::unique_ptr<base::WeakPtrFactory<Client>> client_ptr_factory_; | |
253 base::WeakPtr<Client> client_; | |
254 | |
255 // WeakPtr to post from the encoder thread back to the ChildThread, as it may | |
256 // outlive this. Posting from the ChildThread using base::Unretained(this) | |
257 // to the encoder thread is safe, because |this| always outlives the encoder | |
258 // thread (it's a member of this class). | |
259 base::WeakPtr<VaapiVideoEncodeAccelerator> weak_this_; | |
260 base::WeakPtrFactory<VaapiVideoEncodeAccelerator> weak_this_ptr_factory_; | |
261 | |
262 DISALLOW_COPY_AND_ASSIGN(VaapiVideoEncodeAccelerator); | |
263 }; | |
264 | |
265 } // namespace content | |
266 | |
267 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_ENCODE_ACCELERATOR_H_ | |
OLD | NEW |