[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_video_encode_accelerator.cc
blobcdd03a156fb5194eeef7ed0f4b786315b37dd5c2
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.
5 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/metrics/histogram.h"
10 #include "base/numerics/safe_conversions.h"
11 #include "content/common/gpu/media/h264_dpb.h"
12 #include "media/base/bind_to_current_loop.h"
13 #include "third_party/libva/va/va_enc_h264.h"
15 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): "
17 #define NOTIFY_ERROR(error, msg) \
18 do { \
19 SetState(kError); \
20 LOG(ERROR) << msg; \
21 LOG(ERROR) << "Calling NotifyError(" << error << ")";\
22 NotifyError(error); \
23 } while (0)
25 namespace content {
27 namespace {
28 // Need 2 surfaces for each frame: one for input data and one for
29 // reconstructed picture, which is later used for reference.
30 const size_t kMinSurfacesToEncode = 2;
32 // Subjectively chosen.
33 const size_t kNumInputBuffers = 4;
34 const size_t kMaxNumReferenceFrames = 4;
36 // We need up to kMaxNumReferenceFrames surfaces for reference, plus one
37 // for input and one for encode (which will be added to the set of reference
38 // frames for subsequent frames). Actual execution of HW encode is done
39 // in parallel, and we want to process more frames in the meantime.
40 // To have kNumInputBuffers in flight, we need a full set of reference +
41 // encode surfaces (i.e. kMaxNumReferenceFrames + kMinSurfacesToEncode), and
42 // (kNumInputBuffers - 1) of kMinSurfacesToEncode for the remaining frames
43 // in flight.
44 const size_t kNumSurfaces = kMaxNumReferenceFrames + kMinSurfacesToEncode +
45 kMinSurfacesToEncode * (kNumInputBuffers - 1);
47 // An IDR every 2048 frames, an I frame every 256 and no B frames.
48 // We choose IDR period to equal MaxFrameNum so it must be a power of 2.
49 const int kIDRPeriod = 2048;
50 const int kIPeriod = 256;
51 const int kIPPeriod = 1;
53 const int kDefaultFramerate = 30;
55 // HRD parameters (ch. E.2.2 in spec).
56 const int kBitRateScale = 0; // bit_rate_scale for SPS HRD parameters.
57 const int kCPBSizeScale = 0; // cpb_size_scale for SPS HRD parameters.
59 const int kDefaultQP = 26;
60 // All Intel codecs can do at least 4.1.
61 const int kDefaultLevelIDC = 41;
62 const int kChromaFormatIDC = 1; // 4:2:0
64 // Arbitrarily chosen bitrate window size for rate control, in ms.
65 const int kCPBWindowSizeMs = 1500;
67 // UMA errors that the VaapiVideoEncodeAccelerator class reports.
68 enum VAVEAEncoderFailure {
69 VAAPI_ERROR = 0,
70 VAVEA_ENCODER_FAILURES_MAX,
75 // Round |value| up to |alignment|, which must be a power of 2.
76 static inline size_t RoundUpToPowerOf2(size_t value, size_t alignment) {
77 // Check that |alignment| is a power of 2.
78 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1)));
79 return ((value + (alignment - 1)) & ~(alignment - 1));
82 static void ReportToUMA(VAVEAEncoderFailure failure) {
83 UMA_HISTOGRAM_ENUMERATION(
84 "Media.VAVEA.EncoderFailure",
85 failure,
86 VAVEA_ENCODER_FAILURES_MAX);
89 struct VaapiVideoEncodeAccelerator::InputFrameRef {
90 InputFrameRef(const scoped_refptr<media::VideoFrame>& frame,
91 bool force_keyframe)
92 : frame(frame), force_keyframe(force_keyframe) {}
93 const scoped_refptr<media::VideoFrame> frame;
94 const bool force_keyframe;
97 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef {
98 BitstreamBufferRef(int32 id, scoped_ptr<base::SharedMemory> shm, size_t size)
99 : id(id), shm(shm.Pass()), size(size) {}
100 const int32 id;
101 const scoped_ptr<base::SharedMemory> shm;
102 const size_t size;
105 media::VideoEncodeAccelerator::SupportedProfiles
106 VaapiVideoEncodeAccelerator::GetSupportedProfiles() {
107 return VaapiWrapper::GetSupportedEncodeProfiles();
110 static unsigned int Log2OfPowerOf2(unsigned int x) {
111 CHECK_GT(x, 0u);
112 DCHECK_EQ(x & (x - 1), 0u);
114 int log = 0;
115 while (x > 1) {
116 x >>= 1;
117 ++log;
119 return log;
122 VaapiVideoEncodeAccelerator::VaapiVideoEncodeAccelerator()
123 : profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN),
124 mb_width_(0),
125 mb_height_(0),
126 output_buffer_byte_size_(0),
127 state_(kUninitialized),
128 frame_num_(0),
129 idr_pic_id_(0),
130 bitrate_(0),
131 framerate_(0),
132 cpb_size_(0),
133 encoding_parameters_changed_(false),
134 encoder_thread_("VAVEAEncoderThread"),
135 child_task_runner_(base::ThreadTaskRunnerHandle::Get()),
136 weak_this_ptr_factory_(this) {
137 DVLOGF(4);
138 weak_this_ = weak_this_ptr_factory_.GetWeakPtr();
140 max_ref_idx_l0_size_ = kMaxNumReferenceFrames;
141 qp_ = kDefaultQP;
142 idr_period_ = kIDRPeriod;
143 i_period_ = kIPeriod;
144 ip_period_ = kIPPeriod;
147 VaapiVideoEncodeAccelerator::~VaapiVideoEncodeAccelerator() {
148 DVLOGF(4);
149 DCHECK(child_task_runner_->BelongsToCurrentThread());
150 DCHECK(!encoder_thread_.IsRunning());
153 bool VaapiVideoEncodeAccelerator::Initialize(
154 media::VideoFrame::Format format,
155 const gfx::Size& input_visible_size,
156 media::VideoCodecProfile output_profile,
157 uint32 initial_bitrate,
158 Client* client) {
159 DCHECK(child_task_runner_->BelongsToCurrentThread());
160 DCHECK(!encoder_thread_.IsRunning());
161 DCHECK_EQ(state_, kUninitialized);
163 DVLOGF(1) << "Initializing VAVEA, input_format: "
164 << media::VideoFrame::FormatToString(format)
165 << ", input_visible_size: " << input_visible_size.ToString()
166 << ", output_profile: " << output_profile
167 << ", initial_bitrate: " << initial_bitrate;
169 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
170 client_ = client_ptr_factory_->GetWeakPtr();
172 if (output_profile < media::H264PROFILE_BASELINE ||
173 output_profile > media::H264PROFILE_MAIN) {
174 DVLOGF(1) << "Unsupported output profile: " << output_profile;
175 return false;
178 if (format != media::VideoFrame::I420) {
179 DVLOGF(1) << "Unsupported input format: "
180 << media::VideoFrame::FormatToString(format);
181 return false;
184 profile_ = output_profile;
185 visible_size_ = input_visible_size;
186 // 4:2:0 format has to be 2-aligned.
187 DCHECK_EQ(visible_size_.width() % 2, 0);
188 DCHECK_EQ(visible_size_.height() % 2, 0);
189 coded_size_ = gfx::Size(RoundUpToPowerOf2(visible_size_.width(), 16),
190 RoundUpToPowerOf2(visible_size_.height(), 16));
191 mb_width_ = coded_size_.width() / 16;
192 mb_height_ = coded_size_.height() / 16;
193 output_buffer_byte_size_ = coded_size_.GetArea();
195 UpdateRates(initial_bitrate, kDefaultFramerate);
197 vaapi_wrapper_ =
198 VaapiWrapper::CreateForVideoCodec(VaapiWrapper::kEncode, output_profile,
199 base::Bind(&ReportToUMA, VAAPI_ERROR));
200 if (!vaapi_wrapper_.get()) {
201 DVLOGF(1) << "Failed initializing VAAPI for profile " << output_profile;
202 return false;
205 if (!encoder_thread_.Start()) {
206 LOG(ERROR) << "Failed to start encoder thread";
207 return false;
209 encoder_thread_task_runner_ = encoder_thread_.task_runner();
211 // Finish the remaining initialization on the encoder thread.
212 encoder_thread_task_runner_->PostTask(
213 FROM_HERE, base::Bind(&VaapiVideoEncodeAccelerator::InitializeTask,
214 base::Unretained(this)));
216 return true;
219 void VaapiVideoEncodeAccelerator::InitializeTask() {
220 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
221 DCHECK_EQ(state_, kUninitialized);
222 DVLOGF(4);
224 va_surface_release_cb_ = media::BindToCurrentLoop(
225 base::Bind(&VaapiVideoEncodeAccelerator::RecycleVASurfaceID,
226 base::Unretained(this)));
228 if (!vaapi_wrapper_->CreateSurfaces(
229 coded_size_, kNumSurfaces, &available_va_surface_ids_)) {
230 NOTIFY_ERROR(kPlatformFailureError, "Failed creating VASurfaces");
231 return;
234 UpdateSPS();
235 GeneratePackedSPS();
237 UpdatePPS();
238 GeneratePackedPPS();
240 child_task_runner_->PostTask(
241 FROM_HERE,
242 base::Bind(&Client::RequireBitstreamBuffers, client_, kNumInputBuffers,
243 coded_size_, output_buffer_byte_size_));
245 SetState(kEncoding);
248 void VaapiVideoEncodeAccelerator::RecycleVASurfaceID(
249 VASurfaceID va_surface_id) {
250 DVLOGF(4) << "va_surface_id: " << va_surface_id;
251 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
253 available_va_surface_ids_.push_back(va_surface_id);
254 EncodeFrameTask();
257 void VaapiVideoEncodeAccelerator::BeginFrame(bool force_keyframe) {
258 current_pic_ = new H264Picture();
260 // If the current picture is an IDR picture, frame_num shall be equal to 0.
261 if (force_keyframe)
262 frame_num_ = 0;
264 current_pic_->frame_num = frame_num_++;
265 frame_num_ %= idr_period_;
267 if (current_pic_->frame_num == 0) {
268 current_pic_->idr = true;
269 // H264 spec mandates idr_pic_id to differ between two consecutive IDRs.
270 idr_pic_id_ ^= 1;
271 ref_pic_list0_.clear();
274 if (current_pic_->frame_num % i_period_ == 0)
275 current_pic_->type = media::H264SliceHeader::kISlice;
276 else
277 current_pic_->type = media::H264SliceHeader::kPSlice;
279 if (current_pic_->type != media::H264SliceHeader::kBSlice)
280 current_pic_->ref = true;
282 current_pic_->pic_order_cnt = current_pic_->frame_num * 2;
283 current_pic_->top_field_order_cnt = current_pic_->pic_order_cnt;
284 current_pic_->pic_order_cnt_lsb = current_pic_->pic_order_cnt;
286 current_encode_job_->keyframe = current_pic_->idr;
288 DVLOGF(4) << "Starting a new frame, type: " << current_pic_->type
289 << (force_keyframe ? " (forced keyframe)" : "")
290 << " frame_num: " << current_pic_->frame_num
291 << " POC: " << current_pic_->pic_order_cnt;
294 void VaapiVideoEncodeAccelerator::EndFrame() {
295 DCHECK(current_pic_);
296 // Store the picture on the list of reference pictures and keep the list
297 // below maximum size, dropping oldest references.
298 if (current_pic_->ref)
299 ref_pic_list0_.push_front(current_encode_job_->recon_surface);
300 size_t max_num_ref_frames =
301 base::checked_cast<size_t>(current_sps_.max_num_ref_frames);
302 while (ref_pic_list0_.size() > max_num_ref_frames)
303 ref_pic_list0_.pop_back();
305 submitted_encode_jobs_.push(make_linked_ptr(current_encode_job_.release()));
308 static void InitVAPicture(VAPictureH264* va_pic) {
309 memset(va_pic, 0, sizeof(*va_pic));
310 va_pic->picture_id = VA_INVALID_ID;
311 va_pic->flags = VA_PICTURE_H264_INVALID;
314 bool VaapiVideoEncodeAccelerator::SubmitFrameParameters() {
315 DCHECK(current_pic_);
316 VAEncSequenceParameterBufferH264 seq_param;
317 memset(&seq_param, 0, sizeof(seq_param));
319 #define SPS_TO_SP(a) seq_param.a = current_sps_.a;
320 SPS_TO_SP(seq_parameter_set_id);
321 SPS_TO_SP(level_idc);
323 seq_param.intra_period = i_period_;
324 seq_param.intra_idr_period = idr_period_;
325 seq_param.ip_period = ip_period_;
326 seq_param.bits_per_second = bitrate_;
328 SPS_TO_SP(max_num_ref_frames);
329 seq_param.picture_width_in_mbs = mb_width_;
330 seq_param.picture_height_in_mbs = mb_height_;
332 #define SPS_TO_SP_FS(a) seq_param.seq_fields.bits.a = current_sps_.a;
333 SPS_TO_SP_FS(chroma_format_idc);
334 SPS_TO_SP_FS(frame_mbs_only_flag);
335 SPS_TO_SP_FS(log2_max_frame_num_minus4);
336 SPS_TO_SP_FS(pic_order_cnt_type);
337 SPS_TO_SP_FS(log2_max_pic_order_cnt_lsb_minus4);
338 #undef SPS_TO_SP_FS
340 SPS_TO_SP(bit_depth_luma_minus8);
341 SPS_TO_SP(bit_depth_chroma_minus8);
343 SPS_TO_SP(frame_cropping_flag);
344 if (current_sps_.frame_cropping_flag) {
345 SPS_TO_SP(frame_crop_left_offset);
346 SPS_TO_SP(frame_crop_right_offset);
347 SPS_TO_SP(frame_crop_top_offset);
348 SPS_TO_SP(frame_crop_bottom_offset);
351 SPS_TO_SP(vui_parameters_present_flag);
352 #define SPS_TO_SP_VF(a) seq_param.vui_fields.bits.a = current_sps_.a;
353 SPS_TO_SP_VF(timing_info_present_flag);
354 #undef SPS_TO_SP_VF
355 SPS_TO_SP(num_units_in_tick);
356 SPS_TO_SP(time_scale);
357 #undef SPS_TO_SP
359 if (!vaapi_wrapper_->SubmitBuffer(VAEncSequenceParameterBufferType,
360 sizeof(seq_param),
361 &seq_param))
362 return false;
364 VAEncPictureParameterBufferH264 pic_param;
365 memset(&pic_param, 0, sizeof(pic_param));
367 pic_param.CurrPic.picture_id = current_encode_job_->recon_surface->id();
368 pic_param.CurrPic.TopFieldOrderCnt = current_pic_->top_field_order_cnt;
369 pic_param.CurrPic.BottomFieldOrderCnt = current_pic_->bottom_field_order_cnt;
370 pic_param.CurrPic.flags = 0;
372 for (size_t i = 0; i < arraysize(pic_param.ReferenceFrames); ++i)
373 InitVAPicture(&pic_param.ReferenceFrames[i]);
375 DCHECK_LE(ref_pic_list0_.size(), arraysize(pic_param.ReferenceFrames));
376 RefPicList::const_iterator iter = ref_pic_list0_.begin();
377 for (size_t i = 0;
378 i < arraysize(pic_param.ReferenceFrames) && iter != ref_pic_list0_.end();
379 ++iter, ++i) {
380 pic_param.ReferenceFrames[i].picture_id = (*iter)->id();
381 pic_param.ReferenceFrames[i].flags = 0;
384 pic_param.coded_buf = current_encode_job_->coded_buffer;
385 pic_param.pic_parameter_set_id = current_pps_.pic_parameter_set_id;
386 pic_param.seq_parameter_set_id = current_pps_.seq_parameter_set_id;
387 pic_param.frame_num = current_pic_->frame_num;
388 pic_param.pic_init_qp = qp_;
389 pic_param.num_ref_idx_l0_active_minus1 = max_ref_idx_l0_size_ - 1;
390 pic_param.pic_fields.bits.idr_pic_flag = current_pic_->idr;
391 pic_param.pic_fields.bits.reference_pic_flag = current_pic_->ref;
392 #define PPS_TO_PP_PF(a) pic_param.pic_fields.bits.a = current_pps_.a;
393 PPS_TO_PP_PF(entropy_coding_mode_flag);
394 PPS_TO_PP_PF(transform_8x8_mode_flag);
395 PPS_TO_PP_PF(deblocking_filter_control_present_flag);
396 #undef PPS_TO_PP_PF
398 if (!vaapi_wrapper_->SubmitBuffer(VAEncPictureParameterBufferType,
399 sizeof(pic_param),
400 &pic_param))
401 return false;
403 VAEncSliceParameterBufferH264 slice_param;
404 memset(&slice_param, 0, sizeof(slice_param));
406 slice_param.num_macroblocks = mb_width_ * mb_height_;
407 slice_param.macroblock_info = VA_INVALID_ID;
408 slice_param.slice_type = current_pic_->type;
409 slice_param.pic_parameter_set_id = current_pps_.pic_parameter_set_id;
410 slice_param.idr_pic_id = idr_pic_id_;
411 slice_param.pic_order_cnt_lsb = current_pic_->pic_order_cnt_lsb;
412 slice_param.num_ref_idx_active_override_flag = true;
414 for (size_t i = 0; i < arraysize(slice_param.RefPicList0); ++i)
415 InitVAPicture(&slice_param.RefPicList0[i]);
417 for (size_t i = 0; i < arraysize(slice_param.RefPicList1); ++i)
418 InitVAPicture(&slice_param.RefPicList1[i]);
420 DCHECK_LE(ref_pic_list0_.size(), arraysize(slice_param.RefPicList0));
421 iter = ref_pic_list0_.begin();
422 for (size_t i = 0;
423 i < arraysize(slice_param.RefPicList0) && iter != ref_pic_list0_.end();
424 ++iter, ++i) {
425 InitVAPicture(&slice_param.RefPicList0[i]);
426 slice_param.RefPicList0[i].picture_id = (*iter)->id();
427 slice_param.RefPicList0[i].flags = 0;
430 if (!vaapi_wrapper_->SubmitBuffer(VAEncSliceParameterBufferType,
431 sizeof(slice_param),
432 &slice_param))
433 return false;
435 VAEncMiscParameterRateControl rate_control_param;
436 memset(&rate_control_param, 0, sizeof(rate_control_param));
437 rate_control_param.bits_per_second = bitrate_;
438 rate_control_param.target_percentage = 90;
439 rate_control_param.window_size = kCPBWindowSizeMs;
440 rate_control_param.initial_qp = qp_;
441 rate_control_param.rc_flags.bits.disable_frame_skip = true;
443 if (!vaapi_wrapper_->SubmitVAEncMiscParamBuffer(
444 VAEncMiscParameterTypeRateControl,
445 sizeof(rate_control_param),
446 &rate_control_param))
447 return false;
449 VAEncMiscParameterFrameRate framerate_param;
450 memset(&framerate_param, 0, sizeof(framerate_param));
451 framerate_param.framerate = framerate_;
452 if (!vaapi_wrapper_->SubmitVAEncMiscParamBuffer(
453 VAEncMiscParameterTypeFrameRate,
454 sizeof(framerate_param),
455 &framerate_param))
456 return false;
458 VAEncMiscParameterHRD hrd_param;
459 memset(&hrd_param, 0, sizeof(hrd_param));
460 hrd_param.buffer_size = cpb_size_;
461 hrd_param.initial_buffer_fullness = cpb_size_ / 2;
462 if (!vaapi_wrapper_->SubmitVAEncMiscParamBuffer(VAEncMiscParameterTypeHRD,
463 sizeof(hrd_param),
464 &hrd_param))
465 return false;
467 return true;
470 bool VaapiVideoEncodeAccelerator::SubmitHeadersIfNeeded() {
471 DCHECK(current_pic_);
472 if (current_pic_->type != media::H264SliceHeader::kISlice)
473 return true;
475 // Submit PPS.
476 VAEncPackedHeaderParameterBuffer par_buffer;
477 memset(&par_buffer, 0, sizeof(par_buffer));
478 par_buffer.type = VAEncPackedHeaderSequence;
479 par_buffer.bit_length = packed_sps_.BytesInBuffer() * 8;
481 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderParameterBufferType,
482 sizeof(par_buffer),
483 &par_buffer))
484 return false;
486 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderDataBufferType,
487 packed_sps_.BytesInBuffer(),
488 packed_sps_.data()))
489 return false;
491 // Submit PPS.
492 memset(&par_buffer, 0, sizeof(par_buffer));
493 par_buffer.type = VAEncPackedHeaderPicture;
494 par_buffer.bit_length = packed_pps_.BytesInBuffer() * 8;
496 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderParameterBufferType,
497 sizeof(par_buffer),
498 &par_buffer))
499 return false;
501 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderDataBufferType,
502 packed_pps_.BytesInBuffer(),
503 packed_pps_.data()))
504 return false;
506 return true;
509 bool VaapiVideoEncodeAccelerator::ExecuteEncode() {
510 DCHECK(current_pic_);
511 DVLOGF(3) << "Encoding frame_num: " << current_pic_->frame_num;
512 return vaapi_wrapper_->ExecuteAndDestroyPendingBuffers(
513 current_encode_job_->input_surface->id());
516 bool VaapiVideoEncodeAccelerator::UploadFrame(
517 const scoped_refptr<media::VideoFrame>& frame) {
518 return vaapi_wrapper_->UploadVideoFrameToSurface(
519 frame, current_encode_job_->input_surface->id());
522 void VaapiVideoEncodeAccelerator::TryToReturnBitstreamBuffer() {
523 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
525 if (state_ != kEncoding)
526 return;
528 if (submitted_encode_jobs_.empty() || available_bitstream_buffers_.empty())
529 return;
531 linked_ptr<BitstreamBufferRef> buffer = available_bitstream_buffers_.front();
532 available_bitstream_buffers_.pop();
534 uint8* target_data = reinterpret_cast<uint8*>(buffer->shm->memory());
536 linked_ptr<EncodeJob> encode_job = submitted_encode_jobs_.front();
537 submitted_encode_jobs_.pop();
539 size_t data_size = 0;
540 if (!vaapi_wrapper_->DownloadAndDestroyCodedBuffer(
541 encode_job->coded_buffer,
542 encode_job->input_surface->id(),
543 target_data,
544 buffer->size,
545 &data_size)) {
546 NOTIFY_ERROR(kPlatformFailureError, "Failed downloading coded buffer");
547 return;
550 DVLOGF(3) << "Returning bitstream buffer "
551 << (encode_job->keyframe ? "(keyframe)" : "")
552 << " id: " << buffer->id << " size: " << data_size;
554 child_task_runner_->PostTask(
555 FROM_HERE, base::Bind(&Client::BitstreamBufferReady, client_, buffer->id,
556 data_size, encode_job->keyframe));
559 void VaapiVideoEncodeAccelerator::Encode(
560 const scoped_refptr<media::VideoFrame>& frame,
561 bool force_keyframe) {
562 DVLOGF(3) << "Frame timestamp: " << frame->timestamp().InMilliseconds()
563 << " force_keyframe: " << force_keyframe;
564 DCHECK(child_task_runner_->BelongsToCurrentThread());
566 encoder_thread_task_runner_->PostTask(
567 FROM_HERE, base::Bind(&VaapiVideoEncodeAccelerator::EncodeTask,
568 base::Unretained(this), frame, force_keyframe));
571 bool VaapiVideoEncodeAccelerator::PrepareNextJob() {
572 if (available_va_surface_ids_.size() < kMinSurfacesToEncode)
573 return false;
575 DCHECK(!current_encode_job_);
576 current_encode_job_.reset(new EncodeJob());
578 if (!vaapi_wrapper_->CreateCodedBuffer(output_buffer_byte_size_,
579 &current_encode_job_->coded_buffer)) {
580 NOTIFY_ERROR(kPlatformFailureError, "Failed creating coded buffer");
581 return false;
584 current_encode_job_->input_surface = new VASurface(
585 available_va_surface_ids_.back(), coded_size_, va_surface_release_cb_);
586 available_va_surface_ids_.pop_back();
588 current_encode_job_->recon_surface = new VASurface(
589 available_va_surface_ids_.back(), coded_size_, va_surface_release_cb_);
590 available_va_surface_ids_.pop_back();
592 // Reference surfaces are needed until the job is done, but they get
593 // removed from ref_pic_list0_ when it's full at the end of job submission.
594 // Keep refs to them along with the job and only release after sync.
595 current_encode_job_->reference_surfaces = ref_pic_list0_;
597 return true;
600 void VaapiVideoEncodeAccelerator::EncodeTask(
601 const scoped_refptr<media::VideoFrame>& frame,
602 bool force_keyframe) {
603 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
604 DCHECK_NE(state_, kUninitialized);
606 encoder_input_queue_.push(
607 make_linked_ptr(new InputFrameRef(frame, force_keyframe)));
608 EncodeFrameTask();
611 void VaapiVideoEncodeAccelerator::EncodeFrameTask() {
612 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
614 if (state_ != kEncoding || encoder_input_queue_.empty())
615 return;
617 if (!PrepareNextJob()) {
618 DVLOGF(4) << "Not ready for next frame yet";
619 return;
622 linked_ptr<InputFrameRef> frame_ref = encoder_input_queue_.front();
623 encoder_input_queue_.pop();
625 if (!UploadFrame(frame_ref->frame)) {
626 NOTIFY_ERROR(kPlatformFailureError, "Failed uploading source frame to HW.");
627 return;
630 BeginFrame(frame_ref->force_keyframe || encoding_parameters_changed_);
631 encoding_parameters_changed_ = false;
633 if (!SubmitFrameParameters()) {
634 NOTIFY_ERROR(kPlatformFailureError, "Failed submitting frame parameters.");
635 return;
638 if (!SubmitHeadersIfNeeded()) {
639 NOTIFY_ERROR(kPlatformFailureError, "Failed submitting frame headers.");
640 return;
643 if (!ExecuteEncode()) {
644 NOTIFY_ERROR(kPlatformFailureError, "Failed submitting encode job to HW.");
645 return;
648 EndFrame();
649 TryToReturnBitstreamBuffer();
652 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer(
653 const media::BitstreamBuffer& buffer) {
654 DVLOGF(4) << "id: " << buffer.id();
655 DCHECK(child_task_runner_->BelongsToCurrentThread());
657 if (buffer.size() < output_buffer_byte_size_) {
658 NOTIFY_ERROR(kInvalidArgumentError, "Provided bitstream buffer too small");
659 return;
662 scoped_ptr<base::SharedMemory> shm(
663 new base::SharedMemory(buffer.handle(), false));
664 if (!shm->Map(buffer.size())) {
665 NOTIFY_ERROR(kPlatformFailureError, "Failed mapping shared memory.");
666 return;
669 scoped_ptr<BitstreamBufferRef> buffer_ref(
670 new BitstreamBufferRef(buffer.id(), shm.Pass(), buffer.size()));
672 encoder_thread_task_runner_->PostTask(
673 FROM_HERE,
674 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask,
675 base::Unretained(this), base::Passed(&buffer_ref)));
678 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask(
679 scoped_ptr<BitstreamBufferRef> buffer_ref) {
680 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
681 DCHECK_NE(state_, kUninitialized);
683 available_bitstream_buffers_.push(make_linked_ptr(buffer_ref.release()));
684 TryToReturnBitstreamBuffer();
687 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
688 uint32 bitrate,
689 uint32 framerate) {
690 DVLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
691 DCHECK(child_task_runner_->BelongsToCurrentThread());
693 encoder_thread_task_runner_->PostTask(
694 FROM_HERE,
695 base::Bind(
696 &VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask,
697 base::Unretained(this), bitrate, framerate));
700 void VaapiVideoEncodeAccelerator::UpdateRates(uint32 bitrate,
701 uint32 framerate) {
702 if (encoder_thread_.IsRunning())
703 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
704 DCHECK_NE(bitrate, 0u);
705 DCHECK_NE(framerate, 0u);
706 bitrate_ = bitrate;
707 framerate_ = framerate;
708 cpb_size_ = bitrate_ * kCPBWindowSizeMs / 1000;
711 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
712 uint32 bitrate,
713 uint32 framerate) {
714 DVLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
715 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
716 DCHECK_NE(state_, kUninitialized);
718 // This is a workaround to zero being temporarily, as part of the initial
719 // setup, provided by the webrtc video encode and a zero bitrate and
720 // framerate not being accepted by VAAPI
721 // TODO: This code is common with v4l2_video_encode_accelerator.cc, perhaps
722 // it could be pulled up to RTCVideoEncoder
723 if (bitrate < 1)
724 bitrate = 1;
725 if (framerate < 1)
726 framerate = 1;
728 if (bitrate_ == bitrate && framerate_ == framerate)
729 return;
731 UpdateRates(bitrate, framerate);
733 UpdateSPS();
734 GeneratePackedSPS();
736 // Submit new parameters along with next frame that will be processed.
737 encoding_parameters_changed_ = true;
740 void VaapiVideoEncodeAccelerator::Destroy() {
741 DCHECK(child_task_runner_->BelongsToCurrentThread());
743 // Can't call client anymore after Destroy() returns.
744 client_ptr_factory_.reset();
745 weak_this_ptr_factory_.InvalidateWeakPtrs();
747 // Early-exit encoder tasks if they are running and join the thread.
748 if (encoder_thread_.IsRunning()) {
749 encoder_thread_.message_loop()->PostTask(
750 FROM_HERE,
751 base::Bind(&VaapiVideoEncodeAccelerator::DestroyTask,
752 base::Unretained(this)));
753 encoder_thread_.Stop();
756 delete this;
759 void VaapiVideoEncodeAccelerator::DestroyTask() {
760 DVLOGF(2);
761 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
762 SetState(kError);
765 void VaapiVideoEncodeAccelerator::UpdateSPS() {
766 memset(&current_sps_, 0, sizeof(media::H264SPS));
768 // Spec A.2 and A.3.
769 switch (profile_) {
770 case media::H264PROFILE_BASELINE:
771 // Due to crbug.com/345569, we don't distinguish between constrained
772 // and non-constrained baseline profiles. Since many codecs can't do
773 // non-constrained, and constrained is usually what we mean (and it's a
774 // subset of non-constrained), default to it.
775 current_sps_.profile_idc = media::H264SPS::kProfileIDCBaseline;
776 current_sps_.constraint_set0_flag = true;
777 break;
778 case media::H264PROFILE_MAIN:
779 current_sps_.profile_idc = media::H264SPS::kProfileIDCMain;
780 current_sps_.constraint_set1_flag = true;
781 break;
782 case media::H264PROFILE_HIGH:
783 current_sps_.profile_idc = media::H264SPS::kProfileIDCHigh;
784 break;
785 default:
786 NOTIMPLEMENTED();
787 return;
790 current_sps_.level_idc = kDefaultLevelIDC;
791 current_sps_.seq_parameter_set_id = 0;
792 current_sps_.chroma_format_idc = kChromaFormatIDC;
794 DCHECK_GE(idr_period_, 1u << 4);
795 current_sps_.log2_max_frame_num_minus4 = Log2OfPowerOf2(idr_period_) - 4;
796 current_sps_.pic_order_cnt_type = 0;
797 current_sps_.log2_max_pic_order_cnt_lsb_minus4 =
798 Log2OfPowerOf2(idr_period_ * 2) - 4;
799 current_sps_.max_num_ref_frames = max_ref_idx_l0_size_;
801 current_sps_.frame_mbs_only_flag = true;
803 DCHECK_GT(mb_width_, 0u);
804 DCHECK_GT(mb_height_, 0u);
805 current_sps_.pic_width_in_mbs_minus1 = mb_width_ - 1;
806 DCHECK(current_sps_.frame_mbs_only_flag);
807 current_sps_.pic_height_in_map_units_minus1 = mb_height_ - 1;
809 if (visible_size_ != coded_size_) {
810 // Visible size differs from coded size, fill crop information.
811 current_sps_.frame_cropping_flag = true;
812 DCHECK(!current_sps_.separate_colour_plane_flag);
813 // Spec table 6-1. Only 4:2:0 for now.
814 DCHECK_EQ(current_sps_.chroma_format_idc, 1);
815 // Spec 7.4.2.1.1. Crop is in crop units, which is 2 pixels for 4:2:0.
816 const unsigned int crop_unit_x = 2;
817 const unsigned int crop_unit_y = 2 * (2 - current_sps_.frame_mbs_only_flag);
818 current_sps_.frame_crop_left_offset = 0;
819 current_sps_.frame_crop_right_offset =
820 (coded_size_.width() - visible_size_.width()) / crop_unit_x;
821 current_sps_.frame_crop_top_offset = 0;
822 current_sps_.frame_crop_bottom_offset =
823 (coded_size_.height() - visible_size_.height()) / crop_unit_y;
826 current_sps_.vui_parameters_present_flag = true;
827 current_sps_.timing_info_present_flag = true;
828 current_sps_.num_units_in_tick = 1;
829 current_sps_.time_scale = framerate_ * 2; // See equation D-2 in spec.
830 current_sps_.fixed_frame_rate_flag = true;
832 current_sps_.nal_hrd_parameters_present_flag = true;
833 // H.264 spec ch. E.2.2.
834 current_sps_.cpb_cnt_minus1 = 0;
835 current_sps_.bit_rate_scale = kBitRateScale;
836 current_sps_.cpb_size_scale = kCPBSizeScale;
837 current_sps_.bit_rate_value_minus1[0] =
838 (bitrate_ >>
839 (kBitRateScale + media::H264SPS::kBitRateScaleConstantTerm)) - 1;
840 current_sps_.cpb_size_value_minus1[0] =
841 (cpb_size_ >>
842 (kCPBSizeScale + media::H264SPS::kCPBSizeScaleConstantTerm)) - 1;
843 current_sps_.cbr_flag[0] = true;
844 current_sps_.initial_cpb_removal_delay_length_minus_1 =
845 media::H264SPS::kDefaultInitialCPBRemovalDelayLength - 1;
846 current_sps_.cpb_removal_delay_length_minus1 =
847 media::H264SPS::kDefaultInitialCPBRemovalDelayLength - 1;
848 current_sps_.dpb_output_delay_length_minus1 =
849 media::H264SPS::kDefaultDPBOutputDelayLength - 1;
850 current_sps_.time_offset_length = media::H264SPS::kDefaultTimeOffsetLength;
851 current_sps_.low_delay_hrd_flag = false;
854 void VaapiVideoEncodeAccelerator::GeneratePackedSPS() {
855 packed_sps_.Reset();
857 packed_sps_.BeginNALU(media::H264NALU::kSPS, 3);
859 packed_sps_.AppendBits(8, current_sps_.profile_idc);
860 packed_sps_.AppendBool(current_sps_.constraint_set0_flag);
861 packed_sps_.AppendBool(current_sps_.constraint_set1_flag);
862 packed_sps_.AppendBool(current_sps_.constraint_set2_flag);
863 packed_sps_.AppendBool(current_sps_.constraint_set3_flag);
864 packed_sps_.AppendBool(current_sps_.constraint_set4_flag);
865 packed_sps_.AppendBool(current_sps_.constraint_set5_flag);
866 packed_sps_.AppendBits(2, 0); // reserved_zero_2bits
867 packed_sps_.AppendBits(8, current_sps_.level_idc);
868 packed_sps_.AppendUE(current_sps_.seq_parameter_set_id);
870 if (current_sps_.profile_idc == media::H264SPS::kProfileIDCHigh) {
871 packed_sps_.AppendUE(current_sps_.chroma_format_idc);
872 if (current_sps_.chroma_format_idc == 3)
873 packed_sps_.AppendBool(current_sps_.separate_colour_plane_flag);
874 packed_sps_.AppendUE(current_sps_.bit_depth_luma_minus8);
875 packed_sps_.AppendUE(current_sps_.bit_depth_chroma_minus8);
876 packed_sps_.AppendBool(current_sps_.qpprime_y_zero_transform_bypass_flag);
877 packed_sps_.AppendBool(current_sps_.seq_scaling_matrix_present_flag);
878 CHECK(!current_sps_.seq_scaling_matrix_present_flag);
881 packed_sps_.AppendUE(current_sps_.log2_max_frame_num_minus4);
882 packed_sps_.AppendUE(current_sps_.pic_order_cnt_type);
883 if (current_sps_.pic_order_cnt_type == 0)
884 packed_sps_.AppendUE(current_sps_.log2_max_pic_order_cnt_lsb_minus4);
885 else if (current_sps_.pic_order_cnt_type == 1) {
886 CHECK(1);
889 packed_sps_.AppendUE(current_sps_.max_num_ref_frames);
890 packed_sps_.AppendBool(current_sps_.gaps_in_frame_num_value_allowed_flag);
891 packed_sps_.AppendUE(current_sps_.pic_width_in_mbs_minus1);
892 packed_sps_.AppendUE(current_sps_.pic_height_in_map_units_minus1);
894 packed_sps_.AppendBool(current_sps_.frame_mbs_only_flag);
895 if (!current_sps_.frame_mbs_only_flag)
896 packed_sps_.AppendBool(current_sps_.mb_adaptive_frame_field_flag);
898 packed_sps_.AppendBool(current_sps_.direct_8x8_inference_flag);
900 packed_sps_.AppendBool(current_sps_.frame_cropping_flag);
901 if (current_sps_.frame_cropping_flag) {
902 packed_sps_.AppendUE(current_sps_.frame_crop_left_offset);
903 packed_sps_.AppendUE(current_sps_.frame_crop_right_offset);
904 packed_sps_.AppendUE(current_sps_.frame_crop_top_offset);
905 packed_sps_.AppendUE(current_sps_.frame_crop_bottom_offset);
908 packed_sps_.AppendBool(current_sps_.vui_parameters_present_flag);
909 if (current_sps_.vui_parameters_present_flag) {
910 packed_sps_.AppendBool(false); // aspect_ratio_info_present_flag
911 packed_sps_.AppendBool(false); // overscan_info_present_flag
912 packed_sps_.AppendBool(false); // video_signal_type_present_flag
913 packed_sps_.AppendBool(false); // chroma_loc_info_present_flag
915 packed_sps_.AppendBool(current_sps_.timing_info_present_flag);
916 if (current_sps_.timing_info_present_flag) {
917 packed_sps_.AppendBits(32, current_sps_.num_units_in_tick);
918 packed_sps_.AppendBits(32, current_sps_.time_scale);
919 packed_sps_.AppendBool(current_sps_.fixed_frame_rate_flag);
922 packed_sps_.AppendBool(current_sps_.nal_hrd_parameters_present_flag);
923 if (current_sps_.nal_hrd_parameters_present_flag) {
924 packed_sps_.AppendUE(current_sps_.cpb_cnt_minus1);
925 packed_sps_.AppendBits(4, current_sps_.bit_rate_scale);
926 packed_sps_.AppendBits(4, current_sps_.cpb_size_scale);
927 CHECK_LT(base::checked_cast<size_t>(current_sps_.cpb_cnt_minus1),
928 arraysize(current_sps_.bit_rate_value_minus1));
929 for (int i = 0; i <= current_sps_.cpb_cnt_minus1; ++i) {
930 packed_sps_.AppendUE(current_sps_.bit_rate_value_minus1[i]);
931 packed_sps_.AppendUE(current_sps_.cpb_size_value_minus1[i]);
932 packed_sps_.AppendBool(current_sps_.cbr_flag[i]);
934 packed_sps_.AppendBits(
935 5, current_sps_.initial_cpb_removal_delay_length_minus_1);
936 packed_sps_.AppendBits(5, current_sps_.cpb_removal_delay_length_minus1);
937 packed_sps_.AppendBits(5, current_sps_.dpb_output_delay_length_minus1);
938 packed_sps_.AppendBits(5, current_sps_.time_offset_length);
941 packed_sps_.AppendBool(false); // vcl_hrd_parameters_flag
942 if (current_sps_.nal_hrd_parameters_present_flag)
943 packed_sps_.AppendBool(current_sps_.low_delay_hrd_flag);
945 packed_sps_.AppendBool(false); // pic_struct_present_flag
946 packed_sps_.AppendBool(true); // bitstream_restriction_flag
948 packed_sps_.AppendBool(false); // motion_vectors_over_pic_boundaries_flag
949 packed_sps_.AppendUE(2); // max_bytes_per_pic_denom
950 packed_sps_.AppendUE(1); // max_bits_per_mb_denom
951 packed_sps_.AppendUE(16); // log2_max_mv_length_horizontal
952 packed_sps_.AppendUE(16); // log2_max_mv_length_vertical
954 // Explicitly set max_num_reorder_frames to 0 to allow the decoder to
955 // output pictures early.
956 packed_sps_.AppendUE(0); // max_num_reorder_frames
958 // The value of max_dec_frame_buffering shall be greater than or equal to
959 // max_num_ref_frames.
960 const unsigned int max_dec_frame_buffering =
961 current_sps_.max_num_ref_frames;
962 packed_sps_.AppendUE(max_dec_frame_buffering);
965 packed_sps_.FinishNALU();
968 void VaapiVideoEncodeAccelerator::UpdatePPS() {
969 memset(&current_pps_, 0, sizeof(media::H264PPS));
971 current_pps_.seq_parameter_set_id = current_sps_.seq_parameter_set_id;
972 current_pps_.pic_parameter_set_id = 0;
974 current_pps_.entropy_coding_mode_flag =
975 current_sps_.profile_idc >= media::H264SPS::kProfileIDCMain;
977 CHECK_GT(max_ref_idx_l0_size_, 0u);
978 current_pps_.num_ref_idx_l0_default_active_minus1 = max_ref_idx_l0_size_ - 1;
979 current_pps_.num_ref_idx_l1_default_active_minus1 = 0;
980 DCHECK_LE(qp_, 51u);
981 current_pps_.pic_init_qp_minus26 = qp_ - 26;
982 current_pps_.deblocking_filter_control_present_flag = true;
983 current_pps_.transform_8x8_mode_flag =
984 (current_sps_.profile_idc == media::H264SPS::kProfileIDCHigh);
987 void VaapiVideoEncodeAccelerator::GeneratePackedPPS() {
988 packed_pps_.Reset();
990 packed_pps_.BeginNALU(media::H264NALU::kPPS, 3);
992 packed_pps_.AppendUE(current_pps_.pic_parameter_set_id);
993 packed_pps_.AppendUE(current_pps_.seq_parameter_set_id);
994 packed_pps_.AppendBool(current_pps_.entropy_coding_mode_flag);
995 packed_pps_.AppendBool(
996 current_pps_.bottom_field_pic_order_in_frame_present_flag);
997 CHECK_EQ(current_pps_.num_slice_groups_minus1, 0);
998 packed_pps_.AppendUE(current_pps_.num_slice_groups_minus1);
1000 packed_pps_.AppendUE(current_pps_.num_ref_idx_l0_default_active_minus1);
1001 packed_pps_.AppendUE(current_pps_.num_ref_idx_l1_default_active_minus1);
1003 packed_pps_.AppendBool(current_pps_.weighted_pred_flag);
1004 packed_pps_.AppendBits(2, current_pps_.weighted_bipred_idc);
1006 packed_pps_.AppendSE(current_pps_.pic_init_qp_minus26);
1007 packed_pps_.AppendSE(current_pps_.pic_init_qs_minus26);
1008 packed_pps_.AppendSE(current_pps_.chroma_qp_index_offset);
1010 packed_pps_.AppendBool(current_pps_.deblocking_filter_control_present_flag);
1011 packed_pps_.AppendBool(current_pps_.constrained_intra_pred_flag);
1012 packed_pps_.AppendBool(current_pps_.redundant_pic_cnt_present_flag);
1014 packed_pps_.AppendBool(current_pps_.transform_8x8_mode_flag);
1015 packed_pps_.AppendBool(current_pps_.pic_scaling_matrix_present_flag);
1016 DCHECK(!current_pps_.pic_scaling_matrix_present_flag);
1017 packed_pps_.AppendSE(current_pps_.second_chroma_qp_index_offset);
1019 packed_pps_.FinishNALU();
1022 void VaapiVideoEncodeAccelerator::SetState(State state) {
1023 // Only touch state on encoder thread, unless it's not running.
1024 if (encoder_thread_.IsRunning() &&
1025 !encoder_thread_task_runner_->BelongsToCurrentThread()) {
1026 encoder_thread_task_runner_->PostTask(
1027 FROM_HERE, base::Bind(&VaapiVideoEncodeAccelerator::SetState,
1028 base::Unretained(this), state));
1029 return;
1032 DVLOGF(1) << "setting state to: " << state;
1033 state_ = state;
1036 void VaapiVideoEncodeAccelerator::NotifyError(Error error) {
1037 if (!child_task_runner_->BelongsToCurrentThread()) {
1038 child_task_runner_->PostTask(
1039 FROM_HERE, base::Bind(&VaapiVideoEncodeAccelerator::NotifyError,
1040 weak_this_, error));
1041 return;
1044 if (client_) {
1045 client_->NotifyError(error);
1046 client_ptr_factory_.reset();
1050 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob()
1051 : coded_buffer(VA_INVALID_ID), keyframe(false) {
1054 VaapiVideoEncodeAccelerator::EncodeJob::~EncodeJob() {
1057 } // namespace content