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

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

Issue 333253002: Add VaapiVideoEncodeAccelerator for HW-accelerated video encode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (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 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/metrics/histogram.h"
12 #include "base/numerics/safe_conversions.h"
13 #include "cc/base/util.h"
14 #include "content/common/gpu/media/h264_dpb.h"
15 #include "content/public/common/content_switches.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "third_party/libva/va/va_enc_h264.h"
18
19 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): "
20
21 #define NOTIFY_ERROR(error, msg) \
22 do { \
23 SetState(kError); \
24 DVLOGF(1) << msg; \
25 DVLOGF(1) << "Calling NotifyError(" << error << ")"; \
26 NotifyError(error); \
27 } while (0)
28
29 namespace content {
30
31 static void ReportToUMA(
32 VaapiVideoEncodeAccelerator::VAVEAEncoderFailure failure) {
33 UMA_HISTOGRAM_ENUMERATION(
34 "Media.VAVEA.EncoderFailure",
35 failure,
36 VaapiVideoEncodeAccelerator::VAVEA_ENCODER_FAILURES_MAX);
37 }
38
39 struct VaapiVideoEncodeAccelerator::InputFrameRef {
40 InputFrameRef(const scoped_refptr<media::VideoFrame>& frame,
41 bool force_keyframe)
42 : frame(frame), force_keyframe(force_keyframe) {}
43 const scoped_refptr<media::VideoFrame> frame;
44 const bool force_keyframe;
45 };
46
47 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef {
48 BitstreamBufferRef(int32 id, scoped_ptr<base::SharedMemory> shm, size_t size)
49 : id(id), shm(shm.Pass()), size(size) {}
50 const int32 id;
51 const scoped_ptr<base::SharedMemory> shm;
52 const size_t size;
53 };
54
55 // static
56 std::vector<media::VideoEncodeAccelerator::SupportedProfile>
57 VaapiVideoEncodeAccelerator::GetSupportedProfiles() {
58 std::vector<SupportedProfile> profiles;
59
60 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
61 if (!cmd_line->HasSwitch(switches::kEnableVaapiAcceleratedVideoEncode))
62 return profiles;
63
64 SupportedProfile profile;
65 profile.profile = media::H264PROFILE_MAIN;
66 profile.max_resolution.SetSize(1920, 1088);
67 profile.max_framerate.numerator = kDefaultFramerate;
68 profile.max_framerate.denominator = 1;
69 profiles.push_back(profile);
70
71 // This is actually only constrained (see crbug.com/345569).
72 profile.profile = media::H264PROFILE_BASELINE;
73 profiles.push_back(profile);
74
75 profile.profile = media::H264PROFILE_HIGH;
76 profiles.push_back(profile);
77
78 return profiles;
79 }
80
81 static unsigned int Log2OfPowerOf2(unsigned int x) {
82 CHECK_GT(x, 0);
83 DCHECK_EQ(x & (x - 1), 0);
84
85 int log = 0;
86 while (x) {
87 x >>= 1;
88 ++log;
89 }
90 return log;
91 }
92
93 VaapiVideoEncodeAccelerator::VaapiVideoEncodeAccelerator(Display* x_display)
94 : profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN),
95 mb_width_(0),
96 mb_height_(0),
97 output_buffer_byte_size_(0),
98 x_display_(x_display),
99 state_(kUninitialized),
100 frame_num_(0),
101 last_idr_frame_num_(0),
102 bitrate_(0),
103 framerate_(0),
104 cpb_size_(0),
105 encoding_parameters_changed_(false),
106 encoder_thread_("VAVEAEncoderThread"),
107 child_message_loop_proxy_(base::MessageLoopProxy::current()),
108 weak_this_ptr_factory_(this) {
109 DVLOGF(4);
110 weak_this_ = weak_this_ptr_factory_.GetWeakPtr();
111
112 max_ref_idx_l0_size_ = kMaxNumReferenceFrames;
113 qp_ = kDefaultQP;
114 idr_period_ = kIDRPeriod;
115 i_period_ = kIPeriod;
116 ip_period_ = kIPPeriod;
117 }
118
119 VaapiVideoEncodeAccelerator::~VaapiVideoEncodeAccelerator() {
120 DVLOGF(4);
121 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread());
122 DCHECK(!encoder_thread_.IsRunning());
123 }
124
125 bool VaapiVideoEncodeAccelerator::Initialize(
126 media::VideoFrame::Format format,
127 const gfx::Size& input_visible_size,
128 media::VideoCodecProfile output_profile,
129 uint32 initial_bitrate,
130 Client* client) {
131 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread());
132 DCHECK(!encoder_thread_.IsRunning());
133 DCHECK_EQ(state_, kUninitialized);
134
135 DVLOGF(1) << "Initializing VAVEA, input_format: "
136 << media::VideoFrame::FormatToString(format)
137 << ", input_visible_size: " << input_visible_size.ToString()
138 << ", output_profile: " << output_profile
139 << ", initial_bitrate: " << initial_bitrate;
140
141 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client));
142 client_ = client_ptr_factory_->GetWeakPtr();
143
144 if (output_profile < media::H264PROFILE_BASELINE ||
145 output_profile > media::H264PROFILE_MAIN) {
146 DVLOGF(1) << "Unsupported output profile: " << output_profile;
147 return false;
148 }
149
150 if (format != media::VideoFrame::I420) {
151 DVLOGF(1) << "Unsupported input format: "
152 << media::VideoFrame::FormatToString(format);
153 return false;
154 }
155
156 profile_ = output_profile;
157 visible_size_ = input_visible_size;
158 // 4:2:0 format has to be 2-aligned.
159 DCHECK_EQ(visible_size_.width() % 2, 0);
160 DCHECK_EQ(visible_size_.height() % 2, 0);
161 mb_width_ = cc::RoundUp(visible_size_.width(), 16) / 16;
162 mb_height_ = cc::RoundUp(visible_size_.height(), 16) / 16;
163 coded_size_ = gfx::Size(mb_width_ * 16, mb_height_ * 16);
164 output_buffer_byte_size_ = coded_size_.GetArea();
165
166 UpdateRates(initial_bitrate, kDefaultFramerate);
167
168 vaapi_wrapper_ = VaapiWrapper::Create(VaapiWrapper::kEncode,
169 output_profile,
170 x_display_,
171 base::Bind(&ReportToUMA, VAAPI_ERROR));
172 if (!vaapi_wrapper_) {
173 DVLOG(1) << "Failed initializing VAAPI";
174 return false;
175 }
176
177 if (!encoder_thread_.Start()) {
178 DVLOGF(1) << "Failed to start encoder thread";
179 return false;
180 }
181 encoder_thread_proxy_ = encoder_thread_.message_loop_proxy();
182
183 // Finish the remaining initialization on the encoder thread.
184 encoder_thread_proxy_->PostTask(
185 FROM_HERE,
186 base::Bind(&VaapiVideoEncodeAccelerator::InitializeTask,
187 base::Unretained(this)));
188
189 return true;
190 }
191
192 void VaapiVideoEncodeAccelerator::InitializeTask() {
193 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
194 DCHECK_EQ(state_, kUninitialized);
195 DVLOGF(4);
196
197 va_surface_release_cb_ = media::BindToCurrentLoop(
198 base::Bind(&VaapiVideoEncodeAccelerator::RecycleVASurfaceID,
199 base::Unretained(this)));
200
201 if (!vaapi_wrapper_->CreateSurfaces(
202 coded_size_, kNumSurfaces, &available_va_surface_ids_)) {
203 NOTIFY_ERROR(kPlatformFailureError, "Failed creating VASurfaces");
204 return;
205 }
206
207 UpdateSPS();
208 GeneratePackedSPS();
209
210 UpdatePPS();
211 GeneratePackedPPS();
212
213 child_message_loop_proxy_->PostTask(
214 FROM_HERE,
215 base::Bind(&Client::RequireBitstreamBuffers,
216 client_,
217 kNumInputBuffers,
218 coded_size_,
219 output_buffer_byte_size_));
220
221 SetState(kEncoding);
222 }
223
224 void VaapiVideoEncodeAccelerator::RecycleVASurfaceID(
225 VASurfaceID va_surface_id) {
226 DVLOGF(4) << "va_surface_id: " << va_surface_id;
227 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
228
229 available_va_surface_ids_.push_back(va_surface_id);
230 EncodeFrameTask();
231 }
232
233 void VaapiVideoEncodeAccelerator::BeginFrame(bool force_keyframe) {
234 memset(&current_pic_, 0, sizeof(current_pic_));
235
236 current_pic_.frame_num = frame_num_++;
237 frame_num_ %= idr_period_;
238
239 if (current_pic_.frame_num % i_period_ == 0 || force_keyframe)
240 current_pic_.type = media::H264SliceHeader::kISlice;
241 else
242 current_pic_.type = media::H264SliceHeader::kPSlice;
243
244 if (current_pic_.frame_num % idr_period_ == 0) {
245 current_pic_.idr = true;
246 last_idr_frame_num_ = current_pic_.frame_num;
247 ref_pic_list0_.clear();
248 }
249
250 if (current_pic_.type != media::H264SliceHeader::kBSlice)
251 current_pic_.ref = true;
252
253 current_pic_.pic_order_cnt = current_pic_.frame_num * 2;
254 current_pic_.top_field_order_cnt = current_pic_.pic_order_cnt;
255 current_pic_.pic_order_cnt_lsb = current_pic_.pic_order_cnt;
256
257 current_encode_job_->keyframe =
258 (current_pic_.type == media::H264SliceHeader::kISlice);
259
260 DVLOG(4) << "Starting a new frame, type: " << current_pic_.type
261 << (force_keyframe ? " (forced keyframe)" : "")
262 << " frame_num: " << current_pic_.frame_num
263 << " POC: " << current_pic_.pic_order_cnt;
264 }
265
266 void VaapiVideoEncodeAccelerator::EndFrame() {
267 // Store the picture on the list of reference pictures and keep the list
268 // below maximum size, dropping oldest references.
269 if (current_pic_.ref)
270 ref_pic_list0_.push_front(current_encode_job_->recon_surface);
271 size_t max_num_ref_frames =
272 base::checked_cast<size_t>(current_sps_.max_num_ref_frames);
273 while (ref_pic_list0_.size() > max_num_ref_frames)
274 ref_pic_list0_.pop_back();
275
276 submitted_encode_jobs_.push(make_linked_ptr(current_encode_job_.release()));
277 }
278
279 static void InitVAPicture(VAPictureH264* va_pic) {
280 memset(va_pic, 0, sizeof(*va_pic));
281 va_pic->picture_id = VA_INVALID_ID;
282 va_pic->flags = VA_PICTURE_H264_INVALID;
283 }
284
285 bool VaapiVideoEncodeAccelerator::SubmitFrameParameters() {
286 VAEncSequenceParameterBufferH264 seq_param;
287 memset(&seq_param, 0, sizeof(seq_param));
288
289 #define SPS_TO_SP(a) seq_param.a = current_sps_.a;
290 SPS_TO_SP(seq_parameter_set_id);
291 SPS_TO_SP(level_idc);
292
293 seq_param.intra_period = i_period_;
294 seq_param.intra_idr_period = idr_period_;
295 seq_param.ip_period = ip_period_;
296 seq_param.bits_per_second = bitrate_;
297
298 SPS_TO_SP(max_num_ref_frames);
299 seq_param.picture_width_in_mbs = mb_width_;
300 seq_param.picture_height_in_mbs = mb_height_;
301
302 #define SPS_TO_SP_FS(a) seq_param.seq_fields.bits.a = current_sps_.a;
303 SPS_TO_SP_FS(chroma_format_idc);
304 SPS_TO_SP_FS(frame_mbs_only_flag);
305 SPS_TO_SP_FS(log2_max_frame_num_minus4);
306 SPS_TO_SP_FS(pic_order_cnt_type);
307 SPS_TO_SP_FS(log2_max_pic_order_cnt_lsb_minus4);
308 #undef SPS_TO_SP_FS
309
310 SPS_TO_SP(bit_depth_luma_minus8);
311 SPS_TO_SP(bit_depth_chroma_minus8);
312
313 SPS_TO_SP(frame_cropping_flag);
314 if (current_sps_.frame_cropping_flag) {
315 SPS_TO_SP(frame_crop_left_offset);
316 SPS_TO_SP(frame_crop_right_offset);
317 SPS_TO_SP(frame_crop_top_offset);
318 SPS_TO_SP(frame_crop_bottom_offset);
319 }
320
321 SPS_TO_SP(vui_parameters_present_flag);
322 #define SPS_TO_SP_VF(a) seq_param.vui_fields.bits.a = current_sps_.a;
323 SPS_TO_SP_VF(timing_info_present_flag);
324 #undef SPS_TO_SP_VF
325 SPS_TO_SP(num_units_in_tick);
326 SPS_TO_SP(time_scale);
327 #undef SPS_TO_SP
328
329 if (!vaapi_wrapper_->SubmitBuffer(VAEncSequenceParameterBufferType,
330 sizeof(seq_param),
331 &seq_param))
332 return false;
333
334 VAEncPictureParameterBufferH264 pic_param;
335 memset(&pic_param, 0, sizeof(pic_param));
336
337 pic_param.CurrPic.picture_id = current_encode_job_->recon_surface->id();
338 pic_param.CurrPic.TopFieldOrderCnt = current_pic_.top_field_order_cnt;
339 pic_param.CurrPic.BottomFieldOrderCnt = current_pic_.bottom_field_order_cnt;
340 pic_param.CurrPic.flags = 0;
341
342 for (size_t i = 0; i < arraysize(pic_param.ReferenceFrames); ++i)
343 InitVAPicture(&pic_param.ReferenceFrames[i]);
344
345 DCHECK_LE(ref_pic_list0_.size(), arraysize(pic_param.ReferenceFrames));
346 RefPicList::const_iterator iter = ref_pic_list0_.begin();
347 for (size_t i = 0;
348 i < arraysize(pic_param.ReferenceFrames) && iter != ref_pic_list0_.end();
349 ++iter, ++i) {
350 pic_param.ReferenceFrames[i].picture_id = (*iter)->id();
351 pic_param.ReferenceFrames[i].flags = 0;
352 }
353
354 pic_param.coded_buf = current_encode_job_->coded_buffer;
355 pic_param.pic_parameter_set_id = current_pps_.pic_parameter_set_id;
356 pic_param.seq_parameter_set_id = current_pps_.seq_parameter_set_id;
357 pic_param.frame_num = current_pic_.frame_num;
358 pic_param.pic_init_qp = qp_;
359 pic_param.num_ref_idx_l0_active_minus1 = max_ref_idx_l0_size_ - 1;
360 pic_param.pic_fields.bits.idr_pic_flag = current_pic_.idr;
361 pic_param.pic_fields.bits.reference_pic_flag = current_pic_.ref;
362 #define PPS_TO_PP_PF(a) pic_param.pic_fields.bits.a = current_pps_.a;
363 PPS_TO_PP_PF(entropy_coding_mode_flag);
364 PPS_TO_PP_PF(transform_8x8_mode_flag);
365 PPS_TO_PP_PF(deblocking_filter_control_present_flag);
366 #undef PPS_TO_PP_PF
367
368 if (!vaapi_wrapper_->SubmitBuffer(VAEncPictureParameterBufferType,
369 sizeof(pic_param),
370 &pic_param))
371 return false;
372
373 VAEncSliceParameterBufferH264 slice_param;
374 memset(&slice_param, 0, sizeof(slice_param));
375
376 slice_param.num_macroblocks = mb_width_ * mb_height_;
377 slice_param.macroblock_info = VA_INVALID_ID;
378 slice_param.slice_type = current_pic_.type;
379 slice_param.pic_parameter_set_id = current_pps_.pic_parameter_set_id;
380 slice_param.idr_pic_id = last_idr_frame_num_;
381 slice_param.pic_order_cnt_lsb = current_pic_.pic_order_cnt_lsb;
382 slice_param.num_ref_idx_active_override_flag = true;
383
384 for (size_t i = 0; i < arraysize(slice_param.RefPicList0); ++i)
385 InitVAPicture(&slice_param.RefPicList0[i]);
386
387 for (size_t i = 0; i < arraysize(slice_param.RefPicList1); ++i)
388 InitVAPicture(&slice_param.RefPicList1[i]);
389
390 DCHECK_LE(ref_pic_list0_.size(), arraysize(slice_param.RefPicList0));
391 iter = ref_pic_list0_.begin();
392 for (size_t i = 0;
393 i < arraysize(slice_param.RefPicList0) && iter != ref_pic_list0_.end();
394 ++iter, ++i) {
395 InitVAPicture(&slice_param.RefPicList0[i]);
396 slice_param.RefPicList0[i].picture_id = (*iter)->id();
397 slice_param.RefPicList0[i].flags = 0;
398 }
399
400 if (!vaapi_wrapper_->SubmitBuffer(VAEncSliceParameterBufferType,
401 sizeof(slice_param),
402 &slice_param))
403 return false;
404
405 VAEncMiscParameterRateControl rate_control_param;
406 memset(&rate_control_param, 0, sizeof(rate_control_param));
407 rate_control_param.bits_per_second = bitrate_;
408 rate_control_param.target_percentage = 90;
409 rate_control_param.window_size = kCPBWindowSizeMs;
410 rate_control_param.initial_qp = qp_;
411 rate_control_param.rc_flags.bits.disable_frame_skip = true;
412
413 if (!vaapi_wrapper_->SubmitVAEncMiscParamBuffer(
414 VAEncMiscParameterTypeRateControl,
415 sizeof(rate_control_param),
416 &rate_control_param))
417 return false;
418
419 VAEncMiscParameterFrameRate framerate_param;
420 memset(&framerate_param, 0, sizeof(framerate_param));
421 framerate_param.framerate = framerate_;
422 if (!vaapi_wrapper_->SubmitVAEncMiscParamBuffer(
423 VAEncMiscParameterTypeFrameRate,
424 sizeof(framerate_param),
425 &framerate_param))
426 return false;
427
428 VAEncMiscParameterHRD hrd_param;
429 memset(&hrd_param, 0, sizeof(hrd_param));
430 hrd_param.buffer_size = cpb_size_;
431 hrd_param.initial_buffer_fullness = cpb_size_ / 2;
432 if (!vaapi_wrapper_->SubmitVAEncMiscParamBuffer(VAEncMiscParameterTypeHRD,
433 sizeof(hrd_param),
434 &hrd_param))
435 return false;
436
437 return true;
438 }
439
440 bool VaapiVideoEncodeAccelerator::SubmitHeadersIfNeeded() {
441 if (current_pic_.type != media::H264SliceHeader::kISlice)
442 return true;
443
444 // Submit PPS.
445 VAEncPackedHeaderParameterBuffer par_buffer;
446 memset(&par_buffer, 0, sizeof(par_buffer));
447 par_buffer.type = VAEncPackedHeaderSequence;
448 par_buffer.bit_length = packed_sps_.BytesInBuffer() * 8;
449
450 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderParameterBufferType,
451 sizeof(par_buffer),
452 &par_buffer))
453 return false;
454
455 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderDataBufferType,
456 packed_sps_.BytesInBuffer(),
457 packed_sps_.data()))
458 return false;
459
460 // Submit PPS.
461 memset(&par_buffer, 0, sizeof(par_buffer));
462 par_buffer.type = VAEncPackedHeaderPicture;
463 par_buffer.bit_length = packed_pps_.BytesInBuffer() * 8;
464
465 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderParameterBufferType,
466 sizeof(par_buffer),
467 &par_buffer))
468 return false;
469
470 if (!vaapi_wrapper_->SubmitBuffer(VAEncPackedHeaderDataBufferType,
471 packed_pps_.BytesInBuffer(),
472 packed_pps_.data()))
473 return false;
474
475 return true;
476 }
477
478 bool VaapiVideoEncodeAccelerator::ExecuteEncode() {
479 DVLOGF(3) << "Encoding frame_num: " << current_pic_.frame_num;
480 return vaapi_wrapper_->ExecuteAndDestroyPendingBuffers(
481 current_encode_job_->input_surface->id());
482 }
483
484 bool VaapiVideoEncodeAccelerator::UploadFrame(
485 const scoped_refptr<media::VideoFrame>& frame) {
486 return vaapi_wrapper_->UploadVideoFrameToSurface(
487 frame, current_encode_job_->input_surface->id());
488 }
489
490 void VaapiVideoEncodeAccelerator::TryToReturnBitstreamBuffer() {
491 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
492
493 if (state_ != kEncoding)
494 return;
495
496 if (submitted_encode_jobs_.empty() || available_bitstream_buffers_.empty())
497 return;
498
499 linked_ptr<BitstreamBufferRef> buffer = available_bitstream_buffers_.front();
500 available_bitstream_buffers_.pop();
501
502 uint8* target_data = reinterpret_cast<uint8*>(buffer->shm->memory());
503
504 linked_ptr<EncodeJob> encode_job = submitted_encode_jobs_.front();
505 submitted_encode_jobs_.pop();
506
507 size_t data_size = 0;
508 if (!vaapi_wrapper_->DownloadAndDestroyCodedBuffer(
509 encode_job->coded_buffer,
510 encode_job->input_surface->id(),
511 target_data,
512 buffer->size,
513 &data_size)) {
514 NOTIFY_ERROR(kPlatformFailureError, "Failed downloading coded buffer");
515 return;
516 }
517
518 DVLOG(3) << "Returning bitstream buffer "
519 << (encode_job->keyframe ? "(keyframe)" : "")
520 << " id: " << buffer->id << " size: " << data_size;
521
522 child_message_loop_proxy_->PostTask(FROM_HERE,
523 base::Bind(&Client::BitstreamBufferReady,
524 client_,
525 buffer->id,
526 data_size,
527 encode_job->keyframe));
528 }
529
530 void VaapiVideoEncodeAccelerator::Encode(
531 const scoped_refptr<media::VideoFrame>& frame,
532 bool force_keyframe) {
533 DVLOGF(3) << "Frame timestamp: " << frame->timestamp().InMilliseconds()
534 << " force_keyframe: " << force_keyframe;
535 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread());
536
537 encoder_thread_proxy_->PostTask(
538 FROM_HERE,
539 base::Bind(&VaapiVideoEncodeAccelerator::EncodeTask,
540 base::Unretained(this),
541 frame,
542 force_keyframe));
543 }
544
545 bool VaapiVideoEncodeAccelerator::PrepareNextJob() {
546 if (available_va_surface_ids_.size() < kMinSurfacesToEncode)
547 return false;
548
549 DCHECK(!current_encode_job_);
550 current_encode_job_.reset(new EncodeJob());
551
552 if (!vaapi_wrapper_->CreateCodedBuffer(output_buffer_byte_size_,
553 &current_encode_job_->coded_buffer)) {
554 NOTIFY_ERROR(kPlatformFailureError, "Failed creating coded buffer");
555 return false;
556 }
557
558 current_encode_job_->input_surface =
559 new VASurface(available_va_surface_ids_.back(), va_surface_release_cb_);
560 available_va_surface_ids_.pop_back();
561
562 current_encode_job_->recon_surface =
563 new VASurface(available_va_surface_ids_.back(), va_surface_release_cb_);
564 available_va_surface_ids_.pop_back();
565
566 // Reference surfaces are needed until the job is done, but they get
567 // removed from ref_pic_list0_ when it's full at the end of job submission.
568 // Keep refs to them along with the job and only release after sync.
569 current_encode_job_->reference_surfaces = ref_pic_list0_;
570
571 return true;
572 }
573
574 void VaapiVideoEncodeAccelerator::EncodeTask(
575 const scoped_refptr<media::VideoFrame>& frame,
576 bool force_keyframe) {
577 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
578 DCHECK_NE(state_, kUninitialized);
579
580 encoder_input_queue_.push(
581 make_linked_ptr(new InputFrameRef(frame, force_keyframe)));
582 EncodeFrameTask();
583 }
584
585 void VaapiVideoEncodeAccelerator::EncodeFrameTask() {
586 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
587
588 if (state_ != kEncoding || encoder_input_queue_.empty())
589 return;
590
591 if (!PrepareNextJob()) {
592 DVLOGF(4) << "Not ready for next frame yet";
593 return;
594 }
595
596 linked_ptr<InputFrameRef> frame_ref = encoder_input_queue_.front();
597 encoder_input_queue_.pop();
598
599 if (!UploadFrame(frame_ref->frame)) {
600 NOTIFY_ERROR(kPlatformFailureError, "Failed uploading source frame to HW.");
601 return;
602 }
603
604 BeginFrame(frame_ref->force_keyframe || encoding_parameters_changed_);
605 encoding_parameters_changed_ = false;
606
607 if (!SubmitFrameParameters()) {
608 NOTIFY_ERROR(kPlatformFailureError, "Failed submitting frame parameters.");
609 return;
610 }
611
612 if (!SubmitHeadersIfNeeded()) {
613 NOTIFY_ERROR(kPlatformFailureError, "Failed submitting frame headers.");
614 return;
615 }
616
617 if (!ExecuteEncode()) {
618 NOTIFY_ERROR(kPlatformFailureError, "Failed submitting encode job to HW.");
619 return;
620 }
621
622 EndFrame();
623 TryToReturnBitstreamBuffer();
624 }
625
626 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer(
627 const media::BitstreamBuffer& buffer) {
628 DVLOGF(4) << "id: " << buffer.id();
629 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread());
630
631 if (buffer.size() < output_buffer_byte_size_) {
632 NOTIFY_ERROR(kInvalidArgumentError, "Provided bitstream buffer too small");
633 return;
634 }
635
636 scoped_ptr<base::SharedMemory> shm(
637 new base::SharedMemory(buffer.handle(), false));
638 if (!shm->Map(buffer.size())) {
639 NOTIFY_ERROR(kPlatformFailureError, "Failed mapping shared memory.");
640 return;
641 }
642
643 scoped_ptr<BitstreamBufferRef> buffer_ref(
644 new BitstreamBufferRef(buffer.id(), shm.Pass(), buffer.size()));
645
646 encoder_thread_proxy_->PostTask(
647 FROM_HERE,
648 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask,
649 base::Unretained(this),
650 base::Passed(&buffer_ref)));
651 }
652
653 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask(
654 scoped_ptr<BitstreamBufferRef> buffer_ref) {
655 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
656 DCHECK_NE(state_, kUninitialized);
657
658 available_bitstream_buffers_.push(make_linked_ptr(buffer_ref.release()));
659 TryToReturnBitstreamBuffer();
660 }
661
662 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
663 uint32 bitrate,
664 uint32 framerate) {
665 DVLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
666 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread());
667
668 encoder_thread_proxy_->PostTask(
669 FROM_HERE,
670 base::Bind(
671 &VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask,
672 base::Unretained(this),
673 bitrate,
674 framerate));
675 }
676
677 void VaapiVideoEncodeAccelerator::UpdateRates(uint32 bitrate,
678 uint32 framerate) {
679 if (encoder_thread_.IsRunning())
680 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
681 DCHECK_NE(bitrate, 0);
682 DCHECK_NE(framerate, 0);
683 bitrate_ = base::checked_cast<unsigned int>(bitrate);
684 framerate_ = base::checked_cast<unsigned int>(framerate);
685 cpb_size_ = bitrate_ * kCPBWindowSizeMs / 1000;
686 }
687
688 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
689 uint32 bitrate,
690 uint32 framerate) {
691 DVLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
692 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
693 DCHECK_NE(state_, kUninitialized);
694
695 UpdateRates(bitrate, framerate);
696
697 UpdateSPS();
698 GeneratePackedSPS();
699
700 // Submit new parameters along with next frame that will be processed.
701 encoding_parameters_changed_ = true;
702 }
703
704 void VaapiVideoEncodeAccelerator::Destroy() {
705 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread());
706
707 // Can't call client anymore after Destroy() returns.
708 client_ptr_factory_.reset();
709 weak_this_ptr_factory_.InvalidateWeakPtrs();
710
711 // Early-exit encoder tasks if they are running and join the thread.
712 if (encoder_thread_.IsRunning()) {
713 encoder_thread_.message_loop()->PostTask(
714 FROM_HERE,
715 base::Bind(&VaapiVideoEncodeAccelerator::DestroyTask,
716 base::Unretained(this)));
717 encoder_thread_.Stop();
718 }
719
720 delete this;
721 }
722
723 void VaapiVideoEncodeAccelerator::DestroyTask() {
724 DVLOGF(2);
725 DCHECK(encoder_thread_proxy_->BelongsToCurrentThread());
726 SetState(kError);
727 }
728
729 void VaapiVideoEncodeAccelerator::UpdateSPS() {
730 memset(&current_sps_, 0, sizeof(media::H264SPS));
731
732 // Spec A.2 and A.3.
733 switch (profile_) {
734 case media::H264PROFILE_BASELINE:
735 // Due to crbug.com/345569, we don't distinguish between constrained
736 // and non-constrained baseline profiles. Since many codecs can't do
737 // non-constrained, and constrained is usually what we mean (and it's a
738 // subset of non-constrained), default to it.
739 current_sps_.profile_idc = media::H264SPS::kProfileIDCBaseline;
740 current_sps_.constraint_set0_flag = true;
741 break;
742 case media::H264PROFILE_MAIN:
743 current_sps_.profile_idc = media::H264SPS::kProfileIDCMain;
744 current_sps_.constraint_set1_flag = true;
745 break;
746 case media::H264PROFILE_HIGH:
747 current_sps_.profile_idc = media::H264SPS::kProfileIDCHigh;
748 break;
749 default:
750 NOTIMPLEMENTED();
751 return;
752 }
753
754 current_sps_.level_idc = kDefaultLevelIDC;
755 current_sps_.seq_parameter_set_id = 0;
756 current_sps_.chroma_format_idc = kChromaFormatIDC;
757
758 DCHECK_GE(idr_period_, 1 << 4);
759 current_sps_.log2_max_frame_num_minus4 = Log2OfPowerOf2(idr_period_) - 4;
760 current_sps_.pic_order_cnt_type = 0;
761 current_sps_.log2_max_pic_order_cnt_lsb_minus4 =
762 Log2OfPowerOf2(idr_period_ * 2) - 4;
763 current_sps_.max_num_ref_frames = max_ref_idx_l0_size_;
764
765 current_sps_.frame_mbs_only_flag = true;
766
767 DCHECK_GT(mb_width_, 0);
768 DCHECK_GT(mb_height_, 0);
769 current_sps_.pic_width_in_mbs_minus1 = mb_width_ - 1;
770 DCHECK(current_sps_.frame_mbs_only_flag);
771 current_sps_.pic_height_in_map_units_minus1 = mb_height_ - 1;
772
773 if (visible_size_ != coded_size_) {
774 // Visible size differs from coded size, fill crop information.
775 current_sps_.frame_cropping_flag = true;
776 DCHECK(!current_sps_.separate_colour_plane_flag);
777 // Spec table 6-1. Only 4:2:0 for now.
778 DCHECK_EQ(current_sps_.chroma_format_idc, 1);
779 // Spec 7.4.2.1.1. Crop is in crop units, which is 2 pixels for 4:2:0.
780 const unsigned int crop_unit_x = 2;
781 const unsigned int crop_unit_y = 2 * (2 - current_sps_.frame_mbs_only_flag);
782 current_sps_.frame_crop_left_offset = 0;
783 current_sps_.frame_crop_right_offset =
784 (coded_size_.width() - visible_size_.width()) / crop_unit_x;
785 current_sps_.frame_crop_top_offset = 0;
786 current_sps_.frame_crop_bottom_offset =
787 (coded_size_.height() - visible_size_.height()) / crop_unit_y;
788 }
789
790 current_sps_.vui_parameters_present_flag = true;
791 current_sps_.timing_info_present_flag = true;
792 current_sps_.num_units_in_tick = 1;
793 current_sps_.time_scale = framerate_ * 2; // See equation D-2 in spec.
794 current_sps_.fixed_frame_rate_flag = true;
795
796 current_sps_.nal_hrd_parameters_present_flag = true;
797 // H.264 spec ch. E.2.2.
798 current_sps_.cpb_cnt_minus1 = 0;
799 current_sps_.bit_rate_scale = kBitRateScale;
800 current_sps_.cpb_size_scale = kCPBSizeScale;
801 current_sps_.bit_rate_value_minus1[0] =
802 (bitrate_ >>
803 (kBitRateScale + media::H264SPS::kBitRateScaleConstantTerm)) - 1;
804 current_sps_.cpb_size_value_minus1[0] =
805 (cpb_size_ >>
806 (kCPBSizeScale + media::H264SPS::kCPBSizeScaleConstantTerm)) - 1;
807 current_sps_.cbr_flag[0] = true;
808 current_sps_.initial_cpb_removal_delay_length_minus_1 =
809 media::H264SPS::kDefaultInitialCPBRemovalDelayLength - 1;
810 current_sps_.cpb_removal_delay_length_minus1 =
811 media::H264SPS::kDefaultInitialCPBRemovalDelayLength - 1;
812 current_sps_.dpb_output_delay_length_minus1 =
813 media::H264SPS::kDefaultDPBOutputDelayLength - 1;
814 current_sps_.time_offset_length = media::H264SPS::kDefaultTimeOffsetLength;
815 current_sps_.low_delay_hrd_flag = false;
816 }
817
818 void VaapiVideoEncodeAccelerator::GeneratePackedSPS() {
819 packed_sps_.Reset();
820
821 packed_sps_.BeginNALU(media::H264NALU::kSPS, 3);
822
823 packed_sps_.AppendBits(8, current_sps_.profile_idc);
824 packed_sps_.AppendBool(current_sps_.constraint_set0_flag);
825 packed_sps_.AppendBool(current_sps_.constraint_set1_flag);
826 packed_sps_.AppendBool(current_sps_.constraint_set2_flag);
827 packed_sps_.AppendBool(current_sps_.constraint_set3_flag);
828 packed_sps_.AppendBool(current_sps_.constraint_set4_flag);
829 packed_sps_.AppendBool(current_sps_.constraint_set5_flag);
830 packed_sps_.AppendBits(2, 0); // reserved_zero_2bits
831 packed_sps_.AppendBits(8, current_sps_.level_idc);
832 packed_sps_.AppendUE(current_sps_.seq_parameter_set_id);
833
834 if (current_sps_.profile_idc == media::H264SPS::kProfileIDCHigh) {
835 packed_sps_.AppendUE(current_sps_.chroma_format_idc);
836 if (current_sps_.chroma_format_idc == 3)
837 packed_sps_.AppendBool(current_sps_.separate_colour_plane_flag);
838 packed_sps_.AppendUE(current_sps_.bit_depth_luma_minus8);
839 packed_sps_.AppendUE(current_sps_.bit_depth_chroma_minus8);
840 packed_sps_.AppendBool(current_sps_.qpprime_y_zero_transform_bypass_flag);
841 packed_sps_.AppendBool(current_sps_.seq_scaling_matrix_present_flag);
842 CHECK(!current_sps_.seq_scaling_matrix_present_flag);
843 }
844
845 packed_sps_.AppendUE(current_sps_.log2_max_frame_num_minus4);
846 packed_sps_.AppendUE(current_sps_.pic_order_cnt_type);
847 if (current_sps_.pic_order_cnt_type == 0)
848 packed_sps_.AppendUE(current_sps_.log2_max_pic_order_cnt_lsb_minus4);
849 else if (current_sps_.pic_order_cnt_type == 1) {
850 CHECK(1);
851 }
852
853 packed_sps_.AppendUE(current_sps_.max_num_ref_frames);
854 packed_sps_.AppendBool(current_sps_.gaps_in_frame_num_value_allowed_flag);
855 packed_sps_.AppendUE(current_sps_.pic_width_in_mbs_minus1);
856 packed_sps_.AppendUE(current_sps_.pic_height_in_map_units_minus1);
857
858 packed_sps_.AppendBool(current_sps_.frame_mbs_only_flag);
859 if (!current_sps_.frame_mbs_only_flag)
860 packed_sps_.AppendBool(current_sps_.mb_adaptive_frame_field_flag);
861
862 packed_sps_.AppendBool(current_sps_.direct_8x8_inference_flag);
863
864 packed_sps_.AppendBool(current_sps_.frame_cropping_flag);
865 if (current_sps_.frame_cropping_flag) {
866 packed_sps_.AppendUE(current_sps_.frame_crop_left_offset);
867 packed_sps_.AppendUE(current_sps_.frame_crop_right_offset);
868 packed_sps_.AppendUE(current_sps_.frame_crop_top_offset);
869 packed_sps_.AppendUE(current_sps_.frame_crop_bottom_offset);
870 }
871
872 packed_sps_.AppendBool(current_sps_.vui_parameters_present_flag);
873 if (current_sps_.vui_parameters_present_flag) {
874 packed_sps_.AppendBool(false); // aspect_ratio_info_present_flag
875 packed_sps_.AppendBool(false); // overscan_info_present_flag
876 packed_sps_.AppendBool(false); // video_signal_type_present_flag
877 packed_sps_.AppendBool(false); // chroma_loc_info_present_flag
878
879 packed_sps_.AppendBool(current_sps_.timing_info_present_flag);
880 if (current_sps_.timing_info_present_flag) {
881 packed_sps_.AppendBits(32, current_sps_.num_units_in_tick);
882 packed_sps_.AppendBits(32, current_sps_.time_scale);
883 packed_sps_.AppendBool(current_sps_.fixed_frame_rate_flag);
884 }
885
886 packed_sps_.AppendBool(current_sps_.nal_hrd_parameters_present_flag);
887 if (current_sps_.nal_hrd_parameters_present_flag) {
888 packed_sps_.AppendUE(current_sps_.cpb_cnt_minus1);
889 packed_sps_.AppendBits(4, current_sps_.bit_rate_scale);
890 packed_sps_.AppendBits(4, current_sps_.cpb_size_scale);
891 CHECK_LT(base::checked_cast<size_t>(current_sps_.cpb_cnt_minus1),
892 arraysize(current_sps_.bit_rate_value_minus1));
893 for (int i = 0; i <= current_sps_.cpb_cnt_minus1; ++i) {
894 packed_sps_.AppendUE(current_sps_.bit_rate_value_minus1[i]);
895 packed_sps_.AppendUE(current_sps_.cpb_size_value_minus1[i]);
896 packed_sps_.AppendBool(current_sps_.cbr_flag[i]);
897 }
898 packed_sps_.AppendBits(
899 5, current_sps_.initial_cpb_removal_delay_length_minus_1);
900 packed_sps_.AppendBits(5, current_sps_.cpb_removal_delay_length_minus1);
901 packed_sps_.AppendBits(5, current_sps_.dpb_output_delay_length_minus1);
902 packed_sps_.AppendBits(5, current_sps_.time_offset_length);
903 }
904
905 packed_sps_.AppendBool(false); // vcl_hrd_parameters_flag
906 if (current_sps_.nal_hrd_parameters_present_flag)
907 packed_sps_.AppendBool(current_sps_.low_delay_hrd_flag);
908
909 packed_sps_.AppendBool(false); // pic_struct_present_flag
910 packed_sps_.AppendBool(false); // bitstream_restriction_flag
911 }
912
913 packed_sps_.FinishNALU();
914 }
915
916 void VaapiVideoEncodeAccelerator::UpdatePPS() {
917 memset(&current_pps_, 0, sizeof(media::H264PPS));
918
919 current_pps_.seq_parameter_set_id = current_sps_.seq_parameter_set_id;
920 current_pps_.pic_parameter_set_id = 0;
921
922 current_pps_.entropy_coding_mode_flag =
923 current_sps_.profile_idc >= media::H264SPS::kProfileIDCMain;
924
925 CHECK_GT(max_ref_idx_l0_size_, 0);
926 current_pps_.num_ref_idx_l0_default_active_minus1 = max_ref_idx_l0_size_ - 1;
927 current_pps_.num_ref_idx_l1_default_active_minus1 = 0;
928 DCHECK_LE(qp_, 51);
929 current_pps_.pic_init_qp_minus26 = qp_ - 26;
930 current_pps_.deblocking_filter_control_present_flag = true;
931 current_pps_.transform_8x8_mode_flag =
932 (current_sps_.profile_idc == media::H264SPS::kProfileIDCHigh);
933 }
934
935 void VaapiVideoEncodeAccelerator::GeneratePackedPPS() {
936 packed_pps_.Reset();
937
938 packed_pps_.BeginNALU(media::H264NALU::kPPS, 3);
939
940 packed_pps_.AppendUE(current_pps_.pic_parameter_set_id);
941 packed_pps_.AppendUE(current_pps_.seq_parameter_set_id);
942 packed_pps_.AppendBool(current_pps_.entropy_coding_mode_flag);
943 packed_pps_.AppendBool(
944 current_pps_.bottom_field_pic_order_in_frame_present_flag);
945 CHECK_EQ(current_pps_.num_slice_groups_minus1, 0);
946 packed_pps_.AppendUE(current_pps_.num_slice_groups_minus1);
947
948 packed_pps_.AppendUE(current_pps_.num_ref_idx_l0_default_active_minus1);
949 packed_pps_.AppendUE(current_pps_.num_ref_idx_l1_default_active_minus1);
950
951 packed_pps_.AppendBool(current_pps_.weighted_pred_flag);
952 packed_pps_.AppendBits(2, current_pps_.weighted_bipred_idc);
953
954 packed_pps_.AppendSE(current_pps_.pic_init_qp_minus26);
955 packed_pps_.AppendSE(current_pps_.pic_init_qs_minus26);
956 packed_pps_.AppendSE(current_pps_.chroma_qp_index_offset);
957
958 packed_pps_.AppendBool(current_pps_.deblocking_filter_control_present_flag);
959 packed_pps_.AppendBool(current_pps_.constrained_intra_pred_flag);
960 packed_pps_.AppendBool(current_pps_.redundant_pic_cnt_present_flag);
961
962 packed_pps_.AppendBool(current_pps_.transform_8x8_mode_flag);
963 packed_pps_.AppendBool(current_pps_.pic_scaling_matrix_present_flag);
964 DCHECK(!current_pps_.pic_scaling_matrix_present_flag);
965 packed_pps_.AppendSE(current_pps_.second_chroma_qp_index_offset);
966
967 packed_pps_.FinishNALU();
968 }
969
970 void VaapiVideoEncodeAccelerator::SetState(State state) {
971 // Only touch state on encoder thread, unless it's not running.
972 if (encoder_thread_.IsRunning() &&
973 !encoder_thread_proxy_->BelongsToCurrentThread()) {
974 encoder_thread_proxy_->PostTask(
975 FROM_HERE,
976 base::Bind(&VaapiVideoEncodeAccelerator::SetState,
977 base::Unretained(this),
978 state));
979 return;
980 }
981
982 DVLOGF(1) << "setting state to: " << state;
983 state_ = state;
984 }
985
986 void VaapiVideoEncodeAccelerator::NotifyError(Error error) {
987 if (!child_message_loop_proxy_->BelongsToCurrentThread()) {
988 child_message_loop_proxy_->PostTask(
989 FROM_HERE,
990 base::Bind(
991 &VaapiVideoEncodeAccelerator::NotifyError, weak_this_, error));
992 return;
993 }
994
995 if (client_) {
996 client_->NotifyError(error);
997 client_ptr_factory_.reset();
998 }
999 }
1000
1001 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob()
1002 : coded_buffer(VA_INVALID_ID), keyframe(false) {
1003 }
1004
1005 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698