| 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 <string.h> | 7 #include <string.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 15 #include "content/common/gpu/media/h264_dpb.h" | 15 #include "content/common/gpu/media/h264_dpb.h" |
| 16 #include "content/common/gpu/media/shared_memory_region.h" |
| 16 #include "media/base/bind_to_current_loop.h" | 17 #include "media/base/bind_to_current_loop.h" |
| 17 #include "third_party/libva/va/va_enc_h264.h" | 18 #include "third_party/libva/va/va_enc_h264.h" |
| 18 | 19 |
| 19 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): " | 20 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): " |
| 20 | 21 |
| 21 #define NOTIFY_ERROR(error, msg) \ | 22 #define NOTIFY_ERROR(error, msg) \ |
| 22 do { \ | 23 do { \ |
| 23 SetState(kError); \ | 24 SetState(kError); \ |
| 24 LOG(ERROR) << msg; \ | 25 LOG(ERROR) << msg; \ |
| 25 LOG(ERROR) << "Calling NotifyError(" << error << ")";\ | 26 LOG(ERROR) << "Calling NotifyError(" << error << ")";\ |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 94 |
| 94 struct VaapiVideoEncodeAccelerator::InputFrameRef { | 95 struct VaapiVideoEncodeAccelerator::InputFrameRef { |
| 95 InputFrameRef(const scoped_refptr<media::VideoFrame>& frame, | 96 InputFrameRef(const scoped_refptr<media::VideoFrame>& frame, |
| 96 bool force_keyframe) | 97 bool force_keyframe) |
| 97 : frame(frame), force_keyframe(force_keyframe) {} | 98 : frame(frame), force_keyframe(force_keyframe) {} |
| 98 const scoped_refptr<media::VideoFrame> frame; | 99 const scoped_refptr<media::VideoFrame> frame; |
| 99 const bool force_keyframe; | 100 const bool force_keyframe; |
| 100 }; | 101 }; |
| 101 | 102 |
| 102 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef { | 103 struct VaapiVideoEncodeAccelerator::BitstreamBufferRef { |
| 103 BitstreamBufferRef(int32_t id, | 104 BitstreamBufferRef(int32_t id, scoped_ptr<SharedMemoryRegion> shm) |
| 104 scoped_ptr<base::SharedMemory> shm, | 105 : id(id), shm(std::move(shm)) {} |
| 105 size_t size) | |
| 106 : id(id), shm(std::move(shm)), size(size) {} | |
| 107 const int32_t id; | 106 const int32_t id; |
| 108 const scoped_ptr<base::SharedMemory> shm; | 107 const scoped_ptr<SharedMemoryRegion> shm; |
| 109 const size_t size; | |
| 110 }; | 108 }; |
| 111 | 109 |
| 112 media::VideoEncodeAccelerator::SupportedProfiles | 110 media::VideoEncodeAccelerator::SupportedProfiles |
| 113 VaapiVideoEncodeAccelerator::GetSupportedProfiles() { | 111 VaapiVideoEncodeAccelerator::GetSupportedProfiles() { |
| 114 return VaapiWrapper::GetSupportedEncodeProfiles(); | 112 return VaapiWrapper::GetSupportedEncodeProfiles(); |
| 115 } | 113 } |
| 116 | 114 |
| 117 static unsigned int Log2OfPowerOf2(unsigned int x) { | 115 static unsigned int Log2OfPowerOf2(unsigned int x) { |
| 118 CHECK_GT(x, 0u); | 116 CHECK_GT(x, 0u); |
| 119 DCHECK_EQ(x & (x - 1), 0u); | 117 DCHECK_EQ(x & (x - 1), 0u); |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 linked_ptr<BitstreamBufferRef> buffer = available_bitstream_buffers_.front(); | 537 linked_ptr<BitstreamBufferRef> buffer = available_bitstream_buffers_.front(); |
| 540 available_bitstream_buffers_.pop(); | 538 available_bitstream_buffers_.pop(); |
| 541 | 539 |
| 542 uint8_t* target_data = reinterpret_cast<uint8_t*>(buffer->shm->memory()); | 540 uint8_t* target_data = reinterpret_cast<uint8_t*>(buffer->shm->memory()); |
| 543 | 541 |
| 544 linked_ptr<EncodeJob> encode_job = submitted_encode_jobs_.front(); | 542 linked_ptr<EncodeJob> encode_job = submitted_encode_jobs_.front(); |
| 545 submitted_encode_jobs_.pop(); | 543 submitted_encode_jobs_.pop(); |
| 546 | 544 |
| 547 size_t data_size = 0; | 545 size_t data_size = 0; |
| 548 if (!vaapi_wrapper_->DownloadAndDestroyCodedBuffer( | 546 if (!vaapi_wrapper_->DownloadAndDestroyCodedBuffer( |
| 549 encode_job->coded_buffer, | 547 encode_job->coded_buffer, encode_job->input_surface->id(), |
| 550 encode_job->input_surface->id(), | 548 target_data, buffer->shm->size(), &data_size)) { |
| 551 target_data, | |
| 552 buffer->size, | |
| 553 &data_size)) { | |
| 554 NOTIFY_ERROR(kPlatformFailureError, "Failed downloading coded buffer"); | 549 NOTIFY_ERROR(kPlatformFailureError, "Failed downloading coded buffer"); |
| 555 return; | 550 return; |
| 556 } | 551 } |
| 557 | 552 |
| 558 DVLOGF(3) << "Returning bitstream buffer " | 553 DVLOGF(3) << "Returning bitstream buffer " |
| 559 << (encode_job->keyframe ? "(keyframe)" : "") | 554 << (encode_job->keyframe ? "(keyframe)" : "") |
| 560 << " id: " << buffer->id << " size: " << data_size; | 555 << " id: " << buffer->id << " size: " << data_size; |
| 561 | 556 |
| 562 child_task_runner_->PostTask( | 557 child_task_runner_->PostTask( |
| 563 FROM_HERE, base::Bind(&Client::BitstreamBufferReady, client_, buffer->id, | 558 FROM_HERE, base::Bind(&Client::BitstreamBufferReady, client_, buffer->id, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer( | 657 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer( |
| 663 const media::BitstreamBuffer& buffer) { | 658 const media::BitstreamBuffer& buffer) { |
| 664 DVLOGF(4) << "id: " << buffer.id(); | 659 DVLOGF(4) << "id: " << buffer.id(); |
| 665 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 660 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
| 666 | 661 |
| 667 if (buffer.size() < output_buffer_byte_size_) { | 662 if (buffer.size() < output_buffer_byte_size_) { |
| 668 NOTIFY_ERROR(kInvalidArgumentError, "Provided bitstream buffer too small"); | 663 NOTIFY_ERROR(kInvalidArgumentError, "Provided bitstream buffer too small"); |
| 669 return; | 664 return; |
| 670 } | 665 } |
| 671 | 666 |
| 672 scoped_ptr<base::SharedMemory> shm( | 667 scoped_ptr<SharedMemoryRegion> shm(new SharedMemoryRegion(buffer, false)); |
| 673 new base::SharedMemory(buffer.handle(), false)); | 668 if (!shm->Map()) { |
| 674 if (!shm->Map(buffer.size())) { | |
| 675 NOTIFY_ERROR(kPlatformFailureError, "Failed mapping shared memory."); | 669 NOTIFY_ERROR(kPlatformFailureError, "Failed mapping shared memory."); |
| 676 return; | 670 return; |
| 677 } | 671 } |
| 678 | 672 |
| 679 scoped_ptr<BitstreamBufferRef> buffer_ref( | 673 scoped_ptr<BitstreamBufferRef> buffer_ref( |
| 680 new BitstreamBufferRef(buffer.id(), std::move(shm), buffer.size())); | 674 new BitstreamBufferRef(buffer.id(), std::move(shm))); |
| 681 | 675 |
| 682 encoder_thread_task_runner_->PostTask( | 676 encoder_thread_task_runner_->PostTask( |
| 683 FROM_HERE, | 677 FROM_HERE, |
| 684 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask, | 678 base::Bind(&VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask, |
| 685 base::Unretained(this), base::Passed(&buffer_ref))); | 679 base::Unretained(this), base::Passed(&buffer_ref))); |
| 686 } | 680 } |
| 687 | 681 |
| 688 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask( | 682 void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask( |
| 689 scoped_ptr<BitstreamBufferRef> buffer_ref) { | 683 scoped_ptr<BitstreamBufferRef> buffer_ref) { |
| 690 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread()); | 684 DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread()); |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1058 } | 1052 } |
| 1059 | 1053 |
| 1060 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob() | 1054 VaapiVideoEncodeAccelerator::EncodeJob::EncodeJob() |
| 1061 : coded_buffer(VA_INVALID_ID), keyframe(false) { | 1055 : coded_buffer(VA_INVALID_ID), keyframe(false) { |
| 1062 } | 1056 } |
| 1063 | 1057 |
| 1064 VaapiVideoEncodeAccelerator::EncodeJob::~EncodeJob() { | 1058 VaapiVideoEncodeAccelerator::EncodeJob::~EncodeJob() { |
| 1065 } | 1059 } |
| 1066 | 1060 |
| 1067 } // namespace content | 1061 } // namespace content |
| OLD | NEW |