Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 static std::vector<media::VideoEncodeAccelerator::SupportedProfile> | |
| 45 GetSupportedProfiles(); | |
| 46 | |
| 47 // UMA errors this class reports. | |
| 48 enum VAVEAEncoderFailure { | |
| 49 VAAPI_ERROR = 0, | |
| 50 VAVEA_ENCODER_FAILURES_MAX, | |
| 51 }; | |
| 52 | |
| 53 private: | |
| 54 enum { | |
| 55 // Need 2 surfaces for each frame: one for input data and one for | |
| 56 // reconstructed picture, which is later used for reference. | |
| 57 kMinSurfacesToEncode = 2, | |
| 58 | |
| 59 // Subjectively chosen. | |
| 60 kNumInputBuffers = 4, | |
| 61 kMaxNumReferenceFrames = 4, | |
| 62 | |
| 63 // We need up to kMaxNumReferenceFrames surfaces for reference, plus one | |
| 64 // for input and one for encode (which will be added to the set of reference | |
| 65 // frames for subsequent frames). Actual execution of HW encode is done | |
| 66 // in parallel, and we want to process more frames in the meantime. | |
| 67 // To have kNumInputBuffers in flight, we need a full set of reference + | |
| 68 // encode surfaces (i.e. kMaxNumReferenceFrames + kMinSurfacesToEncode), and | |
| 69 // (kNumInputBuffers - 1) of kMinSurfacesToEncode for the remaining frames | |
| 70 // in flight. | |
| 71 kNumSurfaces = kMaxNumReferenceFrames + kMinSurfacesToEncode + | |
| 72 kMinSurfacesToEncode * (kNumInputBuffers - 1), | |
|
wuchengli
2014/06/19 07:31:48
Is it simpler to use "kNumSurfaces = kMaxNumRefere
Pawel Osciak
2014/06/19 10:59:59
I felt this was easier to understand, because this
| |
| 73 | |
| 74 // An IDR every 128 frames, an I frame every 30 and no B frames. | |
| 75 kIDRPeriod = 128, | |
| 76 kIPeriod = 30, | |
| 77 kIPPeriod = 1, | |
| 78 | |
| 79 kDefaultFramerate = 30, | |
| 80 | |
| 81 // HRD parameters (ch. E.2.2 in spec). | |
| 82 kBitRateScale = 0, // bit_rate_scale for SPS HRD parameters. | |
| 83 kCPBSizeScale = 0, // cpb_size_scale for SPS HRD parameters. | |
| 84 | |
| 85 kDefaultQP = 26, | |
| 86 // All Intel codecs can do at least 4.1. | |
| 87 kDefaultLevelIDC = 41, | |
| 88 kChromaFormatIDC = 1, // 4:2:0 | |
| 89 | |
| 90 // Arbitrarily chosen bitrate window size for rate control, in ms. | |
| 91 kCPBWindowSizeMs = 1500, | |
| 92 }; | |
| 93 | |
| 94 // Reference picture list. | |
| 95 typedef std::list<scoped_refptr<VASurface> > RefPicList; | |
| 96 | |
| 97 // Encode job for one frame. Created when an input frame is awaiting and | |
| 98 // enough resources are available to proceed. Once the job is prepared and | |
| 99 // submitted to the hardware, it awaits on the submitted_encode_jobs_ queue | |
| 100 // for an output bitstream buffer to become available. Once one is ready, | |
| 101 // the encoded bytes are downloaded to it and job resources are released | |
| 102 // and become available for reuse. | |
| 103 struct EncodeJob { | |
| 104 // Input surface for video frame data. | |
| 105 scoped_refptr<VASurface> input_surface; | |
| 106 // Surface for a reconstructed picture, which is used for reference | |
| 107 // for subsequent frames. | |
| 108 scoped_refptr<VASurface> recon_surface; | |
| 109 // Buffer that will contain output bitstream for this frame. | |
| 110 VABufferID coded_buffer; | |
| 111 // Reference surfaces required to encode this picture. We keep references | |
| 112 // to them here, because we may discard some of them from ref_pic_list* | |
|
wuchengli
2014/06/19 07:31:48
You mean |ref_pic_list0_|?
Pawel Osciak
2014/06/19 10:59:59
No, this is intentional. I want to suggest that wh
| |
| 113 // before the HW job is done. | |
| 114 RefPicList reference_surfaces; | |
| 115 // True if this job will produce a keyframe. Used to report | |
| 116 // to BitstreamBufferReady(). | |
| 117 bool keyframe; | |
| 118 | |
| 119 EncodeJob(); | |
| 120 }; | |
| 121 | |
| 122 // Encoder state. | |
| 123 enum State { | |
| 124 kUninitialized, | |
| 125 kEncoding, | |
| 126 kError, | |
| 127 }; | |
| 128 | |
| 129 // Holds input frames coming from the client ready to be encoded. | |
| 130 struct InputFrameRef; | |
| 131 // Holds output buffers coming from the client ready to be filled. | |
| 132 struct BitstreamBufferRef; | |
| 133 | |
| 134 // Tasks for each of the VEA interface calls to be executed on the | |
| 135 // encoder thread. | |
| 136 void InitializeTask(); | |
| 137 void EncodeTask(const scoped_refptr<media::VideoFrame>& frame, | |
| 138 bool force_keyframe); | |
| 139 void UseOutputBitstreamBufferTask(scoped_ptr<BitstreamBufferRef> buffer_ref); | |
| 140 void RequestEncodingParametersChangeTask(uint32 bitrate, uint32 framerate); | |
| 141 void DestroyTask(); | |
| 142 | |
| 143 // Prepare and schedule an encode job if we have an input to encode | |
| 144 // and enough resources to proceed. | |
| 145 void EncodeFrameTask(); | |
| 146 | |
| 147 // Fill current_sps_/current_pps_ with current values. | |
| 148 void UpdateSPS(); | |
| 149 void UpdatePPS(); | |
| 150 void UpdateRates(uint32 bitrate, uint32 framerate); | |
| 151 | |
| 152 // Generate packed SPS and PPS in packed_sps_/packed_pps_, using | |
| 153 // values in current_sps_/current_pps_. | |
| 154 void GeneratePackedSPS(); | |
| 155 void GeneratePackedPPS(); | |
| 156 | |
| 157 // Check if we have sufficient resources for a new encode job, claim them and | |
| 158 // fill current_encode_job_ with them. | |
| 159 // Return false if we cannot start a new job yet, true otherwise. | |
| 160 bool PrepareNextJob(); | |
| 161 | |
| 162 // Begin a new frame, making it a keyframe if |force_keyframe| is true, | |
| 163 // updating current_pic_. | |
| 164 void BeginFrame(bool force_keyframe); | |
| 165 | |
| 166 // End current frame, updating reference picture lists and storing current | |
| 167 // job in the jobs awaiting completion on submitted_encode_jobs_. | |
| 168 void EndFrame(); | |
| 169 | |
| 170 // Submit parameters for the current frame to the hardware. | |
| 171 bool SubmitFrameParameters(); | |
| 172 // Submit keyframe headers to the hardware if the current frame is a keyframe. | |
| 173 bool SubmitHeadersIfNeeded(); | |
| 174 | |
| 175 // Upload image data from |frame| to the input surface for current job. | |
| 176 bool UploadFrame(const scoped_refptr<media::VideoFrame>& frame); | |
| 177 | |
| 178 // Execute encode in hardware. This does not block and will return before | |
| 179 // the job is finished. | |
| 180 bool ExecuteEncode(); | |
| 181 | |
| 182 // Callback that returns a no longer used VASurfaceID to | |
| 183 // available_va_surface_ids_ for reuse. | |
| 184 void RecycleVASurfaceID(VASurfaceID va_surface_id); | |
| 185 | |
| 186 // Tries to return a bitstream buffer if both a submitted job awaits to | |
| 187 // be completed and we have bitstream buffers from the client available | |
| 188 // to download the encoded data to. | |
| 189 void TryToReturnBitstreamBuffer(); | |
| 190 | |
| 191 // Puts the encoder into en error state and notifies client about the error. | |
| 192 void NotifyError(Error error); | |
| 193 | |
| 194 // Sets the encoder state on the correct thread. | |
| 195 void SetState(State state); | |
| 196 | |
| 197 // VaapiWrapper is the owner of all HW resources (surfaces and buffers) | |
| 198 // and will free them on destruction. | |
| 199 scoped_ptr<VaapiWrapper> vaapi_wrapper_; | |
| 200 | |
| 201 // Input profile and sizes. | |
| 202 media::VideoCodecProfile profile_; | |
| 203 gfx::Size visible_size_; | |
| 204 gfx::Size coded_size_; // Macroblock-aligned. | |
| 205 // Width/height in macroblocks. | |
| 206 unsigned int mb_width_; | |
| 207 unsigned int mb_height_; | |
| 208 | |
| 209 // Maximum size of the reference list 0. | |
| 210 unsigned int max_ref_idx_l0_size_; | |
| 211 | |
| 212 // Initial QP. | |
| 213 unsigned int qp_; | |
| 214 | |
| 215 // IDR frame period. | |
| 216 unsigned int idr_period_; | |
| 217 // I frame period. | |
| 218 unsigned int i_period_; | |
| 219 // IP period, i.e. how often do we need to have either an I or a P frame in | |
| 220 // the stream. Period of 1 means we can have no B frames. | |
| 221 unsigned int ip_period_; | |
| 222 | |
| 223 // Size in bytes required for input bitstream buffers. | |
| 224 size_t output_buffer_byte_size_; | |
| 225 | |
| 226 Display* x_display_; | |
| 227 | |
| 228 // All of the members below musty be accessed on the encoder_thread_, | |
| 229 // while it is running. | |
| 230 | |
| 231 // Encoder state. Encode tasks will only run in kEncoding state. | |
| 232 State state_; | |
| 233 | |
| 234 // frame_num to be used for the next frame. | |
| 235 unsigned int frame_num_; | |
| 236 // frame_num of the previous IDR. | |
| 237 unsigned int last_idr_frame_num_; | |
| 238 | |
| 239 // Current bitrate in bps. | |
| 240 unsigned int bitrate_; | |
| 241 // Current bitrate in fps. | |
| 242 unsigned int framerate_; | |
| 243 // CPB size in bits, i.e. bitrate in kbps * window size in ms/1000. | |
| 244 unsigned int cpb_size_; | |
| 245 // True if the parameters have changed and we need to submit a keyframe | |
| 246 // with updated parameters. | |
| 247 bool encoding_parameters_changed_; | |
| 248 | |
| 249 // Job currently being prepared for encode. | |
| 250 scoped_ptr<EncodeJob> current_encode_job_; | |
| 251 | |
| 252 // Current SPS, PPS and their packed versions. Packed versions are their NALUs | |
| 253 // in AnnexB format *without* emulation prevention three-byte sequences | |
| 254 // (those will be added by the driver). | |
| 255 media::H264SPS current_sps_; | |
| 256 media::H264BitstreamBuffer packed_sps_; | |
| 257 media::H264PPS current_pps_; | |
| 258 media::H264BitstreamBuffer packed_pps_; | |
| 259 | |
| 260 // Picture currently being prepared for encode. | |
| 261 H264Picture current_pic_; | |
| 262 | |
| 263 // VA surfaces available for reuse. | |
| 264 std::vector<VASurfaceID> available_va_surface_ids_; | |
| 265 | |
| 266 // VA buffers for coded frames. | |
| 267 std::vector<VABufferID> available_va_buffer_ids_; | |
| 268 | |
| 269 // Currently active reference surfaces. | |
| 270 RefPicList ref_pic_list0_; | |
| 271 | |
| 272 // Callback via which finished VA surfaces are returned to us. | |
| 273 VASurface::ReleaseCB va_surface_release_cb_; | |
| 274 | |
| 275 // VideoFrames passed from the client, waiting to be encoded. | |
| 276 std::queue<linked_ptr<InputFrameRef> > encoder_input_queue_; | |
| 277 | |
| 278 // BitstreamBuffers mapped, ready to be filled. | |
| 279 std::queue<linked_ptr<BitstreamBufferRef> > available_bitstream_buffers_; | |
| 280 | |
| 281 // Jobs submitted for encode, awaiting bitstream buffers to become available. | |
| 282 std::queue<linked_ptr<EncodeJob> > submitted_encode_jobs_; | |
| 283 | |
| 284 // Encoder thread. All tasks are executed on it. | |
| 285 base::Thread encoder_thread_; | |
| 286 scoped_refptr<base::MessageLoopProxy> encoder_thread_proxy_; | |
| 287 | |
| 288 const scoped_refptr<base::MessageLoopProxy> child_message_loop_proxy_; | |
| 289 | |
| 290 // To expose client callbacks from VideoEncodeAccelerator. | |
| 291 // NOTE: all calls to these objects *MUST* be executed on | |
| 292 // child_message_loop_proxy_. | |
| 293 scoped_ptr<base::WeakPtrFactory<Client> > client_ptr_factory_; | |
| 294 base::WeakPtr<Client> client_; | |
| 295 | |
| 296 // WeakPtr to post from the encoder thread back to the ChildThread, as it may | |
| 297 // outlive this. Posting from the ChildThread using base::Unretained(this) | |
| 298 // to the encoder thread is safe, because this always outlives the encoder | |
|
wuchengli
2014/06/18 15:57:44
s/this/|this|/
Pawel Osciak
2014/06/19 01:31:11
Done.
| |
| 299 // thread (it's a member of this class). | |
| 300 base::WeakPtr<VaapiVideoEncodeAccelerator> weak_this_; | |
| 301 base::WeakPtrFactory<VaapiVideoEncodeAccelerator> weak_this_ptr_factory_; | |
| 302 | |
| 303 DISALLOW_COPY_AND_ASSIGN(VaapiVideoEncodeAccelerator); | |
| 304 }; | |
| 305 | |
| 306 } // namespace content | |
| 307 | |
| 308 #endif // CONTENT_COMMON_GPU_MEDIA_VAAPI_VIDEO_ENCODE_ACCELERATOR_H_ | |
| OLD | NEW |