| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" | 5 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
| 11 #include "content/common/gpu/media/h264_dpb.h" | 11 #include "content/common/gpu/media/h264_dpb.h" |
| 12 #include "content/common/gpu/media/shared_memory_region.h" |
| 12 #include "media/base/bind_to_current_loop.h" | 13 #include "media/base/bind_to_current_loop.h" |
| 13 #include "third_party/libva/va/va_enc_h264.h" | 14 #include "third_party/libva/va/va_enc_h264.h" |
| 14 | 15 |
| 15 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): " | 16 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): " |
| 16 | 17 |
| 17 #define NOTIFY_ERROR(error, msg) \ | 18 #define NOTIFY_ERROR(error, msg) \ |
| 18 do { \ | 19 do { \ |
| 19 SetState(kError); \ | 20 SetState(kError); \ |
| 20 LOG(ERROR) << msg; \ | 21 LOG(ERROR) << msg; \ |
| 21 LOG(ERROR) << "Calling NotifyError(" << error << ")";\ | 22 LOG(ERROR) << "Calling NotifyError(" << error << ")";\ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 | 90 |
| 90 struct VaapiVideoEncodeAccelerator::InputFrameRef { | 91 struct VaapiVideoEncodeAccelerator::InputFrameRef { |
| 91 InputFrameRef(const scoped_refptr<media::VideoFrame>& frame, | 92 InputFrameRef(const scoped_refptr<media::VideoFrame>& frame, |
| 92 bool force_keyframe) | 93 bool force_keyframe) |
| 93 : frame(frame), force_keyframe(force_keyframe) {} | 94 : frame(frame), force_keyframe(force_keyframe) {} |
| 94 const scoped_refptr<media::VideoFrame> frame; | 95 const scoped_refptr<media::VideoFrame> frame; |
| 95 const bool force_keyframe; | 96 const bool force_keyframe; |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef { | 99 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef { |
| 99 BitstreamBufferRef(int32 id, scoped_ptr<base::SharedMemory> shm, size_t size) | 100 BitstreamBufferRef(int32 id, scoped_ptr<SharedMemoryRegion> shm) |
| 100 : id(id), shm(shm.Pass()), size(size) {} | 101 : id(id), shm(std::move(shm)) {} |
| 101 const int32 id; | 102 const int32 id; |
| 102 const scoped_ptr<base::SharedMemory> shm; | 103 const scoped_ptr<SharedMemoryRegion> shm; |
| 103 const size_t size; | |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 media::VideoEncodeAccelerator::SupportedProfiles | 106 media::VideoEncodeAccelerator::SupportedProfiles |
| 107 VaapiVideoEncodeAccelerator::GetSupportedProfiles() { | 107 VaapiVideoEncodeAccelerator::GetSupportedProfiles() { |
| 108 return VaapiWrapper::GetSupportedEncodeProfiles(); | 108 return VaapiWrapper::GetSupportedEncodeProfiles(); |
| 109 } | 109 } |
| 110 | 110 |
| 111 static unsigned int Log2OfPowerOf2(unsigned int x) { | 111 static unsigned int Log2OfPowerOf2(unsigned int x) { |
| 112 CHECK_GT(x, 0u); | 112 CHECK_GT(x, 0u); |
| 113 DCHECK_EQ(x & (x - 1), 0u); | 113 DCHECK_EQ(x & (x - 1), 0u); |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 linked_ptr<BitstreamBufferRef> buffer = available_bitstream_buffers_.front(); | 533 linked_ptr<BitstreamBufferRef> buffer = available_bitstream_buffers_.front(); |
| 534 available_bitstream_buffers_.pop(); | 534 available_bitstream_buffers_.pop(); |
| 535 | 535 |
| 536 uint8* target_data = reinterpret_cast<uint8*>(buffer->shm->memory()); | 536 uint8* target_data = reinterpret_cast<uint8*>(buffer->shm->memory()); |
| 537 | 537 |
| 538 linked_ptr<EncodeJob> encode_job = submitted_encode_jobs_.front(); | 538 linked_ptr<EncodeJob> encode_job = submitted_encode_jobs_.front(); |
| 539 submitted_encode_jobs_.pop(); | 539 submitted_encode_jobs_.pop(); |
| 540 | 540 |
| 541 size_t data_size = 0; | 541 size_t data_size = 0; |
| 542 if (!vaapi_wrapper_->DownloadAndDestroyCodedBuffer( | 542 if (!vaapi_wrapper_->DownloadAndDestroyCodedBuffer( |
| 543 encode_job->coded_buffer, | 543 encode_job->coded_buffer, encode_job->input_surface->id(), |
| 544 encode_job->input_surface->id(), | 544 target_data, buffer->shm->size(), &data_size)) { |
| 545 target_data, | |
| 546 buffer->size, | |
| 547 &data_size)) { | |
| 548 NOTIFY_ERROR(kPlatformFailureError, "Failed downloading coded buffer"); | 545 NOTIFY_ERROR(kPlatformFailureError, "Failed downloading coded buffer"); |
| 549 return; | 546 return; |
| 550 } | 547 } |
| 551 | 548 |
| 552 DVLOGF(3) << "Returning bitstream buffer " | 549 DVLOGF(3) << "Returning bitstream buffer " |
| 553 << (encode_job->keyframe ? "(keyframe)" : "") | 550 << (encode_job->keyframe ? "(keyframe)" : "") |
| 554 << " id: " << buffer->id << " size: " << data_size; | 551 << " id: " << buffer->id << " size: " << data_size; |
| 555 | 552 |
| 556 child_task_runner_->PostTask( | 553 child_task_runner_->PostTask( |
| 557 FROM_HERE, base::Bind(&Client::BitstreamBufferReady, client_, buffer->id, | 554 FROM_HERE, base::Bind(&Client::BitstreamBufferReady, client_, buffer->id, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer( | 653 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer( |
| 657 const media::BitstreamBuffer& buffer) { | 654 const media::BitstreamBuffer& buffer) { |
| 658 DVLOGF(4) << "id: " << buffer.id(); | 655 DVLOGF(4) << "id: " << buffer.id(); |
| 659 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 656 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
| 660 | 657 |
| 661 if (buffer.size() < output_buffer_byte_size_) { | 658 if (buffer.size() < output_buffer_byte_size_) { |
| 662 NOTIFY_ERROR(kInvalidArgumentError, "Provided bitstream buffer too small"); | 659 NOTIFY_ERROR(kInvalidArgumentError, "Provided bitstream buffer too small"); |
| 663 return; | 660 return; |
| 664 } | 661 } |
| 665 | 662 |
| 666 scoped_ptr<base::SharedMemory> shm( | 663 scoped_ptr<SharedMemoryRegion> shm(new SharedMemoryRegion(buffer, false)); |
| 667 new base::SharedMemory(buffer.handle(), false)); | 664 if (!shm->Map()) { |
| 668 if (!shm->Map(buffer.size())) { | |
| 669 NOTIFY_ERROR(kPlatformFailureError, "Failed mapping shared memory."); | 665 NOTIFY_ERROR(kPlatformFailureError, "Failed mapping shared memory."); |
| 670 return; | 666 return; |
| 671 } | 667 } |
| 672 | 668 |
| 673 scoped_ptr<BitstreamBufferRef> buffer_ref( | 669 scoped_ptr<BitstreamBufferRef> buffer_ref( |
| 674 new BitstreamBufferRef(buffer.id(), shm.Pass(), buffer.size())); | 670 new BitstreamBufferRef(buffer.id(), std::move(shm))); |
| 675 | 671 |
| 676 encoder_thread_task_runner_->PostTask( | 672 encoder_thread_task_runner_->PostTask( |
| 677 FROM_HERE, | 673 FROM_HERE, |
| 678 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask, | 674 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask, |
| 679 base::Unretained(this), base::Passed(&buffer_ref))); | 675 base::Unretained(this), base::Passed(&buffer_ref))); |
| 680 } | 676 } |
| 681 | 677 |
| 682 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask( | 678 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask( |
| 683 scoped_ptr<BitstreamBufferRef> buffer_ref) { | 679 scoped_ptr<BitstreamBufferRef> buffer_ref) { |
| 684 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread()); | 680 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread()); |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1052 } | 1048 } |
| 1053 | 1049 |
| 1054 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob() | 1050 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob() |
| 1055 : coded_buffer(VA_INVALID_ID), keyframe(false) { | 1051 : coded_buffer(VA_INVALID_ID), keyframe(false) { |
| 1056 } | 1052 } |
| 1057 | 1053 |
| 1058 VaapiVideoEncodeAccelerator::EncodeJob::~EncodeJob() { | 1054 VaapiVideoEncodeAccelerator::EncodeJob::~EncodeJob() { |
| 1059 } | 1055 } |
| 1060 | 1056 |
| 1061 } // namespace content | 1057 } // namespace content |
| OLD | NEW |