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"
8 #include "base/callback.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/metrics/histogram.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "content/common/gpu/media/h264_dpb.h"
13 #include "media/base/bind_to_current_loop.h"
14 #include "third_party/libva/va/va_enc_h264.h"
16 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): "
18 #define NOTIFY_ERROR(error, msg) \
22 LOG(ERROR) << "Calling NotifyError(" << error << ")";\
29 // Need 2 surfaces for each frame: one for input data and one for
30 // reconstructed picture, which is later used for reference.
31 const size_t kMinSurfacesToEncode
= 2;
33 // Subjectively chosen.
34 const size_t kNumInputBuffers
= 4;
35 const size_t kMaxNumReferenceFrames
= 4;
37 // We need up to kMaxNumReferenceFrames surfaces for reference, plus one
38 // for input and one for encode (which will be added to the set of reference
39 // frames for subsequent frames). Actual execution of HW encode is done
40 // in parallel, and we want to process more frames in the meantime.
41 // To have kNumInputBuffers in flight, we need a full set of reference +
42 // encode surfaces (i.e. kMaxNumReferenceFrames + kMinSurfacesToEncode), and
43 // (kNumInputBuffers - 1) of kMinSurfacesToEncode for the remaining frames
45 const size_t kNumSurfaces
= kMaxNumReferenceFrames
+ kMinSurfacesToEncode
+
46 kMinSurfacesToEncode
* (kNumInputBuffers
- 1);
48 // An IDR every 2048 frames, an I frame every 256 and no B frames.
49 // We choose IDR period to equal MaxFrameNum so it must be a power of 2.
50 const int kIDRPeriod
= 2048;
51 const int kIPeriod
= 256;
52 const int kIPPeriod
= 1;
54 const int kDefaultFramerate
= 30;
56 // HRD parameters (ch. E.2.2 in spec).
57 const int kBitRateScale
= 0; // bit_rate_scale for SPS HRD parameters.
58 const int kCPBSizeScale
= 0; // cpb_size_scale for SPS HRD parameters.
60 const int kDefaultQP
= 26;
61 // All Intel codecs can do at least 4.1.
62 const int kDefaultLevelIDC
= 41;
63 const int kChromaFormatIDC
= 1; // 4:2:0
65 // Arbitrarily chosen bitrate window size for rate control, in ms.
66 const int kCPBWindowSizeMs
= 1500;
68 // UMA errors that the VaapiVideoEncodeAccelerator class reports.
69 enum VAVEAEncoderFailure
{
71 VAVEA_ENCODER_FAILURES_MAX
,
76 // Round |value| up to |alignment|, which must be a power of 2.
77 static inline size_t RoundUpToPowerOf2(size_t value
, size_t alignment
) {
78 // Check that |alignment| is a power of 2.
79 DCHECK((alignment
+ (alignment
- 1)) == (alignment
| (alignment
- 1)));
80 return ((value
+ (alignment
- 1)) & ~(alignment
- 1));
83 static void ReportToUMA(VAVEAEncoderFailure failure
) {
84 UMA_HISTOGRAM_ENUMERATION(
85 "Media.VAVEA.EncoderFailure",
87 VAVEA_ENCODER_FAILURES_MAX
);
90 struct VaapiVideoEncodeAccelerator::InputFrameRef
{
91 InputFrameRef(const scoped_refptr
<media::VideoFrame
>& frame
,
93 : frame(frame
), force_keyframe(force_keyframe
) {}
94 const scoped_refptr
<media::VideoFrame
> frame
;
95 const bool force_keyframe
;
98 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef
{
99 BitstreamBufferRef(int32 id
, scoped_ptr
<base::SharedMemory
> shm
, size_t size
)
100 : id(id
), shm(shm
.Pass()), size(size
) {}
102 const scoped_ptr
<base::SharedMemory
> shm
;
106 std::vector
<media::VideoEncodeAccelerator::SupportedProfile
>
107 VaapiVideoEncodeAccelerator::GetSupportedProfiles() {
109 return VaapiWrapper::GetSupportedEncodeProfiles();
112 static unsigned int Log2OfPowerOf2(unsigned int x
) {
114 DCHECK_EQ(x
& (x
- 1), 0u);
124 VaapiVideoEncodeAccelerator::VaapiVideoEncodeAccelerator()
125 : profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN
),
128 output_buffer_byte_size_(0),
129 state_(kUninitialized
),
135 encoding_parameters_changed_(false),
136 encoder_thread_("VAVEAEncoderThread"),
137 child_message_loop_proxy_(base::MessageLoopProxy::current()),
138 weak_this_ptr_factory_(this) {
140 weak_this_
= weak_this_ptr_factory_
.GetWeakPtr();
142 max_ref_idx_l0_size_
= kMaxNumReferenceFrames
;
144 idr_period_
= kIDRPeriod
;
145 i_period_
= kIPeriod
;
146 ip_period_
= kIPPeriod
;
149 VaapiVideoEncodeAccelerator::~VaapiVideoEncodeAccelerator() {
151 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
152 DCHECK(!encoder_thread_
.IsRunning());
155 bool VaapiVideoEncodeAccelerator::Initialize(
156 media::VideoFrame::Format format
,
157 const gfx::Size
& input_visible_size
,
158 media::VideoCodecProfile output_profile
,
159 uint32 initial_bitrate
,
161 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
162 DCHECK(!encoder_thread_
.IsRunning());
163 DCHECK_EQ(state_
, kUninitialized
);
165 DVLOGF(1) << "Initializing VAVEA, input_format: "
166 << media::VideoFrame::FormatToString(format
)
167 << ", input_visible_size: " << input_visible_size
.ToString()
168 << ", output_profile: " << output_profile
169 << ", initial_bitrate: " << initial_bitrate
;
171 client_ptr_factory_
.reset(new base::WeakPtrFactory
<Client
>(client
));
172 client_
= client_ptr_factory_
->GetWeakPtr();
174 if (output_profile
< media::H264PROFILE_BASELINE
||
175 output_profile
> media::H264PROFILE_MAIN
) {
176 DVLOGF(1) << "Unsupported output profile: " << output_profile
;
180 if (format
!= media::VideoFrame::I420
) {
181 DVLOGF(1) << "Unsupported input format: "
182 << media::VideoFrame::FormatToString(format
);
186 profile_
= output_profile
;
187 visible_size_
= input_visible_size
;
188 // 4:2:0 format has to be 2-aligned.
189 DCHECK_EQ(visible_size_
.width() % 2, 0);
190 DCHECK_EQ(visible_size_
.height() % 2, 0);
191 coded_size_
= gfx::Size(RoundUpToPowerOf2(visible_size_
.width(), 16),
192 RoundUpToPowerOf2(visible_size_
.height(), 16));
193 mb_width_
= coded_size_
.width() / 16;
194 mb_height_
= coded_size_
.height() / 16;
195 output_buffer_byte_size_
= coded_size_
.GetArea();
197 UpdateRates(initial_bitrate
, kDefaultFramerate
);
200 VaapiWrapper::CreateForVideoCodec(VaapiWrapper::kEncode
, output_profile
,
201 base::Bind(&ReportToUMA
, VAAPI_ERROR
));
202 if (!vaapi_wrapper_
.get()) {
203 DVLOGF(1) << "Failed initializing VAAPI for profile " << output_profile
;
207 if (!encoder_thread_
.Start()) {
208 LOG(ERROR
) << "Failed to start encoder thread";
211 encoder_thread_proxy_
= encoder_thread_
.message_loop_proxy();
213 // Finish the remaining initialization on the encoder thread.
214 encoder_thread_proxy_
->PostTask(
216 base::Bind(&VaapiVideoEncodeAccelerator::InitializeTask
,
217 base::Unretained(this)));
222 void VaapiVideoEncodeAccelerator::InitializeTask() {
223 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
224 DCHECK_EQ(state_
, kUninitialized
);
227 va_surface_release_cb_
= media::BindToCurrentLoop(
228 base::Bind(&VaapiVideoEncodeAccelerator::RecycleVASurfaceID
,
229 base::Unretained(this)));
231 if (!vaapi_wrapper_
->CreateSurfaces(
232 coded_size_
, kNumSurfaces
, &available_va_surface_ids_
)) {
233 NOTIFY_ERROR(kPlatformFailureError
, "Failed creating VASurfaces");
243 child_message_loop_proxy_
->PostTask(
245 base::Bind(&Client::RequireBitstreamBuffers
,
249 output_buffer_byte_size_
));
254 void VaapiVideoEncodeAccelerator::RecycleVASurfaceID(
255 VASurfaceID va_surface_id
) {
256 DVLOGF(4) << "va_surface_id: " << va_surface_id
;
257 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
259 available_va_surface_ids_
.push_back(va_surface_id
);
263 void VaapiVideoEncodeAccelerator::BeginFrame(bool force_keyframe
) {
264 memset(¤t_pic_
, 0, sizeof(current_pic_
));
266 // If the current picture is an IDR picture, frame_num shall be equal to 0.
270 current_pic_
.frame_num
= frame_num_
++;
271 frame_num_
%= idr_period_
;
273 if (current_pic_
.frame_num
== 0) {
274 current_pic_
.idr
= true;
275 // H264 spec mandates idr_pic_id to differ between two consecutive IDRs.
277 ref_pic_list0_
.clear();
280 if (current_pic_
.frame_num
% i_period_
== 0)
281 current_pic_
.type
= media::H264SliceHeader::kISlice
;
283 current_pic_
.type
= media::H264SliceHeader::kPSlice
;
285 if (current_pic_
.type
!= media::H264SliceHeader::kBSlice
)
286 current_pic_
.ref
= true;
288 current_pic_
.pic_order_cnt
= current_pic_
.frame_num
* 2;
289 current_pic_
.top_field_order_cnt
= current_pic_
.pic_order_cnt
;
290 current_pic_
.pic_order_cnt_lsb
= current_pic_
.pic_order_cnt
;
292 current_encode_job_
->keyframe
= current_pic_
.idr
;
294 DVLOGF(4) << "Starting a new frame, type: " << current_pic_
.type
295 << (force_keyframe
? " (forced keyframe)" : "")
296 << " frame_num: " << current_pic_
.frame_num
297 << " POC: " << current_pic_
.pic_order_cnt
;
300 void VaapiVideoEncodeAccelerator::EndFrame() {
301 // Store the picture on the list of reference pictures and keep the list
302 // below maximum size, dropping oldest references.
303 if (current_pic_
.ref
)
304 ref_pic_list0_
.push_front(current_encode_job_
->recon_surface
);
305 size_t max_num_ref_frames
=
306 base::checked_cast
<size_t>(current_sps_
.max_num_ref_frames
);
307 while (ref_pic_list0_
.size() > max_num_ref_frames
)
308 ref_pic_list0_
.pop_back();
310 submitted_encode_jobs_
.push(make_linked_ptr(current_encode_job_
.release()));
313 static void InitVAPicture(VAPictureH264
* va_pic
) {
314 memset(va_pic
, 0, sizeof(*va_pic
));
315 va_pic
->picture_id
= VA_INVALID_ID
;
316 va_pic
->flags
= VA_PICTURE_H264_INVALID
;
319 bool VaapiVideoEncodeAccelerator::SubmitFrameParameters() {
320 VAEncSequenceParameterBufferH264 seq_param
;
321 memset(&seq_param
, 0, sizeof(seq_param
));
323 #define SPS_TO_SP(a) seq_param.a = current_sps_.a;
324 SPS_TO_SP(seq_parameter_set_id
);
325 SPS_TO_SP(level_idc
);
327 seq_param
.intra_period
= i_period_
;
328 seq_param
.intra_idr_period
= idr_period_
;
329 seq_param
.ip_period
= ip_period_
;
330 seq_param
.bits_per_second
= bitrate_
;
332 SPS_TO_SP(max_num_ref_frames
);
333 seq_param
.picture_width_in_mbs
= mb_width_
;
334 seq_param
.picture_height_in_mbs
= mb_height_
;
336 #define SPS_TO_SP_FS(a) seq_param.seq_fields.bits.a = current_sps_.a;
337 SPS_TO_SP_FS(chroma_format_idc
);
338 SPS_TO_SP_FS(frame_mbs_only_flag
);
339 SPS_TO_SP_FS(log2_max_frame_num_minus4
);
340 SPS_TO_SP_FS(pic_order_cnt_type
);
341 SPS_TO_SP_FS(log2_max_pic_order_cnt_lsb_minus4
);
344 SPS_TO_SP(bit_depth_luma_minus8
);
345 SPS_TO_SP(bit_depth_chroma_minus8
);
347 SPS_TO_SP(frame_cropping_flag
);
348 if (current_sps_
.frame_cropping_flag
) {
349 SPS_TO_SP(frame_crop_left_offset
);
350 SPS_TO_SP(frame_crop_right_offset
);
351 SPS_TO_SP(frame_crop_top_offset
);
352 SPS_TO_SP(frame_crop_bottom_offset
);
355 SPS_TO_SP(vui_parameters_present_flag
);
356 #define SPS_TO_SP_VF(a) seq_param.vui_fields.bits.a = current_sps_.a;
357 SPS_TO_SP_VF(timing_info_present_flag
);
359 SPS_TO_SP(num_units_in_tick
);
360 SPS_TO_SP(time_scale
);
363 if (!vaapi_wrapper_
->SubmitBuffer(VAEncSequenceParameterBufferType
,
368 VAEncPictureParameterBufferH264 pic_param
;
369 memset(&pic_param
, 0, sizeof(pic_param
));
371 pic_param
.CurrPic
.picture_id
= current_encode_job_
->recon_surface
->id();
372 pic_param
.CurrPic
.TopFieldOrderCnt
= current_pic_
.top_field_order_cnt
;
373 pic_param
.CurrPic
.BottomFieldOrderCnt
= current_pic_
.bottom_field_order_cnt
;
374 pic_param
.CurrPic
.flags
= 0;
376 for (size_t i
= 0; i
< arraysize(pic_param
.ReferenceFrames
); ++i
)
377 InitVAPicture(&pic_param
.ReferenceFrames
[i
]);
379 DCHECK_LE(ref_pic_list0_
.size(), arraysize(pic_param
.ReferenceFrames
));
380 RefPicList::const_iterator iter
= ref_pic_list0_
.begin();
382 i
< arraysize(pic_param
.ReferenceFrames
) && iter
!= ref_pic_list0_
.end();
384 pic_param
.ReferenceFrames
[i
].picture_id
= (*iter
)->id();
385 pic_param
.ReferenceFrames
[i
].flags
= 0;
388 pic_param
.coded_buf
= current_encode_job_
->coded_buffer
;
389 pic_param
.pic_parameter_set_id
= current_pps_
.pic_parameter_set_id
;
390 pic_param
.seq_parameter_set_id
= current_pps_
.seq_parameter_set_id
;
391 pic_param
.frame_num
= current_pic_
.frame_num
;
392 pic_param
.pic_init_qp
= qp_
;
393 pic_param
.num_ref_idx_l0_active_minus1
= max_ref_idx_l0_size_
- 1;
394 pic_param
.pic_fields
.bits
.idr_pic_flag
= current_pic_
.idr
;
395 pic_param
.pic_fields
.bits
.reference_pic_flag
= current_pic_
.ref
;
396 #define PPS_TO_PP_PF(a) pic_param.pic_fields.bits.a = current_pps_.a;
397 PPS_TO_PP_PF(entropy_coding_mode_flag
);
398 PPS_TO_PP_PF(transform_8x8_mode_flag
);
399 PPS_TO_PP_PF(deblocking_filter_control_present_flag
);
402 if (!vaapi_wrapper_
->SubmitBuffer(VAEncPictureParameterBufferType
,
407 VAEncSliceParameterBufferH264 slice_param
;
408 memset(&slice_param
, 0, sizeof(slice_param
));
410 slice_param
.num_macroblocks
= mb_width_
* mb_height_
;
411 slice_param
.macroblock_info
= VA_INVALID_ID
;
412 slice_param
.slice_type
= current_pic_
.type
;
413 slice_param
.pic_parameter_set_id
= current_pps_
.pic_parameter_set_id
;
414 slice_param
.idr_pic_id
= idr_pic_id_
;
415 slice_param
.pic_order_cnt_lsb
= current_pic_
.pic_order_cnt_lsb
;
416 slice_param
.num_ref_idx_active_override_flag
= true;
418 for (size_t i
= 0; i
< arraysize(slice_param
.RefPicList0
); ++i
)
419 InitVAPicture(&slice_param
.RefPicList0
[i
]);
421 for (size_t i
= 0; i
< arraysize(slice_param
.RefPicList1
); ++i
)
422 InitVAPicture(&slice_param
.RefPicList1
[i
]);
424 DCHECK_LE(ref_pic_list0_
.size(), arraysize(slice_param
.RefPicList0
));
425 iter
= ref_pic_list0_
.begin();
427 i
< arraysize(slice_param
.RefPicList0
) && iter
!= ref_pic_list0_
.end();
429 InitVAPicture(&slice_param
.RefPicList0
[i
]);
430 slice_param
.RefPicList0
[i
].picture_id
= (*iter
)->id();
431 slice_param
.RefPicList0
[i
].flags
= 0;
434 if (!vaapi_wrapper_
->SubmitBuffer(VAEncSliceParameterBufferType
,
439 VAEncMiscParameterRateControl rate_control_param
;
440 memset(&rate_control_param
, 0, sizeof(rate_control_param
));
441 rate_control_param
.bits_per_second
= bitrate_
;
442 rate_control_param
.target_percentage
= 90;
443 rate_control_param
.window_size
= kCPBWindowSizeMs
;
444 rate_control_param
.initial_qp
= qp_
;
445 rate_control_param
.rc_flags
.bits
.disable_frame_skip
= true;
447 if (!vaapi_wrapper_
->SubmitVAEncMiscParamBuffer(
448 VAEncMiscParameterTypeRateControl
,
449 sizeof(rate_control_param
),
450 &rate_control_param
))
453 VAEncMiscParameterFrameRate framerate_param
;
454 memset(&framerate_param
, 0, sizeof(framerate_param
));
455 framerate_param
.framerate
= framerate_
;
456 if (!vaapi_wrapper_
->SubmitVAEncMiscParamBuffer(
457 VAEncMiscParameterTypeFrameRate
,
458 sizeof(framerate_param
),
462 VAEncMiscParameterHRD hrd_param
;
463 memset(&hrd_param
, 0, sizeof(hrd_param
));
464 hrd_param
.buffer_size
= cpb_size_
;
465 hrd_param
.initial_buffer_fullness
= cpb_size_
/ 2;
466 if (!vaapi_wrapper_
->SubmitVAEncMiscParamBuffer(VAEncMiscParameterTypeHRD
,
474 bool VaapiVideoEncodeAccelerator::SubmitHeadersIfNeeded() {
475 if (current_pic_
.type
!= media::H264SliceHeader::kISlice
)
479 VAEncPackedHeaderParameterBuffer par_buffer
;
480 memset(&par_buffer
, 0, sizeof(par_buffer
));
481 par_buffer
.type
= VAEncPackedHeaderSequence
;
482 par_buffer
.bit_length
= packed_sps_
.BytesInBuffer() * 8;
484 if (!vaapi_wrapper_
->SubmitBuffer(VAEncPackedHeaderParameterBufferType
,
489 if (!vaapi_wrapper_
->SubmitBuffer(VAEncPackedHeaderDataBufferType
,
490 packed_sps_
.BytesInBuffer(),
495 memset(&par_buffer
, 0, sizeof(par_buffer
));
496 par_buffer
.type
= VAEncPackedHeaderPicture
;
497 par_buffer
.bit_length
= packed_pps_
.BytesInBuffer() * 8;
499 if (!vaapi_wrapper_
->SubmitBuffer(VAEncPackedHeaderParameterBufferType
,
504 if (!vaapi_wrapper_
->SubmitBuffer(VAEncPackedHeaderDataBufferType
,
505 packed_pps_
.BytesInBuffer(),
512 bool VaapiVideoEncodeAccelerator::ExecuteEncode() {
513 DVLOGF(3) << "Encoding frame_num: " << current_pic_
.frame_num
;
514 return vaapi_wrapper_
->ExecuteAndDestroyPendingBuffers(
515 current_encode_job_
->input_surface
->id());
518 bool VaapiVideoEncodeAccelerator::UploadFrame(
519 const scoped_refptr
<media::VideoFrame
>& frame
) {
520 return vaapi_wrapper_
->UploadVideoFrameToSurface(
521 frame
, current_encode_job_
->input_surface
->id());
524 void VaapiVideoEncodeAccelerator::TryToReturnBitstreamBuffer() {
525 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
527 if (state_
!= kEncoding
)
530 if (submitted_encode_jobs_
.empty() || available_bitstream_buffers_
.empty())
533 linked_ptr
<BitstreamBufferRef
> buffer
= available_bitstream_buffers_
.front();
534 available_bitstream_buffers_
.pop();
536 uint8
* target_data
= reinterpret_cast<uint8
*>(buffer
->shm
->memory());
538 linked_ptr
<EncodeJob
> encode_job
= submitted_encode_jobs_
.front();
539 submitted_encode_jobs_
.pop();
541 size_t data_size
= 0;
542 if (!vaapi_wrapper_
->DownloadAndDestroyCodedBuffer(
543 encode_job
->coded_buffer
,
544 encode_job
->input_surface
->id(),
548 NOTIFY_ERROR(kPlatformFailureError
, "Failed downloading coded buffer");
552 DVLOGF(3) << "Returning bitstream buffer "
553 << (encode_job
->keyframe
? "(keyframe)" : "")
554 << " id: " << buffer
->id
<< " size: " << data_size
;
556 child_message_loop_proxy_
->PostTask(FROM_HERE
,
557 base::Bind(&Client::BitstreamBufferReady
,
561 encode_job
->keyframe
));
564 void VaapiVideoEncodeAccelerator::Encode(
565 const scoped_refptr
<media::VideoFrame
>& frame
,
566 bool force_keyframe
) {
567 DVLOGF(3) << "Frame timestamp: " << frame
->timestamp().InMilliseconds()
568 << " force_keyframe: " << force_keyframe
;
569 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
571 encoder_thread_proxy_
->PostTask(
573 base::Bind(&VaapiVideoEncodeAccelerator::EncodeTask
,
574 base::Unretained(this),
579 bool VaapiVideoEncodeAccelerator::PrepareNextJob() {
580 if (available_va_surface_ids_
.size() < kMinSurfacesToEncode
)
583 DCHECK(!current_encode_job_
);
584 current_encode_job_
.reset(new EncodeJob());
586 if (!vaapi_wrapper_
->CreateCodedBuffer(output_buffer_byte_size_
,
587 ¤t_encode_job_
->coded_buffer
)) {
588 NOTIFY_ERROR(kPlatformFailureError
, "Failed creating coded buffer");
592 current_encode_job_
->input_surface
= new VASurface(
593 available_va_surface_ids_
.back(), coded_size_
, va_surface_release_cb_
);
594 available_va_surface_ids_
.pop_back();
596 current_encode_job_
->recon_surface
= new VASurface(
597 available_va_surface_ids_
.back(), coded_size_
, va_surface_release_cb_
);
598 available_va_surface_ids_
.pop_back();
600 // Reference surfaces are needed until the job is done, but they get
601 // removed from ref_pic_list0_ when it's full at the end of job submission.
602 // Keep refs to them along with the job and only release after sync.
603 current_encode_job_
->reference_surfaces
= ref_pic_list0_
;
608 void VaapiVideoEncodeAccelerator::EncodeTask(
609 const scoped_refptr
<media::VideoFrame
>& frame
,
610 bool force_keyframe
) {
611 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
612 DCHECK_NE(state_
, kUninitialized
);
614 encoder_input_queue_
.push(
615 make_linked_ptr(new InputFrameRef(frame
, force_keyframe
)));
619 void VaapiVideoEncodeAccelerator::EncodeFrameTask() {
620 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
622 if (state_
!= kEncoding
|| encoder_input_queue_
.empty())
625 if (!PrepareNextJob()) {
626 DVLOGF(4) << "Not ready for next frame yet";
630 linked_ptr
<InputFrameRef
> frame_ref
= encoder_input_queue_
.front();
631 encoder_input_queue_
.pop();
633 if (!UploadFrame(frame_ref
->frame
)) {
634 NOTIFY_ERROR(kPlatformFailureError
, "Failed uploading source frame to HW.");
638 BeginFrame(frame_ref
->force_keyframe
|| encoding_parameters_changed_
);
639 encoding_parameters_changed_
= false;
641 if (!SubmitFrameParameters()) {
642 NOTIFY_ERROR(kPlatformFailureError
, "Failed submitting frame parameters.");
646 if (!SubmitHeadersIfNeeded()) {
647 NOTIFY_ERROR(kPlatformFailureError
, "Failed submitting frame headers.");
651 if (!ExecuteEncode()) {
652 NOTIFY_ERROR(kPlatformFailureError
, "Failed submitting encode job to HW.");
657 TryToReturnBitstreamBuffer();
660 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer(
661 const media::BitstreamBuffer
& buffer
) {
662 DVLOGF(4) << "id: " << buffer
.id();
663 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
665 if (buffer
.size() < output_buffer_byte_size_
) {
666 NOTIFY_ERROR(kInvalidArgumentError
, "Provided bitstream buffer too small");
670 scoped_ptr
<base::SharedMemory
> shm(
671 new base::SharedMemory(buffer
.handle(), false));
672 if (!shm
->Map(buffer
.size())) {
673 NOTIFY_ERROR(kPlatformFailureError
, "Failed mapping shared memory.");
677 scoped_ptr
<BitstreamBufferRef
> buffer_ref(
678 new BitstreamBufferRef(buffer
.id(), shm
.Pass(), buffer
.size()));
680 encoder_thread_proxy_
->PostTask(
682 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask
,
683 base::Unretained(this),
684 base::Passed(&buffer_ref
)));
687 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask(
688 scoped_ptr
<BitstreamBufferRef
> buffer_ref
) {
689 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
690 DCHECK_NE(state_
, kUninitialized
);
692 available_bitstream_buffers_
.push(make_linked_ptr(buffer_ref
.release()));
693 TryToReturnBitstreamBuffer();
696 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
699 DVLOGF(2) << "bitrate: " << bitrate
<< " framerate: " << framerate
;
700 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
702 encoder_thread_proxy_
->PostTask(
705 &VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask
,
706 base::Unretained(this),
711 void VaapiVideoEncodeAccelerator::UpdateRates(uint32 bitrate
,
713 if (encoder_thread_
.IsRunning())
714 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
715 DCHECK_NE(bitrate
, 0u);
716 DCHECK_NE(framerate
, 0u);
718 framerate_
= framerate
;
719 cpb_size_
= bitrate_
* kCPBWindowSizeMs
/ 1000;
722 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
725 DVLOGF(2) << "bitrate: " << bitrate
<< " framerate: " << framerate
;
726 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
727 DCHECK_NE(state_
, kUninitialized
);
729 // This is a workaround to zero being temporarily, as part of the initial
730 // setup, provided by the webrtc video encode and a zero bitrate and
731 // framerate not being accepted by VAAPI
732 // TODO: This code is common with v4l2_video_encode_accelerator.cc, perhaps
733 // it could be pulled up to RTCVideoEncoder
739 if (bitrate_
== bitrate
&& framerate_
== framerate
)
742 UpdateRates(bitrate
, framerate
);
747 // Submit new parameters along with next frame that will be processed.
748 encoding_parameters_changed_
= true;
751 void VaapiVideoEncodeAccelerator::Destroy() {
752 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
754 // Can't call client anymore after Destroy() returns.
755 client_ptr_factory_
.reset();
756 weak_this_ptr_factory_
.InvalidateWeakPtrs();
758 // Early-exit encoder tasks if they are running and join the thread.
759 if (encoder_thread_
.IsRunning()) {
760 encoder_thread_
.message_loop()->PostTask(
762 base::Bind(&VaapiVideoEncodeAccelerator::DestroyTask
,
763 base::Unretained(this)));
764 encoder_thread_
.Stop();
770 void VaapiVideoEncodeAccelerator::DestroyTask() {
772 DCHECK(encoder_thread_proxy_
->BelongsToCurrentThread());
776 void VaapiVideoEncodeAccelerator::UpdateSPS() {
777 memset(¤t_sps_
, 0, sizeof(media::H264SPS
));
781 case media::H264PROFILE_BASELINE
:
782 // Due to crbug.com/345569, we don't distinguish between constrained
783 // and non-constrained baseline profiles. Since many codecs can't do
784 // non-constrained, and constrained is usually what we mean (and it's a
785 // subset of non-constrained), default to it.
786 current_sps_
.profile_idc
= media::H264SPS::kProfileIDCBaseline
;
787 current_sps_
.constraint_set0_flag
= true;
789 case media::H264PROFILE_MAIN
:
790 current_sps_
.profile_idc
= media::H264SPS::kProfileIDCMain
;
791 current_sps_
.constraint_set1_flag
= true;
793 case media::H264PROFILE_HIGH
:
794 current_sps_
.profile_idc
= media::H264SPS::kProfileIDCHigh
;
801 current_sps_
.level_idc
= kDefaultLevelIDC
;
802 current_sps_
.seq_parameter_set_id
= 0;
803 current_sps_
.chroma_format_idc
= kChromaFormatIDC
;
805 DCHECK_GE(idr_period_
, 1u << 4);
806 current_sps_
.log2_max_frame_num_minus4
= Log2OfPowerOf2(idr_period_
) - 4;
807 current_sps_
.pic_order_cnt_type
= 0;
808 current_sps_
.log2_max_pic_order_cnt_lsb_minus4
=
809 Log2OfPowerOf2(idr_period_
* 2) - 4;
810 current_sps_
.max_num_ref_frames
= max_ref_idx_l0_size_
;
812 current_sps_
.frame_mbs_only_flag
= true;
814 DCHECK_GT(mb_width_
, 0u);
815 DCHECK_GT(mb_height_
, 0u);
816 current_sps_
.pic_width_in_mbs_minus1
= mb_width_
- 1;
817 DCHECK(current_sps_
.frame_mbs_only_flag
);
818 current_sps_
.pic_height_in_map_units_minus1
= mb_height_
- 1;
820 if (visible_size_
!= coded_size_
) {
821 // Visible size differs from coded size, fill crop information.
822 current_sps_
.frame_cropping_flag
= true;
823 DCHECK(!current_sps_
.separate_colour_plane_flag
);
824 // Spec table 6-1. Only 4:2:0 for now.
825 DCHECK_EQ(current_sps_
.chroma_format_idc
, 1);
826 // Spec 7.4.2.1.1. Crop is in crop units, which is 2 pixels for 4:2:0.
827 const unsigned int crop_unit_x
= 2;
828 const unsigned int crop_unit_y
= 2 * (2 - current_sps_
.frame_mbs_only_flag
);
829 current_sps_
.frame_crop_left_offset
= 0;
830 current_sps_
.frame_crop_right_offset
=
831 (coded_size_
.width() - visible_size_
.width()) / crop_unit_x
;
832 current_sps_
.frame_crop_top_offset
= 0;
833 current_sps_
.frame_crop_bottom_offset
=
834 (coded_size_
.height() - visible_size_
.height()) / crop_unit_y
;
837 current_sps_
.vui_parameters_present_flag
= true;
838 current_sps_
.timing_info_present_flag
= true;
839 current_sps_
.num_units_in_tick
= 1;
840 current_sps_
.time_scale
= framerate_
* 2; // See equation D-2 in spec.
841 current_sps_
.fixed_frame_rate_flag
= true;
843 current_sps_
.nal_hrd_parameters_present_flag
= true;
844 // H.264 spec ch. E.2.2.
845 current_sps_
.cpb_cnt_minus1
= 0;
846 current_sps_
.bit_rate_scale
= kBitRateScale
;
847 current_sps_
.cpb_size_scale
= kCPBSizeScale
;
848 current_sps_
.bit_rate_value_minus1
[0] =
850 (kBitRateScale
+ media::H264SPS::kBitRateScaleConstantTerm
)) - 1;
851 current_sps_
.cpb_size_value_minus1
[0] =
853 (kCPBSizeScale
+ media::H264SPS::kCPBSizeScaleConstantTerm
)) - 1;
854 current_sps_
.cbr_flag
[0] = true;
855 current_sps_
.initial_cpb_removal_delay_length_minus_1
=
856 media::H264SPS::kDefaultInitialCPBRemovalDelayLength
- 1;
857 current_sps_
.cpb_removal_delay_length_minus1
=
858 media::H264SPS::kDefaultInitialCPBRemovalDelayLength
- 1;
859 current_sps_
.dpb_output_delay_length_minus1
=
860 media::H264SPS::kDefaultDPBOutputDelayLength
- 1;
861 current_sps_
.time_offset_length
= media::H264SPS::kDefaultTimeOffsetLength
;
862 current_sps_
.low_delay_hrd_flag
= false;
865 void VaapiVideoEncodeAccelerator::GeneratePackedSPS() {
868 packed_sps_
.BeginNALU(media::H264NALU::kSPS
, 3);
870 packed_sps_
.AppendBits(8, current_sps_
.profile_idc
);
871 packed_sps_
.AppendBool(current_sps_
.constraint_set0_flag
);
872 packed_sps_
.AppendBool(current_sps_
.constraint_set1_flag
);
873 packed_sps_
.AppendBool(current_sps_
.constraint_set2_flag
);
874 packed_sps_
.AppendBool(current_sps_
.constraint_set3_flag
);
875 packed_sps_
.AppendBool(current_sps_
.constraint_set4_flag
);
876 packed_sps_
.AppendBool(current_sps_
.constraint_set5_flag
);
877 packed_sps_
.AppendBits(2, 0); // reserved_zero_2bits
878 packed_sps_
.AppendBits(8, current_sps_
.level_idc
);
879 packed_sps_
.AppendUE(current_sps_
.seq_parameter_set_id
);
881 if (current_sps_
.profile_idc
== media::H264SPS::kProfileIDCHigh
) {
882 packed_sps_
.AppendUE(current_sps_
.chroma_format_idc
);
883 if (current_sps_
.chroma_format_idc
== 3)
884 packed_sps_
.AppendBool(current_sps_
.separate_colour_plane_flag
);
885 packed_sps_
.AppendUE(current_sps_
.bit_depth_luma_minus8
);
886 packed_sps_
.AppendUE(current_sps_
.bit_depth_chroma_minus8
);
887 packed_sps_
.AppendBool(current_sps_
.qpprime_y_zero_transform_bypass_flag
);
888 packed_sps_
.AppendBool(current_sps_
.seq_scaling_matrix_present_flag
);
889 CHECK(!current_sps_
.seq_scaling_matrix_present_flag
);
892 packed_sps_
.AppendUE(current_sps_
.log2_max_frame_num_minus4
);
893 packed_sps_
.AppendUE(current_sps_
.pic_order_cnt_type
);
894 if (current_sps_
.pic_order_cnt_type
== 0)
895 packed_sps_
.AppendUE(current_sps_
.log2_max_pic_order_cnt_lsb_minus4
);
896 else if (current_sps_
.pic_order_cnt_type
== 1) {
900 packed_sps_
.AppendUE(current_sps_
.max_num_ref_frames
);
901 packed_sps_
.AppendBool(current_sps_
.gaps_in_frame_num_value_allowed_flag
);
902 packed_sps_
.AppendUE(current_sps_
.pic_width_in_mbs_minus1
);
903 packed_sps_
.AppendUE(current_sps_
.pic_height_in_map_units_minus1
);
905 packed_sps_
.AppendBool(current_sps_
.frame_mbs_only_flag
);
906 if (!current_sps_
.frame_mbs_only_flag
)
907 packed_sps_
.AppendBool(current_sps_
.mb_adaptive_frame_field_flag
);
909 packed_sps_
.AppendBool(current_sps_
.direct_8x8_inference_flag
);
911 packed_sps_
.AppendBool(current_sps_
.frame_cropping_flag
);
912 if (current_sps_
.frame_cropping_flag
) {
913 packed_sps_
.AppendUE(current_sps_
.frame_crop_left_offset
);
914 packed_sps_
.AppendUE(current_sps_
.frame_crop_right_offset
);
915 packed_sps_
.AppendUE(current_sps_
.frame_crop_top_offset
);
916 packed_sps_
.AppendUE(current_sps_
.frame_crop_bottom_offset
);
919 packed_sps_
.AppendBool(current_sps_
.vui_parameters_present_flag
);
920 if (current_sps_
.vui_parameters_present_flag
) {
921 packed_sps_
.AppendBool(false); // aspect_ratio_info_present_flag
922 packed_sps_
.AppendBool(false); // overscan_info_present_flag
923 packed_sps_
.AppendBool(false); // video_signal_type_present_flag
924 packed_sps_
.AppendBool(false); // chroma_loc_info_present_flag
926 packed_sps_
.AppendBool(current_sps_
.timing_info_present_flag
);
927 if (current_sps_
.timing_info_present_flag
) {
928 packed_sps_
.AppendBits(32, current_sps_
.num_units_in_tick
);
929 packed_sps_
.AppendBits(32, current_sps_
.time_scale
);
930 packed_sps_
.AppendBool(current_sps_
.fixed_frame_rate_flag
);
933 packed_sps_
.AppendBool(current_sps_
.nal_hrd_parameters_present_flag
);
934 if (current_sps_
.nal_hrd_parameters_present_flag
) {
935 packed_sps_
.AppendUE(current_sps_
.cpb_cnt_minus1
);
936 packed_sps_
.AppendBits(4, current_sps_
.bit_rate_scale
);
937 packed_sps_
.AppendBits(4, current_sps_
.cpb_size_scale
);
938 CHECK_LT(base::checked_cast
<size_t>(current_sps_
.cpb_cnt_minus1
),
939 arraysize(current_sps_
.bit_rate_value_minus1
));
940 for (int i
= 0; i
<= current_sps_
.cpb_cnt_minus1
; ++i
) {
941 packed_sps_
.AppendUE(current_sps_
.bit_rate_value_minus1
[i
]);
942 packed_sps_
.AppendUE(current_sps_
.cpb_size_value_minus1
[i
]);
943 packed_sps_
.AppendBool(current_sps_
.cbr_flag
[i
]);
945 packed_sps_
.AppendBits(
946 5, current_sps_
.initial_cpb_removal_delay_length_minus_1
);
947 packed_sps_
.AppendBits(5, current_sps_
.cpb_removal_delay_length_minus1
);
948 packed_sps_
.AppendBits(5, current_sps_
.dpb_output_delay_length_minus1
);
949 packed_sps_
.AppendBits(5, current_sps_
.time_offset_length
);
952 packed_sps_
.AppendBool(false); // vcl_hrd_parameters_flag
953 if (current_sps_
.nal_hrd_parameters_present_flag
)
954 packed_sps_
.AppendBool(current_sps_
.low_delay_hrd_flag
);
956 packed_sps_
.AppendBool(false); // pic_struct_present_flag
957 packed_sps_
.AppendBool(true); // bitstream_restriction_flag
959 packed_sps_
.AppendBool(false); // motion_vectors_over_pic_boundaries_flag
960 packed_sps_
.AppendUE(2); // max_bytes_per_pic_denom
961 packed_sps_
.AppendUE(1); // max_bits_per_mb_denom
962 packed_sps_
.AppendUE(16); // log2_max_mv_length_horizontal
963 packed_sps_
.AppendUE(16); // log2_max_mv_length_vertical
965 // Explicitly set max_num_reorder_frames to 0 to allow the decoder to
966 // output pictures early.
967 packed_sps_
.AppendUE(0); // max_num_reorder_frames
969 // The value of max_dec_frame_buffering shall be greater than or equal to
970 // max_num_ref_frames.
971 const unsigned int max_dec_frame_buffering
=
972 current_sps_
.max_num_ref_frames
;
973 packed_sps_
.AppendUE(max_dec_frame_buffering
);
976 packed_sps_
.FinishNALU();
979 void VaapiVideoEncodeAccelerator::UpdatePPS() {
980 memset(¤t_pps_
, 0, sizeof(media::H264PPS
));
982 current_pps_
.seq_parameter_set_id
= current_sps_
.seq_parameter_set_id
;
983 current_pps_
.pic_parameter_set_id
= 0;
985 current_pps_
.entropy_coding_mode_flag
=
986 current_sps_
.profile_idc
>= media::H264SPS::kProfileIDCMain
;
988 CHECK_GT(max_ref_idx_l0_size_
, 0u);
989 current_pps_
.num_ref_idx_l0_default_active_minus1
= max_ref_idx_l0_size_
- 1;
990 current_pps_
.num_ref_idx_l1_default_active_minus1
= 0;
992 current_pps_
.pic_init_qp_minus26
= qp_
- 26;
993 current_pps_
.deblocking_filter_control_present_flag
= true;
994 current_pps_
.transform_8x8_mode_flag
=
995 (current_sps_
.profile_idc
== media::H264SPS::kProfileIDCHigh
);
998 void VaapiVideoEncodeAccelerator::GeneratePackedPPS() {
1001 packed_pps_
.BeginNALU(media::H264NALU::kPPS
, 3);
1003 packed_pps_
.AppendUE(current_pps_
.pic_parameter_set_id
);
1004 packed_pps_
.AppendUE(current_pps_
.seq_parameter_set_id
);
1005 packed_pps_
.AppendBool(current_pps_
.entropy_coding_mode_flag
);
1006 packed_pps_
.AppendBool(
1007 current_pps_
.bottom_field_pic_order_in_frame_present_flag
);
1008 CHECK_EQ(current_pps_
.num_slice_groups_minus1
, 0);
1009 packed_pps_
.AppendUE(current_pps_
.num_slice_groups_minus1
);
1011 packed_pps_
.AppendUE(current_pps_
.num_ref_idx_l0_default_active_minus1
);
1012 packed_pps_
.AppendUE(current_pps_
.num_ref_idx_l1_default_active_minus1
);
1014 packed_pps_
.AppendBool(current_pps_
.weighted_pred_flag
);
1015 packed_pps_
.AppendBits(2, current_pps_
.weighted_bipred_idc
);
1017 packed_pps_
.AppendSE(current_pps_
.pic_init_qp_minus26
);
1018 packed_pps_
.AppendSE(current_pps_
.pic_init_qs_minus26
);
1019 packed_pps_
.AppendSE(current_pps_
.chroma_qp_index_offset
);
1021 packed_pps_
.AppendBool(current_pps_
.deblocking_filter_control_present_flag
);
1022 packed_pps_
.AppendBool(current_pps_
.constrained_intra_pred_flag
);
1023 packed_pps_
.AppendBool(current_pps_
.redundant_pic_cnt_present_flag
);
1025 packed_pps_
.AppendBool(current_pps_
.transform_8x8_mode_flag
);
1026 packed_pps_
.AppendBool(current_pps_
.pic_scaling_matrix_present_flag
);
1027 DCHECK(!current_pps_
.pic_scaling_matrix_present_flag
);
1028 packed_pps_
.AppendSE(current_pps_
.second_chroma_qp_index_offset
);
1030 packed_pps_
.FinishNALU();
1033 void VaapiVideoEncodeAccelerator::SetState(State state
) {
1034 // Only touch state on encoder thread, unless it's not running.
1035 if (encoder_thread_
.IsRunning() &&
1036 !encoder_thread_proxy_
->BelongsToCurrentThread()) {
1037 encoder_thread_proxy_
->PostTask(
1039 base::Bind(&VaapiVideoEncodeAccelerator::SetState
,
1040 base::Unretained(this),
1045 DVLOGF(1) << "setting state to: " << state
;
1049 void VaapiVideoEncodeAccelerator::NotifyError(Error error
) {
1050 if (!child_message_loop_proxy_
->BelongsToCurrentThread()) {
1051 child_message_loop_proxy_
->PostTask(
1054 &VaapiVideoEncodeAccelerator::NotifyError
, weak_this_
, error
));
1059 client_
->NotifyError(error
);
1060 client_ptr_factory_
.reset();
1064 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob()
1065 : coded_buffer(VA_INVALID_ID
), keyframe(false) {
1068 VaapiVideoEncodeAccelerator::EncodeJob::~EncodeJob() {
1071 } // namespace content