Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1559)

Side by Side Diff: content/common/gpu/media/vaapi_video_encode_accelerator.h

Issue 333253002: Add VaapiVideoEncodeAccelerator for HW-accelerated video encode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // UMA errors this class reports.
49 enum VAVEAEncoderFailure {
50 VAAPI_ERROR = 0,
51 VAVEA_ENCODER_FAILURES_MAX,
52 };
Ilya Sherman 2014/06/20 20:04:30 Can this be defined in the anonymous namespace ins
Pawel Osciak 2014/06/20 22:28:11 Sure, just curious why would that be preferred tho
Ilya Sherman 2014/06/20 22:33:30 In general, if you don't need to expose something
Pawel Osciak 2014/06/20 22:42:44 Ah of course, good point. Thanks. Moved.
53
54 private:
55 // Reference picture list.
56 typedef std::list<scoped_refptr<VASurface> > RefPicList;
57
58 // Encode job for one frame. Created when an input frame is awaiting and
59 // enough resources are available to proceed. Once the job is prepared and
60 // submitted to the hardware, it awaits on the submitted_encode_jobs_ queue
61 // for an output bitstream buffer to become available. Once one is ready,
62 // the encoded bytes are downloaded to it and job resources are released
63 // and become available for reuse.
64 struct EncodeJob {
65 // Input surface for video frame data.
66 scoped_refptr<VASurface> input_surface;
67 // Surface for a reconstructed picture, which is used for reference
68 // for subsequent frames.
69 scoped_refptr<VASurface> recon_surface;
70 // Buffer that will contain output bitstream for this frame.
71 VABufferID coded_buffer;
72 // Reference surfaces required to encode this picture. We keep references
73 // to them here, because we may discard some of them from ref_pic_list*
74 // before the HW job is done.
75 RefPicList reference_surfaces;
76 // True if this job will produce a keyframe. Used to report
77 // to BitstreamBufferReady().
78 bool keyframe;
79
80 EncodeJob();
81 };
82
83 // Encoder state.
84 enum State {
85 kUninitialized,
86 kEncoding,
87 kError,
88 };
89
90 // Holds input frames coming from the client ready to be encoded.
91 struct InputFrameRef;
92 // Holds output buffers coming from the client ready to be filled.
93 struct BitstreamBufferRef;
94
95 // Tasks for each of the VEA interface calls to be executed on the
96 // encoder thread.
97 void InitializeTask();
98 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame,
99 bool force_keyframe);
100 void UseOutputBitstreamBufferTask(scoped_ptr<BitstreamBufferRef> buffer_ref);
101 void RequestEncodingParametersChangeTask(uint32 bitrate, uint32 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 bitrate, uint32 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_ptr<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 Display* x_display_;
188
189 // All of the members below must be accessed on the encoder_thread_,
190 // while it is running.
191
192 // Encoder state. Encode tasks will only run in kEncoding state.
193 State state_;
194
195 // frame_num to be used for the next frame.
196 unsigned int frame_num_;
197 // frame_num of the previous IDR.
198 unsigned int last_idr_frame_num_;
199
200 // Current bitrate in bps.
201 unsigned int bitrate_;
202 // Current fps.
203 unsigned int framerate_;
204 // CPB size in bits, i.e. bitrate in kbps * window size in ms/1000.
205 unsigned int cpb_size_;
206 // True if the parameters have changed and we need to submit a keyframe
207 // with updated parameters.
208 bool encoding_parameters_changed_;
209
210 // Job currently being prepared for encode.
211 scoped_ptr<EncodeJob> current_encode_job_;
212
213 // Current SPS, PPS and their packed versions. Packed versions are their NALUs
214 // in AnnexB format *without* emulation prevention three-byte sequences
215 // (those will be added by the driver).
216 media::H264SPS current_sps_;
217 media::H264BitstreamBuffer packed_sps_;
218 media::H264PPS current_pps_;
219 media::H264BitstreamBuffer packed_pps_;
220
221 // Picture currently being prepared for encode.
222 H264Picture current_pic_;
223
224 // VA surfaces available for reuse.
225 std::vector<VASurfaceID> available_va_surface_ids_;
226
227 // VA buffers for coded frames.
228 std::vector<VABufferID> available_va_buffer_ids_;
229
230 // Currently active reference surfaces.
231 RefPicList ref_pic_list0_;
232
233 // Callback via which finished VA surfaces are returned to us.
234 VASurface::ReleaseCB va_surface_release_cb_;
235
236 // VideoFrames passed from the client, waiting to be encoded.
237 std::queue<linked_ptr<InputFrameRef> > encoder_input_queue_;
238
239 // BitstreamBuffers mapped, ready to be filled.
240 std::queue<linked_ptr<BitstreamBufferRef> > available_bitstream_buffers_;
241
242 // Jobs submitted for encode, awaiting bitstream buffers to become available.
243 std::queue<linked_ptr<EncodeJob> > submitted_encode_jobs_;
244
245 // Encoder thread. All tasks are executed on it.
246 base::Thread encoder_thread_;
247 scoped_refptr<base::MessageLoopProxy> encoder_thread_proxy_;
248
249 const scoped_refptr<base::MessageLoopProxy> child_message_loop_proxy_;
250
251 // To expose client callbacks from VideoEncodeAccelerator.
252 // NOTE: all calls to these objects *MUST* be executed on
253 // child_message_loop_proxy_.
254 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_;
255 base::WeakPtr<Client> client_;
256
257 // WeakPtr to post from the encoder thread back to the ChildThread, as it may
258 // outlive this. Posting from the ChildThread using base::Unretained(this)
259 // to the encoder thread is safe, because |this| always outlives the encoder
260 // thread (it's a member of this class).
261 base::WeakPtr<VaapiVideoEncodeAccelerator> weak_this_;
262 base::WeakPtrFactory<VaapiVideoEncodeAccelerator> weak_this_ptr_factory_;
263
264 DISALLOW_COPY_AND_ASSIGN(VaapiVideoEncodeAccelerator);
265 };
266
267 } // namespace content
268
269 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_ENCODE_ACCELERATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698