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