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

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

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

Powered by Google App Engine
This is Rietveld 408576698